From 26368cd5d93887a02399df9f1708943638d32ff6 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Thu, 31 May 2018 19:55:27 +0000 Subject: [InstCombine] narrow select to match condition operands' size This is the planned enhancement to D47163 / rL333611. We want to match cmp/select sizes because that will be recognized as min/max more easily and lead to better codegen (especially for vector types). As mentioned in D47163, this improves some of the tests that would also be folded by D46380, so we may want to adjust that patch to match the new patterns where the extend op occurs after the select. llvm-svn: 333689 --- llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp') diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp index 9fe3c59e5e0..e70078a73c5 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -1164,6 +1164,11 @@ static Instruction *foldAddSubSelect(SelectInst &SI, } Instruction *InstCombiner::foldSelectExtConst(SelectInst &Sel) { + Constant *C; + if (!match(Sel.getTrueValue(), m_Constant(C)) && + !match(Sel.getFalseValue(), m_Constant(C))) + return nullptr; + Instruction *ExtInst; if (!match(Sel.getTrueValue(), m_Instruction(ExtInst)) && !match(Sel.getFalseValue(), m_Instruction(ExtInst))) @@ -1173,20 +1178,18 @@ Instruction *InstCombiner::foldSelectExtConst(SelectInst &Sel) { if (ExtOpcode != Instruction::ZExt && ExtOpcode != Instruction::SExt) return nullptr; - // TODO: Handle larger types? That requires adjusting FoldOpIntoSelect too. + // If we are extending from a boolean type or if we can create a select that + // has the same size operands as its condition, try to narrow the select. Value *X = ExtInst->getOperand(0); Type *SmallType = X->getType(); - if (!SmallType->isIntOrIntVectorTy(1)) - return nullptr; - - Constant *C; - if (!match(Sel.getTrueValue(), m_Constant(C)) && - !match(Sel.getFalseValue(), m_Constant(C))) + Value *Cond = Sel.getCondition(); + auto *Cmp = dyn_cast(Cond); + if (!SmallType->isIntOrIntVectorTy(1) && + (!Cmp || Cmp->getOperand(0)->getType() != SmallType)) return nullptr; // If the constant is the same after truncation to the smaller type and // extension to the original type, we can narrow the select. - Value *Cond = Sel.getCondition(); Type *SelType = Sel.getType(); Constant *TruncC = ConstantExpr::getTrunc(C, SmallType); Constant *ExtC = ConstantExpr::getCast(ExtOpcode, TruncC, SelType); -- cgit v1.2.3