sitofp.ll 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. ; RUN: opt < %s -instcombine -S | FileCheck %s
  2. ; CHECK-LABEL: test1
  3. ; CHECK: ret i1 true
  4. define i1 @test1(i8 %A) {
  5. %B = sitofp i8 %A to double
  6. %C = fcmp ult double %B, 128.0
  7. ret i1 %C
  8. }
  9. ; CHECK-LABEL: test2
  10. ; CHECK: ret i1 true
  11. define i1 @test2(i8 %A) {
  12. %B = sitofp i8 %A to double
  13. %C = fcmp ugt double %B, -128.1
  14. ret i1 %C
  15. }
  16. ; CHECK-LABEL: test3
  17. ; CHECK: ret i1 true
  18. define i1 @test3(i8 %A) {
  19. %B = sitofp i8 %A to double
  20. %C = fcmp ule double %B, 127.0
  21. ret i1 %C
  22. }
  23. ; CHECK-LABEL: test4
  24. ; CHECK: icmp ne i8 %A, 127
  25. ; CHECK-NEXT: ret i1
  26. define i1 @test4(i8 %A) {
  27. %B = sitofp i8 %A to double
  28. %C = fcmp ult double %B, 127.0
  29. ret i1 %C
  30. }
  31. ; CHECK-LABEL: test5
  32. ; CHECK: ret i32
  33. define i32 @test5(i32 %A) {
  34. %B = sitofp i32 %A to double
  35. %C = fptosi double %B to i32
  36. %D = uitofp i32 %C to double
  37. %E = fptoui double %D to i32
  38. ret i32 %E
  39. }
  40. ; CHECK-LABEL: test6
  41. ; CHECK: and i32 %A, 39
  42. ; CHECK-NEXT: ret i32
  43. define i32 @test6(i32 %A) {
  44. %B = and i32 %A, 7
  45. %C = and i32 %A, 32
  46. %D = sitofp i32 %B to double
  47. %E = sitofp i32 %C to double
  48. %F = fadd double %D, %E
  49. %G = fptosi double %F to i32
  50. ret i32 %G
  51. }
  52. ; CHECK-LABEL: test7
  53. ; CHECK: ret i32
  54. define i32 @test7(i32 %A) nounwind {
  55. %B = sitofp i32 %A to double
  56. %C = fptoui double %B to i32
  57. ret i32 %C
  58. }
  59. ; CHECK-LABEL: test8
  60. ; CHECK: ret i32
  61. define i32 @test8(i32 %A) nounwind {
  62. %B = uitofp i32 %A to double
  63. %C = fptosi double %B to i32
  64. ret i32 %C
  65. }
  66. ; CHECK-LABEL: test9
  67. ; CHECK: zext i8
  68. ; CHECK-NEXT: ret i32
  69. define i32 @test9(i8 %A) nounwind {
  70. %B = sitofp i8 %A to float
  71. %C = fptoui float %B to i32
  72. ret i32 %C
  73. }
  74. ; CHECK-LABEL: test10
  75. ; CHECK: sext i8
  76. ; CHECK-NEXT: ret i32
  77. define i32 @test10(i8 %A) nounwind {
  78. %B = sitofp i8 %A to float
  79. %C = fptosi float %B to i32
  80. ret i32 %C
  81. }
  82. ; If the input value is outside of the range of the output cast, it's
  83. ; undefined behavior, so we can assume it fits.
  84. ; CHECK-LABEL: test11
  85. ; CHECK: trunc
  86. ; CHECK-NEXT: ret i8
  87. define i8 @test11(i32 %A) nounwind {
  88. %B = sitofp i32 %A to float
  89. %C = fptosi float %B to i8
  90. ret i8 %C
  91. }
  92. ; If the input value is negative, it'll be outside the range of the
  93. ; output cast, and thus undefined behavior.
  94. ; CHECK-LABEL: test12
  95. ; CHECK: zext i8
  96. ; CHECK-NEXT: ret i32
  97. define i32 @test12(i8 %A) nounwind {
  98. %B = sitofp i8 %A to float
  99. %C = fptoui float %B to i32
  100. ret i32 %C
  101. }
  102. ; This can't fold because the 25-bit input doesn't fit in the mantissa.
  103. ; CHECK-LABEL: test13
  104. ; CHECK: uitofp
  105. ; CHECK-NEXT: fptoui
  106. define i32 @test13(i25 %A) nounwind {
  107. %B = uitofp i25 %A to float
  108. %C = fptoui float %B to i32
  109. ret i32 %C
  110. }
  111. ; But this one can.
  112. ; CHECK-LABEL: test14
  113. ; CHECK: zext i24
  114. ; CHECK-NEXT: ret i32
  115. define i32 @test14(i24 %A) nounwind {
  116. %B = uitofp i24 %A to float
  117. %C = fptoui float %B to i32
  118. ret i32 %C
  119. }
  120. ; And this one can too.
  121. ; CHECK-LABEL: test15
  122. ; CHECK: trunc i32
  123. ; CHECK-NEXT: ret i24
  124. define i24 @test15(i32 %A) nounwind {
  125. %B = uitofp i32 %A to float
  126. %C = fptoui float %B to i24
  127. ret i24 %C
  128. }
  129. ; This can fold because the 25-bit input is signed and we disard the sign bit.
  130. ; CHECK-LABEL: test16
  131. ; CHECK: zext
  132. define i32 @test16(i25 %A) nounwind {
  133. %B = sitofp i25 %A to float
  134. %C = fptoui float %B to i32
  135. ret i32 %C
  136. }
  137. ; This can't fold because the 26-bit input won't fit the mantissa
  138. ; even after disarding the signed bit.
  139. ; CHECK-LABEL: test17
  140. ; CHECK: sitofp
  141. ; CHECK-NEXT: fptoui
  142. define i32 @test17(i26 %A) nounwind {
  143. %B = sitofp i26 %A to float
  144. %C = fptoui float %B to i32
  145. ret i32 %C
  146. }
  147. ; This can fold because the 54-bit output is signed and we disard the sign bit.
  148. ; CHECK-LABEL: test18
  149. ; CHECK: trunc
  150. define i54 @test18(i64 %A) nounwind {
  151. %B = sitofp i64 %A to double
  152. %C = fptosi double %B to i54
  153. ret i54 %C
  154. }
  155. ; This can't fold because the 55-bit output won't fit the mantissa
  156. ; even after disarding the sign bit.
  157. ; CHECK-LABEL: test19
  158. ; CHECK: sitofp
  159. ; CHECK-NEXT: fptosi
  160. define i55 @test19(i64 %A) nounwind {
  161. %B = sitofp i64 %A to double
  162. %C = fptosi double %B to i55
  163. ret i55 %C
  164. }