[llvm-commits] [llvm] r41928 - /llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp

Chris Lattner sabre at nondot.org
Thu Sep 13 09:37:21 PDT 2007


Author: lattner
Date: Thu Sep 13 11:37:20 2007
New Revision: 41928

URL: http://llvm.org/viewvc/llvm-project?rev=41928&view=rev
Log:
Make ValueIsOnlyUsedLocallyOrStoredToOneGlobal smart enough to see through
bitcasts and phis.  This is a step to fixing PR1639.

Modified:
    llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp

Modified: llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp?rev=41928&r1=41927&r2=41928&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp Thu Sep 13 11:37:20 2007
@@ -811,7 +811,8 @@
 /// like dereferencing the pointer, but not storing through the address, unless
 /// it is to the specified global.
 static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal(Instruction *V,
-                                                      GlobalVariable *GV) {
+                                                      GlobalVariable *GV,
+                                              SmallPtrSet<PHINode*, 8> &PHIs) {
   for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI)
     if (isa<LoadInst>(*UI) || isa<CmpInst>(*UI)) {
       // Fine, ignore.
@@ -819,9 +820,16 @@
       if (SI->getOperand(0) == V && SI->getOperand(1) != GV)
         return false;  // Storing the pointer itself... bad.
       // Otherwise, storing through it, or storing into GV... fine.
-    } else if (isa<GetElementPtrInst>(*UI) || isa<SelectInst>(*UI)) {
-      if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(cast<Instruction>(*UI),GV))
+    } else if (isa<GetElementPtrInst>(*UI) || isa<SelectInst>(*UI) ||
+               isa<BitCastInst>(*UI)) {
+      if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(cast<Instruction>(*UI),
+                                                     GV, PHIs))
         return false;
+    } else if (PHINode *PN = dyn_cast<PHINode>(*UI)) {
+      // PHIs are ok if all uses are ok.  Don't infinitely recurse through PHI
+      // cycles.
+      if (PHIs.insert(PN))
+        return ValueIsOnlyUsedLocallyOrStoredToOneGlobal(PN, GV, PHIs);
     } else {
       return false;
     }
@@ -1125,8 +1133,11 @@
       // malloc to be stored into the specified global, loaded setcc'd, and
       // GEP'd.  These are all things we could transform to using the global
       // for.
-      if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(MI, GV))
-        return false;
+      {
+        SmallPtrSet<PHINode*, 8> PHIs;
+        if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(MI, GV, PHIs))
+          return false;
+      }
 
       
       // If we have a global that is only initialized with a fixed size malloc,





More information about the llvm-commits mailing list