diff options
| -rw-r--r-- | llvm/include/llvm/SandboxIR/SandboxIR.h | 15 | ||||
| -rw-r--r-- | llvm/lib/SandboxIR/SandboxIR.cpp | 6 | ||||
| -rw-r--r-- | llvm/unittests/SandboxIR/SandboxIRTest.cpp | 11 | ||||
| -rw-r--r-- | llvm/unittests/SandboxIR/TrackerTest.cpp | 26 |
4 files changed, 57 insertions, 1 deletions
diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h index 2fdbbbd..8888468 100644 --- a/llvm/include/llvm/SandboxIR/SandboxIR.h +++ b/llvm/include/llvm/SandboxIR/SandboxIR.h @@ -1506,6 +1506,10 @@ public: static Value *create(Value *Cond, Value *True, Value *False, BasicBlock *InsertAtEnd, Context &Ctx, const Twine &Name = ""); + + const Value *getCondition() const { return getOperand(0); } + const Value *getTrueValue() const { return getOperand(1); } + const Value *getFalseValue() const { return getOperand(2); } Value *getCondition() { return getOperand(0); } Value *getTrueValue() { return getOperand(1); } Value *getFalseValue() { return getOperand(2); } @@ -1513,7 +1517,16 @@ public: void setCondition(Value *New) { setOperand(0, New); } void setTrueValue(Value *New) { setOperand(1, New); } void setFalseValue(Value *New) { setOperand(2, New); } - void swapValues() { cast<llvm::SelectInst>(Val)->swapValues(); } + void swapValues(); + + /// Return a string if the specified operands are invalid for a select + /// operation, otherwise return null. + static const char *areInvalidOperands(Value *Cond, Value *True, + Value *False) { + return llvm::SelectInst::areInvalidOperands(Cond->Val, True->Val, + False->Val); + } + /// For isa/dyn_cast. static bool classof(const Value *From); }; diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp index 18fdcda..df38395 100644 --- a/llvm/lib/SandboxIR/SandboxIR.cpp +++ b/llvm/lib/SandboxIR/SandboxIR.cpp @@ -662,6 +662,12 @@ Value *SelectInst::create(Value *Cond, Value *True, Value *False, return createCommon(Cond, True, False, Name, Builder, Ctx); } +void SelectInst::swapValues() { + Ctx.getTracker().emplaceIfTracking<UseSwap>(getOperandUse(1), + getOperandUse(2)); + cast<llvm::SelectInst>(Val)->swapValues(); +} + bool SelectInst::classof(const Value *From) { return From->getSubclassID() == ClassID::Select; } diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp index b76d24d..148afd9 100644 --- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp +++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp @@ -1354,14 +1354,18 @@ define void @foo(i1 %c0, i8 %v0, i8 %v1, i1 %c1) { auto *BB = &*F->begin(); auto It = BB->begin(); auto *Select = cast<sandboxir::SelectInst>(&*It++); + const auto *ConstSelect = Select; // To test the const getters. auto *Ret = &*It++; // Check getCondition(). EXPECT_EQ(Select->getCondition(), Cond0); + EXPECT_EQ(ConstSelect->getCondition(), Cond0); // Check getTrueValue(). EXPECT_EQ(Select->getTrueValue(), V0); + EXPECT_EQ(ConstSelect->getTrueValue(), V0); // Check getFalseValue(). EXPECT_EQ(Select->getFalseValue(), V1); + EXPECT_EQ(ConstSelect->getFalseValue(), V1); // Check setCondition(). Select->setCondition(Cond1); EXPECT_EQ(Select->getCondition(), Cond1); @@ -1371,6 +1375,13 @@ define void @foo(i1 %c0, i8 %v0, i8 %v1, i1 %c1) { // Check setFalseValue(). Select->setFalseValue(V0); EXPECT_EQ(Select->getFalseValue(), V0); + // Check swapValues(). + Select->swapValues(); + EXPECT_EQ(Select->getTrueValue(), V0); + EXPECT_EQ(Select->getFalseValue(), V1); + // Check areInvalidOperands. + EXPECT_EQ(sandboxir::SelectInst::areInvalidOperands(Cond0, V0, V1), nullptr); + EXPECT_NE(sandboxir::SelectInst::areInvalidOperands(V0, V1, Cond0), nullptr); { // Check SelectInst::create() InsertBefore. diff --git a/llvm/unittests/SandboxIR/TrackerTest.cpp b/llvm/unittests/SandboxIR/TrackerTest.cpp index a1f39fe..a1a4117 100644 --- a/llvm/unittests/SandboxIR/TrackerTest.cpp +++ b/llvm/unittests/SandboxIR/TrackerTest.cpp @@ -964,6 +964,32 @@ define void @foo(i32 %cond0, i32 %cond1) { EXPECT_EQ(Switch->findCaseDest(BB1), One); } +TEST_F(TrackerTest, SelectInst) { + parseIR(C, R"IR( +define void @foo(i1 %c0, i8 %v0, i8 %v1) { + %sel = select i1 %c0, i8 %v0, i8 %v1 + ret void +} +)IR"); + llvm::Function *LLVMF = &*M->getFunction("foo"); + sandboxir::Context Ctx(C); + sandboxir::Function *F = Ctx.createFunction(LLVMF); + auto *V0 = F->getArg(1); + auto *V1 = F->getArg(2); + auto *BB = &*F->begin(); + auto It = BB->begin(); + auto *Select = cast<sandboxir::SelectInst>(&*It++); + + // Check tracking for swapValues. + Ctx.save(); + Select->swapValues(); + EXPECT_EQ(Select->getTrueValue(), V1); + EXPECT_EQ(Select->getFalseValue(), V0); + Ctx.revert(); + EXPECT_EQ(Select->getTrueValue(), V0); + EXPECT_EQ(Select->getFalseValue(), V1); +} + TEST_F(TrackerTest, ShuffleVectorInst) { parseIR(C, R"IR( define void @foo(<2 x i8> %v1, <2 x i8> %v2) { |
