Add a clear() method to PriorityQueue.
[oota-llvm.git] / lib / Transforms / IPO / LowerSetJmp.cpp
index 010bc592afc702db712fc7a08aeac07281d8ccda..80158b43a5c0bf043d925092f9ea0d044c2daf95 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.
 //
 //===----------------------------------------------------------------------===//
 //
@@ -49,6 +49,8 @@
 #include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/VectorExtras.h"
+#include "llvm/ADT/SmallVector.h"
+#include <map>
 using namespace llvm;
 
 STATISTIC(LongJmpsTransformed, "Number of longjmps transformed");
@@ -109,7 +111,7 @@ namespace {
 
     bool IsTransformableFunction(const std::string& Name);
   public:
-    static const int ID; // Pass identifcation, replacement for typeid
+    static char ID; // Pass identification, replacement for typeid
     LowerSetJmp() : ModulePass((intptr_t)&ID) {}
 
     void visitCallInst(CallInst& CI);
@@ -120,11 +122,11 @@ namespace {
     bool runOnModule(Module& M);
     bool doInitialization(Module& M);
   };
-
-  const int LowerSetJmp::ID = 0;
-  RegisterPass<LowerSetJmp> X("lowersetjmp", "Lower Set Jump");
 } // end anonymous namespace
 
+char LowerSetJmp::ID = 0;
+static RegisterPass<LowerSetJmp> X("lowersetjmp", "Lower Set Jump");
+
 // run - Run the transformation on the program. We grab the function
 // prototypes for longjmp and setjmp. If they are used in the program,
 // then we can go directly to the places they're at and transform them.
@@ -198,8 +200,8 @@ bool LowerSetJmp::runOnModule(Module& M) {
 // This function is always successful, unless it isn't.
 bool LowerSetJmp::doInitialization(Module& M)
 {
-  const Type *SBPTy = PointerType::get(Type::Int8Ty);
-  const Type *SBPPTy = PointerType::get(SBPTy);
+  const Type *SBPTy = PointerType::getUnqual(Type::Int8Ty);
+  const Type *SBPPTy = PointerType::getUnqual(SBPTy);
 
   // N.B. See llvm/runtime/GCCLibraries/libexception/SJLJ-Exception.h for
   // a description of the following library functions.
@@ -255,7 +257,7 @@ bool LowerSetJmp::IsTransformableFunction(const std::string& Name) {
 // throwing the exception for us.
 void LowerSetJmp::TransformLongJmpCall(CallInst* Inst)
 {
-  const Type* SBPTy = PointerType::get(Type::Int8Ty);
+  const Type* SBPTy = PointerType::getUnqual(Type::Int8Ty);
 
   // Create the call to "__llvm_sjljeh_throw_longjmp". This takes the
   // same parameters as "longjmp", except that the buffer is cast to a
@@ -263,7 +265,10 @@ void LowerSetJmp::TransformLongJmpCall(CallInst* Inst)
   // Inst's uses and doesn't get a name.
   CastInst* CI = 
     new BitCastInst(Inst->getOperand(1), SBPTy, "LJBuf", Inst);
-  new CallInst(ThrowLongJmp, CI, Inst->getOperand(2), "", Inst);
+  SmallVector<Value *, 2> Args;
+  Args.push_back(CI);
+  Args.push_back(Inst->getOperand(2));
+  CallInst::Create(ThrowLongJmp, Args.begin(), Args.end(), "", Inst);
 
   SwitchValuePair& SVP = SwitchValMap[Inst->getParent()->getParent()];
 
@@ -271,7 +276,7 @@ void LowerSetJmp::TransformLongJmpCall(CallInst* Inst)
   // we should branch to the basic block that determines if this longjmp
   // is applicable here. Otherwise, issue an unwind.
   if (SVP.first)
-    new BranchInst(SVP.first->getParent(), Inst);
+    BranchInst::Create(SVP.first->getParent(), Inst);
   else
     new UnwindInst(Inst);
 
@@ -304,9 +309,9 @@ AllocaInst* LowerSetJmp::GetSetJmpMap(Function* Func)
   assert(Inst && "Couldn't find even ONE instruction in entry block!");
 
   // Fill in the alloca and call to initialize the SJ map.
-  const Type *SBPTy = PointerType::get(Type::Int8Ty);
+  const Type *SBPTy = PointerType::getUnqual(Type::Int8Ty);
   AllocaInst* Map = new AllocaInst(SBPTy, 0, "SJMap", Inst);
-  new CallInst(InitSJMap, Map, "", Inst);
+  CallInst::Create(InitSJMap, Map, "", Inst);
   return SJMap[Func] = Map;
 }
 
@@ -319,7 +324,7 @@ BasicBlock* LowerSetJmp::GetRethrowBB(Function* Func)
 
   // The basic block we're going to jump to if we need to rethrow the
   // exception.
-  BasicBlock* Rethrow = new BasicBlock("RethrowExcept", Func);
+  BasicBlock* Rethrow = BasicBlock::Create("RethrowExcept", Func);
 
   // Fill in the "Rethrow" BB with a call to rethrow the exception. This
   // is the last instruction in the BB since at this point the runtime
@@ -335,32 +340,28 @@ LowerSetJmp::SwitchValuePair LowerSetJmp::GetSJSwitch(Function* Func,
 {
   if (SwitchValMap[Func].first) return SwitchValMap[Func];
 
-  BasicBlock* LongJmpPre = new BasicBlock("LongJmpBlkPre", Func);
-  BasicBlock::InstListType& LongJmpPreIL = LongJmpPre->getInstList();
+  BasicBlock* LongJmpPre = BasicBlock::Create("LongJmpBlkPre", Func);
 
   // Keep track of the preliminary basic block for some of the other
   // transformations.
   PrelimBBMap[Func] = LongJmpPre;
 
   // Grab the exception.
-  CallInst* Cond = new CallInst(IsLJException, "IsLJExcept");
-  LongJmpPreIL.push_back(Cond);
+  CallInst* Cond = CallInst::Create(IsLJException, "IsLJExcept", LongJmpPre);
 
   // The "decision basic block" gets the number associated with the
   // setjmp call returning to switch on and the value returned by
   // longjmp.
-  BasicBlock* DecisionBB = new BasicBlock("LJDecisionBB", Func);
-  BasicBlock::InstListType& DecisionBBIL = DecisionBB->getInstList();
+  BasicBlock* DecisionBB = BasicBlock::Create("LJDecisionBB", Func);
 
-  new BranchInst(DecisionBB, Rethrow, Cond, LongJmpPre);
+  BranchInst::Create(DecisionBB, Rethrow, Cond, LongJmpPre);
 
   // Fill in the "decision" basic block.
-  CallInst* LJVal = new CallInst(GetLJValue, "LJVal");
-  DecisionBBIL.push_back(LJVal);
-  CallInst* SJNum = new CallInst(TryCatchLJ, GetSetJmpMap(Func), "SJNum");
-  DecisionBBIL.push_back(SJNum);
+  CallInst* LJVal = CallInst::Create(GetLJValue, "LJVal", DecisionBB);
+  CallInst* SJNum = CallInst::Create(TryCatchLJ, GetSetJmpMap(Func), "SJNum",
+                                     DecisionBB);
 
-  SwitchInst* SI = new SwitchInst(SJNum, Rethrow, 0, DecisionBB);
+  SwitchInst* SI = SwitchInst::Create(SJNum, Rethrow, 0, DecisionBB);
   return SwitchValMap[Func] = SwitchValuePair(SI, LJVal);
 }
 
@@ -374,14 +375,14 @@ void LowerSetJmp::TransformSetJmpCall(CallInst* Inst)
   Function* Func = ABlock->getParent();
 
   // Add this setjmp to the setjmp map.
-  const Type* SBPTy = PointerType::get(Type::Int8Ty);
+  const Type* SBPTy = PointerType::getUnqual(Type::Int8Ty);
   CastInst* BufPtr = 
     new BitCastInst(Inst->getOperand(1), SBPTy, "SBJmpBuf", Inst);
   std::vector<Value*> Args = 
     make_vector<Value*>(GetSetJmpMap(Func), BufPtr,
                         ConstantInt::get(Type::Int32Ty,
                                          SetJmpIDMap[Func]++), 0);
-  new CallInst(AddSJToMap, &Args[0], Args.size(), "", Inst);
+  CallInst::Create(AddSJToMap, Args.begin(), Args.end(), "", Inst);
 
   // We are guaranteed that there are no values live across basic blocks
   // (because we are "not in SSA form" yet), but there can still be values live
@@ -423,7 +424,7 @@ void LowerSetJmp::TransformSetJmpCall(CallInst* Inst)
 
   // This PHI node will be in the new block created from the
   // splitBasicBlock call.
-  PHINode* PHI = new PHINode(Type::Int32Ty, "SetJmpReturn", Inst);
+  PHINode* PHI = PHINode::Create(Type::Int32Ty, "SetJmpReturn", Inst);
 
   // Coming from a call to setjmp, the return is 0.
   PHI->addIncoming(ConstantInt::getNullValue(Type::Int32Ty), ABlock);
@@ -439,7 +440,7 @@ void LowerSetJmp::TransformSetJmpCall(CallInst* Inst)
   // Replace all uses of this instruction with the PHI node created by
   // the eradication of setjmp.
   Inst->replaceAllUsesWith(PHI);
-  Inst->getParent()->getInstList().erase(Inst);
+  Inst->eraseFromParent();
 
   ++SetJmpsTransformed;
 }
@@ -469,16 +470,18 @@ void LowerSetJmp::visitCallInst(CallInst& CI)
   // Construct the new "invoke" instruction.
   TerminatorInst* Term = OldBB->getTerminator();
   std::vector<Value*> Params(CI.op_begin() + 1, CI.op_end());
-  InvokeInst* II = new
-    InvokeInst(CI.getCalledValue(), NewBB, PrelimBBMap[Func],
-               &Params[0], Params.size(), CI.getName(), Term);
+  InvokeInst* II =
+    InvokeInst::Create(CI.getCalledValue(), NewBB, PrelimBBMap[Func],
+                       Params.begin(), Params.end(), CI.getName(), Term);
+  II->setCallingConv(CI.getCallingConv());
+  II->setParamAttrs(CI.getParamAttrs());
 
   // Replace the old call inst with the invoke inst and remove the call.
   CI.replaceAllUsesWith(II);
-  CI.getParent()->getInstList().erase(&CI);
+  CI.eraseFromParent();
 
   // The old terminator is useless now that we have the invoke inst.
-  Term->getParent()->getInstList().erase(Term);
+  Term->eraseFromParent();
   ++CallsTransformed;
 }
 
@@ -500,15 +503,14 @@ void LowerSetJmp::visitInvokeInst(InvokeInst& II)
   BasicBlock* ExceptBB = II.getUnwindDest();
 
   Function* Func = BB->getParent();
-  BasicBlock* NewExceptBB = new BasicBlock("InvokeExcept", Func);
-  BasicBlock::InstListType& InstList = NewExceptBB->getInstList();
+  BasicBlock* NewExceptBB = BasicBlock::Create("InvokeExcept", Func);
 
   // If this is a longjmp exception, then branch to the preliminary BB of
   // the longjmp exception handling. Otherwise, go to the old exception.
-  CallInst* IsLJExcept = new CallInst(IsLJException, "IsLJExcept");
-  InstList.push_back(IsLJExcept);
+  CallInst* IsLJExcept = CallInst::Create(IsLJException, "IsLJExcept",
+                                          NewExceptBB);
 
-  new BranchInst(PrelimBBMap[Func], ExceptBB, IsLJExcept, NewExceptBB);
+  BranchInst::Create(PrelimBBMap[Func], ExceptBB, IsLJExcept, NewExceptBB);
 
   II.setUnwindDest(NewExceptBB);
   ++InvokesTransformed;
@@ -518,14 +520,14 @@ void LowerSetJmp::visitInvokeInst(InvokeInst& II)
 // function.
 void LowerSetJmp::visitReturnInst(ReturnInst &RI) {
   Function* Func = RI.getParent()->getParent();
-  new CallInst(DestroySJMap, GetSetJmpMap(Func), "", &RI);
+  CallInst::Create(DestroySJMap, GetSetJmpMap(Func), "", &RI);
 }
 
 // visitUnwindInst - We want to destroy the setjmp map upon exit from the
 // function.
 void LowerSetJmp::visitUnwindInst(UnwindInst &UI) {
   Function* Func = UI.getParent()->getParent();
-  new CallInst(DestroySJMap, GetSetJmpMap(Func), "", &UI);
+  CallInst::Create(DestroySJMap, GetSetJmpMap(Func), "", &UI);
 }
 
 ModulePass *llvm::createLowerSetJmpPass() {