CodeExtractor.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 "llvm/ADT/SmallPtrSet.h"
  24. #include <limits>
  25. namespace llvm {
  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. // If true, varargs functions can be extracted.
  87. bool AllowVarArgs;
  88. // Bits of intermediate state computed at various phases of extraction.
  89. SetVector<BasicBlock *> Blocks;
  90. unsigned NumExitBlocks = std::numeric_limits<unsigned>::max();
  91. Type *RetTy;
  92. // Suffix to use when creating extracted function (appended to the original
  93. // function name + "."). If empty, the default is to use the entry block
  94. // label, if non-empty, otherwise "extracted".
  95. std::string Suffix;
  96. public:
  97. /// Create a code extractor for a sequence of blocks.
  98. ///
  99. /// Given a sequence of basic blocks where the first block in the sequence
  100. /// dominates the rest, prepare a code extractor object for pulling this
  101. /// sequence out into its new function. When a DominatorTree is also given,
  102. /// extra checking and transformations are enabled. If AllowVarArgs is true,
  103. /// vararg functions can be extracted. This is safe, if all vararg handling
  104. /// code is extracted, including vastart. If AllowAlloca is true, then
  105. /// extraction of blocks containing alloca instructions would be possible,
  106. /// however code extractor won't validate whether extraction is legal.
  107. CodeExtractor(ArrayRef<BasicBlock *> BBs, DominatorTree *DT = nullptr,
  108. bool AggregateArgs = false, BlockFrequencyInfo *BFI = nullptr,
  109. BranchProbabilityInfo *BPI = nullptr,
  110. AssumptionCache *AC = nullptr,
  111. bool AllowVarArgs = false, bool AllowAlloca = false,
  112. std::string Suffix = "");
  113. /// Create a code extractor for a loop body.
  114. ///
  115. /// Behaves just like the generic code sequence constructor, but uses the
  116. /// block sequence of the loop.
  117. CodeExtractor(DominatorTree &DT, Loop &L, bool AggregateArgs = false,
  118. BlockFrequencyInfo *BFI = nullptr,
  119. BranchProbabilityInfo *BPI = nullptr,
  120. AssumptionCache *AC = nullptr,
  121. std::string Suffix = "");
  122. /// Perform the extraction, returning the new function.
  123. ///
  124. /// Returns zero when called on a CodeExtractor instance where isEligible
  125. /// returns false.
  126. Function *extractCodeRegion(const CodeExtractorAnalysisCache &CEAC);
  127. /// Verify that assumption cache isn't stale after a region is extracted.
  128. /// Returns true when verifier finds errors. AssumptionCache is passed as
  129. /// parameter to make this function stateless.
  130. static bool verifyAssumptionCache(const Function &OldFunc,
  131. const Function &NewFunc,
  132. AssumptionCache *AC);
  133. /// Test whether this code extractor is eligible.
  134. ///
  135. /// Based on the blocks used when constructing the code extractor,
  136. /// determine whether it is eligible for extraction.
  137. ///
  138. /// Checks that varargs handling (with vastart and vaend) is only done in
  139. /// the outlined blocks.
  140. bool isEligible() const;
  141. /// Compute the set of input values and output values for the code.
  142. ///
  143. /// These can be used either when performing the extraction or to evaluate
  144. /// the expected size of a call to the extracted function. Note that this
  145. /// work cannot be cached between the two as once we decide to extract
  146. /// a code sequence, that sequence is modified, including changing these
  147. /// sets, before extraction occurs. These modifications won't have any
  148. /// significant impact on the cost however.
  149. void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs,
  150. const ValueSet &Allocas) const;
  151. /// Check if life time marker nodes can be hoisted/sunk into the outline
  152. /// region.
  153. ///
  154. /// Returns true if it is safe to do the code motion.
  155. bool
  156. isLegalToShrinkwrapLifetimeMarkers(const CodeExtractorAnalysisCache &CEAC,
  157. Instruction *AllocaAddr) const;
  158. /// Find the set of allocas whose life ranges are contained within the
  159. /// outlined region.
  160. ///
  161. /// Allocas which have life_time markers contained in the outlined region
  162. /// should be pushed to the outlined function. The address bitcasts that
  163. /// are used by the lifetime markers are also candidates for shrink-
  164. /// wrapping. The instructions that need to be sunk are collected in
  165. /// 'Allocas'.
  166. void findAllocas(const CodeExtractorAnalysisCache &CEAC,
  167. ValueSet &SinkCands, ValueSet &HoistCands,
  168. BasicBlock *&ExitBlock) const;
  169. /// Find or create a block within the outline region for placing hoisted
  170. /// code.
  171. ///
  172. /// CommonExitBlock is block outside the outline region. It is the common
  173. /// successor of blocks inside the region. If there exists a single block
  174. /// inside the region that is the predecessor of CommonExitBlock, that block
  175. /// will be returned. Otherwise CommonExitBlock will be split and the
  176. /// original block will be added to the outline region.
  177. BasicBlock *findOrCreateBlockForHoisting(BasicBlock *CommonExitBlock);
  178. private:
  179. struct LifetimeMarkerInfo {
  180. bool SinkLifeStart = false;
  181. bool HoistLifeEnd = false;
  182. Instruction *LifeStart = nullptr;
  183. Instruction *LifeEnd = nullptr;
  184. };
  185. LifetimeMarkerInfo
  186. getLifetimeMarkers(const CodeExtractorAnalysisCache &CEAC,
  187. Instruction *Addr, BasicBlock *ExitBlock) const;
  188. void severSplitPHINodesOfEntry(BasicBlock *&Header);
  189. void severSplitPHINodesOfExits(const SmallPtrSetImpl<BasicBlock *> &Exits);
  190. void splitReturnBlocks();
  191. Function *constructFunction(const ValueSet &inputs,
  192. const ValueSet &outputs,
  193. BasicBlock *header,
  194. BasicBlock *newRootNode, BasicBlock *newHeader,
  195. Function *oldFunction, Module *M);
  196. void moveCodeToFunction(Function *newFunction);
  197. void calculateNewCallTerminatorWeights(
  198. BasicBlock *CodeReplacer,
  199. DenseMap<BasicBlock *, BlockFrequency> &ExitWeights,
  200. BranchProbabilityInfo *BPI);
  201. CallInst *emitCallAndSwitchStatement(Function *newFunction,
  202. BasicBlock *newHeader,
  203. ValueSet &inputs, ValueSet &outputs);
  204. };
  205. } // end namespace llvm
  206. #endif // LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H
  207. #ifdef __GNUC__
  208. #pragma GCC diagnostic pop
  209. #endif