Convert analyses to new pass structure
[oota-llvm.git] / lib / Analysis / IntervalPartition.cpp
1 //===- IntervalPartition.cpp - Interval Partition module code ----*- C++ -*--=//
2 //
3 // This file contains the definition of the cfg::IntervalPartition class, which
4 // calculates and represent the interval partition of a method.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/Analysis/IntervalIterator.h"
9 #include "Support/STLExtras.h"
10
11 using namespace cfg;
12 using std::make_pair;
13
14 AnalysisID IntervalPartition::ID(AnalysisID::create<IntervalPartition>());
15
16 //===----------------------------------------------------------------------===//
17 // IntervalPartition Implementation
18 //===----------------------------------------------------------------------===//
19
20 // destroy - Reset state back to before method was analyzed
21 void IntervalPartition::destroy() {
22   for_each(begin(), end(), deleter<cfg::Interval>);
23   IntervalMap.clear();
24   RootInterval = 0;
25 }
26
27 // addIntervalToPartition - Add an interval to the internal list of intervals,
28 // and then add mappings from all of the basic blocks in the interval to the
29 // interval itself (in the IntervalMap).
30 //
31 void IntervalPartition::addIntervalToPartition(Interval *I) {
32   push_back(I);
33
34   // Add mappings for all of the basic blocks in I to the IntervalPartition
35   for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end();
36        It != End; ++It)
37     IntervalMap.insert(make_pair(*It, I));
38 }
39
40 // updatePredecessors - Interval generation only sets the successor fields of
41 // the interval data structures.  After interval generation is complete,
42 // run through all of the intervals and propogate successor info as
43 // predecessor info.
44 //
45 void IntervalPartition::updatePredecessors(cfg::Interval *Int) {
46   BasicBlock *Header = Int->getHeaderNode();
47   for (Interval::succ_iterator I = Int->Successors.begin(), 
48                                E = Int->Successors.end(); I != E; ++I)
49     getBlockInterval(*I)->Predecessors.push_back(Header);
50 }
51
52 // IntervalPartition ctor - Build the first level interval partition for the
53 // specified method...
54 //
55 bool IntervalPartition::runOnMethod(Method *M) {
56   assert(M->front() && "Cannot operate on prototypes!");
57
58   // Pass false to intervals_begin because we take ownership of it's memory
59   method_interval_iterator I = intervals_begin(M, false);
60   assert(I != intervals_end(M) && "No intervals in method!?!?!");
61
62   addIntervalToPartition(RootInterval = *I);
63
64   ++I;  // After the first one...
65
66   // Add the rest of the intervals to the partition...
67   for_each(I, intervals_end(M),
68            bind_obj(this, &IntervalPartition::addIntervalToPartition));
69
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));
74   return false;
75 }
76
77
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.
81 //
82 IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) {
83   Interval *MethodStart = IP.getRootInterval();
84   assert(MethodStart && "Cannot operate on empty IntervalPartitions!");
85
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!?!?!");
89
90   addIntervalToPartition(RootInterval = *I);
91
92   ++I;  // After the first one...
93
94   // Add the rest of the intervals to the partition...
95   for_each(I, intervals_end(IP),
96            bind_obj(this, &IntervalPartition::addIntervalToPartition));
97
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));
102 }