induction.ll 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. ; RUN: opt < %s -loop-vectorize -force-vector-interleave=1 -force-vector-width=2 -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. ; Make sure that we can handle multiple integer induction variables.
  4. ; CHECK-LABEL: @multi_int_induction(
  5. ; CHECK: vector.body:
  6. ; CHECK: %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  7. ; CHECK: %normalized.idx = sub i64 %index, 0
  8. ; CHECK: %[[VAR:.*]] = trunc i64 %normalized.idx to i32
  9. ; CHECK: %offset.idx = add i32 190, %[[VAR]]
  10. define void @multi_int_induction(i32* %A, i32 %N) {
  11. for.body.lr.ph:
  12. br label %for.body
  13. for.body:
  14. %indvars.iv = phi i64 [ 0, %for.body.lr.ph ], [ %indvars.iv.next, %for.body ]
  15. %count.09 = phi i32 [ 190, %for.body.lr.ph ], [ %inc, %for.body ]
  16. %arrayidx2 = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
  17. store i32 %count.09, i32* %arrayidx2, align 4
  18. %inc = add nsw i32 %count.09, 1
  19. %indvars.iv.next = add i64 %indvars.iv, 1
  20. %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  21. %exitcond = icmp ne i32 %lftr.wideiv, %N
  22. br i1 %exitcond, label %for.body, label %for.end
  23. for.end:
  24. ret void
  25. }
  26. ; RUN: opt < %s -loop-vectorize -force-vector-interleave=1 -force-vector-width=2 -instcombine -S | FileCheck %s --check-prefix=IND
  27. ; Make sure we remove unneeded vectorization of induction variables.
  28. ; In order for instcombine to cleanup the vectorized induction variables that we
  29. ; create in the loop vectorizer we need to perform some form of redundancy
  30. ; elimination to get rid of multiple uses.
  31. ; IND-LABEL: scalar_use
  32. ; IND: br label %vector.body
  33. ; IND: vector.body:
  34. ; Vectorized induction variable.
  35. ; IND-NOT: insertelement <2 x i64>
  36. ; IND-NOT: shufflevector <2 x i64>
  37. ; IND: br {{.*}}, label %vector.body
  38. define void @scalar_use(float* %a, float %b, i64 %offset, i64 %offset2, i64 %n) {
  39. entry:
  40. br label %for.body
  41. for.body:
  42. %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]
  43. %ind.sum = add i64 %iv, %offset
  44. %arr.idx = getelementptr inbounds float, float* %a, i64 %ind.sum
  45. %l1 = load float, float* %arr.idx, align 4
  46. %ind.sum2 = add i64 %iv, %offset2
  47. %arr.idx2 = getelementptr inbounds float, float* %a, i64 %ind.sum2
  48. %l2 = load float, float* %arr.idx2, align 4
  49. %m = fmul fast float %b, %l2
  50. %ad = fadd fast float %l1, %m
  51. store float %ad, float* %arr.idx, align 4
  52. %iv.next = add nuw nsw i64 %iv, 1
  53. %exitcond = icmp eq i64 %iv.next, %n
  54. br i1 %exitcond, label %loopexit, label %for.body
  55. loopexit:
  56. ret void
  57. }
  58. ; Make sure that the loop exit count computation does not overflow for i8 and
  59. ; i16. The exit count of these loops is i8/i16 max + 1. If we don't cast the
  60. ; induction variable to a bigger type the exit count computation will overflow
  61. ; to 0.
  62. ; PR17532
  63. ; CHECK-LABEL: i8_loop
  64. ; CHECK: icmp eq i32 {{.*}}, 256
  65. define i32 @i8_loop() nounwind readnone ssp uwtable {
  66. br label %1
  67. ; <label>:1 ; preds = %1, %0
  68. %a.0 = phi i32 [ 1, %0 ], [ %2, %1 ]
  69. %b.0 = phi i8 [ 0, %0 ], [ %3, %1 ]
  70. %2 = and i32 %a.0, 4
  71. %3 = add i8 %b.0, -1
  72. %4 = icmp eq i8 %3, 0
  73. br i1 %4, label %5, label %1
  74. ; <label>:5 ; preds = %1
  75. ret i32 %2
  76. }
  77. ; CHECK-LABEL: i16_loop
  78. ; CHECK: icmp eq i32 {{.*}}, 65536
  79. define i32 @i16_loop() nounwind readnone ssp uwtable {
  80. br label %1
  81. ; <label>:1 ; preds = %1, %0
  82. %a.0 = phi i32 [ 1, %0 ], [ %2, %1 ]
  83. %b.0 = phi i16 [ 0, %0 ], [ %3, %1 ]
  84. %2 = and i32 %a.0, 4
  85. %3 = add i16 %b.0, -1
  86. %4 = icmp eq i16 %3, 0
  87. br i1 %4, label %5, label %1
  88. ; <label>:5 ; preds = %1
  89. ret i32 %2
  90. }
  91. ; This loop has a backedge taken count of i32_max. We need to check for this
  92. ; condition and branch directly to the scalar loop.
  93. ; CHECK-LABEL: max_i32_backedgetaken
  94. ; CHECK: %backedge.overflow = icmp eq i32 -1, -1
  95. ; CHECK: br i1 %backedge.overflow, label %scalar.ph, label %overflow.checked
  96. ; CHECK: scalar.ph:
  97. ; CHECK: %bc.resume.val = phi i32 [ %resume.val, %middle.block ], [ 0, %0 ]
  98. ; CHECK: %bc.merge.rdx = phi i32 [ 1, %0 ], [ %5, %middle.block ]
  99. define i32 @max_i32_backedgetaken() nounwind readnone ssp uwtable {
  100. br label %1
  101. ; <label>:1 ; preds = %1, %0
  102. %a.0 = phi i32 [ 1, %0 ], [ %2, %1 ]
  103. %b.0 = phi i32 [ 0, %0 ], [ %3, %1 ]
  104. %2 = and i32 %a.0, 4
  105. %3 = add i32 %b.0, -1
  106. %4 = icmp eq i32 %3, 0
  107. br i1 %4, label %5, label %1
  108. ; <label>:5 ; preds = %1
  109. ret i32 %2
  110. }
  111. ; When generating the overflow check we must sure that the induction start value
  112. ; is defined before the branch to the scalar preheader.
  113. ; CHECK-LABEL: testoverflowcheck
  114. ; CHECK: entry
  115. ; CHECK: %[[LOAD:.*]] = load i8
  116. ; CHECK: %[[VAL:.*]] = zext i8 %[[LOAD]] to i32
  117. ; CHECK: br
  118. ; CHECK: scalar.ph
  119. ; CHECK: phi i32 [ %{{.*}}, %middle.block ], [ %[[VAL]], %entry ]
  120. @e = global i8 1, align 1
  121. @d = common global i32 0, align 4
  122. @c = common global i32 0, align 4
  123. define i32 @testoverflowcheck() {
  124. entry:
  125. %.pr.i = load i8, i8* @e, align 1
  126. %0 = load i32, i32* @d, align 4
  127. %c.promoted.i = load i32, i32* @c, align 4
  128. br label %cond.end.i
  129. cond.end.i:
  130. %inc4.i = phi i8 [ %.pr.i, %entry ], [ %inc.i, %cond.end.i ]
  131. %and3.i = phi i32 [ %c.promoted.i, %entry ], [ %and.i, %cond.end.i ]
  132. %and.i = and i32 %0, %and3.i
  133. %inc.i = add i8 %inc4.i, 1
  134. %tobool.i = icmp eq i8 %inc.i, 0
  135. br i1 %tobool.i, label %loopexit, label %cond.end.i
  136. loopexit:
  137. ret i32 %and.i
  138. }