Skip to content

Commit 63e8102

Browse files
committed
[InstCombine] Fold getelementptr inbounds null, idx -> null
1 parent 989938f commit 63e8102

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2910,10 +2910,12 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
29102910
SQ.getWithInstruction(&GEP)))
29112911
return replaceInstUsesWith(GEP, V);
29122912

2913-
// // getelementptr inbounds null, idx -> null
2914-
// if (auto *BaseC = dyn_cast<Constant>(PtrOp))
2915-
// if (GEP.isInBounds() && BaseC->isNullValue() && !NullPointerIsDefined(GEP.getFunction(), GEPType->getPointerAddressSpace()))
2916-
// return replaceInstUsesWith(GEP, PtrOp);
2913+
// getelementptr inbounds null, idx -> null
2914+
if (auto *BaseC = dyn_cast<Constant>(PtrOp))
2915+
if (GEP.isInBounds() && BaseC->isNullValue() &&
2916+
!NullPointerIsDefined(GEP.getFunction(),
2917+
GEPType->getPointerAddressSpace()))
2918+
return replaceInstUsesWith(GEP, Constant::getNullValue(GEPType));
29172919

29182920
// For vector geps, use the generic demanded vector support.
29192921
// Skip if GEP return type is scalable. The number of elements is unknown at

llvm/test/Transforms/InstCombine/getelementptr.ll

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,8 +1328,7 @@ define ptr @PR45084_extra_use(i1 %cond, ptr %p) {
13281328

13291329
define ptr @gep_null_inbounds(i64 %idx) {
13301330
; CHECK-LABEL: @gep_null_inbounds(
1331-
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, ptr null, i64 [[IDX:%.*]]
1332-
; CHECK-NEXT: ret ptr [[GEP]]
1331+
; CHECK-NEXT: ret ptr null
13331332
;
13341333
%gep = getelementptr inbounds i8, ptr null, i64 %idx
13351334
ret ptr %gep
@@ -1355,8 +1354,7 @@ define ptr @gep_null_defined(i64 %idx) null_pointer_is_valid {
13551354

13561355
define ptr @gep_null_inbounds_different_type(i64 %idx1, i64 %idx2) {
13571356
; CHECK-LABEL: @gep_null_inbounds_different_type(
1358-
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [0 x i8], ptr null, i64 0, i64 [[IDX2:%.*]]
1359-
; CHECK-NEXT: ret ptr [[GEP]]
1357+
; CHECK-NEXT: ret ptr null
13601358
;
13611359
%gep = getelementptr inbounds [0 x i8], ptr null, i64 %idx1, i64 %idx2
13621360
ret ptr %gep
@@ -2021,17 +2019,15 @@ define ptr @gep_merge_nusw_const(ptr %p, i64 %idx, i64 %idx2) {
20212019

20222020
define <2 x ptr> @gep_inbounds_null_vec(i64 %idx) {
20232021
; CHECK-LABEL: @gep_inbounds_null_vec(
2024-
; CHECK-NEXT: [[P:%.*]] = getelementptr inbounds i8, <2 x ptr> zeroinitializer, i64 [[IDX:%.*]]
2025-
; CHECK-NEXT: ret <2 x ptr> [[P]]
2022+
; CHECK-NEXT: ret <2 x ptr> zeroinitializer
20262023
;
20272024
%p = getelementptr inbounds i8, <2 x ptr> zeroinitializer, i64 %idx
20282025
ret <2 x ptr> %p
20292026
}
20302027

20312028
define <2 x ptr> @gep_inbounds_null_vec_broadcast(<2 x i64> %idx) {
20322029
; CHECK-LABEL: @gep_inbounds_null_vec_broadcast(
2033-
; CHECK-NEXT: [[P:%.*]] = getelementptr inbounds i8, ptr null, <2 x i64> [[IDX:%.*]]
2034-
; CHECK-NEXT: ret <2 x ptr> [[P]]
2030+
; CHECK-NEXT: ret <2 x ptr> zeroinitializer
20352031
;
20362032
%p = getelementptr inbounds i8, ptr null, <2 x i64> %idx
20372033
ret <2 x ptr> %p

llvm/test/Transforms/InstCombine/store.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ define void @test2(ptr %P) {
4949

5050
define void @store_at_gep_off_null_inbounds(i64 %offset) {
5151
; CHECK-LABEL: @store_at_gep_off_null_inbounds(
52-
; CHECK-NEXT: [[PTR:%.*]] = getelementptr inbounds i32, ptr null, i64 [[OFFSET:%.*]]
53-
; CHECK-NEXT: store i32 poison, ptr [[PTR]], align 4
52+
; CHECK-NEXT: store i32 poison, ptr null, align 4
5453
; CHECK-NEXT: ret void
5554
;
5655
%ptr = getelementptr inbounds i32, ptr null, i64 %offset

0 commit comments

Comments
 (0)