[cfe-commits] r39560 - /cfe/cfe/trunk/CodeGen/CGExpr.cpp

clattner at cs.uiuc.edu clattner at cs.uiuc.edu
Wed Jul 11 09:45:36 PDT 2007


Author: clattner
Date: Wed Jul 11 11:45:36 2007
New Revision: 39560

URL: http://llvm.org/viewvc/llvm-project?rev=39560&view=rev
Log:
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

Modified:
    cfe/cfe/trunk/CodeGen/CGExpr.cpp

Modified: cfe/cfe/trunk/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/CodeGen/CGExpr.cpp?rev=39560&r1=39559&r2=39560&view=diff

==============================================================================
--- cfe/cfe/trunk/CodeGen/CGExpr.cpp (original)
+++ cfe/cfe/trunk/CodeGen/CGExpr.cpp Wed Jul 11 11:45:36 2007
@@ -75,13 +75,25 @@
   
   // 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 @@
   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.





More information about the cfe-commits mailing list