Add a clear() method to PriorityQueue.
[oota-llvm.git] / lib / Transforms / IPO / RaiseAllocations.cpp
index ed4a3769dbebe9d99e1fb3671793e7b0d8db6637..daf8ef00053ef22a69f8d2cc97609ed694093675 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     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.
 //
 //===----------------------------------------------------------------------===//
 //
@@ -48,12 +48,11 @@ namespace {
     //
     bool runOnModule(Module &M);
   };
-
-  char RaiseAllocations::ID = 0;
-  RegisterPass<RaiseAllocations>
-  X("raiseallocs", "Raise allocations from calls to instructions");
 }  // end anonymous namespace
 
+char RaiseAllocations::ID = 0;
+static RegisterPass<RaiseAllocations>
+X("raiseallocs", "Raise allocations from calls to instructions");
 
 // createRaiseAllocationsPass - The interface to this file...
 ModulePass *llvm::createRaiseAllocationsPass() {
@@ -78,7 +77,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
@@ -86,14 +85,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
@@ -108,7 +107,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 
@@ -154,7 +153,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())) {
@@ -165,7 +164,7 @@ bool RaiseAllocations::runOnModule(Module &M) {
           // source size.
           if (Source->getType() != Type::Int32Ty)
             Source = 
-              CastInst::createIntegerCast(Source, Type::Int32Ty, false/*ZExt*/,
+              CastInst::CreateIntegerCast(Source, Type::Int32Ty, false/*ZExt*/,
                                           "MallocAmtCast", I);
 
           MallocInst *MI = new MallocInst(Type::Int8Ty, Source, "", I);
@@ -175,10 +174,10 @@ bool RaiseAllocations::runOnModule(Module &M) {
           // If the old instruction was an invoke, add an unconditional branch
           // before the invoke, which will become the new terminator.
           if (InvokeInst *II = dyn_cast<InvokeInst>(I))
-            new BranchInst(II->getNormalDest(), I);
+            BranchInst::Create(II->getNormalDest(), I);
 
           // Delete the old call site
-          MI->getParent()->getInstList().erase(I);
+          I->eraseFromParent();
           Changed = true;
           ++NumRaised;
         }
@@ -204,8 +203,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())) {
@@ -217,14 +218,15 @@ 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);
 
           // If the old instruction was an invoke, add an unconditional branch
           // before the invoke, which will become the new terminator.
           if (InvokeInst *II = dyn_cast<InvokeInst>(I))
-            new BranchInst(II->getNormalDest(), I);
+            BranchInst::Create(II->getNormalDest(), I);
 
           // Delete the old call site
           if (I->getType() != Type::VoidTy)