1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -instcombine -S | FileCheck %s
; a & (a ^ b) --> a & ~b
define i32 @and_xor_common_op(i32 %pa, i32 %pb) {
; CHECK-LABEL: @and_xor_common_op(
; CHECK-NEXT: [[A:%.*]] = udiv i32 42, [[PA:%.*]]
; CHECK-NEXT: [[B:%.*]] = udiv i32 43, [[PB:%.*]]
; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[B]], -1
; CHECK-NEXT: [[R:%.*]] = and i32 [[A]], [[TMP1]]
; CHECK-NEXT: ret i32 [[R]]
;
%a = udiv i32 42, %pa ; thwart complexity-based canonicalization
%b = udiv i32 43, %pb ; thwart complexity-based canonicalization
%xor = xor i32 %a, %b
%r = and i32 %a, %xor
ret i32 %r
}
; a & (b ^ a) --> a & ~b
define i32 @and_xor_common_op_commute1(i32 %pa, i32 %pb) {
; CHECK-LABEL: @and_xor_common_op_commute1(
; CHECK-NEXT: [[A:%.*]] = udiv i32 42, [[PA:%.*]]
; CHECK-NEXT: [[B:%.*]] = udiv i32 43, [[PB:%.*]]
; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[B]], -1
; CHECK-NEXT: [[R:%.*]] = and i32 [[A]], [[TMP1]]
; CHECK-NEXT: ret i32 [[R]]
;
%a = udiv i32 42, %pa ; thwart complexity-based canonicalization
%b = udiv i32 43, %pb ; thwart complexity-based canonicalization
%xor = xor i32 %b, %a
%r = and i32 %a, %xor
ret i32 %r
}
; (b ^ a) & a --> a & ~b
define i32 @and_xor_common_op_commute2(i32 %pa, i32 %pb) {
; CHECK-LABEL: @and_xor_common_op_commute2(
; CHECK-NEXT: [[A:%.*]] = udiv i32 42, [[PA:%.*]]
; CHECK-NEXT: [[B:%.*]] = udiv i32 43, [[PB:%.*]]
; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[B]], -1
; CHECK-NEXT: [[R:%.*]] = and i32 [[A]], [[TMP1]]
; CHECK-NEXT: ret i32 [[R]]
;
%a = udiv i32 42, %pa ; thwart complexity-based canonicalization
%b = udiv i32 43, %pb ; thwart complexity-based canonicalization
%xor = xor i32 %b, %a
%r = and i32 %xor, %a
ret i32 %r
}
; (a ^ b) & a --> a & ~b
define <2 x i32> @and_xor_common_op_commute3(<2 x i32> %pa, <2 x i32> %pb) {
; CHECK-LABEL: @and_xor_common_op_commute3(
; CHECK-NEXT: [[A:%.*]] = udiv <2 x i32> <i32 42, i32 43>, [[PA:%.*]]
; CHECK-NEXT: [[B:%.*]] = udiv <2 x i32> <i32 43, i32 42>, [[PB:%.*]]
; CHECK-NEXT: [[TMP1:%.*]] = xor <2 x i32> [[B]], <i32 -1, i32 -1>
; CHECK-NEXT: [[R:%.*]] = and <2 x i32> [[A]], [[TMP1]]
; CHECK-NEXT: ret <2 x i32> [[R]]
;
%a = udiv <2 x i32> <i32 42, i32 43>, %pa ; thwart complexity-based canonicalization
%b = udiv <2 x i32> <i32 43, i32 42>, %pb ; thwart complexity-based canonicalization
%xor = xor <2 x i32> %a, %b
%r = and <2 x i32> %xor, %a
ret <2 x i32> %r
}
; It's ok to match a common constant.
; TODO: The xor should be a 'not' op (-1 constant), but demanded bits shrinks it.
define <4 x i32> @and_xor_common_op_constant(<4 x i32> %A) {
; CHECK-LABEL: @and_xor_common_op_constant(
; CHECK-NEXT: [[TMP1:%.*]] = xor <4 x i32> [[A:%.*]], <i32 7, i32 7, i32 7, i32 7>
; CHECK-NEXT: [[TMP2:%.*]] = and <4 x i32> [[TMP1]], <i32 1, i32 2, i32 3, i32 4>
; CHECK-NEXT: ret <4 x i32> [[TMP2]]
;
%1 = xor <4 x i32> %A, <i32 1, i32 2, i32 3, i32 4>
%2 = and <4 x i32> <i32 1, i32 2, i32 3, i32 4>, %1
ret <4 x i32> %2
}
; a & (a ^ ~b) --> a & b
define i32 @and_xor_not_common_op(i32 %a, i32 %b) {
; CHECK-LABEL: @and_xor_not_common_op(
; CHECK-NEXT: [[T4:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: ret i32 [[T4]]
;
%b2 = xor i32 %b, -1
%t2 = xor i32 %a, %b2
%t4 = and i32 %t2, %a
ret i32 %t4
}
; rdar://10770603
; (x & y) | (x ^ y) -> x | y
define i64 @or(i64 %x, i64 %y) {
; CHECK-LABEL: @or(
; CHECK-NEXT: [[TMP1:%.*]] = or i64 [[Y:%.*]], [[X:%.*]]
; CHECK-NEXT: ret i64 [[TMP1]]
;
%1 = and i64 %y, %x
%2 = xor i64 %y, %x
%3 = add i64 %1, %2
ret i64 %3
}
; (x & y) + (x ^ y) -> x | y
define i64 @or2(i64 %x, i64 %y) {
; CHECK-LABEL: @or2(
; CHECK-NEXT: [[TMP1:%.*]] = or i64 [[Y:%.*]], [[X:%.*]]
; CHECK-NEXT: ret i64 [[TMP1]]
;
%1 = and i64 %y, %x
%2 = xor i64 %y, %x
%3 = or i64 %1, %2
ret i64 %3
}
; PR37098 - https://bugs.llvm.org/show_bug.cgi?id=37098
; Reassociate bitwise logic to eliminate a shift.
; There are 4 commuted * 3 shift ops * 3 logic ops = 36 potential variations of this fold.
; Mix the commutation options to provide coverage using less tests.
define i8 @and_shl(i8 %x, i8 %y, i8 %z, i8 %shamt) {
; CHECK-LABEL: @and_shl(
; CHECK-NEXT: [[SX:%.*]] = shl i8 [[X:%.*]], [[SHAMT:%.*]]
; CHECK-NEXT: [[SY:%.*]] = shl i8 [[Y:%.*]], [[SHAMT]]
; CHECK-NEXT: [[A:%.*]] = and i8 [[SX]], [[Z:%.*]]
; CHECK-NEXT: [[R:%.*]] = and i8 [[SY]], [[A]]
; CHECK-NEXT: ret i8 [[R]]
;
%sx = shl i8 %x, %shamt
%sy = shl i8 %y, %shamt
%a = and i8 %sx, %z
%r = and i8 %sy, %a
ret i8 %r
}
define i8 @or_shl(i8 %x, i8 %y, i8 %z, i8 %shamt) {
; CHECK-LABEL: @or_shl(
; CHECK-NEXT: [[SX:%.*]] = shl i8 [[X:%.*]], [[SHAMT:%.*]]
; CHECK-NEXT: [[SY:%.*]] = shl i8 [[Y:%.*]], [[SHAMT]]
; CHECK-NEXT: [[A:%.*]] = or i8 [[SX]], [[Z:%.*]]
; CHECK-NEXT: [[R:%.*]] = or i8 [[A]], [[SY]]
; CHECK-NEXT: ret i8 [[R]]
;
%sx = shl i8 %x, %shamt
%sy = shl i8 %y, %shamt
%a = or i8 %sx, %z
%r = or i8 %a, %sy
ret i8 %r
}
define i8 @xor_shl(i8 %x, i8 %y, i8 %zarg, i8 %shamt) {
; CHECK-LABEL: @xor_shl(
; CHECK-NEXT: [[Z:%.*]] = sdiv i8 42, [[ZARG:%.*]]
; CHECK-NEXT: [[SX:%.*]] = shl i8 [[X:%.*]], [[SHAMT:%.*]]
; CHECK-NEXT: [[SY:%.*]] = shl i8 [[Y:%.*]], [[SHAMT]]
; CHECK-NEXT: [[A:%.*]] = xor i8 [[Z]], [[SX]]
; CHECK-NEXT: [[R:%.*]] = xor i8 [[A]], [[SY]]
; CHECK-NEXT: ret i8 [[R]]
;
%z = sdiv i8 42, %zarg ; thwart complexity-based canonicalization
%sx = shl i8 %x, %shamt
%sy = shl i8 %y, %shamt
%a = xor i8 %z, %sx
%r = xor i8 %a, %sy
ret i8 %r
}
define i8 @and_lshr(i8 %x, i8 %y, i8 %zarg, i8 %shamt) {
; CHECK-LABEL: @and_lshr(
; CHECK-NEXT: [[Z:%.*]] = sdiv i8 42, [[ZARG:%.*]]
; CHECK-NEXT: [[SX:%.*]] = lshr i8 [[X:%.*]], [[SHAMT:%.*]]
; CHECK-NEXT: [[SY:%.*]] = lshr i8 [[Y:%.*]], [[SHAMT]]
; CHECK-NEXT: [[A:%.*]] = and i8 [[Z]], [[SX]]
; CHECK-NEXT: [[R:%.*]] = and i8 [[SY]], [[A]]
; CHECK-NEXT: ret i8 [[R]]
;
%z = sdiv i8 42, %zarg ; thwart complexity-based canonicalization
%sx = lshr i8 %x, %shamt
%sy = lshr i8 %y, %shamt
%a = and i8 %z, %sx
%r = and i8 %sy, %a
ret i8 %r
}
define i8 @or_lshr(i8 %x, i8 %y, i8 %z, i8 %shamt) {
; CHECK-LABEL: @or_lshr(
; CHECK-NEXT: [[SX:%.*]] = lshr i8 [[X:%.*]], [[SHAMT:%.*]]
; CHECK-NEXT: [[SY:%.*]] = lshr i8 [[Y:%.*]], [[SHAMT]]
; CHECK-NEXT: [[A:%.*]] = or i8 [[SX]], [[Z:%.*]]
; CHECK-NEXT: [[R:%.*]] = or i8 [[SY]], [[A]]
; CHECK-NEXT: ret i8 [[R]]
;
%sx = lshr i8 %x, %shamt
%sy = lshr i8 %y, %shamt
%a = or i8 %sx, %z
%r = or i8 %sy, %a
ret i8 %r
}
define i8 @xor_lshr(i8 %x, i8 %y, i8 %z, i8 %shamt) {
; CHECK-LABEL: @xor_lshr(
; CHECK-NEXT: [[SX:%.*]] = lshr i8 [[X:%.*]], [[SHAMT:%.*]]
; CHECK-NEXT: [[SY:%.*]] = lshr i8 [[Y:%.*]], [[SHAMT]]
; CHECK-NEXT: [[A:%.*]] = xor i8 [[SX]], [[Z:%.*]]
; CHECK-NEXT: [[R:%.*]] = xor i8 [[A]], [[SY]]
; CHECK-NEXT: ret i8 [[R]]
;
%sx = lshr i8 %x, %shamt
%sy = lshr i8 %y, %shamt
%a = xor i8 %sx, %z
%r = xor i8 %a, %sy
ret i8 %r
}
define i8 @and_ashr(i8 %x, i8 %y, i8 %zarg, i8 %shamt) {
; CHECK-LABEL: @and_ashr(
; CHECK-NEXT: [[Z:%.*]] = sdiv i8 42, [[ZARG:%.*]]
; CHECK-NEXT: [[SX:%.*]] = ashr i8 [[X:%.*]], [[SHAMT:%.*]]
; CHECK-NEXT: [[SY:%.*]] = ashr i8 [[Y:%.*]], [[SHAMT]]
; CHECK-NEXT: [[A:%.*]] = and i8 [[Z]], [[SX]]
; CHECK-NEXT: [[R:%.*]] = and i8 [[A]], [[SY]]
; CHECK-NEXT: ret i8 [[R]]
;
%z = sdiv i8 42, %zarg ; thwart complexity-based canonicalization
%sx = ashr i8 %x, %shamt
%sy = ashr i8 %y, %shamt
%a = and i8 %z, %sx
%r = and i8 %a, %sy
ret i8 %r
}
define i8 @or_ashr(i8 %x, i8 %y, i8 %zarg, i8 %shamt) {
; CHECK-LABEL: @or_ashr(
; CHECK-NEXT: [[Z:%.*]] = sdiv i8 42, [[ZARG:%.*]]
; CHECK-NEXT: [[SX:%.*]] = ashr i8 [[X:%.*]], [[SHAMT:%.*]]
; CHECK-NEXT: [[SY:%.*]] = ashr i8 [[Y:%.*]], [[SHAMT]]
; CHECK-NEXT: [[A:%.*]] = or i8 [[Z]], [[SX]]
; CHECK-NEXT: [[R:%.*]] = or i8 [[SY]], [[A]]
; CHECK-NEXT: ret i8 [[R]]
;
%z = sdiv i8 42, %zarg ; thwart complexity-based canonicalization
%sx = ashr i8 %x, %shamt
%sy = ashr i8 %y, %shamt
%a = or i8 %z, %sx
%r = or i8 %sy, %a
ret i8 %r
}
define <2 x i8> @xor_ashr(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z, <2 x i8> %shamt) {
; CHECK-LABEL: @xor_ashr(
; CHECK-NEXT: [[SX:%.*]] = ashr <2 x i8> [[X:%.*]], [[SHAMT:%.*]]
; CHECK-NEXT: [[SY:%.*]] = ashr <2 x i8> [[Y:%.*]], [[SHAMT]]
; CHECK-NEXT: [[A:%.*]] = xor <2 x i8> [[SX]], [[Z:%.*]]
; CHECK-NEXT: [[R:%.*]] = xor <2 x i8> [[A]], [[SY]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%sx = ashr <2 x i8> %x, %shamt
%sy = ashr <2 x i8> %y, %shamt
%a = xor <2 x i8> %sx, %z
%r = xor <2 x i8> %a, %sy
ret <2 x i8> %r
}
; Negative test - different logic ops
define i8 @or_and_shl(i8 %x, i8 %y, i8 %z, i8 %shamt) {
; CHECK-LABEL: @or_and_shl(
; CHECK-NEXT: [[SX:%.*]] = shl i8 [[X:%.*]], [[SHAMT:%.*]]
; CHECK-NEXT: [[SY:%.*]] = shl i8 [[Y:%.*]], [[SHAMT]]
; CHECK-NEXT: [[A:%.*]] = or i8 [[SX]], [[Z:%.*]]
; CHECK-NEXT: [[R:%.*]] = and i8 [[SY]], [[A]]
; CHECK-NEXT: ret i8 [[R]]
;
%sx = shl i8 %x, %shamt
%sy = shl i8 %y, %shamt
%a = or i8 %sx, %z
%r = and i8 %sy, %a
ret i8 %r
}
; Negative test - different shift ops
define i8 @or_lshr_shl(i8 %x, i8 %y, i8 %z, i8 %shamt) {
; CHECK-LABEL: @or_lshr_shl(
; CHECK-NEXT: [[SX:%.*]] = lshr i8 [[X:%.*]], [[SHAMT:%.*]]
; CHECK-NEXT: [[SY:%.*]] = shl i8 [[Y:%.*]], [[SHAMT]]
; CHECK-NEXT: [[A:%.*]] = or i8 [[SX]], [[Z:%.*]]
; CHECK-NEXT: [[R:%.*]] = or i8 [[A]], [[SY]]
; CHECK-NEXT: ret i8 [[R]]
;
%sx = lshr i8 %x, %shamt
%sy = shl i8 %y, %shamt
%a = or i8 %sx, %z
%r = or i8 %a, %sy
ret i8 %r
}
; Negative test - different shift amounts
define i8 @or_lshr_shamt2(i8 %x, i8 %y, i8 %z, i8 %shamt) {
; CHECK-LABEL: @or_lshr_shamt2(
; CHECK-NEXT: [[SX:%.*]] = lshr i8 [[X:%.*]], 5
; CHECK-NEXT: [[SY:%.*]] = lshr i8 [[Y:%.*]], [[SHAMT:%.*]]
; CHECK-NEXT: [[A:%.*]] = or i8 [[SX]], [[Z:%.*]]
; CHECK-NEXT: [[R:%.*]] = or i8 [[SY]], [[A]]
; CHECK-NEXT: ret i8 [[R]]
;
%sx = lshr i8 %x, 5
%sy = lshr i8 %y, %shamt
%a = or i8 %sx, %z
%r = or i8 %sy, %a
ret i8 %r
}
; Negative test - multi-use
define i8 @xor_lshr_multiuse(i8 %x, i8 %y, i8 %z, i8 %shamt) {
; CHECK-LABEL: @xor_lshr_multiuse(
; CHECK-NEXT: [[SX:%.*]] = lshr i8 [[X:%.*]], [[SHAMT:%.*]]
; CHECK-NEXT: [[SY:%.*]] = lshr i8 [[Y:%.*]], [[SHAMT]]
; CHECK-NEXT: [[A:%.*]] = xor i8 [[SX]], [[Z:%.*]]
; CHECK-NEXT: [[R:%.*]] = xor i8 [[A]], [[SY]]
; CHECK-NEXT: [[R2:%.*]] = sdiv i8 [[A]], [[R]]
; CHECK-NEXT: ret i8 [[R2]]
;
%sx = lshr i8 %x, %shamt
%sy = lshr i8 %y, %shamt
%a = xor i8 %sx, %z
%r = xor i8 %a, %sy
%r2 = sdiv i8 %a, %r
ret i8 %r2
}
|