# [LLVMdev] determining the number of iteration of a loop

ether zhhb etherzhhb at gmail.com
Thu Apr 22 03:26:07 PDT 2010

```hi,

you could try the getBackedgeTakenCount function of ScalarEvolution.

opt -scalar-evolution -analyze
define void @f(i64* nocapture %a, i64 %N) nounwind {
entry:
%0 = icmp sgt i64 %N, 0                         ; <i1> [#uses=1]
br i1 %0, label %bb, label %return

bb:                                               ; preds = %bb, %entry
%1 = phi i64 [ 0, %entry ], [ %2, %bb ]         ; <i64> [#uses=3]
%scevgep = getelementptr i64* %a, i64 %1        ; <i64*> [#uses=1]
store i64 %1, i64* %scevgep, align 8
%2 = add nsw i64 %1, 1                          ; <i64> [#uses=2]
%exitcond = icmp eq i64 %2, %N                  ; <i1> [#uses=1]
br i1 %exitcond, label %return, label %bb

return:                                           ; preds = %bb, %entry
ret void
}
Printing analysis 'Scalar Evolution Analysis' for function 'f':
Classifying expressions for: @f
%0 = icmp sgt i64 %N, 0                         ; <i1> [#uses=1]
-->  %0
%1 = phi i64 [ 0, %entry ], [ %2, %bb ]         ; <i64> [#uses=3]
-->  {0,+,1}<%bb>             Exits: (-1 + %N)
%scevgep = getelementptr i64* %a, i64 %1        ; <i64*> [#uses=1]
-->  {%a,+,sizeof(i64)}<%bb>          Exits: (((-1 + %N) * sizeof(i64)) +
%a)
%2 = add nsw i64 %1, 1                          ; <i64> [#uses=2]
-->  {1,+,1}<%bb>             Exits: %N
%exitcond = icmp eq i64 %2, %N                  ; <i1> [#uses=1]
-->  %exitcond                Exits: <<Unknown>>
Determining loop execution counts for: @f
Loop %bb: backedge-taken count is (-1 + %N)
Loop %bb: max backedge-taken count is (-1 + %N)

--best regares
ether
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20100422/aa1edcca/attachment.html>
```