diff options
author | Sanjay Patel <spatel@rotateright.com> | 2018-10-16 15:26:08 +0000 |
---|---|---|
committer | Sanjay Patel <spatel@rotateright.com> | 2018-10-16 15:26:08 +0000 |
commit | bb3dd34e62d2b53402a84d9d8b2a3c865fdf43fd (patch) | |
tree | c33540c41aa41bdabc6005a25cddfe2b33a1608b /llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp | |
parent | 29270717502afeebd584f393f4182f26ae5f4536 (diff) | |
download | bcm5719-llvm-bb3dd34e62d2b53402a84d9d8b2a3c865fdf43fd.tar.gz bcm5719-llvm-bb3dd34e62d2b53402a84d9d8b2a3c865fdf43fd.zip |
revert rL344609: [InstCombine] try harder to form select from logic ops
I noticed a missing check and added it at rL344610, but there actually
are codegen tests that will fail without that, so I'll edit those and
submit a fixed patch with more tests.
llvm-svn: 344612
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp')
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp | 67 |
1 files changed, 29 insertions, 38 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp index ecda713a6bc..a6280ec95a9 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -1831,29 +1831,14 @@ static bool areInverseVectorBitmasks(Constant *C1, Constant *C2) { /// We have an expression of the form (A & C) | (B & D). If A is a scalar or /// vector composed of all-zeros or all-ones values and is the bitwise 'not' of /// B, it can be used as the condition operand of a select instruction. -Value *InstCombiner::getSelectCondition(Value *A, Value *B) { - // Step 1: We need 0 or all-1's bitmasks. +static Value *getSelectCondition(Value *A, Value *B, + InstCombiner::BuilderTy &Builder) { + // If these are scalars or vectors of i1, A can be used directly. Type *Ty = A->getType(); - if (Ty->isIntOrIntVectorTy() && - ComputeNumSignBits(A) != Ty->getScalarSizeInBits()) - return nullptr; - - // Step 2: If B is the 'not' value of A, we have our answer. - if (match(A, m_Not(m_Specific(B)))) { - // If these are scalars or vectors of i1, A can be used directly. - if (Ty->isIntOrIntVectorTy(1)) - return A; - return Builder.CreateTrunc(A, CmpInst::makeCmpResultType(Ty)); - } - - // If both operands are constants, see if the constants are inverse bitmasks. - Constant *AConst, *BConst; - if (match(A, m_Constant(AConst)) && match(B, m_Constant(BConst))) - if (AConst == ConstantExpr::getNot(BConst)) - return Builder.CreateZExtOrTrunc(A, CmpInst::makeCmpResultType(Ty)); + if (match(A, m_Not(m_Specific(B))) && Ty->isIntOrIntVectorTy(1)) + return A; - // Look for more complex patterns. The 'not' op may be hidden behind various - // casts. Look through sexts and bitcasts to find the booleans. + // If A and B are sign-extended, look through the sexts to find the booleans. Value *Cond; Value *NotB; if (match(A, m_SExt(m_Value(Cond))) && @@ -1869,30 +1854,36 @@ Value *InstCombiner::getSelectCondition(Value *A, Value *B) { if (!Ty->isVectorTy()) return nullptr; + // If both operands are constants, see if the constants are inverse bitmasks. + Constant *AC, *BC; + if (match(A, m_Constant(AC)) && match(B, m_Constant(BC)) && + areInverseVectorBitmasks(AC, BC)) { + return Builder.CreateZExtOrTrunc(AC, CmpInst::makeCmpResultType(Ty)); + } + // If both operands are xor'd with constants using the same sexted boolean // operand, see if the constants are inverse bitmasks. - // TODO: Use ConstantExpr::getNot()? - if (match(A, (m_Xor(m_SExt(m_Value(Cond)), m_Constant(AConst)))) && - match(B, (m_Xor(m_SExt(m_Specific(Cond)), m_Constant(BConst)))) && + if (match(A, (m_Xor(m_SExt(m_Value(Cond)), m_Constant(AC)))) && + match(B, (m_Xor(m_SExt(m_Specific(Cond)), m_Constant(BC)))) && Cond->getType()->isIntOrIntVectorTy(1) && - areInverseVectorBitmasks(AConst, BConst)) { - AConst = ConstantExpr::getTrunc(AConst, CmpInst::makeCmpResultType(Ty)); - return Builder.CreateXor(Cond, AConst); + areInverseVectorBitmasks(AC, BC)) { + AC = ConstantExpr::getTrunc(AC, CmpInst::makeCmpResultType(Ty)); + return Builder.CreateXor(Cond, AC); } return nullptr; } /// We have an expression of the form (A & C) | (B & D). Try to simplify this /// to "A' ? C : D", where A' is a boolean or vector of booleans. -Value *InstCombiner::matchSelectFromAndOr(Value *A, Value *C, Value *B, - Value *D) { +static Value *matchSelectFromAndOr(Value *A, Value *C, Value *B, Value *D, + InstCombiner::BuilderTy &Builder) { // The potential condition of the select may be bitcasted. In that case, look // through its bitcast and the corresponding bitcast of the 'not' condition. Type *OrigType = A->getType(); A = peekThroughBitcast(A, true); B = peekThroughBitcast(B, true); - if (Value *Cond = getSelectCondition(A, B)) { + if (Value *Cond = getSelectCondition(A, B, Builder)) { // ((bc Cond) & C) | ((bc ~Cond) & D) --> bc (select Cond, (bc C), (bc D)) // The bitcasts will either all exist or all not exist. The builder will // not create unnecessary casts if the types already match. @@ -2243,21 +2234,21 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { // 'or' that it is replacing. if (Op0->hasOneUse() || Op1->hasOneUse()) { // (Cond & C) | (~Cond & D) -> Cond ? C : D, and commuted variants. - if (Value *V = matchSelectFromAndOr(A, C, B, D)) + if (Value *V = matchSelectFromAndOr(A, C, B, D, Builder)) return replaceInstUsesWith(I, V); - if (Value *V = matchSelectFromAndOr(A, C, D, B)) + if (Value *V = matchSelectFromAndOr(A, C, D, B, Builder)) return replaceInstUsesWith(I, V); - if (Value *V = matchSelectFromAndOr(C, A, B, D)) + if (Value *V = matchSelectFromAndOr(C, A, B, D, Builder)) return replaceInstUsesWith(I, V); - if (Value *V = matchSelectFromAndOr(C, A, D, B)) + if (Value *V = matchSelectFromAndOr(C, A, D, B, Builder)) return replaceInstUsesWith(I, V); - if (Value *V = matchSelectFromAndOr(B, D, A, C)) + if (Value *V = matchSelectFromAndOr(B, D, A, C, Builder)) return replaceInstUsesWith(I, V); - if (Value *V = matchSelectFromAndOr(B, D, C, A)) + if (Value *V = matchSelectFromAndOr(B, D, C, A, Builder)) return replaceInstUsesWith(I, V); - if (Value *V = matchSelectFromAndOr(D, B, A, C)) + if (Value *V = matchSelectFromAndOr(D, B, A, C, Builder)) return replaceInstUsesWith(I, V); - if (Value *V = matchSelectFromAndOr(D, B, C, A)) + if (Value *V = matchSelectFromAndOr(D, B, C, A, Builder)) return replaceInstUsesWith(I, V); } } |