AbstractCallSite.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //===- AbstractCallSite.h - Abstract call sites -----------------*- 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 AbstractCallSite class, which is a is a wrapper that
  10. // allows treating direct, indirect, and callback calls the same.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_IR_ABSTRACTCALLSITE_H
  14. #define LLVM_IR_ABSTRACTCALLSITE_H
  15. #include "llvm/IR/Function.h"
  16. #include "llvm/IR/InstrTypes.h"
  17. #include "llvm/IR/Instruction.h"
  18. #include "llvm/IR/Use.h"
  19. #include "llvm/IR/User.h"
  20. #include "llvm/IR/Value.h"
  21. #include "llvm/Support/Casting.h"
  22. #include <cassert>
  23. namespace llvm {
  24. /// AbstractCallSite
  25. ///
  26. /// An abstract call site is a wrapper that allows to treat direct,
  27. /// indirect, and callback calls the same. If an abstract call site
  28. /// represents a direct or indirect call site it behaves like a stripped
  29. /// down version of a normal call site object. The abstract call site can
  30. /// also represent a callback call, thus the fact that the initially
  31. /// called function (=broker) may invoke a third one (=callback callee).
  32. /// In this case, the abstract call site hides the middle man, hence the
  33. /// broker function. The result is a representation of the callback call,
  34. /// inside the broker, but in the context of the original call to the broker.
  35. ///
  36. /// There are up to three functions involved when we talk about callback call
  37. /// sites. The caller (1), which invokes the broker function. The broker
  38. /// function (2), that will invoke the callee zero or more times. And finally
  39. /// the callee (3), which is the target of the callback call.
  40. ///
  41. /// The abstract call site will handle the mapping from parameters to arguments
  42. /// depending on the semantic of the broker function. However, it is important
  43. /// to note that the mapping is often partial. Thus, some arguments of the
  44. /// call/invoke instruction are mapped to parameters of the callee while others
  45. /// are not.
  46. class AbstractCallSite {
  47. public:
  48. /// The encoding of a callback with regards to the underlying instruction.
  49. struct CallbackInfo {
  50. /// For direct/indirect calls the parameter encoding is empty. If it is not,
  51. /// the abstract call site represents a callback. In that case, the first
  52. /// element of the encoding vector represents which argument of the call
  53. /// site CB is the callback callee. The remaining elements map parameters
  54. /// (identified by their position) to the arguments that will be passed
  55. /// through (also identified by position but in the call site instruction).
  56. ///
  57. /// NOTE that we use LLVM argument numbers (starting at 0) and not
  58. /// clang/source argument numbers (starting at 1). The -1 entries represent
  59. /// unknown values that are passed to the callee.
  60. using ParameterEncodingTy = SmallVector<int, 0>;
  61. ParameterEncodingTy ParameterEncoding;
  62. };
  63. private:
  64. /// The underlying call site:
  65. /// caller -> callee, if this is a direct or indirect call site
  66. /// caller -> broker function, if this is a callback call site
  67. CallBase *CB;
  68. /// The encoding of a callback with regards to the underlying instruction.
  69. CallbackInfo CI;
  70. public:
  71. /// Sole constructor for abstract call sites (ACS).
  72. ///
  73. /// An abstract call site can only be constructed through a llvm::Use because
  74. /// each operand (=use) of an instruction could potentially be a different
  75. /// abstract call site. Furthermore, even if the value of the llvm::Use is the
  76. /// same, and the user is as well, the abstract call sites might not be.
  77. ///
  78. /// If a use is not associated with an abstract call site the constructed ACS
  79. /// will evaluate to false if converted to a boolean.
  80. ///
  81. /// If the use is the callee use of a call or invoke instruction, the
  82. /// constructed abstract call site will behave as a llvm::CallSite would.
  83. ///
  84. /// If the use is not a callee use of a call or invoke instruction, the
  85. /// callback metadata is used to determine the argument <-> parameter mapping
  86. /// as well as the callee of the abstract call site.
  87. AbstractCallSite(const Use *U);
  88. /// Add operand uses of \p CB that represent callback uses into
  89. /// \p CallbackUses.
  90. ///
  91. /// All uses added to \p CallbackUses can be used to create abstract call
  92. /// sites for which AbstractCallSite::isCallbackCall() will return true.
  93. static void getCallbackUses(const CallBase &CB,
  94. SmallVectorImpl<const Use *> &CallbackUses);
  95. /// Conversion operator to conveniently check for a valid/initialized ACS.
  96. explicit operator bool() const { return CB != nullptr; }
  97. /// Return the underlying instruction.
  98. CallBase *getInstruction() const { return CB; }
  99. /// Return true if this ACS represents a direct call.
  100. bool isDirectCall() const {
  101. return !isCallbackCall() && !CB->isIndirectCall();
  102. }
  103. /// Return true if this ACS represents an indirect call.
  104. bool isIndirectCall() const {
  105. return !isCallbackCall() && CB->isIndirectCall();
  106. }
  107. /// Return true if this ACS represents a callback call.
  108. bool isCallbackCall() const {
  109. // For a callback call site the callee is ALWAYS stored first in the
  110. // transitive values vector. Thus, a non-empty vector indicates a callback.
  111. return !CI.ParameterEncoding.empty();
  112. }
  113. /// Return true if @p UI is the use that defines the callee of this ACS.
  114. bool isCallee(Value::const_user_iterator UI) const {
  115. return isCallee(&UI.getUse());
  116. }
  117. /// Return true if @p U is the use that defines the callee of this ACS.
  118. bool isCallee(const Use *U) const {
  119. if (isDirectCall())
  120. return CB->isCallee(U);
  121. assert(!CI.ParameterEncoding.empty() &&
  122. "Callback without parameter encoding!");
  123. // If the use is actually in a constant cast expression which itself
  124. // has only one use, we look through the constant cast expression.
  125. if (auto *CE = dyn_cast<ConstantExpr>(U->getUser()))
  126. if (CE->hasOneUse() && CE->isCast())
  127. U = &*CE->use_begin();
  128. return (int)CB->getArgOperandNo(U) == CI.ParameterEncoding[0];
  129. }
  130. /// Return the number of parameters of the callee.
  131. unsigned getNumArgOperands() const {
  132. if (isDirectCall())
  133. return CB->getNumArgOperands();
  134. // Subtract 1 for the callee encoding.
  135. return CI.ParameterEncoding.size() - 1;
  136. }
  137. /// Return the operand index of the underlying instruction associated with @p
  138. /// Arg.
  139. int getCallArgOperandNo(Argument &Arg) const {
  140. return getCallArgOperandNo(Arg.getArgNo());
  141. }
  142. /// Return the operand index of the underlying instruction associated with
  143. /// the function parameter number @p ArgNo or -1 if there is none.
  144. int getCallArgOperandNo(unsigned ArgNo) const {
  145. if (isDirectCall())
  146. return ArgNo;
  147. // Add 1 for the callee encoding.
  148. return CI.ParameterEncoding[ArgNo + 1];
  149. }
  150. /// Return the operand of the underlying instruction associated with @p Arg.
  151. Value *getCallArgOperand(Argument &Arg) const {
  152. return getCallArgOperand(Arg.getArgNo());
  153. }
  154. /// Return the operand of the underlying instruction associated with the
  155. /// function parameter number @p ArgNo or nullptr if there is none.
  156. Value *getCallArgOperand(unsigned ArgNo) const {
  157. if (isDirectCall())
  158. return CB->getArgOperand(ArgNo);
  159. // Add 1 for the callee encoding.
  160. return CI.ParameterEncoding[ArgNo + 1] >= 0
  161. ? CB->getArgOperand(CI.ParameterEncoding[ArgNo + 1])
  162. : nullptr;
  163. }
  164. /// Return the operand index of the underlying instruction associated with the
  165. /// callee of this ACS. Only valid for callback calls!
  166. int getCallArgOperandNoForCallee() const {
  167. assert(isCallbackCall());
  168. assert(CI.ParameterEncoding.size() && CI.ParameterEncoding[0] >= 0);
  169. return CI.ParameterEncoding[0];
  170. }
  171. /// Return the use of the callee value in the underlying instruction. Only
  172. /// valid for callback calls!
  173. const Use &getCalleeUseForCallback() const {
  174. int CalleeArgIdx = getCallArgOperandNoForCallee();
  175. assert(CalleeArgIdx >= 0 &&
  176. unsigned(CalleeArgIdx) < getInstruction()->getNumOperands());
  177. return getInstruction()->getOperandUse(CalleeArgIdx);
  178. }
  179. /// Return the pointer to function that is being called.
  180. Value *getCalledOperand() const {
  181. if (isDirectCall())
  182. return CB->getCalledOperand();
  183. return CB->getArgOperand(getCallArgOperandNoForCallee());
  184. }
  185. /// Return the function being called if this is a direct call, otherwise
  186. /// return null (if it's an indirect call).
  187. Function *getCalledFunction() const {
  188. Value *V = getCalledOperand();
  189. return V ? dyn_cast<Function>(V->stripPointerCasts()) : nullptr;
  190. }
  191. };
  192. /// Apply function Func to each CB's callback call site.
  193. template <typename UnaryFunction>
  194. void forEachCallbackCallSite(const CallBase &CB, UnaryFunction Func) {
  195. SmallVector<const Use *, 4u> CallbackUses;
  196. AbstractCallSite::getCallbackUses(CB, CallbackUses);
  197. for (const Use *U : CallbackUses) {
  198. AbstractCallSite ACS(U);
  199. assert(ACS && ACS.isCallbackCall() && "must be a callback call");
  200. Func(ACS);
  201. }
  202. }
  203. /// Apply function Func to each CB's callback function.
  204. template <typename UnaryFunction>
  205. void forEachCallbackFunction(const CallBase &CB, UnaryFunction Func) {
  206. forEachCallbackCallSite(CB, [&Func](AbstractCallSite &ACS) {
  207. if (Function *Callback = ACS.getCalledFunction())
  208. Func(Callback);
  209. });
  210. }
  211. } // end namespace llvm
  212. #endif // LLVM_IR_ABSTRACTCALLSITE_H