[llvm-commits] [llvm] r61537 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/xor-demorgans.ll
Bill Wendling
isanbard at gmail.com
Wed Dec 31 17:18:23 PST 2008
Author: void
Date: Wed Dec 31 19:18:23 2008
New Revision: 61537
URL: http://llvm.org/viewvc/llvm-project?rev=61537&view=rev
Log:
Add transformation:
xor (or (icmp, icmp), true) -> and(icmp, icmp)
This is possible because of De Morgan's law.
Added:
llvm/trunk/test/Transforms/InstCombine/xor-demorgans.ll
Modified:
llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=61537&r1=61536&r2=61537&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Wed Dec 31 19:18:23 2008
@@ -4871,8 +4871,8 @@
if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
- // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B
if (RHS == ConstantInt::getTrue() && Op0->hasOneUse()) {
+ // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B
if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0))
return new ICmpInst(ICI->getInversePredicate(),
ICI->getOperand(0), ICI->getOperand(1));
@@ -4880,6 +4880,37 @@
if (FCmpInst *FCI = dyn_cast<FCmpInst>(Op0))
return new FCmpInst(FCI->getInversePredicate(),
FCI->getOperand(0), FCI->getOperand(1));
+
+ // xor (or (cmp x,m),(cmp y,n)),true --> and (!cmp x,m),(!cmp y,n)
+ //
+ // Proof:
+ // Let A = (cmp x,m)
+ // Let B = (cmp y,n)
+ // Let C = (or A, B)
+ // C true implies that either A, B, or both are true.
+ //
+ // (xor C, true) is true only if C is false. We can the apply de
+ // Morgan's law. QED.
+ BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0);
+ if (Op0I) {
+ Value *A, *B;
+ if (match(Op0I, m_Or(m_Value(A), m_Value(B)))) {
+ ICmpInst *AOp = dyn_cast<ICmpInst>(A);
+ ICmpInst *BOp = dyn_cast<ICmpInst>(B);
+
+ if (AOp && BOp) {
+ ICmpInst *NewA = new ICmpInst(AOp->getInversePredicate(),
+ AOp->getOperand(0),
+ AOp->getOperand(1));
+ InsertNewInstBefore(NewA, I);
+ ICmpInst *NewB = new ICmpInst(BOp->getInversePredicate(),
+ BOp->getOperand(0),
+ BOp->getOperand(1));
+ InsertNewInstBefore(NewB, I);
+ return BinaryOperator::CreateAnd(NewA, NewB);
+ }
+ }
+ }
}
// fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp).
Added: llvm/trunk/test/Transforms/InstCombine/xor-demorgans.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/xor-demorgans.ll?rev=61537&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/xor-demorgans.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/xor-demorgans.ll Wed Dec 31 19:18:23 2008
@@ -0,0 +1,11 @@
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | not grep {= or}
+; PR3266
+
+define i1 @foo(i32 %x, i32 %y) nounwind {
+.summary:
+ %0 = icmp sgt i32 %x, 4 ; <i1> [#uses=1]
+ %1 = icmp sgt i32 %y, 0 ; <i1> [#uses=1]
+ %.demorgan = or i1 %1, %0 ; <i1> [#uses=1]
+ %2 = xor i1 %.demorgan, true ; <i1> [#uses=1]
+ ret i1 %2
+}
More information about the llvm-commits
mailing list