shift.ll 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. ; This test makes sure that these instructions are properly eliminated.
  2. ;
  3. ; RUN: opt < %s -instcombine -S | FileCheck %s
  4. define i32 @test1(i32 %A) {
  5. ; CHECK-LABEL: @test1(
  6. ; CHECK: ret i32 %A
  7. %B = shl i32 %A, 0 ; <i32> [#uses=1]
  8. ret i32 %B
  9. }
  10. define i32 @test2(i8 %A) {
  11. ; CHECK-LABEL: @test2(
  12. ; CHECK: ret i32 0
  13. %shift.upgrd.1 = zext i8 %A to i32 ; <i32> [#uses=1]
  14. %B = shl i32 0, %shift.upgrd.1 ; <i32> [#uses=1]
  15. ret i32 %B
  16. }
  17. define i32 @test3(i32 %A) {
  18. ; CHECK-LABEL: @test3(
  19. ; CHECK: ret i32 %A
  20. %B = ashr i32 %A, 0 ; <i32> [#uses=1]
  21. ret i32 %B
  22. }
  23. define i32 @test4(i8 %A) {
  24. ; CHECK-LABEL: @test4(
  25. ; CHECK: ret i32 0
  26. %shift.upgrd.2 = zext i8 %A to i32 ; <i32> [#uses=1]
  27. %B = ashr i32 0, %shift.upgrd.2 ; <i32> [#uses=1]
  28. ret i32 %B
  29. }
  30. define i32 @test5(i32 %A) {
  31. ; CHECK-LABEL: @test5(
  32. ; CHECK: ret i32 undef
  33. %B = lshr i32 %A, 32 ;; shift all bits out
  34. ret i32 %B
  35. }
  36. define <4 x i32> @test5_splat_vector(<4 x i32> %A) {
  37. ; CHECK-LABEL: @test5_splat_vector(
  38. ; CHECK: ret <4 x i32> undef
  39. %B = lshr <4 x i32> %A, <i32 32, i32 32, i32 32, i32 32> ;; shift all bits out
  40. ret <4 x i32> %B
  41. }
  42. define <4 x i32> @test5_zero_vector(<4 x i32> %A) {
  43. ; CHECK-LABEL: @test5_zero_vector(
  44. ; CHECK-NEXT: ret <4 x i32> %A
  45. %B = lshr <4 x i32> %A, zeroinitializer
  46. ret <4 x i32> %B
  47. }
  48. define <4 x i32> @test5_non_splat_vector(<4 x i32> %A) {
  49. ; CHECK-LABEL: @test5_non_splat_vector(
  50. ; CHECK-NOT: ret <4 x i32> undef
  51. %B = lshr <4 x i32> %A, <i32 32, i32 1, i32 2, i32 3>
  52. ret <4 x i32> %B
  53. }
  54. define i32 @test5a(i32 %A) {
  55. ; CHECK-LABEL: @test5a(
  56. ; CHECK: ret i32 undef
  57. %B = shl i32 %A, 32 ;; shift all bits out
  58. ret i32 %B
  59. }
  60. define <4 x i32> @test5a_splat_vector(<4 x i32> %A) {
  61. ; CHECK-LABEL: @test5a_splat_vector(
  62. ; CHECK: ret <4 x i32> undef
  63. %B = shl <4 x i32> %A, <i32 32, i32 32, i32 32, i32 32> ;; shift all bits out
  64. ret <4 x i32> %B
  65. }
  66. define <4 x i32> @test5a_non_splat_vector(<4 x i32> %A) {
  67. ; CHECK-LABEL: @test5a_non_splat_vector(
  68. ; CHECK-NOT: ret <4 x i32> undef
  69. %B = shl <4 x i32> %A, <i32 32, i32 1, i32 2, i32 3>
  70. ret <4 x i32> %B
  71. }
  72. define i32 @test5b() {
  73. ; CHECK-LABEL: @test5b(
  74. ; CHECK: ret i32 0
  75. %B = ashr i32 undef, 2 ;; top two bits must be equal, so not undef
  76. ret i32 %B
  77. }
  78. define i32 @test5b2(i32 %A) {
  79. ; CHECK-LABEL: @test5b2(
  80. ; CHECK: ret i32 0
  81. %B = ashr i32 undef, %A ;; top %A bits must be equal, so not undef
  82. ret i32 %B
  83. }
  84. define i32 @test6(i32 %A) {
  85. ; CHECK-LABEL: @test6(
  86. ; CHECK-NEXT: mul i32 %A, 6
  87. ; CHECK-NEXT: ret i32
  88. %B = shl i32 %A, 1 ;; convert to an mul instruction
  89. %C = mul i32 %B, 3
  90. ret i32 %C
  91. }
  92. define i32 @test6a(i32 %A) {
  93. ; CHECK-LABEL: @test6a(
  94. ; CHECK-NEXT: mul i32 %A, 6
  95. ; CHECK-NEXT: ret i32
  96. %B = mul i32 %A, 3
  97. %C = shl i32 %B, 1 ;; convert to an mul instruction
  98. ret i32 %C
  99. }
  100. define i32 @test7(i8 %A) {
  101. ; CHECK-LABEL: @test7(
  102. ; CHECK-NEXT: ret i32 -1
  103. %shift.upgrd.3 = zext i8 %A to i32
  104. %B = ashr i32 -1, %shift.upgrd.3 ;; Always equal to -1
  105. ret i32 %B
  106. }
  107. ;; (A << 5) << 3 === A << 8 == 0
  108. define i8 @test8(i8 %A) {
  109. ; CHECK-LABEL: @test8(
  110. ; CHECK: ret i8 0
  111. %B = shl i8 %A, 5 ; <i8> [#uses=1]
  112. %C = shl i8 %B, 3 ; <i8> [#uses=1]
  113. ret i8 %C
  114. }
  115. ;; (A << 7) >> 7 === A & 1
  116. define i8 @test9(i8 %A) {
  117. ; CHECK-LABEL: @test9(
  118. ; CHECK-NEXT: and i8 %A, 1
  119. ; CHECK-NEXT: ret i8
  120. %B = shl i8 %A, 7 ; <i8> [#uses=1]
  121. %C = lshr i8 %B, 7 ; <i8> [#uses=1]
  122. ret i8 %C
  123. }
  124. ;; This transformation is deferred to DAGCombine:
  125. ;; (A >> 7) << 7 === A & 128
  126. ;; The shl may be valuable to scalar evolution.
  127. define i8 @test10(i8 %A) {
  128. ; CHECK-LABEL: @test10(
  129. ; CHECK-NEXT: and i8 %A, -128
  130. ; CHECK-NEXT: ret i8
  131. %B = lshr i8 %A, 7 ; <i8> [#uses=1]
  132. %C = shl i8 %B, 7 ; <i8> [#uses=1]
  133. ret i8 %C
  134. }
  135. ;; Allow the simplification when the lshr shift is exact.
  136. define i8 @test10a(i8 %A) {
  137. ; CHECK-LABEL: @test10a(
  138. ; CHECK-NEXT: ret i8 %A
  139. %B = lshr exact i8 %A, 7
  140. %C = shl i8 %B, 7
  141. ret i8 %C
  142. }
  143. ;; This transformation is deferred to DAGCombine:
  144. ;; (A >> 3) << 4 === (A & 0x1F) << 1
  145. ;; The shl may be valuable to scalar evolution.
  146. define i8 @test11(i8 %A) {
  147. ; CHECK-LABEL: @test11(
  148. ; CHECK: shl i8
  149. ; CHECK-NEXT: ret i8
  150. %a = mul i8 %A, 3 ; <i8> [#uses=1]
  151. %B = lshr i8 %a, 3 ; <i8> [#uses=1]
  152. %C = shl i8 %B, 4 ; <i8> [#uses=1]
  153. ret i8 %C
  154. }
  155. ;; Allow the simplification in InstCombine when the lshr shift is exact.
  156. define i8 @test11a(i8 %A) {
  157. ; CHECK-LABEL: @test11a(
  158. ; CHECK-NEXT: mul i8 %A, 6
  159. ; CHECK-NEXT: ret i8
  160. %a = mul i8 %A, 3
  161. %B = lshr exact i8 %a, 3
  162. %C = shl i8 %B, 4
  163. ret i8 %C
  164. }
  165. ;; This is deferred to DAGCombine unless %B is single-use.
  166. ;; (A >> 8) << 8 === A & -256
  167. define i32 @test12(i32 %A) {
  168. ; CHECK-LABEL: @test12(
  169. ; CHECK-NEXT: and i32 %A, -256
  170. ; CHECK-NEXT: ret i32
  171. %B = ashr i32 %A, 8 ; <i32> [#uses=1]
  172. %C = shl i32 %B, 8 ; <i32> [#uses=1]
  173. ret i32 %C
  174. }
  175. ;; This transformation is deferred to DAGCombine:
  176. ;; (A >> 3) << 4 === (A & -8) * 2
  177. ;; The shl may be valuable to scalar evolution.
  178. define i8 @test13(i8 %A) {
  179. ; CHECK-LABEL: @test13(
  180. ; CHECK: shl i8
  181. ; CHECK-NEXT: ret i8
  182. %a = mul i8 %A, 3 ; <i8> [#uses=1]
  183. %B = ashr i8 %a, 3 ; <i8> [#uses=1]
  184. %C = shl i8 %B, 4 ; <i8> [#uses=1]
  185. ret i8 %C
  186. }
  187. define i8 @test13a(i8 %A) {
  188. ; CHECK-LABEL: @test13a(
  189. ; CHECK-NEXT: mul i8 %A, 6
  190. ; CHECK-NEXT: ret i8
  191. %a = mul i8 %A, 3
  192. %B = ashr exact i8 %a, 3
  193. %C = shl i8 %B, 4
  194. ret i8 %C
  195. }
  196. ;; D = ((B | 1234) << 4) === ((B << 4)|(1234 << 4)
  197. define i32 @test14(i32 %A) {
  198. ; CHECK-LABEL: @test14(
  199. ; CHECK-NEXT: %B = and i32 %A, -19760
  200. ; CHECK-NEXT: or i32 %B, 19744
  201. ; CHECK-NEXT: ret i32
  202. %B = lshr i32 %A, 4 ; <i32> [#uses=1]
  203. %C = or i32 %B, 1234 ; <i32> [#uses=1]
  204. %D = shl i32 %C, 4 ; <i32> [#uses=1]
  205. ret i32 %D
  206. }
  207. ;; D = ((B | 1234) << 4) === ((B << 4)|(1234 << 4)
  208. define i32 @test14a(i32 %A) {
  209. ; CHECK-LABEL: @test14a(
  210. ; CHECK-NEXT: and i32 %A, 77
  211. ; CHECK-NEXT: ret i32
  212. %B = shl i32 %A, 4 ; <i32> [#uses=1]
  213. %C = and i32 %B, 1234 ; <i32> [#uses=1]
  214. %D = lshr i32 %C, 4 ; <i32> [#uses=1]
  215. ret i32 %D
  216. }
  217. define i32 @test15(i1 %C) {
  218. ; CHECK-LABEL: @test15(
  219. ; CHECK-NEXT: select i1 %C, i32 12, i32 4
  220. ; CHECK-NEXT: ret i32
  221. %A = select i1 %C, i32 3, i32 1 ; <i32> [#uses=1]
  222. %V = shl i32 %A, 2 ; <i32> [#uses=1]
  223. ret i32 %V
  224. }
  225. define i32 @test15a(i1 %C) {
  226. ; CHECK-LABEL: @test15a(
  227. ; CHECK-NEXT: select i1 %C, i32 512, i32 128
  228. ; CHECK-NEXT: ret i32
  229. %A = select i1 %C, i8 3, i8 1 ; <i8> [#uses=1]
  230. %shift.upgrd.4 = zext i8 %A to i32 ; <i32> [#uses=1]
  231. %V = shl i32 64, %shift.upgrd.4 ; <i32> [#uses=1]
  232. ret i32 %V
  233. }
  234. define i1 @test16(i32 %X) {
  235. ; CHECK-LABEL: @test16(
  236. ; CHECK-NEXT: and i32 %X, 16
  237. ; CHECK-NEXT: icmp ne i32
  238. ; CHECK-NEXT: ret i1
  239. %tmp.3 = ashr i32 %X, 4
  240. %tmp.6 = and i32 %tmp.3, 1
  241. %tmp.7 = icmp ne i32 %tmp.6, 0
  242. ret i1 %tmp.7
  243. }
  244. define i1 @test17(i32 %A) {
  245. ; CHECK-LABEL: @test17(
  246. ; CHECK-NEXT: and i32 %A, -8
  247. ; CHECK-NEXT: icmp eq i32
  248. ; CHECK-NEXT: ret i1
  249. %B = lshr i32 %A, 3 ; <i32> [#uses=1]
  250. %C = icmp eq i32 %B, 1234 ; <i1> [#uses=1]
  251. ret i1 %C
  252. }
  253. define i1 @test18(i8 %A) {
  254. ; CHECK-LABEL: @test18(
  255. ; CHECK: ret i1 false
  256. %B = lshr i8 %A, 7 ; <i8> [#uses=1]
  257. ;; false
  258. %C = icmp eq i8 %B, 123 ; <i1> [#uses=1]
  259. ret i1 %C
  260. }
  261. define i1 @test19(i32 %A) {
  262. ; CHECK-LABEL: @test19(
  263. ; CHECK-NEXT: icmp ult i32 %A, 4
  264. ; CHECK-NEXT: ret i1
  265. %B = ashr i32 %A, 2 ; <i32> [#uses=1]
  266. ;; (X & -4) == 0
  267. %C = icmp eq i32 %B, 0 ; <i1> [#uses=1]
  268. ret i1 %C
  269. }
  270. define i1 @test19a(i32 %A) {
  271. ; CHECK-LABEL: @test19a(
  272. ; CHECK-NEXT: icmp ugt i32 %A, -5
  273. ; CHECK-NEXT: ret i1
  274. %B = ashr i32 %A, 2 ; <i32> [#uses=1]
  275. ;; X >u ~4
  276. %C = icmp eq i32 %B, -1 ; <i1> [#uses=1]
  277. ret i1 %C
  278. }
  279. define i1 @test20(i8 %A) {
  280. ; CHECK-LABEL: @test20(
  281. ; CHECK: ret i1 false
  282. %B = ashr i8 %A, 7 ; <i8> [#uses=1]
  283. ;; false
  284. %C = icmp eq i8 %B, 123 ; <i1> [#uses=1]
  285. ret i1 %C
  286. }
  287. define i1 @test21(i8 %A) {
  288. ; CHECK-LABEL: @test21(
  289. ; CHECK-NEXT: and i8 %A, 15
  290. ; CHECK-NEXT: icmp eq i8
  291. ; CHECK-NEXT: ret i1
  292. %B = shl i8 %A, 4 ; <i8> [#uses=1]
  293. %C = icmp eq i8 %B, -128 ; <i1> [#uses=1]
  294. ret i1 %C
  295. }
  296. define i1 @test22(i8 %A) {
  297. ; CHECK-LABEL: @test22(
  298. ; CHECK-NEXT: and i8 %A, 15
  299. ; CHECK-NEXT: icmp eq i8
  300. ; CHECK-NEXT: ret i1
  301. %B = shl i8 %A, 4 ; <i8> [#uses=1]
  302. %C = icmp eq i8 %B, 0 ; <i1> [#uses=1]
  303. ret i1 %C
  304. }
  305. define i8 @test23(i32 %A) {
  306. ; CHECK-LABEL: @test23(
  307. ; CHECK-NEXT: trunc i32 %A to i8
  308. ; CHECK-NEXT: ret i8
  309. ;; casts not needed
  310. %B = shl i32 %A, 24 ; <i32> [#uses=1]
  311. %C = ashr i32 %B, 24 ; <i32> [#uses=1]
  312. %D = trunc i32 %C to i8 ; <i8> [#uses=1]
  313. ret i8 %D
  314. }
  315. define i8 @test24(i8 %X) {
  316. ; CHECK-LABEL: @test24(
  317. ; CHECK-NEXT: and i8 %X, 3
  318. ; CHECK-NEXT: ret i8
  319. %Y = and i8 %X, -5 ; <i8> [#uses=1]
  320. %Z = shl i8 %Y, 5 ; <i8> [#uses=1]
  321. %Q = ashr i8 %Z, 5 ; <i8> [#uses=1]
  322. ret i8 %Q
  323. }
  324. define i32 @test25(i32 %tmp.2, i32 %AA) {
  325. ; CHECK-LABEL: @test25(
  326. ; CHECK-NEXT: and i32 %tmp.2, -131072
  327. ; CHECK-NEXT: add i32 %{{[^,]*}}, %AA
  328. ; CHECK-NEXT: and i32 %{{[^,]*}}, -131072
  329. ; CHECK-NEXT: ret i32
  330. %x = lshr i32 %AA, 17 ; <i32> [#uses=1]
  331. %tmp.3 = lshr i32 %tmp.2, 17 ; <i32> [#uses=1]
  332. %tmp.5 = add i32 %tmp.3, %x ; <i32> [#uses=1]
  333. %tmp.6 = shl i32 %tmp.5, 17 ; <i32> [#uses=1]
  334. ret i32 %tmp.6
  335. }
  336. define <2 x i32> @test25_vector(<2 x i32> %tmp.2, <2 x i32> %AA) {
  337. ; CHECK-LABEL: @test25_vector(
  338. ; CHECK: %tmp.3 = lshr <2 x i32> %tmp.2, <i32 17, i32 17>
  339. ; CHECK-NEXT: shl <2 x i32> %tmp.3, <i32 17, i32 17>
  340. ; CHECK-NEXT: add <2 x i32> %tmp.51, %AA
  341. ; CHECK-NEXT: and <2 x i32> %x2, <i32 -131072, i32 -131072>
  342. ; CHECK-NEXT: ret <2 x i32>
  343. %x = lshr <2 x i32> %AA, <i32 17, i32 17>
  344. %tmp.3 = lshr <2 x i32> %tmp.2, <i32 17, i32 17>
  345. %tmp.5 = add <2 x i32> %tmp.3, %x
  346. %tmp.6 = shl <2 x i32> %tmp.5, <i32 17, i32 17>
  347. ret <2 x i32> %tmp.6
  348. }
  349. ;; handle casts between shifts.
  350. define i32 @test26(i32 %A) {
  351. ; CHECK-LABEL: @test26(
  352. ; CHECK-NEXT: and i32 %A, -2
  353. ; CHECK-NEXT: ret i32
  354. %B = lshr i32 %A, 1 ; <i32> [#uses=1]
  355. %C = bitcast i32 %B to i32 ; <i32> [#uses=1]
  356. %D = shl i32 %C, 1 ; <i32> [#uses=1]
  357. ret i32 %D
  358. }
  359. define i1 @test27(i32 %x) nounwind {
  360. ; CHECK-LABEL: @test27(
  361. ; CHECK-NEXT: and i32 %x, 8
  362. ; CHECK-NEXT: icmp ne i32
  363. ; CHECK-NEXT: ret i1
  364. %y = lshr i32 %x, 3
  365. %z = trunc i32 %y to i1
  366. ret i1 %z
  367. }
  368. define i8 @test28(i8 %x) {
  369. entry:
  370. ; CHECK-LABEL: @test28(
  371. ; CHECK: icmp slt i8 %x, 0
  372. ; CHECK-NEXT: br i1
  373. %tmp1 = lshr i8 %x, 7
  374. %cond1 = icmp ne i8 %tmp1, 0
  375. br i1 %cond1, label %bb1, label %bb2
  376. bb1:
  377. ret i8 0
  378. bb2:
  379. ret i8 1
  380. }
  381. define i8 @test28a(i8 %x, i8 %y) {
  382. entry:
  383. ; This shouldn't be transformed.
  384. ; CHECK-LABEL: @test28a(
  385. ; CHECK: %tmp1 = lshr i8 %x, 7
  386. ; CHECK: %cond1 = icmp eq i8 %tmp1, 0
  387. ; CHECK: br i1 %cond1, label %bb2, label %bb1
  388. %tmp1 = lshr i8 %x, 7
  389. %cond1 = icmp ne i8 %tmp1, 0
  390. br i1 %cond1, label %bb1, label %bb2
  391. bb1:
  392. ret i8 %tmp1
  393. bb2:
  394. %tmp2 = add i8 %tmp1, %y
  395. ret i8 %tmp2
  396. }
  397. define i32 @test29(i64 %d18) {
  398. entry:
  399. %tmp916 = lshr i64 %d18, 32
  400. %tmp917 = trunc i64 %tmp916 to i32
  401. %tmp10 = lshr i32 %tmp917, 31
  402. ret i32 %tmp10
  403. ; CHECK-LABEL: @test29(
  404. ; CHECK: %tmp916 = lshr i64 %d18, 63
  405. ; CHECK: %tmp10 = trunc i64 %tmp916 to i32
  406. }
  407. define i32 @test30(i32 %A, i32 %B, i32 %C) {
  408. %X = shl i32 %A, %C
  409. %Y = shl i32 %B, %C
  410. %Z = and i32 %X, %Y
  411. ret i32 %Z
  412. ; CHECK-LABEL: @test30(
  413. ; CHECK: %X1 = and i32 %A, %B
  414. ; CHECK: %Z = shl i32 %X1, %C
  415. }
  416. define i32 @test31(i32 %A, i32 %B, i32 %C) {
  417. %X = lshr i32 %A, %C
  418. %Y = lshr i32 %B, %C
  419. %Z = or i32 %X, %Y
  420. ret i32 %Z
  421. ; CHECK-LABEL: @test31(
  422. ; CHECK: %X1 = or i32 %A, %B
  423. ; CHECK: %Z = lshr i32 %X1, %C
  424. }
  425. define i32 @test32(i32 %A, i32 %B, i32 %C) {
  426. %X = ashr i32 %A, %C
  427. %Y = ashr i32 %B, %C
  428. %Z = xor i32 %X, %Y
  429. ret i32 %Z
  430. ; CHECK-LABEL: @test32(
  431. ; CHECK: %X1 = xor i32 %A, %B
  432. ; CHECK: %Z = ashr i32 %X1, %C
  433. ; CHECK: ret i32 %Z
  434. }
  435. define i1 @test33(i32 %X) {
  436. %tmp1 = shl i32 %X, 7
  437. %tmp2 = icmp slt i32 %tmp1, 0
  438. ret i1 %tmp2
  439. ; CHECK-LABEL: @test33(
  440. ; CHECK: %tmp1.mask = and i32 %X, 16777216
  441. ; CHECK: %tmp2 = icmp ne i32 %tmp1.mask, 0
  442. }
  443. define i1 @test34(i32 %X) {
  444. %tmp1 = lshr i32 %X, 7
  445. %tmp2 = icmp slt i32 %tmp1, 0
  446. ret i1 %tmp2
  447. ; CHECK-LABEL: @test34(
  448. ; CHECK: ret i1 false
  449. }
  450. define i1 @test35(i32 %X) {
  451. %tmp1 = ashr i32 %X, 7
  452. %tmp2 = icmp slt i32 %tmp1, 0
  453. ret i1 %tmp2
  454. ; CHECK-LABEL: @test35(
  455. ; CHECK: %tmp2 = icmp slt i32 %X, 0
  456. ; CHECK: ret i1 %tmp2
  457. }
  458. define i128 @test36(i128 %A, i128 %B) {
  459. entry:
  460. %tmp27 = shl i128 %A, 64
  461. %tmp23 = shl i128 %B, 64
  462. %ins = or i128 %tmp23, %tmp27
  463. %tmp45 = lshr i128 %ins, 64
  464. ret i128 %tmp45
  465. ; CHECK-LABEL: @test36(
  466. ; CHECK: %tmp231 = or i128 %B, %A
  467. ; CHECK: %ins = and i128 %tmp231, 18446744073709551615
  468. ; CHECK: ret i128 %ins
  469. }
  470. define i64 @test37(i128 %A, i32 %B) {
  471. entry:
  472. %tmp27 = shl i128 %A, 64
  473. %tmp22 = zext i32 %B to i128
  474. %tmp23 = shl i128 %tmp22, 96
  475. %ins = or i128 %tmp23, %tmp27
  476. %tmp45 = lshr i128 %ins, 64
  477. %tmp46 = trunc i128 %tmp45 to i64
  478. ret i64 %tmp46
  479. ; CHECK-LABEL: @test37(
  480. ; CHECK: %tmp23 = shl nuw nsw i128 %tmp22, 32
  481. ; CHECK: %ins = or i128 %tmp23, %A
  482. ; CHECK: %tmp46 = trunc i128 %ins to i64
  483. }
  484. define i32 @test38(i32 %x) nounwind readnone {
  485. %rem = srem i32 %x, 32
  486. %shl = shl i32 1, %rem
  487. ret i32 %shl
  488. ; CHECK-LABEL: @test38(
  489. ; CHECK-NEXT: and i32 %x, 31
  490. ; CHECK-NEXT: shl i32 1
  491. ; CHECK-NEXT: ret i32
  492. }
  493. ; <rdar://problem/8756731>
  494. ; CHECK-LABEL: @test39(
  495. define i8 @test39(i32 %a0) {
  496. entry:
  497. %tmp4 = trunc i32 %a0 to i8
  498. ; CHECK: and i8 %tmp49, 64
  499. %tmp5 = shl i8 %tmp4, 5
  500. %tmp48 = and i8 %tmp5, 32
  501. %tmp49 = lshr i8 %tmp48, 5
  502. %tmp50 = mul i8 %tmp49, 64
  503. %tmp51 = xor i8 %tmp50, %tmp5
  504. %tmp52 = and i8 %tmp51, -128
  505. %tmp53 = lshr i8 %tmp52, 7
  506. %tmp54 = mul i8 %tmp53, 16
  507. ; CHECK: %0 = shl i8 %tmp4, 2
  508. ; CHECK: %tmp54 = and i8 %0, 16
  509. %tmp55 = xor i8 %tmp54, %tmp51
  510. ; CHECK: ret i8 %tmp55.1
  511. ret i8 %tmp55
  512. }
  513. ; PR9809
  514. define i32 @test40(i32 %a, i32 %b) nounwind {
  515. %shl1 = shl i32 1, %b
  516. %shl2 = shl i32 %shl1, 2
  517. %div = udiv i32 %a, %shl2
  518. ret i32 %div
  519. ; CHECK-LABEL: @test40(
  520. ; CHECK-NEXT: add i32 %b, 2
  521. ; CHECK-NEXT: lshr i32 %a
  522. ; CHECK-NEXT: ret i32
  523. }
  524. define i32 @test41(i32 %a, i32 %b) nounwind {
  525. %1 = shl i32 1, %b
  526. %2 = shl i32 %1, 3
  527. ret i32 %2
  528. ; CHECK-LABEL: @test41(
  529. ; CHECK-NEXT: shl i32 8, %b
  530. ; CHECK-NEXT: ret i32
  531. }
  532. define i32 @test42(i32 %a, i32 %b) nounwind {
  533. %div = lshr i32 4096, %b ; must be exact otherwise we'd divide by zero
  534. %div2 = udiv i32 %a, %div
  535. ret i32 %div2
  536. ; CHECK-LABEL: @test42(
  537. ; CHECK-NEXT: lshr exact i32 4096, %b
  538. }
  539. define i32 @test43(i32 %a, i32 %b) nounwind {
  540. %div = shl i32 4096, %b ; must be exact otherwise we'd divide by zero
  541. %div2 = udiv i32 %a, %div
  542. ret i32 %div2
  543. ; CHECK-LABEL: @test43(
  544. ; CHECK-NEXT: add i32 %b, 12
  545. ; CHECK-NEXT: lshr
  546. ; CHECK-NEXT: ret
  547. }
  548. define i32 @test44(i32 %a) nounwind {
  549. %y = shl nuw i32 %a, 1
  550. %z = shl i32 %y, 4
  551. ret i32 %z
  552. ; CHECK-LABEL: @test44(
  553. ; CHECK-NEXT: %y = shl i32 %a, 5
  554. ; CHECK-NEXT: ret i32 %y
  555. }
  556. define i32 @test45(i32 %a) nounwind {
  557. %y = lshr exact i32 %a, 1
  558. %z = lshr i32 %y, 4
  559. ret i32 %z
  560. ; CHECK-LABEL: @test45(
  561. ; CHECK-NEXT: %y = lshr i32 %a, 5
  562. ; CHECK-NEXT: ret i32 %y
  563. }
  564. define i32 @test46(i32 %a) {
  565. %y = ashr exact i32 %a, 3
  566. %z = shl i32 %y, 1
  567. ret i32 %z
  568. ; CHECK-LABEL: @test46(
  569. ; CHECK-NEXT: %z = ashr exact i32 %a, 2
  570. ; CHECK-NEXT: ret i32 %z
  571. }
  572. define i32 @test47(i32 %a) {
  573. %y = lshr exact i32 %a, 3
  574. %z = shl i32 %y, 1
  575. ret i32 %z
  576. ; CHECK-LABEL: @test47(
  577. ; CHECK-NEXT: %z = lshr exact i32 %a, 2
  578. ; CHECK-NEXT: ret i32 %z
  579. }
  580. define i32 @test48(i32 %x) {
  581. %A = lshr exact i32 %x, 1
  582. %B = shl i32 %A, 3
  583. ret i32 %B
  584. ; CHECK-LABEL: @test48(
  585. ; CHECK-NEXT: %B = shl i32 %x, 2
  586. ; CHECK-NEXT: ret i32 %B
  587. }
  588. define i32 @test49(i32 %x) {
  589. %A = ashr exact i32 %x, 1
  590. %B = shl i32 %A, 3
  591. ret i32 %B
  592. ; CHECK-LABEL: @test49(
  593. ; CHECK-NEXT: %B = shl i32 %x, 2
  594. ; CHECK-NEXT: ret i32 %B
  595. }
  596. define i32 @test50(i32 %x) {
  597. %A = shl nsw i32 %x, 1
  598. %B = ashr i32 %A, 3
  599. ret i32 %B
  600. ; CHECK-LABEL: @test50(
  601. ; CHECK-NEXT: %B = ashr i32 %x, 2
  602. ; CHECK-NEXT: ret i32 %B
  603. }
  604. define i32 @test51(i32 %x) {
  605. %A = shl nuw i32 %x, 1
  606. %B = lshr i32 %A, 3
  607. ret i32 %B
  608. ; CHECK-LABEL: @test51(
  609. ; CHECK-NEXT: %B = lshr i32 %x, 2
  610. ; CHECK-NEXT: ret i32 %B
  611. }
  612. define i32 @test52(i32 %x) {
  613. %A = shl nsw i32 %x, 3
  614. %B = ashr i32 %A, 1
  615. ret i32 %B
  616. ; CHECK-LABEL: @test52(
  617. ; CHECK-NEXT: %B = shl nsw i32 %x, 2
  618. ; CHECK-NEXT: ret i32 %B
  619. }
  620. define i32 @test53(i32 %x) {
  621. %A = shl nuw i32 %x, 3
  622. %B = lshr i32 %A, 1
  623. ret i32 %B
  624. ; CHECK-LABEL: @test53(
  625. ; CHECK-NEXT: %B = shl nuw i32 %x, 2
  626. ; CHECK-NEXT: ret i32 %B
  627. }
  628. define i32 @test54(i32 %x) {
  629. %shr2 = lshr i32 %x, 1
  630. %shl = shl i32 %shr2, 4
  631. %and = and i32 %shl, 16
  632. ret i32 %and
  633. ; CHECK-LABEL: @test54(
  634. ; CHECK: shl i32 %x, 3
  635. }
  636. define i32 @test55(i32 %x) {
  637. %shr2 = lshr i32 %x, 1
  638. %shl = shl i32 %shr2, 4
  639. %or = or i32 %shl, 8
  640. ret i32 %or
  641. ; CHECK-LABEL: @test55(
  642. ; CHECK: shl i32 %x, 3
  643. }
  644. define i32 @test56(i32 %x) {
  645. %shr2 = lshr i32 %x, 1
  646. %shl = shl i32 %shr2, 4
  647. %or = or i32 %shl, 7
  648. ret i32 %or
  649. ; CHECK-LABEL: @test56(
  650. ; CHECK: shl i32 %shr2, 4
  651. }
  652. define i32 @test57(i32 %x) {
  653. %shr = ashr i32 %x, 1
  654. %shl = shl i32 %shr, 4
  655. %or = or i32 %shl, 7
  656. ret i32 %or
  657. ; CHECK-LABEL: @test57(
  658. ; CHECK: %shl = shl i32 %shr.1, 4
  659. }
  660. define i32 @test58(i32 %x) {
  661. %shr = ashr i32 %x, 4
  662. %shl = shl i32 %shr, 1
  663. %or = or i32 %shl, 1
  664. ret i32 %or
  665. ; CHECK-LABEL: @test58(
  666. ; CHECK: ashr i32 %x, 3
  667. }
  668. define i32 @test59(i32 %x) {
  669. %shr = ashr i32 %x, 4
  670. %shl = shl i32 %shr, 1
  671. %or = or i32 %shl, 2
  672. ret i32 %or
  673. ; CHECK-LABEL: @test59(
  674. ; CHECK: ashr i32 %x, 4
  675. }
  676. ; propagate "exact" trait
  677. define i32 @test60(i32 %x) {
  678. %shr = ashr exact i32 %x, 4
  679. %shl = shl i32 %shr, 1
  680. %or = or i32 %shl, 1
  681. ret i32 %or
  682. ; CHECK-LABEL: @test60(
  683. ; CHECK: ashr exact i32 %x, 3
  684. }
  685. ; PR17026
  686. ; CHECK-LABEL: @test61(
  687. ; CHECK-NOT: sh
  688. ; CHECK: ret
  689. define void @test61(i128 %arg) {
  690. bb:
  691. br i1 undef, label %bb1, label %bb12
  692. bb1: ; preds = %bb11, %bb
  693. br label %bb2
  694. bb2: ; preds = %bb7, %bb1
  695. br i1 undef, label %bb3, label %bb7
  696. bb3: ; preds = %bb2
  697. %tmp = lshr i128 %arg, 36893488147419103232
  698. %tmp4 = shl i128 %tmp, 0
  699. %tmp5 = or i128 %tmp4, undef
  700. %tmp6 = trunc i128 %tmp5 to i16
  701. br label %bb8
  702. bb7: ; preds = %bb2
  703. br i1 undef, label %bb8, label %bb2
  704. bb8: ; preds = %bb7, %bb3
  705. %tmp9 = phi i16 [ %tmp6, %bb3 ], [ undef, %bb7 ]
  706. %tmp10 = icmp eq i16 %tmp9, 0
  707. br i1 %tmp10, label %bb11, label %bb12
  708. bb11: ; preds = %bb8
  709. br i1 undef, label %bb1, label %bb12
  710. bb12: ; preds = %bb11, %bb8, %bb
  711. ret void
  712. }
  713. define i32 @test62(i32 %a) {
  714. ; CHECK-LABEL: @test62(
  715. ; CHECK-NEXT: ret i32 undef
  716. %b = ashr i32 %a, 32 ; shift all bits out
  717. ret i32 %b
  718. }
  719. define <4 x i32> @test62_splat_vector(<4 x i32> %a) {
  720. ; CHECK-LABEL: @test62_splat_vector
  721. ; CHECK-NEXT: ret <4 x i32> undef
  722. %b = ashr <4 x i32> %a, <i32 32, i32 32, i32 32, i32 32> ; shift all bits out
  723. ret <4 x i32> %b
  724. }
  725. define <4 x i32> @test62_non_splat_vector(<4 x i32> %a) {
  726. ; CHECK-LABEL: @test62_non_splat_vector
  727. ; CHECK-NOT: ret <4 x i32> undef
  728. %b = ashr <4 x i32> %a, <i32 32, i32 0, i32 1, i32 2> ; shift all bits out
  729. ret <4 x i32> %b
  730. }
  731. define <2 x i65> @test_63(<2 x i64> %t) {
  732. ; CHECK-LABEL: @test_63
  733. %a = zext <2 x i64> %t to <2 x i65>
  734. %sext = shl <2 x i65> %a, <i65 33, i65 33>
  735. %b = ashr <2 x i65> %sext, <i65 33, i65 33>
  736. ret <2 x i65> %b
  737. }