Skip to content

Commit 22d4ff1

Browse files
authored
[SandboxIR] Fix CmpInst::create() when it gets folded (#123408)
If the operands of a CmpInst are constants then it gets folded into a constant. Therefore CmpInst::create() should return a Value*, not a Constant* and should handle the creation of the constant correctly.
1 parent 2523d3b commit 22d4ff1

File tree

3 files changed

+53
-28
lines changed

3 files changed

+53
-28
lines changed

llvm/include/llvm/SandboxIR/Instruction.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,13 +2478,12 @@ class CmpInst : public SingleLLVMInstructionImpl<llvm::CmpInst> {
24782478
public:
24792479
using Predicate = llvm::CmpInst::Predicate;
24802480

2481-
static CmpInst *create(Predicate Pred, Value *S1, Value *S2,
2482-
InsertPosition Pos, Context &Ctx,
2483-
const Twine &Name = "");
2484-
static CmpInst *createWithCopiedFlags(Predicate Pred, Value *S1, Value *S2,
2485-
const Instruction *FlagsSource,
2486-
InsertPosition Pos, Context &Ctx,
2487-
const Twine &Name = "");
2481+
static Value *create(Predicate Pred, Value *S1, Value *S2, InsertPosition Pos,
2482+
Context &Ctx, const Twine &Name = "");
2483+
static Value *createWithCopiedFlags(Predicate Pred, Value *S1, Value *S2,
2484+
const Instruction *FlagsSource,
2485+
InsertPosition Pos, Context &Ctx,
2486+
const Twine &Name = "");
24882487
void setPredicate(Predicate P);
24892488
void swapOperands();
24902489

llvm/lib/SandboxIR/Instruction.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -926,21 +926,26 @@ void PHINode::removeIncomingValueIf(function_ref<bool(unsigned)> Predicate) {
926926
}
927927
}
928928

929-
CmpInst *CmpInst::create(Predicate P, Value *S1, Value *S2, InsertPosition Pos,
930-
Context &Ctx, const Twine &Name) {
929+
Value *CmpInst::create(Predicate P, Value *S1, Value *S2, InsertPosition Pos,
930+
Context &Ctx, const Twine &Name) {
931931
auto &Builder = setInsertPos(Pos);
932-
auto *LLVMI = Builder.CreateCmp(P, S1->Val, S2->Val, Name);
933-
if (dyn_cast<llvm::ICmpInst>(LLVMI))
934-
return Ctx.createICmpInst(cast<llvm::ICmpInst>(LLVMI));
935-
return Ctx.createFCmpInst(cast<llvm::FCmpInst>(LLVMI));
936-
}
937-
CmpInst *CmpInst::createWithCopiedFlags(Predicate P, Value *S1, Value *S2,
938-
const Instruction *F,
939-
InsertPosition Pos, Context &Ctx,
940-
const Twine &Name) {
941-
CmpInst *Inst = create(P, S1, S2, Pos, Ctx, Name);
942-
cast<llvm::CmpInst>(Inst->Val)->copyIRFlags(F->Val);
943-
return Inst;
932+
auto *LLVMV = Builder.CreateCmp(P, S1->Val, S2->Val, Name);
933+
// It may have been folded into a constant.
934+
if (auto *LLVMC = dyn_cast<llvm::Constant>(LLVMV))
935+
return Ctx.getOrCreateConstant(LLVMC);
936+
if (isa<llvm::ICmpInst>(LLVMV))
937+
return Ctx.createICmpInst(cast<llvm::ICmpInst>(LLVMV));
938+
return Ctx.createFCmpInst(cast<llvm::FCmpInst>(LLVMV));
939+
}
940+
941+
Value *CmpInst::createWithCopiedFlags(Predicate P, Value *S1, Value *S2,
942+
const Instruction *F, InsertPosition Pos,
943+
Context &Ctx, const Twine &Name) {
944+
Value *V = create(P, S1, S2, Pos, Ctx, Name);
945+
if (auto *C = dyn_cast<Constant>(V))
946+
return C;
947+
cast<llvm::CmpInst>(V->Val)->copyIRFlags(F->Val);
948+
return V;
944949
}
945950

946951
Type *CmpInst::makeCmpResultType(Type *OpndType) {

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5841,9 +5841,9 @@ define void @foo(i32 %i0, i32 %i1) {
58415841
EXPECT_EQ(ICmp->getSignedPredicate(), LLVMICmp->getSignedPredicate());
58425842
EXPECT_EQ(ICmp->getUnsignedPredicate(), LLVMICmp->getUnsignedPredicate());
58435843
}
5844-
auto *NewCmp =
5844+
auto *NewCmp = cast<sandboxir::CmpInst>(
58455845
sandboxir::CmpInst::create(llvm::CmpInst::ICMP_ULE, F.getArg(0),
5846-
F.getArg(1), BB->begin(), Ctx, "NewCmp");
5846+
F.getArg(1), BB->begin(), Ctx, "NewCmp"));
58475847
EXPECT_EQ(NewCmp, &*BB->begin());
58485848
EXPECT_EQ(NewCmp->getPredicate(), llvm::CmpInst::ICMP_ULE);
58495849
EXPECT_EQ(NewCmp->getOperand(0), F.getArg(0));
@@ -5856,6 +5856,16 @@ define void @foo(i32 %i0, i32 %i1) {
58565856
sandboxir::Type *RT =
58575857
sandboxir::CmpInst::makeCmpResultType(F.getArg(0)->getType());
58585858
EXPECT_TRUE(RT->isIntegerTy(1)); // Only one bit in a single comparison
5859+
5860+
{
5861+
// Check create() when operands are constant.
5862+
auto *Const42 =
5863+
sandboxir::ConstantInt::get(sandboxir::Type::getInt32Ty(Ctx), 42);
5864+
auto *NewConstCmp =
5865+
sandboxir::CmpInst::create(llvm::CmpInst::ICMP_ULE, Const42, Const42,
5866+
BB->begin(), Ctx, "NewConstCmp");
5867+
EXPECT_TRUE(isa<sandboxir::Constant>(NewConstCmp));
5868+
}
58595869
}
58605870

58615871
TEST_F(SandboxIRTest, FCmpInst) {
@@ -5906,8 +5916,8 @@ define void @foo(float %f0, float %f1) {
59065916
CopyFrom->setFastMathFlags(FastMathFlags::getFast());
59075917

59085918
// create with default flags
5909-
auto *NewFCmp = sandboxir::CmpInst::create(
5910-
llvm::CmpInst::FCMP_ONE, F.getArg(0), F.getArg(1), It1, Ctx, "NewFCmp");
5919+
auto *NewFCmp = cast<sandboxir::CmpInst>(sandboxir::CmpInst::create(
5920+
llvm::CmpInst::FCMP_ONE, F.getArg(0), F.getArg(1), It1, Ctx, "NewFCmp"));
59115921
EXPECT_EQ(NewFCmp->getPredicate(), llvm::CmpInst::FCMP_ONE);
59125922
EXPECT_EQ(NewFCmp->getOperand(0), F.getArg(0));
59135923
EXPECT_EQ(NewFCmp->getOperand(1), F.getArg(1));
@@ -5917,9 +5927,10 @@ define void @foo(float %f0, float %f1) {
59175927
FastMathFlags DefaultFMF = NewFCmp->getFastMathFlags();
59185928
EXPECT_TRUE(CopyFrom->getFastMathFlags() != DefaultFMF);
59195929
// create with copied flags
5920-
auto *NewFCmpFlags = sandboxir::CmpInst::createWithCopiedFlags(
5921-
llvm::CmpInst::FCMP_ONE, F.getArg(0), F.getArg(1), CopyFrom, It1, Ctx,
5922-
"NewFCmpFlags");
5930+
auto *NewFCmpFlags =
5931+
cast<sandboxir::CmpInst>(sandboxir::CmpInst::createWithCopiedFlags(
5932+
llvm::CmpInst::FCMP_ONE, F.getArg(0), F.getArg(1), CopyFrom, It1, Ctx,
5933+
"NewFCmpFlags"));
59235934
EXPECT_FALSE(NewFCmpFlags->getFastMathFlags() !=
59245935
CopyFrom->getFastMathFlags());
59255936
EXPECT_EQ(NewFCmpFlags->getPredicate(), llvm::CmpInst::FCMP_ONE);
@@ -5928,6 +5939,16 @@ define void @foo(float %f0, float %f1) {
59285939
#ifndef NDEBUG
59295940
EXPECT_EQ(NewFCmpFlags->getName(), "NewFCmpFlags");
59305941
#endif // NDEBUG
5942+
5943+
{
5944+
// Check create() when operands are constant.
5945+
auto *Const42 =
5946+
sandboxir::ConstantFP::get(sandboxir::Type::getFloatTy(Ctx), 42.0);
5947+
auto *NewConstCmp =
5948+
sandboxir::CmpInst::create(llvm::CmpInst::FCMP_ULE, Const42, Const42,
5949+
BB->begin(), Ctx, "NewConstCmp");
5950+
EXPECT_TRUE(isa<sandboxir::Constant>(NewConstCmp));
5951+
}
59315952
}
59325953

59335954
TEST_F(SandboxIRTest, UnreachableInst) {

0 commit comments

Comments
 (0)