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 AnalysisID IntervalPartition::ID(AnalysisID::create<IntervalPartition>(), true);
15 //===----------------------------------------------------------------------===//
16 // IntervalPartition Implementation
17 //===----------------------------------------------------------------------===//
19 // destroy - Reset state back to before function was analyzed
20 void IntervalPartition::destroy() {
21 for_each(begin(), end(), deleter<Interval>);
26 // addIntervalToPartition - Add an interval to the internal list of intervals,
27 // and then add mappings from all of the basic blocks in the interval to the
28 // interval itself (in the IntervalMap).
30 void IntervalPartition::addIntervalToPartition(Interval *I) {
33 // Add mappings for all of the basic blocks in I to the IntervalPartition
34 for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end();
36 IntervalMap.insert(make_pair(*It, I));
39 // updatePredecessors - Interval generation only sets the successor fields of
40 // the interval data structures. After interval generation is complete,
41 // run through all of the intervals and propogate successor info as
44 void IntervalPartition::updatePredecessors(Interval *Int) {
45 BasicBlock *Header = Int->getHeaderNode();
46 for (Interval::succ_iterator I = Int->Successors.begin(),
47 E = Int->Successors.end(); I != E; ++I)
48 getBlockInterval(*I)->Predecessors.push_back(Header);
51 // IntervalPartition ctor - Build the first level interval partition for the
52 // specified function...
54 bool IntervalPartition::runOnFunction(Function *F) {
55 assert(F->front() && "Cannot operate on prototypes!");
57 // Pass false to intervals_begin because we take ownership of it's memory
58 function_interval_iterator I = intervals_begin(F, false);
59 assert(I != intervals_end(F) && "No intervals in function!?!?!");
61 addIntervalToPartition(RootInterval = *I);
63 ++I; // After the first one...
65 // Add the rest of the intervals to the partition...
66 for_each(I, intervals_end(F),
67 bind_obj(this, &IntervalPartition::addIntervalToPartition));
69 // Now that we know all of the successor information, propogate this to the
70 // predecessors for each block...
71 for_each(begin(), end(),
72 bind_obj(this, &IntervalPartition::updatePredecessors));
77 // IntervalPartition ctor - Build a reduced interval partition from an
78 // existing interval graph. This takes an additional boolean parameter to
79 // distinguish it from a copy constructor. Always pass in false for now.
81 IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) {
82 Interval *FunctionStart = IP.getRootInterval();
83 assert(FunctionStart && "Cannot operate on empty IntervalPartitions!");
85 // Pass false to intervals_begin because we take ownership of it's memory
86 interval_part_interval_iterator I = intervals_begin(IP, false);
87 assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
89 addIntervalToPartition(RootInterval = *I);
91 ++I; // After the first one...
93 // Add the rest of the intervals to the partition...
94 for_each(I, intervals_end(IP),
95 bind_obj(this, &IntervalPartition::addIntervalToPartition));
97 // Now that we know all of the successor information, propogate this to the
98 // predecessors for each block...
99 for_each(begin(), end(),
100 bind_obj(this, &IntervalPartition::updatePredecessors));