Skip to content

Commit 940f892

Browse files
committed
[InstCombine] Do not modify GEP in place
This was modifying the GEP in place, with code to adjust the inbounds flag. This was correct at the time, but now fails to account for other GEP flags like nuw, leading to miscompilations. Remove the special case, and always create a new GEP instruction. Logic for preserving nuw in the cases where it is valid will be added in a followup patch.
1 parent c0e308b commit 940f892

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,13 +2508,6 @@ Instruction *InstCombinerImpl::visitGEPOfGEP(GetElementPtrInst &GEP,
25082508
if (Sum == nullptr)
25092509
return nullptr;
25102510

2511-
// Update the GEP in place if possible.
2512-
if (Src->getNumOperands() == 2) {
2513-
GEP.setIsInBounds(isMergedGEPInBounds(*Src, *cast<GEPOperator>(&GEP)));
2514-
replaceOperand(GEP, 0, Src->getOperand(0));
2515-
replaceOperand(GEP, 1, Sum);
2516-
return &GEP;
2517-
}
25182511
Indices.append(Src->op_begin()+1, Src->op_end()-1);
25192512
Indices.push_back(Sum);
25202513
Indices.append(GEP.op_begin()+2, GEP.op_end());

llvm/test/Transforms/InstCombine/getelementptr.ll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,5 +1905,17 @@ define ptr @gep_of_ashr_fail_not_divisible(ptr %p, i64 %x) {
19051905
ret ptr %r
19061906
}
19071907

1908+
define ptr @gep_merge_not_nuw(ptr %p, i64 %idx) {
1909+
; CHECK-LABEL: @gep_merge_not_nuw(
1910+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, ptr [[P:%.*]], i64 -1
1911+
; CHECK-NEXT: ret ptr [[GEP]]
1912+
;
1913+
%idx.neg = sub i64 0, %idx
1914+
%add = add i64 %idx, -1
1915+
%gep1 = getelementptr inbounds i8, ptr %p, i64 %idx.neg
1916+
%gep = getelementptr inbounds nuw i8, ptr %gep1, i64 %add
1917+
ret ptr %gep
1918+
}
1919+
19081920

19091921
!0 = !{!"branch_weights", i32 2, i32 10}

0 commit comments

Comments
 (0)