1 //===- CallSite.h - Abstract Call & Invoke instrs ---------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the CallSite class, which is a handy wrapper for code that 10 // wants to treat Call, Invoke and CallBr instructions in a generic way. When 11 // in non-mutation context (e.g. an analysis) ImmutableCallSite should be used. 12 // Finally, when some degree of customization is necessary between these two 13 // extremes, CallSiteBase<> can be supplied with fine-tuned parameters. 14 // 15 // NOTE: These classes are supposed to have "value semantics". So they should be 16 // passed by value, not by reference; they should not be "new"ed or "delete"d. 17 // They are efficiently copyable, assignable and constructable, with cost 18 // equivalent to copying a pointer (notice that they have only a single data 19 // member). The internal representation carries a flag which indicates which of 20 // the three variants is enclosed. This allows for cheaper checks when various 21 // accessors of CallSite are employed. 22 // 23 //===----------------------------------------------------------------------===// 24 25 #ifndef LLVM_IR_CALLSITE_H 26 #define LLVM_IR_CALLSITE_H 27 28 #include "llvm/ADT/Optional.h" 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/Function.h" 34 #include "llvm/IR/InstrTypes.h" 35 #include "llvm/IR/Instruction.h" 36 #include "llvm/IR/Instructions.h" 37 #include "llvm/IR/Use.h" 38 #include "llvm/IR/User.h" 39 #include "llvm/IR/Value.h" 40 #include "llvm/Support/Casting.h" 41 #include <cassert> 42 #include <cstdint> 43 #include <iterator> 44 45 namespace llvm { 46 47 namespace Intrinsic { 48 typedef unsigned ID; 49 } 50 51 template <typename FunTy = const Function, typename BBTy = const BasicBlock, 52 typename ValTy = const Value, typename UserTy = const User, 53 typename UseTy = const Use, typename InstrTy = const Instruction, 54 typename CallTy = const CallInst, 55 typename InvokeTy = const InvokeInst, 56 typename CallBrTy = const CallBrInst, 57 typename IterTy = User::const_op_iterator> 58 class CallSiteBase { 59 protected: 60 PointerIntPair<InstrTy *, 2, int> I; 61 62 CallSiteBase() = default; CallSiteBase(CallTy * CI)63 CallSiteBase(CallTy *CI) : I(CI, 1) { assert(CI); } CallSiteBase(InvokeTy * II)64 CallSiteBase(InvokeTy *II) : I(II, 0) { assert(II); } CallSiteBase(CallBrTy * CBI)65 CallSiteBase(CallBrTy *CBI) : I(CBI, 2) { assert(CBI); } CallSiteBase(ValTy * II)66 explicit CallSiteBase(ValTy *II) { *this = get(II); } 67 68 private: 69 /// This static method is like a constructor. It will create an appropriate 70 /// call site for a Call, Invoke or CallBr instruction, but it can also create 71 /// a null initialized CallSiteBase object for something which is NOT a call 72 /// site. get(ValTy * V)73 static CallSiteBase get(ValTy *V) { 74 if (InstrTy *II = dyn_cast<InstrTy>(V)) { 75 if (II->getOpcode() == Instruction::Call) 76 return CallSiteBase(static_cast<CallTy*>(II)); 77 if (II->getOpcode() == Instruction::Invoke) 78 return CallSiteBase(static_cast<InvokeTy*>(II)); 79 if (II->getOpcode() == Instruction::CallBr) 80 return CallSiteBase(static_cast<CallBrTy *>(II)); 81 } 82 return CallSiteBase(); 83 } 84 85 public: 86 /// Return true if a CallInst is enclosed. isCall()87 bool isCall() const { return I.getInt() == 1; } 88 89 /// Return true if a InvokeInst is enclosed. !I.getInt() may also signify a 90 /// NULL instruction pointer, so check that. isInvoke()91 bool isInvoke() const { return getInstruction() && I.getInt() == 0; } 92 93 /// Return true if a CallBrInst is enclosed. isCallBr()94 bool isCallBr() const { return I.getInt() == 2; } 95 getInstruction()96 InstrTy *getInstruction() const { return I.getPointer(); } 97 InstrTy *operator->() const { return I.getPointer(); } 98 explicit operator bool() const { return I.getPointer(); } 99 100 /// Get the basic block containing the call site. getParent()101 BBTy* getParent() const { return getInstruction()->getParent(); } 102 103 /// Return the pointer to function that is being called. getCalledValue()104 ValTy *getCalledValue() const { 105 assert(getInstruction() && "Not a call, invoke or callbr instruction!"); 106 return *getCallee(); 107 } 108 109 /// Return the function being called if this is a direct call, otherwise 110 /// return null (if it's an indirect call). getCalledFunction()111 FunTy *getCalledFunction() const { 112 return dyn_cast<FunTy>(getCalledValue()); 113 } 114 115 /// Return true if the callsite is an indirect call. isIndirectCall()116 bool isIndirectCall() const { 117 const Value *V = getCalledValue(); 118 if (!V) 119 return false; 120 if (isa<FunTy>(V) || isa<Constant>(V)) 121 return false; 122 if (const CallBase *CB = dyn_cast<CallBase>(getInstruction())) 123 if (CB->isInlineAsm()) 124 return false; 125 return true; 126 } 127 128 /// Set the callee to the specified value. Unlike the function of the same 129 /// name on CallBase, does not modify the type! setCalledFunction(Value * V)130 void setCalledFunction(Value *V) { 131 assert(getInstruction() && "Not a call, callbr, or invoke instruction!"); 132 assert(cast<PointerType>(V->getType())->getElementType() == 133 cast<CallBase>(getInstruction())->getFunctionType() && 134 "New callee type does not match FunctionType on call"); 135 *getCallee() = V; 136 } 137 138 /// Return the intrinsic ID of the intrinsic called by this CallSite, 139 /// or Intrinsic::not_intrinsic if the called function is not an 140 /// intrinsic, or if this CallSite is an indirect call. getIntrinsicID()141 Intrinsic::ID getIntrinsicID() const { 142 if (auto *F = getCalledFunction()) 143 return F->getIntrinsicID(); 144 // Don't use Intrinsic::not_intrinsic, as it will require pulling 145 // Intrinsics.h into every header that uses CallSite. 146 return static_cast<Intrinsic::ID>(0); 147 } 148 149 /// Determine whether the passed iterator points to the callee operand's Use. isCallee(Value::const_user_iterator UI)150 bool isCallee(Value::const_user_iterator UI) const { 151 return isCallee(&UI.getUse()); 152 } 153 154 /// Determine whether this Use is the callee operand's Use. isCallee(const Use * U)155 bool isCallee(const Use *U) const { return getCallee() == U; } 156 157 /// Determine whether the passed iterator points to an argument operand. isArgOperand(Value::const_user_iterator UI)158 bool isArgOperand(Value::const_user_iterator UI) const { 159 return isArgOperand(&UI.getUse()); 160 } 161 162 /// Determine whether the passed use points to an argument operand. isArgOperand(const Use * U)163 bool isArgOperand(const Use *U) const { 164 assert(getInstruction() == U->getUser()); 165 return arg_begin() <= U && U < arg_end(); 166 } 167 168 /// Determine whether the passed iterator points to a bundle operand. isBundleOperand(Value::const_user_iterator UI)169 bool isBundleOperand(Value::const_user_iterator UI) const { 170 return isBundleOperand(&UI.getUse()); 171 } 172 173 /// Determine whether the passed use points to a bundle operand. isBundleOperand(const Use * U)174 bool isBundleOperand(const Use *U) const { 175 assert(getInstruction() == U->getUser()); 176 if (!hasOperandBundles()) 177 return false; 178 unsigned OperandNo = U - (*this)->op_begin(); 179 return getBundleOperandsStartIndex() <= OperandNo && 180 OperandNo < getBundleOperandsEndIndex(); 181 } 182 183 /// Determine whether the passed iterator points to a data operand. isDataOperand(Value::const_user_iterator UI)184 bool isDataOperand(Value::const_user_iterator UI) const { 185 return isDataOperand(&UI.getUse()); 186 } 187 188 /// Determine whether the passed use points to a data operand. isDataOperand(const Use * U)189 bool isDataOperand(const Use *U) const { 190 return data_operands_begin() <= U && U < data_operands_end(); 191 } 192 getArgument(unsigned ArgNo)193 ValTy *getArgument(unsigned ArgNo) const { 194 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!"); 195 return *(arg_begin() + ArgNo); 196 } 197 setArgument(unsigned ArgNo,Value * newVal)198 void setArgument(unsigned ArgNo, Value* newVal) { 199 assert(getInstruction() && "Not a call, invoke or callbr instruction!"); 200 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!"); 201 getInstruction()->setOperand(ArgNo, newVal); 202 } 203 204 /// Given a value use iterator, returns the argument that corresponds to it. 205 /// Iterator must actually correspond to an argument. getArgumentNo(Value::const_user_iterator I)206 unsigned getArgumentNo(Value::const_user_iterator I) const { 207 return getArgumentNo(&I.getUse()); 208 } 209 210 /// Given a use for an argument, get the argument number that corresponds to 211 /// it. getArgumentNo(const Use * U)212 unsigned getArgumentNo(const Use *U) const { 213 assert(getInstruction() && "Not a call, invoke or callbr instruction!"); 214 assert(isArgOperand(U) && "Argument # out of range!"); 215 return U - arg_begin(); 216 } 217 218 /// The type of iterator to use when looping over actual arguments at this 219 /// call site. 220 using arg_iterator = IterTy; 221 args()222 iterator_range<IterTy> args() const { 223 return make_range(arg_begin(), arg_end()); 224 } arg_empty()225 bool arg_empty() const { return arg_end() == arg_begin(); } arg_size()226 unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); } 227 228 /// Given a value use iterator, return the data operand corresponding to it. 229 /// Iterator must actually correspond to a data operand. getDataOperandNo(Value::const_user_iterator UI)230 unsigned getDataOperandNo(Value::const_user_iterator UI) const { 231 return getDataOperandNo(&UI.getUse()); 232 } 233 234 /// Given a use for a data operand, get the data operand number that 235 /// corresponds to it. getDataOperandNo(const Use * U)236 unsigned getDataOperandNo(const Use *U) const { 237 assert(getInstruction() && "Not a call, invoke or callbr instruction!"); 238 assert(isDataOperand(U) && "Data operand # out of range!"); 239 return U - data_operands_begin(); 240 } 241 242 /// Type of iterator to use when looping over data operands at this call site 243 /// (see below). 244 using data_operand_iterator = IterTy; 245 246 /// data_operands_begin/data_operands_end - Return iterators iterating over 247 /// the call / invoke / callbr argument list and bundle operands. For invokes, 248 /// this is the set of instruction operands except the invoke target and the 249 /// two successor blocks; for calls this is the set of instruction operands 250 /// except the call target; for callbrs the number of labels to skip must be 251 /// determined first. 252 data_operands_begin()253 IterTy data_operands_begin() const { 254 assert(getInstruction() && "Not a call or invoke instruction!"); 255 return cast<CallBase>(getInstruction())->data_operands_begin(); 256 } data_operands_end()257 IterTy data_operands_end() const { 258 assert(getInstruction() && "Not a call or invoke instruction!"); 259 return cast<CallBase>(getInstruction())->data_operands_end(); 260 } data_ops()261 iterator_range<IterTy> data_ops() const { 262 return make_range(data_operands_begin(), data_operands_end()); 263 } data_operands_empty()264 bool data_operands_empty() const { 265 return data_operands_end() == data_operands_begin(); 266 } data_operands_size()267 unsigned data_operands_size() const { 268 return std::distance(data_operands_begin(), data_operands_end()); 269 } 270 271 /// Return the type of the instruction that generated this call site. getType()272 Type *getType() const { return (*this)->getType(); } 273 274 /// Return the caller function for this call site. getCaller()275 FunTy *getCaller() const { return (*this)->getParent()->getParent(); } 276 277 /// Tests if this call site must be tail call optimized. Only a CallInst can 278 /// be tail call optimized. isMustTailCall()279 bool isMustTailCall() const { 280 return isCall() && cast<CallInst>(getInstruction())->isMustTailCall(); 281 } 282 283 /// Tests if this call site is marked as a tail call. isTailCall()284 bool isTailCall() const { 285 return isCall() && cast<CallInst>(getInstruction())->isTailCall(); 286 } 287 288 #define CALLSITE_DELEGATE_GETTER(METHOD) \ 289 InstrTy *II = getInstruction(); \ 290 return isCall() ? cast<CallInst>(II)->METHOD \ 291 : isCallBr() ? cast<CallBrInst>(II)->METHOD \ 292 : cast<InvokeInst>(II)->METHOD 293 294 #define CALLSITE_DELEGATE_SETTER(METHOD) \ 295 InstrTy *II = getInstruction(); \ 296 if (isCall()) \ 297 cast<CallInst>(II)->METHOD; \ 298 else if (isCallBr()) \ 299 cast<CallBrInst>(II)->METHOD; \ 300 else \ 301 cast<InvokeInst>(II)->METHOD 302 getNumArgOperands()303 unsigned getNumArgOperands() const { 304 CALLSITE_DELEGATE_GETTER(getNumArgOperands()); 305 } 306 getArgOperand(unsigned i)307 ValTy *getArgOperand(unsigned i) const { 308 CALLSITE_DELEGATE_GETTER(getArgOperand(i)); 309 } 310 getReturnedArgOperand()311 ValTy *getReturnedArgOperand() const { 312 CALLSITE_DELEGATE_GETTER(getReturnedArgOperand()); 313 } 314 isInlineAsm()315 bool isInlineAsm() const { 316 return cast<CallBase>(getInstruction())->isInlineAsm(); 317 } 318 319 /// Get the calling convention of the call. getCallingConv()320 CallingConv::ID getCallingConv() const { 321 CALLSITE_DELEGATE_GETTER(getCallingConv()); 322 } 323 /// Set the calling convention of the call. setCallingConv(CallingConv::ID CC)324 void setCallingConv(CallingConv::ID CC) { 325 CALLSITE_DELEGATE_SETTER(setCallingConv(CC)); 326 } 327 getFunctionType()328 FunctionType *getFunctionType() const { 329 CALLSITE_DELEGATE_GETTER(getFunctionType()); 330 } 331 mutateFunctionType(FunctionType * Ty)332 void mutateFunctionType(FunctionType *Ty) const { 333 CALLSITE_DELEGATE_SETTER(mutateFunctionType(Ty)); 334 } 335 336 /// Get the parameter attributes of the call. getAttributes()337 AttributeList getAttributes() const { 338 CALLSITE_DELEGATE_GETTER(getAttributes()); 339 } 340 /// Set the parameter attributes of the call. setAttributes(AttributeList PAL)341 void setAttributes(AttributeList PAL) { 342 CALLSITE_DELEGATE_SETTER(setAttributes(PAL)); 343 } 344 addAttribute(unsigned i,Attribute::AttrKind Kind)345 void addAttribute(unsigned i, Attribute::AttrKind Kind) { 346 CALLSITE_DELEGATE_SETTER(addAttribute(i, Kind)); 347 } 348 addAttribute(unsigned i,Attribute Attr)349 void addAttribute(unsigned i, Attribute Attr) { 350 CALLSITE_DELEGATE_SETTER(addAttribute(i, Attr)); 351 } 352 addParamAttr(unsigned ArgNo,Attribute::AttrKind Kind)353 void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { 354 CALLSITE_DELEGATE_SETTER(addParamAttr(ArgNo, Kind)); 355 } 356 removeAttribute(unsigned i,Attribute::AttrKind Kind)357 void removeAttribute(unsigned i, Attribute::AttrKind Kind) { 358 CALLSITE_DELEGATE_SETTER(removeAttribute(i, Kind)); 359 } 360 removeAttribute(unsigned i,StringRef Kind)361 void removeAttribute(unsigned i, StringRef Kind) { 362 CALLSITE_DELEGATE_SETTER(removeAttribute(i, Kind)); 363 } 364 removeParamAttr(unsigned ArgNo,Attribute::AttrKind Kind)365 void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { 366 CALLSITE_DELEGATE_SETTER(removeParamAttr(ArgNo, Kind)); 367 } 368 369 /// Return true if this function has the given attribute. hasFnAttr(Attribute::AttrKind Kind)370 bool hasFnAttr(Attribute::AttrKind Kind) const { 371 CALLSITE_DELEGATE_GETTER(hasFnAttr(Kind)); 372 } 373 374 /// Return true if this function has the given attribute. hasFnAttr(StringRef Kind)375 bool hasFnAttr(StringRef Kind) const { 376 CALLSITE_DELEGATE_GETTER(hasFnAttr(Kind)); 377 } 378 379 /// Return true if this return value has the given attribute. hasRetAttr(Attribute::AttrKind Kind)380 bool hasRetAttr(Attribute::AttrKind Kind) const { 381 CALLSITE_DELEGATE_GETTER(hasRetAttr(Kind)); 382 } 383 384 /// Return true if the call or the callee has the given attribute. paramHasAttr(unsigned ArgNo,Attribute::AttrKind Kind)385 bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const { 386 CALLSITE_DELEGATE_GETTER(paramHasAttr(ArgNo, Kind)); 387 } 388 getAttribute(unsigned i,Attribute::AttrKind Kind)389 Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const { 390 CALLSITE_DELEGATE_GETTER(getAttribute(i, Kind)); 391 } 392 getAttribute(unsigned i,StringRef Kind)393 Attribute getAttribute(unsigned i, StringRef Kind) const { 394 CALLSITE_DELEGATE_GETTER(getAttribute(i, Kind)); 395 } 396 397 /// Return true if the data operand at index \p i directly or indirectly has 398 /// the attribute \p A. 399 /// 400 /// Normal call, invoke or callbr arguments have per operand attributes, as 401 /// specified in the attribute set attached to this instruction, while operand 402 /// bundle operands may have some attributes implied by the type of its 403 /// containing operand bundle. dataOperandHasImpliedAttr(unsigned i,Attribute::AttrKind Kind)404 bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const { 405 CALLSITE_DELEGATE_GETTER(dataOperandHasImpliedAttr(i, Kind)); 406 } 407 408 /// Extract the alignment of the return value. getRetAlignment()409 unsigned getRetAlignment() const { 410 CALLSITE_DELEGATE_GETTER(getRetAlignment()); 411 } 412 413 /// Extract the alignment for a call or parameter (0=unknown). getParamAlignment(unsigned ArgNo)414 unsigned getParamAlignment(unsigned ArgNo) const { 415 CALLSITE_DELEGATE_GETTER(getParamAlignment(ArgNo)); 416 } 417 418 /// Extract the byval type for a call or parameter (nullptr=unknown). getParamByValType(unsigned ArgNo)419 Type *getParamByValType(unsigned ArgNo) const { 420 CALLSITE_DELEGATE_GETTER(getParamByValType(ArgNo)); 421 } 422 423 /// Extract the number of dereferenceable bytes for a call or parameter 424 /// (0=unknown). getDereferenceableBytes(unsigned i)425 uint64_t getDereferenceableBytes(unsigned i) const { 426 CALLSITE_DELEGATE_GETTER(getDereferenceableBytes(i)); 427 } 428 429 /// Extract the number of dereferenceable_or_null bytes for a call or 430 /// parameter (0=unknown). getDereferenceableOrNullBytes(unsigned i)431 uint64_t getDereferenceableOrNullBytes(unsigned i) const { 432 CALLSITE_DELEGATE_GETTER(getDereferenceableOrNullBytes(i)); 433 } 434 435 /// Determine if the return value is marked with NoAlias attribute. returnDoesNotAlias()436 bool returnDoesNotAlias() const { 437 CALLSITE_DELEGATE_GETTER(returnDoesNotAlias()); 438 } 439 440 /// Return true if the call should not be treated as a call to a builtin. isNoBuiltin()441 bool isNoBuiltin() const { 442 CALLSITE_DELEGATE_GETTER(isNoBuiltin()); 443 } 444 445 /// Return true if the call requires strict floating point semantics. isStrictFP()446 bool isStrictFP() const { 447 CALLSITE_DELEGATE_GETTER(isStrictFP()); 448 } 449 450 /// Return true if the call should not be inlined. isNoInline()451 bool isNoInline() const { 452 CALLSITE_DELEGATE_GETTER(isNoInline()); 453 } 454 void setIsNoInline(bool Value = true) { 455 CALLSITE_DELEGATE_SETTER(setIsNoInline(Value)); 456 } 457 458 /// Determine if the call does not access memory. doesNotAccessMemory()459 bool doesNotAccessMemory() const { 460 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory()); 461 } setDoesNotAccessMemory()462 void setDoesNotAccessMemory() { 463 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory()); 464 } 465 466 /// Determine if the call does not access or only reads memory. onlyReadsMemory()467 bool onlyReadsMemory() const { 468 CALLSITE_DELEGATE_GETTER(onlyReadsMemory()); 469 } setOnlyReadsMemory()470 void setOnlyReadsMemory() { 471 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory()); 472 } 473 474 /// Determine if the call does not access or only writes memory. doesNotReadMemory()475 bool doesNotReadMemory() const { 476 CALLSITE_DELEGATE_GETTER(doesNotReadMemory()); 477 } setDoesNotReadMemory()478 void setDoesNotReadMemory() { 479 CALLSITE_DELEGATE_SETTER(setDoesNotReadMemory()); 480 } 481 482 /// Determine if the call can access memmory only using pointers based 483 /// on its arguments. onlyAccessesArgMemory()484 bool onlyAccessesArgMemory() const { 485 CALLSITE_DELEGATE_GETTER(onlyAccessesArgMemory()); 486 } setOnlyAccessesArgMemory()487 void setOnlyAccessesArgMemory() { 488 CALLSITE_DELEGATE_SETTER(setOnlyAccessesArgMemory()); 489 } 490 491 /// Determine if the function may only access memory that is 492 /// inaccessible from the IR. onlyAccessesInaccessibleMemory()493 bool onlyAccessesInaccessibleMemory() const { 494 CALLSITE_DELEGATE_GETTER(onlyAccessesInaccessibleMemory()); 495 } setOnlyAccessesInaccessibleMemory()496 void setOnlyAccessesInaccessibleMemory() { 497 CALLSITE_DELEGATE_SETTER(setOnlyAccessesInaccessibleMemory()); 498 } 499 500 /// Determine if the function may only access memory that is 501 /// either inaccessible from the IR or pointed to by its arguments. onlyAccessesInaccessibleMemOrArgMem()502 bool onlyAccessesInaccessibleMemOrArgMem() const { 503 CALLSITE_DELEGATE_GETTER(onlyAccessesInaccessibleMemOrArgMem()); 504 } setOnlyAccessesInaccessibleMemOrArgMem()505 void setOnlyAccessesInaccessibleMemOrArgMem() { 506 CALLSITE_DELEGATE_SETTER(setOnlyAccessesInaccessibleMemOrArgMem()); 507 } 508 509 /// Determine if the call cannot return. doesNotReturn()510 bool doesNotReturn() const { 511 CALLSITE_DELEGATE_GETTER(doesNotReturn()); 512 } setDoesNotReturn()513 void setDoesNotReturn() { 514 CALLSITE_DELEGATE_SETTER(setDoesNotReturn()); 515 } 516 517 /// Determine if the call cannot unwind. doesNotThrow()518 bool doesNotThrow() const { 519 CALLSITE_DELEGATE_GETTER(doesNotThrow()); 520 } setDoesNotThrow()521 void setDoesNotThrow() { 522 CALLSITE_DELEGATE_SETTER(setDoesNotThrow()); 523 } 524 525 /// Determine if the call can be duplicated. cannotDuplicate()526 bool cannotDuplicate() const { 527 CALLSITE_DELEGATE_GETTER(cannotDuplicate()); 528 } setCannotDuplicate()529 void setCannotDuplicate() { 530 CALLSITE_DELEGATE_SETTER(setCannotDuplicate()); 531 } 532 533 /// Determine if the call is convergent. isConvergent()534 bool isConvergent() const { 535 CALLSITE_DELEGATE_GETTER(isConvergent()); 536 } setConvergent()537 void setConvergent() { 538 CALLSITE_DELEGATE_SETTER(setConvergent()); 539 } setNotConvergent()540 void setNotConvergent() { 541 CALLSITE_DELEGATE_SETTER(setNotConvergent()); 542 } 543 getNumOperandBundles()544 unsigned getNumOperandBundles() const { 545 CALLSITE_DELEGATE_GETTER(getNumOperandBundles()); 546 } 547 hasOperandBundles()548 bool hasOperandBundles() const { 549 CALLSITE_DELEGATE_GETTER(hasOperandBundles()); 550 } 551 getBundleOperandsStartIndex()552 unsigned getBundleOperandsStartIndex() const { 553 CALLSITE_DELEGATE_GETTER(getBundleOperandsStartIndex()); 554 } 555 getBundleOperandsEndIndex()556 unsigned getBundleOperandsEndIndex() const { 557 CALLSITE_DELEGATE_GETTER(getBundleOperandsEndIndex()); 558 } 559 getNumTotalBundleOperands()560 unsigned getNumTotalBundleOperands() const { 561 CALLSITE_DELEGATE_GETTER(getNumTotalBundleOperands()); 562 } 563 getOperandBundleAt(unsigned Index)564 OperandBundleUse getOperandBundleAt(unsigned Index) const { 565 CALLSITE_DELEGATE_GETTER(getOperandBundleAt(Index)); 566 } 567 getOperandBundle(StringRef Name)568 Optional<OperandBundleUse> getOperandBundle(StringRef Name) const { 569 CALLSITE_DELEGATE_GETTER(getOperandBundle(Name)); 570 } 571 getOperandBundle(uint32_t ID)572 Optional<OperandBundleUse> getOperandBundle(uint32_t ID) const { 573 CALLSITE_DELEGATE_GETTER(getOperandBundle(ID)); 574 } 575 countOperandBundlesOfType(uint32_t ID)576 unsigned countOperandBundlesOfType(uint32_t ID) const { 577 CALLSITE_DELEGATE_GETTER(countOperandBundlesOfType(ID)); 578 } 579 isBundleOperand(unsigned Idx)580 bool isBundleOperand(unsigned Idx) const { 581 CALLSITE_DELEGATE_GETTER(isBundleOperand(Idx)); 582 } 583 arg_begin()584 IterTy arg_begin() const { 585 CALLSITE_DELEGATE_GETTER(arg_begin()); 586 } 587 arg_end()588 IterTy arg_end() const { 589 CALLSITE_DELEGATE_GETTER(arg_end()); 590 } 591 592 #undef CALLSITE_DELEGATE_GETTER 593 #undef CALLSITE_DELEGATE_SETTER 594 getOperandBundlesAsDefs(SmallVectorImpl<OperandBundleDef> & Defs)595 void getOperandBundlesAsDefs(SmallVectorImpl<OperandBundleDef> &Defs) const { 596 // Since this is actually a getter that "looks like" a setter, don't use the 597 // above macros to avoid confusion. 598 cast<CallBase>(getInstruction())->getOperandBundlesAsDefs(Defs); 599 } 600 601 /// Determine whether this data operand is not captured. doesNotCapture(unsigned OpNo)602 bool doesNotCapture(unsigned OpNo) const { 603 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::NoCapture); 604 } 605 606 /// Determine whether this argument is passed by value. isByValArgument(unsigned ArgNo)607 bool isByValArgument(unsigned ArgNo) const { 608 return paramHasAttr(ArgNo, Attribute::ByVal); 609 } 610 611 /// Determine whether this argument is passed in an alloca. isInAllocaArgument(unsigned ArgNo)612 bool isInAllocaArgument(unsigned ArgNo) const { 613 return paramHasAttr(ArgNo, Attribute::InAlloca); 614 } 615 616 /// Determine whether this argument is passed by value or in an alloca. isByValOrInAllocaArgument(unsigned ArgNo)617 bool isByValOrInAllocaArgument(unsigned ArgNo) const { 618 return paramHasAttr(ArgNo, Attribute::ByVal) || 619 paramHasAttr(ArgNo, Attribute::InAlloca); 620 } 621 622 /// Determine if there are is an inalloca argument. Only the last argument can 623 /// have the inalloca attribute. hasInAllocaArgument()624 bool hasInAllocaArgument() const { 625 return !arg_empty() && paramHasAttr(arg_size() - 1, Attribute::InAlloca); 626 } 627 doesNotAccessMemory(unsigned OpNo)628 bool doesNotAccessMemory(unsigned OpNo) const { 629 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone); 630 } 631 onlyReadsMemory(unsigned OpNo)632 bool onlyReadsMemory(unsigned OpNo) const { 633 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadOnly) || 634 dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone); 635 } 636 doesNotReadMemory(unsigned OpNo)637 bool doesNotReadMemory(unsigned OpNo) const { 638 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::WriteOnly) || 639 dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone); 640 } 641 642 /// Return true if the return value is known to be not null. 643 /// This may be because it has the nonnull attribute, or because at least 644 /// one byte is dereferenceable and the pointer is in addrspace(0). isReturnNonNull()645 bool isReturnNonNull() const { 646 if (hasRetAttr(Attribute::NonNull)) 647 return true; 648 else if (getDereferenceableBytes(AttributeList::ReturnIndex) > 0 && 649 !NullPointerIsDefined(getCaller(), 650 getType()->getPointerAddressSpace())) 651 return true; 652 653 return false; 654 } 655 656 /// Returns true if this CallSite passes the given Value* as an argument to 657 /// the called function. hasArgument(const Value * Arg)658 bool hasArgument(const Value *Arg) const { 659 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; 660 ++AI) 661 if (AI->get() == Arg) 662 return true; 663 return false; 664 } 665 666 private: getCallee()667 IterTy getCallee() const { 668 return cast<CallBase>(getInstruction())->op_end() - 1; 669 } 670 }; 671 672 class CallSite : public CallSiteBase<Function, BasicBlock, Value, User, Use, 673 Instruction, CallInst, InvokeInst, 674 CallBrInst, User::op_iterator> { 675 public: 676 CallSite() = default; CallSite(CallSiteBase B)677 CallSite(CallSiteBase B) : CallSiteBase(B) {} CallSite(CallInst * CI)678 CallSite(CallInst *CI) : CallSiteBase(CI) {} CallSite(InvokeInst * II)679 CallSite(InvokeInst *II) : CallSiteBase(II) {} CallSite(CallBrInst * CBI)680 CallSite(CallBrInst *CBI) : CallSiteBase(CBI) {} CallSite(Instruction * II)681 explicit CallSite(Instruction *II) : CallSiteBase(II) {} CallSite(Value * V)682 explicit CallSite(Value *V) : CallSiteBase(V) {} 683 684 bool operator==(const CallSite &CS) const { return I == CS.I; } 685 bool operator!=(const CallSite &CS) const { return I != CS.I; } 686 bool operator<(const CallSite &CS) const { 687 return getInstruction() < CS.getInstruction(); 688 } 689 690 private: 691 friend struct DenseMapInfo<CallSite>; 692 693 User::op_iterator getCallee() const; 694 }; 695 696 /// Establish a view to a call site for examination. 697 class ImmutableCallSite : public CallSiteBase<> { 698 public: 699 ImmutableCallSite() = default; 700 ImmutableCallSite(const CallInst *CI) : CallSiteBase(CI) {} 701 ImmutableCallSite(const InvokeInst *II) : CallSiteBase(II) {} 702 ImmutableCallSite(const CallBrInst *CBI) : CallSiteBase(CBI) {} 703 explicit ImmutableCallSite(const Instruction *II) : CallSiteBase(II) {} 704 explicit ImmutableCallSite(const Value *V) : CallSiteBase(V) {} 705 ImmutableCallSite(CallSite CS) : CallSiteBase(CS.getInstruction()) {} 706 }; 707 708 /// AbstractCallSite 709 /// 710 /// An abstract call site is a wrapper that allows to treat direct, 711 /// indirect, and callback calls the same. If an abstract call site 712 /// represents a direct or indirect call site it behaves like a stripped 713 /// down version of a normal call site object. The abstract call site can 714 /// also represent a callback call, thus the fact that the initially 715 /// called function (=broker) may invoke a third one (=callback callee). 716 /// In this case, the abstract call site hides the middle man, hence the 717 /// broker function. The result is a representation of the callback call, 718 /// inside the broker, but in the context of the original call to the broker. 719 /// 720 /// There are up to three functions involved when we talk about callback call 721 /// sites. The caller (1), which invokes the broker function. The broker 722 /// function (2), that will invoke the callee zero or more times. And finally 723 /// the callee (3), which is the target of the callback call. 724 /// 725 /// The abstract call site will handle the mapping from parameters to arguments 726 /// depending on the semantic of the broker function. However, it is important 727 /// to note that the mapping is often partial. Thus, some arguments of the 728 /// call/invoke instruction are mapped to parameters of the callee while others 729 /// are not. 730 class AbstractCallSite { 731 public: 732 733 /// The encoding of a callback with regards to the underlying instruction. 734 struct CallbackInfo { 735 736 /// For direct/indirect calls the parameter encoding is empty. If it is not, 737 /// the abstract call site represents a callback. In that case, the first 738 /// element of the encoding vector represents which argument of the call 739 /// site CS is the callback callee. The remaining elements map parameters 740 /// (identified by their position) to the arguments that will be passed 741 /// through (also identified by position but in the call site instruction). 742 /// 743 /// NOTE that we use LLVM argument numbers (starting at 0) and not 744 /// clang/source argument numbers (starting at 1). The -1 entries represent 745 /// unknown values that are passed to the callee. 746 using ParameterEncodingTy = SmallVector<int, 0>; 747 ParameterEncodingTy ParameterEncoding; 748 749 }; 750 751 private: 752 753 /// The underlying call site: 754 /// caller -> callee, if this is a direct or indirect call site 755 /// caller -> broker function, if this is a callback call site 756 CallSite CS; 757 758 /// The encoding of a callback with regards to the underlying instruction. 759 CallbackInfo CI; 760 761 public: 762 /// Sole constructor for abstract call sites (ACS). 763 /// 764 /// An abstract call site can only be constructed through a llvm::Use because 765 /// each operand (=use) of an instruction could potentially be a different 766 /// abstract call site. Furthermore, even if the value of the llvm::Use is the 767 /// same, and the user is as well, the abstract call sites might not be. 768 /// 769 /// If a use is not associated with an abstract call site the constructed ACS 770 /// will evaluate to false if converted to a boolean. 771 /// 772 /// If the use is the callee use of a call or invoke instruction, the 773 /// constructed abstract call site will behave as a llvm::CallSite would. 774 /// 775 /// If the use is not a callee use of a call or invoke instruction, the 776 /// callback metadata is used to determine the argument <-> parameter mapping 777 /// as well as the callee of the abstract call site. 778 AbstractCallSite(const Use *U); 779 780 /// Add operand uses of \p ICS that represent callback uses into \p CBUses. 781 /// 782 /// All uses added to \p CBUses can be used to create abstract call sites for 783 /// which AbstractCallSite::isCallbackCall() will return true. 784 static void getCallbackUses(ImmutableCallSite ICS, 785 SmallVectorImpl<const Use *> &CBUses); 786 787 /// Conversion operator to conveniently check for a valid/initialized ACS. 788 explicit operator bool() const { return (bool)CS; } 789 790 /// Return the underlying instruction. 791 Instruction *getInstruction() const { return CS.getInstruction(); } 792 793 /// Return the call site abstraction for the underlying instruction. 794 CallSite getCallSite() const { return CS; } 795 796 /// Return true if this ACS represents a direct call. 797 bool isDirectCall() const { 798 return !isCallbackCall() && !CS.isIndirectCall(); 799 } 800 801 /// Return true if this ACS represents an indirect call. 802 bool isIndirectCall() const { 803 return !isCallbackCall() && CS.isIndirectCall(); 804 } 805 806 /// Return true if this ACS represents a callback call. 807 bool isCallbackCall() const { 808 // For a callback call site the callee is ALWAYS stored first in the 809 // transitive values vector. Thus, a non-empty vector indicates a callback. 810 return !CI.ParameterEncoding.empty(); 811 } 812 813 /// Return true if @p UI is the use that defines the callee of this ACS. 814 bool isCallee(Value::const_user_iterator UI) const { 815 return isCallee(&UI.getUse()); 816 } 817 818 /// Return true if @p U is the use that defines the callee of this ACS. 819 bool isCallee(const Use *U) const { 820 if (isDirectCall()) 821 return CS.isCallee(U); 822 823 assert(!CI.ParameterEncoding.empty() && 824 "Callback without parameter encoding!"); 825 826 return (int)CS.getArgumentNo(U) == CI.ParameterEncoding[0]; 827 } 828 829 /// Return the number of parameters of the callee. 830 unsigned getNumArgOperands() const { 831 if (isDirectCall()) 832 return CS.getNumArgOperands(); 833 // Subtract 1 for the callee encoding. 834 return CI.ParameterEncoding.size() - 1; 835 } 836 837 /// Return the operand index of the underlying instruction associated with @p 838 /// Arg. 839 int getCallArgOperandNo(Argument &Arg) const { 840 return getCallArgOperandNo(Arg.getArgNo()); 841 } 842 843 /// Return the operand index of the underlying instruction associated with 844 /// the function parameter number @p ArgNo or -1 if there is none. 845 int getCallArgOperandNo(unsigned ArgNo) const { 846 if (isDirectCall()) 847 return ArgNo; 848 // Add 1 for the callee encoding. 849 return CI.ParameterEncoding[ArgNo + 1]; 850 } 851 852 /// Return the operand of the underlying instruction associated with @p Arg. 853 Value *getCallArgOperand(Argument &Arg) const { 854 return getCallArgOperand(Arg.getArgNo()); 855 } 856 857 /// Return the operand of the underlying instruction associated with the 858 /// function parameter number @p ArgNo or nullptr if there is none. 859 Value *getCallArgOperand(unsigned ArgNo) const { 860 if (isDirectCall()) 861 return CS.getArgOperand(ArgNo); 862 // Add 1 for the callee encoding. 863 return CI.ParameterEncoding[ArgNo + 1] >= 0 864 ? CS.getArgOperand(CI.ParameterEncoding[ArgNo + 1]) 865 : nullptr; 866 } 867 868 /// Return the operand index of the underlying instruction associated with the 869 /// callee of this ACS. Only valid for callback calls! 870 int getCallArgOperandNoForCallee() const { 871 assert(isCallbackCall()); 872 assert(CI.ParameterEncoding.size() && CI.ParameterEncoding[0] >= 0); 873 return CI.ParameterEncoding[0]; 874 } 875 876 /// Return the use of the callee value in the underlying instruction. Only 877 /// valid for callback calls! 878 const Use &getCalleeUseForCallback() const { 879 int CalleeArgIdx = getCallArgOperandNoForCallee(); 880 assert(CalleeArgIdx >= 0 && 881 unsigned(CalleeArgIdx) < getInstruction()->getNumOperands()); 882 return getInstruction()->getOperandUse(CalleeArgIdx); 883 } 884 885 /// Return the pointer to function that is being called. 886 Value *getCalledValue() const { 887 if (isDirectCall()) 888 return CS.getCalledValue(); 889 return CS.getArgOperand(getCallArgOperandNoForCallee()); 890 } 891 892 /// Return the function being called if this is a direct call, otherwise 893 /// return null (if it's an indirect call). 894 Function *getCalledFunction() const { 895 Value *V = getCalledValue(); 896 return V ? dyn_cast<Function>(V->stripPointerCasts()) : nullptr; 897 } 898 }; 899 900 template <> struct DenseMapInfo<CallSite> { 901 using BaseInfo = DenseMapInfo<decltype(CallSite::I)>; 902 903 static CallSite getEmptyKey() { 904 CallSite CS; 905 CS.I = BaseInfo::getEmptyKey(); 906 return CS; 907 } 908 909 static CallSite getTombstoneKey() { 910 CallSite CS; 911 CS.I = BaseInfo::getTombstoneKey(); 912 return CS; 913 } 914 915 static unsigned getHashValue(const CallSite &CS) { 916 return BaseInfo::getHashValue(CS.I); 917 } 918 919 static bool isEqual(const CallSite &LHS, const CallSite &RHS) { 920 return LHS == RHS; 921 } 922 }; 923 924 } // end namespace llvm 925 926 #endif // LLVM_IR_CALLSITE_H 927