blob: f25fcf72065b3dc94953aeb6aa1a411d0503613d [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"
13
14#include "llvm/ADT/SetVector.h"
15#include "llvm/CodeGen/MachineBasicBlock.h"
16#include "llvm/CodeGen/MachineDominanceFrontier.h"
17#include "llvm/CodeGen/MachineDominators.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineRegisterInfo.h"
20#include "llvm/Target/TargetInstrInfo.h"
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +000021#include "llvm/Target/TargetLowering.h"
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +000022#include "llvm/Target/TargetRegisterInfo.h"
23
24using namespace llvm;
25using namespace rdf;
26
27// Printing functions. Have them here first, so that the rest of the code
28// can use them.
Benjamin Kramer922efd72016-05-27 10:06:40 +000029namespace llvm {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +000030namespace rdf {
31
32template<>
33raw_ostream &operator<< (raw_ostream &OS, const Print<RegisterRef> &P) {
34 auto &TRI = P.G.getTRI();
35 if (P.Obj.Reg > 0 && P.Obj.Reg < TRI.getNumRegs())
36 OS << TRI.getName(P.Obj.Reg);
37 else
38 OS << '#' << P.Obj.Reg;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +000039 if (P.Obj.Mask != ~LaneBitmask(0))
40 OS << ":" << PrintLaneMask(P.Obj.Mask);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +000041 return OS;
42}
43
44template<>
45raw_ostream &operator<< (raw_ostream &OS, const Print<NodeId> &P) {
46 auto NA = P.G.addr<NodeBase*>(P.Obj);
47 uint16_t Attrs = NA.Addr->getAttrs();
48 uint16_t Kind = NodeAttrs::kind(Attrs);
49 uint16_t Flags = NodeAttrs::flags(Attrs);
50 switch (NodeAttrs::type(Attrs)) {
51 case NodeAttrs::Code:
52 switch (Kind) {
53 case NodeAttrs::Func: OS << 'f'; break;
54 case NodeAttrs::Block: OS << 'b'; break;
55 case NodeAttrs::Stmt: OS << 's'; break;
56 case NodeAttrs::Phi: OS << 'p'; break;
57 default: OS << "c?"; break;
58 }
59 break;
60 case NodeAttrs::Ref:
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +000061 if (Flags & NodeAttrs::Undef)
62 OS << '/';
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +000063 if (Flags & NodeAttrs::Dead)
64 OS << '\\';
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +000065 if (Flags & NodeAttrs::Preserving)
66 OS << '+';
67 if (Flags & NodeAttrs::Clobbering)
68 OS << '~';
69 switch (Kind) {
70 case NodeAttrs::Use: OS << 'u'; break;
71 case NodeAttrs::Def: OS << 'd'; break;
72 case NodeAttrs::Block: OS << 'b'; break;
73 default: OS << "r?"; break;
74 }
75 break;
76 default:
77 OS << '?';
78 break;
79 }
80 OS << P.Obj;
81 if (Flags & NodeAttrs::Shadow)
82 OS << '"';
83 return OS;
84}
85
86namespace {
87 void printRefHeader(raw_ostream &OS, const NodeAddr<RefNode*> RA,
88 const DataFlowGraph &G) {
89 OS << Print<NodeId>(RA.Id, G) << '<'
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +000090 << Print<RegisterRef>(RA.Addr->getRegRef(G), G) << '>';
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +000091 if (RA.Addr->getFlags() & NodeAttrs::Fixed)
92 OS << '!';
93 }
94}
95
96template<>
97raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<DefNode*>> &P) {
98 printRefHeader(OS, P.Obj, P.G);
99 OS << '(';
100 if (NodeId N = P.Obj.Addr->getReachingDef())
101 OS << Print<NodeId>(N, P.G);
102 OS << ',';
103 if (NodeId N = P.Obj.Addr->getReachedDef())
104 OS << Print<NodeId>(N, P.G);
105 OS << ',';
106 if (NodeId N = P.Obj.Addr->getReachedUse())
107 OS << Print<NodeId>(N, P.G);
108 OS << "):";
109 if (NodeId N = P.Obj.Addr->getSibling())
110 OS << Print<NodeId>(N, P.G);
111 return OS;
112}
113
114template<>
115raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<UseNode*>> &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->getSibling())
122 OS << Print<NodeId>(N, P.G);
123 return OS;
124}
125
126template<>
127raw_ostream &operator<< (raw_ostream &OS,
128 const Print<NodeAddr<PhiUseNode*>> &P) {
129 printRefHeader(OS, P.Obj, P.G);
130 OS << '(';
131 if (NodeId N = P.Obj.Addr->getReachingDef())
132 OS << Print<NodeId>(N, P.G);
133 OS << ',';
134 if (NodeId N = P.Obj.Addr->getPredecessor())
135 OS << Print<NodeId>(N, P.G);
136 OS << "):";
137 if (NodeId N = P.Obj.Addr->getSibling())
138 OS << Print<NodeId>(N, P.G);
139 return OS;
140}
141
142template<>
143raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<RefNode*>> &P) {
144 switch (P.Obj.Addr->getKind()) {
145 case NodeAttrs::Def:
146 OS << PrintNode<DefNode*>(P.Obj, P.G);
147 break;
148 case NodeAttrs::Use:
149 if (P.Obj.Addr->getFlags() & NodeAttrs::PhiRef)
150 OS << PrintNode<PhiUseNode*>(P.Obj, P.G);
151 else
152 OS << PrintNode<UseNode*>(P.Obj, P.G);
153 break;
154 }
155 return OS;
156}
157
158template<>
159raw_ostream &operator<< (raw_ostream &OS, const Print<NodeList> &P) {
160 unsigned N = P.Obj.size();
161 for (auto I : P.Obj) {
162 OS << Print<NodeId>(I.Id, P.G);
163 if (--N)
164 OS << ' ';
165 }
166 return OS;
167}
168
169template<>
170raw_ostream &operator<< (raw_ostream &OS, const Print<NodeSet> &P) {
171 unsigned N = P.Obj.size();
172 for (auto I : P.Obj) {
173 OS << Print<NodeId>(I, P.G);
174 if (--N)
175 OS << ' ';
176 }
177 return OS;
178}
179
180namespace {
181 template <typename T>
182 struct PrintListV {
183 PrintListV(const NodeList &L, const DataFlowGraph &G) : List(L), G(G) {}
184 typedef T Type;
185 const NodeList &List;
186 const DataFlowGraph &G;
187 };
188
189 template <typename T>
190 raw_ostream &operator<< (raw_ostream &OS, const PrintListV<T> &P) {
191 unsigned N = P.List.size();
192 for (NodeAddr<T> A : P.List) {
193 OS << PrintNode<T>(A, P.G);
194 if (--N)
195 OS << ", ";
196 }
197 return OS;
198 }
199}
200
201template<>
202raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<PhiNode*>> &P) {
203 OS << Print<NodeId>(P.Obj.Id, P.G) << ": phi ["
204 << PrintListV<RefNode*>(P.Obj.Addr->members(P.G), P.G) << ']';
205 return OS;
206}
207
208template<>
209raw_ostream &operator<< (raw_ostream &OS,
210 const Print<NodeAddr<StmtNode*>> &P) {
Krzysztof Parzyszek670e0ca2016-09-22 20:58:19 +0000211 const MachineInstr &MI = *P.Obj.Addr->getCode();
212 unsigned Opc = MI.getOpcode();
213 OS << Print<NodeId>(P.Obj.Id, P.G) << ": " << P.G.getTII().getName(Opc);
Krzysztof Parzyszekab26e2d2016-10-03 17:54:33 +0000214 // Print the target for calls and branches (for readability).
215 if (MI.isCall() || MI.isBranch()) {
216 MachineInstr::const_mop_iterator T =
Krzysztof Parzyszek670e0ca2016-09-22 20:58:19 +0000217 find_if(MI.operands(),
218 [] (const MachineOperand &Op) -> bool {
Krzysztof Parzyszekab26e2d2016-10-03 17:54:33 +0000219 return Op.isMBB() || Op.isGlobal() || Op.isSymbol();
Krzysztof Parzyszek670e0ca2016-09-22 20:58:19 +0000220 });
Krzysztof Parzyszekab26e2d2016-10-03 17:54:33 +0000221 if (T != MI.operands_end()) {
222 OS << ' ';
223 if (T->isMBB())
224 OS << "BB#" << T->getMBB()->getNumber();
225 else if (T->isGlobal())
226 OS << T->getGlobal()->getName();
227 else if (T->isSymbol())
228 OS << T->getSymbolName();
Krzysztof Parzyszek670e0ca2016-09-22 20:58:19 +0000229 }
230 }
231 OS << " [" << PrintListV<RefNode*>(P.Obj.Addr->members(P.G), P.G) << ']';
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000232 return OS;
233}
234
235template<>
236raw_ostream &operator<< (raw_ostream &OS,
237 const Print<NodeAddr<InstrNode*>> &P) {
238 switch (P.Obj.Addr->getKind()) {
239 case NodeAttrs::Phi:
240 OS << PrintNode<PhiNode*>(P.Obj, P.G);
241 break;
242 case NodeAttrs::Stmt:
243 OS << PrintNode<StmtNode*>(P.Obj, P.G);
244 break;
245 default:
246 OS << "instr? " << Print<NodeId>(P.Obj.Id, P.G);
247 break;
248 }
249 return OS;
250}
251
252template<>
253raw_ostream &operator<< (raw_ostream &OS,
254 const Print<NodeAddr<BlockNode*>> &P) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000255 MachineBasicBlock *BB = P.Obj.Addr->getCode();
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000256 unsigned NP = BB->pred_size();
257 std::vector<int> Ns;
258 auto PrintBBs = [&OS,&P] (std::vector<int> Ns) -> void {
259 unsigned N = Ns.size();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000260 for (int I : Ns) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000261 OS << "BB#" << I;
262 if (--N)
263 OS << ", ";
264 }
265 };
266
Krzysztof Parzyszekab26e2d2016-10-03 17:54:33 +0000267 OS << Print<NodeId>(P.Obj.Id, P.G) << ": --- BB#" << BB->getNumber()
268 << " --- preds(" << NP << "): ";
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000269 for (MachineBasicBlock *B : BB->predecessors())
270 Ns.push_back(B->getNumber());
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000271 PrintBBs(Ns);
272
273 unsigned NS = BB->succ_size();
274 OS << " succs(" << NS << "): ";
275 Ns.clear();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000276 for (MachineBasicBlock *B : BB->successors())
277 Ns.push_back(B->getNumber());
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000278 PrintBBs(Ns);
279 OS << '\n';
280
281 for (auto I : P.Obj.Addr->members(P.G))
282 OS << PrintNode<InstrNode*>(I, P.G) << '\n';
283 return OS;
284}
285
286template<>
287raw_ostream &operator<< (raw_ostream &OS,
288 const Print<NodeAddr<FuncNode*>> &P) {
289 OS << "DFG dump:[\n" << Print<NodeId>(P.Obj.Id, P.G) << ": Function: "
290 << P.Obj.Addr->getCode()->getName() << '\n';
291 for (auto I : P.Obj.Addr->members(P.G))
292 OS << PrintNode<BlockNode*>(I, P.G) << '\n';
293 OS << "]\n";
294 return OS;
295}
296
297template<>
298raw_ostream &operator<< (raw_ostream &OS, const Print<RegisterSet> &P) {
299 OS << '{';
300 for (auto I : P.Obj)
301 OS << ' ' << Print<RegisterRef>(I, P.G);
302 OS << " }";
303 return OS;
304}
305
306template<>
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000307raw_ostream &operator<< (raw_ostream &OS, const Print<RegisterAggr> &P) {
308 P.Obj.print(OS);
309 return OS;
310}
311
312template<>
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000313raw_ostream &operator<< (raw_ostream &OS,
314 const Print<DataFlowGraph::DefStack> &P) {
315 for (auto I = P.Obj.top(), E = P.Obj.bottom(); I != E; ) {
316 OS << Print<NodeId>(I->Id, P.G)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000317 << '<' << Print<RegisterRef>(I->Addr->getRegRef(P.G), P.G) << '>';
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000318 I.down();
319 if (I != E)
320 OS << ' ';
321 }
322 return OS;
323}
324
325} // namespace rdf
Benjamin Kramer922efd72016-05-27 10:06:40 +0000326} // namespace llvm
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000327
328// Node allocation functions.
329//
330// Node allocator is like a slab memory allocator: it allocates blocks of
331// memory in sizes that are multiples of the size of a node. Each block has
332// the same size. Nodes are allocated from the currently active block, and
333// when it becomes full, a new one is created.
334// There is a mapping scheme between node id and its location in a block,
335// and within that block is described in the header file.
336//
337void NodeAllocator::startNewBlock() {
338 void *T = MemPool.Allocate(NodesPerBlock*NodeMemSize, NodeMemSize);
339 char *P = static_cast<char*>(T);
340 Blocks.push_back(P);
341 // Check if the block index is still within the allowed range, i.e. less
342 // than 2^N, where N is the number of bits in NodeId for the block index.
343 // BitsPerIndex is the number of bits per node index.
Simon Pilgrim99c6c292016-01-18 21:11:19 +0000344 assert((Blocks.size() < ((size_t)1 << (8*sizeof(NodeId)-BitsPerIndex))) &&
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000345 "Out of bits for block index");
346 ActiveEnd = P;
347}
348
349bool NodeAllocator::needNewBlock() {
350 if (Blocks.empty())
351 return true;
352
353 char *ActiveBegin = Blocks.back();
354 uint32_t Index = (ActiveEnd-ActiveBegin)/NodeMemSize;
355 return Index >= NodesPerBlock;
356}
357
358NodeAddr<NodeBase*> NodeAllocator::New() {
359 if (needNewBlock())
360 startNewBlock();
361
362 uint32_t ActiveB = Blocks.size()-1;
363 uint32_t Index = (ActiveEnd - Blocks[ActiveB])/NodeMemSize;
364 NodeAddr<NodeBase*> NA = { reinterpret_cast<NodeBase*>(ActiveEnd),
365 makeId(ActiveB, Index) };
366 ActiveEnd += NodeMemSize;
367 return NA;
368}
369
370NodeId NodeAllocator::id(const NodeBase *P) const {
371 uintptr_t A = reinterpret_cast<uintptr_t>(P);
372 for (unsigned i = 0, n = Blocks.size(); i != n; ++i) {
373 uintptr_t B = reinterpret_cast<uintptr_t>(Blocks[i]);
374 if (A < B || A >= B + NodesPerBlock*NodeMemSize)
375 continue;
376 uint32_t Idx = (A-B)/NodeMemSize;
377 return makeId(i, Idx);
378 }
379 llvm_unreachable("Invalid node address");
380}
381
382void NodeAllocator::clear() {
383 MemPool.Reset();
384 Blocks.clear();
385 ActiveEnd = nullptr;
386}
387
388
389// Insert node NA after "this" in the circular chain.
390void NodeBase::append(NodeAddr<NodeBase*> NA) {
391 NodeId Nx = Next;
392 // If NA is already "next", do nothing.
393 if (Next != NA.Id) {
394 Next = NA.Id;
395 NA.Addr->Next = Nx;
396 }
397}
398
399
400// Fundamental node manipulator functions.
401
402// Obtain the register reference from a reference node.
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000403RegisterRef RefNode::getRegRef(const DataFlowGraph &G) const {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000404 assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
405 if (NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000406 return G.unpack(Ref.PR);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000407 assert(Ref.Op != nullptr);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000408 return G.makeRegRef(Ref.Op->getReg(), Ref.Op->getSubReg());
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000409}
410
411// Set the register reference in the reference node directly (for references
412// in phi nodes).
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000413void RefNode::setRegRef(RegisterRef RR, DataFlowGraph &G) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000414 assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
415 assert(NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000416 Ref.PR = G.pack(RR);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000417}
418
419// Set the register reference in the reference node based on a machine
420// operand (for references in statement nodes).
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000421void RefNode::setRegRef(MachineOperand *Op, DataFlowGraph &G) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000422 assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
423 assert(!(NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef));
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000424 (void)G;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000425 Ref.Op = Op;
426}
427
428// Get the owner of a given reference node.
429NodeAddr<NodeBase*> RefNode::getOwner(const DataFlowGraph &G) {
430 NodeAddr<NodeBase*> NA = G.addr<NodeBase*>(getNext());
431
432 while (NA.Addr != this) {
433 if (NA.Addr->getType() == NodeAttrs::Code)
434 return NA;
435 NA = G.addr<NodeBase*>(NA.Addr->getNext());
436 }
437 llvm_unreachable("No owner in circular list");
438}
439
440// Connect the def node to the reaching def node.
441void DefNode::linkToDef(NodeId Self, NodeAddr<DefNode*> DA) {
442 Ref.RD = DA.Id;
443 Ref.Sib = DA.Addr->getReachedDef();
444 DA.Addr->setReachedDef(Self);
445}
446
447// Connect the use node to the reaching def node.
448void UseNode::linkToDef(NodeId Self, NodeAddr<DefNode*> DA) {
449 Ref.RD = DA.Id;
450 Ref.Sib = DA.Addr->getReachedUse();
451 DA.Addr->setReachedUse(Self);
452}
453
454// Get the first member of the code node.
455NodeAddr<NodeBase*> CodeNode::getFirstMember(const DataFlowGraph &G) const {
456 if (Code.FirstM == 0)
457 return NodeAddr<NodeBase*>();
458 return G.addr<NodeBase*>(Code.FirstM);
459}
460
461// Get the last member of the code node.
462NodeAddr<NodeBase*> CodeNode::getLastMember(const DataFlowGraph &G) const {
463 if (Code.LastM == 0)
464 return NodeAddr<NodeBase*>();
465 return G.addr<NodeBase*>(Code.LastM);
466}
467
468// Add node NA at the end of the member list of the given code node.
469void CodeNode::addMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000470 NodeAddr<NodeBase*> ML = getLastMember(G);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000471 if (ML.Id != 0) {
472 ML.Addr->append(NA);
473 } else {
474 Code.FirstM = NA.Id;
475 NodeId Self = G.id(this);
476 NA.Addr->setNext(Self);
477 }
478 Code.LastM = NA.Id;
479}
480
481// Add node NA after member node MA in the given code node.
482void CodeNode::addMemberAfter(NodeAddr<NodeBase*> MA, NodeAddr<NodeBase*> NA,
483 const DataFlowGraph &G) {
484 MA.Addr->append(NA);
485 if (Code.LastM == MA.Id)
486 Code.LastM = NA.Id;
487}
488
489// Remove member node NA from the given code node.
490void CodeNode::removeMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000491 NodeAddr<NodeBase*> MA = getFirstMember(G);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000492 assert(MA.Id != 0);
493
494 // Special handling if the member to remove is the first member.
495 if (MA.Id == NA.Id) {
496 if (Code.LastM == MA.Id) {
497 // If it is the only member, set both first and last to 0.
498 Code.FirstM = Code.LastM = 0;
499 } else {
500 // Otherwise, advance the first member.
501 Code.FirstM = MA.Addr->getNext();
502 }
503 return;
504 }
505
506 while (MA.Addr != this) {
507 NodeId MX = MA.Addr->getNext();
508 if (MX == NA.Id) {
509 MA.Addr->setNext(NA.Addr->getNext());
510 // If the member to remove happens to be the last one, update the
511 // LastM indicator.
512 if (Code.LastM == NA.Id)
513 Code.LastM = MA.Id;
514 return;
515 }
516 MA = G.addr<NodeBase*>(MX);
517 }
518 llvm_unreachable("No such member");
519}
520
521// Return the list of all members of the code node.
522NodeList CodeNode::members(const DataFlowGraph &G) const {
523 static auto True = [] (NodeAddr<NodeBase*>) -> bool { return true; };
524 return members_if(True, G);
525}
526
527// Return the owner of the given instr node.
528NodeAddr<NodeBase*> InstrNode::getOwner(const DataFlowGraph &G) {
529 NodeAddr<NodeBase*> NA = G.addr<NodeBase*>(getNext());
530
531 while (NA.Addr != this) {
532 assert(NA.Addr->getType() == NodeAttrs::Code);
533 if (NA.Addr->getKind() == NodeAttrs::Block)
534 return NA;
535 NA = G.addr<NodeBase*>(NA.Addr->getNext());
536 }
537 llvm_unreachable("No owner in circular list");
538}
539
540// Add the phi node PA to the given block node.
541void BlockNode::addPhi(NodeAddr<PhiNode*> PA, const DataFlowGraph &G) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000542 NodeAddr<NodeBase*> M = getFirstMember(G);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000543 if (M.Id == 0) {
544 addMember(PA, G);
545 return;
546 }
547
548 assert(M.Addr->getType() == NodeAttrs::Code);
549 if (M.Addr->getKind() == NodeAttrs::Stmt) {
550 // If the first member of the block is a statement, insert the phi as
551 // the first member.
552 Code.FirstM = PA.Id;
553 PA.Addr->setNext(M.Id);
554 } else {
555 // If the first member is a phi, find the last phi, and append PA to it.
556 assert(M.Addr->getKind() == NodeAttrs::Phi);
557 NodeAddr<NodeBase*> MN = M;
558 do {
559 M = MN;
560 MN = G.addr<NodeBase*>(M.Addr->getNext());
561 assert(MN.Addr->getType() == NodeAttrs::Code);
562 } while (MN.Addr->getKind() == NodeAttrs::Phi);
563
564 // M is the last phi.
565 addMemberAfter(M, PA, G);
566 }
567}
568
569// Find the block node corresponding to the machine basic block BB in the
570// given func node.
571NodeAddr<BlockNode*> FuncNode::findBlock(const MachineBasicBlock *BB,
572 const DataFlowGraph &G) const {
573 auto EqBB = [BB] (NodeAddr<NodeBase*> NA) -> bool {
574 return NodeAddr<BlockNode*>(NA).Addr->getCode() == BB;
575 };
576 NodeList Ms = members_if(EqBB, G);
577 if (!Ms.empty())
578 return Ms[0];
579 return NodeAddr<BlockNode*>();
580}
581
582// Get the block node for the entry block in the given function.
583NodeAddr<BlockNode*> FuncNode::getEntryBlock(const DataFlowGraph &G) {
584 MachineBasicBlock *EntryB = &getCode()->front();
585 return findBlock(EntryB, G);
586}
587
588
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000589// Target operand information.
590//
591
592// For a given instruction, check if there are any bits of RR that can remain
593// unchanged across this def.
594bool TargetOperandInfo::isPreserving(const MachineInstr &In, unsigned OpNum)
595 const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000596 return TII.isPredicated(In);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000597}
598
599// Check if the definition of RR produces an unspecified value.
600bool TargetOperandInfo::isClobbering(const MachineInstr &In, unsigned OpNum)
601 const {
602 if (In.isCall())
603 if (In.getOperand(OpNum).isImplicit())
604 return true;
605 return false;
606}
607
Krzysztof Parzyszekc5a4e262016-04-28 20:33:33 +0000608// Check if the given instruction specifically requires
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000609bool TargetOperandInfo::isFixedReg(const MachineInstr &In, unsigned OpNum)
610 const {
Krzysztof Parzyszekc5a4e262016-04-28 20:33:33 +0000611 if (In.isCall() || In.isReturn() || In.isInlineAsm())
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000612 return true;
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +0000613 // Check for a tail call.
614 if (In.isBranch())
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000615 for (const MachineOperand &O : In.operands())
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +0000616 if (O.isGlobal() || O.isSymbol())
617 return true;
618
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000619 const MCInstrDesc &D = In.getDesc();
620 if (!D.getImplicitDefs() && !D.getImplicitUses())
621 return false;
622 const MachineOperand &Op = In.getOperand(OpNum);
623 // If there is a sub-register, treat the operand as non-fixed. Currently,
624 // fixed registers are those that are listed in the descriptor as implicit
625 // uses or defs, and those lists do not allow sub-registers.
626 if (Op.getSubReg() != 0)
627 return false;
Krzysztof Parzyszekc51f2392016-09-22 20:56:39 +0000628 uint32_t Reg = Op.getReg();
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000629 const MCPhysReg *ImpR = Op.isDef() ? D.getImplicitDefs()
630 : D.getImplicitUses();
631 if (!ImpR)
632 return false;
633 while (*ImpR)
634 if (*ImpR++ == Reg)
635 return true;
636 return false;
637}
638
639
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000640RegisterRef RegisterAggr::normalize(RegisterRef RR) const {
641 uint32_t SuperReg = RR.Reg;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000642 while (true) {
643 MCSuperRegIterator SR(SuperReg, &TRI, false);
644 if (!SR.isValid())
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000645 break;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000646 SuperReg = *SR;
647 }
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000648
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000649 uint32_t Sub = TRI.getSubRegIndex(SuperReg, RR.Reg);
650 const TargetRegisterClass &RC = *TRI.getMinimalPhysRegClass(RR.Reg);
651 LaneBitmask SuperMask = RR.Mask &
652 TRI.composeSubRegIndexLaneMask(Sub, RC.LaneMask);
653 return RegisterRef(SuperReg, SuperMask);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000654}
655
656bool RegisterAggr::hasAliasOf(RegisterRef RR) const {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000657 RegisterRef NR = normalize(RR);
658 auto F = Masks.find(NR.Reg);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000659 if (F != Masks.end()) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000660 if (F->second & NR.Mask)
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000661 return true;
662 }
663 if (CheckUnits) {
664 for (MCRegUnitIterator U(RR.Reg, &TRI); U.isValid(); ++U)
665 if (ExpAliasUnits.test(*U))
666 return true;
667 }
668 return false;
669}
670
671bool RegisterAggr::hasCoverOf(RegisterRef RR) const {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000672 // Always have a cover for empty lane mask.
673 RegisterRef NR = normalize(RR);
674 if (!NR.Mask)
675 return true;
676 auto F = Masks.find(NR.Reg);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000677 if (F == Masks.end())
678 return false;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000679 return (NR.Mask & F->second) == NR.Mask;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000680}
681
682RegisterAggr &RegisterAggr::insert(RegisterRef RR) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000683 RegisterRef NR = normalize(RR);
684 auto F = Masks.find(NR.Reg);
685 if (F == Masks.end())
686 Masks.insert({NR.Reg, NR.Mask});
687 else
688 F->second |= NR.Mask;
689
690 // Visit all register units to see if there are any that were created
691 // by explicit aliases. Add those that were to the bit vector.
692 for (MCRegUnitIterator U(RR.Reg, &TRI); U.isValid(); ++U) {
693 MCRegUnitRootIterator R(*U, &TRI);
694 ++R;
695 if (!R.isValid())
696 continue;
697 ExpAliasUnits.set(*U);
698 CheckUnits = true;
699 }
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000700 return *this;
701}
702
703RegisterAggr &RegisterAggr::insert(const RegisterAggr &RG) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000704 for (std::pair<uint32_t,LaneBitmask> P : RG.Masks)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000705 insert(RegisterRef(P.first, P.second));
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000706 return *this;
707}
708
709RegisterAggr &RegisterAggr::clear(RegisterRef RR) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000710 RegisterRef NR = normalize(RR);
711 auto F = Masks.find(NR.Reg);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000712 if (F == Masks.end())
713 return *this;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000714 LaneBitmask NewM = F->second & ~NR.Mask;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000715 if (NewM == LaneBitmask(0))
716 Masks.erase(F);
717 else
718 F->second = NewM;
719 return *this;
720}
721
722void RegisterAggr::print(raw_ostream &OS) const {
723 OS << '{';
724 for (auto I : Masks)
725 OS << ' ' << PrintReg(I.first, &TRI) << ':' << PrintLaneMask(I.second);
726 OS << " }";
727}
728
729
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000730//
731// The data flow graph construction.
732//
733
734DataFlowGraph::DataFlowGraph(MachineFunction &mf, const TargetInstrInfo &tii,
735 const TargetRegisterInfo &tri, const MachineDominatorTree &mdt,
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000736 const MachineDominanceFrontier &mdf, const TargetOperandInfo &toi)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000737 : TimeG("rdf"), LMI(), MF(mf), TII(tii), TRI(tri), MDT(mdt), MDF(mdf),
738 TOI(toi) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000739}
740
741
742// The implementation of the definition stack.
743// Each register reference has its own definition stack. In particular,
744// for a register references "Reg" and "Reg:subreg" will each have their
745// own definition stacks.
746
747// Construct a stack iterator.
748DataFlowGraph::DefStack::Iterator::Iterator(const DataFlowGraph::DefStack &S,
749 bool Top) : DS(S) {
750 if (!Top) {
751 // Initialize to bottom.
752 Pos = 0;
753 return;
754 }
755 // Initialize to the top, i.e. top-most non-delimiter (or 0, if empty).
756 Pos = DS.Stack.size();
757 while (Pos > 0 && DS.isDelimiter(DS.Stack[Pos-1]))
758 Pos--;
759}
760
761// Return the size of the stack, including block delimiters.
762unsigned DataFlowGraph::DefStack::size() const {
763 unsigned S = 0;
764 for (auto I = top(), E = bottom(); I != E; I.down())
765 S++;
766 return S;
767}
768
769// Remove the top entry from the stack. Remove all intervening delimiters
770// so that after this, the stack is either empty, or the top of the stack
771// is a non-delimiter.
772void DataFlowGraph::DefStack::pop() {
773 assert(!empty());
774 unsigned P = nextDown(Stack.size());
775 Stack.resize(P);
776}
777
778// Push a delimiter for block node N on the stack.
779void DataFlowGraph::DefStack::start_block(NodeId N) {
780 assert(N != 0);
781 Stack.push_back(NodeAddr<DefNode*>(nullptr, N));
782}
783
784// Remove all nodes from the top of the stack, until the delimited for
785// block node N is encountered. Remove the delimiter as well. In effect,
786// this will remove from the stack all definitions from block N.
787void DataFlowGraph::DefStack::clear_block(NodeId N) {
788 assert(N != 0);
789 unsigned P = Stack.size();
790 while (P > 0) {
791 bool Found = isDelimiter(Stack[P-1], N);
792 P--;
793 if (Found)
794 break;
795 }
796 // This will also remove the delimiter, if found.
797 Stack.resize(P);
798}
799
800// Move the stack iterator up by one.
801unsigned DataFlowGraph::DefStack::nextUp(unsigned P) const {
802 // Get the next valid position after P (skipping all delimiters).
803 // The input position P does not have to point to a non-delimiter.
804 unsigned SS = Stack.size();
805 bool IsDelim;
Krzysztof Parzyszek8dca45e2016-01-12 16:51:55 +0000806 assert(P < SS);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000807 do {
808 P++;
809 IsDelim = isDelimiter(Stack[P-1]);
810 } while (P < SS && IsDelim);
811 assert(!IsDelim);
812 return P;
813}
814
815// Move the stack iterator down by one.
816unsigned DataFlowGraph::DefStack::nextDown(unsigned P) const {
817 // Get the preceding valid position before P (skipping all delimiters).
818 // The input position P does not have to point to a non-delimiter.
819 assert(P > 0 && P <= Stack.size());
820 bool IsDelim = isDelimiter(Stack[P-1]);
821 do {
822 if (--P == 0)
823 break;
824 IsDelim = isDelimiter(Stack[P-1]);
825 } while (P > 0 && IsDelim);
826 assert(!IsDelim);
827 return P;
828}
829
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000830
831// Register information.
832
833// Get the list of references aliased to RR. Lane masks are ignored.
834RegisterSet DataFlowGraph::getAliasSet(uint32_t Reg) const {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000835 // Do not include RR in the alias set.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000836 RegisterSet AS;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000837 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
838
839 for (MCRegAliasIterator AI(Reg, &TRI, false); AI.isValid(); ++AI)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000840 AS.insert(RegisterRef(*AI));
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000841 return AS;
842}
843
844RegisterSet DataFlowGraph::getLandingPadLiveIns() const {
845 RegisterSet LR;
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +0000846 const Function &F = *MF.getFunction();
847 const Constant *PF = F.hasPersonalityFn() ? F.getPersonalityFn()
848 : nullptr;
849 const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering();
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000850 if (uint32_t R = TLI.getExceptionPointerRegister(PF))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000851 LR.insert(RegisterRef(R));
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000852 if (uint32_t R = TLI.getExceptionSelectorRegister(PF))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000853 LR.insert(RegisterRef(R));
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +0000854 return LR;
855}
856
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000857// Node management functions.
858
859// Get the pointer to the node with the id N.
860NodeBase *DataFlowGraph::ptr(NodeId N) const {
861 if (N == 0)
862 return nullptr;
863 return Memory.ptr(N);
864}
865
866// Get the id of the node at the address P.
867NodeId DataFlowGraph::id(const NodeBase *P) const {
868 if (P == nullptr)
869 return 0;
870 return Memory.id(P);
871}
872
873// Allocate a new node and set the attributes to Attrs.
874NodeAddr<NodeBase*> DataFlowGraph::newNode(uint16_t Attrs) {
875 NodeAddr<NodeBase*> P = Memory.New();
876 P.Addr->init();
877 P.Addr->setAttrs(Attrs);
878 return P;
879}
880
881// Make a copy of the given node B, except for the data-flow links, which
882// are set to 0.
883NodeAddr<NodeBase*> DataFlowGraph::cloneNode(const NodeAddr<NodeBase*> B) {
884 NodeAddr<NodeBase*> NA = newNode(0);
885 memcpy(NA.Addr, B.Addr, sizeof(NodeBase));
886 // Ref nodes need to have the data-flow links reset.
887 if (NA.Addr->getType() == NodeAttrs::Ref) {
888 NodeAddr<RefNode*> RA = NA;
889 RA.Addr->setReachingDef(0);
890 RA.Addr->setSibling(0);
891 if (NA.Addr->getKind() == NodeAttrs::Def) {
892 NodeAddr<DefNode*> DA = NA;
893 DA.Addr->setReachedDef(0);
894 DA.Addr->setReachedUse(0);
895 }
896 }
897 return NA;
898}
899
900
901// Allocation routines for specific node types/kinds.
902
903NodeAddr<UseNode*> DataFlowGraph::newUse(NodeAddr<InstrNode*> Owner,
904 MachineOperand &Op, uint16_t Flags) {
905 NodeAddr<UseNode*> UA = newNode(NodeAttrs::Ref | NodeAttrs::Use | Flags);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000906 UA.Addr->setRegRef(&Op, *this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000907 return UA;
908}
909
910NodeAddr<PhiUseNode*> DataFlowGraph::newPhiUse(NodeAddr<PhiNode*> Owner,
911 RegisterRef RR, NodeAddr<BlockNode*> PredB, uint16_t Flags) {
912 NodeAddr<PhiUseNode*> PUA = newNode(NodeAttrs::Ref | NodeAttrs::Use | Flags);
913 assert(Flags & NodeAttrs::PhiRef);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000914 PUA.Addr->setRegRef(RR, *this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000915 PUA.Addr->setPredecessor(PredB.Id);
916 return PUA;
917}
918
919NodeAddr<DefNode*> DataFlowGraph::newDef(NodeAddr<InstrNode*> Owner,
920 MachineOperand &Op, uint16_t Flags) {
921 NodeAddr<DefNode*> DA = newNode(NodeAttrs::Ref | NodeAttrs::Def | Flags);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000922 DA.Addr->setRegRef(&Op, *this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000923 return DA;
924}
925
926NodeAddr<DefNode*> DataFlowGraph::newDef(NodeAddr<InstrNode*> Owner,
927 RegisterRef RR, uint16_t Flags) {
928 NodeAddr<DefNode*> DA = newNode(NodeAttrs::Ref | NodeAttrs::Def | Flags);
929 assert(Flags & NodeAttrs::PhiRef);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000930 DA.Addr->setRegRef(RR, *this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000931 return DA;
932}
933
934NodeAddr<PhiNode*> DataFlowGraph::newPhi(NodeAddr<BlockNode*> Owner) {
935 NodeAddr<PhiNode*> PA = newNode(NodeAttrs::Code | NodeAttrs::Phi);
936 Owner.Addr->addPhi(PA, *this);
937 return PA;
938}
939
940NodeAddr<StmtNode*> DataFlowGraph::newStmt(NodeAddr<BlockNode*> Owner,
941 MachineInstr *MI) {
942 NodeAddr<StmtNode*> SA = newNode(NodeAttrs::Code | NodeAttrs::Stmt);
943 SA.Addr->setCode(MI);
944 Owner.Addr->addMember(SA, *this);
945 return SA;
946}
947
948NodeAddr<BlockNode*> DataFlowGraph::newBlock(NodeAddr<FuncNode*> Owner,
949 MachineBasicBlock *BB) {
950 NodeAddr<BlockNode*> BA = newNode(NodeAttrs::Code | NodeAttrs::Block);
951 BA.Addr->setCode(BB);
952 Owner.Addr->addMember(BA, *this);
953 return BA;
954}
955
956NodeAddr<FuncNode*> DataFlowGraph::newFunc(MachineFunction *MF) {
957 NodeAddr<FuncNode*> FA = newNode(NodeAttrs::Code | NodeAttrs::Func);
958 FA.Addr->setCode(MF);
959 return FA;
960}
961
962// Build the data flow graph.
Krzysztof Parzyszek55874cf2016-04-28 20:17:06 +0000963void DataFlowGraph::build(unsigned Options) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000964 reset();
965 Func = newFunc(&MF);
966
967 if (MF.empty())
968 return;
969
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000970 for (MachineBasicBlock &B : MF) {
971 NodeAddr<BlockNode*> BA = newBlock(Func, &B);
Krzysztof Parzyszek047149f2016-07-22 16:09:47 +0000972 BlockNodes.insert(std::make_pair(&B, BA));
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000973 for (MachineInstr &I : B) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000974 if (I.isDebugValue())
975 continue;
976 buildStmt(BA, I);
977 }
978 }
979
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000980 NodeAddr<BlockNode*> EA = Func.Addr->getEntryBlock(*this);
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +0000981 NodeList Blocks = Func.Addr->members(*this);
982
983 // Collect information about block references.
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000984 BlockRefsMap RefM;
985 buildBlockRefs(EA, RefM);
986
987 // Add function-entry phi nodes.
988 MachineRegisterInfo &MRI = MF.getRegInfo();
989 for (auto I = MRI.livein_begin(), E = MRI.livein_end(); I != E; ++I) {
990 NodeAddr<PhiNode*> PA = newPhi(EA);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000991 RegisterRef RR = RegisterRef(I->first);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000992 uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving;
993 NodeAddr<DefNode*> DA = newDef(PA, RR, PhiFlags);
994 PA.Addr->addMember(DA, *this);
995 }
996
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +0000997 // Add phis for landing pads.
998 // Landing pads, unlike usual backs blocks, are not entered through
999 // branches in the program, or fall-throughs from other blocks. They
1000 // are entered from the exception handling runtime and target's ABI
1001 // may define certain registers as defined on entry to such a block.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001002 RegisterSet EHRegs = getLandingPadLiveIns();
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001003 if (!EHRegs.empty()) {
1004 for (NodeAddr<BlockNode*> BA : Blocks) {
1005 const MachineBasicBlock &B = *BA.Addr->getCode();
1006 if (!B.isEHPad())
1007 continue;
1008
1009 // Prepare a list of NodeIds of the block's predecessors.
1010 NodeList Preds;
1011 for (MachineBasicBlock *PB : B.predecessors())
1012 Preds.push_back(findBlock(PB));
1013
1014 // Build phi nodes for each live-in.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001015 for (RegisterRef RR : EHRegs) {
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001016 NodeAddr<PhiNode*> PA = newPhi(BA);
1017 uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving;
1018 // Add def:
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001019 NodeAddr<DefNode*> DA = newDef(PA, RR, PhiFlags);
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001020 PA.Addr->addMember(DA, *this);
1021 // Add uses (no reaching defs for phi uses):
1022 for (NodeAddr<BlockNode*> PBA : Preds) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001023 NodeAddr<PhiUseNode*> PUA = newPhiUse(PA, RR, PBA);
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001024 PA.Addr->addMember(PUA, *this);
1025 }
1026 }
1027 }
1028 }
1029
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001030 // Build a map "PhiM" which will contain, for each block, the set
1031 // of references that will require phi definitions in that block.
1032 BlockRefsMap PhiM;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001033 for (NodeAddr<BlockNode*> BA : Blocks)
1034 recordDefsForDF(PhiM, RefM, BA);
1035 for (NodeAddr<BlockNode*> BA : Blocks)
1036 buildPhis(PhiM, RefM, BA);
1037
1038 // Link all the refs. This will recursively traverse the dominator tree.
1039 DefStackMap DM;
1040 linkBlockRefs(DM, EA);
1041
1042 // Finally, remove all unused phi nodes.
Krzysztof Parzyszek55874cf2016-04-28 20:17:06 +00001043 if (!(Options & BuildOptions::KeepDeadPhis))
1044 removeUnusedPhis();
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001045}
1046
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001047RegisterRef DataFlowGraph::makeRegRef(unsigned Reg, unsigned Sub) const {
1048 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
Krzysztof Parzyszek775a2092016-10-14 19:06:25 +00001049 if (Sub != 0)
1050 Reg = TRI.getSubReg(Reg, Sub);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001051 return RegisterRef(Reg);
1052}
1053
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001054// For each stack in the map DefM, push the delimiter for block B on it.
1055void DataFlowGraph::markBlock(NodeId B, DefStackMap &DefM) {
1056 // Push block delimiters.
1057 for (auto I = DefM.begin(), E = DefM.end(); I != E; ++I)
1058 I->second.start_block(B);
1059}
1060
1061// Remove all definitions coming from block B from each stack in DefM.
1062void DataFlowGraph::releaseBlock(NodeId B, DefStackMap &DefM) {
1063 // Pop all defs from this block from the definition stack. Defs that were
1064 // added to the map during the traversal of instructions will not have a
1065 // delimiter, but for those, the whole stack will be emptied.
1066 for (auto I = DefM.begin(), E = DefM.end(); I != E; ++I)
1067 I->second.clear_block(B);
1068
1069 // Finally, remove empty stacks from the map.
1070 for (auto I = DefM.begin(), E = DefM.end(), NextI = I; I != E; I = NextI) {
1071 NextI = std::next(I);
1072 // This preserves the validity of iterators other than I.
1073 if (I->second.empty())
1074 DefM.erase(I);
1075 }
1076}
1077
1078// Push all definitions from the instruction node IA to an appropriate
1079// stack in DefM.
1080void DataFlowGraph::pushDefs(NodeAddr<InstrNode*> IA, DefStackMap &DefM) {
1081 NodeList Defs = IA.Addr->members_if(IsDef, *this);
1082 NodeSet Visited;
1083#ifndef NDEBUG
1084 RegisterSet Defined;
1085#endif
1086
1087 // The important objectives of this function are:
1088 // - to be able to handle instructions both while the graph is being
1089 // constructed, and after the graph has been constructed, and
1090 // - maintain proper ordering of definitions on the stack for each
1091 // register reference:
1092 // - if there are two or more related defs in IA (i.e. coming from
1093 // the same machine operand), then only push one def on the stack,
1094 // - if there are multiple unrelated defs of non-overlapping
1095 // subregisters of S, then the stack for S will have both (in an
1096 // unspecified order), but the order does not matter from the data-
1097 // -flow perspective.
1098
1099 for (NodeAddr<DefNode*> DA : Defs) {
1100 if (Visited.count(DA.Id))
1101 continue;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001102
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001103 NodeList Rel = getRelatedRefs(IA, DA);
1104 NodeAddr<DefNode*> PDA = Rel.front();
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001105 RegisterRef RR = PDA.Addr->getRegRef(*this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001106#ifndef NDEBUG
1107 // Assert if the register is defined in two or more unrelated defs.
1108 // This could happen if there are two or more def operands defining it.
1109 if (!Defined.insert(RR).second) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001110 MachineInstr *MI = NodeAddr<StmtNode*>(IA).Addr->getCode();
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001111 dbgs() << "Multiple definitions of register: "
1112 << Print<RegisterRef>(RR, *this) << " in\n " << *MI
1113 << "in BB#" << MI->getParent()->getNumber() << '\n';
1114 llvm_unreachable(nullptr);
1115 }
1116#endif
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001117 // Push the definition on the stack for the register and all aliases.
1118 // The def stack traversal in linkNodeUp will check the exact aliasing.
1119 DefM[RR.Reg].push(DA);
1120 for (RegisterRef A : getAliasSet(RR.Reg /*FIXME? use RegisterRef*/)) {
1121 // Check that we don't push the same def twice.
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001122 assert(A != RR);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001123 DefM[A.Reg].push(DA);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001124 }
1125 // Mark all the related defs as visited.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001126 for (NodeAddr<NodeBase*> T : Rel)
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001127 Visited.insert(T.Id);
1128 }
1129}
1130
1131// Return the list of all reference nodes related to RA, including RA itself.
1132// See "getNextRelated" for the meaning of a "related reference".
1133NodeList DataFlowGraph::getRelatedRefs(NodeAddr<InstrNode*> IA,
1134 NodeAddr<RefNode*> RA) const {
1135 assert(IA.Id != 0 && RA.Id != 0);
1136
1137 NodeList Refs;
1138 NodeId Start = RA.Id;
1139 do {
1140 Refs.push_back(RA);
1141 RA = getNextRelated(IA, RA);
1142 } while (RA.Id != 0 && RA.Id != Start);
1143 return Refs;
1144}
1145
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001146// Return true if RA and RB overlap, false otherwise.
1147bool DataFlowGraph::alias(RegisterRef RA, RegisterRef RB) const {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001148 assert(TargetRegisterInfo::isPhysicalRegister(RA.Reg));
1149 assert(TargetRegisterInfo::isPhysicalRegister(RB.Reg));
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001150
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001151 MCRegUnitMaskIterator UMA(RA.Reg, &TRI);
1152 MCRegUnitMaskIterator UMB(RB.Reg, &TRI);
1153 // Reg units are returned in the numerical order.
1154 while (UMA.isValid() && UMB.isValid()) {
1155 std::pair<uint32_t,LaneBitmask> PA = *UMA;
1156 std::pair<uint32_t,LaneBitmask> PB = *UMB;
1157 // If the returned lane mask is 0, it should be treated as ~0
1158 // (or the lane mask from the given register ref should be ignored).
1159 // This can happen when a register has only one unit.
1160 if (PA.first == PB.first) {
1161 if (!PA.second || !PB.second || (PA.second & PB.second))
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001162 return true;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001163 ++UMA;
1164 ++UMB;
1165 continue;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001166 }
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001167 if (PA.first < PB.first)
1168 ++UMA;
1169 else if (PB.first < PA.first)
1170 ++UMB;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001171 }
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001172 return false;
1173}
1174
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001175
1176// Clear all information in the graph.
1177void DataFlowGraph::reset() {
1178 Memory.clear();
Krzysztof Parzyszek047149f2016-07-22 16:09:47 +00001179 BlockNodes.clear();
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001180 Func = NodeAddr<FuncNode*>();
1181}
1182
1183
1184// Return the next reference node in the instruction node IA that is related
1185// to RA. Conceptually, two reference nodes are related if they refer to the
1186// same instance of a register access, but differ in flags or other minor
1187// characteristics. Specific examples of related nodes are shadow reference
1188// nodes.
1189// Return the equivalent of nullptr if there are no more related references.
1190NodeAddr<RefNode*> DataFlowGraph::getNextRelated(NodeAddr<InstrNode*> IA,
1191 NodeAddr<RefNode*> RA) const {
1192 assert(IA.Id != 0 && RA.Id != 0);
1193
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001194 auto Related = [this,RA](NodeAddr<RefNode*> TA) -> bool {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001195 if (TA.Addr->getKind() != RA.Addr->getKind())
1196 return false;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001197 if (TA.Addr->getRegRef(*this) != RA.Addr->getRegRef(*this))
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001198 return false;
1199 return true;
1200 };
1201 auto RelatedStmt = [&Related,RA](NodeAddr<RefNode*> TA) -> bool {
1202 return Related(TA) &&
1203 &RA.Addr->getOp() == &TA.Addr->getOp();
1204 };
1205 auto RelatedPhi = [&Related,RA](NodeAddr<RefNode*> TA) -> bool {
1206 if (!Related(TA))
1207 return false;
1208 if (TA.Addr->getKind() != NodeAttrs::Use)
1209 return true;
1210 // For phi uses, compare predecessor blocks.
1211 const NodeAddr<const PhiUseNode*> TUA = TA;
1212 const NodeAddr<const PhiUseNode*> RUA = RA;
1213 return TUA.Addr->getPredecessor() == RUA.Addr->getPredecessor();
1214 };
1215
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001216 RegisterRef RR = RA.Addr->getRegRef(*this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001217 if (IA.Addr->getKind() == NodeAttrs::Stmt)
1218 return RA.Addr->getNextRef(RR, RelatedStmt, true, *this);
1219 return RA.Addr->getNextRef(RR, RelatedPhi, true, *this);
1220}
1221
1222// Find the next node related to RA in IA that satisfies condition P.
1223// If such a node was found, return a pair where the second element is the
1224// located node. If such a node does not exist, return a pair where the
1225// first element is the element after which such a node should be inserted,
1226// and the second element is a null-address.
1227template <typename Predicate>
1228std::pair<NodeAddr<RefNode*>,NodeAddr<RefNode*>>
1229DataFlowGraph::locateNextRef(NodeAddr<InstrNode*> IA, NodeAddr<RefNode*> RA,
1230 Predicate P) const {
1231 assert(IA.Id != 0 && RA.Id != 0);
1232
1233 NodeAddr<RefNode*> NA;
1234 NodeId Start = RA.Id;
1235 while (true) {
1236 NA = getNextRelated(IA, RA);
1237 if (NA.Id == 0 || NA.Id == Start)
1238 break;
1239 if (P(NA))
1240 break;
1241 RA = NA;
1242 }
1243
1244 if (NA.Id != 0 && NA.Id != Start)
1245 return std::make_pair(RA, NA);
1246 return std::make_pair(RA, NodeAddr<RefNode*>());
1247}
1248
1249// Get the next shadow node in IA corresponding to RA, and optionally create
1250// such a node if it does not exist.
1251NodeAddr<RefNode*> DataFlowGraph::getNextShadow(NodeAddr<InstrNode*> IA,
1252 NodeAddr<RefNode*> RA, bool Create) {
1253 assert(IA.Id != 0 && RA.Id != 0);
1254
1255 uint16_t Flags = RA.Addr->getFlags() | NodeAttrs::Shadow;
1256 auto IsShadow = [Flags] (NodeAddr<RefNode*> TA) -> bool {
1257 return TA.Addr->getFlags() == Flags;
1258 };
1259 auto Loc = locateNextRef(IA, RA, IsShadow);
1260 if (Loc.second.Id != 0 || !Create)
1261 return Loc.second;
1262
1263 // Create a copy of RA and mark is as shadow.
1264 NodeAddr<RefNode*> NA = cloneNode(RA);
1265 NA.Addr->setFlags(Flags | NodeAttrs::Shadow);
1266 IA.Addr->addMemberAfter(Loc.first, NA, *this);
1267 return NA;
1268}
1269
1270// Get the next shadow node in IA corresponding to RA. Return null-address
1271// if such a node does not exist.
1272NodeAddr<RefNode*> DataFlowGraph::getNextShadow(NodeAddr<InstrNode*> IA,
1273 NodeAddr<RefNode*> RA) const {
1274 assert(IA.Id != 0 && RA.Id != 0);
1275 uint16_t Flags = RA.Addr->getFlags() | NodeAttrs::Shadow;
1276 auto IsShadow = [Flags] (NodeAddr<RefNode*> TA) -> bool {
1277 return TA.Addr->getFlags() == Flags;
1278 };
1279 return locateNextRef(IA, RA, IsShadow).second;
1280}
1281
1282// Create a new statement node in the block node BA that corresponds to
1283// the machine instruction MI.
1284void DataFlowGraph::buildStmt(NodeAddr<BlockNode*> BA, MachineInstr &In) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001285 NodeAddr<StmtNode*> SA = newStmt(BA, &In);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001286
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +00001287 auto isCall = [] (const MachineInstr &In) -> bool {
1288 if (In.isCall())
1289 return true;
1290 // Is tail call?
1291 if (In.isBranch())
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001292 for (const MachineOperand &Op : In.operands())
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +00001293 if (Op.isGlobal() || Op.isSymbol())
1294 return true;
1295 return false;
1296 };
1297
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001298 auto isDefUndef = [this] (const MachineInstr &In, RegisterRef DR) -> bool {
1299 // This instruction defines DR. Check if there is a use operand that
1300 // would make DR live on entry to the instruction.
1301 for (const MachineOperand &UseOp : In.operands()) {
1302 if (!UseOp.isReg() || !UseOp.isUse() || UseOp.isUndef())
1303 continue;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001304 RegisterRef UR = makeRegRef(UseOp.getReg(), UseOp.getSubReg());
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001305 if (alias(DR, UR))
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001306 return false;
1307 }
1308 return true;
1309 };
1310
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001311 // Collect a set of registers that this instruction implicitly uses
1312 // or defines. Implicit operands from an instruction will be ignored
1313 // unless they are listed here.
1314 RegisterSet ImpUses, ImpDefs;
1315 if (const uint16_t *ImpD = In.getDesc().getImplicitDefs())
1316 while (uint16_t R = *ImpD++)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001317 ImpDefs.insert(RegisterRef(R));
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001318 if (const uint16_t *ImpU = In.getDesc().getImplicitUses())
1319 while (uint16_t R = *ImpU++)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001320 ImpUses.insert(RegisterRef(R));
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001321
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +00001322 bool IsCall = isCall(In);
1323 bool NeedsImplicit = IsCall || In.isInlineAsm() || In.isReturn();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001324 bool IsPredicated = TII.isPredicated(In);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001325 unsigned NumOps = In.getNumOperands();
1326
1327 // Avoid duplicate implicit defs. This will not detect cases of implicit
1328 // defs that define registers that overlap, but it is not clear how to
1329 // interpret that in the absence of explicit defs. Overlapping explicit
1330 // defs are likely illegal already.
1331 RegisterSet DoneDefs;
1332 // Process explicit defs first.
1333 for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1334 MachineOperand &Op = In.getOperand(OpN);
1335 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1336 continue;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001337 RegisterRef RR = makeRegRef(Op.getReg(), Op.getSubReg());
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001338 uint16_t Flags = NodeAttrs::None;
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001339 if (TOI.isPreserving(In, OpN)) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001340 Flags |= NodeAttrs::Preserving;
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001341 // If the def is preserving, check if it is also undefined.
1342 if (isDefUndef(In, RR))
1343 Flags |= NodeAttrs::Undef;
1344 }
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001345 if (TOI.isClobbering(In, OpN))
1346 Flags |= NodeAttrs::Clobbering;
1347 if (TOI.isFixedReg(In, OpN))
1348 Flags |= NodeAttrs::Fixed;
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +00001349 if (IsCall && Op.isDead())
1350 Flags |= NodeAttrs::Dead;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001351 NodeAddr<DefNode*> DA = newDef(SA, Op, Flags);
1352 SA.Addr->addMember(DA, *this);
1353 DoneDefs.insert(RR);
1354 }
1355
1356 // Process implicit defs, skipping those that have already been added
1357 // as explicit.
1358 for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1359 MachineOperand &Op = In.getOperand(OpN);
1360 if (!Op.isReg() || !Op.isDef() || !Op.isImplicit())
1361 continue;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001362 RegisterRef RR = makeRegRef(Op.getReg(), Op.getSubReg());
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +00001363 if (!NeedsImplicit && !ImpDefs.count(RR))
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001364 continue;
1365 if (DoneDefs.count(RR))
1366 continue;
1367 uint16_t Flags = NodeAttrs::None;
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001368 if (TOI.isPreserving(In, OpN)) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001369 Flags |= NodeAttrs::Preserving;
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001370 // If the def is preserving, check if it is also undefined.
1371 if (isDefUndef(In, RR))
1372 Flags |= NodeAttrs::Undef;
1373 }
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001374 if (TOI.isClobbering(In, OpN))
1375 Flags |= NodeAttrs::Clobbering;
1376 if (TOI.isFixedReg(In, OpN))
1377 Flags |= NodeAttrs::Fixed;
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +00001378 if (IsCall && Op.isDead())
1379 Flags |= NodeAttrs::Dead;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001380 NodeAddr<DefNode*> DA = newDef(SA, Op, Flags);
1381 SA.Addr->addMember(DA, *this);
1382 DoneDefs.insert(RR);
1383 }
1384
1385 for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1386 MachineOperand &Op = In.getOperand(OpN);
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001387 if (!Op.isReg() || !Op.isUse())
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001388 continue;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001389 RegisterRef RR = makeRegRef(Op.getReg(), Op.getSubReg());
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001390 // Add implicit uses on return and call instructions, and on predicated
1391 // instructions regardless of whether or not they appear in the instruction
1392 // descriptor's list.
1393 bool Implicit = Op.isImplicit();
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +00001394 bool TakeImplicit = NeedsImplicit || IsPredicated;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001395 if (Implicit && !TakeImplicit && !ImpUses.count(RR))
1396 continue;
1397 uint16_t Flags = NodeAttrs::None;
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001398 if (Op.isUndef())
1399 Flags |= NodeAttrs::Undef;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001400 if (TOI.isFixedReg(In, OpN))
1401 Flags |= NodeAttrs::Fixed;
1402 NodeAddr<UseNode*> UA = newUse(SA, Op, Flags);
1403 SA.Addr->addMember(UA, *this);
1404 }
1405}
1406
1407// Build a map that for each block will have the set of all references from
1408// that block, and from all blocks dominated by it.
1409void DataFlowGraph::buildBlockRefs(NodeAddr<BlockNode*> BA,
1410 BlockRefsMap &RefM) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001411 RegisterSet &Refs = RefM[BA.Id];
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001412 MachineDomTreeNode *N = MDT.getNode(BA.Addr->getCode());
1413 assert(N);
1414 for (auto I : *N) {
1415 MachineBasicBlock *SB = I->getBlock();
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001416 NodeAddr<BlockNode*> SBA = findBlock(SB);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001417 buildBlockRefs(SBA, RefM);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001418 const RegisterSet &RefsS = RefM[SBA.Id];
1419 Refs.insert(RefsS.begin(), RefsS.end());
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001420 }
1421
1422 for (NodeAddr<InstrNode*> IA : BA.Addr->members(*this))
1423 for (NodeAddr<RefNode*> RA : IA.Addr->members(*this))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001424 Refs.insert(RA.Addr->getRegRef(*this));
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001425}
1426
1427// Scan all defs in the block node BA and record in PhiM the locations of
1428// phi nodes corresponding to these defs.
1429void DataFlowGraph::recordDefsForDF(BlockRefsMap &PhiM, BlockRefsMap &RefM,
1430 NodeAddr<BlockNode*> BA) {
1431 // Check all defs from block BA and record them in each block in BA's
1432 // iterated dominance frontier. This information will later be used to
1433 // create phi nodes.
1434 MachineBasicBlock *BB = BA.Addr->getCode();
1435 assert(BB);
1436 auto DFLoc = MDF.find(BB);
1437 if (DFLoc == MDF.end() || DFLoc->second.empty())
1438 return;
1439
1440 // Traverse all instructions in the block and collect the set of all
1441 // defined references. For each reference there will be a phi created
1442 // in the block's iterated dominance frontier.
1443 // This is done to make sure that each defined reference gets only one
1444 // phi node, even if it is defined multiple times.
1445 RegisterSet Defs;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001446 for (NodeAddr<InstrNode*> IA : BA.Addr->members(*this))
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001447 for (NodeAddr<RefNode*> RA : IA.Addr->members_if(IsDef, *this))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001448 Defs.insert(RA.Addr->getRegRef(*this));
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001449
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001450 // Calculate the iterated dominance frontier of BB.
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001451 const MachineDominanceFrontier::DomSetType &DF = DFLoc->second;
1452 SetVector<MachineBasicBlock*> IDF(DF.begin(), DF.end());
1453 for (unsigned i = 0; i < IDF.size(); ++i) {
1454 auto F = MDF.find(IDF[i]);
1455 if (F != MDF.end())
1456 IDF.insert(F->second.begin(), F->second.end());
1457 }
1458
1459 // Get the register references that are reachable from this block.
1460 RegisterSet &Refs = RefM[BA.Id];
1461 for (auto DB : IDF) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001462 NodeAddr<BlockNode*> DBA = findBlock(DB);
1463 const RegisterSet &RefsD = RefM[DBA.Id];
1464 Refs.insert(RefsD.begin(), RefsD.end());
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001465 }
1466
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001467 // Finally, add the set of defs to each block in the iterated dominance
1468 // frontier.
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001469 for (auto DB : IDF) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001470 NodeAddr<BlockNode*> DBA = findBlock(DB);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001471 PhiM[DBA.Id].insert(Defs.begin(), Defs.end());
1472 }
1473}
1474
1475// Given the locations of phi nodes in the map PhiM, create the phi nodes
1476// that are located in the block node BA.
1477void DataFlowGraph::buildPhis(BlockRefsMap &PhiM, BlockRefsMap &RefM,
1478 NodeAddr<BlockNode*> BA) {
1479 // Check if this blocks has any DF defs, i.e. if there are any defs
1480 // that this block is in the iterated dominance frontier of.
1481 auto HasDF = PhiM.find(BA.Id);
1482 if (HasDF == PhiM.end() || HasDF->second.empty())
1483 return;
1484
1485 // First, remove all R in Refs in such that there exists T in Refs
1486 // such that T covers R. In other words, only leave those refs that
1487 // are not covered by another ref (i.e. maximal with respect to covering).
1488
1489 auto MaxCoverIn = [this] (RegisterRef RR, RegisterSet &RRs) -> RegisterRef {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001490 for (RegisterRef I : RRs)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001491 if (I != RR && RegisterAggr::isCoverOf(I, RR, TRI))
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001492 RR = I;
1493 return RR;
1494 };
1495
1496 RegisterSet MaxDF;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001497 for (RegisterRef I : HasDF->second)
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001498 MaxDF.insert(MaxCoverIn(I, HasDF->second));
1499
1500 std::vector<RegisterRef> MaxRefs;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001501 RegisterSet &RefB = RefM[BA.Id];
1502 for (RegisterRef I : MaxDF)
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001503 MaxRefs.push_back(MaxCoverIn(I, RefB));
1504
1505 // Now, for each R in MaxRefs, get the alias closure of R. If the closure
1506 // only has R in it, create a phi a def for R. Otherwise, create a phi,
1507 // and add a def for each S in the closure.
1508
1509 // Sort the refs so that the phis will be created in a deterministic order.
1510 std::sort(MaxRefs.begin(), MaxRefs.end());
1511 // Remove duplicates.
1512 auto NewEnd = std::unique(MaxRefs.begin(), MaxRefs.end());
1513 MaxRefs.erase(NewEnd, MaxRefs.end());
1514
1515 auto Aliased = [this,&MaxRefs](RegisterRef RR,
1516 std::vector<unsigned> &Closure) -> bool {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001517 for (unsigned I : Closure)
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001518 if (alias(RR, MaxRefs[I]))
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001519 return true;
1520 return false;
1521 };
1522
1523 // Prepare a list of NodeIds of the block's predecessors.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001524 NodeList Preds;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001525 const MachineBasicBlock *MBB = BA.Addr->getCode();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001526 for (MachineBasicBlock *PB : MBB->predecessors())
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001527 Preds.push_back(findBlock(PB));
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001528
1529 while (!MaxRefs.empty()) {
1530 // Put the first element in the closure, and then add all subsequent
1531 // elements from MaxRefs to it, if they alias at least one element
1532 // already in the closure.
1533 // ClosureIdx: vector of indices in MaxRefs of members of the closure.
1534 std::vector<unsigned> ClosureIdx = { 0 };
1535 for (unsigned i = 1; i != MaxRefs.size(); ++i)
1536 if (Aliased(MaxRefs[i], ClosureIdx))
1537 ClosureIdx.push_back(i);
1538
1539 // Build a phi for the closure.
1540 unsigned CS = ClosureIdx.size();
1541 NodeAddr<PhiNode*> PA = newPhi(BA);
1542
1543 // Add defs.
1544 for (unsigned X = 0; X != CS; ++X) {
1545 RegisterRef RR = MaxRefs[ClosureIdx[X]];
1546 uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving;
1547 NodeAddr<DefNode*> DA = newDef(PA, RR, PhiFlags);
1548 PA.Addr->addMember(DA, *this);
1549 }
1550 // Add phi uses.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001551 for (NodeAddr<BlockNode*> PBA : Preds) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001552 for (unsigned X = 0; X != CS; ++X) {
1553 RegisterRef RR = MaxRefs[ClosureIdx[X]];
1554 NodeAddr<PhiUseNode*> PUA = newPhiUse(PA, RR, PBA);
1555 PA.Addr->addMember(PUA, *this);
1556 }
1557 }
1558
1559 // Erase from MaxRefs all elements in the closure.
1560 auto Begin = MaxRefs.begin();
1561 for (unsigned i = ClosureIdx.size(); i != 0; --i)
1562 MaxRefs.erase(Begin + ClosureIdx[i-1]);
1563 }
1564}
1565
1566// Remove any unneeded phi nodes that were created during the build process.
1567void DataFlowGraph::removeUnusedPhis() {
1568 // This will remove unused phis, i.e. phis where each def does not reach
1569 // any uses or other defs. This will not detect or remove circular phi
1570 // chains that are otherwise dead. Unused/dead phis are created during
1571 // the build process and this function is intended to remove these cases
1572 // that are easily determinable to be unnecessary.
1573
1574 SetVector<NodeId> PhiQ;
1575 for (NodeAddr<BlockNode*> BA : Func.Addr->members(*this)) {
1576 for (auto P : BA.Addr->members_if(IsPhi, *this))
1577 PhiQ.insert(P.Id);
1578 }
1579
1580 static auto HasUsedDef = [](NodeList &Ms) -> bool {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001581 for (NodeAddr<NodeBase*> M : Ms) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001582 if (M.Addr->getKind() != NodeAttrs::Def)
1583 continue;
1584 NodeAddr<DefNode*> DA = M;
1585 if (DA.Addr->getReachedDef() != 0 || DA.Addr->getReachedUse() != 0)
1586 return true;
1587 }
1588 return false;
1589 };
1590
1591 // Any phi, if it is removed, may affect other phis (make them dead).
1592 // For each removed phi, collect the potentially affected phis and add
1593 // them back to the queue.
1594 while (!PhiQ.empty()) {
1595 auto PA = addr<PhiNode*>(PhiQ[0]);
1596 PhiQ.remove(PA.Id);
1597 NodeList Refs = PA.Addr->members(*this);
1598 if (HasUsedDef(Refs))
1599 continue;
1600 for (NodeAddr<RefNode*> RA : Refs) {
1601 if (NodeId RD = RA.Addr->getReachingDef()) {
1602 auto RDA = addr<DefNode*>(RD);
1603 NodeAddr<InstrNode*> OA = RDA.Addr->getOwner(*this);
1604 if (IsPhi(OA))
1605 PhiQ.insert(OA.Id);
1606 }
1607 if (RA.Addr->isDef())
Krzysztof Parzyszek69e670d52016-01-18 20:41:34 +00001608 unlinkDef(RA, true);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001609 else
Krzysztof Parzyszek69e670d52016-01-18 20:41:34 +00001610 unlinkUse(RA, true);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001611 }
1612 NodeAddr<BlockNode*> BA = PA.Addr->getOwner(*this);
1613 BA.Addr->removeMember(PA, *this);
1614 }
1615}
1616
1617// For a given reference node TA in an instruction node IA, connect the
1618// reaching def of TA to the appropriate def node. Create any shadow nodes
1619// as appropriate.
1620template <typename T>
1621void DataFlowGraph::linkRefUp(NodeAddr<InstrNode*> IA, NodeAddr<T> TA,
1622 DefStack &DS) {
1623 if (DS.empty())
1624 return;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001625 RegisterRef RR = TA.Addr->getRegRef(*this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001626 NodeAddr<T> TAP;
1627
1628 // References from the def stack that have been examined so far.
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001629 RegisterAggr Defs(TRI);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001630
1631 for (auto I = DS.top(), E = DS.bottom(); I != E; I.down()) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001632 RegisterRef QR = I->Addr->getRegRef(*this);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001633
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001634 // Skip all defs that are aliased to any of the defs that we have already
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001635 // seen. If this completes a cover of RR, stop the stack traversal.
1636 bool Alias = Defs.hasAliasOf(QR);
1637 bool Cover = Defs.insert(QR).hasCoverOf(RR);
1638 if (Alias) {
1639 if (Cover)
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001640 break;
1641 continue;
1642 }
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001643
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001644 // The reaching def.
1645 NodeAddr<DefNode*> RDA = *I;
1646
1647 // Pick the reached node.
1648 if (TAP.Id == 0) {
1649 TAP = TA;
1650 } else {
1651 // Mark the existing ref as "shadow" and create a new shadow.
1652 TAP.Addr->setFlags(TAP.Addr->getFlags() | NodeAttrs::Shadow);
1653 TAP = getNextShadow(IA, TAP, true);
1654 }
1655
1656 // Create the link.
1657 TAP.Addr->linkToDef(TAP.Id, RDA);
1658
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001659 if (Cover)
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001660 break;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001661 }
1662}
1663
1664// Create data-flow links for all reference nodes in the statement node SA.
1665void DataFlowGraph::linkStmtRefs(DefStackMap &DefM, NodeAddr<StmtNode*> SA) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001666#ifndef NDEBUG
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001667 RegisterSet Defs;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001668#endif
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001669
1670 // Link all nodes (upwards in the data-flow) with their reaching defs.
1671 for (NodeAddr<RefNode*> RA : SA.Addr->members(*this)) {
1672 uint16_t Kind = RA.Addr->getKind();
1673 assert(Kind == NodeAttrs::Def || Kind == NodeAttrs::Use);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001674 RegisterRef RR = RA.Addr->getRegRef(*this);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001675#ifndef NDEBUG
1676 // Do not expect multiple defs of the same reference.
1677 assert(Kind != NodeAttrs::Def || !Defs.count(RR));
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001678 Defs.insert(RR);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001679#endif
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001680
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001681 auto F = DefM.find(RR.Reg);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001682 if (F == DefM.end())
1683 continue;
1684 DefStack &DS = F->second;
1685 if (Kind == NodeAttrs::Use)
1686 linkRefUp<UseNode*>(SA, RA, DS);
1687 else if (Kind == NodeAttrs::Def)
1688 linkRefUp<DefNode*>(SA, RA, DS);
1689 else
1690 llvm_unreachable("Unexpected node in instruction");
1691 }
1692}
1693
1694// Create data-flow links for all instructions in the block node BA. This
1695// will include updating any phi nodes in BA.
1696void DataFlowGraph::linkBlockRefs(DefStackMap &DefM, NodeAddr<BlockNode*> BA) {
1697 // Push block delimiters.
1698 markBlock(BA.Id, DefM);
1699
Krzysztof Parzyszek89757432016-05-05 22:00:44 +00001700 assert(BA.Addr && "block node address is needed to create a data-flow link");
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001701 // For each non-phi instruction in the block, link all the defs and uses
1702 // to their reaching defs. For any member of the block (including phis),
1703 // push the defs on the corresponding stacks.
1704 for (NodeAddr<InstrNode*> IA : BA.Addr->members(*this)) {
1705 // Ignore phi nodes here. They will be linked part by part from the
1706 // predecessors.
1707 if (IA.Addr->getKind() == NodeAttrs::Stmt)
1708 linkStmtRefs(DefM, IA);
1709
1710 // Push the definitions on the stack.
1711 pushDefs(IA, DefM);
1712 }
1713
1714 // Recursively process all children in the dominator tree.
1715 MachineDomTreeNode *N = MDT.getNode(BA.Addr->getCode());
1716 for (auto I : *N) {
1717 MachineBasicBlock *SB = I->getBlock();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001718 NodeAddr<BlockNode*> SBA = findBlock(SB);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001719 linkBlockRefs(DefM, SBA);
1720 }
1721
1722 // Link the phi uses from the successor blocks.
1723 auto IsUseForBA = [BA](NodeAddr<NodeBase*> NA) -> bool {
1724 if (NA.Addr->getKind() != NodeAttrs::Use)
1725 return false;
1726 assert(NA.Addr->getFlags() & NodeAttrs::PhiRef);
1727 NodeAddr<PhiUseNode*> PUA = NA;
1728 return PUA.Addr->getPredecessor() == BA.Id;
1729 };
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001730
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001731 RegisterSet EHLiveIns = getLandingPadLiveIns();
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001732 MachineBasicBlock *MBB = BA.Addr->getCode();
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001733
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001734 for (MachineBasicBlock *SB : MBB->successors()) {
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001735 bool IsEHPad = SB->isEHPad();
1736 NodeAddr<BlockNode*> SBA = findBlock(SB);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001737 for (NodeAddr<InstrNode*> IA : SBA.Addr->members_if(IsPhi, *this)) {
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001738 // Do not link phi uses for landing pad live-ins.
1739 if (IsEHPad) {
1740 // Find what register this phi is for.
1741 NodeAddr<RefNode*> RA = IA.Addr->getFirstMember(*this);
1742 assert(RA.Id != 0);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001743 if (EHLiveIns.count(RA.Addr->getRegRef(*this)))
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001744 continue;
1745 }
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001746 // Go over each phi use associated with MBB, and link it.
1747 for (auto U : IA.Addr->members_if(IsUseForBA, *this)) {
1748 NodeAddr<PhiUseNode*> PUA = U;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001749 RegisterRef RR = PUA.Addr->getRegRef(*this);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001750 linkRefUp<UseNode*>(IA, PUA, DefM[RR.Reg]);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001751 }
1752 }
1753 }
1754
1755 // Pop all defs from this block from the definition stacks.
1756 releaseBlock(BA.Id, DefM);
1757}
1758
1759// Remove the use node UA from any data-flow and structural links.
Krzysztof Parzyszek69e670d52016-01-18 20:41:34 +00001760void DataFlowGraph::unlinkUseDF(NodeAddr<UseNode*> UA) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001761 NodeId RD = UA.Addr->getReachingDef();
1762 NodeId Sib = UA.Addr->getSibling();
1763
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001764 if (RD == 0) {
1765 assert(Sib == 0);
1766 return;
1767 }
1768
1769 auto RDA = addr<DefNode*>(RD);
1770 auto TA = addr<UseNode*>(RDA.Addr->getReachedUse());
1771 if (TA.Id == UA.Id) {
1772 RDA.Addr->setReachedUse(Sib);
1773 return;
1774 }
1775
1776 while (TA.Id != 0) {
1777 NodeId S = TA.Addr->getSibling();
1778 if (S == UA.Id) {
1779 TA.Addr->setSibling(UA.Addr->getSibling());
1780 return;
1781 }
1782 TA = addr<UseNode*>(S);
1783 }
1784}
1785
1786// Remove the def node DA from any data-flow and structural links.
Krzysztof Parzyszek69e670d52016-01-18 20:41:34 +00001787void DataFlowGraph::unlinkDefDF(NodeAddr<DefNode*> DA) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001788 //
1789 // RD
1790 // | reached
1791 // | def
1792 // :
1793 // .
1794 // +----+
1795 // ... -- | DA | -- ... -- 0 : sibling chain of DA
1796 // +----+
1797 // | | reached
1798 // | : def
1799 // | .
1800 // | ... : Siblings (defs)
1801 // |
1802 // : reached
1803 // . use
1804 // ... : sibling chain of reached uses
1805
1806 NodeId RD = DA.Addr->getReachingDef();
1807
1808 // Visit all siblings of the reached def and reset their reaching defs.
1809 // Also, defs reached by DA are now "promoted" to being reached by RD,
1810 // so all of them will need to be spliced into the sibling chain where
1811 // DA belongs.
1812 auto getAllNodes = [this] (NodeId N) -> NodeList {
1813 NodeList Res;
1814 while (N) {
1815 auto RA = addr<RefNode*>(N);
1816 // Keep the nodes in the exact sibling order.
1817 Res.push_back(RA);
1818 N = RA.Addr->getSibling();
1819 }
1820 return Res;
1821 };
1822 NodeList ReachedDefs = getAllNodes(DA.Addr->getReachedDef());
1823 NodeList ReachedUses = getAllNodes(DA.Addr->getReachedUse());
1824
1825 if (RD == 0) {
1826 for (NodeAddr<RefNode*> I : ReachedDefs)
1827 I.Addr->setSibling(0);
1828 for (NodeAddr<RefNode*> I : ReachedUses)
1829 I.Addr->setSibling(0);
1830 }
1831 for (NodeAddr<DefNode*> I : ReachedDefs)
1832 I.Addr->setReachingDef(RD);
1833 for (NodeAddr<UseNode*> I : ReachedUses)
1834 I.Addr->setReachingDef(RD);
1835
1836 NodeId Sib = DA.Addr->getSibling();
1837 if (RD == 0) {
1838 assert(Sib == 0);
1839 return;
1840 }
1841
1842 // Update the reaching def node and remove DA from the sibling list.
1843 auto RDA = addr<DefNode*>(RD);
1844 auto TA = addr<DefNode*>(RDA.Addr->getReachedDef());
1845 if (TA.Id == DA.Id) {
1846 // If DA is the first reached def, just update the RD's reached def
1847 // to the DA's sibling.
1848 RDA.Addr->setReachedDef(Sib);
1849 } else {
1850 // Otherwise, traverse the sibling list of the reached defs and remove
1851 // DA from it.
1852 while (TA.Id != 0) {
1853 NodeId S = TA.Addr->getSibling();
1854 if (S == DA.Id) {
1855 TA.Addr->setSibling(Sib);
1856 break;
1857 }
1858 TA = addr<DefNode*>(S);
1859 }
1860 }
1861
1862 // Splice the DA's reached defs into the RDA's reached def chain.
1863 if (!ReachedDefs.empty()) {
1864 auto Last = NodeAddr<DefNode*>(ReachedDefs.back());
1865 Last.Addr->setSibling(RDA.Addr->getReachedDef());
1866 RDA.Addr->setReachedDef(ReachedDefs.front().Id);
1867 }
1868 // Splice the DA's reached uses into the RDA's reached use chain.
1869 if (!ReachedUses.empty()) {
1870 auto Last = NodeAddr<UseNode*>(ReachedUses.back());
1871 Last.Addr->setSibling(RDA.Addr->getReachedUse());
1872 RDA.Addr->setReachedUse(ReachedUses.front().Id);
1873 }
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001874}