max-trip-count.ll 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. ; RUN: opt < %s -analyze -scalar-evolution | FileCheck %s
  2. ; ScalarEvolution should be able to understand the loop and eliminate the casts.
  3. ; CHECK: {%d,+,4}
  4. define void @foo(i32* nocapture %d, i32 %n) nounwind {
  5. entry:
  6. %0 = icmp sgt i32 %n, 0 ; <i1> [#uses=1]
  7. br i1 %0, label %bb.nph, label %return
  8. bb.nph: ; preds = %entry
  9. br label %bb
  10. bb: ; preds = %bb1, %bb.nph
  11. %i.02 = phi i32 [ %5, %bb1 ], [ 0, %bb.nph ] ; <i32> [#uses=2]
  12. %p.01 = phi i8 [ %4, %bb1 ], [ -1, %bb.nph ] ; <i8> [#uses=2]
  13. %1 = sext i8 %p.01 to i32 ; <i32> [#uses=1]
  14. %2 = sext i32 %i.02 to i64 ; <i64> [#uses=1]
  15. %3 = getelementptr i32, i32* %d, i64 %2 ; <i32*> [#uses=1]
  16. store i32 %1, i32* %3, align 4
  17. %4 = add i8 %p.01, 1 ; <i8> [#uses=1]
  18. %5 = add i32 %i.02, 1 ; <i32> [#uses=2]
  19. br label %bb1
  20. bb1: ; preds = %bb
  21. %6 = icmp slt i32 %5, %n ; <i1> [#uses=1]
  22. br i1 %6, label %bb, label %bb1.return_crit_edge
  23. bb1.return_crit_edge: ; preds = %bb1
  24. br label %return
  25. return: ; preds = %bb1.return_crit_edge, %entry
  26. ret void
  27. }
  28. ; ScalarEvolution should be able to find the maximum tripcount
  29. ; of this multiple-exit loop, and if it doesn't know the exact
  30. ; count, it should say so.
  31. ; PR7845
  32. ; CHECK: Loop %for.cond: <multiple exits> Unpredictable backedge-taken count.
  33. ; CHECK: Loop %for.cond: max backedge-taken count is 5
  34. @.str = private constant [4 x i8] c"%d\0A\00" ; <[4 x i8]*> [#uses=2]
  35. define i32 @main() nounwind {
  36. entry:
  37. br label %for.cond
  38. for.cond: ; preds = %for.inc, %entry
  39. %g_4.0 = phi i32 [ 0, %entry ], [ %add, %for.inc ] ; <i32> [#uses=5]
  40. %cmp = icmp slt i32 %g_4.0, 5 ; <i1> [#uses=1]
  41. br i1 %cmp, label %for.body, label %for.end
  42. for.body: ; preds = %for.cond
  43. %conv = trunc i32 %g_4.0 to i16 ; <i16> [#uses=1]
  44. %tobool.not = icmp eq i16 %conv, 0 ; <i1> [#uses=1]
  45. %tobool3 = icmp ne i32 %g_4.0, 0 ; <i1> [#uses=1]
  46. %or.cond = and i1 %tobool.not, %tobool3 ; <i1> [#uses=1]
  47. br i1 %or.cond, label %for.end, label %for.inc
  48. for.inc: ; preds = %for.body
  49. %add = add nsw i32 %g_4.0, 1 ; <i32> [#uses=1]
  50. br label %for.cond
  51. for.end: ; preds = %for.body, %for.cond
  52. %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %g_4.0) nounwind ; <i32> [#uses=0]
  53. ret i32 0
  54. }
  55. declare i32 @printf(i8*, ...)
  56. define void @test(i8* %a, i32 %n) nounwind {
  57. entry:
  58. %cmp1 = icmp sgt i32 %n, 0
  59. br i1 %cmp1, label %for.body.lr.ph, label %for.end
  60. for.body.lr.ph: ; preds = %entry
  61. %tmp = zext i32 %n to i64
  62. br label %for.body
  63. for.body: ; preds = %for.body, %for.body.lr.ph
  64. %indvar = phi i64 [ %indvar.next, %for.body ], [ 0, %for.body.lr.ph ]
  65. %arrayidx = getelementptr i8, i8* %a, i64 %indvar
  66. store i8 0, i8* %arrayidx, align 1
  67. %indvar.next = add i64 %indvar, 1
  68. %exitcond = icmp ne i64 %indvar.next, %tmp
  69. br i1 %exitcond, label %for.body, label %for.cond.for.end_crit_edge
  70. for.cond.for.end_crit_edge: ; preds = %for.body
  71. br label %for.end
  72. for.end: ; preds = %for.cond.for.end_crit_edge, %entry
  73. ret void
  74. }
  75. ; CHECK: Determining loop execution counts for: @test
  76. ; CHECK-NEXT: backedge-taken count is
  77. ; CHECK-NEXT: max backedge-taken count is -1
  78. ; PR19799: Indvars miscompile due to an incorrect max backedge taken count from SCEV.
  79. ; CHECK-LABEL: @pr19799
  80. ; CHECK: Loop %for.body.i: <multiple exits> Unpredictable backedge-taken count.
  81. ; CHECK: Loop %for.body.i: max backedge-taken count is 1
  82. @a = common global i32 0, align 4
  83. define i32 @pr19799() {
  84. entry:
  85. store i32 -1, i32* @a, align 4
  86. br label %for.body.i
  87. for.body.i: ; preds = %for.cond.i, %entry
  88. %storemerge1.i = phi i32 [ -1, %entry ], [ %add.i.i, %for.cond.i ]
  89. %tobool.i = icmp eq i32 %storemerge1.i, 0
  90. %add.i.i = add nsw i32 %storemerge1.i, 2
  91. br i1 %tobool.i, label %bar.exit, label %for.cond.i
  92. for.cond.i: ; preds = %for.body.i
  93. store i32 %add.i.i, i32* @a, align 4
  94. %cmp.i = icmp slt i32 %storemerge1.i, 0
  95. br i1 %cmp.i, label %for.body.i, label %bar.exit
  96. bar.exit: ; preds = %for.cond.i, %for.body.i
  97. ret i32 0
  98. }
  99. ; PR18886: Indvars miscompile due to an incorrect max backedge taken count from SCEV.
  100. ; CHECK-LABEL: @pr18886
  101. ; CHECK: Loop %for.body: <multiple exits> Unpredictable backedge-taken count.
  102. ; CHECK: Loop %for.body: max backedge-taken count is 3
  103. @aa = global i64 0, align 8
  104. define i32 @pr18886() {
  105. entry:
  106. store i64 -21, i64* @aa, align 8
  107. br label %for.body
  108. for.body:
  109. %storemerge1 = phi i64 [ -21, %entry ], [ %add, %for.cond ]
  110. %tobool = icmp eq i64 %storemerge1, 0
  111. %add = add nsw i64 %storemerge1, 8
  112. br i1 %tobool, label %return, label %for.cond
  113. for.cond:
  114. store i64 %add, i64* @aa, align 8
  115. %cmp = icmp slt i64 %add, 9
  116. br i1 %cmp, label %for.body, label %return
  117. return:
  118. %retval.0 = phi i32 [ 1, %for.body ], [ 0, %for.cond ]
  119. ret i32 %retval.0
  120. }
  121. ; Here we have a must-exit loop latch that is not computable and a
  122. ; may-exit early exit that can only have one non-exiting iteration
  123. ; before the check is forever skipped.
  124. ;
  125. ; CHECK-LABEL: @cannot_compute_mustexit
  126. ; CHECK: Loop %for.body.i: <multiple exits> Unpredictable backedge-taken count.
  127. ; CHECK: Loop %for.body.i: Unpredictable max backedge-taken count.
  128. @b = common global i32 0, align 4
  129. define i32 @cannot_compute_mustexit() {
  130. entry:
  131. store i32 -1, i32* @a, align 4
  132. br label %for.body.i
  133. for.body.i: ; preds = %for.cond.i, %entry
  134. %storemerge1.i = phi i32 [ -1, %entry ], [ %add.i.i, %for.cond.i ]
  135. %tobool.i = icmp eq i32 %storemerge1.i, 0
  136. %add.i.i = add nsw i32 %storemerge1.i, 2
  137. br i1 %tobool.i, label %bar.exit, label %for.cond.i
  138. for.cond.i: ; preds = %for.body.i
  139. store i32 %add.i.i, i32* @a, align 4
  140. %ld = load volatile i32, i32* @b
  141. %cmp.i = icmp ne i32 %ld, 0
  142. br i1 %cmp.i, label %for.body.i, label %bar.exit
  143. bar.exit: ; preds = %for.cond.i, %for.body.i
  144. ret i32 0
  145. }
  146. ; This loop has two must-exits, both of which dominate the latch. The
  147. ; MaxBECount should be the minimum of them.
  148. ;
  149. ; CHECK-LABEL: @two_mustexit
  150. ; CHECK: Loop %for.body.i: <multiple exits> Unpredictable backedge-taken count.
  151. ; CHECK: Loop %for.body.i: max backedge-taken count is 1
  152. define i32 @two_mustexit() {
  153. entry:
  154. store i32 -1, i32* @a, align 4
  155. br label %for.body.i
  156. for.body.i: ; preds = %for.cond.i, %entry
  157. %storemerge1.i = phi i32 [ -1, %entry ], [ %add.i.i, %for.cond.i ]
  158. %tobool.i = icmp sgt i32 %storemerge1.i, 0
  159. %add.i.i = add nsw i32 %storemerge1.i, 2
  160. br i1 %tobool.i, label %bar.exit, label %for.cond.i
  161. for.cond.i: ; preds = %for.body.i
  162. store i32 %add.i.i, i32* @a, align 4
  163. %cmp.i = icmp slt i32 %storemerge1.i, 3
  164. br i1 %cmp.i, label %for.body.i, label %bar.exit
  165. bar.exit: ; preds = %for.cond.i, %for.body.i
  166. ret i32 0
  167. }