xor2.ll 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. ; This test makes sure that these instructions are properly eliminated.
  2. ;
  3. ; RUN: opt < %s -instcombine -S | FileCheck %s
  4. ; PR1253
  5. define i1 @test0(i32 %A) {
  6. ; CHECK-LABEL: @test0(
  7. ; CHECK: %C = icmp slt i32 %A, 0
  8. %B = xor i32 %A, -2147483648
  9. %C = icmp sgt i32 %B, -1
  10. ret i1 %C
  11. }
  12. define i1 @test1(i32 %A) {
  13. ; CHECK-LABEL: @test1(
  14. ; CHECK: %C = icmp slt i32 %A, 0
  15. %B = xor i32 %A, 12345
  16. %C = icmp slt i32 %B, 0
  17. ret i1 %C
  18. }
  19. ; PR1014
  20. define i32 @test2(i32 %tmp1) {
  21. ; CHECK-LABEL: @test2(
  22. ; CHECK-NEXT: and i32 %tmp1, 32
  23. ; CHECK-NEXT: or i32 %ovm, 8
  24. ; CHECK-NEXT: ret i32
  25. %ovm = and i32 %tmp1, 32
  26. %ov3 = add i32 %ovm, 145
  27. %ov110 = xor i32 %ov3, 153
  28. ret i32 %ov110
  29. }
  30. define i32 @test3(i32 %tmp1) {
  31. ; CHECK-LABEL: @test3(
  32. ; CHECK-NEXT: and i32 %tmp1, 32
  33. ; CHECK-NEXT: or i32 %ovm, 8
  34. ; CHECK-NEXT: ret i32
  35. %ovm = or i32 %tmp1, 145
  36. %ov31 = and i32 %ovm, 177
  37. %ov110 = xor i32 %ov31, 153
  38. ret i32 %ov110
  39. }
  40. define i32 @test4(i32 %A, i32 %B) {
  41. %1 = xor i32 %A, -1
  42. %2 = ashr i32 %1, %B
  43. %3 = xor i32 %2, -1
  44. ret i32 %3
  45. ; CHECK-LABEL: @test4(
  46. ; CHECK: %1 = ashr i32 %A, %B
  47. ; CHECK: ret i32 %1
  48. }
  49. ; defect-2 in rdar://12329730
  50. ; (X^C1) >> C2) ^ C3 -> (X>>C2) ^ ((C1>>C2)^C3)
  51. ; where the "X" has more than one use
  52. define i32 @test5(i32 %val1) {
  53. test5:
  54. %xor = xor i32 %val1, 1234
  55. %shr = lshr i32 %xor, 8
  56. %xor1 = xor i32 %shr, 1
  57. %add = add i32 %xor1, %xor
  58. ret i32 %add
  59. ; CHECK-LABEL: @test5(
  60. ; CHECK: lshr i32 %val1, 8
  61. ; CHECK: ret
  62. }
  63. ; defect-1 in rdar://12329730
  64. ; Simplify (X^Y) -> X or Y in the user's context if we know that
  65. ; only bits from X or Y are demanded.
  66. ; e.g. the "x ^ 1234" can be optimized into x in the context of "t >> 16".
  67. ; Put in other word, t >> 16 -> x >> 16.
  68. ; unsigned foo(unsigned x) { unsigned t = x ^ 1234; ; return (t >> 16) + t;}
  69. define i32 @test6(i32 %x) {
  70. %xor = xor i32 %x, 1234
  71. %shr = lshr i32 %xor, 16
  72. %add = add i32 %shr, %xor
  73. ret i32 %add
  74. ; CHECK-LABEL: @test6(
  75. ; CHECK: lshr i32 %x, 16
  76. ; CHECK: ret
  77. }
  78. ; (A | B) ^ (~A) -> (A | ~B)
  79. define i32 @test7(i32 %a, i32 %b) {
  80. %or = or i32 %a, %b
  81. %neg = xor i32 %a, -1
  82. %xor = xor i32 %or, %neg
  83. ret i32 %xor
  84. ; CHECK-LABEL: @test7(
  85. ; CHECK-NEXT: %[[b_not:.*]] = xor i32 %b, -1
  86. ; CHECK-NEXT: %[[or:.*]] = or i32 %a, %[[b_not]]
  87. ; CHECK-NEXT: ret i32 %[[or]]
  88. }
  89. ; (~A) ^ (A | B) -> (A | ~B)
  90. define i32 @test8(i32 %a, i32 %b) {
  91. %neg = xor i32 %a, -1
  92. %or = or i32 %a, %b
  93. %xor = xor i32 %neg, %or
  94. ret i32 %xor
  95. ; CHECK-LABEL: @test8(
  96. ; CHECK-NEXT: %[[b_not:.*]] = xor i32 %b, -1
  97. ; CHECK-NEXT: %[[or:.*]] = or i32 %a, %[[b_not]]
  98. ; CHECK-NEXT: ret i32 %[[or]]
  99. }
  100. ; (A & B) ^ (A ^ B) -> (A | B)
  101. define i32 @test9(i32 %b, i32 %c) {
  102. %and = and i32 %b, %c
  103. %xor = xor i32 %b, %c
  104. %xor2 = xor i32 %and, %xor
  105. ret i32 %xor2
  106. ; CHECK-LABEL: @test9(
  107. ; CHECK-NEXT: %xor2 = or i32 %b, %c
  108. }
  109. ; (A ^ B) ^ (A & B) -> (A | B)
  110. define i32 @test10(i32 %b, i32 %c) {
  111. %xor = xor i32 %b, %c
  112. %and = and i32 %b, %c
  113. %xor2 = xor i32 %xor, %and
  114. ret i32 %xor2
  115. ; CHECK-LABEL: @test10(
  116. ; CHECK-NEXT: %xor2 = or i32 %b, %c
  117. }
  118. define i32 @test11(i32 %A, i32 %B) {
  119. %xor1 = xor i32 %B, %A
  120. %not = xor i32 %A, -1
  121. %xor2 = xor i32 %not, %B
  122. %and = and i32 %xor1, %xor2
  123. ret i32 %and
  124. ; CHECK-LABEL: @test11(
  125. ; CHECK-NEXT: ret i32 0
  126. }
  127. define i32 @test12(i32 %a, i32 %b) {
  128. %negb = xor i32 %b, -1
  129. %and = and i32 %a, %negb
  130. %nega = xor i32 %a, -1
  131. %xor = xor i32 %and, %nega
  132. ret i32 %xor
  133. ; CHECK-LABEL: @test12(
  134. ; CHECK-NEXT: %1 = and i32 %a, %b
  135. ; CHECK-NEXT: %xor = xor i32 %1, -1
  136. }
  137. define i32 @test13(i32 %a, i32 %b) {
  138. %nega = xor i32 %a, -1
  139. %negb = xor i32 %b, -1
  140. %and = and i32 %a, %negb
  141. %xor = xor i32 %nega, %and
  142. ret i32 %xor
  143. ; CHECK-LABEL: @test13(
  144. ; CHECK-NEXT: %1 = and i32 %a, %b
  145. ; CHECK-NEXT: %xor = xor i32 %1, -1
  146. }
  147. ; (A ^ C) ^ (A | B) -> ((~A) & B) ^ C
  148. define i32 @test14(i32 %a, i32 %b, i32 %c) {
  149. %neg = xor i32 %a, %c
  150. %or = or i32 %a, %b
  151. %xor = xor i32 %neg, %or
  152. ret i32 %xor
  153. ; CHECK-LABEL: @test14(
  154. ; CHECK-NEXT: %[[not:.*]] = xor i32 %a, -1
  155. ; CHECK-NEXT: %[[and:.*]] = and i32 %[[not]], %b
  156. ; CHECK-NEXT: %[[xor:.*]] = xor i32 %[[and]], %c
  157. ; CHECK-NEXT: ret i32 %[[xor]]
  158. }