cast.ll 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. ; Tests to make sure elimination of casts is working correctly
  2. ; RUN: opt < %s -instcombine -S | FileCheck %s
  3. target datalayout = "E-p:64:64:64-p1:32:32:32-p2:64:64:64-p3:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128-n8:16:32:64"
  4. @inbuf = external global [32832 x i8] ; <[32832 x i8]*> [#uses=1]
  5. define i32 @test1(i32 %A) {
  6. %c1 = bitcast i32 %A to i32 ; <i32> [#uses=1]
  7. %c2 = bitcast i32 %c1 to i32 ; <i32> [#uses=1]
  8. ret i32 %c2
  9. ; CHECK: ret i32 %A
  10. }
  11. define i64 @test2(i8 %A) {
  12. %c1 = zext i8 %A to i16 ; <i16> [#uses=1]
  13. %c2 = zext i16 %c1 to i32 ; <i32> [#uses=1]
  14. %Ret = zext i32 %c2 to i64 ; <i64> [#uses=1]
  15. ret i64 %Ret
  16. ; CHECK: %Ret = zext i8 %A to i64
  17. ; CHECK: ret i64 %Ret
  18. }
  19. ; This function should just use bitwise AND
  20. define i64 @test3(i64 %A) {
  21. %c1 = trunc i64 %A to i8 ; <i8> [#uses=1]
  22. %c2 = zext i8 %c1 to i64 ; <i64> [#uses=1]
  23. ret i64 %c2
  24. ; CHECK: %c2 = and i64 %A, 255
  25. ; CHECK: ret i64 %c2
  26. }
  27. define i32 @test4(i32 %A, i32 %B) {
  28. %COND = icmp slt i32 %A, %B ; <i1> [#uses=1]
  29. ; Booleans are unsigned integrals
  30. %c = zext i1 %COND to i8 ; <i8> [#uses=1]
  31. ; for the cast elim purpose
  32. %result = zext i8 %c to i32 ; <i32> [#uses=1]
  33. ret i32 %result
  34. ; CHECK: %COND = icmp slt i32 %A, %B
  35. ; CHECK: %result = zext i1 %COND to i32
  36. ; CHECK: ret i32 %result
  37. }
  38. define i32 @test5(i1 %B) {
  39. ; This cast should get folded into
  40. %c = zext i1 %B to i8 ; <i8> [#uses=1]
  41. ; this cast
  42. %result = zext i8 %c to i32 ; <i32> [#uses=1]
  43. ret i32 %result
  44. ; CHECK: %result = zext i1 %B to i32
  45. ; CHECK: ret i32 %result
  46. }
  47. define i32 @test6(i64 %A) {
  48. %c1 = trunc i64 %A to i32 ; <i32> [#uses=1]
  49. %res = bitcast i32 %c1 to i32 ; <i32> [#uses=1]
  50. ret i32 %res
  51. ; CHECK: trunc i64 %A to i32
  52. ; CHECK-NEXT: ret i32
  53. }
  54. define i64 @test7(i1 %A) {
  55. %c1 = zext i1 %A to i32 ; <i32> [#uses=1]
  56. %res = sext i32 %c1 to i64 ; <i64> [#uses=1]
  57. ret i64 %res
  58. ; CHECK: %res = zext i1 %A to i64
  59. ; CHECK: ret i64 %res
  60. }
  61. define i64 @test8(i8 %A) {
  62. %c1 = sext i8 %A to i64 ; <i64> [#uses=1]
  63. %res = bitcast i64 %c1 to i64 ; <i64> [#uses=1]
  64. ret i64 %res
  65. ; CHECK: = sext i8 %A to i64
  66. ; CHECK-NEXT: ret i64
  67. }
  68. define i16 @test9(i16 %A) {
  69. %c1 = sext i16 %A to i32 ; <i32> [#uses=1]
  70. %c2 = trunc i32 %c1 to i16 ; <i16> [#uses=1]
  71. ret i16 %c2
  72. ; CHECK: ret i16 %A
  73. }
  74. define i16 @test10(i16 %A) {
  75. %c1 = sext i16 %A to i32 ; <i32> [#uses=1]
  76. %c2 = trunc i32 %c1 to i16 ; <i16> [#uses=1]
  77. ret i16 %c2
  78. ; CHECK: ret i16 %A
  79. }
  80. declare void @varargs(i32, ...)
  81. define void @test11(i32* %P) {
  82. %c = bitcast i32* %P to i16* ; <i16*> [#uses=1]
  83. call void (i32, ...) @varargs( i32 5, i16* %c )
  84. ret void
  85. ; CHECK: call void (i32, ...) @varargs(i32 5, i32* %P)
  86. ; CHECK: ret void
  87. }
  88. declare i32 @__gxx_personality_v0(...)
  89. define void @test_invoke_vararg_cast(i32* %a, i32* %b) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
  90. entry:
  91. %0 = bitcast i32* %b to i8*
  92. %1 = bitcast i32* %a to i64*
  93. invoke void (i32, ...) @varargs(i32 1, i8* %0, i64* %1)
  94. to label %invoke.cont unwind label %lpad
  95. invoke.cont: ; preds = %entry
  96. ret void
  97. lpad: ; preds = %entry
  98. %2 = landingpad { i8*, i32 }
  99. cleanup
  100. ret void
  101. ; CHECK-LABEL: test_invoke_vararg_cast
  102. ; CHECK-LABEL: entry:
  103. ; CHECK: invoke void (i32, ...) @varargs(i32 1, i32* %b, i32* %a)
  104. }
  105. define i8* @test13(i64 %A) {
  106. %c = getelementptr [0 x i8], [0 x i8]* bitcast ([32832 x i8]* @inbuf to [0 x i8]*), i64 0, i64 %A ; <i8*> [#uses=1]
  107. ret i8* %c
  108. ; CHECK: %c = getelementptr [32832 x i8], [32832 x i8]* @inbuf, i64 0, i64 %A
  109. ; CHECK: ret i8* %c
  110. }
  111. define i1 @test14(i8 %A) {
  112. %c = bitcast i8 %A to i8 ; <i8> [#uses=1]
  113. %X = icmp ult i8 %c, -128 ; <i1> [#uses=1]
  114. ret i1 %X
  115. ; CHECK: %X = icmp sgt i8 %A, -1
  116. ; CHECK: ret i1 %X
  117. }
  118. ; This just won't occur when there's no difference between ubyte and sbyte
  119. ;bool %test15(ubyte %A) {
  120. ; %c = cast ubyte %A to sbyte
  121. ; %X = setlt sbyte %c, 0 ; setgt %A, 127
  122. ; ret bool %X
  123. ;}
  124. define i1 @test16(i32* %P) {
  125. %c = icmp ne i32* %P, null ; <i1> [#uses=1]
  126. ret i1 %c
  127. ; CHECK: %c = icmp ne i32* %P, null
  128. ; CHECK: ret i1 %c
  129. }
  130. define i16 @test17(i1 %tmp3) {
  131. %c = zext i1 %tmp3 to i32 ; <i32> [#uses=1]
  132. %t86 = trunc i32 %c to i16 ; <i16> [#uses=1]
  133. ret i16 %t86
  134. ; CHECK: %t86 = zext i1 %tmp3 to i16
  135. ; CHECK: ret i16 %t86
  136. }
  137. define i16 @test18(i8 %tmp3) {
  138. %c = sext i8 %tmp3 to i32 ; <i32> [#uses=1]
  139. %t86 = trunc i32 %c to i16 ; <i16> [#uses=1]
  140. ret i16 %t86
  141. ; CHECK: %t86 = sext i8 %tmp3 to i16
  142. ; CHECK: ret i16 %t86
  143. }
  144. define i1 @test19(i32 %X) {
  145. %c = sext i32 %X to i64 ; <i64> [#uses=1]
  146. %Z = icmp slt i64 %c, 12345 ; <i1> [#uses=1]
  147. ret i1 %Z
  148. ; CHECK: %Z = icmp slt i32 %X, 12345
  149. ; CHECK: ret i1 %Z
  150. }
  151. define i1 @test20(i1 %B) {
  152. %c = zext i1 %B to i32 ; <i32> [#uses=1]
  153. %D = icmp slt i32 %c, -1 ; <i1> [#uses=1]
  154. ;; false
  155. ret i1 %D
  156. ; CHECK: ret i1 false
  157. }
  158. define i32 @test21(i32 %X) {
  159. %c1 = trunc i32 %X to i8 ; <i8> [#uses=1]
  160. ;; sext -> zext -> and -> nop
  161. %c2 = sext i8 %c1 to i32 ; <i32> [#uses=1]
  162. %RV = and i32 %c2, 255 ; <i32> [#uses=1]
  163. ret i32 %RV
  164. ; CHECK: %c2.1 = and i32 %X, 255
  165. ; CHECK: ret i32 %c2.1
  166. }
  167. define i32 @test22(i32 %X) {
  168. %c1 = trunc i32 %X to i8 ; <i8> [#uses=1]
  169. ;; sext -> zext -> and -> nop
  170. %c2 = sext i8 %c1 to i32 ; <i32> [#uses=1]
  171. %RV = shl i32 %c2, 24 ; <i32> [#uses=1]
  172. ret i32 %RV
  173. ; CHECK: shl i32 %X, 24
  174. ; CHECK-NEXT: ret i32
  175. }
  176. define i32 @test23(i32 %X) {
  177. ;; Turn into an AND even though X
  178. %c1 = trunc i32 %X to i16 ; <i16> [#uses=1]
  179. ;; and Z are signed.
  180. %c2 = zext i16 %c1 to i32 ; <i32> [#uses=1]
  181. ret i32 %c2
  182. ; CHECK: %c2 = and i32 %X, 65535
  183. ; CHECK: ret i32 %c2
  184. }
  185. define i1 @test24(i1 %C) {
  186. %X = select i1 %C, i32 14, i32 1234 ; <i32> [#uses=1]
  187. ;; Fold cast into select
  188. %c = icmp ne i32 %X, 0 ; <i1> [#uses=1]
  189. ret i1 %c
  190. ; CHECK: ret i1 true
  191. }
  192. define i32 @test26(float %F) {
  193. ;; no need to cast from float->double.
  194. %c = fpext float %F to double ; <double> [#uses=1]
  195. %D = fptosi double %c to i32 ; <i32> [#uses=1]
  196. ret i32 %D
  197. ; CHECK: %D = fptosi float %F to i32
  198. ; CHECK: ret i32 %D
  199. }
  200. define [4 x float]* @test27([9 x [4 x float]]* %A) {
  201. %c = bitcast [9 x [4 x float]]* %A to [4 x float]* ; <[4 x float]*> [#uses=1]
  202. ret [4 x float]* %c
  203. ; CHECK: %c = getelementptr inbounds [9 x [4 x float]], [9 x [4 x float]]* %A, i64 0, i64 0
  204. ; CHECK: ret [4 x float]* %c
  205. }
  206. define float* @test28([4 x float]* %A) {
  207. %c = bitcast [4 x float]* %A to float* ; <float*> [#uses=1]
  208. ret float* %c
  209. ; CHECK: %c = getelementptr inbounds [4 x float], [4 x float]* %A, i64 0, i64 0
  210. ; CHECK: ret float* %c
  211. }
  212. define i32 @test29(i32 %c1, i32 %c2) {
  213. %tmp1 = trunc i32 %c1 to i8 ; <i8> [#uses=1]
  214. %tmp4.mask = trunc i32 %c2 to i8 ; <i8> [#uses=1]
  215. %tmp = or i8 %tmp4.mask, %tmp1 ; <i8> [#uses=1]
  216. %tmp10 = zext i8 %tmp to i32 ; <i32> [#uses=1]
  217. ret i32 %tmp10
  218. ; CHECK: %tmp2 = or i32 %c2, %c1
  219. ; CHECK: %tmp10 = and i32 %tmp2, 255
  220. ; CHECK: ret i32 %tmp10
  221. }
  222. define i32 @test30(i32 %c1) {
  223. %c2 = trunc i32 %c1 to i8 ; <i8> [#uses=1]
  224. %c3 = xor i8 %c2, 1 ; <i8> [#uses=1]
  225. %c4 = zext i8 %c3 to i32 ; <i32> [#uses=1]
  226. ret i32 %c4
  227. ; CHECK: %c3 = and i32 %c1, 255
  228. ; CHECK: %c4 = xor i32 %c3, 1
  229. ; CHECK: ret i32 %c4
  230. }
  231. define i1 @test31(i64 %A) {
  232. %B = trunc i64 %A to i32 ; <i32> [#uses=1]
  233. %C = and i32 %B, 42 ; <i32> [#uses=1]
  234. %D = icmp eq i32 %C, 10 ; <i1> [#uses=1]
  235. ret i1 %D
  236. ; CHECK: %C = and i64 %A, 42
  237. ; CHECK: %D = icmp eq i64 %C, 10
  238. ; CHECK: ret i1 %D
  239. }
  240. define i32 @test33(i32 %c1) {
  241. %x = bitcast i32 %c1 to float ; <float> [#uses=1]
  242. %y = bitcast float %x to i32 ; <i32> [#uses=1]
  243. ret i32 %y
  244. ; CHECK: ret i32 %c1
  245. }
  246. define i16 @test34(i16 %a) {
  247. %c1 = zext i16 %a to i32 ; <i32> [#uses=1]
  248. %tmp21 = lshr i32 %c1, 8 ; <i32> [#uses=1]
  249. %c2 = trunc i32 %tmp21 to i16 ; <i16> [#uses=1]
  250. ret i16 %c2
  251. ; CHECK: %tmp21 = lshr i16 %a, 8
  252. ; CHECK: ret i16 %tmp21
  253. }
  254. define i16 @test35(i16 %a) {
  255. %c1 = bitcast i16 %a to i16 ; <i16> [#uses=1]
  256. %tmp2 = lshr i16 %c1, 8 ; <i16> [#uses=1]
  257. %c2 = bitcast i16 %tmp2 to i16 ; <i16> [#uses=1]
  258. ret i16 %c2
  259. ; CHECK: %tmp2 = lshr i16 %a, 8
  260. ; CHECK: ret i16 %tmp2
  261. }
  262. ; icmp sgt i32 %a, -1
  263. ; rdar://6480391
  264. define i1 @test36(i32 %a) {
  265. %b = lshr i32 %a, 31
  266. %c = trunc i32 %b to i8
  267. %d = icmp eq i8 %c, 0
  268. ret i1 %d
  269. ; CHECK: %d = icmp sgt i32 %a, -1
  270. ; CHECK: ret i1 %d
  271. }
  272. ; ret i1 false
  273. define i1 @test37(i32 %a) {
  274. %b = lshr i32 %a, 31
  275. %c = or i32 %b, 512
  276. %d = trunc i32 %c to i8
  277. %e = icmp eq i8 %d, 11
  278. ret i1 %e
  279. ; CHECK: ret i1 false
  280. }
  281. define i64 @test38(i32 %a) {
  282. %1 = icmp eq i32 %a, -2
  283. %2 = zext i1 %1 to i8
  284. %3 = xor i8 %2, 1
  285. %4 = zext i8 %3 to i64
  286. ret i64 %4
  287. ; CHECK: %1 = icmp ne i32 %a, -2
  288. ; CHECK: %2 = zext i1 %1 to i64
  289. ; CHECK: ret i64 %2
  290. }
  291. define i16 @test39(i16 %a) {
  292. %tmp = zext i16 %a to i32
  293. %tmp21 = lshr i32 %tmp, 8
  294. %tmp5 = shl i32 %tmp, 8
  295. %tmp.upgrd.32 = or i32 %tmp21, %tmp5
  296. %tmp.upgrd.3 = trunc i32 %tmp.upgrd.32 to i16
  297. ret i16 %tmp.upgrd.3
  298. ; CHECK-LABEL: @test39(
  299. ; CHECK: %tmp.upgrd.32 = call i16 @llvm.bswap.i16(i16 %a)
  300. ; CHECK: ret i16 %tmp.upgrd.32
  301. }
  302. define i16 @test40(i16 %a) {
  303. %tmp = zext i16 %a to i32
  304. %tmp21 = lshr i32 %tmp, 9
  305. %tmp5 = shl i32 %tmp, 8
  306. %tmp.upgrd.32 = or i32 %tmp21, %tmp5
  307. %tmp.upgrd.3 = trunc i32 %tmp.upgrd.32 to i16
  308. ret i16 %tmp.upgrd.3
  309. ; CHECK-LABEL: @test40(
  310. ; CHECK: %tmp21 = lshr i16 %a, 9
  311. ; CHECK: %tmp5 = shl i16 %a, 8
  312. ; CHECK: %tmp.upgrd.32 = or i16 %tmp21, %tmp5
  313. ; CHECK: ret i16 %tmp.upgrd.32
  314. }
  315. ; PR1263
  316. define i32* @test41(i32* %tmp1) {
  317. %tmp64 = bitcast i32* %tmp1 to { i32 }*
  318. %tmp65 = getelementptr { i32 }, { i32 }* %tmp64, i32 0, i32 0
  319. ret i32* %tmp65
  320. ; CHECK-LABEL: @test41(
  321. ; CHECK: ret i32* %tmp1
  322. }
  323. define i32 addrspace(1)* @test41_addrspacecast_smaller(i32* %tmp1) {
  324. %tmp64 = addrspacecast i32* %tmp1 to { i32 } addrspace(1)*
  325. %tmp65 = getelementptr { i32 }, { i32 } addrspace(1)* %tmp64, i32 0, i32 0
  326. ret i32 addrspace(1)* %tmp65
  327. ; CHECK-LABEL: @test41_addrspacecast_smaller(
  328. ; CHECK: addrspacecast i32* %tmp1 to i32 addrspace(1)*
  329. ; CHECK-NEXT: ret i32 addrspace(1)*
  330. }
  331. define i32* @test41_addrspacecast_larger(i32 addrspace(1)* %tmp1) {
  332. %tmp64 = addrspacecast i32 addrspace(1)* %tmp1 to { i32 }*
  333. %tmp65 = getelementptr { i32 }, { i32 }* %tmp64, i32 0, i32 0
  334. ret i32* %tmp65
  335. ; CHECK-LABEL: @test41_addrspacecast_larger(
  336. ; CHECK: addrspacecast i32 addrspace(1)* %tmp1 to i32*
  337. ; CHECK-NEXT: ret i32*
  338. }
  339. define i32 @test42(i32 %X) {
  340. %Y = trunc i32 %X to i8 ; <i8> [#uses=1]
  341. %Z = zext i8 %Y to i32 ; <i32> [#uses=1]
  342. ret i32 %Z
  343. ; CHECK-LABEL: @test42(
  344. ; CHECK: %Z = and i32 %X, 255
  345. }
  346. ; rdar://6598839
  347. define zeroext i64 @test43(i8 zeroext %on_off) nounwind readonly {
  348. %A = zext i8 %on_off to i32
  349. %B = add i32 %A, -1
  350. %C = sext i32 %B to i64
  351. ret i64 %C ;; Should be (add (zext i8 -> i64), -1)
  352. ; CHECK-LABEL: @test43(
  353. ; CHECK-NEXT: %A = zext i8 %on_off to i64
  354. ; CHECK-NEXT: %B = add nsw i64 %A, -1
  355. ; CHECK-NEXT: ret i64 %B
  356. }
  357. define i64 @test44(i8 %T) {
  358. %A = zext i8 %T to i16
  359. %B = or i16 %A, 1234
  360. %C = zext i16 %B to i64
  361. ret i64 %C
  362. ; CHECK-LABEL: @test44(
  363. ; CHECK-NEXT: %A = zext i8 %T to i64
  364. ; CHECK-NEXT: %B = or i64 %A, 1234
  365. ; CHECK-NEXT: ret i64 %B
  366. }
  367. define i64 @test45(i8 %A, i64 %Q) {
  368. %D = trunc i64 %Q to i32 ;; should be removed
  369. %B = sext i8 %A to i32
  370. %C = or i32 %B, %D
  371. %E = zext i32 %C to i64
  372. ret i64 %E
  373. ; CHECK-LABEL: @test45(
  374. ; CHECK-NEXT: %B = sext i8 %A to i64
  375. ; CHECK-NEXT: %C = or i64 %B, %Q
  376. ; CHECK-NEXT: %E = and i64 %C, 4294967295
  377. ; CHECK-NEXT: ret i64 %E
  378. }
  379. define i64 @test46(i64 %A) {
  380. %B = trunc i64 %A to i32
  381. %C = and i32 %B, 42
  382. %D = shl i32 %C, 8
  383. %E = zext i32 %D to i64
  384. ret i64 %E
  385. ; CHECK-LABEL: @test46(
  386. ; CHECK-NEXT: %C = shl i64 %A, 8
  387. ; CHECK-NEXT: %D = and i64 %C, 10752
  388. ; CHECK-NEXT: ret i64 %D
  389. }
  390. define i64 @test47(i8 %A) {
  391. %B = sext i8 %A to i32
  392. %C = or i32 %B, 42
  393. %E = zext i32 %C to i64
  394. ret i64 %E
  395. ; CHECK-LABEL: @test47(
  396. ; CHECK-NEXT: %B = sext i8 %A to i64
  397. ; CHECK-NEXT: %C = and i64 %B, 4294967253
  398. ; CHECK-NEXT: %E = or i64 %C, 42
  399. ; CHECK-NEXT: ret i64 %E
  400. }
  401. define i64 @test48(i8 %A, i8 %a) {
  402. %b = zext i8 %a to i32
  403. %B = zext i8 %A to i32
  404. %C = shl i32 %B, 8
  405. %D = or i32 %C, %b
  406. %E = zext i32 %D to i64
  407. ret i64 %E
  408. ; CHECK-LABEL: @test48(
  409. ; CHECK-NEXT: %b = zext i8 %a to i64
  410. ; CHECK-NEXT: %B = zext i8 %A to i64
  411. ; CHECK-NEXT: %C = shl nuw nsw i64 %B, 8
  412. ; CHECK-NEXT: %D = or i64 %C, %b
  413. ; CHECK-NEXT: ret i64 %D
  414. }
  415. define i64 @test49(i64 %A) {
  416. %B = trunc i64 %A to i32
  417. %C = or i32 %B, 1
  418. %D = sext i32 %C to i64
  419. ret i64 %D
  420. ; CHECK-LABEL: @test49(
  421. ; CHECK-NEXT: %C = shl i64 %A, 32
  422. ; CHECK-NEXT: ashr exact i64 %C, 32
  423. ; CHECK-NEXT: %D = or i64 {{.*}}, 1
  424. ; CHECK-NEXT: ret i64 %D
  425. }
  426. define i64 @test50(i64 %A) {
  427. %a = lshr i64 %A, 2
  428. %B = trunc i64 %a to i32
  429. %D = add i32 %B, -1
  430. %E = sext i32 %D to i64
  431. ret i64 %E
  432. ; CHECK-LABEL: @test50(
  433. ; lshr+shl will be handled by DAGCombine.
  434. ; CHECK-NEXT: lshr i64 %A, 2
  435. ; CHECK-NEXT: shl i64 %a, 32
  436. ; CHECK-NEXT: add i64 {{.*}}, -4294967296
  437. ; CHECK-NEXT: %E = ashr exact i64 {{.*}}, 32
  438. ; CHECK-NEXT: ret i64 %E
  439. }
  440. define i64 @test51(i64 %A, i1 %cond) {
  441. %B = trunc i64 %A to i32
  442. %C = and i32 %B, -2
  443. %D = or i32 %B, 1
  444. %E = select i1 %cond, i32 %C, i32 %D
  445. %F = sext i32 %E to i64
  446. ret i64 %F
  447. ; CHECK-LABEL: @test51(
  448. ; CHECK-NEXT: %C = and i64 %A, 4294967294
  449. ; CHECK-NEXT: %D = or i64 %A, 1
  450. ; CHECK-NEXT: %E = select i1 %cond, i64 %C, i64 %D
  451. ; CHECK-NEXT: %sext = shl i64 %E, 32
  452. ; CHECK-NEXT: %F = ashr exact i64 %sext, 32
  453. ; CHECK-NEXT: ret i64 %F
  454. }
  455. define i32 @test52(i64 %A) {
  456. %B = trunc i64 %A to i16
  457. %C = or i16 %B, -32574
  458. %D = and i16 %C, -25350
  459. %E = zext i16 %D to i32
  460. ret i32 %E
  461. ; CHECK-LABEL: @test52(
  462. ; CHECK-NEXT: %B = trunc i64 %A to i32
  463. ; CHECK-NEXT: %C = and i32 %B, 7224
  464. ; CHECK-NEXT: %D = or i32 %C, 32962
  465. ; CHECK-NEXT: ret i32 %D
  466. }
  467. define i64 @test53(i32 %A) {
  468. %B = trunc i32 %A to i16
  469. %C = or i16 %B, -32574
  470. %D = and i16 %C, -25350
  471. %E = zext i16 %D to i64
  472. ret i64 %E
  473. ; CHECK-LABEL: @test53(
  474. ; CHECK-NEXT: %B = zext i32 %A to i64
  475. ; CHECK-NEXT: %C = and i64 %B, 7224
  476. ; CHECK-NEXT: %D = or i64 %C, 32962
  477. ; CHECK-NEXT: ret i64 %D
  478. }
  479. define i32 @test54(i64 %A) {
  480. %B = trunc i64 %A to i16
  481. %C = or i16 %B, -32574
  482. %D = and i16 %C, -25350
  483. %E = sext i16 %D to i32
  484. ret i32 %E
  485. ; CHECK-LABEL: @test54(
  486. ; CHECK-NEXT: %B = trunc i64 %A to i32
  487. ; CHECK-NEXT: %C = and i32 %B, 7224
  488. ; CHECK-NEXT: %D = or i32 %C, -32574
  489. ; CHECK-NEXT: ret i32 %D
  490. }
  491. define i64 @test55(i32 %A) {
  492. %B = trunc i32 %A to i16
  493. %C = or i16 %B, -32574
  494. %D = and i16 %C, -25350
  495. %E = sext i16 %D to i64
  496. ret i64 %E
  497. ; CHECK-LABEL: @test55(
  498. ; CHECK-NEXT: %B = zext i32 %A to i64
  499. ; CHECK-NEXT: %C = and i64 %B, 7224
  500. ; CHECK-NEXT: %D = or i64 %C, -32574
  501. ; CHECK-NEXT: ret i64 %D
  502. }
  503. define i64 @test56(i16 %A) nounwind {
  504. %tmp353 = sext i16 %A to i32
  505. %tmp354 = lshr i32 %tmp353, 5
  506. %tmp355 = zext i32 %tmp354 to i64
  507. ret i64 %tmp355
  508. ; CHECK-LABEL: @test56(
  509. ; CHECK-NEXT: %tmp353 = sext i16 %A to i64
  510. ; CHECK-NEXT: %tmp354 = lshr i64 %tmp353, 5
  511. ; CHECK-NEXT: %tmp355 = and i64 %tmp354, 134217727
  512. ; CHECK-NEXT: ret i64 %tmp355
  513. }
  514. define i64 @test57(i64 %A) nounwind {
  515. %B = trunc i64 %A to i32
  516. %C = lshr i32 %B, 8
  517. %E = zext i32 %C to i64
  518. ret i64 %E
  519. ; CHECK-LABEL: @test57(
  520. ; CHECK-NEXT: %C = lshr i64 %A, 8
  521. ; CHECK-NEXT: %E = and i64 %C, 16777215
  522. ; CHECK-NEXT: ret i64 %E
  523. }
  524. define i64 @test58(i64 %A) nounwind {
  525. %B = trunc i64 %A to i32
  526. %C = lshr i32 %B, 8
  527. %D = or i32 %C, 128
  528. %E = zext i32 %D to i64
  529. ret i64 %E
  530. ; CHECK-LABEL: @test58(
  531. ; CHECK-NEXT: %C = lshr i64 %A, 8
  532. ; CHECK-NEXT: %D = and i64 %C, 16777087
  533. ; CHECK-NEXT: %E = or i64 %D, 128
  534. ; CHECK-NEXT: ret i64 %E
  535. }
  536. define i64 @test59(i8 %A, i8 %B) nounwind {
  537. %C = zext i8 %A to i32
  538. %D = shl i32 %C, 4
  539. %E = and i32 %D, 48
  540. %F = zext i8 %B to i32
  541. %G = lshr i32 %F, 4
  542. %H = or i32 %G, %E
  543. %I = zext i32 %H to i64
  544. ret i64 %I
  545. ; CHECK-LABEL: @test59(
  546. ; CHECK-NEXT: %C = zext i8 %A to i64
  547. ; CHECK-NOT: i32
  548. ; CHECK: %F = zext i8 %B to i64
  549. ; CHECK-NOT: i32
  550. ; CHECK: ret i64 %H
  551. }
  552. define <3 x i32> @test60(<4 x i32> %call4) nounwind {
  553. %tmp11 = bitcast <4 x i32> %call4 to i128
  554. %tmp9 = trunc i128 %tmp11 to i96
  555. %tmp10 = bitcast i96 %tmp9 to <3 x i32>
  556. ret <3 x i32> %tmp10
  557. ; CHECK-LABEL: @test60(
  558. ; CHECK-NEXT: shufflevector
  559. ; CHECK-NEXT: ret
  560. }
  561. define <4 x i32> @test61(<3 x i32> %call4) nounwind {
  562. %tmp11 = bitcast <3 x i32> %call4 to i96
  563. %tmp9 = zext i96 %tmp11 to i128
  564. %tmp10 = bitcast i128 %tmp9 to <4 x i32>
  565. ret <4 x i32> %tmp10
  566. ; CHECK-LABEL: @test61(
  567. ; CHECK-NEXT: shufflevector
  568. ; CHECK-NEXT: ret
  569. }
  570. define <4 x i32> @test62(<3 x float> %call4) nounwind {
  571. %tmp11 = bitcast <3 x float> %call4 to i96
  572. %tmp9 = zext i96 %tmp11 to i128
  573. %tmp10 = bitcast i128 %tmp9 to <4 x i32>
  574. ret <4 x i32> %tmp10
  575. ; CHECK-LABEL: @test62(
  576. ; CHECK-NEXT: bitcast
  577. ; CHECK-NEXT: shufflevector
  578. ; CHECK-NEXT: ret
  579. }
  580. ; PR7311 - Don't create invalid IR on scalar->vector cast.
  581. define <2 x float> @test63(i64 %tmp8) nounwind {
  582. entry:
  583. %a = bitcast i64 %tmp8 to <2 x i32>
  584. %vcvt.i = uitofp <2 x i32> %a to <2 x float>
  585. ret <2 x float> %vcvt.i
  586. ; CHECK-LABEL: @test63(
  587. ; CHECK: bitcast
  588. ; CHECK: uitofp
  589. }
  590. define <4 x float> @test64(<4 x float> %c) nounwind {
  591. %t0 = bitcast <4 x float> %c to <4 x i32>
  592. %t1 = bitcast <4 x i32> %t0 to <4 x float>
  593. ret <4 x float> %t1
  594. ; CHECK-LABEL: @test64(
  595. ; CHECK-NEXT: ret <4 x float> %c
  596. }
  597. define <4 x float> @test65(<4 x float> %c) nounwind {
  598. %t0 = bitcast <4 x float> %c to <2 x double>
  599. %t1 = bitcast <2 x double> %t0 to <4 x float>
  600. ret <4 x float> %t1
  601. ; CHECK-LABEL: @test65(
  602. ; CHECK-NEXT: ret <4 x float> %c
  603. }
  604. define <2 x float> @test66(<2 x float> %c) nounwind {
  605. %t0 = bitcast <2 x float> %c to double
  606. %t1 = bitcast double %t0 to <2 x float>
  607. ret <2 x float> %t1
  608. ; CHECK-LABEL: @test66(
  609. ; CHECK-NEXT: ret <2 x float> %c
  610. }
  611. define float @test2c() {
  612. ret float extractelement (<2 x float> bitcast (double bitcast (<2 x float> <float -1.000000e+00, float -1.000000e+00> to double) to <2 x float>), i32 0)
  613. ; CHECK-LABEL: @test2c(
  614. ; CHECK-NOT: extractelement
  615. }
  616. define i64 @test_mmx(<2 x i32> %c) nounwind {
  617. %A = bitcast <2 x i32> %c to x86_mmx
  618. %B = bitcast x86_mmx %A to <2 x i32>
  619. %C = bitcast <2 x i32> %B to i64
  620. ret i64 %C
  621. ; CHECK-LABEL: @test_mmx(
  622. ; CHECK-NOT: x86_mmx
  623. }
  624. define i64 @test_mmx_const(<2 x i32> %c) nounwind {
  625. %A = bitcast <2 x i32> zeroinitializer to x86_mmx
  626. %B = bitcast x86_mmx %A to <2 x i32>
  627. %C = bitcast <2 x i32> %B to i64
  628. ret i64 %C
  629. ; CHECK-LABEL: @test_mmx_const(
  630. ; CHECK-NOT: x86_mmx
  631. }
  632. ; PR12514
  633. define i1 @test67(i1 %a, i32 %b) {
  634. %tmp2 = zext i1 %a to i32
  635. %conv6 = xor i32 %tmp2, 1
  636. %and = and i32 %b, %conv6
  637. %sext = shl nuw nsw i32 %and, 24
  638. %neg.i = xor i32 %sext, -16777216
  639. %conv.i.i = ashr exact i32 %neg.i, 24
  640. %trunc = trunc i32 %conv.i.i to i8
  641. %tobool.i = icmp eq i8 %trunc, 0
  642. ret i1 %tobool.i
  643. ; CHECK-LABEL: @test67(
  644. ; CHECK: ret i1 false
  645. }
  646. %s = type { i32, i32, i32 }
  647. define %s @test68(%s *%p, i64 %i) {
  648. ; CHECK-LABEL: @test68(
  649. %o = mul i64 %i, 12
  650. %q = bitcast %s* %p to i8*
  651. %pp = getelementptr inbounds i8, i8* %q, i64 %o
  652. ; CHECK-NEXT: getelementptr %s, %s*
  653. %r = bitcast i8* %pp to %s*
  654. %l = load %s, %s* %r
  655. ; CHECK-NEXT: load %s, %s*
  656. ret %s %l
  657. ; CHECK-NEXT: ret %s
  658. }
  659. ; addrspacecasts should be eliminated.
  660. define %s @test68_addrspacecast(%s* %p, i64 %i) {
  661. ; CHECK-LABEL: @test68_addrspacecast(
  662. ; CHECK-NEXT: getelementptr %s, %s*
  663. ; CHECK-NEXT: load %s, %s*
  664. ; CHECK-NEXT: ret %s
  665. %o = mul i64 %i, 12
  666. %q = addrspacecast %s* %p to i8 addrspace(2)*
  667. %pp = getelementptr inbounds i8, i8 addrspace(2)* %q, i64 %o
  668. %r = addrspacecast i8 addrspace(2)* %pp to %s*
  669. %l = load %s, %s* %r
  670. ret %s %l
  671. }
  672. define %s @test68_addrspacecast_2(%s* %p, i64 %i) {
  673. ; CHECK-LABEL: @test68_addrspacecast_2(
  674. ; CHECK-NEXT: getelementptr %s, %s* %p
  675. ; CHECK-NEXT: addrspacecast
  676. ; CHECK-NEXT: load %s, %s addrspace(1)*
  677. ; CHECK-NEXT: ret %s
  678. %o = mul i64 %i, 12
  679. %q = addrspacecast %s* %p to i8 addrspace(2)*
  680. %pp = getelementptr inbounds i8, i8 addrspace(2)* %q, i64 %o
  681. %r = addrspacecast i8 addrspace(2)* %pp to %s addrspace(1)*
  682. %l = load %s, %s addrspace(1)* %r
  683. ret %s %l
  684. }
  685. define %s @test68_as1(%s addrspace(1)* %p, i32 %i) {
  686. ; CHECK-LABEL: @test68_as1(
  687. %o = mul i32 %i, 12
  688. %q = bitcast %s addrspace(1)* %p to i8 addrspace(1)*
  689. %pp = getelementptr inbounds i8, i8 addrspace(1)* %q, i32 %o
  690. ; CHECK-NEXT: getelementptr %s, %s addrspace(1)*
  691. %r = bitcast i8 addrspace(1)* %pp to %s addrspace(1)*
  692. %l = load %s, %s addrspace(1)* %r
  693. ; CHECK-NEXT: load %s, %s addrspace(1)*
  694. ret %s %l
  695. ; CHECK-NEXT: ret %s
  696. }
  697. define double @test69(double *%p, i64 %i) {
  698. ; CHECK-LABEL: @test69(
  699. %o = shl nsw i64 %i, 3
  700. %q = bitcast double* %p to i8*
  701. %pp = getelementptr inbounds i8, i8* %q, i64 %o
  702. ; CHECK-NEXT: getelementptr inbounds double, double*
  703. %r = bitcast i8* %pp to double*
  704. %l = load double, double* %r
  705. ; CHECK-NEXT: load double, double*
  706. ret double %l
  707. ; CHECK-NEXT: ret double
  708. }
  709. define %s @test70(%s *%p, i64 %i) {
  710. ; CHECK-LABEL: @test70(
  711. %o = mul nsw i64 %i, 36
  712. ; CHECK-NEXT: mul nsw i64 %i, 3
  713. %q = bitcast %s* %p to i8*
  714. %pp = getelementptr inbounds i8, i8* %q, i64 %o
  715. ; CHECK-NEXT: getelementptr inbounds %s, %s*
  716. %r = bitcast i8* %pp to %s*
  717. %l = load %s, %s* %r
  718. ; CHECK-NEXT: load %s, %s*
  719. ret %s %l
  720. ; CHECK-NEXT: ret %s
  721. }
  722. define double @test71(double *%p, i64 %i) {
  723. ; CHECK-LABEL: @test71(
  724. %o = shl i64 %i, 5
  725. ; CHECK-NEXT: shl i64 %i, 2
  726. %q = bitcast double* %p to i8*
  727. %pp = getelementptr i8, i8* %q, i64 %o
  728. ; CHECK-NEXT: getelementptr double, double*
  729. %r = bitcast i8* %pp to double*
  730. %l = load double, double* %r
  731. ; CHECK-NEXT: load double, double*
  732. ret double %l
  733. ; CHECK-NEXT: ret double
  734. }
  735. define double @test72(double *%p, i32 %i) {
  736. ; CHECK-LABEL: @test72(
  737. %so = shl nsw i32 %i, 3
  738. %o = sext i32 %so to i64
  739. ; CHECK-NEXT: sext i32 %i to i64
  740. %q = bitcast double* %p to i8*
  741. %pp = getelementptr inbounds i8, i8* %q, i64 %o
  742. ; CHECK-NEXT: getelementptr inbounds double, double*
  743. %r = bitcast i8* %pp to double*
  744. %l = load double, double* %r
  745. ; CHECK-NEXT: load double, double*
  746. ret double %l
  747. ; CHECK-NEXT: ret double
  748. }
  749. define double @test73(double *%p, i128 %i) {
  750. ; CHECK-LABEL: @test73(
  751. %lo = shl nsw i128 %i, 3
  752. %o = trunc i128 %lo to i64
  753. ; CHECK-NEXT: trunc i128 %i to i64
  754. %q = bitcast double* %p to i8*
  755. %pp = getelementptr inbounds i8, i8* %q, i64 %o
  756. ; CHECK-NEXT: getelementptr double, double*
  757. %r = bitcast i8* %pp to double*
  758. %l = load double, double* %r
  759. ; CHECK-NEXT: load double, double*
  760. ret double %l
  761. ; CHECK-NEXT: ret double
  762. }
  763. define double @test74(double *%p, i64 %i) {
  764. ; CHECK-LABEL: @test74(
  765. %q = bitcast double* %p to i64*
  766. %pp = getelementptr inbounds i64, i64* %q, i64 %i
  767. ; CHECK-NEXT: getelementptr inbounds double, double*
  768. %r = bitcast i64* %pp to double*
  769. %l = load double, double* %r
  770. ; CHECK-NEXT: load double, double*
  771. ret double %l
  772. ; CHECK-NEXT: ret double
  773. }
  774. define i32* @test75(i32* %p, i32 %x) {
  775. ; CHECK-LABEL: @test75(
  776. %y = shl i32 %x, 3
  777. ; CHECK-NEXT: shl i32 %x, 3
  778. %z = sext i32 %y to i64
  779. ; CHECK-NEXT: sext i32 %y to i64
  780. %q = bitcast i32* %p to i8*
  781. %r = getelementptr i8, i8* %q, i64 %z
  782. %s = bitcast i8* %r to i32*
  783. ret i32* %s
  784. }
  785. define %s @test76(%s *%p, i64 %i, i64 %j) {
  786. ; CHECK-LABEL: @test76(
  787. %o = mul i64 %i, 12
  788. %o2 = mul nsw i64 %o, %j
  789. ; CHECK-NEXT: %o2 = mul i64 %i, %j
  790. %q = bitcast %s* %p to i8*
  791. %pp = getelementptr inbounds i8, i8* %q, i64 %o2
  792. ; CHECK-NEXT: getelementptr %s, %s* %p, i64 %o2
  793. %r = bitcast i8* %pp to %s*
  794. %l = load %s, %s* %r
  795. ; CHECK-NEXT: load %s, %s*
  796. ret %s %l
  797. ; CHECK-NEXT: ret %s
  798. }
  799. define %s @test77(%s *%p, i64 %i, i64 %j) {
  800. ; CHECK-LABEL: @test77(
  801. %o = mul nsw i64 %i, 36
  802. %o2 = mul nsw i64 %o, %j
  803. ; CHECK-NEXT: %o = mul nsw i64 %i, 3
  804. ; CHECK-NEXT: %o2 = mul nsw i64 %o, %j
  805. %q = bitcast %s* %p to i8*
  806. %pp = getelementptr inbounds i8, i8* %q, i64 %o2
  807. ; CHECK-NEXT: getelementptr inbounds %s, %s* %p, i64 %o2
  808. %r = bitcast i8* %pp to %s*
  809. %l = load %s, %s* %r
  810. ; CHECK-NEXT: load %s, %s*
  811. ret %s %l
  812. ; CHECK-NEXT: ret %s
  813. }
  814. define %s @test78(%s *%p, i64 %i, i64 %j, i32 %k, i32 %l, i128 %m, i128 %n) {
  815. ; CHECK-LABEL: @test78(
  816. %a = mul nsw i32 %k, 36
  817. ; CHECK-NEXT: mul nsw i32 %k, 3
  818. %b = mul nsw i32 %a, %l
  819. ; CHECK-NEXT: mul nsw i32 %a, %l
  820. %c = sext i32 %b to i128
  821. ; CHECK-NEXT: sext i32 %b to i128
  822. %d = mul nsw i128 %c, %m
  823. ; CHECK-NEXT: mul nsw i128 %c, %m
  824. %e = mul i128 %d, %n
  825. ; CHECK-NEXT: mul i128 %d, %n
  826. %f = trunc i128 %e to i64
  827. ; CHECK-NEXT: trunc i128 %e to i64
  828. %g = mul nsw i64 %f, %i
  829. ; CHECK-NEXT: mul i64 %f, %i
  830. %h = mul nsw i64 %g, %j
  831. ; CHECK-NEXT: mul i64 %g, %j
  832. %q = bitcast %s* %p to i8*
  833. %pp = getelementptr inbounds i8, i8* %q, i64 %h
  834. ; CHECK-NEXT: getelementptr %s, %s* %p, i64 %h
  835. %r = bitcast i8* %pp to %s*
  836. %load = load %s, %s* %r
  837. ; CHECK-NEXT: load %s, %s*
  838. ret %s %load
  839. ; CHECK-NEXT: ret %s
  840. }
  841. define %s @test79(%s *%p, i64 %i, i32 %j) {
  842. ; CHECK-LABEL: @test79(
  843. %a = mul nsw i64 %i, 36
  844. ; CHECK: mul nsw i64 %i, 36
  845. %b = trunc i64 %a to i32
  846. %c = mul i32 %b, %j
  847. %q = bitcast %s* %p to i8*
  848. ; CHECK: bitcast
  849. %pp = getelementptr inbounds i8, i8* %q, i32 %c
  850. %r = bitcast i8* %pp to %s*
  851. %l = load %s, %s* %r
  852. ret %s %l
  853. }
  854. define double @test80([100 x double]* %p, i32 %i) {
  855. ; CHECK-LABEL: @test80(
  856. %tmp = shl nsw i32 %i, 3
  857. ; CHECK-NEXT: sext i32 %i to i64
  858. %q = bitcast [100 x double]* %p to i8*
  859. %pp = getelementptr i8, i8* %q, i32 %tmp
  860. ; CHECK-NEXT: getelementptr [100 x double], [100 x double]*
  861. %r = bitcast i8* %pp to double*
  862. %l = load double, double* %r
  863. ; CHECK-NEXT: load double, double*
  864. ret double %l
  865. ; CHECK-NEXT: ret double
  866. }
  867. define double @test80_addrspacecast([100 x double] addrspace(1)* %p, i32 %i) {
  868. ; CHECK-LABEL: @test80_addrspacecast(
  869. ; CHECK-NEXT: getelementptr [100 x double], [100 x double] addrspace(1)* %p
  870. ; CHECK-NEXT: load double, double addrspace(1)*
  871. ; CHECK-NEXT: ret double
  872. %tmp = shl nsw i32 %i, 3
  873. %q = addrspacecast [100 x double] addrspace(1)* %p to i8 addrspace(2)*
  874. %pp = getelementptr i8, i8 addrspace(2)* %q, i32 %tmp
  875. %r = addrspacecast i8 addrspace(2)* %pp to double addrspace(1)*
  876. %l = load double, double addrspace(1)* %r
  877. ret double %l
  878. }
  879. define double @test80_addrspacecast_2([100 x double] addrspace(1)* %p, i32 %i) {
  880. ; CHECK-LABEL: @test80_addrspacecast_2(
  881. ; CHECK-NEXT: getelementptr [100 x double], [100 x double] addrspace(1)*
  882. ; CHECK-NEXT: addrspacecast double addrspace(1)*
  883. ; CHECK-NEXT: load double, double addrspace(3)*
  884. ; CHECK-NEXT: ret double
  885. %tmp = shl nsw i32 %i, 3
  886. %q = addrspacecast [100 x double] addrspace(1)* %p to i8 addrspace(2)*
  887. %pp = getelementptr i8, i8 addrspace(2)* %q, i32 %tmp
  888. %r = addrspacecast i8 addrspace(2)* %pp to double addrspace(3)*
  889. %l = load double, double addrspace(3)* %r
  890. ret double %l
  891. }
  892. define double @test80_as1([100 x double] addrspace(1)* %p, i16 %i) {
  893. ; CHECK-LABEL: @test80_as1(
  894. %tmp = shl nsw i16 %i, 3
  895. ; CHECK-NEXT: sext i16 %i to i32
  896. %q = bitcast [100 x double] addrspace(1)* %p to i8 addrspace(1)*
  897. %pp = getelementptr i8, i8 addrspace(1)* %q, i16 %tmp
  898. ; CHECK-NEXT: getelementptr [100 x double], [100 x double] addrspace(1)*
  899. %r = bitcast i8 addrspace(1)* %pp to double addrspace(1)*
  900. %l = load double, double addrspace(1)* %r
  901. ; CHECK-NEXT: load double, double addrspace(1)*
  902. ret double %l
  903. ; CHECK-NEXT: ret double
  904. }
  905. define double @test81(double *%p, float %f) {
  906. %i = fptosi float %f to i64
  907. %q = bitcast double* %p to i8*
  908. %pp = getelementptr i8, i8* %q, i64 %i
  909. %r = bitcast i8* %pp to double*
  910. %l = load double, double* %r
  911. ret double %l
  912. }
  913. define i64 @test82(i64 %A) nounwind {
  914. %B = trunc i64 %A to i32
  915. %C = lshr i32 %B, 8
  916. %D = shl i32 %C, 9
  917. %E = zext i32 %D to i64
  918. ret i64 %E
  919. ; CHECK-LABEL: @test82(
  920. ; CHECK-NEXT: [[REG:%[0-9]*]] = shl i64 %A, 1
  921. ; CHECK-NEXT: %E = and i64 [[REG]], 4294966784
  922. ; CHECK-NEXT: ret i64 %E
  923. }
  924. ; PR15959
  925. define i64 @test83(i16 %a, i64 %k) {
  926. %conv = sext i16 %a to i32
  927. %sub = add nsw i64 %k, -1
  928. %sh_prom = trunc i64 %sub to i32
  929. %shl = shl i32 %conv, %sh_prom
  930. %sh_prom1 = zext i32 %shl to i64
  931. ret i64 %sh_prom1
  932. ; CHECK-LABEL: @test83(
  933. ; CHECK: %sub = add i64 %k, 4294967295
  934. ; CHECK: %sh_prom = trunc i64 %sub to i32
  935. ; CHECK: %shl = shl i32 %conv, %sh_prom
  936. }
  937. define i8 @test84(i32 %a) {
  938. %add = add nsw i32 %a, -16777216
  939. %shr = lshr exact i32 %add, 23
  940. %trunc = trunc i32 %shr to i8
  941. ret i8 %trunc
  942. ; CHECK-LABEL: @test84(
  943. ; CHECK: [[ADD:%.*]] = add i32 %a, 2130706432
  944. ; CHECK: [[SHR:%.*]] = lshr exact i32 [[ADD]], 23
  945. ; CHECK: [[CST:%.*]] = trunc i32 [[SHR]] to i8
  946. }
  947. define i8 @test85(i32 %a) {
  948. %add = add nuw i32 %a, -16777216
  949. %shr = lshr exact i32 %add, 23
  950. %trunc = trunc i32 %shr to i8
  951. ret i8 %trunc
  952. ; CHECK-LABEL: @test85(
  953. ; CHECK: [[ADD:%.*]] = add i32 %a, 2130706432
  954. ; CHECK: [[SHR:%.*]] = lshr exact i32 [[ADD]], 23
  955. ; CHECK: [[CST:%.*]] = trunc i32 [[SHR]] to i8
  956. }
  957. ; Overflow on a float to int or int to float conversion is undefined (PR21130).
  958. define i8 @overflow_fptosi() {
  959. %i = fptosi double 1.56e+02 to i8
  960. ret i8 %i
  961. ; CHECK-LABEL: @overflow_fptosi(
  962. ; CHECK-NEXT: ret i8 undef
  963. }
  964. define i8 @overflow_fptoui() {
  965. %i = fptoui double 2.56e+02 to i8
  966. ret i8 %i
  967. ; CHECK-LABEL: @overflow_fptoui(
  968. ; CHECK-NEXT: ret i8 undef
  969. }
  970. ; The maximum float is approximately 2 ** 128 which is 3.4E38.
  971. ; The constant below is 4E38. Use a 130 bit integer to hold that
  972. ; number; 129-bits for the value + 1 bit for the sign.
  973. define float @overflow_uitofp() {
  974. %i = uitofp i130 400000000000000000000000000000000000000 to float
  975. ret float %i
  976. ; CHECK-LABEL: @overflow_uitofp(
  977. ; CHECK-NEXT: ret float undef
  978. }
  979. define float @overflow_sitofp() {
  980. %i = sitofp i130 400000000000000000000000000000000000000 to float
  981. ret float %i
  982. ; CHECK-LABEL: @overflow_sitofp(
  983. ; CHECK-NEXT: ret float undef
  984. }
  985. define i32 @PR21388(i32* %v) {
  986. %icmp = icmp slt i32* %v, null
  987. %sext = sext i1 %icmp to i32
  988. ret i32 %sext
  989. ; CHECK-LABEL: @PR21388(
  990. ; CHECK-NEXT: %[[icmp:.*]] = icmp slt i32* %v, null
  991. ; CHECK-NEXT: %[[sext:.*]] = sext i1 %[[icmp]] to i32
  992. ; CHECK-NEXT: ret i32 %[[sext]]
  993. }
  994. define float @sitofp_zext(i16 %a) {
  995. ; CHECK-LABEL: @sitofp_zext(
  996. ; CHECK-NEXT: %[[sitofp:.*]] = uitofp i16 %a to float
  997. ; CHECK-NEXT: ret float %[[sitofp]]
  998. %zext = zext i16 %a to i32
  999. %sitofp = sitofp i32 %zext to float
  1000. ret float %sitofp
  1001. }
  1002. define i1 @PR23309(i32 %A, i32 %B) {
  1003. ; CHECK-LABEL: @PR23309(
  1004. ; CHECK-NEXT: %[[sub:.*]] = sub i32 %A, %B
  1005. ; CHECK-NEXT: %[[and:.*]] = and i32 %[[sub]], 1
  1006. ; CHECK-NEXT: %[[cmp:.*]] = icmp ne i32 %[[and]], 0
  1007. ; CHECK-NEXT: ret i1 %[[cmp]]
  1008. %add = add i32 %A, -4
  1009. %sub = sub nsw i32 %add, %B
  1010. %trunc = trunc i32 %sub to i1
  1011. ret i1 %trunc
  1012. }
  1013. define i1 @PR23309v2(i32 %A, i32 %B) {
  1014. ; CHECK-LABEL: @PR23309v2(
  1015. ; CHECK-NEXT: %[[sub:.*]] = add i32 %A, %B
  1016. ; CHECK-NEXT: %[[and:.*]] = and i32 %[[sub]], 1
  1017. ; CHECK-NEXT: %[[cmp:.*]] = icmp ne i32 %[[and]], 0
  1018. ; CHECK-NEXT: ret i1 %[[cmp]]
  1019. %add = add i32 %A, -4
  1020. %sub = add nuw i32 %add, %B
  1021. %trunc = trunc i32 %sub to i1
  1022. ret i1 %trunc
  1023. }