fast-math.ll 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. ; RUN: opt < %s -instcombine -S | FileCheck %s
  2. ; testing-case "float fold(float a) { return 1.2f * a * 2.3f; }"
  3. ; 1.2f and 2.3f is supposed to be fold.
  4. define float @fold(float %a) {
  5. %mul = fmul fast float %a, 0x3FF3333340000000
  6. %mul1 = fmul fast float %mul, 0x4002666660000000
  7. ret float %mul1
  8. ; CHECK-LABEL: @fold(
  9. ; CHECK: fmul fast float %a, 0x4006147AE0000000
  10. }
  11. ; Same testing-case as the one used in fold() except that the operators have
  12. ; fixed FP mode.
  13. define float @notfold(float %a) {
  14. ; CHECK-LABEL: @notfold(
  15. ; CHECK: %mul = fmul fast float %a, 0x3FF3333340000000
  16. %mul = fmul fast float %a, 0x3FF3333340000000
  17. %mul1 = fmul float %mul, 0x4002666660000000
  18. ret float %mul1
  19. }
  20. define float @fold2(float %a) {
  21. ; CHECK-LABEL: @fold2(
  22. ; CHECK: fmul fast float %a, 0x4006147AE0000000
  23. %mul = fmul float %a, 0x3FF3333340000000
  24. %mul1 = fmul fast float %mul, 0x4002666660000000
  25. ret float %mul1
  26. }
  27. ; C * f1 + f1 = (C+1) * f1
  28. define double @fold3(double %f1) {
  29. %t1 = fmul fast double 2.000000e+00, %f1
  30. %t2 = fadd fast double %f1, %t1
  31. ret double %t2
  32. ; CHECK-LABEL: @fold3(
  33. ; CHECK: fmul fast double %f1, 3.000000e+00
  34. }
  35. ; (C1 - X) + (C2 - Y) => (C1+C2) - (X + Y)
  36. define float @fold4(float %f1, float %f2) {
  37. %sub = fsub float 4.000000e+00, %f1
  38. %sub1 = fsub float 5.000000e+00, %f2
  39. %add = fadd fast float %sub, %sub1
  40. ret float %add
  41. ; CHECK-LABEL: @fold4(
  42. ; CHECK: %1 = fadd fast float %f1, %f2
  43. ; CHECK: fsub fast float 9.000000e+00, %1
  44. }
  45. ; (X + C1) + C2 => X + (C1 + C2)
  46. define float @fold5(float %f1, float %f2) {
  47. %add = fadd float %f1, 4.000000e+00
  48. %add1 = fadd fast float %add, 5.000000e+00
  49. ret float %add1
  50. ; CHECK-LABEL: @fold5(
  51. ; CHECK: fadd fast float %f1, 9.000000e+00
  52. }
  53. ; (X + X) + X => 3.0 * X
  54. define float @fold6(float %f1) {
  55. %t1 = fadd fast float %f1, %f1
  56. %t2 = fadd fast float %f1, %t1
  57. ret float %t2
  58. ; CHECK-LABEL: @fold6(
  59. ; CHECK: fmul fast float %f1, 3.000000e+00
  60. }
  61. ; C1 * X + (X + X) = (C1 + 2) * X
  62. define float @fold7(float %f1) {
  63. %t1 = fmul fast float %f1, 5.000000e+00
  64. %t2 = fadd fast float %f1, %f1
  65. %t3 = fadd fast float %t1, %t2
  66. ret float %t3
  67. ; CHECK-LABEL: @fold7(
  68. ; CHECK: fmul fast float %f1, 7.000000e+00
  69. }
  70. ; (X + X) + (X + X) => 4.0 * X
  71. define float @fold8(float %f1) {
  72. %t1 = fadd fast float %f1, %f1
  73. %t2 = fadd fast float %f1, %f1
  74. %t3 = fadd fast float %t1, %t2
  75. ret float %t3
  76. ; CHECK: fold8
  77. ; CHECK: fmul fast float %f1, 4.000000e+00
  78. }
  79. ; X - (X + Y) => 0 - Y
  80. define float @fold9(float %f1, float %f2) {
  81. %t1 = fadd float %f1, %f2
  82. %t3 = fsub fast float %f1, %t1
  83. ret float %t3
  84. ; CHECK-LABEL: @fold9(
  85. ; CHECK: fsub fast float -0.000000e+00, %f2
  86. }
  87. ; Let C3 = C1 + C2. (f1 + C1) + (f2 + C2) => (f1 + f2) + C3 instead of
  88. ; "(f1 + C3) + f2" or "(f2 + C3) + f1". Placing constant-addend at the
  89. ; top of resulting simplified expression tree may potentially reveal some
  90. ; optimization opportunities in the super-expression trees.
  91. ;
  92. define float @fold10(float %f1, float %f2) {
  93. %t1 = fadd fast float 2.000000e+00, %f1
  94. %t2 = fsub fast float %f2, 3.000000e+00
  95. %t3 = fadd fast float %t1, %t2
  96. ret float %t3
  97. ; CHECK-LABEL: @fold10(
  98. ; CHECK: %t3 = fadd fast float %t2, -1.000000e+00
  99. ; CHECK: ret float %t3
  100. }
  101. ; once cause Crash/miscompilation
  102. define float @fail1(float %f1, float %f2) {
  103. %conv3 = fadd fast float %f1, -1.000000e+00
  104. %add = fadd fast float %conv3, %conv3
  105. %add2 = fadd fast float %add, %conv3
  106. ret float %add2
  107. ; CHECK-LABEL: @fail1(
  108. ; CHECK: ret
  109. }
  110. define double @fail2(double %f1, double %f2) {
  111. %t1 = fsub fast double %f1, %f2
  112. %t2 = fadd fast double %f1, %f2
  113. %t3 = fsub fast double %t1, %t2
  114. ret double %t3
  115. ; CHECK-LABEL: @fail2(
  116. ; CHECK: ret
  117. }
  118. ; c1 * x - x => (c1 - 1.0) * x
  119. define float @fold13(float %x) {
  120. %mul = fmul fast float %x, 7.000000e+00
  121. %sub = fsub fast float %mul, %x
  122. ret float %sub
  123. ; CHECK: fold13
  124. ; CHECK: fmul fast float %x, 6.000000e+00
  125. ; CHECK: ret
  126. }
  127. ; -x + y => y - x
  128. define float @fold14(float %x, float %y) {
  129. %neg = fsub fast float -0.0, %x
  130. %add = fadd fast float %neg, %y
  131. ret float %add
  132. ; CHECK: fold14
  133. ; CHECK: fsub fast float %y, %x
  134. ; CHECK: ret
  135. }
  136. ; x + -y => x - y
  137. define float @fold15(float %x, float %y) {
  138. %neg = fsub fast float -0.0, %y
  139. %add = fadd fast float %x, %neg
  140. ret float %add
  141. ; CHECK: fold15
  142. ; CHECK: fsub fast float %x, %y
  143. ; CHECK: ret
  144. }
  145. ; (select X+Y, X-Y) => X + (select Y, -Y)
  146. define float @fold16(float %x, float %y) {
  147. %cmp = fcmp ogt float %x, %y
  148. %plus = fadd fast float %x, %y
  149. %minus = fsub fast float %x, %y
  150. %r = select i1 %cmp, float %plus, float %minus
  151. ret float %r
  152. ; CHECK: fold16
  153. ; CHECK: fsub fast float
  154. ; CHECK: select
  155. ; CHECK: fadd fast float
  156. ; CHECK: ret
  157. }
  158. ; =========================================================================
  159. ;
  160. ; Testing-cases about fmul begin
  161. ;
  162. ; =========================================================================
  163. ; ((X*C1) + C2) * C3 => (X * (C1*C3)) + (C2*C3) (i.e. distribution)
  164. define float @fmul_distribute1(float %f1) {
  165. %t1 = fmul float %f1, 6.0e+3
  166. %t2 = fadd float %t1, 2.0e+3
  167. %t3 = fmul fast float %t2, 5.0e+3
  168. ret float %t3
  169. ; CHECK-LABEL: @fmul_distribute1(
  170. ; CHECK: %1 = fmul fast float %f1, 3.000000e+07
  171. ; CHECK: %t3 = fadd fast float %1, 1.000000e+07
  172. }
  173. ; (X/C1 + C2) * C3 => X/(C1/C3) + C2*C3
  174. define double @fmul_distribute2(double %f1, double %f2) {
  175. %t1 = fdiv double %f1, 3.0e+0
  176. %t2 = fadd double %t1, 5.0e+1
  177. ; 0x10000000000000 = DBL_MIN
  178. %t3 = fmul fast double %t2, 0x10000000000000
  179. ret double %t3
  180. ; CHECK-LABEL: @fmul_distribute2(
  181. ; CHECK: %1 = fdiv fast double %f1, 0x7FE8000000000000
  182. ; CHECK: fadd fast double %1, 0x69000000000000
  183. }
  184. ; 5.0e-1 * DBL_MIN yields denormal, so "(f1*3.0 + 5.0e-1) * DBL_MIN" cannot
  185. ; be simplified into f1 * (3.0*DBL_MIN) + (5.0e-1*DBL_MIN)
  186. define double @fmul_distribute3(double %f1) {
  187. %t1 = fdiv double %f1, 3.0e+0
  188. %t2 = fadd double %t1, 5.0e-1
  189. %t3 = fmul fast double %t2, 0x10000000000000
  190. ret double %t3
  191. ; CHECK-LABEL: @fmul_distribute3(
  192. ; CHECK: fmul fast double %t2, 0x10000000000000
  193. }
  194. ; ((X*C1) + C2) * C3 => (X * (C1*C3)) + (C2*C3) (i.e. distribution)
  195. define float @fmul_distribute4(float %f1) {
  196. %t1 = fmul float %f1, 6.0e+3
  197. %t2 = fsub float 2.0e+3, %t1
  198. %t3 = fmul fast float %t2, 5.0e+3
  199. ret float %t3
  200. ; CHECK-LABEL: @fmul_distribute4(
  201. ; CHECK: %1 = fmul fast float %f1, 3.000000e+07
  202. ; CHECK: %t3 = fsub fast float 1.000000e+07, %1
  203. }
  204. ; C1/X * C2 => (C1*C2) / X
  205. define float @fmul2(float %f1) {
  206. %t1 = fdiv float 2.0e+3, %f1
  207. %t3 = fmul fast float %t1, 6.0e+3
  208. ret float %t3
  209. ; CHECK-LABEL: @fmul2(
  210. ; CHECK: fdiv fast float 1.200000e+07, %f1
  211. }
  212. ; X/C1 * C2 => X * (C2/C1) is disabled if X/C1 has multiple uses
  213. @fmul2_external = external global float
  214. define float @fmul2_disable(float %f1) {
  215. %div = fdiv fast float 1.000000e+00, %f1
  216. store float %div, float* @fmul2_external
  217. %mul = fmul fast float %div, 2.000000e+00
  218. ret float %mul
  219. ; CHECK-LABEL: @fmul2_disable
  220. ; CHECK: store
  221. ; CHECK: fmul fast
  222. }
  223. ; X/C1 * C2 => X * (C2/C1) (if C2/C1 is normal Fp)
  224. define float @fmul3(float %f1, float %f2) {
  225. %t1 = fdiv float %f1, 2.0e+3
  226. %t3 = fmul fast float %t1, 6.0e+3
  227. ret float %t3
  228. ; CHECK-LABEL: @fmul3(
  229. ; CHECK: fmul fast float %f1, 3.000000e+00
  230. }
  231. define <4 x float> @fmul3_vec(<4 x float> %f1, <4 x float> %f2) {
  232. %t1 = fdiv <4 x float> %f1, <float 2.0e+3, float 3.0e+3, float 2.0e+3, float 1.0e+3>
  233. %t3 = fmul fast <4 x float> %t1, <float 6.0e+3, float 6.0e+3, float 2.0e+3, float 1.0e+3>
  234. ret <4 x float> %t3
  235. ; CHECK-LABEL: @fmul3_vec(
  236. ; CHECK: fmul fast <4 x float> %f1, <float 3.000000e+00, float 2.000000e+00, float 1.000000e+00, float 1.000000e+00>
  237. }
  238. ; Make sure fmul with constant expression doesn't assert.
  239. define <4 x float> @fmul3_vec_constexpr(<4 x float> %f1, <4 x float> %f2) {
  240. %constExprMul = bitcast i128 trunc (i160 bitcast (<5 x float> <float 6.0e+3, float 6.0e+3, float 2.0e+3, float 1.0e+3, float undef> to i160) to i128) to <4 x float>
  241. %t1 = fdiv <4 x float> %f1, <float 2.0e+3, float 3.0e+3, float 2.0e+3, float 1.0e+3>
  242. %t3 = fmul fast <4 x float> %t1, %constExprMul
  243. ret <4 x float> %t3
  244. }
  245. ; Rule "X/C1 * C2 => X * (C2/C1) is not applicable if C2/C1 is either a special
  246. ; value of a denormal. The 0x3810000000000000 here take value FLT_MIN
  247. ;
  248. define float @fmul4(float %f1, float %f2) {
  249. %t1 = fdiv float %f1, 2.0e+3
  250. %t3 = fmul fast float %t1, 0x3810000000000000
  251. ret float %t3
  252. ; CHECK-LABEL: @fmul4(
  253. ; CHECK: fmul fast float %t1, 0x3810000000000000
  254. }
  255. ; X / C1 * C2 => X / (C2/C1) if C1/C2 is either a special value of a denormal,
  256. ; and C2/C1 is a normal value.
  257. ;
  258. define float @fmul5(float %f1, float %f2) {
  259. %t1 = fdiv float %f1, 3.0e+0
  260. %t3 = fmul fast float %t1, 0x3810000000000000
  261. ret float %t3
  262. ; CHECK-LABEL: @fmul5(
  263. ; CHECK: fdiv fast float %f1, 0x47E8000000000000
  264. }
  265. ; (X*Y) * X => (X*X) * Y
  266. define float @fmul6(float %f1, float %f2) {
  267. %mul = fmul float %f1, %f2
  268. %mul1 = fmul fast float %mul, %f1
  269. ret float %mul1
  270. ; CHECK-LABEL: @fmul6(
  271. ; CHECK: fmul fast float %f1, %f1
  272. }
  273. ; "(X*Y) * X => (X*X) * Y" is disabled if "X*Y" has multiple uses
  274. define float @fmul7(float %f1, float %f2) {
  275. %mul = fmul float %f1, %f2
  276. %mul1 = fmul fast float %mul, %f1
  277. %add = fadd float %mul1, %mul
  278. ret float %add
  279. ; CHECK-LABEL: @fmul7(
  280. ; CHECK: fmul fast float %mul, %f1
  281. }
  282. ; =========================================================================
  283. ;
  284. ; Testing-cases about negation
  285. ;
  286. ; =========================================================================
  287. define float @fneg1(float %f1, float %f2) {
  288. %sub = fsub float -0.000000e+00, %f1
  289. %sub1 = fsub nsz float 0.000000e+00, %f2
  290. %mul = fmul float %sub, %sub1
  291. ret float %mul
  292. ; CHECK-LABEL: @fneg1(
  293. ; CHECK: fmul float %f1, %f2
  294. }
  295. define float @fneg2(float %x) {
  296. %sub = fsub nsz float 0.0, %x
  297. ret float %sub
  298. ; CHECK-LABEL: @fneg2(
  299. ; CHECK-NEXT: fsub nsz float -0.000000e+00, %x
  300. ; CHECK-NEXT: ret float
  301. }
  302. ; =========================================================================
  303. ;
  304. ; Testing-cases about div
  305. ;
  306. ; =========================================================================
  307. ; X/C1 / C2 => X * (1/(C2*C1))
  308. define float @fdiv1(float %x) {
  309. %div = fdiv float %x, 0x3FF3333340000000
  310. %div1 = fdiv fast float %div, 0x4002666660000000
  311. ret float %div1
  312. ; 0x3FF3333340000000 = 1.2f
  313. ; 0x4002666660000000 = 2.3f
  314. ; 0x3FD7303B60000000 = 0.36231884057971014492
  315. ; CHECK-LABEL: @fdiv1(
  316. ; CHECK: fmul fast float %x, 0x3FD7303B60000000
  317. }
  318. ; X*C1 / C2 => X * (C1/C2)
  319. define float @fdiv2(float %x) {
  320. %mul = fmul float %x, 0x3FF3333340000000
  321. %div1 = fdiv fast float %mul, 0x4002666660000000
  322. ret float %div1
  323. ; 0x3FF3333340000000 = 1.2f
  324. ; 0x4002666660000000 = 2.3f
  325. ; 0x3FE0B21660000000 = 0.52173918485641479492
  326. ; CHECK-LABEL: @fdiv2(
  327. ; CHECK: fmul fast float %x, 0x3FE0B21660000000
  328. }
  329. define <2 x float> @fdiv2_vec(<2 x float> %x) {
  330. %mul = fmul <2 x float> %x, <float 6.0, float 9.0>
  331. %div1 = fdiv fast <2 x float> %mul, <float 2.0, float 3.0>
  332. ret <2 x float> %div1
  333. ; CHECK-LABEL: @fdiv2_vec(
  334. ; CHECK: fmul fast <2 x float> %x, <float 3.000000e+00, float 3.000000e+00>
  335. }
  336. ; "X/C1 / C2 => X * (1/(C2*C1))" is disabled (for now) is C2/C1 is a denormal
  337. ;
  338. define float @fdiv3(float %x) {
  339. %div = fdiv float %x, 0x47EFFFFFE0000000
  340. %div1 = fdiv fast float %div, 0x4002666660000000
  341. ret float %div1
  342. ; CHECK-LABEL: @fdiv3(
  343. ; CHECK: fdiv float %x, 0x47EFFFFFE0000000
  344. }
  345. ; "X*C1 / C2 => X * (C1/C2)" is disabled if C1/C2 is a denormal
  346. define float @fdiv4(float %x) {
  347. %mul = fmul float %x, 0x47EFFFFFE0000000
  348. %div = fdiv float %mul, 0x3FC99999A0000000
  349. ret float %div
  350. ; CHECK-LABEL: @fdiv4(
  351. ; CHECK: fmul float %x, 0x47EFFFFFE0000000
  352. }
  353. ; (X/Y)/Z = > X/(Y*Z)
  354. define float @fdiv5(float %f1, float %f2, float %f3) {
  355. %t1 = fdiv float %f1, %f2
  356. %t2 = fdiv fast float %t1, %f3
  357. ret float %t2
  358. ; CHECK-LABEL: @fdiv5(
  359. ; CHECK: fmul float %f2, %f3
  360. }
  361. ; Z/(X/Y) = > (Z*Y)/X
  362. define float @fdiv6(float %f1, float %f2, float %f3) {
  363. %t1 = fdiv float %f1, %f2
  364. %t2 = fdiv fast float %f3, %t1
  365. ret float %t2
  366. ; CHECK-LABEL: @fdiv6(
  367. ; CHECK: fmul float %f3, %f2
  368. }
  369. ; C1/(X*C2) => (C1/C2) / X
  370. define float @fdiv7(float %x) {
  371. %t1 = fmul float %x, 3.0e0
  372. %t2 = fdiv fast float 15.0e0, %t1
  373. ret float %t2
  374. ; CHECK-LABEL: @fdiv7(
  375. ; CHECK: fdiv fast float 5.000000e+00, %x
  376. }
  377. ; C1/(X/C2) => (C1*C2) / X
  378. define float @fdiv8(float %x) {
  379. %t1 = fdiv float %x, 3.0e0
  380. %t2 = fdiv fast float 15.0e0, %t1
  381. ret float %t2
  382. ; CHECK-LABEL: @fdiv8(
  383. ; CHECK: fdiv fast float 4.500000e+01, %x
  384. }
  385. ; C1/(C2/X) => (C1/C2) * X
  386. define float @fdiv9(float %x) {
  387. %t1 = fdiv float 3.0e0, %x
  388. %t2 = fdiv fast float 15.0e0, %t1
  389. ret float %t2
  390. ; CHECK-LABEL: @fdiv9(
  391. ; CHECK: fmul fast float %x, 5.000000e+00
  392. }
  393. ; =========================================================================
  394. ;
  395. ; Testing-cases about factorization
  396. ;
  397. ; =========================================================================
  398. ; x*z + y*z => (x+y) * z
  399. define float @fact_mul1(float %x, float %y, float %z) {
  400. %t1 = fmul fast float %x, %z
  401. %t2 = fmul fast float %y, %z
  402. %t3 = fadd fast float %t1, %t2
  403. ret float %t3
  404. ; CHECK-LABEL: @fact_mul1(
  405. ; CHECK: fmul fast float %1, %z
  406. }
  407. ; z*x + y*z => (x+y) * z
  408. define float @fact_mul2(float %x, float %y, float %z) {
  409. %t1 = fmul fast float %z, %x
  410. %t2 = fmul fast float %y, %z
  411. %t3 = fsub fast float %t1, %t2
  412. ret float %t3
  413. ; CHECK-LABEL: @fact_mul2(
  414. ; CHECK: fmul fast float %1, %z
  415. }
  416. ; z*x - z*y => (x-y) * z
  417. define float @fact_mul3(float %x, float %y, float %z) {
  418. %t2 = fmul fast float %z, %y
  419. %t1 = fmul fast float %z, %x
  420. %t3 = fsub fast float %t1, %t2
  421. ret float %t3
  422. ; CHECK-LABEL: @fact_mul3(
  423. ; CHECK: fmul fast float %1, %z
  424. }
  425. ; x*z - z*y => (x-y) * z
  426. define float @fact_mul4(float %x, float %y, float %z) {
  427. %t1 = fmul fast float %x, %z
  428. %t2 = fmul fast float %z, %y
  429. %t3 = fsub fast float %t1, %t2
  430. ret float %t3
  431. ; CHECK-LABEL: @fact_mul4(
  432. ; CHECK: fmul fast float %1, %z
  433. }
  434. ; x/y + x/z, no xform
  435. define float @fact_div1(float %x, float %y, float %z) {
  436. %t1 = fdiv fast float %x, %y
  437. %t2 = fdiv fast float %x, %z
  438. %t3 = fadd fast float %t1, %t2
  439. ret float %t3
  440. ; CHECK: fact_div1
  441. ; CHECK: fadd fast float %t1, %t2
  442. }
  443. ; x/y + z/x; no xform
  444. define float @fact_div2(float %x, float %y, float %z) {
  445. %t1 = fdiv fast float %x, %y
  446. %t2 = fdiv fast float %z, %x
  447. %t3 = fadd fast float %t1, %t2
  448. ret float %t3
  449. ; CHECK: fact_div2
  450. ; CHECK: fadd fast float %t1, %t2
  451. }
  452. ; y/x + z/x => (y+z)/x
  453. define float @fact_div3(float %x, float %y, float %z) {
  454. %t1 = fdiv fast float %y, %x
  455. %t2 = fdiv fast float %z, %x
  456. %t3 = fadd fast float %t1, %t2
  457. ret float %t3
  458. ; CHECK: fact_div3
  459. ; CHECK: fdiv fast float %1, %x
  460. }
  461. ; y/x - z/x => (y-z)/x
  462. define float @fact_div4(float %x, float %y, float %z) {
  463. %t1 = fdiv fast float %y, %x
  464. %t2 = fdiv fast float %z, %x
  465. %t3 = fsub fast float %t1, %t2
  466. ret float %t3
  467. ; CHECK: fact_div4
  468. ; CHECK: fdiv fast float %1, %x
  469. }
  470. ; y/x - z/x => (y-z)/x is disabled if y-z is denormal.
  471. define float @fact_div5(float %x) {
  472. %t1 = fdiv fast float 0x3810000000000000, %x
  473. %t2 = fdiv fast float 0x3800000000000000, %x
  474. %t3 = fadd fast float %t1, %t2
  475. ret float %t3
  476. ; CHECK: fact_div5
  477. ; CHECK: fdiv fast float 0x3818000000000000, %x
  478. }
  479. ; y/x - z/x => (y-z)/x is disabled if y-z is denormal.
  480. define float @fact_div6(float %x) {
  481. %t1 = fdiv fast float 0x3810000000000000, %x
  482. %t2 = fdiv fast float 0x3800000000000000, %x
  483. %t3 = fsub fast float %t1, %t2
  484. ret float %t3
  485. ; CHECK: fact_div6
  486. ; CHECK: %t3 = fsub fast float %t1, %t2
  487. }
  488. ; =========================================================================
  489. ;
  490. ; Test-cases for square root
  491. ;
  492. ; =========================================================================
  493. ; A squared factor fed into a square root intrinsic should be hoisted out
  494. ; as a fabs() value.
  495. ; We have to rely on a function-level attribute to enable this optimization
  496. ; because intrinsics don't currently have access to IR-level fast-math
  497. ; flags. If that changes, we can relax the requirement on all of these
  498. ; tests to just specify 'fast' on the sqrt.
  499. attributes #0 = { "unsafe-fp-math" = "true" }
  500. declare double @llvm.sqrt.f64(double)
  501. define double @sqrt_intrinsic_arg_squared(double %x) #0 {
  502. %mul = fmul fast double %x, %x
  503. %sqrt = call double @llvm.sqrt.f64(double %mul)
  504. ret double %sqrt
  505. ; CHECK-LABEL: sqrt_intrinsic_arg_squared(
  506. ; CHECK-NEXT: %fabs = call double @llvm.fabs.f64(double %x)
  507. ; CHECK-NEXT: ret double %fabs
  508. }
  509. ; Check all 6 combinations of a 3-way multiplication tree where
  510. ; one factor is repeated.
  511. define double @sqrt_intrinsic_three_args1(double %x, double %y) #0 {
  512. %mul = fmul fast double %y, %x
  513. %mul2 = fmul fast double %mul, %x
  514. %sqrt = call double @llvm.sqrt.f64(double %mul2)
  515. ret double %sqrt
  516. ; CHECK-LABEL: sqrt_intrinsic_three_args1(
  517. ; CHECK-NEXT: %fabs = call double @llvm.fabs.f64(double %x)
  518. ; CHECK-NEXT: %sqrt1 = call double @llvm.sqrt.f64(double %y)
  519. ; CHECK-NEXT: %1 = fmul fast double %fabs, %sqrt1
  520. ; CHECK-NEXT: ret double %1
  521. }
  522. define double @sqrt_intrinsic_three_args2(double %x, double %y) #0 {
  523. %mul = fmul fast double %x, %y
  524. %mul2 = fmul fast double %mul, %x
  525. %sqrt = call double @llvm.sqrt.f64(double %mul2)
  526. ret double %sqrt
  527. ; CHECK-LABEL: sqrt_intrinsic_three_args2(
  528. ; CHECK-NEXT: %fabs = call double @llvm.fabs.f64(double %x)
  529. ; CHECK-NEXT: %sqrt1 = call double @llvm.sqrt.f64(double %y)
  530. ; CHECK-NEXT: %1 = fmul fast double %fabs, %sqrt1
  531. ; CHECK-NEXT: ret double %1
  532. }
  533. define double @sqrt_intrinsic_three_args3(double %x, double %y) #0 {
  534. %mul = fmul fast double %x, %x
  535. %mul2 = fmul fast double %mul, %y
  536. %sqrt = call double @llvm.sqrt.f64(double %mul2)
  537. ret double %sqrt
  538. ; CHECK-LABEL: sqrt_intrinsic_three_args3(
  539. ; CHECK-NEXT: %fabs = call double @llvm.fabs.f64(double %x)
  540. ; CHECK-NEXT: %sqrt1 = call double @llvm.sqrt.f64(double %y)
  541. ; CHECK-NEXT: %1 = fmul fast double %fabs, %sqrt1
  542. ; CHECK-NEXT: ret double %1
  543. }
  544. define double @sqrt_intrinsic_three_args4(double %x, double %y) #0 {
  545. %mul = fmul fast double %y, %x
  546. %mul2 = fmul fast double %x, %mul
  547. %sqrt = call double @llvm.sqrt.f64(double %mul2)
  548. ret double %sqrt
  549. ; CHECK-LABEL: sqrt_intrinsic_three_args4(
  550. ; CHECK-NEXT: %fabs = call double @llvm.fabs.f64(double %x)
  551. ; CHECK-NEXT: %sqrt1 = call double @llvm.sqrt.f64(double %y)
  552. ; CHECK-NEXT: %1 = fmul fast double %fabs, %sqrt1
  553. ; CHECK-NEXT: ret double %1
  554. }
  555. define double @sqrt_intrinsic_three_args5(double %x, double %y) #0 {
  556. %mul = fmul fast double %x, %y
  557. %mul2 = fmul fast double %x, %mul
  558. %sqrt = call double @llvm.sqrt.f64(double %mul2)
  559. ret double %sqrt
  560. ; CHECK-LABEL: sqrt_intrinsic_three_args5(
  561. ; CHECK-NEXT: %fabs = call double @llvm.fabs.f64(double %x)
  562. ; CHECK-NEXT: %sqrt1 = call double @llvm.sqrt.f64(double %y)
  563. ; CHECK-NEXT: %1 = fmul fast double %fabs, %sqrt1
  564. ; CHECK-NEXT: ret double %1
  565. }
  566. define double @sqrt_intrinsic_three_args6(double %x, double %y) #0 {
  567. %mul = fmul fast double %x, %x
  568. %mul2 = fmul fast double %y, %mul
  569. %sqrt = call double @llvm.sqrt.f64(double %mul2)
  570. ret double %sqrt
  571. ; CHECK-LABEL: sqrt_intrinsic_three_args6(
  572. ; CHECK-NEXT: %fabs = call double @llvm.fabs.f64(double %x)
  573. ; CHECK-NEXT: %sqrt1 = call double @llvm.sqrt.f64(double %y)
  574. ; CHECK-NEXT: %1 = fmul fast double %fabs, %sqrt1
  575. ; CHECK-NEXT: ret double %1
  576. }
  577. define double @sqrt_intrinsic_arg_4th(double %x) #0 {
  578. %mul = fmul fast double %x, %x
  579. %mul2 = fmul fast double %mul, %mul
  580. %sqrt = call double @llvm.sqrt.f64(double %mul2)
  581. ret double %sqrt
  582. ; CHECK-LABEL: sqrt_intrinsic_arg_4th(
  583. ; CHECK-NEXT: %mul = fmul fast double %x, %x
  584. ; CHECK-NEXT: ret double %mul
  585. }
  586. define double @sqrt_intrinsic_arg_5th(double %x) #0 {
  587. %mul = fmul fast double %x, %x
  588. %mul2 = fmul fast double %mul, %x
  589. %mul3 = fmul fast double %mul2, %mul
  590. %sqrt = call double @llvm.sqrt.f64(double %mul3)
  591. ret double %sqrt
  592. ; CHECK-LABEL: sqrt_intrinsic_arg_5th(
  593. ; CHECK-NEXT: %mul = fmul fast double %x, %x
  594. ; CHECK-NEXT: %sqrt1 = call double @llvm.sqrt.f64(double %x)
  595. ; CHECK-NEXT: %1 = fmul fast double %mul, %sqrt1
  596. ; CHECK-NEXT: ret double %1
  597. }
  598. ; Check that square root calls have the same behavior.
  599. declare float @sqrtf(float)
  600. declare double @sqrt(double)
  601. declare fp128 @sqrtl(fp128)
  602. define float @sqrt_call_squared_f32(float %x) #0 {
  603. %mul = fmul fast float %x, %x
  604. %sqrt = call float @sqrtf(float %mul)
  605. ret float %sqrt
  606. ; CHECK-LABEL: sqrt_call_squared_f32(
  607. ; CHECK-NEXT: %fabs = call float @llvm.fabs.f32(float %x)
  608. ; CHECK-NEXT: ret float %fabs
  609. }
  610. define double @sqrt_call_squared_f64(double %x) #0 {
  611. %mul = fmul fast double %x, %x
  612. %sqrt = call double @sqrt(double %mul)
  613. ret double %sqrt
  614. ; CHECK-LABEL: sqrt_call_squared_f64(
  615. ; CHECK-NEXT: %fabs = call double @llvm.fabs.f64(double %x)
  616. ; CHECK-NEXT: ret double %fabs
  617. }
  618. define fp128 @sqrt_call_squared_f128(fp128 %x) #0 {
  619. %mul = fmul fast fp128 %x, %x
  620. %sqrt = call fp128 @sqrtl(fp128 %mul)
  621. ret fp128 %sqrt
  622. ; CHECK-LABEL: sqrt_call_squared_f128(
  623. ; CHECK-NEXT: %fabs = call fp128 @llvm.fabs.f128(fp128 %x)
  624. ; CHECK-NEXT: ret fp128 %fabs
  625. }