Fix spelling of `propagate'.
[oota-llvm.git] / lib / Analysis / IntervalPartition.cpp
1 //===- IntervalPartition.cpp - Interval Partition module code ----*- C++ -*--=//
2 //
3 // This file contains the definition of the IntervalPartition class, which
4 // calculates and represent the interval partition of a function.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/Analysis/IntervalIterator.h"
9 #include "Support/STLExtras.h"
10
11 using std::make_pair;
12
13 static RegisterAnalysis<IntervalPartition>
14 X("intervals", "Interval Partition Construction", true);
15
16 //===----------------------------------------------------------------------===//
17 // IntervalPartition Implementation
18 //===----------------------------------------------------------------------===//
19
20 // destroy - Reset state back to before function was analyzed
21 void IntervalPartition::destroy() {
22   for_each(Intervals.begin(), Intervals.end(), deleter<Interval>);
23   IntervalMap.clear();
24   RootInterval = 0;
25 }
26
27 void IntervalPartition::print(std::ostream &O) const {
28   std::copy(Intervals.begin(), Intervals.end(),
29             std::ostream_iterator<const Interval *>(O, "\n"));
30 }
31
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).
35 //
36 void IntervalPartition::addIntervalToPartition(Interval *I) {
37   Intervals.push_back(I);
38
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();
41        It != End; ++It)
42     IntervalMap.insert(make_pair(*It, I));
43 }
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
57 // IntervalPartition ctor - Build the first level interval partition for the
58 // specified function...
59 //
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!?!?!");
64
65   addIntervalToPartition(RootInterval = *I);
66
67   ++I;  // After the first one...
68
69   // Add the rest of the intervals to the partition...
70   for_each(I, intervals_end(&F),
71            bind_obj(this, &IntervalPartition::addIntervalToPartition));
72
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));
77   return false;
78 }
79
80
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.
84 //
85 IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) {
86   Interval *FunctionStart = IP.getRootInterval();
87   assert(FunctionStart && "Cannot operate on empty IntervalPartitions!");
88
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!?!?!");
92
93   addIntervalToPartition(RootInterval = *I);
94
95   ++I;  // After the first one...
96
97   // Add the rest of the intervals to the partition...
98   for_each(I, intervals_end(IP),
99            bind_obj(this, &IntervalPartition::addIntervalToPartition));
100
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));
105 }