[llvm-commits] [llvm] r92403 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/cast_ptr.ll

Chris Lattner sabre at nondot.org
Fri Jan 1 15:09:09 PST 2010


Author: lattner
Date: Fri Jan  1 17:09:08 2010
New Revision: 92403

URL: http://llvm.org/viewvc/llvm-project?rev=92403&view=rev
Log:
add a simple instcombine xform, simplify another one to use hasAllZeroIndices()
instead of hand rolling a loop.

Modified:
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
    llvm/trunk/test/Transforms/InstCombine/cast_ptr.ll

Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=92403&r1=92402&r2=92403&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Fri Jan  1 17:09:08 2010
@@ -6427,21 +6427,12 @@
     if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
       switch (LHSI->getOpcode()) {
       case Instruction::GetElementPtr:
-        if (RHSC->isNullValue()) {
           // icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null
-          bool isAllZeros = true;
-          for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i)
-            if (!isa<Constant>(LHSI->getOperand(i)) ||
-                !cast<Constant>(LHSI->getOperand(i))->isNullValue()) {
-              isAllZeros = false;
-              break;
-            }
-          if (isAllZeros)
-            return new ICmpInst(I.getPredicate(), LHSI->getOperand(0),
-                    Constant::getNullValue(LHSI->getOperand(0)->getType()));
-        }
+        if (RHSC->isNullValue() &&
+            cast<GetElementPtrInst>(LHSI)->hasAllZeroIndices())
+          return new ICmpInst(I.getPredicate(), LHSI->getOperand(0),
+                  Constant::getNullValue(LHSI->getOperand(0)->getType()));
         break;
-
       case Instruction::PHI:
         // Only fold icmp into the PHI if the phi and icmp are in the same
         // block.  If in the same block, we're encouraging jump threading.  If
@@ -6506,6 +6497,14 @@
             }
         }
         break;
+      case Instruction::IntToPtr:
+        // icmp pred inttoptr(X), null -> icmp pred X, 0
+        if (RHSC->isNullValue() && TD &&
+            TD->getIntPtrType(RHSC->getContext()) == 
+               LHSI->getOperand(0)->getType())
+          return new ICmpInst(I.getPredicate(), LHSI->getOperand(0),
+                        Constant::getNullValue(LHSI->getOperand(0)->getType()));
+        break;
       }
   }
 

Modified: llvm/trunk/test/Transforms/InstCombine/cast_ptr.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/cast_ptr.ll?rev=92403&r1=92402&r2=92403&view=diff

==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/cast_ptr.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/cast_ptr.ll Fri Jan  1 17:09:08 2010
@@ -36,3 +36,12 @@
         %r = icmp eq i32 %tmpa, ptrtoint (i8* @global to i32)
         ret i1 %r
 }
+
+define i1 @test4(i32 %A) {
+  %B = inttoptr i32 %A to i8*
+  %C = icmp eq i8* %B, null
+  ret i1 %C
+; CHECK: @test4
+; CHECK-NEXT: %C = icmp eq i32 %A, 0
+; CHECK-NEXT: ret i1 %C 
+}





More information about the llvm-commits mailing list