MEGAPATCH checkin.
[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 AnalysisID IntervalPartition::ID(AnalysisID::create<IntervalPartition>(), true);
14
15 //===----------------------------------------------------------------------===//
16 // IntervalPartition Implementation
17 //===----------------------------------------------------------------------===//
18
19 // destroy - Reset state back to before function was analyzed
20 void IntervalPartition::destroy() {
21   for_each(begin(), end(), deleter<Interval>);
22   IntervalMap.clear();
23   RootInterval = 0;
24 }
25
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).
29 //
30 void IntervalPartition::addIntervalToPartition(Interval *I) {
31   push_back(I);
32
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();
35        It != End; ++It)
36     IntervalMap.insert(make_pair(*It, I));
37 }
38
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
42 // predecessor info.
43 //
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);
49 }
50
51 // IntervalPartition ctor - Build the first level interval partition for the
52 // specified function...
53 //
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!?!?!");
58
59   addIntervalToPartition(RootInterval = *I);
60
61   ++I;  // After the first one...
62
63   // Add the rest of the intervals to the partition...
64   for_each(I, intervals_end(&F),
65            bind_obj(this, &IntervalPartition::addIntervalToPartition));
66
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));
71   return false;
72 }
73
74
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.
78 //
79 IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) {
80   Interval *FunctionStart = IP.getRootInterval();
81   assert(FunctionStart && "Cannot operate on empty IntervalPartitions!");
82
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!?!?!");
86
87   addIntervalToPartition(RootInterval = *I);
88
89   ++I;  // After the first one...
90
91   // Add the rest of the intervals to the partition...
92   for_each(I, intervals_end(IP),
93            bind_obj(this, &IntervalPartition::addIntervalToPartition));
94
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));
99 }