Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 1 | ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py |
| 2 | ; RUN: opt < %s -instsimplify -S | FileCheck %s |
| 3 | |
| 4 | define i32 @test1(i32 %A) { |
| 5 | ; CHECK-LABEL: @test1( |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 6 | ; CHECK-NEXT: ret i32 [[A:%.*]] |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 7 | ; |
| 8 | %B = or i32 %A, 0 |
| 9 | ret i32 %B |
| 10 | } |
| 11 | |
| 12 | define i32 @all_ones(i32 %A) { |
| 13 | ; CHECK-LABEL: @all_ones( |
| 14 | ; CHECK-NEXT: ret i32 -1 |
| 15 | ; |
| 16 | %B = or i32 %A, -1 |
| 17 | ret i32 %B |
| 18 | } |
| 19 | |
| 20 | define <3 x i8> @all_ones_vec_with_undef_elt(<3 x i8> %A) { |
| 21 | ; CHECK-LABEL: @all_ones_vec_with_undef_elt( |
| 22 | ; CHECK-NEXT: ret <3 x i8> <i8 -1, i8 -1, i8 -1> |
| 23 | ; |
| 24 | %B = or <3 x i8> %A, <i8 -1, i8 undef, i8 -1> |
| 25 | ret <3 x i8> %B |
| 26 | } |
| 27 | |
| 28 | define i1 @test3(i1 %A) { |
| 29 | ; CHECK-LABEL: @test3( |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 30 | ; CHECK-NEXT: ret i1 [[A:%.*]] |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 31 | ; |
| 32 | %B = or i1 %A, false |
| 33 | ret i1 %B |
| 34 | } |
| 35 | |
| 36 | define i1 @test4(i1 %A) { |
| 37 | ; CHECK-LABEL: @test4( |
| 38 | ; CHECK-NEXT: ret i1 true |
| 39 | ; |
| 40 | %B = or i1 %A, true |
| 41 | ret i1 %B |
| 42 | } |
| 43 | |
| 44 | define i1 @test5(i1 %A) { |
| 45 | ; CHECK-LABEL: @test5( |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 46 | ; CHECK-NEXT: ret i1 [[A:%.*]] |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 47 | ; |
| 48 | %B = or i1 %A, %A |
| 49 | ret i1 %B |
| 50 | } |
| 51 | |
| 52 | define i32 @test6(i32 %A) { |
| 53 | ; CHECK-LABEL: @test6( |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 54 | ; CHECK-NEXT: ret i32 [[A:%.*]] |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 55 | ; |
| 56 | %B = or i32 %A, %A |
| 57 | ret i32 %B |
| 58 | } |
| 59 | |
| 60 | ; A | ~A == -1 |
| 61 | define i32 @test7(i32 %A) { |
| 62 | ; CHECK-LABEL: @test7( |
| 63 | ; CHECK-NEXT: ret i32 -1 |
| 64 | ; |
| 65 | %NotA = xor i32 %A, -1 |
| 66 | %B = or i32 %A, %NotA |
| 67 | ret i32 %B |
| 68 | } |
| 69 | |
| 70 | define i8 @test8(i8 %A) { |
| 71 | ; CHECK-LABEL: @test8( |
| 72 | ; CHECK-NEXT: ret i8 -1 |
| 73 | ; |
| 74 | %B = or i8 %A, -2 |
| 75 | %C = or i8 %B, 1 |
| 76 | ret i8 %C |
| 77 | } |
| 78 | |
| 79 | ; Test that (A|c1)|(B|c2) == (A|B)|(c1|c2) |
| 80 | define i8 @test9(i8 %A, i8 %B) { |
| 81 | ; CHECK-LABEL: @test9( |
| 82 | ; CHECK-NEXT: ret i8 -1 |
| 83 | ; |
| 84 | %C = or i8 %A, 1 |
| 85 | %D = or i8 %B, -2 |
| 86 | %E = or i8 %C, %D |
| 87 | ret i8 %E |
| 88 | } |
| 89 | |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 90 | ; (X & C1) | C2 --> (X | C2) & (C1|C2) |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 91 | define i8 @test10(i8 %A) { |
| 92 | ; CHECK-LABEL: @test10( |
| 93 | ; CHECK-NEXT: ret i8 -2 |
| 94 | ; |
| 95 | %B = or i8 %A, 1 |
| 96 | %C = and i8 %B, -2 |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 97 | %D = or i8 %C, -2 |
| 98 | ret i8 %D |
| 99 | } |
| 100 | |
Nikita Popov | 5a22656 | 2020-03-20 11:57:20 +0100 | [diff] [blame] | 101 | ; The following two cases only get folded by InstCombine, |
| 102 | ; see InstCombine/or-xor.ll. |
| 103 | |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 104 | ; (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 105 | define i8 @test11(i8 %A) { |
| 106 | ; CHECK-LABEL: @test11( |
Nikita Popov | 5a22656 | 2020-03-20 11:57:20 +0100 | [diff] [blame] | 107 | ; CHECK-NEXT: [[B:%.*]] = or i8 [[A:%.*]], -2 |
| 108 | ; CHECK-NEXT: [[C:%.*]] = xor i8 [[B]], 13 |
| 109 | ; CHECK-NEXT: [[D:%.*]] = or i8 [[C]], 1 |
| 110 | ; CHECK-NEXT: [[E:%.*]] = xor i8 [[D]], 12 |
| 111 | ; CHECK-NEXT: ret i8 [[E]] |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 112 | ; |
| 113 | %B = or i8 %A, -2 |
| 114 | %C = xor i8 %B, 13 |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 115 | %D = or i8 %C, 1 |
| 116 | %E = xor i8 %D, 12 |
| 117 | ret i8 %E |
| 118 | } |
| 119 | |
Simon Pilgrim | 47ce140 | 2020-03-18 19:12:43 +0000 | [diff] [blame] | 120 | define i8 @test11v(<2 x i8> %A) { |
| 121 | ; CHECK-LABEL: @test11v( |
Nikita Popov | 5a22656 | 2020-03-20 11:57:20 +0100 | [diff] [blame] | 122 | ; CHECK-NEXT: [[B:%.*]] = or <2 x i8> [[A:%.*]], <i8 -2, i8 0> |
| 123 | ; CHECK-NEXT: [[CV:%.*]] = xor <2 x i8> [[B]], <i8 13, i8 13> |
| 124 | ; CHECK-NEXT: [[C:%.*]] = extractelement <2 x i8> [[CV]], i32 0 |
| 125 | ; CHECK-NEXT: [[D:%.*]] = or i8 [[C]], 1 |
| 126 | ; CHECK-NEXT: [[E:%.*]] = xor i8 [[D]], 12 |
| 127 | ; CHECK-NEXT: ret i8 [[E]] |
Simon Pilgrim | 47ce140 | 2020-03-18 19:12:43 +0000 | [diff] [blame] | 128 | ; |
| 129 | %B = or <2 x i8> %A, <i8 -2, i8 0> |
| 130 | %CV = xor <2 x i8> %B, <i8 13, i8 13> |
| 131 | %C = extractelement <2 x i8> %CV, i32 0 |
| 132 | %D = or i8 %C, 1 |
| 133 | %E = xor i8 %D, 12 |
| 134 | ret i8 %E |
| 135 | } |
| 136 | |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 137 | ; Test the case where integer BitWidth <= 64 && BitWidth % 2 != 0. |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 138 | ; If we have: ((V + N) & C1) | (V & C2) |
| 139 | ; .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 140 | ; replace with V+N. |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 141 | define i39 @test1_apint(i39 %V, i39 %M) { |
| 142 | ; CHECK-LABEL: @test1_apint( |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 143 | ; CHECK-NEXT: [[N:%.*]] = and i39 [[M:%.*]], -274877906944 |
| 144 | ; CHECK-NEXT: [[A:%.*]] = add i39 [[V:%.*]], [[N]] |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 145 | ; CHECK-NEXT: ret i39 [[A]] |
| 146 | ; |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 147 | %C1 = xor i39 274877906943, -1 ;; C2 = 274877906943 |
| 148 | %N = and i39 %M, 274877906944 |
| 149 | %A = add i39 %V, %N |
| 150 | %B = and i39 %A, %C1 |
| 151 | %D = and i39 %V, 274877906943 |
| 152 | %R = or i39 %B, %D |
| 153 | ret i39 %R |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | define i7 @test2_apint(i7 %X) { |
| 157 | ; CHECK-LABEL: @test2_apint( |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 158 | ; CHECK-NEXT: ret i7 [[X:%.*]] |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 159 | ; |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 160 | %Y = or i7 %X, 0 |
| 161 | ret i7 %Y |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | define i17 @test3_apint(i17 %X) { |
| 165 | ; CHECK-LABEL: @test3_apint( |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 166 | ; CHECK-NEXT: ret i17 -1 |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 167 | ; |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 168 | %Y = or i17 %X, -1 |
| 169 | ret i17 %Y |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | ; Test the case where Integer BitWidth > 64 && BitWidth <= 1024. |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 173 | ; If we have: ((V + N) & C1) | (V & C2) |
| 174 | ; .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 175 | ; replace with V+N. |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 176 | define i399 @test4_apint(i399 %V, i399 %M) { |
| 177 | ; CHECK-LABEL: @test4_apint( |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 178 | ; CHECK-NEXT: [[N:%.*]] = and i399 [[M:%.*]], 18446742974197923840 |
| 179 | ; CHECK-NEXT: [[A:%.*]] = add i399 [[V:%.*]], [[N]] |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 180 | ; CHECK-NEXT: ret i399 [[A]] |
| 181 | ; |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 182 | %C1 = xor i399 274877906943, -1 ;; C2 = 274877906943 |
| 183 | %N = and i399 %M, 18446742974197923840 |
| 184 | %A = add i399 %V, %N |
| 185 | %B = and i399 %A, %C1 |
| 186 | %D = and i399 %V, 274877906943 |
| 187 | %R = or i399 %D, %B |
| 188 | ret i399 %R |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | define i777 @test5_apint(i777 %X) { |
| 192 | ; CHECK-LABEL: @test5_apint( |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 193 | ; CHECK-NEXT: ret i777 [[X:%.*]] |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 194 | ; |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 195 | %Y = or i777 %X, 0 |
| 196 | ret i777 %Y |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | define i117 @test6_apint(i117 %X) { |
| 200 | ; CHECK-LABEL: @test6_apint( |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 201 | ; CHECK-NEXT: ret i117 -1 |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 202 | ; |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 203 | %Y = or i117 %X, -1 |
| 204 | ret i117 %Y |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | ; Test the case where integer BitWidth <= 64 && BitWidth % 2 != 0. |
| 208 | ; Vector version of test1_apint with the add commuted |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 209 | ; If we have: ((V + N) & C1) | (V & C2) |
| 210 | ; .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 211 | ; replace with V+N. |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 212 | define <2 x i39> @test7_apint(<2 x i39> %V, <2 x i39> %M) { |
| 213 | ; CHECK-LABEL: @test7_apint( |
| 214 | ; CHECK-NEXT: [[N:%.*]] = and <2 x i39> [[M:%.*]], <i39 -274877906944, i39 -274877906944> |
| 215 | ; CHECK-NEXT: [[A:%.*]] = add <2 x i39> [[N]], [[V:%.*]] |
| 216 | ; CHECK-NEXT: ret <2 x i39> [[A]] |
| 217 | ; |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 218 | %C1 = xor <2 x i39> <i39 274877906943, i39 274877906943>, <i39 -1, i39 -1> ;; C2 = 274877906943 |
| 219 | %N = and <2 x i39> %M, <i39 274877906944, i39 274877906944> |
| 220 | %A = add <2 x i39> %N, %V |
| 221 | %B = and <2 x i39> %A, %C1 |
| 222 | %D = and <2 x i39> %V, <i39 274877906943, i39 274877906943> |
| 223 | %R = or <2 x i39> %B, %D |
| 224 | ret <2 x i39> %R |
| 225 | } |
| 226 | |
| 227 | ; Test the case where Integer BitWidth > 64 && BitWidth <= 1024. |
| 228 | ; Vector version of test4_apint with the add and the or commuted |
Simon Pilgrim | 6bdb0ef | 2020-03-18 19:02:44 +0000 | [diff] [blame] | 229 | ; If we have: ((V + N) & C1) | (V & C2) |
| 230 | ; .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 231 | ; replace with V+N. |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 232 | define <2 x i399> @test8_apint(<2 x i399> %V, <2 x i399> %M) { |
| 233 | ; CHECK-LABEL: @test8_apint( |
| 234 | ; CHECK-NEXT: [[N:%.*]] = and <2 x i399> [[M:%.*]], <i399 18446742974197923840, i399 18446742974197923840> |
| 235 | ; CHECK-NEXT: [[A:%.*]] = add <2 x i399> [[N]], [[V:%.*]] |
| 236 | ; CHECK-NEXT: ret <2 x i399> [[A]] |
| 237 | ; |
Eric Christopher | cee313d | 2019-04-17 04:52:47 +0000 | [diff] [blame] | 238 | %C1 = xor <2 x i399> <i399 274877906943, i399 274877906943>, <i399 -1, i399 -1> ;; C2 = 274877906943 |
| 239 | %N = and <2 x i399> %M, <i399 18446742974197923840, i399 18446742974197923840> |
| 240 | %A = add <2 x i399> %N, %V |
| 241 | %B = and <2 x i399> %A, %C1 |
| 242 | %D = and <2 x i399> %V, <i399 274877906943, i399 274877906943> |
| 243 | %R = or <2 x i399> %D, %B |
| 244 | ret <2 x i399> %R |
| 245 | } |
Sanjay Patel | a26cd73 | 2020-06-03 08:00:35 -0400 | [diff] [blame] | 246 | |
| 247 | ; A | ~(A & B) = -1 |
| 248 | |
| 249 | define i1 @or_with_not_op_commute1(i1 %a, i1 %b) { |
| 250 | ; CHECK-LABEL: @or_with_not_op_commute1( |
Dorit Nuzman | a9fe69c | 2020-06-03 13:11:54 -0400 | [diff] [blame^] | 251 | ; CHECK-NEXT: ret i1 true |
Sanjay Patel | a26cd73 | 2020-06-03 08:00:35 -0400 | [diff] [blame] | 252 | ; |
| 253 | %ab = and i1 %a, %b |
| 254 | %not = xor i1 %ab, -1 |
| 255 | %r = or i1 %a, %not |
| 256 | ret i1 %r |
| 257 | } |
| 258 | |
| 259 | ; A | ~(B & A) = -1 |
| 260 | |
| 261 | define i8 @or_with_not_op_commute2(i8 %a, i8 %b) { |
| 262 | ; CHECK-LABEL: @or_with_not_op_commute2( |
Dorit Nuzman | a9fe69c | 2020-06-03 13:11:54 -0400 | [diff] [blame^] | 263 | ; CHECK-NEXT: ret i8 -1 |
Sanjay Patel | a26cd73 | 2020-06-03 08:00:35 -0400 | [diff] [blame] | 264 | ; |
| 265 | %ab = and i8 %b, %a |
| 266 | %not = xor i8 %ab, -1 |
| 267 | %r = or i8 %a, %not |
| 268 | ret i8 %r |
| 269 | } |
| 270 | |
| 271 | ; ~(A & B) | A = -1 |
| 272 | |
| 273 | define <3 x i17> @or_with_not_op_commute3(<3 x i17> %a, <3 x i17> %b) { |
| 274 | ; CHECK-LABEL: @or_with_not_op_commute3( |
| 275 | ; CHECK-NEXT: ret <3 x i17> <i17 -1, i17 -1, i17 -1> |
| 276 | ; |
| 277 | %ab = and <3 x i17> %a, %b |
| 278 | %not = xor <3 x i17> %ab, <i17 -1, i17 -1, i17 -1> |
| 279 | %r = or <3 x i17> %not, %a |
| 280 | ret <3 x i17> %r |
| 281 | } |
| 282 | |
| 283 | ; ~(B & A) | A = -1 |
| 284 | |
| 285 | define <2 x i1> @or_with_not_op_commute4(<2 x i1> %a, <2 x i1> %b) { |
| 286 | ; CHECK-LABEL: @or_with_not_op_commute4( |
| 287 | ; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true> |
| 288 | ; |
| 289 | %ab = and <2 x i1> %b, %a |
| 290 | %not = xor <2 x i1> %ab, <i1 -1, i1 undef> |
| 291 | %r = or <2 x i1> %not, %a |
| 292 | ret <2 x i1> %r |
| 293 | } |