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