IntervalPartition.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //===- IntervalPartition.cpp - Interval Partition module code -------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains the definition of the IntervalPartition class, which
  10. // calculates and represent the interval partition of a function.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Analysis/IntervalPartition.h"
  14. #include "llvm/Analysis/Interval.h"
  15. #include "llvm/Analysis/IntervalIterator.h"
  16. #include "llvm/InitializePasses.h"
  17. #include "llvm/Pass.h"
  18. #include <cassert>
  19. #include <utility>
  20. using namespace llvm;
  21. char IntervalPartition::ID = 0;
  22. IntervalPartition::IntervalPartition() : FunctionPass(ID) {
  23. initializeIntervalPartitionPass(*PassRegistry::getPassRegistry());
  24. }
  25. INITIALIZE_PASS(IntervalPartition, "intervals",
  26. "Interval Partition Construction", true, true)
  27. //===----------------------------------------------------------------------===//
  28. // IntervalPartition Implementation
  29. //===----------------------------------------------------------------------===//
  30. // releaseMemory - Reset state back to before function was analyzed
  31. void IntervalPartition::releaseMemory() {
  32. for (Interval *I : Intervals)
  33. delete I;
  34. IntervalMap.clear();
  35. Intervals.clear();
  36. RootInterval = nullptr;
  37. }
  38. void IntervalPartition::print(raw_ostream &O, const Module*) const {
  39. for (const Interval *I : Intervals)
  40. I->print(O);
  41. }
  42. // addIntervalToPartition - Add an interval to the internal list of intervals,
  43. // and then add mappings from all of the basic blocks in the interval to the
  44. // interval itself (in the IntervalMap).
  45. void IntervalPartition::addIntervalToPartition(Interval *I) {
  46. Intervals.push_back(I);
  47. // Add mappings for all of the basic blocks in I to the IntervalPartition
  48. for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end();
  49. It != End; ++It)
  50. IntervalMap.insert(std::make_pair(*It, I));
  51. }
  52. // updatePredecessors - Interval generation only sets the successor fields of
  53. // the interval data structures. After interval generation is complete,
  54. // run through all of the intervals and propagate successor info as
  55. // predecessor info.
  56. void IntervalPartition::updatePredecessors(Interval *Int) {
  57. BasicBlock *Header = Int->getHeaderNode();
  58. for (BasicBlock *Successor : Int->Successors)
  59. getBlockInterval(Successor)->Predecessors.push_back(Header);
  60. }
  61. // IntervalPartition ctor - Build the first level interval partition for the
  62. // specified function...
  63. bool IntervalPartition::runOnFunction(Function &F) {
  64. // Pass false to intervals_begin because we take ownership of it's memory
  65. function_interval_iterator I = intervals_begin(&F, false);
  66. assert(I != intervals_end(&F) && "No intervals in function!?!?!");
  67. addIntervalToPartition(RootInterval = *I);
  68. ++I; // After the first one...
  69. // Add the rest of the intervals to the partition.
  70. for (function_interval_iterator E = intervals_end(&F); I != E; ++I)
  71. addIntervalToPartition(*I);
  72. // Now that we know all of the successor information, propagate this to the
  73. // predecessors for each block.
  74. for (Interval *I : Intervals)
  75. updatePredecessors(I);
  76. return false;
  77. }
  78. // IntervalPartition ctor - Build a reduced interval partition from an
  79. // existing interval graph. This takes an additional boolean parameter to
  80. // distinguish it from a copy constructor. Always pass in false for now.
  81. IntervalPartition::IntervalPartition(IntervalPartition &IP, bool)
  82. : FunctionPass(ID) {
  83. assert(IP.getRootInterval() && "Cannot operate on empty IntervalPartitions!");
  84. // Pass false to intervals_begin because we take ownership of it's memory
  85. interval_part_interval_iterator I = intervals_begin(IP, false);
  86. assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
  87. addIntervalToPartition(RootInterval = *I);
  88. ++I; // After the first one...
  89. // Add the rest of the intervals to the partition.
  90. for (interval_part_interval_iterator E = intervals_end(IP); I != E; ++I)
  91. addIntervalToPartition(*I);
  92. // Now that we know all of the successor information, propagate this to the
  93. // predecessors for each block.
  94. for (Interval *I : Intervals)
  95. updatePredecessors(I);
  96. }