Skip to content

Commit 8dd9a13

Browse files
committed
[InstCombine] Preserve inbounds when merging with zero-index GEP (PR44423)
This addresses https://bugs.llvm.org/show_bug.cgi?id=44423. If one of the GEPs is inbounds and the other is zero-index, we can also preserve inbounds. Differential Revision: https://reviews.llvm.org/D72060
1 parent 8dbe2f0 commit 8dd9a13

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,15 @@ Instruction *InstCombiner::narrowMathIfNoOverflow(BinaryOperator &BO) {
16491649
return CastInst::Create(CastOpc, NarrowBO, BO.getType());
16501650
}
16511651

1652+
bool isMergedGEPInBounds(GEPOperator &GEP1, GEPOperator &GEP2) {
1653+
// At least one GEP must be inbounds.
1654+
if (!GEP1.isInBounds() && !GEP2.isInBounds())
1655+
return false;
1656+
1657+
return (GEP1.isInBounds() || GEP1.hasAllZeroIndices()) &&
1658+
(GEP2.isInBounds() || GEP2.hasAllZeroIndices());
1659+
}
1660+
16521661
Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
16531662
SmallVector<Value*, 8> Ops(GEP.op_begin(), GEP.op_end());
16541663
Type *GEPType = GEP.getType();
@@ -1922,7 +1931,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
19221931

19231932
// Update the GEP in place if possible.
19241933
if (Src->getNumOperands() == 2) {
1925-
GEP.setIsInBounds(GEP.isInBounds() && Src->isInBounds());
1934+
GEP.setIsInBounds(isMergedGEPInBounds(*Src, *cast<GEPOperator>(&GEP)));
19261935
GEP.setOperand(0, Src->getOperand(0));
19271936
GEP.setOperand(1, Sum);
19281937
return &GEP;
@@ -1939,7 +1948,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
19391948
}
19401949

19411950
if (!Indices.empty())
1942-
return GEP.isInBounds() && Src->isInBounds()
1951+
return isMergedGEPInBounds(*Src, *cast<GEPOperator>(&GEP))
19431952
? GetElementPtrInst::CreateInBounds(
19441953
Src->getSourceElementType(), Src->getOperand(0), Indices,
19451954
GEP.getName())

llvm/test/Transforms/InstCombine/getelementptr.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ define <2 x i32*> @PR32414(i32** %ptr) {
11711171

11721172
define i32* @test_bitcast_nzgep([1 x i32]* %base, i64 %idx) {
11731173
; CHECK-LABEL: @test_bitcast_nzgep(
1174-
; CHECK-NEXT: [[PTR:%.*]] = getelementptr [1 x i32], [1 x i32]* [[BASE:%.*]], i64 0, i64 [[IDX:%.*]]
1174+
; CHECK-NEXT: [[PTR:%.*]] = getelementptr inbounds [1 x i32], [1 x i32]* [[BASE:%.*]], i64 0, i64 [[IDX:%.*]]
11751175
; CHECK-NEXT: ret i32* [[PTR]]
11761176
;
11771177
%base2 = bitcast [1 x i32]* %base to i32*
@@ -1181,7 +1181,7 @@ define i32* @test_bitcast_nzgep([1 x i32]* %base, i64 %idx) {
11811181

11821182
define i32* @test_zgep_nzgep([1 x i32]* %base, i64 %idx) {
11831183
; CHECK-LABEL: @test_zgep_nzgep(
1184-
; CHECK-NEXT: [[PTR:%.*]] = getelementptr [1 x i32], [1 x i32]* [[BASE:%.*]], i64 0, i64 [[IDX:%.*]]
1184+
; CHECK-NEXT: [[PTR:%.*]] = getelementptr inbounds [1 x i32], [1 x i32]* [[BASE:%.*]], i64 0, i64 [[IDX:%.*]]
11851185
; CHECK-NEXT: ret i32* [[PTR]]
11861186
;
11871187
%base2 = getelementptr [1 x i32], [1 x i32]* %base, i64 0, i64 0
@@ -1191,7 +1191,7 @@ define i32* @test_zgep_nzgep([1 x i32]* %base, i64 %idx) {
11911191

11921192
define i32* @test_nzgep_zgep([1 x i32]* %base, i64 %idx) {
11931193
; CHECK-LABEL: @test_nzgep_zgep(
1194-
; CHECK-NEXT: [[PTR:%.*]] = getelementptr [1 x i32], [1 x i32]* [[BASE:%.*]], i64 [[IDX:%.*]], i64 0
1194+
; CHECK-NEXT: [[PTR:%.*]] = getelementptr inbounds [1 x i32], [1 x i32]* [[BASE:%.*]], i64 [[IDX:%.*]], i64 0
11951195
; CHECK-NEXT: ret i32* [[PTR]]
11961196
;
11971197
%base2 = getelementptr inbounds [1 x i32], [1 x i32]* %base, i64 %idx

0 commit comments

Comments
 (0)