[PATCH] D155049: [ScalarEvolution] Infer loop max trip count from memory accesses

Liren.Peng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 18 06:42:22 PDT 2023


Peakulorain added a comment.

In D155049#4509641 <https://reviews.llvm.org/D155049#4509641>, @nikic wrote:

> Could you please explain at a high level why we need all this custom wrapping logic? Why are the no-wrap flags on the AddRec insufficient?

The purpose of using the logic I implemented myself is to focus on calculating  **how many iterations** the index of GEP will wrap. With this value, we can know if the loop will fall into an infinite loop.

  for.body:
    %iv = phi i8 [ %inc, %for.body ], [ 0, %for.body.preheader ]
    %idxprom = zext i8 %iv to i64
    %arrayidx = getelementptr inbounds [500 x i32], [500 x i32]* %a, i64 0, i64 %idxprom
    store i32 0, i32* %arrayidx, align 4
    %inc = add i8 %iv, 1
    %inc_zext = zext i8 %inc to i32
    %cmp = icmp ult i32 %inc_zext, %len
    br i1 %cmp, label %for.body, label %loopexit

If the value of `%len` (which comes from argument) is greater than the maximum value that **i8** can represent, loop falls into infinite, but the **store** access is wandering in a fixed area without UB.  This is done to ensure that in this case the inference is correct.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155049/new/

https://reviews.llvm.org/D155049



More information about the llvm-commits mailing list