IntervalPartition.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //===- IntervalPartition.h - Interval partition Calculation -----*- 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. // This file contains the declaration of the IntervalPartition class, which
  11. // calculates and represents the interval partition of a function, or a
  12. // preexisting interval partition.
  13. //
  14. // In this way, the interval partition may be used to reduce a flow graph down
  15. // to its degenerate single node interval partition (unless it is irreducible).
  16. //
  17. // TODO: The IntervalPartition class should take a bool parameter that tells
  18. // whether it should add the "tails" of an interval to an interval itself or if
  19. // they should be represented as distinct intervals.
  20. //
  21. //===----------------------------------------------------------------------===//
  22. #ifndef LLVM_ANALYSIS_INTERVALPARTITION_H
  23. #define LLVM_ANALYSIS_INTERVALPARTITION_H
  24. #include "llvm/Analysis/Interval.h"
  25. #include "llvm/Pass.h"
  26. #include <map>
  27. namespace llvm {
  28. // //
  29. ///////////////////////////////////////////////////////////////////////////////
  30. //
  31. // IntervalPartition - This class builds and holds an "interval partition" for
  32. // a function. This partition divides the control flow graph into a set of
  33. // maximal intervals, as defined with the properties above. Intuitively, an
  34. // interval is a (possibly nonexistent) loop with a "tail" of non-looping
  35. // nodes following it.
  36. //
  37. class IntervalPartition : public FunctionPass {
  38. typedef std::map<BasicBlock*, Interval*> IntervalMapTy;
  39. IntervalMapTy IntervalMap;
  40. typedef std::vector<Interval*> IntervalListTy;
  41. Interval *RootInterval;
  42. std::vector<Interval*> Intervals;
  43. public:
  44. static char ID; // Pass identification, replacement for typeid
  45. IntervalPartition() : FunctionPass(ID), RootInterval(nullptr) {
  46. initializeIntervalPartitionPass(*PassRegistry::getPassRegistry());
  47. }
  48. // run - Calculate the interval partition for this function
  49. bool runOnFunction(Function &F) override;
  50. // IntervalPartition ctor - Build a reduced interval partition from an
  51. // existing interval graph. This takes an additional boolean parameter to
  52. // distinguish it from a copy constructor. Always pass in false for now.
  53. //
  54. IntervalPartition(IntervalPartition &I, bool);
  55. // print - Show contents in human readable format...
  56. void print(raw_ostream &O, const Module* = nullptr) const override;
  57. // getRootInterval() - Return the root interval that contains the starting
  58. // block of the function.
  59. inline Interval *getRootInterval() { return RootInterval; }
  60. // isDegeneratePartition() - Returns true if the interval partition contains
  61. // a single interval, and thus cannot be simplified anymore.
  62. bool isDegeneratePartition() { return Intervals.size() == 1; }
  63. // TODO: isIrreducible - look for triangle graph.
  64. // getBlockInterval - Return the interval that a basic block exists in.
  65. inline Interval *getBlockInterval(BasicBlock *BB) {
  66. IntervalMapTy::iterator I = IntervalMap.find(BB);
  67. return I != IntervalMap.end() ? I->second : nullptr;
  68. }
  69. // getAnalysisUsage - Implement the Pass API
  70. void getAnalysisUsage(AnalysisUsage &AU) const override {
  71. AU.setPreservesAll();
  72. }
  73. // Interface to Intervals vector...
  74. const std::vector<Interval*> &getIntervals() const { return Intervals; }
  75. // releaseMemory - Reset state back to before function was analyzed
  76. void releaseMemory() override;
  77. private:
  78. // addIntervalToPartition - Add an interval to the internal list of intervals,
  79. // and then add mappings from all of the basic blocks in the interval to the
  80. // interval itself (in the IntervalMap).
  81. //
  82. void addIntervalToPartition(Interval *I);
  83. // updatePredecessors - Interval generation only sets the successor fields of
  84. // the interval data structures. After interval generation is complete,
  85. // run through all of the intervals and propagate successor info as
  86. // predecessor info.
  87. //
  88. void updatePredecessors(Interval *Int);
  89. };
  90. } // End llvm namespace
  91. #endif