range-check.ll 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. ; RUN: opt < %s -instcombine -S | FileCheck %s
  2. ; Check simplification of
  3. ; (icmp sgt x, -1) & (icmp sgt/sge n, x) --> icmp ugt/uge n, x
  4. ; CHECK-LABEL: define i1 @test_and1
  5. ; CHECK: [[R:%[0-9]+]] = icmp ugt i32 %nn, %x
  6. ; CHECK: ret i1 [[R]]
  7. define i1 @test_and1(i32 %x, i32 %n) {
  8. %nn = and i32 %n, 2147483647
  9. %a = icmp sge i32 %x, 0
  10. %b = icmp slt i32 %x, %nn
  11. %c = and i1 %a, %b
  12. ret i1 %c
  13. }
  14. ; CHECK-LABEL: define i1 @test_and2
  15. ; CHECK: [[R:%[0-9]+]] = icmp uge i32 %nn, %x
  16. ; CHECK: ret i1 [[R]]
  17. define i1 @test_and2(i32 %x, i32 %n) {
  18. %nn = and i32 %n, 2147483647
  19. %a = icmp sgt i32 %x, -1
  20. %b = icmp sle i32 %x, %nn
  21. %c = and i1 %a, %b
  22. ret i1 %c
  23. }
  24. ; CHECK-LABEL: define i1 @test_and3
  25. ; CHECK: [[R:%[0-9]+]] = icmp ugt i32 %nn, %x
  26. ; CHECK: ret i1 [[R]]
  27. define i1 @test_and3(i32 %x, i32 %n) {
  28. %nn = and i32 %n, 2147483647
  29. %a = icmp sgt i32 %nn, %x
  30. %b = icmp sge i32 %x, 0
  31. %c = and i1 %a, %b
  32. ret i1 %c
  33. }
  34. ; CHECK-LABEL: define i1 @test_and4
  35. ; CHECK: [[R:%[0-9]+]] = icmp uge i32 %nn, %x
  36. ; CHECK: ret i1 [[R]]
  37. define i1 @test_and4(i32 %x, i32 %n) {
  38. %nn = and i32 %n, 2147483647
  39. %a = icmp sge i32 %nn, %x
  40. %b = icmp sge i32 %x, 0
  41. %c = and i1 %a, %b
  42. ret i1 %c
  43. }
  44. ; CHECK-LABEL: define i1 @test_or1
  45. ; CHECK: [[R:%[0-9]+]] = icmp ule i32 %nn, %x
  46. ; CHECK: ret i1 [[R]]
  47. define i1 @test_or1(i32 %x, i32 %n) {
  48. %nn = and i32 %n, 2147483647
  49. %a = icmp slt i32 %x, 0
  50. %b = icmp sge i32 %x, %nn
  51. %c = or i1 %a, %b
  52. ret i1 %c
  53. }
  54. ; CHECK-LABEL: define i1 @test_or2
  55. ; CHECK: [[R:%[0-9]+]] = icmp ult i32 %nn, %x
  56. ; CHECK: ret i1 [[R]]
  57. define i1 @test_or2(i32 %x, i32 %n) {
  58. %nn = and i32 %n, 2147483647
  59. %a = icmp sle i32 %x, -1
  60. %b = icmp sgt i32 %x, %nn
  61. %c = or i1 %a, %b
  62. ret i1 %c
  63. }
  64. ; CHECK-LABEL: define i1 @test_or3
  65. ; CHECK: [[R:%[0-9]+]] = icmp ule i32 %nn, %x
  66. ; CHECK: ret i1 [[R]]
  67. define i1 @test_or3(i32 %x, i32 %n) {
  68. %nn = and i32 %n, 2147483647
  69. %a = icmp sle i32 %nn, %x
  70. %b = icmp slt i32 %x, 0
  71. %c = or i1 %a, %b
  72. ret i1 %c
  73. }
  74. ; CHECK-LABEL: define i1 @test_or4
  75. ; CHECK: [[R:%[0-9]+]] = icmp ult i32 %nn, %x
  76. ; CHECK: ret i1 [[R]]
  77. define i1 @test_or4(i32 %x, i32 %n) {
  78. %nn = and i32 %n, 2147483647
  79. %a = icmp slt i32 %nn, %x
  80. %b = icmp slt i32 %x, 0
  81. %c = or i1 %a, %b
  82. ret i1 %c
  83. }
  84. ; Negative tests
  85. ; CHECK-LABEL: define i1 @negative1
  86. ; CHECK: %a = icmp
  87. ; CHECK: %b = icmp
  88. ; CHECK: %c = and i1 %a, %b
  89. ; CHECK: ret i1 %c
  90. define i1 @negative1(i32 %x, i32 %n) {
  91. %nn = and i32 %n, 2147483647
  92. %a = icmp slt i32 %x, %nn
  93. %b = icmp sgt i32 %x, 0 ; should be: icmp sge
  94. %c = and i1 %a, %b
  95. ret i1 %c
  96. }
  97. ; CHECK-LABEL: define i1 @negative2
  98. ; CHECK: %a = icmp
  99. ; CHECK: %b = icmp
  100. ; CHECK: %c = and i1 %a, %b
  101. ; CHECK: ret i1 %c
  102. define i1 @negative2(i32 %x, i32 %n) {
  103. %a = icmp slt i32 %x, %n ; n can be negative
  104. %b = icmp sge i32 %x, 0
  105. %c = and i1 %a, %b
  106. ret i1 %c
  107. }
  108. ; CHECK-LABEL: define i1 @negative3
  109. ; CHECK: %a = icmp
  110. ; CHECK: %b = icmp
  111. ; CHECK: %c = and i1 %a, %b
  112. ; CHECK: ret i1 %c
  113. define i1 @negative3(i32 %x, i32 %y, i32 %n) {
  114. %nn = and i32 %n, 2147483647
  115. %a = icmp slt i32 %x, %nn
  116. %b = icmp sge i32 %y, 0 ; should compare %x and not %y
  117. %c = and i1 %a, %b
  118. ret i1 %c
  119. }
  120. ; CHECK-LABEL: define i1 @negative4
  121. ; CHECK: %a = icmp
  122. ; CHECK: %b = icmp
  123. ; CHECK: %c = and i1 %a, %b
  124. ; CHECK: ret i1 %c
  125. define i1 @negative4(i32 %x, i32 %n) {
  126. %nn = and i32 %n, 2147483647
  127. %a = icmp ne i32 %x, %nn ; should be: icmp slt/sle
  128. %b = icmp sge i32 %x, 0
  129. %c = and i1 %a, %b
  130. ret i1 %c
  131. }
  132. ; CHECK-LABEL: define i1 @negative5
  133. ; CHECK: %a = icmp
  134. ; CHECK: %b = icmp
  135. ; CHECK: %c = or i1 %a, %b
  136. ; CHECK: ret i1 %c
  137. define i1 @negative5(i32 %x, i32 %n) {
  138. %nn = and i32 %n, 2147483647
  139. %a = icmp slt i32 %x, %nn
  140. %b = icmp sge i32 %x, 0
  141. %c = or i1 %a, %b ; should be: and
  142. ret i1 %c
  143. }