Implement a trivial optimization to reduce the number of compares emitted.

For:

  register short X;
  if (!X) {

We now produce:

        %tmp = load i16* %X             ; <i16> [#uses=1]
        %tobool = icmp ne i16 %tmp, 0           ; <i1> [#uses=1]
        %lnot = xor i1 %tobool, true            ; <i1> [#uses=1]
        br i1 %lnot, label %ifthen, label %ifend

instead of:

        %tmp = load i16* %X             ; <i16> [#uses=1]
        %tobool = icmp ne i16 %tmp, 0           ; <i1> [#uses=1]
        %lnot = xor i1 %tobool, true            ; <i1> [#uses=1]
        %lnot.ext = zext i1 %lnot to i32                ; <i32> [#uses=1]
        %tobool1 = icmp ne i32 %lnot.ext, 0             ; <i1> [#uses=1]
        br i1 %tobool1, label %ifthen, label %ifend

llvm-svn: 39560
This commit is contained in:
Chris Lattner 2007-06-02 19:47:04 +00:00
parent f0106d2578
commit a45c5af87b
1 changed files with 16 additions and 2 deletions

View File

@ -75,13 +75,25 @@ Value *CodeGenFunction::EvaluateScalarValueToBool(ExprResult Val, QualType Ty) {
// Usual case for integers, pointers, and enums: compare against zero.
Result = Val.getVal();
// Because of the type rules of C, we often end up computing a logical value,
// then zero extending it to int, then wanting it as a logical value again.
// Optimize this common case.
if (llvm::ZExtInst *ZI = dyn_cast<ZExtInst>(Result)) {
if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
Result = ZI->getOperand(0);
ZI->eraseFromParent();
return Result;
}
}
llvm::Value *Zero = Constant::getNullValue(Result->getType());
return Builder.CreateICmpNE(Result, Zero, "tobool");
}
//===--------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
// LValue Expression Emission
//===--------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
LValue CodeGenFunction::EmitLValue(const Expr *E) {
switch (E->getStmtClass()) {
@ -171,6 +183,8 @@ ExprResult CodeGenFunction::EmitUnaryLNot(const UnaryOperator *E) {
Value *BoolVal = EvaluateScalarValueToBool(Op, E->getSubExpr()->getType());
// Invert value.
// TODO: Could dynamically modify easy computations here. For example, if
// the operand is an icmp ne, turn into icmp eq.
BoolVal = Builder.CreateNot(BoolVal, "lnot");
// ZExt result to int.