2016-08-10 00:17:46 +08:00
|
|
|
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
|
2009-10-12 05:42:08 +08:00
|
|
|
; RUN: opt < %s -instcombine -S | FileCheck %s
|
2007-04-03 09:45:32 +08:00
|
|
|
|
|
|
|
; PR1253
|
|
|
|
define i1 @test0(i32 %A) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @test0(
|
2016-08-10 00:17:46 +08:00
|
|
|
; CHECK-NEXT: [[C:%.*]] = icmp slt i32 %A, 0
|
|
|
|
; CHECK-NEXT: ret i1 [[C]]
|
|
|
|
;
|
|
|
|
%B = xor i32 %A, -2147483648
|
|
|
|
%C = icmp sgt i32 %B, -1
|
|
|
|
ret i1 %C
|
2007-04-03 09:45:32 +08:00
|
|
|
}
|
|
|
|
|
2016-08-10 00:39:05 +08:00
|
|
|
define <2 x i1> @test0vec(<2 x i32> %A) {
|
|
|
|
; CHECK-LABEL: @test0vec(
|
2016-08-18 22:10:48 +08:00
|
|
|
; CHECK-NEXT: [[C:%.*]] = icmp slt <2 x i32> %A, zeroinitializer
|
2016-08-10 00:39:05 +08:00
|
|
|
; CHECK-NEXT: ret <2 x i1> [[C]]
|
|
|
|
;
|
|
|
|
%B = xor <2 x i32> %A, <i32 -2147483648, i32 -2147483648>
|
|
|
|
%C = icmp sgt <2 x i32> %B, <i32 -1, i32 -1>
|
|
|
|
ret <2 x i1> %C
|
|
|
|
}
|
|
|
|
|
2007-04-03 09:45:32 +08:00
|
|
|
define i1 @test1(i32 %A) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @test1(
|
2016-08-10 00:17:46 +08:00
|
|
|
; CHECK-NEXT: [[C:%.*]] = icmp slt i32 %A, 0
|
|
|
|
; CHECK-NEXT: ret i1 [[C]]
|
|
|
|
;
|
|
|
|
%B = xor i32 %A, 12345
|
|
|
|
%C = icmp slt i32 %B, 0
|
|
|
|
ret i1 %C
|
2007-04-03 09:45:32 +08:00
|
|
|
}
|
|
|
|
|
2009-10-12 05:42:08 +08:00
|
|
|
; PR1014
|
|
|
|
define i32 @test2(i32 %tmp1) {
|
2016-08-10 00:17:46 +08:00
|
|
|
; CHECK-LABEL: @test2(
|
|
|
|
; CHECK-NEXT: [[OVM:%.*]] = and i32 %tmp1, 32
|
|
|
|
; CHECK-NEXT: [[OV1101:%.*]] = or i32 [[OVM]], 8
|
|
|
|
; CHECK-NEXT: ret i32 [[OV1101]]
|
|
|
|
;
|
|
|
|
%ovm = and i32 %tmp1, 32
|
|
|
|
%ov3 = add i32 %ovm, 145
|
|
|
|
%ov110 = xor i32 %ov3, 153
|
|
|
|
ret i32 %ov110
|
2009-10-12 05:42:08 +08:00
|
|
|
}
|
2009-10-12 06:22:13 +08:00
|
|
|
|
|
|
|
define i32 @test3(i32 %tmp1) {
|
2016-08-10 00:17:46 +08:00
|
|
|
; CHECK-LABEL: @test3(
|
|
|
|
; CHECK-NEXT: [[OVM:%.*]] = and i32 %tmp1, 32
|
|
|
|
; CHECK-NEXT: [[OV1101:%.*]] = or i32 [[OVM]], 8
|
|
|
|
; CHECK-NEXT: ret i32 [[OV1101]]
|
|
|
|
;
|
|
|
|
%ovm = or i32 %tmp1, 145
|
2009-10-12 06:22:13 +08:00
|
|
|
%ov31 = and i32 %ovm, 177
|
|
|
|
%ov110 = xor i32 %ov31, 153
|
|
|
|
ret i32 %ov110
|
|
|
|
}
|
2010-01-20 02:16:19 +08:00
|
|
|
|
|
|
|
define i32 @test4(i32 %A, i32 %B) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @test4(
|
2016-08-10 00:17:46 +08:00
|
|
|
; CHECK-NEXT: [[TMP1:%.*]] = ashr i32 %A, %B
|
|
|
|
; CHECK-NEXT: ret i32 [[TMP1]]
|
|
|
|
;
|
|
|
|
%1 = xor i32 %A, -1
|
|
|
|
%2 = ashr i32 %1, %B
|
|
|
|
%3 = xor i32 %2, -1
|
|
|
|
ret i32 %3
|
2010-01-20 02:16:19 +08:00
|
|
|
}
|
2012-11-27 05:44:25 +08:00
|
|
|
|
|
|
|
; defect-2 in rdar://12329730
|
|
|
|
; (X^C1) >> C2) ^ C3 -> (X>>C2) ^ ((C1>>C2)^C3)
|
|
|
|
; where the "X" has more than one use
|
|
|
|
define i32 @test5(i32 %val1) {
|
2016-08-10 00:17:46 +08:00
|
|
|
; CHECK-LABEL: @test5(
|
|
|
|
; CHECK-NEXT: [[XOR:%.*]] = xor i32 %val1, 1234
|
|
|
|
; CHECK-NEXT: [[SHR:%.*]] = lshr i32 %val1, 8
|
|
|
|
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[SHR]], 5
|
|
|
|
; CHECK-NEXT: [[ADD:%.*]] = add i32 [[XOR1]], [[XOR]]
|
|
|
|
; CHECK-NEXT: ret i32 [[ADD]]
|
|
|
|
;
|
2012-11-27 05:44:25 +08:00
|
|
|
%xor = xor i32 %val1, 1234
|
|
|
|
%shr = lshr i32 %xor, 8
|
|
|
|
%xor1 = xor i32 %shr, 1
|
|
|
|
%add = add i32 %xor1, %xor
|
|
|
|
ret i32 %add
|
|
|
|
}
|
2012-12-05 06:15:32 +08:00
|
|
|
|
|
|
|
; defect-1 in rdar://12329730
|
2016-08-10 00:17:46 +08:00
|
|
|
; Simplify (X^Y) -> X or Y in the user's context if we know that
|
2012-12-05 06:15:32 +08:00
|
|
|
; only bits from X or Y are demanded.
|
|
|
|
; e.g. the "x ^ 1234" can be optimized into x in the context of "t >> 16".
|
|
|
|
; Put in other word, t >> 16 -> x >> 16.
|
2012-12-05 08:33:16 +08:00
|
|
|
; unsigned foo(unsigned x) { unsigned t = x ^ 1234; ; return (t >> 16) + t;}
|
2012-12-05 06:15:32 +08:00
|
|
|
define i32 @test6(i32 %x) {
|
2016-08-10 00:17:46 +08:00
|
|
|
; CHECK-LABEL: @test6(
|
|
|
|
; CHECK-NEXT: [[XOR:%.*]] = xor i32 %x, 1234
|
|
|
|
; CHECK-NEXT: [[SHR:%.*]] = lshr i32 %x, 16
|
|
|
|
; CHECK-NEXT: [[ADD:%.*]] = add i32 [[SHR]], [[XOR]]
|
|
|
|
; CHECK-NEXT: ret i32 [[ADD]]
|
|
|
|
;
|
2012-12-05 06:15:32 +08:00
|
|
|
%xor = xor i32 %x, 1234
|
|
|
|
%shr = lshr i32 %xor, 16
|
|
|
|
%add = add i32 %shr, %xor
|
|
|
|
ret i32 %add
|
|
|
|
}
|
2014-07-22 23:37:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
; (A | B) ^ (~A) -> (A | ~B)
|
2014-09-05 14:09:24 +08:00
|
|
|
define i32 @test7(i32 %a, i32 %b) {
|
2014-07-22 23:37:39 +08:00
|
|
|
; CHECK-LABEL: @test7(
|
2016-08-10 00:17:46 +08:00
|
|
|
; CHECK-NEXT: [[B_NOT:%.*]] = xor i32 %b, -1
|
|
|
|
; CHECK-NEXT: [[XOR:%.*]] = or i32 %a, [[B_NOT]]
|
|
|
|
; CHECK-NEXT: ret i32 [[XOR]]
|
|
|
|
;
|
|
|
|
%or = or i32 %a, %b
|
|
|
|
%neg = xor i32 %a, -1
|
|
|
|
%xor = xor i32 %or, %neg
|
|
|
|
ret i32 %xor
|
2014-07-22 23:37:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
; (~A) ^ (A | B) -> (A | ~B)
|
2014-09-05 14:09:24 +08:00
|
|
|
define i32 @test8(i32 %a, i32 %b) {
|
2014-07-22 23:37:39 +08:00
|
|
|
; CHECK-LABEL: @test8(
|
2016-08-10 00:17:46 +08:00
|
|
|
; CHECK-NEXT: [[B_NOT:%.*]] = xor i32 %b, -1
|
|
|
|
; CHECK-NEXT: [[XOR:%.*]] = or i32 %a, [[B_NOT]]
|
|
|
|
; CHECK-NEXT: ret i32 [[XOR]]
|
|
|
|
;
|
|
|
|
%neg = xor i32 %a, -1
|
|
|
|
%or = or i32 %a, %b
|
|
|
|
%xor = xor i32 %neg, %or
|
|
|
|
ret i32 %xor
|
2014-07-22 23:37:39 +08:00
|
|
|
}
|
2014-07-23 02:30:54 +08:00
|
|
|
|
|
|
|
; (A & B) ^ (A ^ B) -> (A | B)
|
|
|
|
define i32 @test9(i32 %b, i32 %c) {
|
|
|
|
; CHECK-LABEL: @test9(
|
2016-08-10 00:17:46 +08:00
|
|
|
; CHECK-NEXT: [[XOR2:%.*]] = or i32 %b, %c
|
|
|
|
; CHECK-NEXT: ret i32 [[XOR2]]
|
|
|
|
;
|
|
|
|
%and = and i32 %b, %c
|
|
|
|
%xor = xor i32 %b, %c
|
|
|
|
%xor2 = xor i32 %and, %xor
|
|
|
|
ret i32 %xor2
|
2014-07-23 02:30:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
; (A ^ B) ^ (A & B) -> (A | B)
|
|
|
|
define i32 @test10(i32 %b, i32 %c) {
|
|
|
|
; CHECK-LABEL: @test10(
|
2016-08-10 00:17:46 +08:00
|
|
|
; CHECK-NEXT: [[XOR2:%.*]] = or i32 %b, %c
|
|
|
|
; CHECK-NEXT: ret i32 [[XOR2]]
|
|
|
|
;
|
|
|
|
%xor = xor i32 %b, %c
|
|
|
|
%and = and i32 %b, %c
|
|
|
|
%xor2 = xor i32 %xor, %and
|
|
|
|
ret i32 %xor2
|
2014-07-23 02:30:54 +08:00
|
|
|
}
|
InstCombine: Simplify (A ^ B) or/and (A ^ B ^ C)
While we can already transform A | (A ^ B) into A | B, things get bad
once we have (A ^ B) | (A ^ B ^ Cst) because reassociation will morph
this into (A ^ B) | ((A ^ Cst) ^ B). Our existing patterns fail once
this happens.
To fix this, we add a new pattern which looks through the tree of xor
binary operators to see that, in fact, there exists a redundant xor
operation.
What follows bellow is a correctness proof of the transform using CVC3.
$ cat t.cvc
A, B, C : BITVECTOR(64);
QUERY BVXOR(A, B) | BVXOR(BVXOR(B, C), A) = BVXOR(A, B) | C;
QUERY BVXOR(BVXOR(A, C), B) | BVXOR(A, B) = BVXOR(A, B) | C;
QUERY BVXOR(A, B) & BVXOR(BVXOR(B, C), A) = BVXOR(A, B) & ~C;
QUERY BVXOR(BVXOR(A, C), B) & BVXOR(A, B) = BVXOR(A, B) & ~C;
$ cvc3 < t.cvc
Valid.
Valid.
Valid.
Valid.
llvm-svn: 214342
2014-07-31 05:26:37 +08:00
|
|
|
|
|
|
|
define i32 @test11(i32 %A, i32 %B) {
|
2016-08-10 00:17:46 +08:00
|
|
|
; CHECK-LABEL: @test11(
|
|
|
|
; CHECK-NEXT: ret i32 0
|
|
|
|
;
|
InstCombine: Simplify (A ^ B) or/and (A ^ B ^ C)
While we can already transform A | (A ^ B) into A | B, things get bad
once we have (A ^ B) | (A ^ B ^ Cst) because reassociation will morph
this into (A ^ B) | ((A ^ Cst) ^ B). Our existing patterns fail once
this happens.
To fix this, we add a new pattern which looks through the tree of xor
binary operators to see that, in fact, there exists a redundant xor
operation.
What follows bellow is a correctness proof of the transform using CVC3.
$ cat t.cvc
A, B, C : BITVECTOR(64);
QUERY BVXOR(A, B) | BVXOR(BVXOR(B, C), A) = BVXOR(A, B) | C;
QUERY BVXOR(BVXOR(A, C), B) | BVXOR(A, B) = BVXOR(A, B) | C;
QUERY BVXOR(A, B) & BVXOR(BVXOR(B, C), A) = BVXOR(A, B) & ~C;
QUERY BVXOR(BVXOR(A, C), B) & BVXOR(A, B) = BVXOR(A, B) & ~C;
$ cvc3 < t.cvc
Valid.
Valid.
Valid.
Valid.
llvm-svn: 214342
2014-07-31 05:26:37 +08:00
|
|
|
%xor1 = xor i32 %B, %A
|
|
|
|
%not = xor i32 %A, -1
|
|
|
|
%xor2 = xor i32 %not, %B
|
|
|
|
%and = and i32 %xor1, %xor2
|
|
|
|
ret i32 %and
|
|
|
|
}
|
|
|
|
|
2014-09-16 16:50:10 +08:00
|
|
|
define i32 @test12(i32 %a, i32 %b) {
|
|
|
|
; CHECK-LABEL: @test12(
|
2016-08-10 00:17:46 +08:00
|
|
|
; CHECK-NEXT: [[TMP1:%.*]] = and i32 %a, %b
|
|
|
|
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[TMP1]], -1
|
|
|
|
; CHECK-NEXT: ret i32 [[XOR]]
|
|
|
|
;
|
|
|
|
%negb = xor i32 %b, -1
|
|
|
|
%and = and i32 %a, %negb
|
|
|
|
%nega = xor i32 %a, -1
|
|
|
|
%xor = xor i32 %and, %nega
|
|
|
|
ret i32 %xor
|
2014-08-01 13:07:20 +08:00
|
|
|
}
|
|
|
|
|
2016-09-10 00:35:20 +08:00
|
|
|
; FIXME: We miss the fold because the pattern matching is inadequate.
|
|
|
|
|
|
|
|
define i32 @test12commuted(i32 %a, i32 %b) {
|
|
|
|
; CHECK-LABEL: @test12commuted(
|
|
|
|
; CHECK-NEXT: [[NEGB:%.*]] = xor i32 %b, -1
|
|
|
|
; CHECK-NEXT: [[AND:%.*]] = and i32 [[NEGB]], %a
|
|
|
|
; CHECK-NEXT: [[NEGA:%.*]] = xor i32 %a, -1
|
|
|
|
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[AND]], [[NEGA]]
|
|
|
|
; CHECK-NEXT: ret i32 [[XOR]]
|
|
|
|
;
|
|
|
|
%negb = xor i32 %b, -1
|
|
|
|
%and = and i32 %negb, %a
|
|
|
|
%nega = xor i32 %a, -1
|
|
|
|
%xor = xor i32 %and, %nega
|
|
|
|
ret i32 %xor
|
|
|
|
}
|
|
|
|
|
|
|
|
; This is a test of canonicalization via operand complexity.
|
|
|
|
; The final xor has a binary operator and a (fake) unary operator,
|
|
|
|
; so binary (more complex) should come first.
|
|
|
|
|
2014-09-16 16:50:10 +08:00
|
|
|
define i32 @test13(i32 %a, i32 %b) {
|
|
|
|
; CHECK-LABEL: @test13(
|
2016-08-10 00:17:46 +08:00
|
|
|
; CHECK-NEXT: [[TMP1:%.*]] = and i32 %a, %b
|
|
|
|
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[TMP1]], -1
|
|
|
|
; CHECK-NEXT: ret i32 [[XOR]]
|
|
|
|
;
|
|
|
|
%nega = xor i32 %a, -1
|
|
|
|
%negb = xor i32 %b, -1
|
|
|
|
%and = and i32 %a, %negb
|
|
|
|
%xor = xor i32 %nega, %and
|
|
|
|
ret i32 %xor
|
2014-08-01 13:07:20 +08:00
|
|
|
}
|
2014-09-05 14:09:24 +08:00
|
|
|
|
2016-09-10 00:35:20 +08:00
|
|
|
; FIXME: We miss the fold because the pattern matching is inadequate.
|
|
|
|
|
|
|
|
define i32 @test13commuted(i32 %a, i32 %b) {
|
|
|
|
; CHECK-LABEL: @test13commuted(
|
|
|
|
; CHECK-NEXT: [[NEGA:%.*]] = xor i32 %a, -1
|
|
|
|
; CHECK-NEXT: [[NEGB:%.*]] = xor i32 %b, -1
|
|
|
|
; CHECK-NEXT: [[AND:%.*]] = and i32 [[NEGB]], %a
|
|
|
|
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[AND]], [[NEGA]]
|
|
|
|
; CHECK-NEXT: ret i32 [[XOR]]
|
|
|
|
;
|
|
|
|
%nega = xor i32 %a, -1
|
|
|
|
%negb = xor i32 %b, -1
|
|
|
|
%and = and i32 %negb, %a
|
|
|
|
%xor = xor i32 %nega, %and
|
|
|
|
ret i32 %xor
|
|
|
|
}
|
|
|
|
|
2014-09-05 14:09:24 +08:00
|
|
|
; (A ^ C) ^ (A | B) -> ((~A) & B) ^ C
|
2014-09-16 16:50:10 +08:00
|
|
|
define i32 @test14(i32 %a, i32 %b, i32 %c) {
|
|
|
|
; CHECK-LABEL: @test14(
|
2016-08-10 00:17:46 +08:00
|
|
|
; CHECK-NEXT: [[TMP1:%.*]] = xor i32 %a, -1
|
|
|
|
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], %b
|
|
|
|
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[TMP2]], %c
|
|
|
|
; CHECK-NEXT: ret i32 [[XOR]]
|
|
|
|
;
|
|
|
|
%neg = xor i32 %a, %c
|
|
|
|
%or = or i32 %a, %b
|
|
|
|
%xor = xor i32 %neg, %or
|
|
|
|
ret i32 %xor
|
2014-09-05 14:09:24 +08:00
|
|
|
}
|
2016-08-10 00:17:46 +08:00
|
|
|
|