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