[llvm] 22ca38d - [ScalarEvolution] Analyze ranges for heap allocations

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 29 09:35:46 PDT 2023


Author: Arthur Eubanks
Date: 2023-06-29T09:35:20-07:00
New Revision: 22ca38da25e19a7c5fcfeb3f22159aba92ec381e

URL: https://github.com/llvm/llvm-project/commit/22ca38da25e19a7c5fcfeb3f22159aba92ec381e
DIFF: https://github.com/llvm/llvm-project/commit/22ca38da25e19a7c5fcfeb3f22159aba92ec381e.diff

LOG: [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

Added: 
    

Modified: 
    llvm/lib/Analysis/ScalarEvolution.cpp
    llvm/test/Analysis/ScalarEvolution/malloc.ll
    llvm/test/Transforms/PhaseOrdering/gep-null-compare-in-loop.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 2bce32df06bae..d4bbb4b467d13 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -6816,7 +6816,8 @@ const ConstantRange &ScalarEvolution::getRangeRef(
       Opts.RoundToAlign = false;
       Opts.NullIsUnknownSize = true;
       uint64_t ObjSize;
-      if ((isa<GlobalVariable>(V) || isa<AllocaInst>(V)) &&
+      if ((isa<GlobalVariable>(V) || isa<AllocaInst>(V) ||
+           isAllocationFn(V, &TLI)) &&
           getObjectSize(V, ObjSize, DL, &TLI, Opts) && ObjSize > 1) {
         // The highest address the object can start is ObjSize bytes before the
         // end (unsigned max value). If this value is not a multiple of the

diff  --git a/llvm/test/Analysis/ScalarEvolution/malloc.ll b/llvm/test/Analysis/ScalarEvolution/malloc.ll
index f2e373532063f..80d1d65c7981d 100644
--- a/llvm/test/Analysis/ScalarEvolution/malloc.ll
+++ b/llvm/test/Analysis/ScalarEvolution/malloc.ll
@@ -5,7 +5,7 @@ define ptr @f() {
 ; CHECK-LABEL: 'f'
 ; CHECK-NEXT:  Classifying expressions for: @f
 ; CHECK-NEXT:    %alloc = tail call dereferenceable(64) ptr @malloc(i64 64)
-; CHECK-NEXT:    --> %alloc U: full-set S: full-set
+; CHECK-NEXT:    --> %alloc U: [1,-64) S: full-set
 ; CHECK-NEXT:  Determining loop execution counts for: @f
 ;
   %alloc = tail call dereferenceable(64) ptr @malloc(i64 64)
@@ -16,7 +16,7 @@ define ptr @f2() {
 ; CHECK-LABEL: 'f2'
 ; CHECK-NEXT:  Classifying expressions for: @f2
 ; CHECK-NEXT:    %alloc = tail call dereferenceable_or_null(64) ptr @malloc(i64 64)
-; CHECK-NEXT:    --> %alloc U: full-set S: full-set
+; CHECK-NEXT:    --> %alloc U: [0,-64) S: full-set
 ; CHECK-NEXT:  Determining loop execution counts for: @f2
 ;
   %alloc = tail call dereferenceable_or_null(64) ptr @malloc(i64 64)

diff  --git a/llvm/test/Transforms/PhaseOrdering/gep-null-compare-in-loop.ll b/llvm/test/Transforms/PhaseOrdering/gep-null-compare-in-loop.ll
index 29b1717bffeac..a2d56f2f27925 100644
--- a/llvm/test/Transforms/PhaseOrdering/gep-null-compare-in-loop.ll
+++ b/llvm/test/Transforms/PhaseOrdering/gep-null-compare-in-loop.ll
@@ -60,20 +60,14 @@ define i32 @using_malloc() {
 ; CHECK-NEXT:    store i32 2, ptr [[GETELEMENTPTR]], align 4
 ; CHECK-NEXT:    [[GETELEMENTPTR1:%.*]] = getelementptr i32, ptr [[ALLOC]], i64 2
 ; CHECK-NEXT:    store i32 3, ptr [[GETELEMENTPTR1]], align 4
-; CHECK-NEXT:    [[GETELEMENTPTR2:%.*]] = getelementptr i32, ptr [[ALLOC]], i64 3
-; CHECK-NEXT:    br label [[BB11_I:%.*]]
-; CHECK:       bb11.i:
-; CHECK-NEXT:    [[PHI37_I:%.*]] = phi i32 [ [[ADD_I:%.*]], [[BB11_I]] ], [ 0, [[BB:%.*]] ]
-; CHECK-NEXT:    [[PHI6_I:%.*]] = phi ptr [ [[SPEC_SELECT_I:%.*]], [[BB11_I]] ], [ [[ALLOC]], [[BB]] ]
-; CHECK-NEXT:    [[SPEC_SELECT_I]] = getelementptr i32, ptr [[PHI6_I]], i64 1
-; CHECK-NEXT:    [[LOAD_I:%.*]] = load i32, ptr [[PHI6_I]], align 4
-; CHECK-NEXT:    [[ADD_I]] = add i32 [[LOAD_I]], [[PHI37_I]]
-; CHECK-NEXT:    [[ICMP4_I:%.*]] = icmp ne ptr [[SPEC_SELECT_I]], [[GETELEMENTPTR2]]
-; CHECK-NEXT:    [[ICMP102_I:%.*]] = icmp ne ptr [[SPEC_SELECT_I]], null
-; CHECK-NEXT:    [[ICMP10_NOT_I:%.*]] = and i1 [[ICMP102_I]], [[ICMP4_I]]
-; CHECK-NEXT:    br i1 [[ICMP10_NOT_I]], label [[BB11_I]], label [[LOOP_EXIT:%.*]]
-; CHECK:       loop.exit:
-; CHECK-NEXT:    ret i32 [[ADD_I]]
+; CHECK-NEXT:    [[SPEC_SELECT_I:%.*]] = getelementptr i32, ptr [[ALLOC]], i64 1
+; CHECK-NEXT:    [[LOAD_I:%.*]] = load i32, ptr [[ALLOC]], align 4
+; CHECK-NEXT:    [[SPEC_SELECT_I_1:%.*]] = getelementptr i32, ptr [[ALLOC]], i64 2
+; CHECK-NEXT:    [[LOAD_I_1:%.*]] = load i32, ptr [[SPEC_SELECT_I]], align 4
+; CHECK-NEXT:    [[ADD_I_1:%.*]] = add i32 [[LOAD_I_1]], [[LOAD_I]]
+; CHECK-NEXT:    [[LOAD_I_2:%.*]] = load i32, ptr [[SPEC_SELECT_I_1]], align 4
+; CHECK-NEXT:    [[ADD_I_2:%.*]] = add i32 [[LOAD_I_2]], [[ADD_I_1]]
+; CHECK-NEXT:    ret i32 [[ADD_I_2]]
 ;
 bb:
   %alloc = call dereferenceable_or_null(64) ptr @malloc(i64 64)


        


More information about the llvm-commits mailing list