Remove use of bind_obj, deleter, and finegrainify namespacification.
[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 using namespace llvm;
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 (unsigned i = 0, e = Intervals.size(); i != e; ++i)
28     delete Intervals[i];
29   IntervalMap.clear();
30   RootInterval = 0;
31 }
32
33 void IntervalPartition::print(std::ostream &O, const Module*) const {
34   std::copy(Intervals.begin(), Intervals.end(),
35             std::ostream_iterator<const Interval *>(O, "\n"));
36 }
37
38 // addIntervalToPartition - Add an interval to the internal list of intervals,
39 // and then add mappings from all of the basic blocks in the interval to the
40 // interval itself (in the IntervalMap).
41 //
42 void IntervalPartition::addIntervalToPartition(Interval *I) {
43   Intervals.push_back(I);
44
45   // Add mappings for all of the basic blocks in I to the IntervalPartition
46   for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end();
47        It != End; ++It)
48     IntervalMap.insert(std::make_pair(*It, I));
49 }
50
51 // updatePredecessors - Interval generation only sets the successor fields of
52 // the interval data structures.  After interval generation is complete,
53 // run through all of the intervals and propagate successor info as
54 // predecessor info.
55 //
56 void IntervalPartition::updatePredecessors(Interval *Int) {
57   BasicBlock *Header = Int->getHeaderNode();
58   for (Interval::succ_iterator I = Int->Successors.begin(), 
59                                E = Int->Successors.end(); I != E; ++I)
60     getBlockInterval(*I)->Predecessors.push_back(Header);
61 }
62
63 // IntervalPartition ctor - Build the first level interval partition for the
64 // specified function...
65 //
66 bool IntervalPartition::runOnFunction(Function &F) {
67   // Pass false to intervals_begin because we take ownership of it's memory
68   function_interval_iterator I = intervals_begin(&F, false);
69   assert(I != intervals_end(&F) && "No intervals in function!?!?!");
70
71   addIntervalToPartition(RootInterval = *I);
72
73   ++I;  // After the first one...
74
75   // Add the rest of the intervals to the partition.
76   for (function_interval_iterator E = intervals_end(&F); I != E; ++I)
77     addIntervalToPartition(*I);
78
79   // Now that we know all of the successor information, propagate this to the
80   // predecessors for each block.
81   for (unsigned i = 0, e = Intervals.size(); i != e; ++i)
82     updatePredecessors(Intervals[i]);
83   return false;
84 }
85
86
87 // IntervalPartition ctor - Build a reduced interval partition from an
88 // existing interval graph.  This takes an additional boolean parameter to
89 // distinguish it from a copy constructor.  Always pass in false for now.
90 //
91 IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) {
92   Interval *FunctionStart = IP.getRootInterval();
93   assert(FunctionStart && "Cannot operate on empty IntervalPartitions!");
94
95   // Pass false to intervals_begin because we take ownership of it's memory
96   interval_part_interval_iterator I = intervals_begin(IP, false);
97   assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
98
99   addIntervalToPartition(RootInterval = *I);
100
101   ++I;  // After the first one...
102
103   // Add the rest of the intervals to the partition.
104   for (interval_part_interval_iterator E = intervals_end(IP); I != E; ++I)
105     addIntervalToPartition(*I);
106
107   // Now that we know all of the successor information, propagate this to the
108   // predecessors for each block.
109   for (unsigned i = 0, e = Intervals.size(); i != e; ++i)
110     updatePredecessors(Intervals[i]);
111 }
112