CodeExtractor.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Transform/Utils/CodeExtractor.h - Code extraction util ---*- 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. // A utility to support extracting code from one function into its own
  15. // stand-alone function.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H
  19. #define LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H
  20. #include "llvm/ADT/ArrayRef.h"
  21. #include "llvm/ADT/DenseMap.h"
  22. #include "llvm/ADT/SetVector.h"
  23. #include <limits>
  24. namespace llvm {
  25. template <typename PtrType> class SmallPtrSetImpl;
  26. class AllocaInst;
  27. class BasicBlock;
  28. class BlockFrequency;
  29. class BlockFrequencyInfo;
  30. class BranchProbabilityInfo;
  31. class AssumptionCache;
  32. class CallInst;
  33. class DominatorTree;
  34. class Function;
  35. class Instruction;
  36. class Loop;
  37. class Module;
  38. class Type;
  39. class Value;
  40. /// A cache for the CodeExtractor analysis. The operation \ref
  41. /// CodeExtractor::extractCodeRegion is guaranteed not to invalidate this
  42. /// object. This object should conservatively be considered invalid if any
  43. /// other mutating operations on the IR occur.
  44. ///
  45. /// Constructing this object is O(n) in the size of the function.
  46. class CodeExtractorAnalysisCache {
  47. /// The allocas in the function.
  48. SmallVector<AllocaInst *, 16> Allocas;
  49. /// Base memory addresses of load/store instructions, grouped by block.
  50. DenseMap<BasicBlock *, DenseSet<Value *>> BaseMemAddrs;
  51. /// Blocks which contain instructions which may have unknown side-effects
  52. /// on memory.
  53. DenseSet<BasicBlock *> SideEffectingBlocks;
  54. void findSideEffectInfoForBlock(BasicBlock &BB);
  55. public:
  56. CodeExtractorAnalysisCache(Function &F);
  57. /// Get the allocas in the function at the time the analysis was created.
  58. /// Note that some of these allocas may no longer be present in the function,
  59. /// due to \ref CodeExtractor::extractCodeRegion.
  60. ArrayRef<AllocaInst *> getAllocas() const { return Allocas; }
  61. /// Check whether \p BB contains an instruction thought to load from, store
  62. /// to, or otherwise clobber the alloca \p Addr.
  63. bool doesBlockContainClobberOfAddr(BasicBlock &BB, AllocaInst *Addr) const;
  64. };
  65. /// Utility class for extracting code into a new function.
  66. ///
  67. /// This utility provides a simple interface for extracting some sequence of
  68. /// code into its own function, replacing it with a call to that function. It
  69. /// also provides various methods to query about the nature and result of
  70. /// such a transformation.
  71. ///
  72. /// The rough algorithm used is:
  73. /// 1) Find both the inputs and outputs for the extracted region.
  74. /// 2) Pass the inputs as arguments, remapping them within the extracted
  75. /// function to arguments.
  76. /// 3) Add allocas for any scalar outputs, adding all of the outputs' allocas
  77. /// as arguments, and inserting stores to the arguments for any scalars.
  78. class CodeExtractor {
  79. using ValueSet = SetVector<Value *>;
  80. // Various bits of state computed on construction.
  81. DominatorTree *const DT;
  82. const bool AggregateArgs;
  83. BlockFrequencyInfo *BFI;
  84. BranchProbabilityInfo *BPI;
  85. AssumptionCache *AC;
  86. // A block outside of the extraction set where any intermediate
  87. // allocations will be placed inside. If this is null, allocations
  88. // will be placed in the entry block of the function.
  89. BasicBlock *AllocationBlock;
  90. // If true, varargs functions can be extracted.
  91. bool AllowVarArgs;
  92. // Bits of intermediate state computed at various phases of extraction.
  93. SetVector<BasicBlock *> Blocks;
  94. unsigned NumExitBlocks = std::numeric_limits<unsigned>::max();
  95. Type *RetTy;
  96. // Mapping from the original exit blocks, to the new blocks inside
  97. // the function.
  98. SmallVector<BasicBlock *, 4> OldTargets;
  99. // Suffix to use when creating extracted function (appended to the original
  100. // function name + "."). If empty, the default is to use the entry block
  101. // label, if non-empty, otherwise "extracted".
  102. std::string Suffix;
  103. public:
  104. /// Create a code extractor for a sequence of blocks.
  105. ///
  106. /// Given a sequence of basic blocks where the first block in the sequence
  107. /// dominates the rest, prepare a code extractor object for pulling this
  108. /// sequence out into its new function. When a DominatorTree is also given,
  109. /// extra checking and transformations are enabled. If AllowVarArgs is true,
  110. /// vararg functions can be extracted. This is safe, if all vararg handling
  111. /// code is extracted, including vastart. If AllowAlloca is true, then
  112. /// extraction of blocks containing alloca instructions would be possible,
  113. /// however code extractor won't validate whether extraction is legal.
  114. /// Any new allocations will be placed in the AllocationBlock, unless
  115. /// it is null, in which case it will be placed in the entry block of
  116. /// the function from which the code is being extracted.
  117. CodeExtractor(ArrayRef<BasicBlock *> BBs, DominatorTree *DT = nullptr,
  118. bool AggregateArgs = false, BlockFrequencyInfo *BFI = nullptr,
  119. BranchProbabilityInfo *BPI = nullptr,
  120. AssumptionCache *AC = nullptr, bool AllowVarArgs = false,
  121. bool AllowAlloca = false,
  122. BasicBlock *AllocationBlock = nullptr,
  123. std::string Suffix = "");
  124. /// Create a code extractor for a loop body.
  125. ///
  126. /// Behaves just like the generic code sequence constructor, but uses the
  127. /// block sequence of the loop.
  128. CodeExtractor(DominatorTree &DT, Loop &L, bool AggregateArgs = false,
  129. BlockFrequencyInfo *BFI = nullptr,
  130. BranchProbabilityInfo *BPI = nullptr,
  131. AssumptionCache *AC = nullptr,
  132. std::string Suffix = "");
  133. /// Perform the extraction, returning the new function.
  134. ///
  135. /// Returns zero when called on a CodeExtractor instance where isEligible
  136. /// returns false.
  137. Function *extractCodeRegion(const CodeExtractorAnalysisCache &CEAC);
  138. /// Perform the extraction, returning the new function and providing an
  139. /// interface to see what was categorized as inputs and outputs.
  140. ///
  141. /// \param CEAC - Cache to speed up operations for the CodeExtractor when
  142. /// hoisting, and extracting lifetime values and assumes.
  143. /// \param Inputs [out] - filled with values marked as inputs to the
  144. /// newly outlined function.
  145. /// \param Outputs [out] - filled with values marked as outputs to the
  146. /// newly outlined function.
  147. /// \returns zero when called on a CodeExtractor instance where isEligible
  148. /// returns false.
  149. Function *extractCodeRegion(const CodeExtractorAnalysisCache &CEAC,
  150. ValueSet &Inputs, ValueSet &Outputs);
  151. /// Verify that assumption cache isn't stale after a region is extracted.
  152. /// Returns true when verifier finds errors. AssumptionCache is passed as
  153. /// parameter to make this function stateless.
  154. static bool verifyAssumptionCache(const Function &OldFunc,
  155. const Function &NewFunc,
  156. AssumptionCache *AC);
  157. /// Test whether this code extractor is eligible.
  158. ///
  159. /// Based on the blocks used when constructing the code extractor,
  160. /// determine whether it is eligible for extraction.
  161. ///
  162. /// Checks that varargs handling (with vastart and vaend) is only done in
  163. /// the outlined blocks.
  164. bool isEligible() const;
  165. /// Compute the set of input values and output values for the code.
  166. ///
  167. /// These can be used either when performing the extraction or to evaluate
  168. /// the expected size of a call to the extracted function. Note that this
  169. /// work cannot be cached between the two as once we decide to extract
  170. /// a code sequence, that sequence is modified, including changing these
  171. /// sets, before extraction occurs. These modifications won't have any
  172. /// significant impact on the cost however.
  173. void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs,
  174. const ValueSet &Allocas) const;
  175. /// Check if life time marker nodes can be hoisted/sunk into the outline
  176. /// region.
  177. ///
  178. /// Returns true if it is safe to do the code motion.
  179. bool
  180. isLegalToShrinkwrapLifetimeMarkers(const CodeExtractorAnalysisCache &CEAC,
  181. Instruction *AllocaAddr) const;
  182. /// Find the set of allocas whose life ranges are contained within the
  183. /// outlined region.
  184. ///
  185. /// Allocas which have life_time markers contained in the outlined region
  186. /// should be pushed to the outlined function. The address bitcasts that
  187. /// are used by the lifetime markers are also candidates for shrink-
  188. /// wrapping. The instructions that need to be sunk are collected in
  189. /// 'Allocas'.
  190. void findAllocas(const CodeExtractorAnalysisCache &CEAC,
  191. ValueSet &SinkCands, ValueSet &HoistCands,
  192. BasicBlock *&ExitBlock) const;
  193. /// Find or create a block within the outline region for placing hoisted
  194. /// code.
  195. ///
  196. /// CommonExitBlock is block outside the outline region. It is the common
  197. /// successor of blocks inside the region. If there exists a single block
  198. /// inside the region that is the predecessor of CommonExitBlock, that block
  199. /// will be returned. Otherwise CommonExitBlock will be split and the
  200. /// original block will be added to the outline region.
  201. BasicBlock *findOrCreateBlockForHoisting(BasicBlock *CommonExitBlock);
  202. /// Exclude a value from aggregate argument passing when extracting a code
  203. /// region, passing it instead as a scalar.
  204. void excludeArgFromAggregate(Value *Arg);
  205. private:
  206. struct LifetimeMarkerInfo {
  207. bool SinkLifeStart = false;
  208. bool HoistLifeEnd = false;
  209. Instruction *LifeStart = nullptr;
  210. Instruction *LifeEnd = nullptr;
  211. };
  212. ValueSet ExcludeArgsFromAggregate;
  213. LifetimeMarkerInfo
  214. getLifetimeMarkers(const CodeExtractorAnalysisCache &CEAC,
  215. Instruction *Addr, BasicBlock *ExitBlock) const;
  216. void severSplitPHINodesOfEntry(BasicBlock *&Header);
  217. void severSplitPHINodesOfExits(const SmallPtrSetImpl<BasicBlock *> &Exits);
  218. void splitReturnBlocks();
  219. Function *constructFunction(const ValueSet &inputs,
  220. const ValueSet &outputs,
  221. BasicBlock *header,
  222. BasicBlock *newRootNode, BasicBlock *newHeader,
  223. Function *oldFunction, Module *M);
  224. void moveCodeToFunction(Function *newFunction);
  225. void calculateNewCallTerminatorWeights(
  226. BasicBlock *CodeReplacer,
  227. DenseMap<BasicBlock *, BlockFrequency> &ExitWeights,
  228. BranchProbabilityInfo *BPI);
  229. CallInst *emitCallAndSwitchStatement(Function *newFunction,
  230. BasicBlock *newHeader,
  231. ValueSet &inputs, ValueSet &outputs);
  232. };
  233. } // end namespace llvm
  234. #endif // LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H
  235. #ifdef __GNUC__
  236. #pragma GCC diagnostic pop
  237. #endif