[llvm-commits] [poolalloc] r128960 - in /poolalloc/trunk/lib/AssistDS: FuncSpec.cpp Int2PtrCmp.cpp

Arushi Aggarwal aggarwa4 at illinois.edu
Tue Apr 5 16:40:49 PDT 2011


Author: aggarwa4
Date: Tue Apr  5 18:40:49 2011
New Revision: 128960

URL: http://llvm.org/viewvc/llvm-project?rev=128960&view=rev
Log:
Added comments.

Modified:
    poolalloc/trunk/lib/AssistDS/FuncSpec.cpp
    poolalloc/trunk/lib/AssistDS/Int2PtrCmp.cpp

Modified: poolalloc/trunk/lib/AssistDS/FuncSpec.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/AssistDS/FuncSpec.cpp?rev=128960&r1=128959&r2=128960&view=diff
==============================================================================
--- poolalloc/trunk/lib/AssistDS/FuncSpec.cpp (original)
+++ poolalloc/trunk/lib/AssistDS/FuncSpec.cpp Tue Apr  5 18:40:49 2011
@@ -121,6 +121,9 @@
   };
 }
 
+// Pass ID variable
 char FuncSpec::ID = 0;
+
+// Register the pass
 static RegisterPass<FuncSpec>
 X("funcspec", "Specialize for Function Pointers");

Modified: poolalloc/trunk/lib/AssistDS/Int2PtrCmp.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/AssistDS/Int2PtrCmp.cpp?rev=128960&r1=128959&r2=128960&view=diff
==============================================================================
--- poolalloc/trunk/lib/AssistDS/Int2PtrCmp.cpp (original)
+++ poolalloc/trunk/lib/AssistDS/Int2PtrCmp.cpp Tue Apr  5 18:40:49 2011
@@ -6,7 +6,12 @@
 // License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
-#define DEBUG_TYPE "varargfunc"
+// Remove unnecessary inttoptr casts
+// Specially ones used in just compares
+// Most cases derived from InstCombine
+//
+//===----------------------------------------------------------------------===//
+#define DEBUG_TYPE "int2ptr-cmp"
 
 #include "llvm/Instructions.h"
 #include "llvm/Module.h"
@@ -32,31 +37,45 @@
   public:
     static char ID;
     Int2PtrCmp() : ModulePass(&ID) {}
+
+    //
+    // Method: runOnModule()
+    //
+    // Description:
+    //  Entry point for this LLVM pass.
+    //  Remove unnecessary inttoptr instructions.
+    //
+    // Inputs:
+    //  M - A reference to the LLVM module to transform
+    //
+    // Outputs:
+    //  M - The transformed LLVM module.
+    //
+    // Return value:
+    //  true  - The module was modified.
+    //  false - The module was not modified.
+    //
     bool runOnModule(Module& M) {
-    TD = &getAnalysis<TargetData>();
-      //std::vector<PtrToIntInst*> worklist;
+      TD = &getAnalysis<TargetData>();
       for (Module::iterator F = M.begin(); F != M.end(); ++F) {
         for (Function::iterator B = F->begin(), FE = F->end(); B != FE; ++B) {      
           for (BasicBlock::iterator I = B->begin(), BE = B->end(); I != BE;) {
+            // ptrtoint(inttoptr ty Y) to ty -> Y 
             if(PtrToIntInst *P2I = dyn_cast<PtrToIntInst>(I++)) {
               if(IntToPtrInst *I2P = dyn_cast<IntToPtrInst>(P2I->getOperand(0))) {
                 if(I2P->getSrcTy() == P2I->getDestTy()){
                   P2I->replaceAllUsesWith(I2P->getOperand(0));
                   P2I->eraseFromParent();
-                  if(I2P->use_empty())
+                  if(I2P->use_empty()) {
+                    // If this is the only use of the cast delete it.
                     I2P->eraseFromParent();
+                  }
                 }
-
               }
             }
           }
-        }
-      }
-      
-      //icmp pred inttoptr(X), null  -> icmp pred X 0
-      for (Module::iterator F = M.begin(); F != M.end(); ++F) {
-        for (Function::iterator B = F->begin(), FE = F->end(); B != FE; ++B) {      
           for (BasicBlock::iterator I = B->begin(), BE = B->end(); I != BE;) {
+            //icmp pred inttoptr(X), null  -> icmp pred X 0
             if(ICmpInst *CI = dyn_cast<ICmpInst>(I++)) {
               Value *Op0 = CI->getOperand(0);
               Value *Op1 = CI->getOperand(1);
@@ -68,11 +87,13 @@
                         LHSI->getOperand(0)->getType()){
                       ICmpInst *CI_new = new ICmpInst(CI, CI->getPredicate(), LHSI->getOperand(0),
                                                       Constant::getNullValue(LHSI->getOperand(0)->getType()));
-                      
+
                       CI->replaceAllUsesWith(CI_new);
                       CI->eraseFromParent();
-                      if(LHSI->use_empty())
+                      if(LHSI->use_empty()) {
+                        // If this is the only use of the cast delete it.
                         LHSI->eraseFromParent();
+                      }
                     }
                   }
                 }
@@ -98,10 +119,12 @@
                       break;
                     Value *P, *Q, *R;
                     if (match(LHSI, m_Or(m_PtrToInt(m_Value(P)), m_PtrToInt(m_Value(Q))))) {
+                      // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0
+                      // -> and (icmp eq P, null), (icmp eq Q, null).
                       Value *ICIP = new ICmpInst(ICI, ICI->getPredicate(), P,
-                                                        Constant::getNullValue(P->getType()));
+                                                 Constant::getNullValue(P->getType()));
                       Value *ICIQ = new ICmpInst(ICI, ICI->getPredicate(), Q,
-                                                        Constant::getNullValue(Q->getType()));
+                                                 Constant::getNullValue(Q->getType()));
                       Instruction *Op;
                       if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
                         Op = BinaryOperator::CreateAnd(ICIP, ICIQ,"",ICI);
@@ -109,13 +132,17 @@
                         Op = BinaryOperator::CreateOr(ICIP, ICIQ, "", ICI);
                       ICI->replaceAllUsesWith(Op);
 
-                    } else if(match(LHSI, m_Or(m_Or(m_PtrToInt(m_Value(P)), m_PtrToInt(m_Value(Q))), m_PtrToInt(m_Value(R))))) {
+                    } else if(match(LHSI, m_Or(m_Or(m_PtrToInt(m_Value(P)), 
+                                                    m_PtrToInt(m_Value(Q))), 
+                                               m_PtrToInt(m_Value(R))))) {
+                      // Simplify icmp eq (or (or (ptrtoint P), (ptrtoint Q)), ptrtoint(R)), 0
+                      // -> and (and (icmp eq P, null), (icmp eq Q, null)), (icmp eq R, null).
                       Value *ICIP = new ICmpInst(ICI, ICI->getPredicate(), P,
-                                                        Constant::getNullValue(P->getType()));
+                                                 Constant::getNullValue(P->getType()));
                       Value *ICIQ = new ICmpInst(ICI, ICI->getPredicate(), Q,
-                                                        Constant::getNullValue(Q->getType()));
+                                                 Constant::getNullValue(Q->getType()));
                       Value *ICIR = new ICmpInst(ICI, ICI->getPredicate(), R,
-                                                        Constant::getNullValue(R->getType()));
+                                                 Constant::getNullValue(R->getType()));
                       Instruction *Op;
                       if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
                         Op = BinaryOperator::CreateAnd(ICIP, ICIQ,"",ICI);
@@ -128,13 +155,16 @@
                         Op = BinaryOperator::CreateOr(Op, ICIR, "", ICI);
                       ICI->replaceAllUsesWith(Op);
 
-                    } else if(match(LHSI, m_Or(m_PtrToInt(m_Value(Q)), m_Or(m_PtrToInt(m_Value(P)), m_PtrToInt(m_Value(R)))))) {
+                    } else if(match(LHSI, m_Or(m_PtrToInt(m_Value(Q)), m_Or(
+                            m_PtrToInt(m_Value(P)), m_PtrToInt(m_Value(R)))))) {
+                      // Simplify icmp eq (or  (ptrtoint P), or((ptrtoint Q), ptrtoint(R))), 0
+                      // -> and (icmp eq P, null), (and (icmp eq Q, null), (icmp eq R, null)).
                       Value *ICIP = new ICmpInst(ICI, ICI->getPredicate(), P,
-                                                        Constant::getNullValue(P->getType()));
+                                                 Constant::getNullValue(P->getType()));
                       Value *ICIQ = new ICmpInst(ICI, ICI->getPredicate(), Q,
-                                                        Constant::getNullValue(Q->getType()));
+                                                 Constant::getNullValue(Q->getType()));
                       Value *ICIR = new ICmpInst(ICI, ICI->getPredicate(), R,
-                                                        Constant::getNullValue(R->getType()));
+                                                 Constant::getNullValue(R->getType()));
                       Instruction *Op;
                       if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
                         Op = BinaryOperator::CreateAnd(ICIP, ICIQ,"",ICI);
@@ -162,6 +192,9 @@
   };
 }
 
+// Pass ID variable
 char Int2PtrCmp::ID = 0;
+
+// Register the pass
 static RegisterPass<Int2PtrCmp>
-X("int2ptrcmp", "Simplify inttoptr/ptrtoint if derived from the other");
+X("int2ptrcmp", "Simplify inttoptr/ptrtoint insts");





More information about the llvm-commits mailing list