Skip to content

Commit 738cf5a

Browse files
authored
InstSimplify: improve computePointerICmp (NFC) (llvm#126255)
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 7ee56b9 commit 738cf5a

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,27 +2686,14 @@ 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 predicates on pointer comparisons, but forbid
2690+
// signed predicates since a GEP with inbounds could cross the sign boundary.
2691+
if (CmpInst::isSigned(Pred))
26922692
return nullptr;
26932693

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-
}
2694+
// We have to switch to a signed predicate to handle negative indices from
2695+
// the base pointer.
2696+
Pred = ICmpInst::getSignedPredicate(Pred);
27102697

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

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

0 commit comments

Comments
 (0)