From fd947f33ba2b1260d7496776c96d5674ee3c9ef4 Mon Sep 17 00:00:00 2001 From: Sanjoy Das Date: Thu, 10 Dec 2015 06:39:02 +0000 Subject: [PATCH] Add arg_begin() and arg_end() to CallInst and InvokeInst; NFCI - This simplifies the CallSite class, arg_begin / arg_end are now simple wrapper getters. - In several places, we were creating CallSite instances solely to call arg_begin and arg_end. With this change, that's no longer required. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@255226 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/CallSite.h | 29 ++++--------- include/llvm/IR/Instructions.h | 54 ++++++++++++++++++++----- lib/IR/Instructions.cpp | 6 +-- lib/Transforms/IPO/PruneEH.cpp | 3 +- lib/Transforms/Utils/InlineFunction.cpp | 5 +-- lib/Transforms/Utils/Local.cpp | 3 +- 6 files changed, 58 insertions(+), 42 deletions(-) diff --git a/include/llvm/IR/CallSite.h b/include/llvm/IR/CallSite.h index 8556dda163b..7d9902f944e 100644 --- a/include/llvm/IR/CallSite.h +++ b/include/llvm/IR/CallSite.h @@ -148,15 +148,6 @@ public: /// arguments at this call site. typedef IterTy arg_iterator; - /// arg_begin/arg_end - Return iterators corresponding to the actual argument - /// list for a call site. - IterTy arg_begin() const { - assert(getInstruction() && "Not a call or invoke instruction!"); - // Skip non-arguments - return (*this)->op_begin(); - } - - IterTy arg_end() const { return (*this)->op_end() - getArgumentEndOffset(); } iterator_range args() const { return make_range(arg_begin(), arg_end()); } @@ -387,6 +378,14 @@ public: CALLSITE_DELEGATE_GETTER(getOperandBundle(ID)); } + IterTy arg_begin() const { + CALLSITE_DELEGATE_GETTER(arg_begin()); + } + + IterTy arg_end() const { + CALLSITE_DELEGATE_GETTER(arg_end()); + } + #undef CALLSITE_DELEGATE_GETTER #undef CALLSITE_DELEGATE_SETTER @@ -460,18 +459,6 @@ public: } private: - unsigned getArgumentEndOffset() const { - if (isCall()) { - // Skip [ operand bundles ], Callee - auto *CI = cast(getInstruction()); - return 1 + CI->getNumTotalBundleOperands(); - } else { - // Skip [ operand bundles ], BB, BB, Callee - auto *II = cast(getInstruction()); - return 3 + II->getNumTotalBundleOperands(); - } - } - IterTy getCallee() const { if (isCall()) // Skip Callee return cast(getInstruction())->op_end() - 1; diff --git a/include/llvm/IR/Instructions.h b/include/llvm/IR/Instructions.h index ae06a5f641a..84ab72138f0 100644 --- a/include/llvm/IR/Instructions.h +++ b/include/llvm/IR/Instructions.h @@ -1543,16 +1543,32 @@ public: setOperand(i, v); } - /// arg_operands - iteration adapter for range-for loops. + /// \brief Return the iterator pointing to the beginning of the argument list. + op_iterator arg_begin() { return op_begin(); } + + /// \brief Return the iterator pointing to the end of the argument list. + op_iterator arg_end() { + // [ call args ], [ operand bundles ], callee + return op_end() - getNumTotalBundleOperands() - 1; + }; + + /// \brief Iteration adapter for range-for loops. iterator_range arg_operands() { - // The last operand in the op list is the callee - it's not one of the args - // so we don't want to iterate over it. - return make_range(op_begin(), op_end() - getNumTotalBundleOperands() - 1); + return make_range(arg_begin(), arg_end()); } - /// arg_operands - iteration adapter for range-for loops. + /// \brief Return the iterator pointing to the beginning of the argument list. + const_op_iterator arg_begin() const { return op_begin(); } + + /// \brief Return the iterator pointing to the end of the argument list. + const_op_iterator arg_end() const { + // [ call args ], [ operand bundles ], callee + return op_end() - getNumTotalBundleOperands() - 1; + }; + + /// \brief Iteration adapter for range-for loops. iterator_range arg_operands() const { - return make_range(op_begin(), op_end() - getNumTotalBundleOperands() - 1); + return make_range(arg_begin(), arg_end()); } /// \brief Wrappers for getting the \c Use of a call argument. @@ -3450,14 +3466,32 @@ public: setOperand(i, v); } - /// arg_operands - iteration adapter for range-for loops. + /// \brief Return the iterator pointing to the beginning of the argument list. + op_iterator arg_begin() { return op_begin(); } + + /// \brief Return the iterator pointing to the end of the argument list. + op_iterator arg_end() { + // [ invoke args ], [ operand bundles ], normal dest, unwind dest, callee + return op_end() - getNumTotalBundleOperands() - 3; + }; + + /// \brief Iteration adapter for range-for loops. iterator_range arg_operands() { - return make_range(op_begin(), op_end() - getNumTotalBundleOperands() - 3); + return make_range(arg_begin(), arg_end()); } - /// arg_operands - iteration adapter for range-for loops. + /// \brief Return the iterator pointing to the beginning of the argument list. + const_op_iterator arg_begin() const { return op_begin(); } + + /// \brief Return the iterator pointing to the end of the argument list. + const_op_iterator arg_end() const { + // [ invoke args ], [ operand bundles ], normal dest, unwind dest, callee + return op_end() - getNumTotalBundleOperands() - 3; + }; + + /// \brief Iteration adapter for range-for loops. iterator_range arg_operands() const { - return make_range(op_begin(), op_end() - getNumTotalBundleOperands() - 3); + return make_range(arg_begin(), arg_end()); } /// \brief Wrappers for getting the \c Use of a invoke argument. diff --git a/lib/IR/Instructions.cpp b/lib/IR/Instructions.cpp index 6ec2e289970..f185caacdf6 100644 --- a/lib/IR/Instructions.cpp +++ b/lib/IR/Instructions.cpp @@ -299,8 +299,7 @@ CallInst::CallInst(const CallInst &CI) CallInst *CallInst::Create(CallInst *CI, ArrayRef OpB, Instruction *InsertPt) { - CallSite CS(CI); - std::vector Args(CS.arg_begin(), CS.arg_end()); + std::vector Args(CI->arg_begin(), CI->arg_end()); auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(), InsertPt); @@ -587,8 +586,7 @@ InvokeInst::InvokeInst(const InvokeInst &II) InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef OpB, Instruction *InsertPt) { - CallSite CS(II); - std::vector Args(CS.arg_begin(), CS.arg_end()); + std::vector Args(II->arg_begin(), II->arg_end()); auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(), II->getUnwindDest(), Args, OpB, diff --git a/lib/Transforms/IPO/PruneEH.cpp b/lib/Transforms/IPO/PruneEH.cpp index c9c0b197eae..cd2411ba554 100644 --- a/lib/Transforms/IPO/PruneEH.cpp +++ b/lib/Transforms/IPO/PruneEH.cpp @@ -191,8 +191,7 @@ bool PruneEH::SimplifyFunction(Function *F) { for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { if (InvokeInst *II = dyn_cast(BB->getTerminator())) if (II->doesNotThrow() && canSimplifyInvokeNoUnwind(F)) { - CallSite CS(II); - SmallVector Args(CS.arg_begin(), CS.arg_end()); + SmallVector Args(II->arg_begin(), II->arg_end()); SmallVector OpBundles; II->getOperandBundlesAsDefs(OpBundles); diff --git a/lib/Transforms/Utils/InlineFunction.cpp b/lib/Transforms/Utils/InlineFunction.cpp index 9a0aabc38a5..cafd1818fed 100644 --- a/lib/Transforms/Utils/InlineFunction.cpp +++ b/lib/Transforms/Utils/InlineFunction.cpp @@ -206,11 +206,10 @@ HandleCallsInBlockInlinedThroughInvoke(BasicBlock *BB, BasicBlock *UnwindEdge) { BB->getInstList().pop_back(); // Create the new invoke instruction. - ImmutableCallSite CS(CI); - SmallVector InvokeArgs(CS.arg_begin(), CS.arg_end()); + SmallVector InvokeArgs(CI->arg_begin(), CI->arg_end()); SmallVector OpBundles; - CS.getOperandBundlesAsDefs(OpBundles); + CI->getOperandBundlesAsDefs(OpBundles); // Note: we're round tripping operand bundles through memory here, and that // can potentially be avoided with a cleverer API design that we do not have diff --git a/lib/Transforms/Utils/Local.cpp b/lib/Transforms/Utils/Local.cpp index 19122dd21a7..391ed685766 100644 --- a/lib/Transforms/Utils/Local.cpp +++ b/lib/Transforms/Utils/Local.cpp @@ -1210,8 +1210,7 @@ static void changeToUnreachable(Instruction *I, bool UseLLVMTrap) { /// changeToCall - Convert the specified invoke into a normal call. static void changeToCall(InvokeInst *II) { - CallSite CS(II); - SmallVector Args(CS.arg_begin(), CS.arg_end()); + SmallVector Args(II->arg_begin(), II->arg_end()); SmallVector OpBundles; II->getOperandBundlesAsDefs(OpBundles); CallInst *NewCall = CallInst::Create(II->getCalledValue(), Args, OpBundles, -- 2.34.1