[llvm] [SandboxIR] Add setOperand() and RAUW,RUWIf,RUOW (PR #98410)
Arthur Eubanks via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 11 13:02:25 PDT 2024
================
@@ -203,6 +206,107 @@ OperandNo: 0
EXPECT_FALSE(I0->hasNUses(0u));
EXPECT_TRUE(I0->hasNUses(1u));
EXPECT_FALSE(I0->hasNUses(2u));
+
+ // Check User.setOperand().
+ Ret->setOperand(0, Arg0);
+ EXPECT_EQ(Ret->getOperand(0), Arg0);
+ EXPECT_EQ(Ret->getOperandUse(0).get(), Arg0);
+ EXPECT_EQ(LLVMRet->getOperand(0), LLVMArg0);
+
+ Ret->setOperand(0, Arg1);
+ EXPECT_EQ(Ret->getOperand(0), Arg1);
+ EXPECT_EQ(Ret->getOperandUse(0).get(), Arg1);
+ EXPECT_EQ(LLVMRet->getOperand(0), LLVMArg1);
+}
+
+TEST_F(SandboxIRTest, RUOW) {
+ parseIR(C, R"IR(
+declare void @bar0()
+declare void @bar1()
+
+ at glob0 = global ptr @bar0
+ at glob1 = global ptr @bar1
+
+define i32 @foo(i32 %v0, i32 %v1) {
+ %add0 = add i32 %v0, %v1
+ %gep1 = getelementptr i8, ptr @glob0, i32 1
+ %gep2 = getelementptr i8, ptr @glob1, i32 1
+ ret i32 %add0
+}
+)IR");
+ llvm::Function &LLVMF = *M->getFunction("foo");
+ sandboxir::Context Ctx(C);
+
+ auto &F = *Ctx.createFunction(&LLVMF);
+ auto &BB = *F.begin();
+ auto *Arg0 = F.getArg(0);
+ auto *Arg1 = F.getArg(1);
+ auto It = BB.begin();
+ auto *I0 = &*It++;
+ auto *I1 = &*It++;
+ auto *I2 = &*It++;
+ auto *Ret = &*It++;
+
+ bool Replaced;
+ // Try to replace an operand that doesn't match.
+ Replaced = I0->replaceUsesOfWith(Ret, Arg1);
+ EXPECT_FALSE(Replaced);
+ EXPECT_EQ(I0->getOperand(0), Arg0);
+ EXPECT_EQ(I0->getOperand(1), Arg1);
+
+ // Replace I0 operands when operands differ.
+ Replaced = I0->replaceUsesOfWith(Arg0, Arg1);
+ EXPECT_TRUE(Replaced);
+ EXPECT_EQ(I0->getOperand(0), Arg1);
+ EXPECT_EQ(I0->getOperand(1), Arg1);
+
+ // Replace I0 operands when operands are the same.
+ Replaced = I0->replaceUsesOfWith(Arg1, Arg0);
+ EXPECT_TRUE(Replaced);
+ EXPECT_EQ(I0->getOperand(0), Arg0);
+ EXPECT_EQ(I0->getOperand(1), Arg0);
+
+ // Replace Ret operand.
+ Replaced = Ret->replaceUsesOfWith(I0, Arg0);
+ EXPECT_TRUE(Replaced);
+ EXPECT_EQ(Ret->getOperand(0), Arg0);
+
+ // Check RAUW on constant.
+ auto *Glob0 = cast<sandboxir::Constant>(I1->getOperand(0));
+ auto *Glob1 = cast<sandboxir::Constant>(I2->getOperand(0));
+ auto *Glob0Op = Glob0->getOperand(0);
+ Glob0->replaceUsesOfWith(Glob0Op, Glob1);
+ EXPECT_EQ(Glob0->getOperand(0), Glob1);
+}
+
+TEST_F(SandboxIRTest, RAW_RUWIf) {
----------------
aeubanks wrote:
is the name supposed to be `RAUW_RUWIf`?
https://github.com/llvm/llvm-project/pull/98410
More information about the llvm-commits
mailing list