1 //===- 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. When in non- 12 // mutation context (e.g. an analysis) ImmutableCallSite should be used. 13 // Finally, when some degree of customization is necessary between these two 14 // extremes, CallSiteBase<> can be supplied with fine-tuned parameters. 15 // 16 // NOTE: These classes are supposed to have "value semantics". So they should be 17 // passed by value, not by reference; they should not be "new"ed or "delete"d. 18 // They are efficiently copyable, assignable and constructable, with cost 19 // equivalent to copying a pointer (notice that they have only a single data 20 // member). The internal representation carries a flag which indicates which of 21 // the two variants is enclosed. This allows for cheaper checks when various 22 // accessors of CallSite are employed. 23 // 24 //===----------------------------------------------------------------------===// 25 26 #ifndef LLVM_IR_CALLSITE_H 27 #define LLVM_IR_CALLSITE_H 28 29 #include "llvm/ADT/PointerIntPair.h" 30 #include "llvm/ADT/iterator_range.h" 31 #include "llvm/IR/Attributes.h" 32 #include "llvm/IR/CallingConv.h" 33 #include "llvm/IR/Instructions.h" 34 35 namespace llvm { 36 37 class CallInst; 38 class InvokeInst; 39 40 template <typename FunTy = const Function, 41 typename BBTy = const BasicBlock, 42 typename ValTy = const Value, 43 typename UserTy = const User, 44 typename UseTy = const Use, 45 typename InstrTy = const Instruction, 46 typename CallTy = const CallInst, 47 typename InvokeTy = const InvokeInst, 48 typename IterTy = User::const_op_iterator> 49 class CallSiteBase { 50 protected: 51 PointerIntPair<InstrTy*, 1, bool> I; 52 CallSiteBase()53 CallSiteBase() : I(nullptr, false) {} CallSiteBase(CallTy * CI)54 CallSiteBase(CallTy *CI) : I(CI, true) { assert(CI); } CallSiteBase(InvokeTy * II)55 CallSiteBase(InvokeTy *II) : I(II, false) { assert(II); } CallSiteBase(ValTy * II)56 explicit CallSiteBase(ValTy *II) { *this = get(II); } 57 58 private: 59 /// CallSiteBase::get - This static method is sort of like a constructor. It 60 /// will create an appropriate call site for a Call or Invoke instruction, but 61 /// it can also create a null initialized CallSiteBase object for something 62 /// which is NOT a call site. 63 /// get(ValTy * V)64 static CallSiteBase get(ValTy *V) { 65 if (InstrTy *II = dyn_cast<InstrTy>(V)) { 66 if (II->getOpcode() == Instruction::Call) 67 return CallSiteBase(static_cast<CallTy*>(II)); 68 else if (II->getOpcode() == Instruction::Invoke) 69 return CallSiteBase(static_cast<InvokeTy*>(II)); 70 } 71 return CallSiteBase(); 72 } 73 74 public: 75 /// isCall - true if a CallInst is enclosed. 76 /// Note that !isCall() does not mean it is an InvokeInst enclosed, 77 /// it also could signify a NULL Instruction pointer. isCall()78 bool isCall() const { return I.getInt(); } 79 80 /// isInvoke - true if a InvokeInst is enclosed. 81 /// isInvoke()82 bool isInvoke() const { return getInstruction() && !I.getInt(); } 83 getInstruction()84 InstrTy *getInstruction() const { return I.getPointer(); } 85 InstrTy *operator->() const { return I.getPointer(); } 86 explicit operator bool() const { return I.getPointer(); } 87 88 /// Get the basic block containing the call site getParent()89 BBTy* getParent() const { return getInstruction()->getParent(); } 90 91 /// getCalledValue - Return the pointer to function that is being called. 92 /// getCalledValue()93 ValTy *getCalledValue() const { 94 assert(getInstruction() && "Not a call or invoke instruction!"); 95 return *getCallee(); 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 /// getCalledFunction()101 FunTy *getCalledFunction() const { 102 return dyn_cast<FunTy>(getCalledValue()); 103 } 104 105 /// setCalledFunction - Set the callee to the specified value. 106 /// setCalledFunction(Value * V)107 void setCalledFunction(Value *V) { 108 assert(getInstruction() && "Not a call or invoke instruction!"); 109 *getCallee() = V; 110 } 111 112 /// isCallee - Determine whether the passed iterator points to the 113 /// callee operand's Use. isCallee(Value::const_user_iterator UI)114 bool isCallee(Value::const_user_iterator UI) const { 115 return isCallee(&UI.getUse()); 116 } 117 118 /// Determine whether this Use is the callee operand's Use. isCallee(const Use * U)119 bool isCallee(const Use *U) const { return getCallee() == U; } 120 121 /// \brief Determine whether the passed iterator points to an argument 122 /// operand. isArgOperand(Value::const_user_iterator UI)123 bool isArgOperand(Value::const_user_iterator UI) const { 124 return isArgOperand(&UI.getUse()); 125 } 126 127 /// \brief Determine whether the passed use points to an argument operand. isArgOperand(const Use * U)128 bool isArgOperand(const Use *U) const { 129 assert(getInstruction() == U->getUser()); 130 return arg_begin() <= U && U < arg_end(); 131 } 132 133 /// \brief Determine whether the passed iterator points to a bundle operand. isBundleOperand(Value::const_user_iterator UI)134 bool isBundleOperand(Value::const_user_iterator UI) const { 135 return isBundleOperand(&UI.getUse()); 136 } 137 138 /// \brief Determine whether the passed use points to a bundle operand. isBundleOperand(const Use * U)139 bool isBundleOperand(const Use *U) const { 140 assert(getInstruction() == U->getUser()); 141 if (!hasOperandBundles()) 142 return false; 143 unsigned OperandNo = U - (*this)->op_begin(); 144 return getBundleOperandsStartIndex() <= OperandNo && 145 OperandNo < getBundleOperandsEndIndex(); 146 } 147 148 /// \brief Determine whether the passed iterator points to a data operand. isDataOperand(Value::const_user_iterator UI)149 bool isDataOperand(Value::const_user_iterator UI) const { 150 return isDataOperand(&UI.getUse()); 151 } 152 153 /// \brief Determine whether the passed use points to a data operand. isDataOperand(const Use * U)154 bool isDataOperand(const Use *U) const { 155 return data_operands_begin() <= U && U < data_operands_end(); 156 } 157 getArgument(unsigned ArgNo)158 ValTy *getArgument(unsigned ArgNo) const { 159 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!"); 160 return *(arg_begin() + ArgNo); 161 } 162 setArgument(unsigned ArgNo,Value * newVal)163 void setArgument(unsigned ArgNo, Value* newVal) { 164 assert(getInstruction() && "Not a call or invoke instruction!"); 165 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!"); 166 getInstruction()->setOperand(ArgNo, newVal); 167 } 168 169 /// Given a value use iterator, returns the argument that corresponds to it. 170 /// Iterator must actually correspond to an argument. getArgumentNo(Value::const_user_iterator I)171 unsigned getArgumentNo(Value::const_user_iterator I) const { 172 return getArgumentNo(&I.getUse()); 173 } 174 175 /// Given a use for an argument, get the argument number that corresponds to 176 /// it. getArgumentNo(const Use * U)177 unsigned getArgumentNo(const Use *U) const { 178 assert(getInstruction() && "Not a call or invoke instruction!"); 179 assert(isArgOperand(U) && "Argument # out of range!"); 180 return U - arg_begin(); 181 } 182 183 /// arg_iterator - The type of iterator to use when looping over actual 184 /// arguments at this call site. 185 typedef IterTy arg_iterator; 186 args()187 iterator_range<IterTy> args() const { 188 return make_range(arg_begin(), arg_end()); 189 } arg_empty()190 bool arg_empty() const { return arg_end() == arg_begin(); } arg_size()191 unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); } 192 193 /// Given a value use iterator, returns the data operand that corresponds to 194 /// it. 195 /// Iterator must actually correspond to a data operand. getDataOperandNo(Value::const_user_iterator UI)196 unsigned getDataOperandNo(Value::const_user_iterator UI) const { 197 return getDataOperandNo(&UI.getUse()); 198 } 199 200 /// Given a use for a data operand, get the data operand number that 201 /// corresponds to it. getDataOperandNo(const Use * U)202 unsigned getDataOperandNo(const Use *U) const { 203 assert(getInstruction() && "Not a call or invoke instruction!"); 204 assert(isDataOperand(U) && "Data operand # out of range!"); 205 return U - data_operands_begin(); 206 } 207 208 /// Type of iterator to use when looping over data operands at this call site 209 /// (see below). 210 typedef IterTy data_operand_iterator; 211 212 /// data_operands_begin/data_operands_end - Return iterators iterating over 213 /// the call / invoke argument list and bundle operands. For invokes, this is 214 /// the set of instruction operands except the invoke target and the two 215 /// successor blocks; and for calls this is the set of instruction operands 216 /// except the call target. 217 data_operands_begin()218 IterTy data_operands_begin() const { 219 assert(getInstruction() && "Not a call or invoke instruction!"); 220 return (*this)->op_begin(); 221 } data_operands_end()222 IterTy data_operands_end() const { 223 assert(getInstruction() && "Not a call or invoke instruction!"); 224 return (*this)->op_end() - (isCall() ? 1 : 3); 225 } data_ops()226 iterator_range<IterTy> data_ops() const { 227 return make_range(data_operands_begin(), data_operands_end()); 228 } data_operands_empty()229 bool data_operands_empty() const { 230 return data_operands_end() == data_operands_begin(); 231 } data_operands_size()232 unsigned data_operands_size() const { 233 return std::distance(data_operands_begin(), data_operands_end()); 234 } 235 236 /// getType - Return the type of the instruction that generated this call site 237 /// getType()238 Type *getType() const { return (*this)->getType(); } 239 240 /// getCaller - Return the caller function for this call site 241 /// getCaller()242 FunTy *getCaller() const { return (*this)->getParent()->getParent(); } 243 244 /// \brief Tests if this call site must be tail call optimized. Only a 245 /// CallInst can be tail call optimized. isMustTailCall()246 bool isMustTailCall() const { 247 return isCall() && cast<CallInst>(getInstruction())->isMustTailCall(); 248 } 249 250 /// \brief Tests if this call site is marked as a tail call. isTailCall()251 bool isTailCall() const { 252 return isCall() && cast<CallInst>(getInstruction())->isTailCall(); 253 } 254 255 #define CALLSITE_DELEGATE_GETTER(METHOD) \ 256 InstrTy *II = getInstruction(); \ 257 return isCall() \ 258 ? cast<CallInst>(II)->METHOD \ 259 : cast<InvokeInst>(II)->METHOD 260 261 #define CALLSITE_DELEGATE_SETTER(METHOD) \ 262 InstrTy *II = getInstruction(); \ 263 if (isCall()) \ 264 cast<CallInst>(II)->METHOD; \ 265 else \ 266 cast<InvokeInst>(II)->METHOD 267 getNumArgOperands()268 unsigned getNumArgOperands() const { 269 CALLSITE_DELEGATE_GETTER(getNumArgOperands()); 270 } 271 getArgOperand(unsigned i)272 ValTy *getArgOperand(unsigned i) const { 273 CALLSITE_DELEGATE_GETTER(getArgOperand(i)); 274 } 275 getReturnedArgOperand()276 ValTy *getReturnedArgOperand() const { 277 CALLSITE_DELEGATE_GETTER(getReturnedArgOperand()); 278 } 279 isInlineAsm()280 bool isInlineAsm() const { 281 if (isCall()) 282 return cast<CallInst>(getInstruction())->isInlineAsm(); 283 return false; 284 } 285 286 /// getCallingConv/setCallingConv - get or set the calling convention of the 287 /// call. getCallingConv()288 CallingConv::ID getCallingConv() const { 289 CALLSITE_DELEGATE_GETTER(getCallingConv()); 290 } setCallingConv(CallingConv::ID CC)291 void setCallingConv(CallingConv::ID CC) { 292 CALLSITE_DELEGATE_SETTER(setCallingConv(CC)); 293 } 294 getFunctionType()295 FunctionType *getFunctionType() const { 296 CALLSITE_DELEGATE_GETTER(getFunctionType()); 297 } 298 mutateFunctionType(FunctionType * Ty)299 void mutateFunctionType(FunctionType *Ty) const { 300 CALLSITE_DELEGATE_SETTER(mutateFunctionType(Ty)); 301 } 302 303 /// getAttributes/setAttributes - get or set the parameter attributes of 304 /// the call. getAttributes()305 const AttributeSet &getAttributes() const { 306 CALLSITE_DELEGATE_GETTER(getAttributes()); 307 } setAttributes(const AttributeSet & PAL)308 void setAttributes(const AttributeSet &PAL) { 309 CALLSITE_DELEGATE_SETTER(setAttributes(PAL)); 310 } 311 addAttribute(unsigned i,Attribute::AttrKind Kind)312 void addAttribute(unsigned i, Attribute::AttrKind Kind) { 313 CALLSITE_DELEGATE_SETTER(addAttribute(i, Kind)); 314 } 315 addAttribute(unsigned i,StringRef Kind,StringRef Value)316 void addAttribute(unsigned i, StringRef Kind, StringRef Value) { 317 CALLSITE_DELEGATE_SETTER(addAttribute(i, Kind, Value)); 318 } 319 addAttribute(unsigned i,Attribute Attr)320 void addAttribute(unsigned i, Attribute Attr) { 321 CALLSITE_DELEGATE_SETTER(addAttribute(i, Attr)); 322 } 323 removeAttribute(unsigned i,Attribute::AttrKind Kind)324 void removeAttribute(unsigned i, Attribute::AttrKind Kind) { 325 CALLSITE_DELEGATE_SETTER(removeAttribute(i, Kind)); 326 } 327 removeAttribute(unsigned i,StringRef Kind)328 void removeAttribute(unsigned i, StringRef Kind) { 329 CALLSITE_DELEGATE_SETTER(removeAttribute(i, Kind)); 330 } 331 removeAttribute(unsigned i,Attribute Attr)332 void removeAttribute(unsigned i, Attribute Attr) { 333 CALLSITE_DELEGATE_SETTER(removeAttribute(i, Attr)); 334 } 335 336 /// \brief Return true if this function has the given attribute. hasFnAttr(Attribute::AttrKind Kind)337 bool hasFnAttr(Attribute::AttrKind Kind) const { 338 CALLSITE_DELEGATE_GETTER(hasFnAttr(Kind)); 339 } 340 341 /// \brief Return true if this function has the given attribute. hasFnAttr(StringRef Kind)342 bool hasFnAttr(StringRef Kind) const { 343 CALLSITE_DELEGATE_GETTER(hasFnAttr(Kind)); 344 } 345 346 /// \brief Return true if the call or the callee has the given attribute. paramHasAttr(unsigned i,Attribute::AttrKind Kind)347 bool paramHasAttr(unsigned i, Attribute::AttrKind Kind) const { 348 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, Kind)); 349 } 350 getAttribute(unsigned i,Attribute::AttrKind Kind)351 Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const { 352 CALLSITE_DELEGATE_GETTER(getAttribute(i, Kind)); 353 } 354 getAttribute(unsigned i,StringRef Kind)355 Attribute getAttribute(unsigned i, StringRef Kind) const { 356 CALLSITE_DELEGATE_GETTER(getAttribute(i, Kind)); 357 } 358 359 /// \brief Return true if the data operand at index \p i directly or 360 /// indirectly has the attribute \p A. 361 /// 362 /// Normal call or invoke arguments have per operand attributes, as specified 363 /// in the attribute set attached to this instruction, while operand bundle 364 /// operands may have some attributes implied by the type of its containing 365 /// operand bundle. dataOperandHasImpliedAttr(unsigned i,Attribute::AttrKind Kind)366 bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const { 367 CALLSITE_DELEGATE_GETTER(dataOperandHasImpliedAttr(i, Kind)); 368 } 369 370 /// @brief Extract the alignment for a call or parameter (0=unknown). getParamAlignment(uint16_t i)371 uint16_t getParamAlignment(uint16_t i) const { 372 CALLSITE_DELEGATE_GETTER(getParamAlignment(i)); 373 } 374 375 /// @brief Extract the number of dereferenceable bytes for a call or 376 /// parameter (0=unknown). getDereferenceableBytes(uint16_t i)377 uint64_t getDereferenceableBytes(uint16_t i) const { 378 CALLSITE_DELEGATE_GETTER(getDereferenceableBytes(i)); 379 } 380 381 /// @brief Extract the number of dereferenceable_or_null bytes for a call or 382 /// parameter (0=unknown). getDereferenceableOrNullBytes(uint16_t i)383 uint64_t getDereferenceableOrNullBytes(uint16_t i) const { 384 CALLSITE_DELEGATE_GETTER(getDereferenceableOrNullBytes(i)); 385 } 386 387 /// @brief Determine if the parameter or return value is marked with NoAlias 388 /// attribute. 389 /// @param n The parameter to check. 1 is the first parameter, 0 is the return doesNotAlias(unsigned n)390 bool doesNotAlias(unsigned n) const { 391 CALLSITE_DELEGATE_GETTER(doesNotAlias(n)); 392 } 393 394 /// \brief Return true if the call should not be treated as a call to a 395 /// builtin. isNoBuiltin()396 bool isNoBuiltin() const { 397 CALLSITE_DELEGATE_GETTER(isNoBuiltin()); 398 } 399 400 /// @brief Return true if the call should not be inlined. isNoInline()401 bool isNoInline() const { 402 CALLSITE_DELEGATE_GETTER(isNoInline()); 403 } 404 void setIsNoInline(bool Value = true) { 405 CALLSITE_DELEGATE_SETTER(setIsNoInline(Value)); 406 } 407 408 /// @brief Determine if the call does not access memory. doesNotAccessMemory()409 bool doesNotAccessMemory() const { 410 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory()); 411 } setDoesNotAccessMemory()412 void setDoesNotAccessMemory() { 413 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory()); 414 } 415 416 /// @brief Determine if the call does not access or only reads memory. onlyReadsMemory()417 bool onlyReadsMemory() const { 418 CALLSITE_DELEGATE_GETTER(onlyReadsMemory()); 419 } setOnlyReadsMemory()420 void setOnlyReadsMemory() { 421 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory()); 422 } 423 424 /// @brief Determine if the call does not access or only writes memory. doesNotReadMemory()425 bool doesNotReadMemory() const { 426 CALLSITE_DELEGATE_GETTER(doesNotReadMemory()); 427 } setDoesNotReadMemory()428 void setDoesNotReadMemory() { 429 CALLSITE_DELEGATE_SETTER(setDoesNotReadMemory()); 430 } 431 432 /// @brief Determine if the call can access memmory only using pointers based 433 /// on its arguments. onlyAccessesArgMemory()434 bool onlyAccessesArgMemory() const { 435 CALLSITE_DELEGATE_GETTER(onlyAccessesArgMemory()); 436 } setOnlyAccessesArgMemory()437 void setOnlyAccessesArgMemory() { 438 CALLSITE_DELEGATE_SETTER(setOnlyAccessesArgMemory()); 439 } 440 441 /// @brief Determine if the call cannot return. doesNotReturn()442 bool doesNotReturn() const { 443 CALLSITE_DELEGATE_GETTER(doesNotReturn()); 444 } setDoesNotReturn()445 void setDoesNotReturn() { 446 CALLSITE_DELEGATE_SETTER(setDoesNotReturn()); 447 } 448 449 /// @brief Determine if the call cannot unwind. doesNotThrow()450 bool doesNotThrow() const { 451 CALLSITE_DELEGATE_GETTER(doesNotThrow()); 452 } setDoesNotThrow()453 void setDoesNotThrow() { 454 CALLSITE_DELEGATE_SETTER(setDoesNotThrow()); 455 } 456 457 /// @brief Determine if the call can be duplicated. cannotDuplicate()458 bool cannotDuplicate() const { 459 CALLSITE_DELEGATE_GETTER(cannotDuplicate()); 460 } setCannotDuplicate()461 void setCannotDuplicate() { 462 CALLSITE_DELEGATE_GETTER(setCannotDuplicate()); 463 } 464 465 /// @brief Determine if the call is convergent. isConvergent()466 bool isConvergent() const { 467 CALLSITE_DELEGATE_GETTER(isConvergent()); 468 } setConvergent()469 void setConvergent() { 470 CALLSITE_DELEGATE_SETTER(setConvergent()); 471 } setNotConvergent()472 void setNotConvergent() { 473 CALLSITE_DELEGATE_SETTER(setNotConvergent()); 474 } 475 getNumOperandBundles()476 unsigned getNumOperandBundles() const { 477 CALLSITE_DELEGATE_GETTER(getNumOperandBundles()); 478 } 479 hasOperandBundles()480 bool hasOperandBundles() const { 481 CALLSITE_DELEGATE_GETTER(hasOperandBundles()); 482 } 483 getBundleOperandsStartIndex()484 unsigned getBundleOperandsStartIndex() const { 485 CALLSITE_DELEGATE_GETTER(getBundleOperandsStartIndex()); 486 } 487 getBundleOperandsEndIndex()488 unsigned getBundleOperandsEndIndex() const { 489 CALLSITE_DELEGATE_GETTER(getBundleOperandsEndIndex()); 490 } 491 getNumTotalBundleOperands()492 unsigned getNumTotalBundleOperands() const { 493 CALLSITE_DELEGATE_GETTER(getNumTotalBundleOperands()); 494 } 495 getOperandBundleAt(unsigned Index)496 OperandBundleUse getOperandBundleAt(unsigned Index) const { 497 CALLSITE_DELEGATE_GETTER(getOperandBundleAt(Index)); 498 } 499 getOperandBundle(StringRef Name)500 Optional<OperandBundleUse> getOperandBundle(StringRef Name) const { 501 CALLSITE_DELEGATE_GETTER(getOperandBundle(Name)); 502 } 503 getOperandBundle(uint32_t ID)504 Optional<OperandBundleUse> getOperandBundle(uint32_t ID) const { 505 CALLSITE_DELEGATE_GETTER(getOperandBundle(ID)); 506 } 507 countOperandBundlesOfType(uint32_t ID)508 unsigned countOperandBundlesOfType(uint32_t ID) const { 509 CALLSITE_DELEGATE_GETTER(countOperandBundlesOfType(ID)); 510 } 511 arg_begin()512 IterTy arg_begin() const { 513 CALLSITE_DELEGATE_GETTER(arg_begin()); 514 } 515 arg_end()516 IterTy arg_end() const { 517 CALLSITE_DELEGATE_GETTER(arg_end()); 518 } 519 520 #undef CALLSITE_DELEGATE_GETTER 521 #undef CALLSITE_DELEGATE_SETTER 522 getOperandBundlesAsDefs(SmallVectorImpl<OperandBundleDef> & Defs)523 void getOperandBundlesAsDefs(SmallVectorImpl<OperandBundleDef> &Defs) const { 524 const Instruction *II = getInstruction(); 525 // Since this is actually a getter that "looks like" a setter, don't use the 526 // above macros to avoid confusion. 527 if (isCall()) 528 cast<CallInst>(II)->getOperandBundlesAsDefs(Defs); 529 else 530 cast<InvokeInst>(II)->getOperandBundlesAsDefs(Defs); 531 } 532 533 /// @brief Determine whether this data operand is not captured. doesNotCapture(unsigned OpNo)534 bool doesNotCapture(unsigned OpNo) const { 535 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::NoCapture); 536 } 537 538 /// @brief Determine whether this argument is passed by value. isByValArgument(unsigned ArgNo)539 bool isByValArgument(unsigned ArgNo) const { 540 return paramHasAttr(ArgNo + 1, Attribute::ByVal); 541 } 542 543 /// @brief Determine whether this argument is passed in an alloca. isInAllocaArgument(unsigned ArgNo)544 bool isInAllocaArgument(unsigned ArgNo) const { 545 return paramHasAttr(ArgNo + 1, Attribute::InAlloca); 546 } 547 548 /// @brief Determine whether this argument is passed by value or in an alloca. isByValOrInAllocaArgument(unsigned ArgNo)549 bool isByValOrInAllocaArgument(unsigned ArgNo) const { 550 return paramHasAttr(ArgNo + 1, Attribute::ByVal) || 551 paramHasAttr(ArgNo + 1, Attribute::InAlloca); 552 } 553 554 /// @brief Determine if there are is an inalloca argument. Only the last 555 /// argument can have the inalloca attribute. hasInAllocaArgument()556 bool hasInAllocaArgument() const { 557 return paramHasAttr(arg_size(), Attribute::InAlloca); 558 } 559 doesNotAccessMemory(unsigned OpNo)560 bool doesNotAccessMemory(unsigned OpNo) const { 561 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone); 562 } 563 onlyReadsMemory(unsigned OpNo)564 bool onlyReadsMemory(unsigned OpNo) const { 565 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadOnly) || 566 dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone); 567 } 568 569 /// @brief Return true if the return value is known to be not null. 570 /// This may be because it has the nonnull attribute, or because at least 571 /// one byte is dereferenceable and the pointer is in addrspace(0). isReturnNonNull()572 bool isReturnNonNull() const { 573 if (paramHasAttr(0, Attribute::NonNull)) 574 return true; 575 else if (getDereferenceableBytes(0) > 0 && 576 getType()->getPointerAddressSpace() == 0) 577 return true; 578 579 return false; 580 } 581 582 /// hasArgument - Returns true if this CallSite passes the given Value* as an 583 /// argument to the called function. hasArgument(const Value * Arg)584 bool hasArgument(const Value *Arg) const { 585 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; 586 ++AI) 587 if (AI->get() == Arg) 588 return true; 589 return false; 590 } 591 592 private: getCallee()593 IterTy getCallee() const { 594 if (isCall()) // Skip Callee 595 return cast<CallInst>(getInstruction())->op_end() - 1; 596 else // Skip BB, BB, Callee 597 return cast<InvokeInst>(getInstruction())->op_end() - 3; 598 } 599 }; 600 601 class CallSite : public CallSiteBase<Function, BasicBlock, Value, User, Use, 602 Instruction, CallInst, InvokeInst, 603 User::op_iterator> { 604 public: CallSite()605 CallSite() {} CallSite(CallSiteBase B)606 CallSite(CallSiteBase B) : CallSiteBase(B) {} CallSite(CallInst * CI)607 CallSite(CallInst *CI) : CallSiteBase(CI) {} CallSite(InvokeInst * II)608 CallSite(InvokeInst *II) : CallSiteBase(II) {} CallSite(Instruction * II)609 explicit CallSite(Instruction *II) : CallSiteBase(II) {} CallSite(Value * V)610 explicit CallSite(Value *V) : CallSiteBase(V) {} 611 612 bool operator==(const CallSite &CS) const { return I == CS.I; } 613 bool operator!=(const CallSite &CS) const { return I != CS.I; } 614 bool operator<(const CallSite &CS) const { 615 return getInstruction() < CS.getInstruction(); 616 } 617 618 private: 619 User::op_iterator getCallee() const; 620 }; 621 622 /// ImmutableCallSite - establish a view to a call site for examination 623 class ImmutableCallSite : public CallSiteBase<> { 624 public: ImmutableCallSite()625 ImmutableCallSite() {} ImmutableCallSite(const CallInst * CI)626 ImmutableCallSite(const CallInst *CI) : CallSiteBase(CI) {} ImmutableCallSite(const InvokeInst * II)627 ImmutableCallSite(const InvokeInst *II) : CallSiteBase(II) {} ImmutableCallSite(const Instruction * II)628 explicit ImmutableCallSite(const Instruction *II) : CallSiteBase(II) {} ImmutableCallSite(const Value * V)629 explicit ImmutableCallSite(const Value *V) : CallSiteBase(V) {} ImmutableCallSite(CallSite CS)630 ImmutableCallSite(CallSite CS) : CallSiteBase(CS.getInstruction()) {} 631 }; 632 633 } // End llvm namespace 634 635 #endif 636