fix pasto
[oota-llvm.git] / lib / Transforms / IPO / RaiseAllocations.cpp
index b689de70830421188e2512f571763bf624a98897..e28400924738b4fe1133bdc1612257b4506e026b 100644 (file)
@@ -1,9 +1,9 @@
-//===- RaiseAllocations.cpp - Convert %malloc & %free calls to insts ------===//
+//===- RaiseAllocations.cpp - Convert @malloc & @free calls to insts ------===//
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
 #include "llvm/Support/CallSite.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/ADT/Statistic.h"
+#include <algorithm>
 using namespace llvm;
 
 STATISTIC(NumRaised, "Number of allocations raised");
 
 namespace {
-  // RaiseAllocations - Turn %malloc and %free calls into the appropriate
+  // RaiseAllocations - Turn @malloc and @free calls into the appropriate
   // instruction.
   //
   class VISIBILITY_HIDDEN RaiseAllocations : public ModulePass {
     Function *MallocFunc;   // Functions in the module we are processing
     Function *FreeFunc;     // Initialized by doPassInitializationVirt
   public:
-    RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
+    static char ID; // Pass identification, replacement for typeid
+    RaiseAllocations() 
+      : ModulePass((intptr_t)&ID), MallocFunc(0), FreeFunc(0) {}
 
     // doPassInitialization - For the raise allocations pass, this finds a
     // declaration for malloc and free if they exist.
@@ -46,6 +49,7 @@ namespace {
     bool runOnModule(Module &M);
   };
 
+  char RaiseAllocations::ID = 0;
   RegisterPass<RaiseAllocations>
   X("raiseallocs", "Raise allocations from calls to instructions");
 }  // end anonymous namespace
@@ -61,7 +65,7 @@ ModulePass *llvm::createRaiseAllocationsPass() {
 // free functions.  If this is the case, grab the method pointers that the
 // module is using.
 //
-// Lookup %malloc and %free in the symbol table, for later use.  If they don't
+// Lookup @malloc and @free in the symbol table, for later use.  If they don't
 // exist, or are not external, we do not worry about converting calls to that
 // function into the appropriate instruction.
 //
@@ -74,7 +78,7 @@ void RaiseAllocations::doInitialization(Module &M) {
 
     // Get the expected prototype for malloc
     const FunctionType *Malloc1Type = 
-      FunctionType::get(PointerType::get(Type::Int8Ty),
+      FunctionType::get(PointerType::getUnqual(Type::Int8Ty),
                       std::vector<const Type*>(1, Type::Int64Ty), false);
 
     // Chck to see if we got the expected malloc
@@ -82,14 +86,14 @@ void RaiseAllocations::doInitialization(Module &M) {
       // Check to see if the prototype is wrong, giving us sbyte*(uint) * malloc
       // This handles the common declaration of: 'void *malloc(unsigned);'
       const FunctionType *Malloc2Type = 
-        FunctionType::get(PointerType::get(Type::Int8Ty),
+        FunctionType::get(PointerType::getUnqual(Type::Int8Ty),
                           std::vector<const Type*>(1, Type::Int32Ty), false);
       if (TyWeHave != Malloc2Type) {
         // Check to see if the prototype is missing, giving us 
         // sbyte*(...) * malloc
         // This handles the common declaration of: 'void *malloc();'
         const FunctionType *Malloc3Type = 
-          FunctionType::get(PointerType::get(Type::Int8Ty),
+          FunctionType::get(PointerType::getUnqual(Type::Int8Ty),
                             std::vector<const Type*>(), true);
         if (TyWeHave != Malloc3Type)
           // Give up
@@ -104,7 +108,7 @@ void RaiseAllocations::doInitialization(Module &M) {
     
     // Get the expected prototype for void free(i8*)
     const FunctionType *Free1Type = FunctionType::get(Type::VoidTy,
-        std::vector<const Type*>(1, PointerType::get(Type::Int8Ty)), false);
+      std::vector<const Type*>(1, PointerType::getUnqual(Type::Int8Ty)), false);
 
     if (TyWeHave != Free1Type) {
       // Check to see if the prototype was forgotten, giving us 
@@ -150,7 +154,7 @@ bool RaiseAllocations::runOnModule(Module &M) {
 
       if (Instruction *I = dyn_cast<Instruction>(U)) {
         CallSite CS = CallSite::get(I);
-        if (CS.getInstruction() && CS.arg_begin() != CS.arg_end() &&
+        if (CS.getInstruction() && !CS.arg_empty() &&
             (CS.getCalledFunction() == MallocFunc ||
              std::find(EqPointers.begin(), EqPointers.end(),
                        CS.getCalledValue()) != EqPointers.end())) {
@@ -164,8 +168,8 @@ bool RaiseAllocations::runOnModule(Module &M) {
               CastInst::createIntegerCast(Source, Type::Int32Ty, false/*ZExt*/,
                                           "MallocAmtCast", I);
 
-          std::string Name(I->getName()); I->setName("");
-          MallocInst *MI = new MallocInst(Type::Int8Ty, Source, Name, I);
+          MallocInst *MI = new MallocInst(Type::Int8Ty, Source, "", I);
+          MI->takeName(I);
           I->replaceAllUsesWith(MI);
 
           // If the old instruction was an invoke, add an unconditional branch
@@ -200,8 +204,10 @@ bool RaiseAllocations::runOnModule(Module &M) {
       Users.pop_back();
 
       if (Instruction *I = dyn_cast<Instruction>(U)) {
+        if (isa<InvokeInst>(I))
+          continue;
         CallSite CS = CallSite::get(I);
-        if (CS.getInstruction() && CS.arg_begin() != CS.arg_end() &&
+        if (CS.getInstruction() && !CS.arg_empty() &&
             (CS.getCalledFunction() == FreeFunc ||
              std::find(EqPointers.begin(), EqPointers.end(),
                        CS.getCalledValue()) != EqPointers.end())) {
@@ -213,7 +219,8 @@ bool RaiseAllocations::runOnModule(Module &M) {
           //
           Value *Source = *CS.arg_begin();
           if (!isa<PointerType>(Source->getType()))
-            Source = new IntToPtrInst(Source, PointerType::get(Type::Int8Ty), 
+            Source = new IntToPtrInst(Source,           
+                                      PointerType::getUnqual(Type::Int8Ty), 
                                       "FreePtrCast", I);
           new FreeInst(Source, I);