reduction.ll 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. ; RUN: opt < %s -loop-vectorize -force-vector-interleave=1 -force-vector-width=4 -dce -instcombine -S | FileCheck %s
  2. 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-S128"
  3. target triple = "x86_64-apple-macosx10.8.0"
  4. ;CHECK-LABEL: @reduction_sum(
  5. ;CHECK: phi <4 x i32>
  6. ;CHECK: load <4 x i32>
  7. ;CHECK: add <4 x i32>
  8. ;CHECK: shufflevector <4 x i32> %{{.*}}, <4 x i32> undef, <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
  9. ;CHECK: add <4 x i32>
  10. ;CHECK: shufflevector <4 x i32> %{{.*}}, <4 x i32> undef, <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
  11. ;CHECK: add <4 x i32>
  12. ;CHECK: extractelement <4 x i32> %{{.*}}, i32 0
  13. ;CHECK: ret i32
  14. define i32 @reduction_sum(i32 %n, i32* noalias nocapture %A, i32* noalias nocapture %B) nounwind uwtable readonly noinline ssp {
  15. %1 = icmp sgt i32 %n, 0
  16. br i1 %1, label %.lr.ph, label %._crit_edge
  17. .lr.ph: ; preds = %0, %.lr.ph
  18. %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  19. %sum.02 = phi i32 [ %9, %.lr.ph ], [ 0, %0 ]
  20. %2 = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
  21. %3 = load i32, i32* %2, align 4
  22. %4 = getelementptr inbounds i32, i32* %B, i64 %indvars.iv
  23. %5 = load i32, i32* %4, align 4
  24. %6 = trunc i64 %indvars.iv to i32
  25. %7 = add i32 %sum.02, %6
  26. %8 = add i32 %7, %3
  27. %9 = add i32 %8, %5
  28. %indvars.iv.next = add i64 %indvars.iv, 1
  29. %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  30. %exitcond = icmp eq i32 %lftr.wideiv, %n
  31. br i1 %exitcond, label %._crit_edge, label %.lr.ph
  32. ._crit_edge: ; preds = %.lr.ph, %0
  33. %sum.0.lcssa = phi i32 [ 0, %0 ], [ %9, %.lr.ph ]
  34. ret i32 %sum.0.lcssa
  35. }
  36. ;CHECK-LABEL: @reduction_prod(
  37. ;CHECK: phi <4 x i32>
  38. ;CHECK: load <4 x i32>
  39. ;CHECK: mul <4 x i32>
  40. ;CHECK: shufflevector <4 x i32> %{{.*}}, <4 x i32> undef, <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
  41. ;CHECK: mul <4 x i32>
  42. ;CHECK: shufflevector <4 x i32> %{{.*}}, <4 x i32> undef, <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
  43. ;CHECK: mul <4 x i32>
  44. ;CHECK: extractelement <4 x i32> %{{.*}}, i32 0
  45. ;CHECK: ret i32
  46. define i32 @reduction_prod(i32 %n, i32* noalias nocapture %A, i32* noalias nocapture %B) nounwind uwtable readonly noinline ssp {
  47. %1 = icmp sgt i32 %n, 0
  48. br i1 %1, label %.lr.ph, label %._crit_edge
  49. .lr.ph: ; preds = %0, %.lr.ph
  50. %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  51. %prod.02 = phi i32 [ %9, %.lr.ph ], [ 1, %0 ]
  52. %2 = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
  53. %3 = load i32, i32* %2, align 4
  54. %4 = getelementptr inbounds i32, i32* %B, i64 %indvars.iv
  55. %5 = load i32, i32* %4, align 4
  56. %6 = trunc i64 %indvars.iv to i32
  57. %7 = mul i32 %prod.02, %6
  58. %8 = mul i32 %7, %3
  59. %9 = mul i32 %8, %5
  60. %indvars.iv.next = add i64 %indvars.iv, 1
  61. %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  62. %exitcond = icmp eq i32 %lftr.wideiv, %n
  63. br i1 %exitcond, label %._crit_edge, label %.lr.ph
  64. ._crit_edge: ; preds = %.lr.ph, %0
  65. %prod.0.lcssa = phi i32 [ 1, %0 ], [ %9, %.lr.ph ]
  66. ret i32 %prod.0.lcssa
  67. }
  68. ;CHECK-LABEL: @reduction_mix(
  69. ;CHECK: phi <4 x i32>
  70. ;CHECK: load <4 x i32>
  71. ;CHECK: mul nsw <4 x i32>
  72. ;CHECK: shufflevector <4 x i32> %{{.*}}, <4 x i32> undef, <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
  73. ;CHECK: add <4 x i32>
  74. ;CHECK: shufflevector <4 x i32> %{{.*}}, <4 x i32> undef, <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
  75. ;CHECK: add <4 x i32>
  76. ;CHECK: extractelement <4 x i32> %{{.*}}, i32 0
  77. ;CHECK: ret i32
  78. define i32 @reduction_mix(i32 %n, i32* noalias nocapture %A, i32* noalias nocapture %B) nounwind uwtable readonly noinline ssp {
  79. %1 = icmp sgt i32 %n, 0
  80. br i1 %1, label %.lr.ph, label %._crit_edge
  81. .lr.ph: ; preds = %0, %.lr.ph
  82. %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  83. %sum.02 = phi i32 [ %9, %.lr.ph ], [ 0, %0 ]
  84. %2 = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
  85. %3 = load i32, i32* %2, align 4
  86. %4 = getelementptr inbounds i32, i32* %B, i64 %indvars.iv
  87. %5 = load i32, i32* %4, align 4
  88. %6 = mul nsw i32 %5, %3
  89. %7 = trunc i64 %indvars.iv to i32
  90. %8 = add i32 %sum.02, %7
  91. %9 = add i32 %8, %6
  92. %indvars.iv.next = add i64 %indvars.iv, 1
  93. %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  94. %exitcond = icmp eq i32 %lftr.wideiv, %n
  95. br i1 %exitcond, label %._crit_edge, label %.lr.ph
  96. ._crit_edge: ; preds = %.lr.ph, %0
  97. %sum.0.lcssa = phi i32 [ 0, %0 ], [ %9, %.lr.ph ]
  98. ret i32 %sum.0.lcssa
  99. }
  100. ;CHECK-LABEL: @reduction_mul(
  101. ;CHECK: mul <4 x i32>
  102. ;CHECK: shufflevector <4 x i32> %{{.*}}, <4 x i32> undef, <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
  103. ;CHECK: mul <4 x i32>
  104. ;CHECK: shufflevector <4 x i32> %{{.*}}, <4 x i32> undef, <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
  105. ;CHECK: mul <4 x i32>
  106. ;CHECK: extractelement <4 x i32> %{{.*}}, i32 0
  107. ;CHECK: ret i32
  108. define i32 @reduction_mul(i32 %n, i32* noalias nocapture %A, i32* noalias nocapture %B) nounwind uwtable readonly noinline ssp {
  109. %1 = icmp sgt i32 %n, 0
  110. br i1 %1, label %.lr.ph, label %._crit_edge
  111. .lr.ph: ; preds = %0, %.lr.ph
  112. %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  113. %sum.02 = phi i32 [ %9, %.lr.ph ], [ 19, %0 ]
  114. %2 = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
  115. %3 = load i32, i32* %2, align 4
  116. %4 = getelementptr inbounds i32, i32* %B, i64 %indvars.iv
  117. %5 = load i32, i32* %4, align 4
  118. %6 = trunc i64 %indvars.iv to i32
  119. %7 = add i32 %3, %6
  120. %8 = add i32 %7, %5
  121. %9 = mul i32 %8, %sum.02
  122. %indvars.iv.next = add i64 %indvars.iv, 1
  123. %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  124. %exitcond = icmp eq i32 %lftr.wideiv, %n
  125. br i1 %exitcond, label %._crit_edge, label %.lr.ph
  126. ._crit_edge: ; preds = %.lr.ph, %0
  127. %sum.0.lcssa = phi i32 [ 0, %0 ], [ %9, %.lr.ph ]
  128. ret i32 %sum.0.lcssa
  129. }
  130. ;CHECK-LABEL: @start_at_non_zero(
  131. ;CHECK: phi <4 x i32>
  132. ;CHECK: <i32 120, i32 0, i32 0, i32 0>
  133. ;CHECK: shufflevector <4 x i32> %{{.*}}, <4 x i32> undef, <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
  134. ;CHECK: add <4 x i32>
  135. ;CHECK: shufflevector <4 x i32> %{{.*}}, <4 x i32> undef, <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
  136. ;CHECK: add <4 x i32>
  137. ;CHECK: extractelement <4 x i32> %{{.*}}, i32 0
  138. ;CHECK: ret i32
  139. define i32 @start_at_non_zero(i32* nocapture %in, i32* nocapture %coeff, i32* nocapture %out, i32 %n) nounwind uwtable readonly ssp {
  140. entry:
  141. %cmp7 = icmp sgt i32 %n, 0
  142. br i1 %cmp7, label %for.body, label %for.end
  143. for.body: ; preds = %entry, %for.body
  144. %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
  145. %sum.09 = phi i32 [ %add, %for.body ], [ 120, %entry ]
  146. %arrayidx = getelementptr inbounds i32, i32* %in, i64 %indvars.iv
  147. %0 = load i32, i32* %arrayidx, align 4
  148. %arrayidx2 = getelementptr inbounds i32, i32* %coeff, i64 %indvars.iv
  149. %1 = load i32, i32* %arrayidx2, align 4
  150. %mul = mul nsw i32 %1, %0
  151. %add = add nsw i32 %mul, %sum.09
  152. %indvars.iv.next = add i64 %indvars.iv, 1
  153. %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  154. %exitcond = icmp eq i32 %lftr.wideiv, %n
  155. br i1 %exitcond, label %for.end, label %for.body
  156. for.end: ; preds = %for.body, %entry
  157. %sum.0.lcssa = phi i32 [ 120, %entry ], [ %add, %for.body ]
  158. ret i32 %sum.0.lcssa
  159. }
  160. ;CHECK-LABEL: @reduction_and(
  161. ;CHECK: and <4 x i32>
  162. ;CHECK: <i32 -1, i32 -1, i32 -1, i32 -1>
  163. ;CHECK: shufflevector <4 x i32> %{{.*}}, <4 x i32> undef, <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
  164. ;CHECK: and <4 x i32>
  165. ;CHECK: shufflevector <4 x i32> %{{.*}}, <4 x i32> undef, <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
  166. ;CHECK: and <4 x i32>
  167. ;CHECK: extractelement <4 x i32> %{{.*}}, i32 0
  168. ;CHECK: ret i32
  169. define i32 @reduction_and(i32 %n, i32* nocapture %A, i32* nocapture %B) nounwind uwtable readonly {
  170. entry:
  171. %cmp7 = icmp sgt i32 %n, 0
  172. br i1 %cmp7, label %for.body, label %for.end
  173. for.body: ; preds = %entry, %for.body
  174. %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
  175. %result.08 = phi i32 [ %and, %for.body ], [ -1, %entry ]
  176. %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
  177. %0 = load i32, i32* %arrayidx, align 4
  178. %arrayidx2 = getelementptr inbounds i32, i32* %B, i64 %indvars.iv
  179. %1 = load i32, i32* %arrayidx2, align 4
  180. %add = add nsw i32 %1, %0
  181. %and = and i32 %add, %result.08
  182. %indvars.iv.next = add i64 %indvars.iv, 1
  183. %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  184. %exitcond = icmp eq i32 %lftr.wideiv, %n
  185. br i1 %exitcond, label %for.end, label %for.body
  186. for.end: ; preds = %for.body, %entry
  187. %result.0.lcssa = phi i32 [ -1, %entry ], [ %and, %for.body ]
  188. ret i32 %result.0.lcssa
  189. }
  190. ;CHECK-LABEL: @reduction_or(
  191. ;CHECK: or <4 x i32>
  192. ;CHECK: shufflevector <4 x i32> %{{.*}}, <4 x i32> undef, <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
  193. ;CHECK: or <4 x i32>
  194. ;CHECK: shufflevector <4 x i32> %{{.*}}, <4 x i32> undef, <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
  195. ;CHECK: or <4 x i32>
  196. ;CHECK: extractelement <4 x i32> %{{.*}}, i32 0
  197. ;CHECK: ret i32
  198. define i32 @reduction_or(i32 %n, i32* nocapture %A, i32* nocapture %B) nounwind uwtable readonly {
  199. entry:
  200. %cmp7 = icmp sgt i32 %n, 0
  201. br i1 %cmp7, label %for.body, label %for.end
  202. for.body: ; preds = %entry, %for.body
  203. %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
  204. %result.08 = phi i32 [ %or, %for.body ], [ 0, %entry ]
  205. %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
  206. %0 = load i32, i32* %arrayidx, align 4
  207. %arrayidx2 = getelementptr inbounds i32, i32* %B, i64 %indvars.iv
  208. %1 = load i32, i32* %arrayidx2, align 4
  209. %add = add nsw i32 %1, %0
  210. %or = or i32 %add, %result.08
  211. %indvars.iv.next = add i64 %indvars.iv, 1
  212. %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  213. %exitcond = icmp eq i32 %lftr.wideiv, %n
  214. br i1 %exitcond, label %for.end, label %for.body
  215. for.end: ; preds = %for.body, %entry
  216. %result.0.lcssa = phi i32 [ 0, %entry ], [ %or, %for.body ]
  217. ret i32 %result.0.lcssa
  218. }
  219. ;CHECK-LABEL: @reduction_xor(
  220. ;CHECK: xor <4 x i32>
  221. ;CHECK: shufflevector <4 x i32> %{{.*}}, <4 x i32> undef, <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
  222. ;CHECK: xor <4 x i32>
  223. ;CHECK: shufflevector <4 x i32> %{{.*}}, <4 x i32> undef, <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
  224. ;CHECK: xor <4 x i32>
  225. ;CHECK: extractelement <4 x i32> %{{.*}}, i32 0
  226. ;CHECK: ret i32
  227. define i32 @reduction_xor(i32 %n, i32* nocapture %A, i32* nocapture %B) nounwind uwtable readonly {
  228. entry:
  229. %cmp7 = icmp sgt i32 %n, 0
  230. br i1 %cmp7, label %for.body, label %for.end
  231. for.body: ; preds = %entry, %for.body
  232. %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
  233. %result.08 = phi i32 [ %xor, %for.body ], [ 0, %entry ]
  234. %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
  235. %0 = load i32, i32* %arrayidx, align 4
  236. %arrayidx2 = getelementptr inbounds i32, i32* %B, i64 %indvars.iv
  237. %1 = load i32, i32* %arrayidx2, align 4
  238. %add = add nsw i32 %1, %0
  239. %xor = xor i32 %add, %result.08
  240. %indvars.iv.next = add i64 %indvars.iv, 1
  241. %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  242. %exitcond = icmp eq i32 %lftr.wideiv, %n
  243. br i1 %exitcond, label %for.end, label %for.body
  244. for.end: ; preds = %for.body, %entry
  245. %result.0.lcssa = phi i32 [ 0, %entry ], [ %xor, %for.body ]
  246. ret i32 %result.0.lcssa
  247. }
  248. ; In this code the subtracted variable is on the RHS and this is not an induction variable.
  249. ;CHECK-LABEL: @reduction_sub_rhs(
  250. ;CHECK-NOT: phi <4 x i32>
  251. ;CHECK-NOT: sub nsw <4 x i32>
  252. ;CHECK: ret i32
  253. define i32 @reduction_sub_rhs(i32 %n, i32* noalias nocapture %A) nounwind uwtable readonly {
  254. entry:
  255. %cmp4 = icmp sgt i32 %n, 0
  256. br i1 %cmp4, label %for.body, label %for.end
  257. for.body: ; preds = %entry, %for.body
  258. %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
  259. %x.05 = phi i32 [ %sub, %for.body ], [ 0, %entry ]
  260. %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
  261. %0 = load i32, i32* %arrayidx, align 4
  262. %sub = sub nsw i32 %0, %x.05
  263. %indvars.iv.next = add i64 %indvars.iv, 1
  264. %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  265. %exitcond = icmp eq i32 %lftr.wideiv, %n
  266. br i1 %exitcond, label %for.end, label %for.body
  267. for.end: ; preds = %for.body, %entry
  268. %x.0.lcssa = phi i32 [ 0, %entry ], [ %sub, %for.body ]
  269. ret i32 %x.0.lcssa
  270. }
  271. ; In this test the reduction variable is on the LHS and we can vectorize it.
  272. ;CHECK-LABEL: @reduction_sub_lhs(
  273. ;CHECK: phi <4 x i32>
  274. ;CHECK: sub nsw <4 x i32>
  275. ;CHECK: ret i32
  276. define i32 @reduction_sub_lhs(i32 %n, i32* noalias nocapture %A) nounwind uwtable readonly {
  277. entry:
  278. %cmp4 = icmp sgt i32 %n, 0
  279. br i1 %cmp4, label %for.body, label %for.end
  280. for.body: ; preds = %entry, %for.body
  281. %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
  282. %x.05 = phi i32 [ %sub, %for.body ], [ 0, %entry ]
  283. %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
  284. %0 = load i32, i32* %arrayidx, align 4
  285. %sub = sub nsw i32 %x.05, %0
  286. %indvars.iv.next = add i64 %indvars.iv, 1
  287. %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  288. %exitcond = icmp eq i32 %lftr.wideiv, %n
  289. br i1 %exitcond, label %for.end, label %for.body
  290. for.end: ; preds = %for.body, %entry
  291. %x.0.lcssa = phi i32 [ 0, %entry ], [ %sub, %for.body ]
  292. ret i32 %x.0.lcssa
  293. }
  294. ; We can vectorize conditional reductions with multi-input phis.
  295. ; CHECK: reduction_conditional
  296. ; CHECK: fadd <4 x float>
  297. define float @reduction_conditional(float* %A, float* %B, float* %C, float %S) {
  298. entry:
  299. br label %for.body
  300. for.body:
  301. %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.inc ]
  302. %sum.033 = phi float [ %S, %entry ], [ %sum.1, %for.inc ]
  303. %arrayidx = getelementptr inbounds float, float* %A, i64 %indvars.iv
  304. %0 = load float, float* %arrayidx, align 4
  305. %arrayidx2 = getelementptr inbounds float, float* %B, i64 %indvars.iv
  306. %1 = load float, float* %arrayidx2, align 4
  307. %cmp3 = fcmp ogt float %0, %1
  308. br i1 %cmp3, label %if.then, label %for.inc
  309. if.then:
  310. %cmp6 = fcmp ogt float %1, 1.000000e+00
  311. br i1 %cmp6, label %if.then8, label %if.else
  312. if.then8:
  313. %add = fadd fast float %sum.033, %0
  314. br label %for.inc
  315. if.else:
  316. %cmp14 = fcmp ogt float %0, 2.000000e+00
  317. br i1 %cmp14, label %if.then16, label %for.inc
  318. if.then16:
  319. %add19 = fadd fast float %sum.033, %1
  320. br label %for.inc
  321. for.inc:
  322. %sum.1 = phi float [ %add, %if.then8 ], [ %add19, %if.then16 ], [ %sum.033, %if.else ], [ %sum.033, %for.body ]
  323. %indvars.iv.next = add i64 %indvars.iv, 1
  324. %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  325. %exitcond = icmp ne i32 %lftr.wideiv, 128
  326. br i1 %exitcond, label %for.body, label %for.end
  327. for.end:
  328. %sum.1.lcssa = phi float [ %sum.1, %for.inc ]
  329. ret float %sum.1.lcssa
  330. }
  331. ; We can't vectorize reductions with phi inputs from outside the reduction.
  332. ; CHECK: noreduction_phi
  333. ; CHECK-NOT: fadd <4 x float>
  334. define float @noreduction_phi(float* %A, float* %B, float* %C, float %S) {
  335. entry:
  336. br label %for.body
  337. for.body:
  338. %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.inc ]
  339. %sum.033 = phi float [ %S, %entry ], [ %sum.1, %for.inc ]
  340. %arrayidx = getelementptr inbounds float, float* %A, i64 %indvars.iv
  341. %0 = load float, float* %arrayidx, align 4
  342. %arrayidx2 = getelementptr inbounds float, float* %B, i64 %indvars.iv
  343. %1 = load float, float* %arrayidx2, align 4
  344. %cmp3 = fcmp ogt float %0, %1
  345. br i1 %cmp3, label %if.then, label %for.inc
  346. if.then:
  347. %cmp6 = fcmp ogt float %1, 1.000000e+00
  348. br i1 %cmp6, label %if.then8, label %if.else
  349. if.then8:
  350. %add = fadd fast float %sum.033, %0
  351. br label %for.inc
  352. if.else:
  353. %cmp14 = fcmp ogt float %0, 2.000000e+00
  354. br i1 %cmp14, label %if.then16, label %for.inc
  355. if.then16:
  356. %add19 = fadd fast float %sum.033, %1
  357. br label %for.inc
  358. for.inc:
  359. %sum.1 = phi float [ %add, %if.then8 ], [ %add19, %if.then16 ], [ 0.000000e+00, %if.else ], [ %sum.033, %for.body ]
  360. %indvars.iv.next = add i64 %indvars.iv, 1
  361. %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  362. %exitcond = icmp ne i32 %lftr.wideiv, 128
  363. br i1 %exitcond, label %for.body, label %for.end
  364. for.end:
  365. %sum.1.lcssa = phi float [ %sum.1, %for.inc ]
  366. ret float %sum.1.lcssa
  367. }
  368. ; We can't vectorize reductions that feed another header PHI.
  369. ; CHECK: noredux_header_phi
  370. ; CHECK-NOT: fadd <4 x float>
  371. define float @noredux_header_phi(float* %A, float* %B, float* %C, float %S) {
  372. entry:
  373. br label %for.body
  374. for.body:
  375. %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
  376. %sum2.09 = phi float [ 0.000000e+00, %entry ], [ %add1, %for.body ]
  377. %sum.08 = phi float [ %S, %entry ], [ %add, %for.body ]
  378. %arrayidx = getelementptr inbounds float, float* %B, i64 %indvars.iv
  379. %0 = load float, float* %arrayidx, align 4
  380. %add = fadd fast float %sum.08, %0
  381. %add1 = fadd fast float %sum2.09, %add
  382. %indvars.iv.next = add i64 %indvars.iv, 1
  383. %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  384. %exitcond = icmp ne i32 %lftr.wideiv, 128
  385. br i1 %exitcond, label %for.body, label %for.end
  386. for.end:
  387. %add1.lcssa = phi float [ %add1, %for.body ]
  388. %add.lcssa = phi float [ %add, %for.body ]
  389. %add2 = fadd fast float %add.lcssa, %add1.lcssa
  390. ret float %add2
  391. }
  392. ; When vectorizing a reduction whose loop header phi value is used outside the
  393. ; loop special care must be taken. Otherwise, the reduced value feeding into the
  394. ; outside user misses a few iterations (VF-1) of the loop.
  395. ; PR16522
  396. ; CHECK-LABEL: @phivalueredux(
  397. ; CHECK-NOT: x i32>
  398. define i32 @phivalueredux(i32 %p) {
  399. entry:
  400. br label %for.body
  401. for.body:
  402. %t.03 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
  403. %p.addr.02 = phi i32 [ %p, %entry ], [ %xor, %for.body ]
  404. %xor = xor i32 %p.addr.02, -1
  405. %inc = add nsw i32 %t.03, 1
  406. %exitcond = icmp eq i32 %inc, 16
  407. br i1 %exitcond, label %for.end, label %for.body
  408. for.end:
  409. ret i32 %p.addr.02
  410. }
  411. ; Don't vectorize a reduction value that is not the last in a reduction cyle. We
  412. ; would loose iterations (VF-1) on the operations after that use.
  413. ; PR17498
  414. ; CHECK-LABEL: not_last_operation
  415. ; CHECK-NOT: x i32>
  416. define i32 @not_last_operation(i32 %p, i32 %val) {
  417. entry:
  418. %tobool = icmp eq i32 %p, 0
  419. br label %for.body
  420. for.body:
  421. %inc613.1 = phi i32 [ 0, %entry ], [ %inc6.1, %for.body ]
  422. %inc511.1 = phi i32 [ %val, %entry ], [ %inc5.1, %for.body ]
  423. %0 = zext i1 %tobool to i32
  424. %inc4.1 = xor i32 %0, 1
  425. %inc511.1.inc4.1 = add nsw i32 %inc511.1, %inc4.1
  426. %inc5.1 = add nsw i32 %inc511.1.inc4.1, 1
  427. %inc6.1 = add nsw i32 %inc613.1, 1
  428. %exitcond.1 = icmp eq i32 %inc6.1, 22
  429. br i1 %exitcond.1, label %exit, label %for.body
  430. exit:
  431. %inc.2 = add nsw i32 %inc511.1.inc4.1, 2
  432. ret i32 %inc.2
  433. }