From f1355a55f8d815f5385e9a4432195f03b65f3a42 Mon Sep 17 00:00:00 2001 From: David Greene Date: Mon, 27 Aug 2007 19:04:21 +0000 Subject: [PATCH] Update InvokeInst to work like CallInst git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41506 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Instructions.h | 64 +++++++++++++++++-- include/llvm/Support/LLVMBuilder.h | 10 +-- lib/AsmParser/llvmAsmParser.y | 2 +- lib/Bitcode/Reader/BitcodeReader.cpp | 2 +- lib/Transforms/IPO/ArgumentPromotion.cpp | 2 +- .../IPO/DeadArgumentElimination.cpp | 4 +- lib/Transforms/IPO/LowerSetJmp.cpp | 2 +- .../Scalar/InstructionCombining.cpp | 2 +- lib/Transforms/Scalar/LowerGC.cpp | 2 +- lib/Transforms/Utils/InlineFunction.cpp | 2 +- lib/VMCore/Instructions.cpp | 22 ------- tools/llvm-upgrade/UpgradeParser.y | 5 +- tools/llvm2cpp/CppWriter.cpp | 3 +- 13 files changed, 76 insertions(+), 46 deletions(-) diff --git a/include/llvm/Instructions.h b/include/llvm/Instructions.h index 677378382d0..2913341cb89 100644 --- a/include/llvm/Instructions.h +++ b/include/llvm/Instructions.h @@ -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 + 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::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 + InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, + InputIterator ArgBegin, InputIterator ArgEnd, + const std::string &Name = "", Instruction *InsertBefore = 0) + : TerminatorInst(cast(cast(Func->getType()) + ->getElementType())->getReturnType(), + Instruction::Invoke, 0, 0, InsertBefore) { + init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name, + typename std::iterator_traits::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 + InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, + InputIterator ArgBegin, InputIterator ArgEnd, + const std::string &Name, BasicBlock *InsertAtEnd) + : TerminatorInst(cast(cast(Func->getType()) + ->getElementType())->getReturnType(), + Instruction::Invoke, 0, 0, InsertAtEnd) { + init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name, + typename std::iterator_traits::iterator_category()); + } + ~InvokeInst(); virtual InvokeInst *clone() const; diff --git a/include/llvm/Support/LLVMBuilder.h b/include/llvm/Support/LLVMBuilder.h index 0323f2955b5..ce403f17525 100644 --- a/include/llvm/Support/LLVMBuilder.h +++ b/include/llvm/Support/LLVMBuilder.h @@ -115,12 +115,12 @@ public: } /// CreateInvoke - Create an invoke instruction. + template 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() { diff --git a/lib/AsmParser/llvmAsmParser.y b/lib/AsmParser/llvmAsmParser.y index adf6eadc7c7..5570692864a 100644 --- a/lib/AsmParser/llvmAsmParser.y +++ b/lib/AsmParser/llvmAsmParser.y @@ -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; diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index eb18e315310..e1cd668a83e 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -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(I)->setCallingConv(CCInfo); break; } diff --git a/lib/Transforms/IPO/ArgumentPromotion.cpp b/lib/Transforms/IPO/ArgumentPromotion.cpp index d40df48b29a..9853afe8752 100644 --- a/lib/Transforms/IPO/ArgumentPromotion.cpp +++ b/lib/Transforms/IPO/ArgumentPromotion.cpp @@ -447,7 +447,7 @@ Function *ArgPromotion::DoPromotion(Function *F, Instruction *New; if (InvokeInst *II = dyn_cast(Call)) { New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(), - &Args[0], Args.size(), "", Call); + Args.begin(), Args.end(), "", Call); cast(New)->setCallingConv(CS.getCallingConv()); } else { New = new CallInst(NF, Args.begin(), Args.end(), "", Call); diff --git a/lib/Transforms/IPO/DeadArgumentElimination.cpp b/lib/Transforms/IPO/DeadArgumentElimination.cpp index b5ec103b7cd..dd5d668bc17 100644 --- a/lib/Transforms/IPO/DeadArgumentElimination.cpp +++ b/lib/Transforms/IPO/DeadArgumentElimination.cpp @@ -174,7 +174,7 @@ bool DAE::DeleteDeadVarargs(Function &Fn) { Instruction *New; if (InvokeInst *II = dyn_cast(Call)) { New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(), - &Args[0], Args.size(), "", Call); + Args.begin(), Args.end(), "", Call); cast(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(Call)) { New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(), - &Args[0], Args.size(), "", Call); + Args.begin(), Args.end(), "", Call); cast(New)->setCallingConv(CS.getCallingConv()); } else { New = new CallInst(NF, Args.begin(), Args.end(), "", Call); diff --git a/lib/Transforms/IPO/LowerSetJmp.cpp b/lib/Transforms/IPO/LowerSetJmp.cpp index 2fa6a10cedb..dbc3199162a 100644 --- a/lib/Transforms/IPO/LowerSetJmp.cpp +++ b/lib/Transforms/IPO/LowerSetJmp.cpp @@ -475,7 +475,7 @@ void LowerSetJmp::visitCallInst(CallInst& CI) std::vector 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); diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 2fad0abb151..416e1f012a6 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -8006,7 +8006,7 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) { Instruction *NC; if (InvokeInst *II = dyn_cast(Caller)) { NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(), - &Args[0], Args.size(), Caller->getName(), Caller); + Args.begin(), Args.end(), Caller->getName(), Caller); cast(NC)->setCallingConv(II->getCallingConv()); } else { NC = new CallInst(Callee, Args.begin(), Args.end(), diff --git a/lib/Transforms/Scalar/LowerGC.cpp b/lib/Transforms/Scalar/LowerGC.cpp index 8e6611a4f91..e1576845dc2 100644 --- a/lib/Transforms/Scalar/LowerGC.cpp +++ b/lib/Transforms/Scalar/LowerGC.cpp @@ -331,7 +331,7 @@ bool LowerGC::runOnFunction(Function &F) { std::vector 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; } diff --git a/lib/Transforms/Utils/InlineFunction.cpp b/lib/Transforms/Utils/InlineFunction.cpp index 9735a2fcda4..96ad420d340 100644 --- a/lib/Transforms/Utils/InlineFunction.cpp +++ b/lib/Transforms/Utils/InlineFunction.cpp @@ -85,7 +85,7 @@ static void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock, SmallVector 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()); diff --git a/lib/VMCore/Instructions.cpp b/lib/VMCore/Instructions.cpp index 43f89760af8..bb11a4b87bc 100644 --- a/lib/VMCore/Instructions.cpp +++ b/lib/VMCore/Instructions.cpp @@ -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(cast(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(cast(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()) { diff --git a/tools/llvm-upgrade/UpgradeParser.y b/tools/llvm-upgrade/UpgradeParser.y index 251cc77ded6..5893fcd5ccc 100644 --- a/tools/llvm-upgrade/UpgradeParser.y +++ b/tools/llvm-upgrade/UpgradeParser.y @@ -3342,7 +3342,8 @@ BBTerminatorInst // Create the call node... if (!$6) { // Has no arguments? - $$.TI = new InvokeInst(V, Normal, Except, 0, 0); + std::vector 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($$.TI)->setCallingConv(upgradeCallingConv($2)); delete $3.PAT; diff --git a/tools/llvm2cpp/CppWriter.cpp b/tools/llvm2cpp/CppWriter.cpp index 1cba7578bc8..99d79def019 100644 --- a/tools/llvm2cpp/CppWriter.cpp +++ b/tools/llvm2cpp/CppWriter.cpp @@ -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("; -- 2.34.1