14
14
#include " llvm/IR/Function.h"
15
15
#include " llvm/IR/Instruction.h"
16
16
#include " llvm/IR/Module.h"
17
+ #include " llvm/SandboxIR/Utils.h"
17
18
#include " llvm/Support/SourceMgr.h"
18
19
#include " gmock/gmock-matchers.h"
19
20
#include " gtest/gtest.h"
@@ -1373,6 +1374,8 @@ OperandNo: 0
1373
1374
EXPECT_TRUE (I0->hasNUses (1u ));
1374
1375
EXPECT_FALSE (I0->hasNUses (2u ));
1375
1376
1377
+ // Check Value.getExpectedType
1378
+
1376
1379
// Check User.setOperand().
1377
1380
Ret->setOperand (0 , Arg0);
1378
1381
EXPECT_EQ (Ret->getOperand (0 ), Arg0);
@@ -1436,7 +1439,6 @@ define i32 @foo(i32 %arg0, i32 %arg1) {
1436
1439
Replaced = Ret->replaceUsesOfWith (I0, Arg0);
1437
1440
EXPECT_TRUE (Replaced);
1438
1441
EXPECT_EQ (Ret->getOperand (0 ), Arg0);
1439
-
1440
1442
// Check RAUW on constant.
1441
1443
auto *Glob0 = cast<sandboxir::Constant>(I1->getOperand (0 ));
1442
1444
auto *Glob1 = cast<sandboxir::Constant>(I2->getOperand (0 ));
@@ -1445,6 +1447,68 @@ define i32 @foo(i32 %arg0, i32 %arg1) {
1445
1447
EXPECT_EQ (Glob0->getOperand (0 ), Glob1);
1446
1448
}
1447
1449
1450
+ TEST_F (SandboxIRTest, GetExpected) {
1451
+ parseIR (C, R"IR(
1452
+ define float @foo(float %v, ptr %ptr) {
1453
+ %add = fadd float %v, %v
1454
+ store float %v, ptr %ptr
1455
+ ret float %v
1456
+ }
1457
+ define void @bar(float %v, ptr %ptr) {
1458
+ ret void
1459
+ }
1460
+ )IR" );
1461
+ llvm::Function &Foo = *M->getFunction (" foo" );
1462
+ sandboxir::Context Ctx (C);
1463
+
1464
+ Ctx.createFunction (&Foo);
1465
+ auto *FooBB = cast<sandboxir::BasicBlock>(Ctx.getValue (&*Foo.begin ()));
1466
+ auto FooIt = FooBB->begin ();
1467
+ auto Add = cast<sandboxir::Instruction>(&*FooIt++);
1468
+ auto *S0 = cast<sandboxir::Instruction>(&*FooIt++);
1469
+ auto *RetF = cast<sandboxir::Instruction>(&*FooIt++);
1470
+ // getExpectedValue
1471
+ EXPECT_EQ (sandboxir::Utils::getExpectedValue (Add), Add);
1472
+ EXPECT_EQ (sandboxir::Utils::getExpectedValue (S0),
1473
+ cast<sandboxir::StoreInst>(S0)->getValueOperand ());
1474
+ EXPECT_EQ (sandboxir::Utils::getExpectedValue (RetF),
1475
+ cast<sandboxir::ReturnInst>(RetF)->getReturnValue ());
1476
+ // getExpectedType
1477
+ EXPECT_EQ (sandboxir::Utils::getExpectedType (Add), Add->getType ());
1478
+ EXPECT_EQ (sandboxir::Utils::getExpectedType (S0),
1479
+ cast<sandboxir::StoreInst>(S0)->getValueOperand ()->getType ());
1480
+ EXPECT_EQ (sandboxir::Utils::getExpectedType (RetF),
1481
+ cast<sandboxir::ReturnInst>(RetF)->getReturnValue ()->getType ());
1482
+
1483
+ // getExpectedValue for void returns
1484
+ llvm::Function &Bar = *M->getFunction (" bar" );
1485
+ Ctx.createFunction (&Bar);
1486
+ auto *BarBB = cast<sandboxir::BasicBlock>(Ctx.getValue (&*Bar.begin ()));
1487
+ auto BarIt = BarBB->begin ();
1488
+ auto *RetV = cast<sandboxir::Instruction>(&*BarIt++);
1489
+ EXPECT_EQ (sandboxir::Utils::getExpectedValue (RetV), nullptr );
1490
+ }
1491
+
1492
+ TEST_F (SandboxIRTest, GetNumBits) {
1493
+ parseIR (C, R"IR(
1494
+ define void @foo(float %arg0, double %arg1, i8 %arg2, i64 %arg3) {
1495
+ bb0:
1496
+ ret void
1497
+ }
1498
+ )IR" );
1499
+ llvm::Function &Foo = *M->getFunction (" foo" );
1500
+ sandboxir::Context Ctx (C);
1501
+ sandboxir::Function *F = Ctx.createFunction (&Foo);
1502
+ const DataLayout &DL = M->getDataLayout ();
1503
+ // getNumBits for scalars
1504
+ EXPECT_EQ (sandboxir::Utils::getNumBits (F->getArg (0 ), DL),
1505
+ DL.getTypeSizeInBits (Type::getFloatTy (C)));
1506
+ EXPECT_EQ (sandboxir::Utils::getNumBits (F->getArg (1 ), DL),
1507
+ DL.getTypeSizeInBits (Type::getDoubleTy (C)));
1508
+ EXPECT_EQ (sandboxir::Utils::getNumBits (F->getArg (2 ), DL), 8u );
1509
+ EXPECT_EQ (sandboxir::Utils::getNumBits (F->getArg (3 ), DL), 64u );
1510
+ }
1511
+
1448
1512
TEST_F (SandboxIRTest, RAUW_RUWIf) {
1449
1513
parseIR (C, R"IR(
1450
1514
define void @foo(ptr %ptr) {
0 commit comments