blob: ce3cf7b7611870559fc63f556c0fa5b25c5c3256 [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"
21#include "llvm/Target/TargetRegisterInfo.h"
22
23using namespace llvm;
24using namespace rdf;
25
26// Printing functions. Have them here first, so that the rest of the code
27// can use them.
28namespace rdf {
29
30template<>
31raw_ostream &operator<< (raw_ostream &OS, const Print<RegisterRef> &P) {
32 auto &TRI = P.G.getTRI();
33 if (P.Obj.Reg > 0 && P.Obj.Reg < TRI.getNumRegs())
34 OS << TRI.getName(P.Obj.Reg);
35 else
36 OS << '#' << P.Obj.Reg;
37 if (P.Obj.Sub > 0) {
38 OS << ':';
39 if (P.Obj.Sub < TRI.getNumSubRegIndices())
40 OS << TRI.getSubRegIndexName(P.Obj.Sub);
41 else
42 OS << '#' << P.Obj.Sub;
43 }
44 return OS;
45}
46
47template<>
48raw_ostream &operator<< (raw_ostream &OS, const Print<NodeId> &P) {
49 auto NA = P.G.addr<NodeBase*>(P.Obj);
50 uint16_t Attrs = NA.Addr->getAttrs();
51 uint16_t Kind = NodeAttrs::kind(Attrs);
52 uint16_t Flags = NodeAttrs::flags(Attrs);
53 switch (NodeAttrs::type(Attrs)) {
54 case NodeAttrs::Code:
55 switch (Kind) {
56 case NodeAttrs::Func: OS << 'f'; break;
57 case NodeAttrs::Block: OS << 'b'; break;
58 case NodeAttrs::Stmt: OS << 's'; break;
59 case NodeAttrs::Phi: OS << 'p'; break;
60 default: OS << "c?"; break;
61 }
62 break;
63 case NodeAttrs::Ref:
64 if (Flags & NodeAttrs::Preserving)
65 OS << '+';
66 if (Flags & NodeAttrs::Clobbering)
67 OS << '~';
68 switch (Kind) {
69 case NodeAttrs::Use: OS << 'u'; break;
70 case NodeAttrs::Def: OS << 'd'; break;
71 case NodeAttrs::Block: OS << 'b'; break;
72 default: OS << "r?"; break;
73 }
74 break;
75 default:
76 OS << '?';
77 break;
78 }
79 OS << P.Obj;
80 if (Flags & NodeAttrs::Shadow)
81 OS << '"';
82 return OS;
83}
84
85namespace {
86 void printRefHeader(raw_ostream &OS, const NodeAddr<RefNode*> RA,
87 const DataFlowGraph &G) {
88 OS << Print<NodeId>(RA.Id, G) << '<'
89 << Print<RegisterRef>(RA.Addr->getRegRef(), G) << '>';
90 if (RA.Addr->getFlags() & NodeAttrs::Fixed)
91 OS << '!';
92 }
93}
94
95template<>
96raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<DefNode*>> &P) {
97 printRefHeader(OS, P.Obj, P.G);
98 OS << '(';
99 if (NodeId N = P.Obj.Addr->getReachingDef())
100 OS << Print<NodeId>(N, P.G);
101 OS << ',';
102 if (NodeId N = P.Obj.Addr->getReachedDef())
103 OS << Print<NodeId>(N, P.G);
104 OS << ',';
105 if (NodeId N = P.Obj.Addr->getReachedUse())
106 OS << Print<NodeId>(N, P.G);
107 OS << "):";
108 if (NodeId N = P.Obj.Addr->getSibling())
109 OS << Print<NodeId>(N, P.G);
110 return OS;
111}
112
113template<>
114raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<UseNode*>> &P) {
115 printRefHeader(OS, P.Obj, P.G);
116 OS << '(';
117 if (NodeId N = P.Obj.Addr->getReachingDef())
118 OS << Print<NodeId>(N, P.G);
119 OS << "):";
120 if (NodeId N = P.Obj.Addr->getSibling())
121 OS << Print<NodeId>(N, P.G);
122 return OS;
123}
124
125template<>
126raw_ostream &operator<< (raw_ostream &OS,
127 const Print<NodeAddr<PhiUseNode*>> &P) {
128 printRefHeader(OS, P.Obj, P.G);
129 OS << '(';
130 if (NodeId N = P.Obj.Addr->getReachingDef())
131 OS << Print<NodeId>(N, P.G);
132 OS << ',';
133 if (NodeId N = P.Obj.Addr->getPredecessor())
134 OS << Print<NodeId>(N, P.G);
135 OS << "):";
136 if (NodeId N = P.Obj.Addr->getSibling())
137 OS << Print<NodeId>(N, P.G);
138 return OS;
139}
140
141template<>
142raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<RefNode*>> &P) {
143 switch (P.Obj.Addr->getKind()) {
144 case NodeAttrs::Def:
145 OS << PrintNode<DefNode*>(P.Obj, P.G);
146 break;
147 case NodeAttrs::Use:
148 if (P.Obj.Addr->getFlags() & NodeAttrs::PhiRef)
149 OS << PrintNode<PhiUseNode*>(P.Obj, P.G);
150 else
151 OS << PrintNode<UseNode*>(P.Obj, P.G);
152 break;
153 }
154 return OS;
155}
156
157template<>
158raw_ostream &operator<< (raw_ostream &OS, const Print<NodeList> &P) {
159 unsigned N = P.Obj.size();
160 for (auto I : P.Obj) {
161 OS << Print<NodeId>(I.Id, P.G);
162 if (--N)
163 OS << ' ';
164 }
165 return OS;
166}
167
168template<>
169raw_ostream &operator<< (raw_ostream &OS, const Print<NodeSet> &P) {
170 unsigned N = P.Obj.size();
171 for (auto I : P.Obj) {
172 OS << Print<NodeId>(I, P.G);
173 if (--N)
174 OS << ' ';
175 }
176 return OS;
177}
178
179namespace {
180 template <typename T>
181 struct PrintListV {
182 PrintListV(const NodeList &L, const DataFlowGraph &G) : List(L), G(G) {}
183 typedef T Type;
184 const NodeList &List;
185 const DataFlowGraph &G;
186 };
187
188 template <typename T>
189 raw_ostream &operator<< (raw_ostream &OS, const PrintListV<T> &P) {
190 unsigned N = P.List.size();
191 for (NodeAddr<T> A : P.List) {
192 OS << PrintNode<T>(A, P.G);
193 if (--N)
194 OS << ", ";
195 }
196 return OS;
197 }
198}
199
200template<>
201raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<PhiNode*>> &P) {
202 OS << Print<NodeId>(P.Obj.Id, P.G) << ": phi ["
203 << PrintListV<RefNode*>(P.Obj.Addr->members(P.G), P.G) << ']';
204 return OS;
205}
206
207template<>
208raw_ostream &operator<< (raw_ostream &OS,
209 const Print<NodeAddr<StmtNode*>> &P) {
210 unsigned Opc = P.Obj.Addr->getCode()->getOpcode();
211 OS << Print<NodeId>(P.Obj.Id, P.G) << ": " << P.G.getTII().getName(Opc)
212 << " [" << PrintListV<RefNode*>(P.Obj.Addr->members(P.G), P.G) << ']';
213 return OS;
214}
215
216template<>
217raw_ostream &operator<< (raw_ostream &OS,
218 const Print<NodeAddr<InstrNode*>> &P) {
219 switch (P.Obj.Addr->getKind()) {
220 case NodeAttrs::Phi:
221 OS << PrintNode<PhiNode*>(P.Obj, P.G);
222 break;
223 case NodeAttrs::Stmt:
224 OS << PrintNode<StmtNode*>(P.Obj, P.G);
225 break;
226 default:
227 OS << "instr? " << Print<NodeId>(P.Obj.Id, P.G);
228 break;
229 }
230 return OS;
231}
232
233template<>
234raw_ostream &operator<< (raw_ostream &OS,
235 const Print<NodeAddr<BlockNode*>> &P) {
236 auto *BB = P.Obj.Addr->getCode();
237 unsigned NP = BB->pred_size();
238 std::vector<int> Ns;
239 auto PrintBBs = [&OS,&P] (std::vector<int> Ns) -> void {
240 unsigned N = Ns.size();
241 for (auto I : Ns) {
242 OS << "BB#" << I;
243 if (--N)
244 OS << ", ";
245 }
246 };
247
248 OS << Print<NodeId>(P.Obj.Id, P.G) << ": === BB#" << BB->getNumber()
249 << " === preds(" << NP << "): ";
250 for (auto I : BB->predecessors())
251 Ns.push_back(I->getNumber());
252 PrintBBs(Ns);
253
254 unsigned NS = BB->succ_size();
255 OS << " succs(" << NS << "): ";
256 Ns.clear();
257 for (auto I : BB->successors())
258 Ns.push_back(I->getNumber());
259 PrintBBs(Ns);
260 OS << '\n';
261
262 for (auto I : P.Obj.Addr->members(P.G))
263 OS << PrintNode<InstrNode*>(I, P.G) << '\n';
264 return OS;
265}
266
267template<>
268raw_ostream &operator<< (raw_ostream &OS,
269 const Print<NodeAddr<FuncNode*>> &P) {
270 OS << "DFG dump:[\n" << Print<NodeId>(P.Obj.Id, P.G) << ": Function: "
271 << P.Obj.Addr->getCode()->getName() << '\n';
272 for (auto I : P.Obj.Addr->members(P.G))
273 OS << PrintNode<BlockNode*>(I, P.G) << '\n';
274 OS << "]\n";
275 return OS;
276}
277
278template<>
279raw_ostream &operator<< (raw_ostream &OS, const Print<RegisterSet> &P) {
280 OS << '{';
281 for (auto I : P.Obj)
282 OS << ' ' << Print<RegisterRef>(I, P.G);
283 OS << " }";
284 return OS;
285}
286
287template<>
288raw_ostream &operator<< (raw_ostream &OS,
289 const Print<DataFlowGraph::DefStack> &P) {
290 for (auto I = P.Obj.top(), E = P.Obj.bottom(); I != E; ) {
291 OS << Print<NodeId>(I->Id, P.G)
292 << '<' << Print<RegisterRef>(I->Addr->getRegRef(), P.G) << '>';
293 I.down();
294 if (I != E)
295 OS << ' ';
296 }
297 return OS;
298}
299
300} // namespace rdf
301
302// Node allocation functions.
303//
304// Node allocator is like a slab memory allocator: it allocates blocks of
305// memory in sizes that are multiples of the size of a node. Each block has
306// the same size. Nodes are allocated from the currently active block, and
307// when it becomes full, a new one is created.
308// There is a mapping scheme between node id and its location in a block,
309// and within that block is described in the header file.
310//
311void NodeAllocator::startNewBlock() {
312 void *T = MemPool.Allocate(NodesPerBlock*NodeMemSize, NodeMemSize);
313 char *P = static_cast<char*>(T);
314 Blocks.push_back(P);
315 // Check if the block index is still within the allowed range, i.e. less
316 // than 2^N, where N is the number of bits in NodeId for the block index.
317 // BitsPerIndex is the number of bits per node index.
Simon Pilgrim99c6c292016-01-18 21:11:19 +0000318 assert((Blocks.size() < ((size_t)1 << (8*sizeof(NodeId)-BitsPerIndex))) &&
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000319 "Out of bits for block index");
320 ActiveEnd = P;
321}
322
323bool NodeAllocator::needNewBlock() {
324 if (Blocks.empty())
325 return true;
326
327 char *ActiveBegin = Blocks.back();
328 uint32_t Index = (ActiveEnd-ActiveBegin)/NodeMemSize;
329 return Index >= NodesPerBlock;
330}
331
332NodeAddr<NodeBase*> NodeAllocator::New() {
333 if (needNewBlock())
334 startNewBlock();
335
336 uint32_t ActiveB = Blocks.size()-1;
337 uint32_t Index = (ActiveEnd - Blocks[ActiveB])/NodeMemSize;
338 NodeAddr<NodeBase*> NA = { reinterpret_cast<NodeBase*>(ActiveEnd),
339 makeId(ActiveB, Index) };
340 ActiveEnd += NodeMemSize;
341 return NA;
342}
343
344NodeId NodeAllocator::id(const NodeBase *P) const {
345 uintptr_t A = reinterpret_cast<uintptr_t>(P);
346 for (unsigned i = 0, n = Blocks.size(); i != n; ++i) {
347 uintptr_t B = reinterpret_cast<uintptr_t>(Blocks[i]);
348 if (A < B || A >= B + NodesPerBlock*NodeMemSize)
349 continue;
350 uint32_t Idx = (A-B)/NodeMemSize;
351 return makeId(i, Idx);
352 }
353 llvm_unreachable("Invalid node address");
354}
355
356void NodeAllocator::clear() {
357 MemPool.Reset();
358 Blocks.clear();
359 ActiveEnd = nullptr;
360}
361
362
363// Insert node NA after "this" in the circular chain.
364void NodeBase::append(NodeAddr<NodeBase*> NA) {
365 NodeId Nx = Next;
366 // If NA is already "next", do nothing.
367 if (Next != NA.Id) {
368 Next = NA.Id;
369 NA.Addr->Next = Nx;
370 }
371}
372
373
374// Fundamental node manipulator functions.
375
376// Obtain the register reference from a reference node.
377RegisterRef RefNode::getRegRef() const {
378 assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
379 if (NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef)
380 return Ref.RR;
381 assert(Ref.Op != nullptr);
382 return { Ref.Op->getReg(), Ref.Op->getSubReg() };
383}
384
385// Set the register reference in the reference node directly (for references
386// in phi nodes).
387void RefNode::setRegRef(RegisterRef RR) {
388 assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
389 assert(NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef);
390 Ref.RR = RR;
391}
392
393// Set the register reference in the reference node based on a machine
394// operand (for references in statement nodes).
395void RefNode::setRegRef(MachineOperand *Op) {
396 assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
397 assert(!(NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef));
398 Ref.Op = Op;
399}
400
401// Get the owner of a given reference node.
402NodeAddr<NodeBase*> RefNode::getOwner(const DataFlowGraph &G) {
403 NodeAddr<NodeBase*> NA = G.addr<NodeBase*>(getNext());
404
405 while (NA.Addr != this) {
406 if (NA.Addr->getType() == NodeAttrs::Code)
407 return NA;
408 NA = G.addr<NodeBase*>(NA.Addr->getNext());
409 }
410 llvm_unreachable("No owner in circular list");
411}
412
413// Connect the def node to the reaching def node.
414void DefNode::linkToDef(NodeId Self, NodeAddr<DefNode*> DA) {
415 Ref.RD = DA.Id;
416 Ref.Sib = DA.Addr->getReachedDef();
417 DA.Addr->setReachedDef(Self);
418}
419
420// Connect the use node to the reaching def node.
421void UseNode::linkToDef(NodeId Self, NodeAddr<DefNode*> DA) {
422 Ref.RD = DA.Id;
423 Ref.Sib = DA.Addr->getReachedUse();
424 DA.Addr->setReachedUse(Self);
425}
426
427// Get the first member of the code node.
428NodeAddr<NodeBase*> CodeNode::getFirstMember(const DataFlowGraph &G) const {
429 if (Code.FirstM == 0)
430 return NodeAddr<NodeBase*>();
431 return G.addr<NodeBase*>(Code.FirstM);
432}
433
434// Get the last member of the code node.
435NodeAddr<NodeBase*> CodeNode::getLastMember(const DataFlowGraph &G) const {
436 if (Code.LastM == 0)
437 return NodeAddr<NodeBase*>();
438 return G.addr<NodeBase*>(Code.LastM);
439}
440
441// Add node NA at the end of the member list of the given code node.
442void CodeNode::addMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G) {
443 auto ML = getLastMember(G);
444 if (ML.Id != 0) {
445 ML.Addr->append(NA);
446 } else {
447 Code.FirstM = NA.Id;
448 NodeId Self = G.id(this);
449 NA.Addr->setNext(Self);
450 }
451 Code.LastM = NA.Id;
452}
453
454// Add node NA after member node MA in the given code node.
455void CodeNode::addMemberAfter(NodeAddr<NodeBase*> MA, NodeAddr<NodeBase*> NA,
456 const DataFlowGraph &G) {
457 MA.Addr->append(NA);
458 if (Code.LastM == MA.Id)
459 Code.LastM = NA.Id;
460}
461
462// Remove member node NA from the given code node.
463void CodeNode::removeMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G) {
464 auto MA = getFirstMember(G);
465 assert(MA.Id != 0);
466
467 // Special handling if the member to remove is the first member.
468 if (MA.Id == NA.Id) {
469 if (Code.LastM == MA.Id) {
470 // If it is the only member, set both first and last to 0.
471 Code.FirstM = Code.LastM = 0;
472 } else {
473 // Otherwise, advance the first member.
474 Code.FirstM = MA.Addr->getNext();
475 }
476 return;
477 }
478
479 while (MA.Addr != this) {
480 NodeId MX = MA.Addr->getNext();
481 if (MX == NA.Id) {
482 MA.Addr->setNext(NA.Addr->getNext());
483 // If the member to remove happens to be the last one, update the
484 // LastM indicator.
485 if (Code.LastM == NA.Id)
486 Code.LastM = MA.Id;
487 return;
488 }
489 MA = G.addr<NodeBase*>(MX);
490 }
491 llvm_unreachable("No such member");
492}
493
494// Return the list of all members of the code node.
495NodeList CodeNode::members(const DataFlowGraph &G) const {
496 static auto True = [] (NodeAddr<NodeBase*>) -> bool { return true; };
497 return members_if(True, G);
498}
499
500// Return the owner of the given instr node.
501NodeAddr<NodeBase*> InstrNode::getOwner(const DataFlowGraph &G) {
502 NodeAddr<NodeBase*> NA = G.addr<NodeBase*>(getNext());
503
504 while (NA.Addr != this) {
505 assert(NA.Addr->getType() == NodeAttrs::Code);
506 if (NA.Addr->getKind() == NodeAttrs::Block)
507 return NA;
508 NA = G.addr<NodeBase*>(NA.Addr->getNext());
509 }
510 llvm_unreachable("No owner in circular list");
511}
512
513// Add the phi node PA to the given block node.
514void BlockNode::addPhi(NodeAddr<PhiNode*> PA, const DataFlowGraph &G) {
515 auto M = getFirstMember(G);
516 if (M.Id == 0) {
517 addMember(PA, G);
518 return;
519 }
520
521 assert(M.Addr->getType() == NodeAttrs::Code);
522 if (M.Addr->getKind() == NodeAttrs::Stmt) {
523 // If the first member of the block is a statement, insert the phi as
524 // the first member.
525 Code.FirstM = PA.Id;
526 PA.Addr->setNext(M.Id);
527 } else {
528 // If the first member is a phi, find the last phi, and append PA to it.
529 assert(M.Addr->getKind() == NodeAttrs::Phi);
530 NodeAddr<NodeBase*> MN = M;
531 do {
532 M = MN;
533 MN = G.addr<NodeBase*>(M.Addr->getNext());
534 assert(MN.Addr->getType() == NodeAttrs::Code);
535 } while (MN.Addr->getKind() == NodeAttrs::Phi);
536
537 // M is the last phi.
538 addMemberAfter(M, PA, G);
539 }
540}
541
542// Find the block node corresponding to the machine basic block BB in the
543// given func node.
544NodeAddr<BlockNode*> FuncNode::findBlock(const MachineBasicBlock *BB,
545 const DataFlowGraph &G) const {
546 auto EqBB = [BB] (NodeAddr<NodeBase*> NA) -> bool {
547 return NodeAddr<BlockNode*>(NA).Addr->getCode() == BB;
548 };
549 NodeList Ms = members_if(EqBB, G);
550 if (!Ms.empty())
551 return Ms[0];
552 return NodeAddr<BlockNode*>();
553}
554
555// Get the block node for the entry block in the given function.
556NodeAddr<BlockNode*> FuncNode::getEntryBlock(const DataFlowGraph &G) {
557 MachineBasicBlock *EntryB = &getCode()->front();
558 return findBlock(EntryB, G);
559}
560
561
562// Register aliasing information.
563//
564// In theory, the lane information could be used to determine register
565// covering (and aliasing), but depending on the sub-register structure,
566// the lane mask information may be missing. The covering information
567// must be available for this framework to work, so relying solely on
568// the lane data is not sufficient.
569
570// Determine whether RA covers RB.
571bool RegisterAliasInfo::covers(RegisterRef RA, RegisterRef RB) const {
572 if (RA == RB)
573 return true;
574 if (TargetRegisterInfo::isVirtualRegister(RA.Reg)) {
575 assert(TargetRegisterInfo::isVirtualRegister(RB.Reg));
576 if (RA.Reg != RB.Reg)
577 return false;
578 if (RA.Sub == 0)
579 return true;
580 return TRI.composeSubRegIndices(RA.Sub, RB.Sub) == RA.Sub;
581 }
582
583 assert(TargetRegisterInfo::isPhysicalRegister(RA.Reg) &&
584 TargetRegisterInfo::isPhysicalRegister(RB.Reg));
585 unsigned A = RA.Sub != 0 ? TRI.getSubReg(RA.Reg, RA.Sub) : RA.Reg;
586 unsigned B = RB.Sub != 0 ? TRI.getSubReg(RB.Reg, RB.Sub) : RB.Reg;
587 return TRI.isSubRegister(A, B);
588}
589
590// Determine whether RR is covered by the set of references RRs.
591bool RegisterAliasInfo::covers(const RegisterSet &RRs, RegisterRef RR) const {
592 if (RRs.count(RR))
593 return true;
594
595 // For virtual registers, we cannot accurately determine covering based
596 // on subregisters. If RR itself is not present in RRs, but it has a sub-
597 // register reference, check for the super-register alone. Otherwise,
598 // assume non-covering.
599 if (TargetRegisterInfo::isVirtualRegister(RR.Reg)) {
600 if (RR.Sub != 0)
601 return RRs.count({RR.Reg, 0});
602 return false;
603 }
604
605 // If any super-register of RR is present, then RR is covered.
606 unsigned Reg = RR.Sub == 0 ? RR.Reg : TRI.getSubReg(RR.Reg, RR.Sub);
607 for (MCSuperRegIterator SR(Reg, &TRI); SR.isValid(); ++SR)
608 if (RRs.count({*SR, 0}))
609 return true;
610
611 return false;
612}
613
614// Get the list of references aliased to RR.
615std::vector<RegisterRef> RegisterAliasInfo::getAliasSet(RegisterRef RR) const {
616 // Do not include RR in the alias set. For virtual registers return an
617 // empty set.
618 std::vector<RegisterRef> AS;
619 if (TargetRegisterInfo::isVirtualRegister(RR.Reg))
620 return AS;
621 assert(TargetRegisterInfo::isPhysicalRegister(RR.Reg));
622 unsigned R = RR.Reg;
623 if (RR.Sub)
624 R = TRI.getSubReg(RR.Reg, RR.Sub);
625
626 for (MCRegAliasIterator AI(R, &TRI, false); AI.isValid(); ++AI)
627 AS.push_back(RegisterRef({*AI, 0}));
628 return AS;
629}
630
631// Check whether RA and RB are aliased.
632bool RegisterAliasInfo::alias(RegisterRef RA, RegisterRef RB) const {
633 bool VirtA = TargetRegisterInfo::isVirtualRegister(RA.Reg);
634 bool VirtB = TargetRegisterInfo::isVirtualRegister(RB.Reg);
635 bool PhysA = TargetRegisterInfo::isPhysicalRegister(RA.Reg);
636 bool PhysB = TargetRegisterInfo::isPhysicalRegister(RB.Reg);
637
638 if (VirtA != VirtB)
639 return false;
640
641 if (VirtA) {
642 if (RA.Reg != RB.Reg)
643 return false;
644 // RA and RB refer to the same register. If any of them refer to the
645 // whole register, they must be aliased.
646 if (RA.Sub == 0 || RB.Sub == 0)
647 return true;
648 unsigned SA = TRI.getSubRegIdxSize(RA.Sub);
649 unsigned OA = TRI.getSubRegIdxOffset(RA.Sub);
650 unsigned SB = TRI.getSubRegIdxSize(RB.Sub);
651 unsigned OB = TRI.getSubRegIdxOffset(RB.Sub);
652 if (OA <= OB && OA+SA > OB)
653 return true;
654 if (OB <= OA && OB+SB > OA)
655 return true;
656 return false;
657 }
658
659 assert(PhysA && PhysB);
660 (void)PhysA, (void)PhysB;
661 unsigned A = RA.Sub ? TRI.getSubReg(RA.Reg, RA.Sub) : RA.Reg;
662 unsigned B = RB.Sub ? TRI.getSubReg(RB.Reg, RB.Sub) : RB.Reg;
663 for (MCRegAliasIterator I(A, &TRI, true); I.isValid(); ++I)
664 if (B == *I)
665 return true;
666 return false;
667}
668
669
670// Target operand information.
671//
672
673// For a given instruction, check if there are any bits of RR that can remain
674// unchanged across this def.
675bool TargetOperandInfo::isPreserving(const MachineInstr &In, unsigned OpNum)
676 const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000677 return TII.isPredicated(In);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000678}
679
680// Check if the definition of RR produces an unspecified value.
681bool TargetOperandInfo::isClobbering(const MachineInstr &In, unsigned OpNum)
682 const {
683 if (In.isCall())
684 if (In.getOperand(OpNum).isImplicit())
685 return true;
686 return false;
687}
688
Krzysztof Parzyszekc5a4e262016-04-28 20:33:33 +0000689// Check if the given instruction specifically requires
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000690bool TargetOperandInfo::isFixedReg(const MachineInstr &In, unsigned OpNum)
691 const {
Krzysztof Parzyszekc5a4e262016-04-28 20:33:33 +0000692 if (In.isCall() || In.isReturn() || In.isInlineAsm())
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000693 return true;
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +0000694 // Check for a tail call.
695 if (In.isBranch())
696 for (auto &O : In.operands())
697 if (O.isGlobal() || O.isSymbol())
698 return true;
699
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000700 const MCInstrDesc &D = In.getDesc();
701 if (!D.getImplicitDefs() && !D.getImplicitUses())
702 return false;
703 const MachineOperand &Op = In.getOperand(OpNum);
704 // If there is a sub-register, treat the operand as non-fixed. Currently,
705 // fixed registers are those that are listed in the descriptor as implicit
706 // uses or defs, and those lists do not allow sub-registers.
707 if (Op.getSubReg() != 0)
708 return false;
709 unsigned Reg = Op.getReg();
710 const MCPhysReg *ImpR = Op.isDef() ? D.getImplicitDefs()
711 : D.getImplicitUses();
712 if (!ImpR)
713 return false;
714 while (*ImpR)
715 if (*ImpR++ == Reg)
716 return true;
717 return false;
718}
719
720
721//
722// The data flow graph construction.
723//
724
725DataFlowGraph::DataFlowGraph(MachineFunction &mf, const TargetInstrInfo &tii,
726 const TargetRegisterInfo &tri, const MachineDominatorTree &mdt,
727 const MachineDominanceFrontier &mdf, const RegisterAliasInfo &rai,
728 const TargetOperandInfo &toi)
729 : TimeG("rdf"), MF(mf), TII(tii), TRI(tri), MDT(mdt), MDF(mdf), RAI(rai),
730 TOI(toi) {
731}
732
733
734// The implementation of the definition stack.
735// Each register reference has its own definition stack. In particular,
736// for a register references "Reg" and "Reg:subreg" will each have their
737// own definition stacks.
738
739// Construct a stack iterator.
740DataFlowGraph::DefStack::Iterator::Iterator(const DataFlowGraph::DefStack &S,
741 bool Top) : DS(S) {
742 if (!Top) {
743 // Initialize to bottom.
744 Pos = 0;
745 return;
746 }
747 // Initialize to the top, i.e. top-most non-delimiter (or 0, if empty).
748 Pos = DS.Stack.size();
749 while (Pos > 0 && DS.isDelimiter(DS.Stack[Pos-1]))
750 Pos--;
751}
752
753// Return the size of the stack, including block delimiters.
754unsigned DataFlowGraph::DefStack::size() const {
755 unsigned S = 0;
756 for (auto I = top(), E = bottom(); I != E; I.down())
757 S++;
758 return S;
759}
760
761// Remove the top entry from the stack. Remove all intervening delimiters
762// so that after this, the stack is either empty, or the top of the stack
763// is a non-delimiter.
764void DataFlowGraph::DefStack::pop() {
765 assert(!empty());
766 unsigned P = nextDown(Stack.size());
767 Stack.resize(P);
768}
769
770// Push a delimiter for block node N on the stack.
771void DataFlowGraph::DefStack::start_block(NodeId N) {
772 assert(N != 0);
773 Stack.push_back(NodeAddr<DefNode*>(nullptr, N));
774}
775
776// Remove all nodes from the top of the stack, until the delimited for
777// block node N is encountered. Remove the delimiter as well. In effect,
778// this will remove from the stack all definitions from block N.
779void DataFlowGraph::DefStack::clear_block(NodeId N) {
780 assert(N != 0);
781 unsigned P = Stack.size();
782 while (P > 0) {
783 bool Found = isDelimiter(Stack[P-1], N);
784 P--;
785 if (Found)
786 break;
787 }
788 // This will also remove the delimiter, if found.
789 Stack.resize(P);
790}
791
792// Move the stack iterator up by one.
793unsigned DataFlowGraph::DefStack::nextUp(unsigned P) const {
794 // Get the next valid position after P (skipping all delimiters).
795 // The input position P does not have to point to a non-delimiter.
796 unsigned SS = Stack.size();
797 bool IsDelim;
Krzysztof Parzyszek8dca45e2016-01-12 16:51:55 +0000798 assert(P < SS);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000799 do {
800 P++;
801 IsDelim = isDelimiter(Stack[P-1]);
802 } while (P < SS && IsDelim);
803 assert(!IsDelim);
804 return P;
805}
806
807// Move the stack iterator down by one.
808unsigned DataFlowGraph::DefStack::nextDown(unsigned P) const {
809 // Get the preceding valid position before P (skipping all delimiters).
810 // The input position P does not have to point to a non-delimiter.
811 assert(P > 0 && P <= Stack.size());
812 bool IsDelim = isDelimiter(Stack[P-1]);
813 do {
814 if (--P == 0)
815 break;
816 IsDelim = isDelimiter(Stack[P-1]);
817 } while (P > 0 && IsDelim);
818 assert(!IsDelim);
819 return P;
820}
821
822// Node management functions.
823
824// Get the pointer to the node with the id N.
825NodeBase *DataFlowGraph::ptr(NodeId N) const {
826 if (N == 0)
827 return nullptr;
828 return Memory.ptr(N);
829}
830
831// Get the id of the node at the address P.
832NodeId DataFlowGraph::id(const NodeBase *P) const {
833 if (P == nullptr)
834 return 0;
835 return Memory.id(P);
836}
837
838// Allocate a new node and set the attributes to Attrs.
839NodeAddr<NodeBase*> DataFlowGraph::newNode(uint16_t Attrs) {
840 NodeAddr<NodeBase*> P = Memory.New();
841 P.Addr->init();
842 P.Addr->setAttrs(Attrs);
843 return P;
844}
845
846// Make a copy of the given node B, except for the data-flow links, which
847// are set to 0.
848NodeAddr<NodeBase*> DataFlowGraph::cloneNode(const NodeAddr<NodeBase*> B) {
849 NodeAddr<NodeBase*> NA = newNode(0);
850 memcpy(NA.Addr, B.Addr, sizeof(NodeBase));
851 // Ref nodes need to have the data-flow links reset.
852 if (NA.Addr->getType() == NodeAttrs::Ref) {
853 NodeAddr<RefNode*> RA = NA;
854 RA.Addr->setReachingDef(0);
855 RA.Addr->setSibling(0);
856 if (NA.Addr->getKind() == NodeAttrs::Def) {
857 NodeAddr<DefNode*> DA = NA;
858 DA.Addr->setReachedDef(0);
859 DA.Addr->setReachedUse(0);
860 }
861 }
862 return NA;
863}
864
865
866// Allocation routines for specific node types/kinds.
867
868NodeAddr<UseNode*> DataFlowGraph::newUse(NodeAddr<InstrNode*> Owner,
869 MachineOperand &Op, uint16_t Flags) {
870 NodeAddr<UseNode*> UA = newNode(NodeAttrs::Ref | NodeAttrs::Use | Flags);
871 UA.Addr->setRegRef(&Op);
872 return UA;
873}
874
875NodeAddr<PhiUseNode*> DataFlowGraph::newPhiUse(NodeAddr<PhiNode*> Owner,
876 RegisterRef RR, NodeAddr<BlockNode*> PredB, uint16_t Flags) {
877 NodeAddr<PhiUseNode*> PUA = newNode(NodeAttrs::Ref | NodeAttrs::Use | Flags);
878 assert(Flags & NodeAttrs::PhiRef);
879 PUA.Addr->setRegRef(RR);
880 PUA.Addr->setPredecessor(PredB.Id);
881 return PUA;
882}
883
884NodeAddr<DefNode*> DataFlowGraph::newDef(NodeAddr<InstrNode*> Owner,
885 MachineOperand &Op, uint16_t Flags) {
886 NodeAddr<DefNode*> DA = newNode(NodeAttrs::Ref | NodeAttrs::Def | Flags);
887 DA.Addr->setRegRef(&Op);
888 return DA;
889}
890
891NodeAddr<DefNode*> DataFlowGraph::newDef(NodeAddr<InstrNode*> Owner,
892 RegisterRef RR, uint16_t Flags) {
893 NodeAddr<DefNode*> DA = newNode(NodeAttrs::Ref | NodeAttrs::Def | Flags);
894 assert(Flags & NodeAttrs::PhiRef);
895 DA.Addr->setRegRef(RR);
896 return DA;
897}
898
899NodeAddr<PhiNode*> DataFlowGraph::newPhi(NodeAddr<BlockNode*> Owner) {
900 NodeAddr<PhiNode*> PA = newNode(NodeAttrs::Code | NodeAttrs::Phi);
901 Owner.Addr->addPhi(PA, *this);
902 return PA;
903}
904
905NodeAddr<StmtNode*> DataFlowGraph::newStmt(NodeAddr<BlockNode*> Owner,
906 MachineInstr *MI) {
907 NodeAddr<StmtNode*> SA = newNode(NodeAttrs::Code | NodeAttrs::Stmt);
908 SA.Addr->setCode(MI);
909 Owner.Addr->addMember(SA, *this);
910 return SA;
911}
912
913NodeAddr<BlockNode*> DataFlowGraph::newBlock(NodeAddr<FuncNode*> Owner,
914 MachineBasicBlock *BB) {
915 NodeAddr<BlockNode*> BA = newNode(NodeAttrs::Code | NodeAttrs::Block);
916 BA.Addr->setCode(BB);
917 Owner.Addr->addMember(BA, *this);
918 return BA;
919}
920
921NodeAddr<FuncNode*> DataFlowGraph::newFunc(MachineFunction *MF) {
922 NodeAddr<FuncNode*> FA = newNode(NodeAttrs::Code | NodeAttrs::Func);
923 FA.Addr->setCode(MF);
924 return FA;
925}
926
927// Build the data flow graph.
Krzysztof Parzyszek55874cf2016-04-28 20:17:06 +0000928void DataFlowGraph::build(unsigned Options) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000929 reset();
930 Func = newFunc(&MF);
931
932 if (MF.empty())
933 return;
934
935 for (auto &B : MF) {
936 auto BA = newBlock(Func, &B);
937 for (auto &I : B) {
938 if (I.isDebugValue())
939 continue;
940 buildStmt(BA, I);
941 }
942 }
943
944 // Collect information about block references.
945 NodeAddr<BlockNode*> EA = Func.Addr->getEntryBlock(*this);
946 BlockRefsMap RefM;
947 buildBlockRefs(EA, RefM);
948
949 // Add function-entry phi nodes.
950 MachineRegisterInfo &MRI = MF.getRegInfo();
951 for (auto I = MRI.livein_begin(), E = MRI.livein_end(); I != E; ++I) {
952 NodeAddr<PhiNode*> PA = newPhi(EA);
953 RegisterRef RR = { I->first, 0 };
954 uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving;
955 NodeAddr<DefNode*> DA = newDef(PA, RR, PhiFlags);
956 PA.Addr->addMember(DA, *this);
957 }
958
959 // Build a map "PhiM" which will contain, for each block, the set
960 // of references that will require phi definitions in that block.
961 BlockRefsMap PhiM;
962 auto Blocks = Func.Addr->members(*this);
963 for (NodeAddr<BlockNode*> BA : Blocks)
964 recordDefsForDF(PhiM, RefM, BA);
965 for (NodeAddr<BlockNode*> BA : Blocks)
966 buildPhis(PhiM, RefM, BA);
967
968 // Link all the refs. This will recursively traverse the dominator tree.
969 DefStackMap DM;
970 linkBlockRefs(DM, EA);
971
972 // Finally, remove all unused phi nodes.
Krzysztof Parzyszek55874cf2016-04-28 20:17:06 +0000973 if (!(Options & BuildOptions::KeepDeadPhis))
974 removeUnusedPhis();
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +0000975}
976
977// For each stack in the map DefM, push the delimiter for block B on it.
978void DataFlowGraph::markBlock(NodeId B, DefStackMap &DefM) {
979 // Push block delimiters.
980 for (auto I = DefM.begin(), E = DefM.end(); I != E; ++I)
981 I->second.start_block(B);
982}
983
984// Remove all definitions coming from block B from each stack in DefM.
985void DataFlowGraph::releaseBlock(NodeId B, DefStackMap &DefM) {
986 // Pop all defs from this block from the definition stack. Defs that were
987 // added to the map during the traversal of instructions will not have a
988 // delimiter, but for those, the whole stack will be emptied.
989 for (auto I = DefM.begin(), E = DefM.end(); I != E; ++I)
990 I->second.clear_block(B);
991
992 // Finally, remove empty stacks from the map.
993 for (auto I = DefM.begin(), E = DefM.end(), NextI = I; I != E; I = NextI) {
994 NextI = std::next(I);
995 // This preserves the validity of iterators other than I.
996 if (I->second.empty())
997 DefM.erase(I);
998 }
999}
1000
1001// Push all definitions from the instruction node IA to an appropriate
1002// stack in DefM.
1003void DataFlowGraph::pushDefs(NodeAddr<InstrNode*> IA, DefStackMap &DefM) {
1004 NodeList Defs = IA.Addr->members_if(IsDef, *this);
1005 NodeSet Visited;
1006#ifndef NDEBUG
1007 RegisterSet Defined;
1008#endif
1009
1010 // The important objectives of this function are:
1011 // - to be able to handle instructions both while the graph is being
1012 // constructed, and after the graph has been constructed, and
1013 // - maintain proper ordering of definitions on the stack for each
1014 // register reference:
1015 // - if there are two or more related defs in IA (i.e. coming from
1016 // the same machine operand), then only push one def on the stack,
1017 // - if there are multiple unrelated defs of non-overlapping
1018 // subregisters of S, then the stack for S will have both (in an
1019 // unspecified order), but the order does not matter from the data-
1020 // -flow perspective.
1021
1022 for (NodeAddr<DefNode*> DA : Defs) {
1023 if (Visited.count(DA.Id))
1024 continue;
1025 NodeList Rel = getRelatedRefs(IA, DA);
1026 NodeAddr<DefNode*> PDA = Rel.front();
1027 // Push the definition on the stack for the register and all aliases.
1028 RegisterRef RR = PDA.Addr->getRegRef();
1029#ifndef NDEBUG
1030 // Assert if the register is defined in two or more unrelated defs.
1031 // This could happen if there are two or more def operands defining it.
1032 if (!Defined.insert(RR).second) {
1033 auto *MI = NodeAddr<StmtNode*>(IA).Addr->getCode();
1034 dbgs() << "Multiple definitions of register: "
1035 << Print<RegisterRef>(RR, *this) << " in\n " << *MI
1036 << "in BB#" << MI->getParent()->getNumber() << '\n';
1037 llvm_unreachable(nullptr);
1038 }
1039#endif
1040 DefM[RR].push(DA);
1041 for (auto A : RAI.getAliasSet(RR)) {
1042 assert(A != RR);
1043 DefM[A].push(DA);
1044 }
1045 // Mark all the related defs as visited.
1046 for (auto T : Rel)
1047 Visited.insert(T.Id);
1048 }
1049}
1050
1051// Return the list of all reference nodes related to RA, including RA itself.
1052// See "getNextRelated" for the meaning of a "related reference".
1053NodeList DataFlowGraph::getRelatedRefs(NodeAddr<InstrNode*> IA,
1054 NodeAddr<RefNode*> RA) const {
1055 assert(IA.Id != 0 && RA.Id != 0);
1056
1057 NodeList Refs;
1058 NodeId Start = RA.Id;
1059 do {
1060 Refs.push_back(RA);
1061 RA = getNextRelated(IA, RA);
1062 } while (RA.Id != 0 && RA.Id != Start);
1063 return Refs;
1064}
1065
1066
1067// Clear all information in the graph.
1068void DataFlowGraph::reset() {
1069 Memory.clear();
1070 Func = NodeAddr<FuncNode*>();
1071}
1072
1073
1074// Return the next reference node in the instruction node IA that is related
1075// to RA. Conceptually, two reference nodes are related if they refer to the
1076// same instance of a register access, but differ in flags or other minor
1077// characteristics. Specific examples of related nodes are shadow reference
1078// nodes.
1079// Return the equivalent of nullptr if there are no more related references.
1080NodeAddr<RefNode*> DataFlowGraph::getNextRelated(NodeAddr<InstrNode*> IA,
1081 NodeAddr<RefNode*> RA) const {
1082 assert(IA.Id != 0 && RA.Id != 0);
1083
1084 auto Related = [RA](NodeAddr<RefNode*> TA) -> bool {
1085 if (TA.Addr->getKind() != RA.Addr->getKind())
1086 return false;
1087 if (TA.Addr->getRegRef() != RA.Addr->getRegRef())
1088 return false;
1089 return true;
1090 };
1091 auto RelatedStmt = [&Related,RA](NodeAddr<RefNode*> TA) -> bool {
1092 return Related(TA) &&
1093 &RA.Addr->getOp() == &TA.Addr->getOp();
1094 };
1095 auto RelatedPhi = [&Related,RA](NodeAddr<RefNode*> TA) -> bool {
1096 if (!Related(TA))
1097 return false;
1098 if (TA.Addr->getKind() != NodeAttrs::Use)
1099 return true;
1100 // For phi uses, compare predecessor blocks.
1101 const NodeAddr<const PhiUseNode*> TUA = TA;
1102 const NodeAddr<const PhiUseNode*> RUA = RA;
1103 return TUA.Addr->getPredecessor() == RUA.Addr->getPredecessor();
1104 };
1105
1106 RegisterRef RR = RA.Addr->getRegRef();
1107 if (IA.Addr->getKind() == NodeAttrs::Stmt)
1108 return RA.Addr->getNextRef(RR, RelatedStmt, true, *this);
1109 return RA.Addr->getNextRef(RR, RelatedPhi, true, *this);
1110}
1111
1112// Find the next node related to RA in IA that satisfies condition P.
1113// If such a node was found, return a pair where the second element is the
1114// located node. If such a node does not exist, return a pair where the
1115// first element is the element after which such a node should be inserted,
1116// and the second element is a null-address.
1117template <typename Predicate>
1118std::pair<NodeAddr<RefNode*>,NodeAddr<RefNode*>>
1119DataFlowGraph::locateNextRef(NodeAddr<InstrNode*> IA, NodeAddr<RefNode*> RA,
1120 Predicate P) const {
1121 assert(IA.Id != 0 && RA.Id != 0);
1122
1123 NodeAddr<RefNode*> NA;
1124 NodeId Start = RA.Id;
1125 while (true) {
1126 NA = getNextRelated(IA, RA);
1127 if (NA.Id == 0 || NA.Id == Start)
1128 break;
1129 if (P(NA))
1130 break;
1131 RA = NA;
1132 }
1133
1134 if (NA.Id != 0 && NA.Id != Start)
1135 return std::make_pair(RA, NA);
1136 return std::make_pair(RA, NodeAddr<RefNode*>());
1137}
1138
1139// Get the next shadow node in IA corresponding to RA, and optionally create
1140// such a node if it does not exist.
1141NodeAddr<RefNode*> DataFlowGraph::getNextShadow(NodeAddr<InstrNode*> IA,
1142 NodeAddr<RefNode*> RA, bool Create) {
1143 assert(IA.Id != 0 && RA.Id != 0);
1144
1145 uint16_t Flags = RA.Addr->getFlags() | NodeAttrs::Shadow;
1146 auto IsShadow = [Flags] (NodeAddr<RefNode*> TA) -> bool {
1147 return TA.Addr->getFlags() == Flags;
1148 };
1149 auto Loc = locateNextRef(IA, RA, IsShadow);
1150 if (Loc.second.Id != 0 || !Create)
1151 return Loc.second;
1152
1153 // Create a copy of RA and mark is as shadow.
1154 NodeAddr<RefNode*> NA = cloneNode(RA);
1155 NA.Addr->setFlags(Flags | NodeAttrs::Shadow);
1156 IA.Addr->addMemberAfter(Loc.first, NA, *this);
1157 return NA;
1158}
1159
1160// Get the next shadow node in IA corresponding to RA. Return null-address
1161// if such a node does not exist.
1162NodeAddr<RefNode*> DataFlowGraph::getNextShadow(NodeAddr<InstrNode*> IA,
1163 NodeAddr<RefNode*> RA) const {
1164 assert(IA.Id != 0 && RA.Id != 0);
1165 uint16_t Flags = RA.Addr->getFlags() | NodeAttrs::Shadow;
1166 auto IsShadow = [Flags] (NodeAddr<RefNode*> TA) -> bool {
1167 return TA.Addr->getFlags() == Flags;
1168 };
1169 return locateNextRef(IA, RA, IsShadow).second;
1170}
1171
1172// Create a new statement node in the block node BA that corresponds to
1173// the machine instruction MI.
1174void DataFlowGraph::buildStmt(NodeAddr<BlockNode*> BA, MachineInstr &In) {
1175 auto SA = newStmt(BA, &In);
1176
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +00001177 auto isCall = [] (const MachineInstr &In) -> bool {
1178 if (In.isCall())
1179 return true;
1180 // Is tail call?
1181 if (In.isBranch())
1182 for (auto &Op : In.operands())
1183 if (Op.isGlobal() || Op.isSymbol())
1184 return true;
1185 return false;
1186 };
1187
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001188 // Collect a set of registers that this instruction implicitly uses
1189 // or defines. Implicit operands from an instruction will be ignored
1190 // unless they are listed here.
1191 RegisterSet ImpUses, ImpDefs;
1192 if (const uint16_t *ImpD = In.getDesc().getImplicitDefs())
1193 while (uint16_t R = *ImpD++)
1194 ImpDefs.insert({R, 0});
1195 if (const uint16_t *ImpU = In.getDesc().getImplicitUses())
1196 while (uint16_t R = *ImpU++)
1197 ImpUses.insert({R, 0});
1198
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +00001199 bool NeedsImplicit = isCall(In) || In.isInlineAsm() || In.isReturn();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001200 bool IsPredicated = TII.isPredicated(In);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001201 unsigned NumOps = In.getNumOperands();
1202
1203 // Avoid duplicate implicit defs. This will not detect cases of implicit
1204 // defs that define registers that overlap, but it is not clear how to
1205 // interpret that in the absence of explicit defs. Overlapping explicit
1206 // defs are likely illegal already.
1207 RegisterSet DoneDefs;
1208 // Process explicit defs first.
1209 for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1210 MachineOperand &Op = In.getOperand(OpN);
1211 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1212 continue;
1213 RegisterRef RR = { Op.getReg(), Op.getSubReg() };
1214 uint16_t Flags = NodeAttrs::None;
1215 if (TOI.isPreserving(In, OpN))
1216 Flags |= NodeAttrs::Preserving;
1217 if (TOI.isClobbering(In, OpN))
1218 Flags |= NodeAttrs::Clobbering;
1219 if (TOI.isFixedReg(In, OpN))
1220 Flags |= NodeAttrs::Fixed;
1221 NodeAddr<DefNode*> DA = newDef(SA, Op, Flags);
1222 SA.Addr->addMember(DA, *this);
1223 DoneDefs.insert(RR);
1224 }
1225
1226 // Process implicit defs, skipping those that have already been added
1227 // as explicit.
1228 for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1229 MachineOperand &Op = In.getOperand(OpN);
1230 if (!Op.isReg() || !Op.isDef() || !Op.isImplicit())
1231 continue;
1232 RegisterRef RR = { Op.getReg(), Op.getSubReg() };
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +00001233 if (!NeedsImplicit && !ImpDefs.count(RR))
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001234 continue;
1235 if (DoneDefs.count(RR))
1236 continue;
1237 uint16_t Flags = NodeAttrs::None;
1238 if (TOI.isPreserving(In, OpN))
1239 Flags |= NodeAttrs::Preserving;
1240 if (TOI.isClobbering(In, OpN))
1241 Flags |= NodeAttrs::Clobbering;
1242 if (TOI.isFixedReg(In, OpN))
1243 Flags |= NodeAttrs::Fixed;
1244 NodeAddr<DefNode*> DA = newDef(SA, Op, Flags);
1245 SA.Addr->addMember(DA, *this);
1246 DoneDefs.insert(RR);
1247 }
1248
1249 for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1250 MachineOperand &Op = In.getOperand(OpN);
1251 if (!Op.isReg() || !Op.isUse())
1252 continue;
1253 RegisterRef RR = { Op.getReg(), Op.getSubReg() };
1254 // Add implicit uses on return and call instructions, and on predicated
1255 // instructions regardless of whether or not they appear in the instruction
1256 // descriptor's list.
1257 bool Implicit = Op.isImplicit();
Krzysztof Parzyszekbf90d5a2016-04-28 20:40:08 +00001258 bool TakeImplicit = NeedsImplicit || IsPredicated;
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001259 if (Implicit && !TakeImplicit && !ImpUses.count(RR))
1260 continue;
1261 uint16_t Flags = NodeAttrs::None;
1262 if (TOI.isFixedReg(In, OpN))
1263 Flags |= NodeAttrs::Fixed;
1264 NodeAddr<UseNode*> UA = newUse(SA, Op, Flags);
1265 SA.Addr->addMember(UA, *this);
1266 }
1267}
1268
1269// Build a map that for each block will have the set of all references from
1270// that block, and from all blocks dominated by it.
1271void DataFlowGraph::buildBlockRefs(NodeAddr<BlockNode*> BA,
1272 BlockRefsMap &RefM) {
1273 auto &Refs = RefM[BA.Id];
1274 MachineDomTreeNode *N = MDT.getNode(BA.Addr->getCode());
1275 assert(N);
1276 for (auto I : *N) {
1277 MachineBasicBlock *SB = I->getBlock();
1278 auto SBA = Func.Addr->findBlock(SB, *this);
1279 buildBlockRefs(SBA, RefM);
1280 const auto &SRs = RefM[SBA.Id];
1281 Refs.insert(SRs.begin(), SRs.end());
1282 }
1283
1284 for (NodeAddr<InstrNode*> IA : BA.Addr->members(*this))
1285 for (NodeAddr<RefNode*> RA : IA.Addr->members(*this))
1286 Refs.insert(RA.Addr->getRegRef());
1287}
1288
1289// Scan all defs in the block node BA and record in PhiM the locations of
1290// phi nodes corresponding to these defs.
1291void DataFlowGraph::recordDefsForDF(BlockRefsMap &PhiM, BlockRefsMap &RefM,
1292 NodeAddr<BlockNode*> BA) {
1293 // Check all defs from block BA and record them in each block in BA's
1294 // iterated dominance frontier. This information will later be used to
1295 // create phi nodes.
1296 MachineBasicBlock *BB = BA.Addr->getCode();
1297 assert(BB);
1298 auto DFLoc = MDF.find(BB);
1299 if (DFLoc == MDF.end() || DFLoc->second.empty())
1300 return;
1301
1302 // Traverse all instructions in the block and collect the set of all
1303 // defined references. For each reference there will be a phi created
1304 // in the block's iterated dominance frontier.
1305 // This is done to make sure that each defined reference gets only one
1306 // phi node, even if it is defined multiple times.
1307 RegisterSet Defs;
1308 for (auto I : BA.Addr->members(*this)) {
1309 assert(I.Addr->getType() == NodeAttrs::Code);
1310 assert(I.Addr->getKind() == NodeAttrs::Phi ||
1311 I.Addr->getKind() == NodeAttrs::Stmt);
1312 NodeAddr<InstrNode*> IA = I;
1313 for (NodeAddr<RefNode*> RA : IA.Addr->members_if(IsDef, *this))
1314 Defs.insert(RA.Addr->getRegRef());
1315 }
1316
1317 // Finally, add the set of defs to each block in the iterated dominance
1318 // frontier.
1319 const MachineDominanceFrontier::DomSetType &DF = DFLoc->second;
1320 SetVector<MachineBasicBlock*> IDF(DF.begin(), DF.end());
1321 for (unsigned i = 0; i < IDF.size(); ++i) {
1322 auto F = MDF.find(IDF[i]);
1323 if (F != MDF.end())
1324 IDF.insert(F->second.begin(), F->second.end());
1325 }
1326
1327 // Get the register references that are reachable from this block.
1328 RegisterSet &Refs = RefM[BA.Id];
1329 for (auto DB : IDF) {
1330 auto DBA = Func.Addr->findBlock(DB, *this);
1331 const auto &Rs = RefM[DBA.Id];
1332 Refs.insert(Rs.begin(), Rs.end());
1333 }
1334
1335 for (auto DB : IDF) {
1336 auto DBA = Func.Addr->findBlock(DB, *this);
1337 PhiM[DBA.Id].insert(Defs.begin(), Defs.end());
1338 }
1339}
1340
1341// Given the locations of phi nodes in the map PhiM, create the phi nodes
1342// that are located in the block node BA.
1343void DataFlowGraph::buildPhis(BlockRefsMap &PhiM, BlockRefsMap &RefM,
1344 NodeAddr<BlockNode*> BA) {
1345 // Check if this blocks has any DF defs, i.e. if there are any defs
1346 // that this block is in the iterated dominance frontier of.
1347 auto HasDF = PhiM.find(BA.Id);
1348 if (HasDF == PhiM.end() || HasDF->second.empty())
1349 return;
1350
1351 // First, remove all R in Refs in such that there exists T in Refs
1352 // such that T covers R. In other words, only leave those refs that
1353 // are not covered by another ref (i.e. maximal with respect to covering).
1354
1355 auto MaxCoverIn = [this] (RegisterRef RR, RegisterSet &RRs) -> RegisterRef {
1356 for (auto I : RRs)
1357 if (I != RR && RAI.covers(I, RR))
1358 RR = I;
1359 return RR;
1360 };
1361
1362 RegisterSet MaxDF;
1363 for (auto I : HasDF->second)
1364 MaxDF.insert(MaxCoverIn(I, HasDF->second));
1365
1366 std::vector<RegisterRef> MaxRefs;
1367 auto &RefB = RefM[BA.Id];
1368 for (auto I : MaxDF)
1369 MaxRefs.push_back(MaxCoverIn(I, RefB));
1370
1371 // Now, for each R in MaxRefs, get the alias closure of R. If the closure
1372 // only has R in it, create a phi a def for R. Otherwise, create a phi,
1373 // and add a def for each S in the closure.
1374
1375 // Sort the refs so that the phis will be created in a deterministic order.
1376 std::sort(MaxRefs.begin(), MaxRefs.end());
1377 // Remove duplicates.
1378 auto NewEnd = std::unique(MaxRefs.begin(), MaxRefs.end());
1379 MaxRefs.erase(NewEnd, MaxRefs.end());
1380
1381 auto Aliased = [this,&MaxRefs](RegisterRef RR,
1382 std::vector<unsigned> &Closure) -> bool {
1383 for (auto I : Closure)
1384 if (RAI.alias(RR, MaxRefs[I]))
1385 return true;
1386 return false;
1387 };
1388
1389 // Prepare a list of NodeIds of the block's predecessors.
1390 std::vector<NodeId> PredList;
1391 const MachineBasicBlock *MBB = BA.Addr->getCode();
1392 for (auto PB : MBB->predecessors()) {
1393 auto B = Func.Addr->findBlock(PB, *this);
1394 PredList.push_back(B.Id);
1395 }
1396
1397 while (!MaxRefs.empty()) {
1398 // Put the first element in the closure, and then add all subsequent
1399 // elements from MaxRefs to it, if they alias at least one element
1400 // already in the closure.
1401 // ClosureIdx: vector of indices in MaxRefs of members of the closure.
1402 std::vector<unsigned> ClosureIdx = { 0 };
1403 for (unsigned i = 1; i != MaxRefs.size(); ++i)
1404 if (Aliased(MaxRefs[i], ClosureIdx))
1405 ClosureIdx.push_back(i);
1406
1407 // Build a phi for the closure.
1408 unsigned CS = ClosureIdx.size();
1409 NodeAddr<PhiNode*> PA = newPhi(BA);
1410
1411 // Add defs.
1412 for (unsigned X = 0; X != CS; ++X) {
1413 RegisterRef RR = MaxRefs[ClosureIdx[X]];
1414 uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving;
1415 NodeAddr<DefNode*> DA = newDef(PA, RR, PhiFlags);
1416 PA.Addr->addMember(DA, *this);
1417 }
1418 // Add phi uses.
1419 for (auto P : PredList) {
1420 auto PBA = addr<BlockNode*>(P);
1421 for (unsigned X = 0; X != CS; ++X) {
1422 RegisterRef RR = MaxRefs[ClosureIdx[X]];
1423 NodeAddr<PhiUseNode*> PUA = newPhiUse(PA, RR, PBA);
1424 PA.Addr->addMember(PUA, *this);
1425 }
1426 }
1427
1428 // Erase from MaxRefs all elements in the closure.
1429 auto Begin = MaxRefs.begin();
1430 for (unsigned i = ClosureIdx.size(); i != 0; --i)
1431 MaxRefs.erase(Begin + ClosureIdx[i-1]);
1432 }
1433}
1434
1435// Remove any unneeded phi nodes that were created during the build process.
1436void DataFlowGraph::removeUnusedPhis() {
1437 // This will remove unused phis, i.e. phis where each def does not reach
1438 // any uses or other defs. This will not detect or remove circular phi
1439 // chains that are otherwise dead. Unused/dead phis are created during
1440 // the build process and this function is intended to remove these cases
1441 // that are easily determinable to be unnecessary.
1442
1443 SetVector<NodeId> PhiQ;
1444 for (NodeAddr<BlockNode*> BA : Func.Addr->members(*this)) {
1445 for (auto P : BA.Addr->members_if(IsPhi, *this))
1446 PhiQ.insert(P.Id);
1447 }
1448
1449 static auto HasUsedDef = [](NodeList &Ms) -> bool {
1450 for (auto M : Ms) {
1451 if (M.Addr->getKind() != NodeAttrs::Def)
1452 continue;
1453 NodeAddr<DefNode*> DA = M;
1454 if (DA.Addr->getReachedDef() != 0 || DA.Addr->getReachedUse() != 0)
1455 return true;
1456 }
1457 return false;
1458 };
1459
1460 // Any phi, if it is removed, may affect other phis (make them dead).
1461 // For each removed phi, collect the potentially affected phis and add
1462 // them back to the queue.
1463 while (!PhiQ.empty()) {
1464 auto PA = addr<PhiNode*>(PhiQ[0]);
1465 PhiQ.remove(PA.Id);
1466 NodeList Refs = PA.Addr->members(*this);
1467 if (HasUsedDef(Refs))
1468 continue;
1469 for (NodeAddr<RefNode*> RA : Refs) {
1470 if (NodeId RD = RA.Addr->getReachingDef()) {
1471 auto RDA = addr<DefNode*>(RD);
1472 NodeAddr<InstrNode*> OA = RDA.Addr->getOwner(*this);
1473 if (IsPhi(OA))
1474 PhiQ.insert(OA.Id);
1475 }
1476 if (RA.Addr->isDef())
Krzysztof Parzyszek69e670d52016-01-18 20:41:34 +00001477 unlinkDef(RA, true);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001478 else
Krzysztof Parzyszek69e670d52016-01-18 20:41:34 +00001479 unlinkUse(RA, true);
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001480 }
1481 NodeAddr<BlockNode*> BA = PA.Addr->getOwner(*this);
1482 BA.Addr->removeMember(PA, *this);
1483 }
1484}
1485
1486// For a given reference node TA in an instruction node IA, connect the
1487// reaching def of TA to the appropriate def node. Create any shadow nodes
1488// as appropriate.
1489template <typename T>
1490void DataFlowGraph::linkRefUp(NodeAddr<InstrNode*> IA, NodeAddr<T> TA,
1491 DefStack &DS) {
1492 if (DS.empty())
1493 return;
1494 RegisterRef RR = TA.Addr->getRegRef();
1495 NodeAddr<T> TAP;
1496
1497 // References from the def stack that have been examined so far.
1498 RegisterSet Defs;
1499
1500 for (auto I = DS.top(), E = DS.bottom(); I != E; I.down()) {
1501 RegisterRef QR = I->Addr->getRegRef();
1502 auto AliasQR = [QR,this] (RegisterRef RR) -> bool {
1503 return RAI.alias(QR, RR);
1504 };
1505 bool PrecUp = RAI.covers(QR, RR);
1506 // Skip all defs that are aliased to any of the defs that we have already
1507 // seen. If we encounter a covering def, stop the stack traversal early.
1508 if (std::any_of(Defs.begin(), Defs.end(), AliasQR)) {
1509 if (PrecUp)
1510 break;
1511 continue;
1512 }
1513 // The reaching def.
1514 NodeAddr<DefNode*> RDA = *I;
1515
1516 // Pick the reached node.
1517 if (TAP.Id == 0) {
1518 TAP = TA;
1519 } else {
1520 // Mark the existing ref as "shadow" and create a new shadow.
1521 TAP.Addr->setFlags(TAP.Addr->getFlags() | NodeAttrs::Shadow);
1522 TAP = getNextShadow(IA, TAP, true);
1523 }
1524
1525 // Create the link.
1526 TAP.Addr->linkToDef(TAP.Id, RDA);
1527
1528 if (PrecUp)
1529 break;
1530 Defs.insert(QR);
1531 }
1532}
1533
1534// Create data-flow links for all reference nodes in the statement node SA.
1535void DataFlowGraph::linkStmtRefs(DefStackMap &DefM, NodeAddr<StmtNode*> SA) {
1536 RegisterSet Defs;
1537
1538 // Link all nodes (upwards in the data-flow) with their reaching defs.
1539 for (NodeAddr<RefNode*> RA : SA.Addr->members(*this)) {
1540 uint16_t Kind = RA.Addr->getKind();
1541 assert(Kind == NodeAttrs::Def || Kind == NodeAttrs::Use);
1542 RegisterRef RR = RA.Addr->getRegRef();
1543 // Do not process multiple defs of the same reference.
1544 if (Kind == NodeAttrs::Def && Defs.count(RR))
1545 continue;
1546 Defs.insert(RR);
1547
1548 auto F = DefM.find(RR);
1549 if (F == DefM.end())
1550 continue;
1551 DefStack &DS = F->second;
1552 if (Kind == NodeAttrs::Use)
1553 linkRefUp<UseNode*>(SA, RA, DS);
1554 else if (Kind == NodeAttrs::Def)
1555 linkRefUp<DefNode*>(SA, RA, DS);
1556 else
1557 llvm_unreachable("Unexpected node in instruction");
1558 }
1559}
1560
1561// Create data-flow links for all instructions in the block node BA. This
1562// will include updating any phi nodes in BA.
1563void DataFlowGraph::linkBlockRefs(DefStackMap &DefM, NodeAddr<BlockNode*> BA) {
1564 // Push block delimiters.
1565 markBlock(BA.Id, DefM);
1566
Krzysztof Parzyszek89757432016-05-05 22:00:44 +00001567 assert(BA.Addr && "block node address is needed to create a data-flow link");
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001568 // For each non-phi instruction in the block, link all the defs and uses
1569 // to their reaching defs. For any member of the block (including phis),
1570 // push the defs on the corresponding stacks.
1571 for (NodeAddr<InstrNode*> IA : BA.Addr->members(*this)) {
1572 // Ignore phi nodes here. They will be linked part by part from the
1573 // predecessors.
1574 if (IA.Addr->getKind() == NodeAttrs::Stmt)
1575 linkStmtRefs(DefM, IA);
1576
1577 // Push the definitions on the stack.
1578 pushDefs(IA, DefM);
1579 }
1580
1581 // Recursively process all children in the dominator tree.
1582 MachineDomTreeNode *N = MDT.getNode(BA.Addr->getCode());
1583 for (auto I : *N) {
1584 MachineBasicBlock *SB = I->getBlock();
1585 auto SBA = Func.Addr->findBlock(SB, *this);
1586 linkBlockRefs(DefM, SBA);
1587 }
1588
1589 // Link the phi uses from the successor blocks.
1590 auto IsUseForBA = [BA](NodeAddr<NodeBase*> NA) -> bool {
1591 if (NA.Addr->getKind() != NodeAttrs::Use)
1592 return false;
1593 assert(NA.Addr->getFlags() & NodeAttrs::PhiRef);
1594 NodeAddr<PhiUseNode*> PUA = NA;
1595 return PUA.Addr->getPredecessor() == BA.Id;
1596 };
1597 MachineBasicBlock *MBB = BA.Addr->getCode();
1598 for (auto SB : MBB->successors()) {
1599 auto SBA = Func.Addr->findBlock(SB, *this);
1600 for (NodeAddr<InstrNode*> IA : SBA.Addr->members_if(IsPhi, *this)) {
1601 // Go over each phi use associated with MBB, and link it.
1602 for (auto U : IA.Addr->members_if(IsUseForBA, *this)) {
1603 NodeAddr<PhiUseNode*> PUA = U;
1604 RegisterRef RR = PUA.Addr->getRegRef();
1605 linkRefUp<UseNode*>(IA, PUA, DefM[RR]);
1606 }
1607 }
1608 }
1609
1610 // Pop all defs from this block from the definition stacks.
1611 releaseBlock(BA.Id, DefM);
1612}
1613
1614// Remove the use node UA from any data-flow and structural links.
Krzysztof Parzyszek69e670d52016-01-18 20:41:34 +00001615void DataFlowGraph::unlinkUseDF(NodeAddr<UseNode*> UA) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001616 NodeId RD = UA.Addr->getReachingDef();
1617 NodeId Sib = UA.Addr->getSibling();
1618
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001619 if (RD == 0) {
1620 assert(Sib == 0);
1621 return;
1622 }
1623
1624 auto RDA = addr<DefNode*>(RD);
1625 auto TA = addr<UseNode*>(RDA.Addr->getReachedUse());
1626 if (TA.Id == UA.Id) {
1627 RDA.Addr->setReachedUse(Sib);
1628 return;
1629 }
1630
1631 while (TA.Id != 0) {
1632 NodeId S = TA.Addr->getSibling();
1633 if (S == UA.Id) {
1634 TA.Addr->setSibling(UA.Addr->getSibling());
1635 return;
1636 }
1637 TA = addr<UseNode*>(S);
1638 }
1639}
1640
1641// Remove the def node DA from any data-flow and structural links.
Krzysztof Parzyszek69e670d52016-01-18 20:41:34 +00001642void DataFlowGraph::unlinkDefDF(NodeAddr<DefNode*> DA) {
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001643 //
1644 // RD
1645 // | reached
1646 // | def
1647 // :
1648 // .
1649 // +----+
1650 // ... -- | DA | -- ... -- 0 : sibling chain of DA
1651 // +----+
1652 // | | reached
1653 // | : def
1654 // | .
1655 // | ... : Siblings (defs)
1656 // |
1657 // : reached
1658 // . use
1659 // ... : sibling chain of reached uses
1660
1661 NodeId RD = DA.Addr->getReachingDef();
1662
1663 // Visit all siblings of the reached def and reset their reaching defs.
1664 // Also, defs reached by DA are now "promoted" to being reached by RD,
1665 // so all of them will need to be spliced into the sibling chain where
1666 // DA belongs.
1667 auto getAllNodes = [this] (NodeId N) -> NodeList {
1668 NodeList Res;
1669 while (N) {
1670 auto RA = addr<RefNode*>(N);
1671 // Keep the nodes in the exact sibling order.
1672 Res.push_back(RA);
1673 N = RA.Addr->getSibling();
1674 }
1675 return Res;
1676 };
1677 NodeList ReachedDefs = getAllNodes(DA.Addr->getReachedDef());
1678 NodeList ReachedUses = getAllNodes(DA.Addr->getReachedUse());
1679
1680 if (RD == 0) {
1681 for (NodeAddr<RefNode*> I : ReachedDefs)
1682 I.Addr->setSibling(0);
1683 for (NodeAddr<RefNode*> I : ReachedUses)
1684 I.Addr->setSibling(0);
1685 }
1686 for (NodeAddr<DefNode*> I : ReachedDefs)
1687 I.Addr->setReachingDef(RD);
1688 for (NodeAddr<UseNode*> I : ReachedUses)
1689 I.Addr->setReachingDef(RD);
1690
1691 NodeId Sib = DA.Addr->getSibling();
1692 if (RD == 0) {
1693 assert(Sib == 0);
1694 return;
1695 }
1696
1697 // Update the reaching def node and remove DA from the sibling list.
1698 auto RDA = addr<DefNode*>(RD);
1699 auto TA = addr<DefNode*>(RDA.Addr->getReachedDef());
1700 if (TA.Id == DA.Id) {
1701 // If DA is the first reached def, just update the RD's reached def
1702 // to the DA's sibling.
1703 RDA.Addr->setReachedDef(Sib);
1704 } else {
1705 // Otherwise, traverse the sibling list of the reached defs and remove
1706 // DA from it.
1707 while (TA.Id != 0) {
1708 NodeId S = TA.Addr->getSibling();
1709 if (S == DA.Id) {
1710 TA.Addr->setSibling(Sib);
1711 break;
1712 }
1713 TA = addr<DefNode*>(S);
1714 }
1715 }
1716
1717 // Splice the DA's reached defs into the RDA's reached def chain.
1718 if (!ReachedDefs.empty()) {
1719 auto Last = NodeAddr<DefNode*>(ReachedDefs.back());
1720 Last.Addr->setSibling(RDA.Addr->getReachedDef());
1721 RDA.Addr->setReachedDef(ReachedDefs.front().Id);
1722 }
1723 // Splice the DA's reached uses into the RDA's reached use chain.
1724 if (!ReachedUses.empty()) {
1725 auto Last = NodeAddr<UseNode*>(ReachedUses.back());
1726 Last.Addr->setSibling(RDA.Addr->getReachedUse());
1727 RDA.Addr->setReachedUse(ReachedUses.front().Id);
1728 }
Krzysztof Parzyszekb5b5a1d2016-01-12 15:09:49 +00001729}