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");
16 AnalysisID IntervalPartition::ID(AnalysisID::create<IntervalPartition>(), true);
18 //===----------------------------------------------------------------------===//
19 // IntervalPartition Implementation
20 //===----------------------------------------------------------------------===//
22 // destroy - Reset state back to before function was analyzed
23 void IntervalPartition::destroy() {
24 for_each(begin(), end(), deleter<Interval>);
29 // addIntervalToPartition - Add an interval to the internal list of intervals,
30 // and then add mappings from all of the basic blocks in the interval to the
31 // interval itself (in the IntervalMap).
33 void IntervalPartition::addIntervalToPartition(Interval *I) {
36 // Add mappings for all of the basic blocks in I to the IntervalPartition
37 for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end();
39 IntervalMap.insert(make_pair(*It, I));
42 // updatePredecessors - Interval generation only sets the successor fields of
43 // the interval data structures. After interval generation is complete,
44 // run through all of the intervals and propogate successor info as
47 void IntervalPartition::updatePredecessors(Interval *Int) {
48 BasicBlock *Header = Int->getHeaderNode();
49 for (Interval::succ_iterator I = Int->Successors.begin(),
50 E = Int->Successors.end(); I != E; ++I)
51 getBlockInterval(*I)->Predecessors.push_back(Header);
54 // IntervalPartition ctor - Build the first level interval partition for the
55 // specified function...
57 bool IntervalPartition::runOnFunction(Function &F) {
58 // Pass false to intervals_begin because we take ownership of it's memory
59 function_interval_iterator I = intervals_begin(&F, false);
60 assert(I != intervals_end(&F) && "No intervals in function!?!?!");
62 addIntervalToPartition(RootInterval = *I);
64 ++I; // After the first one...
66 // Add the rest of the intervals to the partition...
67 for_each(I, intervals_end(&F),
68 bind_obj(this, &IntervalPartition::addIntervalToPartition));
70 // Now that we know all of the successor information, propogate this to the
71 // predecessors for each block...
72 for_each(begin(), end(),
73 bind_obj(this, &IntervalPartition::updatePredecessors));
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.
82 IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) {
83 Interval *FunctionStart = IP.getRootInterval();
84 assert(FunctionStart && "Cannot operate on empty IntervalPartitions!");
86 // Pass false to intervals_begin because we take ownership of it's memory
87 interval_part_interval_iterator I = intervals_begin(IP, false);
88 assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
90 addIntervalToPartition(RootInterval = *I);
92 ++I; // After the first one...
94 // Add the rest of the intervals to the partition...
95 for_each(I, intervals_end(IP),
96 bind_obj(this, &IntervalPartition::addIntervalToPartition));
98 // Now that we know all of the successor information, propogate this to the
99 // predecessors for each block...
100 for_each(begin(), end(),
101 bind_obj(this, &IntervalPartition::updatePredecessors));