[ScalarEvolution] Infer loop max trip count from array accesses


I have something unclear about calling memset for array storage, the case is showing as below.

// cat test.c

void foo(int *p);

void test (int len) {

int a[7];

for (int i = 0; i < len; i++) a[i] = 0;



; Function Attrs: nounwind

define void @test(i32 signext %len) {


%a = alloca [7 x i32], align 4

%0 = bitcast [7 x i32]* %a to i8*

%cmp4 = icmp sgt i32 %len, 0

for.body.preheader: ; preds = %entry

%1 = zext i32 %len to i64

%2 = shl nuw nsw i64 %1, 2

call void @llvm.memset.p0i8.i64(i8* nonnull align 4 %0, i8 0, i64 %2, i1 false)

br label %for.cond.cleanup

for.cond.cleanup: ; preds = %for.body.preheader, %entry

%arraydecay = getelementptr inbounds [7 x i32], [7 x i32]* %a, i64 0, i64 0

call void @foo(i32* nonnull %arraydecay) #4

ret void


For such a case, I want to get the exact loop upper bound, but now there is no API to help. So I post a patch about to infer loop bound from array accesses. And a modified patch was posted after some review comments.

For the implementation details, please comment on this patch : https://reviews.llvm.org/D109821

Any comment are precious to me, thank you all.