Skip to content

Commit e93fb4a

Browse files
nikicChenyang-L
authored andcommitted
[InstSimplify] Make sure offsets have same size in computePointerICmp()
The way this is currently implemented the accumulated offsets can end up having a different size, which causes unnecessary complication for further extension of the code. Don't strip pointer casts at the start and rely on stripAndAccumulate to do any necessary stripping. It gracefully handles different index sizes and will always retain the width of the original pointer index type. This is not NFC, but unlikely to make any practical difference.
1 parent 493c16b commit e93fb4a

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2717,16 +2717,13 @@ static bool haveNonOverlappingStorage(const Value *V1, const Value *V2) {
27172717
// this optimization.
27182718
static Constant *computePointerICmp(CmpInst::Predicate Pred, Value *LHS,
27192719
Value *RHS, const SimplifyQuery &Q) {
2720+
assert(LHS->getType() == RHS->getType() && "Must have same types");
27202721
const DataLayout &DL = Q.DL;
27212722
const TargetLibraryInfo *TLI = Q.TLI;
27222723
const DominatorTree *DT = Q.DT;
27232724
const Instruction *CxtI = Q.CxtI;
27242725
const InstrInfoQuery &IIQ = Q.IIQ;
27252726

2726-
// First, skip past any trivial no-ops.
2727-
LHS = LHS->stripPointerCasts();
2728-
RHS = RHS->stripPointerCasts();
2729-
27302727
// A non-null pointer is not equal to a null pointer.
27312728
if (isa<ConstantPointerNull>(RHS) && ICmpInst::isEquality(Pred) &&
27322729
llvm::isKnownNonZero(LHS, DL, 0, nullptr, nullptr, nullptr,
@@ -2765,8 +2762,10 @@ static Constant *computePointerICmp(CmpInst::Predicate Pred, Value *LHS,
27652762
// Even if an non-inbounds GEP occurs along the path we can still optimize
27662763
// equality comparisons concerning the result.
27672764
bool AllowNonInbounds = ICmpInst::isEquality(Pred);
2768-
APInt LHSOffset = stripAndComputeConstantOffsets(DL, LHS, AllowNonInbounds);
2769-
APInt RHSOffset = stripAndComputeConstantOffsets(DL, RHS, AllowNonInbounds);
2765+
unsigned IndexSize = DL.getIndexTypeSizeInBits(LHS->getType());
2766+
APInt LHSOffset(IndexSize, 0), RHSOffset(IndexSize, 0);
2767+
LHS = LHS->stripAndAccumulateConstantOffsets(DL, LHSOffset, AllowNonInbounds);
2768+
RHS = RHS->stripAndAccumulateConstantOffsets(DL, RHSOffset, AllowNonInbounds);
27702769

27712770
// If LHS and RHS are related via constant offsets to the same base
27722771
// value, we can replace it with an icmp which just compares the offsets.
@@ -3996,10 +3995,9 @@ static Value *simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
39963995
return C;
39973996
if (auto *CLHS = dyn_cast<PtrToIntOperator>(LHS))
39983997
if (auto *CRHS = dyn_cast<PtrToIntOperator>(RHS))
3999-
if (Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) ==
4000-
Q.DL.getTypeSizeInBits(CLHS->getType()) &&
4001-
Q.DL.getTypeSizeInBits(CRHS->getPointerOperandType()) ==
4002-
Q.DL.getTypeSizeInBits(CRHS->getType()))
3998+
if (CLHS->getPointerOperandType() == CRHS->getPointerOperandType() &&
3999+
Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) ==
4000+
Q.DL.getTypeSizeInBits(CLHS->getType()))
40034001
if (auto *C = computePointerICmp(Pred, CLHS->getPointerOperand(),
40044002
CRHS->getPointerOperand(), Q))
40054003
return C;

0 commit comments

Comments
 (0)