diff options
| -rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp | 55 | 
1 files changed, 28 insertions, 27 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp index e70171ce989..ea64910e445 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -1328,6 +1328,32 @@ Instruction *InstCombiner::foldCastedBitwiseLogic(BinaryOperator &I) {    return nullptr;  } +static Instruction *foldBoolSextMaskToSelect(BinaryOperator &I) { +  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); + +  // Canonicalize SExt or Not to the LHS +  if (match(Op1, m_SExt(m_Value())) || match(Op1, m_Not(m_Value()))) { +    std::swap(Op0, Op1); +  } + +  // Fold (and (sext bool to A), B) --> (select bool, B, 0) +  Value *X = nullptr; +  if (match(Op0, m_SExt(m_Value(X))) && +      X->getType()->getScalarType()->isIntegerTy(1)) { +    Value *Zero = Constant::getNullValue(Op1->getType()); +    return SelectInst::Create(X, Op1, Zero); +  } + +  // Fold (and ~(sext bool to A), B) --> (select bool, 0, B) +  if (match(Op0, m_Not(m_SExt(m_Value(X)))) && +      X->getType()->getScalarType()->isIntegerTy(1)) { +    Value *Zero = Constant::getNullValue(Op0->getType()); +    return SelectInst::Create(X, Zero, Op1); +  } +   +  return nullptr; +} +  Instruction *InstCombiner::visitAnd(BinaryOperator &I) {    bool Changed = SimplifyAssociativeOrCommutative(I);    Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); @@ -1586,33 +1612,8 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) {      }    } -  { -    Value *X = nullptr; -    bool OpsSwapped = false; -    // Canonicalize SExt or Not to the LHS -    if (match(Op1, m_SExt(m_Value())) || -        match(Op1, m_Not(m_Value()))) { -      std::swap(Op0, Op1); -      OpsSwapped = true; -    } - -    // Fold (and (sext bool to A), B) --> (select bool, B, 0) -    if (match(Op0, m_SExt(m_Value(X))) && -        X->getType()->getScalarType()->isIntegerTy(1)) { -      Value *Zero = Constant::getNullValue(Op1->getType()); -      return SelectInst::Create(X, Op1, Zero); -    } - -    // Fold (and ~(sext bool to A), B) --> (select bool, 0, B) -    if (match(Op0, m_Not(m_SExt(m_Value(X)))) && -        X->getType()->getScalarType()->isIntegerTy(1)) { -      Value *Zero = Constant::getNullValue(Op0->getType()); -      return SelectInst::Create(X, Zero, Op1); -    } - -    if (OpsSwapped) -      std::swap(Op0, Op1); -  } +  if (Instruction *Select = foldBoolSextMaskToSelect(I)) +    return Select;    return Changed ? &I : nullptr;  }  | 

