From d1bf4340ef2949a59645597294039b152d2ff276 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Fri, 11 Nov 2016 17:42:16 +0000 Subject: [PATCH] [InstCombine] fix formatting of FoldOpIntoSelect(); NFCI llvm-svn: 286604 --- .../InstCombine/InstructionCombining.cpp | 84 +++++++++++----------- 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 6b31a54..43b4eb5 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -783,51 +783,53 @@ static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, /// This also works for Cast instructions, which obviously do not have a second /// operand. Instruction *InstCombiner::FoldOpIntoSelect(Instruction &Op, SelectInst *SI) { - // Don't modify shared select instructions - if (!SI->hasOneUse()) return nullptr; - Value *TV = SI->getOperand(1); - Value *FV = SI->getOperand(2); - - if (isa(TV) || isa(FV)) { - // Bool selects with constant operands can be folded to logical ops. - if (SI->getType()->getScalarType()->isIntegerTy(1)) return nullptr; - - // If it's a bitcast involving vectors, make sure it has the same number of - // elements on both sides. - if (BitCastInst *BC = dyn_cast(&Op)) { - VectorType *DestTy = dyn_cast(BC->getDestTy()); - VectorType *SrcTy = dyn_cast(BC->getSrcTy()); - - // Verify that either both or neither are vectors. - if ((SrcTy == nullptr) != (DestTy == nullptr)) return nullptr; - // If vectors, verify that they have the same number of elements. - if (SrcTy && SrcTy->getNumElements() != DestTy->getNumElements()) - return nullptr; - } + // Don't modify shared select instructions. + if (!SI->hasOneUse()) + return nullptr; - // Test if a CmpInst instruction is used exclusively by a select as - // part of a minimum or maximum operation. If so, refrain from doing - // any other folding. This helps out other analyses which understand - // non-obfuscated minimum and maximum idioms, such as ScalarEvolution - // and CodeGen. And in this case, at least one of the comparison - // operands has at least one user besides the compare (the select), - // which would often largely negate the benefit of folding anyway. - if (auto *CI = dyn_cast(SI->getCondition())) { - if (CI->hasOneUse()) { - Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1); - if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) || - (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1)) - return nullptr; - } - } + Value *TV = SI->getTrueValue(); + Value *FV = SI->getFalseValue(); + if (!(isa(TV) || isa(FV))) + return nullptr; + + // Bool selects with constant operands can be folded to logical ops. + if (SI->getType()->getScalarType()->isIntegerTy(1)) + return nullptr; - Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, this); - Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, this); + // If it's a bitcast involving vectors, make sure it has the same number of + // elements on both sides. + if (auto *BC = dyn_cast(&Op)) { + VectorType *DestTy = dyn_cast(BC->getDestTy()); + VectorType *SrcTy = dyn_cast(BC->getSrcTy()); - return SelectInst::Create(SI->getCondition(), - SelectTrueVal, SelectFalseVal); + // Verify that either both or neither are vectors. + if ((SrcTy == nullptr) != (DestTy == nullptr)) + return nullptr; + + // If vectors, verify that they have the same number of elements. + if (SrcTy && SrcTy->getNumElements() != DestTy->getNumElements()) + return nullptr; } - return nullptr; + + // Test if a CmpInst instruction is used exclusively by a select as + // part of a minimum or maximum operation. If so, refrain from doing + // any other folding. This helps out other analyses which understand + // non-obfuscated minimum and maximum idioms, such as ScalarEvolution + // and CodeGen. And in this case, at least one of the comparison + // operands has at least one user besides the compare (the select), + // which would often largely negate the benefit of folding anyway. + if (auto *CI = dyn_cast(SI->getCondition())) { + if (CI->hasOneUse()) { + Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1); + if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) || + (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1)) + return nullptr; + } + } + + Value *SelectTVal = FoldOperationIntoSelectOperand(Op, TV, this); + Value *SelectFVal = FoldOperationIntoSelectOperand(Op, FV, this); + return SelectInst::Create(SI->getCondition(), SelectTVal, SelectFVal); } /// Given a binary operator, cast instruction, or select which has a PHI node as -- 2.7.4