Skip to content

Commit ee63502

Browse files
committed
[InstSimplify] Move fold into InstSimplify
1 parent 63e8102 commit ee63502

File tree

11 files changed

+75
-83
lines changed

11 files changed

+75
-83
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5041,6 +5041,14 @@ static Value *simplifyGEPInst(Type *SrcTy, Value *Ptr,
50415041
if (Q.isUndefValue(Ptr))
50425042
return UndefValue::get(GEPTy);
50435043

5044+
// getelementptr inbounds null, idx -> null
5045+
if (NW.isInBounds() && Q.IIQ.UseInstrInfo && Q.CxtI) {
5046+
if (auto *BaseC = dyn_cast<Constant>(Ptr))
5047+
if (BaseC->isNullValue() &&
5048+
!NullPointerIsDefined(Q.CxtI->getFunction(), AS))
5049+
return Constant::getNullValue(GEPTy);
5050+
}
5051+
50445052
bool IsScalableVec =
50455053
SrcTy->isScalableTy() || any_of(Indices, [](const Value *V) {
50465054
return isa<ScalableVectorType>(V->getType());

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2910,13 +2910,6 @@ 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() &&
2916-
!NullPointerIsDefined(GEP.getFunction(),
2917-
GEPType->getPointerAddressSpace()))
2918-
return replaceInstUsesWith(GEP, Constant::getNullValue(GEPType));
2919-
29202913
// For vector geps, use the generic demanded vector support.
29212914
// Skip if GEP return type is scalable. The number of elements is unknown at
29222915
// compile-time.

llvm/test/Analysis/MemoryDependenceAnalysis/InvariantLoad.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ alive:
135135

136136
; This is reduced test case catching regression in the first version of the
137137
; fix for invariant loads (https://reviews.llvm.org/D64405).
138-
define void @test4() {
138+
define void @test4() null_pointer_is_valid {
139139
; CHECK-LABEL: @test4(
140140
; CHECK-NEXT: entry:
141141
; CHECK-NEXT: [[TMP0:%.*]] = load float, ptr inttoptr (i64 8 to ptr), align 4

llvm/test/Analysis/ValueTracking/gep-negative-issue.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ target triple = "x86_64-unknown-linux-gnu"
55
%ArrayImpl = type { i64, ptr addrspace(100), [1 x i64], [1 x i64], [1 x i64], i64, i64, ptr addrspace(100), ptr addrspace(100), i8, i64 }
66
%_array = type { i64, ptr addrspace(100), i8 }
77

8-
define void @test(i64 %n_chpl) {
8+
define void @test(i64 %n_chpl) null_pointer_is_valid {
99
entry:
1010
; First section is some code
1111
%0 = getelementptr inbounds %_array, ptr null, i32 0, i32 1

llvm/test/CodeGen/AMDGPU/chain-hi-to-lo.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ bb:
383383
ret <2 x half> %result
384384
}
385385

386-
define <2 x half> @chain_hi_to_lo_flat() {
386+
define <2 x half> @chain_hi_to_lo_flat() null_pointer_is_valid {
387387
; GCN-LABEL: chain_hi_to_lo_flat:
388388
; GCN: ; %bb.0: ; %bb
389389
; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)

llvm/test/CodeGen/AMDGPU/memcpy-crash-issue63986.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
%"struct.__llvm_libc::rpc::Buffer" = type { [8 x i64] }
55

6-
define void @issue63986(i64 %0, i64 %idxprom) {
6+
define void @issue63986(i64 %0, i64 %idxprom) null_pointer_is_valid {
77
; CHECK-LABEL: issue63986:
88
; CHECK: ; %bb.0: ; %entry
99
; CHECK-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)

llvm/test/Transforms/GVN/constexpr-vector-constainsundef-crash-inseltpoison.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
; https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=24278
66

77
; Make sure we do not crash when dealing with a vector constant expression.
8-
define <4 x ptr> @test(ptr %ptr) {
8+
define <4 x ptr> @test(ptr %ptr) null_pointer_is_valid {
99
; CHECK-LABEL: @test(
1010
; CHECK-NEXT: entry:
1111
; CHECK-NEXT: [[L3:%.*]] = load i64, ptr [[PTR:%.*]], align 4

llvm/test/Transforms/GVN/constexpr-vector-constainsundef-crash.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
; https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=24278
66

77
; Make sure we do not crash when dealing with a vector constant expression.
8-
define <4 x ptr> @test(ptr %ptr) {
8+
define <4 x ptr> @test(ptr %ptr) null_pointer_is_valid {
99
; CHECK-LABEL: @test(
1010
; CHECK-NEXT: entry:
1111
; CHECK-NEXT: [[L3:%.*]] = load i64, ptr [[PTR:%.*]], align 4

llvm/test/Transforms/InstCombine/getelementptr.ll

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,40 +1326,6 @@ define ptr @PR45084_extra_use(i1 %cond, ptr %p) {
13261326
ret ptr %sel
13271327
}
13281328

1329-
define ptr @gep_null_inbounds(i64 %idx) {
1330-
; CHECK-LABEL: @gep_null_inbounds(
1331-
; CHECK-NEXT: ret ptr null
1332-
;
1333-
%gep = getelementptr inbounds i8, ptr null, i64 %idx
1334-
ret ptr %gep
1335-
}
1336-
1337-
define ptr @gep_null_not_inbounds(i64 %idx) {
1338-
; CHECK-LABEL: @gep_null_not_inbounds(
1339-
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr null, i64 [[IDX:%.*]]
1340-
; CHECK-NEXT: ret ptr [[GEP]]
1341-
;
1342-
%gep = getelementptr i8, ptr null, i64 %idx
1343-
ret ptr %gep
1344-
}
1345-
1346-
define ptr @gep_null_defined(i64 %idx) null_pointer_is_valid {
1347-
; CHECK-LABEL: @gep_null_defined(
1348-
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, ptr null, i64 [[IDX:%.*]]
1349-
; CHECK-NEXT: ret ptr [[GEP]]
1350-
;
1351-
%gep = getelementptr inbounds i8, ptr null, i64 %idx
1352-
ret ptr %gep
1353-
}
1354-
1355-
define ptr @gep_null_inbounds_different_type(i64 %idx1, i64 %idx2) {
1356-
; CHECK-LABEL: @gep_null_inbounds_different_type(
1357-
; CHECK-NEXT: ret ptr null
1358-
;
1359-
%gep = getelementptr inbounds [0 x i8], ptr null, i64 %idx1, i64 %idx2
1360-
ret ptr %gep
1361-
}
1362-
13631329
define ptr @D98588(ptr %c1, i64 %offset) {
13641330
; CHECK-LABEL: @D98588(
13651331
; CHECK-NEXT: [[C2_NEXT_IDX:%.*]] = shl nsw i64 [[OFFSET:%.*]], 3
@@ -2017,38 +1983,4 @@ define ptr @gep_merge_nusw_const(ptr %p, i64 %idx, i64 %idx2) {
20171983
ret ptr %gep
20181984
}
20191985

2020-
define <2 x ptr> @gep_inbounds_null_vec(i64 %idx) {
2021-
; CHECK-LABEL: @gep_inbounds_null_vec(
2022-
; CHECK-NEXT: ret <2 x ptr> zeroinitializer
2023-
;
2024-
%p = getelementptr inbounds i8, <2 x ptr> zeroinitializer, i64 %idx
2025-
ret <2 x ptr> %p
2026-
}
2027-
2028-
define <2 x ptr> @gep_inbounds_null_vec_broadcast(<2 x i64> %idx) {
2029-
; CHECK-LABEL: @gep_inbounds_null_vec_broadcast(
2030-
; CHECK-NEXT: ret <2 x ptr> zeroinitializer
2031-
;
2032-
%p = getelementptr inbounds i8, ptr null, <2 x i64> %idx
2033-
ret <2 x ptr> %p
2034-
}
2035-
2036-
define ptr @gep_noinbounds_null(i64 %idx) {
2037-
; CHECK-LABEL: @gep_noinbounds_null(
2038-
; CHECK-NEXT: [[P:%.*]] = getelementptr i8, ptr null, i64 [[IDX:%.*]]
2039-
; CHECK-NEXT: ret ptr [[P]]
2040-
;
2041-
%p = getelementptr i8, ptr null, i64 %idx
2042-
ret ptr %p
2043-
}
2044-
2045-
define ptr @gep_inbounds_null_null_is_valid(i64 %idx) null_pointer_is_valid {
2046-
; CHECK-LABEL: @gep_inbounds_null_null_is_valid(
2047-
; CHECK-NEXT: [[P:%.*]] = getelementptr inbounds i8, ptr null, i64 [[IDX:%.*]]
2048-
; CHECK-NEXT: ret ptr [[P]]
2049-
;
2050-
%p = getelementptr inbounds i8, ptr null, i64 %idx
2051-
ret ptr %p
2052-
}
2053-
20541986
!0 = !{!"branch_weights", i32 2, i32 10}

llvm/test/Transforms/InstSimplify/ConstProp/cast-vector.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
; "offsetof-like expression" case).
66
; This used to hit an assert due to not supporting vectors in
77
; llvm::ConstantFoldCastInstruction when handling ptrtoint.
8-
define <2 x i16> @test1() {
8+
define <2 x i16> @test1() null_pointer_is_valid {
99
; CHECK-LABEL: @test1(
1010
; CHECK-NEXT: entry:
1111
; CHECK-NEXT: ret <2 x i16> ptrtoint (<2 x ptr> getelementptr inbounds ([10 x i32], ptr null, <2 x i64> zeroinitializer, <2 x i64> <i64 5, i64 7>) to <2 x i16>)
@@ -20,7 +20,7 @@ entry:
2020
; "sizeof-like expression" case).
2121
; This used to hit an assert due to not supporting vectors in
2222
; llvm::ConstantFoldCastInstruction when handling ptrtoint.
23-
define <2 x i16> @test2() {
23+
define <2 x i16> @test2() null_pointer_is_valid {
2424
; CHECK-LABEL: @test2(
2525
; CHECK-NEXT: entry:
2626
; CHECK-NEXT: ret <2 x i16> ptrtoint (<2 x ptr> getelementptr (i32, ptr null, <2 x i64> <i64 5, i64 7>) to <2 x i16>)

llvm/test/Transforms/InstSimplify/gep.ll

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,62 @@ define i64 @gep_array_of_scalable_vectors_ptrdiff(ptr %ptr) {
386386
%diff = sub i64 %c2.int, %c1.int
387387
ret i64 %diff
388388
}
389+
390+
define ptr @gep_null_inbounds(i64 %idx) {
391+
; CHECK-LABEL: @gep_null_inbounds(
392+
; CHECK-NEXT: ret ptr null
393+
;
394+
%gep = getelementptr inbounds i8, ptr null, i64 %idx
395+
ret ptr %gep
396+
}
397+
398+
define ptr @gep_null_not_inbounds(i64 %idx) {
399+
; CHECK-LABEL: @gep_null_not_inbounds(
400+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr null, i64 [[IDX:%.*]]
401+
; CHECK-NEXT: ret ptr [[GEP]]
402+
;
403+
%gep = getelementptr i8, ptr null, i64 %idx
404+
ret ptr %gep
405+
}
406+
407+
define ptr @gep_null_defined(i64 %idx) null_pointer_is_valid {
408+
; CHECK-LABEL: @gep_null_defined(
409+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, ptr null, i64 [[IDX:%.*]]
410+
; CHECK-NEXT: ret ptr [[GEP]]
411+
;
412+
%gep = getelementptr inbounds i8, ptr null, i64 %idx
413+
ret ptr %gep
414+
}
415+
416+
define ptr @gep_null_inbounds_different_type(i64 %idx1, i64 %idx2) {
417+
; CHECK-LABEL: @gep_null_inbounds_different_type(
418+
; CHECK-NEXT: ret ptr null
419+
;
420+
%gep = getelementptr inbounds [0 x i8], ptr null, i64 %idx1, i64 %idx2
421+
ret ptr %gep
422+
}
423+
424+
define <2 x ptr> @gep_inbounds_null_vec(i64 %idx) {
425+
; CHECK-LABEL: @gep_inbounds_null_vec(
426+
; CHECK-NEXT: ret <2 x ptr> zeroinitializer
427+
;
428+
%p = getelementptr inbounds i8, <2 x ptr> zeroinitializer, i64 %idx
429+
ret <2 x ptr> %p
430+
}
431+
432+
define <2 x ptr> @gep_inbounds_null_vec_broadcast(<2 x i64> %idx) {
433+
; CHECK-LABEL: @gep_inbounds_null_vec_broadcast(
434+
; CHECK-NEXT: ret <2 x ptr> zeroinitializer
435+
;
436+
%p = getelementptr inbounds i8, ptr null, <2 x i64> %idx
437+
ret <2 x ptr> %p
438+
}
439+
440+
define ptr @gep_noinbounds_null(i64 %idx) {
441+
; CHECK-LABEL: @gep_noinbounds_null(
442+
; CHECK-NEXT: [[P:%.*]] = getelementptr i8, ptr null, i64 [[IDX:%.*]]
443+
; CHECK-NEXT: ret ptr [[P]]
444+
;
445+
%p = getelementptr i8, ptr null, i64 %idx
446+
ret ptr %p
447+
}

0 commit comments

Comments
 (0)