Change the PointerType api for creating pointer types. The old functionality of Point...
[oota-llvm.git] / lib / Transforms / IPO / LowerSetJmp.cpp
index cd46718ba6c1227a2924a570d9c571f4141c1e9f..389ddebc6a0fc52166490b751375980f09f0e3a8 100644 (file)
@@ -33,6 +33,7 @@
 // pass invokable via the "opt" command at will.
 //===----------------------------------------------------------------------===//
 
+#define DEBUG_TYPE "lowersetjmp"
 #include "llvm/Transforms/IPO.h"
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Module.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/CFG.h"
+#include "llvm/Support/Compiler.h"
 #include "llvm/Support/InstVisitor.h"
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/ADT/DepthFirstIterator.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/VectorExtras.h"
+#include "llvm/ADT/SmallVector.h"
 using namespace llvm;
 
-namespace {
-  Statistic<> LongJmpsTransformed("lowersetjmp",
-                                  "Number of longjmps transformed");
-  Statistic<> SetJmpsTransformed("lowersetjmp",
-                                 "Number of setjmps transformed");
-  Statistic<> CallsTransformed("lowersetjmp",
-                               "Number of calls invokified");
-  Statistic<> InvokesTransformed("lowersetjmp",
-                                 "Number of invokes modified");
+STATISTIC(LongJmpsTransformed, "Number of longjmps transformed");
+STATISTIC(SetJmpsTransformed , "Number of setjmps transformed");
+STATISTIC(CallsTransformed   , "Number of calls invokified");
+STATISTIC(InvokesTransformed , "Number of invokes modified");
 
+namespace {
   //===--------------------------------------------------------------------===//
   // LowerSetJmp pass implementation.
-  class LowerSetJmp : public ModulePass,
+  class VISIBILITY_HIDDEN LowerSetJmp : public ModulePass,
                       public InstVisitor<LowerSetJmp> {
     // LLVM library functions...
-    Function* InitSJMap;        // __llvm_sjljeh_init_setjmpmap
-    Function* DestroySJMap;     // __llvm_sjljeh_destroy_setjmpmap
-    Function* AddSJToMap;       // __llvm_sjljeh_add_setjmp_to_map
-    Function* ThrowLongJmp;     // __llvm_sjljeh_throw_longjmp
-    Function* TryCatchLJ;       // __llvm_sjljeh_try_catching_longjmp_exception
-    Function* IsLJException;    // __llvm_sjljeh_is_longjmp_exception
-    Function* GetLJValue;       // __llvm_sjljeh_get_longjmp_value
+    Constant *InitSJMap;        // __llvm_sjljeh_init_setjmpmap
+    Constant *DestroySJMap;     // __llvm_sjljeh_destroy_setjmpmap
+    Constant *AddSJToMap;       // __llvm_sjljeh_add_setjmp_to_map
+    Constant *ThrowLongJmp;     // __llvm_sjljeh_throw_longjmp
+    Constant *TryCatchLJ;       // __llvm_sjljeh_try_catching_longjmp_exception
+    Constant *IsLJException;    // __llvm_sjljeh_is_longjmp_exception
+    Constant *GetLJValue;       // __llvm_sjljeh_get_longjmp_value
 
     typedef std::pair<SwitchInst*, CallInst*> SwitchValuePair;
 
@@ -111,6 +110,9 @@ namespace {
 
     bool IsTransformableFunction(const std::string& Name);
   public:
+    static char ID; // Pass identification, replacement for typeid
+    LowerSetJmp() : ModulePass((intptr_t)&ID) {}
+
     void visitCallInst(CallInst& CI);
     void visitInvokeInst(InvokeInst& II);
     void visitReturnInst(ReturnInst& RI);
@@ -120,6 +122,7 @@ namespace {
     bool doInitialization(Module& M);
   };
 
+  char LowerSetJmp::ID = 0;
   RegisterPass<LowerSetJmp> X("lowersetjmp", "Lower Set Jump");
 } // end anonymous namespace
 
@@ -130,8 +133,8 @@ bool LowerSetJmp::runOnModule(Module& M) {
   bool Changed = false;
 
   // These are what the functions are called.
-  Function* SetJmp = M.getNamedFunction("llvm.setjmp");
-  Function* LongJmp = M.getNamedFunction("llvm.longjmp");
+  Function* SetJmp = M.getFunction("llvm.setjmp");
+  Function* LongJmp = M.getFunction("llvm.longjmp");
 
   // This program doesn't have longjmp and setjmp calls.
   if ((!LongJmp || LongJmp->use_empty()) &&
@@ -196,8 +199,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::SByteTy);
-  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.
@@ -212,25 +215,25 @@ bool LowerSetJmp::doInitialization(Module& M)
   // void __llvm_sjljeh_add_setjmp_to_map(void**, void*, unsigned)
   AddSJToMap = M.getOrInsertFunction("__llvm_sjljeh_add_setjmp_to_map",
                                      Type::VoidTy, SBPPTy, SBPTy,
-                                     Type::UIntTy, (Type *)0);
+                                     Type::Int32Ty, (Type *)0);
 
   // void __llvm_sjljeh_throw_longjmp(int*, int)
   ThrowLongJmp = M.getOrInsertFunction("__llvm_sjljeh_throw_longjmp",
-                                       Type::VoidTy, SBPTy, Type::IntTy,
+                                       Type::VoidTy, SBPTy, Type::Int32Ty,
                                        (Type *)0);
 
   // unsigned __llvm_sjljeh_try_catching_longjmp_exception(void **)
   TryCatchLJ =
     M.getOrInsertFunction("__llvm_sjljeh_try_catching_longjmp_exception",
-                          Type::UIntTy, SBPPTy, (Type *)0);
+                          Type::Int32Ty, SBPPTy, (Type *)0);
 
   // bool __llvm_sjljeh_is_longjmp_exception()
   IsLJException = M.getOrInsertFunction("__llvm_sjljeh_is_longjmp_exception",
-                                        Type::BoolTy, (Type *)0);
+                                        Type::Int1Ty, (Type *)0);
 
   // int __llvm_sjljeh_get_longjmp_value()
   GetLJValue = M.getOrInsertFunction("__llvm_sjljeh_get_longjmp_value",
-                                     Type::IntTy, (Type *)0);
+                                     Type::Int32Ty, (Type *)0);
   return true;
 }
 
@@ -253,15 +256,18 @@ bool LowerSetJmp::IsTransformableFunction(const std::string& Name) {
 // throwing the exception for us.
 void LowerSetJmp::TransformLongJmpCall(CallInst* Inst)
 {
-  const Type* SBPTy = PointerType::get(Type::SByteTy);
+  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
   // char*. It returns "void", so it doesn't need to replace any of
   // Inst's uses and doesn't get a name.
-  CastInst* CI = new CastInst(Inst->getOperand(1), SBPTy, "LJBuf", Inst);
-  new CallInst(ThrowLongJmp, make_vector<Value*>(CI, Inst->getOperand(2), 0),
-               "", Inst);
+  CastInst* CI = 
+    new BitCastInst(Inst->getOperand(1), SBPTy, "LJBuf", Inst);
+  SmallVector<Value *, 2> Args;
+  Args.push_back(CI);
+  Args.push_back(Inst->getOperand(2));
+  new CallInst(ThrowLongJmp, Args.begin(), Args.end(), "", Inst);
 
   SwitchValuePair& SVP = SwitchValMap[Inst->getParent()->getParent()];
 
@@ -302,9 +308,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::SByteTy);
+  const Type *SBPTy = PointerType::getUnqual(Type::Int8Ty);
   AllocaInst* Map = new AllocaInst(SBPTy, 0, "SJMap", Inst);
-  new CallInst(InitSJMap, make_vector<Value*>(Map, 0), "", Inst);
+  new CallInst(InitSJMap, Map, "", Inst);
   return SJMap[Func] = Map;
 }
 
@@ -341,8 +347,7 @@ LowerSetJmp::SwitchValuePair LowerSetJmp::GetSJSwitch(Function* Func,
   PrelimBBMap[Func] = LongJmpPre;
 
   // Grab the exception.
-  CallInst* Cond = new
-    CallInst(IsLJException, std::vector<Value*>(), "IsLJExcept");
+  CallInst* Cond = new CallInst(IsLJException, "IsLJExcept");
   LongJmpPreIL.push_back(Cond);
 
   // The "decision basic block" gets the number associated with the
@@ -354,10 +359,9 @@ LowerSetJmp::SwitchValuePair LowerSetJmp::GetSJSwitch(Function* Func,
   new BranchInst(DecisionBB, Rethrow, Cond, LongJmpPre);
 
   // Fill in the "decision" basic block.
-  CallInst* LJVal = new CallInst(GetLJValue, std::vector<Value*>(), "LJVal");
+  CallInst* LJVal = new CallInst(GetLJValue, "LJVal");
   DecisionBBIL.push_back(LJVal);
-  CallInst* SJNum = new
-    CallInst(TryCatchLJ, make_vector<Value*>(GetSetJmpMap(Func), 0), "SJNum");
+  CallInst* SJNum = new CallInst(TryCatchLJ, GetSetJmpMap(Func), "SJNum");
   DecisionBBIL.push_back(SJNum);
 
   SwitchInst* SI = new SwitchInst(SJNum, Rethrow, 0, DecisionBB);
@@ -374,13 +378,14 @@ void LowerSetJmp::TransformSetJmpCall(CallInst* Inst)
   Function* Func = ABlock->getParent();
 
   // Add this setjmp to the setjmp map.
-  const Type* SBPTy = PointerType::get(Type::SByteTy);
-  CastInst* BufPtr = new CastInst(Inst->getOperand(1), SBPTy, "SBJmpBuf", Inst);
-  new CallInst(AddSJToMap,
-               make_vector<Value*>(GetSetJmpMap(Func), BufPtr,
-                                   ConstantInt::get(Type::UIntTy,
-                                                     SetJmpIDMap[Func]++), 0),
-               "", Inst);
+  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.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
@@ -422,14 +427,14 @@ void LowerSetJmp::TransformSetJmpCall(CallInst* Inst)
 
   // This PHI node will be in the new block created from the
   // splitBasicBlock call.
-  PHINode* PHI = new PHINode(Type::IntTy, "SetJmpReturn", Inst);
+  PHINode* PHI = new PHINode(Type::Int32Ty, "SetJmpReturn", Inst);
 
   // Coming from a call to setjmp, the return is 0.
-  PHI->addIncoming(ConstantInt::getNullValue(Type::IntTy), ABlock);
+  PHI->addIncoming(ConstantInt::getNullValue(Type::Int32Ty), ABlock);
 
   // Add the case for this setjmp's number...
   SwitchValuePair SVP = GetSJSwitch(Func, GetRethrowBB(Func));
-  SVP.first->addCase(ConstantInt::get(Type::UIntTy, SetJmpIDMap[Func] - 1),
+  SVP.first->addCase(ConstantInt::get(Type::Int32Ty, SetJmpIDMap[Func] - 1),
                      SetJmpContBlock);
 
   // Value coming from the handling of the exception.
@@ -470,7 +475,9 @@ void LowerSetJmp::visitCallInst(CallInst& CI)
   std::vector<Value*> Params(CI.op_begin() + 1, CI.op_end());
   InvokeInst* II = new
     InvokeInst(CI.getCalledValue(), NewBB, PrelimBBMap[Func],
-               Params, CI.getName(), Term);
+               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);
@@ -496,7 +503,6 @@ void LowerSetJmp::visitInvokeInst(InvokeInst& II)
   // If not reachable from a setjmp call, don't transform.
   if (!DFSBlocks.count(BB)) return;
 
-  BasicBlock* NormalBB = II.getNormalDest();
   BasicBlock* ExceptBB = II.getUnwindDest();
 
   Function* Func = BB->getParent();
@@ -505,8 +511,7 @@ void LowerSetJmp::visitInvokeInst(InvokeInst& II)
 
   // 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, std::vector<Value*>(), "IsLJExcept");
+  CallInst* IsLJExcept = new CallInst(IsLJException, "IsLJExcept");
   InstList.push_back(IsLJExcept);
 
   new BranchInst(PrelimBBMap[Func], ExceptBB, IsLJExcept, NewExceptBB);
@@ -519,16 +524,14 @@ void LowerSetJmp::visitInvokeInst(InvokeInst& II)
 // function.
 void LowerSetJmp::visitReturnInst(ReturnInst &RI) {
   Function* Func = RI.getParent()->getParent();
-  new CallInst(DestroySJMap, make_vector<Value*>(GetSetJmpMap(Func), 0),
-               "", &RI);
+  new CallInst(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, make_vector<Value*>(GetSetJmpMap(Func), 0),
-               "", &UI);
+  new CallInst(DestroySJMap, GetSetJmpMap(Func), "", &UI);
 }
 
 ModulePass *llvm::createLowerSetJmpPass() {