Skip to content

Commit 1607671

Browse files
committed
InstSimplify: improve computePointerICmp (NFC)
The comment about inbounds protecting only against unsigned wrapping is incorrect: it also protects against signed wrapping, but the issue is that it could cross the sign boundary.
1 parent c269182 commit 1607671

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,27 +2686,15 @@ static Constant *computePointerICmp(CmpPredicate Pred, Value *LHS, Value *RHS,
26862686
const DataLayout &DL = Q.DL;
26872687
const TargetLibraryInfo *TLI = Q.TLI;
26882688

2689-
// We can only fold certain predicates on pointer comparisons.
2690-
switch (Pred) {
2691-
default:
2689+
// We fold equality and unsigned integer predicates on pointer comparisons,
2690+
// but forbid signed predicates since a GEP with inbounds could cross the sign
2691+
// boundary.
2692+
if (!CmpInst::isIntPredicate(Pred) || CmpInst::isSigned(Pred))
26922693
return nullptr;
26932694

2694-
// Equality comparisons are easy to fold.
2695-
case CmpInst::ICMP_EQ:
2696-
case CmpInst::ICMP_NE:
2697-
break;
2698-
2699-
// We can only handle unsigned relational comparisons because 'inbounds' on
2700-
// a GEP only protects against unsigned wrapping.
2701-
case CmpInst::ICMP_UGT:
2702-
case CmpInst::ICMP_UGE:
2703-
case CmpInst::ICMP_ULT:
2704-
case CmpInst::ICMP_ULE:
2705-
// However, we have to switch them to their signed variants to handle
2706-
// negative indices from the base pointer.
2707-
Pred = ICmpInst::getSignedPredicate(Pred);
2708-
break;
2709-
}
2695+
// We have to switch to a signed predicate to handle negative indices from
2696+
// the base pointer.
2697+
Pred = ICmpInst::getSignedPredicate(Pred);
27102698

27112699
// Strip off any constant offsets so that we can reason about them.
27122700
// It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets
@@ -2730,7 +2718,7 @@ static Constant *computePointerICmp(CmpPredicate Pred, Value *LHS, Value *RHS,
27302718
ICmpInst::compare(LHSOffset, RHSOffset, Pred));
27312719

27322720
// Various optimizations for (in)equality comparisons.
2733-
if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) {
2721+
if (ICmpInst::isEquality(Pred)) {
27342722
// Different non-empty allocations that exist at the same time have
27352723
// different addresses (if the program can tell). If the offsets are
27362724
// within the bounds of their allocations (and not one-past-the-end!

0 commit comments

Comments
 (0)