From: Chris Lattner Date: Tue, 30 Mar 2004 19:37:13 +0000 (+0000) Subject: Implement select.ll:test[3-6] X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=c32b30a42999e7f67b87125e0301330608ec1287;p=oota-llvm.git Implement select.ll:test[3-6] git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12544 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 9b91705110d..13c6b26db1b 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -1994,14 +1994,42 @@ Instruction *InstCombiner::visitCastInst(CastInst &CI) { } Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { - if (ConstantBool *C = dyn_cast(SI.getCondition())) + Value *CondVal = SI.getCondition(); + Value *TrueVal = SI.getTrueValue(); + Value *FalseVal = SI.getFalseValue(); + + // select true, X, Y -> X + // select false, X, Y -> Y + if (ConstantBool *C = dyn_cast(CondVal)) if (C == ConstantBool::True) - return ReplaceInstUsesWith(SI, SI.getTrueValue()); + return ReplaceInstUsesWith(SI, TrueVal); else { assert(C == ConstantBool::False); - return ReplaceInstUsesWith(SI, SI.getFalseValue()); + return ReplaceInstUsesWith(SI, FalseVal); + } + + // select C, X, X -> X + if (TrueVal == FalseVal) + return ReplaceInstUsesWith(SI, TrueVal); + + // Selecting between two constants? + if (Constant *TrueValC = dyn_cast(TrueVal)) + if (Constant *FalseValC = dyn_cast(FalseVal)) { + if (SI.getType() == Type::BoolTy && + isa(TrueValC) && isa(FalseValC)) { + // select C, true, false -> C + if (TrueValC == ConstantBool::True) + return ReplaceInstUsesWith(SI, CondVal); + // select C, false, true -> !C + return BinaryOperator::createNot(CondVal); + } + + // 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()); } - // Other transformations are possible! return 0; }