scevunroll.ll 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. ; RUN: opt < %s -S -indvars -loop-unroll -verify-loop-info | FileCheck %s
  2. ;
  3. ; Unit tests for loop unrolling using ScalarEvolution to compute trip counts.
  4. ;
  5. ; Indvars is run first to generate an "old" SCEV result. Some unit
  6. ; tests may check that SCEV is properly invalidated between passes.
  7. ; Completely unroll loops without a canonical IV.
  8. ;
  9. ; CHECK-LABEL: @sansCanonical(
  10. ; CHECK-NOT: phi
  11. ; CHECK-NOT: icmp
  12. ; CHECK: ret
  13. define i32 @sansCanonical(i32* %base) nounwind {
  14. entry:
  15. br label %while.body
  16. while.body:
  17. %iv = phi i64 [ 10, %entry ], [ %iv.next, %while.body ]
  18. %sum = phi i32 [ 0, %entry ], [ %sum.next, %while.body ]
  19. %iv.next = add i64 %iv, -1
  20. %adr = getelementptr inbounds i32, i32* %base, i64 %iv.next
  21. %tmp = load i32, i32* %adr, align 8
  22. %sum.next = add i32 %sum, %tmp
  23. %iv.narrow = trunc i64 %iv.next to i32
  24. %cmp.i65 = icmp sgt i32 %iv.narrow, 0
  25. br i1 %cmp.i65, label %while.body, label %exit
  26. exit:
  27. ret i32 %sum
  28. }
  29. ; SCEV unrolling properly handles loops with multiple exits. In this
  30. ; case, the computed trip count based on a canonical IV is *not* for a
  31. ; latch block. Canonical unrolling incorrectly unrolls it, but SCEV
  32. ; unrolling does not.
  33. ;
  34. ; CHECK-LABEL: @earlyLoopTest(
  35. ; CHECK: tail:
  36. ; CHECK-NOT: br
  37. ; CHECK: br i1 %cmp2, label %loop, label %exit2
  38. define i64 @earlyLoopTest(i64* %base) nounwind {
  39. entry:
  40. br label %loop
  41. loop:
  42. %iv = phi i64 [ 0, %entry ], [ %inc, %tail ]
  43. %s = phi i64 [ 0, %entry ], [ %s.next, %tail ]
  44. %adr = getelementptr i64, i64* %base, i64 %iv
  45. %val = load i64, i64* %adr
  46. %s.next = add i64 %s, %val
  47. %inc = add i64 %iv, 1
  48. %cmp = icmp ne i64 %inc, 4
  49. br i1 %cmp, label %tail, label %exit1
  50. tail:
  51. %cmp2 = icmp ne i64 %val, 0
  52. br i1 %cmp2, label %loop, label %exit2
  53. exit1:
  54. ret i64 %s
  55. exit2:
  56. ret i64 %s.next
  57. }
  58. ; SCEV properly unrolls multi-exit loops.
  59. ;
  60. ; CHECK-LABEL: @multiExit(
  61. ; CHECK: getelementptr i32, i32* %base, i32 10
  62. ; CHECK-NEXT: load i32, i32*
  63. ; CHECK: br i1 false, label %l2.10, label %exit1
  64. ; CHECK: l2.10:
  65. ; CHECK-NOT: br
  66. ; CHECK: ret i32
  67. define i32 @multiExit(i32* %base) nounwind {
  68. entry:
  69. br label %l1
  70. l1:
  71. %iv1 = phi i32 [ 0, %entry ], [ %inc1, %l2 ]
  72. %iv2 = phi i32 [ 0, %entry ], [ %inc2, %l2 ]
  73. %inc1 = add i32 %iv1, 1
  74. %inc2 = add i32 %iv2, 1
  75. %adr = getelementptr i32, i32* %base, i32 %iv1
  76. %val = load i32, i32* %adr
  77. %cmp1 = icmp slt i32 %iv1, 5
  78. br i1 %cmp1, label %l2, label %exit1
  79. l2:
  80. %cmp2 = icmp slt i32 %iv2, 10
  81. br i1 %cmp2, label %l1, label %exit2
  82. exit1:
  83. ret i32 1
  84. exit2:
  85. ret i32 %val
  86. }
  87. ; SCEV should not unroll a multi-exit loops unless the latch block has
  88. ; a known trip count, regardless of the early exit trip counts. The
  89. ; LoopUnroll utility uses this assumption to optimize the latch
  90. ; block's branch.
  91. ;
  92. ; CHECK-LABEL: @multiExitIncomplete(
  93. ; CHECK: l3:
  94. ; CHECK-NOT: br
  95. ; CHECK: br i1 %cmp3, label %l1, label %exit3
  96. define i32 @multiExitIncomplete(i32* %base) nounwind {
  97. entry:
  98. br label %l1
  99. l1:
  100. %iv1 = phi i32 [ 0, %entry ], [ %inc1, %l3 ]
  101. %iv2 = phi i32 [ 0, %entry ], [ %inc2, %l3 ]
  102. %inc1 = add i32 %iv1, 1
  103. %inc2 = add i32 %iv2, 1
  104. %adr = getelementptr i32, i32* %base, i32 %iv1
  105. %val = load i32, i32* %adr
  106. %cmp1 = icmp slt i32 %iv1, 5
  107. br i1 %cmp1, label %l2, label %exit1
  108. l2:
  109. %cmp2 = icmp slt i32 %iv2, 10
  110. br i1 %cmp2, label %l3, label %exit2
  111. l3:
  112. %cmp3 = icmp ne i32 %val, 0
  113. br i1 %cmp3, label %l1, label %exit3
  114. exit1:
  115. ret i32 1
  116. exit2:
  117. ret i32 2
  118. exit3:
  119. ret i32 3
  120. }
  121. ; When loop unroll merges a loop exit with one of its parent loop's
  122. ; exits, SCEV must forget its ExitNotTaken info.
  123. ;
  124. ; CHECK-LABEL: @nestedUnroll(
  125. ; CHECK-NOT: br i1
  126. ; CHECK: for.body87:
  127. define void @nestedUnroll() nounwind {
  128. entry:
  129. br label %for.inc
  130. for.inc:
  131. br i1 false, label %for.inc, label %for.body38.preheader
  132. for.body38.preheader:
  133. br label %for.body38
  134. for.body38:
  135. %i.113 = phi i32 [ %inc76, %for.inc74 ], [ 0, %for.body38.preheader ]
  136. %mul48 = mul nsw i32 %i.113, 6
  137. br label %for.body43
  138. for.body43:
  139. %j.011 = phi i32 [ 0, %for.body38 ], [ %inc72, %for.body43 ]
  140. %add49 = add nsw i32 %j.011, %mul48
  141. %sh_prom50 = zext i32 %add49 to i64
  142. %inc72 = add nsw i32 %j.011, 1
  143. br i1 false, label %for.body43, label %for.inc74
  144. for.inc74:
  145. %inc76 = add nsw i32 %i.113, 1
  146. br i1 false, label %for.body38, label %for.body87.preheader
  147. for.body87.preheader:
  148. br label %for.body87
  149. for.body87:
  150. br label %for.body87
  151. }
  152. ; PR16130: clang produces incorrect code with loop/expression at -O2
  153. ; rdar:14036816 loop-unroll makes assumptions about undefined behavior
  154. ;
  155. ; The loop latch is assumed to exit after the first iteration because
  156. ; of the induction variable's NSW flag. However, the loop latch's
  157. ; equality test is skipped and the loop exits after the second
  158. ; iteration via the early exit. So loop unrolling cannot assume that
  159. ; the loop latch's exit count of zero is an upper bound on the number
  160. ; of iterations.
  161. ;
  162. ; CHECK-LABEL: @nsw_latch(
  163. ; CHECK: for.body:
  164. ; CHECK: %b.03 = phi i32 [ 0, %entry ], [ %add, %for.cond ]
  165. ; CHECK: return:
  166. ; CHECK: %b.03.lcssa = phi i32 [ %b.03, %for.body ], [ %b.03, %for.cond ]
  167. define void @nsw_latch(i32* %a) nounwind {
  168. entry:
  169. br label %for.body
  170. for.body: ; preds = %for.cond, %entry
  171. %b.03 = phi i32 [ 0, %entry ], [ %add, %for.cond ]
  172. %tobool = icmp eq i32 %b.03, 0
  173. %add = add nsw i32 %b.03, 8
  174. br i1 %tobool, label %for.cond, label %return
  175. for.cond: ; preds = %for.body
  176. %cmp = icmp eq i32 %add, 13
  177. br i1 %cmp, label %return, label %for.body
  178. return: ; preds = %for.body, %for.cond
  179. %b.03.lcssa = phi i32 [ %b.03, %for.body ], [ %b.03, %for.cond ]
  180. %retval.0 = phi i32 [ 1, %for.body ], [ 0, %for.cond ]
  181. store i32 %b.03.lcssa, i32* %a, align 4
  182. ret void
  183. }