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

Chris Lattner lattner at cs.uiuc.edu
Sun Sep 25 19:31:29 PDT 2005



Changes in directory llvm/lib/Transforms/IPO:

GlobalOpt.cpp updated: 1.43 -> 1.44
---
Log message:

factor some code into a InstallGlobalCtors method, add comments.  No functionality change.


---
Diffs of the changes:  (+52 -35)

 GlobalOpt.cpp |   87 ++++++++++++++++++++++++++++++++++------------------------
 1 files changed, 52 insertions(+), 35 deletions(-)


Index: llvm/lib/Transforms/IPO/GlobalOpt.cpp
diff -u llvm/lib/Transforms/IPO/GlobalOpt.cpp:1.43 llvm/lib/Transforms/IPO/GlobalOpt.cpp:1.44
--- llvm/lib/Transforms/IPO/GlobalOpt.cpp:1.43	Sun Sep 25 21:19:27 2005
+++ llvm/lib/Transforms/IPO/GlobalOpt.cpp	Sun Sep 25 21:31:18 2005
@@ -1164,6 +1164,8 @@
   return 0;
 }
 
+/// ParseGlobalCtors - Given a llvm.global_ctors list that we can understand,
+/// return a list of the functions and null terminator as a vector.
 static std::vector<Function*> ParseGlobalCtors(GlobalVariable *GV) {
   ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
   std::vector<Function*> Result;
@@ -1175,38 +1177,11 @@
   return Result;
 }
 
-/// OptimizeGlobalCtorsList - Simplify and evaluation global ctors if possible.
-/// Return true if anything changed.
-bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) {
-  std::vector<Function*> Ctors = ParseGlobalCtors(GCL);
-  bool MadeChange = false;
-  if (Ctors.empty()) return false;
-  
-  // Loop over global ctors, optimizing them when we can.
-  for (unsigned i = 0; i != Ctors.size(); ++i) {
-    Function *F = Ctors[i];
-    // Found a null terminator in the middle of the list, prune off the rest of
-    // the list.
-    if (F == 0) {
-      if (i != Ctors.size()-1) {
-        Ctors.resize(i+1);
-        MadeChange = true;
-      }
-      break;
-    }
-    
-    // If the function is empty, just remove it from the ctor list.
-    if (!F->empty() && isa<ReturnInst>(F->begin()->getTerminator()) &&
-        &F->begin()->front() == F->begin()->getTerminator()) {
-      Ctors.erase(Ctors.begin()+i);
-      MadeChange = true;
-      --i;
-      ++NumEmptyCtor;
-    }
-  }
-  
-  if (!MadeChange) return false;
-  
+/// InstallGlobalCtors - Given a specified llvm.global_ctors list, install the
+/// specified array, returning the new global to use.
+static GlobalVariable *InstallGlobalCtors(GlobalVariable *GCL, 
+                                          const std::vector<Function*> &Ctors) {
+  // If we made a change, reassemble the initializer list.
   std::vector<Constant*> CSVals;
   CSVals.push_back(ConstantSInt::get(Type::IntTy, 65535));
   CSVals.push_back(0);
@@ -1225,13 +1200,19 @@
     }
     CAList.push_back(ConstantStruct::get(CSVals));
   }
-
+  
   // Create the array initializer.
   const Type *StructTy =
     cast<ArrayType>(GCL->getType()->getElementType())->getElementType();
   Constant *CA = ConstantArray::get(ArrayType::get(StructTy, CAList.size()),
                                     CAList);
   
+  // If we didn't change the number of elements, don't create a new GV.
+  if (CA->getType() == GCL->getInitializer()->getType()) {
+    GCL->setInitializer(CA);
+    return GCL;
+  }
+  
   // Create the new global and insert it next to the existing list.
   GlobalVariable *NGV = new GlobalVariable(CA->getType(), GCL->isConstant(),
                                            GCL->getLinkage(), CA,
@@ -1249,9 +1230,45 @@
   GCL->eraseFromParent();
   
   if (Ctors.size())
-    GCL = NGV;
+    return NGV;
   else
-    GCL = 0;
+    return 0;
+}
+                                          
+
+/// OptimizeGlobalCtorsList - Simplify and evaluation global ctors if possible.
+/// Return true if anything changed.
+bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) {
+  std::vector<Function*> Ctors = ParseGlobalCtors(GCL);
+  bool MadeChange = false;
+  if (Ctors.empty()) return false;
+  
+  // Loop over global ctors, optimizing them when we can.
+  for (unsigned i = 0; i != Ctors.size(); ++i) {
+    Function *F = Ctors[i];
+    // Found a null terminator in the middle of the list, prune off the rest of
+    // the list.
+    if (F == 0) {
+      if (i != Ctors.size()-1) {
+        Ctors.resize(i+1);
+        MadeChange = true;
+      }
+      break;
+    }
+    
+    // If the function is empty, just remove it from the ctor list.
+    if (!F->empty() && isa<ReturnInst>(F->begin()->getTerminator()) &&
+        &F->begin()->front() == F->begin()->getTerminator()) {
+      Ctors.erase(Ctors.begin()+i);
+      MadeChange = true;
+      --i;
+      ++NumEmptyCtor;
+    }
+  }
+  
+  if (!MadeChange) return false;
+  
+  GCL = InstallGlobalCtors(GCL, Ctors);
   return true;
 }
 






More information about the llvm-commits mailing list