Skip to content

Commit 22ca38d

Browse files
committed
[ScalarEvolution] Analyze ranges for heap allocations
Followup to D153624. Allows for better exit count calculations for loops checking heap allocations against null. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D154001
1 parent 110dfb4 commit 22ca38d

File tree

3 files changed

+12
-17
lines changed

3 files changed

+12
-17
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6816,7 +6816,8 @@ const ConstantRange &ScalarEvolution::getRangeRef(
68166816
Opts.RoundToAlign = false;
68176817
Opts.NullIsUnknownSize = true;
68186818
uint64_t ObjSize;
6819-
if ((isa<GlobalVariable>(V) || isa<AllocaInst>(V)) &&
6819+
if ((isa<GlobalVariable>(V) || isa<AllocaInst>(V) ||
6820+
isAllocationFn(V, &TLI)) &&
68206821
getObjectSize(V, ObjSize, DL, &TLI, Opts) && ObjSize > 1) {
68216822
// The highest address the object can start is ObjSize bytes before the
68226823
// end (unsigned max value). If this value is not a multiple of the

llvm/test/Analysis/ScalarEvolution/malloc.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ define ptr @f() {
55
; CHECK-LABEL: 'f'
66
; CHECK-NEXT: Classifying expressions for: @f
77
; CHECK-NEXT: %alloc = tail call dereferenceable(64) ptr @malloc(i64 64)
8-
; CHECK-NEXT: --> %alloc U: full-set S: full-set
8+
; CHECK-NEXT: --> %alloc U: [1,-64) S: full-set
99
; CHECK-NEXT: Determining loop execution counts for: @f
1010
;
1111
%alloc = tail call dereferenceable(64) ptr @malloc(i64 64)
@@ -16,7 +16,7 @@ define ptr @f2() {
1616
; CHECK-LABEL: 'f2'
1717
; CHECK-NEXT: Classifying expressions for: @f2
1818
; CHECK-NEXT: %alloc = tail call dereferenceable_or_null(64) ptr @malloc(i64 64)
19-
; CHECK-NEXT: --> %alloc U: full-set S: full-set
19+
; CHECK-NEXT: --> %alloc U: [0,-64) S: full-set
2020
; CHECK-NEXT: Determining loop execution counts for: @f2
2121
;
2222
%alloc = tail call dereferenceable_or_null(64) ptr @malloc(i64 64)

llvm/test/Transforms/PhaseOrdering/gep-null-compare-in-loop.ll

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,14 @@ define i32 @using_malloc() {
6060
; CHECK-NEXT: store i32 2, ptr [[GETELEMENTPTR]], align 4
6161
; CHECK-NEXT: [[GETELEMENTPTR1:%.*]] = getelementptr i32, ptr [[ALLOC]], i64 2
6262
; CHECK-NEXT: store i32 3, ptr [[GETELEMENTPTR1]], align 4
63-
; CHECK-NEXT: [[GETELEMENTPTR2:%.*]] = getelementptr i32, ptr [[ALLOC]], i64 3
64-
; CHECK-NEXT: br label [[BB11_I:%.*]]
65-
; CHECK: bb11.i:
66-
; CHECK-NEXT: [[PHI37_I:%.*]] = phi i32 [ [[ADD_I:%.*]], [[BB11_I]] ], [ 0, [[BB:%.*]] ]
67-
; CHECK-NEXT: [[PHI6_I:%.*]] = phi ptr [ [[SPEC_SELECT_I:%.*]], [[BB11_I]] ], [ [[ALLOC]], [[BB]] ]
68-
; CHECK-NEXT: [[SPEC_SELECT_I]] = getelementptr i32, ptr [[PHI6_I]], i64 1
69-
; CHECK-NEXT: [[LOAD_I:%.*]] = load i32, ptr [[PHI6_I]], align 4
70-
; CHECK-NEXT: [[ADD_I]] = add i32 [[LOAD_I]], [[PHI37_I]]
71-
; CHECK-NEXT: [[ICMP4_I:%.*]] = icmp ne ptr [[SPEC_SELECT_I]], [[GETELEMENTPTR2]]
72-
; CHECK-NEXT: [[ICMP102_I:%.*]] = icmp ne ptr [[SPEC_SELECT_I]], null
73-
; CHECK-NEXT: [[ICMP10_NOT_I:%.*]] = and i1 [[ICMP102_I]], [[ICMP4_I]]
74-
; CHECK-NEXT: br i1 [[ICMP10_NOT_I]], label [[BB11_I]], label [[LOOP_EXIT:%.*]]
75-
; CHECK: loop.exit:
76-
; CHECK-NEXT: ret i32 [[ADD_I]]
63+
; CHECK-NEXT: [[SPEC_SELECT_I:%.*]] = getelementptr i32, ptr [[ALLOC]], i64 1
64+
; CHECK-NEXT: [[LOAD_I:%.*]] = load i32, ptr [[ALLOC]], align 4
65+
; CHECK-NEXT: [[SPEC_SELECT_I_1:%.*]] = getelementptr i32, ptr [[ALLOC]], i64 2
66+
; CHECK-NEXT: [[LOAD_I_1:%.*]] = load i32, ptr [[SPEC_SELECT_I]], align 4
67+
; CHECK-NEXT: [[ADD_I_1:%.*]] = add i32 [[LOAD_I_1]], [[LOAD_I]]
68+
; CHECK-NEXT: [[LOAD_I_2:%.*]] = load i32, ptr [[SPEC_SELECT_I_1]], align 4
69+
; CHECK-NEXT: [[ADD_I_2:%.*]] = add i32 [[LOAD_I_2]], [[ADD_I_1]]
70+
; CHECK-NEXT: ret i32 [[ADD_I_2]]
7771
;
7872
bb:
7973
%alloc = call dereferenceable_or_null(64) ptr @malloc(i64 64)

0 commit comments

Comments
 (0)