CodeExtractor.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //===-- Transform/Utils/CodeExtractor.h - Code extraction util --*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // A utility to support extracting code from one function into its own
  11. // stand-alone function.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H
  15. #define LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H
  16. #include "llvm/ADT/SetVector.h"
  17. namespace llvm {
  18. template <typename T> class ArrayRef;
  19. class BasicBlock;
  20. class DominatorTree;
  21. class Function;
  22. class Loop;
  23. class Module;
  24. class RegionNode;
  25. class Type;
  26. class Value;
  27. /// \brief Utility class for extracting code into a new function.
  28. ///
  29. /// This utility provides a simple interface for extracting some sequence of
  30. /// code into its own function, replacing it with a call to that function. It
  31. /// also provides various methods to query about the nature and result of
  32. /// such a transformation.
  33. ///
  34. /// The rough algorithm used is:
  35. /// 1) Find both the inputs and outputs for the extracted region.
  36. /// 2) Pass the inputs as arguments, remapping them within the extracted
  37. /// function to arguments.
  38. /// 3) Add allocas for any scalar outputs, adding all of the outputs' allocas
  39. /// as arguments, and inserting stores to the arguments for any scalars.
  40. class CodeExtractor {
  41. typedef SetVector<Value *> ValueSet;
  42. // Various bits of state computed on construction.
  43. DominatorTree *const DT;
  44. const bool AggregateArgs;
  45. // Bits of intermediate state computed at various phases of extraction.
  46. SetVector<BasicBlock *> Blocks;
  47. unsigned NumExitBlocks;
  48. Type *RetTy;
  49. public:
  50. /// \brief Check to see if a block is valid for extraction.
  51. ///
  52. /// Blocks containing EHPads, allocas, invokes, or vastarts are not valid.
  53. static bool isBlockValidForExtraction(const BasicBlock &BB);
  54. /// \brief Create a code extractor for a single basic block.
  55. ///
  56. /// In this formation, we don't require a dominator tree. The given basic
  57. /// block is set up for extraction.
  58. CodeExtractor(BasicBlock *BB, bool AggregateArgs = false);
  59. /// \brief Create a code extractor for a sequence of blocks.
  60. ///
  61. /// Given a sequence of basic blocks where the first block in the sequence
  62. /// dominates the rest, prepare a code extractor object for pulling this
  63. /// sequence out into its new function. When a DominatorTree is also given,
  64. /// extra checking and transformations are enabled.
  65. CodeExtractor(ArrayRef<BasicBlock *> BBs, DominatorTree *DT = nullptr,
  66. bool AggregateArgs = false);
  67. /// \brief Create a code extractor for a loop body.
  68. ///
  69. /// Behaves just like the generic code sequence constructor, but uses the
  70. /// block sequence of the loop.
  71. CodeExtractor(DominatorTree &DT, Loop &L, bool AggregateArgs = false);
  72. /// \brief Create a code extractor for a region node.
  73. ///
  74. /// Behaves just like the generic code sequence constructor, but uses the
  75. /// block sequence of the region node passed in.
  76. CodeExtractor(DominatorTree &DT, const RegionNode &RN,
  77. bool AggregateArgs = false);
  78. /// \brief Perform the extraction, returning the new function.
  79. ///
  80. /// Returns zero when called on a CodeExtractor instance where isEligible
  81. /// returns false.
  82. Function *extractCodeRegion();
  83. /// \brief Test whether this code extractor is eligible.
  84. ///
  85. /// Based on the blocks used when constructing the code extractor,
  86. /// determine whether it is eligible for extraction.
  87. bool isEligible() const { return !Blocks.empty(); }
  88. /// \brief Compute the set of input values and output values for the code.
  89. ///
  90. /// These can be used either when performing the extraction or to evaluate
  91. /// the expected size of a call to the extracted function. Note that this
  92. /// work cannot be cached between the two as once we decide to extract
  93. /// a code sequence, that sequence is modified, including changing these
  94. /// sets, before extraction occurs. These modifications won't have any
  95. /// significant impact on the cost however.
  96. void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs) const;
  97. private:
  98. void severSplitPHINodes(BasicBlock *&Header);
  99. void splitReturnBlocks();
  100. Function *constructFunction(const ValueSet &inputs,
  101. const ValueSet &outputs,
  102. BasicBlock *header,
  103. BasicBlock *newRootNode, BasicBlock *newHeader,
  104. Function *oldFunction, Module *M);
  105. void moveCodeToFunction(Function *newFunction);
  106. void emitCallAndSwitchStatement(Function *newFunction,
  107. BasicBlock *newHeader,
  108. ValueSet &inputs,
  109. ValueSet &outputs);
  110. };
  111. }
  112. #endif