[InstSimplify] Fold getelementptr inbounds null, idx -> null by dtcxzyw · Pull Request #130742 · llvm/llvm-project (original) (raw)

@llvm/pr-subscribers-llvm-analysis

@llvm/pr-subscribers-llvm-transforms

Author: Yingwei Zheng (dtcxzyw)

Changes

Proof: https://alive2.llvm.org/ce/z/5ZkPx-


Full diff: https://github.com/llvm/llvm-project/pull/130742.diff

4 Files Affected:

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 4c14dcfb4d75f..dcb17ee8d7b43 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -2910,6 +2910,13 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) { SQ.getWithInstruction(&GEP))) return replaceInstUsesWith(GEP, V);

-; CHECK-NEXT: [[GEP:%.]] = getelementptr inbounds i8, ptr null, i64 [[IDX:%.]] -; CHECK-NEXT: ret ptr [[GEP]] +; CHECK-NEXT: ret ptr null ; %gep = getelementptr inbounds i8, ptr null, i64 %idx ret ptr %gep @@ -1355,8 +1354,7 @@ define ptr @gep_null_defined(i64 %idx) null_pointer_is_valid {

define ptr @gep_null_inbounds_different_type(i64 %idx1, i64 %idx2) { ; CHECK-LABEL: @gep_null_inbounds_different_type( -; CHECK-NEXT: [[GEP:%.]] = getelementptr inbounds [0 x i8], ptr null, i64 0, i64 [[IDX2:%.]] -; CHECK-NEXT: ret ptr [[GEP]] +; CHECK-NEXT: ret ptr null ; %gep = getelementptr inbounds [0 x i8], ptr null, i64 %idx1, i64 %idx2 ret ptr %gep @@ -2019,5 +2017,38 @@ define ptr @gep_merge_nusw_const(ptr %p, i64 %idx, i64 %idx2) { ret ptr %gep }

+define <2 x ptr> @gep_inbounds_null_vec(i64 %idx) { +; CHECK-LABEL: @gep_inbounds_null_vec( +; CHECK-NEXT: ret <2 x ptr> zeroinitializer +;

!0 = !{!"branch_weights", i32 2, i32 10} diff --git a/llvm/test/Transforms/InstCombine/store.ll b/llvm/test/Transforms/InstCombine/store.ll index daa40da1828b5..48c63c6f24c72 100644 --- a/llvm/test/Transforms/InstCombine/store.ll +++ b/llvm/test/Transforms/InstCombine/store.ll @@ -49,8 +49,7 @@ define void @test2(ptr %P) {

define void @store_at_gep_off_null_inbounds(i64 %offset) { ; CHECK-LABEL: @store_at_gep_off_null_inbounds( -; CHECK-NEXT: [[PTR:%.]] = getelementptr inbounds i32, ptr null, i64 [[OFFSET:%.]] -; CHECK-NEXT: store i32 poison, ptr [[PTR]], align 4 +; CHECK-NEXT: store i32 poison, ptr null, align 4 ; CHECK-NEXT: ret void ; %ptr = getelementptr inbounds i32, ptr null, i64 %offset diff --git a/llvm/test/Transforms/InstCombine/sub-gep.ll b/llvm/test/Transforms/InstCombine/sub-gep.ll index 3f8728d3a4381..c86a1a37bd7ad 100644 --- a/llvm/test/Transforms/InstCombine/sub-gep.ll +++ b/llvm/test/Transforms/InstCombine/sub-gep.ll @@ -741,7 +741,7 @@ define i64 @nullptrtoint_scalable_c() { ; CHECK-NEXT: ret i64 [[PTR_IDX]] ; entry: