big-endian.ll 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. ; RUN: opt < %s -sroa -S | FileCheck %s
  2. ; RUN: opt < %s -sroa -force-ssa-updater -S | FileCheck %s
  3. target datalayout = "E-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-n8:16:32:64"
  4. define i8 @test1() {
  5. ; We fully promote these to the i24 load or store size, resulting in just masks
  6. ; and other operations that instcombine will fold, but no alloca. Note this is
  7. ; the same as test12 in basictest.ll, but here we assert big-endian byte
  8. ; ordering.
  9. ;
  10. ; CHECK-LABEL: @test1(
  11. entry:
  12. %a = alloca [3 x i8]
  13. %b = alloca [3 x i8]
  14. ; CHECK-NOT: alloca
  15. %a0ptr = getelementptr [3 x i8], [3 x i8]* %a, i64 0, i32 0
  16. store i8 0, i8* %a0ptr
  17. %a1ptr = getelementptr [3 x i8], [3 x i8]* %a, i64 0, i32 1
  18. store i8 0, i8* %a1ptr
  19. %a2ptr = getelementptr [3 x i8], [3 x i8]* %a, i64 0, i32 2
  20. store i8 0, i8* %a2ptr
  21. %aiptr = bitcast [3 x i8]* %a to i24*
  22. %ai = load i24, i24* %aiptr
  23. ; CHECK-NOT: store
  24. ; CHECK-NOT: load
  25. ; CHECK: %[[ext2:.*]] = zext i8 0 to i24
  26. ; CHECK-NEXT: %[[mask2:.*]] = and i24 undef, -256
  27. ; CHECK-NEXT: %[[insert2:.*]] = or i24 %[[mask2]], %[[ext2]]
  28. ; CHECK-NEXT: %[[ext1:.*]] = zext i8 0 to i24
  29. ; CHECK-NEXT: %[[shift1:.*]] = shl i24 %[[ext1]], 8
  30. ; CHECK-NEXT: %[[mask1:.*]] = and i24 %[[insert2]], -65281
  31. ; CHECK-NEXT: %[[insert1:.*]] = or i24 %[[mask1]], %[[shift1]]
  32. ; CHECK-NEXT: %[[ext0:.*]] = zext i8 0 to i24
  33. ; CHECK-NEXT: %[[shift0:.*]] = shl i24 %[[ext0]], 16
  34. ; CHECK-NEXT: %[[mask0:.*]] = and i24 %[[insert1]], 65535
  35. ; CHECK-NEXT: %[[insert0:.*]] = or i24 %[[mask0]], %[[shift0]]
  36. %biptr = bitcast [3 x i8]* %b to i24*
  37. store i24 %ai, i24* %biptr
  38. %b0ptr = getelementptr [3 x i8], [3 x i8]* %b, i64 0, i32 0
  39. %b0 = load i8, i8* %b0ptr
  40. %b1ptr = getelementptr [3 x i8], [3 x i8]* %b, i64 0, i32 1
  41. %b1 = load i8, i8* %b1ptr
  42. %b2ptr = getelementptr [3 x i8], [3 x i8]* %b, i64 0, i32 2
  43. %b2 = load i8, i8* %b2ptr
  44. ; CHECK-NOT: store
  45. ; CHECK-NOT: load
  46. ; CHECK: %[[shift0:.*]] = lshr i24 %[[insert0]], 16
  47. ; CHECK-NEXT: %[[trunc0:.*]] = trunc i24 %[[shift0]] to i8
  48. ; CHECK-NEXT: %[[shift1:.*]] = lshr i24 %[[insert0]], 8
  49. ; CHECK-NEXT: %[[trunc1:.*]] = trunc i24 %[[shift1]] to i8
  50. ; CHECK-NEXT: %[[trunc2:.*]] = trunc i24 %[[insert0]] to i8
  51. %bsum0 = add i8 %b0, %b1
  52. %bsum1 = add i8 %bsum0, %b2
  53. ret i8 %bsum1
  54. ; CHECK: %[[sum0:.*]] = add i8 %[[trunc0]], %[[trunc1]]
  55. ; CHECK-NEXT: %[[sum1:.*]] = add i8 %[[sum0]], %[[trunc2]]
  56. ; CHECK-NEXT: ret i8 %[[sum1]]
  57. }
  58. define i64 @test2() {
  59. ; Test for various mixed sizes of integer loads and stores all getting
  60. ; promoted.
  61. ;
  62. ; CHECK-LABEL: @test2(
  63. entry:
  64. %a = alloca [7 x i8]
  65. ; CHECK-NOT: alloca
  66. %a0ptr = getelementptr [7 x i8], [7 x i8]* %a, i64 0, i32 0
  67. %a1ptr = getelementptr [7 x i8], [7 x i8]* %a, i64 0, i32 1
  68. %a2ptr = getelementptr [7 x i8], [7 x i8]* %a, i64 0, i32 2
  69. %a3ptr = getelementptr [7 x i8], [7 x i8]* %a, i64 0, i32 3
  70. ; CHECK-NOT: store
  71. ; CHECK-NOT: load
  72. %a0i16ptr = bitcast i8* %a0ptr to i16*
  73. store i16 1, i16* %a0i16ptr
  74. store i8 1, i8* %a2ptr
  75. ; CHECK: %[[mask1:.*]] = and i40 undef, 4294967295
  76. ; CHECK-NEXT: %[[insert1:.*]] = or i40 %[[mask1]], 4294967296
  77. %a3i24ptr = bitcast i8* %a3ptr to i24*
  78. store i24 1, i24* %a3i24ptr
  79. ; CHECK-NEXT: %[[mask2:.*]] = and i40 %[[insert1]], -4294967041
  80. ; CHECK-NEXT: %[[insert2:.*]] = or i40 %[[mask2]], 256
  81. %a2i40ptr = bitcast i8* %a2ptr to i40*
  82. store i40 1, i40* %a2i40ptr
  83. ; CHECK-NEXT: %[[ext3:.*]] = zext i40 1 to i56
  84. ; CHECK-NEXT: %[[mask3:.*]] = and i56 undef, -1099511627776
  85. ; CHECK-NEXT: %[[insert3:.*]] = or i56 %[[mask3]], %[[ext3]]
  86. ; CHECK-NOT: store
  87. ; CHECK-NOT: load
  88. %aiptr = bitcast [7 x i8]* %a to i56*
  89. %ai = load i56, i56* %aiptr
  90. %ret = zext i56 %ai to i64
  91. ret i64 %ret
  92. ; CHECK-NEXT: %[[ext4:.*]] = zext i16 1 to i56
  93. ; CHECK-NEXT: %[[shift4:.*]] = shl i56 %[[ext4]], 40
  94. ; CHECK-NEXT: %[[mask4:.*]] = and i56 %[[insert3]], 1099511627775
  95. ; CHECK-NEXT: %[[insert4:.*]] = or i56 %[[mask4]], %[[shift4]]
  96. ; CHECK-NEXT: %[[ret:.*]] = zext i56 %[[insert4]] to i64
  97. ; CHECK-NEXT: ret i64 %[[ret]]
  98. }
  99. define i64 @PR14132(i1 %flag) {
  100. ; CHECK-LABEL: @PR14132(
  101. ; Here we form a PHI-node by promoting the pointer alloca first, and then in
  102. ; order to promote the other two allocas, we speculate the load of the
  103. ; now-phi-node-pointer. In doing so we end up loading a 64-bit value from an i8
  104. ; alloca. While this is a bit dubious, we were asserting on trying to
  105. ; rewrite it. The trick is that the code using the value may carefully take
  106. ; steps to only use the not-undef bits, and so we need to at least loosely
  107. ; support this. This test is particularly interesting because how we handle
  108. ; a load of an i64 from an i8 alloca is dependent on endianness.
  109. entry:
  110. %a = alloca i64, align 8
  111. %b = alloca i8, align 8
  112. %ptr = alloca i64*, align 8
  113. ; CHECK-NOT: alloca
  114. %ptr.cast = bitcast i64** %ptr to i8**
  115. store i64 0, i64* %a
  116. store i8 1, i8* %b
  117. store i64* %a, i64** %ptr
  118. br i1 %flag, label %if.then, label %if.end
  119. if.then:
  120. store i8* %b, i8** %ptr.cast
  121. br label %if.end
  122. ; CHECK-NOT: store
  123. ; CHECK: %[[ext:.*]] = zext i8 1 to i64
  124. ; CHECK: %[[shift:.*]] = shl i64 %[[ext]], 56
  125. if.end:
  126. %tmp = load i64*, i64** %ptr
  127. %result = load i64, i64* %tmp
  128. ; CHECK-NOT: load
  129. ; CHECK: %[[result:.*]] = phi i64 [ %[[shift]], %if.then ], [ 0, %entry ]
  130. ret i64 %result
  131. ; CHECK-NEXT: ret i64 %[[result]]
  132. }
  133. declare void @f(i64 %x, i32 %y)
  134. define void @test3() {
  135. ; CHECK-LABEL: @test3(
  136. ;
  137. ; This is a test that specifically exercises the big-endian lowering because it
  138. ; ends up splitting a 64-bit integer into two smaller integers and has a number
  139. ; of tricky aspects (the i24 type) that make that hard. Historically, SROA
  140. ; would miscompile this by either dropping a most significant byte or least
  141. ; significant byte due to shrinking the [4,8) slice to an i24, or by failing to
  142. ; move the bytes around correctly.
  143. ;
  144. ; The magical number 34494054408 is used because it has bits set in various
  145. ; bytes so that it is clear if those bytes fail to be propagated.
  146. ;
  147. ; If you're debugging this, rather than using the direct magical numbers, run
  148. ; the IR through '-sroa -instcombine'. With '-instcombine' these will be
  149. ; constant folded, and if the i64 doesn't round-trip correctly, you've found
  150. ; a bug!
  151. ;
  152. entry:
  153. %a = alloca { i32, i24 }, align 4
  154. ; CHECK-NOT: alloca
  155. %tmp0 = bitcast { i32, i24 }* %a to i64*
  156. store i64 34494054408, i64* %tmp0
  157. %tmp1 = load i64, i64* %tmp0, align 4
  158. %tmp2 = bitcast { i32, i24 }* %a to i32*
  159. %tmp3 = load i32, i32* %tmp2, align 4
  160. ; CHECK: %[[HI_EXT:.*]] = zext i32 134316040 to i64
  161. ; CHECK: %[[HI_INPUT:.*]] = and i64 undef, -4294967296
  162. ; CHECK: %[[HI_MERGE:.*]] = or i64 %[[HI_INPUT]], %[[HI_EXT]]
  163. ; CHECK: %[[LO_EXT:.*]] = zext i32 8 to i64
  164. ; CHECK: %[[LO_SHL:.*]] = shl i64 %[[LO_EXT]], 32
  165. ; CHECK: %[[LO_INPUT:.*]] = and i64 %[[HI_MERGE]], 4294967295
  166. ; CHECK: %[[LO_MERGE:.*]] = or i64 %[[LO_INPUT]], %[[LO_SHL]]
  167. call void @f(i64 %tmp1, i32 %tmp3)
  168. ; CHECK: call void @f(i64 %[[LO_MERGE]], i32 8)
  169. ret void
  170. ; CHECK: ret void
  171. }
  172. define void @test4() {
  173. ; CHECK-LABEL: @test4
  174. ;
  175. ; Much like @test3, this is specifically testing big-endian management of data.
  176. ; Also similarly, it uses constants with particular bits set to help track
  177. ; whether values are corrupted, and can be easily evaluated by running through
  178. ; -instcombine to see that the i64 round-trips.
  179. ;
  180. entry:
  181. %a = alloca { i32, i24 }, align 4
  182. %a2 = alloca i64, align 4
  183. ; CHECK-NOT: alloca
  184. store i64 34494054408, i64* %a2
  185. %tmp0 = bitcast { i32, i24 }* %a to i8*
  186. %tmp1 = bitcast i64* %a2 to i8*
  187. call void @llvm.memcpy.p0i8.p0i8.i64(i8* %tmp0, i8* %tmp1, i64 8, i32 4, i1 false)
  188. ; CHECK: %[[LO_SHR:.*]] = lshr i64 34494054408, 32
  189. ; CHECK: %[[LO_START:.*]] = trunc i64 %[[LO_SHR]] to i32
  190. ; CHECK: %[[HI_START:.*]] = trunc i64 34494054408 to i32
  191. %tmp2 = bitcast { i32, i24 }* %a to i64*
  192. %tmp3 = load i64, i64* %tmp2, align 4
  193. %tmp4 = bitcast { i32, i24 }* %a to i32*
  194. %tmp5 = load i32, i32* %tmp4, align 4
  195. ; CHECK: %[[HI_EXT:.*]] = zext i32 %[[HI_START]] to i64
  196. ; CHECK: %[[HI_INPUT:.*]] = and i64 undef, -4294967296
  197. ; CHECK: %[[HI_MERGE:.*]] = or i64 %[[HI_INPUT]], %[[HI_EXT]]
  198. ; CHECK: %[[LO_EXT:.*]] = zext i32 %[[LO_START]] to i64
  199. ; CHECK: %[[LO_SHL:.*]] = shl i64 %[[LO_EXT]], 32
  200. ; CHECK: %[[LO_INPUT:.*]] = and i64 %[[HI_MERGE]], 4294967295
  201. ; CHECK: %[[LO_MERGE:.*]] = or i64 %[[LO_INPUT]], %[[LO_SHL]]
  202. call void @f(i64 %tmp3, i32 %tmp5)
  203. ; CHECK: call void @f(i64 %[[LO_MERGE]], i32 %[[LO_START]])
  204. ret void
  205. ; CHECK: ret void
  206. }
  207. declare void @llvm.memcpy.p0i8.p0i8.i64(i8*, i8*, i64, i32, i1)