Simplify code a lot by using the Module::getFunction & getOrInsertFunction
authorChris Lattner <sabre@nondot.org>
Fri, 29 Mar 2002 03:38:05 +0000 (03:38 +0000)
committerChris Lattner <sabre@nondot.org>
Fri, 29 Mar 2002 03:38:05 +0000 (03:38 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2028 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Utils/LowerAllocations.cpp

index 3ee74d49c7fc88a58701123b932da7ba9e3ccecc..3345acd6f73c354907ae0e369a6fd1d23b82cebd 100644 (file)
@@ -13,7 +13,6 @@
 #include "llvm/DerivedTypes.h"
 #include "llvm/iMemory.h"
 #include "llvm/iOther.h"
-#include "llvm/SymbolTable.h"
 #include "llvm/ConstantVals.h"
 #include "llvm/Pass.h"
 #include "TransformInternals.h"
@@ -73,37 +72,18 @@ public:
 // This function is always successful.
 //
 bool LowerAllocations::doInitialization(Module *M) {
-  bool Changed = false;
-  const MethodType *MallocType = 
-    MethodType::get(PointerType::get(Type::SByteTy),
-                    vector<const Type*>(1, Type::UIntTy), false);
-
-  SymbolTable *SymTab = M->getSymbolTableSure();
-  
-  // Check for a definition of malloc
-  if (Value *V = SymTab->lookup(PointerType::get(MallocType), "malloc")) {
-    MallocFunc = cast<Function>(V);      // Yup, got it
-  } else {                             // Nope, add one
-    M->getFunctionList().push_back(MallocFunc = new Function(MallocType, false, 
-                                                             "malloc"));
-    Changed = true;
-  }
+  const FunctionType *MallocType = 
+    FunctionType::get(PointerType::get(Type::SByteTy),
+                      vector<const Type*>(1, Type::UIntTy), false);
+  const FunctionType *FreeType = 
+    FunctionType::get(Type::VoidTy,
+                      vector<const Type*>(1, PointerType::get(Type::SByteTy)),
+                      false);
 
-  const MethodType *FreeType = 
-    MethodType::get(Type::VoidTy,
-                    vector<const Type*>(1, PointerType::get(Type::SByteTy)),
-                   false);
-
-  // Check for a definition of free
-  if (Value *V = SymTab->lookup(PointerType::get(FreeType), "free")) {
-    FreeFunc = cast<Function>(V);      // Yup, got it
-  } else {                             // Nope, add one
-    FreeFunc = new Function(FreeType, false,"free");
-    M->getFunctionList().push_back(FreeFunc);
-    Changed = true;
-  }
+  MallocFunc = M->getOrInsertFunction("malloc", MallocType);
+  FreeFunc   = M->getOrInsertFunction("free"  , FreeType);
 
-  return Changed;
+  return false;
 }
 
 // runOnBasicBlock - This method does the actual work of converting
@@ -172,9 +152,6 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock *BB) {
 }
 
 bool RaiseAllocations::doInitialization(Module *M) {
-  SymbolTable *ST = M->getSymbolTable();
-  if (!ST) return false;
-
   // If the module has a symbol table, they might be referring to the malloc
   // and free functions.  If this is the case, grab the method pointers that 
   // the module is using.
@@ -183,20 +160,21 @@ bool RaiseAllocations::doInitialization(Module *M) {
   // don't exist, or are not external, we do not worry about converting calls
   // to that function into the appropriate instruction.
   //
-  const PointerType *MallocType =   // Get the type for malloc
-    PointerType::get(MethodType::get(PointerType::get(Type::SByteTy),
-                                  vector<const Type*>(1, Type::UIntTy), false));
-  MallocFunc = cast_or_null<Function>(ST->lookup(MallocType, "malloc"));
-  if (MallocFunc && !MallocFunc->isExternal())
-    MallocFunc = 0;  // Don't mess with locally defined versions of the fn
-
-  const PointerType *FreeType =     // Get the type for free
-    PointerType::get(MethodType::get(Type::VoidTy,
-            vector<const Type*>(1, PointerType::get(Type::SByteTy)), false));
-  FreeFunc = cast_or_null<Function>(ST->lookup(FreeType, "free"));
-  if (FreeFunc && !FreeFunc->isExternal())
-    FreeFunc = 0;  // Don't mess with locally defined versions of the fn
+  const FunctionType *MallocType =   // Get the type for malloc
+    FunctionType::get(PointerType::get(Type::SByteTy),
+                      vector<const Type*>(1, Type::UIntTy), false);
+
+  const FunctionType *FreeType =     // Get the type for free
+    FunctionType::get(Type::VoidTy,
+                      vector<const Type*>(1, PointerType::get(Type::SByteTy)),
+                      false);
+
+  MallocFunc = M->getFunction("malloc", MallocType);
+  FreeFunc   = M->getFunction("free"  , FreeType);
 
+  // Don't mess with locally defined versions of these functions...
+  if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0;
+  if (FreeFunc && !FreeFunc->isExternal())     FreeFunc = 0;
   return false;
 }