Added LLVM project notice to the top of every C++ source file.
[oota-llvm.git] / lib / Analysis / IntervalPartition.cpp
1 //===- IntervalPartition.cpp - Interval Partition module code -------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the definition of the IntervalPartition class, which
11 // calculates and represent the interval partition of a function.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Analysis/IntervalIterator.h"
16 #include "Support/STLExtras.h"
17
18 static RegisterAnalysis<IntervalPartition>
19 X("intervals", "Interval Partition Construction", true);
20
21 //===----------------------------------------------------------------------===//
22 // IntervalPartition Implementation
23 //===----------------------------------------------------------------------===//
24
25 // destroy - Reset state back to before function was analyzed
26 void IntervalPartition::destroy() {
27   for_each(Intervals.begin(), Intervals.end(), deleter<Interval>);
28   IntervalMap.clear();
29   RootInterval = 0;
30 }
31
32 void IntervalPartition::print(std::ostream &O) const {
33   std::copy(Intervals.begin(), Intervals.end(),
34             std::ostream_iterator<const Interval *>(O, "\n"));
35 }
36
37 // addIntervalToPartition - Add an interval to the internal list of intervals,
38 // and then add mappings from all of the basic blocks in the interval to the
39 // interval itself (in the IntervalMap).
40 //
41 void IntervalPartition::addIntervalToPartition(Interval *I) {
42   Intervals.push_back(I);
43
44   // Add mappings for all of the basic blocks in I to the IntervalPartition
45   for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end();
46        It != End; ++It)
47     IntervalMap.insert(std::make_pair(*It, I));
48 }
49
50 // updatePredecessors - Interval generation only sets the successor fields of
51 // the interval data structures.  After interval generation is complete,
52 // run through all of the intervals and propagate successor info as
53 // predecessor info.
54 //
55 void IntervalPartition::updatePredecessors(Interval *Int) {
56   BasicBlock *Header = Int->getHeaderNode();
57   for (Interval::succ_iterator I = Int->Successors.begin(), 
58                                E = Int->Successors.end(); I != E; ++I)
59     getBlockInterval(*I)->Predecessors.push_back(Header);
60 }
61
62 // IntervalPartition ctor - Build the first level interval partition for the
63 // specified function...
64 //
65 bool IntervalPartition::runOnFunction(Function &F) {
66   // Pass false to intervals_begin because we take ownership of it's memory
67   function_interval_iterator I = intervals_begin(&F, false);
68   assert(I != intervals_end(&F) && "No intervals in function!?!?!");
69
70   addIntervalToPartition(RootInterval = *I);
71
72   ++I;  // After the first one...
73
74   // Add the rest of the intervals to the partition...
75   for_each(I, intervals_end(&F),
76            bind_obj(this, &IntervalPartition::addIntervalToPartition));
77
78   // Now that we know all of the successor information, propagate this to the
79   // predecessors for each block...
80   for_each(Intervals.begin(), Intervals.end(), 
81            bind_obj(this, &IntervalPartition::updatePredecessors));
82   return false;
83 }
84
85
86 // IntervalPartition ctor - Build a reduced interval partition from an
87 // existing interval graph.  This takes an additional boolean parameter to
88 // distinguish it from a copy constructor.  Always pass in false for now.
89 //
90 IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) {
91   Interval *FunctionStart = IP.getRootInterval();
92   assert(FunctionStart && "Cannot operate on empty IntervalPartitions!");
93
94   // Pass false to intervals_begin because we take ownership of it's memory
95   interval_part_interval_iterator I = intervals_begin(IP, false);
96   assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
97
98   addIntervalToPartition(RootInterval = *I);
99
100   ++I;  // After the first one...
101
102   // Add the rest of the intervals to the partition...
103   for_each(I, intervals_end(IP),
104            bind_obj(this, &IntervalPartition::addIntervalToPartition));
105
106   // Now that we know all of the successor information, propagate this to the
107   // predecessors for each block...
108   for_each(Intervals.begin(), Intervals.end(), 
109            bind_obj(this, &IntervalPartition::updatePredecessors));
110 }