Update InvokeInst to work like CallInst
authorDavid Greene <greened@obbligato.org>
Mon, 27 Aug 2007 19:04:21 +0000 (19:04 +0000)
committerDavid Greene <greened@obbligato.org>
Mon, 27 Aug 2007 19:04:21 +0000 (19:04 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41506 91177308-0d34-0410-b5e6-96231b3b80d8

13 files changed:
include/llvm/Instructions.h
include/llvm/Support/LLVMBuilder.h
lib/AsmParser/llvmAsmParser.y
lib/Bitcode/Reader/BitcodeReader.cpp
lib/Transforms/IPO/ArgumentPromotion.cpp
lib/Transforms/IPO/DeadArgumentElimination.cpp
lib/Transforms/IPO/LowerSetJmp.cpp
lib/Transforms/Scalar/InstructionCombining.cpp
lib/Transforms/Scalar/LowerGC.cpp
lib/Transforms/Utils/InlineFunction.cpp
lib/VMCore/Instructions.cpp
tools/llvm-upgrade/UpgradeParser.y
tools/llvm2cpp/CppWriter.cpp

index 677378382d04cefb131c4076dc33353baa03f56e..2913341cb8988b8037786dd7131e01e09579670a 100644 (file)
@@ -1544,13 +1544,65 @@ class InvokeInst : public TerminatorInst {
   InvokeInst(const InvokeInst &BI);
   void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
             Value* const *Args, unsigned NumArgs);
+
+  template<typename InputIterator>
+  void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
+            InputIterator ArgBegin, InputIterator ArgEnd,
+            const std::string &Name,
+            // This argument ensures that we have an iterator we can
+            // do arithmetic on in constant time
+            std::random_access_iterator_tag) {
+    typename std::iterator_traits<InputIterator>::difference_type NumArgs = 
+      std::distance(ArgBegin, ArgEnd);
+    
+    if (NumArgs > 0) {
+      // This requires that the iterator points to contiguous memory.
+      init(Func, IfNormal, IfException, &*ArgBegin, NumArgs);
+    }
+    else {
+      init(Func, IfNormal, IfException, 0, NumArgs);
+    }
+    
+    setName(Name);
+  }
+
 public:
-  InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
-             Value* const* Args, unsigned NumArgs, const std::string &Name = "",
-             Instruction *InsertBefore = 0);
-  InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
-             Value* const* Args, unsigned NumArgs, const std::string &Name,
-             BasicBlock *InsertAtEnd);
+  /// Construct an InvokeInst given a range of arguments.
+  /// InputIterator must be a random-access iterator pointing to
+  /// contiguous storage (e.g. a std::vector<>::iterator).  Checks are
+  /// made for random-accessness but not for contiguous storage as
+  /// that would incur runtime overhead.
+  ///
+  /// @brief Construct an InvokeInst from a range of arguments
+  template<typename InputIterator>
+  InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
+             InputIterator ArgBegin, InputIterator ArgEnd,
+             const std::string &Name = "", Instruction *InsertBefore = 0)
+      : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
+                                          ->getElementType())->getReturnType(),
+                       Instruction::Invoke, 0, 0, InsertBefore) {
+    init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
+         typename std::iterator_traits<InputIterator>::iterator_category());
+  }
+
+  /// Construct an InvokeInst given a range of arguments.
+  /// InputIterator must be a random-access iterator pointing to
+  /// contiguous storage (e.g. a std::vector<>::iterator).  Checks are
+  /// made for random-accessness but not for contiguous storage as
+  /// that would incur runtime overhead.
+  ///
+  /// @brief Construct an InvokeInst from a range of arguments
+  template<typename InputIterator>
+  InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
+             InputIterator ArgBegin, InputIterator ArgEnd,
+             const std::string &Name, BasicBlock *InsertAtEnd)
+      : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
+                                          ->getElementType())->getReturnType(),
+                       Instruction::Invoke, 0, 0, InsertAtEnd) {
+    init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
+         typename std::iterator_traits<InputIterator>::iterator_category());
+  }
+
   ~InvokeInst();
 
   virtual InvokeInst *clone() const;
index 0323f2955b57252bf275e9d4f13b155e20397e9d..ce403f17525782470a763ab4336b0165e1bf824d 100644 (file)
@@ -115,12 +115,12 @@ public:
   }
   
   /// CreateInvoke - Create an invoke instruction.
+  template<typename InputIterator>
   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest, 
-                           BasicBlock *UnwindDest,
-                           Value *const* Args, unsigned NumArgs,
-                           const char *Name = "") {
-    return Insert(new InvokeInst(Callee, NormalDest, UnwindDest, Args, NumArgs,
-                                 Name));
+                           BasicBlock *UnwindDest, InputIterator ArgBegin, 
+                           InputIterator ArgEnd, const char *Name = "") {
+    return(Insert(new InvokeInst(Callee, NormalDest, UnwindDest,
+                                 ArgBegin, ArgEnd, Name)));
   }
   
   UnwindInst *CreateUnwind() {
index adf6eadc7c7786f9f1775c5283439a99098b1a22..5570692864a9a29aaece7e7897ca1fe5d1cdd64c 100644 (file)
@@ -2652,7 +2652,7 @@ BBTerminatorInst : RET ResolvedVal {              // Return with a result...
     }
 
     // Create the InvokeInst
-    InvokeInst *II = new InvokeInst(V, Normal, Except, &Args[0], Args.size());
+    InvokeInst *II = new InvokeInst(V, Normal, Except, Args.begin(), Args.end());
     II->setCallingConv($2);
     $$ = II;
     delete $6;
index eb18e31531015b6c762f89ad84b172b2727a3e8d..e1cd668a83e8fe3b2ac332ca4c4881de6c385bfd 100644 (file)
@@ -1400,7 +1400,7 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {
         }
       }
       
-      I = new InvokeInst(Callee, NormalBB, UnwindBB, &Ops[0], Ops.size());
+      I = new InvokeInst(Callee, NormalBB, UnwindBB, Ops.begin(), Ops.end());
       cast<InvokeInst>(I)->setCallingConv(CCInfo);
       break;
     }
index d40df48b29af5560ca7615f12db8809f07c57df7..9853afe8752489c7c298c7b855defd05fd25901e 100644 (file)
@@ -447,7 +447,7 @@ Function *ArgPromotion::DoPromotion(Function *F,
     Instruction *New;
     if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
       New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(),
-                           &Args[0], Args.size(), "", Call);
+                           Args.begin(), Args.end(), "", Call);
       cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
     } else {
       New = new CallInst(NF, Args.begin(), Args.end(), "", Call);
index b5ec103b7cdd3b7f3a9b6b55d1d08745c65f9958..dd5d668bc170327dc329d4bff98f19bfca930748 100644 (file)
@@ -174,7 +174,7 @@ bool DAE::DeleteDeadVarargs(Function &Fn) {
     Instruction *New;
     if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
       New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(),
-                           &Args[0], Args.size(), "", Call);
+                           Args.begin(), Args.end(), "", Call);
       cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
     } else {
       New = new CallInst(NF, Args.begin(), Args.end(), "", Call);
@@ -540,7 +540,7 @@ void DAE::RemoveDeadArgumentsFromFunction(Function *F) {
     Instruction *New;
     if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
       New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(),
-                           &Args[0], Args.size(), "", Call);
+                           Args.begin(), Args.end(), "", Call);
       cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
     } else {
       New = new CallInst(NF, Args.begin(), Args.end(), "", Call);
index 2fa6a10cedb96bba112b2b5855ebc07ddb861209..dbc3199162a33f7d0f4b5a95d67b19c01cf7da0e 100644 (file)
@@ -475,7 +475,7 @@ 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[0], Params.size(), CI.getName(), Term);
+               Params.begin(), Params.end(), CI.getName(), Term);
 
   // Replace the old call inst with the invoke inst and remove the call.
   CI.replaceAllUsesWith(II);
index 2fad0abb151ab5e002fc1572b410897a944f6ed2..416e1f012a64eaca2cfaf8ce18f44f4c485c5d2f 100644 (file)
@@ -8006,7 +8006,7 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) {
   Instruction *NC;
   if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
     NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(),
-                        &Args[0], Args.size(), Caller->getName(), Caller);
+                        Args.begin(), Args.end(), Caller->getName(), Caller);
     cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv());
   } else {
     NC = new CallInst(Callee, Args.begin(), Args.end(),
index 8e6611a4f9120f176da2c20c8c075701ad3ddfc6..e1576845dc2affffc60c570640d830abe4d76b3f 100644 (file)
@@ -331,7 +331,7 @@ bool LowerGC::runOnFunction(Function &F) {
       std::vector<Value*> Args(CI->op_begin()+1, CI->op_end());
 
       Value *II = new InvokeInst(CI->getCalledValue(), NewBB, Cleanup,
-                                 &Args[0], Args.size(), CI->getName(), CBB);
+                                 Args.begin(), Args.end(), CI->getName(), CBB);
       CI->replaceAllUsesWith(II);
       delete CI;
     }
index 9735a2fcda444b0502fbfd83cc6f5cc5b69a47b6..96ad420d340355f0e9f4add1469afb5b7a550a15 100644 (file)
@@ -85,7 +85,7 @@ static void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock,
           SmallVector<Value*, 8> InvokeArgs(CI->op_begin()+1, CI->op_end());
           InvokeInst *II =
             new InvokeInst(CI->getCalledValue(), Split, InvokeDest,
-                           &InvokeArgs[0], InvokeArgs.size(),
+                           InvokeArgs.begin(), InvokeArgs.end(),
                            CI->getName(), BB->getTerminator());
           II->setCallingConv(CI->getCallingConv());
           
index 43f89760af81366c9d22d64914e7f1f8ce468bde..bb11a4b87bc4d12ae1f36c7a6737e29fc9d9df13 100644 (file)
@@ -395,28 +395,6 @@ void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
   }
 }
 
-InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
-                       BasicBlock *IfException,
-                       Value* const *Args, unsigned NumArgs,
-                       const std::string &Name, Instruction *InsertBefore)
-  : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
-                                    ->getElementType())->getReturnType(),
-                   Instruction::Invoke, 0, 0, InsertBefore) {
-  init(Fn, IfNormal, IfException, Args, NumArgs);
-  setName(Name);
-}
-
-InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
-                       BasicBlock *IfException,
-                       Value* const *Args, unsigned NumArgs,
-                       const std::string &Name, BasicBlock *InsertAtEnd)
-  : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
-                                    ->getElementType())->getReturnType(),
-                   Instruction::Invoke, 0, 0, InsertAtEnd) {
-  init(Fn, IfNormal, IfException, Args, NumArgs);
-  setName(Name);
-}
-
 InvokeInst::InvokeInst(const InvokeInst &II)
   : TerminatorInst(II.getType(), Instruction::Invoke,
                    new Use[II.getNumOperands()], II.getNumOperands()) {
index 251cc77ded6b528b6098ed39a9d664dda03a5320..5893fcd5ccc1de8d972f9c8940e6466890a0a67a 100644 (file)
@@ -3342,7 +3342,8 @@ BBTerminatorInst
 
     // Create the call node...
     if (!$6) {                                   // Has no arguments?
-      $$.TI = new InvokeInst(V, Normal, Except, 0, 0);
+      std::vector<Value*> Args;
+      $$.TI = new InvokeInst(V, Normal, Except, Args.begin(), Args.end());
     } else {                                     // Has arguments?
       // Loop through FunctionType's arguments and ensure they are specified
       // correctly!
@@ -3362,7 +3363,7 @@ BBTerminatorInst
       if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
         error("Invalid number of parameters detected");
 
-      $$.TI = new InvokeInst(V, Normal, Except, &Args[0], Args.size());
+      $$.TI = new InvokeInst(V, Normal, Except, Args.begin(), Args.end());
     }
     cast<InvokeInst>($$.TI)->setCallingConv(upgradeCallingConv($2));
     delete $3.PAT;
index 1cba7578bc8779e210b71aeeedb55104e7dc5458..99d79def01943aa086146d84a87ddc4f377b8135 100644 (file)
@@ -1083,8 +1083,7 @@ CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
           << opNames[0] << ", "
           << opNames[1] << ", "
           << opNames[2] << ", "
-          << "&" << iName << "_params[0], " << inv->getNumOperands() - 3 
-          << ", \"";
+          << iName << "_params.begin(), " << iName << "_params.end(), \"";    
       printEscapedString(inv->getName());
       Out << "\", " << bbname << ");";
       nl(Out) << iName << "->setCallingConv(";