Start using the new function cloning header
[oota-llvm.git] / lib / VMCore / Module.cpp
index a895929280d9c280bf3799be02fddb735863e2a6..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) {
@@ -67,7 +75,7 @@ void Module::dump() const {
 }
 
 SymbolTable *Module::getSymbolTableSure() {
-  if (!SymTab) SymTab = new SymbolTable(0);
+  if (!SymTab) SymTab = new SymbolTable();
   return SymTab;
 }
 
@@ -131,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.
 //