Skip to content

Commit f893dcc

Browse files
Replace uses of ConstantExpr::getCompare. (#91558)
Use ICmpInst::compare() where possible, ConstantFoldCompareInstOperands in other places. This only changes places where the either the fold is guaranteed to succeed, or the code doesn't use the resulting compare if we fail to fold.
1 parent 95f208f commit f893dcc

File tree

16 files changed

+76
-69
lines changed

16 files changed

+76
-69
lines changed

llvm/include/llvm/Transforms/Scalar/JumpThreading.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class JumpThreadingPass : public PassInfoMixin<JumpThreadingPass> {
142142
}
143143

144144
Constant *evaluateOnPredecessorEdge(BasicBlock *BB, BasicBlock *PredPredBB,
145-
Value *cond);
145+
Value *cond, const DataLayout &DL);
146146
bool maybethreadThroughTwoBasicBlocks(BasicBlock *BB, Value *Cond);
147147
void threadThroughTwoBasicBlocks(BasicBlock *PredPredBB, BasicBlock *PredBB,
148148
BasicBlock *BB, BasicBlock *SuccBB);

llvm/lib/Analysis/BranchProbabilityInfo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,8 @@ computeUnlikelySuccessors(const BasicBlock *BB, Loop *L,
630630
if (!CmpLHSConst)
631631
continue;
632632
// Now constant-evaluate the compare
633-
Constant *Result = ConstantExpr::getCompare(CI->getPredicate(),
634-
CmpLHSConst, CmpConst, true);
633+
Constant *Result = ConstantFoldCompareInstOperands(
634+
CI->getPredicate(), CmpLHSConst, CmpConst, DL);
635635
// If the result means we don't branch to the block then that block is
636636
// unlikely.
637637
if (Result &&

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,10 +1268,10 @@ Constant *llvm::ConstantFoldCompareInstOperands(
12681268
Value *Stripped1 =
12691269
Ops1->stripAndAccumulateInBoundsConstantOffsets(DL, Offset1);
12701270
if (Stripped0 == Stripped1)
1271-
return ConstantExpr::getCompare(
1272-
ICmpInst::getSignedPredicate(Predicate),
1273-
ConstantInt::get(CE0->getContext(), Offset0),
1274-
ConstantInt::get(CE0->getContext(), Offset1));
1271+
return ConstantInt::getBool(
1272+
Ops0->getContext(),
1273+
ICmpInst::compare(Offset0, Offset1,
1274+
ICmpInst::getSignedPredicate(Predicate)));
12751275
}
12761276
} else if (isa<ConstantExpr>(Ops1)) {
12771277
// If RHS is a constant expression, but the left side isn't, swap the

llvm/lib/Analysis/InlineCost.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,13 +2046,11 @@ bool CallAnalyzer::visitCmpInst(CmpInst &I) {
20462046
if (RHSBase && LHSBase == RHSBase) {
20472047
// We have common bases, fold the icmp to a constant based on the
20482048
// offsets.
2049-
Constant *CLHS = ConstantInt::get(LHS->getContext(), LHSOffset);
2050-
Constant *CRHS = ConstantInt::get(RHS->getContext(), RHSOffset);
2051-
if (Constant *C = ConstantExpr::getICmp(I.getPredicate(), CLHS, CRHS)) {
2052-
SimplifiedValues[&I] = C;
2053-
++NumConstantPtrCmps;
2054-
return true;
2055-
}
2049+
SimplifiedValues[&I] = ConstantInt::getBool(
2050+
I.getType(),
2051+
ICmpInst::compare(LHSOffset, RHSOffset, I.getPredicate()));
2052+
++NumConstantPtrCmps;
2053+
return true;
20562054
}
20572055
}
20582056

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10615,9 +10615,7 @@ bool ScalarEvolution::SimplifyICmpOperands(ICmpInst::Predicate &Pred,
1061510615
if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
1061610616
// Check for both operands constant.
1061710617
if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
10618-
if (ConstantExpr::getICmp(Pred,
10619-
LHSC->getValue(),
10620-
RHSC->getValue())->isNullValue())
10618+
if (!ICmpInst::compare(LHSC->getAPInt(), RHSC->getAPInt(), Pred))
1062110619
return TrivialCase(false);
1062210620
return TrivialCase(true);
1062310621
}

llvm/lib/IR/Constants.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,8 @@ bool Constant::isElementWiseEqual(Value *Y) const {
315315
Type *IntTy = VectorType::getInteger(VTy);
316316
Constant *C0 = ConstantExpr::getBitCast(const_cast<Constant *>(this), IntTy);
317317
Constant *C1 = ConstantExpr::getBitCast(cast<Constant>(Y), IntTy);
318-
Constant *CmpEq = ConstantExpr::getICmp(ICmpInst::ICMP_EQ, C0, C1);
319-
return isa<PoisonValue>(CmpEq) || match(CmpEq, m_One());
318+
Constant *CmpEq = ConstantFoldCompareInstruction(ICmpInst::ICMP_EQ, C0, C1);
319+
return CmpEq && (isa<PoisonValue>(CmpEq) || match(CmpEq, m_One()));
320320
}
321321

322322
static bool

llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -854,8 +854,9 @@ GCNTTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
854854

855855
if (auto *CSrc0 = dyn_cast<Constant>(Src0)) {
856856
if (auto *CSrc1 = dyn_cast<Constant>(Src1)) {
857-
Constant *CCmp = ConstantExpr::getCompare(CCVal, CSrc0, CSrc1);
858-
if (CCmp->isNullValue()) {
857+
Constant *CCmp = ConstantFoldCompareInstOperands(
858+
(ICmpInst::Predicate)CCVal, CSrc0, CSrc1, DL);
859+
if (CCmp && CCmp->isNullValue()) {
859860
return IC.replaceInstUsesWith(
860861
II, IC.Builder.CreateSExt(CCmp, II.getType()));
861862
}

llvm/lib/Target/X86/X86InstCombineIntrinsic.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,21 @@ using namespace llvm;
2626

2727
/// Return a constant boolean vector that has true elements in all positions
2828
/// where the input constant data vector has an element with the sign bit set.
29-
static Constant *getNegativeIsTrueBoolVec(Constant *V) {
29+
static Constant *getNegativeIsTrueBoolVec(Constant *V, const DataLayout &DL) {
3030
VectorType *IntTy = VectorType::getInteger(cast<VectorType>(V->getType()));
3131
V = ConstantExpr::getBitCast(V, IntTy);
32-
V = ConstantExpr::getICmp(CmpInst::ICMP_SGT, Constant::getNullValue(IntTy),
33-
V);
32+
V = ConstantFoldCompareInstOperands(CmpInst::ICMP_SGT,
33+
Constant::getNullValue(IntTy), V, DL);
34+
assert(V && "Vector must be foldable");
3435
return V;
3536
}
3637

3738
/// Convert the x86 XMM integer vector mask to a vector of bools based on
3839
/// each element's most significant bit (the sign bit).
39-
static Value *getBoolVecFromMask(Value *Mask) {
40+
static Value *getBoolVecFromMask(Value *Mask, const DataLayout &DL) {
4041
// Fold Constant Mask.
4142
if (auto *ConstantMask = dyn_cast<ConstantDataVector>(Mask))
42-
return getNegativeIsTrueBoolVec(ConstantMask);
43+
return getNegativeIsTrueBoolVec(ConstantMask, DL);
4344

4445
// Mask was extended from a boolean vector.
4546
Value *ExtMask;
@@ -65,7 +66,7 @@ static Instruction *simplifyX86MaskedLoad(IntrinsicInst &II, InstCombiner &IC) {
6566

6667
// The mask is constant or extended from a bool vector. Convert this x86
6768
// intrinsic to the LLVM intrinsic to allow target-independent optimizations.
68-
if (Value *BoolMask = getBoolVecFromMask(Mask)) {
69+
if (Value *BoolMask = getBoolVecFromMask(Mask, IC.getDataLayout())) {
6970
// First, cast the x86 intrinsic scalar pointer to a vector pointer to match
7071
// the LLVM intrinsic definition for the pointer argument.
7172
unsigned AddrSpace = cast<PointerType>(Ptr->getType())->getAddressSpace();
@@ -102,7 +103,7 @@ static bool simplifyX86MaskedStore(IntrinsicInst &II, InstCombiner &IC) {
102103

103104
// The mask is constant or extended from a bool vector. Convert this x86
104105
// intrinsic to the LLVM intrinsic to allow target-independent optimizations.
105-
if (Value *BoolMask = getBoolVecFromMask(Mask)) {
106+
if (Value *BoolMask = getBoolVecFromMask(Mask, IC.getDataLayout())) {
106107
unsigned AddrSpace = cast<PointerType>(Ptr->getType())->getAddressSpace();
107108
PointerType *VecPtrTy = PointerType::get(Vec->getType(), AddrSpace);
108109
Value *PtrCast = IC.Builder.CreateBitCast(Ptr, VecPtrTy, "castvec");
@@ -2688,7 +2689,8 @@ X86TTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
26882689

26892690
// Constant Mask - select 1st/2nd argument lane based on top bit of mask.
26902691
if (auto *ConstantMask = dyn_cast<ConstantDataVector>(Mask)) {
2691-
Constant *NewSelector = getNegativeIsTrueBoolVec(ConstantMask);
2692+
Constant *NewSelector =
2693+
getNegativeIsTrueBoolVec(ConstantMask, IC.getDataLayout());
26922694
return SelectInst::Create(NewSelector, Op1, Op0, "blendv");
26932695
}
26942696

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2504,8 +2504,8 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
25042504
match(C1, m_Power2())) {
25052505
Constant *Log2C1 = ConstantExpr::getExactLogBase2(C1);
25062506
Constant *Cmp =
2507-
ConstantExpr::getCompare(ICmpInst::ICMP_ULT, Log2C3, C2);
2508-
if (Cmp->isZeroValue()) {
2507+
ConstantFoldCompareInstOperands(ICmpInst::ICMP_ULT, Log2C3, C2, DL);
2508+
if (Cmp && Cmp->isZeroValue()) {
25092509
// iff C1,C3 is pow2 and Log2(C3) >= C2:
25102510
// ((C1 >> X) << C2) & C3 -> X == (cttz(C1)+C2-cttz(C3)) ? C3 : 0
25112511
Constant *ShlC = ConstantExpr::getAdd(C2, Log2C1);

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1982,7 +1982,8 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
19821982
if (ModuloC != ShAmtC)
19831983
return replaceOperand(*II, 2, ModuloC);
19841984

1985-
assert(match(ConstantExpr::getICmp(ICmpInst::ICMP_UGT, WidthC, ShAmtC),
1985+
assert(match(ConstantFoldCompareInstOperands(ICmpInst::ICMP_UGT, WidthC,
1986+
ShAmtC, DL),
19861987
m_One()) &&
19871988
"Shift amount expected to be modulo bitwidth");
19881989

0 commit comments

Comments
 (0)