Skip to content

Commit f35af77

Browse files
committed
[InstSimplify] Strip offsets once in computePointerICmp()
Instead of doing an inbounds strip first and another non-inbounds strip afterward for equality comparisons, directly do a single inbounds or non-inbounds strip based on whether we have an equality predicate or not. This is NFC-ish in that the alloca equality codepath is the only part that sees additional non-inbounds offsets now, and for that codepath it doesn't matter whether or not the GEP is inbounds, as it does a stronger check itself. InstCombine would infer inbounds for such GEPs.
1 parent eccdf2d commit f35af77

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

+8-13
Original file line numberDiff line numberDiff line change
@@ -2588,8 +2588,14 @@ computePointerICmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
25882588
// numerous hazards. AliasAnalysis and its utilities rely on special rules
25892589
// governing loads and stores which don't apply to icmps. Also, AliasAnalysis
25902590
// doesn't need to guarantee pointer inequality when it says NoAlias.
2591-
Constant *LHSOffset = stripAndComputeConstantOffsets(DL, LHS);
2592-
Constant *RHSOffset = stripAndComputeConstantOffsets(DL, RHS);
2591+
2592+
// Even if an non-inbounds GEP occurs along the path we can still optimize
2593+
// equality comparisons concerning the result.
2594+
bool AllowNonInbounds = ICmpInst::isEquality(Pred);
2595+
Constant *LHSOffset =
2596+
stripAndComputeConstantOffsets(DL, LHS, AllowNonInbounds);
2597+
Constant *RHSOffset =
2598+
stripAndComputeConstantOffsets(DL, RHS, AllowNonInbounds);
25932599

25942600
// If LHS and RHS are related via constant offsets to the same base
25952601
// value, we can replace it with an icmp which just compares the offsets.
@@ -2659,17 +2665,6 @@ computePointerICmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
26592665
!CmpInst::isTrueWhenEqual(Pred));
26602666
}
26612667

2662-
// Even if an non-inbounds GEP occurs along the path we can still optimize
2663-
// equality comparisons concerning the result. We avoid walking the whole
2664-
// chain again by starting where the last calls to
2665-
// stripAndComputeConstantOffsets left off and accumulate the offsets.
2666-
Constant *LHSNoBound = stripAndComputeConstantOffsets(DL, LHS, true);
2667-
Constant *RHSNoBound = stripAndComputeConstantOffsets(DL, RHS, true);
2668-
if (LHS == RHS)
2669-
return ConstantExpr::getICmp(Pred,
2670-
ConstantExpr::getAdd(LHSOffset, LHSNoBound),
2671-
ConstantExpr::getAdd(RHSOffset, RHSNoBound));
2672-
26732668
// If one side of the equality comparison must come from a noalias call
26742669
// (meaning a system memory allocation function), and the other side must
26752670
// come from a pointer that cannot overlap with dynamically-allocated

0 commit comments

Comments
 (0)