Skip to content

Commit aaec1d0

Browse files
committed
[Analysis] Teach ScalarEvolution::getRangeRef about more dereferenceable objects
Whilst dealing with review comments on llvm#96752 I discovered that SCEV does not know about the dereferenceable attribute on function arguments so I have updated getRangeRef to make use of it by calling getPointerDereferenceableBytes.
1 parent 816068e commit aaec1d0

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6858,10 +6858,9 @@ const ConstantRange &ScalarEvolution::getRangeRef(
68586858
ObjectSizeOpts Opts;
68596859
Opts.RoundToAlign = false;
68606860
Opts.NullIsUnknownSize = true;
6861-
uint64_t ObjSize;
6862-
if ((isa<GlobalVariable>(V) || isa<AllocaInst>(V) ||
6863-
isAllocationFn(V, &TLI)) &&
6864-
getObjectSize(V, ObjSize, DL, &TLI, Opts) && ObjSize > 1) {
6861+
bool CanBeNull, CanBeFreed;
6862+
uint64_t ObjSize = V->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed);
6863+
if (ObjSize > 1) {
68656864
// The highest address the object can start is ObjSize bytes before the
68666865
// end (unsigned max value). If this value is not a multiple of the
68676866
// alignment, the last possible start value is the next lowest multiple

llvm/test/Analysis/ScalarEvolution/different-loops-recs.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ define void @test_05(i32 %N) {
457457
; CHECK-NEXT: %"alloca point" = bitcast i32 0 to i32
458458
; CHECK-NEXT: --> 0 U: [0,1) S: [0,1)
459459
; CHECK-NEXT: %tmp = getelementptr [1000 x i32], ptr @A, i32 0, i32 %i.0
460-
; CHECK-NEXT: --> {(8 + @A)<nuw><nsw>,+,4}<nw><%bb3> U: [0,-3) S: [-9223372036854775808,9223372036854775805) Exits: (408 + @A) LoopDispositions: { %bb3: Computable }
460+
; CHECK-NEXT: --> {(8 + @A)<nuw><nsw>,+,4}<nw><%bb3> U: [40,-3623) S: [-9223372036854775808,9223372036854775805) Exits: (408 + @A)<nuw> LoopDispositions: { %bb3: Computable }
461461
; CHECK-NEXT: %tmp2 = add i32 %i.0, 1
462462
; CHECK-NEXT: --> {3,+,1}<nuw><nsw><%bb3> U: [3,104) S: [3,104) Exits: 103 LoopDispositions: { %bb3: Computable }
463463
; CHECK-NEXT: %i.0 = phi i32 [ 2, %entry ], [ %tmp2, %bb ]

llvm/test/Analysis/ScalarEvolution/no-wrap-add-exprs.ll

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,15 @@ define void @f3(ptr %x_addr, ptr %y_addr, ptr %tmp_addr) {
183183
; CHECK-NEXT: %s3.zext = zext i8 %s3 to i16
184184
; CHECK-NEXT: --> (1 + (zext i8 (4 + (32 * %x) + (36 * %y)) to i16))<nuw><nsw> U: [1,254) S: [1,257)
185185
; CHECK-NEXT: %ptr = bitcast ptr @z_addr to ptr
186-
; CHECK-NEXT: --> @z_addr U: [0,-3) S: [-9223372036854775808,9223372036854775805)
186+
; CHECK-NEXT: --> @z_addr U: [4,-19) S: [-9223372036854775808,9223372036854775805)
187187
; CHECK-NEXT: %int0 = ptrtoint ptr %ptr to i32
188188
; CHECK-NEXT: --> (trunc i64 (ptrtoint ptr @z_addr to i64) to i32) U: [0,-3) S: [-2147483648,2147483645)
189189
; CHECK-NEXT: %int5 = add i32 %int0, 5
190190
; CHECK-NEXT: --> (5 + (trunc i64 (ptrtoint ptr @z_addr to i64) to i32)) U: [5,2) S: [-2147483643,-2147483646)
191191
; CHECK-NEXT: %int.zext = zext i32 %int5 to i64
192192
; CHECK-NEXT: --> (1 + (zext i32 (4 + (trunc i64 (ptrtoint ptr @z_addr to i64) to i32)) to i64))<nuw><nsw> U: [1,4294967294) S: [1,4294967297)
193193
; CHECK-NEXT: %ptr_noalign = bitcast ptr @z_addr_noalign to ptr
194-
; CHECK-NEXT: --> @z_addr_noalign U: full-set S: full-set
194+
; CHECK-NEXT: --> @z_addr_noalign U: [1,-16) S: full-set
195195
; CHECK-NEXT: %int0_na = ptrtoint ptr %ptr_noalign to i32
196196
; CHECK-NEXT: --> (trunc i64 (ptrtoint ptr @z_addr_noalign to i64) to i32) U: full-set S: full-set
197197
; CHECK-NEXT: %int5_na = add i32 %int0_na, 5
@@ -362,3 +362,20 @@ loop:
362362
exit2:
363363
ret i1 false
364364
}
365+
366+
367+
define void @dereferenceable_arg(ptr dereferenceable(128) %len_addr, ptr dereferenceable(128) align(8) %len_addr2) {
368+
; CHECK-LABEL: 'dereferenceable_arg'
369+
; CHECK-NEXT: Classifying expressions for: @dereferenceable_arg
370+
; CHECK-NEXT: %ptr = bitcast ptr %len_addr to ptr
371+
; CHECK-NEXT: --> %len_addr U: [1,-128) S: full-set
372+
; CHECK-NEXT: %ptr2 = bitcast ptr %len_addr2 to ptr
373+
; CHECK-NEXT: --> %len_addr2 U: [8,-135) S: [-9223372036854775808,9223372036854775801)
374+
; CHECK-NEXT: Determining loop execution counts for: @dereferenceable_arg
375+
;
376+
entry:
377+
%ptr = bitcast ptr %len_addr to ptr
378+
%ptr2 = bitcast ptr %len_addr2 to ptr
379+
380+
ret void
381+
}

0 commit comments

Comments
 (0)