[llvm-commits] CVS: llvm/lib/Transforms/IPO/GlobalOpt.cpp

Chris Lattner lattner at cs.uiuc.edu
Wed Dec 1 23:11:22 PST 2004



Changes in directory llvm/lib/Transforms/IPO:

GlobalOpt.cpp updated: 1.28 -> 1.29
---
Log message:

Implement a FIXME by checking to make sure that a malloc is not being used
in scary and unknown ways before we promote it.  This fixes the miscompilation
of 188.ammp that has been plauging us since a globalopt patch went in.

Thanks a ton to Tanya for helping me diagnose the problem!




---
Diffs of the changes:  (+25 -3)

Index: llvm/lib/Transforms/IPO/GlobalOpt.cpp
diff -u llvm/lib/Transforms/IPO/GlobalOpt.cpp:1.28 llvm/lib/Transforms/IPO/GlobalOpt.cpp:1.29
--- llvm/lib/Transforms/IPO/GlobalOpt.cpp:1.28	Thu Dec  2 00:25:58 2004
+++ llvm/lib/Transforms/IPO/GlobalOpt.cpp	Thu Dec  2 01:11:07 2004
@@ -738,6 +738,29 @@
   return NewGV;
 }
 
+/// ValueIsOnlyUsedLocallyOrStoredToOneGlobal - Scan the use-list of V checking
+/// to make sure that there are no complex uses of V.  We permit simple things
+/// like dereferencing the pointer, but not storing through the address, unless
+/// it is to the specified global.
+static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal(Instruction *V,
+                                                      GlobalVariable *GV) {
+  for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI)
+    if (isa<LoadInst>(*UI) || isa<SetCondInst>(*UI)) {
+      // Fine, ignore.
+    } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
+      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))
+        return false;
+    } else {
+      return false;
+    }
+  return true;
+
+}
+
 // OptimizeOnceStoredGlobal - Try to optimize globals based on the knowledge
 // that only one value (besides its initializer) is ever stored to the global.
 static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal,
@@ -783,9 +806,8 @@
         if (MI->getAllocatedType()->isSized() &&
             NElements->getRawValue()*
                      TD.getTypeSize(MI->getAllocatedType()) < 2048 &&
-            AllUsesOfLoadedValueWillTrapIfNull(GV)) {
-          // FIXME: do more correctness checking to make sure the result of the
-          // malloc isn't squirrelled away somewhere.
+            AllUsesOfLoadedValueWillTrapIfNull(GV) &&
+            ValueIsOnlyUsedLocallyOrStoredToOneGlobal(MI, GV)) {
           GVI = OptimizeGlobalAddressOfMalloc(GV, MI);
           return true;
         }






More information about the llvm-commits mailing list