CodeExtractor.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/ArrayRef.h"
  17. #include "llvm/ADT/SetVector.h"
  18. namespace llvm {
  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 Create a code extractor for a single basic block.
  51. ///
  52. /// In this formation, we don't require a dominator tree. The given basic
  53. /// block is set up for extraction.
  54. CodeExtractor(BasicBlock *BB, bool AggregateArgs = false);
  55. /// \brief Create a code extractor for a sequence of blocks.
  56. ///
  57. /// Given a sequence of basic blocks where the first block in the sequence
  58. /// dominates the rest, prepare a code extractor object for pulling this
  59. /// sequence out into its new function. When a DominatorTree is also given,
  60. /// extra checking and transformations are enabled.
  61. CodeExtractor(ArrayRef<BasicBlock *> BBs, DominatorTree *DT = nullptr,
  62. bool AggregateArgs = false);
  63. /// \brief Create a code extractor for a loop body.
  64. ///
  65. /// Behaves just like the generic code sequence constructor, but uses the
  66. /// block sequence of the loop.
  67. CodeExtractor(DominatorTree &DT, Loop &L, bool AggregateArgs = false);
  68. /// \brief Create a code extractor for a region node.
  69. ///
  70. /// Behaves just like the generic code sequence constructor, but uses the
  71. /// block sequence of the region node passed in.
  72. CodeExtractor(DominatorTree &DT, const RegionNode &RN,
  73. bool AggregateArgs = false);
  74. /// \brief Perform the extraction, returning the new function.
  75. ///
  76. /// Returns zero when called on a CodeExtractor instance where isEligible
  77. /// returns false.
  78. Function *extractCodeRegion();
  79. /// \brief Test whether this code extractor is eligible.
  80. ///
  81. /// Based on the blocks used when constructing the code extractor,
  82. /// determine whether it is eligible for extraction.
  83. bool isEligible() const { return !Blocks.empty(); }
  84. /// \brief Compute the set of input values and output values for the code.
  85. ///
  86. /// These can be used either when performing the extraction or to evaluate
  87. /// the expected size of a call to the extracted function. Note that this
  88. /// work cannot be cached between the two as once we decide to extract
  89. /// a code sequence, that sequence is modified, including changing these
  90. /// sets, before extraction occurs. These modifications won't have any
  91. /// significant impact on the cost however.
  92. void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs) const;
  93. private:
  94. void severSplitPHINodes(BasicBlock *&Header);
  95. void splitReturnBlocks();
  96. Function *constructFunction(const ValueSet &inputs,
  97. const ValueSet &outputs,
  98. BasicBlock *header,
  99. BasicBlock *newRootNode, BasicBlock *newHeader,
  100. Function *oldFunction, Module *M);
  101. void moveCodeToFunction(Function *newFunction);
  102. void emitCallAndSwitchStatement(Function *newFunction,
  103. BasicBlock *newHeader,
  104. ValueSet &inputs,
  105. ValueSet &outputs);
  106. };
  107. }
  108. #endif