Skip to content

Commit 9c5a84b

Browse files
committed
[InstCombine] Support nusw in icmp of gep with base
Proof: https://alive2.llvm.org/ce/z/omnQXt
1 parent 7d72525 commit 9c5a84b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,8 @@ Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
691691
RHS = RHS->stripPointerCasts();
692692

693693
Value *PtrBase = GEPLHS->getOperand(0);
694-
if (PtrBase == RHS && (GEPLHS->isInBounds() || ICmpInst::isEquality(Cond))) {
694+
if (PtrBase == RHS &&
695+
(GEPLHS->hasNoUnsignedSignedWrap() || ICmpInst::isEquality(Cond))) {
695696
// ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
696697
Value *Offset = EmitGEPOffset(GEPLHS);
697698
return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset,

llvm/test/Transforms/InstCombine/icmp-gep.ll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ define i1 @ult_base_inbounds(ptr %x, i64 %y) {
133133
ret i1 %r
134134
}
135135

136+
define i1 @ult_base_nusw(ptr %x, i64 %y) {
137+
; CHECK-LABEL: @ult_base_nusw(
138+
; CHECK-NEXT: [[R:%.*]] = icmp slt i64 [[Y:%.*]], 0
139+
; CHECK-NEXT: ret i1 [[R]]
140+
;
141+
%g = getelementptr nusw i8, ptr %x, i64 %y
142+
%r = icmp ult ptr %g, %x
143+
ret i1 %r
144+
}
145+
136146
define i1 @ugt_base_inbounds_commute(i64 %y) {
137147
; CHECK-LABEL: @ugt_base_inbounds_commute(
138148
; CHECK-NEXT: [[X:%.*]] = call ptr @getptr()

0 commit comments

Comments
 (0)