factor some code into a InstallGlobalCtors method, add comments. No functionality...
authorChris Lattner <sabre@nondot.org>
Mon, 26 Sep 2005 02:31:18 +0000 (02:31 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 26 Sep 2005 02:31:18 +0000 (02:31 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23435 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/IPO/GlobalOpt.cpp

index b0163146530f6aeed2566d6ad23b4dd12ba45149..0b4b351d659c66d9f8e17bbd038d0eae8bc9b8fb 100644 (file)
@@ -1164,6 +1164,8 @@ GlobalVariable *GlobalOpt::FindGlobalCtors(Module &M) {
   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 @@ static std::vector<Function*> ParseGlobalCtors(GlobalVariable *GV) {
   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 @@ bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) {
     }
     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 @@ bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) {
   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;
 }