blob: ddadc577bfced49d7beeeeeec296c265fd3fe7bd [file] [log] [blame]
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001//===- subzero/src/IceCfgNode.cpp - Basic block (node) 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//
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070010// This file implements the CfgNode class, including the complexities
11// of instruction insertion and in-edge calculation.
Jim Stichnothf7c9a142014-04-29 10:52:43 -070012//
13//===----------------------------------------------------------------------===//
14
John Portoaff4ccf2015-06-10 16:35:06 -070015#include "IceAssembler.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070016#include "IceCfg.h"
17#include "IceCfgNode.h"
John Portof8b4cc82015-06-09 18:06:19 -070018#include "IceGlobalInits.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070019#include "IceInst.h"
Jim Stichnothd97c7df2014-06-04 11:57:08 -070020#include "IceLiveness.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070021#include "IceOperand.h"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070022#include "IceTargetLowering.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070023
24namespace Ice {
25
Jim Stichnoth668a7a32014-12-10 15:32:25 -080026CfgNode::CfgNode(Cfg *Func, SizeT LabelNumber)
Jim Stichnotheafb56c2015-06-22 10:35:22 -070027 : Func(Func), Number(LabelNumber) {}
Jim Stichnothf7c9a142014-04-29 10:52:43 -070028
29// Returns the name the node was created with. If no name was given,
30// it synthesizes a (hopefully) unique name.
31IceString CfgNode::getName() const {
Jim Stichnoth668a7a32014-12-10 15:32:25 -080032 if (NameIndex >= 0)
Jim Stichnoth9a04c072014-12-11 15:51:42 -080033 return Func->getIdentifierName(NameIndex);
Jim Stichnoth088b2be2014-10-23 12:02:08 -070034 return "__" + std::to_string(getIndex());
Jim Stichnothf7c9a142014-04-29 10:52:43 -070035}
36
37// Adds an instruction to either the Phi list or the regular
38// instruction list. Validates that all Phis are added before all
39// regular instructions.
40void CfgNode::appendInst(Inst *Inst) {
Jim Stichnoth47752552014-10-13 17:15:08 -070041 ++InstCountEstimate;
Jim Stichnothf7c9a142014-04-29 10:52:43 -070042 if (InstPhi *Phi = llvm::dyn_cast<InstPhi>(Inst)) {
43 if (!Insts.empty()) {
44 Func->setError("Phi instruction added to the middle of a block");
45 return;
46 }
47 Phis.push_back(Phi);
48 } else {
49 Insts.push_back(Inst);
50 }
Jim Stichnothf7c9a142014-04-29 10:52:43 -070051}
52
Jim Stichnothd97c7df2014-06-04 11:57:08 -070053// Renumbers the non-deleted instructions in the node. This needs to
54// be done in preparation for live range analysis. The instruction
55// numbers in a block must be monotonically increasing. The range of
56// instruction numbers in a block, from lowest to highest, must not
57// overlap with the range of any other block.
58void CfgNode::renumberInstructions() {
Jim Stichnoth47752552014-10-13 17:15:08 -070059 InstNumberT FirstNumber = Func->getNextInstNumber();
Jim Stichnoth29841e82014-12-23 12:26:24 -080060 for (Inst &I : Phis)
61 I.renumber(Func);
62 for (Inst &I : Insts)
63 I.renumber(Func);
Jim Stichnoth47752552014-10-13 17:15:08 -070064 InstCountEstimate = Func->getNextInstNumber() - FirstNumber;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070065}
66
Jim Stichnoth088b2be2014-10-23 12:02:08 -070067// When a node is created, the OutEdges are immediately known, but the
Jim Stichnothf7c9a142014-04-29 10:52:43 -070068// InEdges have to be built up incrementally. After the CFG has been
69// constructed, the computePredecessors() pass finalizes it by
70// creating the InEdges list.
71void CfgNode::computePredecessors() {
Jim Stichnothf44f3712014-10-01 14:05:51 -070072 for (CfgNode *Succ : OutEdges)
73 Succ->InEdges.push_back(this);
Jim Stichnothf7c9a142014-04-29 10:52:43 -070074}
75
Jim Stichnoth69d3f9c2015-03-23 10:33:38 -070076void CfgNode::computeSuccessors() {
77 OutEdges = Insts.rbegin()->getTerminatorEdges();
78}
79
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070080// This does part 1 of Phi lowering, by creating a new dest variable
81// for each Phi instruction, replacing the Phi instruction's dest with
82// that variable, and adding an explicit assignment of the old dest to
83// the new dest. For example,
84// a=phi(...)
85// changes to
86// "a_phi=phi(...); a=a_phi".
87//
88// This is in preparation for part 2 which deletes the Phi
89// instructions and appends assignment instructions to predecessor
90// blocks. Note that this transformation preserves SSA form.
91void CfgNode::placePhiLoads() {
Jim Stichnoth29841e82014-12-23 12:26:24 -080092 for (Inst &I : Phis) {
93 auto Phi = llvm::dyn_cast<InstPhi>(&I);
Jim Stichnoth1502e592014-12-11 09:22:45 -080094 Insts.insert(Insts.begin(), Phi->lower(Func));
95 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070096}
97
98// This does part 2 of Phi lowering. For each Phi instruction at each
99// out-edge, create a corresponding assignment instruction, and add
100// all the assignments near the end of this block. They need to be
101// added before any branch instruction, and also if the block ends
102// with a compare instruction followed by a branch instruction that we
103// may want to fuse, it's better to insert the new assignments before
Jan Voungc820ddf2014-07-29 14:38:51 -0700104// the compare instruction. The tryOptimizedCmpxchgCmpBr() method
105// assumes this ordering of instructions.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700106//
107// Note that this transformation takes the Phi dest variables out of
108// SSA form, as there may be assignments to the dest variable in
109// multiple blocks.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700110void CfgNode::placePhiStores() {
Jim Stichnothe5a5be72014-09-10 11:51:38 -0700111 // Find the insertion point.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700112 InstList::iterator InsertionPoint = Insts.end();
Jim Stichnothe5a5be72014-09-10 11:51:38 -0700113 // Every block must end in a terminator instruction, and therefore
114 // must have at least one instruction, so it's valid to decrement
115 // InsertionPoint (but assert just in case).
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700116 assert(InsertionPoint != Insts.begin());
117 --InsertionPoint;
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700118 // Confirm that InsertionPoint is a terminator instruction. Calling
119 // getTerminatorEdges() on a non-terminator instruction will cause
120 // an llvm_unreachable().
Jim Stichnoth607e9f02014-11-06 13:32:05 -0800121 (void)InsertionPoint->getTerminatorEdges();
Jim Stichnothe5a5be72014-09-10 11:51:38 -0700122 // SafeInsertionPoint is always immediately before the terminator
123 // instruction. If the block ends in a compare and conditional
124 // branch, it's better to place the Phi store before the compare so
125 // as not to interfere with compare/branch fusing. However, if the
126 // compare instruction's dest operand is the same as the new
127 // assignment statement's source operand, this can't be done due to
128 // data dependences, so we need to fall back to the
129 // SafeInsertionPoint. To illustrate:
130 // ; <label>:95
131 // %97 = load i8* %96, align 1
132 // %98 = icmp ne i8 %97, 0
133 // br i1 %98, label %99, label %2132
134 // ; <label>:99
135 // %100 = phi i8 [ %97, %95 ], [ %110, %108 ]
136 // %101 = phi i1 [ %98, %95 ], [ %111, %108 ]
137 // would be Phi-lowered as:
138 // ; <label>:95
139 // %97 = load i8* %96, align 1
140 // %100_phi = %97 ; can be at InsertionPoint
141 // %98 = icmp ne i8 %97, 0
142 // %101_phi = %98 ; must be at SafeInsertionPoint
143 // br i1 %98, label %99, label %2132
144 // ; <label>:99
145 // %100 = %100_phi
146 // %101 = %101_phi
147 //
148 // TODO(stichnot): It may be possible to bypass this whole
149 // SafeInsertionPoint mechanism. If a source basic block ends in a
150 // conditional branch:
151 // labelSource:
152 // ...
153 // br i1 %foo, label %labelTrue, label %labelFalse
154 // and a branch target has a Phi involving the branch operand:
155 // labelTrue:
156 // %bar = phi i1 [ %foo, %labelSource ], ...
157 // then we actually know the constant i1 value of the Phi operand:
158 // labelTrue:
159 // %bar = phi i1 [ true, %labelSource ], ...
160 // It seems that this optimization should be done by clang or opt,
161 // but we could also do it here.
162 InstList::iterator SafeInsertionPoint = InsertionPoint;
163 // Keep track of the dest variable of a compare instruction, so that
164 // we insert the new instruction at the SafeInsertionPoint if the
165 // compare's dest matches the Phi-lowered assignment's source.
Jim Stichnothae953202014-12-20 06:17:49 -0800166 Variable *CmpInstDest = nullptr;
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700167 // If the current insertion point is at a conditional branch
168 // instruction, and the previous instruction is a compare
169 // instruction, then we move the insertion point before the compare
170 // instruction so as not to interfere with compare/branch fusing.
Jim Stichnoth607e9f02014-11-06 13:32:05 -0800171 if (InstBr *Branch = llvm::dyn_cast<InstBr>(InsertionPoint)) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700172 if (!Branch->isUnconditional()) {
173 if (InsertionPoint != Insts.begin()) {
174 --InsertionPoint;
Jim Stichnoth607e9f02014-11-06 13:32:05 -0800175 if (llvm::isa<InstIcmp>(InsertionPoint) ||
176 llvm::isa<InstFcmp>(InsertionPoint)) {
177 CmpInstDest = InsertionPoint->getDest();
Jim Stichnothe5a5be72014-09-10 11:51:38 -0700178 } else {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700179 ++InsertionPoint;
180 }
181 }
182 }
183 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700184
185 // Consider every out-edge.
Jim Stichnothf44f3712014-10-01 14:05:51 -0700186 for (CfgNode *Succ : OutEdges) {
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700187 // Consider every Phi instruction at the out-edge.
Jim Stichnoth29841e82014-12-23 12:26:24 -0800188 for (Inst &I : Succ->Phis) {
189 auto Phi = llvm::dyn_cast<InstPhi>(&I);
Jim Stichnoth1502e592014-12-11 09:22:45 -0800190 Operand *Operand = Phi->getOperandForTarget(this);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700191 assert(Operand);
Jim Stichnoth29841e82014-12-23 12:26:24 -0800192 Variable *Dest = I.getDest();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700193 assert(Dest);
194 InstAssign *NewInst = InstAssign::create(Func, Dest, Operand);
Jim Stichnothe5a5be72014-09-10 11:51:38 -0700195 if (CmpInstDest == Operand)
196 Insts.insert(SafeInsertionPoint, NewInst);
197 else
198 Insts.insert(InsertionPoint, NewInst);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700199 }
200 }
201}
202
203// Deletes the phi instructions after the loads and stores are placed.
204void CfgNode::deletePhis() {
Jim Stichnoth29841e82014-12-23 12:26:24 -0800205 for (Inst &I : Phis)
206 I.setDeleted();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700207}
208
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700209// Splits the edge from Pred to this node by creating a new node and
210// hooking up the in and out edges appropriately. (The EdgeIndex
211// parameter is only used to make the new node's name unique when
212// there are multiple edges between the same pair of nodes.) The new
213// node's instruction list is initialized to the empty list, with no
214// terminator instruction. If there are multiple edges from Pred to
215// this node, only one edge is split, and the particular choice of
216// edge is undefined. This could happen with a switch instruction, or
217// a conditional branch that weirdly has both branches to the same
218// place. TODO(stichnot,kschimpf): Figure out whether this is legal
219// in the LLVM IR or the PNaCl bitcode, and if so, we need to
220// establish a strong relationship among the ordering of Pred's
221// out-edge list, this node's in-edge list, and the Phi instruction's
222// operand list.
223CfgNode *CfgNode::splitIncomingEdge(CfgNode *Pred, SizeT EdgeIndex) {
Jim Stichnoth668a7a32014-12-10 15:32:25 -0800224 CfgNode *NewNode = Func->makeNode();
225 if (ALLOW_DUMP)
226 NewNode->setName("split_" + Pred->getName() + "_" + getName() + "_" +
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700227 std::to_string(EdgeIndex));
228 // The new node is added to the end of the node list, and will later
229 // need to be sorted into a reasonable topological order.
230 NewNode->setNeedsPlacement(true);
231 // Repoint Pred's out-edge.
232 bool Found = false;
233 for (auto I = Pred->OutEdges.begin(), E = Pred->OutEdges.end();
234 !Found && I != E; ++I) {
235 if (*I == this) {
236 *I = NewNode;
237 NewNode->InEdges.push_back(Pred);
238 Found = true;
239 }
240 }
241 assert(Found);
242 // Repoint this node's in-edge.
243 Found = false;
244 for (auto I = InEdges.begin(), E = InEdges.end(); !Found && I != E; ++I) {
245 if (*I == Pred) {
246 *I = NewNode;
247 NewNode->OutEdges.push_back(this);
248 Found = true;
249 }
250 }
251 assert(Found);
Jim Stichnoth7e571362015-01-09 11:43:26 -0800252 // Repoint a suitable branch instruction's target and return.
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700253 Found = false;
Jim Stichnoth7e571362015-01-09 11:43:26 -0800254 for (Inst &I : reverse_range(Pred->getInsts())) {
255 if (!I.isDeleted() && I.repointEdge(this, NewNode))
256 return NewNode;
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700257 }
Jim Stichnoth7e571362015-01-09 11:43:26 -0800258 // This should be unreachable, so the assert will fail.
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700259 assert(Found);
260 return NewNode;
261}
262
263namespace {
264
265// Helper function used by advancedPhiLowering().
266bool sameVarOrReg(const Variable *Var, const Operand *Opnd) {
267 if (Var == Opnd)
268 return true;
269 if (const auto Var2 = llvm::dyn_cast<Variable>(Opnd)) {
270 if (Var->hasReg() && Var->getRegNum() == Var2->getRegNum())
271 return true;
272 }
273 return false;
274}
275
276} // end of anonymous namespace
277
278// This the "advanced" version of Phi lowering for a basic block, in
279// contrast to the simple version that lowers through assignments
280// involving temporaries.
281//
282// All Phi instructions in a basic block are conceptually executed in
283// parallel. However, if we lower Phis early and commit to a
284// sequential ordering, we may end up creating unnecessary
285// interferences which lead to worse register allocation. Delaying
286// Phi scheduling until after register allocation can help unless
287// there are no free registers for shuffling registers or stack slots
288// and spilling becomes necessary.
289//
290// The advanced Phi lowering starts by finding a topological sort of
291// the Phi instructions, where "A=B" comes before "B=C" due to the
292// anti-dependence on B. If a topological sort is not possible due to
293// a cycle, the cycle is broken by introducing a non-parallel
294// temporary. For example, a cycle arising from a permutation like
295// "A=B;B=C;C=A" can become "T=A;A=B;B=C;C=T". All else being equal,
296// prefer to schedule assignments with register-allocated Src operands
297// earlier, in case that register becomes free afterwards, and prefer
298// to schedule assignments with register-allocated Dest variables
299// later, to keep that register free for longer.
300//
301// Once the ordering is determined, the Cfg edge is split and the
302// assignment list is lowered by the target lowering layer. The
303// specific placement of the new node within the Cfg node list is
304// deferred until later, including after empty node contraction.
305void CfgNode::advancedPhiLowering() {
306 if (getPhis().empty())
307 return;
308
309 // Count the number of non-deleted Phi instructions.
JF Bastienf2e93b62015-01-22 14:30:57 -0800310 struct PhiDesc {
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700311 InstPhi *Phi;
312 Variable *Dest;
313 Operand *Src;
314 bool Processed;
315 size_t NumPred; // number of entries whose Src is this Dest
316 int32_t Weight; // preference for topological order
JF Bastienf2e93b62015-01-22 14:30:57 -0800317 };
318 llvm::SmallVector<PhiDesc, 32> Desc(getPhis().size());
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700319
320 size_t NumPhis = 0;
Jim Stichnoth29841e82014-12-23 12:26:24 -0800321 for (Inst &I : Phis) {
322 auto Inst = llvm::dyn_cast<InstPhi>(&I);
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700323 if (!Inst->isDeleted()) {
324 Desc[NumPhis].Phi = Inst;
325 Desc[NumPhis].Dest = Inst->getDest();
326 ++NumPhis;
327 }
328 }
329 if (NumPhis == 0)
330 return;
331
332 SizeT InEdgeIndex = 0;
333 for (CfgNode *Pred : InEdges) {
334 CfgNode *Split = splitIncomingEdge(Pred, InEdgeIndex++);
335 AssignList Assignments;
336 SizeT Remaining = NumPhis;
337
338 // First pass computes Src and initializes NumPred.
339 for (size_t I = 0; I < NumPhis; ++I) {
340 Variable *Dest = Desc[I].Dest;
341 Operand *Src = Desc[I].Phi->getOperandForTarget(Pred);
342 Desc[I].Src = Src;
343 Desc[I].Processed = false;
344 Desc[I].NumPred = 0;
345 // Cherry-pick any trivial assignments, so that they don't
346 // contribute to the running complexity of the topological sort.
347 if (sameVarOrReg(Dest, Src)) {
348 Desc[I].Processed = true;
349 --Remaining;
350 if (Dest != Src)
351 // If Dest and Src are syntactically the same, don't bother
352 // adding the assignment, because in all respects it would
353 // be redundant, and if Dest/Src are on the stack, the
354 // target lowering may naively decide to lower it using a
355 // temporary register.
356 Assignments.push_back(InstAssign::create(Func, Dest, Src));
357 }
358 }
359 // Second pass computes NumPred by comparing every pair of Phi
360 // instructions.
361 for (size_t I = 0; I < NumPhis; ++I) {
362 if (Desc[I].Processed)
363 continue;
364 const Variable *Dest = Desc[I].Dest;
365 for (size_t J = 0; J < NumPhis; ++J) {
366 if (Desc[J].Processed)
367 continue;
368 if (I != J) {
369 // There shouldn't be two Phis with the same Dest variable
370 // or register.
371 assert(!sameVarOrReg(Dest, Desc[J].Dest));
372 }
373 const Operand *Src = Desc[J].Src;
374 if (sameVarOrReg(Dest, Src))
375 ++Desc[I].NumPred;
376 }
377 }
378
379 // Another pass to compute initial Weight values.
380
381 // Always pick NumPred=0 over NumPred>0.
382 const int32_t WeightNoPreds = 4;
383 // Prefer Src as a register because the register might free up.
384 const int32_t WeightSrcIsReg = 2;
385 // Prefer Dest not as a register because the register stays free
386 // longer.
387 const int32_t WeightDestNotReg = 1;
388
389 for (size_t I = 0; I < NumPhis; ++I) {
390 if (Desc[I].Processed)
391 continue;
392 int32_t Weight = 0;
393 if (Desc[I].NumPred == 0)
394 Weight += WeightNoPreds;
395 if (auto Var = llvm::dyn_cast<Variable>(Desc[I].Src))
396 if (Var->hasReg())
397 Weight += WeightSrcIsReg;
398 if (!Desc[I].Dest->hasReg())
399 Weight += WeightDestNotReg;
400 Desc[I].Weight = Weight;
401 }
402
403 // Repeatedly choose and process the best candidate in the
404 // topological sort, until no candidates remain. This
405 // implementation is O(N^2) where N is the number of Phi
406 // instructions, but with a small constant factor compared to a
407 // likely implementation of O(N) topological sort.
408 for (; Remaining; --Remaining) {
409 size_t BestIndex = 0;
410 int32_t BestWeight = -1;
411 // Find the best candidate.
412 for (size_t I = 0; I < NumPhis; ++I) {
413 if (Desc[I].Processed)
414 continue;
415 int32_t Weight = 0;
416 Weight = Desc[I].Weight;
417 if (Weight > BestWeight) {
418 BestIndex = I;
419 BestWeight = Weight;
420 }
421 }
422 assert(BestWeight >= 0);
423 assert(Desc[BestIndex].NumPred <= 1);
424 Variable *Dest = Desc[BestIndex].Dest;
425 Operand *Src = Desc[BestIndex].Src;
426 assert(!sameVarOrReg(Dest, Src));
427 // Break a cycle by introducing a temporary.
428 if (Desc[BestIndex].NumPred) {
429 bool Found = false;
430 // If the target instruction "A=B" is part of a cycle, find
431 // the "X=A" assignment in the cycle because it will have to
432 // be rewritten as "X=tmp".
433 for (size_t J = 0; !Found && J < NumPhis; ++J) {
434 if (Desc[J].Processed)
435 continue;
436 Operand *OtherSrc = Desc[J].Src;
437 if (Desc[J].NumPred && sameVarOrReg(Dest, OtherSrc)) {
438 SizeT VarNum = Func->getNumVariables();
Jim Stichnoth9a04c072014-12-11 15:51:42 -0800439 Variable *Tmp = Func->makeVariable(OtherSrc->getType());
440 if (ALLOW_DUMP)
441 Tmp->setName(Func, "__split_" + std::to_string(VarNum));
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700442 Assignments.push_back(InstAssign::create(Func, Tmp, OtherSrc));
443 Desc[J].Src = Tmp;
444 Found = true;
445 }
446 }
447 assert(Found);
448 }
449 // Now that a cycle (if any) has been broken, create the actual
450 // assignment.
451 Assignments.push_back(InstAssign::create(Func, Dest, Src));
452 // Update NumPred for all Phi assignments using this Phi's Src
453 // as their Dest variable. Also update Weight if NumPred
454 // dropped from 1 to 0.
455 if (auto Var = llvm::dyn_cast<Variable>(Src)) {
456 for (size_t I = 0; I < NumPhis; ++I) {
457 if (Desc[I].Processed)
458 continue;
459 if (sameVarOrReg(Var, Desc[I].Dest)) {
460 if (--Desc[I].NumPred == 0)
461 Desc[I].Weight += WeightNoPreds;
462 }
463 }
464 }
465 Desc[BestIndex].Processed = true;
466 }
467
468 Func->getTarget()->lowerPhiAssignments(Split, Assignments);
469
470 // Renumber the instructions to be monotonically increasing so
471 // that addNode() doesn't assert when multi-definitions are added
472 // out of order.
473 Split->renumberInstructions();
474 Func->getVMetadata()->addNode(Split);
475 }
476
Jim Stichnoth29841e82014-12-23 12:26:24 -0800477 for (Inst &I : Phis)
478 I.setDeleted();
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700479}
480
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700481// Does address mode optimization. Pass each instruction to the
482// TargetLowering object. If it returns a new instruction
483// (representing the optimized address mode), then insert the new
484// instruction and delete the old.
485void CfgNode::doAddressOpt() {
486 TargetLowering *Target = Func->getTarget();
487 LoweringContext &Context = Target->getContext();
488 Context.init(this);
489 while (!Context.atEnd()) {
490 Target->doAddressOpt();
491 }
492}
493
Matt Walac3302742014-08-15 16:21:56 -0700494void CfgNode::doNopInsertion() {
495 TargetLowering *Target = Func->getTarget();
496 LoweringContext &Context = Target->getContext();
497 Context.init(this);
498 while (!Context.atEnd()) {
499 Target->doNopInsertion();
500 // Ensure Cur=Next, so that the nops are inserted before the current
501 // instruction rather than after.
502 Context.advanceNext();
503 Context.advanceCur();
504 }
505 // Insert before all instructions.
506 Context.setInsertPoint(getInsts().begin());
507 Context.advanceNext();
508 Context.advanceCur();
509 Target->doNopInsertion();
510}
511
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700512// Drives the target lowering. Passes the current instruction and the
513// next non-deleted instruction for target lowering.
514void CfgNode::genCode() {
515 TargetLowering *Target = Func->getTarget();
516 LoweringContext &Context = Target->getContext();
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700517 // Lower the regular instructions.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700518 Context.init(this);
Jim Stichnotha59ae6f2015-05-17 10:11:41 -0700519 Target->initNodeForLowering(this);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700520 while (!Context.atEnd()) {
521 InstList::iterator Orig = Context.getCur();
522 if (llvm::isa<InstRet>(*Orig))
523 setHasReturn();
524 Target->lower();
525 // Ensure target lowering actually moved the cursor.
526 assert(Context.getCur() != Orig);
527 }
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700528 // Do preliminary lowering of the Phi instructions.
529 Target->prelowerPhis();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700530}
531
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700532void CfgNode::livenessLightweight() {
533 SizeT NumVars = Func->getNumVariables();
Jim Stichnoth47752552014-10-13 17:15:08 -0700534 LivenessBV Live(NumVars);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700535 // Process regular instructions in reverse order.
Jim Stichnoth7e571362015-01-09 11:43:26 -0800536 for (Inst &I : reverse_range(Insts)) {
537 if (I.isDeleted())
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700538 continue;
Jim Stichnoth7e571362015-01-09 11:43:26 -0800539 I.livenessLightweight(Func, Live);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700540 }
Jim Stichnoth29841e82014-12-23 12:26:24 -0800541 for (Inst &I : Phis) {
542 if (I.isDeleted())
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700543 continue;
Jim Stichnoth29841e82014-12-23 12:26:24 -0800544 I.livenessLightweight(Func, Live);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700545 }
546}
547
548// Performs liveness analysis on the block. Returns true if the
549// incoming liveness changed from before, false if it stayed the same.
550// (If it changes, the node's predecessors need to be processed
551// again.)
552bool CfgNode::liveness(Liveness *Liveness) {
553 SizeT NumVars = Liveness->getNumVarsInNode(this);
Jim Stichnoth47752552014-10-13 17:15:08 -0700554 LivenessBV Live(NumVars);
Jim Stichnothae953202014-12-20 06:17:49 -0800555 LiveBeginEndMap *LiveBegin = nullptr;
556 LiveBeginEndMap *LiveEnd = nullptr;
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700557 // Mark the beginning and ending of each variable's live range
558 // with the sentinel instruction number 0.
Jim Stichnoth47752552014-10-13 17:15:08 -0700559 if (Liveness->getMode() == Liveness_Intervals) {
560 LiveBegin = Liveness->getLiveBegin(this);
561 LiveEnd = Liveness->getLiveEnd(this);
562 LiveBegin->clear();
563 LiveEnd->clear();
564 // Guess that the number of live ranges beginning is roughly the
565 // number of instructions, and same for live ranges ending.
566 LiveBegin->reserve(getInstCountEstimate());
567 LiveEnd->reserve(getInstCountEstimate());
568 }
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700569 // Initialize Live to be the union of all successors' LiveIn.
Jim Stichnothf44f3712014-10-01 14:05:51 -0700570 for (CfgNode *Succ : OutEdges) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700571 Live |= Liveness->getLiveIn(Succ);
572 // Mark corresponding argument of phis in successor as live.
Jim Stichnoth29841e82014-12-23 12:26:24 -0800573 for (Inst &I : Succ->Phis) {
574 auto Phi = llvm::dyn_cast<InstPhi>(&I);
Jim Stichnoth1502e592014-12-11 09:22:45 -0800575 Phi->livenessPhiOperand(Live, this, Liveness);
576 }
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700577 }
578 Liveness->getLiveOut(this) = Live;
579
580 // Process regular instructions in reverse order.
Jim Stichnoth7e571362015-01-09 11:43:26 -0800581 for (Inst &I : reverse_range(Insts)) {
582 if (I.isDeleted())
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700583 continue;
Jim Stichnoth7e571362015-01-09 11:43:26 -0800584 I.liveness(I.getNumber(), Live, Liveness, LiveBegin, LiveEnd);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700585 }
586 // Process phis in forward order so that we can override the
587 // instruction number to be that of the earliest phi instruction in
588 // the block.
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700589 SizeT NumNonDeadPhis = 0;
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700590 InstNumberT FirstPhiNumber = Inst::NumberSentinel;
Jim Stichnoth29841e82014-12-23 12:26:24 -0800591 for (Inst &I : Phis) {
592 if (I.isDeleted())
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700593 continue;
594 if (FirstPhiNumber == Inst::NumberSentinel)
Jim Stichnoth29841e82014-12-23 12:26:24 -0800595 FirstPhiNumber = I.getNumber();
596 if (I.liveness(FirstPhiNumber, Live, Liveness, LiveBegin, LiveEnd))
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700597 ++NumNonDeadPhis;
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700598 }
599
600 // When using the sparse representation, after traversing the
601 // instructions in the block, the Live bitvector should only contain
602 // set bits for global variables upon block entry. We validate this
603 // by shrinking the Live vector and then testing it against the
604 // pre-shrunk version. (The shrinking is required, but the
605 // validation is not.)
Jim Stichnoth47752552014-10-13 17:15:08 -0700606 LivenessBV LiveOrig = Live;
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700607 Live.resize(Liveness->getNumGlobalVars());
608 // Non-global arguments in the entry node are allowed to be live on
609 // entry.
610 bool IsEntry = (Func->getEntryNode() == this);
611 if (!(IsEntry || Live == LiveOrig)) {
Jim Stichnoth69d3f9c2015-03-23 10:33:38 -0700612 if (ALLOW_DUMP) {
613 // This is a fatal liveness consistency error. Print some
614 // diagnostics and abort.
615 Ostream &Str = Func->getContext()->getStrDump();
616 Func->resetCurrentNode();
617 Str << "LiveOrig-Live =";
618 for (SizeT i = Live.size(); i < LiveOrig.size(); ++i) {
619 if (LiveOrig.test(i)) {
620 Str << " ";
621 Liveness->getVariable(i, this)->dump(Func);
622 }
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700623 }
Jim Stichnoth69d3f9c2015-03-23 10:33:38 -0700624 Str << "\n";
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700625 }
Jim Stichnoth69d3f9c2015-03-23 10:33:38 -0700626 llvm::report_fatal_error("Fatal inconsistency in liveness analysis");
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700627 }
628
629 bool Changed = false;
Jim Stichnoth47752552014-10-13 17:15:08 -0700630 LivenessBV &LiveIn = Liveness->getLiveIn(this);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700631 // Add in current LiveIn
632 Live |= LiveIn;
633 // Check result, set LiveIn=Live
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700634 SizeT &PrevNumNonDeadPhis = Liveness->getNumNonDeadPhis(this);
635 bool LiveInChanged = (Live != LiveIn);
636 Changed = (NumNonDeadPhis != PrevNumNonDeadPhis || LiveInChanged);
637 if (LiveInChanged)
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700638 LiveIn = Live;
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700639 PrevNumNonDeadPhis = NumNonDeadPhis;
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700640 return Changed;
641}
642
Jim Stichnothe5b73e62014-12-15 09:58:51 -0800643// Once basic liveness is complete, compute actual live ranges. It is
644// assumed that within a single basic block, a live range begins at
645// most once and ends at most once. This is certainly true for pure
646// SSA form. It is also true once phis are lowered, since each
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700647// assignment to the phi-based temporary is in a different basic
648// block, and there is a single read that ends the live in the basic
649// block that contained the actual phi instruction.
Jim Stichnothe5b73e62014-12-15 09:58:51 -0800650void CfgNode::livenessAddIntervals(Liveness *Liveness, InstNumberT FirstInstNum,
651 InstNumberT LastInstNum) {
652 TimerMarker T1(TimerStack::TT_liveRange, Func);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700653
654 SizeT NumVars = Liveness->getNumVarsInNode(this);
Jim Stichnoth47752552014-10-13 17:15:08 -0700655 LivenessBV &LiveIn = Liveness->getLiveIn(this);
656 LivenessBV &LiveOut = Liveness->getLiveOut(this);
657 LiveBeginEndMap &MapBegin = *Liveness->getLiveBegin(this);
658 LiveBeginEndMap &MapEnd = *Liveness->getLiveEnd(this);
659 std::sort(MapBegin.begin(), MapBegin.end());
660 std::sort(MapEnd.begin(), MapEnd.end());
661 // Verify there are no duplicates.
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700662 struct ComparePair {
663 bool operator()(const LiveBeginEndMapEntry &A,
664 const LiveBeginEndMapEntry &B) {
665 return A.first == B.first;
666 }
667 };
668 assert(std::adjacent_find(MapBegin.begin(), MapBegin.end(), ComparePair()) ==
Jim Stichnoth47752552014-10-13 17:15:08 -0700669 MapBegin.end());
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700670 assert(std::adjacent_find(MapEnd.begin(), MapEnd.end(), ComparePair()) ==
Jim Stichnoth47752552014-10-13 17:15:08 -0700671 MapEnd.end());
672
673 LivenessBV LiveInAndOut = LiveIn;
674 LiveInAndOut &= LiveOut;
675
676 // Iterate in parallel across the sorted MapBegin[] and MapEnd[].
677 auto IBB = MapBegin.begin(), IEB = MapEnd.begin();
678 auto IBE = MapBegin.end(), IEE = MapEnd.end();
679 while (IBB != IBE || IEB != IEE) {
680 SizeT i1 = IBB == IBE ? NumVars : IBB->first;
681 SizeT i2 = IEB == IEE ? NumVars : IEB->first;
682 SizeT i = std::min(i1, i2);
683 // i1 is the Variable number of the next MapBegin entry, and i2 is
684 // the Variable number of the next MapEnd entry. If i1==i2, then
685 // the Variable's live range begins and ends in this block. If
686 // i1<i2, then i1's live range begins at instruction IBB->second
687 // and extends through the end of the block. If i1>i2, then i2's
688 // live range begins at the first instruction of the block and
689 // ends at IEB->second. In any case, we choose the lesser of i1
690 // and i2 and proceed accordingly.
691 InstNumberT LB = i == i1 ? IBB->second : FirstInstNum;
692 InstNumberT LE = i == i2 ? IEB->second : LastInstNum + 1;
693
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700694 Variable *Var = Liveness->getVariable(i, this);
Jim Stichnoth47752552014-10-13 17:15:08 -0700695 if (!Var->getIgnoreLiveness()) {
696 if (LB > LE) {
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700697 Var->addLiveRange(FirstInstNum, LE, 1);
698 Var->addLiveRange(LB, LastInstNum + 1, 1);
Jim Stichnoth47752552014-10-13 17:15:08 -0700699 // Assert that Var is a global variable by checking that its
700 // liveness index is less than the number of globals. This
701 // ensures that the LiveInAndOut[] access is valid.
702 assert(i < Liveness->getNumGlobalVars());
703 LiveInAndOut[i] = false;
704 } else {
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700705 Var->addLiveRange(LB, LE, 1);
Jim Stichnoth47752552014-10-13 17:15:08 -0700706 }
707 }
708 if (i == i1)
709 ++IBB;
710 if (i == i2)
711 ++IEB;
712 }
713 // Process the variables that are live across the entire block.
714 for (int i = LiveInAndOut.find_first(); i != -1;
715 i = LiveInAndOut.find_next(i)) {
716 Variable *Var = Liveness->getVariable(i, this);
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700717 Var->addLiveRange(FirstInstNum, LastInstNum + 1, 1);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700718 }
719}
720
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700721// If this node contains only deleted instructions, and ends in an
722// unconditional branch, contract the node by repointing all its
723// in-edges to its successor.
724void CfgNode::contractIfEmpty() {
Jim Stichnothbfb410d2014-11-05 16:04:05 -0800725 if (InEdges.empty())
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700726 return;
Jim Stichnothae953202014-12-20 06:17:49 -0800727 Inst *Branch = nullptr;
Jim Stichnoth29841e82014-12-23 12:26:24 -0800728 for (Inst &I : Insts) {
729 if (I.isDeleted())
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700730 continue;
Jim Stichnoth29841e82014-12-23 12:26:24 -0800731 if (I.isUnconditionalBranch())
732 Branch = &I;
733 else if (!I.isRedundantAssign())
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700734 return;
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700735 }
736 Branch->setDeleted();
737 assert(OutEdges.size() == 1);
Jim Stichnothbfb410d2014-11-05 16:04:05 -0800738 // Repoint all this node's in-edges to this node's successor, unless
739 // this node's successor is actually itself (in which case the
740 // statement "OutEdges.front()->InEdges.push_back(Pred)" could
741 // invalidate the iterator over this->InEdges).
742 if (OutEdges.front() != this) {
743 for (CfgNode *Pred : InEdges) {
744 for (auto I = Pred->OutEdges.begin(), E = Pred->OutEdges.end(); I != E;
745 ++I) {
746 if (*I == this) {
747 *I = OutEdges.front();
748 OutEdges.front()->InEdges.push_back(Pred);
749 }
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700750 }
Jim Stichnoth29841e82014-12-23 12:26:24 -0800751 for (Inst &I : Pred->getInsts()) {
752 if (!I.isDeleted())
753 I.repointEdge(this, OutEdges.front());
Jim Stichnothbfb410d2014-11-05 16:04:05 -0800754 }
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700755 }
756 }
757 InEdges.clear();
758 // Don't bother removing the single out-edge, which would also
759 // require finding the corresponding in-edge in the successor and
760 // removing it.
761}
762
Jim Stichnothff9c7062014-09-18 04:50:49 -0700763void CfgNode::doBranchOpt(const CfgNode *NextNode) {
764 TargetLowering *Target = Func->getTarget();
765 // Check every instruction for a branch optimization opportunity.
766 // It may be more efficient to iterate in reverse and stop after the
767 // first opportunity, unless there is some target lowering where we
768 // have the possibility of multiple such optimizations per block
769 // (currently not the case for x86 lowering).
Jim Stichnoth29841e82014-12-23 12:26:24 -0800770 for (Inst &I : Insts) {
771 if (!I.isDeleted()) {
772 Target->doBranchOpt(&I, NextNode);
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700773 }
774 }
Jim Stichnothff9c7062014-09-18 04:50:49 -0700775}
776
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700777// ======================== Dump routines ======================== //
778
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700779namespace {
780
781// Helper functions for emit().
782
783void emitRegisterUsage(Ostream &Str, const Cfg *Func, const CfgNode *Node,
784 bool IsLiveIn, std::vector<SizeT> &LiveRegCount) {
Karl Schimpfb6c96af2014-11-17 10:58:39 -0800785 if (!ALLOW_DUMP)
786 return;
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700787 Liveness *Liveness = Func->getLiveness();
788 const LivenessBV *Live;
789 if (IsLiveIn) {
790 Live = &Liveness->getLiveIn(Node);
791 Str << "\t\t\t\t# LiveIn=";
792 } else {
793 Live = &Liveness->getLiveOut(Node);
794 Str << "\t\t\t\t# LiveOut=";
795 }
796 if (!Live->empty()) {
Jim Stichnoth9a05aea2015-05-26 16:01:23 -0700797 std::vector<Variable *> LiveRegs;
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700798 for (SizeT i = 0; i < Live->size(); ++i) {
799 if ((*Live)[i]) {
800 Variable *Var = Liveness->getVariable(i, Node);
801 if (Var->hasReg()) {
802 if (IsLiveIn)
803 ++LiveRegCount[Var->getRegNum()];
Jim Stichnoth9a05aea2015-05-26 16:01:23 -0700804 LiveRegs.push_back(Var);
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700805 }
806 }
807 }
Jim Stichnoth9a05aea2015-05-26 16:01:23 -0700808 // Sort the variables by regnum so they are always printed in a
809 // familiar order.
810 std::sort(LiveRegs.begin(), LiveRegs.end(),
811 [](const Variable *V1, const Variable *V2) {
Jim Stichnoth8e6bf6e2015-06-03 15:58:12 -0700812 return V1->getRegNum() < V2->getRegNum();
813 });
Jim Stichnoth9a05aea2015-05-26 16:01:23 -0700814 bool First = true;
815 for (Variable *Var : LiveRegs) {
816 if (!First)
817 Str << ",";
818 First = false;
819 Var->emit(Func);
820 }
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700821 }
822 Str << "\n";
823}
824
825void emitLiveRangesEnded(Ostream &Str, const Cfg *Func, const Inst *Instr,
826 std::vector<SizeT> &LiveRegCount) {
Karl Schimpfb6c96af2014-11-17 10:58:39 -0800827 if (!ALLOW_DUMP)
828 return;
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700829 bool First = true;
830 Variable *Dest = Instr->getDest();
Jim Stichnothb82baf22015-05-27 10:41:07 -0700831 // Normally we increment the live count for the dest register. But
832 // we shouldn't if the instruction's IsDestNonKillable flag is set,
833 // because this means that the target lowering created this
834 // instruction as a non-SSA assignment; i.e., a different, previous
835 // instruction started the dest variable's live range.
836 if (!Instr->isDestNonKillable() && Dest && Dest->hasReg())
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700837 ++LiveRegCount[Dest->getRegNum()];
838 for (SizeT I = 0; I < Instr->getSrcSize(); ++I) {
839 Operand *Src = Instr->getSrc(I);
840 SizeT NumVars = Src->getNumVars();
841 for (SizeT J = 0; J < NumVars; ++J) {
842 const Variable *Var = Src->getVar(J);
Jim Stichnothb82baf22015-05-27 10:41:07 -0700843 bool ShouldReport = Instr->isLastUse(Var);
844 if (ShouldReport && Var->hasReg()) {
Jim Stichnoth9a05aea2015-05-26 16:01:23 -0700845 // Don't report end of live range until the live count reaches 0.
846 SizeT NewCount = --LiveRegCount[Var->getRegNum()];
847 if (NewCount)
Jim Stichnothb82baf22015-05-27 10:41:07 -0700848 ShouldReport = false;
Jim Stichnoth9a05aea2015-05-26 16:01:23 -0700849 }
Jim Stichnothb82baf22015-05-27 10:41:07 -0700850 if (ShouldReport) {
Jim Stichnoth4175b2a2015-04-30 12:26:22 -0700851 if (First)
852 Str << " \t# END=";
853 else
854 Str << ",";
855 Var->emit(Func);
856 First = false;
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700857 }
858 }
859 }
860}
861
Jan Voung0faec4c2014-11-05 17:29:56 -0800862void updateStats(Cfg *Func, const Inst *I) {
Jim Stichnoth9f42d8c2015-02-20 09:20:14 -0800863 if (!ALLOW_DUMP)
864 return;
Jan Voung0faec4c2014-11-05 17:29:56 -0800865 // Update emitted instruction count, plus fill/spill count for
866 // Variable operands without a physical register.
867 if (uint32_t Count = I->getEmitInstCount()) {
868 Func->getContext()->statsUpdateEmitted(Count);
869 if (Variable *Dest = I->getDest()) {
870 if (!Dest->hasReg())
871 Func->getContext()->statsUpdateFills();
872 }
873 for (SizeT S = 0; S < I->getSrcSize(); ++S) {
874 if (Variable *Src = llvm::dyn_cast<Variable>(I->getSrc(S))) {
875 if (!Src->hasReg())
876 Func->getContext()->statsUpdateSpills();
877 }
878 }
879 }
880}
881
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700882} // end of anonymous namespace
883
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700884void CfgNode::emit(Cfg *Func) const {
Karl Schimpfb6c96af2014-11-17 10:58:39 -0800885 if (!ALLOW_DUMP)
886 return;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700887 Func->setCurrentNode(this);
888 Ostream &Str = Func->getContext()->getStrEmit();
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700889 Liveness *Liveness = Func->getLiveness();
Karl Schimpfdf80eb82015-02-09 14:20:22 -0800890 bool DecorateAsm =
891 Liveness && Func->getContext()->getFlags().getDecorateAsm();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700892 Str << getAsmName() << ":\n";
Jim Stichnothb82baf22015-05-27 10:41:07 -0700893 // LiveRegCount keeps track of the number of currently live
894 // variables that each register is assigned to. Normally that would
895 // be only 0 or 1, but the register allocator's AllowOverlap
896 // inference allows it to be greater than 1 for short periods.
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700897 std::vector<SizeT> LiveRegCount(Func->getTarget()->getNumRegisters());
Jim Stichnoth4175b2a2015-04-30 12:26:22 -0700898 if (DecorateAsm) {
899 const bool IsLiveIn = true;
900 emitRegisterUsage(Str, Func, this, IsLiveIn, LiveRegCount);
901 }
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700902
Jim Stichnoth29841e82014-12-23 12:26:24 -0800903 for (const Inst &I : Phis) {
904 if (I.isDeleted())
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700905 continue;
906 // Emitting a Phi instruction should cause an error.
Jim Stichnoth29841e82014-12-23 12:26:24 -0800907 I.emit(Func);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700908 }
Jim Stichnoth29841e82014-12-23 12:26:24 -0800909 for (const Inst &I : Insts) {
910 if (I.isDeleted())
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700911 continue;
Jim Stichnothb82baf22015-05-27 10:41:07 -0700912 if (I.isRedundantAssign()) {
913 // Usually, redundant assignments end the live range of the src
914 // variable and begin the live range of the dest variable, with
915 // no net effect on the liveness of their register. However, if
916 // the register allocator infers the AllowOverlap condition,
917 // then this may be a redundant assignment that does not end the
918 // src variable's live range, in which case the active variable
919 // count for that register needs to be bumped. That normally
920 // would have happened as part of emitLiveRangesEnded(), but
921 // that isn't called for redundant assignments.
922 Variable *Dest = I.getDest();
923 if (DecorateAsm && Dest->hasReg() && !I.isLastUse(I.getSrc(0)))
924 ++LiveRegCount[Dest->getRegNum()];
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700925 continue;
Jim Stichnothb82baf22015-05-27 10:41:07 -0700926 }
Jim Stichnoth29841e82014-12-23 12:26:24 -0800927 I.emit(Func);
Jan Voung0faec4c2014-11-05 17:29:56 -0800928 if (DecorateAsm)
Jim Stichnoth29841e82014-12-23 12:26:24 -0800929 emitLiveRangesEnded(Str, Func, &I, LiveRegCount);
Jan Voung0faec4c2014-11-05 17:29:56 -0800930 Str << "\n";
Jim Stichnoth29841e82014-12-23 12:26:24 -0800931 updateStats(Func, &I);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700932 }
Jim Stichnoth4175b2a2015-04-30 12:26:22 -0700933 if (DecorateAsm) {
934 const bool IsLiveIn = false;
935 emitRegisterUsage(Str, Func, this, IsLiveIn, LiveRegCount);
936 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700937}
938
Jim Stichnoth9f42d8c2015-02-20 09:20:14 -0800939// Helper class for emitIAS().
940namespace {
941class BundleEmitHelper {
942 BundleEmitHelper() = delete;
943 BundleEmitHelper(const BundleEmitHelper &) = delete;
944 BundleEmitHelper &operator=(const BundleEmitHelper &) = delete;
945
946public:
Jim Stichnoth9738a9e2015-02-23 16:39:06 -0800947 BundleEmitHelper(Assembler *Asm, TargetLowering *Target,
948 const InstList &Insts)
949 : Asm(Asm), Target(Target), End(Insts.end()), BundleLockStart(End),
Jim Stichnoth9f42d8c2015-02-20 09:20:14 -0800950 BundleSize(1 << Asm->getBundleAlignLog2Bytes()),
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700951 BundleMaskLo(BundleSize - 1), BundleMaskHi(~BundleMaskLo) {}
Jim Stichnoth9f42d8c2015-02-20 09:20:14 -0800952 // Check whether we're currently within a bundle_lock region.
953 bool isInBundleLockRegion() const { return BundleLockStart != End; }
954 // Check whether the current bundle_lock region has the align_to_end
955 // option.
956 bool isAlignToEnd() const {
957 assert(isInBundleLockRegion());
958 return llvm::cast<InstBundleLock>(getBundleLockStart())->getOption() ==
959 InstBundleLock::Opt_AlignToEnd;
960 }
961 // Check whether the entire bundle_lock region falls within the same
962 // bundle.
963 bool isSameBundle() const {
964 assert(isInBundleLockRegion());
965 return SizeSnapshotPre == SizeSnapshotPost ||
966 (SizeSnapshotPre & BundleMaskHi) ==
967 ((SizeSnapshotPost - 1) & BundleMaskHi);
968 }
969 // Get the bundle alignment of the first instruction of the
970 // bundle_lock region.
971 intptr_t getPreAlignment() const {
972 assert(isInBundleLockRegion());
973 return SizeSnapshotPre & BundleMaskLo;
974 }
975 // Get the bundle alignment of the first instruction past the
976 // bundle_lock region.
977 intptr_t getPostAlignment() const {
978 assert(isInBundleLockRegion());
979 return SizeSnapshotPost & BundleMaskLo;
980 }
981 // Get the iterator pointing to the bundle_lock instruction, e.g. to
982 // roll back the instruction iteration to that point.
983 InstList::const_iterator getBundleLockStart() const {
984 assert(isInBundleLockRegion());
985 return BundleLockStart;
986 }
987 // Set up bookkeeping when the bundle_lock instruction is first
988 // processed.
989 void enterBundleLock(InstList::const_iterator I) {
990 assert(!isInBundleLockRegion());
991 BundleLockStart = I;
992 SizeSnapshotPre = Asm->getBufferSize();
993 Asm->setPreliminary(true);
Jim Stichnoth9738a9e2015-02-23 16:39:06 -0800994 Target->snapshotEmitState();
Jim Stichnoth9f42d8c2015-02-20 09:20:14 -0800995 assert(isInBundleLockRegion());
996 }
997 // Update bookkeeping when the bundle_unlock instruction is
998 // processed.
999 void enterBundleUnlock() {
1000 assert(isInBundleLockRegion());
1001 SizeSnapshotPost = Asm->getBufferSize();
1002 }
1003 // Update bookkeeping when we are completely finished with the
1004 // bundle_lock region.
1005 void leaveBundleLockRegion() { BundleLockStart = End; }
1006 // Check whether the instruction sequence fits within the current
1007 // bundle, and if not, add nop padding to the end of the current
1008 // bundle.
1009 void padToNextBundle() {
1010 assert(isInBundleLockRegion());
1011 if (!isSameBundle()) {
1012 intptr_t PadToNextBundle = BundleSize - getPreAlignment();
1013 Asm->padWithNop(PadToNextBundle);
1014 SizeSnapshotPre += PadToNextBundle;
1015 SizeSnapshotPost += PadToNextBundle;
1016 assert((Asm->getBufferSize() & BundleMaskLo) == 0);
1017 assert(Asm->getBufferSize() == SizeSnapshotPre);
1018 }
1019 }
1020 // If align_to_end is specified, add padding such that the
1021 // instruction sequences ends precisely at a bundle boundary.
1022 void padForAlignToEnd() {
1023 assert(isInBundleLockRegion());
1024 if (isAlignToEnd()) {
1025 if (intptr_t Offset = getPostAlignment()) {
1026 Asm->padWithNop(BundleSize - Offset);
1027 SizeSnapshotPre = Asm->getBufferSize();
1028 }
1029 }
1030 }
1031 // Update bookkeeping when rolling back for the second pass.
1032 void rollback() {
1033 assert(isInBundleLockRegion());
1034 Asm->setBufferSize(SizeSnapshotPre);
1035 Asm->setPreliminary(false);
Jim Stichnoth9738a9e2015-02-23 16:39:06 -08001036 Target->rollbackEmitState();
Jim Stichnoth9f42d8c2015-02-20 09:20:14 -08001037 }
1038
1039private:
1040 Assembler *const Asm;
Jim Stichnoth9738a9e2015-02-23 16:39:06 -08001041 TargetLowering *const Target;
Jim Stichnoth9f42d8c2015-02-20 09:20:14 -08001042 // End is a sentinel value such that BundleLockStart==End implies
1043 // that we are not in a bundle_lock region.
1044 const InstList::const_iterator End;
1045 InstList::const_iterator BundleLockStart;
1046 const intptr_t BundleSize;
1047 // Masking with BundleMaskLo identifies an address's bundle offset.
1048 const intptr_t BundleMaskLo;
1049 // Masking with BundleMaskHi identifies an address's bundle.
1050 const intptr_t BundleMaskHi;
Jim Stichnotheafb56c2015-06-22 10:35:22 -07001051 intptr_t SizeSnapshotPre = 0;
1052 intptr_t SizeSnapshotPost = 0;
Jim Stichnoth9f42d8c2015-02-20 09:20:14 -08001053};
1054
1055} // end of anonymous namespace
1056
Jan Voung0faec4c2014-11-05 17:29:56 -08001057void CfgNode::emitIAS(Cfg *Func) const {
1058 Func->setCurrentNode(this);
Jim Stichnothbbca7542015-02-11 16:08:31 -08001059 Assembler *Asm = Func->getAssembler<>();
Jim Stichnoth9f42d8c2015-02-20 09:20:14 -08001060 // TODO(stichnot): When sandboxing, defer binding the node label
1061 // until just before the first instruction is emitted, to reduce the
1062 // chance that a padding nop is a branch target.
John Portoaff4ccf2015-06-10 16:35:06 -07001063 Asm->bindCfgNodeLabel(getIndex());
Jim Stichnoth29841e82014-12-23 12:26:24 -08001064 for (const Inst &I : Phis) {
1065 if (I.isDeleted())
Jan Voung0faec4c2014-11-05 17:29:56 -08001066 continue;
1067 // Emitting a Phi instruction should cause an error.
Jim Stichnoth29841e82014-12-23 12:26:24 -08001068 I.emitIAS(Func);
Jan Voung0faec4c2014-11-05 17:29:56 -08001069 }
Jim Stichnoth9f42d8c2015-02-20 09:20:14 -08001070
1071 // Do the simple emission if not sandboxed.
1072 if (!Func->getContext()->getFlags().getUseSandboxing()) {
1073 for (const Inst &I : Insts) {
1074 if (!I.isDeleted() && !I.isRedundantAssign()) {
1075 I.emitIAS(Func);
1076 updateStats(Func, &I);
1077 }
1078 }
1079 return;
Jan Voung0faec4c2014-11-05 17:29:56 -08001080 }
Jim Stichnoth9f42d8c2015-02-20 09:20:14 -08001081
1082 // The remainder of the function handles emission with sandboxing.
1083 // There are explicit bundle_lock regions delimited by bundle_lock
1084 // and bundle_unlock instructions. All other instructions are
1085 // treated as an implicit one-instruction bundle_lock region.
1086 // Emission is done twice for each bundle_lock region. The first
1087 // pass is a preliminary pass, after which we can figure out what
1088 // nop padding is needed, then roll back, and make the final pass.
1089 //
1090 // Ideally, the first pass would be speculative and the second pass
1091 // would only be done if nop padding were needed, but the structure
1092 // of the integrated assembler makes it hard to roll back the state
1093 // of label bindings, label links, and relocation fixups. Instead,
1094 // the first pass just disables all mutation of that state.
1095
Jim Stichnoth9738a9e2015-02-23 16:39:06 -08001096 BundleEmitHelper Helper(Asm, Func->getTarget(), Insts);
Jim Stichnoth9f42d8c2015-02-20 09:20:14 -08001097 InstList::const_iterator End = Insts.end();
1098 // Retrying indicates that we had to roll back to the bundle_lock
1099 // instruction to apply padding before the bundle_lock sequence.
1100 bool Retrying = false;
1101 for (InstList::const_iterator I = Insts.begin(); I != End; ++I) {
1102 if (I->isDeleted() || I->isRedundantAssign())
1103 continue;
1104
1105 if (llvm::isa<InstBundleLock>(I)) {
1106 // Set up the initial bundle_lock state. This should not happen
1107 // while retrying, because the retry rolls back to the
1108 // instruction following the bundle_lock instruction.
1109 assert(!Retrying);
1110 Helper.enterBundleLock(I);
1111 continue;
1112 }
1113
1114 if (llvm::isa<InstBundleUnlock>(I)) {
1115 Helper.enterBundleUnlock();
1116 if (Retrying) {
1117 // Make sure all instructions are in the same bundle.
1118 assert(Helper.isSameBundle());
1119 // If align_to_end is specified, make sure the next
1120 // instruction begins the bundle.
1121 assert(!Helper.isAlignToEnd() || Helper.getPostAlignment() == 0);
1122 Helper.leaveBundleLockRegion();
1123 Retrying = false;
1124 } else {
1125 // This is the first pass, so roll back for the retry pass.
1126 Helper.rollback();
1127 // Pad to the next bundle if the instruction sequence crossed
1128 // a bundle boundary.
1129 Helper.padToNextBundle();
1130 // Insert additional padding to make AlignToEnd work.
1131 Helper.padForAlignToEnd();
1132 // Prepare for the retry pass after padding is done.
1133 Retrying = true;
1134 I = Helper.getBundleLockStart();
1135 }
1136 continue;
1137 }
1138
1139 // I points to a non bundle_lock/bundle_unlock instruction.
1140 if (Helper.isInBundleLockRegion()) {
1141 I->emitIAS(Func);
1142 // Only update stats during the final pass.
1143 if (Retrying)
1144 updateStats(Func, I);
1145 } else {
1146 // Treat it as though there were an implicit bundle_lock and
1147 // bundle_unlock wrapping the instruction.
1148 Helper.enterBundleLock(I);
1149 I->emitIAS(Func);
1150 Helper.enterBundleUnlock();
1151 Helper.rollback();
1152 Helper.padToNextBundle();
1153 I->emitIAS(Func);
1154 updateStats(Func, I);
1155 Helper.leaveBundleLockRegion();
1156 }
1157 }
1158
1159 // Don't allow bundle locking across basic blocks, to keep the
1160 // backtracking mechanism simple.
1161 assert(!Helper.isInBundleLockRegion());
1162 assert(!Retrying);
Jan Voung0faec4c2014-11-05 17:29:56 -08001163}
1164
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001165void CfgNode::dump(Cfg *Func) const {
Karl Schimpfb6c96af2014-11-17 10:58:39 -08001166 if (!ALLOW_DUMP)
1167 return;
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001168 Func->setCurrentNode(this);
1169 Ostream &Str = Func->getContext()->getStrDump();
Jim Stichnothd97c7df2014-06-04 11:57:08 -07001170 Liveness *Liveness = Func->getLiveness();
Jim Stichnothfa4efea2015-01-27 05:06:03 -08001171 if (Func->isVerbose(IceV_Instructions)) {
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001172 Str << getName() << ":\n";
1173 }
1174 // Dump list of predecessor nodes.
Jim Stichnothfa4efea2015-01-27 05:06:03 -08001175 if (Func->isVerbose(IceV_Preds) && !InEdges.empty()) {
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001176 Str << " // preds = ";
Jim Stichnothf44f3712014-10-01 14:05:51 -07001177 bool First = true;
1178 for (CfgNode *I : InEdges) {
Jim Stichnoth8363a062014-10-07 10:02:38 -07001179 if (!First)
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001180 Str << ", ";
Jim Stichnothf44f3712014-10-01 14:05:51 -07001181 First = false;
1182 Str << "%" << I->getName();
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001183 }
1184 Str << "\n";
1185 }
Jim Stichnothd97c7df2014-06-04 11:57:08 -07001186 // Dump the live-in variables.
Jim Stichnoth47752552014-10-13 17:15:08 -07001187 LivenessBV LiveIn;
Jim Stichnothd97c7df2014-06-04 11:57:08 -07001188 if (Liveness)
1189 LiveIn = Liveness->getLiveIn(this);
Jim Stichnothfa4efea2015-01-27 05:06:03 -08001190 if (Func->isVerbose(IceV_Liveness) && !LiveIn.empty()) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -07001191 Str << " // LiveIn:";
1192 for (SizeT i = 0; i < LiveIn.size(); ++i) {
1193 if (LiveIn[i]) {
Jim Stichnoth088b2be2014-10-23 12:02:08 -07001194 Variable *Var = Liveness->getVariable(i, this);
Jim Stichnoth9a04c072014-12-11 15:51:42 -08001195 Str << " %" << Var->getName(Func);
Jim Stichnothfa4efea2015-01-27 05:06:03 -08001196 if (Func->isVerbose(IceV_RegOrigins) && Var->hasReg()) {
Jim Stichnothdd842db2015-01-27 12:53:53 -08001197 Str << ":"
1198 << Func->getTarget()->getRegName(Var->getRegNum(),
1199 Var->getType());
Jim Stichnoth088b2be2014-10-23 12:02:08 -07001200 }
Jim Stichnothd97c7df2014-06-04 11:57:08 -07001201 }
1202 }
1203 Str << "\n";
1204 }
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001205 // Dump each instruction.
Jim Stichnothfa4efea2015-01-27 05:06:03 -08001206 if (Func->isVerbose(IceV_Instructions)) {
Jim Stichnoth29841e82014-12-23 12:26:24 -08001207 for (const Inst &I : Phis)
1208 I.dumpDecorated(Func);
1209 for (const Inst &I : Insts)
1210 I.dumpDecorated(Func);
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001211 }
Jim Stichnothd97c7df2014-06-04 11:57:08 -07001212 // Dump the live-out variables.
Jim Stichnoth47752552014-10-13 17:15:08 -07001213 LivenessBV LiveOut;
Jim Stichnothd97c7df2014-06-04 11:57:08 -07001214 if (Liveness)
1215 LiveOut = Liveness->getLiveOut(this);
Jim Stichnothfa4efea2015-01-27 05:06:03 -08001216 if (Func->isVerbose(IceV_Liveness) && !LiveOut.empty()) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -07001217 Str << " // LiveOut:";
1218 for (SizeT i = 0; i < LiveOut.size(); ++i) {
1219 if (LiveOut[i]) {
Jim Stichnoth088b2be2014-10-23 12:02:08 -07001220 Variable *Var = Liveness->getVariable(i, this);
Jim Stichnoth9a04c072014-12-11 15:51:42 -08001221 Str << " %" << Var->getName(Func);
Jim Stichnothfa4efea2015-01-27 05:06:03 -08001222 if (Func->isVerbose(IceV_RegOrigins) && Var->hasReg()) {
Jim Stichnothdd842db2015-01-27 12:53:53 -08001223 Str << ":"
1224 << Func->getTarget()->getRegName(Var->getRegNum(),
1225 Var->getType());
Jim Stichnoth088b2be2014-10-23 12:02:08 -07001226 }
Jim Stichnothd97c7df2014-06-04 11:57:08 -07001227 }
1228 }
1229 Str << "\n";
1230 }
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001231 // Dump list of successor nodes.
Jim Stichnothfa4efea2015-01-27 05:06:03 -08001232 if (Func->isVerbose(IceV_Succs)) {
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001233 Str << " // succs = ";
Jim Stichnothf44f3712014-10-01 14:05:51 -07001234 bool First = true;
1235 for (CfgNode *I : OutEdges) {
Jim Stichnoth8363a062014-10-07 10:02:38 -07001236 if (!First)
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001237 Str << ", ";
Jim Stichnothf44f3712014-10-01 14:05:51 -07001238 First = false;
1239 Str << "%" << I->getName();
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001240 }
1241 Str << "\n";
1242 }
1243}
1244
John Portof8b4cc82015-06-09 18:06:19 -07001245void CfgNode::profileExecutionCount(VariableDeclaration *Var) {
1246 constexpr char RMW_I64[] = "llvm.nacl.atomic.rmw.i64";
1247
1248 GlobalContext *Context = Func->getContext();
1249
1250 bool BadIntrinsic = false;
1251 const Intrinsics::FullIntrinsicInfo *Info =
1252 Context->getIntrinsicsInfo().find(RMW_I64, BadIntrinsic);
1253 assert(!BadIntrinsic);
1254 assert(Info != nullptr);
1255
1256 Operand *RMWI64Name = Context->getConstantExternSym(RMW_I64);
John Porto8b1a7052015-06-17 13:20:08 -07001257 constexpr RelocOffsetT Offset = 0;
1258 constexpr bool SuppressMangling = true;
1259 Constant *Counter =
1260 Context->getConstantSym(Offset, Var->getName(), SuppressMangling);
John Portof8b4cc82015-06-09 18:06:19 -07001261 Constant *AtomicRMWOp = Context->getConstantInt32(Intrinsics::AtomicAdd);
1262 Constant *One = Context->getConstantInt64(1);
1263 Constant *OrderAcquireRelease =
1264 Context->getConstantInt32(Intrinsics::MemoryOrderAcquireRelease);
1265
1266 InstIntrinsicCall *Inst = InstIntrinsicCall::create(
1267 Func, 5, Func->makeVariable(IceType_i64), RMWI64Name, Info->Info);
1268 Inst->addArg(AtomicRMWOp);
1269 Inst->addArg(Counter);
1270 Inst->addArg(One);
1271 Inst->addArg(OrderAcquireRelease);
1272 Insts.push_front(Inst);
1273}
1274
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001275} // end of namespace Ice