On Nov 11, 2008, at 11:04 PM, Chris Lattner wrote: > Author: lattner > Date: Wed Nov 12 01:04:29 2008 > New Revision: 59105 > > URL: http://llvm.org/viewvc/llvm-project?rev=59105&view=rev > Log: > Teach the aggressive constant folder to fold X && 0 -> 0 and X || 1 - > > 1
I assume that you care that 'X' has no side-effect here. - fariborz > > > Modified: > cfe/trunk/lib/AST/ExprConstant.cpp > > Modified: cfe/trunk/lib/AST/ExprConstant.cpp > URL: > http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=59105&r1=59104&r2=59105&view=diff > > = > = > = > = > = > = > = > = > ====================================================================== > --- cfe/trunk/lib/AST/ExprConstant.cpp (original) > +++ cfe/trunk/lib/AST/ExprConstant.cpp Wed Nov 12 01:04:29 2008 > @@ -323,8 +323,28 @@ > bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { > // The LHS of a constant expr is always evaluated and needed. > llvm::APSInt RHS(32); > - if (!Visit(E->getLHS())) > + if (!Visit(E->getLHS())) { > + // If the LHS is unfoldable, we generally can't fold this. > However, if this > + // is a logical operator like &&/||, and if we know that the > RHS determines > + // the outcome of the result (e.g. X && 0), return the outcome. > + if (!E->isLogicalOp()) > + return false; > + > + // If this is a logical op, see if the RHS determines the > outcome. > + EvalInfo Info2(Info.Ctx); > + if (!EvaluateInteger(E->getRHS(), RHS, Info2)) > + return false; > + > + // X && 0 -> 0, X || 1 -> 1. > + if (E->getOpcode() == BinaryOperator::LAnd && RHS == 0 || > + E->getOpcode() == BinaryOperator::LOr && RHS != 0) { > + Result = RHS != 0; > + Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); > + return true; > + } > + > return false; // error in subexpression. > + } > > bool OldEval = Info.isEvaluated; > > > > _______________________________________________ > cfe-commits mailing list > [email protected] > http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits _______________________________________________ cfe-commits mailing list [email protected] http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
