OLD | NEW |
---|---|
1 //===- subzero/src/IceCfg.cpp - Control flow graph implementation ---------===// | 1 //===- subzero/src/IceCfg.cpp - Control flow graph implementation ---------===// |
2 // | 2 // |
3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // This file implements the Cfg class, including constant pool | 10 // This file implements the Cfg class, including constant pool |
11 // management. | 11 // management. |
12 // | 12 // |
13 //===----------------------------------------------------------------------===// | 13 //===----------------------------------------------------------------------===// |
14 | 14 |
15 #include "IceCfg.h" | 15 #include "IceCfg.h" |
16 #include "IceCfgNode.h" | 16 #include "IceCfgNode.h" |
17 #include "IceDefs.h" | 17 #include "IceDefs.h" |
18 #include "IceInst.h" | 18 #include "IceInst.h" |
19 #include "IceLiveness.h" | |
19 #include "IceOperand.h" | 20 #include "IceOperand.h" |
20 #include "IceTargetLowering.h" | 21 #include "IceTargetLowering.h" |
21 | 22 |
22 namespace Ice { | 23 namespace Ice { |
23 | 24 |
24 Cfg::Cfg(GlobalContext *Ctx) | 25 Cfg::Cfg(GlobalContext *Ctx) |
25 : Ctx(Ctx), FunctionName(""), ReturnType(IceType_void), | 26 : Ctx(Ctx), FunctionName(""), ReturnType(IceType_void), |
26 IsInternalLinkage(false), HasError(false), ErrorMessage(""), Entry(NULL), | 27 IsInternalLinkage(false), HasError(false), ErrorMessage(""), Entry(NULL), |
27 NextInstNumber(1), | 28 NextInstNumber(1), Live(NULL), |
28 Target(TargetLowering::createLowering(Ctx->getTargetArch(), this)), | 29 Target(TargetLowering::createLowering(Ctx->getTargetArch(), this)), |
29 CurrentNode(NULL) {} | 30 CurrentNode(NULL) {} |
30 | 31 |
31 Cfg::~Cfg() {} | 32 Cfg::~Cfg() {} |
32 | 33 |
33 void Cfg::setError(const IceString &Message) { | 34 void Cfg::setError(const IceString &Message) { |
34 HasError = true; | 35 HasError = true; |
35 ErrorMessage = Message; | 36 ErrorMessage = Message; |
36 Ctx->getStrDump() << "ICE translation error: " << ErrorMessage << "\n"; | 37 Ctx->getStrDump() << "ICE translation error: " << ErrorMessage << "\n"; |
37 } | 38 } |
(...skipping 17 matching lines...) Expand all Loading... | |
55 void Cfg::addArg(Variable *Arg) { | 56 void Cfg::addArg(Variable *Arg) { |
56 Arg->setIsArg(this); | 57 Arg->setIsArg(this); |
57 Args.push_back(Arg); | 58 Args.push_back(Arg); |
58 } | 59 } |
59 | 60 |
60 // Returns whether the stack frame layout has been computed yet. This | 61 // Returns whether the stack frame layout has been computed yet. This |
61 // is used for dumping the stack frame location of Variables. | 62 // is used for dumping the stack frame location of Variables. |
62 bool Cfg::hasComputedFrame() const { return getTarget()->hasComputedFrame(); } | 63 bool Cfg::hasComputedFrame() const { return getTarget()->hasComputedFrame(); } |
63 | 64 |
64 void Cfg::translate() { | 65 void Cfg::translate() { |
65 Ostream &Str = Ctx->getStrDump(); | |
66 if (hasError()) | 66 if (hasError()) |
67 return; | 67 return; |
68 | 68 |
69 if (Ctx->isVerbose()) { | 69 dump("Initial CFG"); |
70 Str << "================ Initial CFG ================\n"; | |
71 dump(); | |
72 } | |
73 | 70 |
74 Timer T_translate; | 71 Timer T_translate; |
75 // The set of translation passes and their order are determined by | 72 // The set of translation passes and their order are determined by |
76 // the target. | 73 // the target. |
77 getTarget()->translate(); | 74 getTarget()->translate(); |
78 T_translate.printElapsedUs(getContext(), "translate()"); | 75 T_translate.printElapsedUs(getContext(), "translate()"); |
79 | 76 |
80 if (Ctx->isVerbose()) { | 77 dump("Final output"); |
81 Str << "================ Final output ================\n"; | |
82 dump(); | |
83 } | |
84 } | 78 } |
85 | 79 |
86 void Cfg::computePredecessors() { | 80 void Cfg::computePredecessors() { |
87 for (NodeList::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { | 81 for (NodeList::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { |
88 (*I)->computePredecessors(); | 82 (*I)->computePredecessors(); |
89 } | 83 } |
90 } | 84 } |
91 | 85 |
86 void Cfg::renumberInstructions() { | |
87 NextInstNumber = 1; | |
88 for (NodeList::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { | |
89 (*I)->renumberInstructions(); | |
90 } | |
91 } | |
92 | |
92 // placePhiLoads() must be called before placePhiStores(). | 93 // placePhiLoads() must be called before placePhiStores(). |
93 void Cfg::placePhiLoads() { | 94 void Cfg::placePhiLoads() { |
94 for (NodeList::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { | 95 for (NodeList::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { |
95 (*I)->placePhiLoads(); | 96 (*I)->placePhiLoads(); |
96 } | 97 } |
97 } | 98 } |
98 | 99 |
99 // placePhiStores() must be called after placePhiLoads(). | 100 // placePhiStores() must be called after placePhiLoads(). |
100 void Cfg::placePhiStores() { | 101 void Cfg::placePhiStores() { |
101 for (NodeList::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { | 102 for (NodeList::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { |
102 (*I)->placePhiStores(); | 103 (*I)->placePhiStores(); |
103 } | 104 } |
104 } | 105 } |
105 | 106 |
106 void Cfg::deletePhis() { | 107 void Cfg::deletePhis() { |
107 for (NodeList::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { | 108 for (NodeList::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { |
108 (*I)->deletePhis(); | 109 (*I)->deletePhis(); |
109 } | 110 } |
110 } | 111 } |
111 | 112 |
113 void Cfg::doAddressOpt() { | |
114 for (NodeList::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { | |
115 (*I)->doAddressOpt(); | |
116 } | |
117 } | |
118 | |
112 void Cfg::genCode() { | 119 void Cfg::genCode() { |
113 for (NodeList::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { | 120 for (NodeList::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { |
114 (*I)->genCode(); | 121 (*I)->genCode(); |
115 } | 122 } |
116 } | 123 } |
117 | 124 |
118 // Compute the stack frame layout. | 125 // Compute the stack frame layout. |
119 void Cfg::genFrame() { | 126 void Cfg::genFrame() { |
120 getTarget()->addProlog(Entry); | 127 getTarget()->addProlog(Entry); |
121 // TODO: Consider folding epilog generation into the final | 128 // TODO: Consider folding epilog generation into the final |
122 // emission/assembly pass to avoid an extra iteration over the node | 129 // emission/assembly pass to avoid an extra iteration over the node |
123 // list. Or keep a separate list of exit nodes. | 130 // list. Or keep a separate list of exit nodes. |
124 for (NodeList::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { | 131 for (NodeList::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { |
125 CfgNode *Node = *I; | 132 CfgNode *Node = *I; |
126 if (Node->getHasReturn()) | 133 if (Node->getHasReturn()) |
127 getTarget()->addEpilog(Node); | 134 getTarget()->addEpilog(Node); |
128 } | 135 } |
129 } | 136 } |
130 | 137 |
138 // This is a lightweight version of live-range-end calculation. Marks | |
139 // the last use of only those variables whose definition and uses are | |
140 // completely with a single block. It is a quick single pass and | |
141 // doesn't need to iterate until convergence. | |
142 void Cfg::livenessLightweight() { | |
143 for (NodeList::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { | |
144 (*I)->livenessLightweight(); | |
145 } | |
146 } | |
147 | |
148 void Cfg::liveness(LivenessMode Mode) { | |
149 Live.reset(new Liveness(this, Mode)); | |
150 Live->init(); | |
151 // Initialize with all nodes needing to be processed. | |
152 llvm::BitVector NeedToProcess(Nodes.size(), true); | |
153 while (NeedToProcess.any()) { | |
154 // Iterate in reverse topological order to speed up convergence. | |
155 for (NodeList::reverse_iterator I = Nodes.rbegin(), E = Nodes.rend(); | |
156 I != E; ++I) { | |
157 CfgNode *Node = *I; | |
158 if (NeedToProcess[Node->getIndex()]) { | |
159 NeedToProcess[Node->getIndex()] = false; | |
160 bool Changed = Node->liveness(getLiveness()); | |
161 if (Changed) { | |
162 // If the beginning-of-block liveness changed since the last | |
163 // iteration, mark all in-edges as needing to be processed. | |
164 const NodeList &InEdges = Node->getInEdges(); | |
165 for (NodeList::const_iterator I1 = InEdges.begin(), | |
166 E1 = InEdges.end(); | |
167 I1 != E1; ++I1) { | |
168 CfgNode *Pred = *I1; | |
169 NeedToProcess[Pred->getIndex()] = true; | |
170 } | |
171 } | |
172 } | |
173 } | |
174 } | |
175 if (Mode == Liveness_Intervals) { | |
176 // Reset each variable's live range. | |
177 for (VarList::const_iterator I = Variables.begin(), E = Variables.end(); | |
178 I != E; ++I) { | |
179 if (Variable *Var = *I) | |
180 Var->resetLiveRange(); | |
181 } | |
182 } | |
183 // Collect timing for just the portion that constructs the live | |
184 // range intervals based on the end-of-live-range computation, for a | |
185 // finer breakdown of the cost. | |
186 Timer T_liveRange; | |
187 // Make a final pass over instructions to delete dead instructions | |
188 // and build each Variable's live range. | |
189 for (NodeList::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { | |
190 (*I)->livenessPostprocess(Mode, getLiveness()); | |
191 } | |
192 if (Mode == Liveness_Intervals) { | |
193 // Special treatment for live in-args. Their liveness needs to | |
194 // extend beyond the beginning of the function, otherwise an arg | |
195 // whose only use is in the first instruction will end up having | |
196 // the trivial live range [1,1) and will *not* interfere with | |
197 // other arguments. So if the first instruction of the method is | |
198 // "r=arg1+arg2", both args may be assigned the same register. | |
199 for (SizeT I = 0; I < Args.size(); ++I) { | |
200 Variable *Arg = Args[I]; | |
201 if (!Live->getLiveRange(Arg).isEmpty()) { | |
202 // Add live range [-1,0) with weight 0. | |
203 Live->addLiveRange(Arg, -1, 0, 0); | |
204 } | |
205 // Do the same for i64 args that may have been lowered into i32 | |
206 // Lo and Hi components. | |
207 Variable *Lo = Arg->getLo(); | |
208 if (Lo && !Live->getLiveRange(Lo).isEmpty()) | |
209 Live->addLiveRange(Lo, -1, 0, 0); | |
210 Variable *Hi = Arg->getHi(); | |
211 if (Hi && !Live->getLiveRange(Hi).isEmpty()) | |
212 Live->addLiveRange(Hi, -1, 0, 0); | |
213 } | |
214 // Copy Liveness::LiveRanges into individual variables. TODO: | |
215 // Remove Variable::LiveRange and redirect to | |
216 // Liveness::LiveRanges. TODO: make sure Variable weights | |
217 // are applied properly. | |
218 SizeT NumVars = Variables.size(); | |
219 for (SizeT i = 0; i < NumVars; ++i) { | |
220 Variable *Var = Variables[i]; | |
221 Var->setLiveRange(Live->getLiveRange(Var)); | |
222 if (Var->getWeight().isInf()) | |
223 Var->setLiveRangeInfiniteWeight(); | |
224 setCurrentNode(NULL); | |
jvoung (off chromium)
2014/05/30 15:41:31
Does setCurrentNode(NULL) need to happen for every
Jim Stichnoth
2014/05/30 23:06:18
Loop invariant, but actually it doesn't need to be
| |
225 } | |
226 T_liveRange.printElapsedUs(getContext(), "live range construction"); | |
227 dump(); | |
228 } | |
229 } | |
230 | |
231 // Traverse every Variable of every Inst and verify that it | |
232 // appears within the Variable's computed live range. | |
233 bool Cfg::validateLiveness() const { | |
234 bool Valid = true; | |
235 Ostream &Str = Ctx->getStrDump(); | |
236 for (NodeList::const_iterator I1 = Nodes.begin(), E1 = Nodes.end(); I1 != E1; | |
237 ++I1) { | |
238 CfgNode *Node = *I1; | |
239 InstList &Insts = Node->getInsts(); | |
240 for (InstList::const_iterator I2 = Insts.begin(), E2 = Insts.end(); | |
241 I2 != E2; ++I2) { | |
242 Inst *Inst = *I2; | |
243 if (Inst->isDeleted()) | |
244 continue; | |
245 if (llvm::isa<InstFakeKill>(Inst)) | |
246 continue; | |
247 int32_t InstNumber = Inst->getNumber(); | |
248 Variable *Dest = Inst->getDest(); | |
249 if (Dest) { | |
250 // TODO: This instruction should actually begin Dest's live | |
251 // range, so we could probably test that this instruction is | |
252 // the beginning of some segment of Dest's live range. But | |
253 // this wouldn't work with non-SSA temporaries during | |
254 // lowering. | |
255 if (!Dest->getLiveRange().containsValue(InstNumber)) { | |
256 Valid = false; | |
257 Str << "Liveness error: inst " << Inst->getNumber() << " dest "; | |
258 Dest->dump(this); | |
259 Str << " live range " << Dest->getLiveRange() << "\n"; | |
260 } | |
261 } | |
262 for (SizeT I = 0; I < Inst->getSrcSize(); ++I) { | |
263 Operand *Src = Inst->getSrc(I); | |
264 SizeT NumVars = Src->getNumVars(); | |
265 for (SizeT J = 0; J < NumVars; ++J) { | |
266 const Variable *Var = Src->getVar(J); | |
267 if (!Var->getLiveRange().containsValue(InstNumber)) { | |
268 Valid = false; | |
269 Str << "Liveness error: inst " << Inst->getNumber() << " var "; | |
270 Var->dump(this); | |
271 Str << " live range " << Var->getLiveRange() << "\n"; | |
272 } | |
273 } | |
274 } | |
275 } | |
276 } | |
277 return Valid; | |
278 } | |
279 | |
131 // ======================== Dump routines ======================== // | 280 // ======================== Dump routines ======================== // |
132 | 281 |
133 void Cfg::emit() { | 282 void Cfg::emit() { |
134 Ostream &Str = Ctx->getStrEmit(); | 283 Ostream &Str = Ctx->getStrEmit(); |
135 Timer T_emit; | 284 Timer T_emit; |
136 if (!Ctx->testAndSetHasEmittedFirstMethod()) { | 285 if (!Ctx->testAndSetHasEmittedFirstMethod()) { |
137 // Print a helpful command for assembling the output. | 286 // Print a helpful command for assembling the output. |
138 // TODO: have the Target emit the header | 287 // TODO: have the Target emit the header |
139 // TODO: need a per-file emit in addition to per-CFG | 288 // TODO: need a per-file emit in addition to per-CFG |
140 Str << "# $LLVM_BIN_PATH/llvm-mc" | 289 Str << "# $LLVM_BIN_PATH/llvm-mc" |
(...skipping 10 matching lines...) Expand all Loading... | |
151 Str << "\t.type\t" << MangledName << ",@function\n"; | 300 Str << "\t.type\t" << MangledName << ",@function\n"; |
152 } | 301 } |
153 for (NodeList::const_iterator I = Nodes.begin(), E = Nodes.end(); I != E; | 302 for (NodeList::const_iterator I = Nodes.begin(), E = Nodes.end(); I != E; |
154 ++I) { | 303 ++I) { |
155 (*I)->emit(this); | 304 (*I)->emit(this); |
156 } | 305 } |
157 Str << "\n"; | 306 Str << "\n"; |
158 T_emit.printElapsedUs(Ctx, "emit()"); | 307 T_emit.printElapsedUs(Ctx, "emit()"); |
159 } | 308 } |
160 | 309 |
161 void Cfg::dump() { | 310 // Dumps the IR with an optional introductory message. |
311 void Cfg::dump(const IceString &Message) { | |
312 if (!Ctx->isVerbose()) | |
313 return; | |
162 Ostream &Str = Ctx->getStrDump(); | 314 Ostream &Str = Ctx->getStrDump(); |
315 if (!Message.empty()) | |
316 Str << "================ " << Message << " ================\n"; | |
163 setCurrentNode(getEntryNode()); | 317 setCurrentNode(getEntryNode()); |
164 // Print function name+args | 318 // Print function name+args |
165 if (getContext()->isVerbose(IceV_Instructions)) { | 319 if (getContext()->isVerbose(IceV_Instructions)) { |
166 Str << "define "; | 320 Str << "define "; |
167 if (getInternal()) | 321 if (getInternal()) |
168 Str << "internal "; | 322 Str << "internal "; |
169 Str << ReturnType << " @" << getFunctionName() << "("; | 323 Str << ReturnType << " @" << getFunctionName() << "("; |
170 for (SizeT i = 0; i < Args.size(); ++i) { | 324 for (SizeT i = 0; i < Args.size(); ++i) { |
171 if (i > 0) | 325 if (i > 0) |
172 Str << ", "; | 326 Str << ", "; |
173 Str << Args[i]->getType() << " "; | 327 Str << Args[i]->getType() << " "; |
174 Args[i]->dump(this); | 328 Args[i]->dump(this); |
175 } | 329 } |
176 Str << ") {\n"; | 330 Str << ") {\n"; |
177 } | 331 } |
178 setCurrentNode(NULL); | 332 setCurrentNode(NULL); |
333 if (getContext()->isVerbose(IceV_Liveness)) { | |
334 // Print summary info about variables | |
335 for (VarList::const_iterator I = Variables.begin(), E = Variables.end(); | |
336 I != E; ++I) { | |
337 Variable *Var = *I; | |
338 Str << "//" | |
339 << " multiblock=" << Var->isMultiblockLife() << " " | |
340 << " weight=" << Var->getWeight() << " "; | |
341 Var->dump(this); | |
342 Str << " LIVE=" << Var->getLiveRange() << "\n"; | |
343 } | |
344 } | |
179 // Print each basic block | 345 // Print each basic block |
180 for (NodeList::const_iterator I = Nodes.begin(), E = Nodes.end(); I != E; | 346 for (NodeList::const_iterator I = Nodes.begin(), E = Nodes.end(); I != E; |
181 ++I) { | 347 ++I) { |
182 (*I)->dump(this); | 348 (*I)->dump(this); |
183 } | 349 } |
184 if (getContext()->isVerbose(IceV_Instructions)) { | 350 if (getContext()->isVerbose(IceV_Instructions)) { |
185 Str << "}\n"; | 351 Str << "}\n"; |
186 } | 352 } |
187 } | 353 } |
188 | 354 |
189 } // end of namespace Ice | 355 } // end of namespace Ice |
OLD | NEW |