IntervalPartition.h 4.2 KB

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