[llvm] [SandboxIR] Implement CmpInst, FCmpInst, and ICmpInst (PR #106301)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 30 12:33:04 PDT 2024
================
@@ -4392,6 +4392,159 @@ define void @foo(i32 %arg) {
EXPECT_EQ(NewPHI->getNumIncomingValues(), PHI->getNumIncomingValues());
}
+static void checkSwapOperands(sandboxir::Context &Ctx,
+ llvm::sandboxir::CmpInst *Cmp,
+ llvm::CmpInst *LLVMCmp) {
+ auto OrigOp0 = Cmp->getOperand(0);
+ auto OrigOp1 = Cmp->getOperand(1);
+ EXPECT_EQ(Ctx.getValue(LLVMCmp->getOperand(0)), OrigOp0);
+ EXPECT_EQ(Ctx.getValue(LLVMCmp->getOperand(1)), OrigOp1);
+ // This checks the dispatch mechanism in CmpInst, as well as
+ // the specific implementations.
+ Cmp->swapOperands();
+ EXPECT_EQ(Ctx.getValue(LLVMCmp->getOperand(1)), OrigOp0);
+ EXPECT_EQ(Ctx.getValue(LLVMCmp->getOperand(0)), OrigOp1);
+ // Undo it to keep the rest of the test consistent
+ Cmp->swapOperands();
+}
+
+static void checkCommonPredicates(sandboxir::CmpInst *Cmp,
+ llvm::CmpInst *LLVMCmp) {
+ // Check proper creation
+ auto Pred = Cmp->getPredicate();
+ auto LLVMPred = LLVMCmp->getPredicate();
+ EXPECT_EQ(Pred, LLVMPred);
+ // Check setPredicate
+ Cmp->setPredicate(llvm::CmpInst::FCMP_FALSE);
+ EXPECT_EQ(Cmp->getPredicate(), llvm::CmpInst::FCMP_FALSE);
+ EXPECT_EQ(LLVMCmp->getPredicate(), llvm::CmpInst::FCMP_FALSE);
+ Cmp->setPredicate(Pred);
+ EXPECT_EQ(LLVMCmp->getPredicate(), Pred);
+ // Ensure the accessors properly forward to the underlying implementation
+ EXPECT_STREQ(sandboxir::CmpInst::getPredicateName(Pred).data(),
+ llvm::CmpInst::getPredicateName(LLVMPred).data());
+ EXPECT_EQ(Cmp->isFPPredicate(), LLVMCmp->isFPPredicate());
+ EXPECT_EQ(Cmp->isIntPredicate(), LLVMCmp->isIntPredicate());
+ EXPECT_EQ(Cmp->getInversePredicate(), LLVMCmp->getInversePredicate());
+ EXPECT_EQ(Cmp->getOrderedPredicate(), LLVMCmp->getOrderedPredicate());
+ EXPECT_EQ(Cmp->getUnorderedPredicate(), LLVMCmp->getUnorderedPredicate());
+ EXPECT_EQ(Cmp->getSwappedPredicate(), LLVMCmp->getSwappedPredicate());
+ EXPECT_EQ(Cmp->isStrictPredicate(), LLVMCmp->isStrictPredicate());
+ EXPECT_EQ(Cmp->isNonStrictPredicate(), LLVMCmp->isNonStrictPredicate());
+ EXPECT_EQ(Cmp->isRelational(), LLVMCmp->isRelational());
+ if (Cmp->isRelational()) {
+ EXPECT_EQ(Cmp->getFlippedStrictnessPredicate(),
+ LLVMCmp->getFlippedStrictnessPredicate());
+ }
+ EXPECT_EQ(Cmp->isCommutative(), LLVMCmp->isCommutative());
+ EXPECT_EQ(Cmp->isTrueWhenEqual(), LLVMCmp->isTrueWhenEqual());
+ EXPECT_EQ(Cmp->isFalseWhenEqual(), LLVMCmp->isFalseWhenEqual());
+ EXPECT_EQ(sandboxir::CmpInst::isOrdered(Pred),
+ llvm::CmpInst::isOrdered(LLVMPred));
+ EXPECT_EQ(sandboxir::CmpInst::isUnordered(Pred),
+ llvm::CmpInst::isUnordered(LLVMPred));
+}
+
+TEST_F(SandboxIRTest, ICmpInst) {
+ SCOPED_TRACE("SandboxIRTest sandboxir::ICmpInst tests");
+ parseIR(C, R"IR(
+define void @foo(i32 %i0, i32 %i1) {
+ bb:
+ %ine = icmp ne i32 %i0, %i1
+ %iugt = icmp ugt i32 %i0, %i1
+ %iuge = icmp uge i32 %i0, %i1
+ %iult = icmp ult i32 %i0, %i1
+ %iule = icmp ule i32 %i0, %i1
+ %isgt = icmp sgt i32 %i0, %i1
+ %isle = icmp sle i32 %i0, %i1
+ %ieg = icmp eq i32 %i0, %i1
+ ret void
+}
+)IR");
+ Function &LLVMF = *M->getFunction("foo");
+ sandboxir::Context Ctx(C);
+ [[maybe_unused]] auto &F = *Ctx.createFunction(&LLVMF);
+
+ auto *LLVMBB = getBasicBlockByName(LLVMF, "bb");
+ auto LLVMIt = LLVMBB->begin();
+ auto *BB = cast<sandboxir::BasicBlock>(Ctx.getValue(LLVMBB));
+ auto It = BB->begin();
+ // Check classof()
+ while (auto *ICmp = dyn_cast<sandboxir::ICmpInst>(&*It++)) {
+ auto *LLVMICmp = cast<llvm::ICmpInst>(&*LLVMIt++);
+ checkSwapOperands(Ctx, ICmp, LLVMICmp);
+ checkCommonPredicates(ICmp, LLVMICmp);
+ EXPECT_EQ(ICmp->isSigned(), LLVMICmp->isSigned());
+ EXPECT_EQ(ICmp->isUnsigned(), LLVMICmp->isUnsigned());
+ EXPECT_EQ(ICmp->getSignedPredicate(), LLVMICmp->getSignedPredicate());
+ EXPECT_EQ(ICmp->getUnsignedPredicate(), LLVMICmp->getUnsignedPredicate());
+ }
+ auto *NewCmp =
+ sandboxir::CmpInst::create(llvm::CmpInst::ICMP_ULE, F.getArg(0),
+ F.getArg(1), &*BB->begin(), Ctx, "");
----------------
vporpo wrote:
Let's set a name "NewCmp" instead of "", so we can check it below.
https://github.com/llvm/llvm-project/pull/106301
More information about the llvm-commits
mailing list