blob: 4b5c212a7f8c8e442262dc711441db70e61291dd [file] [log] [blame]
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001//===--- RDFGraph.cpp -----------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Target-independent, SSA-based data flow graph for register data flow (RDF).
11//
12#include "RDFGraph.h"
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +000013#include "llvm/ADT/SetVector.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000014#include "llvm/ADT/STLExtras.h"
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +000015#include "llvm/CodeGen/MachineBasicBlock.h"
16#include "llvm/CodeGen/MachineDominanceFrontier.h"
17#include "llvm/CodeGen/MachineDominators.h"
18#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000019#include "llvm/CodeGen/MachineInstr.h"
20#include "llvm/CodeGen/MachineOperand.h"
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000022#include "llvm/IR/Function.h"
23#include "llvm/MC/LaneBitmask.h"
24#include "llvm/MC/MCInstrDesc.h"
25#include "llvm/MC/MCRegisterInfo.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/raw_ostream.h"
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +000028#include "llvm/Target/TargetInstrInfo.h"
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +000029#include "llvm/Target/TargetLowering.h"
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +000030#include "llvm/Target/TargetRegisterInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000031#include <algorithm>
32#include <cassert>
33#include <cstdint>
34#include <cstring>
35#include <iterator>
36#include <utility>
37#include <vector>
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +000038
39using namespace llvm;
40using namespace rdf;
41
42// Printing functions. Have them here first, so that the rest of the code
43// can use them.
Benjamin Kramer922efd72016-05-27 10:06:40 +000044namespace llvm {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +000045namespace rdf {
46
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +000047raw_ostream &operator<< (raw_ostream &OS, const PrintLaneMaskOpt &P) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +000048 if (!P.Mask.all())
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +000049 OS << ':' << PrintLaneMask(P.Mask);
50 return OS;
51}
52
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +000053template<>
54raw_ostream &operator<< (raw_ostream &OS, const Print<RegisterRef> &P) {
55 auto &TRI = P.G.getTRI();
56 if (P.Obj.Reg > 0 && P.Obj.Reg < TRI.getNumRegs())
57 OS << TRI.getName(P.Obj.Reg);
58 else
59 OS << '#' << P.Obj.Reg;
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +000060 OS << PrintLaneMaskOpt(P.Obj.Mask);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +000061 return OS;
62}
63
64template<>
65raw_ostream &operator<< (raw_ostream &OS, const Print<NodeId> &P) {
66 auto NA = P.G.addr<NodeBase*>(P.Obj);
67 uint16_t Attrs = NA.Addr->getAttrs();
68 uint16_t Kind = NodeAttrs::kind(Attrs);
69 uint16_t Flags = NodeAttrs::flags(Attrs);
70 switch (NodeAttrs::type(Attrs)) {
71 case NodeAttrs::Code:
72 switch (Kind) {
73 case NodeAttrs::Func: OS << 'f'; break;
74 case NodeAttrs::Block: OS << 'b'; break;
75 case NodeAttrs::Stmt: OS << 's'; break;
76 case NodeAttrs::Phi: OS << 'p'; break;
77 default: OS << "c?"; break;
78 }
79 break;
80 case NodeAttrs::Ref:
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +000081 if (Flags & NodeAttrs::Undef)
82 OS << '/';
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +000083 if (Flags & NodeAttrs::Dead)
84 OS << '\\';
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +000085 if (Flags & NodeAttrs::Preserving)
86 OS << '+';
87 if (Flags & NodeAttrs::Clobbering)
88 OS << '~';
89 switch (Kind) {
90 case NodeAttrs::Use: OS << 'u'; break;
91 case NodeAttrs::Def: OS << 'd'; break;
92 case NodeAttrs::Block: OS << 'b'; break;
93 default: OS << "r?"; break;
94 }
95 break;
96 default:
97 OS << '?';
98 break;
99 }
100 OS << P.Obj;
101 if (Flags & NodeAttrs::Shadow)
102 OS << '"';
103 return OS;
104}
105
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000106static void printRefHeader(raw_ostream &OS, const NodeAddr<RefNode*> RA,
107 const DataFlowGraph &G) {
108 OS << Print<NodeId>(RA.Id, G) << '<'
109 << Print<RegisterRef>(RA.Addr->getRegRef(G), G) << '>';
110 if (RA.Addr->getFlags() & NodeAttrs::Fixed)
111 OS << '!';
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000112}
113
114template<>
115raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<DefNode*>> &P) {
116 printRefHeader(OS, P.Obj, P.G);
117 OS << '(';
118 if (NodeId N = P.Obj.Addr->getReachingDef())
119 OS << Print<NodeId>(N, P.G);
120 OS << ',';
121 if (NodeId N = P.Obj.Addr->getReachedDef())
122 OS << Print<NodeId>(N, P.G);
123 OS << ',';
124 if (NodeId N = P.Obj.Addr->getReachedUse())
125 OS << Print<NodeId>(N, P.G);
126 OS << "):";
127 if (NodeId N = P.Obj.Addr->getSibling())
128 OS << Print<NodeId>(N, P.G);
129 return OS;
130}
131
132template<>
133raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<UseNode*>> &P) {
134 printRefHeader(OS, P.Obj, P.G);
135 OS << '(';
136 if (NodeId N = P.Obj.Addr->getReachingDef())
137 OS << Print<NodeId>(N, P.G);
138 OS << "):";
139 if (NodeId N = P.Obj.Addr->getSibling())
140 OS << Print<NodeId>(N, P.G);
141 return OS;
142}
143
144template<>
145raw_ostream &operator<< (raw_ostream &OS,
146 const Print<NodeAddr<PhiUseNode*>> &P) {
147 printRefHeader(OS, P.Obj, P.G);
148 OS << '(';
149 if (NodeId N = P.Obj.Addr->getReachingDef())
150 OS << Print<NodeId>(N, P.G);
151 OS << ',';
152 if (NodeId N = P.Obj.Addr->getPredecessor())
153 OS << Print<NodeId>(N, P.G);
154 OS << "):";
155 if (NodeId N = P.Obj.Addr->getSibling())
156 OS << Print<NodeId>(N, P.G);
157 return OS;
158}
159
160template<>
161raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<RefNode*>> &P) {
162 switch (P.Obj.Addr->getKind()) {
163 case NodeAttrs::Def:
164 OS << PrintNode<DefNode*>(P.Obj, P.G);
165 break;
166 case NodeAttrs::Use:
167 if (P.Obj.Addr->getFlags() & NodeAttrs::PhiRef)
168 OS << PrintNode<PhiUseNode*>(P.Obj, P.G);
169 else
170 OS << PrintNode<UseNode*>(P.Obj, P.G);
171 break;
172 }
173 return OS;
174}
175
176template<>
177raw_ostream &operator<< (raw_ostream &OS, const Print<NodeList> &P) {
178 unsigned N = P.Obj.size();
179 for (auto I : P.Obj) {
180 OS << Print<NodeId>(I.Id, P.G);
181 if (--N)
182 OS << ' ';
183 }
184 return OS;
185}
186
187template<>
188raw_ostream &operator<< (raw_ostream &OS, const Print<NodeSet> &P) {
189 unsigned N = P.Obj.size();
190 for (auto I : P.Obj) {
191 OS << Print<NodeId>(I, P.G);
192 if (--N)
193 OS << ' ';
194 }
195 return OS;
196}
197
198namespace {
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000199
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000200 template <typename T>
201 struct PrintListV {
202 PrintListV(const NodeList &L, const DataFlowGraph &G) : List(L), G(G) {}
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000203
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000204 typedef T Type;
205 const NodeList &List;
206 const DataFlowGraph &G;
207 };
208
209 template <typename T>
210 raw_ostream &operator<< (raw_ostream &OS, const PrintListV<T> &P) {
211 unsigned N = P.List.size();
212 for (NodeAddr<T> A : P.List) {
213 OS << PrintNode<T>(A, P.G);
214 if (--N)
215 OS << ", ";
216 }
217 return OS;
218 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000219
220} // end anonymous namespace
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000221
222template<>
223raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<PhiNode*>> &P) {
224 OS << Print<NodeId>(P.Obj.Id, P.G) << ": phi ["
225 << PrintListV<RefNode*>(P.Obj.Addr->members(P.G), P.G) << ']';
226 return OS;
227}
228
229template<>
230raw_ostream &operator<< (raw_ostream &OS,
231 const Print<NodeAddr<StmtNode*>> &P) {
Krzysztof Parzyszek670e0ca2016-09-22 20:58:19 +0000232 const MachineInstr &MI = *P.Obj.Addr->getCode();
233 unsigned Opc = MI.getOpcode();
234 OS << Print<NodeId>(P.Obj.Id, P.G) << ": " << P.G.getTII().getName(Opc);
Krzysztof Parzyszekab26e2d2016-10-03 17:54:33 +0000235 // Print the target for calls and branches (for readability).
236 if (MI.isCall() || MI.isBranch()) {
237 MachineInstr::const_mop_iterator T =
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000238 llvm::find_if(MI.operands(),
239 [] (const MachineOperand &Op) -> bool {
240 return Op.isMBB() || Op.isGlobal() || Op.isSymbol();
241 });
Krzysztof Parzyszekab26e2d2016-10-03 17:54:33 +0000242 if (T != MI.operands_end()) {
243 OS << ' ';
244 if (T->isMBB())
245 OS << "BB#" << T->getMBB()->getNumber();
246 else if (T->isGlobal())
247 OS << T->getGlobal()->getName();
248 else if (T->isSymbol())
249 OS << T->getSymbolName();
Krzysztof Parzyszek670e0ca2016-09-22 20:58:19 +0000250 }
251 }
252 OS << " [" << PrintListV<RefNode*>(P.Obj.Addr->members(P.G), P.G) << ']';
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000253 return OS;
254}
255
256template<>
257raw_ostream &operator<< (raw_ostream &OS,
258 const Print<NodeAddr<InstrNode*>> &P) {
259 switch (P.Obj.Addr->getKind()) {
260 case NodeAttrs::Phi:
261 OS << PrintNode<PhiNode*>(P.Obj, P.G);
262 break;
263 case NodeAttrs::Stmt:
264 OS << PrintNode<StmtNode*>(P.Obj, P.G);
265 break;
266 default:
267 OS << "instr? " << Print<NodeId>(P.Obj.Id, P.G);
268 break;
269 }
270 return OS;
271}
272
273template<>
274raw_ostream &operator<< (raw_ostream &OS,
275 const Print<NodeAddr<BlockNode*>> &P) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000276 MachineBasicBlock *BB = P.Obj.Addr->getCode();
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000277 unsigned NP = BB->pred_size();
278 std::vector<int> Ns;
Malcolm Parsons17d266b2017-01-13 17:12:16 +0000279 auto PrintBBs = [&OS] (std::vector<int> Ns) -> void {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000280 unsigned N = Ns.size();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000281 for (int I : Ns) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000282 OS << "BB#" << I;
283 if (--N)
284 OS << ", ";
285 }
286 };
287
Krzysztof Parzyszekab26e2d2016-10-03 17:54:33 +0000288 OS << Print<NodeId>(P.Obj.Id, P.G) << ": --- BB#" << BB->getNumber()
289 << " --- preds(" << NP << "): ";
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000290 for (MachineBasicBlock *B : BB->predecessors())
291 Ns.push_back(B->getNumber());
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000292 PrintBBs(Ns);
293
294 unsigned NS = BB->succ_size();
295 OS << " succs(" << NS << "): ";
296 Ns.clear();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000297 for (MachineBasicBlock *B : BB->successors())
298 Ns.push_back(B->getNumber());
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000299 PrintBBs(Ns);
300 OS << '\n';
301
302 for (auto I : P.Obj.Addr->members(P.G))
303 OS << PrintNode<InstrNode*>(I, P.G) << '\n';
304 return OS;
305}
306
307template<>
308raw_ostream &operator<< (raw_ostream &OS,
309 const Print<NodeAddr<FuncNode*>> &P) {
310 OS << "DFG dump:[\n" << Print<NodeId>(P.Obj.Id, P.G) << ": Function: "
311 << P.Obj.Addr->getCode()->getName() << '\n';
312 for (auto I : P.Obj.Addr->members(P.G))
313 OS << PrintNode<BlockNode*>(I, P.G) << '\n';
314 OS << "]\n";
315 return OS;
316}
317
318template<>
319raw_ostream &operator<< (raw_ostream &OS, const Print<RegisterSet> &P) {
320 OS << '{';
321 for (auto I : P.Obj)
322 OS << ' ' << Print<RegisterRef>(I, P.G);
323 OS << " }";
324 return OS;
325}
326
327template<>
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000328raw_ostream &operator<< (raw_ostream &OS, const Print<RegisterAggr> &P) {
329 P.Obj.print(OS);
330 return OS;
331}
332
333template<>
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000334raw_ostream &operator<< (raw_ostream &OS,
335 const Print<DataFlowGraph::DefStack> &P) {
336 for (auto I = P.Obj.top(), E = P.Obj.bottom(); I != E; ) {
337 OS << Print<NodeId>(I->Id, P.G)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000338 << '<' << Print<RegisterRef>(I->Addr->getRegRef(P.G), P.G) << '>';
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000339 I.down();
340 if (I != E)
341 OS << ' ';
342 }
343 return OS;
344}
345
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000346} // end namespace rdf
347} // end namespace llvm
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000348
349// Node allocation functions.
350//
351// Node allocator is like a slab memory allocator: it allocates blocks of
352// memory in sizes that are multiples of the size of a node. Each block has
353// the same size. Nodes are allocated from the currently active block, and
354// when it becomes full, a new one is created.
355// There is a mapping scheme between node id and its location in a block,
356// and within that block is described in the header file.
357//
358void NodeAllocator::startNewBlock() {
359 void *T = MemPool.Allocate(NodesPerBlock*NodeMemSize, NodeMemSize);
360 char *P = static_cast<char*>(T);
361 Blocks.push_back(P);
362 // Check if the block index is still within the allowed range, i.e. less
363 // than 2^N, where N is the number of bits in NodeId for the block index.
364 // BitsPerIndex is the number of bits per node index.
Simon Pilgrim99c6c292016-01-18 21:11:19 +0000365 assert((Blocks.size() < ((size_t)1 << (8*sizeof(NodeId)-BitsPerIndex))) &&
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000366 "Out of bits for block index");
367 ActiveEnd = P;
368}
369
370bool NodeAllocator::needNewBlock() {
371 if (Blocks.empty())
372 return true;
373
374 char *ActiveBegin = Blocks.back();
375 uint32_t Index = (ActiveEnd-ActiveBegin)/NodeMemSize;
376 return Index >= NodesPerBlock;
377}
378
379NodeAddr<NodeBase*> NodeAllocator::New() {
380 if (needNewBlock())
381 startNewBlock();
382
383 uint32_t ActiveB = Blocks.size()-1;
384 uint32_t Index = (ActiveEnd - Blocks[ActiveB])/NodeMemSize;
385 NodeAddr<NodeBase*> NA = { reinterpret_cast<NodeBase*>(ActiveEnd),
386 makeId(ActiveB, Index) };
387 ActiveEnd += NodeMemSize;
388 return NA;
389}
390
391NodeId NodeAllocator::id(const NodeBase *P) const {
392 uintptr_t A = reinterpret_cast<uintptr_t>(P);
393 for (unsigned i = 0, n = Blocks.size(); i != n; ++i) {
394 uintptr_t B = reinterpret_cast<uintptr_t>(Blocks[i]);
395 if (A < B || A >= B + NodesPerBlock*NodeMemSize)
396 continue;
397 uint32_t Idx = (A-B)/NodeMemSize;
398 return makeId(i, Idx);
399 }
400 llvm_unreachable("Invalid node address");
401}
402
403void NodeAllocator::clear() {
404 MemPool.Reset();
405 Blocks.clear();
406 ActiveEnd = nullptr;
407}
408
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000409// Insert node NA after "this" in the circular chain.
410void NodeBase::append(NodeAddr<NodeBase*> NA) {
411 NodeId Nx = Next;
412 // If NA is already "next", do nothing.
413 if (Next != NA.Id) {
414 Next = NA.Id;
415 NA.Addr->Next = Nx;
416 }
417}
418
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000419// Fundamental node manipulator functions.
420
421// Obtain the register reference from a reference node.
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000422RegisterRef RefNode::getRegRef(const DataFlowGraph &G) const {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000423 assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
424 if (NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000425 return G.unpack(Ref.PR);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000426 assert(Ref.Op != nullptr);
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +0000427 return G.makeRegRef(*Ref.Op);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000428}
429
430// Set the register reference in the reference node directly (for references
431// in phi nodes).
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000432void RefNode::setRegRef(RegisterRef RR, DataFlowGraph &G) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000433 assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
434 assert(NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000435 Ref.PR = G.pack(RR);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000436}
437
438// Set the register reference in the reference node based on a machine
439// operand (for references in statement nodes).
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000440void RefNode::setRegRef(MachineOperand *Op, DataFlowGraph &G) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000441 assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
442 assert(!(NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef));
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000443 (void)G;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000444 Ref.Op = Op;
445}
446
447// Get the owner of a given reference node.
448NodeAddr<NodeBase*> RefNode::getOwner(const DataFlowGraph &G) {
449 NodeAddr<NodeBase*> NA = G.addr<NodeBase*>(getNext());
450
451 while (NA.Addr != this) {
452 if (NA.Addr->getType() == NodeAttrs::Code)
453 return NA;
454 NA = G.addr<NodeBase*>(NA.Addr->getNext());
455 }
456 llvm_unreachable("No owner in circular list");
457}
458
459// Connect the def node to the reaching def node.
460void DefNode::linkToDef(NodeId Self, NodeAddr<DefNode*> DA) {
461 Ref.RD = DA.Id;
462 Ref.Sib = DA.Addr->getReachedDef();
463 DA.Addr->setReachedDef(Self);
464}
465
466// Connect the use node to the reaching def node.
467void UseNode::linkToDef(NodeId Self, NodeAddr<DefNode*> DA) {
468 Ref.RD = DA.Id;
469 Ref.Sib = DA.Addr->getReachedUse();
470 DA.Addr->setReachedUse(Self);
471}
472
473// Get the first member of the code node.
474NodeAddr<NodeBase*> CodeNode::getFirstMember(const DataFlowGraph &G) const {
475 if (Code.FirstM == 0)
476 return NodeAddr<NodeBase*>();
477 return G.addr<NodeBase*>(Code.FirstM);
478}
479
480// Get the last member of the code node.
481NodeAddr<NodeBase*> CodeNode::getLastMember(const DataFlowGraph &G) const {
482 if (Code.LastM == 0)
483 return NodeAddr<NodeBase*>();
484 return G.addr<NodeBase*>(Code.LastM);
485}
486
487// Add node NA at the end of the member list of the given code node.
488void CodeNode::addMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000489 NodeAddr<NodeBase*> ML = getLastMember(G);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000490 if (ML.Id != 0) {
491 ML.Addr->append(NA);
492 } else {
493 Code.FirstM = NA.Id;
494 NodeId Self = G.id(this);
495 NA.Addr->setNext(Self);
496 }
497 Code.LastM = NA.Id;
498}
499
500// Add node NA after member node MA in the given code node.
501void CodeNode::addMemberAfter(NodeAddr<NodeBase*> MA, NodeAddr<NodeBase*> NA,
502 const DataFlowGraph &G) {
503 MA.Addr->append(NA);
504 if (Code.LastM == MA.Id)
505 Code.LastM = NA.Id;
506}
507
508// Remove member node NA from the given code node.
509void CodeNode::removeMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000510 NodeAddr<NodeBase*> MA = getFirstMember(G);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000511 assert(MA.Id != 0);
512
513 // Special handling if the member to remove is the first member.
514 if (MA.Id == NA.Id) {
515 if (Code.LastM == MA.Id) {
516 // If it is the only member, set both first and last to 0.
517 Code.FirstM = Code.LastM = 0;
518 } else {
519 // Otherwise, advance the first member.
520 Code.FirstM = MA.Addr->getNext();
521 }
522 return;
523 }
524
525 while (MA.Addr != this) {
526 NodeId MX = MA.Addr->getNext();
527 if (MX == NA.Id) {
528 MA.Addr->setNext(NA.Addr->getNext());
529 // If the member to remove happens to be the last one, update the
530 // LastM indicator.
531 if (Code.LastM == NA.Id)
532 Code.LastM = MA.Id;
533 return;
534 }
535 MA = G.addr<NodeBase*>(MX);
536 }
537 llvm_unreachable("No such member");
538}
539
540// Return the list of all members of the code node.
541NodeList CodeNode::members(const DataFlowGraph &G) const {
542 static auto True = [] (NodeAddr<NodeBase*>) -> bool { return true; };
543 return members_if(True, G);
544}
545
546// Return the owner of the given instr node.
547NodeAddr<NodeBase*> InstrNode::getOwner(const DataFlowGraph &G) {
548 NodeAddr<NodeBase*> NA = G.addr<NodeBase*>(getNext());
549
550 while (NA.Addr != this) {
551 assert(NA.Addr->getType() == NodeAttrs::Code);
552 if (NA.Addr->getKind() == NodeAttrs::Block)
553 return NA;
554 NA = G.addr<NodeBase*>(NA.Addr->getNext());
555 }
556 llvm_unreachable("No owner in circular list");
557}
558
559// Add the phi node PA to the given block node.
560void BlockNode::addPhi(NodeAddr<PhiNode*> PA, const DataFlowGraph &G) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000561 NodeAddr<NodeBase*> M = getFirstMember(G);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000562 if (M.Id == 0) {
563 addMember(PA, G);
564 return;
565 }
566
567 assert(M.Addr->getType() == NodeAttrs::Code);
568 if (M.Addr->getKind() == NodeAttrs::Stmt) {
569 // If the first member of the block is a statement, insert the phi as
570 // the first member.
571 Code.FirstM = PA.Id;
572 PA.Addr->setNext(M.Id);
573 } else {
574 // If the first member is a phi, find the last phi, and append PA to it.
575 assert(M.Addr->getKind() == NodeAttrs::Phi);
576 NodeAddr<NodeBase*> MN = M;
577 do {
578 M = MN;
579 MN = G.addr<NodeBase*>(M.Addr->getNext());
580 assert(MN.Addr->getType() == NodeAttrs::Code);
581 } while (MN.Addr->getKind() == NodeAttrs::Phi);
582
583 // M is the last phi.
584 addMemberAfter(M, PA, G);
585 }
586}
587
588// Find the block node corresponding to the machine basic block BB in the
589// given func node.
590NodeAddr<BlockNode*> FuncNode::findBlock(const MachineBasicBlock *BB,
591 const DataFlowGraph &G) const {
592 auto EqBB = [BB] (NodeAddr<NodeBase*> NA) -> bool {
593 return NodeAddr<BlockNode*>(NA).Addr->getCode() == BB;
594 };
595 NodeList Ms = members_if(EqBB, G);
596 if (!Ms.empty())
597 return Ms[0];
598 return NodeAddr<BlockNode*>();
599}
600
601// Get the block node for the entry block in the given function.
602NodeAddr<BlockNode*> FuncNode::getEntryBlock(const DataFlowGraph &G) {
603 MachineBasicBlock *EntryB = &getCode()->front();
604 return findBlock(EntryB, G);
605}
606
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000607// Target operand information.
608//
609
610// For a given instruction, check if there are any bits of RR that can remain
611// unchanged across this def.
612bool TargetOperandInfo::isPreserving(const MachineInstr &In, unsigned OpNum)
613 const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000614 return TII.isPredicated(In);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000615}
616
617// Check if the definition of RR produces an unspecified value.
618bool TargetOperandInfo::isClobbering(const MachineInstr &In, unsigned OpNum)
619 const {
620 if (In.isCall())
621 if (In.getOperand(OpNum).isImplicit())
622 return true;
623 return false;
624}
625
Krzysztof Parzyszekc5a4e262016-04-28 20:33:33 +0000626// Check if the given instruction specifically requires
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000627bool TargetOperandInfo::isFixedReg(const MachineInstr &In, unsigned OpNum)
628 const {
Krzysztof Parzyszekc5a4e262016-04-28 20:33:33 +0000629 if (In.isCall() || In.isReturn() || In.isInlineAsm())
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000630 return true;
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +0000631 // Check for a tail call.
632 if (In.isBranch())
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000633 for (const MachineOperand &O : In.operands())
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +0000634 if (O.isGlobal() || O.isSymbol())
635 return true;
636
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000637 const MCInstrDesc &D = In.getDesc();
638 if (!D.getImplicitDefs() && !D.getImplicitUses())
639 return false;
640 const MachineOperand &Op = In.getOperand(OpNum);
641 // If there is a sub-register, treat the operand as non-fixed. Currently,
642 // fixed registers are those that are listed in the descriptor as implicit
643 // uses or defs, and those lists do not allow sub-registers.
644 if (Op.getSubReg() != 0)
645 return false;
Krzysztof Parzyszek6e7fa992016-10-21 19:12:13 +0000646 RegisterId Reg = Op.getReg();
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000647 const MCPhysReg *ImpR = Op.isDef() ? D.getImplicitDefs()
648 : D.getImplicitUses();
649 if (!ImpR)
650 return false;
651 while (*ImpR)
652 if (*ImpR++ == Reg)
653 return true;
654 return false;
655}
656
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000657//
658// The data flow graph construction.
659//
660
661DataFlowGraph::DataFlowGraph(MachineFunction &mf, const TargetInstrInfo &tii,
662 const TargetRegisterInfo &tri, const MachineDominatorTree &mdt,
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000663 const MachineDominanceFrontier &mdf, const TargetOperandInfo &toi)
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +0000664 : MF(mf), TII(tii), TRI(tri), PRI(tri, mf), MDT(mdt), MDF(mdf), TOI(toi),
665 LiveIns(PRI) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000666}
667
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000668// The implementation of the definition stack.
669// Each register reference has its own definition stack. In particular,
670// for a register references "Reg" and "Reg:subreg" will each have their
671// own definition stacks.
672
673// Construct a stack iterator.
674DataFlowGraph::DefStack::Iterator::Iterator(const DataFlowGraph::DefStack &S,
675 bool Top) : DS(S) {
676 if (!Top) {
677 // Initialize to bottom.
678 Pos = 0;
679 return;
680 }
681 // Initialize to the top, i.e. top-most non-delimiter (or 0, if empty).
682 Pos = DS.Stack.size();
683 while (Pos > 0 && DS.isDelimiter(DS.Stack[Pos-1]))
684 Pos--;
685}
686
687// Return the size of the stack, including block delimiters.
688unsigned DataFlowGraph::DefStack::size() const {
689 unsigned S = 0;
690 for (auto I = top(), E = bottom(); I != E; I.down())
691 S++;
692 return S;
693}
694
695// Remove the top entry from the stack. Remove all intervening delimiters
696// so that after this, the stack is either empty, or the top of the stack
697// is a non-delimiter.
698void DataFlowGraph::DefStack::pop() {
699 assert(!empty());
700 unsigned P = nextDown(Stack.size());
701 Stack.resize(P);
702}
703
704// Push a delimiter for block node N on the stack.
705void DataFlowGraph::DefStack::start_block(NodeId N) {
706 assert(N != 0);
707 Stack.push_back(NodeAddr<DefNode*>(nullptr, N));
708}
709
710// Remove all nodes from the top of the stack, until the delimited for
711// block node N is encountered. Remove the delimiter as well. In effect,
712// this will remove from the stack all definitions from block N.
713void DataFlowGraph::DefStack::clear_block(NodeId N) {
714 assert(N != 0);
715 unsigned P = Stack.size();
716 while (P > 0) {
717 bool Found = isDelimiter(Stack[P-1], N);
718 P--;
719 if (Found)
720 break;
721 }
722 // This will also remove the delimiter, if found.
723 Stack.resize(P);
724}
725
726// Move the stack iterator up by one.
727unsigned DataFlowGraph::DefStack::nextUp(unsigned P) const {
728 // Get the next valid position after P (skipping all delimiters).
729 // The input position P does not have to point to a non-delimiter.
730 unsigned SS = Stack.size();
731 bool IsDelim;
Krzysztof Parzyszek8dca45e2016-01-12 16:51:55 +0000732 assert(P < SS);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000733 do {
734 P++;
735 IsDelim = isDelimiter(Stack[P-1]);
736 } while (P < SS && IsDelim);
737 assert(!IsDelim);
738 return P;
739}
740
741// Move the stack iterator down by one.
742unsigned DataFlowGraph::DefStack::nextDown(unsigned P) const {
743 // Get the preceding valid position before P (skipping all delimiters).
744 // The input position P does not have to point to a non-delimiter.
745 assert(P > 0 && P <= Stack.size());
746 bool IsDelim = isDelimiter(Stack[P-1]);
747 do {
748 if (--P == 0)
749 break;
750 IsDelim = isDelimiter(Stack[P-1]);
751 } while (P > 0 && IsDelim);
752 assert(!IsDelim);
753 return P;
754}
755
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000756// Register information.
757
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000758RegisterSet DataFlowGraph::getLandingPadLiveIns() const {
759 RegisterSet LR;
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +0000760 const Function &F = *MF.getFunction();
761 const Constant *PF = F.hasPersonalityFn() ? F.getPersonalityFn()
762 : nullptr;
763 const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering();
Krzysztof Parzyszek6e7fa992016-10-21 19:12:13 +0000764 if (RegisterId R = TLI.getExceptionPointerRegister(PF))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000765 LR.insert(RegisterRef(R));
Krzysztof Parzyszek6e7fa992016-10-21 19:12:13 +0000766 if (RegisterId R = TLI.getExceptionSelectorRegister(PF))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000767 LR.insert(RegisterRef(R));
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +0000768 return LR;
769}
770
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000771// Node management functions.
772
773// Get the pointer to the node with the id N.
774NodeBase *DataFlowGraph::ptr(NodeId N) const {
775 if (N == 0)
776 return nullptr;
777 return Memory.ptr(N);
778}
779
780// Get the id of the node at the address P.
781NodeId DataFlowGraph::id(const NodeBase *P) const {
782 if (P == nullptr)
783 return 0;
784 return Memory.id(P);
785}
786
787// Allocate a new node and set the attributes to Attrs.
788NodeAddr<NodeBase*> DataFlowGraph::newNode(uint16_t Attrs) {
789 NodeAddr<NodeBase*> P = Memory.New();
790 P.Addr->init();
791 P.Addr->setAttrs(Attrs);
792 return P;
793}
794
795// Make a copy of the given node B, except for the data-flow links, which
796// are set to 0.
797NodeAddr<NodeBase*> DataFlowGraph::cloneNode(const NodeAddr<NodeBase*> B) {
798 NodeAddr<NodeBase*> NA = newNode(0);
799 memcpy(NA.Addr, B.Addr, sizeof(NodeBase));
800 // Ref nodes need to have the data-flow links reset.
801 if (NA.Addr->getType() == NodeAttrs::Ref) {
802 NodeAddr<RefNode*> RA = NA;
803 RA.Addr->setReachingDef(0);
804 RA.Addr->setSibling(0);
805 if (NA.Addr->getKind() == NodeAttrs::Def) {
806 NodeAddr<DefNode*> DA = NA;
807 DA.Addr->setReachedDef(0);
808 DA.Addr->setReachedUse(0);
809 }
810 }
811 return NA;
812}
813
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000814// Allocation routines for specific node types/kinds.
815
816NodeAddr<UseNode*> DataFlowGraph::newUse(NodeAddr<InstrNode*> Owner,
817 MachineOperand &Op, uint16_t Flags) {
818 NodeAddr<UseNode*> UA = newNode(NodeAttrs::Ref | NodeAttrs::Use | Flags);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000819 UA.Addr->setRegRef(&Op, *this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000820 return UA;
821}
822
823NodeAddr<PhiUseNode*> DataFlowGraph::newPhiUse(NodeAddr<PhiNode*> Owner,
824 RegisterRef RR, NodeAddr<BlockNode*> PredB, uint16_t Flags) {
825 NodeAddr<PhiUseNode*> PUA = newNode(NodeAttrs::Ref | NodeAttrs::Use | Flags);
826 assert(Flags & NodeAttrs::PhiRef);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000827 PUA.Addr->setRegRef(RR, *this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000828 PUA.Addr->setPredecessor(PredB.Id);
829 return PUA;
830}
831
832NodeAddr<DefNode*> DataFlowGraph::newDef(NodeAddr<InstrNode*> Owner,
833 MachineOperand &Op, uint16_t Flags) {
834 NodeAddr<DefNode*> DA = newNode(NodeAttrs::Ref | NodeAttrs::Def | Flags);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000835 DA.Addr->setRegRef(&Op, *this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000836 return DA;
837}
838
839NodeAddr<DefNode*> DataFlowGraph::newDef(NodeAddr<InstrNode*> Owner,
840 RegisterRef RR, uint16_t Flags) {
841 NodeAddr<DefNode*> DA = newNode(NodeAttrs::Ref | NodeAttrs::Def | Flags);
842 assert(Flags & NodeAttrs::PhiRef);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000843 DA.Addr->setRegRef(RR, *this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000844 return DA;
845}
846
847NodeAddr<PhiNode*> DataFlowGraph::newPhi(NodeAddr<BlockNode*> Owner) {
848 NodeAddr<PhiNode*> PA = newNode(NodeAttrs::Code | NodeAttrs::Phi);
849 Owner.Addr->addPhi(PA, *this);
850 return PA;
851}
852
853NodeAddr<StmtNode*> DataFlowGraph::newStmt(NodeAddr<BlockNode*> Owner,
854 MachineInstr *MI) {
855 NodeAddr<StmtNode*> SA = newNode(NodeAttrs::Code | NodeAttrs::Stmt);
856 SA.Addr->setCode(MI);
857 Owner.Addr->addMember(SA, *this);
858 return SA;
859}
860
861NodeAddr<BlockNode*> DataFlowGraph::newBlock(NodeAddr<FuncNode*> Owner,
862 MachineBasicBlock *BB) {
863 NodeAddr<BlockNode*> BA = newNode(NodeAttrs::Code | NodeAttrs::Block);
864 BA.Addr->setCode(BB);
865 Owner.Addr->addMember(BA, *this);
866 return BA;
867}
868
869NodeAddr<FuncNode*> DataFlowGraph::newFunc(MachineFunction *MF) {
870 NodeAddr<FuncNode*> FA = newNode(NodeAttrs::Code | NodeAttrs::Func);
871 FA.Addr->setCode(MF);
872 return FA;
873}
874
875// Build the data flow graph.
Krzysztof Parzyszek55874cf2016-04-28 20:17:06 +0000876void DataFlowGraph::build(unsigned Options) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000877 reset();
878 Func = newFunc(&MF);
879
880 if (MF.empty())
881 return;
882
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000883 for (MachineBasicBlock &B : MF) {
884 NodeAddr<BlockNode*> BA = newBlock(Func, &B);
Krzysztof Parzyszek047149f2016-07-22 16:09:47 +0000885 BlockNodes.insert(std::make_pair(&B, BA));
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000886 for (MachineInstr &I : B) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000887 if (I.isDebugValue())
888 continue;
889 buildStmt(BA, I);
890 }
891 }
892
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000893 NodeAddr<BlockNode*> EA = Func.Addr->getEntryBlock(*this);
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +0000894 NodeList Blocks = Func.Addr->members(*this);
895
896 // Collect information about block references.
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000897 BlockRefsMap RefM;
898 buildBlockRefs(EA, RefM);
899
Krzysztof Parzyszekb561cf92017-01-30 16:20:30 +0000900 // Collect function live-ins and entry block live-ins.
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000901 MachineRegisterInfo &MRI = MF.getRegInfo();
Krzysztof Parzyszekb561cf92017-01-30 16:20:30 +0000902 MachineBasicBlock &EntryB = *EA.Addr->getCode();
903 assert(EntryB.pred_empty() && "Function entry block has predecessors");
904 for (auto I = MRI.livein_begin(), E = MRI.livein_end(); I != E; ++I)
905 LiveIns.insert(RegisterRef(I->first));
906 for (auto I : EntryB.liveins())
907 LiveIns.insert(RegisterRef(I.PhysReg, I.LaneMask));
908
909 // Add function-entry phi nodes for the live-in registers.
910 for (std::pair<RegisterId,LaneBitmask> P : LiveIns) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000911 NodeAddr<PhiNode*> PA = newPhi(EA);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000912 uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving;
Krzysztof Parzyszekb561cf92017-01-30 16:20:30 +0000913 RegisterRef RR(P.first, P.second);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000914 NodeAddr<DefNode*> DA = newDef(PA, RR, PhiFlags);
915 PA.Addr->addMember(DA, *this);
916 }
917
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +0000918 // Add phis for landing pads.
919 // Landing pads, unlike usual backs blocks, are not entered through
920 // branches in the program, or fall-throughs from other blocks. They
921 // are entered from the exception handling runtime and target's ABI
922 // may define certain registers as defined on entry to such a block.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000923 RegisterSet EHRegs = getLandingPadLiveIns();
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +0000924 if (!EHRegs.empty()) {
925 for (NodeAddr<BlockNode*> BA : Blocks) {
926 const MachineBasicBlock &B = *BA.Addr->getCode();
927 if (!B.isEHPad())
928 continue;
929
930 // Prepare a list of NodeIds of the block's predecessors.
931 NodeList Preds;
932 for (MachineBasicBlock *PB : B.predecessors())
933 Preds.push_back(findBlock(PB));
934
935 // Build phi nodes for each live-in.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000936 for (RegisterRef RR : EHRegs) {
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +0000937 NodeAddr<PhiNode*> PA = newPhi(BA);
938 uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving;
939 // Add def:
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000940 NodeAddr<DefNode*> DA = newDef(PA, RR, PhiFlags);
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +0000941 PA.Addr->addMember(DA, *this);
942 // Add uses (no reaching defs for phi uses):
943 for (NodeAddr<BlockNode*> PBA : Preds) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000944 NodeAddr<PhiUseNode*> PUA = newPhiUse(PA, RR, PBA);
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +0000945 PA.Addr->addMember(PUA, *this);
946 }
947 }
948 }
949 }
950
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000951 // Build a map "PhiM" which will contain, for each block, the set
952 // of references that will require phi definitions in that block.
953 BlockRefsMap PhiM;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000954 for (NodeAddr<BlockNode*> BA : Blocks)
955 recordDefsForDF(PhiM, RefM, BA);
956 for (NodeAddr<BlockNode*> BA : Blocks)
957 buildPhis(PhiM, RefM, BA);
958
959 // Link all the refs. This will recursively traverse the dominator tree.
960 DefStackMap DM;
961 linkBlockRefs(DM, EA);
962
963 // Finally, remove all unused phi nodes.
Krzysztof Parzyszek55874cf2016-04-28 20:17:06 +0000964 if (!(Options & BuildOptions::KeepDeadPhis))
965 removeUnusedPhis();
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000966}
967
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000968RegisterRef DataFlowGraph::makeRegRef(unsigned Reg, unsigned Sub) const {
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +0000969 assert(PhysicalRegisterInfo::isRegMaskId(Reg) ||
970 TargetRegisterInfo::isPhysicalRegister(Reg));
971 assert(Reg != 0);
Krzysztof Parzyszek775a2092016-10-14 19:06:25 +0000972 if (Sub != 0)
973 Reg = TRI.getSubReg(Reg, Sub);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000974 return RegisterRef(Reg);
975}
976
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +0000977RegisterRef DataFlowGraph::makeRegRef(const MachineOperand &Op) const {
978 assert(Op.isReg() || Op.isRegMask());
979 if (Op.isReg())
980 return makeRegRef(Op.getReg(), Op.getSubReg());
981 return RegisterRef(PRI.getRegMaskId(Op.getRegMask()), LaneBitmask::getAll());
982}
983
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000984RegisterRef DataFlowGraph::normalizeRef(RegisterRef RR) const {
985 // FIXME copied from RegisterAggr
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +0000986 if (PhysicalRegisterInfo::isRegMaskId(RR.Reg))
987 return RR;
988 const TargetRegisterClass *RC = PRI.RegInfos[RR.Reg].RegClass;
989 LaneBitmask RCMask = RC != nullptr ? RC->LaneMask : LaneBitmask(0x00000001);
990 LaneBitmask Common = RR.Mask & RCMask;
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000991
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +0000992 RegisterId SuperReg = PRI.RegInfos[RR.Reg].MaxSuper;
993// Ex: IP/EIP/RIP
994// assert(RC != nullptr || RR.Reg == SuperReg);
995 uint32_t Sub = PRI.getTRI().getSubRegIndex(SuperReg, RR.Reg);
996 LaneBitmask SuperMask = PRI.getTRI().composeSubRegIndexLaneMask(Sub, Common);
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000997 return RegisterRef(SuperReg, SuperMask);
998}
999
1000RegisterRef DataFlowGraph::restrictRef(RegisterRef AR, RegisterRef BR) const {
1001 if (AR.Reg == BR.Reg) {
1002 LaneBitmask M = AR.Mask & BR.Mask;
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001003 return M.any() ? RegisterRef(AR.Reg, M) : RegisterRef();
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001004 }
1005#ifndef NDEBUG
1006 RegisterRef NAR = normalizeRef(AR);
1007 RegisterRef NBR = normalizeRef(BR);
1008 assert(NAR.Reg != NBR.Reg);
1009#endif
1010 // This isn't strictly correct, because the overlap may happen in the
1011 // part masked out.
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +00001012 if (PRI.alias(AR, BR))
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001013 return AR;
1014 return RegisterRef();
1015}
1016
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001017// For each stack in the map DefM, push the delimiter for block B on it.
1018void DataFlowGraph::markBlock(NodeId B, DefStackMap &DefM) {
1019 // Push block delimiters.
1020 for (auto I = DefM.begin(), E = DefM.end(); I != E; ++I)
1021 I->second.start_block(B);
1022}
1023
1024// Remove all definitions coming from block B from each stack in DefM.
1025void DataFlowGraph::releaseBlock(NodeId B, DefStackMap &DefM) {
1026 // Pop all defs from this block from the definition stack. Defs that were
1027 // added to the map during the traversal of instructions will not have a
1028 // delimiter, but for those, the whole stack will be emptied.
1029 for (auto I = DefM.begin(), E = DefM.end(); I != E; ++I)
1030 I->second.clear_block(B);
1031
1032 // Finally, remove empty stacks from the map.
1033 for (auto I = DefM.begin(), E = DefM.end(), NextI = I; I != E; I = NextI) {
1034 NextI = std::next(I);
1035 // This preserves the validity of iterators other than I.
1036 if (I->second.empty())
1037 DefM.erase(I);
1038 }
1039}
1040
1041// Push all definitions from the instruction node IA to an appropriate
1042// stack in DefM.
1043void DataFlowGraph::pushDefs(NodeAddr<InstrNode*> IA, DefStackMap &DefM) {
1044 NodeList Defs = IA.Addr->members_if(IsDef, *this);
1045 NodeSet Visited;
1046#ifndef NDEBUG
1047 RegisterSet Defined;
1048#endif
1049
1050 // The important objectives of this function are:
1051 // - to be able to handle instructions both while the graph is being
1052 // constructed, and after the graph has been constructed, and
1053 // - maintain proper ordering of definitions on the stack for each
1054 // register reference:
1055 // - if there are two or more related defs in IA (i.e. coming from
1056 // the same machine operand), then only push one def on the stack,
1057 // - if there are multiple unrelated defs of non-overlapping
1058 // subregisters of S, then the stack for S will have both (in an
1059 // unspecified order), but the order does not matter from the data-
1060 // -flow perspective.
1061
1062 for (NodeAddr<DefNode*> DA : Defs) {
1063 if (Visited.count(DA.Id))
1064 continue;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001065
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001066 NodeList Rel = getRelatedRefs(IA, DA);
1067 NodeAddr<DefNode*> PDA = Rel.front();
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001068 RegisterRef RR = PDA.Addr->getRegRef(*this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001069#ifndef NDEBUG
1070 // Assert if the register is defined in two or more unrelated defs.
1071 // This could happen if there are two or more def operands defining it.
1072 if (!Defined.insert(RR).second) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001073 MachineInstr *MI = NodeAddr<StmtNode*>(IA).Addr->getCode();
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001074 dbgs() << "Multiple definitions of register: "
1075 << Print<RegisterRef>(RR, *this) << " in\n " << *MI
1076 << "in BB#" << MI->getParent()->getNumber() << '\n';
1077 llvm_unreachable(nullptr);
1078 }
1079#endif
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001080 // Push the definition on the stack for the register and all aliases.
1081 // The def stack traversal in linkNodeUp will check the exact aliasing.
1082 DefM[RR.Reg].push(DA);
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +00001083 for (RegisterId A : PRI.getAliasSet(RR.Reg)) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001084 // Check that we don't push the same def twice.
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +00001085 assert(A != RR.Reg);
1086 DefM[A].push(DA);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001087 }
1088 // Mark all the related defs as visited.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001089 for (NodeAddr<NodeBase*> T : Rel)
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001090 Visited.insert(T.Id);
1091 }
1092}
1093
1094// Return the list of all reference nodes related to RA, including RA itself.
1095// See "getNextRelated" for the meaning of a "related reference".
1096NodeList DataFlowGraph::getRelatedRefs(NodeAddr<InstrNode*> IA,
1097 NodeAddr<RefNode*> RA) const {
1098 assert(IA.Id != 0 && RA.Id != 0);
1099
1100 NodeList Refs;
1101 NodeId Start = RA.Id;
1102 do {
1103 Refs.push_back(RA);
1104 RA = getNextRelated(IA, RA);
1105 } while (RA.Id != 0 && RA.Id != Start);
1106 return Refs;
1107}
1108
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001109// Clear all information in the graph.
1110void DataFlowGraph::reset() {
1111 Memory.clear();
Krzysztof Parzyszek047149f2016-07-22 16:09:47 +00001112 BlockNodes.clear();
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001113 Func = NodeAddr<FuncNode*>();
1114}
1115
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001116// Return the next reference node in the instruction node IA that is related
1117// to RA. Conceptually, two reference nodes are related if they refer to the
1118// same instance of a register access, but differ in flags or other minor
1119// characteristics. Specific examples of related nodes are shadow reference
1120// nodes.
1121// Return the equivalent of nullptr if there are no more related references.
1122NodeAddr<RefNode*> DataFlowGraph::getNextRelated(NodeAddr<InstrNode*> IA,
1123 NodeAddr<RefNode*> RA) const {
1124 assert(IA.Id != 0 && RA.Id != 0);
1125
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001126 auto Related = [this,RA](NodeAddr<RefNode*> TA) -> bool {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001127 if (TA.Addr->getKind() != RA.Addr->getKind())
1128 return false;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001129 if (TA.Addr->getRegRef(*this) != RA.Addr->getRegRef(*this))
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001130 return false;
1131 return true;
1132 };
1133 auto RelatedStmt = [&Related,RA](NodeAddr<RefNode*> TA) -> bool {
1134 return Related(TA) &&
1135 &RA.Addr->getOp() == &TA.Addr->getOp();
1136 };
1137 auto RelatedPhi = [&Related,RA](NodeAddr<RefNode*> TA) -> bool {
1138 if (!Related(TA))
1139 return false;
1140 if (TA.Addr->getKind() != NodeAttrs::Use)
1141 return true;
1142 // For phi uses, compare predecessor blocks.
1143 const NodeAddr<const PhiUseNode*> TUA = TA;
1144 const NodeAddr<const PhiUseNode*> RUA = RA;
1145 return TUA.Addr->getPredecessor() == RUA.Addr->getPredecessor();
1146 };
1147
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001148 RegisterRef RR = RA.Addr->getRegRef(*this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001149 if (IA.Addr->getKind() == NodeAttrs::Stmt)
1150 return RA.Addr->getNextRef(RR, RelatedStmt, true, *this);
1151 return RA.Addr->getNextRef(RR, RelatedPhi, true, *this);
1152}
1153
1154// Find the next node related to RA in IA that satisfies condition P.
1155// If such a node was found, return a pair where the second element is the
1156// located node. If such a node does not exist, return a pair where the
1157// first element is the element after which such a node should be inserted,
1158// and the second element is a null-address.
1159template <typename Predicate>
1160std::pair<NodeAddr<RefNode*>,NodeAddr<RefNode*>>
1161DataFlowGraph::locateNextRef(NodeAddr<InstrNode*> IA, NodeAddr<RefNode*> RA,
1162 Predicate P) const {
1163 assert(IA.Id != 0 && RA.Id != 0);
1164
1165 NodeAddr<RefNode*> NA;
1166 NodeId Start = RA.Id;
1167 while (true) {
1168 NA = getNextRelated(IA, RA);
1169 if (NA.Id == 0 || NA.Id == Start)
1170 break;
1171 if (P(NA))
1172 break;
1173 RA = NA;
1174 }
1175
1176 if (NA.Id != 0 && NA.Id != Start)
1177 return std::make_pair(RA, NA);
1178 return std::make_pair(RA, NodeAddr<RefNode*>());
1179}
1180
1181// Get the next shadow node in IA corresponding to RA, and optionally create
1182// such a node if it does not exist.
1183NodeAddr<RefNode*> DataFlowGraph::getNextShadow(NodeAddr<InstrNode*> IA,
1184 NodeAddr<RefNode*> RA, bool Create) {
1185 assert(IA.Id != 0 && RA.Id != 0);
1186
1187 uint16_t Flags = RA.Addr->getFlags() | NodeAttrs::Shadow;
1188 auto IsShadow = [Flags] (NodeAddr<RefNode*> TA) -> bool {
1189 return TA.Addr->getFlags() == Flags;
1190 };
1191 auto Loc = locateNextRef(IA, RA, IsShadow);
1192 if (Loc.second.Id != 0 || !Create)
1193 return Loc.second;
1194
1195 // Create a copy of RA and mark is as shadow.
1196 NodeAddr<RefNode*> NA = cloneNode(RA);
1197 NA.Addr->setFlags(Flags | NodeAttrs::Shadow);
1198 IA.Addr->addMemberAfter(Loc.first, NA, *this);
1199 return NA;
1200}
1201
1202// Get the next shadow node in IA corresponding to RA. Return null-address
1203// if such a node does not exist.
1204NodeAddr<RefNode*> DataFlowGraph::getNextShadow(NodeAddr<InstrNode*> IA,
1205 NodeAddr<RefNode*> RA) const {
1206 assert(IA.Id != 0 && RA.Id != 0);
1207 uint16_t Flags = RA.Addr->getFlags() | NodeAttrs::Shadow;
1208 auto IsShadow = [Flags] (NodeAddr<RefNode*> TA) -> bool {
1209 return TA.Addr->getFlags() == Flags;
1210 };
1211 return locateNextRef(IA, RA, IsShadow).second;
1212}
1213
1214// Create a new statement node in the block node BA that corresponds to
1215// the machine instruction MI.
1216void DataFlowGraph::buildStmt(NodeAddr<BlockNode*> BA, MachineInstr &In) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001217 NodeAddr<StmtNode*> SA = newStmt(BA, &In);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001218
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +00001219 auto isCall = [] (const MachineInstr &In) -> bool {
1220 if (In.isCall())
1221 return true;
1222 // Is tail call?
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +00001223 if (In.isBranch()) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001224 for (const MachineOperand &Op : In.operands())
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +00001225 if (Op.isGlobal() || Op.isSymbol())
1226 return true;
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +00001227 // Assume indirect branches are calls. This is for the purpose of
1228 // keeping implicit operands, and so it won't hurt on intra-function
1229 // indirect branches.
1230 if (In.isIndirectBranch())
1231 return true;
1232 }
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +00001233 return false;
1234 };
1235
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001236 auto isDefUndef = [this] (const MachineInstr &In, RegisterRef DR) -> bool {
1237 // This instruction defines DR. Check if there is a use operand that
1238 // would make DR live on entry to the instruction.
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +00001239 for (const MachineOperand &Op : In.operands()) {
1240 if (!Op.isReg() || Op.getReg() == 0 || !Op.isUse() || Op.isUndef())
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001241 continue;
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +00001242 RegisterRef UR = makeRegRef(Op);
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +00001243 if (PRI.alias(DR, UR))
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001244 return false;
1245 }
1246 return true;
1247 };
1248
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001249 // Collect a set of registers that this instruction implicitly uses
1250 // or defines. Implicit operands from an instruction will be ignored
1251 // unless they are listed here.
1252 RegisterSet ImpUses, ImpDefs;
1253 if (const uint16_t *ImpD = In.getDesc().getImplicitDefs())
1254 while (uint16_t R = *ImpD++)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001255 ImpDefs.insert(RegisterRef(R));
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001256 if (const uint16_t *ImpU = In.getDesc().getImplicitUses())
1257 while (uint16_t R = *ImpU++)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001258 ImpUses.insert(RegisterRef(R));
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001259
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +00001260 bool IsCall = isCall(In);
1261 bool NeedsImplicit = IsCall || In.isInlineAsm() || In.isReturn();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001262 bool IsPredicated = TII.isPredicated(In);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001263 unsigned NumOps = In.getNumOperands();
1264
1265 // Avoid duplicate implicit defs. This will not detect cases of implicit
1266 // defs that define registers that overlap, but it is not clear how to
1267 // interpret that in the absence of explicit defs. Overlapping explicit
1268 // defs are likely illegal already.
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +00001269 BitVector DoneDefs(TRI.getNumRegs());
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001270 // Process explicit defs first.
1271 for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1272 MachineOperand &Op = In.getOperand(OpN);
1273 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1274 continue;
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +00001275 unsigned R = Op.getReg();
1276 if (!R || !TargetRegisterInfo::isPhysicalRegister(R))
1277 continue;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001278 uint16_t Flags = NodeAttrs::None;
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001279 if (TOI.isPreserving(In, OpN)) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001280 Flags |= NodeAttrs::Preserving;
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001281 // If the def is preserving, check if it is also undefined.
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +00001282 if (isDefUndef(In, makeRegRef(Op)))
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001283 Flags |= NodeAttrs::Undef;
1284 }
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001285 if (TOI.isClobbering(In, OpN))
1286 Flags |= NodeAttrs::Clobbering;
1287 if (TOI.isFixedReg(In, OpN))
1288 Flags |= NodeAttrs::Fixed;
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +00001289 if (IsCall && Op.isDead())
1290 Flags |= NodeAttrs::Dead;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001291 NodeAddr<DefNode*> DA = newDef(SA, Op, Flags);
1292 SA.Addr->addMember(DA, *this);
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +00001293 assert(!DoneDefs.test(R));
1294 DoneDefs.set(R);
1295 }
1296
1297 // Process reg-masks (as clobbers).
1298 BitVector DoneClobbers(TRI.getNumRegs());
1299 for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1300 MachineOperand &Op = In.getOperand(OpN);
1301 if (!Op.isRegMask())
1302 continue;
1303 uint16_t Flags = NodeAttrs::Clobbering | NodeAttrs::Fixed |
1304 NodeAttrs::Dead;
1305 NodeAddr<DefNode*> DA = newDef(SA, Op, Flags);
1306 SA.Addr->addMember(DA, *this);
1307 // Record all clobbered registers in DoneDefs.
1308 const uint32_t *RM = Op.getRegMask();
1309 for (unsigned i = 1, e = TRI.getNumRegs(); i != e; ++i)
1310 if (!(RM[i/32] & (1u << (i%32))))
1311 DoneClobbers.set(i);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001312 }
1313
1314 // Process implicit defs, skipping those that have already been added
1315 // as explicit.
1316 for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1317 MachineOperand &Op = In.getOperand(OpN);
1318 if (!Op.isReg() || !Op.isDef() || !Op.isImplicit())
1319 continue;
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +00001320 unsigned R = Op.getReg();
1321 if (!R || !TargetRegisterInfo::isPhysicalRegister(R) || DoneDefs.test(R))
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001322 continue;
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +00001323 RegisterRef RR = makeRegRef(Op);
1324 if (!NeedsImplicit && !ImpDefs.count(RR))
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001325 continue;
1326 uint16_t Flags = NodeAttrs::None;
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001327 if (TOI.isPreserving(In, OpN)) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001328 Flags |= NodeAttrs::Preserving;
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001329 // If the def is preserving, check if it is also undefined.
1330 if (isDefUndef(In, RR))
1331 Flags |= NodeAttrs::Undef;
1332 }
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001333 if (TOI.isClobbering(In, OpN))
1334 Flags |= NodeAttrs::Clobbering;
1335 if (TOI.isFixedReg(In, OpN))
1336 Flags |= NodeAttrs::Fixed;
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +00001337 if (IsCall && Op.isDead()) {
1338 if (DoneClobbers.test(R))
1339 continue;
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +00001340 Flags |= NodeAttrs::Dead;
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +00001341 }
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001342 NodeAddr<DefNode*> DA = newDef(SA, Op, Flags);
1343 SA.Addr->addMember(DA, *this);
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +00001344 DoneDefs.set(R);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001345 }
1346
1347 for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1348 MachineOperand &Op = In.getOperand(OpN);
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001349 if (!Op.isReg() || !Op.isUse())
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001350 continue;
Krzysztof Parzyszek3695d062017-01-30 19:16:30 +00001351 unsigned R = Op.getReg();
1352 if (!R || !TargetRegisterInfo::isPhysicalRegister(R))
1353 continue;
1354 RegisterRef RR = makeRegRef(Op);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001355 // Add implicit uses on return and call instructions, and on predicated
1356 // instructions regardless of whether or not they appear in the instruction
1357 // descriptor's list.
1358 bool Implicit = Op.isImplicit();
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +00001359 bool TakeImplicit = NeedsImplicit || IsPredicated;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001360 if (Implicit && !TakeImplicit && !ImpUses.count(RR))
1361 continue;
1362 uint16_t Flags = NodeAttrs::None;
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001363 if (Op.isUndef())
1364 Flags |= NodeAttrs::Undef;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001365 if (TOI.isFixedReg(In, OpN))
1366 Flags |= NodeAttrs::Fixed;
1367 NodeAddr<UseNode*> UA = newUse(SA, Op, Flags);
1368 SA.Addr->addMember(UA, *this);
1369 }
1370}
1371
1372// Build a map that for each block will have the set of all references from
1373// that block, and from all blocks dominated by it.
1374void DataFlowGraph::buildBlockRefs(NodeAddr<BlockNode*> BA,
1375 BlockRefsMap &RefM) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001376 RegisterSet &Refs = RefM[BA.Id];
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001377 MachineDomTreeNode *N = MDT.getNode(BA.Addr->getCode());
1378 assert(N);
1379 for (auto I : *N) {
1380 MachineBasicBlock *SB = I->getBlock();
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001381 NodeAddr<BlockNode*> SBA = findBlock(SB);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001382 buildBlockRefs(SBA, RefM);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001383 const RegisterSet &RefsS = RefM[SBA.Id];
1384 Refs.insert(RefsS.begin(), RefsS.end());
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001385 }
1386
1387 for (NodeAddr<InstrNode*> IA : BA.Addr->members(*this))
1388 for (NodeAddr<RefNode*> RA : IA.Addr->members(*this))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001389 Refs.insert(RA.Addr->getRegRef(*this));
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001390}
1391
1392// Scan all defs in the block node BA and record in PhiM the locations of
1393// phi nodes corresponding to these defs.
1394void DataFlowGraph::recordDefsForDF(BlockRefsMap &PhiM, BlockRefsMap &RefM,
1395 NodeAddr<BlockNode*> BA) {
1396 // Check all defs from block BA and record them in each block in BA's
1397 // iterated dominance frontier. This information will later be used to
1398 // create phi nodes.
1399 MachineBasicBlock *BB = BA.Addr->getCode();
1400 assert(BB);
1401 auto DFLoc = MDF.find(BB);
1402 if (DFLoc == MDF.end() || DFLoc->second.empty())
1403 return;
1404
1405 // Traverse all instructions in the block and collect the set of all
1406 // defined references. For each reference there will be a phi created
1407 // in the block's iterated dominance frontier.
1408 // This is done to make sure that each defined reference gets only one
1409 // phi node, even if it is defined multiple times.
1410 RegisterSet Defs;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001411 for (NodeAddr<InstrNode*> IA : BA.Addr->members(*this))
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001412 for (NodeAddr<RefNode*> RA : IA.Addr->members_if(IsDef, *this))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001413 Defs.insert(RA.Addr->getRegRef(*this));
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001414
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001415 // Calculate the iterated dominance frontier of BB.
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001416 const MachineDominanceFrontier::DomSetType &DF = DFLoc->second;
1417 SetVector<MachineBasicBlock*> IDF(DF.begin(), DF.end());
1418 for (unsigned i = 0; i < IDF.size(); ++i) {
1419 auto F = MDF.find(IDF[i]);
1420 if (F != MDF.end())
1421 IDF.insert(F->second.begin(), F->second.end());
1422 }
1423
1424 // Get the register references that are reachable from this block.
1425 RegisterSet &Refs = RefM[BA.Id];
1426 for (auto DB : IDF) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001427 NodeAddr<BlockNode*> DBA = findBlock(DB);
1428 const RegisterSet &RefsD = RefM[DBA.Id];
1429 Refs.insert(RefsD.begin(), RefsD.end());
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001430 }
1431
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001432 // Finally, add the set of defs to each block in the iterated dominance
1433 // frontier.
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001434 for (auto DB : IDF) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001435 NodeAddr<BlockNode*> DBA = findBlock(DB);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001436 PhiM[DBA.Id].insert(Defs.begin(), Defs.end());
1437 }
1438}
1439
1440// Given the locations of phi nodes in the map PhiM, create the phi nodes
1441// that are located in the block node BA.
1442void DataFlowGraph::buildPhis(BlockRefsMap &PhiM, BlockRefsMap &RefM,
1443 NodeAddr<BlockNode*> BA) {
1444 // Check if this blocks has any DF defs, i.e. if there are any defs
1445 // that this block is in the iterated dominance frontier of.
1446 auto HasDF = PhiM.find(BA.Id);
1447 if (HasDF == PhiM.end() || HasDF->second.empty())
1448 return;
1449
1450 // First, remove all R in Refs in such that there exists T in Refs
1451 // such that T covers R. In other words, only leave those refs that
1452 // are not covered by another ref (i.e. maximal with respect to covering).
1453
1454 auto MaxCoverIn = [this] (RegisterRef RR, RegisterSet &RRs) -> RegisterRef {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001455 for (RegisterRef I : RRs)
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +00001456 if (I != RR && RegisterAggr::isCoverOf(I, RR, PRI))
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001457 RR = I;
1458 return RR;
1459 };
1460
1461 RegisterSet MaxDF;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001462 for (RegisterRef I : HasDF->second)
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001463 MaxDF.insert(MaxCoverIn(I, HasDF->second));
1464
1465 std::vector<RegisterRef> MaxRefs;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001466 RegisterSet &RefB = RefM[BA.Id];
1467 for (RegisterRef I : MaxDF)
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001468 MaxRefs.push_back(MaxCoverIn(I, RefB));
1469
1470 // Now, for each R in MaxRefs, get the alias closure of R. If the closure
1471 // only has R in it, create a phi a def for R. Otherwise, create a phi,
1472 // and add a def for each S in the closure.
1473
1474 // Sort the refs so that the phis will be created in a deterministic order.
1475 std::sort(MaxRefs.begin(), MaxRefs.end());
1476 // Remove duplicates.
1477 auto NewEnd = std::unique(MaxRefs.begin(), MaxRefs.end());
1478 MaxRefs.erase(NewEnd, MaxRefs.end());
1479
1480 auto Aliased = [this,&MaxRefs](RegisterRef RR,
1481 std::vector<unsigned> &Closure) -> bool {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001482 for (unsigned I : Closure)
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +00001483 if (PRI.alias(RR, MaxRefs[I]))
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001484 return true;
1485 return false;
1486 };
1487
1488 // Prepare a list of NodeIds of the block's predecessors.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001489 NodeList Preds;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001490 const MachineBasicBlock *MBB = BA.Addr->getCode();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001491 for (MachineBasicBlock *PB : MBB->predecessors())
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001492 Preds.push_back(findBlock(PB));
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001493
1494 while (!MaxRefs.empty()) {
1495 // Put the first element in the closure, and then add all subsequent
1496 // elements from MaxRefs to it, if they alias at least one element
1497 // already in the closure.
1498 // ClosureIdx: vector of indices in MaxRefs of members of the closure.
1499 std::vector<unsigned> ClosureIdx = { 0 };
1500 for (unsigned i = 1; i != MaxRefs.size(); ++i)
1501 if (Aliased(MaxRefs[i], ClosureIdx))
1502 ClosureIdx.push_back(i);
1503
1504 // Build a phi for the closure.
1505 unsigned CS = ClosureIdx.size();
1506 NodeAddr<PhiNode*> PA = newPhi(BA);
1507
1508 // Add defs.
1509 for (unsigned X = 0; X != CS; ++X) {
1510 RegisterRef RR = MaxRefs[ClosureIdx[X]];
1511 uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving;
1512 NodeAddr<DefNode*> DA = newDef(PA, RR, PhiFlags);
1513 PA.Addr->addMember(DA, *this);
1514 }
1515 // Add phi uses.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001516 for (NodeAddr<BlockNode*> PBA : Preds) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001517 for (unsigned X = 0; X != CS; ++X) {
1518 RegisterRef RR = MaxRefs[ClosureIdx[X]];
1519 NodeAddr<PhiUseNode*> PUA = newPhiUse(PA, RR, PBA);
1520 PA.Addr->addMember(PUA, *this);
1521 }
1522 }
1523
1524 // Erase from MaxRefs all elements in the closure.
1525 auto Begin = MaxRefs.begin();
1526 for (unsigned i = ClosureIdx.size(); i != 0; --i)
1527 MaxRefs.erase(Begin + ClosureIdx[i-1]);
1528 }
1529}
1530
1531// Remove any unneeded phi nodes that were created during the build process.
1532void DataFlowGraph::removeUnusedPhis() {
1533 // This will remove unused phis, i.e. phis where each def does not reach
1534 // any uses or other defs. This will not detect or remove circular phi
1535 // chains that are otherwise dead. Unused/dead phis are created during
1536 // the build process and this function is intended to remove these cases
1537 // that are easily determinable to be unnecessary.
1538
1539 SetVector<NodeId> PhiQ;
1540 for (NodeAddr<BlockNode*> BA : Func.Addr->members(*this)) {
1541 for (auto P : BA.Addr->members_if(IsPhi, *this))
1542 PhiQ.insert(P.Id);
1543 }
1544
1545 static auto HasUsedDef = [](NodeList &Ms) -> bool {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001546 for (NodeAddr<NodeBase*> M : Ms) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001547 if (M.Addr->getKind() != NodeAttrs::Def)
1548 continue;
1549 NodeAddr<DefNode*> DA = M;
1550 if (DA.Addr->getReachedDef() != 0 || DA.Addr->getReachedUse() != 0)
1551 return true;
1552 }
1553 return false;
1554 };
1555
1556 // Any phi, if it is removed, may affect other phis (make them dead).
1557 // For each removed phi, collect the potentially affected phis and add
1558 // them back to the queue.
1559 while (!PhiQ.empty()) {
1560 auto PA = addr<PhiNode*>(PhiQ[0]);
1561 PhiQ.remove(PA.Id);
1562 NodeList Refs = PA.Addr->members(*this);
1563 if (HasUsedDef(Refs))
1564 continue;
1565 for (NodeAddr<RefNode*> RA : Refs) {
1566 if (NodeId RD = RA.Addr->getReachingDef()) {
1567 auto RDA = addr<DefNode*>(RD);
1568 NodeAddr<InstrNode*> OA = RDA.Addr->getOwner(*this);
1569 if (IsPhi(OA))
1570 PhiQ.insert(OA.Id);
1571 }
1572 if (RA.Addr->isDef())
Krzysztof Parzyszek69e670d52016-01-18 20:41:34 +00001573 unlinkDef(RA, true);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001574 else
Krzysztof Parzyszek69e670d52016-01-18 20:41:34 +00001575 unlinkUse(RA, true);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001576 }
1577 NodeAddr<BlockNode*> BA = PA.Addr->getOwner(*this);
1578 BA.Addr->removeMember(PA, *this);
1579 }
1580}
1581
1582// For a given reference node TA in an instruction node IA, connect the
1583// reaching def of TA to the appropriate def node. Create any shadow nodes
1584// as appropriate.
1585template <typename T>
1586void DataFlowGraph::linkRefUp(NodeAddr<InstrNode*> IA, NodeAddr<T> TA,
1587 DefStack &DS) {
1588 if (DS.empty())
1589 return;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001590 RegisterRef RR = TA.Addr->getRegRef(*this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001591 NodeAddr<T> TAP;
1592
1593 // References from the def stack that have been examined so far.
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +00001594 RegisterAggr Defs(PRI);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001595
1596 for (auto I = DS.top(), E = DS.bottom(); I != E; I.down()) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001597 RegisterRef QR = I->Addr->getRegRef(*this);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001598
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001599 // Skip all defs that are aliased to any of the defs that we have already
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001600 // seen. If this completes a cover of RR, stop the stack traversal.
1601 bool Alias = Defs.hasAliasOf(QR);
1602 bool Cover = Defs.insert(QR).hasCoverOf(RR);
1603 if (Alias) {
1604 if (Cover)
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001605 break;
1606 continue;
1607 }
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001608
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001609 // The reaching def.
1610 NodeAddr<DefNode*> RDA = *I;
1611
1612 // Pick the reached node.
1613 if (TAP.Id == 0) {
1614 TAP = TA;
1615 } else {
1616 // Mark the existing ref as "shadow" and create a new shadow.
1617 TAP.Addr->setFlags(TAP.Addr->getFlags() | NodeAttrs::Shadow);
1618 TAP = getNextShadow(IA, TAP, true);
1619 }
1620
1621 // Create the link.
1622 TAP.Addr->linkToDef(TAP.Id, RDA);
1623
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001624 if (Cover)
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001625 break;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001626 }
1627}
1628
1629// Create data-flow links for all reference nodes in the statement node SA.
1630void DataFlowGraph::linkStmtRefs(DefStackMap &DefM, NodeAddr<StmtNode*> SA) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001631#ifndef NDEBUG
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001632 RegisterSet Defs;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001633#endif
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001634
1635 // Link all nodes (upwards in the data-flow) with their reaching defs.
1636 for (NodeAddr<RefNode*> RA : SA.Addr->members(*this)) {
1637 uint16_t Kind = RA.Addr->getKind();
1638 assert(Kind == NodeAttrs::Def || Kind == NodeAttrs::Use);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001639 RegisterRef RR = RA.Addr->getRegRef(*this);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001640#ifndef NDEBUG
1641 // Do not expect multiple defs of the same reference.
1642 assert(Kind != NodeAttrs::Def || !Defs.count(RR));
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001643 Defs.insert(RR);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001644#endif
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001645
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001646 auto F = DefM.find(RR.Reg);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001647 if (F == DefM.end())
1648 continue;
1649 DefStack &DS = F->second;
1650 if (Kind == NodeAttrs::Use)
1651 linkRefUp<UseNode*>(SA, RA, DS);
1652 else if (Kind == NodeAttrs::Def)
1653 linkRefUp<DefNode*>(SA, RA, DS);
1654 else
1655 llvm_unreachable("Unexpected node in instruction");
1656 }
1657}
1658
1659// Create data-flow links for all instructions in the block node BA. This
1660// will include updating any phi nodes in BA.
1661void DataFlowGraph::linkBlockRefs(DefStackMap &DefM, NodeAddr<BlockNode*> BA) {
1662 // Push block delimiters.
1663 markBlock(BA.Id, DefM);
1664
Krzysztof Parzyszek89757432016-05-05 22:00:44 +00001665 assert(BA.Addr && "block node address is needed to create a data-flow link");
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001666 // For each non-phi instruction in the block, link all the defs and uses
1667 // to their reaching defs. For any member of the block (including phis),
1668 // push the defs on the corresponding stacks.
1669 for (NodeAddr<InstrNode*> IA : BA.Addr->members(*this)) {
1670 // Ignore phi nodes here. They will be linked part by part from the
1671 // predecessors.
1672 if (IA.Addr->getKind() == NodeAttrs::Stmt)
1673 linkStmtRefs(DefM, IA);
1674
1675 // Push the definitions on the stack.
1676 pushDefs(IA, DefM);
1677 }
1678
1679 // Recursively process all children in the dominator tree.
1680 MachineDomTreeNode *N = MDT.getNode(BA.Addr->getCode());
1681 for (auto I : *N) {
1682 MachineBasicBlock *SB = I->getBlock();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001683 NodeAddr<BlockNode*> SBA = findBlock(SB);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001684 linkBlockRefs(DefM, SBA);
1685 }
1686
1687 // Link the phi uses from the successor blocks.
1688 auto IsUseForBA = [BA](NodeAddr<NodeBase*> NA) -> bool {
1689 if (NA.Addr->getKind() != NodeAttrs::Use)
1690 return false;
1691 assert(NA.Addr->getFlags() & NodeAttrs::PhiRef);
1692 NodeAddr<PhiUseNode*> PUA = NA;
1693 return PUA.Addr->getPredecessor() == BA.Id;
1694 };
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001695
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001696 RegisterSet EHLiveIns = getLandingPadLiveIns();
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001697 MachineBasicBlock *MBB = BA.Addr->getCode();
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001698
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001699 for (MachineBasicBlock *SB : MBB->successors()) {
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001700 bool IsEHPad = SB->isEHPad();
1701 NodeAddr<BlockNode*> SBA = findBlock(SB);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001702 for (NodeAddr<InstrNode*> IA : SBA.Addr->members_if(IsPhi, *this)) {
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001703 // Do not link phi uses for landing pad live-ins.
1704 if (IsEHPad) {
1705 // Find what register this phi is for.
1706 NodeAddr<RefNode*> RA = IA.Addr->getFirstMember(*this);
1707 assert(RA.Id != 0);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001708 if (EHLiveIns.count(RA.Addr->getRegRef(*this)))
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001709 continue;
1710 }
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001711 // Go over each phi use associated with MBB, and link it.
1712 for (auto U : IA.Addr->members_if(IsUseForBA, *this)) {
1713 NodeAddr<PhiUseNode*> PUA = U;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001714 RegisterRef RR = PUA.Addr->getRegRef(*this);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001715 linkRefUp<UseNode*>(IA, PUA, DefM[RR.Reg]);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001716 }
1717 }
1718 }
1719
1720 // Pop all defs from this block from the definition stacks.
1721 releaseBlock(BA.Id, DefM);
1722}
1723
1724// Remove the use node UA from any data-flow and structural links.
Krzysztof Parzyszek69e670d52016-01-18 20:41:34 +00001725void DataFlowGraph::unlinkUseDF(NodeAddr<UseNode*> UA) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001726 NodeId RD = UA.Addr->getReachingDef();
1727 NodeId Sib = UA.Addr->getSibling();
1728
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001729 if (RD == 0) {
1730 assert(Sib == 0);
1731 return;
1732 }
1733
1734 auto RDA = addr<DefNode*>(RD);
1735 auto TA = addr<UseNode*>(RDA.Addr->getReachedUse());
1736 if (TA.Id == UA.Id) {
1737 RDA.Addr->setReachedUse(Sib);
1738 return;
1739 }
1740
1741 while (TA.Id != 0) {
1742 NodeId S = TA.Addr->getSibling();
1743 if (S == UA.Id) {
1744 TA.Addr->setSibling(UA.Addr->getSibling());
1745 return;
1746 }
1747 TA = addr<UseNode*>(S);
1748 }
1749}
1750
1751// Remove the def node DA from any data-flow and structural links.
Krzysztof Parzyszek69e670d52016-01-18 20:41:34 +00001752void DataFlowGraph::unlinkDefDF(NodeAddr<DefNode*> DA) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001753 //
1754 // RD
1755 // | reached
1756 // | def
1757 // :
1758 // .
1759 // +----+
1760 // ... -- | DA | -- ... -- 0 : sibling chain of DA
1761 // +----+
1762 // | | reached
1763 // | : def
1764 // | .
1765 // | ... : Siblings (defs)
1766 // |
1767 // : reached
1768 // . use
1769 // ... : sibling chain of reached uses
1770
1771 NodeId RD = DA.Addr->getReachingDef();
1772
1773 // Visit all siblings of the reached def and reset their reaching defs.
1774 // Also, defs reached by DA are now "promoted" to being reached by RD,
1775 // so all of them will need to be spliced into the sibling chain where
1776 // DA belongs.
1777 auto getAllNodes = [this] (NodeId N) -> NodeList {
1778 NodeList Res;
1779 while (N) {
1780 auto RA = addr<RefNode*>(N);
1781 // Keep the nodes in the exact sibling order.
1782 Res.push_back(RA);
1783 N = RA.Addr->getSibling();
1784 }
1785 return Res;
1786 };
1787 NodeList ReachedDefs = getAllNodes(DA.Addr->getReachedDef());
1788 NodeList ReachedUses = getAllNodes(DA.Addr->getReachedUse());
1789
1790 if (RD == 0) {
1791 for (NodeAddr<RefNode*> I : ReachedDefs)
1792 I.Addr->setSibling(0);
1793 for (NodeAddr<RefNode*> I : ReachedUses)
1794 I.Addr->setSibling(0);
1795 }
1796 for (NodeAddr<DefNode*> I : ReachedDefs)
1797 I.Addr->setReachingDef(RD);
1798 for (NodeAddr<UseNode*> I : ReachedUses)
1799 I.Addr->setReachingDef(RD);
1800
1801 NodeId Sib = DA.Addr->getSibling();
1802 if (RD == 0) {
1803 assert(Sib == 0);
1804 return;
1805 }
1806
1807 // Update the reaching def node and remove DA from the sibling list.
1808 auto RDA = addr<DefNode*>(RD);
1809 auto TA = addr<DefNode*>(RDA.Addr->getReachedDef());
1810 if (TA.Id == DA.Id) {
1811 // If DA is the first reached def, just update the RD's reached def
1812 // to the DA's sibling.
1813 RDA.Addr->setReachedDef(Sib);
1814 } else {
1815 // Otherwise, traverse the sibling list of the reached defs and remove
1816 // DA from it.
1817 while (TA.Id != 0) {
1818 NodeId S = TA.Addr->getSibling();
1819 if (S == DA.Id) {
1820 TA.Addr->setSibling(Sib);
1821 break;
1822 }
1823 TA = addr<DefNode*>(S);
1824 }
1825 }
1826
1827 // Splice the DA's reached defs into the RDA's reached def chain.
1828 if (!ReachedDefs.empty()) {
1829 auto Last = NodeAddr<DefNode*>(ReachedDefs.back());
1830 Last.Addr->setSibling(RDA.Addr->getReachedDef());
1831 RDA.Addr->setReachedDef(ReachedDefs.front().Id);
1832 }
1833 // Splice the DA's reached uses into the RDA's reached use chain.
1834 if (!ReachedUses.empty()) {
1835 auto Last = NodeAddr<UseNode*>(ReachedUses.back());
1836 Last.Addr->setSibling(RDA.Addr->getReachedUse());
1837 RDA.Addr->setReachedUse(ReachedUses.front().Id);
1838 }
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001839}