/3rd_party/llvm/lib/Analysis/IntervalPartition.cpp

https://code.google.com/p/softart/ · C++ · 114 lines · 51 code · 21 blank · 42 comment · 21 complexity · 2122b359a520405a23da190e366031fc MD5 · raw file

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