Index: llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp =================================================================== --- llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp +++ llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp @@ -1819,6 +1819,9 @@ } Value *SCEVExpander::visitUMinExpr(const SCEVUMinExpr *S) { + // For the umin(1,X1...Xn) pattern described in the cost modeling, we let + // instcombine do the actual transform, relying on the order of SCEV + // canonicalization to put the constant in the outermost umin. Value *LHS = expand(S->getOperand(S->getNumOperands() - 1)); Type *Ty = LHS->getType(); for (int i = S->getNumOperands() - 2; i >= 0; --i) { @@ -2312,10 +2315,21 @@ // see SCEVExpander::visitMulExpr(), ExpandOpBinPowN(). Cost = ArithCost(Instruction::Mul, S->getNumOperands() - 1); break; + case scUMinExpr: + // For umin(1, x2, ..., xn), the form we generate will be converted by + // instcombine to: zext(icmp ne umin(x1..xn), 0). This saves one select. + if (S->getOperand(0)->getType()->isIntegerTy() && + S->getOperand(0)->isOne()) { + Cost += CmpSelCost(Instruction::ICmp, S->getNumOperands() - 1, 0, 1); + if (S->getNumOperands() > 2) + Cost += CmpSelCost(Instruction::Select, S->getNumOperands() - 2, 0, 2); + Cost += CastCost(Instruction::ZExt); + break; + } + LLVM_FALLTHROUGH; case scSMaxExpr: case scUMaxExpr: - case scSMinExpr: - case scUMinExpr: { + case scSMinExpr: { // FIXME: should this ask the cost for Intrinsic's? Cost += CmpSelCost(Instruction::ICmp, S->getNumOperands() - 1, 0, 1); Cost += CmpSelCost(Instruction::Select, S->getNumOperands() - 1, 0, 2);