Skip to content

Commit dcdbb39

Browse files
authored
[InstCombine] Use intersectForOffsetAdd() in CommonPointerBase (#157851)
Transforms using this helper will add up all the offsets, so we should use intersectForOffsetAdd() instead of plain intersection. Annoyingly, this requires specially handling the first GEP to avoid losing flags in that case. Fixes #157714.
1 parent 924bf24 commit dcdbb39

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,6 +2115,7 @@ CommonPointerBase CommonPointerBase::compute(Value *LHS, Value *RHS) {
21152115
}
21162116

21172117
// Find common base and collect RHS GEPs.
2118+
bool First = true;
21182119
while (true) {
21192120
if (Ptrs.contains(RHS)) {
21202121
Base.Ptr = RHS;
@@ -2123,7 +2124,12 @@ CommonPointerBase CommonPointerBase::compute(Value *LHS, Value *RHS) {
21232124

21242125
if (auto *GEP = dyn_cast<GEPOperator>(RHS)) {
21252126
Base.RHSGEPs.push_back(GEP);
2126-
Base.RHSNW &= GEP->getNoWrapFlags();
2127+
if (First) {
2128+
First = false;
2129+
Base.RHSNW = GEP->getNoWrapFlags();
2130+
} else {
2131+
Base.RHSNW = Base.RHSNW.intersectForOffsetAdd(GEP->getNoWrapFlags());
2132+
}
21272133
RHS = GEP->getPointerOperand();
21282134
} else {
21292135
// No common base.
@@ -2132,13 +2138,19 @@ CommonPointerBase CommonPointerBase::compute(Value *LHS, Value *RHS) {
21322138
}
21332139

21342140
// Collect LHS GEPs.
2141+
First = true;
21352142
while (true) {
21362143
if (LHS == Base.Ptr)
21372144
break;
21382145

21392146
auto *GEP = cast<GEPOperator>(LHS);
21402147
Base.LHSGEPs.push_back(GEP);
2141-
Base.LHSNW &= GEP->getNoWrapFlags();
2148+
if (First) {
2149+
First = false;
2150+
Base.LHSNW = GEP->getNoWrapFlags();
2151+
} else {
2152+
Base.LHSNW = Base.LHSNW.intersectForOffsetAdd(GEP->getNoWrapFlags());
2153+
}
21422154
LHS = GEP->getPointerOperand();
21432155
}
21442156

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -838,9 +838,9 @@ define i1 @gep_mugtiple_ugt_not_all_nuw(ptr %base, i64 %idx, i64 %idx2) {
838838

839839
define i1 @gep_mugtiple_ugt_inbounds_nusw(ptr %base, i64 %idx, i64 %idx2) {
840840
; CHECK-LABEL: @gep_mugtiple_ugt_inbounds_nusw(
841-
; CHECK-NEXT: [[GEP1_IDX1:%.*]] = add i64 [[IDX:%.*]], [[IDX2:%.*]]
842-
; CHECK-NEXT: [[TMP1:%.*]] = shl i64 [[GEP1_IDX1]], 2
843-
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i64 [[TMP1]], 0
841+
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds i32, ptr [[BASE:%.*]], i64 [[IDX:%.*]]
842+
; CHECK-NEXT: [[GEP2:%.*]] = getelementptr nusw i32, ptr [[GEP1]], i64 [[IDX2:%.*]]
843+
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt ptr [[GEP2]], [[BASE]]
844844
; CHECK-NEXT: ret i1 [[CMP]]
845845
;
846846
%gep1 = getelementptr inbounds i32, ptr %base, i64 %idx

0 commit comments

Comments
 (0)