API changes for class Use size reduction, wave 1.
[oota-llvm.git] / lib / Transforms / Utils / LowerInvoke.cpp
index a1bd065df8b18f0c9d4f576aa193fd8fef022573..7f85b10af49925501bfeb3e5cf1e667e28151103 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.
 //
 //===----------------------------------------------------------------------===//
 //
@@ -75,14 +75,15 @@ namespace {
     const TargetLowering *TLI;
     
   public:
-    LowerInvoke(const TargetLowering *tli = NULL) : TLI(tli) { }
+    static char ID; // Pass identification, replacement for typeid
+    explicit LowerInvoke(const TargetLowering *tli = NULL)
+      : FunctionPass((intptr_t)&ID), TLI(tli) { }
     bool doInitialization(Module &M);
     bool runOnFunction(Function &F);
  
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
       // This is a cluster of orthogonal Transforms
       AU.addPreservedID(PromoteMemoryToRegisterID);
-      AU.addPreservedID(LowerSelectID);
       AU.addPreservedID(LowerSwitchID);
       AU.addPreservedID(LowerAllocationsID);
     }
@@ -97,6 +98,7 @@ namespace {
     bool insertExpensiveEHSupport(Function &F);
   };
 
+  char LowerInvoke::ID = 0;
   RegisterPass<LowerInvoke>
   X("lowerinvoke", "Lower invoke and unwind, for unwindless code generators");
 }
@@ -111,7 +113,7 @@ FunctionPass *llvm::createLowerInvokePass(const TargetLowering *TLI) {
 // doInitialization - Make sure that there is a prototype for abort in the
 // current module.
 bool LowerInvoke::doInitialization(Module &M) {
-  const Type *VoidPtrTy = PointerType::get(Type::Int8Ty);
+  const Type *VoidPtrTy = PointerType::getUnqual(Type::Int8Ty);
   AbortMessage = 0;
   if (ExpensiveEHSupport) {
     // Insert a type for the linked list of jump buffers.
@@ -123,14 +125,14 @@ bool LowerInvoke::doInitialization(Module &M) {
       std::vector<const Type*> Elements;
       Elements.push_back(JmpBufTy);
       OpaqueType *OT = OpaqueType::get();
-      Elements.push_back(PointerType::get(OT));
+      Elements.push_back(PointerType::getUnqual(OT));
       PATypeHolder JBLType(StructType::get(Elements));
       OT->refineAbstractTypeTo(JBLType.get());  // Complete the cycle.
       JBLinkTy = JBLType.get();
       M.addTypeName("llvm.sjljeh.jmpbufty", JBLinkTy);
     }
 
-    const Type *PtrJBList = PointerType::get(JBLinkTy);
+    const Type *PtrJBList = PointerType::getUnqual(JBLinkTy);
 
     // Now that we've done that, insert the jmpbuf list head global, unless it
     // already exists.
@@ -141,16 +143,21 @@ bool LowerInvoke::doInitialization(Module &M) {
                                       "llvm.sjljeh.jblist", &M);
     }
     SetJmpFn = M.getOrInsertFunction("llvm.setjmp", Type::Int32Ty,
-                                     PointerType::get(JmpBufTy), (Type *)0);
+                                     PointerType::getUnqual(JmpBufTy), 
+                                     (Type *)0);
     LongJmpFn = M.getOrInsertFunction("llvm.longjmp", Type::VoidTy,
-                                      PointerType::get(JmpBufTy),
+                                      PointerType::getUnqual(JmpBufTy),
                                       Type::Int32Ty, (Type *)0);
   }
 
   // We need the 'write' and 'abort' functions for both models.
   AbortFn = M.getOrInsertFunction("abort", Type::VoidTy, (Type *)0);
+#if 0 // "write" is Unix-specific.. code is going away soon anyway.
   WriteFn = M.getOrInsertFunction("write", Type::VoidTy, Type::Int32Ty,
                                   VoidPtrTy, Type::Int32Ty, (Type *)0);
+#else
+  WriteFn = 0;
+#endif
   return true;
 }
 
@@ -185,6 +192,7 @@ void LowerInvoke::createAbortMessage(Module *M) {
 
 
 void LowerInvoke::writeAbortMessage(Instruction *IB) {
+#if 0
   if (AbortMessage == 0)
     createAbortMessage(IB->getParent()->getParent()->getParent());
 
@@ -194,6 +202,7 @@ void LowerInvoke::writeAbortMessage(Instruction *IB) {
   Args[1] = AbortMessage;
   Args[2] = ConstantInt::get(Type::Int32Ty, AbortMessageLength);
   (new CallInst(WriteFn, Args, 3, "", IB))->setTailCall();
+#endif
 }
 
 bool LowerInvoke::insertCheapEHSupport(Function &F) {
@@ -202,14 +211,15 @@ bool LowerInvoke::insertCheapEHSupport(Function &F) {
     if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
       std::vector<Value*> CallArgs(II->op_begin()+3, II->op_end());
       // Insert a normal call instruction...
-      CallInst *NewCall = new CallInst(II->getCalledValue(),
-                                       &CallArgs[0], CallArgs.size(), "", II);
+      CallInst *NewCall = CallInst::Create(II->getCalledValue(),
+                                           CallArgs.begin(), CallArgs.end(), "",II);
       NewCall->takeName(II);
       NewCall->setCallingConv(II->getCallingConv());
+      NewCall->setParamAttrs(II->getParamAttrs());
       II->replaceAllUsesWith(NewCall);
 
       // Insert an unconditional branch to the normal destination.
-      new BranchInst(II->getNormalDest(), II);
+      BranchInst::Create(II->getNormalDest(), II);
 
       // Remove any PHI node entries from the exception destination.
       II->getUnwindDest()->removePredecessor(BB);
@@ -223,12 +233,12 @@ bool LowerInvoke::insertCheapEHSupport(Function &F) {
       writeAbortMessage(UI);
 
       // Insert a call to abort()
-      (new CallInst(AbortFn, "", UI))->setTailCall();
+      CallInst::Create(AbortFn, "", UI)->setTailCall();
 
       // Insert a return instruction.  This really should be a "barrier", as it
       // is unreachable.
-      new ReturnInst(F.getReturnType() == Type::VoidTy ? 0 :
-                            Constant::getNullValue(F.getReturnType()), UI);
+      ReturnInst::Create(F.getReturnType() == Type::VoidTy ? 0 :
+                         Constant::getNullValue(F.getReturnType()), UI);
 
       // Remove the unwind instruction now.
       BB->getInstList().erase(UI);
@@ -245,6 +255,17 @@ void LowerInvoke::rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
                                          SwitchInst *CatchSwitch) {
   ConstantInt *InvokeNoC = ConstantInt::get(Type::Int32Ty, InvokeNo);
 
+  // If the unwind edge has phi nodes, split the edge.
+  if (isa<PHINode>(II->getUnwindDest()->begin())) {
+    SplitCriticalEdge(II, 1, this);
+   
+    // If there are any phi nodes left, they must have a single predecessor.
+    while (PHINode *PN = dyn_cast<PHINode>(II->getUnwindDest()->begin())) {
+      PN->replaceAllUsesWith(PN->getIncomingValue(0));
+      PN->eraseFromParent();
+    }
+  }
+  
   // Insert a store of the invoke num before the invoke and store zero into the
   // location afterward.
   new StoreInst(InvokeNoC, InvokeNum, true, II);  // volatile
@@ -259,15 +280,16 @@ void LowerInvoke::rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
   
   // Insert a normal call instruction.
   std::vector<Value*> CallArgs(II->op_begin()+3, II->op_end());
-  CallInst *NewCall = new CallInst(II->getCalledValue(),
-                                   &CallArgs[0], CallArgs.size(), "",
-                                   II);
+  CallInst *NewCall = CallInst::Create(II->getCalledValue(),
+                                       CallArgs.begin(), CallArgs.end(), "",
+                                       II);
   NewCall->takeName(II);
   NewCall->setCallingConv(II->getCallingConv());
+  NewCall->setParamAttrs(II->getParamAttrs());
   II->replaceAllUsesWith(NewCall);
   
   // Replace the invoke with an uncond branch.
-  new BranchInst(II->getNormalDest(), NewCall->getParent());
+  BranchInst::Create(II->getNormalDest(), NewCall->getParent());
   II->eraseFromParent();
 }
 
@@ -441,8 +463,8 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
     std::vector<Value*> Idx;
     Idx.push_back(Constant::getNullValue(Type::Int32Ty));
     Idx.push_back(ConstantInt::get(Type::Int32Ty, 1));
-    OldJmpBufPtr = new GetElementPtrInst(JmpBuf, &Idx[0], 2, "OldBuf",
-                                         EntryBB->getTerminator());
+    OldJmpBufPtr = GetElementPtrInst::Create(JmpBuf, Idx.begin(), Idx.end(),
+                                             "OldBuf", EntryBB->getTerminator());
 
     // Copy the JBListHead to the alloca.
     Value *OldBuf = new LoadInst(JBListHead, "oldjmpbufptr", true,
@@ -454,7 +476,7 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
 
     // Create the catch block.  The catch block is basically a big switch
     // statement that goes to all of the invoke catch blocks.
-    BasicBlock *CatchBB = new BasicBlock("setjmp.catch", &F);
+    BasicBlock *CatchBB = BasicBlock::Create("setjmp.catch", &F);
     
     // Create an alloca which keeps track of which invoke is currently
     // executing.  For normal calls it contains zero.
@@ -466,12 +488,12 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
     // Insert a load in the Catch block, and a switch on its value.  By default,
     // we go to a block that just does an unwind (which is the correct action
     // for a standard call).
-    BasicBlock *UnwindBB = new BasicBlock("unwindbb", &F);
+    BasicBlock *UnwindBB = BasicBlock::Create("unwindbb", &F);
     Unwinds.push_back(new UnwindInst(UnwindBB));
     
     Value *CatchLoad = new LoadInst(InvokeNum, "invoke.num", true, CatchBB);
-    SwitchInst *CatchSwitch = 
-      new SwitchInst(CatchLoad, UnwindBB, Invokes.size(), CatchBB);
+    SwitchInst *CatchSwitch =
+      SwitchInst::Create(CatchLoad, UnwindBB, Invokes.size(), CatchBB);
 
     // Now that things are set up, insert the setjmp call itself.
     
@@ -480,11 +502,11 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
                                                      "setjmp.cont");
 
     Idx[1] = ConstantInt::get(Type::Int32Ty, 0);
-    Value *JmpBufPtr = new GetElementPtrInst(JmpBuf, &Idx[0], Idx.size(),
-                                             "TheJmpBuf",
-                                             EntryBB->getTerminator());
-    Value *SJRet = new CallInst(SetJmpFn, JmpBufPtr, "sjret",
-                                EntryBB->getTerminator());
+    Value *JmpBufPtr = GetElementPtrInst::Create(JmpBuf, Idx.begin(), Idx.end(),
+                                                 "TheJmpBuf",
+                                                 EntryBB->getTerminator());
+    Value *SJRet = CallInst::Create(SetJmpFn, JmpBufPtr, "sjret",
+                                    EntryBB->getTerminator());
     
     // Compare the return value to zero.
     Value *IsNormal = new ICmpInst(ICmpInst::ICMP_EQ, SJRet, 
@@ -494,7 +516,7 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
     EntryBB->getTerminator()->eraseFromParent();
     
     // Put in a new condbranch in its place.
-    new BranchInst(ContBlock, CatchBB, IsNormal, EntryBB);
+    BranchInst::Create(ContBlock, CatchBB, IsNormal, EntryBB);
 
     // At this point, we are all set up, rewrite each invoke instruction.
     for (unsigned i = 0, e = Invokes.size(); i != e; ++i)
@@ -506,9 +528,9 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
   // Create three new blocks, the block to load the jmpbuf ptr and compare
   // against null, the block to do the longjmp, and the error block for if it
   // is null.  Add them at the end of the function because they are not hot.
-  BasicBlock *UnwindHandler = new BasicBlock("dounwind", &F);
-  BasicBlock *UnwindBlock = new BasicBlock("unwind", &F);
-  BasicBlock *TermBlock = new BasicBlock("unwinderror", &F);
+  BasicBlock *UnwindHandler = BasicBlock::Create("dounwind", &F);
+  BasicBlock *UnwindBlock = BasicBlock::Create("unwind", &F);
+  BasicBlock *TermBlock = BasicBlock::Create("unwinderror", &F);
 
   // If this function contains an invoke, restore the old jumpbuf ptr.
   Value *BufPtr;
@@ -524,16 +546,17 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
   Value *NotNull = new ICmpInst(ICmpInst::ICMP_NE, BufPtr, 
                                 Constant::getNullValue(BufPtr->getType()),
     "notnull", UnwindHandler);
-  new BranchInst(UnwindBlock, TermBlock, NotNull, UnwindHandler);
+  BranchInst::Create(UnwindBlock, TermBlock, NotNull, UnwindHandler);
   
   // Create the block to do the longjmp.
   // Get a pointer to the jmpbuf and longjmp.
   std::vector<Value*> Idx;
   Idx.push_back(Constant::getNullValue(Type::Int32Ty));
   Idx.push_back(ConstantInt::get(Type::Int32Ty, 0));
-  Idx[0] = new GetElementPtrInst(BufPtr, &Idx[0], 2, "JmpBuf", UnwindBlock);
+  Idx[0] = GetElementPtrInst::Create(BufPtr, Idx.begin(), Idx.end(), "JmpBuf",
+                                     UnwindBlock);
   Idx[1] = ConstantInt::get(Type::Int32Ty, 1);
-  new CallInst(LongJmpFn, &Idx[0], Idx.size(), "", UnwindBlock);
+  CallInst::Create(LongJmpFn, Idx.begin(), Idx.end(), "", UnwindBlock);
   new UnreachableInst(UnwindBlock);
   
   // Set up the term block ("throw without a catch").
@@ -543,13 +566,13 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
   writeAbortMessage(TermBlock->getTerminator());
   
   // Insert a call to abort()
-  (new CallInst(AbortFn, "",
-                TermBlock->getTerminator()))->setTailCall();
+  CallInst::Create(AbortFn, "",
+                   TermBlock->getTerminator())->setTailCall();
     
   
   // Replace all unwinds with a branch to the unwind handler.
   for (unsigned i = 0, e = Unwinds.size(); i != e; ++i) {
-    new BranchInst(UnwindHandler, Unwinds[i]);
+    BranchInst::Create(UnwindHandler, Unwinds[i]);
     Unwinds[i]->eraseFromParent();    
   }