1 //===- RDFGraph.cpp -------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Target-independent, SSA-based data flow graph for register data flow (RDF).
12 #include "RDFRegisters.h"
13 #include "llvm/ADT/BitVector.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SetVector.h"
16 #include "llvm/CodeGen/MachineBasicBlock.h"
17 #include "llvm/CodeGen/MachineDominanceFrontier.h"
18 #include "llvm/CodeGen/MachineDominators.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineInstr.h"
21 #include "llvm/CodeGen/MachineOperand.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/TargetInstrInfo.h"
24 #include "llvm/CodeGen/TargetLowering.h"
25 #include "llvm/CodeGen/TargetRegisterInfo.h"
26 #include "llvm/CodeGen/TargetSubtargetInfo.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/MC/LaneBitmask.h"
29 #include "llvm/MC/MCInstrDesc.h"
30 #include "llvm/MC/MCRegisterInfo.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/raw_ostream.h"
46 // Printing functions. Have them here first, so that the rest of the code
51 raw_ostream
&operator<< (raw_ostream
&OS
, const PrintLaneMaskOpt
&P
) {
53 OS
<< ':' << PrintLaneMask(P
.Mask
);
57 raw_ostream
&operator<< (raw_ostream
&OS
, const Print
<RegisterRef
> &P
) {
58 auto &TRI
= P
.G
.getTRI();
59 if (P
.Obj
.Reg
> 0 && P
.Obj
.Reg
< TRI
.getNumRegs())
60 OS
<< TRI
.getName(P
.Obj
.Reg
);
62 OS
<< '#' << P
.Obj
.Reg
;
63 OS
<< PrintLaneMaskOpt(P
.Obj
.Mask
);
67 raw_ostream
&operator<< (raw_ostream
&OS
, const Print
<NodeId
> &P
) {
68 auto NA
= P
.G
.addr
<NodeBase
*>(P
.Obj
);
69 uint16_t Attrs
= NA
.Addr
->getAttrs();
70 uint16_t Kind
= NodeAttrs::kind(Attrs
);
71 uint16_t Flags
= NodeAttrs::flags(Attrs
);
72 switch (NodeAttrs::type(Attrs
)) {
75 case NodeAttrs::Func
: OS
<< 'f'; break;
76 case NodeAttrs::Block
: OS
<< 'b'; break;
77 case NodeAttrs::Stmt
: OS
<< 's'; break;
78 case NodeAttrs::Phi
: OS
<< 'p'; break;
79 default: OS
<< "c?"; break;
83 if (Flags
& NodeAttrs::Undef
)
85 if (Flags
& NodeAttrs::Dead
)
87 if (Flags
& NodeAttrs::Preserving
)
89 if (Flags
& NodeAttrs::Clobbering
)
92 case NodeAttrs::Use
: OS
<< 'u'; break;
93 case NodeAttrs::Def
: OS
<< 'd'; break;
94 case NodeAttrs::Block
: OS
<< 'b'; break;
95 default: OS
<< "r?"; break;
103 if (Flags
& NodeAttrs::Shadow
)
108 static void printRefHeader(raw_ostream
&OS
, const NodeAddr
<RefNode
*> RA
,
109 const DataFlowGraph
&G
) {
110 OS
<< Print
<NodeId
>(RA
.Id
, G
) << '<'
111 << Print
<RegisterRef
>(RA
.Addr
->getRegRef(G
), G
) << '>';
112 if (RA
.Addr
->getFlags() & NodeAttrs::Fixed
)
116 raw_ostream
&operator<< (raw_ostream
&OS
, const Print
<NodeAddr
<DefNode
*>> &P
) {
117 printRefHeader(OS
, P
.Obj
, P
.G
);
119 if (NodeId N
= P
.Obj
.Addr
->getReachingDef())
120 OS
<< Print
<NodeId
>(N
, P
.G
);
122 if (NodeId N
= P
.Obj
.Addr
->getReachedDef())
123 OS
<< Print
<NodeId
>(N
, P
.G
);
125 if (NodeId N
= P
.Obj
.Addr
->getReachedUse())
126 OS
<< Print
<NodeId
>(N
, P
.G
);
128 if (NodeId N
= P
.Obj
.Addr
->getSibling())
129 OS
<< Print
<NodeId
>(N
, P
.G
);
133 raw_ostream
&operator<< (raw_ostream
&OS
, const Print
<NodeAddr
<UseNode
*>> &P
) {
134 printRefHeader(OS
, P
.Obj
, P
.G
);
136 if (NodeId N
= P
.Obj
.Addr
->getReachingDef())
137 OS
<< Print
<NodeId
>(N
, P
.G
);
139 if (NodeId N
= P
.Obj
.Addr
->getSibling())
140 OS
<< Print
<NodeId
>(N
, P
.G
);
144 raw_ostream
&operator<< (raw_ostream
&OS
,
145 const Print
<NodeAddr
<PhiUseNode
*>> &P
) {
146 printRefHeader(OS
, P
.Obj
, P
.G
);
148 if (NodeId N
= P
.Obj
.Addr
->getReachingDef())
149 OS
<< Print
<NodeId
>(N
, P
.G
);
151 if (NodeId N
= P
.Obj
.Addr
->getPredecessor())
152 OS
<< Print
<NodeId
>(N
, P
.G
);
154 if (NodeId N
= P
.Obj
.Addr
->getSibling())
155 OS
<< Print
<NodeId
>(N
, P
.G
);
159 raw_ostream
&operator<< (raw_ostream
&OS
, const Print
<NodeAddr
<RefNode
*>> &P
) {
160 switch (P
.Obj
.Addr
->getKind()) {
162 OS
<< PrintNode
<DefNode
*>(P
.Obj
, P
.G
);
165 if (P
.Obj
.Addr
->getFlags() & NodeAttrs::PhiRef
)
166 OS
<< PrintNode
<PhiUseNode
*>(P
.Obj
, P
.G
);
168 OS
<< PrintNode
<UseNode
*>(P
.Obj
, P
.G
);
174 raw_ostream
&operator<< (raw_ostream
&OS
, const Print
<NodeList
> &P
) {
175 unsigned N
= P
.Obj
.size();
176 for (auto I
: P
.Obj
) {
177 OS
<< Print
<NodeId
>(I
.Id
, P
.G
);
184 raw_ostream
&operator<< (raw_ostream
&OS
, const Print
<NodeSet
> &P
) {
185 unsigned N
= P
.Obj
.size();
186 for (auto I
: P
.Obj
) {
187 OS
<< Print
<NodeId
>(I
, P
.G
);
196 template <typename T
>
198 PrintListV(const NodeList
&L
, const DataFlowGraph
&G
) : List(L
), G(G
) {}
201 const NodeList
&List
;
202 const DataFlowGraph
&G
;
205 template <typename T
>
206 raw_ostream
&operator<< (raw_ostream
&OS
, const PrintListV
<T
> &P
) {
207 unsigned N
= P
.List
.size();
208 for (NodeAddr
<T
> A
: P
.List
) {
209 OS
<< PrintNode
<T
>(A
, P
.G
);
216 } // end anonymous namespace
218 raw_ostream
&operator<< (raw_ostream
&OS
, const Print
<NodeAddr
<PhiNode
*>> &P
) {
219 OS
<< Print
<NodeId
>(P
.Obj
.Id
, P
.G
) << ": phi ["
220 << PrintListV
<RefNode
*>(P
.Obj
.Addr
->members(P
.G
), P
.G
) << ']';
224 raw_ostream
&operator<<(raw_ostream
&OS
, const Print
<NodeAddr
<StmtNode
*>> &P
) {
225 const MachineInstr
&MI
= *P
.Obj
.Addr
->getCode();
226 unsigned Opc
= MI
.getOpcode();
227 OS
<< Print
<NodeId
>(P
.Obj
.Id
, P
.G
) << ": " << P
.G
.getTII().getName(Opc
);
228 // Print the target for calls and branches (for readability).
229 if (MI
.isCall() || MI
.isBranch()) {
230 MachineInstr::const_mop_iterator T
=
231 llvm::find_if(MI
.operands(),
232 [] (const MachineOperand
&Op
) -> bool {
233 return Op
.isMBB() || Op
.isGlobal() || Op
.isSymbol();
235 if (T
!= MI
.operands_end()) {
238 OS
<< printMBBReference(*T
->getMBB());
239 else if (T
->isGlobal())
240 OS
<< T
->getGlobal()->getName();
241 else if (T
->isSymbol())
242 OS
<< T
->getSymbolName();
245 OS
<< " [" << PrintListV
<RefNode
*>(P
.Obj
.Addr
->members(P
.G
), P
.G
) << ']';
249 raw_ostream
&operator<< (raw_ostream
&OS
,
250 const Print
<NodeAddr
<InstrNode
*>> &P
) {
251 switch (P
.Obj
.Addr
->getKind()) {
253 OS
<< PrintNode
<PhiNode
*>(P
.Obj
, P
.G
);
255 case NodeAttrs::Stmt
:
256 OS
<< PrintNode
<StmtNode
*>(P
.Obj
, P
.G
);
259 OS
<< "instr? " << Print
<NodeId
>(P
.Obj
.Id
, P
.G
);
265 raw_ostream
&operator<< (raw_ostream
&OS
,
266 const Print
<NodeAddr
<BlockNode
*>> &P
) {
267 MachineBasicBlock
*BB
= P
.Obj
.Addr
->getCode();
268 unsigned NP
= BB
->pred_size();
270 auto PrintBBs
= [&OS
] (std::vector
<int> Ns
) -> void {
271 unsigned N
= Ns
.size();
279 OS
<< Print
<NodeId
>(P
.Obj
.Id
, P
.G
) << ": --- " << printMBBReference(*BB
)
280 << " --- preds(" << NP
<< "): ";
281 for (MachineBasicBlock
*B
: BB
->predecessors())
282 Ns
.push_back(B
->getNumber());
285 unsigned NS
= BB
->succ_size();
286 OS
<< " succs(" << NS
<< "): ";
288 for (MachineBasicBlock
*B
: BB
->successors())
289 Ns
.push_back(B
->getNumber());
293 for (auto I
: P
.Obj
.Addr
->members(P
.G
))
294 OS
<< PrintNode
<InstrNode
*>(I
, P
.G
) << '\n';
298 raw_ostream
&operator<<(raw_ostream
&OS
, const Print
<NodeAddr
<FuncNode
*>> &P
) {
299 OS
<< "DFG dump:[\n" << Print
<NodeId
>(P
.Obj
.Id
, P
.G
) << ": Function: "
300 << P
.Obj
.Addr
->getCode()->getName() << '\n';
301 for (auto I
: P
.Obj
.Addr
->members(P
.G
))
302 OS
<< PrintNode
<BlockNode
*>(I
, P
.G
) << '\n';
307 raw_ostream
&operator<< (raw_ostream
&OS
, const Print
<RegisterSet
> &P
) {
310 OS
<< ' ' << Print
<RegisterRef
>(I
, P
.G
);
315 raw_ostream
&operator<< (raw_ostream
&OS
, const Print
<RegisterAggr
> &P
) {
320 raw_ostream
&operator<< (raw_ostream
&OS
,
321 const Print
<DataFlowGraph::DefStack
> &P
) {
322 for (auto I
= P
.Obj
.top(), E
= P
.Obj
.bottom(); I
!= E
; ) {
323 OS
<< Print
<NodeId
>(I
->Id
, P
.G
)
324 << '<' << Print
<RegisterRef
>(I
->Addr
->getRegRef(P
.G
), P
.G
) << '>';
332 } // end namespace rdf
333 } // end namespace llvm
335 // Node allocation functions.
337 // Node allocator is like a slab memory allocator: it allocates blocks of
338 // memory in sizes that are multiples of the size of a node. Each block has
339 // the same size. Nodes are allocated from the currently active block, and
340 // when it becomes full, a new one is created.
341 // There is a mapping scheme between node id and its location in a block,
342 // and within that block is described in the header file.
344 void NodeAllocator::startNewBlock() {
345 void *T
= MemPool
.Allocate(NodesPerBlock
*NodeMemSize
, NodeMemSize
);
346 char *P
= static_cast<char*>(T
);
348 // Check if the block index is still within the allowed range, i.e. less
349 // than 2^N, where N is the number of bits in NodeId for the block index.
350 // BitsPerIndex is the number of bits per node index.
351 assert((Blocks
.size() < ((size_t)1 << (8*sizeof(NodeId
)-BitsPerIndex
))) &&
352 "Out of bits for block index");
356 bool NodeAllocator::needNewBlock() {
360 char *ActiveBegin
= Blocks
.back();
361 uint32_t Index
= (ActiveEnd
-ActiveBegin
)/NodeMemSize
;
362 return Index
>= NodesPerBlock
;
365 NodeAddr
<NodeBase
*> NodeAllocator::New() {
369 uint32_t ActiveB
= Blocks
.size()-1;
370 uint32_t Index
= (ActiveEnd
- Blocks
[ActiveB
])/NodeMemSize
;
371 NodeAddr
<NodeBase
*> NA
= { reinterpret_cast<NodeBase
*>(ActiveEnd
),
372 makeId(ActiveB
, Index
) };
373 ActiveEnd
+= NodeMemSize
;
377 NodeId
NodeAllocator::id(const NodeBase
*P
) const {
378 uintptr_t A
= reinterpret_cast<uintptr_t>(P
);
379 for (unsigned i
= 0, n
= Blocks
.size(); i
!= n
; ++i
) {
380 uintptr_t B
= reinterpret_cast<uintptr_t>(Blocks
[i
]);
381 if (A
< B
|| A
>= B
+ NodesPerBlock
*NodeMemSize
)
383 uint32_t Idx
= (A
-B
)/NodeMemSize
;
384 return makeId(i
, Idx
);
386 llvm_unreachable("Invalid node address");
389 void NodeAllocator::clear() {
395 // Insert node NA after "this" in the circular chain.
396 void NodeBase::append(NodeAddr
<NodeBase
*> NA
) {
398 // If NA is already "next", do nothing.
405 // Fundamental node manipulator functions.
407 // Obtain the register reference from a reference node.
408 RegisterRef
RefNode::getRegRef(const DataFlowGraph
&G
) const {
409 assert(NodeAttrs::type(Attrs
) == NodeAttrs::Ref
);
410 if (NodeAttrs::flags(Attrs
) & NodeAttrs::PhiRef
)
411 return G
.unpack(Ref
.PR
);
412 assert(Ref
.Op
!= nullptr);
413 return G
.makeRegRef(*Ref
.Op
);
416 // Set the register reference in the reference node directly (for references
418 void RefNode::setRegRef(RegisterRef RR
, DataFlowGraph
&G
) {
419 assert(NodeAttrs::type(Attrs
) == NodeAttrs::Ref
);
420 assert(NodeAttrs::flags(Attrs
) & NodeAttrs::PhiRef
);
424 // Set the register reference in the reference node based on a machine
425 // operand (for references in statement nodes).
426 void RefNode::setRegRef(MachineOperand
*Op
, DataFlowGraph
&G
) {
427 assert(NodeAttrs::type(Attrs
) == NodeAttrs::Ref
);
428 assert(!(NodeAttrs::flags(Attrs
) & NodeAttrs::PhiRef
));
433 // Get the owner of a given reference node.
434 NodeAddr
<NodeBase
*> RefNode::getOwner(const DataFlowGraph
&G
) {
435 NodeAddr
<NodeBase
*> NA
= G
.addr
<NodeBase
*>(getNext());
437 while (NA
.Addr
!= this) {
438 if (NA
.Addr
->getType() == NodeAttrs::Code
)
440 NA
= G
.addr
<NodeBase
*>(NA
.Addr
->getNext());
442 llvm_unreachable("No owner in circular list");
445 // Connect the def node to the reaching def node.
446 void DefNode::linkToDef(NodeId Self
, NodeAddr
<DefNode
*> DA
) {
448 Ref
.Sib
= DA
.Addr
->getReachedDef();
449 DA
.Addr
->setReachedDef(Self
);
452 // Connect the use node to the reaching def node.
453 void UseNode::linkToDef(NodeId Self
, NodeAddr
<DefNode
*> DA
) {
455 Ref
.Sib
= DA
.Addr
->getReachedUse();
456 DA
.Addr
->setReachedUse(Self
);
459 // Get the first member of the code node.
460 NodeAddr
<NodeBase
*> CodeNode::getFirstMember(const DataFlowGraph
&G
) const {
461 if (Code
.FirstM
== 0)
462 return NodeAddr
<NodeBase
*>();
463 return G
.addr
<NodeBase
*>(Code
.FirstM
);
466 // Get the last member of the code node.
467 NodeAddr
<NodeBase
*> CodeNode::getLastMember(const DataFlowGraph
&G
) const {
469 return NodeAddr
<NodeBase
*>();
470 return G
.addr
<NodeBase
*>(Code
.LastM
);
473 // Add node NA at the end of the member list of the given code node.
474 void CodeNode::addMember(NodeAddr
<NodeBase
*> NA
, const DataFlowGraph
&G
) {
475 NodeAddr
<NodeBase
*> ML
= getLastMember(G
);
480 NodeId Self
= G
.id(this);
481 NA
.Addr
->setNext(Self
);
486 // Add node NA after member node MA in the given code node.
487 void CodeNode::addMemberAfter(NodeAddr
<NodeBase
*> MA
, NodeAddr
<NodeBase
*> NA
,
488 const DataFlowGraph
&G
) {
490 if (Code
.LastM
== MA
.Id
)
494 // Remove member node NA from the given code node.
495 void CodeNode::removeMember(NodeAddr
<NodeBase
*> NA
, const DataFlowGraph
&G
) {
496 NodeAddr
<NodeBase
*> MA
= getFirstMember(G
);
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;
505 // Otherwise, advance the first member.
506 Code
.FirstM
= MA
.Addr
->getNext();
511 while (MA
.Addr
!= this) {
512 NodeId MX
= MA
.Addr
->getNext();
514 MA
.Addr
->setNext(NA
.Addr
->getNext());
515 // If the member to remove happens to be the last one, update the
517 if (Code
.LastM
== NA
.Id
)
521 MA
= G
.addr
<NodeBase
*>(MX
);
523 llvm_unreachable("No such member");
526 // Return the list of all members of the code node.
527 NodeList
CodeNode::members(const DataFlowGraph
&G
) const {
528 static auto True
= [] (NodeAddr
<NodeBase
*>) -> bool { return true; };
529 return members_if(True
, G
);
532 // Return the owner of the given instr node.
533 NodeAddr
<NodeBase
*> InstrNode::getOwner(const DataFlowGraph
&G
) {
534 NodeAddr
<NodeBase
*> NA
= G
.addr
<NodeBase
*>(getNext());
536 while (NA
.Addr
!= this) {
537 assert(NA
.Addr
->getType() == NodeAttrs::Code
);
538 if (NA
.Addr
->getKind() == NodeAttrs::Block
)
540 NA
= G
.addr
<NodeBase
*>(NA
.Addr
->getNext());
542 llvm_unreachable("No owner in circular list");
545 // Add the phi node PA to the given block node.
546 void BlockNode::addPhi(NodeAddr
<PhiNode
*> PA
, const DataFlowGraph
&G
) {
547 NodeAddr
<NodeBase
*> M
= getFirstMember(G
);
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
558 PA
.Addr
->setNext(M
.Id
);
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
;
565 MN
= G
.addr
<NodeBase
*>(M
.Addr
->getNext());
566 assert(MN
.Addr
->getType() == NodeAttrs::Code
);
567 } while (MN
.Addr
->getKind() == NodeAttrs::Phi
);
569 // M is the last phi.
570 addMemberAfter(M
, PA
, G
);
574 // Find the block node corresponding to the machine basic block BB in the
576 NodeAddr
<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
;
581 NodeList Ms
= members_if(EqBB
, G
);
584 return NodeAddr
<BlockNode
*>();
587 // Get the block node for the entry block in the given function.
588 NodeAddr
<BlockNode
*> FuncNode::getEntryBlock(const DataFlowGraph
&G
) {
589 MachineBasicBlock
*EntryB
= &getCode()->front();
590 return findBlock(EntryB
, G
);
593 // Target operand information.
596 // For a given instruction, check if there are any bits of RR that can remain
597 // unchanged across this def.
598 bool TargetOperandInfo::isPreserving(const MachineInstr
&In
, unsigned OpNum
)
600 return TII
.isPredicated(In
);
603 // Check if the definition of RR produces an unspecified value.
604 bool TargetOperandInfo::isClobbering(const MachineInstr
&In
, unsigned OpNum
)
606 const MachineOperand
&Op
= In
.getOperand(OpNum
);
611 if (Op
.isDef() && Op
.isDead())
616 // Check if the given instruction specifically requires
617 bool TargetOperandInfo::isFixedReg(const MachineInstr
&In
, unsigned OpNum
)
619 if (In
.isCall() || In
.isReturn() || In
.isInlineAsm())
621 // Check for a tail call.
623 for (const MachineOperand
&O
: In
.operands())
624 if (O
.isGlobal() || O
.isSymbol())
627 const MCInstrDesc
&D
= In
.getDesc();
628 if (!D
.getImplicitDefs() && !D
.getImplicitUses())
630 const MachineOperand
&Op
= In
.getOperand(OpNum
);
631 // If there is a sub-register, treat the operand as non-fixed. Currently,
632 // fixed registers are those that are listed in the descriptor as implicit
633 // uses or defs, and those lists do not allow sub-registers.
634 if (Op
.getSubReg() != 0)
636 Register Reg
= Op
.getReg();
637 const MCPhysReg
*ImpR
= Op
.isDef() ? D
.getImplicitDefs()
638 : D
.getImplicitUses();
648 // The data flow graph construction.
651 DataFlowGraph::DataFlowGraph(MachineFunction
&mf
, const TargetInstrInfo
&tii
,
652 const TargetRegisterInfo
&tri
, const MachineDominatorTree
&mdt
,
653 const MachineDominanceFrontier
&mdf
, const TargetOperandInfo
&toi
)
654 : MF(mf
), TII(tii
), TRI(tri
), PRI(tri
, mf
), MDT(mdt
), MDF(mdf
), TOI(toi
),
658 // The implementation of the definition stack.
659 // Each register reference has its own definition stack. In particular,
660 // for a register references "Reg" and "Reg:subreg" will each have their
661 // own definition stacks.
663 // Construct a stack iterator.
664 DataFlowGraph::DefStack::Iterator::Iterator(const DataFlowGraph::DefStack
&S
,
667 // Initialize to bottom.
671 // Initialize to the top, i.e. top-most non-delimiter (or 0, if empty).
672 Pos
= DS
.Stack
.size();
673 while (Pos
> 0 && DS
.isDelimiter(DS
.Stack
[Pos
-1]))
677 // Return the size of the stack, including block delimiters.
678 unsigned DataFlowGraph::DefStack::size() const {
680 for (auto I
= top(), E
= bottom(); I
!= E
; I
.down())
685 // Remove the top entry from the stack. Remove all intervening delimiters
686 // so that after this, the stack is either empty, or the top of the stack
687 // is a non-delimiter.
688 void DataFlowGraph::DefStack::pop() {
690 unsigned P
= nextDown(Stack
.size());
694 // Push a delimiter for block node N on the stack.
695 void DataFlowGraph::DefStack::start_block(NodeId N
) {
697 Stack
.push_back(NodeAddr
<DefNode
*>(nullptr, N
));
700 // Remove all nodes from the top of the stack, until the delimited for
701 // block node N is encountered. Remove the delimiter as well. In effect,
702 // this will remove from the stack all definitions from block N.
703 void DataFlowGraph::DefStack::clear_block(NodeId N
) {
705 unsigned P
= Stack
.size();
707 bool Found
= isDelimiter(Stack
[P
-1], N
);
712 // This will also remove the delimiter, if found.
716 // Move the stack iterator up by one.
717 unsigned DataFlowGraph::DefStack::nextUp(unsigned P
) const {
718 // Get the next valid position after P (skipping all delimiters).
719 // The input position P does not have to point to a non-delimiter.
720 unsigned SS
= Stack
.size();
725 IsDelim
= isDelimiter(Stack
[P
-1]);
726 } while (P
< SS
&& IsDelim
);
731 // Move the stack iterator down by one.
732 unsigned DataFlowGraph::DefStack::nextDown(unsigned P
) const {
733 // Get the preceding valid position before P (skipping all delimiters).
734 // The input position P does not have to point to a non-delimiter.
735 assert(P
> 0 && P
<= Stack
.size());
736 bool IsDelim
= isDelimiter(Stack
[P
-1]);
740 IsDelim
= isDelimiter(Stack
[P
-1]);
741 } while (P
> 0 && IsDelim
);
746 // Register information.
748 RegisterSet
DataFlowGraph::getLandingPadLiveIns() const {
750 const Function
&F
= MF
.getFunction();
751 const Constant
*PF
= F
.hasPersonalityFn() ? F
.getPersonalityFn()
753 const TargetLowering
&TLI
= *MF
.getSubtarget().getTargetLowering();
754 if (RegisterId R
= TLI
.getExceptionPointerRegister(PF
))
755 LR
.insert(RegisterRef(R
));
756 if (RegisterId R
= TLI
.getExceptionSelectorRegister(PF
))
757 LR
.insert(RegisterRef(R
));
761 // Node management functions.
763 // Get the pointer to the node with the id N.
764 NodeBase
*DataFlowGraph::ptr(NodeId N
) const {
767 return Memory
.ptr(N
);
770 // Get the id of the node at the address P.
771 NodeId
DataFlowGraph::id(const NodeBase
*P
) const {
777 // Allocate a new node and set the attributes to Attrs.
778 NodeAddr
<NodeBase
*> DataFlowGraph::newNode(uint16_t Attrs
) {
779 NodeAddr
<NodeBase
*> P
= Memory
.New();
781 P
.Addr
->setAttrs(Attrs
);
785 // Make a copy of the given node B, except for the data-flow links, which
787 NodeAddr
<NodeBase
*> DataFlowGraph::cloneNode(const NodeAddr
<NodeBase
*> B
) {
788 NodeAddr
<NodeBase
*> NA
= newNode(0);
789 memcpy(NA
.Addr
, B
.Addr
, sizeof(NodeBase
));
790 // Ref nodes need to have the data-flow links reset.
791 if (NA
.Addr
->getType() == NodeAttrs::Ref
) {
792 NodeAddr
<RefNode
*> RA
= NA
;
793 RA
.Addr
->setReachingDef(0);
794 RA
.Addr
->setSibling(0);
795 if (NA
.Addr
->getKind() == NodeAttrs::Def
) {
796 NodeAddr
<DefNode
*> DA
= NA
;
797 DA
.Addr
->setReachedDef(0);
798 DA
.Addr
->setReachedUse(0);
804 // Allocation routines for specific node types/kinds.
806 NodeAddr
<UseNode
*> DataFlowGraph::newUse(NodeAddr
<InstrNode
*> Owner
,
807 MachineOperand
&Op
, uint16_t Flags
) {
808 NodeAddr
<UseNode
*> UA
= newNode(NodeAttrs::Ref
| NodeAttrs::Use
| Flags
);
809 UA
.Addr
->setRegRef(&Op
, *this);
813 NodeAddr
<PhiUseNode
*> DataFlowGraph::newPhiUse(NodeAddr
<PhiNode
*> Owner
,
814 RegisterRef RR
, NodeAddr
<BlockNode
*> PredB
, uint16_t Flags
) {
815 NodeAddr
<PhiUseNode
*> PUA
= newNode(NodeAttrs::Ref
| NodeAttrs::Use
| Flags
);
816 assert(Flags
& NodeAttrs::PhiRef
);
817 PUA
.Addr
->setRegRef(RR
, *this);
818 PUA
.Addr
->setPredecessor(PredB
.Id
);
822 NodeAddr
<DefNode
*> DataFlowGraph::newDef(NodeAddr
<InstrNode
*> Owner
,
823 MachineOperand
&Op
, uint16_t Flags
) {
824 NodeAddr
<DefNode
*> DA
= newNode(NodeAttrs::Ref
| NodeAttrs::Def
| Flags
);
825 DA
.Addr
->setRegRef(&Op
, *this);
829 NodeAddr
<DefNode
*> DataFlowGraph::newDef(NodeAddr
<InstrNode
*> Owner
,
830 RegisterRef RR
, uint16_t Flags
) {
831 NodeAddr
<DefNode
*> DA
= newNode(NodeAttrs::Ref
| NodeAttrs::Def
| Flags
);
832 assert(Flags
& NodeAttrs::PhiRef
);
833 DA
.Addr
->setRegRef(RR
, *this);
837 NodeAddr
<PhiNode
*> DataFlowGraph::newPhi(NodeAddr
<BlockNode
*> Owner
) {
838 NodeAddr
<PhiNode
*> PA
= newNode(NodeAttrs::Code
| NodeAttrs::Phi
);
839 Owner
.Addr
->addPhi(PA
, *this);
843 NodeAddr
<StmtNode
*> DataFlowGraph::newStmt(NodeAddr
<BlockNode
*> Owner
,
845 NodeAddr
<StmtNode
*> SA
= newNode(NodeAttrs::Code
| NodeAttrs::Stmt
);
846 SA
.Addr
->setCode(MI
);
847 Owner
.Addr
->addMember(SA
, *this);
851 NodeAddr
<BlockNode
*> DataFlowGraph::newBlock(NodeAddr
<FuncNode
*> Owner
,
852 MachineBasicBlock
*BB
) {
853 NodeAddr
<BlockNode
*> BA
= newNode(NodeAttrs::Code
| NodeAttrs::Block
);
854 BA
.Addr
->setCode(BB
);
855 Owner
.Addr
->addMember(BA
, *this);
859 NodeAddr
<FuncNode
*> DataFlowGraph::newFunc(MachineFunction
*MF
) {
860 NodeAddr
<FuncNode
*> FA
= newNode(NodeAttrs::Code
| NodeAttrs::Func
);
861 FA
.Addr
->setCode(MF
);
865 // Build the data flow graph.
866 void DataFlowGraph::build(unsigned Options
) {
873 for (MachineBasicBlock
&B
: MF
) {
874 NodeAddr
<BlockNode
*> BA
= newBlock(Func
, &B
);
875 BlockNodes
.insert(std::make_pair(&B
, BA
));
876 for (MachineInstr
&I
: B
) {
877 if (I
.isDebugInstr())
883 NodeAddr
<BlockNode
*> EA
= Func
.Addr
->getEntryBlock(*this);
884 NodeList Blocks
= Func
.Addr
->members(*this);
886 // Collect information about block references.
888 for (NodeAddr
<BlockNode
*> BA
: Blocks
)
889 for (NodeAddr
<InstrNode
*> IA
: BA
.Addr
->members(*this))
890 for (NodeAddr
<RefNode
*> RA
: IA
.Addr
->members(*this))
891 AllRefs
.insert(RA
.Addr
->getRegRef(*this));
893 // Collect function live-ins and entry block live-ins.
894 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
895 MachineBasicBlock
&EntryB
= *EA
.Addr
->getCode();
896 assert(EntryB
.pred_empty() && "Function entry block has predecessors");
897 for (std::pair
<unsigned,unsigned> P
: MRI
.liveins())
898 LiveIns
.insert(RegisterRef(P
.first
));
899 if (MRI
.tracksLiveness()) {
900 for (auto I
: EntryB
.liveins())
901 LiveIns
.insert(RegisterRef(I
.PhysReg
, I
.LaneMask
));
904 // Add function-entry phi nodes for the live-in registers.
905 //for (std::pair<RegisterId,LaneBitmask> P : LiveIns) {
906 for (auto I
= LiveIns
.rr_begin(), E
= LiveIns
.rr_end(); I
!= E
; ++I
) {
908 NodeAddr
<PhiNode
*> PA
= newPhi(EA
);
909 uint16_t PhiFlags
= NodeAttrs::PhiRef
| NodeAttrs::Preserving
;
910 NodeAddr
<DefNode
*> DA
= newDef(PA
, RR
, PhiFlags
);
911 PA
.Addr
->addMember(DA
, *this);
914 // Add phis for landing pads.
915 // Landing pads, unlike usual backs blocks, are not entered through
916 // branches in the program, or fall-throughs from other blocks. They
917 // are entered from the exception handling runtime and target's ABI
918 // may define certain registers as defined on entry to such a block.
919 RegisterSet EHRegs
= getLandingPadLiveIns();
920 if (!EHRegs
.empty()) {
921 for (NodeAddr
<BlockNode
*> BA
: Blocks
) {
922 const MachineBasicBlock
&B
= *BA
.Addr
->getCode();
926 // Prepare a list of NodeIds of the block's predecessors.
928 for (MachineBasicBlock
*PB
: B
.predecessors())
929 Preds
.push_back(findBlock(PB
));
931 // Build phi nodes for each live-in.
932 for (RegisterRef RR
: EHRegs
) {
933 NodeAddr
<PhiNode
*> PA
= newPhi(BA
);
934 uint16_t PhiFlags
= NodeAttrs::PhiRef
| NodeAttrs::Preserving
;
936 NodeAddr
<DefNode
*> DA
= newDef(PA
, RR
, PhiFlags
);
937 PA
.Addr
->addMember(DA
, *this);
938 // Add uses (no reaching defs for phi uses):
939 for (NodeAddr
<BlockNode
*> PBA
: Preds
) {
940 NodeAddr
<PhiUseNode
*> PUA
= newPhiUse(PA
, RR
, PBA
);
941 PA
.Addr
->addMember(PUA
, *this);
947 // Build a map "PhiM" which will contain, for each block, the set
948 // of references that will require phi definitions in that block.
950 for (NodeAddr
<BlockNode
*> BA
: Blocks
)
951 recordDefsForDF(PhiM
, BA
);
952 for (NodeAddr
<BlockNode
*> BA
: Blocks
)
953 buildPhis(PhiM
, AllRefs
, BA
);
955 // Link all the refs. This will recursively traverse the dominator tree.
957 linkBlockRefs(DM
, EA
);
959 // Finally, remove all unused phi nodes.
960 if (!(Options
& BuildOptions::KeepDeadPhis
))
964 RegisterRef
DataFlowGraph::makeRegRef(unsigned Reg
, unsigned Sub
) const {
965 assert(PhysicalRegisterInfo::isRegMaskId(Reg
) ||
966 Register::isPhysicalRegister(Reg
));
969 Reg
= TRI
.getSubReg(Reg
, Sub
);
970 return RegisterRef(Reg
);
973 RegisterRef
DataFlowGraph::makeRegRef(const MachineOperand
&Op
) const {
974 assert(Op
.isReg() || Op
.isRegMask());
976 return makeRegRef(Op
.getReg(), Op
.getSubReg());
977 return RegisterRef(PRI
.getRegMaskId(Op
.getRegMask()), LaneBitmask::getAll());
980 RegisterRef
DataFlowGraph::restrictRef(RegisterRef AR
, RegisterRef BR
) const {
981 if (AR
.Reg
== BR
.Reg
) {
982 LaneBitmask M
= AR
.Mask
& BR
.Mask
;
983 return M
.any() ? RegisterRef(AR
.Reg
, M
) : RegisterRef();
986 // RegisterRef NAR = PRI.normalize(AR);
987 // RegisterRef NBR = PRI.normalize(BR);
988 // assert(NAR.Reg != NBR.Reg);
990 // This isn't strictly correct, because the overlap may happen in the
992 if (PRI
.alias(AR
, BR
))
994 return RegisterRef();
997 // For each stack in the map DefM, push the delimiter for block B on it.
998 void DataFlowGraph::markBlock(NodeId B
, DefStackMap
&DefM
) {
999 // Push block delimiters.
1000 for (auto I
= DefM
.begin(), E
= DefM
.end(); I
!= E
; ++I
)
1001 I
->second
.start_block(B
);
1004 // Remove all definitions coming from block B from each stack in DefM.
1005 void DataFlowGraph::releaseBlock(NodeId B
, DefStackMap
&DefM
) {
1006 // Pop all defs from this block from the definition stack. Defs that were
1007 // added to the map during the traversal of instructions will not have a
1008 // delimiter, but for those, the whole stack will be emptied.
1009 for (auto I
= DefM
.begin(), E
= DefM
.end(); I
!= E
; ++I
)
1010 I
->second
.clear_block(B
);
1012 // Finally, remove empty stacks from the map.
1013 for (auto I
= DefM
.begin(), E
= DefM
.end(), NextI
= I
; I
!= E
; I
= NextI
) {
1014 NextI
= std::next(I
);
1015 // This preserves the validity of iterators other than I.
1016 if (I
->second
.empty())
1021 // Push all definitions from the instruction node IA to an appropriate
1023 void DataFlowGraph::pushAllDefs(NodeAddr
<InstrNode
*> IA
, DefStackMap
&DefM
) {
1024 pushClobbers(IA
, DefM
);
1028 // Push all definitions from the instruction node IA to an appropriate
1030 void DataFlowGraph::pushClobbers(NodeAddr
<InstrNode
*> IA
, DefStackMap
&DefM
) {
1032 std::set
<RegisterId
> Defined
;
1034 // The important objectives of this function are:
1035 // - to be able to handle instructions both while the graph is being
1036 // constructed, and after the graph has been constructed, and
1037 // - maintain proper ordering of definitions on the stack for each
1038 // register reference:
1039 // - if there are two or more related defs in IA (i.e. coming from
1040 // the same machine operand), then only push one def on the stack,
1041 // - if there are multiple unrelated defs of non-overlapping
1042 // subregisters of S, then the stack for S will have both (in an
1043 // unspecified order), but the order does not matter from the data-
1044 // -flow perspective.
1046 for (NodeAddr
<DefNode
*> DA
: IA
.Addr
->members_if(IsDef
, *this)) {
1047 if (Visited
.count(DA
.Id
))
1049 if (!(DA
.Addr
->getFlags() & NodeAttrs::Clobbering
))
1052 NodeList Rel
= getRelatedRefs(IA
, DA
);
1053 NodeAddr
<DefNode
*> PDA
= Rel
.front();
1054 RegisterRef RR
= PDA
.Addr
->getRegRef(*this);
1056 // Push the definition on the stack for the register and all aliases.
1057 // The def stack traversal in linkNodeUp will check the exact aliasing.
1058 DefM
[RR
.Reg
].push(DA
);
1059 Defined
.insert(RR
.Reg
);
1060 for (RegisterId A
: PRI
.getAliasSet(RR
.Reg
)) {
1061 // Check that we don't push the same def twice.
1062 assert(A
!= RR
.Reg
);
1063 if (!Defined
.count(A
))
1066 // Mark all the related defs as visited.
1067 for (NodeAddr
<NodeBase
*> T
: Rel
)
1068 Visited
.insert(T
.Id
);
1072 // Push all definitions from the instruction node IA to an appropriate
1074 void DataFlowGraph::pushDefs(NodeAddr
<InstrNode
*> IA
, DefStackMap
&DefM
) {
1077 std::set
<RegisterId
> Defined
;
1080 // The important objectives of this function are:
1081 // - to be able to handle instructions both while the graph is being
1082 // constructed, and after the graph has been constructed, and
1083 // - maintain proper ordering of definitions on the stack for each
1084 // register reference:
1085 // - if there are two or more related defs in IA (i.e. coming from
1086 // the same machine operand), then only push one def on the stack,
1087 // - if there are multiple unrelated defs of non-overlapping
1088 // subregisters of S, then the stack for S will have both (in an
1089 // unspecified order), but the order does not matter from the data-
1090 // -flow perspective.
1092 for (NodeAddr
<DefNode
*> DA
: IA
.Addr
->members_if(IsDef
, *this)) {
1093 if (Visited
.count(DA
.Id
))
1095 if (DA
.Addr
->getFlags() & NodeAttrs::Clobbering
)
1098 NodeList Rel
= getRelatedRefs(IA
, DA
);
1099 NodeAddr
<DefNode
*> PDA
= Rel
.front();
1100 RegisterRef RR
= PDA
.Addr
->getRegRef(*this);
1102 // Assert if the register is defined in two or more unrelated defs.
1103 // This could happen if there are two or more def operands defining it.
1104 if (!Defined
.insert(RR
.Reg
).second
) {
1105 MachineInstr
*MI
= NodeAddr
<StmtNode
*>(IA
).Addr
->getCode();
1106 dbgs() << "Multiple definitions of register: "
1107 << Print
<RegisterRef
>(RR
, *this) << " in\n " << *MI
<< "in "
1108 << printMBBReference(*MI
->getParent()) << '\n';
1109 llvm_unreachable(nullptr);
1112 // Push the definition on the stack for the register and all aliases.
1113 // The def stack traversal in linkNodeUp will check the exact aliasing.
1114 DefM
[RR
.Reg
].push(DA
);
1115 for (RegisterId A
: PRI
.getAliasSet(RR
.Reg
)) {
1116 // Check that we don't push the same def twice.
1117 assert(A
!= RR
.Reg
);
1120 // Mark all the related defs as visited.
1121 for (NodeAddr
<NodeBase
*> T
: Rel
)
1122 Visited
.insert(T
.Id
);
1126 // Return the list of all reference nodes related to RA, including RA itself.
1127 // See "getNextRelated" for the meaning of a "related reference".
1128 NodeList
DataFlowGraph::getRelatedRefs(NodeAddr
<InstrNode
*> IA
,
1129 NodeAddr
<RefNode
*> RA
) const {
1130 assert(IA
.Id
!= 0 && RA
.Id
!= 0);
1133 NodeId Start
= RA
.Id
;
1136 RA
= getNextRelated(IA
, RA
);
1137 } while (RA
.Id
!= 0 && RA
.Id
!= Start
);
1141 // Clear all information in the graph.
1142 void DataFlowGraph::reset() {
1145 Func
= NodeAddr
<FuncNode
*>();
1148 // Return the next reference node in the instruction node IA that is related
1149 // to RA. Conceptually, two reference nodes are related if they refer to the
1150 // same instance of a register access, but differ in flags or other minor
1151 // characteristics. Specific examples of related nodes are shadow reference
1153 // Return the equivalent of nullptr if there are no more related references.
1154 NodeAddr
<RefNode
*> DataFlowGraph::getNextRelated(NodeAddr
<InstrNode
*> IA
,
1155 NodeAddr
<RefNode
*> RA
) const {
1156 assert(IA
.Id
!= 0 && RA
.Id
!= 0);
1158 auto Related
= [this,RA
](NodeAddr
<RefNode
*> TA
) -> bool {
1159 if (TA
.Addr
->getKind() != RA
.Addr
->getKind())
1161 if (TA
.Addr
->getRegRef(*this) != RA
.Addr
->getRegRef(*this))
1165 auto RelatedStmt
= [&Related
,RA
](NodeAddr
<RefNode
*> TA
) -> bool {
1166 return Related(TA
) &&
1167 &RA
.Addr
->getOp() == &TA
.Addr
->getOp();
1169 auto RelatedPhi
= [&Related
,RA
](NodeAddr
<RefNode
*> TA
) -> bool {
1172 if (TA
.Addr
->getKind() != NodeAttrs::Use
)
1174 // For phi uses, compare predecessor blocks.
1175 const NodeAddr
<const PhiUseNode
*> TUA
= TA
;
1176 const NodeAddr
<const PhiUseNode
*> RUA
= RA
;
1177 return TUA
.Addr
->getPredecessor() == RUA
.Addr
->getPredecessor();
1180 RegisterRef RR
= RA
.Addr
->getRegRef(*this);
1181 if (IA
.Addr
->getKind() == NodeAttrs::Stmt
)
1182 return RA
.Addr
->getNextRef(RR
, RelatedStmt
, true, *this);
1183 return RA
.Addr
->getNextRef(RR
, RelatedPhi
, true, *this);
1186 // Find the next node related to RA in IA that satisfies condition P.
1187 // If such a node was found, return a pair where the second element is the
1188 // located node. If such a node does not exist, return a pair where the
1189 // first element is the element after which such a node should be inserted,
1190 // and the second element is a null-address.
1191 template <typename Predicate
>
1192 std::pair
<NodeAddr
<RefNode
*>,NodeAddr
<RefNode
*>>
1193 DataFlowGraph::locateNextRef(NodeAddr
<InstrNode
*> IA
, NodeAddr
<RefNode
*> RA
,
1194 Predicate P
) const {
1195 assert(IA
.Id
!= 0 && RA
.Id
!= 0);
1197 NodeAddr
<RefNode
*> NA
;
1198 NodeId Start
= RA
.Id
;
1200 NA
= getNextRelated(IA
, RA
);
1201 if (NA
.Id
== 0 || NA
.Id
== Start
)
1208 if (NA
.Id
!= 0 && NA
.Id
!= Start
)
1209 return std::make_pair(RA
, NA
);
1210 return std::make_pair(RA
, NodeAddr
<RefNode
*>());
1213 // Get the next shadow node in IA corresponding to RA, and optionally create
1214 // such a node if it does not exist.
1215 NodeAddr
<RefNode
*> DataFlowGraph::getNextShadow(NodeAddr
<InstrNode
*> IA
,
1216 NodeAddr
<RefNode
*> RA
, bool Create
) {
1217 assert(IA
.Id
!= 0 && RA
.Id
!= 0);
1219 uint16_t Flags
= RA
.Addr
->getFlags() | NodeAttrs::Shadow
;
1220 auto IsShadow
= [Flags
] (NodeAddr
<RefNode
*> TA
) -> bool {
1221 return TA
.Addr
->getFlags() == Flags
;
1223 auto Loc
= locateNextRef(IA
, RA
, IsShadow
);
1224 if (Loc
.second
.Id
!= 0 || !Create
)
1227 // Create a copy of RA and mark is as shadow.
1228 NodeAddr
<RefNode
*> NA
= cloneNode(RA
);
1229 NA
.Addr
->setFlags(Flags
| NodeAttrs::Shadow
);
1230 IA
.Addr
->addMemberAfter(Loc
.first
, NA
, *this);
1234 // Get the next shadow node in IA corresponding to RA. Return null-address
1235 // if such a node does not exist.
1236 NodeAddr
<RefNode
*> DataFlowGraph::getNextShadow(NodeAddr
<InstrNode
*> IA
,
1237 NodeAddr
<RefNode
*> RA
) const {
1238 assert(IA
.Id
!= 0 && RA
.Id
!= 0);
1239 uint16_t Flags
= RA
.Addr
->getFlags() | NodeAttrs::Shadow
;
1240 auto IsShadow
= [Flags
] (NodeAddr
<RefNode
*> TA
) -> bool {
1241 return TA
.Addr
->getFlags() == Flags
;
1243 return locateNextRef(IA
, RA
, IsShadow
).second
;
1246 // Create a new statement node in the block node BA that corresponds to
1247 // the machine instruction MI.
1248 void DataFlowGraph::buildStmt(NodeAddr
<BlockNode
*> BA
, MachineInstr
&In
) {
1249 NodeAddr
<StmtNode
*> SA
= newStmt(BA
, &In
);
1251 auto isCall
= [] (const MachineInstr
&In
) -> bool {
1255 if (In
.isBranch()) {
1256 for (const MachineOperand
&Op
: In
.operands())
1257 if (Op
.isGlobal() || Op
.isSymbol())
1259 // Assume indirect branches are calls. This is for the purpose of
1260 // keeping implicit operands, and so it won't hurt on intra-function
1261 // indirect branches.
1262 if (In
.isIndirectBranch())
1268 auto isDefUndef
= [this] (const MachineInstr
&In
, RegisterRef DR
) -> bool {
1269 // This instruction defines DR. Check if there is a use operand that
1270 // would make DR live on entry to the instruction.
1271 for (const MachineOperand
&Op
: In
.operands()) {
1272 if (!Op
.isReg() || Op
.getReg() == 0 || !Op
.isUse() || Op
.isUndef())
1274 RegisterRef UR
= makeRegRef(Op
);
1275 if (PRI
.alias(DR
, UR
))
1281 bool IsCall
= isCall(In
);
1282 unsigned NumOps
= In
.getNumOperands();
1284 // Avoid duplicate implicit defs. This will not detect cases of implicit
1285 // defs that define registers that overlap, but it is not clear how to
1286 // interpret that in the absence of explicit defs. Overlapping explicit
1287 // defs are likely illegal already.
1288 BitVector
DoneDefs(TRI
.getNumRegs());
1289 // Process explicit defs first.
1290 for (unsigned OpN
= 0; OpN
< NumOps
; ++OpN
) {
1291 MachineOperand
&Op
= In
.getOperand(OpN
);
1292 if (!Op
.isReg() || !Op
.isDef() || Op
.isImplicit())
1294 Register R
= Op
.getReg();
1295 if (!R
|| !Register::isPhysicalRegister(R
))
1297 uint16_t Flags
= NodeAttrs::None
;
1298 if (TOI
.isPreserving(In
, OpN
)) {
1299 Flags
|= NodeAttrs::Preserving
;
1300 // If the def is preserving, check if it is also undefined.
1301 if (isDefUndef(In
, makeRegRef(Op
)))
1302 Flags
|= NodeAttrs::Undef
;
1304 if (TOI
.isClobbering(In
, OpN
))
1305 Flags
|= NodeAttrs::Clobbering
;
1306 if (TOI
.isFixedReg(In
, OpN
))
1307 Flags
|= NodeAttrs::Fixed
;
1308 if (IsCall
&& Op
.isDead())
1309 Flags
|= NodeAttrs::Dead
;
1310 NodeAddr
<DefNode
*> DA
= newDef(SA
, Op
, Flags
);
1311 SA
.Addr
->addMember(DA
, *this);
1312 assert(!DoneDefs
.test(R
));
1316 // Process reg-masks (as clobbers).
1317 BitVector
DoneClobbers(TRI
.getNumRegs());
1318 for (unsigned OpN
= 0; OpN
< NumOps
; ++OpN
) {
1319 MachineOperand
&Op
= In
.getOperand(OpN
);
1320 if (!Op
.isRegMask())
1322 uint16_t Flags
= NodeAttrs::Clobbering
| NodeAttrs::Fixed
|
1324 NodeAddr
<DefNode
*> DA
= newDef(SA
, Op
, Flags
);
1325 SA
.Addr
->addMember(DA
, *this);
1326 // Record all clobbered registers in DoneDefs.
1327 const uint32_t *RM
= Op
.getRegMask();
1328 for (unsigned i
= 1, e
= TRI
.getNumRegs(); i
!= e
; ++i
)
1329 if (!(RM
[i
/32] & (1u << (i
%32))))
1330 DoneClobbers
.set(i
);
1333 // Process implicit defs, skipping those that have already been added
1335 for (unsigned OpN
= 0; OpN
< NumOps
; ++OpN
) {
1336 MachineOperand
&Op
= In
.getOperand(OpN
);
1337 if (!Op
.isReg() || !Op
.isDef() || !Op
.isImplicit())
1339 Register R
= Op
.getReg();
1340 if (!R
|| !Register::isPhysicalRegister(R
) || DoneDefs
.test(R
))
1342 RegisterRef RR
= makeRegRef(Op
);
1343 uint16_t Flags
= NodeAttrs::None
;
1344 if (TOI
.isPreserving(In
, OpN
)) {
1345 Flags
|= NodeAttrs::Preserving
;
1346 // If the def is preserving, check if it is also undefined.
1347 if (isDefUndef(In
, RR
))
1348 Flags
|= NodeAttrs::Undef
;
1350 if (TOI
.isClobbering(In
, OpN
))
1351 Flags
|= NodeAttrs::Clobbering
;
1352 if (TOI
.isFixedReg(In
, OpN
))
1353 Flags
|= NodeAttrs::Fixed
;
1354 if (IsCall
&& Op
.isDead()) {
1355 if (DoneClobbers
.test(R
))
1357 Flags
|= NodeAttrs::Dead
;
1359 NodeAddr
<DefNode
*> DA
= newDef(SA
, Op
, Flags
);
1360 SA
.Addr
->addMember(DA
, *this);
1364 for (unsigned OpN
= 0; OpN
< NumOps
; ++OpN
) {
1365 MachineOperand
&Op
= In
.getOperand(OpN
);
1366 if (!Op
.isReg() || !Op
.isUse())
1368 Register R
= Op
.getReg();
1369 if (!R
|| !Register::isPhysicalRegister(R
))
1371 uint16_t Flags
= NodeAttrs::None
;
1373 Flags
|= NodeAttrs::Undef
;
1374 if (TOI
.isFixedReg(In
, OpN
))
1375 Flags
|= NodeAttrs::Fixed
;
1376 NodeAddr
<UseNode
*> UA
= newUse(SA
, Op
, Flags
);
1377 SA
.Addr
->addMember(UA
, *this);
1381 // Scan all defs in the block node BA and record in PhiM the locations of
1382 // phi nodes corresponding to these defs.
1383 void DataFlowGraph::recordDefsForDF(BlockRefsMap
&PhiM
,
1384 NodeAddr
<BlockNode
*> BA
) {
1385 // Check all defs from block BA and record them in each block in BA's
1386 // iterated dominance frontier. This information will later be used to
1387 // create phi nodes.
1388 MachineBasicBlock
*BB
= BA
.Addr
->getCode();
1390 auto DFLoc
= MDF
.find(BB
);
1391 if (DFLoc
== MDF
.end() || DFLoc
->second
.empty())
1394 // Traverse all instructions in the block and collect the set of all
1395 // defined references. For each reference there will be a phi created
1396 // in the block's iterated dominance frontier.
1397 // This is done to make sure that each defined reference gets only one
1398 // phi node, even if it is defined multiple times.
1400 for (NodeAddr
<InstrNode
*> IA
: BA
.Addr
->members(*this))
1401 for (NodeAddr
<RefNode
*> RA
: IA
.Addr
->members_if(IsDef
, *this))
1402 Defs
.insert(RA
.Addr
->getRegRef(*this));
1404 // Calculate the iterated dominance frontier of BB.
1405 const MachineDominanceFrontier::DomSetType
&DF
= DFLoc
->second
;
1406 SetVector
<MachineBasicBlock
*> IDF(DF
.begin(), DF
.end());
1407 for (unsigned i
= 0; i
< IDF
.size(); ++i
) {
1408 auto F
= MDF
.find(IDF
[i
]);
1410 IDF
.insert(F
->second
.begin(), F
->second
.end());
1413 // Finally, add the set of defs to each block in the iterated dominance
1415 for (auto DB
: IDF
) {
1416 NodeAddr
<BlockNode
*> DBA
= findBlock(DB
);
1417 PhiM
[DBA
.Id
].insert(Defs
.begin(), Defs
.end());
1421 // Given the locations of phi nodes in the map PhiM, create the phi nodes
1422 // that are located in the block node BA.
1423 void DataFlowGraph::buildPhis(BlockRefsMap
&PhiM
, RegisterSet
&AllRefs
,
1424 NodeAddr
<BlockNode
*> BA
) {
1425 // Check if this blocks has any DF defs, i.e. if there are any defs
1426 // that this block is in the iterated dominance frontier of.
1427 auto HasDF
= PhiM
.find(BA
.Id
);
1428 if (HasDF
== PhiM
.end() || HasDF
->second
.empty())
1431 // First, remove all R in Refs in such that there exists T in Refs
1432 // such that T covers R. In other words, only leave those refs that
1433 // are not covered by another ref (i.e. maximal with respect to covering).
1435 auto MaxCoverIn
= [this] (RegisterRef RR
, RegisterSet
&RRs
) -> RegisterRef
{
1436 for (RegisterRef I
: RRs
)
1437 if (I
!= RR
&& RegisterAggr::isCoverOf(I
, RR
, PRI
))
1443 for (RegisterRef I
: HasDF
->second
)
1444 MaxDF
.insert(MaxCoverIn(I
, HasDF
->second
));
1446 std::vector
<RegisterRef
> MaxRefs
;
1447 for (RegisterRef I
: MaxDF
)
1448 MaxRefs
.push_back(MaxCoverIn(I
, AllRefs
));
1450 // Now, for each R in MaxRefs, get the alias closure of R. If the closure
1451 // only has R in it, create a phi a def for R. Otherwise, create a phi,
1452 // and add a def for each S in the closure.
1454 // Sort the refs so that the phis will be created in a deterministic order.
1455 llvm::sort(MaxRefs
);
1456 // Remove duplicates.
1457 auto NewEnd
= std::unique(MaxRefs
.begin(), MaxRefs
.end());
1458 MaxRefs
.erase(NewEnd
, MaxRefs
.end());
1460 auto Aliased
= [this,&MaxRefs
](RegisterRef RR
,
1461 std::vector
<unsigned> &Closure
) -> bool {
1462 for (unsigned I
: Closure
)
1463 if (PRI
.alias(RR
, MaxRefs
[I
]))
1468 // Prepare a list of NodeIds of the block's predecessors.
1470 const MachineBasicBlock
*MBB
= BA
.Addr
->getCode();
1471 for (MachineBasicBlock
*PB
: MBB
->predecessors())
1472 Preds
.push_back(findBlock(PB
));
1474 while (!MaxRefs
.empty()) {
1475 // Put the first element in the closure, and then add all subsequent
1476 // elements from MaxRefs to it, if they alias at least one element
1477 // already in the closure.
1478 // ClosureIdx: vector of indices in MaxRefs of members of the closure.
1479 std::vector
<unsigned> ClosureIdx
= { 0 };
1480 for (unsigned i
= 1; i
!= MaxRefs
.size(); ++i
)
1481 if (Aliased(MaxRefs
[i
], ClosureIdx
))
1482 ClosureIdx
.push_back(i
);
1484 // Build a phi for the closure.
1485 unsigned CS
= ClosureIdx
.size();
1486 NodeAddr
<PhiNode
*> PA
= newPhi(BA
);
1489 for (unsigned X
= 0; X
!= CS
; ++X
) {
1490 RegisterRef RR
= MaxRefs
[ClosureIdx
[X
]];
1491 uint16_t PhiFlags
= NodeAttrs::PhiRef
| NodeAttrs::Preserving
;
1492 NodeAddr
<DefNode
*> DA
= newDef(PA
, RR
, PhiFlags
);
1493 PA
.Addr
->addMember(DA
, *this);
1496 for (NodeAddr
<BlockNode
*> PBA
: Preds
) {
1497 for (unsigned X
= 0; X
!= CS
; ++X
) {
1498 RegisterRef RR
= MaxRefs
[ClosureIdx
[X
]];
1499 NodeAddr
<PhiUseNode
*> PUA
= newPhiUse(PA
, RR
, PBA
);
1500 PA
.Addr
->addMember(PUA
, *this);
1504 // Erase from MaxRefs all elements in the closure.
1505 auto Begin
= MaxRefs
.begin();
1506 for (unsigned i
= ClosureIdx
.size(); i
!= 0; --i
)
1507 MaxRefs
.erase(Begin
+ ClosureIdx
[i
-1]);
1511 // Remove any unneeded phi nodes that were created during the build process.
1512 void DataFlowGraph::removeUnusedPhis() {
1513 // This will remove unused phis, i.e. phis where each def does not reach
1514 // any uses or other defs. This will not detect or remove circular phi
1515 // chains that are otherwise dead. Unused/dead phis are created during
1516 // the build process and this function is intended to remove these cases
1517 // that are easily determinable to be unnecessary.
1519 SetVector
<NodeId
> PhiQ
;
1520 for (NodeAddr
<BlockNode
*> BA
: Func
.Addr
->members(*this)) {
1521 for (auto P
: BA
.Addr
->members_if(IsPhi
, *this))
1525 static auto HasUsedDef
= [](NodeList
&Ms
) -> bool {
1526 for (NodeAddr
<NodeBase
*> M
: Ms
) {
1527 if (M
.Addr
->getKind() != NodeAttrs::Def
)
1529 NodeAddr
<DefNode
*> DA
= M
;
1530 if (DA
.Addr
->getReachedDef() != 0 || DA
.Addr
->getReachedUse() != 0)
1536 // Any phi, if it is removed, may affect other phis (make them dead).
1537 // For each removed phi, collect the potentially affected phis and add
1538 // them back to the queue.
1539 while (!PhiQ
.empty()) {
1540 auto PA
= addr
<PhiNode
*>(PhiQ
[0]);
1542 NodeList Refs
= PA
.Addr
->members(*this);
1543 if (HasUsedDef(Refs
))
1545 for (NodeAddr
<RefNode
*> RA
: Refs
) {
1546 if (NodeId RD
= RA
.Addr
->getReachingDef()) {
1547 auto RDA
= addr
<DefNode
*>(RD
);
1548 NodeAddr
<InstrNode
*> OA
= RDA
.Addr
->getOwner(*this);
1552 if (RA
.Addr
->isDef())
1553 unlinkDef(RA
, true);
1555 unlinkUse(RA
, true);
1557 NodeAddr
<BlockNode
*> BA
= PA
.Addr
->getOwner(*this);
1558 BA
.Addr
->removeMember(PA
, *this);
1562 // For a given reference node TA in an instruction node IA, connect the
1563 // reaching def of TA to the appropriate def node. Create any shadow nodes
1565 template <typename T
>
1566 void DataFlowGraph::linkRefUp(NodeAddr
<InstrNode
*> IA
, NodeAddr
<T
> TA
,
1570 RegisterRef RR
= TA
.Addr
->getRegRef(*this);
1573 // References from the def stack that have been examined so far.
1574 RegisterAggr
Defs(PRI
);
1576 for (auto I
= DS
.top(), E
= DS
.bottom(); I
!= E
; I
.down()) {
1577 RegisterRef QR
= I
->Addr
->getRegRef(*this);
1579 // Skip all defs that are aliased to any of the defs that we have already
1580 // seen. If this completes a cover of RR, stop the stack traversal.
1581 bool Alias
= Defs
.hasAliasOf(QR
);
1582 bool Cover
= Defs
.insert(QR
).hasCoverOf(RR
);
1589 // The reaching def.
1590 NodeAddr
<DefNode
*> RDA
= *I
;
1592 // Pick the reached node.
1596 // Mark the existing ref as "shadow" and create a new shadow.
1597 TAP
.Addr
->setFlags(TAP
.Addr
->getFlags() | NodeAttrs::Shadow
);
1598 TAP
= getNextShadow(IA
, TAP
, true);
1602 TAP
.Addr
->linkToDef(TAP
.Id
, RDA
);
1609 // Create data-flow links for all reference nodes in the statement node SA.
1610 template <typename Predicate
>
1611 void DataFlowGraph::linkStmtRefs(DefStackMap
&DefM
, NodeAddr
<StmtNode
*> SA
,
1617 // Link all nodes (upwards in the data-flow) with their reaching defs.
1618 for (NodeAddr
<RefNode
*> RA
: SA
.Addr
->members_if(P
, *this)) {
1619 uint16_t Kind
= RA
.Addr
->getKind();
1620 assert(Kind
== NodeAttrs::Def
|| Kind
== NodeAttrs::Use
);
1621 RegisterRef RR
= RA
.Addr
->getRegRef(*this);
1623 // Do not expect multiple defs of the same reference.
1624 assert(Kind
!= NodeAttrs::Def
|| !Defs
.count(RR
));
1628 auto F
= DefM
.find(RR
.Reg
);
1629 if (F
== DefM
.end())
1631 DefStack
&DS
= F
->second
;
1632 if (Kind
== NodeAttrs::Use
)
1633 linkRefUp
<UseNode
*>(SA
, RA
, DS
);
1634 else if (Kind
== NodeAttrs::Def
)
1635 linkRefUp
<DefNode
*>(SA
, RA
, DS
);
1637 llvm_unreachable("Unexpected node in instruction");
1641 // Create data-flow links for all instructions in the block node BA. This
1642 // will include updating any phi nodes in BA.
1643 void DataFlowGraph::linkBlockRefs(DefStackMap
&DefM
, NodeAddr
<BlockNode
*> BA
) {
1644 // Push block delimiters.
1645 markBlock(BA
.Id
, DefM
);
1647 auto IsClobber
= [] (NodeAddr
<RefNode
*> RA
) -> bool {
1648 return IsDef(RA
) && (RA
.Addr
->getFlags() & NodeAttrs::Clobbering
);
1650 auto IsNoClobber
= [] (NodeAddr
<RefNode
*> RA
) -> bool {
1651 return IsDef(RA
) && !(RA
.Addr
->getFlags() & NodeAttrs::Clobbering
);
1654 assert(BA
.Addr
&& "block node address is needed to create a data-flow link");
1655 // For each non-phi instruction in the block, link all the defs and uses
1656 // to their reaching defs. For any member of the block (including phis),
1657 // push the defs on the corresponding stacks.
1658 for (NodeAddr
<InstrNode
*> IA
: BA
.Addr
->members(*this)) {
1659 // Ignore phi nodes here. They will be linked part by part from the
1661 if (IA
.Addr
->getKind() == NodeAttrs::Stmt
) {
1662 linkStmtRefs(DefM
, IA
, IsUse
);
1663 linkStmtRefs(DefM
, IA
, IsClobber
);
1666 // Push the definitions on the stack.
1667 pushClobbers(IA
, DefM
);
1669 if (IA
.Addr
->getKind() == NodeAttrs::Stmt
)
1670 linkStmtRefs(DefM
, IA
, IsNoClobber
);
1675 // Recursively process all children in the dominator tree.
1676 MachineDomTreeNode
*N
= MDT
.getNode(BA
.Addr
->getCode());
1678 MachineBasicBlock
*SB
= I
->getBlock();
1679 NodeAddr
<BlockNode
*> SBA
= findBlock(SB
);
1680 linkBlockRefs(DefM
, SBA
);
1683 // Link the phi uses from the successor blocks.
1684 auto IsUseForBA
= [BA
](NodeAddr
<NodeBase
*> NA
) -> bool {
1685 if (NA
.Addr
->getKind() != NodeAttrs::Use
)
1687 assert(NA
.Addr
->getFlags() & NodeAttrs::PhiRef
);
1688 NodeAddr
<PhiUseNode
*> PUA
= NA
;
1689 return PUA
.Addr
->getPredecessor() == BA
.Id
;
1692 RegisterSet EHLiveIns
= getLandingPadLiveIns();
1693 MachineBasicBlock
*MBB
= BA
.Addr
->getCode();
1695 for (MachineBasicBlock
*SB
: MBB
->successors()) {
1696 bool IsEHPad
= SB
->isEHPad();
1697 NodeAddr
<BlockNode
*> SBA
= findBlock(SB
);
1698 for (NodeAddr
<InstrNode
*> IA
: SBA
.Addr
->members_if(IsPhi
, *this)) {
1699 // Do not link phi uses for landing pad live-ins.
1701 // Find what register this phi is for.
1702 NodeAddr
<RefNode
*> RA
= IA
.Addr
->getFirstMember(*this);
1704 if (EHLiveIns
.count(RA
.Addr
->getRegRef(*this)))
1707 // Go over each phi use associated with MBB, and link it.
1708 for (auto U
: IA
.Addr
->members_if(IsUseForBA
, *this)) {
1709 NodeAddr
<PhiUseNode
*> PUA
= U
;
1710 RegisterRef RR
= PUA
.Addr
->getRegRef(*this);
1711 linkRefUp
<UseNode
*>(IA
, PUA
, DefM
[RR
.Reg
]);
1716 // Pop all defs from this block from the definition stacks.
1717 releaseBlock(BA
.Id
, DefM
);
1720 // Remove the use node UA from any data-flow and structural links.
1721 void DataFlowGraph::unlinkUseDF(NodeAddr
<UseNode
*> UA
) {
1722 NodeId RD
= UA
.Addr
->getReachingDef();
1723 NodeId Sib
= UA
.Addr
->getSibling();
1730 auto RDA
= addr
<DefNode
*>(RD
);
1731 auto TA
= addr
<UseNode
*>(RDA
.Addr
->getReachedUse());
1732 if (TA
.Id
== UA
.Id
) {
1733 RDA
.Addr
->setReachedUse(Sib
);
1737 while (TA
.Id
!= 0) {
1738 NodeId S
= TA
.Addr
->getSibling();
1740 TA
.Addr
->setSibling(UA
.Addr
->getSibling());
1743 TA
= addr
<UseNode
*>(S
);
1747 // Remove the def node DA from any data-flow and structural links.
1748 void DataFlowGraph::unlinkDefDF(NodeAddr
<DefNode
*> DA
) {
1756 // ... -- | DA | -- ... -- 0 : sibling chain of DA
1761 // | ... : Siblings (defs)
1765 // ... : sibling chain of reached uses
1767 NodeId RD
= DA
.Addr
->getReachingDef();
1769 // Visit all siblings of the reached def and reset their reaching defs.
1770 // Also, defs reached by DA are now "promoted" to being reached by RD,
1771 // so all of them will need to be spliced into the sibling chain where
1773 auto getAllNodes
= [this] (NodeId N
) -> NodeList
{
1776 auto RA
= addr
<RefNode
*>(N
);
1777 // Keep the nodes in the exact sibling order.
1779 N
= RA
.Addr
->getSibling();
1783 NodeList ReachedDefs
= getAllNodes(DA
.Addr
->getReachedDef());
1784 NodeList ReachedUses
= getAllNodes(DA
.Addr
->getReachedUse());
1787 for (NodeAddr
<RefNode
*> I
: ReachedDefs
)
1788 I
.Addr
->setSibling(0);
1789 for (NodeAddr
<RefNode
*> I
: ReachedUses
)
1790 I
.Addr
->setSibling(0);
1792 for (NodeAddr
<DefNode
*> I
: ReachedDefs
)
1793 I
.Addr
->setReachingDef(RD
);
1794 for (NodeAddr
<UseNode
*> I
: ReachedUses
)
1795 I
.Addr
->setReachingDef(RD
);
1797 NodeId Sib
= DA
.Addr
->getSibling();
1803 // Update the reaching def node and remove DA from the sibling list.
1804 auto RDA
= addr
<DefNode
*>(RD
);
1805 auto TA
= addr
<DefNode
*>(RDA
.Addr
->getReachedDef());
1806 if (TA
.Id
== DA
.Id
) {
1807 // If DA is the first reached def, just update the RD's reached def
1808 // to the DA's sibling.
1809 RDA
.Addr
->setReachedDef(Sib
);
1811 // Otherwise, traverse the sibling list of the reached defs and remove
1813 while (TA
.Id
!= 0) {
1814 NodeId S
= TA
.Addr
->getSibling();
1816 TA
.Addr
->setSibling(Sib
);
1819 TA
= addr
<DefNode
*>(S
);
1823 // Splice the DA's reached defs into the RDA's reached def chain.
1824 if (!ReachedDefs
.empty()) {
1825 auto Last
= NodeAddr
<DefNode
*>(ReachedDefs
.back());
1826 Last
.Addr
->setSibling(RDA
.Addr
->getReachedDef());
1827 RDA
.Addr
->setReachedDef(ReachedDefs
.front().Id
);
1829 // Splice the DA's reached uses into the RDA's reached use chain.
1830 if (!ReachedUses
.empty()) {
1831 auto Last
= NodeAddr
<UseNode
*>(ReachedUses
.back());
1832 Last
.Addr
->setSibling(RDA
.Addr
->getReachedUse());
1833 RDA
.Addr
->setReachedUse(ReachedUses
.front().Id
);