7e95e5db7b6e9309582a8ffe1e61ccfb71ad25b9
[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 is distributed under the University of Illinois Open Source
6 // 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 class ParamAttrsList;
31
32 class CallSite {
33   Instruction *I;
34 public:
35   CallSite() : I(0) {}
36   CallSite(CallInst *CI) : I(reinterpret_cast<Instruction*>(CI)) {}
37   CallSite(InvokeInst *II) : I(reinterpret_cast<Instruction*>(II)) {}
38   CallSite(const CallSite &CS) : I(CS.I) {}
39   CallSite &operator=(const CallSite &CS) { I = CS.I; return *this; }
40
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
44   /// NOT a call site.
45   ///
46   static CallSite get(Value *V) {
47     if (Instruction *I = dyn_cast<Instruction>(V)) {
48       if (I->getOpcode() == Instruction::Call)
49         return CallSite(reinterpret_cast<CallInst*>(I));
50       else if (I->getOpcode() == Instruction::Invoke)
51         return CallSite(reinterpret_cast<InvokeInst*>(I));
52     }
53     return CallSite();
54   }
55
56   /// getCallingConv/setCallingConv - get or set the calling convention of the
57   /// call.
58   unsigned getCallingConv() const;
59   void setCallingConv(unsigned CC);
60
61   /// getParamAttrs/setParamAttrs - get or set the parameter attributes of
62   /// the call.
63   const ParamAttrsList *getParamAttrs() const;
64   void setParamAttrs(const ParamAttrsList *PAL);
65
66   /// paramHasAttr - whether the call or the callee has the given attribute.
67   bool paramHasAttr(uint16_t i, unsigned attr) const;
68
69   /// @brief Determine if the call does not access memory.
70   bool doesNotAccessMemory() const;
71
72   /// @brief Determine if the call does not access or only reads memory.
73   bool onlyReadsMemory() const;
74
75   /// @brief Determine if the call cannot unwind.
76   bool doesNotThrow() const;
77   void setDoesNotThrow(bool doesNotThrow = true);
78
79   /// getType - Return the type of the instruction that generated this call site
80   ///
81   const Type *getType() const { return I->getType(); }
82
83   /// getInstruction - Return the instruction this call site corresponds to
84   ///
85   Instruction *getInstruction() const { return I; }
86
87   /// getCaller - Return the caller function for this call site
88   ///
89   Function *getCaller() const { return I->getParent()->getParent(); }
90
91   /// getCalledValue - Return the pointer to function that is being called...
92   ///
93   Value *getCalledValue() const {
94     assert(I && "Not a call or invoke instruction!");
95     return I->getOperand(0);
96   }
97
98   /// getCalledFunction - Return the function being called if this is a direct
99   /// call, otherwise return null (if it's an indirect call).
100   ///
101   Function *getCalledFunction() const {
102     return dyn_cast<Function>(getCalledValue());
103   }
104
105   /// setCalledFunction - Set the callee to the specified value...
106   ///
107   void setCalledFunction(Value *V) {
108     assert(I && "Not a call or invoke instruction!");
109     I->setOperand(0, V);
110   }
111
112   Value *getArgument(unsigned ArgNo) const {
113     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
114     return *(arg_begin()+ArgNo);
115   }
116
117   void setArgument(unsigned ArgNo, Value* newVal) {
118     assert(I && "Not a call or invoke instruction!");
119     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
120     if (I->getOpcode() == Instruction::Call)
121       I->setOperand(ArgNo+1, newVal); // Skip Function
122     else
123       I->setOperand(ArgNo+3, newVal); // Skip Function, BB, BB
124   }
125
126   /// arg_iterator - The type of iterator to use when looping over actual
127   /// arguments at this call site...
128   typedef User::op_iterator arg_iterator;
129
130   /// arg_begin/arg_end - Return iterators corresponding to the actual argument
131   /// list for a call site.
132   ///
133   arg_iterator arg_begin() const {
134     assert(I && "Not a call or invoke instruction!");
135     if (I->getOpcode() == Instruction::Call)
136       return I->op_begin()+1; // Skip Function
137     else
138       return I->op_begin()+3; // Skip Function, BB, BB
139   }
140   arg_iterator arg_end() const { return I->op_end(); }
141   bool arg_empty() const { return arg_end() == arg_begin(); }
142   unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
143
144   bool operator<(const CallSite &CS) const {
145     return getInstruction() < CS.getInstruction();
146   }
147 };
148
149 } // End llvm namespace
150
151 #endif