hoist-bitcast-load.ll 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. ; RUN: opt -S -basicaa -licm < %s | FileCheck %s
  2. target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
  3. target triple = "x86_64-unknown-linux-gnu"
  4. ; Make sure the basic alloca pointer hoisting works:
  5. ; CHECK-LABEL: @test1
  6. ; CHECK: load i32, i32* %c, align 4
  7. ; CHECK: for.body:
  8. ; Function Attrs: nounwind uwtable
  9. define void @test1(i32* nocapture %a, i32* nocapture readonly %b, i32 %n) #0 {
  10. entry:
  11. %cmp6 = icmp sgt i32 %n, 0
  12. %c = alloca i32
  13. br i1 %cmp6, label %for.body, label %for.end
  14. for.body: ; preds = %entry, %for.inc
  15. %indvars.iv = phi i64 [ %indvars.iv.next, %for.inc ], [ 0, %entry ]
  16. %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv
  17. %0 = load i32, i32* %arrayidx, align 4
  18. %cmp1 = icmp sgt i32 %0, 0
  19. br i1 %cmp1, label %if.then, label %for.inc
  20. if.then: ; preds = %for.body
  21. %1 = load i32, i32* %c, align 4
  22. %arrayidx3 = getelementptr inbounds i32, i32* %b, i64 %indvars.iv
  23. %2 = load i32, i32* %arrayidx3, align 4
  24. %mul = mul nsw i32 %2, %1
  25. store i32 %mul, i32* %arrayidx, align 4
  26. br label %for.inc
  27. for.inc: ; preds = %for.body, %if.then
  28. %indvars.iv.next = add nuw nsw 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 %for.end, label %for.body
  32. for.end: ; preds = %for.inc, %entry
  33. ret void
  34. }
  35. ; Make sure the basic alloca pointer hoisting works through a bitcast to a
  36. ; pointer to a smaller type:
  37. ; CHECK-LABEL: @test2
  38. ; CHECK: load i32, i32* %c, align 4
  39. ; CHECK: for.body:
  40. ; Function Attrs: nounwind uwtable
  41. define void @test2(i32* nocapture %a, i32* nocapture readonly %b, i32 %n) #0 {
  42. entry:
  43. %cmp6 = icmp sgt i32 %n, 0
  44. %ca = alloca i64
  45. %c = bitcast i64* %ca to i32*
  46. br i1 %cmp6, label %for.body, label %for.end
  47. for.body: ; preds = %entry, %for.inc
  48. %indvars.iv = phi i64 [ %indvars.iv.next, %for.inc ], [ 0, %entry ]
  49. %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv
  50. %0 = load i32, i32* %arrayidx, align 4
  51. %cmp1 = icmp sgt i32 %0, 0
  52. br i1 %cmp1, label %if.then, label %for.inc
  53. if.then: ; preds = %for.body
  54. %1 = load i32, i32* %c, align 4
  55. %arrayidx3 = getelementptr inbounds i32, i32* %b, i64 %indvars.iv
  56. %2 = load i32, i32* %arrayidx3, align 4
  57. %mul = mul nsw i32 %2, %1
  58. store i32 %mul, i32* %arrayidx, align 4
  59. br label %for.inc
  60. for.inc: ; preds = %for.body, %if.then
  61. %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  62. %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  63. %exitcond = icmp eq i32 %lftr.wideiv, %n
  64. br i1 %exitcond, label %for.end, label %for.body
  65. for.end: ; preds = %for.inc, %entry
  66. ret void
  67. }
  68. ; Make sure the basic alloca pointer hoisting works through an addrspacecast
  69. ; CHECK-LABEL: @test2_addrspacecast
  70. ; CHECK: load i32, i32 addrspace(1)* %c, align 4
  71. ; CHECK: for.body:
  72. ; Function Attrs: nounwind uwtable
  73. define void @test2_addrspacecast(i32 addrspace(1)* nocapture %a, i32 addrspace(1)* nocapture readonly %b, i32 %n) #0 {
  74. entry:
  75. %cmp6 = icmp sgt i32 %n, 0
  76. %ca = alloca i64
  77. %c = addrspacecast i64* %ca to i32 addrspace(1)*
  78. br i1 %cmp6, label %for.body, label %for.end
  79. for.body: ; preds = %entry, %for.inc
  80. %indvars.iv = phi i64 [ %indvars.iv.next, %for.inc ], [ 0, %entry ]
  81. %arrayidx = getelementptr inbounds i32, i32 addrspace(1)* %a, i64 %indvars.iv
  82. %0 = load i32, i32 addrspace(1)* %arrayidx, align 4
  83. %cmp1 = icmp sgt i32 %0, 0
  84. br i1 %cmp1, label %if.then, label %for.inc
  85. if.then: ; preds = %for.body
  86. %1 = load i32, i32 addrspace(1)* %c, align 4
  87. %arrayidx3 = getelementptr inbounds i32, i32 addrspace(1)* %b, i64 %indvars.iv
  88. %2 = load i32, i32 addrspace(1)* %arrayidx3, align 4
  89. %mul = mul nsw i32 %2, %1
  90. store i32 %mul, i32 addrspace(1)* %arrayidx, align 4
  91. br label %for.inc
  92. for.inc: ; preds = %for.body, %if.then
  93. %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  94. %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  95. %exitcond = icmp eq i32 %lftr.wideiv, %n
  96. br i1 %exitcond, label %for.end, label %for.body
  97. for.end: ; preds = %for.inc, %entry
  98. ret void
  99. }
  100. ; Make sure the basic alloca pointer hoisting works through a bitcast to a
  101. ; pointer to a smaller type (where the bitcast also needs to be hoisted):
  102. ; CHECK-LABEL: @test3
  103. ; CHECK: load i32, i32* %c, align 4
  104. ; CHECK: for.body:
  105. ; Function Attrs: nounwind uwtable
  106. define void @test3(i32* nocapture %a, i32* nocapture readonly %b, i32 %n) #0 {
  107. entry:
  108. %cmp6 = icmp sgt i32 %n, 0
  109. %ca = alloca i64
  110. br i1 %cmp6, label %for.body, label %for.end
  111. for.body: ; preds = %entry, %for.inc
  112. %indvars.iv = phi i64 [ %indvars.iv.next, %for.inc ], [ 0, %entry ]
  113. %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv
  114. %0 = load i32, i32* %arrayidx, align 4
  115. %cmp1 = icmp sgt i32 %0, 0
  116. br i1 %cmp1, label %if.then, label %for.inc
  117. if.then: ; preds = %for.body
  118. %c = bitcast i64* %ca to i32*
  119. %1 = load i32, i32* %c, align 4
  120. %arrayidx3 = getelementptr inbounds i32, i32* %b, i64 %indvars.iv
  121. %2 = load i32, i32* %arrayidx3, align 4
  122. %mul = mul nsw i32 %2, %1
  123. store i32 %mul, i32* %arrayidx, align 4
  124. br label %for.inc
  125. for.inc: ; preds = %for.body, %if.then
  126. %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  127. %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  128. %exitcond = icmp eq i32 %lftr.wideiv, %n
  129. br i1 %exitcond, label %for.end, label %for.body
  130. for.end: ; preds = %for.inc, %entry
  131. ret void
  132. }
  133. ; Make sure the basic alloca pointer hoisting does not happen through a bitcast
  134. ; to a pointer to a larger type:
  135. ; CHECK-LABEL: @test4
  136. ; CHECK: for.body:
  137. ; CHECK: load i32, i32* %c, align 4
  138. ; Function Attrs: nounwind uwtable
  139. define void @test4(i32* nocapture %a, i32* nocapture readonly %b, i32 %n) #0 {
  140. entry:
  141. %cmp6 = icmp sgt i32 %n, 0
  142. %ca = alloca i16
  143. %c = bitcast i16* %ca to i32*
  144. br i1 %cmp6, label %for.body, label %for.end
  145. for.body: ; preds = %entry, %for.inc
  146. %indvars.iv = phi i64 [ %indvars.iv.next, %for.inc ], [ 0, %entry ]
  147. %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv
  148. %0 = load i32, i32* %arrayidx, align 4
  149. %cmp1 = icmp sgt i32 %0, 0
  150. br i1 %cmp1, label %if.then, label %for.inc
  151. if.then: ; preds = %for.body
  152. %1 = load i32, i32* %c, align 4
  153. %arrayidx3 = getelementptr inbounds i32, i32* %b, i64 %indvars.iv
  154. %2 = load i32, i32* %arrayidx3, align 4
  155. %mul = mul nsw i32 %2, %1
  156. store i32 %mul, i32* %arrayidx, align 4
  157. br label %for.inc
  158. for.inc: ; preds = %for.body, %if.then
  159. %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  160. %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  161. %exitcond = icmp eq i32 %lftr.wideiv, %n
  162. br i1 %exitcond, label %for.end, label %for.body
  163. for.end: ; preds = %for.inc, %entry
  164. ret void
  165. }
  166. ; Don't crash on bitcasts to unsized types.
  167. ; CHECK-LABEL: @test5
  168. ; CHECK: for.body:
  169. ; CHECK: load i32, i32* %c, align 4
  170. %atype = type opaque
  171. ; Function Attrs: nounwind uwtable
  172. define void @test5(i32* nocapture %a, i32* nocapture readonly %b, i32 %n) #0 {
  173. entry:
  174. %cmp6 = icmp sgt i32 %n, 0
  175. %ca = alloca i16
  176. %cab = bitcast i16* %ca to %atype*
  177. %c = bitcast %atype* %cab to i32*
  178. br i1 %cmp6, label %for.body, label %for.end
  179. for.body: ; preds = %entry, %for.inc
  180. %indvars.iv = phi i64 [ %indvars.iv.next, %for.inc ], [ 0, %entry ]
  181. %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv
  182. %0 = load i32, i32* %arrayidx, align 4
  183. %cmp1 = icmp sgt i32 %0, 0
  184. br i1 %cmp1, label %if.then, label %for.inc
  185. if.then: ; preds = %for.body
  186. %1 = load i32, i32* %c, align 4
  187. %arrayidx3 = getelementptr inbounds i32, i32* %b, i64 %indvars.iv
  188. %2 = load i32, i32* %arrayidx3, align 4
  189. %mul = mul nsw i32 %2, %1
  190. store i32 %mul, i32* %arrayidx, align 4
  191. br label %for.inc
  192. for.inc: ; preds = %for.body, %if.then
  193. %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  194. %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  195. %exitcond = icmp eq i32 %lftr.wideiv, %n
  196. br i1 %exitcond, label %for.end, label %for.body
  197. for.end: ; preds = %for.inc, %entry
  198. ret void
  199. }
  200. attributes #0 = { nounwind uwtable }