llvm.org GIT mirror llvm / 107109c
IntervalPartition & IntervalIterator classes have been split out into their own .h files & .cpp file git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@62 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 19 years ago
1 changed file(s) with 4 addition(s) and 197 deletion(s). Raw diff Collapse all Expand all
None //===- Intervals.cpp - Interval partition Calculation ------------*- C++ -*--=//
0 //===- Interval.cpp - Interval class code ------------------------*- C++ -*--=//
11 //
2 // This file contains the declaration of the cfg::IntervalPartition class, which
3 // calculates and represent the interval partition of a method.
2 // This file contains the definition of the cfg::Interval class, which
3 // represents a partition of a control flow graph of some kind.
44 //
55 //===----------------------------------------------------------------------===//
66
7 #include "llvm/Analysis/Intervals.h"
8 #include "llvm/Method.h"
7 #include "llvm/Analysis/Interval.h"
98 #include "llvm/BasicBlock.h"
109 #include "llvm/CFG.h"
1110
2827 }
2928
3029
31 //===----------------------------------------------------------------------===//
32 // IntervalPartition Implementation
33 //===----------------------------------------------------------------------===//
34
35 template static inline void deleter(T *Ptr) { delete Ptr; }
36
37 // Destructor - Free memory
38 IntervalPartition::~IntervalPartition() {
39 for_each(begin(), end(), deleter);
40 }
41
42 #if 0
43 // getNodeHeader - Given a source graph node and the source graph, return the
44 // BasicBlock that is the header node. This is the opposite of
45 // getSourceGraphNode.
46 //
47 inline static BasicBlock *getNodeHeader(BasicBlock *BB) { return BB; }
48 inline static BasicBlock *getNodeHeader(Interval *I) { return I->getHeaderNode(); }
49
50 // getSourceGraphNode - Given a BasicBlock and the source graph, return the
51 // source graph node that corresponds to the BasicBlock. This is the opposite
52 // of getNodeHeader.
53 //
54 inline static BasicBlock *getSourceGraphNode(Method *, BasicBlock *BB) {
55 return BB;
56 }
57 inline static Interval *getSourceGraphNode(IntervalPartition *IP,
58 BasicBlock *BB) {
59 return IP->getBlockInterval(BB);
60 }
61 #endif
62
63
64 // addNodeToInterval - This method exists to assist the generic ProcessNode
65 // with the task of adding a node to the new interval, depending on the
66 // type of the source node. In the case of a CFG source graph (BasicBlock
67 // case), the BasicBlock itself is added to the interval.
68 //
69 inline void IntervalPartition::addNodeToInterval(Interval *Int, BasicBlock *BB){
70 Int->Nodes.push_back(BB);
71 IntervalMap.insert(make_pair(BB, Int));
72 }
73
74 // addNodeToInterval - This method exists to assist the generic ProcessNode
75 // with the task of adding a node to the new interval, depending on the
76 // type of the source node. In the case of a CFG source graph (BasicBlock
77 // case), the BasicBlock itself is added to the interval. In the case of
78 // an IntervalPartition source graph (Interval case), all of the member
79 // BasicBlocks are added to the interval.
80 //
81 inline void IntervalPartition::addNodeToInterval(Interval *Int, Interval *I) {
82 // Add all of the nodes in I as new nodes in Int.
83 copy(I->Nodes.begin(), I->Nodes.end(), back_inserter(Int->Nodes));
84
85 // Add mappings for all of the basic blocks in I to the IntervalPartition
86 for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end();
87 It != End; ++It)
88 IntervalMap.insert(make_pair(*It, Int));
89 }
90
91
92 // ProcessNode - This method is called by ProcessInterval to add nodes to the
93 // interval being constructed, and it is also called recursively as it walks
94 // the source graph. A node is added to the current interval only if all of
95 // its predecessors are already in the graph. This also takes care of keeping
96 // the successor set of an interval up to date.
97 //
98 // This method is templated because it may operate on two different source
99 // graphs: a basic block graph, or a preexisting interval graph.
100 //
101 template
102 void IntervalPartition::ProcessNode(Interval *Int,
103 NodeTy *Node, OrigContainer *OC) {
104 assert(Int && "Null interval == bad!");
105 assert(Node && "Null Node == bad!");
106
107 BasicBlock *NodeHeader = getNodeHeader(Node);
108 Interval *CurInt = getBlockInterval(NodeHeader);
109 if (CurInt == Int) { // Already in this interval...
110 return;
111 } else if (CurInt != 0) { // In another interval, add as successor
112 if (!Int->isSuccessor(NodeHeader)) // Add only if not already in set
113 Int->Successors.push_back(NodeHeader);
114 } else { // Otherwise, not in interval yet
115 for (typename NodeTy::pred_iterator I = pred_begin(Node),
116 E = pred_end(Node); I != E; ++I) {
117 if (!Int->contains(*I)) { // If pred not in interval, we can't be
118 if (!Int->isSuccessor(NodeHeader)) // Add only if not already in set
119 Int->Successors.push_back(NodeHeader);
120 return; // See you later
121 }
122 }
123
124 // If we get here, then all of the predecessors of BB are in the interval
125 // already. In this case, we must add BB to the interval!
126 addNodeToInterval(Int, Node);
127
128 if (Int->isSuccessor(NodeHeader)) {
129 // If we were in the successor list from before... remove from succ list
130 Int->Successors.erase(remove(Int->Successors.begin(),
131 Int->Successors.end(), NodeHeader),
132 Int->Successors.end());
133 }
134
135 // Now that we have discovered that Node is in the interval, perhaps some of
136 // its successors are as well?
137 for (typename NodeTy::succ_iterator It = succ_begin(Node),
138 End = succ_end(Node); It != End; ++It)
139 ProcessNode(Int, getSourceGraphNode(OC, *It), OC);
140 }
141 }
142
143
144 // ProcessInterval - This method is used during the construction of the
145 // interval graph. It walks through the source graph, recursively creating
146 // an interval per invokation until the entire graph is covered. This uses
147 // the ProcessNode method to add all of the nodes to the interval.
148 //
149 // This method is templated because it may operate on two different source
150 // graphs: a basic block graph, or a preexisting interval graph.
151 //
152 template
153 void IntervalPartition::ProcessInterval(NodeTy *Node, OrigContainer *OC) {
154 BasicBlock *Header = getNodeHeader(Node);
155 if (getBlockInterval(Header)) return; // Interval already constructed?
156
157 // Create a new interval and add the interval to our current set
158 Interval *Int = new Interval(Header);
159 IntervalList.push_back(Int);
160 IntervalMap.insert(make_pair(Header, Int));
161
162 // Check all of our successors to see if they are in the interval...
163 for (typename NodeTy::succ_iterator I = succ_begin(Node), E = succ_end(Node);
164 I != E; ++I)
165 ProcessNode(Int, getSourceGraphNode(OC, *I), OC);
166
167 // Build all of the successor intervals of this interval now...
168 for(Interval::succ_iterator I = Int->Successors.begin(),
169 E = Int->Successors.end(); I != E; ++I) {
170 ProcessInterval(getSourceGraphNode(OC, *I), OC);
171 }
172 }
173
174
175
176 // updatePredecessors - Interval generation only sets the successor fields of
177 // the interval data structures. After interval generation is complete,
178 // run through all of the intervals and propogate successor info as
179 // predecessor info.
180 //
181 void IntervalPartition::updatePredecessors(cfg::Interval *Int) {
182 BasicBlock *Header = Int->HeaderNode;
183 for (Interval::succ_iterator I = Int->Successors.begin(),
184 E = Int->Successors.end(); I != E; ++I)
185 getBlockInterval(*I)->Predecessors.push_back(Header);
186 }
187
188
189
190 // IntervalPartition ctor - Build the first level interval partition for the
191 // specified method...
192 //
193 IntervalPartition::IntervalPartition(Method *M) {
194 BasicBlock *MethodStart = M->getBasicBlocks().front();
195 assert(MethodStart && "Cannot operate on prototypes!");
196
197 ProcessInterval(MethodStart, M);
198 RootInterval = getBlockInterval(MethodStart);
199
200 // Now that we know all of the successor information, propogate this to the
201 // predecessors for each block...
202 for(iterator I = begin(), E = end(); I != E; ++I)
203 updatePredecessors(*I);
204 }
205
206
207 // IntervalPartition ctor - Build a reduced interval partition from an
208 // existing interval graph. This takes an additional boolean parameter to
209 // distinguish it from a copy constructor. Always pass in false for now.
210 //
211 IntervalPartition::IntervalPartition(IntervalPartition &I, bool) {
212 Interval *MethodStart = I.getRootInterval();
213 assert(MethodStart && "Cannot operate on empty IntervalPartitions!");
214
215 ProcessInterval(MethodStart, &I);
216 RootInterval = getBlockInterval(*MethodStart->Nodes.begin());
217
218 // Now that we know all of the successor information, propogate this to the
219 // predecessors for each block...
220 for(iterator I = begin(), E = end(); I != E; ++I)
221 updatePredecessors(*I);
222 }