pow-1.ll 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. ; Test that the pow library call simplifier works correctly.
  2. ;
  3. ; RUN: opt < %s -instcombine -S | FileCheck %s
  4. ; RUN: opt -instcombine -S < %s -mtriple=x86_64-apple-macosx10.9 | FileCheck %s --check-prefix=CHECK-EXP10
  5. ; RUN: opt -instcombine -S < %s -mtriple=arm-apple-ios7.0 | FileCheck %s --check-prefix=CHECK-EXP10
  6. ; RUN: opt -instcombine -S < %s -mtriple=x86_64-apple-macosx10.8 | FileCheck %s --check-prefix=CHECK-NO-EXP10
  7. ; RUN: opt -instcombine -S < %s -mtriple=arm-apple-ios6.0 | FileCheck %s --check-prefix=CHECK-NO-EXP10
  8. ; RUN: opt -instcombine -S < %s -mtriple=x86_64-netbsd | FileCheck %s --check-prefix=CHECK-NO-EXP10
  9. ; rdar://7251832
  10. ; NOTE: The readonly attribute on the pow call should be preserved
  11. ; in the cases below where pow is transformed into another function call.
  12. declare float @powf(float, float) nounwind readonly
  13. declare double @pow(double, double) nounwind readonly
  14. ; Check pow(1.0, x) -> 1.0.
  15. define float @test_simplify1(float %x) {
  16. ; CHECK-LABEL: @test_simplify1(
  17. %retval = call float @powf(float 1.0, float %x)
  18. ret float %retval
  19. ; CHECK-NEXT: ret float 1.000000e+00
  20. }
  21. define double @test_simplify2(double %x) {
  22. ; CHECK-LABEL: @test_simplify2(
  23. %retval = call double @pow(double 1.0, double %x)
  24. ret double %retval
  25. ; CHECK-NEXT: ret double 1.000000e+00
  26. }
  27. ; Check pow(2.0, x) -> exp2(x).
  28. define float @test_simplify3(float %x) {
  29. ; CHECK-LABEL: @test_simplify3(
  30. %retval = call float @powf(float 2.0, float %x)
  31. ; CHECK-NEXT: [[EXP2F:%[a-z0-9]+]] = call float @exp2f(float %x) [[NUW_RO:#[0-9]+]]
  32. ret float %retval
  33. ; CHECK-NEXT: ret float [[EXP2F]]
  34. }
  35. define double @test_simplify4(double %x) {
  36. ; CHECK-LABEL: @test_simplify4(
  37. %retval = call double @pow(double 2.0, double %x)
  38. ; CHECK-NEXT: [[EXP2:%[a-z0-9]+]] = call double @exp2(double %x) [[NUW_RO]]
  39. ret double %retval
  40. ; CHECK-NEXT: ret double [[EXP2]]
  41. }
  42. ; Check pow(x, 0.0) -> 1.0.
  43. define float @test_simplify5(float %x) {
  44. ; CHECK-LABEL: @test_simplify5(
  45. %retval = call float @powf(float %x, float 0.0)
  46. ret float %retval
  47. ; CHECK-NEXT: ret float 1.000000e+00
  48. }
  49. define double @test_simplify6(double %x) {
  50. ; CHECK-LABEL: @test_simplify6(
  51. %retval = call double @pow(double %x, double 0.0)
  52. ret double %retval
  53. ; CHECK-NEXT: ret double 1.000000e+00
  54. }
  55. ; Check pow(x, 0.5) -> fabs(sqrt(x)), where x != -infinity.
  56. define float @test_simplify7(float %x) {
  57. ; CHECK-LABEL: @test_simplify7(
  58. %retval = call float @powf(float %x, float 0.5)
  59. ; CHECK-NEXT: [[SQRTF:%[a-z0-9]+]] = call float @sqrtf(float %x) [[NUW_RO]]
  60. ; CHECK-NEXT: [[FABSF:%[a-z0-9]+]] = call float @fabsf(float [[SQRTF]]) [[NUW_RO]]
  61. ; CHECK-NEXT: [[FCMP:%[a-z0-9]+]] = fcmp oeq float %x, 0xFFF0000000000000
  62. ; CHECK-NEXT: [[SELECT:%[a-z0-9]+]] = select i1 [[FCMP]], float 0x7FF0000000000000, float [[FABSF]]
  63. ret float %retval
  64. ; CHECK-NEXT: ret float [[SELECT]]
  65. }
  66. define double @test_simplify8(double %x) {
  67. ; CHECK-LABEL: @test_simplify8(
  68. %retval = call double @pow(double %x, double 0.5)
  69. ; CHECK-NEXT: [[SQRT:%[a-z0-9]+]] = call double @sqrt(double %x) [[NUW_RO]]
  70. ; CHECK-NEXT: [[FABS:%[a-z0-9]+]] = call double @fabs(double [[SQRT]]) [[NUW_RO]]
  71. ; CHECK-NEXT: [[FCMP:%[a-z0-9]+]] = fcmp oeq double %x, 0xFFF0000000000000
  72. ; CHECK-NEXT: [[SELECT:%[a-z0-9]+]] = select i1 [[FCMP]], double 0x7FF0000000000000, double [[FABS]]
  73. ret double %retval
  74. ; CHECK-NEXT: ret double [[SELECT]]
  75. }
  76. ; Check pow(-infinity, 0.5) -> +infinity.
  77. define float @test_simplify9(float %x) {
  78. ; CHECK-LABEL: @test_simplify9(
  79. %retval = call float @powf(float 0xFFF0000000000000, float 0.5)
  80. ret float %retval
  81. ; CHECK-NEXT: ret float 0x7FF0000000000000
  82. }
  83. define double @test_simplify10(double %x) {
  84. ; CHECK-LABEL: @test_simplify10(
  85. %retval = call double @pow(double 0xFFF0000000000000, double 0.5)
  86. ret double %retval
  87. ; CHECK-NEXT: ret double 0x7FF0000000000000
  88. }
  89. ; Check pow(x, 1.0) -> x.
  90. define float @test_simplify11(float %x) {
  91. ; CHECK-LABEL: @test_simplify11(
  92. %retval = call float @powf(float %x, float 1.0)
  93. ret float %retval
  94. ; CHECK-NEXT: ret float %x
  95. }
  96. define double @test_simplify12(double %x) {
  97. ; CHECK-LABEL: @test_simplify12(
  98. %retval = call double @pow(double %x, double 1.0)
  99. ret double %retval
  100. ; CHECK-NEXT: ret double %x
  101. }
  102. ; Check pow(x, 2.0) -> x*x.
  103. define float @test_simplify13(float %x) {
  104. ; CHECK-LABEL: @test_simplify13(
  105. %retval = call float @powf(float %x, float 2.0)
  106. ; CHECK-NEXT: [[SQUARE:%[a-z0-9]+]] = fmul float %x, %x
  107. ret float %retval
  108. ; CHECK-NEXT: ret float [[SQUARE]]
  109. }
  110. define double @test_simplify14(double %x) {
  111. ; CHECK-LABEL: @test_simplify14(
  112. %retval = call double @pow(double %x, double 2.0)
  113. ; CHECK-NEXT: [[SQUARE:%[a-z0-9]+]] = fmul double %x, %x
  114. ret double %retval
  115. ; CHECK-NEXT: ret double [[SQUARE]]
  116. }
  117. ; Check pow(x, -1.0) -> 1.0/x.
  118. define float @test_simplify15(float %x) {
  119. ; CHECK-LABEL: @test_simplify15(
  120. %retval = call float @powf(float %x, float -1.0)
  121. ; CHECK-NEXT: [[RECIPROCAL:%[a-z0-9]+]] = fdiv float 1.000000e+00, %x
  122. ret float %retval
  123. ; CHECK-NEXT: ret float [[RECIPROCAL]]
  124. }
  125. define double @test_simplify16(double %x) {
  126. ; CHECK-LABEL: @test_simplify16(
  127. %retval = call double @pow(double %x, double -1.0)
  128. ; CHECK-NEXT: [[RECIPROCAL:%[a-z0-9]+]] = fdiv double 1.000000e+00, %x
  129. ret double %retval
  130. ; CHECK-NEXT: ret double [[RECIPROCAL]]
  131. }
  132. declare double @llvm.pow.f64(double %Val, double %Power)
  133. define double @test_simplify17(double %x) {
  134. ; CHECK-LABEL: @test_simplify17(
  135. %retval = call double @llvm.pow.f64(double %x, double 0.5)
  136. ; CHECK-NEXT: [[SQRT:%[a-z0-9]+]] = call double @sqrt(double %x)
  137. ; CHECK-NEXT: [[FABS:%[a-z0-9]+]] = call double @fabs(double [[SQRT]])
  138. ; CHECK-NEXT: [[FCMP:%[a-z0-9]+]] = fcmp oeq double %x, 0xFFF0000000000000
  139. ; CHECK-NEXT: [[SELECT:%[a-z0-9]+]] = select i1 [[FCMP]], double 0x7FF0000000000000, double [[FABS]]
  140. ret double %retval
  141. ; CHECK-NEXT: ret double [[SELECT]]
  142. }
  143. ; Check pow(10.0, x) -> __exp10(x) on OS X 10.9+ and iOS 7.0+.
  144. define float @test_simplify18(float %x) {
  145. ; CHECK-LABEL: @test_simplify18(
  146. %retval = call float @powf(float 10.0, float %x)
  147. ; CHECK-EXP10: [[EXP10F:%[_a-z0-9]+]] = call float @__exp10f(float %x) [[NUW_RO:#[0-9]+]]
  148. ret float %retval
  149. ; CHECK-EXP10: ret float [[EXP10F]]
  150. ; CHECK-NO-EXP10: call float @powf
  151. }
  152. define double @test_simplify19(double %x) {
  153. ; CHECK-LABEL: @test_simplify19(
  154. %retval = call double @pow(double 10.0, double %x)
  155. ; CHECK-EXP10: [[EXP10:%[_a-z0-9]+]] = call double @__exp10(double %x) [[NUW_RO]]
  156. ret double %retval
  157. ; CHECK-EXP10: ret double [[EXP10]]
  158. ; CHECK-NO-EXP10: call double @pow
  159. }
  160. ; CHECK: attributes [[NUW_RO]] = { nounwind readonly }