Start using the new function cloning header
[oota-llvm.git] / lib / VMCore / Module.cpp
index 836ed47667d7d42b365b3ae6e33bc9196fb6eec9..c12b32d7c1395b6e1aae88970e0df52b0f360137 100644 (file)
@@ -9,16 +9,24 @@
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "Support/STLExtras.h"
+#include "Support/LeakDetector.h"
 #include "SymbolTableListTraitsImpl.h"
 #include <algorithm>
 #include <map>
 
 Function *ilist_traits<Function>::createNode() {
-  return new Function(FunctionType::get(Type::VoidTy,std::vector<const Type*>(),
-                                        false), false);
+  FunctionType *FTy =
+    FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
+  Function *Ret = new Function(FTy, false);
+  // This should not be garbage monitored.
+  LeakDetector::removeGarbageObject(Ret);
+  return Ret;
 }
 GlobalVariable *ilist_traits<GlobalVariable>::createNode() {
-  return new GlobalVariable(Type::IntTy, false, false);
+  GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false, false);
+  // This should not be garbage monitored.
+  LeakDetector::removeGarbageObject(Ret);
+  return Ret;
 }
 
 iplist<Function> &ilist_traits<Function>::getList(Module *M) {
@@ -61,8 +69,13 @@ Module::~Module() {
   delete SymTab;
 }
 
+// Module::dump() - Allow printing from debugger
+void Module::dump() const {
+  print(std::cerr);
+}
+
 SymbolTable *Module::getSymbolTableSure() {
-  if (!SymTab) SymTab = new SymbolTable(0);
+  if (!SymTab) SymTab = new SymbolTable();
   return SymTab;
 }
 
@@ -126,6 +139,67 @@ bool Module::addTypeName(const std::string &Name, const Type *Ty) {
   return false;
 }
 
+/// getMainFunction - This function looks up main efficiently.  This is such a
+/// common case, that it is a method in Module.  If main cannot be found, a
+/// null pointer is returned.
+///
+Function *Module::getMainFunction() {
+  std::vector<const Type*> Params;
+
+  // int main(void)...
+  if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
+                                                          Params, false)))
+    return F;
+
+  // void main(void)...
+  if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
+                                                          Params, false)))
+    return F;
+
+  Params.push_back(Type::IntTy);
+
+  // int main(int argc)...
+  if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
+                                                          Params, false)))
+    return F;
+
+  // void main(int argc)...
+  if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
+                                                          Params, false)))
+    return F;
+
+  for (unsigned i = 0; i != 2; ++i) {  // Check argv and envp
+    Params.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
+
+    // int main(int argc, char **argv)...
+    if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
+                                                            Params, false)))
+      return F;
+    
+    // void main(int argc, char **argv)...
+    if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
+                                                            Params, false)))
+      return F;
+  }
+
+  // Ok, try to find main the hard way...
+  return getNamedFunction("main");
+}
+
+/// getNamedFunction - Return the first function in the module with the
+/// specified name, of arbitrary type.  This method returns null if a function
+/// with the specified name is not found.
+///
+Function *Module::getNamedFunction(const std::string &Name) {
+  // Loop over all of the functions, looking for the function desired
+  for (iterator I = begin(), E = end(); I != E; ++I)
+    if (I->getName() == Name)
+      return I;
+  return 0; // function not found...
+}
+
+
+
 // getTypeName - If there is at least one entry in the symbol table for the
 // specified type, return it.
 //
@@ -147,13 +221,12 @@ std::string Module::getTypeName(const Type *Ty) {
 }
 
 
-// dropAllReferences() - This function causes all the subinstructions to "let
-// go" of all references that they are maintaining.  This allows one to
-// 'delete' a whole class at a time, even though there may be circular
-// references... first all references are dropped, and all use counts go to
-// zero.  Then everything is delete'd for real.  Note that no operations are
-// valid on an object that has "dropped all references", except operator 
-// delete.
+// dropAllReferences() - This function causes all the subelementss to "let go"
+// of all references that they are maintaining.  This allows one to 'delete' a
+// whole module at a time, even though there may be circular references... first
+// all references are dropped, and all use counts go to zero.  Then everything
+// is delete'd for real.  Note that no operations are valid on an object that
+// has "dropped all references", except operator delete.
 //
 void Module::dropAllReferences() {
   for(Module::iterator I = begin(), E = end(); I != E; ++I)
@@ -164,17 +237,13 @@ void Module::dropAllReferences() {
 
   // If there are any GlobalVariable references still out there, nuke them now.
   // Since all references are hereby dropped, nothing could possibly reference
-  // them still.
-  if (GVRefMap) {
-    for (GlobalValueRefMap::iterator I = GVRefMap->Map.begin(),
-           E = GVRefMap->Map.end(); I != E; ++I) {
-      // Delete the ConstantPointerRef node...
-      I->second->destroyConstant();
-    }
-
-    // Since the table is empty, we can now delete it...
-    delete GVRefMap;
-  }
+  // them still.  Note that destroying all of the constant pointer refs will
+  // eventually cause the GVRefMap field to be set to null (by
+  // destroyConstantPointerRef, below).
+  //
+  while (GVRefMap)
+    // Delete the ConstantPointerRef node...  
+    GVRefMap->Map.begin()->second->destroyConstant();
 }
 
 // Accessor for the underlying GlobalValRefMap...
@@ -186,11 +255,21 @@ ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
   if (I != GVRefMap->Map.end()) return I->second;
 
   ConstantPointerRef *Ref = new ConstantPointerRef(V);
-  GVRefMap->Map.insert(std::make_pair(V, Ref));
-
+  GVRefMap->Map[V] = Ref;
   return Ref;
 }
 
+void Module::destroyConstantPointerRef(ConstantPointerRef *CPR) {
+  assert(GVRefMap && "No map allocated, but we have a CPR?");
+  if (!GVRefMap->Map.erase(CPR->getValue()))  // Remove it from the map...
+    assert(0 && "ConstantPointerRef not found in module CPR map!");
+  
+  if (GVRefMap->Map.empty()) {   // If the map is empty, delete it.
+    delete GVRefMap;
+    GVRefMap = 0;
+  }
+}
+
 void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
   GlobalValueRefMap::iterator I = GVRefMap->Map.find(OldGV);
   assert(I != GVRefMap->Map.end() &&