AbstractCallSite.h 9.6 KB

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