no-iv-rewrite.ll 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. ; RUN: opt < %s -indvars -S | FileCheck %s
  2. ;
  3. ; Make sure that indvars isn't inserting canonical IVs.
  4. ; This is kinda hard to do until linear function test replacement is removed.
  5. target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
  6. define i32 @sum(i32* %arr, i32 %n) nounwind {
  7. entry:
  8. %precond = icmp slt i32 0, %n
  9. br i1 %precond, label %ph, label %return
  10. ph:
  11. br label %loop
  12. ; CHECK: loop:
  13. ;
  14. ; We should only have 2 IVs.
  15. ; CHECK: phi
  16. ; CHECK: phi
  17. ; CHECK-NOT: phi
  18. ;
  19. ; sext should be eliminated while preserving gep inboundsness.
  20. ; CHECK-NOT: sext
  21. ; CHECK: getelementptr inbounds
  22. ; CHECK: exit:
  23. loop:
  24. %i.02 = phi i32 [ 0, %ph ], [ %iinc, %loop ]
  25. %s.01 = phi i32 [ 0, %ph ], [ %sinc, %loop ]
  26. %ofs = sext i32 %i.02 to i64
  27. %adr = getelementptr inbounds i32, i32* %arr, i64 %ofs
  28. %val = load i32, i32* %adr
  29. %sinc = add nsw i32 %s.01, %val
  30. %iinc = add nsw i32 %i.02, 1
  31. %cond = icmp slt i32 %iinc, %n
  32. br i1 %cond, label %loop, label %exit
  33. exit:
  34. %s.lcssa = phi i32 [ %sinc, %loop ]
  35. br label %return
  36. return:
  37. %s.0.lcssa = phi i32 [ %s.lcssa, %exit ], [ 0, %entry ]
  38. ret i32 %s.0.lcssa
  39. }
  40. define i64 @suml(i32* %arr, i32 %n) nounwind {
  41. entry:
  42. %precond = icmp slt i32 0, %n
  43. br i1 %precond, label %ph, label %return
  44. ph:
  45. br label %loop
  46. ; CHECK: loop:
  47. ;
  48. ; We should only have 2 IVs.
  49. ; CHECK: phi
  50. ; CHECK: phi
  51. ; CHECK-NOT: phi
  52. ;
  53. ; %ofs sext should be eliminated while preserving gep inboundsness.
  54. ; CHECK-NOT: sext
  55. ; CHECK: getelementptr inbounds
  56. ; %vall sext should obviously not be eliminated
  57. ; CHECK: sext
  58. ; CHECK: exit:
  59. loop:
  60. %i.02 = phi i32 [ 0, %ph ], [ %iinc, %loop ]
  61. %s.01 = phi i64 [ 0, %ph ], [ %sinc, %loop ]
  62. %ofs = sext i32 %i.02 to i64
  63. %adr = getelementptr inbounds i32, i32* %arr, i64 %ofs
  64. %val = load i32, i32* %adr
  65. %vall = sext i32 %val to i64
  66. %sinc = add nsw i64 %s.01, %vall
  67. %iinc = add nsw i32 %i.02, 1
  68. %cond = icmp slt i32 %iinc, %n
  69. br i1 %cond, label %loop, label %exit
  70. exit:
  71. %s.lcssa = phi i64 [ %sinc, %loop ]
  72. br label %return
  73. return:
  74. %s.0.lcssa = phi i64 [ %s.lcssa, %exit ], [ 0, %entry ]
  75. ret i64 %s.0.lcssa
  76. }
  77. define void @outofbounds(i32* %first, i32* %last, i32 %idx) nounwind {
  78. %precond = icmp ne i32* %first, %last
  79. br i1 %precond, label %ph, label %return
  80. ; CHECK: ph:
  81. ; It's not indvars' job to perform LICM on %ofs
  82. ; CHECK-NOT: sext
  83. ph:
  84. br label %loop
  85. ; CHECK: loop:
  86. ;
  87. ; Preserve exactly one pointer type IV.
  88. ; CHECK: phi i32*
  89. ; CHECK-NOT: phi
  90. ;
  91. ; Don't create any extra adds.
  92. ; CHECK-NOT: add
  93. ;
  94. ; Preserve gep inboundsness, and don't factor it.
  95. ; CHECK: getelementptr inbounds i32, i32* %ptriv, i32 1
  96. ; CHECK-NOT: add
  97. ; CHECK: exit:
  98. loop:
  99. %ptriv = phi i32* [ %first, %ph ], [ %ptrpost, %loop ]
  100. %ofs = sext i32 %idx to i64
  101. %adr = getelementptr inbounds i32, i32* %ptriv, i64 %ofs
  102. store i32 3, i32* %adr
  103. %ptrpost = getelementptr inbounds i32, i32* %ptriv, i32 1
  104. %cond = icmp ne i32* %ptrpost, %last
  105. br i1 %cond, label %loop, label %exit
  106. exit:
  107. br label %return
  108. return:
  109. ret void
  110. }
  111. %structI = type { i32 }
  112. define void @bitcastiv(i32 %start, i32 %limit, i32 %step, %structI* %base)
  113. nounwind
  114. {
  115. entry:
  116. br label %loop
  117. ; CHECK: loop:
  118. ;
  119. ; Preserve casts
  120. ; CHECK: phi i32
  121. ; CHECK: bitcast
  122. ; CHECK: getelementptr
  123. ; CHECK: exit:
  124. loop:
  125. %iv = phi i32 [%start, %entry], [%next, %loop]
  126. %p = phi %structI* [%base, %entry], [%pinc, %loop]
  127. %adr = getelementptr %structI, %structI* %p, i32 0, i32 0
  128. store i32 3, i32* %adr
  129. %pp = bitcast %structI* %p to i32*
  130. store i32 4, i32* %pp
  131. %pinc = getelementptr %structI, %structI* %p, i32 1
  132. %next = add i32 %iv, 1
  133. %cond = icmp ne i32 %next, %limit
  134. br i1 %cond, label %loop, label %exit
  135. exit:
  136. ret void
  137. }
  138. define void @maxvisitor(i32 %limit, i32* %base) nounwind {
  139. entry:
  140. br label %loop
  141. ; Test inserting a truncate at a phi use.
  142. ;
  143. ; CHECK: loop:
  144. ; CHECK: phi i64
  145. ; CHECK: trunc
  146. ; CHECK: exit:
  147. loop:
  148. %idx = phi i32 [ 0, %entry ], [ %idx.next, %loop.inc ]
  149. %max = phi i32 [ 0, %entry ], [ %max.next, %loop.inc ]
  150. %idxprom = sext i32 %idx to i64
  151. %adr = getelementptr inbounds i32, i32* %base, i64 %idxprom
  152. %val = load i32, i32* %adr
  153. %cmp19 = icmp sgt i32 %val, %max
  154. br i1 %cmp19, label %if.then, label %if.else
  155. if.then:
  156. br label %loop.inc
  157. if.else:
  158. br label %loop.inc
  159. loop.inc:
  160. %max.next = phi i32 [ %idx, %if.then ], [ %max, %if.else ]
  161. %idx.next = add nsw i32 %idx, 1
  162. %cmp = icmp slt i32 %idx.next, %limit
  163. br i1 %cmp, label %loop, label %exit
  164. exit:
  165. ret void
  166. }
  167. define void @identityphi(i32 %limit) nounwind {
  168. entry:
  169. br label %loop
  170. ; Test an edge case of removing an identity phi that directly feeds
  171. ; back to the loop iv.
  172. ;
  173. ; CHECK: loop:
  174. ; CHECK-NOT: phi
  175. ; CHECK: exit:
  176. loop:
  177. %iv = phi i32 [ 0, %entry], [ %iv.next, %control ]
  178. br i1 undef, label %if.then, label %control
  179. if.then:
  180. br label %control
  181. control:
  182. %iv.next = phi i32 [ %iv, %loop ], [ undef, %if.then ]
  183. %cmp = icmp slt i32 %iv.next, %limit
  184. br i1 %cmp, label %loop, label %exit
  185. exit:
  186. ret void
  187. }
  188. define i64 @cloneOr(i32 %limit, i64* %base) nounwind {
  189. entry:
  190. ; ensure that the loop can't overflow
  191. %halfLim = ashr i32 %limit, 2
  192. br label %loop
  193. ; This test originally checked that the OR instruction was cloned. Now the
  194. ; ScalarEvolution is able to understand the loop evolution and that '%iv' at the
  195. ; end of the loop is an even value. Thus '%val' is computed at the end of the
  196. ; loop and the OR instruction is replaced by an ADD keeping the result
  197. ; equivalent.
  198. ;
  199. ; CHECK: sext
  200. ; CHECK: loop:
  201. ; CHECK: phi i64
  202. ; CHECK-NOT: sext
  203. ; CHECK: icmp slt i64
  204. ; CHECK: exit:
  205. ; CHECK: add i64
  206. loop:
  207. %iv = phi i32 [ 0, %entry], [ %iv.next, %loop ]
  208. %t1 = sext i32 %iv to i64
  209. %adr = getelementptr i64, i64* %base, i64 %t1
  210. %val = load i64, i64* %adr
  211. %t2 = or i32 %iv, 1
  212. %t3 = sext i32 %t2 to i64
  213. %iv.next = add i32 %iv, 2
  214. %cmp = icmp slt i32 %iv.next, %halfLim
  215. br i1 %cmp, label %loop, label %exit
  216. exit:
  217. %result = and i64 %val, %t3
  218. ret i64 %result
  219. }
  220. ; The i induction variable looks like a wrap-around, but it really is just
  221. ; a simple affine IV. Make sure that indvars simplifies through.
  222. define i32 @indirectRecurrence() nounwind {
  223. entry:
  224. br label %loop
  225. ; ReplaceLoopExitValue should fold the return value to constant 9.
  226. ; CHECK: loop:
  227. ; CHECK: phi i32
  228. ; CHECK: ret i32 9
  229. loop:
  230. %j.0 = phi i32 [ 1, %entry ], [ %j.next, %cond_true ]
  231. %i.0 = phi i32 [ 0, %entry ], [ %j.0, %cond_true ]
  232. %tmp = icmp ne i32 %j.0, 10
  233. br i1 %tmp, label %cond_true, label %return
  234. cond_true:
  235. %j.next = add i32 %j.0, 1
  236. br label %loop
  237. return:
  238. ret i32 %i.0
  239. }
  240. ; Eliminate the congruent phis j, k, and l.
  241. ; Eliminate the redundant IV increments k.next and l.next.
  242. ; Two phis should remain, one starting at %init, and one at %init1.
  243. ; Two increments should remain, one by %step and one by %step1.
  244. ; CHECK: loop:
  245. ; CHECK: phi i32
  246. ; CHECK: phi i32
  247. ; CHECK-NOT: phi
  248. ; CHECK: add i32
  249. ; CHECK: add i32
  250. ; CHECK: add i32
  251. ; CHECK-NOT: add
  252. ; CHECK: return:
  253. ;
  254. ; Five live-outs should remain.
  255. ; CHECK: lcssa = phi
  256. ; CHECK: lcssa = phi
  257. ; CHECK: lcssa = phi
  258. ; CHECK: lcssa = phi
  259. ; CHECK: lcssa = phi
  260. ; CHECK-NOT: phi
  261. ; CHECK: ret
  262. define i32 @isomorphic(i32 %init, i32 %step, i32 %lim) nounwind {
  263. entry:
  264. %step1 = add i32 %step, 1
  265. %init1 = add i32 %init, %step1
  266. %l.0 = sub i32 %init1, %step1
  267. br label %loop
  268. loop:
  269. %ii = phi i32 [ %init1, %entry ], [ %ii.next, %loop ]
  270. %i = phi i32 [ %init, %entry ], [ %ii, %loop ]
  271. %j = phi i32 [ %init, %entry ], [ %j.next, %loop ]
  272. %k = phi i32 [ %init1, %entry ], [ %k.next, %loop ]
  273. %l = phi i32 [ %l.0, %entry ], [ %l.next, %loop ]
  274. %ii.next = add i32 %ii, %step1
  275. %j.next = add i32 %j, %step1
  276. %k.next = add i32 %k, %step1
  277. %l.step = add i32 %l, %step
  278. %l.next = add i32 %l.step, 1
  279. %cmp = icmp ne i32 %ii.next, %lim
  280. br i1 %cmp, label %loop, label %return
  281. return:
  282. %sum1 = add i32 %i, %j.next
  283. %sum2 = add i32 %sum1, %k.next
  284. %sum3 = add i32 %sum1, %l.step
  285. %sum4 = add i32 %sum1, %l.next
  286. ret i32 %sum4
  287. }
  288. ; Test a GEP IV that is derived from another GEP IV by a nop gep that
  289. ; lowers the type without changing the expression.
  290. %structIF = type { i32, float }
  291. define void @congruentgepiv(%structIF* %base) nounwind uwtable ssp {
  292. entry:
  293. %first = getelementptr inbounds %structIF, %structIF* %base, i64 0, i32 0
  294. br label %loop
  295. ; CHECK: loop:
  296. ; CHECK: phi %structIF*
  297. ; CHECK-NOT: phi
  298. ; CHECK: getelementptr inbounds
  299. ; CHECK-NOT: getelementptr
  300. ; CHECK: exit:
  301. loop:
  302. %ptr.iv = phi %structIF* [ %ptr.inc, %latch ], [ %base, %entry ]
  303. %next = phi i32* [ %next.inc, %latch ], [ %first, %entry ]
  304. store i32 4, i32* %next
  305. br i1 undef, label %latch, label %exit
  306. latch: ; preds = %for.inc50.i
  307. %ptr.inc = getelementptr inbounds %structIF, %structIF* %ptr.iv, i64 1
  308. %next.inc = getelementptr inbounds %structIF, %structIF* %ptr.inc, i64 0, i32 0
  309. br label %loop
  310. exit:
  311. ret void
  312. }
  313. ; Test a widened IV that is used by a phi on different paths within the loop.
  314. ;
  315. ; CHECK: for.body:
  316. ; CHECK: phi i64
  317. ; CHECK: trunc i64
  318. ; CHECK: if.then:
  319. ; CHECK: for.inc:
  320. ; CHECK: phi i32
  321. ; CHECK: for.end:
  322. define void @phiUsesTrunc() nounwind {
  323. entry:
  324. br i1 undef, label %for.body, label %for.end
  325. for.body:
  326. %iv = phi i32 [ %inc, %for.inc ], [ 1, %entry ]
  327. br i1 undef, label %if.then, label %if.else
  328. if.then:
  329. br i1 undef, label %if.then33, label %for.inc
  330. if.then33:
  331. br label %for.inc
  332. if.else:
  333. br i1 undef, label %if.then97, label %for.inc
  334. if.then97:
  335. %idxprom100 = sext i32 %iv to i64
  336. br label %for.inc
  337. for.inc:
  338. %kmin.1 = phi i32 [ %iv, %if.then33 ], [ 0, %if.then ], [ %iv, %if.then97 ], [ 0, %if.else ]
  339. %inc = add nsw i32 %iv, 1
  340. br i1 undef, label %for.body, label %for.end
  341. for.end:
  342. ret void
  343. }