* Add support for different "PassType's"
[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");
15
16 AnalysisID IntervalPartition::ID(AnalysisID::create<IntervalPartition>(), true);
17
18 //===----------------------------------------------------------------------===//
19 // IntervalPartition Implementation
20 //===----------------------------------------------------------------------===//
21
22 // destroy - Reset state back to before function was analyzed
23 void IntervalPartition::destroy() {
24   for_each(begin(), end(), deleter<Interval>);
25   IntervalMap.clear();
26   RootInterval = 0;
27 }
28
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).
32 //
33 void IntervalPartition::addIntervalToPartition(Interval *I) {
34   push_back(I);
35
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();
38        It != End; ++It)
39     IntervalMap.insert(make_pair(*It, I));
40 }
41
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
45 // predecessor info.
46 //
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);
52 }
53
54 // IntervalPartition ctor - Build the first level interval partition for the
55 // specified function...
56 //
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!?!?!");
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(&F),
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 *FunctionStart = IP.getRootInterval();
84   assert(FunctionStart && "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 }