diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 6774419e797..2f2d32ceab6 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -2055,11 +2055,20 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { // Selecting between two constants? if (Constant *TrueValC = dyn_cast(TrueVal)) if (Constant *FalseValC = dyn_cast(FalseVal)) { - // If the true constant is a 1 and the false is a zero, turn this into a - // cast from bool. - if (FalseValC->isNullValue() && isa(TrueValC) && - cast(TrueValC)->getRawValue() == 1) - return new CastInst(CondVal, SI.getType()); + if (SI.getType()->isInteger()) { + // select C, 1, 0 -> cast C to int + if (FalseValC->isNullValue() && isa(TrueValC) && + cast(TrueValC)->getRawValue() == 1) { + return new CastInst(CondVal, SI.getType()); + } else if (TrueValC->isNullValue() && isa(FalseValC) && + cast(FalseValC)->getRawValue() == 1) { + // select C, 0, 1 -> cast !C to int + Value *NotCond = + InsertNewInstBefore(BinaryOperator::createNot(CondVal, + "not."+CondVal->getName()), SI); + return new CastInst(NotCond, SI.getType()); + } + } } return 0;