Skip to content

Commit

Permalink
[InstSimplify] Make sure offsets have same size in computePointerICmp()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikic committed Jun 27, 2023
1 parent 58056ae commit 793eb0c
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions llvm/lib/Analysis/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2717,16 +2717,13 @@ static bool haveNonOverlappingStorage(const Value *V1, const Value *V2) {
// this optimization.
static Constant *computePointerICmp(CmpInst::Predicate Pred, Value *LHS,
Value *RHS, const SimplifyQuery &Q) {
assert(LHS->getType() == RHS->getType() && "Must have same types");
const DataLayout &DL = Q.DL;
const TargetLibraryInfo *TLI = Q.TLI;
const DominatorTree *DT = Q.DT;
const Instruction *CxtI = Q.CxtI;
const InstrInfoQuery &IIQ = Q.IIQ;

// First, skip past any trivial no-ops.
LHS = LHS->stripPointerCasts();
RHS = RHS->stripPointerCasts();

// A non-null pointer is not equal to a null pointer.
if (isa<ConstantPointerNull>(RHS) && ICmpInst::isEquality(Pred) &&
llvm::isKnownNonZero(LHS, DL, 0, nullptr, nullptr, nullptr,
Expand Down Expand Up @@ -2765,8 +2762,10 @@ static Constant *computePointerICmp(CmpInst::Predicate Pred, Value *LHS,
// Even if an non-inbounds GEP occurs along the path we can still optimize
// equality comparisons concerning the result.
bool AllowNonInbounds = ICmpInst::isEquality(Pred);
APInt LHSOffset = stripAndComputeConstantOffsets(DL, LHS, AllowNonInbounds);
APInt RHSOffset = stripAndComputeConstantOffsets(DL, RHS, AllowNonInbounds);
unsigned IndexSize = DL.getIndexTypeSizeInBits(LHS->getType());
APInt LHSOffset(IndexSize, 0), RHSOffset(IndexSize, 0);
LHS = LHS->stripAndAccumulateConstantOffsets(DL, LHSOffset, AllowNonInbounds);
RHS = RHS->stripAndAccumulateConstantOffsets(DL, RHSOffset, AllowNonInbounds);

// If LHS and RHS are related via constant offsets to the same base
// value, we can replace it with an icmp which just compares the offsets.
Expand Down Expand Up @@ -3996,10 +3995,9 @@ static Value *simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
return C;
if (auto *CLHS = dyn_cast<PtrToIntOperator>(LHS))
if (auto *CRHS = dyn_cast<PtrToIntOperator>(RHS))
if (Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) ==
Q.DL.getTypeSizeInBits(CLHS->getType()) &&
Q.DL.getTypeSizeInBits(CRHS->getPointerOperandType()) ==
Q.DL.getTypeSizeInBits(CRHS->getType()))
if (CLHS->getPointerOperandType() == CRHS->getPointerOperandType() &&
Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) ==
Q.DL.getTypeSizeInBits(CLHS->getType()))
if (auto *C = computePointerICmp(Pred, CLHS->getPointerOperand(),
CRHS->getPointerOperand(), Q))
return C;
Expand Down

0 comments on commit 793eb0c

Please sign in to comment.