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 // Pass false to intervals_begin because we take ownership of it's memory
56 function_interval_iterator I = intervals_begin(&F, false);
57 assert(I != intervals_end(&F) && "No intervals in function!?!?!");
59 addIntervalToPartition(RootInterval = *I);
61 ++I; // After the first one...
63 // Add the rest of the intervals to the partition...
64 for_each(I, intervals_end(&F),
65 bind_obj(this, &IntervalPartition::addIntervalToPartition));
67 // Now that we know all of the successor information, propogate this to the
68 // predecessors for each block...
69 for_each(begin(), end(),
70 bind_obj(this, &IntervalPartition::updatePredecessors));
75 // IntervalPartition ctor - Build a reduced interval partition from an
76 // existing interval graph. This takes an additional boolean parameter to
77 // distinguish it from a copy constructor. Always pass in false for now.
79 IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) {
80 Interval *FunctionStart = IP.getRootInterval();
81 assert(FunctionStart && "Cannot operate on empty IntervalPartitions!");
83 // Pass false to intervals_begin because we take ownership of it's memory
84 interval_part_interval_iterator I = intervals_begin(IP, false);
85 assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
87 addIntervalToPartition(RootInterval = *I);
89 ++I; // After the first one...
91 // Add the rest of the intervals to the partition...
92 for_each(I, intervals_end(IP),
93 bind_obj(this, &IntervalPartition::addIntervalToPartition));
95 // Now that we know all of the successor information, propogate this to the
96 // predecessors for each block...
97 for_each(begin(), end(),
98 bind_obj(this, &IntervalPartition::updatePredecessors));