LLVM API Documentation
00001 //===-- llvm/Support/CallSite.h - Abstract Call & Invoke instrs -*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the CallSite class, which is a handy wrapper for code that 00011 // wants to treat Call and Invoke instructions in a generic way. 00012 // 00013 // NOTE: This class is supposed to have "value semantics". So it should be 00014 // passed by value, not by reference; it should not be "new"ed or "delete"d. It 00015 // is efficiently copyable, assignable and constructable, with cost equivalent 00016 // to copying a pointer (notice that it has only a single data member). 00017 // 00018 //===----------------------------------------------------------------------===// 00019 00020 #ifndef LLVM_SUPPORT_CALLSITE_H 00021 #define LLVM_SUPPORT_CALLSITE_H 00022 00023 #include "llvm/Instruction.h" 00024 #include "llvm/BasicBlock.h" 00025 #include "llvm/ParameterAttributes.h" 00026 00027 namespace llvm { 00028 00029 class CallInst; 00030 class InvokeInst; 00031 00032 class CallSite { 00033 Instruction *I; 00034 public: 00035 CallSite() : I(0) {} 00036 CallSite(CallInst *CI) : I(reinterpret_cast<Instruction*>(CI)) {} 00037 CallSite(InvokeInst *II) : I(reinterpret_cast<Instruction*>(II)) {} 00038 CallSite(Instruction *C); 00039 CallSite(const CallSite &CS) : I(CS.I) {} 00040 CallSite &operator=(const CallSite &CS) { I = CS.I; return *this; } 00041 00042 bool operator==(const CallSite &CS) const { return I == CS.I; } 00043 bool operator!=(const CallSite &CS) const { return I != CS.I; } 00044 00045 /// CallSite::get - This static method is sort of like a constructor. It will 00046 /// create an appropriate call site for a Call or Invoke instruction, but it 00047 /// can also create a null initialized CallSite object for something which is 00048 /// NOT a call site. 00049 /// 00050 static CallSite get(Value *V) { 00051 if (Instruction *I = dyn_cast<Instruction>(V)) { 00052 if (I->getOpcode() == Instruction::Call) 00053 return CallSite(reinterpret_cast<CallInst*>(I)); 00054 else if (I->getOpcode() == Instruction::Invoke) 00055 return CallSite(reinterpret_cast<InvokeInst*>(I)); 00056 } 00057 return CallSite(); 00058 } 00059 00060 /// getCallingConv/setCallingConv - get or set the calling convention of the 00061 /// call. 00062 unsigned getCallingConv() const; 00063 void setCallingConv(unsigned CC); 00064 00065 /// getParamAttrs/setParamAttrs - get or set the parameter attributes of 00066 /// the call. 00067 const PAListPtr &getParamAttrs() const; 00068 void setParamAttrs(const PAListPtr &PAL); 00069 00070 /// paramHasAttr - whether the call or the callee has the given attribute. 00071 bool paramHasAttr(uint16_t i, ParameterAttributes attr) const; 00072 00073 /// @brief Extract the alignment for a call or parameter (0=unknown). 00074 uint16_t getParamAlignment(uint16_t i) const; 00075 00076 /// @brief Determine if the call does not access memory. 00077 bool doesNotAccessMemory() const; 00078 void setDoesNotAccessMemory(bool doesNotAccessMemory = true); 00079 00080 /// @brief Determine if the call does not access or only reads memory. 00081 bool onlyReadsMemory() const; 00082 void setOnlyReadsMemory(bool onlyReadsMemory = true); 00083 00084 /// @brief Determine if the call cannot return. 00085 bool doesNotReturn() const; 00086 void setDoesNotReturn(bool doesNotReturn = true); 00087 00088 /// @brief Determine if the call cannot unwind. 00089 bool doesNotThrow() const; 00090 void setDoesNotThrow(bool doesNotThrow = true); 00091 00092 /// getType - Return the type of the instruction that generated this call site 00093 /// 00094 const Type *getType() const { return I->getType(); } 00095 00096 /// getInstruction - Return the instruction this call site corresponds to 00097 /// 00098 Instruction *getInstruction() const { return I; } 00099 00100 /// getCaller - Return the caller function for this call site 00101 /// 00102 Function *getCaller() const { return I->getParent()->getParent(); } 00103 00104 /// getCalledValue - Return the pointer to function that is being called... 00105 /// 00106 Value *getCalledValue() const { 00107 assert(I && "Not a call or invoke instruction!"); 00108 return I->getOperand(0); 00109 } 00110 00111 /// getCalledFunction - Return the function being called if this is a direct 00112 /// call, otherwise return null (if it's an indirect call). 00113 /// 00114 Function *getCalledFunction() const { 00115 return dyn_cast<Function>(getCalledValue()); 00116 } 00117 00118 /// setCalledFunction - Set the callee to the specified value... 00119 /// 00120 void setCalledFunction(Value *V) { 00121 assert(I && "Not a call or invoke instruction!"); 00122 I->setOperand(0, V); 00123 } 00124 00125 Value *getArgument(unsigned ArgNo) const { 00126 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!"); 00127 return *(arg_begin()+ArgNo); 00128 } 00129 00130 void setArgument(unsigned ArgNo, Value* newVal) { 00131 assert(I && "Not a call or invoke instruction!"); 00132 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!"); 00133 I->setOperand(getArgumentOffset() + ArgNo, newVal); 00134 } 00135 00136 /// Given an operand number, returns the argument that corresponds to it. 00137 /// OperandNo must be a valid operand number that actually corresponds to an 00138 /// argument. 00139 unsigned getArgumentNo(unsigned OperandNo) const { 00140 assert(OperandNo >= getArgumentOffset() && "Operand number passed was not " 00141 "a valid argument"); 00142 return OperandNo - getArgumentOffset(); 00143 } 00144 00145 /// hasArgument - Returns true if this CallSite passes the given Value* as an 00146 /// argument to the called function. 00147 bool hasArgument(const Value *Arg) const; 00148 00149 /// arg_iterator - The type of iterator to use when looping over actual 00150 /// arguments at this call site... 00151 typedef User::op_iterator arg_iterator; 00152 00153 /// arg_begin/arg_end - Return iterators corresponding to the actual argument 00154 /// list for a call site. 00155 arg_iterator arg_begin() const { 00156 assert(I && "Not a call or invoke instruction!"); 00157 return I->op_begin() + getArgumentOffset(); // Skip non-arguments 00158 } 00159 00160 arg_iterator arg_end() const { return I->op_end(); } 00161 bool arg_empty() const { return arg_end() == arg_begin(); } 00162 unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); } 00163 00164 bool operator<(const CallSite &CS) const { 00165 return getInstruction() < CS.getInstruction(); 00166 } 00167 00168 private: 00169 /// Returns the operand number of the first argument 00170 unsigned getArgumentOffset() const { 00171 if (I->getOpcode() == Instruction::Call) 00172 return 1; // Skip Function 00173 else 00174 return 3; // Skip Function, BB, BB 00175 } 00176 }; 00177 00178 } // End llvm namespace 00179 00180 #endif