summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSanjay Patel <spatel@rotateright.com>2017-06-20 12:40:55 +0000
committerSanjay Patel <spatel@rotateright.com>2017-06-20 12:40:55 +0000
commitadca825dc14fcdda6425bebcb2f342ea45231ac5 (patch)
tree575c2bb7d1b24d439d49b10b19704221dbbbf4fc
parenta6e2cebf98c5538c4796fedb7354c87cb0dd2c7d (diff)
downloadbcm5719-llvm-adca825dc14fcdda6425bebcb2f342ea45231ac5.tar.gz
bcm5719-llvm-adca825dc14fcdda6425bebcb2f342ea45231ac5.zip
[InstCombine] try to canonicalize xor-of-icmps to and-of-icmps
We have a large portfolio of folds for and-of-icmps and or-of-icmps in InstSimplify and InstCombine, but hardly anything for xor-of-icmps. Rather than trying to rethink and translate all of those folds, we can use the truth table definition of xor: X ^ Y --> (X | Y) & !(X & Y) ...to see if we can convert the xor to and/or and then use the existing folds. http://rise4fun.com/Alive/J9v Differential Revision: https://reviews.llvm.org/D33342 llvm-svn: 305792
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp24
-rw-r--r--llvm/test/Transforms/InstCombine/set.ll18
2 files changed, 31 insertions, 11 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 62898b06ede..3407cbeb404 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -2351,6 +2351,30 @@ Value *InstCombiner::foldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS) {
}
}
+ // Instead of trying to imitate the folds for and/or, decompose this 'xor'
+ // into those logic ops. That is, try to turn this into an and-of-icmps
+ // because we have many folds for that pattern.
+ //
+ // This is based on a truth table definition of xor:
+ // X ^ Y --> (X | Y) & !(X & Y)
+ if (Value *OrICmp = SimplifyBinOp(Instruction::Or, LHS, RHS, SQ)) {
+ // TODO: If OrICmp is true, then the definition of xor simplifies to !(X&Y).
+ // TODO: If OrICmp is false, the whole thing is false (InstSimplify?).
+ if (Value *AndICmp = SimplifyBinOp(Instruction::And, LHS, RHS, SQ)) {
+ // TODO: Independently handle cases where the 'and' side is a constant.
+ if (OrICmp == LHS && AndICmp == RHS && RHS->hasOneUse()) {
+ // (LHS | RHS) & !(LHS & RHS) --> LHS & !RHS
+ RHS->setPredicate(RHS->getInversePredicate());
+ return Builder->CreateAnd(LHS, RHS);
+ }
+ if (OrICmp == RHS && AndICmp == LHS && LHS->hasOneUse()) {
+ // !(LHS & RHS) & (LHS | RHS) --> !LHS & !RHS
+ LHS->setPredicate(LHS->getInversePredicate());
+ return Builder->CreateAnd(LHS, RHS);
+ }
+ }
+ }
+
return nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/set.ll b/llvm/test/Transforms/InstCombine/set.ll
index db2b4c3558e..1452015d2ef 100644
--- a/llvm/test/Transforms/InstCombine/set.ll
+++ b/llvm/test/Transforms/InstCombine/set.ll
@@ -164,10 +164,8 @@ define i1 @bool_eq0(i64 %a) {
define i1 @xor_of_icmps(i64 %a) {
; CHECK-LABEL: @xor_of_icmps(
-; CHECK-NEXT: [[B:%.*]] = icmp sgt i64 %a, 0
-; CHECK-NEXT: [[C:%.*]] = icmp eq i64 %a, 1
-; CHECK-NEXT: [[XOR:%.*]] = xor i1 [[C]], [[B]]
-; CHECK-NEXT: ret i1 [[XOR]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i64 %a, 1
+; CHECK-NEXT: ret i1 [[TMP1]]
;
%b = icmp sgt i64 %a, 0
%c = icmp eq i64 %a, 1
@@ -179,10 +177,8 @@ define i1 @xor_of_icmps(i64 %a) {
define i1 @xor_of_icmps_commute(i64 %a) {
; CHECK-LABEL: @xor_of_icmps_commute(
-; CHECK-NEXT: [[B:%.*]] = icmp sgt i64 %a, 0
-; CHECK-NEXT: [[C:%.*]] = icmp eq i64 %a, 1
-; CHECK-NEXT: [[XOR:%.*]] = xor i1 [[B]], [[C]]
-; CHECK-NEXT: ret i1 [[XOR]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i64 %a, 1
+; CHECK-NEXT: ret i1 [[TMP1]]
;
%b = icmp sgt i64 %a, 0
%c = icmp eq i64 %a, 1
@@ -209,10 +205,10 @@ define i1 @xor_of_icmps_folds_more(i64 %a) {
define i32 @PR2844(i32 %x) {
; CHECK-LABEL: @PR2844(
-; CHECK-NEXT: [[A:%.*]] = icmp eq i32 %x, 0
+; CHECK-NEXT: [[A:%.*]] = icmp ne i32 %x, 0
; CHECK-NEXT: [[B:%.*]] = icmp sgt i32 %x, -638208502
-; CHECK-NEXT: [[NOT_OR:%.*]] = xor i1 [[A]], [[B]]
-; CHECK-NEXT: [[SEL:%.*]] = zext i1 [[NOT_OR]] to i32
+; CHECK-NEXT: [[TMP1:%.*]] = and i1 [[A]], [[B]]
+; CHECK-NEXT: [[SEL:%.*]] = zext i1 [[TMP1]] to i32
; CHECK-NEXT: ret i32 [[SEL]]
;
%A = icmp eq i32 %x, 0
OpenPOWER on IntegriCloud