Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2115,6 +2115,7 @@ CommonPointerBase CommonPointerBase::compute(Value *LHS, Value *RHS) {
}

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

if (auto *GEP = dyn_cast<GEPOperator>(RHS)) {
Base.RHSGEPs.push_back(GEP);
Base.RHSNW &= GEP->getNoWrapFlags();
if (First) {
First = false;
Base.RHSNW = GEP->getNoWrapFlags();
} else {
Base.RHSNW = Base.RHSNW.intersectForOffsetAdd(GEP->getNoWrapFlags());
}
RHS = GEP->getPointerOperand();
} else {
// No common base.
Expand All @@ -2132,13 +2138,19 @@ CommonPointerBase CommonPointerBase::compute(Value *LHS, Value *RHS) {
}

// Collect LHS GEPs.
First = true;
while (true) {
if (LHS == Base.Ptr)
break;

auto *GEP = cast<GEPOperator>(LHS);
Base.LHSGEPs.push_back(GEP);
Base.LHSNW &= GEP->getNoWrapFlags();
if (First) {
First = false;
Base.LHSNW = GEP->getNoWrapFlags();
} else {
Base.LHSNW = Base.LHSNW.intersectForOffsetAdd(GEP->getNoWrapFlags());
}
LHS = GEP->getPointerOperand();
}

Expand Down
6 changes: 3 additions & 3 deletions llvm/test/Transforms/InstCombine/icmp-gep.ll
Original file line number Diff line number Diff line change
Expand Up @@ -838,9 +838,9 @@ define i1 @gep_mugtiple_ugt_not_all_nuw(ptr %base, i64 %idx, i64 %idx2) {

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