summaryrefslogtreecommitdiff
path: root/clang/lib/CodeGen/CGExprScalar.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2008-11-11 07:41:27 +0000
committerChris Lattner <sabre@nondot.org>2008-11-11 07:41:27 +0000
commit5b1964b5f97f447cdff2e6dd02494212934f25fa (patch)
tree949beca7fe5ff809d1ada73f1e264a02d9e4cf5f /clang/lib/CodeGen/CGExprScalar.cpp
parentaaaa199ad1bd0ea57fc25c113ce6a5244ad5c516 (diff)
short circuit && and || when possible. This substantially reduces
the size of the -O0 output on some cases. For example, on expr.c from 176.gcc, it shrinks the .ll file from 43164 to 42835 lines, and removed references to two external symbols. llvm-svn: 59034
Diffstat (limited to 'clang/lib/CodeGen/CGExprScalar.cpp')
-rw-r--r--clang/lib/CodeGen/CGExprScalar.cpp28
1 files changed, 28 insertions, 0 deletions
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 7d3157b03d47..4a1793835777 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -1028,6 +1028,20 @@ Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
+ if (llvm::ConstantInt *LHSCst = dyn_cast<llvm::ConstantInt>(LHSCond)) {
+ // If we have 0 && RHS, see if we can elide RHS, if so, just return LHSCond.
+ if (LHSCst->getZExtValue() == 0) {
+ if (!CGF.ContainsLabel(E->getRHS()))
+ // Elide RHS, return 0
+ return llvm::Constant::getNullValue(CGF.LLVMIntTy);
+ } else {
+ // If we have 1 && X, just emit X without inserting the control flow.
+ Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
+ // ZExt result to int.
+ return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "land.ext");
+ }
+ }
+
llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("land_cont");
llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("land_rhs");
@@ -1055,6 +1069,20 @@ Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
+ if (llvm::ConstantInt *LHSCst = dyn_cast<llvm::ConstantInt>(LHSCond)) {
+ // If we have 1 || RHS, see if we can elide RHS, if so, just return LHSCond.
+ if (LHSCst->getZExtValue() != 0) {
+ if (!CGF.ContainsLabel(E->getRHS()))
+ // Elide RHS, return 1
+ return llvm::ConstantInt::get(CGF.LLVMIntTy, 1);
+ } else {
+ // If we have 0 || X, just emit X without inserting the control flow.
+ Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
+ // ZExt result to int.
+ return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "lor.ext");
+ }
+ }
+
llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor_cont");
llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor_rhs");