dc137d83b5fe23e15a8d4afb2e4e7a45becdce2b
[oota-llvm.git] / include / llvm / Support / CallSite.h
1 //===-- llvm/Support/CallSite.h - Abstract Call & Invoke instrs -*- C++ -*-===//
2 //
3 // This file defines the CallSite class, which is a handy wrapper for code that
4 // wants to treat Call and Invoke instructions in a generic way.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_SUPPORT_CALLSITE_H
9 #define LLVM_SUPPORT_CALLSITE_H
10
11 #include "llvm/Instruction.h"
12
13 class CallInst;
14 class InvokeInst;
15
16 class CallSite {
17   Instruction *I;
18 public:
19   CallSite() : I(0) {}
20   CallSite(CallInst *CI) : I((Instruction*)CI) {}
21   CallSite(InvokeInst *II) : I((Instruction*)II) {}
22   CallSite(const CallSite &CS) : I(CS.I) {}
23   CallSite &operator=(const CallSite &CS) { I = CS.I; return *this; }
24
25   /// CallSite::get - This static method is sort of like a constructor.  It will
26   /// create an appropriate call site for a Call or Invoke instruction, but it
27   /// can also create a null initialized CallSite object for something which is
28   /// NOT a call site.
29   ///
30   static CallSite get(Value *V) {
31     if (Instruction *I = dyn_cast<Instruction>(V)) {
32       if (I->getOpcode() == Instruction::Call)
33         return CallSite((CallInst*)I);
34       else if (I->getOpcode() == Instruction::Invoke)
35         return CallSite((InvokeInst*)I);
36     }
37     return CallSite();
38   }
39
40   /// getInstruction - Return the instruction this call site corresponds to
41   ///
42   Instruction *getInstruction() const { return I; }
43
44   /// getCalledValue - Return the pointer to function that is being called...
45   ///
46   Value *getCalledValue() const { return I->getOperand(0); }
47
48   /// getCalledFunction - Return the function being called if this is a direct
49   /// call, otherwise return null (if it's an indirect call).
50   ///
51   Function *getCalledFunction() const {
52     return dyn_cast<Function>(getCalledValue());
53   }
54
55   /// setCalledFunction - Set the callee to the specified value...
56   ///
57   void setCalledFunction(Value *V) {
58     I->setOperand(0, V);
59   }
60
61   /// arg_iterator - The type of iterator to use when looping over actual
62   /// arguments at this call site...
63   typedef User::op_iterator arg_iterator;
64
65   /// arg_begin/arg_end - Return iterators corresponding to the actual argument
66   /// list for a call site.
67   ///
68   arg_iterator arg_begin() const {
69     if (I->getOpcode() == Instruction::Call)
70       return I->op_begin()+1; // Skip Function
71     else
72       return I->op_begin()+3; // Skip Function, BB, BB
73   }
74   arg_iterator arg_end() const { return I->op_end(); }
75 };
76
77 #endif