or-xor.ll 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ; RUN: opt -S -instcombine < %s | FileCheck %s
  2. define i32 @test1(i32 %x, i32 %y) nounwind {
  3. %or = or i32 %x, %y
  4. %not = xor i32 %or, -1
  5. %z = or i32 %x, %not
  6. ret i32 %z
  7. ; CHECK-LABEL: @test1(
  8. ; CHECK-NEXT: %y.not = xor i32 %y, -1
  9. ; CHECK-NEXT: %z = or i32 %y.not, %x
  10. ; CHECK-NEXT: ret i32 %z
  11. }
  12. define i32 @test2(i32 %x, i32 %y) nounwind {
  13. %or = or i32 %x, %y
  14. %not = xor i32 %or, -1
  15. %z = or i32 %y, %not
  16. ret i32 %z
  17. ; CHECK-LABEL: @test2(
  18. ; CHECK-NEXT: %x.not = xor i32 %x, -1
  19. ; CHECK-NEXT: %z = or i32 %x.not, %y
  20. ; CHECK-NEXT: ret i32 %z
  21. }
  22. define i32 @test3(i32 %x, i32 %y) nounwind {
  23. %xor = xor i32 %x, %y
  24. %not = xor i32 %xor, -1
  25. %z = or i32 %x, %not
  26. ret i32 %z
  27. ; CHECK-LABEL: @test3(
  28. ; CHECK-NEXT: %y.not = xor i32 %y, -1
  29. ; CHECK-NEXT: %z = or i32 %y.not, %x
  30. ; CHECK-NEXT: ret i32 %z
  31. }
  32. define i32 @test4(i32 %x, i32 %y) nounwind {
  33. %xor = xor i32 %x, %y
  34. %not = xor i32 %xor, -1
  35. %z = or i32 %y, %not
  36. ret i32 %z
  37. ; CHECK-LABEL: @test4(
  38. ; CHECK-NEXT: %x.not = xor i32 %x, -1
  39. ; CHECK-NEXT: %z = or i32 %x.not, %y
  40. ; CHECK-NEXT: ret i32 %z
  41. }
  42. define i32 @test5(i32 %x, i32 %y) nounwind {
  43. %and = and i32 %x, %y
  44. %not = xor i32 %and, -1
  45. %z = or i32 %x, %not
  46. ret i32 %z
  47. ; CHECK-LABEL: @test5(
  48. ; CHECK-NEXT: ret i32 -1
  49. }
  50. define i32 @test6(i32 %x, i32 %y) nounwind {
  51. %and = and i32 %x, %y
  52. %not = xor i32 %and, -1
  53. %z = or i32 %y, %not
  54. ret i32 %z
  55. ; CHECK-LABEL: @test6(
  56. ; CHECK-NEXT: ret i32 -1
  57. }
  58. define i32 @test7(i32 %x, i32 %y) nounwind {
  59. %xor = xor i32 %x, %y
  60. %z = or i32 %y, %xor
  61. ret i32 %z
  62. ; CHECK-LABEL: @test7(
  63. ; CHECK-NEXT: %z = or i32 %x, %y
  64. ; CHECK-NEXT: ret i32 %z
  65. }
  66. define i32 @test8(i32 %x, i32 %y) nounwind {
  67. %not = xor i32 %y, -1
  68. %xor = xor i32 %x, %not
  69. %z = or i32 %y, %xor
  70. ret i32 %z
  71. ; CHECK-LABEL: @test8(
  72. ; CHECK-NEXT: %x.not = xor i32 %x, -1
  73. ; CHECK-NEXT: %z = or i32 %x.not, %y
  74. ; CHECK-NEXT: ret i32 %z
  75. }
  76. define i32 @test9(i32 %x, i32 %y) nounwind {
  77. %not = xor i32 %x, -1
  78. %xor = xor i32 %not, %y
  79. %z = or i32 %x, %xor
  80. ret i32 %z
  81. ; CHECK-LABEL: @test9(
  82. ; CHECK-NEXT: %y.not = xor i32 %y, -1
  83. ; CHECK-NEXT: %z = or i32 %y.not, %x
  84. ; CHECK-NEXT: ret i32 %z
  85. }
  86. define i32 @test10(i32 %A, i32 %B) {
  87. %xor1 = xor i32 %B, %A
  88. %not = xor i32 %A, -1
  89. %xor2 = xor i32 %not, %B
  90. %or = or i32 %xor1, %xor2
  91. ret i32 %or
  92. ; CHECK-LABEL: @test10(
  93. ; CHECK-NEXT: ret i32 -1
  94. }
  95. ; (x | y) & ((~x) ^ y) -> (x & y)
  96. define i32 @test11(i32 %x, i32 %y) {
  97. %or = or i32 %x, %y
  98. %neg = xor i32 %x, -1
  99. %xor = xor i32 %neg, %y
  100. %and = and i32 %or, %xor
  101. ret i32 %and
  102. ; CHECK-LABEL: @test11(
  103. ; CHECK-NEXT: %and = and i32 %x, %y
  104. ; CHECK-NEXT: ret i32 %and
  105. }
  106. ; ((~x) ^ y) & (x | y) -> (x & y)
  107. define i32 @test12(i32 %x, i32 %y) {
  108. %neg = xor i32 %x, -1
  109. %xor = xor i32 %neg, %y
  110. %or = or i32 %x, %y
  111. %and = and i32 %xor, %or
  112. ret i32 %and
  113. ; CHECK-LABEL: @test12(
  114. ; CHECK-NEXT: %and = and i32 %x, %y
  115. ; CHECK-NEXT: ret i32 %and
  116. }
  117. ; ((x | y) ^ (x ^ y)) -> (x & y)
  118. define i32 @test13(i32 %x, i32 %y) {
  119. %1 = xor i32 %y, %x
  120. %2 = or i32 %y, %x
  121. %3 = xor i32 %2, %1
  122. ret i32 %3
  123. ; CHECK-LABEL: @test13(
  124. ; CHECK-NEXT: %1 = and i32 %y, %x
  125. ; CHECK-NEXT: ret i32 %1
  126. }
  127. ; ((x | ~y) ^ (~x | y)) -> x ^ y
  128. define i32 @test14(i32 %x, i32 %y) {
  129. %noty = xor i32 %y, -1
  130. %notx = xor i32 %x, -1
  131. %or1 = or i32 %x, %noty
  132. %or2 = or i32 %notx, %y
  133. %xor = xor i32 %or1, %or2
  134. ret i32 %xor
  135. ; CHECK-LABEL: @test14(
  136. ; CHECK-NEXT: %xor = xor i32 %x, %y
  137. ; CHECK-NEXT: ret i32 %xor
  138. }
  139. ; ((x & ~y) ^ (~x & y)) -> x ^ y
  140. define i32 @test15(i32 %x, i32 %y) {
  141. %noty = xor i32 %y, -1
  142. %notx = xor i32 %x, -1
  143. %and1 = and i32 %x, %noty
  144. %and2 = and i32 %notx, %y
  145. %xor = xor i32 %and1, %and2
  146. ret i32 %xor
  147. ; CHECK-LABEL: @test15(
  148. ; CHECK-NEXT: %xor = xor i32 %x, %y
  149. ; CHECK-NEXT: ret i32 %xor
  150. }
  151. define i32 @test16(i32 %a, i32 %b) {
  152. %or = xor i32 %a, %b
  153. %and1 = and i32 %or, 1
  154. %and2 = and i32 %b, -2
  155. %xor = or i32 %and1, %and2
  156. ret i32 %xor
  157. ; CHECK-LABEL: @test16(
  158. ; CHECK-NEXT: %1 = and i32 %a, 1
  159. ; CHECK-NEXT: %xor = xor i32 %1, %b
  160. ; CHECK-NEXT: ret i32 %xor
  161. }