Add support for explicit calling conventions
[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 // 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 (notice that it has only a single data member).
17 //
18 //===----------------------------------------------------------------------===//
19
20 #ifndef LLVM_SUPPORT_CALLSITE_H
21 #define LLVM_SUPPORT_CALLSITE_H
22
23 #include "llvm/Instruction.h"
24 #include "llvm/BasicBlock.h"
25
26 namespace llvm {
27
28 class CallInst;
29 class InvokeInst;
30
31 class CallSite {
32   Instruction *I;
33 public:
34   CallSite() : I(0) {}
35   CallSite(CallInst *CI) : I((Instruction*)CI) {}
36   CallSite(InvokeInst *II) : I((Instruction*)II) {}
37   CallSite(const CallSite &CS) : I(CS.I) {}
38   CallSite &operator=(const CallSite &CS) { I = CS.I; return *this; }
39
40   /// CallSite::get - This static method is sort of like a constructor.  It will
41   /// create an appropriate call site for a Call or Invoke instruction, but it
42   /// can also create a null initialized CallSite object for something which is
43   /// NOT a call site.
44   ///
45   static CallSite get(Value *V) {
46     if (Instruction *I = dyn_cast<Instruction>(V)) {
47       if (I->getOpcode() == Instruction::Call)
48         return CallSite((CallInst*)I);
49       else if (I->getOpcode() == Instruction::Invoke)
50         return CallSite((InvokeInst*)I);
51     }
52     return CallSite();
53   }
54
55   /// getCallingConv/setCallingConv - get or set the calling convention of the
56   /// call.
57   unsigned getCallingConv() const;
58   void setCallingConv(unsigned CC);
59
60   /// getType - Return the type of the instruction that generated this call site
61   ///
62   const Type *getType() const { return I->getType(); }
63
64   /// getInstruction - Return the instruction this call site corresponds to
65   ///
66   Instruction *getInstruction() const { return I; }
67
68   /// getCaller - Return the caller function for this call site
69   ///
70   Function *getCaller() const { return I->getParent()->getParent(); }
71
72   /// getCalledValue - Return the pointer to function that is being called...
73   ///
74   Value *getCalledValue() const {
75     assert(I && "Not a call or invoke instruction!");
76     return I->getOperand(0);
77   }
78
79   /// getCalledFunction - Return the function being called if this is a direct
80   /// call, otherwise return null (if it's an indirect call).
81   ///
82   Function *getCalledFunction() const {
83     return dyn_cast<Function>(getCalledValue());
84   }
85
86   /// setCalledFunction - Set the callee to the specified value...
87   ///
88   void setCalledFunction(Value *V) {
89     assert(I && "Not a call or invoke instruction!");
90     I->setOperand(0, V);
91   }
92
93   Value *getArgument(unsigned ArgNo) const {
94     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
95     return *(arg_begin()+ArgNo);
96   }
97
98   /// arg_iterator - The type of iterator to use when looping over actual
99   /// arguments at this call site...
100   typedef User::op_iterator arg_iterator;
101
102   /// arg_begin/arg_end - Return iterators corresponding to the actual argument
103   /// list for a call site.
104   ///
105   arg_iterator arg_begin() const {
106     assert(I && "Not a call or invoke instruction!");
107     if (I->getOpcode() == Instruction::Call)
108       return I->op_begin()+1; // Skip Function
109     else
110       return I->op_begin()+3; // Skip Function, BB, BB
111   }
112   arg_iterator arg_end() const { return I->op_end(); }
113   unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
114
115   bool operator<(const CallSite &CS) const {
116     return getInstruction() < CS.getInstruction();
117   }
118 };
119
120 } // End llvm namespace
121
122 #endif