diff options
| author | Eli Friedman <eli.friedman@gmail.com> | 2011-09-19 21:58:15 +0000 | 
|---|---|---|
| committer | Eli Friedman <eli.friedman@gmail.com> | 2011-09-19 21:58:15 +0000 | 
| commit | 61d7c8a06562aa84b335a62af6fb70b2dae37003 (patch) | |
| tree | fdb98fad3b50cc1dd3ae209ba677551722669231 /llvm/lib/Transforms | |
| parent | ac559323aaa30c5adde9c0a16b001afe9b0de911 (diff) | |
| download | bcm5719-llvm-61d7c8a06562aa84b335a62af6fb70b2dae37003.tar.gz bcm5719-llvm-61d7c8a06562aa84b335a62af6fb70b2dae37003.zip | |
Fix an infinite loop where a transform in InstCombiner::visitAnd claims a construct is changed when it is not.  (See included testcase.)
Patch by Xiaoyi Guo.
llvm-svn: 140072
Diffstat (limited to 'llvm/lib/Transforms')
| -rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp | 43 | 
1 files changed, 22 insertions, 21 deletions
| diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp index 32920fabc3d..d16f8ce821d 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -1174,30 +1174,31 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) {          ((A == C && B == D) || (A == D && B == C)))        return BinaryOperator::CreateXor(A, B); -    if (Op0->hasOneUse() && -        match(Op0, m_Xor(m_Value(A), m_Value(B)))) { -      if (A == Op1) {                                // (A^B)&A -> A&(A^B) -        I.swapOperands();     // Simplify below -        std::swap(Op0, Op1); -      } else if (B == Op1) {                         // (A^B)&B -> B&(B^A) -        cast<BinaryOperator>(Op0)->swapOperands(); -        I.swapOperands();     // Simplify below -        std::swap(Op0, Op1); +    // A&(A^B) => A & ~B +    { +      Value *tmpOp0 = Op0; +      Value *tmpOp1 = Op1; +      if (Op0->hasOneUse() && +          match(Op0, m_Xor(m_Value(A), m_Value(B)))) { +        if (A == Op1 || B == Op1 ) { +          tmpOp1 = Op0; +          tmpOp0 = Op1; +          // Simplify below +        }        } -    } -    if (Op1->hasOneUse() && -        match(Op1, m_Xor(m_Value(A), m_Value(B)))) { -      if (B == Op0) {                                // B&(A^B) -> B&(B^A) -        cast<BinaryOperator>(Op1)->swapOperands(); -        std::swap(A, B); +      if (tmpOp1->hasOneUse() && +          match(tmpOp1, m_Xor(m_Value(A), m_Value(B)))) { +        if (B == tmpOp0) { +          std::swap(A, B); +        } +        // Notice that the patten (A&(~B)) is actually (A&(-1^B)), so if +        // A is originally -1 (or a vector of -1 and undefs), then we enter +        // an endless loop. By checking that A is non-constant we ensure that +        // we will never get to the loop. +        if (A == tmpOp0 && !isa<Constant>(A)) // A&(A^B) -> A & ~B +          return BinaryOperator::CreateAnd(A, Builder->CreateNot(B, "tmp"));        } -      // Notice that the patten (A&(~B)) is actually (A&(-1^B)), so if -      // A is originally -1 (or a vector of -1 and undefs), then we enter -      // an endless loop. By checking that A is non-constant we ensure that -      // we will never get to the loop. -      if (A == Op0 && !isa<Constant>(A)) // A&(A^B) -> A & ~B -        return BinaryOperator::CreateAnd(A, Builder->CreateNot(B, "tmp"));      }      // (A&((~A)|B)) -> A&B | 

