Filter nested structs
[oota-llvm.git] / lib / Transforms / Utils / LowerInvoke.cpp
index 2b0e16c17ba77734b0612f16dd7ba13a12e7eb18..7f0ef85d75ffcabca44bc258846cea4bf58c2145 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.
 //
 //===----------------------------------------------------------------------===//
 //
@@ -34,6 +34,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#define DEBUG_TYPE "lowerinvoke"
 #include "llvm/Transforms/Scalar.h"
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Compiler.h"
+#include "llvm/Target/TargetLowering.h"
 #include <csetjmp>
+#include <set>
 using namespace llvm;
 
-namespace {
-  Statistic<> NumInvokes("lowerinvoke", "Number of invokes replaced");
-  Statistic<> NumUnwinds("lowerinvoke", "Number of unwinds replaced");
-  Statistic<> NumSpilled("lowerinvoke",
-                         "Number of registers live across unwind edges");
-  cl::opt<bool> ExpensiveEHSupport("enable-correct-eh-support",
+STATISTIC(NumInvokes, "Number of invokes replaced");
+STATISTIC(NumUnwinds, "Number of unwinds replaced");
+STATISTIC(NumSpilled, "Number of registers live across unwind edges");
+
+static cl::opt<bool> ExpensiveEHSupport("enable-correct-eh-support",
  cl::desc("Make the -lowerinvoke pass insert expensive, but correct, EH code"));
 
-  class LowerInvoke : public FunctionPass {
+namespace {
+  class VISIBILITY_HIDDEN LowerInvoke : public FunctionPass {
     // Used for both models.
-    Function *WriteFn;
-    Function *AbortFn;
+    Constant *WriteFn;
+    Constant *AbortFn;
     Value *AbortMessage;
     unsigned AbortMessageLength;
 
     // Used for expensive EH support.
     const Type *JBLinkTy;
     GlobalVariable *JBListHead;
-    Function *SetJmpFn, *LongJmpFn;
+    Constant *SetJmpFn, *LongJmpFn;
+    
+    // We peek in TLI to grab the target's jmp_buf size and alignment
+    const TargetLowering *TLI;
+    
   public:
+    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(LowerSwitchID);
+      AU.addPreservedID(LowerAllocationsID);
+    }
+       
   private:
-    void createAbortMessage();
+    void createAbortMessage(Module *M);
     void writeAbortMessage(Instruction *IB);
     bool insertCheapEHSupport(Function &F);
     void splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes);
@@ -80,81 +98,70 @@ namespace {
     bool insertExpensiveEHSupport(Function &F);
   };
 
-  RegisterOpt<LowerInvoke>
+  char LowerInvoke::ID = 0;
+  RegisterPass<LowerInvoke>
   X("lowerinvoke", "Lower invoke and unwind, for unwindless code generators");
 }
 
 const PassInfo *llvm::LowerInvokePassID = X.getPassInfo();
 
 // Public Interface To the LowerInvoke pass.
-FunctionPass *llvm::createLowerInvokePass() { return new LowerInvoke(); }
+FunctionPass *llvm::createLowerInvokePass(const TargetLowering *TLI) { 
+  return new LowerInvoke(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::SByteTy);
+  const Type *VoidPtrTy = PointerType::getUnqual(Type::Int8Ty);
   AbortMessage = 0;
   if (ExpensiveEHSupport) {
-    // Insert a type for the linked list of jump buffers.  Unfortunately, we
-    // don't know the size of the target's setjmp buffer, so we make a guess.
-    // If this guess turns out to be too small, bad stuff could happen.
-    unsigned JmpBufSize = 200;  // PPC has 192 words
-    assert(sizeof(jmp_buf) <= JmpBufSize*sizeof(void*) &&
-       "LowerInvoke doesn't know about targets with jmp_buf size > 200 words!");
-    const Type *JmpBufTy = ArrayType::get(VoidPtrTy, JmpBufSize);
+    // Insert a type for the linked list of jump buffers.
+    unsigned JBSize = TLI ? TLI->getJumpBufSize() : 0;
+    JBSize = JBSize ? JBSize : 200;
+    const Type *JmpBufTy = ArrayType::get(VoidPtrTy, JBSize);
 
     { // The type is recursive, so use a type holder.
       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.
-    if (!(JBListHead = M.getGlobalVariable("llvm.sjljeh.jblist", PtrJBList)))
+    if (!(JBListHead = M.getGlobalVariable("llvm.sjljeh.jblist", PtrJBList))) {
       JBListHead = new GlobalVariable(PtrJBList, false,
                                       GlobalValue::LinkOnceLinkage,
                                       Constant::getNullValue(PtrJBList),
                                       "llvm.sjljeh.jblist", &M);
-    SetJmpFn = M.getOrInsertFunction("llvm.setjmp", Type::IntTy,
-                                     PointerType::get(JmpBufTy), NULL);
+    }
+    SetJmpFn = M.getOrInsertFunction("llvm.setjmp", Type::Int32Ty,
+                                     PointerType::getUnqual(JmpBufTy), 
+                                     (Type *)0);
     LongJmpFn = M.getOrInsertFunction("llvm.longjmp", Type::VoidTy,
-                                      PointerType::get(JmpBufTy),
-                                      Type::IntTy, NULL);
+                                      PointerType::getUnqual(JmpBufTy),
+                                      Type::Int32Ty, (Type *)0);
   }
 
   // We need the 'write' and 'abort' functions for both models.
-  AbortFn = M.getOrInsertFunction("abort", Type::VoidTy, NULL);
-
-  // Unfortunately, 'write' can end up being prototyped in several different
-  // ways.  If the user defines a three (or more) operand function named 'write'
-  // we will use their prototype.  We _do not_ want to insert another instance
-  // of a write prototype, because we don't know that the funcresolve pass will
-  // run after us.  If there is a definition of a write function, but it's not
-  // suitable for our uses, we just don't emit write calls.  If there is no
-  // write prototype at all, we just add one.
-  if (Function *WF = M.getNamedFunction("write")) {
-    if (WF->getFunctionType()->getNumParams() > 3 ||
-        WF->getFunctionType()->isVarArg())
-      WriteFn = WF;
-    else
-      WriteFn = 0;
-  } else {
-    WriteFn = M.getOrInsertFunction("write", Type::VoidTy, Type::IntTy,
-                                    VoidPtrTy, Type::IntTy, NULL);
-  }
+  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;
 }
 
-void LowerInvoke::createAbortMessage() {
-  Module &M = *WriteFn->getParent();
+void LowerInvoke::createAbortMessage(Module *M) {
   if (ExpensiveEHSupport) {
     // The abort message for expensive EH support tells the user that the
     // program 'unwound' without an 'invoke' instruction.
@@ -164,9 +171,9 @@ void LowerInvoke::createAbortMessage() {
 
     GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true,
                                                GlobalValue::InternalLinkage,
-                                               Msg, "abortmsg", &M);
-    std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::IntTy));
-    AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, GEPIdx);
+                                               Msg, "abortmsg", M);
+    std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::Int32Ty));
+    AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, &GEPIdx[0], 2);
   } else {
     // The abort message for cheap EH support tells the user that EH is not
     // enabled.
@@ -177,46 +184,38 @@ void LowerInvoke::createAbortMessage() {
 
     GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true,
                                                GlobalValue::InternalLinkage,
-                                               Msg, "abortmsg", &M);
-    std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::IntTy));
-    AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, GEPIdx);
+                                               Msg, "abortmsg", M);
+    std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::Int32Ty));
+    AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, &GEPIdx[0], 2);
   }
 }
 
 
 void LowerInvoke::writeAbortMessage(Instruction *IB) {
-  if (WriteFn) {
-    if (AbortMessage == 0) createAbortMessage();
-
-    // These are the arguments we WANT...
-    std::vector<Value*> Args;
-    Args.push_back(ConstantInt::get(Type::IntTy, 2));
-    Args.push_back(AbortMessage);
-    Args.push_back(ConstantInt::get(Type::IntTy, AbortMessageLength));
-
-    // If the actual declaration of write disagrees, insert casts as
-    // appropriate.
-    const FunctionType *FT = WriteFn->getFunctionType();
-    unsigned NumArgs = FT->getNumParams();
-    for (unsigned i = 0; i != 3; ++i)
-      if (i < NumArgs && FT->getParamType(i) != Args[i]->getType())
-        Args[i] = ConstantExpr::getCast(cast<Constant>(Args[i]),
-                                        FT->getParamType(i));
-
-    (new CallInst(WriteFn, Args, "", IB))->setTailCall();
-  }
+#if 0
+  if (AbortMessage == 0)
+    createAbortMessage(IB->getParent()->getParent()->getParent());
+
+  // These are the arguments we WANT...
+  Value* Args[3];
+  Args[0] = ConstantInt::get(Type::Int32Ty, 2);
+  Args[1] = AbortMessage;
+  Args[2] = ConstantInt::get(Type::Int32Ty, AbortMessageLength);
+  (new CallInst(WriteFn, Args, 3, "", IB))->setTailCall();
+#endif
 }
 
 bool LowerInvoke::insertCheapEHSupport(Function &F) {
   bool Changed = false;
   for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
     if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
+      std::vector<Value*> CallArgs(II->op_begin()+3, II->op_end());
       // Insert a normal call instruction...
-      std::string Name = II->getName(); II->setName("");
       CallInst *NewCall = new CallInst(II->getCalledValue(),
-                                       std::vector<Value*>(II->op_begin()+3,
-                                                       II->op_end()), Name, II);
+                                       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.
@@ -234,7 +233,7 @@ bool LowerInvoke::insertCheapEHSupport(Function &F) {
       writeAbortMessage(UI);
 
       // Insert a call to abort()
-      (new CallInst(AbortFn, std::vector<Value*>(), "", UI))->setTailCall();
+      (new CallInst(AbortFn, "", UI))->setTailCall();
 
       // Insert a return instruction.  This really should be a "barrier", as it
       // is unreachable.
@@ -254,8 +253,19 @@ bool LowerInvoke::insertCheapEHSupport(Function &F) {
 void LowerInvoke::rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
                                          AllocaInst *InvokeNum,
                                          SwitchInst *CatchSwitch) {
-  ConstantUInt *InvokeNoC = ConstantUInt::get(Type::UIntTy, InvokeNo);
+  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
@@ -263,18 +273,19 @@ void LowerInvoke::rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
   BasicBlock::iterator NI = II->getNormalDest()->begin();
   while (isa<PHINode>(NI)) ++NI;
   // nonvolatile.
-  new StoreInst(Constant::getNullValue(Type::UIntTy), InvokeNum, false, NI);
+  new StoreInst(Constant::getNullValue(Type::Int32Ty), InvokeNum, false, NI);
   
   // Add a switch case to our unwind block.
   CatchSwitch->addCase(InvokeNoC, II->getUnwindDest());
   
   // Insert a normal call instruction.
-  std::string Name = II->getName(); II->setName("");
+  std::vector<Value*> CallArgs(II->op_begin()+3, II->op_end());
   CallInst *NewCall = new CallInst(II->getCalledValue(),
-                                   std::vector<Value*>(II->op_begin()+3,
-                                                       II->op_end()), Name,
+                                   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.
@@ -311,7 +322,7 @@ splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes) {
   Function *F = Invokes.back()->getParent()->getParent();
   
   // To avoid having to handle incoming arguments specially, we lower each arg
-  // to a copy instruction in the entry block.  This ensure that the argument
+  // to a copy instruction in the entry block.  This ensures that the argument
   // value itself cannot be live across the entry block.
   BasicBlock::iterator AfterAllocaInsertPt = F->begin()->begin();
   while (isa<AllocaInst>(AfterAllocaInsertPt) &&
@@ -319,10 +330,16 @@ splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes) {
     ++AfterAllocaInsertPt;
   for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
        AI != E; ++AI) {
-    CastInst *NC = new CastInst(AI, AI->getType(), AI->getName()+".tmp",
-                                AfterAllocaInsertPt);
+    // This is always a no-op cast because we're casting AI to AI->getType() so
+    // src and destination types are identical. BitCast is the only possibility.
+    CastInst *NC = new BitCastInst(
+      AI, AI->getType(), AI->getName()+".tmp", AfterAllocaInsertPt);
     AI->replaceAllUsesWith(NC);
-    NC->setOperand(0, AI);
+    // Normally its is forbidden to replace a CastInst's operand because it
+    // could cause the opcode to reflect an illegal conversion. However, we're
+    // replacing it here with the same value it was constructed with to simply
+    // make NC its user.
+    NC->setOperand(0, AI); 
   }
   
   // Finally, scan the code looking for instructions with bad live ranges.
@@ -355,7 +372,6 @@ splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes) {
       // Scan all of the uses and see if the live range is live across an unwind
       // edge.  If we find a use live across an invoke edge, create an alloca
       // and spill the value.
-      AllocaInst *SpillLoc = 0;
       std::set<InvokeInst*> InvokesWithStoreInserted;
 
       // Find all of the blocks that this value is live in.
@@ -365,7 +381,6 @@ splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes) {
         Instruction *U = Users.back();
         Users.pop_back();
         
-        BasicBlock *UseBlock;
         if (!isa<PHINode>(U)) {
           MarkBlocksLiveIn(U->getParent(), LiveBBs);
         } else {
@@ -441,14 +456,15 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
     // Create an alloca for the incoming jump buffer ptr and the new jump buffer
     // that needs to be restored on all exits from the function.  This is an
     // alloca because the value needs to be live across invokes.
+    unsigned Align = TLI ? TLI->getJumpBufAlignment() : 0;
     AllocaInst *JmpBuf = 
-      new AllocaInst(JBLinkTy, 0, "jblink", F.begin()->begin());
+      new AllocaInst(JBLinkTy, 0, Align, "jblink", F.begin()->begin());
     
     std::vector<Value*> Idx;
-    Idx.push_back(Constant::getNullValue(Type::IntTy));
-    Idx.push_back(ConstantUInt::get(Type::UIntTy, 1));
-    OldJmpBufPtr = new GetElementPtrInst(JmpBuf, Idx, "OldBuf",
-                                         EntryBB->getTerminator());
+    Idx.push_back(Constant::getNullValue(Type::Int32Ty));
+    Idx.push_back(ConstantInt::get(Type::Int32Ty, 1));
+    OldJmpBufPtr = new GetElementPtrInst(JmpBuf, Idx.begin(), Idx.end(),
+                                         "OldBuf", EntryBB->getTerminator());
 
     // Copy the JBListHead to the alloca.
     Value *OldBuf = new LoadInst(JBListHead, "oldjmpbufptr", true,
@@ -464,9 +480,9 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
     
     // Create an alloca which keeps track of which invoke is currently
     // executing.  For normal calls it contains zero.
-    AllocaInst *InvokeNum = new AllocaInst(Type::UIntTy, 0, "invokenum",
+    AllocaInst *InvokeNum = new AllocaInst(Type::Int32Ty, 0, "invokenum",
                                            EntryBB->begin());
-    new StoreInst(ConstantInt::get(Type::UIntTy, 0), InvokeNum, true,
+    new StoreInst(ConstantInt::get(Type::Int32Ty, 0), InvokeNum, true,
                   EntryBB->getTerminator());
     
     // Insert a load in the Catch block, and a switch on its value.  By default,
@@ -485,16 +501,17 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
     BasicBlock *ContBlock = EntryBB->splitBasicBlock(EntryBB->getTerminator(),
                                                      "setjmp.cont");
 
-    Idx[1] = ConstantUInt::get(Type::UIntTy, 0);
-    Value *JmpBufPtr = new GetElementPtrInst(JmpBuf, Idx, "TheJmpBuf",
+    Idx[1] = ConstantInt::get(Type::Int32Ty, 0);
+    Value *JmpBufPtr = new GetElementPtrInst(JmpBuf, Idx.begin(), Idx.end(),
+                                             "TheJmpBuf",
                                              EntryBB->getTerminator());
     Value *SJRet = new CallInst(SetJmpFn, JmpBufPtr, "sjret",
                                 EntryBB->getTerminator());
     
     // Compare the return value to zero.
-    Value *IsNormal = BinaryOperator::createSetEQ(SJRet,
-                                     Constant::getNullValue(SJRet->getType()),
-                                        "notunwind", EntryBB->getTerminator());
+    Value *IsNormal = new ICmpInst(ICmpInst::ICMP_EQ, SJRet, 
+                                   Constant::getNullValue(SJRet->getType()),
+      "notunwind", EntryBB->getTerminator());
     // Nuke the uncond branch.
     EntryBB->getTerminator()->eraseFromParent();
     
@@ -526,19 +543,20 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
   }
   
   // Load the JBList, if it's null, then there was no catch!
-  Value *NotNull = BinaryOperator::createSetNE(BufPtr,
-                                      Constant::getNullValue(BufPtr->getType()),
-                                          "notnull", UnwindHandler);
+  Value *NotNull = new ICmpInst(ICmpInst::ICMP_NE, BufPtr, 
+                                Constant::getNullValue(BufPtr->getType()),
+    "notnull", UnwindHandler);
   new BranchInst(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::IntTy));
-  Idx.push_back(ConstantUInt::get(Type::UIntTy, 0));
-  Idx[0] = new GetElementPtrInst(BufPtr, Idx, "JmpBuf", UnwindBlock);
-  Idx[1] = ConstantInt::get(Type::IntTy, 1);
-  new CallInst(LongJmpFn, Idx, "", UnwindBlock);
+  Idx.push_back(Constant::getNullValue(Type::Int32Ty));
+  Idx.push_back(ConstantInt::get(Type::Int32Ty, 0));
+  Idx[0] = new GetElementPtrInst(BufPtr, Idx.begin(), Idx.end(), "JmpBuf",
+                                 UnwindBlock);
+  Idx[1] = ConstantInt::get(Type::Int32Ty, 1);
+  new CallInst(LongJmpFn, Idx.begin(), Idx.end(), "", UnwindBlock);
   new UnreachableInst(UnwindBlock);
   
   // Set up the term block ("throw without a catch").
@@ -548,7 +566,7 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
   writeAbortMessage(TermBlock->getTerminator());
   
   // Insert a call to abort()
-  (new CallInst(AbortFn, std::vector<Value*>(), "",
+  (new CallInst(AbortFn, "",
                 TermBlock->getTerminator()))->setTailCall();