1 //===- IntervalPartition.cpp - Interval Partition module code ----*- C++ -*--=//
3 // This file contains the definition of the IntervalPartition class, which
4 // calculates and represent the interval partition of a function.
6 //===----------------------------------------------------------------------===//
8 #include "llvm/Analysis/IntervalIterator.h"
9 #include "Support/STLExtras.h"
13 static RegisterAnalysis<IntervalPartition>
14 X("intervals", "Interval Partition Construction", true);
16 //===----------------------------------------------------------------------===//
17 // IntervalPartition Implementation
18 //===----------------------------------------------------------------------===//
20 // destroy - Reset state back to before function was analyzed
21 void IntervalPartition::destroy() {
22 for_each(Intervals.begin(), Intervals.end(), deleter<Interval>);
27 void IntervalPartition::print(std::ostream &O) const {
28 std::copy(Intervals.begin(), Intervals.end(),
29 std::ostream_iterator<const Interval *>(O, "\n"));
32 // addIntervalToPartition - Add an interval to the internal list of intervals,
33 // and then add mappings from all of the basic blocks in the interval to the
34 // interval itself (in the IntervalMap).
36 void IntervalPartition::addIntervalToPartition(Interval *I) {
37 Intervals.push_back(I);
39 // Add mappings for all of the basic blocks in I to the IntervalPartition
40 for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end();
42 IntervalMap.insert(make_pair(*It, I));
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
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);
57 // IntervalPartition ctor - Build the first level interval partition for the
58 // specified function...
60 bool IntervalPartition::runOnFunction(Function &F) {
61 // Pass false to intervals_begin because we take ownership of it's memory
62 function_interval_iterator I = intervals_begin(&F, false);
63 assert(I != intervals_end(&F) && "No intervals in function!?!?!");
65 addIntervalToPartition(RootInterval = *I);
67 ++I; // After the first one...
69 // Add the rest of the intervals to the partition...
70 for_each(I, intervals_end(&F),
71 bind_obj(this, &IntervalPartition::addIntervalToPartition));
73 // Now that we know all of the successor information, propagate this to the
74 // predecessors for each block...
75 for_each(Intervals.begin(), Intervals.end(),
76 bind_obj(this, &IntervalPartition::updatePredecessors));
81 // IntervalPartition ctor - Build a reduced interval partition from an
82 // existing interval graph. This takes an additional boolean parameter to
83 // distinguish it from a copy constructor. Always pass in false for now.
85 IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) {
86 Interval *FunctionStart = IP.getRootInterval();
87 assert(FunctionStart && "Cannot operate on empty IntervalPartitions!");
89 // Pass false to intervals_begin because we take ownership of it's memory
90 interval_part_interval_iterator I = intervals_begin(IP, false);
91 assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
93 addIntervalToPartition(RootInterval = *I);
95 ++I; // After the first one...
97 // Add the rest of the intervals to the partition...
98 for_each(I, intervals_end(IP),
99 bind_obj(this, &IntervalPartition::addIntervalToPartition));
101 // Now that we know all of the successor information, propagate this to the
102 // predecessors for each block...
103 for_each(Intervals.begin(), Intervals.end(),
104 bind_obj(this, &IntervalPartition::updatePredecessors));