Skip to content

Commit 956591b

Browse files
authored
[SandboxIR] Add remaining SelectInst methods and track swapValues() (#108114)
1 parent ea83e1c commit 956591b

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

llvm/include/llvm/SandboxIR/SandboxIR.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1506,14 +1506,27 @@ class SelectInst : public SingleLLVMInstructionImpl<llvm::SelectInst> {
15061506
static Value *create(Value *Cond, Value *True, Value *False,
15071507
BasicBlock *InsertAtEnd, Context &Ctx,
15081508
const Twine &Name = "");
1509+
1510+
const Value *getCondition() const { return getOperand(0); }
1511+
const Value *getTrueValue() const { return getOperand(1); }
1512+
const Value *getFalseValue() const { return getOperand(2); }
15091513
Value *getCondition() { return getOperand(0); }
15101514
Value *getTrueValue() { return getOperand(1); }
15111515
Value *getFalseValue() { return getOperand(2); }
15121516

15131517
void setCondition(Value *New) { setOperand(0, New); }
15141518
void setTrueValue(Value *New) { setOperand(1, New); }
15151519
void setFalseValue(Value *New) { setOperand(2, New); }
1516-
void swapValues() { cast<llvm::SelectInst>(Val)->swapValues(); }
1520+
void swapValues();
1521+
1522+
/// Return a string if the specified operands are invalid for a select
1523+
/// operation, otherwise return null.
1524+
static const char *areInvalidOperands(Value *Cond, Value *True,
1525+
Value *False) {
1526+
return llvm::SelectInst::areInvalidOperands(Cond->Val, True->Val,
1527+
False->Val);
1528+
}
1529+
15171530
/// For isa/dyn_cast.
15181531
static bool classof(const Value *From);
15191532
};

llvm/lib/SandboxIR/SandboxIR.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,12 @@ Value *SelectInst::create(Value *Cond, Value *True, Value *False,
662662
return createCommon(Cond, True, False, Name, Builder, Ctx);
663663
}
664664

665+
void SelectInst::swapValues() {
666+
Ctx.getTracker().emplaceIfTracking<UseSwap>(getOperandUse(1),
667+
getOperandUse(2));
668+
cast<llvm::SelectInst>(Val)->swapValues();
669+
}
670+
665671
bool SelectInst::classof(const Value *From) {
666672
return From->getSubclassID() == ClassID::Select;
667673
}

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,14 +1354,18 @@ define void @foo(i1 %c0, i8 %v0, i8 %v1, i1 %c1) {
13541354
auto *BB = &*F->begin();
13551355
auto It = BB->begin();
13561356
auto *Select = cast<sandboxir::SelectInst>(&*It++);
1357+
const auto *ConstSelect = Select; // To test the const getters.
13571358
auto *Ret = &*It++;
13581359

13591360
// Check getCondition().
13601361
EXPECT_EQ(Select->getCondition(), Cond0);
1362+
EXPECT_EQ(ConstSelect->getCondition(), Cond0);
13611363
// Check getTrueValue().
13621364
EXPECT_EQ(Select->getTrueValue(), V0);
1365+
EXPECT_EQ(ConstSelect->getTrueValue(), V0);
13631366
// Check getFalseValue().
13641367
EXPECT_EQ(Select->getFalseValue(), V1);
1368+
EXPECT_EQ(ConstSelect->getFalseValue(), V1);
13651369
// Check setCondition().
13661370
Select->setCondition(Cond1);
13671371
EXPECT_EQ(Select->getCondition(), Cond1);
@@ -1371,6 +1375,13 @@ define void @foo(i1 %c0, i8 %v0, i8 %v1, i1 %c1) {
13711375
// Check setFalseValue().
13721376
Select->setFalseValue(V0);
13731377
EXPECT_EQ(Select->getFalseValue(), V0);
1378+
// Check swapValues().
1379+
Select->swapValues();
1380+
EXPECT_EQ(Select->getTrueValue(), V0);
1381+
EXPECT_EQ(Select->getFalseValue(), V1);
1382+
// Check areInvalidOperands.
1383+
EXPECT_EQ(sandboxir::SelectInst::areInvalidOperands(Cond0, V0, V1), nullptr);
1384+
EXPECT_NE(sandboxir::SelectInst::areInvalidOperands(V0, V1, Cond0), nullptr);
13741385

13751386
{
13761387
// Check SelectInst::create() InsertBefore.

llvm/unittests/SandboxIR/TrackerTest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,32 @@ define void @foo(i32 %cond0, i32 %cond1) {
964964
EXPECT_EQ(Switch->findCaseDest(BB1), One);
965965
}
966966

967+
TEST_F(TrackerTest, SelectInst) {
968+
parseIR(C, R"IR(
969+
define void @foo(i1 %c0, i8 %v0, i8 %v1) {
970+
%sel = select i1 %c0, i8 %v0, i8 %v1
971+
ret void
972+
}
973+
)IR");
974+
llvm::Function *LLVMF = &*M->getFunction("foo");
975+
sandboxir::Context Ctx(C);
976+
sandboxir::Function *F = Ctx.createFunction(LLVMF);
977+
auto *V0 = F->getArg(1);
978+
auto *V1 = F->getArg(2);
979+
auto *BB = &*F->begin();
980+
auto It = BB->begin();
981+
auto *Select = cast<sandboxir::SelectInst>(&*It++);
982+
983+
// Check tracking for swapValues.
984+
Ctx.save();
985+
Select->swapValues();
986+
EXPECT_EQ(Select->getTrueValue(), V1);
987+
EXPECT_EQ(Select->getFalseValue(), V0);
988+
Ctx.revert();
989+
EXPECT_EQ(Select->getTrueValue(), V0);
990+
EXPECT_EQ(Select->getFalseValue(), V1);
991+
}
992+
967993
TEST_F(TrackerTest, ShuffleVectorInst) {
968994
parseIR(C, R"IR(
969995
define void @foo(<2 x i8> %v1, <2 x i8> %v2) {

0 commit comments

Comments
 (0)