blob: 3d0777af9cd915a0d417688d4d90fe73c29353a7 [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
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +000032raw_ostream &operator<< (raw_ostream &OS, const PrintLaneMaskOpt &P) {
33 if (P.Mask != ~LaneBitmask(0))
34 OS << ':' << PrintLaneMask(P.Mask);
35 return OS;
36}
37
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +000038template<>
39raw_ostream &operator<< (raw_ostream &OS, const Print<RegisterRef> &P) {
40 auto &TRI = P.G.getTRI();
41 if (P.Obj.Reg > 0 && P.Obj.Reg < TRI.getNumRegs())
42 OS << TRI.getName(P.Obj.Reg);
43 else
44 OS << '#' << P.Obj.Reg;
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +000045 OS << PrintLaneMaskOpt(P.Obj.Mask);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +000046 return OS;
47}
48
49template<>
50raw_ostream &operator<< (raw_ostream &OS, const Print<NodeId> &P) {
51 auto NA = P.G.addr<NodeBase*>(P.Obj);
52 uint16_t Attrs = NA.Addr->getAttrs();
53 uint16_t Kind = NodeAttrs::kind(Attrs);
54 uint16_t Flags = NodeAttrs::flags(Attrs);
55 switch (NodeAttrs::type(Attrs)) {
56 case NodeAttrs::Code:
57 switch (Kind) {
58 case NodeAttrs::Func: OS << 'f'; break;
59 case NodeAttrs::Block: OS << 'b'; break;
60 case NodeAttrs::Stmt: OS << 's'; break;
61 case NodeAttrs::Phi: OS << 'p'; break;
62 default: OS << "c?"; break;
63 }
64 break;
65 case NodeAttrs::Ref:
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +000066 if (Flags & NodeAttrs::Undef)
67 OS << '/';
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +000068 if (Flags & NodeAttrs::Dead)
69 OS << '\\';
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +000070 if (Flags & NodeAttrs::Preserving)
71 OS << '+';
72 if (Flags & NodeAttrs::Clobbering)
73 OS << '~';
74 switch (Kind) {
75 case NodeAttrs::Use: OS << 'u'; break;
76 case NodeAttrs::Def: OS << 'd'; break;
77 case NodeAttrs::Block: OS << 'b'; break;
78 default: OS << "r?"; break;
79 }
80 break;
81 default:
82 OS << '?';
83 break;
84 }
85 OS << P.Obj;
86 if (Flags & NodeAttrs::Shadow)
87 OS << '"';
88 return OS;
89}
90
91namespace {
92 void printRefHeader(raw_ostream &OS, const NodeAddr<RefNode*> RA,
93 const DataFlowGraph &G) {
94 OS << Print<NodeId>(RA.Id, G) << '<'
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +000095 << Print<RegisterRef>(RA.Addr->getRegRef(G), G) << '>';
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +000096 if (RA.Addr->getFlags() & NodeAttrs::Fixed)
97 OS << '!';
98 }
99}
100
101template<>
102raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<DefNode*>> &P) {
103 printRefHeader(OS, P.Obj, P.G);
104 OS << '(';
105 if (NodeId N = P.Obj.Addr->getReachingDef())
106 OS << Print<NodeId>(N, P.G);
107 OS << ',';
108 if (NodeId N = P.Obj.Addr->getReachedDef())
109 OS << Print<NodeId>(N, P.G);
110 OS << ',';
111 if (NodeId N = P.Obj.Addr->getReachedUse())
112 OS << Print<NodeId>(N, P.G);
113 OS << "):";
114 if (NodeId N = P.Obj.Addr->getSibling())
115 OS << Print<NodeId>(N, P.G);
116 return OS;
117}
118
119template<>
120raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<UseNode*>> &P) {
121 printRefHeader(OS, P.Obj, P.G);
122 OS << '(';
123 if (NodeId N = P.Obj.Addr->getReachingDef())
124 OS << Print<NodeId>(N, P.G);
125 OS << "):";
126 if (NodeId N = P.Obj.Addr->getSibling())
127 OS << Print<NodeId>(N, P.G);
128 return OS;
129}
130
131template<>
132raw_ostream &operator<< (raw_ostream &OS,
133 const Print<NodeAddr<PhiUseNode*>> &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->getPredecessor())
140 OS << Print<NodeId>(N, P.G);
141 OS << "):";
142 if (NodeId N = P.Obj.Addr->getSibling())
143 OS << Print<NodeId>(N, P.G);
144 return OS;
145}
146
147template<>
148raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<RefNode*>> &P) {
149 switch (P.Obj.Addr->getKind()) {
150 case NodeAttrs::Def:
151 OS << PrintNode<DefNode*>(P.Obj, P.G);
152 break;
153 case NodeAttrs::Use:
154 if (P.Obj.Addr->getFlags() & NodeAttrs::PhiRef)
155 OS << PrintNode<PhiUseNode*>(P.Obj, P.G);
156 else
157 OS << PrintNode<UseNode*>(P.Obj, P.G);
158 break;
159 }
160 return OS;
161}
162
163template<>
164raw_ostream &operator<< (raw_ostream &OS, const Print<NodeList> &P) {
165 unsigned N = P.Obj.size();
166 for (auto I : P.Obj) {
167 OS << Print<NodeId>(I.Id, P.G);
168 if (--N)
169 OS << ' ';
170 }
171 return OS;
172}
173
174template<>
175raw_ostream &operator<< (raw_ostream &OS, const Print<NodeSet> &P) {
176 unsigned N = P.Obj.size();
177 for (auto I : P.Obj) {
178 OS << Print<NodeId>(I, P.G);
179 if (--N)
180 OS << ' ';
181 }
182 return OS;
183}
184
185namespace {
186 template <typename T>
187 struct PrintListV {
188 PrintListV(const NodeList &L, const DataFlowGraph &G) : List(L), G(G) {}
189 typedef T Type;
190 const NodeList &List;
191 const DataFlowGraph &G;
192 };
193
194 template <typename T>
195 raw_ostream &operator<< (raw_ostream &OS, const PrintListV<T> &P) {
196 unsigned N = P.List.size();
197 for (NodeAddr<T> A : P.List) {
198 OS << PrintNode<T>(A, P.G);
199 if (--N)
200 OS << ", ";
201 }
202 return OS;
203 }
204}
205
206template<>
207raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<PhiNode*>> &P) {
208 OS << Print<NodeId>(P.Obj.Id, P.G) << ": phi ["
209 << PrintListV<RefNode*>(P.Obj.Addr->members(P.G), P.G) << ']';
210 return OS;
211}
212
213template<>
214raw_ostream &operator<< (raw_ostream &OS,
215 const Print<NodeAddr<StmtNode*>> &P) {
Krzysztof Parzyszek670e0ca2016-09-22 20:58:19 +0000216 const MachineInstr &MI = *P.Obj.Addr->getCode();
217 unsigned Opc = MI.getOpcode();
218 OS << Print<NodeId>(P.Obj.Id, P.G) << ": " << P.G.getTII().getName(Opc);
Krzysztof Parzyszekab26e2d2016-10-03 17:54:33 +0000219 // Print the target for calls and branches (for readability).
220 if (MI.isCall() || MI.isBranch()) {
221 MachineInstr::const_mop_iterator T =
Krzysztof Parzyszek670e0ca2016-09-22 20:58:19 +0000222 find_if(MI.operands(),
223 [] (const MachineOperand &Op) -> bool {
Krzysztof Parzyszekab26e2d2016-10-03 17:54:33 +0000224 return Op.isMBB() || Op.isGlobal() || Op.isSymbol();
Krzysztof Parzyszek670e0ca2016-09-22 20:58:19 +0000225 });
Krzysztof Parzyszekab26e2d2016-10-03 17:54:33 +0000226 if (T != MI.operands_end()) {
227 OS << ' ';
228 if (T->isMBB())
229 OS << "BB#" << T->getMBB()->getNumber();
230 else if (T->isGlobal())
231 OS << T->getGlobal()->getName();
232 else if (T->isSymbol())
233 OS << T->getSymbolName();
Krzysztof Parzyszek670e0ca2016-09-22 20:58:19 +0000234 }
235 }
236 OS << " [" << PrintListV<RefNode*>(P.Obj.Addr->members(P.G), P.G) << ']';
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000237 return OS;
238}
239
240template<>
241raw_ostream &operator<< (raw_ostream &OS,
242 const Print<NodeAddr<InstrNode*>> &P) {
243 switch (P.Obj.Addr->getKind()) {
244 case NodeAttrs::Phi:
245 OS << PrintNode<PhiNode*>(P.Obj, P.G);
246 break;
247 case NodeAttrs::Stmt:
248 OS << PrintNode<StmtNode*>(P.Obj, P.G);
249 break;
250 default:
251 OS << "instr? " << Print<NodeId>(P.Obj.Id, P.G);
252 break;
253 }
254 return OS;
255}
256
257template<>
258raw_ostream &operator<< (raw_ostream &OS,
259 const Print<NodeAddr<BlockNode*>> &P) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000260 MachineBasicBlock *BB = P.Obj.Addr->getCode();
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000261 unsigned NP = BB->pred_size();
262 std::vector<int> Ns;
263 auto PrintBBs = [&OS,&P] (std::vector<int> Ns) -> void {
264 unsigned N = Ns.size();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000265 for (int I : Ns) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000266 OS << "BB#" << I;
267 if (--N)
268 OS << ", ";
269 }
270 };
271
Krzysztof Parzyszekab26e2d2016-10-03 17:54:33 +0000272 OS << Print<NodeId>(P.Obj.Id, P.G) << ": --- BB#" << BB->getNumber()
273 << " --- preds(" << NP << "): ";
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000274 for (MachineBasicBlock *B : BB->predecessors())
275 Ns.push_back(B->getNumber());
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000276 PrintBBs(Ns);
277
278 unsigned NS = BB->succ_size();
279 OS << " succs(" << NS << "): ";
280 Ns.clear();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000281 for (MachineBasicBlock *B : BB->successors())
282 Ns.push_back(B->getNumber());
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000283 PrintBBs(Ns);
284 OS << '\n';
285
286 for (auto I : P.Obj.Addr->members(P.G))
287 OS << PrintNode<InstrNode*>(I, P.G) << '\n';
288 return OS;
289}
290
291template<>
292raw_ostream &operator<< (raw_ostream &OS,
293 const Print<NodeAddr<FuncNode*>> &P) {
294 OS << "DFG dump:[\n" << Print<NodeId>(P.Obj.Id, P.G) << ": Function: "
295 << P.Obj.Addr->getCode()->getName() << '\n';
296 for (auto I : P.Obj.Addr->members(P.G))
297 OS << PrintNode<BlockNode*>(I, P.G) << '\n';
298 OS << "]\n";
299 return OS;
300}
301
302template<>
303raw_ostream &operator<< (raw_ostream &OS, const Print<RegisterSet> &P) {
304 OS << '{';
305 for (auto I : P.Obj)
306 OS << ' ' << Print<RegisterRef>(I, P.G);
307 OS << " }";
308 return OS;
309}
310
311template<>
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000312raw_ostream &operator<< (raw_ostream &OS, const Print<RegisterAggr> &P) {
313 P.Obj.print(OS);
314 return OS;
315}
316
317template<>
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000318raw_ostream &operator<< (raw_ostream &OS,
319 const Print<DataFlowGraph::DefStack> &P) {
320 for (auto I = P.Obj.top(), E = P.Obj.bottom(); I != E; ) {
321 OS << Print<NodeId>(I->Id, P.G)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000322 << '<' << Print<RegisterRef>(I->Addr->getRegRef(P.G), P.G) << '>';
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000323 I.down();
324 if (I != E)
325 OS << ' ';
326 }
327 return OS;
328}
329
330} // namespace rdf
Benjamin Kramer922efd72016-05-27 10:06:40 +0000331} // namespace llvm
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000332
333// Node allocation functions.
334//
335// Node allocator is like a slab memory allocator: it allocates blocks of
336// memory in sizes that are multiples of the size of a node. Each block has
337// the same size. Nodes are allocated from the currently active block, and
338// when it becomes full, a new one is created.
339// There is a mapping scheme between node id and its location in a block,
340// and within that block is described in the header file.
341//
342void NodeAllocator::startNewBlock() {
343 void *T = MemPool.Allocate(NodesPerBlock*NodeMemSize, NodeMemSize);
344 char *P = static_cast<char*>(T);
345 Blocks.push_back(P);
346 // Check if the block index is still within the allowed range, i.e. less
347 // than 2^N, where N is the number of bits in NodeId for the block index.
348 // BitsPerIndex is the number of bits per node index.
Simon Pilgrim99c6c292016-01-18 21:11:19 +0000349 assert((Blocks.size() < ((size_t)1 << (8*sizeof(NodeId)-BitsPerIndex))) &&
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000350 "Out of bits for block index");
351 ActiveEnd = P;
352}
353
354bool NodeAllocator::needNewBlock() {
355 if (Blocks.empty())
356 return true;
357
358 char *ActiveBegin = Blocks.back();
359 uint32_t Index = (ActiveEnd-ActiveBegin)/NodeMemSize;
360 return Index >= NodesPerBlock;
361}
362
363NodeAddr<NodeBase*> NodeAllocator::New() {
364 if (needNewBlock())
365 startNewBlock();
366
367 uint32_t ActiveB = Blocks.size()-1;
368 uint32_t Index = (ActiveEnd - Blocks[ActiveB])/NodeMemSize;
369 NodeAddr<NodeBase*> NA = { reinterpret_cast<NodeBase*>(ActiveEnd),
370 makeId(ActiveB, Index) };
371 ActiveEnd += NodeMemSize;
372 return NA;
373}
374
375NodeId NodeAllocator::id(const NodeBase *P) const {
376 uintptr_t A = reinterpret_cast<uintptr_t>(P);
377 for (unsigned i = 0, n = Blocks.size(); i != n; ++i) {
378 uintptr_t B = reinterpret_cast<uintptr_t>(Blocks[i]);
379 if (A < B || A >= B + NodesPerBlock*NodeMemSize)
380 continue;
381 uint32_t Idx = (A-B)/NodeMemSize;
382 return makeId(i, Idx);
383 }
384 llvm_unreachable("Invalid node address");
385}
386
387void NodeAllocator::clear() {
388 MemPool.Reset();
389 Blocks.clear();
390 ActiveEnd = nullptr;
391}
392
393
394// Insert node NA after "this" in the circular chain.
395void NodeBase::append(NodeAddr<NodeBase*> NA) {
396 NodeId Nx = Next;
397 // If NA is already "next", do nothing.
398 if (Next != NA.Id) {
399 Next = NA.Id;
400 NA.Addr->Next = Nx;
401 }
402}
403
404
405// Fundamental node manipulator functions.
406
407// Obtain the register reference from a reference node.
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000408RegisterRef RefNode::getRegRef(const DataFlowGraph &G) const {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000409 assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
410 if (NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000411 return G.unpack(Ref.PR);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000412 assert(Ref.Op != nullptr);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000413 return G.makeRegRef(Ref.Op->getReg(), Ref.Op->getSubReg());
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000414}
415
416// Set the register reference in the reference node directly (for references
417// in phi nodes).
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000418void RefNode::setRegRef(RegisterRef RR, DataFlowGraph &G) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000419 assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
420 assert(NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000421 Ref.PR = G.pack(RR);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000422}
423
424// Set the register reference in the reference node based on a machine
425// operand (for references in statement nodes).
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000426void RefNode::setRegRef(MachineOperand *Op, DataFlowGraph &G) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000427 assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
428 assert(!(NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef));
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000429 (void)G;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000430 Ref.Op = Op;
431}
432
433// Get the owner of a given reference node.
434NodeAddr<NodeBase*> RefNode::getOwner(const DataFlowGraph &G) {
435 NodeAddr<NodeBase*> NA = G.addr<NodeBase*>(getNext());
436
437 while (NA.Addr != this) {
438 if (NA.Addr->getType() == NodeAttrs::Code)
439 return NA;
440 NA = G.addr<NodeBase*>(NA.Addr->getNext());
441 }
442 llvm_unreachable("No owner in circular list");
443}
444
445// Connect the def node to the reaching def node.
446void DefNode::linkToDef(NodeId Self, NodeAddr<DefNode*> DA) {
447 Ref.RD = DA.Id;
448 Ref.Sib = DA.Addr->getReachedDef();
449 DA.Addr->setReachedDef(Self);
450}
451
452// Connect the use node to the reaching def node.
453void UseNode::linkToDef(NodeId Self, NodeAddr<DefNode*> DA) {
454 Ref.RD = DA.Id;
455 Ref.Sib = DA.Addr->getReachedUse();
456 DA.Addr->setReachedUse(Self);
457}
458
459// Get the first member of the code node.
460NodeAddr<NodeBase*> CodeNode::getFirstMember(const DataFlowGraph &G) const {
461 if (Code.FirstM == 0)
462 return NodeAddr<NodeBase*>();
463 return G.addr<NodeBase*>(Code.FirstM);
464}
465
466// Get the last member of the code node.
467NodeAddr<NodeBase*> CodeNode::getLastMember(const DataFlowGraph &G) const {
468 if (Code.LastM == 0)
469 return NodeAddr<NodeBase*>();
470 return G.addr<NodeBase*>(Code.LastM);
471}
472
473// Add node NA at the end of the member list of the given code node.
474void CodeNode::addMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000475 NodeAddr<NodeBase*> ML = getLastMember(G);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000476 if (ML.Id != 0) {
477 ML.Addr->append(NA);
478 } else {
479 Code.FirstM = NA.Id;
480 NodeId Self = G.id(this);
481 NA.Addr->setNext(Self);
482 }
483 Code.LastM = NA.Id;
484}
485
486// Add node NA after member node MA in the given code node.
487void CodeNode::addMemberAfter(NodeAddr<NodeBase*> MA, NodeAddr<NodeBase*> NA,
488 const DataFlowGraph &G) {
489 MA.Addr->append(NA);
490 if (Code.LastM == MA.Id)
491 Code.LastM = NA.Id;
492}
493
494// Remove member node NA from the given code node.
495void CodeNode::removeMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000496 NodeAddr<NodeBase*> MA = getFirstMember(G);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000497 assert(MA.Id != 0);
498
499 // Special handling if the member to remove is the first member.
500 if (MA.Id == NA.Id) {
501 if (Code.LastM == MA.Id) {
502 // If it is the only member, set both first and last to 0.
503 Code.FirstM = Code.LastM = 0;
504 } else {
505 // Otherwise, advance the first member.
506 Code.FirstM = MA.Addr->getNext();
507 }
508 return;
509 }
510
511 while (MA.Addr != this) {
512 NodeId MX = MA.Addr->getNext();
513 if (MX == NA.Id) {
514 MA.Addr->setNext(NA.Addr->getNext());
515 // If the member to remove happens to be the last one, update the
516 // LastM indicator.
517 if (Code.LastM == NA.Id)
518 Code.LastM = MA.Id;
519 return;
520 }
521 MA = G.addr<NodeBase*>(MX);
522 }
523 llvm_unreachable("No such member");
524}
525
526// Return the list of all members of the code node.
527NodeList CodeNode::members(const DataFlowGraph &G) const {
528 static auto True = [] (NodeAddr<NodeBase*>) -> bool { return true; };
529 return members_if(True, G);
530}
531
532// Return the owner of the given instr node.
533NodeAddr<NodeBase*> InstrNode::getOwner(const DataFlowGraph &G) {
534 NodeAddr<NodeBase*> NA = G.addr<NodeBase*>(getNext());
535
536 while (NA.Addr != this) {
537 assert(NA.Addr->getType() == NodeAttrs::Code);
538 if (NA.Addr->getKind() == NodeAttrs::Block)
539 return NA;
540 NA = G.addr<NodeBase*>(NA.Addr->getNext());
541 }
542 llvm_unreachable("No owner in circular list");
543}
544
545// Add the phi node PA to the given block node.
546void BlockNode::addPhi(NodeAddr<PhiNode*> PA, const DataFlowGraph &G) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000547 NodeAddr<NodeBase*> M = getFirstMember(G);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000548 if (M.Id == 0) {
549 addMember(PA, G);
550 return;
551 }
552
553 assert(M.Addr->getType() == NodeAttrs::Code);
554 if (M.Addr->getKind() == NodeAttrs::Stmt) {
555 // If the first member of the block is a statement, insert the phi as
556 // the first member.
557 Code.FirstM = PA.Id;
558 PA.Addr->setNext(M.Id);
559 } else {
560 // If the first member is a phi, find the last phi, and append PA to it.
561 assert(M.Addr->getKind() == NodeAttrs::Phi);
562 NodeAddr<NodeBase*> MN = M;
563 do {
564 M = MN;
565 MN = G.addr<NodeBase*>(M.Addr->getNext());
566 assert(MN.Addr->getType() == NodeAttrs::Code);
567 } while (MN.Addr->getKind() == NodeAttrs::Phi);
568
569 // M is the last phi.
570 addMemberAfter(M, PA, G);
571 }
572}
573
574// Find the block node corresponding to the machine basic block BB in the
575// given func node.
576NodeAddr<BlockNode*> FuncNode::findBlock(const MachineBasicBlock *BB,
577 const DataFlowGraph &G) const {
578 auto EqBB = [BB] (NodeAddr<NodeBase*> NA) -> bool {
579 return NodeAddr<BlockNode*>(NA).Addr->getCode() == BB;
580 };
581 NodeList Ms = members_if(EqBB, G);
582 if (!Ms.empty())
583 return Ms[0];
584 return NodeAddr<BlockNode*>();
585}
586
587// Get the block node for the entry block in the given function.
588NodeAddr<BlockNode*> FuncNode::getEntryBlock(const DataFlowGraph &G) {
589 MachineBasicBlock *EntryB = &getCode()->front();
590 return findBlock(EntryB, G);
591}
592
593
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000594// Target operand information.
595//
596
597// For a given instruction, check if there are any bits of RR that can remain
598// unchanged across this def.
599bool TargetOperandInfo::isPreserving(const MachineInstr &In, unsigned OpNum)
600 const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000601 return TII.isPredicated(In);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000602}
603
604// Check if the definition of RR produces an unspecified value.
605bool TargetOperandInfo::isClobbering(const MachineInstr &In, unsigned OpNum)
606 const {
607 if (In.isCall())
608 if (In.getOperand(OpNum).isImplicit())
609 return true;
610 return false;
611}
612
Krzysztof Parzyszekc5a4e262016-04-28 20:33:33 +0000613// Check if the given instruction specifically requires
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000614bool TargetOperandInfo::isFixedReg(const MachineInstr &In, unsigned OpNum)
615 const {
Krzysztof Parzyszekc5a4e262016-04-28 20:33:33 +0000616 if (In.isCall() || In.isReturn() || In.isInlineAsm())
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000617 return true;
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +0000618 // Check for a tail call.
619 if (In.isBranch())
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000620 for (const MachineOperand &O : In.operands())
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +0000621 if (O.isGlobal() || O.isSymbol())
622 return true;
623
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000624 const MCInstrDesc &D = In.getDesc();
625 if (!D.getImplicitDefs() && !D.getImplicitUses())
626 return false;
627 const MachineOperand &Op = In.getOperand(OpNum);
628 // If there is a sub-register, treat the operand as non-fixed. Currently,
629 // fixed registers are those that are listed in the descriptor as implicit
630 // uses or defs, and those lists do not allow sub-registers.
631 if (Op.getSubReg() != 0)
632 return false;
Krzysztof Parzyszekc51f2392016-09-22 20:56:39 +0000633 uint32_t Reg = Op.getReg();
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000634 const MCPhysReg *ImpR = Op.isDef() ? D.getImplicitDefs()
635 : D.getImplicitUses();
636 if (!ImpR)
637 return false;
638 while (*ImpR)
639 if (*ImpR++ == Reg)
640 return true;
641 return false;
642}
643
644
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000645RegisterRef RegisterAggr::normalize(RegisterRef RR) const {
646 uint32_t SuperReg = RR.Reg;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000647 while (true) {
648 MCSuperRegIterator SR(SuperReg, &TRI, false);
649 if (!SR.isValid())
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000650 break;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000651 SuperReg = *SR;
652 }
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000653
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000654 uint32_t Sub = TRI.getSubRegIndex(SuperReg, RR.Reg);
655 const TargetRegisterClass &RC = *TRI.getMinimalPhysRegClass(RR.Reg);
656 LaneBitmask SuperMask = RR.Mask &
657 TRI.composeSubRegIndexLaneMask(Sub, RC.LaneMask);
658 return RegisterRef(SuperReg, SuperMask);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000659}
660
661bool RegisterAggr::hasAliasOf(RegisterRef RR) const {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000662 RegisterRef NR = normalize(RR);
663 auto F = Masks.find(NR.Reg);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000664 if (F != Masks.end()) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000665 if (F->second & NR.Mask)
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000666 return true;
667 }
668 if (CheckUnits) {
669 for (MCRegUnitIterator U(RR.Reg, &TRI); U.isValid(); ++U)
670 if (ExpAliasUnits.test(*U))
671 return true;
672 }
673 return false;
674}
675
676bool RegisterAggr::hasCoverOf(RegisterRef RR) const {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000677 // Always have a cover for empty lane mask.
678 RegisterRef NR = normalize(RR);
679 if (!NR.Mask)
680 return true;
681 auto F = Masks.find(NR.Reg);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000682 if (F == Masks.end())
683 return false;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000684 return (NR.Mask & F->second) == NR.Mask;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000685}
686
687RegisterAggr &RegisterAggr::insert(RegisterRef RR) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000688 RegisterRef NR = normalize(RR);
689 auto F = Masks.find(NR.Reg);
690 if (F == Masks.end())
691 Masks.insert({NR.Reg, NR.Mask});
692 else
693 F->second |= NR.Mask;
694
695 // Visit all register units to see if there are any that were created
696 // by explicit aliases. Add those that were to the bit vector.
697 for (MCRegUnitIterator U(RR.Reg, &TRI); U.isValid(); ++U) {
698 MCRegUnitRootIterator R(*U, &TRI);
699 ++R;
700 if (!R.isValid())
701 continue;
702 ExpAliasUnits.set(*U);
703 CheckUnits = true;
704 }
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000705 return *this;
706}
707
708RegisterAggr &RegisterAggr::insert(const RegisterAggr &RG) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000709 for (std::pair<uint32_t,LaneBitmask> P : RG.Masks)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000710 insert(RegisterRef(P.first, P.second));
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000711 return *this;
712}
713
714RegisterAggr &RegisterAggr::clear(RegisterRef RR) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000715 RegisterRef NR = normalize(RR);
716 auto F = Masks.find(NR.Reg);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000717 if (F == Masks.end())
718 return *this;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000719 LaneBitmask NewM = F->second & ~NR.Mask;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000720 if (NewM == LaneBitmask(0))
721 Masks.erase(F);
722 else
723 F->second = NewM;
724 return *this;
725}
726
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000727RegisterAggr &RegisterAggr::clear(const RegisterAggr &RG) {
728 for (std::pair<uint32_t,LaneBitmask> P : RG.Masks)
729 clear(RegisterRef(P.first, P.second));
730 return *this;
731}
732
733RegisterRef RegisterAggr::clearIn(RegisterRef RR) const {
734 RegisterAggr T(TRI);
735 T.insert(RR).clear(*this);
736 if (T.empty())
737 return RegisterRef();
738 return RegisterRef(T.begin()->first, T.begin()->second);
739}
740
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000741void RegisterAggr::print(raw_ostream &OS) const {
742 OS << '{';
743 for (auto I : Masks)
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000744 OS << ' ' << PrintReg(I.first, &TRI) << PrintLaneMaskOpt(I.second);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000745 OS << " }";
746}
747
748
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000749//
750// The data flow graph construction.
751//
752
753DataFlowGraph::DataFlowGraph(MachineFunction &mf, const TargetInstrInfo &tii,
754 const TargetRegisterInfo &tri, const MachineDominatorTree &mdt,
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000755 const MachineDominanceFrontier &mdf, const TargetOperandInfo &toi)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000756 : TimeG("rdf"), LMI(), MF(mf), TII(tii), TRI(tri), MDT(mdt), MDF(mdf),
757 TOI(toi) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000758}
759
760
761// The implementation of the definition stack.
762// Each register reference has its own definition stack. In particular,
763// for a register references "Reg" and "Reg:subreg" will each have their
764// own definition stacks.
765
766// Construct a stack iterator.
767DataFlowGraph::DefStack::Iterator::Iterator(const DataFlowGraph::DefStack &S,
768 bool Top) : DS(S) {
769 if (!Top) {
770 // Initialize to bottom.
771 Pos = 0;
772 return;
773 }
774 // Initialize to the top, i.e. top-most non-delimiter (or 0, if empty).
775 Pos = DS.Stack.size();
776 while (Pos > 0 && DS.isDelimiter(DS.Stack[Pos-1]))
777 Pos--;
778}
779
780// Return the size of the stack, including block delimiters.
781unsigned DataFlowGraph::DefStack::size() const {
782 unsigned S = 0;
783 for (auto I = top(), E = bottom(); I != E; I.down())
784 S++;
785 return S;
786}
787
788// Remove the top entry from the stack. Remove all intervening delimiters
789// so that after this, the stack is either empty, or the top of the stack
790// is a non-delimiter.
791void DataFlowGraph::DefStack::pop() {
792 assert(!empty());
793 unsigned P = nextDown(Stack.size());
794 Stack.resize(P);
795}
796
797// Push a delimiter for block node N on the stack.
798void DataFlowGraph::DefStack::start_block(NodeId N) {
799 assert(N != 0);
800 Stack.push_back(NodeAddr<DefNode*>(nullptr, N));
801}
802
803// Remove all nodes from the top of the stack, until the delimited for
804// block node N is encountered. Remove the delimiter as well. In effect,
805// this will remove from the stack all definitions from block N.
806void DataFlowGraph::DefStack::clear_block(NodeId N) {
807 assert(N != 0);
808 unsigned P = Stack.size();
809 while (P > 0) {
810 bool Found = isDelimiter(Stack[P-1], N);
811 P--;
812 if (Found)
813 break;
814 }
815 // This will also remove the delimiter, if found.
816 Stack.resize(P);
817}
818
819// Move the stack iterator up by one.
820unsigned DataFlowGraph::DefStack::nextUp(unsigned P) const {
821 // Get the next valid position after P (skipping all delimiters).
822 // The input position P does not have to point to a non-delimiter.
823 unsigned SS = Stack.size();
824 bool IsDelim;
Krzysztof Parzyszek8dca45e2016-01-12 16:51:55 +0000825 assert(P < SS);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000826 do {
827 P++;
828 IsDelim = isDelimiter(Stack[P-1]);
829 } while (P < SS && IsDelim);
830 assert(!IsDelim);
831 return P;
832}
833
834// Move the stack iterator down by one.
835unsigned DataFlowGraph::DefStack::nextDown(unsigned P) const {
836 // Get the preceding valid position before P (skipping all delimiters).
837 // The input position P does not have to point to a non-delimiter.
838 assert(P > 0 && P <= Stack.size());
839 bool IsDelim = isDelimiter(Stack[P-1]);
840 do {
841 if (--P == 0)
842 break;
843 IsDelim = isDelimiter(Stack[P-1]);
844 } while (P > 0 && IsDelim);
845 assert(!IsDelim);
846 return P;
847}
848
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000849
850// Register information.
851
852// Get the list of references aliased to RR. Lane masks are ignored.
853RegisterSet DataFlowGraph::getAliasSet(uint32_t Reg) const {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000854 // Do not include RR in the alias set.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000855 RegisterSet AS;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000856 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
857
858 for (MCRegAliasIterator AI(Reg, &TRI, false); AI.isValid(); ++AI)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000859 AS.insert(RegisterRef(*AI));
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000860 return AS;
861}
862
863RegisterSet DataFlowGraph::getLandingPadLiveIns() const {
864 RegisterSet LR;
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +0000865 const Function &F = *MF.getFunction();
866 const Constant *PF = F.hasPersonalityFn() ? F.getPersonalityFn()
867 : nullptr;
868 const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering();
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000869 if (uint32_t R = TLI.getExceptionPointerRegister(PF))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000870 LR.insert(RegisterRef(R));
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000871 if (uint32_t R = TLI.getExceptionSelectorRegister(PF))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000872 LR.insert(RegisterRef(R));
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +0000873 return LR;
874}
875
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000876// Node management functions.
877
878// Get the pointer to the node with the id N.
879NodeBase *DataFlowGraph::ptr(NodeId N) const {
880 if (N == 0)
881 return nullptr;
882 return Memory.ptr(N);
883}
884
885// Get the id of the node at the address P.
886NodeId DataFlowGraph::id(const NodeBase *P) const {
887 if (P == nullptr)
888 return 0;
889 return Memory.id(P);
890}
891
892// Allocate a new node and set the attributes to Attrs.
893NodeAddr<NodeBase*> DataFlowGraph::newNode(uint16_t Attrs) {
894 NodeAddr<NodeBase*> P = Memory.New();
895 P.Addr->init();
896 P.Addr->setAttrs(Attrs);
897 return P;
898}
899
900// Make a copy of the given node B, except for the data-flow links, which
901// are set to 0.
902NodeAddr<NodeBase*> DataFlowGraph::cloneNode(const NodeAddr<NodeBase*> B) {
903 NodeAddr<NodeBase*> NA = newNode(0);
904 memcpy(NA.Addr, B.Addr, sizeof(NodeBase));
905 // Ref nodes need to have the data-flow links reset.
906 if (NA.Addr->getType() == NodeAttrs::Ref) {
907 NodeAddr<RefNode*> RA = NA;
908 RA.Addr->setReachingDef(0);
909 RA.Addr->setSibling(0);
910 if (NA.Addr->getKind() == NodeAttrs::Def) {
911 NodeAddr<DefNode*> DA = NA;
912 DA.Addr->setReachedDef(0);
913 DA.Addr->setReachedUse(0);
914 }
915 }
916 return NA;
917}
918
919
920// Allocation routines for specific node types/kinds.
921
922NodeAddr<UseNode*> DataFlowGraph::newUse(NodeAddr<InstrNode*> Owner,
923 MachineOperand &Op, uint16_t Flags) {
924 NodeAddr<UseNode*> UA = newNode(NodeAttrs::Ref | NodeAttrs::Use | Flags);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000925 UA.Addr->setRegRef(&Op, *this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000926 return UA;
927}
928
929NodeAddr<PhiUseNode*> DataFlowGraph::newPhiUse(NodeAddr<PhiNode*> Owner,
930 RegisterRef RR, NodeAddr<BlockNode*> PredB, uint16_t Flags) {
931 NodeAddr<PhiUseNode*> PUA = newNode(NodeAttrs::Ref | NodeAttrs::Use | Flags);
932 assert(Flags & NodeAttrs::PhiRef);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000933 PUA.Addr->setRegRef(RR, *this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000934 PUA.Addr->setPredecessor(PredB.Id);
935 return PUA;
936}
937
938NodeAddr<DefNode*> DataFlowGraph::newDef(NodeAddr<InstrNode*> Owner,
939 MachineOperand &Op, uint16_t Flags) {
940 NodeAddr<DefNode*> DA = newNode(NodeAttrs::Ref | NodeAttrs::Def | Flags);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000941 DA.Addr->setRegRef(&Op, *this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000942 return DA;
943}
944
945NodeAddr<DefNode*> DataFlowGraph::newDef(NodeAddr<InstrNode*> Owner,
946 RegisterRef RR, uint16_t Flags) {
947 NodeAddr<DefNode*> DA = newNode(NodeAttrs::Ref | NodeAttrs::Def | Flags);
948 assert(Flags & NodeAttrs::PhiRef);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000949 DA.Addr->setRegRef(RR, *this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000950 return DA;
951}
952
953NodeAddr<PhiNode*> DataFlowGraph::newPhi(NodeAddr<BlockNode*> Owner) {
954 NodeAddr<PhiNode*> PA = newNode(NodeAttrs::Code | NodeAttrs::Phi);
955 Owner.Addr->addPhi(PA, *this);
956 return PA;
957}
958
959NodeAddr<StmtNode*> DataFlowGraph::newStmt(NodeAddr<BlockNode*> Owner,
960 MachineInstr *MI) {
961 NodeAddr<StmtNode*> SA = newNode(NodeAttrs::Code | NodeAttrs::Stmt);
962 SA.Addr->setCode(MI);
963 Owner.Addr->addMember(SA, *this);
964 return SA;
965}
966
967NodeAddr<BlockNode*> DataFlowGraph::newBlock(NodeAddr<FuncNode*> Owner,
968 MachineBasicBlock *BB) {
969 NodeAddr<BlockNode*> BA = newNode(NodeAttrs::Code | NodeAttrs::Block);
970 BA.Addr->setCode(BB);
971 Owner.Addr->addMember(BA, *this);
972 return BA;
973}
974
975NodeAddr<FuncNode*> DataFlowGraph::newFunc(MachineFunction *MF) {
976 NodeAddr<FuncNode*> FA = newNode(NodeAttrs::Code | NodeAttrs::Func);
977 FA.Addr->setCode(MF);
978 return FA;
979}
980
981// Build the data flow graph.
Krzysztof Parzyszek55874cf2016-04-28 20:17:06 +0000982void DataFlowGraph::build(unsigned Options) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000983 reset();
984 Func = newFunc(&MF);
985
986 if (MF.empty())
987 return;
988
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000989 for (MachineBasicBlock &B : MF) {
990 NodeAddr<BlockNode*> BA = newBlock(Func, &B);
Krzysztof Parzyszek047149f2016-07-22 16:09:47 +0000991 BlockNodes.insert(std::make_pair(&B, BA));
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000992 for (MachineInstr &I : B) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000993 if (I.isDebugValue())
994 continue;
995 buildStmt(BA, I);
996 }
997 }
998
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000999 NodeAddr<BlockNode*> EA = Func.Addr->getEntryBlock(*this);
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001000 NodeList Blocks = Func.Addr->members(*this);
1001
1002 // Collect information about block references.
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001003 BlockRefsMap RefM;
1004 buildBlockRefs(EA, RefM);
1005
1006 // Add function-entry phi nodes.
1007 MachineRegisterInfo &MRI = MF.getRegInfo();
1008 for (auto I = MRI.livein_begin(), E = MRI.livein_end(); I != E; ++I) {
1009 NodeAddr<PhiNode*> PA = newPhi(EA);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001010 RegisterRef RR = RegisterRef(I->first);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001011 uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving;
1012 NodeAddr<DefNode*> DA = newDef(PA, RR, PhiFlags);
1013 PA.Addr->addMember(DA, *this);
1014 }
1015
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001016 // Add phis for landing pads.
1017 // Landing pads, unlike usual backs blocks, are not entered through
1018 // branches in the program, or fall-throughs from other blocks. They
1019 // are entered from the exception handling runtime and target's ABI
1020 // may define certain registers as defined on entry to such a block.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001021 RegisterSet EHRegs = getLandingPadLiveIns();
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001022 if (!EHRegs.empty()) {
1023 for (NodeAddr<BlockNode*> BA : Blocks) {
1024 const MachineBasicBlock &B = *BA.Addr->getCode();
1025 if (!B.isEHPad())
1026 continue;
1027
1028 // Prepare a list of NodeIds of the block's predecessors.
1029 NodeList Preds;
1030 for (MachineBasicBlock *PB : B.predecessors())
1031 Preds.push_back(findBlock(PB));
1032
1033 // Build phi nodes for each live-in.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001034 for (RegisterRef RR : EHRegs) {
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001035 NodeAddr<PhiNode*> PA = newPhi(BA);
1036 uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving;
1037 // Add def:
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001038 NodeAddr<DefNode*> DA = newDef(PA, RR, PhiFlags);
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001039 PA.Addr->addMember(DA, *this);
1040 // Add uses (no reaching defs for phi uses):
1041 for (NodeAddr<BlockNode*> PBA : Preds) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001042 NodeAddr<PhiUseNode*> PUA = newPhiUse(PA, RR, PBA);
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001043 PA.Addr->addMember(PUA, *this);
1044 }
1045 }
1046 }
1047 }
1048
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001049 // Build a map "PhiM" which will contain, for each block, the set
1050 // of references that will require phi definitions in that block.
1051 BlockRefsMap PhiM;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001052 for (NodeAddr<BlockNode*> BA : Blocks)
1053 recordDefsForDF(PhiM, RefM, BA);
1054 for (NodeAddr<BlockNode*> BA : Blocks)
1055 buildPhis(PhiM, RefM, BA);
1056
1057 // Link all the refs. This will recursively traverse the dominator tree.
1058 DefStackMap DM;
1059 linkBlockRefs(DM, EA);
1060
1061 // Finally, remove all unused phi nodes.
Krzysztof Parzyszek55874cf2016-04-28 20:17:06 +00001062 if (!(Options & BuildOptions::KeepDeadPhis))
1063 removeUnusedPhis();
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001064}
1065
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001066RegisterRef DataFlowGraph::makeRegRef(unsigned Reg, unsigned Sub) const {
1067 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
Krzysztof Parzyszek775a2092016-10-14 19:06:25 +00001068 if (Sub != 0)
1069 Reg = TRI.getSubReg(Reg, Sub);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001070 return RegisterRef(Reg);
1071}
1072
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001073RegisterRef DataFlowGraph::normalizeRef(RegisterRef RR) const {
1074 // FIXME copied from RegisterAggr
1075 uint32_t SuperReg = RR.Reg;
1076 while (true) {
1077 MCSuperRegIterator SR(SuperReg, &TRI, false);
1078 if (!SR.isValid())
1079 break;
1080 SuperReg = *SR;
1081 }
1082
1083 uint32_t Sub = TRI.getSubRegIndex(SuperReg, RR.Reg);
1084 const TargetRegisterClass &RC = *TRI.getMinimalPhysRegClass(RR.Reg);
1085 LaneBitmask SuperMask = RR.Mask &
1086 TRI.composeSubRegIndexLaneMask(Sub, RC.LaneMask);
1087 return RegisterRef(SuperReg, SuperMask);
1088}
1089
1090RegisterRef DataFlowGraph::restrictRef(RegisterRef AR, RegisterRef BR) const {
1091 if (AR.Reg == BR.Reg) {
1092 LaneBitmask M = AR.Mask & BR.Mask;
1093 return M ? RegisterRef(AR.Reg, M) : RegisterRef();
1094 }
1095#ifndef NDEBUG
1096 RegisterRef NAR = normalizeRef(AR);
1097 RegisterRef NBR = normalizeRef(BR);
1098 assert(NAR.Reg != NBR.Reg);
1099#endif
1100 // This isn't strictly correct, because the overlap may happen in the
1101 // part masked out.
1102 if (TRI.regsOverlap(AR.Reg, BR.Reg))
1103 return AR;
1104 return RegisterRef();
1105}
1106
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001107// For each stack in the map DefM, push the delimiter for block B on it.
1108void DataFlowGraph::markBlock(NodeId B, DefStackMap &DefM) {
1109 // Push block delimiters.
1110 for (auto I = DefM.begin(), E = DefM.end(); I != E; ++I)
1111 I->second.start_block(B);
1112}
1113
1114// Remove all definitions coming from block B from each stack in DefM.
1115void DataFlowGraph::releaseBlock(NodeId B, DefStackMap &DefM) {
1116 // Pop all defs from this block from the definition stack. Defs that were
1117 // added to the map during the traversal of instructions will not have a
1118 // delimiter, but for those, the whole stack will be emptied.
1119 for (auto I = DefM.begin(), E = DefM.end(); I != E; ++I)
1120 I->second.clear_block(B);
1121
1122 // Finally, remove empty stacks from the map.
1123 for (auto I = DefM.begin(), E = DefM.end(), NextI = I; I != E; I = NextI) {
1124 NextI = std::next(I);
1125 // This preserves the validity of iterators other than I.
1126 if (I->second.empty())
1127 DefM.erase(I);
1128 }
1129}
1130
1131// Push all definitions from the instruction node IA to an appropriate
1132// stack in DefM.
1133void DataFlowGraph::pushDefs(NodeAddr<InstrNode*> IA, DefStackMap &DefM) {
1134 NodeList Defs = IA.Addr->members_if(IsDef, *this);
1135 NodeSet Visited;
1136#ifndef NDEBUG
1137 RegisterSet Defined;
1138#endif
1139
1140 // The important objectives of this function are:
1141 // - to be able to handle instructions both while the graph is being
1142 // constructed, and after the graph has been constructed, and
1143 // - maintain proper ordering of definitions on the stack for each
1144 // register reference:
1145 // - if there are two or more related defs in IA (i.e. coming from
1146 // the same machine operand), then only push one def on the stack,
1147 // - if there are multiple unrelated defs of non-overlapping
1148 // subregisters of S, then the stack for S will have both (in an
1149 // unspecified order), but the order does not matter from the data-
1150 // -flow perspective.
1151
1152 for (NodeAddr<DefNode*> DA : Defs) {
1153 if (Visited.count(DA.Id))
1154 continue;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001155
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001156 NodeList Rel = getRelatedRefs(IA, DA);
1157 NodeAddr<DefNode*> PDA = Rel.front();
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001158 RegisterRef RR = PDA.Addr->getRegRef(*this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001159#ifndef NDEBUG
1160 // Assert if the register is defined in two or more unrelated defs.
1161 // This could happen if there are two or more def operands defining it.
1162 if (!Defined.insert(RR).second) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001163 MachineInstr *MI = NodeAddr<StmtNode*>(IA).Addr->getCode();
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001164 dbgs() << "Multiple definitions of register: "
1165 << Print<RegisterRef>(RR, *this) << " in\n " << *MI
1166 << "in BB#" << MI->getParent()->getNumber() << '\n';
1167 llvm_unreachable(nullptr);
1168 }
1169#endif
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001170 // Push the definition on the stack for the register and all aliases.
1171 // The def stack traversal in linkNodeUp will check the exact aliasing.
1172 DefM[RR.Reg].push(DA);
1173 for (RegisterRef A : getAliasSet(RR.Reg /*FIXME? use RegisterRef*/)) {
1174 // Check that we don't push the same def twice.
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001175 assert(A != RR);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001176 DefM[A.Reg].push(DA);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001177 }
1178 // Mark all the related defs as visited.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001179 for (NodeAddr<NodeBase*> T : Rel)
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001180 Visited.insert(T.Id);
1181 }
1182}
1183
1184// Return the list of all reference nodes related to RA, including RA itself.
1185// See "getNextRelated" for the meaning of a "related reference".
1186NodeList DataFlowGraph::getRelatedRefs(NodeAddr<InstrNode*> IA,
1187 NodeAddr<RefNode*> RA) const {
1188 assert(IA.Id != 0 && RA.Id != 0);
1189
1190 NodeList Refs;
1191 NodeId Start = RA.Id;
1192 do {
1193 Refs.push_back(RA);
1194 RA = getNextRelated(IA, RA);
1195 } while (RA.Id != 0 && RA.Id != Start);
1196 return Refs;
1197}
1198
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001199// Return true if RA and RB overlap, false otherwise.
1200bool DataFlowGraph::alias(RegisterRef RA, RegisterRef RB) const {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001201 assert(TargetRegisterInfo::isPhysicalRegister(RA.Reg));
1202 assert(TargetRegisterInfo::isPhysicalRegister(RB.Reg));
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001203
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001204 MCRegUnitMaskIterator UMA(RA.Reg, &TRI);
1205 MCRegUnitMaskIterator UMB(RB.Reg, &TRI);
1206 // Reg units are returned in the numerical order.
1207 while (UMA.isValid() && UMB.isValid()) {
1208 std::pair<uint32_t,LaneBitmask> PA = *UMA;
1209 std::pair<uint32_t,LaneBitmask> PB = *UMB;
1210 // If the returned lane mask is 0, it should be treated as ~0
1211 // (or the lane mask from the given register ref should be ignored).
1212 // This can happen when a register has only one unit.
1213 if (PA.first == PB.first) {
1214 if (!PA.second || !PB.second || (PA.second & PB.second))
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001215 return true;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001216 ++UMA;
1217 ++UMB;
1218 continue;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001219 }
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001220 if (PA.first < PB.first)
1221 ++UMA;
1222 else if (PB.first < PA.first)
1223 ++UMB;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001224 }
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001225 return false;
1226}
1227
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001228
1229// Clear all information in the graph.
1230void DataFlowGraph::reset() {
1231 Memory.clear();
Krzysztof Parzyszek047149f2016-07-22 16:09:47 +00001232 BlockNodes.clear();
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001233 Func = NodeAddr<FuncNode*>();
1234}
1235
1236
1237// Return the next reference node in the instruction node IA that is related
1238// to RA. Conceptually, two reference nodes are related if they refer to the
1239// same instance of a register access, but differ in flags or other minor
1240// characteristics. Specific examples of related nodes are shadow reference
1241// nodes.
1242// Return the equivalent of nullptr if there are no more related references.
1243NodeAddr<RefNode*> DataFlowGraph::getNextRelated(NodeAddr<InstrNode*> IA,
1244 NodeAddr<RefNode*> RA) const {
1245 assert(IA.Id != 0 && RA.Id != 0);
1246
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001247 auto Related = [this,RA](NodeAddr<RefNode*> TA) -> bool {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001248 if (TA.Addr->getKind() != RA.Addr->getKind())
1249 return false;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001250 if (TA.Addr->getRegRef(*this) != RA.Addr->getRegRef(*this))
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001251 return false;
1252 return true;
1253 };
1254 auto RelatedStmt = [&Related,RA](NodeAddr<RefNode*> TA) -> bool {
1255 return Related(TA) &&
1256 &RA.Addr->getOp() == &TA.Addr->getOp();
1257 };
1258 auto RelatedPhi = [&Related,RA](NodeAddr<RefNode*> TA) -> bool {
1259 if (!Related(TA))
1260 return false;
1261 if (TA.Addr->getKind() != NodeAttrs::Use)
1262 return true;
1263 // For phi uses, compare predecessor blocks.
1264 const NodeAddr<const PhiUseNode*> TUA = TA;
1265 const NodeAddr<const PhiUseNode*> RUA = RA;
1266 return TUA.Addr->getPredecessor() == RUA.Addr->getPredecessor();
1267 };
1268
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001269 RegisterRef RR = RA.Addr->getRegRef(*this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001270 if (IA.Addr->getKind() == NodeAttrs::Stmt)
1271 return RA.Addr->getNextRef(RR, RelatedStmt, true, *this);
1272 return RA.Addr->getNextRef(RR, RelatedPhi, true, *this);
1273}
1274
1275// Find the next node related to RA in IA that satisfies condition P.
1276// If such a node was found, return a pair where the second element is the
1277// located node. If such a node does not exist, return a pair where the
1278// first element is the element after which such a node should be inserted,
1279// and the second element is a null-address.
1280template <typename Predicate>
1281std::pair<NodeAddr<RefNode*>,NodeAddr<RefNode*>>
1282DataFlowGraph::locateNextRef(NodeAddr<InstrNode*> IA, NodeAddr<RefNode*> RA,
1283 Predicate P) const {
1284 assert(IA.Id != 0 && RA.Id != 0);
1285
1286 NodeAddr<RefNode*> NA;
1287 NodeId Start = RA.Id;
1288 while (true) {
1289 NA = getNextRelated(IA, RA);
1290 if (NA.Id == 0 || NA.Id == Start)
1291 break;
1292 if (P(NA))
1293 break;
1294 RA = NA;
1295 }
1296
1297 if (NA.Id != 0 && NA.Id != Start)
1298 return std::make_pair(RA, NA);
1299 return std::make_pair(RA, NodeAddr<RefNode*>());
1300}
1301
1302// Get the next shadow node in IA corresponding to RA, and optionally create
1303// such a node if it does not exist.
1304NodeAddr<RefNode*> DataFlowGraph::getNextShadow(NodeAddr<InstrNode*> IA,
1305 NodeAddr<RefNode*> RA, bool Create) {
1306 assert(IA.Id != 0 && RA.Id != 0);
1307
1308 uint16_t Flags = RA.Addr->getFlags() | NodeAttrs::Shadow;
1309 auto IsShadow = [Flags] (NodeAddr<RefNode*> TA) -> bool {
1310 return TA.Addr->getFlags() == Flags;
1311 };
1312 auto Loc = locateNextRef(IA, RA, IsShadow);
1313 if (Loc.second.Id != 0 || !Create)
1314 return Loc.second;
1315
1316 // Create a copy of RA and mark is as shadow.
1317 NodeAddr<RefNode*> NA = cloneNode(RA);
1318 NA.Addr->setFlags(Flags | NodeAttrs::Shadow);
1319 IA.Addr->addMemberAfter(Loc.first, NA, *this);
1320 return NA;
1321}
1322
1323// Get the next shadow node in IA corresponding to RA. Return null-address
1324// if such a node does not exist.
1325NodeAddr<RefNode*> DataFlowGraph::getNextShadow(NodeAddr<InstrNode*> IA,
1326 NodeAddr<RefNode*> RA) const {
1327 assert(IA.Id != 0 && RA.Id != 0);
1328 uint16_t Flags = RA.Addr->getFlags() | NodeAttrs::Shadow;
1329 auto IsShadow = [Flags] (NodeAddr<RefNode*> TA) -> bool {
1330 return TA.Addr->getFlags() == Flags;
1331 };
1332 return locateNextRef(IA, RA, IsShadow).second;
1333}
1334
1335// Create a new statement node in the block node BA that corresponds to
1336// the machine instruction MI.
1337void DataFlowGraph::buildStmt(NodeAddr<BlockNode*> BA, MachineInstr &In) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001338 NodeAddr<StmtNode*> SA = newStmt(BA, &In);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001339
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +00001340 auto isCall = [] (const MachineInstr &In) -> bool {
1341 if (In.isCall())
1342 return true;
1343 // Is tail call?
1344 if (In.isBranch())
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001345 for (const MachineOperand &Op : In.operands())
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +00001346 if (Op.isGlobal() || Op.isSymbol())
1347 return true;
1348 return false;
1349 };
1350
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001351 auto isDefUndef = [this] (const MachineInstr &In, RegisterRef DR) -> bool {
1352 // This instruction defines DR. Check if there is a use operand that
1353 // would make DR live on entry to the instruction.
1354 for (const MachineOperand &UseOp : In.operands()) {
1355 if (!UseOp.isReg() || !UseOp.isUse() || UseOp.isUndef())
1356 continue;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001357 RegisterRef UR = makeRegRef(UseOp.getReg(), UseOp.getSubReg());
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001358 if (alias(DR, UR))
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001359 return false;
1360 }
1361 return true;
1362 };
1363
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001364 // Collect a set of registers that this instruction implicitly uses
1365 // or defines. Implicit operands from an instruction will be ignored
1366 // unless they are listed here.
1367 RegisterSet ImpUses, ImpDefs;
1368 if (const uint16_t *ImpD = In.getDesc().getImplicitDefs())
1369 while (uint16_t R = *ImpD++)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001370 ImpDefs.insert(RegisterRef(R));
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001371 if (const uint16_t *ImpU = In.getDesc().getImplicitUses())
1372 while (uint16_t R = *ImpU++)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001373 ImpUses.insert(RegisterRef(R));
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001374
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +00001375 bool IsCall = isCall(In);
1376 bool NeedsImplicit = IsCall || In.isInlineAsm() || In.isReturn();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001377 bool IsPredicated = TII.isPredicated(In);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001378 unsigned NumOps = In.getNumOperands();
1379
1380 // Avoid duplicate implicit defs. This will not detect cases of implicit
1381 // defs that define registers that overlap, but it is not clear how to
1382 // interpret that in the absence of explicit defs. Overlapping explicit
1383 // defs are likely illegal already.
1384 RegisterSet DoneDefs;
1385 // Process explicit defs first.
1386 for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1387 MachineOperand &Op = In.getOperand(OpN);
1388 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1389 continue;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001390 RegisterRef RR = makeRegRef(Op.getReg(), Op.getSubReg());
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001391 uint16_t Flags = NodeAttrs::None;
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001392 if (TOI.isPreserving(In, OpN)) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001393 Flags |= NodeAttrs::Preserving;
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001394 // If the def is preserving, check if it is also undefined.
1395 if (isDefUndef(In, RR))
1396 Flags |= NodeAttrs::Undef;
1397 }
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001398 if (TOI.isClobbering(In, OpN))
1399 Flags |= NodeAttrs::Clobbering;
1400 if (TOI.isFixedReg(In, OpN))
1401 Flags |= NodeAttrs::Fixed;
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +00001402 if (IsCall && Op.isDead())
1403 Flags |= NodeAttrs::Dead;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001404 NodeAddr<DefNode*> DA = newDef(SA, Op, Flags);
1405 SA.Addr->addMember(DA, *this);
1406 DoneDefs.insert(RR);
1407 }
1408
1409 // Process implicit defs, skipping those that have already been added
1410 // as explicit.
1411 for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1412 MachineOperand &Op = In.getOperand(OpN);
1413 if (!Op.isReg() || !Op.isDef() || !Op.isImplicit())
1414 continue;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001415 RegisterRef RR = makeRegRef(Op.getReg(), Op.getSubReg());
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +00001416 if (!NeedsImplicit && !ImpDefs.count(RR))
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001417 continue;
1418 if (DoneDefs.count(RR))
1419 continue;
1420 uint16_t Flags = NodeAttrs::None;
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001421 if (TOI.isPreserving(In, OpN)) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001422 Flags |= NodeAttrs::Preserving;
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001423 // If the def is preserving, check if it is also undefined.
1424 if (isDefUndef(In, RR))
1425 Flags |= NodeAttrs::Undef;
1426 }
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001427 if (TOI.isClobbering(In, OpN))
1428 Flags |= NodeAttrs::Clobbering;
1429 if (TOI.isFixedReg(In, OpN))
1430 Flags |= NodeAttrs::Fixed;
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +00001431 if (IsCall && Op.isDead())
1432 Flags |= NodeAttrs::Dead;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001433 NodeAddr<DefNode*> DA = newDef(SA, Op, Flags);
1434 SA.Addr->addMember(DA, *this);
1435 DoneDefs.insert(RR);
1436 }
1437
1438 for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1439 MachineOperand &Op = In.getOperand(OpN);
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001440 if (!Op.isReg() || !Op.isUse())
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001441 continue;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001442 RegisterRef RR = makeRegRef(Op.getReg(), Op.getSubReg());
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001443 // Add implicit uses on return and call instructions, and on predicated
1444 // instructions regardless of whether or not they appear in the instruction
1445 // descriptor's list.
1446 bool Implicit = Op.isImplicit();
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +00001447 bool TakeImplicit = NeedsImplicit || IsPredicated;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001448 if (Implicit && !TakeImplicit && !ImpUses.count(RR))
1449 continue;
1450 uint16_t Flags = NodeAttrs::None;
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001451 if (Op.isUndef())
1452 Flags |= NodeAttrs::Undef;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001453 if (TOI.isFixedReg(In, OpN))
1454 Flags |= NodeAttrs::Fixed;
1455 NodeAddr<UseNode*> UA = newUse(SA, Op, Flags);
1456 SA.Addr->addMember(UA, *this);
1457 }
1458}
1459
1460// Build a map that for each block will have the set of all references from
1461// that block, and from all blocks dominated by it.
1462void DataFlowGraph::buildBlockRefs(NodeAddr<BlockNode*> BA,
1463 BlockRefsMap &RefM) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001464 RegisterSet &Refs = RefM[BA.Id];
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001465 MachineDomTreeNode *N = MDT.getNode(BA.Addr->getCode());
1466 assert(N);
1467 for (auto I : *N) {
1468 MachineBasicBlock *SB = I->getBlock();
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001469 NodeAddr<BlockNode*> SBA = findBlock(SB);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001470 buildBlockRefs(SBA, RefM);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001471 const RegisterSet &RefsS = RefM[SBA.Id];
1472 Refs.insert(RefsS.begin(), RefsS.end());
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001473 }
1474
1475 for (NodeAddr<InstrNode*> IA : BA.Addr->members(*this))
1476 for (NodeAddr<RefNode*> RA : IA.Addr->members(*this))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001477 Refs.insert(RA.Addr->getRegRef(*this));
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001478}
1479
1480// Scan all defs in the block node BA and record in PhiM the locations of
1481// phi nodes corresponding to these defs.
1482void DataFlowGraph::recordDefsForDF(BlockRefsMap &PhiM, BlockRefsMap &RefM,
1483 NodeAddr<BlockNode*> BA) {
1484 // Check all defs from block BA and record them in each block in BA's
1485 // iterated dominance frontier. This information will later be used to
1486 // create phi nodes.
1487 MachineBasicBlock *BB = BA.Addr->getCode();
1488 assert(BB);
1489 auto DFLoc = MDF.find(BB);
1490 if (DFLoc == MDF.end() || DFLoc->second.empty())
1491 return;
1492
1493 // Traverse all instructions in the block and collect the set of all
1494 // defined references. For each reference there will be a phi created
1495 // in the block's iterated dominance frontier.
1496 // This is done to make sure that each defined reference gets only one
1497 // phi node, even if it is defined multiple times.
1498 RegisterSet Defs;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001499 for (NodeAddr<InstrNode*> IA : BA.Addr->members(*this))
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001500 for (NodeAddr<RefNode*> RA : IA.Addr->members_if(IsDef, *this))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001501 Defs.insert(RA.Addr->getRegRef(*this));
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001502
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001503 // Calculate the iterated dominance frontier of BB.
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001504 const MachineDominanceFrontier::DomSetType &DF = DFLoc->second;
1505 SetVector<MachineBasicBlock*> IDF(DF.begin(), DF.end());
1506 for (unsigned i = 0; i < IDF.size(); ++i) {
1507 auto F = MDF.find(IDF[i]);
1508 if (F != MDF.end())
1509 IDF.insert(F->second.begin(), F->second.end());
1510 }
1511
1512 // Get the register references that are reachable from this block.
1513 RegisterSet &Refs = RefM[BA.Id];
1514 for (auto DB : IDF) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001515 NodeAddr<BlockNode*> DBA = findBlock(DB);
1516 const RegisterSet &RefsD = RefM[DBA.Id];
1517 Refs.insert(RefsD.begin(), RefsD.end());
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001518 }
1519
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001520 // Finally, add the set of defs to each block in the iterated dominance
1521 // frontier.
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001522 for (auto DB : IDF) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001523 NodeAddr<BlockNode*> DBA = findBlock(DB);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001524 PhiM[DBA.Id].insert(Defs.begin(), Defs.end());
1525 }
1526}
1527
1528// Given the locations of phi nodes in the map PhiM, create the phi nodes
1529// that are located in the block node BA.
1530void DataFlowGraph::buildPhis(BlockRefsMap &PhiM, BlockRefsMap &RefM,
1531 NodeAddr<BlockNode*> BA) {
1532 // Check if this blocks has any DF defs, i.e. if there are any defs
1533 // that this block is in the iterated dominance frontier of.
1534 auto HasDF = PhiM.find(BA.Id);
1535 if (HasDF == PhiM.end() || HasDF->second.empty())
1536 return;
1537
1538 // First, remove all R in Refs in such that there exists T in Refs
1539 // such that T covers R. In other words, only leave those refs that
1540 // are not covered by another ref (i.e. maximal with respect to covering).
1541
1542 auto MaxCoverIn = [this] (RegisterRef RR, RegisterSet &RRs) -> RegisterRef {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001543 for (RegisterRef I : RRs)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001544 if (I != RR && RegisterAggr::isCoverOf(I, RR, TRI))
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001545 RR = I;
1546 return RR;
1547 };
1548
1549 RegisterSet MaxDF;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001550 for (RegisterRef I : HasDF->second)
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001551 MaxDF.insert(MaxCoverIn(I, HasDF->second));
1552
1553 std::vector<RegisterRef> MaxRefs;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001554 RegisterSet &RefB = RefM[BA.Id];
1555 for (RegisterRef I : MaxDF)
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001556 MaxRefs.push_back(MaxCoverIn(I, RefB));
1557
1558 // Now, for each R in MaxRefs, get the alias closure of R. If the closure
1559 // only has R in it, create a phi a def for R. Otherwise, create a phi,
1560 // and add a def for each S in the closure.
1561
1562 // Sort the refs so that the phis will be created in a deterministic order.
1563 std::sort(MaxRefs.begin(), MaxRefs.end());
1564 // Remove duplicates.
1565 auto NewEnd = std::unique(MaxRefs.begin(), MaxRefs.end());
1566 MaxRefs.erase(NewEnd, MaxRefs.end());
1567
1568 auto Aliased = [this,&MaxRefs](RegisterRef RR,
1569 std::vector<unsigned> &Closure) -> bool {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001570 for (unsigned I : Closure)
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001571 if (alias(RR, MaxRefs[I]))
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001572 return true;
1573 return false;
1574 };
1575
1576 // Prepare a list of NodeIds of the block's predecessors.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001577 NodeList Preds;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001578 const MachineBasicBlock *MBB = BA.Addr->getCode();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001579 for (MachineBasicBlock *PB : MBB->predecessors())
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001580 Preds.push_back(findBlock(PB));
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001581
1582 while (!MaxRefs.empty()) {
1583 // Put the first element in the closure, and then add all subsequent
1584 // elements from MaxRefs to it, if they alias at least one element
1585 // already in the closure.
1586 // ClosureIdx: vector of indices in MaxRefs of members of the closure.
1587 std::vector<unsigned> ClosureIdx = { 0 };
1588 for (unsigned i = 1; i != MaxRefs.size(); ++i)
1589 if (Aliased(MaxRefs[i], ClosureIdx))
1590 ClosureIdx.push_back(i);
1591
1592 // Build a phi for the closure.
1593 unsigned CS = ClosureIdx.size();
1594 NodeAddr<PhiNode*> PA = newPhi(BA);
1595
1596 // Add defs.
1597 for (unsigned X = 0; X != CS; ++X) {
1598 RegisterRef RR = MaxRefs[ClosureIdx[X]];
1599 uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving;
1600 NodeAddr<DefNode*> DA = newDef(PA, RR, PhiFlags);
1601 PA.Addr->addMember(DA, *this);
1602 }
1603 // Add phi uses.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001604 for (NodeAddr<BlockNode*> PBA : Preds) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001605 for (unsigned X = 0; X != CS; ++X) {
1606 RegisterRef RR = MaxRefs[ClosureIdx[X]];
1607 NodeAddr<PhiUseNode*> PUA = newPhiUse(PA, RR, PBA);
1608 PA.Addr->addMember(PUA, *this);
1609 }
1610 }
1611
1612 // Erase from MaxRefs all elements in the closure.
1613 auto Begin = MaxRefs.begin();
1614 for (unsigned i = ClosureIdx.size(); i != 0; --i)
1615 MaxRefs.erase(Begin + ClosureIdx[i-1]);
1616 }
1617}
1618
1619// Remove any unneeded phi nodes that were created during the build process.
1620void DataFlowGraph::removeUnusedPhis() {
1621 // This will remove unused phis, i.e. phis where each def does not reach
1622 // any uses or other defs. This will not detect or remove circular phi
1623 // chains that are otherwise dead. Unused/dead phis are created during
1624 // the build process and this function is intended to remove these cases
1625 // that are easily determinable to be unnecessary.
1626
1627 SetVector<NodeId> PhiQ;
1628 for (NodeAddr<BlockNode*> BA : Func.Addr->members(*this)) {
1629 for (auto P : BA.Addr->members_if(IsPhi, *this))
1630 PhiQ.insert(P.Id);
1631 }
1632
1633 static auto HasUsedDef = [](NodeList &Ms) -> bool {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001634 for (NodeAddr<NodeBase*> M : Ms) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001635 if (M.Addr->getKind() != NodeAttrs::Def)
1636 continue;
1637 NodeAddr<DefNode*> DA = M;
1638 if (DA.Addr->getReachedDef() != 0 || DA.Addr->getReachedUse() != 0)
1639 return true;
1640 }
1641 return false;
1642 };
1643
1644 // Any phi, if it is removed, may affect other phis (make them dead).
1645 // For each removed phi, collect the potentially affected phis and add
1646 // them back to the queue.
1647 while (!PhiQ.empty()) {
1648 auto PA = addr<PhiNode*>(PhiQ[0]);
1649 PhiQ.remove(PA.Id);
1650 NodeList Refs = PA.Addr->members(*this);
1651 if (HasUsedDef(Refs))
1652 continue;
1653 for (NodeAddr<RefNode*> RA : Refs) {
1654 if (NodeId RD = RA.Addr->getReachingDef()) {
1655 auto RDA = addr<DefNode*>(RD);
1656 NodeAddr<InstrNode*> OA = RDA.Addr->getOwner(*this);
1657 if (IsPhi(OA))
1658 PhiQ.insert(OA.Id);
1659 }
1660 if (RA.Addr->isDef())
Krzysztof Parzyszek69e670d52016-01-18 20:41:34 +00001661 unlinkDef(RA, true);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001662 else
Krzysztof Parzyszek69e670d52016-01-18 20:41:34 +00001663 unlinkUse(RA, true);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001664 }
1665 NodeAddr<BlockNode*> BA = PA.Addr->getOwner(*this);
1666 BA.Addr->removeMember(PA, *this);
1667 }
1668}
1669
1670// For a given reference node TA in an instruction node IA, connect the
1671// reaching def of TA to the appropriate def node. Create any shadow nodes
1672// as appropriate.
1673template <typename T>
1674void DataFlowGraph::linkRefUp(NodeAddr<InstrNode*> IA, NodeAddr<T> TA,
1675 DefStack &DS) {
1676 if (DS.empty())
1677 return;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001678 RegisterRef RR = TA.Addr->getRegRef(*this);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001679 NodeAddr<T> TAP;
1680
1681 // References from the def stack that have been examined so far.
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001682 RegisterAggr Defs(TRI);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001683
1684 for (auto I = DS.top(), E = DS.bottom(); I != E; I.down()) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001685 RegisterRef QR = I->Addr->getRegRef(*this);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001686
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001687 // Skip all defs that are aliased to any of the defs that we have already
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001688 // seen. If this completes a cover of RR, stop the stack traversal.
1689 bool Alias = Defs.hasAliasOf(QR);
1690 bool Cover = Defs.insert(QR).hasCoverOf(RR);
1691 if (Alias) {
1692 if (Cover)
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001693 break;
1694 continue;
1695 }
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001696
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001697 // The reaching def.
1698 NodeAddr<DefNode*> RDA = *I;
1699
1700 // Pick the reached node.
1701 if (TAP.Id == 0) {
1702 TAP = TA;
1703 } else {
1704 // Mark the existing ref as "shadow" and create a new shadow.
1705 TAP.Addr->setFlags(TAP.Addr->getFlags() | NodeAttrs::Shadow);
1706 TAP = getNextShadow(IA, TAP, true);
1707 }
1708
1709 // Create the link.
1710 TAP.Addr->linkToDef(TAP.Id, RDA);
1711
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001712 if (Cover)
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001713 break;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001714 }
1715}
1716
1717// Create data-flow links for all reference nodes in the statement node SA.
1718void DataFlowGraph::linkStmtRefs(DefStackMap &DefM, NodeAddr<StmtNode*> SA) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001719#ifndef NDEBUG
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001720 RegisterSet Defs;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001721#endif
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001722
1723 // Link all nodes (upwards in the data-flow) with their reaching defs.
1724 for (NodeAddr<RefNode*> RA : SA.Addr->members(*this)) {
1725 uint16_t Kind = RA.Addr->getKind();
1726 assert(Kind == NodeAttrs::Def || Kind == NodeAttrs::Use);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001727 RegisterRef RR = RA.Addr->getRegRef(*this);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001728#ifndef NDEBUG
1729 // Do not expect multiple defs of the same reference.
1730 assert(Kind != NodeAttrs::Def || !Defs.count(RR));
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001731 Defs.insert(RR);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001732#endif
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001733
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001734 auto F = DefM.find(RR.Reg);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001735 if (F == DefM.end())
1736 continue;
1737 DefStack &DS = F->second;
1738 if (Kind == NodeAttrs::Use)
1739 linkRefUp<UseNode*>(SA, RA, DS);
1740 else if (Kind == NodeAttrs::Def)
1741 linkRefUp<DefNode*>(SA, RA, DS);
1742 else
1743 llvm_unreachable("Unexpected node in instruction");
1744 }
1745}
1746
1747// Create data-flow links for all instructions in the block node BA. This
1748// will include updating any phi nodes in BA.
1749void DataFlowGraph::linkBlockRefs(DefStackMap &DefM, NodeAddr<BlockNode*> BA) {
1750 // Push block delimiters.
1751 markBlock(BA.Id, DefM);
1752
Krzysztof Parzyszek89757432016-05-05 22:00:44 +00001753 assert(BA.Addr && "block node address is needed to create a data-flow link");
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001754 // For each non-phi instruction in the block, link all the defs and uses
1755 // to their reaching defs. For any member of the block (including phis),
1756 // push the defs on the corresponding stacks.
1757 for (NodeAddr<InstrNode*> IA : BA.Addr->members(*this)) {
1758 // Ignore phi nodes here. They will be linked part by part from the
1759 // predecessors.
1760 if (IA.Addr->getKind() == NodeAttrs::Stmt)
1761 linkStmtRefs(DefM, IA);
1762
1763 // Push the definitions on the stack.
1764 pushDefs(IA, DefM);
1765 }
1766
1767 // Recursively process all children in the dominator tree.
1768 MachineDomTreeNode *N = MDT.getNode(BA.Addr->getCode());
1769 for (auto I : *N) {
1770 MachineBasicBlock *SB = I->getBlock();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001771 NodeAddr<BlockNode*> SBA = findBlock(SB);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001772 linkBlockRefs(DefM, SBA);
1773 }
1774
1775 // Link the phi uses from the successor blocks.
1776 auto IsUseForBA = [BA](NodeAddr<NodeBase*> NA) -> bool {
1777 if (NA.Addr->getKind() != NodeAttrs::Use)
1778 return false;
1779 assert(NA.Addr->getFlags() & NodeAttrs::PhiRef);
1780 NodeAddr<PhiUseNode*> PUA = NA;
1781 return PUA.Addr->getPredecessor() == BA.Id;
1782 };
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001783
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001784 RegisterSet EHLiveIns = getLandingPadLiveIns();
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001785 MachineBasicBlock *MBB = BA.Addr->getCode();
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001786
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001787 for (MachineBasicBlock *SB : MBB->successors()) {
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001788 bool IsEHPad = SB->isEHPad();
1789 NodeAddr<BlockNode*> SBA = findBlock(SB);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001790 for (NodeAddr<InstrNode*> IA : SBA.Addr->members_if(IsPhi, *this)) {
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001791 // Do not link phi uses for landing pad live-ins.
1792 if (IsEHPad) {
1793 // Find what register this phi is for.
1794 NodeAddr<RefNode*> RA = IA.Addr->getFirstMember(*this);
1795 assert(RA.Id != 0);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001796 if (EHLiveIns.count(RA.Addr->getRegRef(*this)))
Krzysztof Parzyszek1d322202016-09-27 18:18:44 +00001797 continue;
1798 }
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001799 // Go over each phi use associated with MBB, and link it.
1800 for (auto U : IA.Addr->members_if(IsUseForBA, *this)) {
1801 NodeAddr<PhiUseNode*> PUA = U;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001802 RegisterRef RR = PUA.Addr->getRegRef(*this);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +00001803 linkRefUp<UseNode*>(IA, PUA, DefM[RR.Reg]);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001804 }
1805 }
1806 }
1807
1808 // Pop all defs from this block from the definition stacks.
1809 releaseBlock(BA.Id, DefM);
1810}
1811
1812// Remove the use node UA from any data-flow and structural links.
Krzysztof Parzyszek69e670d52016-01-18 20:41:34 +00001813void DataFlowGraph::unlinkUseDF(NodeAddr<UseNode*> UA) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001814 NodeId RD = UA.Addr->getReachingDef();
1815 NodeId Sib = UA.Addr->getSibling();
1816
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001817 if (RD == 0) {
1818 assert(Sib == 0);
1819 return;
1820 }
1821
1822 auto RDA = addr<DefNode*>(RD);
1823 auto TA = addr<UseNode*>(RDA.Addr->getReachedUse());
1824 if (TA.Id == UA.Id) {
1825 RDA.Addr->setReachedUse(Sib);
1826 return;
1827 }
1828
1829 while (TA.Id != 0) {
1830 NodeId S = TA.Addr->getSibling();
1831 if (S == UA.Id) {
1832 TA.Addr->setSibling(UA.Addr->getSibling());
1833 return;
1834 }
1835 TA = addr<UseNode*>(S);
1836 }
1837}
1838
1839// Remove the def node DA from any data-flow and structural links.
Krzysztof Parzyszek69e670d52016-01-18 20:41:34 +00001840void DataFlowGraph::unlinkDefDF(NodeAddr<DefNode*> DA) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001841 //
1842 // RD
1843 // | reached
1844 // | def
1845 // :
1846 // .
1847 // +----+
1848 // ... -- | DA | -- ... -- 0 : sibling chain of DA
1849 // +----+
1850 // | | reached
1851 // | : def
1852 // | .
1853 // | ... : Siblings (defs)
1854 // |
1855 // : reached
1856 // . use
1857 // ... : sibling chain of reached uses
1858
1859 NodeId RD = DA.Addr->getReachingDef();
1860
1861 // Visit all siblings of the reached def and reset their reaching defs.
1862 // Also, defs reached by DA are now "promoted" to being reached by RD,
1863 // so all of them will need to be spliced into the sibling chain where
1864 // DA belongs.
1865 auto getAllNodes = [this] (NodeId N) -> NodeList {
1866 NodeList Res;
1867 while (N) {
1868 auto RA = addr<RefNode*>(N);
1869 // Keep the nodes in the exact sibling order.
1870 Res.push_back(RA);
1871 N = RA.Addr->getSibling();
1872 }
1873 return Res;
1874 };
1875 NodeList ReachedDefs = getAllNodes(DA.Addr->getReachedDef());
1876 NodeList ReachedUses = getAllNodes(DA.Addr->getReachedUse());
1877
1878 if (RD == 0) {
1879 for (NodeAddr<RefNode*> I : ReachedDefs)
1880 I.Addr->setSibling(0);
1881 for (NodeAddr<RefNode*> I : ReachedUses)
1882 I.Addr->setSibling(0);
1883 }
1884 for (NodeAddr<DefNode*> I : ReachedDefs)
1885 I.Addr->setReachingDef(RD);
1886 for (NodeAddr<UseNode*> I : ReachedUses)
1887 I.Addr->setReachingDef(RD);
1888
1889 NodeId Sib = DA.Addr->getSibling();
1890 if (RD == 0) {
1891 assert(Sib == 0);
1892 return;
1893 }
1894
1895 // Update the reaching def node and remove DA from the sibling list.
1896 auto RDA = addr<DefNode*>(RD);
1897 auto TA = addr<DefNode*>(RDA.Addr->getReachedDef());
1898 if (TA.Id == DA.Id) {
1899 // If DA is the first reached def, just update the RD's reached def
1900 // to the DA's sibling.
1901 RDA.Addr->setReachedDef(Sib);
1902 } else {
1903 // Otherwise, traverse the sibling list of the reached defs and remove
1904 // DA from it.
1905 while (TA.Id != 0) {
1906 NodeId S = TA.Addr->getSibling();
1907 if (S == DA.Id) {
1908 TA.Addr->setSibling(Sib);
1909 break;
1910 }
1911 TA = addr<DefNode*>(S);
1912 }
1913 }
1914
1915 // Splice the DA's reached defs into the RDA's reached def chain.
1916 if (!ReachedDefs.empty()) {
1917 auto Last = NodeAddr<DefNode*>(ReachedDefs.back());
1918 Last.Addr->setSibling(RDA.Addr->getReachedDef());
1919 RDA.Addr->setReachedDef(ReachedDefs.front().Id);
1920 }
1921 // Splice the DA's reached uses into the RDA's reached use chain.
1922 if (!ReachedUses.empty()) {
1923 auto Last = NodeAddr<UseNode*>(ReachedUses.back());
1924 Last.Addr->setSibling(RDA.Addr->getReachedUse());
1925 RDA.Addr->setReachedUse(ReachedUses.front().Id);
1926 }
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001927}