1 //===-- llvm/Support/CallSite.h - Abstract Call & Invoke instrs -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the CallSite class, which is a handy wrapper for code that
11 // wants to treat Call and Invoke instructions in a generic way.
13 // NOTE: This class is supposed to have "value semantics". So it should be
14 // passed by value, not by reference; it should not be "new"ed or "delete"d. It
15 // is efficiently copyable, assignable and constructable, with cost equivalent
16 // to copying a pointer. (You will notice that it has only a single data
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_SUPPORT_CALLSITE_H
22 #define LLVM_SUPPORT_CALLSITE_H
24 #include "llvm/Instruction.h"
25 #include "llvm/BasicBlock.h"
36 CallSite(CallInst *CI) : I((Instruction*)CI) {}
37 CallSite(InvokeInst *II) : I((Instruction*)II) {}
38 CallSite(const CallSite &CS) : I(CS.I) {}
39 CallSite &operator=(const CallSite &CS) { I = CS.I; return *this; }
41 /// CallSite::get - This static method is sort of like a constructor. It will
42 /// create an appropriate call site for a Call or Invoke instruction, but it
43 /// can also create a null initialized CallSite object for something which is
46 static CallSite get(Value *V) {
47 if (Instruction *I = dyn_cast<Instruction>(V)) {
48 if (I->getOpcode() == Instruction::Call)
49 return CallSite((CallInst*)I);
50 else if (I->getOpcode() == Instruction::Invoke)
51 return CallSite((InvokeInst*)I);
56 /// getType - Return the type of the instruction that generated this call site
58 const Type *getType () const { return I->getType (); }
60 /// getInstruction - Return the instruction this call site corresponds to
62 Instruction *getInstruction() const { return I; }
64 /// getCaller - Return the caller function for this call site
66 Function *getCaller() const { return I->getParent()->getParent(); }
68 /// getCalledValue - Return the pointer to function that is being called...
70 Value *getCalledValue() const {
71 assert(I && "Not a call or invoke instruction!");
72 return I->getOperand(0);
75 /// getCalledFunction - Return the function being called if this is a direct
76 /// call, otherwise return null (if it's an indirect call).
78 /// FIXME: This should be inlined once ConstantPointerRefs are gone. :(
79 Function *getCalledFunction() const;
81 /// setCalledFunction - Set the callee to the specified value...
83 void setCalledFunction(Value *V) {
84 assert(I && "Not a call or invoke instruction!");
88 /// arg_iterator - The type of iterator to use when looping over actual
89 /// arguments at this call site...
90 typedef User::op_iterator arg_iterator;
92 /// arg_begin/arg_end - Return iterators corresponding to the actual argument
93 /// list for a call site.
95 arg_iterator arg_begin() const {
96 assert(I && "Not a call or invoke instruction!");
97 if (I->getOpcode() == Instruction::Call)
98 return I->op_begin()+1; // Skip Function
100 return I->op_begin()+3; // Skip Function, BB, BB
102 arg_iterator arg_end() const { return I->op_end(); }
103 unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
105 bool operator<(const CallSite &CS) const {
106 return getInstruction() < CS.getInstruction();
110 } // End llvm namespace