diff options
author | Chris Lattner <sabre@nondot.org> | 2008-11-11 18:56:45 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2008-11-11 18:56:45 +0000 |
commit | c657e92d7b44568fe39ae018a69b18cbcc4ea3b0 (patch) | |
tree | dde60d6268fa3aa182f8a459abc257afb8761f24 /lib/CodeGen | |
parent | bc6ec759afc13ebbdd755e04e849beeacf363ccb (diff) |
Try to not emit the dead side of ?: if the condition is a constant.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@59061 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen')
-rw-r--r-- | lib/CodeGen/CGExprScalar.cpp | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp index 82f0cc7e77..bf22902b68 100644 --- a/lib/CodeGen/CGExprScalar.cpp +++ b/lib/CodeGen/CGExprScalar.cpp @@ -1110,14 +1110,31 @@ Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) { Value *ScalarExprEmitter:: VisitConditionalOperator(const ConditionalOperator *E) { - llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.?"); - llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.:"); - llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.cont"); - // Evaluate the conditional, then convert it to bool. We do this explicitly // because we need the unconverted value if this is a GNU ?: expression with // missing middle value. Value *CondVal = CGF.EmitScalarExpr(E->getCond()); + + // If the condition folded to a constant, try to elide the dead side. We + // can't do this if the dead side contains a label. + if (llvm::ConstantInt *CondCI = dyn_cast<llvm::ConstantInt>(CondVal)) { + Expr *Live = E->getLHS(), *Dead = E->getRHS(); + if (CondCI->getZExtValue() == 0) + std::swap(Live, Dead); + if (!Dead || !CGF.ContainsLabel(Dead)) { + // Emit the live side. + if (Live) + return Visit(Live); + // Perform promotions, to handle cases like "short ?: int" + return EmitScalarConversion(CondVal, E->getCond()->getType(), + E->getType()); + } + } + + llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.?"); + llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.:"); + llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.cont"); + Value *CondBoolVal =CGF.EmitScalarConversion(CondVal, E->getCond()->getType(), CGF.getContext().BoolTy); Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock); |