1044eb2f6SDimitry Andric //===- RDFGraph.cpp -------------------------------------------------------===//
2050e163aSDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6050e163aSDimitry Andric //
7050e163aSDimitry Andric //===----------------------------------------------------------------------===//
8050e163aSDimitry Andric //
9050e163aSDimitry Andric // Target-independent, SSA-based data flow graph for register data flow (RDF).
10050e163aSDimitry Andric //
11044eb2f6SDimitry Andric #include "llvm/ADT/BitVector.h"
12c82ad72fSDimitry Andric #include "llvm/ADT/STLExtras.h"
137ab83427SDimitry Andric #include "llvm/ADT/SetVector.h"
14050e163aSDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
15050e163aSDimitry Andric #include "llvm/CodeGen/MachineDominanceFrontier.h"
16050e163aSDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
17050e163aSDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
18c82ad72fSDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
19c82ad72fSDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
20050e163aSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
217fa27ce4SDimitry Andric #include "llvm/CodeGen/RDFGraph.h"
22cfca06d7SDimitry Andric #include "llvm/CodeGen/RDFRegisters.h"
23044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
24044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
25044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
26044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
27c82ad72fSDimitry Andric #include "llvm/IR/Function.h"
28c82ad72fSDimitry Andric #include "llvm/MC/LaneBitmask.h"
29c82ad72fSDimitry Andric #include "llvm/MC/MCInstrDesc.h"
30c82ad72fSDimitry Andric #include "llvm/Support/ErrorHandling.h"
31c82ad72fSDimitry Andric #include "llvm/Support/raw_ostream.h"
32c82ad72fSDimitry Andric #include <algorithm>
33c82ad72fSDimitry Andric #include <cassert>
34c82ad72fSDimitry Andric #include <cstdint>
35c82ad72fSDimitry Andric #include <cstring>
36c82ad72fSDimitry Andric #include <iterator>
37044eb2f6SDimitry Andric #include <set>
38c82ad72fSDimitry Andric #include <utility>
39c82ad72fSDimitry Andric #include <vector>
40050e163aSDimitry Andric
41050e163aSDimitry Andric // Printing functions. Have them here first, so that the rest of the code
42050e163aSDimitry Andric // can use them.
437fa27ce4SDimitry Andric namespace llvm::rdf {
44b915e9e0SDimitry Andric
operator <<(raw_ostream & OS,const Print<RegisterRef> & P)45050e163aSDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Print<RegisterRef> &P) {
467fa27ce4SDimitry Andric P.G.getPRI().print(OS, P.Obj);
47050e163aSDimitry Andric return OS;
48050e163aSDimitry Andric }
49050e163aSDimitry Andric
operator <<(raw_ostream & OS,const Print<NodeId> & P)50050e163aSDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Print<NodeId> &P) {
517fa27ce4SDimitry Andric if (P.Obj == 0)
527fa27ce4SDimitry Andric return OS << "null";
53050e163aSDimitry Andric auto NA = P.G.addr<NodeBase *>(P.Obj);
54050e163aSDimitry Andric uint16_t Attrs = NA.Addr->getAttrs();
55050e163aSDimitry Andric uint16_t Kind = NodeAttrs::kind(Attrs);
56050e163aSDimitry Andric uint16_t Flags = NodeAttrs::flags(Attrs);
57050e163aSDimitry Andric switch (NodeAttrs::type(Attrs)) {
58050e163aSDimitry Andric case NodeAttrs::Code:
59050e163aSDimitry Andric switch (Kind) {
607fa27ce4SDimitry Andric case NodeAttrs::Func:
617fa27ce4SDimitry Andric OS << 'f';
627fa27ce4SDimitry Andric break;
637fa27ce4SDimitry Andric case NodeAttrs::Block:
647fa27ce4SDimitry Andric OS << 'b';
657fa27ce4SDimitry Andric break;
667fa27ce4SDimitry Andric case NodeAttrs::Stmt:
677fa27ce4SDimitry Andric OS << 's';
687fa27ce4SDimitry Andric break;
697fa27ce4SDimitry Andric case NodeAttrs::Phi:
707fa27ce4SDimitry Andric OS << 'p';
717fa27ce4SDimitry Andric break;
727fa27ce4SDimitry Andric default:
737fa27ce4SDimitry Andric OS << "c?";
747fa27ce4SDimitry Andric break;
75050e163aSDimitry Andric }
76050e163aSDimitry Andric break;
77050e163aSDimitry Andric case NodeAttrs::Ref:
78b915e9e0SDimitry Andric if (Flags & NodeAttrs::Undef)
79b915e9e0SDimitry Andric OS << '/';
80b915e9e0SDimitry Andric if (Flags & NodeAttrs::Dead)
81b915e9e0SDimitry Andric OS << '\\';
82050e163aSDimitry Andric if (Flags & NodeAttrs::Preserving)
83050e163aSDimitry Andric OS << '+';
84050e163aSDimitry Andric if (Flags & NodeAttrs::Clobbering)
85050e163aSDimitry Andric OS << '~';
86050e163aSDimitry Andric switch (Kind) {
877fa27ce4SDimitry Andric case NodeAttrs::Use:
887fa27ce4SDimitry Andric OS << 'u';
897fa27ce4SDimitry Andric break;
907fa27ce4SDimitry Andric case NodeAttrs::Def:
917fa27ce4SDimitry Andric OS << 'd';
927fa27ce4SDimitry Andric break;
937fa27ce4SDimitry Andric case NodeAttrs::Block:
947fa27ce4SDimitry Andric OS << 'b';
957fa27ce4SDimitry Andric break;
967fa27ce4SDimitry Andric default:
977fa27ce4SDimitry Andric OS << "r?";
987fa27ce4SDimitry Andric break;
99050e163aSDimitry Andric }
100050e163aSDimitry Andric break;
101050e163aSDimitry Andric default:
102050e163aSDimitry Andric OS << '?';
103050e163aSDimitry Andric break;
104050e163aSDimitry Andric }
105050e163aSDimitry Andric OS << P.Obj;
106050e163aSDimitry Andric if (Flags & NodeAttrs::Shadow)
107050e163aSDimitry Andric OS << '"';
108050e163aSDimitry Andric return OS;
109050e163aSDimitry Andric }
110050e163aSDimitry Andric
printRefHeader(raw_ostream & OS,const Ref RA,const DataFlowGraph & G)1117fa27ce4SDimitry Andric static void printRefHeader(raw_ostream &OS, const Ref RA,
112050e163aSDimitry Andric const DataFlowGraph &G) {
1137fa27ce4SDimitry Andric OS << Print(RA.Id, G) << '<' << Print(RA.Addr->getRegRef(G), G) << '>';
114050e163aSDimitry Andric if (RA.Addr->getFlags() & NodeAttrs::Fixed)
115050e163aSDimitry Andric OS << '!';
116050e163aSDimitry Andric }
117050e163aSDimitry Andric
operator <<(raw_ostream & OS,const Print<Def> & P)1187fa27ce4SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Print<Def> &P) {
119050e163aSDimitry Andric printRefHeader(OS, P.Obj, P.G);
120050e163aSDimitry Andric OS << '(';
121050e163aSDimitry Andric if (NodeId N = P.Obj.Addr->getReachingDef())
122e3b55780SDimitry Andric OS << Print(N, P.G);
123050e163aSDimitry Andric OS << ',';
124050e163aSDimitry Andric if (NodeId N = P.Obj.Addr->getReachedDef())
125e3b55780SDimitry Andric OS << Print(N, P.G);
126050e163aSDimitry Andric OS << ',';
127050e163aSDimitry Andric if (NodeId N = P.Obj.Addr->getReachedUse())
128e3b55780SDimitry Andric OS << Print(N, P.G);
129050e163aSDimitry Andric OS << "):";
130050e163aSDimitry Andric if (NodeId N = P.Obj.Addr->getSibling())
131e3b55780SDimitry Andric OS << Print(N, P.G);
132050e163aSDimitry Andric return OS;
133050e163aSDimitry Andric }
134050e163aSDimitry Andric
operator <<(raw_ostream & OS,const Print<Use> & P)1357fa27ce4SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Print<Use> &P) {
136050e163aSDimitry Andric printRefHeader(OS, P.Obj, P.G);
137050e163aSDimitry Andric OS << '(';
138050e163aSDimitry Andric if (NodeId N = P.Obj.Addr->getReachingDef())
139e3b55780SDimitry Andric OS << Print(N, P.G);
140050e163aSDimitry Andric OS << "):";
141050e163aSDimitry Andric if (NodeId N = P.Obj.Addr->getSibling())
142e3b55780SDimitry Andric OS << Print(N, P.G);
143050e163aSDimitry Andric return OS;
144050e163aSDimitry Andric }
145050e163aSDimitry Andric
operator <<(raw_ostream & OS,const Print<PhiUse> & P)1467fa27ce4SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Print<PhiUse> &P) {
147050e163aSDimitry Andric printRefHeader(OS, P.Obj, P.G);
148050e163aSDimitry Andric OS << '(';
149050e163aSDimitry Andric if (NodeId N = P.Obj.Addr->getReachingDef())
150e3b55780SDimitry Andric OS << Print(N, P.G);
151050e163aSDimitry Andric OS << ',';
152050e163aSDimitry Andric if (NodeId N = P.Obj.Addr->getPredecessor())
153e3b55780SDimitry Andric OS << Print(N, P.G);
154050e163aSDimitry Andric OS << "):";
155050e163aSDimitry Andric if (NodeId N = P.Obj.Addr->getSibling())
156e3b55780SDimitry Andric OS << Print(N, P.G);
157050e163aSDimitry Andric return OS;
158050e163aSDimitry Andric }
159050e163aSDimitry Andric
operator <<(raw_ostream & OS,const Print<Ref> & P)1607fa27ce4SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Print<Ref> &P) {
161050e163aSDimitry Andric switch (P.Obj.Addr->getKind()) {
162050e163aSDimitry Andric case NodeAttrs::Def:
163050e163aSDimitry Andric OS << PrintNode<DefNode *>(P.Obj, P.G);
164050e163aSDimitry Andric break;
165050e163aSDimitry Andric case NodeAttrs::Use:
166050e163aSDimitry Andric if (P.Obj.Addr->getFlags() & NodeAttrs::PhiRef)
167050e163aSDimitry Andric OS << PrintNode<PhiUseNode *>(P.Obj, P.G);
168050e163aSDimitry Andric else
169050e163aSDimitry Andric OS << PrintNode<UseNode *>(P.Obj, P.G);
170050e163aSDimitry Andric break;
171050e163aSDimitry Andric }
172050e163aSDimitry Andric return OS;
173050e163aSDimitry Andric }
174050e163aSDimitry Andric
operator <<(raw_ostream & OS,const Print<NodeList> & P)175050e163aSDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Print<NodeList> &P) {
176050e163aSDimitry Andric unsigned N = P.Obj.size();
177050e163aSDimitry Andric for (auto I : P.Obj) {
178e3b55780SDimitry Andric OS << Print(I.Id, P.G);
179050e163aSDimitry Andric if (--N)
180050e163aSDimitry Andric OS << ' ';
181050e163aSDimitry Andric }
182050e163aSDimitry Andric return OS;
183050e163aSDimitry Andric }
184050e163aSDimitry Andric
operator <<(raw_ostream & OS,const Print<NodeSet> & P)185050e163aSDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Print<NodeSet> &P) {
186050e163aSDimitry Andric unsigned N = P.Obj.size();
187050e163aSDimitry Andric for (auto I : P.Obj) {
188e3b55780SDimitry Andric OS << Print(I, P.G);
189050e163aSDimitry Andric if (--N)
190050e163aSDimitry Andric OS << ' ';
191050e163aSDimitry Andric }
192050e163aSDimitry Andric return OS;
193050e163aSDimitry Andric }
194050e163aSDimitry Andric
195050e163aSDimitry Andric namespace {
196c82ad72fSDimitry Andric
1977fa27ce4SDimitry Andric template <typename T> struct PrintListV {
PrintListVllvm::rdf::__anon61d002970111::PrintListV198050e163aSDimitry Andric PrintListV(const NodeList &L, const DataFlowGraph &G) : List(L), G(G) {}
199c82ad72fSDimitry Andric
200044eb2f6SDimitry Andric using Type = T;
201050e163aSDimitry Andric const NodeList &List;
202050e163aSDimitry Andric const DataFlowGraph &G;
203050e163aSDimitry Andric };
204050e163aSDimitry Andric
205050e163aSDimitry Andric template <typename T>
operator <<(raw_ostream & OS,const PrintListV<T> & P)206050e163aSDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const PrintListV<T> &P) {
207050e163aSDimitry Andric unsigned N = P.List.size();
208050e163aSDimitry Andric for (NodeAddr<T> A : P.List) {
209050e163aSDimitry Andric OS << PrintNode<T>(A, P.G);
210050e163aSDimitry Andric if (--N)
211050e163aSDimitry Andric OS << ", ";
212050e163aSDimitry Andric }
213050e163aSDimitry Andric return OS;
214050e163aSDimitry Andric }
215c82ad72fSDimitry Andric
216c82ad72fSDimitry Andric } // end anonymous namespace
217050e163aSDimitry Andric
operator <<(raw_ostream & OS,const Print<Phi> & P)2187fa27ce4SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Print<Phi> &P) {
219e3b55780SDimitry Andric OS << Print(P.Obj.Id, P.G) << ": phi ["
220050e163aSDimitry Andric << PrintListV<RefNode *>(P.Obj.Addr->members(P.G), P.G) << ']';
221050e163aSDimitry Andric return OS;
222050e163aSDimitry Andric }
223050e163aSDimitry Andric
operator <<(raw_ostream & OS,const Print<Stmt> & P)2247fa27ce4SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Print<Stmt> &P) {
225b915e9e0SDimitry Andric const MachineInstr &MI = *P.Obj.Addr->getCode();
226b915e9e0SDimitry Andric unsigned Opc = MI.getOpcode();
227e3b55780SDimitry Andric OS << Print(P.Obj.Id, P.G) << ": " << P.G.getTII().getName(Opc);
228b915e9e0SDimitry Andric // Print the target for calls and branches (for readability).
229b915e9e0SDimitry Andric if (MI.isCall() || MI.isBranch()) {
230b915e9e0SDimitry Andric MachineInstr::const_mop_iterator T =
2317fa27ce4SDimitry Andric llvm::find_if(MI.operands(), [](const MachineOperand &Op) -> bool {
232b915e9e0SDimitry Andric return Op.isMBB() || Op.isGlobal() || Op.isSymbol();
233b915e9e0SDimitry Andric });
234b915e9e0SDimitry Andric if (T != MI.operands_end()) {
235b915e9e0SDimitry Andric OS << ' ';
236b915e9e0SDimitry Andric if (T->isMBB())
237044eb2f6SDimitry Andric OS << printMBBReference(*T->getMBB());
238b915e9e0SDimitry Andric else if (T->isGlobal())
239b915e9e0SDimitry Andric OS << T->getGlobal()->getName();
240b915e9e0SDimitry Andric else if (T->isSymbol())
241b915e9e0SDimitry Andric OS << T->getSymbolName();
242b915e9e0SDimitry Andric }
243b915e9e0SDimitry Andric }
244b915e9e0SDimitry Andric OS << " [" << PrintListV<RefNode *>(P.Obj.Addr->members(P.G), P.G) << ']';
245050e163aSDimitry Andric return OS;
246050e163aSDimitry Andric }
247050e163aSDimitry Andric
operator <<(raw_ostream & OS,const Print<Instr> & P)2487fa27ce4SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Print<Instr> &P) {
249050e163aSDimitry Andric switch (P.Obj.Addr->getKind()) {
250050e163aSDimitry Andric case NodeAttrs::Phi:
251050e163aSDimitry Andric OS << PrintNode<PhiNode *>(P.Obj, P.G);
252050e163aSDimitry Andric break;
253050e163aSDimitry Andric case NodeAttrs::Stmt:
254050e163aSDimitry Andric OS << PrintNode<StmtNode *>(P.Obj, P.G);
255050e163aSDimitry Andric break;
256050e163aSDimitry Andric default:
257e3b55780SDimitry Andric OS << "instr? " << Print(P.Obj.Id, P.G);
258050e163aSDimitry Andric break;
259050e163aSDimitry Andric }
260050e163aSDimitry Andric return OS;
261050e163aSDimitry Andric }
262050e163aSDimitry Andric
operator <<(raw_ostream & OS,const Print<Block> & P)2637fa27ce4SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Print<Block> &P) {
264b915e9e0SDimitry Andric MachineBasicBlock *BB = P.Obj.Addr->getCode();
265050e163aSDimitry Andric unsigned NP = BB->pred_size();
266050e163aSDimitry Andric std::vector<int> Ns;
267ac9a064cSDimitry Andric auto PrintBBs = [&OS](const std::vector<int> &Ns) -> void {
268050e163aSDimitry Andric unsigned N = Ns.size();
269b915e9e0SDimitry Andric for (int I : Ns) {
270044eb2f6SDimitry Andric OS << "%bb." << I;
271050e163aSDimitry Andric if (--N)
272050e163aSDimitry Andric OS << ", ";
273050e163aSDimitry Andric }
274050e163aSDimitry Andric };
275050e163aSDimitry Andric
276e3b55780SDimitry Andric OS << Print(P.Obj.Id, P.G) << ": --- " << printMBBReference(*BB)
277b915e9e0SDimitry Andric << " --- preds(" << NP << "): ";
278b915e9e0SDimitry Andric for (MachineBasicBlock *B : BB->predecessors())
279b915e9e0SDimitry Andric Ns.push_back(B->getNumber());
280050e163aSDimitry Andric PrintBBs(Ns);
281050e163aSDimitry Andric
282050e163aSDimitry Andric unsigned NS = BB->succ_size();
283050e163aSDimitry Andric OS << " succs(" << NS << "): ";
284050e163aSDimitry Andric Ns.clear();
285b915e9e0SDimitry Andric for (MachineBasicBlock *B : BB->successors())
286b915e9e0SDimitry Andric Ns.push_back(B->getNumber());
287050e163aSDimitry Andric PrintBBs(Ns);
288050e163aSDimitry Andric OS << '\n';
289050e163aSDimitry Andric
290050e163aSDimitry Andric for (auto I : P.Obj.Addr->members(P.G))
291050e163aSDimitry Andric OS << PrintNode<InstrNode *>(I, P.G) << '\n';
292050e163aSDimitry Andric return OS;
293050e163aSDimitry Andric }
294050e163aSDimitry Andric
operator <<(raw_ostream & OS,const Print<Func> & P)2957fa27ce4SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Print<Func> &P) {
2967fa27ce4SDimitry Andric OS << "DFG dump:[\n"
2977fa27ce4SDimitry Andric << Print(P.Obj.Id, P.G)
2987fa27ce4SDimitry Andric << ": Function: " << P.Obj.Addr->getCode()->getName() << '\n';
299050e163aSDimitry Andric for (auto I : P.Obj.Addr->members(P.G))
300050e163aSDimitry Andric OS << PrintNode<BlockNode *>(I, P.G) << '\n';
301050e163aSDimitry Andric OS << "]\n";
302050e163aSDimitry Andric return OS;
303050e163aSDimitry Andric }
304050e163aSDimitry Andric
operator <<(raw_ostream & OS,const Print<RegisterSet> & P)305050e163aSDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Print<RegisterSet> &P) {
306050e163aSDimitry Andric OS << '{';
307050e163aSDimitry Andric for (auto I : P.Obj)
308e3b55780SDimitry Andric OS << ' ' << Print(I, P.G);
309050e163aSDimitry Andric OS << " }";
310050e163aSDimitry Andric return OS;
311050e163aSDimitry Andric }
312050e163aSDimitry Andric
operator <<(raw_ostream & OS,const Print<RegisterAggr> & P)313b915e9e0SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Print<RegisterAggr> &P) {
3147fa27ce4SDimitry Andric OS << P.Obj;
315b915e9e0SDimitry Andric return OS;
316b915e9e0SDimitry Andric }
317b915e9e0SDimitry Andric
operator <<(raw_ostream & OS,const Print<DataFlowGraph::DefStack> & P)318050e163aSDimitry Andric raw_ostream &operator<<(raw_ostream &OS,
319050e163aSDimitry Andric const Print<DataFlowGraph::DefStack> &P) {
320050e163aSDimitry Andric for (auto I = P.Obj.top(), E = P.Obj.bottom(); I != E;) {
3217fa27ce4SDimitry Andric OS << Print(I->Id, P.G) << '<' << Print(I->Addr->getRegRef(P.G), P.G)
3227fa27ce4SDimitry Andric << '>';
323050e163aSDimitry Andric I.down();
324050e163aSDimitry Andric if (I != E)
325050e163aSDimitry Andric OS << ' ';
326050e163aSDimitry Andric }
327050e163aSDimitry Andric return OS;
328050e163aSDimitry Andric }
329050e163aSDimitry Andric
330050e163aSDimitry Andric // Node allocation functions.
331050e163aSDimitry Andric //
332050e163aSDimitry Andric // Node allocator is like a slab memory allocator: it allocates blocks of
333050e163aSDimitry Andric // memory in sizes that are multiples of the size of a node. Each block has
334050e163aSDimitry Andric // the same size. Nodes are allocated from the currently active block, and
335050e163aSDimitry Andric // when it becomes full, a new one is created.
336050e163aSDimitry Andric // There is a mapping scheme between node id and its location in a block,
337050e163aSDimitry Andric // and within that block is described in the header file.
338050e163aSDimitry Andric //
startNewBlock()339050e163aSDimitry Andric void NodeAllocator::startNewBlock() {
340050e163aSDimitry Andric void *T = MemPool.Allocate(NodesPerBlock * NodeMemSize, NodeMemSize);
341050e163aSDimitry Andric char *P = static_cast<char *>(T);
342050e163aSDimitry Andric Blocks.push_back(P);
343050e163aSDimitry Andric // Check if the block index is still within the allowed range, i.e. less
344050e163aSDimitry Andric // than 2^N, where N is the number of bits in NodeId for the block index.
345050e163aSDimitry Andric // BitsPerIndex is the number of bits per node index.
34601095a5dSDimitry Andric assert((Blocks.size() < ((size_t)1 << (8 * sizeof(NodeId) - BitsPerIndex))) &&
347050e163aSDimitry Andric "Out of bits for block index");
348050e163aSDimitry Andric ActiveEnd = P;
349050e163aSDimitry Andric }
350050e163aSDimitry Andric
needNewBlock()351050e163aSDimitry Andric bool NodeAllocator::needNewBlock() {
352050e163aSDimitry Andric if (Blocks.empty())
353050e163aSDimitry Andric return true;
354050e163aSDimitry Andric
355050e163aSDimitry Andric char *ActiveBegin = Blocks.back();
356050e163aSDimitry Andric uint32_t Index = (ActiveEnd - ActiveBegin) / NodeMemSize;
357050e163aSDimitry Andric return Index >= NodesPerBlock;
358050e163aSDimitry Andric }
359050e163aSDimitry Andric
New()3607fa27ce4SDimitry Andric Node NodeAllocator::New() {
361050e163aSDimitry Andric if (needNewBlock())
362050e163aSDimitry Andric startNewBlock();
363050e163aSDimitry Andric
364050e163aSDimitry Andric uint32_t ActiveB = Blocks.size() - 1;
365050e163aSDimitry Andric uint32_t Index = (ActiveEnd - Blocks[ActiveB]) / NodeMemSize;
3667fa27ce4SDimitry Andric Node NA = {reinterpret_cast<NodeBase *>(ActiveEnd), makeId(ActiveB, Index)};
367050e163aSDimitry Andric ActiveEnd += NodeMemSize;
368050e163aSDimitry Andric return NA;
369050e163aSDimitry Andric }
370050e163aSDimitry Andric
id(const NodeBase * P) const371050e163aSDimitry Andric NodeId NodeAllocator::id(const NodeBase *P) const {
372050e163aSDimitry Andric uintptr_t A = reinterpret_cast<uintptr_t>(P);
373050e163aSDimitry Andric for (unsigned i = 0, n = Blocks.size(); i != n; ++i) {
374050e163aSDimitry Andric uintptr_t B = reinterpret_cast<uintptr_t>(Blocks[i]);
375050e163aSDimitry Andric if (A < B || A >= B + NodesPerBlock * NodeMemSize)
376050e163aSDimitry Andric continue;
377050e163aSDimitry Andric uint32_t Idx = (A - B) / NodeMemSize;
378050e163aSDimitry Andric return makeId(i, Idx);
379050e163aSDimitry Andric }
380050e163aSDimitry Andric llvm_unreachable("Invalid node address");
381050e163aSDimitry Andric }
382050e163aSDimitry Andric
clear()383050e163aSDimitry Andric void NodeAllocator::clear() {
384050e163aSDimitry Andric MemPool.Reset();
385050e163aSDimitry Andric Blocks.clear();
386050e163aSDimitry Andric ActiveEnd = nullptr;
387050e163aSDimitry Andric }
388050e163aSDimitry Andric
389050e163aSDimitry Andric // Insert node NA after "this" in the circular chain.
append(Node NA)3907fa27ce4SDimitry Andric void NodeBase::append(Node NA) {
391050e163aSDimitry Andric NodeId Nx = Next;
392050e163aSDimitry Andric // If NA is already "next", do nothing.
393050e163aSDimitry Andric if (Next != NA.Id) {
394050e163aSDimitry Andric Next = NA.Id;
395050e163aSDimitry Andric NA.Addr->Next = Nx;
396050e163aSDimitry Andric }
397050e163aSDimitry Andric }
398050e163aSDimitry Andric
399050e163aSDimitry Andric // Fundamental node manipulator functions.
400050e163aSDimitry Andric
401050e163aSDimitry Andric // Obtain the register reference from a reference node.
getRegRef(const DataFlowGraph & G) const402b915e9e0SDimitry Andric RegisterRef RefNode::getRegRef(const DataFlowGraph &G) const {
403050e163aSDimitry Andric assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
404050e163aSDimitry Andric if (NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef)
4057fa27ce4SDimitry Andric return G.unpack(RefData.PR);
4067fa27ce4SDimitry Andric assert(RefData.Op != nullptr);
4077fa27ce4SDimitry Andric return G.makeRegRef(*RefData.Op);
408050e163aSDimitry Andric }
409050e163aSDimitry Andric
410050e163aSDimitry Andric // Set the register reference in the reference node directly (for references
411050e163aSDimitry Andric // in phi nodes).
setRegRef(RegisterRef RR,DataFlowGraph & G)412b915e9e0SDimitry Andric void RefNode::setRegRef(RegisterRef RR, DataFlowGraph &G) {
413050e163aSDimitry Andric assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
414050e163aSDimitry Andric assert(NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef);
4157fa27ce4SDimitry Andric RefData.PR = G.pack(RR);
416050e163aSDimitry Andric }
417050e163aSDimitry Andric
418050e163aSDimitry Andric // Set the register reference in the reference node based on a machine
419050e163aSDimitry Andric // operand (for references in statement nodes).
setRegRef(MachineOperand * Op,DataFlowGraph & G)420b915e9e0SDimitry Andric void RefNode::setRegRef(MachineOperand *Op, DataFlowGraph &G) {
421050e163aSDimitry Andric assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
422050e163aSDimitry Andric assert(!(NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef));
423b915e9e0SDimitry Andric (void)G;
4247fa27ce4SDimitry Andric RefData.Op = Op;
425050e163aSDimitry Andric }
426050e163aSDimitry Andric
427050e163aSDimitry Andric // Get the owner of a given reference node.
getOwner(const DataFlowGraph & G)4287fa27ce4SDimitry Andric Node RefNode::getOwner(const DataFlowGraph &G) {
4297fa27ce4SDimitry Andric Node NA = G.addr<NodeBase *>(getNext());
430050e163aSDimitry Andric
431050e163aSDimitry Andric while (NA.Addr != this) {
432050e163aSDimitry Andric if (NA.Addr->getType() == NodeAttrs::Code)
433050e163aSDimitry Andric return NA;
434050e163aSDimitry Andric NA = G.addr<NodeBase *>(NA.Addr->getNext());
435050e163aSDimitry Andric }
436050e163aSDimitry Andric llvm_unreachable("No owner in circular list");
437050e163aSDimitry Andric }
438050e163aSDimitry Andric
439050e163aSDimitry Andric // Connect the def node to the reaching def node.
linkToDef(NodeId Self,Def DA)4407fa27ce4SDimitry Andric void DefNode::linkToDef(NodeId Self, Def DA) {
4417fa27ce4SDimitry Andric RefData.RD = DA.Id;
4427fa27ce4SDimitry Andric RefData.Sib = DA.Addr->getReachedDef();
443050e163aSDimitry Andric DA.Addr->setReachedDef(Self);
444050e163aSDimitry Andric }
445050e163aSDimitry Andric
446050e163aSDimitry Andric // Connect the use node to the reaching def node.
linkToDef(NodeId Self,Def DA)4477fa27ce4SDimitry Andric void UseNode::linkToDef(NodeId Self, Def DA) {
4487fa27ce4SDimitry Andric RefData.RD = DA.Id;
4497fa27ce4SDimitry Andric RefData.Sib = DA.Addr->getReachedUse();
450050e163aSDimitry Andric DA.Addr->setReachedUse(Self);
451050e163aSDimitry Andric }
452050e163aSDimitry Andric
453050e163aSDimitry Andric // Get the first member of the code node.
getFirstMember(const DataFlowGraph & G) const4547fa27ce4SDimitry Andric Node CodeNode::getFirstMember(const DataFlowGraph &G) const {
4557fa27ce4SDimitry Andric if (CodeData.FirstM == 0)
4567fa27ce4SDimitry Andric return Node();
4577fa27ce4SDimitry Andric return G.addr<NodeBase *>(CodeData.FirstM);
458050e163aSDimitry Andric }
459050e163aSDimitry Andric
460050e163aSDimitry Andric // Get the last member of the code node.
getLastMember(const DataFlowGraph & G) const4617fa27ce4SDimitry Andric Node CodeNode::getLastMember(const DataFlowGraph &G) const {
4627fa27ce4SDimitry Andric if (CodeData.LastM == 0)
4637fa27ce4SDimitry Andric return Node();
4647fa27ce4SDimitry Andric return G.addr<NodeBase *>(CodeData.LastM);
465050e163aSDimitry Andric }
466050e163aSDimitry Andric
467050e163aSDimitry Andric // Add node NA at the end of the member list of the given code node.
addMember(Node NA,const DataFlowGraph & G)4687fa27ce4SDimitry Andric void CodeNode::addMember(Node NA, const DataFlowGraph &G) {
4697fa27ce4SDimitry Andric Node ML = getLastMember(G);
470050e163aSDimitry Andric if (ML.Id != 0) {
471050e163aSDimitry Andric ML.Addr->append(NA);
472050e163aSDimitry Andric } else {
4737fa27ce4SDimitry Andric CodeData.FirstM = NA.Id;
474050e163aSDimitry Andric NodeId Self = G.id(this);
475050e163aSDimitry Andric NA.Addr->setNext(Self);
476050e163aSDimitry Andric }
4777fa27ce4SDimitry Andric CodeData.LastM = NA.Id;
478050e163aSDimitry Andric }
479050e163aSDimitry Andric
480050e163aSDimitry Andric // Add node NA after member node MA in the given code node.
addMemberAfter(Node MA,Node NA,const DataFlowGraph & G)4817fa27ce4SDimitry Andric void CodeNode::addMemberAfter(Node MA, Node NA, const DataFlowGraph &G) {
482050e163aSDimitry Andric MA.Addr->append(NA);
4837fa27ce4SDimitry Andric if (CodeData.LastM == MA.Id)
4847fa27ce4SDimitry Andric CodeData.LastM = NA.Id;
485050e163aSDimitry Andric }
486050e163aSDimitry Andric
487050e163aSDimitry Andric // Remove member node NA from the given code node.
removeMember(Node NA,const DataFlowGraph & G)4887fa27ce4SDimitry Andric void CodeNode::removeMember(Node NA, const DataFlowGraph &G) {
4897fa27ce4SDimitry Andric Node MA = getFirstMember(G);
490050e163aSDimitry Andric assert(MA.Id != 0);
491050e163aSDimitry Andric
492050e163aSDimitry Andric // Special handling if the member to remove is the first member.
493050e163aSDimitry Andric if (MA.Id == NA.Id) {
4947fa27ce4SDimitry Andric if (CodeData.LastM == MA.Id) {
495050e163aSDimitry Andric // If it is the only member, set both first and last to 0.
4967fa27ce4SDimitry Andric CodeData.FirstM = CodeData.LastM = 0;
497050e163aSDimitry Andric } else {
498050e163aSDimitry Andric // Otherwise, advance the first member.
4997fa27ce4SDimitry Andric CodeData.FirstM = MA.Addr->getNext();
500050e163aSDimitry Andric }
501050e163aSDimitry Andric return;
502050e163aSDimitry Andric }
503050e163aSDimitry Andric
504050e163aSDimitry Andric while (MA.Addr != this) {
505050e163aSDimitry Andric NodeId MX = MA.Addr->getNext();
506050e163aSDimitry Andric if (MX == NA.Id) {
507050e163aSDimitry Andric MA.Addr->setNext(NA.Addr->getNext());
508050e163aSDimitry Andric // If the member to remove happens to be the last one, update the
509050e163aSDimitry Andric // LastM indicator.
5107fa27ce4SDimitry Andric if (CodeData.LastM == NA.Id)
5117fa27ce4SDimitry Andric CodeData.LastM = MA.Id;
512050e163aSDimitry Andric return;
513050e163aSDimitry Andric }
514050e163aSDimitry Andric MA = G.addr<NodeBase *>(MX);
515050e163aSDimitry Andric }
516050e163aSDimitry Andric llvm_unreachable("No such member");
517050e163aSDimitry Andric }
518050e163aSDimitry Andric
519050e163aSDimitry Andric // Return the list of all members of the code node.
members(const DataFlowGraph & G) const520050e163aSDimitry Andric NodeList CodeNode::members(const DataFlowGraph &G) const {
5217fa27ce4SDimitry Andric static auto True = [](Node) -> bool { return true; };
522050e163aSDimitry Andric return members_if(True, G);
523050e163aSDimitry Andric }
524050e163aSDimitry Andric
525050e163aSDimitry Andric // Return the owner of the given instr node.
getOwner(const DataFlowGraph & G)5267fa27ce4SDimitry Andric Node InstrNode::getOwner(const DataFlowGraph &G) {
5277fa27ce4SDimitry Andric Node NA = G.addr<NodeBase *>(getNext());
528050e163aSDimitry Andric
529050e163aSDimitry Andric while (NA.Addr != this) {
530050e163aSDimitry Andric assert(NA.Addr->getType() == NodeAttrs::Code);
531050e163aSDimitry Andric if (NA.Addr->getKind() == NodeAttrs::Block)
532050e163aSDimitry Andric return NA;
533050e163aSDimitry Andric NA = G.addr<NodeBase *>(NA.Addr->getNext());
534050e163aSDimitry Andric }
535050e163aSDimitry Andric llvm_unreachable("No owner in circular list");
536050e163aSDimitry Andric }
537050e163aSDimitry Andric
538050e163aSDimitry Andric // Add the phi node PA to the given block node.
addPhi(Phi PA,const DataFlowGraph & G)5397fa27ce4SDimitry Andric void BlockNode::addPhi(Phi PA, const DataFlowGraph &G) {
5407fa27ce4SDimitry Andric Node M = getFirstMember(G);
541050e163aSDimitry Andric if (M.Id == 0) {
542050e163aSDimitry Andric addMember(PA, G);
543050e163aSDimitry Andric return;
544050e163aSDimitry Andric }
545050e163aSDimitry Andric
546050e163aSDimitry Andric assert(M.Addr->getType() == NodeAttrs::Code);
547050e163aSDimitry Andric if (M.Addr->getKind() == NodeAttrs::Stmt) {
548050e163aSDimitry Andric // If the first member of the block is a statement, insert the phi as
549050e163aSDimitry Andric // the first member.
5507fa27ce4SDimitry Andric CodeData.FirstM = PA.Id;
551050e163aSDimitry Andric PA.Addr->setNext(M.Id);
552050e163aSDimitry Andric } else {
553050e163aSDimitry Andric // If the first member is a phi, find the last phi, and append PA to it.
554050e163aSDimitry Andric assert(M.Addr->getKind() == NodeAttrs::Phi);
5557fa27ce4SDimitry Andric Node MN = M;
556050e163aSDimitry Andric do {
557050e163aSDimitry Andric M = MN;
558050e163aSDimitry Andric MN = G.addr<NodeBase *>(M.Addr->getNext());
559050e163aSDimitry Andric assert(MN.Addr->getType() == NodeAttrs::Code);
560050e163aSDimitry Andric } while (MN.Addr->getKind() == NodeAttrs::Phi);
561050e163aSDimitry Andric
562050e163aSDimitry Andric // M is the last phi.
563050e163aSDimitry Andric addMemberAfter(M, PA, G);
564050e163aSDimitry Andric }
565050e163aSDimitry Andric }
566050e163aSDimitry Andric
567050e163aSDimitry Andric // Find the block node corresponding to the machine basic block BB in the
568050e163aSDimitry Andric // given func node.
findBlock(const MachineBasicBlock * BB,const DataFlowGraph & G) const5697fa27ce4SDimitry Andric Block FuncNode::findBlock(const MachineBasicBlock *BB,
570050e163aSDimitry Andric const DataFlowGraph &G) const {
5717fa27ce4SDimitry Andric auto EqBB = [BB](Node NA) -> bool { return Block(NA).Addr->getCode() == BB; };
572050e163aSDimitry Andric NodeList Ms = members_if(EqBB, G);
573050e163aSDimitry Andric if (!Ms.empty())
574050e163aSDimitry Andric return Ms[0];
5757fa27ce4SDimitry Andric return Block();
576050e163aSDimitry Andric }
577050e163aSDimitry Andric
578050e163aSDimitry Andric // Get the block node for the entry block in the given function.
getEntryBlock(const DataFlowGraph & G)5797fa27ce4SDimitry Andric Block FuncNode::getEntryBlock(const DataFlowGraph &G) {
580050e163aSDimitry Andric MachineBasicBlock *EntryB = &getCode()->front();
581050e163aSDimitry Andric return findBlock(EntryB, G);
582050e163aSDimitry Andric }
583050e163aSDimitry Andric
584050e163aSDimitry Andric // Target operand information.
585050e163aSDimitry Andric //
586050e163aSDimitry Andric
587050e163aSDimitry Andric // For a given instruction, check if there are any bits of RR that can remain
588050e163aSDimitry Andric // unchanged across this def.
isPreserving(const MachineInstr & In,unsigned OpNum) const5897fa27ce4SDimitry Andric bool TargetOperandInfo::isPreserving(const MachineInstr &In,
5907fa27ce4SDimitry Andric unsigned OpNum) const {
59101095a5dSDimitry Andric return TII.isPredicated(In);
592050e163aSDimitry Andric }
593050e163aSDimitry Andric
594050e163aSDimitry Andric // Check if the definition of RR produces an unspecified value.
isClobbering(const MachineInstr & In,unsigned OpNum) const5957fa27ce4SDimitry Andric bool TargetOperandInfo::isClobbering(const MachineInstr &In,
5967fa27ce4SDimitry Andric unsigned OpNum) const {
59771d5a254SDimitry Andric const MachineOperand &Op = In.getOperand(OpNum);
59871d5a254SDimitry Andric if (Op.isRegMask())
59971d5a254SDimitry Andric return true;
60071d5a254SDimitry Andric assert(Op.isReg());
601050e163aSDimitry Andric if (In.isCall())
60271d5a254SDimitry Andric if (Op.isDef() && Op.isDead())
603050e163aSDimitry Andric return true;
604050e163aSDimitry Andric return false;
605050e163aSDimitry Andric }
606050e163aSDimitry Andric
607050e163aSDimitry Andric // Check if the given instruction specifically requires
isFixedReg(const MachineInstr & In,unsigned OpNum) const6087fa27ce4SDimitry Andric bool TargetOperandInfo::isFixedReg(const MachineInstr &In,
6097fa27ce4SDimitry Andric unsigned OpNum) const {
61001095a5dSDimitry Andric if (In.isCall() || In.isReturn() || In.isInlineAsm())
611050e163aSDimitry Andric return true;
61201095a5dSDimitry Andric // Check for a tail call.
61301095a5dSDimitry Andric if (In.isBranch())
614b915e9e0SDimitry Andric for (const MachineOperand &O : In.operands())
61501095a5dSDimitry Andric if (O.isGlobal() || O.isSymbol())
61601095a5dSDimitry Andric return true;
61701095a5dSDimitry Andric
618050e163aSDimitry Andric const MCInstrDesc &D = In.getDesc();
619e3b55780SDimitry Andric if (D.implicit_defs().empty() && D.implicit_uses().empty())
620050e163aSDimitry Andric return false;
621050e163aSDimitry Andric const MachineOperand &Op = In.getOperand(OpNum);
622050e163aSDimitry Andric // If there is a sub-register, treat the operand as non-fixed. Currently,
623050e163aSDimitry Andric // fixed registers are those that are listed in the descriptor as implicit
624050e163aSDimitry Andric // uses or defs, and those lists do not allow sub-registers.
625050e163aSDimitry Andric if (Op.getSubReg() != 0)
626050e163aSDimitry Andric return false;
6271d5ae102SDimitry Andric Register Reg = Op.getReg();
628e3b55780SDimitry Andric ArrayRef<MCPhysReg> ImpOps =
629e3b55780SDimitry Andric Op.isDef() ? D.implicit_defs() : D.implicit_uses();
630e3b55780SDimitry Andric return is_contained(ImpOps, Reg);
631050e163aSDimitry Andric }
632050e163aSDimitry Andric
633050e163aSDimitry Andric //
634050e163aSDimitry Andric // The data flow graph construction.
635050e163aSDimitry Andric //
636050e163aSDimitry Andric
DataFlowGraph(MachineFunction & mf,const TargetInstrInfo & tii,const TargetRegisterInfo & tri,const MachineDominatorTree & mdt,const MachineDominanceFrontier & mdf)637050e163aSDimitry Andric DataFlowGraph::DataFlowGraph(MachineFunction &mf, const TargetInstrInfo &tii,
6387fa27ce4SDimitry Andric const TargetRegisterInfo &tri,
6397fa27ce4SDimitry Andric const MachineDominatorTree &mdt,
640e3b55780SDimitry Andric const MachineDominanceFrontier &mdf)
641e3b55780SDimitry Andric : DefaultTOI(std::make_unique<TargetOperandInfo>(tii)), MF(mf), TII(tii),
642e3b55780SDimitry Andric TRI(tri), PRI(tri, mf), MDT(mdt), MDF(mdf), TOI(*DefaultTOI),
6437fa27ce4SDimitry Andric LiveIns(PRI) {}
644e3b55780SDimitry Andric
DataFlowGraph(MachineFunction & mf,const TargetInstrInfo & tii,const TargetRegisterInfo & tri,const MachineDominatorTree & mdt,const MachineDominanceFrontier & mdf,const TargetOperandInfo & toi)645e3b55780SDimitry Andric DataFlowGraph::DataFlowGraph(MachineFunction &mf, const TargetInstrInfo &tii,
6467fa27ce4SDimitry Andric const TargetRegisterInfo &tri,
6477fa27ce4SDimitry Andric const MachineDominatorTree &mdt,
6487fa27ce4SDimitry Andric const MachineDominanceFrontier &mdf,
6497fa27ce4SDimitry Andric const TargetOperandInfo &toi)
65071d5a254SDimitry Andric : MF(mf), TII(tii), TRI(tri), PRI(tri, mf), MDT(mdt), MDF(mdf), TOI(toi),
6517fa27ce4SDimitry Andric LiveIns(PRI) {}
652050e163aSDimitry Andric
653050e163aSDimitry Andric // The implementation of the definition stack.
654050e163aSDimitry Andric // Each register reference has its own definition stack. In particular,
655050e163aSDimitry Andric // for a register references "Reg" and "Reg:subreg" will each have their
656050e163aSDimitry Andric // own definition stacks.
657050e163aSDimitry Andric
658050e163aSDimitry Andric // Construct a stack iterator.
Iterator(const DataFlowGraph::DefStack & S,bool Top)659050e163aSDimitry Andric DataFlowGraph::DefStack::Iterator::Iterator(const DataFlowGraph::DefStack &S,
6607fa27ce4SDimitry Andric bool Top)
6617fa27ce4SDimitry Andric : DS(S) {
662050e163aSDimitry Andric if (!Top) {
663050e163aSDimitry Andric // Initialize to bottom.
664050e163aSDimitry Andric Pos = 0;
665050e163aSDimitry Andric return;
666050e163aSDimitry Andric }
667050e163aSDimitry Andric // Initialize to the top, i.e. top-most non-delimiter (or 0, if empty).
668050e163aSDimitry Andric Pos = DS.Stack.size();
669050e163aSDimitry Andric while (Pos > 0 && DS.isDelimiter(DS.Stack[Pos - 1]))
670050e163aSDimitry Andric Pos--;
671050e163aSDimitry Andric }
672050e163aSDimitry Andric
673050e163aSDimitry Andric // Return the size of the stack, including block delimiters.
size() const674050e163aSDimitry Andric unsigned DataFlowGraph::DefStack::size() const {
675050e163aSDimitry Andric unsigned S = 0;
676050e163aSDimitry Andric for (auto I = top(), E = bottom(); I != E; I.down())
677050e163aSDimitry Andric S++;
678050e163aSDimitry Andric return S;
679050e163aSDimitry Andric }
680050e163aSDimitry Andric
681050e163aSDimitry Andric // Remove the top entry from the stack. Remove all intervening delimiters
682050e163aSDimitry Andric // so that after this, the stack is either empty, or the top of the stack
683050e163aSDimitry Andric // is a non-delimiter.
pop()684050e163aSDimitry Andric void DataFlowGraph::DefStack::pop() {
685050e163aSDimitry Andric assert(!empty());
686050e163aSDimitry Andric unsigned P = nextDown(Stack.size());
687050e163aSDimitry Andric Stack.resize(P);
688050e163aSDimitry Andric }
689050e163aSDimitry Andric
690050e163aSDimitry Andric // Push a delimiter for block node N on the stack.
start_block(NodeId N)691050e163aSDimitry Andric void DataFlowGraph::DefStack::start_block(NodeId N) {
692050e163aSDimitry Andric assert(N != 0);
6937fa27ce4SDimitry Andric Stack.push_back(Def(nullptr, N));
694050e163aSDimitry Andric }
695050e163aSDimitry Andric
696050e163aSDimitry Andric // Remove all nodes from the top of the stack, until the delimited for
697050e163aSDimitry Andric // block node N is encountered. Remove the delimiter as well. In effect,
698050e163aSDimitry Andric // this will remove from the stack all definitions from block N.
clear_block(NodeId N)699050e163aSDimitry Andric void DataFlowGraph::DefStack::clear_block(NodeId N) {
700050e163aSDimitry Andric assert(N != 0);
701050e163aSDimitry Andric unsigned P = Stack.size();
702050e163aSDimitry Andric while (P > 0) {
703050e163aSDimitry Andric bool Found = isDelimiter(Stack[P - 1], N);
704050e163aSDimitry Andric P--;
705050e163aSDimitry Andric if (Found)
706050e163aSDimitry Andric break;
707050e163aSDimitry Andric }
708050e163aSDimitry Andric // This will also remove the delimiter, if found.
709050e163aSDimitry Andric Stack.resize(P);
710050e163aSDimitry Andric }
711050e163aSDimitry Andric
712050e163aSDimitry Andric // Move the stack iterator up by one.
nextUp(unsigned P) const713050e163aSDimitry Andric unsigned DataFlowGraph::DefStack::nextUp(unsigned P) const {
714050e163aSDimitry Andric // Get the next valid position after P (skipping all delimiters).
715050e163aSDimitry Andric // The input position P does not have to point to a non-delimiter.
716050e163aSDimitry Andric unsigned SS = Stack.size();
717050e163aSDimitry Andric bool IsDelim;
718050e163aSDimitry Andric assert(P < SS);
719050e163aSDimitry Andric do {
720050e163aSDimitry Andric P++;
721050e163aSDimitry Andric IsDelim = isDelimiter(Stack[P - 1]);
722050e163aSDimitry Andric } while (P < SS && IsDelim);
723050e163aSDimitry Andric assert(!IsDelim);
724050e163aSDimitry Andric return P;
725050e163aSDimitry Andric }
726050e163aSDimitry Andric
727050e163aSDimitry Andric // Move the stack iterator down by one.
nextDown(unsigned P) const728050e163aSDimitry Andric unsigned DataFlowGraph::DefStack::nextDown(unsigned P) const {
729050e163aSDimitry Andric // Get the preceding valid position before P (skipping all delimiters).
730050e163aSDimitry Andric // The input position P does not have to point to a non-delimiter.
731050e163aSDimitry Andric assert(P > 0 && P <= Stack.size());
732050e163aSDimitry Andric bool IsDelim = isDelimiter(Stack[P - 1]);
733050e163aSDimitry Andric do {
734050e163aSDimitry Andric if (--P == 0)
735050e163aSDimitry Andric break;
736050e163aSDimitry Andric IsDelim = isDelimiter(Stack[P - 1]);
737050e163aSDimitry Andric } while (P > 0 && IsDelim);
738050e163aSDimitry Andric assert(!IsDelim);
739050e163aSDimitry Andric return P;
740050e163aSDimitry Andric }
741050e163aSDimitry Andric
742b915e9e0SDimitry Andric // Register information.
743b915e9e0SDimitry Andric
getLandingPadLiveIns() const7447fa27ce4SDimitry Andric RegisterAggr DataFlowGraph::getLandingPadLiveIns() const {
7457fa27ce4SDimitry Andric RegisterAggr LR(getPRI());
746044eb2f6SDimitry Andric const Function &F = MF.getFunction();
7477fa27ce4SDimitry Andric const Constant *PF = F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr;
748b915e9e0SDimitry Andric const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering();
749b915e9e0SDimitry Andric if (RegisterId R = TLI.getExceptionPointerRegister(PF))
750b915e9e0SDimitry Andric LR.insert(RegisterRef(R));
751cfca06d7SDimitry Andric if (!isFuncletEHPersonality(classifyEHPersonality(PF))) {
752b915e9e0SDimitry Andric if (RegisterId R = TLI.getExceptionSelectorRegister(PF))
753b915e9e0SDimitry Andric LR.insert(RegisterRef(R));
754cfca06d7SDimitry Andric }
755b915e9e0SDimitry Andric return LR;
756b915e9e0SDimitry Andric }
757b915e9e0SDimitry Andric
758050e163aSDimitry Andric // Node management functions.
759050e163aSDimitry Andric
760050e163aSDimitry Andric // Get the pointer to the node with the id N.
ptr(NodeId N) const761050e163aSDimitry Andric NodeBase *DataFlowGraph::ptr(NodeId N) const {
762050e163aSDimitry Andric if (N == 0)
763050e163aSDimitry Andric return nullptr;
764050e163aSDimitry Andric return Memory.ptr(N);
765050e163aSDimitry Andric }
766050e163aSDimitry Andric
767050e163aSDimitry Andric // Get the id of the node at the address P.
id(const NodeBase * P) const768050e163aSDimitry Andric NodeId DataFlowGraph::id(const NodeBase *P) const {
769050e163aSDimitry Andric if (P == nullptr)
770050e163aSDimitry Andric return 0;
771050e163aSDimitry Andric return Memory.id(P);
772050e163aSDimitry Andric }
773050e163aSDimitry Andric
774050e163aSDimitry Andric // Allocate a new node and set the attributes to Attrs.
newNode(uint16_t Attrs)7757fa27ce4SDimitry Andric Node DataFlowGraph::newNode(uint16_t Attrs) {
7767fa27ce4SDimitry Andric Node P = Memory.New();
777050e163aSDimitry Andric P.Addr->init();
778050e163aSDimitry Andric P.Addr->setAttrs(Attrs);
779050e163aSDimitry Andric return P;
780050e163aSDimitry Andric }
781050e163aSDimitry Andric
782050e163aSDimitry Andric // Make a copy of the given node B, except for the data-flow links, which
783050e163aSDimitry Andric // are set to 0.
cloneNode(const Node B)7847fa27ce4SDimitry Andric Node DataFlowGraph::cloneNode(const Node B) {
7857fa27ce4SDimitry Andric Node NA = newNode(0);
786050e163aSDimitry Andric memcpy(NA.Addr, B.Addr, sizeof(NodeBase));
787050e163aSDimitry Andric // Ref nodes need to have the data-flow links reset.
788050e163aSDimitry Andric if (NA.Addr->getType() == NodeAttrs::Ref) {
7897fa27ce4SDimitry Andric Ref RA = NA;
790050e163aSDimitry Andric RA.Addr->setReachingDef(0);
791050e163aSDimitry Andric RA.Addr->setSibling(0);
792050e163aSDimitry Andric if (NA.Addr->getKind() == NodeAttrs::Def) {
7937fa27ce4SDimitry Andric Def DA = NA;
794050e163aSDimitry Andric DA.Addr->setReachedDef(0);
795050e163aSDimitry Andric DA.Addr->setReachedUse(0);
796050e163aSDimitry Andric }
797050e163aSDimitry Andric }
798050e163aSDimitry Andric return NA;
799050e163aSDimitry Andric }
800050e163aSDimitry Andric
801050e163aSDimitry Andric // Allocation routines for specific node types/kinds.
802050e163aSDimitry Andric
newUse(Instr Owner,MachineOperand & Op,uint16_t Flags)8037fa27ce4SDimitry Andric Use DataFlowGraph::newUse(Instr Owner, MachineOperand &Op, uint16_t Flags) {
8047fa27ce4SDimitry Andric Use UA = newNode(NodeAttrs::Ref | NodeAttrs::Use | Flags);
805b915e9e0SDimitry Andric UA.Addr->setRegRef(&Op, *this);
806050e163aSDimitry Andric return UA;
807050e163aSDimitry Andric }
808050e163aSDimitry Andric
newPhiUse(Phi Owner,RegisterRef RR,Block PredB,uint16_t Flags)8097fa27ce4SDimitry Andric PhiUse DataFlowGraph::newPhiUse(Phi Owner, RegisterRef RR, Block PredB,
8107fa27ce4SDimitry Andric uint16_t Flags) {
8117fa27ce4SDimitry Andric PhiUse PUA = newNode(NodeAttrs::Ref | NodeAttrs::Use | Flags);
812050e163aSDimitry Andric assert(Flags & NodeAttrs::PhiRef);
813b915e9e0SDimitry Andric PUA.Addr->setRegRef(RR, *this);
814050e163aSDimitry Andric PUA.Addr->setPredecessor(PredB.Id);
815050e163aSDimitry Andric return PUA;
816050e163aSDimitry Andric }
817050e163aSDimitry Andric
newDef(Instr Owner,MachineOperand & Op,uint16_t Flags)8187fa27ce4SDimitry Andric Def DataFlowGraph::newDef(Instr Owner, MachineOperand &Op, uint16_t Flags) {
8197fa27ce4SDimitry Andric Def DA = newNode(NodeAttrs::Ref | NodeAttrs::Def | Flags);
820b915e9e0SDimitry Andric DA.Addr->setRegRef(&Op, *this);
821050e163aSDimitry Andric return DA;
822050e163aSDimitry Andric }
823050e163aSDimitry Andric
newDef(Instr Owner,RegisterRef RR,uint16_t Flags)8247fa27ce4SDimitry Andric Def DataFlowGraph::newDef(Instr Owner, RegisterRef RR, uint16_t Flags) {
8257fa27ce4SDimitry Andric Def DA = newNode(NodeAttrs::Ref | NodeAttrs::Def | Flags);
826050e163aSDimitry Andric assert(Flags & NodeAttrs::PhiRef);
827b915e9e0SDimitry Andric DA.Addr->setRegRef(RR, *this);
828050e163aSDimitry Andric return DA;
829050e163aSDimitry Andric }
830050e163aSDimitry Andric
newPhi(Block Owner)8317fa27ce4SDimitry Andric Phi DataFlowGraph::newPhi(Block Owner) {
8327fa27ce4SDimitry Andric Phi PA = newNode(NodeAttrs::Code | NodeAttrs::Phi);
833050e163aSDimitry Andric Owner.Addr->addPhi(PA, *this);
834050e163aSDimitry Andric return PA;
835050e163aSDimitry Andric }
836050e163aSDimitry Andric
newStmt(Block Owner,MachineInstr * MI)8377fa27ce4SDimitry Andric Stmt DataFlowGraph::newStmt(Block Owner, MachineInstr *MI) {
8387fa27ce4SDimitry Andric Stmt SA = newNode(NodeAttrs::Code | NodeAttrs::Stmt);
839050e163aSDimitry Andric SA.Addr->setCode(MI);
840050e163aSDimitry Andric Owner.Addr->addMember(SA, *this);
841050e163aSDimitry Andric return SA;
842050e163aSDimitry Andric }
843050e163aSDimitry Andric
newBlock(Func Owner,MachineBasicBlock * BB)8447fa27ce4SDimitry Andric Block DataFlowGraph::newBlock(Func Owner, MachineBasicBlock *BB) {
8457fa27ce4SDimitry Andric Block BA = newNode(NodeAttrs::Code | NodeAttrs::Block);
846050e163aSDimitry Andric BA.Addr->setCode(BB);
847050e163aSDimitry Andric Owner.Addr->addMember(BA, *this);
848050e163aSDimitry Andric return BA;
849050e163aSDimitry Andric }
850050e163aSDimitry Andric
newFunc(MachineFunction * MF)8517fa27ce4SDimitry Andric Func DataFlowGraph::newFunc(MachineFunction *MF) {
8527fa27ce4SDimitry Andric Func FA = newNode(NodeAttrs::Code | NodeAttrs::Func);
853050e163aSDimitry Andric FA.Addr->setCode(MF);
854050e163aSDimitry Andric return FA;
855050e163aSDimitry Andric }
856050e163aSDimitry Andric
857050e163aSDimitry Andric // Build the data flow graph.
build(const Config & config)8587fa27ce4SDimitry Andric void DataFlowGraph::build(const Config &config) {
859050e163aSDimitry Andric reset();
8607fa27ce4SDimitry Andric BuildCfg = config;
8617fa27ce4SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo();
8627fa27ce4SDimitry Andric ReservedRegs = MRI.getReservedRegs();
8637fa27ce4SDimitry Andric bool SkipReserved = BuildCfg.Options & BuildOptions::OmitReserved;
8647fa27ce4SDimitry Andric
8657fa27ce4SDimitry Andric auto Insert = [](auto &Set, auto &&Range) {
8667fa27ce4SDimitry Andric Set.insert(Range.begin(), Range.end());
8677fa27ce4SDimitry Andric };
8687fa27ce4SDimitry Andric
8697fa27ce4SDimitry Andric if (BuildCfg.TrackRegs.empty()) {
8707fa27ce4SDimitry Andric std::set<RegisterId> BaseSet;
8717fa27ce4SDimitry Andric if (BuildCfg.Classes.empty()) {
8727fa27ce4SDimitry Andric // Insert every register.
873ac9a064cSDimitry Andric for (unsigned R = 1, E = getPRI().getTRI().getNumRegs(); R != E; ++R)
8747fa27ce4SDimitry Andric BaseSet.insert(R);
8757fa27ce4SDimitry Andric } else {
8767fa27ce4SDimitry Andric for (const TargetRegisterClass *RC : BuildCfg.Classes) {
8777fa27ce4SDimitry Andric for (MCPhysReg R : *RC)
8787fa27ce4SDimitry Andric BaseSet.insert(R);
8797fa27ce4SDimitry Andric }
8807fa27ce4SDimitry Andric }
8817fa27ce4SDimitry Andric for (RegisterId R : BaseSet) {
8827fa27ce4SDimitry Andric if (SkipReserved && ReservedRegs[R])
8837fa27ce4SDimitry Andric continue;
8847fa27ce4SDimitry Andric Insert(TrackedUnits, getPRI().getUnits(RegisterRef(R)));
8857fa27ce4SDimitry Andric }
8867fa27ce4SDimitry Andric } else {
8877fa27ce4SDimitry Andric // Track set in Config overrides everything.
8887fa27ce4SDimitry Andric for (unsigned R : BuildCfg.TrackRegs) {
8897fa27ce4SDimitry Andric if (SkipReserved && ReservedRegs[R])
8907fa27ce4SDimitry Andric continue;
8917fa27ce4SDimitry Andric Insert(TrackedUnits, getPRI().getUnits(RegisterRef(R)));
8927fa27ce4SDimitry Andric }
8937fa27ce4SDimitry Andric }
8947fa27ce4SDimitry Andric
8957fa27ce4SDimitry Andric TheFunc = newFunc(&MF);
896050e163aSDimitry Andric
897050e163aSDimitry Andric if (MF.empty())
898050e163aSDimitry Andric return;
899050e163aSDimitry Andric
900b915e9e0SDimitry Andric for (MachineBasicBlock &B : MF) {
9017fa27ce4SDimitry Andric Block BA = newBlock(TheFunc, &B);
902b915e9e0SDimitry Andric BlockNodes.insert(std::make_pair(&B, BA));
903b915e9e0SDimitry Andric for (MachineInstr &I : B) {
904eb11fae6SDimitry Andric if (I.isDebugInstr())
905050e163aSDimitry Andric continue;
906050e163aSDimitry Andric buildStmt(BA, I);
907050e163aSDimitry Andric }
908050e163aSDimitry Andric }
909050e163aSDimitry Andric
9107fa27ce4SDimitry Andric Block EA = TheFunc.Addr->getEntryBlock(*this);
9117fa27ce4SDimitry Andric NodeList Blocks = TheFunc.Addr->members(*this);
912050e163aSDimitry Andric
91371d5a254SDimitry Andric // Collect function live-ins and entry block live-ins.
91471d5a254SDimitry Andric MachineBasicBlock &EntryB = *EA.Addr->getCode();
91571d5a254SDimitry Andric assert(EntryB.pred_empty() && "Function entry block has predecessors");
916044eb2f6SDimitry Andric for (std::pair<unsigned, unsigned> P : MRI.liveins())
917044eb2f6SDimitry Andric LiveIns.insert(RegisterRef(P.first));
91871d5a254SDimitry Andric if (MRI.tracksLiveness()) {
91971d5a254SDimitry Andric for (auto I : EntryB.liveins())
92071d5a254SDimitry Andric LiveIns.insert(RegisterRef(I.PhysReg, I.LaneMask));
92171d5a254SDimitry Andric }
92271d5a254SDimitry Andric
92371d5a254SDimitry Andric // Add function-entry phi nodes for the live-in registers.
9247fa27ce4SDimitry Andric for (RegisterRef RR : LiveIns.refs()) {
9257fa27ce4SDimitry Andric if (RR.isReg() && !isTracked(RR)) // isReg is likely guaranteed
9267fa27ce4SDimitry Andric continue;
9277fa27ce4SDimitry Andric Phi PA = newPhi(EA);
928050e163aSDimitry Andric uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving;
9297fa27ce4SDimitry Andric Def DA = newDef(PA, RR, PhiFlags);
930050e163aSDimitry Andric PA.Addr->addMember(DA, *this);
931050e163aSDimitry Andric }
932050e163aSDimitry Andric
933b915e9e0SDimitry Andric // Add phis for landing pads.
934b915e9e0SDimitry Andric // Landing pads, unlike usual backs blocks, are not entered through
935b915e9e0SDimitry Andric // branches in the program, or fall-throughs from other blocks. They
936b915e9e0SDimitry Andric // are entered from the exception handling runtime and target's ABI
937b915e9e0SDimitry Andric // may define certain registers as defined on entry to such a block.
9387fa27ce4SDimitry Andric RegisterAggr EHRegs = getLandingPadLiveIns();
939b915e9e0SDimitry Andric if (!EHRegs.empty()) {
9407fa27ce4SDimitry Andric for (Block BA : Blocks) {
941b915e9e0SDimitry Andric const MachineBasicBlock &B = *BA.Addr->getCode();
942b915e9e0SDimitry Andric if (!B.isEHPad())
943b915e9e0SDimitry Andric continue;
944b915e9e0SDimitry Andric
945b915e9e0SDimitry Andric // Prepare a list of NodeIds of the block's predecessors.
946b915e9e0SDimitry Andric NodeList Preds;
947b915e9e0SDimitry Andric for (MachineBasicBlock *PB : B.predecessors())
948b915e9e0SDimitry Andric Preds.push_back(findBlock(PB));
949b915e9e0SDimitry Andric
950b915e9e0SDimitry Andric // Build phi nodes for each live-in.
9517fa27ce4SDimitry Andric for (RegisterRef RR : EHRegs.refs()) {
9527fa27ce4SDimitry Andric if (RR.isReg() && !isTracked(RR))
9537fa27ce4SDimitry Andric continue;
9547fa27ce4SDimitry Andric Phi PA = newPhi(BA);
955b915e9e0SDimitry Andric uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving;
956b915e9e0SDimitry Andric // Add def:
9577fa27ce4SDimitry Andric Def DA = newDef(PA, RR, PhiFlags);
958b915e9e0SDimitry Andric PA.Addr->addMember(DA, *this);
959b915e9e0SDimitry Andric // Add uses (no reaching defs for phi uses):
9607fa27ce4SDimitry Andric for (Block PBA : Preds) {
9617fa27ce4SDimitry Andric PhiUse PUA = newPhiUse(PA, RR, PBA);
962b915e9e0SDimitry Andric PA.Addr->addMember(PUA, *this);
963b915e9e0SDimitry Andric }
964b915e9e0SDimitry Andric }
965b915e9e0SDimitry Andric }
966b915e9e0SDimitry Andric }
967b915e9e0SDimitry Andric
968050e163aSDimitry Andric // Build a map "PhiM" which will contain, for each block, the set
969050e163aSDimitry Andric // of references that will require phi definitions in that block.
9707fa27ce4SDimitry Andric BlockRefsMap PhiM(getPRI());
9717fa27ce4SDimitry Andric for (Block BA : Blocks)
972044eb2f6SDimitry Andric recordDefsForDF(PhiM, BA);
9737fa27ce4SDimitry Andric for (Block BA : Blocks)
9747fa27ce4SDimitry Andric buildPhis(PhiM, BA);
975050e163aSDimitry Andric
976050e163aSDimitry Andric // Link all the refs. This will recursively traverse the dominator tree.
977050e163aSDimitry Andric DefStackMap DM;
978050e163aSDimitry Andric linkBlockRefs(DM, EA);
979050e163aSDimitry Andric
980050e163aSDimitry Andric // Finally, remove all unused phi nodes.
9817fa27ce4SDimitry Andric if (!(BuildCfg.Options & BuildOptions::KeepDeadPhis))
982050e163aSDimitry Andric removeUnusedPhis();
983050e163aSDimitry Andric }
984050e163aSDimitry Andric
makeRegRef(unsigned Reg,unsigned Sub) const985b915e9e0SDimitry Andric RegisterRef DataFlowGraph::makeRegRef(unsigned Reg, unsigned Sub) const {
9867fa27ce4SDimitry Andric assert(RegisterRef::isRegId(Reg) || RegisterRef::isMaskId(Reg));
98771d5a254SDimitry Andric assert(Reg != 0);
988b915e9e0SDimitry Andric if (Sub != 0)
989b915e9e0SDimitry Andric Reg = TRI.getSubReg(Reg, Sub);
990b915e9e0SDimitry Andric return RegisterRef(Reg);
991b915e9e0SDimitry Andric }
992b915e9e0SDimitry Andric
makeRegRef(const MachineOperand & Op) const99371d5a254SDimitry Andric RegisterRef DataFlowGraph::makeRegRef(const MachineOperand &Op) const {
99471d5a254SDimitry Andric assert(Op.isReg() || Op.isRegMask());
99571d5a254SDimitry Andric if (Op.isReg())
99671d5a254SDimitry Andric return makeRegRef(Op.getReg(), Op.getSubReg());
9977fa27ce4SDimitry Andric return RegisterRef(getPRI().getRegMaskId(Op.getRegMask()),
9987fa27ce4SDimitry Andric LaneBitmask::getAll());
999b915e9e0SDimitry Andric }
1000b915e9e0SDimitry Andric
1001050e163aSDimitry Andric // For each stack in the map DefM, push the delimiter for block B on it.
markBlock(NodeId B,DefStackMap & DefM)1002050e163aSDimitry Andric void DataFlowGraph::markBlock(NodeId B, DefStackMap &DefM) {
1003050e163aSDimitry Andric // Push block delimiters.
1004344a3780SDimitry Andric for (auto &P : DefM)
1005344a3780SDimitry Andric P.second.start_block(B);
1006050e163aSDimitry Andric }
1007050e163aSDimitry Andric
1008050e163aSDimitry Andric // Remove all definitions coming from block B from each stack in DefM.
releaseBlock(NodeId B,DefStackMap & DefM)1009050e163aSDimitry Andric void DataFlowGraph::releaseBlock(NodeId B, DefStackMap &DefM) {
1010050e163aSDimitry Andric // Pop all defs from this block from the definition stack. Defs that were
1011050e163aSDimitry Andric // added to the map during the traversal of instructions will not have a
1012050e163aSDimitry Andric // delimiter, but for those, the whole stack will be emptied.
1013344a3780SDimitry Andric for (auto &P : DefM)
1014344a3780SDimitry Andric P.second.clear_block(B);
1015050e163aSDimitry Andric
1016050e163aSDimitry Andric // Finally, remove empty stacks from the map.
1017050e163aSDimitry Andric for (auto I = DefM.begin(), E = DefM.end(), NextI = I; I != E; I = NextI) {
1018050e163aSDimitry Andric NextI = std::next(I);
1019050e163aSDimitry Andric // This preserves the validity of iterators other than I.
1020050e163aSDimitry Andric if (I->second.empty())
1021050e163aSDimitry Andric DefM.erase(I);
1022050e163aSDimitry Andric }
1023050e163aSDimitry Andric }
1024050e163aSDimitry Andric
1025050e163aSDimitry Andric // Push all definitions from the instruction node IA to an appropriate
1026050e163aSDimitry Andric // stack in DefM.
pushAllDefs(Instr IA,DefStackMap & DefM)10277fa27ce4SDimitry Andric void DataFlowGraph::pushAllDefs(Instr IA, DefStackMap &DefM) {
102871d5a254SDimitry Andric pushClobbers(IA, DefM);
102971d5a254SDimitry Andric pushDefs(IA, DefM);
103071d5a254SDimitry Andric }
103171d5a254SDimitry Andric
103271d5a254SDimitry Andric // Push all definitions from the instruction node IA to an appropriate
103371d5a254SDimitry Andric // stack in DefM.
pushClobbers(Instr IA,DefStackMap & DefM)10347fa27ce4SDimitry Andric void DataFlowGraph::pushClobbers(Instr IA, DefStackMap &DefM) {
103571d5a254SDimitry Andric NodeSet Visited;
103671d5a254SDimitry Andric std::set<RegisterId> Defined;
103771d5a254SDimitry Andric
103871d5a254SDimitry Andric // The important objectives of this function are:
103971d5a254SDimitry Andric // - to be able to handle instructions both while the graph is being
104071d5a254SDimitry Andric // constructed, and after the graph has been constructed, and
104171d5a254SDimitry Andric // - maintain proper ordering of definitions on the stack for each
104271d5a254SDimitry Andric // register reference:
104371d5a254SDimitry Andric // - if there are two or more related defs in IA (i.e. coming from
104471d5a254SDimitry Andric // the same machine operand), then only push one def on the stack,
104571d5a254SDimitry Andric // - if there are multiple unrelated defs of non-overlapping
104671d5a254SDimitry Andric // subregisters of S, then the stack for S will have both (in an
104771d5a254SDimitry Andric // unspecified order), but the order does not matter from the data-
104871d5a254SDimitry Andric // -flow perspective.
104971d5a254SDimitry Andric
10507fa27ce4SDimitry Andric for (Def DA : IA.Addr->members_if(IsDef, *this)) {
105171d5a254SDimitry Andric if (Visited.count(DA.Id))
105271d5a254SDimitry Andric continue;
105371d5a254SDimitry Andric if (!(DA.Addr->getFlags() & NodeAttrs::Clobbering))
105471d5a254SDimitry Andric continue;
105571d5a254SDimitry Andric
105671d5a254SDimitry Andric NodeList Rel = getRelatedRefs(IA, DA);
10577fa27ce4SDimitry Andric Def PDA = Rel.front();
105871d5a254SDimitry Andric RegisterRef RR = PDA.Addr->getRegRef(*this);
105971d5a254SDimitry Andric
106071d5a254SDimitry Andric // Push the definition on the stack for the register and all aliases.
106171d5a254SDimitry Andric // The def stack traversal in linkNodeUp will check the exact aliasing.
106271d5a254SDimitry Andric DefM[RR.Reg].push(DA);
106371d5a254SDimitry Andric Defined.insert(RR.Reg);
10647fa27ce4SDimitry Andric for (RegisterId A : getPRI().getAliasSet(RR.Reg)) {
10657fa27ce4SDimitry Andric if (RegisterRef::isRegId(A) && !isTracked(RegisterRef(A)))
10667fa27ce4SDimitry Andric continue;
106771d5a254SDimitry Andric // Check that we don't push the same def twice.
106871d5a254SDimitry Andric assert(A != RR.Reg);
106971d5a254SDimitry Andric if (!Defined.count(A))
107071d5a254SDimitry Andric DefM[A].push(DA);
107171d5a254SDimitry Andric }
107271d5a254SDimitry Andric // Mark all the related defs as visited.
10737fa27ce4SDimitry Andric for (Node T : Rel)
107471d5a254SDimitry Andric Visited.insert(T.Id);
107571d5a254SDimitry Andric }
107671d5a254SDimitry Andric }
107771d5a254SDimitry Andric
107871d5a254SDimitry Andric // Push all definitions from the instruction node IA to an appropriate
107971d5a254SDimitry Andric // stack in DefM.
pushDefs(Instr IA,DefStackMap & DefM)10807fa27ce4SDimitry Andric void DataFlowGraph::pushDefs(Instr IA, DefStackMap &DefM) {
1081050e163aSDimitry Andric NodeSet Visited;
1082050e163aSDimitry Andric #ifndef NDEBUG
108371d5a254SDimitry Andric std::set<RegisterId> Defined;
1084050e163aSDimitry Andric #endif
1085050e163aSDimitry Andric
1086050e163aSDimitry Andric // The important objectives of this function are:
1087050e163aSDimitry Andric // - to be able to handle instructions both while the graph is being
1088050e163aSDimitry Andric // constructed, and after the graph has been constructed, and
1089050e163aSDimitry Andric // - maintain proper ordering of definitions on the stack for each
1090050e163aSDimitry Andric // register reference:
1091050e163aSDimitry Andric // - if there are two or more related defs in IA (i.e. coming from
1092050e163aSDimitry Andric // the same machine operand), then only push one def on the stack,
1093050e163aSDimitry Andric // - if there are multiple unrelated defs of non-overlapping
1094050e163aSDimitry Andric // subregisters of S, then the stack for S will have both (in an
1095050e163aSDimitry Andric // unspecified order), but the order does not matter from the data-
1096050e163aSDimitry Andric // -flow perspective.
1097050e163aSDimitry Andric
10987fa27ce4SDimitry Andric for (Def DA : IA.Addr->members_if(IsDef, *this)) {
1099050e163aSDimitry Andric if (Visited.count(DA.Id))
1100050e163aSDimitry Andric continue;
110171d5a254SDimitry Andric if (DA.Addr->getFlags() & NodeAttrs::Clobbering)
110271d5a254SDimitry Andric continue;
1103b915e9e0SDimitry Andric
1104050e163aSDimitry Andric NodeList Rel = getRelatedRefs(IA, DA);
11057fa27ce4SDimitry Andric Def PDA = Rel.front();
1106b915e9e0SDimitry Andric RegisterRef RR = PDA.Addr->getRegRef(*this);
1107050e163aSDimitry Andric #ifndef NDEBUG
1108050e163aSDimitry Andric // Assert if the register is defined in two or more unrelated defs.
1109050e163aSDimitry Andric // This could happen if there are two or more def operands defining it.
111071d5a254SDimitry Andric if (!Defined.insert(RR.Reg).second) {
11117fa27ce4SDimitry Andric MachineInstr *MI = Stmt(IA).Addr->getCode();
11127fa27ce4SDimitry Andric dbgs() << "Multiple definitions of register: " << Print(RR, *this)
11137fa27ce4SDimitry Andric << " in\n " << *MI << "in " << printMBBReference(*MI->getParent())
11147fa27ce4SDimitry Andric << '\n';
1115050e163aSDimitry Andric llvm_unreachable(nullptr);
1116050e163aSDimitry Andric }
1117050e163aSDimitry Andric #endif
1118b915e9e0SDimitry Andric // Push the definition on the stack for the register and all aliases.
1119b915e9e0SDimitry Andric // The def stack traversal in linkNodeUp will check the exact aliasing.
1120b915e9e0SDimitry Andric DefM[RR.Reg].push(DA);
11217fa27ce4SDimitry Andric for (RegisterId A : getPRI().getAliasSet(RR.Reg)) {
11227fa27ce4SDimitry Andric if (RegisterRef::isRegId(A) && !isTracked(RegisterRef(A)))
11237fa27ce4SDimitry Andric continue;
1124b915e9e0SDimitry Andric // Check that we don't push the same def twice.
112571d5a254SDimitry Andric assert(A != RR.Reg);
112671d5a254SDimitry Andric DefM[A].push(DA);
1127050e163aSDimitry Andric }
1128050e163aSDimitry Andric // Mark all the related defs as visited.
11297fa27ce4SDimitry Andric for (Node T : Rel)
1130050e163aSDimitry Andric Visited.insert(T.Id);
1131050e163aSDimitry Andric }
1132050e163aSDimitry Andric }
1133050e163aSDimitry Andric
1134050e163aSDimitry Andric // Return the list of all reference nodes related to RA, including RA itself.
1135050e163aSDimitry Andric // See "getNextRelated" for the meaning of a "related reference".
getRelatedRefs(Instr IA,Ref RA) const11367fa27ce4SDimitry Andric NodeList DataFlowGraph::getRelatedRefs(Instr IA, Ref RA) const {
1137050e163aSDimitry Andric assert(IA.Id != 0 && RA.Id != 0);
1138050e163aSDimitry Andric
1139050e163aSDimitry Andric NodeList Refs;
1140050e163aSDimitry Andric NodeId Start = RA.Id;
1141050e163aSDimitry Andric do {
1142050e163aSDimitry Andric Refs.push_back(RA);
1143050e163aSDimitry Andric RA = getNextRelated(IA, RA);
1144050e163aSDimitry Andric } while (RA.Id != 0 && RA.Id != Start);
1145050e163aSDimitry Andric return Refs;
1146050e163aSDimitry Andric }
1147050e163aSDimitry Andric
1148050e163aSDimitry Andric // Clear all information in the graph.
reset()1149050e163aSDimitry Andric void DataFlowGraph::reset() {
1150050e163aSDimitry Andric Memory.clear();
1151b915e9e0SDimitry Andric BlockNodes.clear();
11527fa27ce4SDimitry Andric TrackedUnits.clear();
11537fa27ce4SDimitry Andric ReservedRegs.clear();
11547fa27ce4SDimitry Andric TheFunc = Func();
1155050e163aSDimitry Andric }
1156050e163aSDimitry Andric
1157050e163aSDimitry Andric // Return the next reference node in the instruction node IA that is related
1158050e163aSDimitry Andric // to RA. Conceptually, two reference nodes are related if they refer to the
1159050e163aSDimitry Andric // same instance of a register access, but differ in flags or other minor
1160050e163aSDimitry Andric // characteristics. Specific examples of related nodes are shadow reference
1161050e163aSDimitry Andric // nodes.
1162050e163aSDimitry Andric // Return the equivalent of nullptr if there are no more related references.
getNextRelated(Instr IA,Ref RA) const11637fa27ce4SDimitry Andric Ref DataFlowGraph::getNextRelated(Instr IA, Ref RA) const {
1164050e163aSDimitry Andric assert(IA.Id != 0 && RA.Id != 0);
1165050e163aSDimitry Andric
11667fa27ce4SDimitry Andric auto IsRelated = [this, RA](Ref TA) -> bool {
1167050e163aSDimitry Andric if (TA.Addr->getKind() != RA.Addr->getKind())
1168050e163aSDimitry Andric return false;
11697fa27ce4SDimitry Andric if (!getPRI().equal_to(TA.Addr->getRegRef(*this),
11707fa27ce4SDimitry Andric RA.Addr->getRegRef(*this))) {
1171050e163aSDimitry Andric return false;
11727fa27ce4SDimitry Andric }
1173050e163aSDimitry Andric return true;
1174050e163aSDimitry Andric };
11757fa27ce4SDimitry Andric
11767fa27ce4SDimitry Andric RegisterRef RR = RA.Addr->getRegRef(*this);
11777fa27ce4SDimitry Andric if (IA.Addr->getKind() == NodeAttrs::Stmt) {
11787fa27ce4SDimitry Andric auto Cond = [&IsRelated, RA](Ref TA) -> bool {
11797fa27ce4SDimitry Andric return IsRelated(TA) && &RA.Addr->getOp() == &TA.Addr->getOp();
1180050e163aSDimitry Andric };
11817fa27ce4SDimitry Andric return RA.Addr->getNextRef(RR, Cond, true, *this);
11827fa27ce4SDimitry Andric }
11837fa27ce4SDimitry Andric
11847fa27ce4SDimitry Andric assert(IA.Addr->getKind() == NodeAttrs::Phi);
11857fa27ce4SDimitry Andric auto Cond = [&IsRelated, RA](Ref TA) -> bool {
11867fa27ce4SDimitry Andric if (!IsRelated(TA))
1187050e163aSDimitry Andric return false;
1188050e163aSDimitry Andric if (TA.Addr->getKind() != NodeAttrs::Use)
1189050e163aSDimitry Andric return true;
1190050e163aSDimitry Andric // For phi uses, compare predecessor blocks.
11917fa27ce4SDimitry Andric return PhiUse(TA).Addr->getPredecessor() ==
11927fa27ce4SDimitry Andric PhiUse(RA).Addr->getPredecessor();
1193050e163aSDimitry Andric };
11947fa27ce4SDimitry Andric return RA.Addr->getNextRef(RR, Cond, true, *this);
1195050e163aSDimitry Andric }
1196050e163aSDimitry Andric
1197050e163aSDimitry Andric // Find the next node related to RA in IA that satisfies condition P.
1198050e163aSDimitry Andric // If such a node was found, return a pair where the second element is the
1199050e163aSDimitry Andric // located node. If such a node does not exist, return a pair where the
1200050e163aSDimitry Andric // first element is the element after which such a node should be inserted,
1201050e163aSDimitry Andric // and the second element is a null-address.
1202050e163aSDimitry Andric template <typename Predicate>
locateNextRef(Instr IA,Ref RA,Predicate P) const12037fa27ce4SDimitry Andric std::pair<Ref, Ref> DataFlowGraph::locateNextRef(Instr IA, Ref RA,
1204050e163aSDimitry Andric Predicate P) const {
1205050e163aSDimitry Andric assert(IA.Id != 0 && RA.Id != 0);
1206050e163aSDimitry Andric
12077fa27ce4SDimitry Andric Ref NA;
1208050e163aSDimitry Andric NodeId Start = RA.Id;
1209050e163aSDimitry Andric while (true) {
1210050e163aSDimitry Andric NA = getNextRelated(IA, RA);
1211050e163aSDimitry Andric if (NA.Id == 0 || NA.Id == Start)
1212050e163aSDimitry Andric break;
1213050e163aSDimitry Andric if (P(NA))
1214050e163aSDimitry Andric break;
1215050e163aSDimitry Andric RA = NA;
1216050e163aSDimitry Andric }
1217050e163aSDimitry Andric
1218050e163aSDimitry Andric if (NA.Id != 0 && NA.Id != Start)
1219050e163aSDimitry Andric return std::make_pair(RA, NA);
12207fa27ce4SDimitry Andric return std::make_pair(RA, Ref());
1221050e163aSDimitry Andric }
1222050e163aSDimitry Andric
1223050e163aSDimitry Andric // Get the next shadow node in IA corresponding to RA, and optionally create
1224050e163aSDimitry Andric // such a node if it does not exist.
getNextShadow(Instr IA,Ref RA,bool Create)12257fa27ce4SDimitry Andric Ref DataFlowGraph::getNextShadow(Instr IA, Ref RA, bool Create) {
1226050e163aSDimitry Andric assert(IA.Id != 0 && RA.Id != 0);
1227050e163aSDimitry Andric
1228050e163aSDimitry Andric uint16_t Flags = RA.Addr->getFlags() | NodeAttrs::Shadow;
12297fa27ce4SDimitry Andric auto IsShadow = [Flags](Ref TA) -> bool {
1230050e163aSDimitry Andric return TA.Addr->getFlags() == Flags;
1231050e163aSDimitry Andric };
1232050e163aSDimitry Andric auto Loc = locateNextRef(IA, RA, IsShadow);
1233050e163aSDimitry Andric if (Loc.second.Id != 0 || !Create)
1234050e163aSDimitry Andric return Loc.second;
1235050e163aSDimitry Andric
1236050e163aSDimitry Andric // Create a copy of RA and mark is as shadow.
12377fa27ce4SDimitry Andric Ref NA = cloneNode(RA);
1238050e163aSDimitry Andric NA.Addr->setFlags(Flags | NodeAttrs::Shadow);
1239050e163aSDimitry Andric IA.Addr->addMemberAfter(Loc.first, NA, *this);
1240050e163aSDimitry Andric return NA;
1241050e163aSDimitry Andric }
1242050e163aSDimitry Andric
1243050e163aSDimitry Andric // Create a new statement node in the block node BA that corresponds to
1244050e163aSDimitry Andric // the machine instruction MI.
buildStmt(Block BA,MachineInstr & In)12457fa27ce4SDimitry Andric void DataFlowGraph::buildStmt(Block BA, MachineInstr &In) {
12467fa27ce4SDimitry Andric Stmt SA = newStmt(BA, &In);
1247050e163aSDimitry Andric
124801095a5dSDimitry Andric auto isCall = [](const MachineInstr &In) -> bool {
124901095a5dSDimitry Andric if (In.isCall())
125001095a5dSDimitry Andric return true;
125101095a5dSDimitry Andric // Is tail call?
125271d5a254SDimitry Andric if (In.isBranch()) {
1253b915e9e0SDimitry Andric for (const MachineOperand &Op : In.operands())
125401095a5dSDimitry Andric if (Op.isGlobal() || Op.isSymbol())
125501095a5dSDimitry Andric return true;
125671d5a254SDimitry Andric // Assume indirect branches are calls. This is for the purpose of
125771d5a254SDimitry Andric // keeping implicit operands, and so it won't hurt on intra-function
125871d5a254SDimitry Andric // indirect branches.
125971d5a254SDimitry Andric if (In.isIndirectBranch())
126071d5a254SDimitry Andric return true;
126171d5a254SDimitry Andric }
126201095a5dSDimitry Andric return false;
126301095a5dSDimitry Andric };
126401095a5dSDimitry Andric
1265b915e9e0SDimitry Andric auto isDefUndef = [this](const MachineInstr &In, RegisterRef DR) -> bool {
1266b915e9e0SDimitry Andric // This instruction defines DR. Check if there is a use operand that
1267b915e9e0SDimitry Andric // would make DR live on entry to the instruction.
12687fa27ce4SDimitry Andric for (const MachineOperand &Op : In.all_uses()) {
12697fa27ce4SDimitry Andric if (Op.getReg() == 0 || Op.isUndef())
1270b915e9e0SDimitry Andric continue;
127171d5a254SDimitry Andric RegisterRef UR = makeRegRef(Op);
12727fa27ce4SDimitry Andric if (getPRI().alias(DR, UR))
1273b915e9e0SDimitry Andric return false;
1274b915e9e0SDimitry Andric }
1275b915e9e0SDimitry Andric return true;
1276b915e9e0SDimitry Andric };
1277b915e9e0SDimitry Andric
1278b915e9e0SDimitry Andric bool IsCall = isCall(In);
1279050e163aSDimitry Andric unsigned NumOps = In.getNumOperands();
1280050e163aSDimitry Andric
1281050e163aSDimitry Andric // Avoid duplicate implicit defs. This will not detect cases of implicit
1282050e163aSDimitry Andric // defs that define registers that overlap, but it is not clear how to
1283050e163aSDimitry Andric // interpret that in the absence of explicit defs. Overlapping explicit
1284050e163aSDimitry Andric // defs are likely illegal already.
128571d5a254SDimitry Andric BitVector DoneDefs(TRI.getNumRegs());
1286050e163aSDimitry Andric // Process explicit defs first.
1287050e163aSDimitry Andric for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1288050e163aSDimitry Andric MachineOperand &Op = In.getOperand(OpN);
1289050e163aSDimitry Andric if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1290050e163aSDimitry Andric continue;
12911d5ae102SDimitry Andric Register R = Op.getReg();
12927fa27ce4SDimitry Andric if (!R || !R.isPhysical() || !isTracked(RegisterRef(R)))
129371d5a254SDimitry Andric continue;
1294050e163aSDimitry Andric uint16_t Flags = NodeAttrs::None;
1295b915e9e0SDimitry Andric if (TOI.isPreserving(In, OpN)) {
1296050e163aSDimitry Andric Flags |= NodeAttrs::Preserving;
1297b915e9e0SDimitry Andric // If the def is preserving, check if it is also undefined.
129871d5a254SDimitry Andric if (isDefUndef(In, makeRegRef(Op)))
1299b915e9e0SDimitry Andric Flags |= NodeAttrs::Undef;
1300b915e9e0SDimitry Andric }
1301050e163aSDimitry Andric if (TOI.isClobbering(In, OpN))
1302050e163aSDimitry Andric Flags |= NodeAttrs::Clobbering;
1303050e163aSDimitry Andric if (TOI.isFixedReg(In, OpN))
1304050e163aSDimitry Andric Flags |= NodeAttrs::Fixed;
1305b915e9e0SDimitry Andric if (IsCall && Op.isDead())
1306b915e9e0SDimitry Andric Flags |= NodeAttrs::Dead;
13077fa27ce4SDimitry Andric Def DA = newDef(SA, Op, Flags);
1308050e163aSDimitry Andric SA.Addr->addMember(DA, *this);
130971d5a254SDimitry Andric assert(!DoneDefs.test(R));
131071d5a254SDimitry Andric DoneDefs.set(R);
131171d5a254SDimitry Andric }
131271d5a254SDimitry Andric
131371d5a254SDimitry Andric // Process reg-masks (as clobbers).
131471d5a254SDimitry Andric BitVector DoneClobbers(TRI.getNumRegs());
131571d5a254SDimitry Andric for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
131671d5a254SDimitry Andric MachineOperand &Op = In.getOperand(OpN);
131771d5a254SDimitry Andric if (!Op.isRegMask())
131871d5a254SDimitry Andric continue;
13197fa27ce4SDimitry Andric uint16_t Flags = NodeAttrs::Clobbering | NodeAttrs::Fixed | NodeAttrs::Dead;
13207fa27ce4SDimitry Andric Def DA = newDef(SA, Op, Flags);
132171d5a254SDimitry Andric SA.Addr->addMember(DA, *this);
132271d5a254SDimitry Andric // Record all clobbered registers in DoneDefs.
132371d5a254SDimitry Andric const uint32_t *RM = Op.getRegMask();
13247fa27ce4SDimitry Andric for (unsigned i = 1, e = TRI.getNumRegs(); i != e; ++i) {
13257fa27ce4SDimitry Andric if (!isTracked(RegisterRef(i)))
13267fa27ce4SDimitry Andric continue;
132771d5a254SDimitry Andric if (!(RM[i / 32] & (1u << (i % 32))))
132871d5a254SDimitry Andric DoneClobbers.set(i);
1329050e163aSDimitry Andric }
13307fa27ce4SDimitry Andric }
1331050e163aSDimitry Andric
1332050e163aSDimitry Andric // Process implicit defs, skipping those that have already been added
1333050e163aSDimitry Andric // as explicit.
1334050e163aSDimitry Andric for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1335050e163aSDimitry Andric MachineOperand &Op = In.getOperand(OpN);
1336050e163aSDimitry Andric if (!Op.isReg() || !Op.isDef() || !Op.isImplicit())
1337050e163aSDimitry Andric continue;
13381d5ae102SDimitry Andric Register R = Op.getReg();
13397fa27ce4SDimitry Andric if (!R || !R.isPhysical() || !isTracked(RegisterRef(R)) || DoneDefs.test(R))
1340050e163aSDimitry Andric continue;
134171d5a254SDimitry Andric RegisterRef RR = makeRegRef(Op);
1342050e163aSDimitry Andric uint16_t Flags = NodeAttrs::None;
1343b915e9e0SDimitry Andric if (TOI.isPreserving(In, OpN)) {
1344050e163aSDimitry Andric Flags |= NodeAttrs::Preserving;
1345b915e9e0SDimitry Andric // If the def is preserving, check if it is also undefined.
1346b915e9e0SDimitry Andric if (isDefUndef(In, RR))
1347b915e9e0SDimitry Andric Flags |= NodeAttrs::Undef;
1348b915e9e0SDimitry Andric }
1349050e163aSDimitry Andric if (TOI.isClobbering(In, OpN))
1350050e163aSDimitry Andric Flags |= NodeAttrs::Clobbering;
1351050e163aSDimitry Andric if (TOI.isFixedReg(In, OpN))
1352050e163aSDimitry Andric Flags |= NodeAttrs::Fixed;
135371d5a254SDimitry Andric if (IsCall && Op.isDead()) {
135471d5a254SDimitry Andric if (DoneClobbers.test(R))
135571d5a254SDimitry Andric continue;
1356b915e9e0SDimitry Andric Flags |= NodeAttrs::Dead;
135771d5a254SDimitry Andric }
13587fa27ce4SDimitry Andric Def DA = newDef(SA, Op, Flags);
1359050e163aSDimitry Andric SA.Addr->addMember(DA, *this);
136071d5a254SDimitry Andric DoneDefs.set(R);
1361050e163aSDimitry Andric }
1362050e163aSDimitry Andric
1363050e163aSDimitry Andric for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1364050e163aSDimitry Andric MachineOperand &Op = In.getOperand(OpN);
1365050e163aSDimitry Andric if (!Op.isReg() || !Op.isUse())
1366050e163aSDimitry Andric continue;
13671d5ae102SDimitry Andric Register R = Op.getReg();
13687fa27ce4SDimitry Andric if (!R || !R.isPhysical() || !isTracked(RegisterRef(R)))
1369050e163aSDimitry Andric continue;
1370050e163aSDimitry Andric uint16_t Flags = NodeAttrs::None;
1371b915e9e0SDimitry Andric if (Op.isUndef())
1372b915e9e0SDimitry Andric Flags |= NodeAttrs::Undef;
1373050e163aSDimitry Andric if (TOI.isFixedReg(In, OpN))
1374050e163aSDimitry Andric Flags |= NodeAttrs::Fixed;
13757fa27ce4SDimitry Andric Use UA = newUse(SA, Op, Flags);
1376050e163aSDimitry Andric SA.Addr->addMember(UA, *this);
1377050e163aSDimitry Andric }
1378050e163aSDimitry Andric }
1379050e163aSDimitry Andric
1380050e163aSDimitry Andric // Scan all defs in the block node BA and record in PhiM the locations of
1381050e163aSDimitry Andric // phi nodes corresponding to these defs.
recordDefsForDF(BlockRefsMap & PhiM,Block BA)13827fa27ce4SDimitry Andric void DataFlowGraph::recordDefsForDF(BlockRefsMap &PhiM, Block BA) {
1383050e163aSDimitry Andric // Check all defs from block BA and record them in each block in BA's
1384050e163aSDimitry Andric // iterated dominance frontier. This information will later be used to
1385050e163aSDimitry Andric // create phi nodes.
1386050e163aSDimitry Andric MachineBasicBlock *BB = BA.Addr->getCode();
1387050e163aSDimitry Andric assert(BB);
1388050e163aSDimitry Andric auto DFLoc = MDF.find(BB);
1389050e163aSDimitry Andric if (DFLoc == MDF.end() || DFLoc->second.empty())
1390050e163aSDimitry Andric return;
1391050e163aSDimitry Andric
1392050e163aSDimitry Andric // Traverse all instructions in the block and collect the set of all
1393050e163aSDimitry Andric // defined references. For each reference there will be a phi created
1394050e163aSDimitry Andric // in the block's iterated dominance frontier.
1395050e163aSDimitry Andric // This is done to make sure that each defined reference gets only one
1396050e163aSDimitry Andric // phi node, even if it is defined multiple times.
13977fa27ce4SDimitry Andric RegisterAggr Defs(getPRI());
13987fa27ce4SDimitry Andric for (Instr IA : BA.Addr->members(*this)) {
13997fa27ce4SDimitry Andric for (Ref RA : IA.Addr->members_if(IsDef, *this)) {
14007fa27ce4SDimitry Andric RegisterRef RR = RA.Addr->getRegRef(*this);
14017fa27ce4SDimitry Andric if (RR.isReg() && isTracked(RR))
14027fa27ce4SDimitry Andric Defs.insert(RR);
14037fa27ce4SDimitry Andric }
14047fa27ce4SDimitry Andric }
1405050e163aSDimitry Andric
1406b915e9e0SDimitry Andric // Calculate the iterated dominance frontier of BB.
1407050e163aSDimitry Andric const MachineDominanceFrontier::DomSetType &DF = DFLoc->second;
1408050e163aSDimitry Andric SetVector<MachineBasicBlock *> IDF(DF.begin(), DF.end());
1409050e163aSDimitry Andric for (unsigned i = 0; i < IDF.size(); ++i) {
1410050e163aSDimitry Andric auto F = MDF.find(IDF[i]);
1411050e163aSDimitry Andric if (F != MDF.end())
1412050e163aSDimitry Andric IDF.insert(F->second.begin(), F->second.end());
1413050e163aSDimitry Andric }
1414050e163aSDimitry Andric
1415b915e9e0SDimitry Andric // Finally, add the set of defs to each block in the iterated dominance
1416b915e9e0SDimitry Andric // frontier.
14174b4fe385SDimitry Andric for (auto *DB : IDF) {
14187fa27ce4SDimitry Andric Block DBA = findBlock(DB);
14197fa27ce4SDimitry Andric PhiM[DBA.Id].insert(Defs);
1420050e163aSDimitry Andric }
1421050e163aSDimitry Andric }
1422050e163aSDimitry Andric
1423050e163aSDimitry Andric // Given the locations of phi nodes in the map PhiM, create the phi nodes
1424050e163aSDimitry Andric // that are located in the block node BA.
buildPhis(BlockRefsMap & PhiM,Block BA)14257fa27ce4SDimitry Andric void DataFlowGraph::buildPhis(BlockRefsMap &PhiM, Block BA) {
1426050e163aSDimitry Andric // Check if this blocks has any DF defs, i.e. if there are any defs
1427050e163aSDimitry Andric // that this block is in the iterated dominance frontier of.
1428050e163aSDimitry Andric auto HasDF = PhiM.find(BA.Id);
1429050e163aSDimitry Andric if (HasDF == PhiM.end() || HasDF->second.empty())
1430050e163aSDimitry Andric return;
1431050e163aSDimitry Andric
1432050e163aSDimitry Andric // Prepare a list of NodeIds of the block's predecessors.
1433b915e9e0SDimitry Andric NodeList Preds;
1434050e163aSDimitry Andric const MachineBasicBlock *MBB = BA.Addr->getCode();
1435b915e9e0SDimitry Andric for (MachineBasicBlock *PB : MBB->predecessors())
1436b915e9e0SDimitry Andric Preds.push_back(findBlock(PB));
1437050e163aSDimitry Andric
14387fa27ce4SDimitry Andric const RegisterAggr &Defs = PhiM[BA.Id];
1439050e163aSDimitry Andric uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving;
1440050e163aSDimitry Andric
14417fa27ce4SDimitry Andric for (RegisterRef RR : Defs.refs()) {
14427fa27ce4SDimitry Andric Phi PA = newPhi(BA);
14437fa27ce4SDimitry Andric PA.Addr->addMember(newDef(PA, RR, PhiFlags), *this);
14447fa27ce4SDimitry Andric
14457fa27ce4SDimitry Andric // Add phi uses.
14467fa27ce4SDimitry Andric for (Block PBA : Preds) {
14477fa27ce4SDimitry Andric PA.Addr->addMember(newPhiUse(PA, RR, PBA), *this);
14487fa27ce4SDimitry Andric }
1449050e163aSDimitry Andric }
1450050e163aSDimitry Andric }
1451050e163aSDimitry Andric
1452050e163aSDimitry Andric // Remove any unneeded phi nodes that were created during the build process.
removeUnusedPhis()1453050e163aSDimitry Andric void DataFlowGraph::removeUnusedPhis() {
1454050e163aSDimitry Andric // This will remove unused phis, i.e. phis where each def does not reach
1455050e163aSDimitry Andric // any uses or other defs. This will not detect or remove circular phi
1456050e163aSDimitry Andric // chains that are otherwise dead. Unused/dead phis are created during
1457050e163aSDimitry Andric // the build process and this function is intended to remove these cases
1458050e163aSDimitry Andric // that are easily determinable to be unnecessary.
1459050e163aSDimitry Andric
1460050e163aSDimitry Andric SetVector<NodeId> PhiQ;
14617fa27ce4SDimitry Andric for (Block BA : TheFunc.Addr->members(*this)) {
1462050e163aSDimitry Andric for (auto P : BA.Addr->members_if(IsPhi, *this))
1463050e163aSDimitry Andric PhiQ.insert(P.Id);
1464050e163aSDimitry Andric }
1465050e163aSDimitry Andric
1466050e163aSDimitry Andric static auto HasUsedDef = [](NodeList &Ms) -> bool {
14677fa27ce4SDimitry Andric for (Node M : Ms) {
1468050e163aSDimitry Andric if (M.Addr->getKind() != NodeAttrs::Def)
1469050e163aSDimitry Andric continue;
14707fa27ce4SDimitry Andric Def DA = M;
1471050e163aSDimitry Andric if (DA.Addr->getReachedDef() != 0 || DA.Addr->getReachedUse() != 0)
1472050e163aSDimitry Andric return true;
1473050e163aSDimitry Andric }
1474050e163aSDimitry Andric return false;
1475050e163aSDimitry Andric };
1476050e163aSDimitry Andric
1477050e163aSDimitry Andric // Any phi, if it is removed, may affect other phis (make them dead).
1478050e163aSDimitry Andric // For each removed phi, collect the potentially affected phis and add
1479050e163aSDimitry Andric // them back to the queue.
1480050e163aSDimitry Andric while (!PhiQ.empty()) {
1481050e163aSDimitry Andric auto PA = addr<PhiNode *>(PhiQ[0]);
1482050e163aSDimitry Andric PhiQ.remove(PA.Id);
1483050e163aSDimitry Andric NodeList Refs = PA.Addr->members(*this);
1484050e163aSDimitry Andric if (HasUsedDef(Refs))
1485050e163aSDimitry Andric continue;
14867fa27ce4SDimitry Andric for (Ref RA : Refs) {
1487050e163aSDimitry Andric if (NodeId RD = RA.Addr->getReachingDef()) {
1488050e163aSDimitry Andric auto RDA = addr<DefNode *>(RD);
14897fa27ce4SDimitry Andric Instr OA = RDA.Addr->getOwner(*this);
1490050e163aSDimitry Andric if (IsPhi(OA))
1491050e163aSDimitry Andric PhiQ.insert(OA.Id);
1492050e163aSDimitry Andric }
1493050e163aSDimitry Andric if (RA.Addr->isDef())
149401095a5dSDimitry Andric unlinkDef(RA, true);
1495050e163aSDimitry Andric else
149601095a5dSDimitry Andric unlinkUse(RA, true);
1497050e163aSDimitry Andric }
14987fa27ce4SDimitry Andric Block BA = PA.Addr->getOwner(*this);
1499050e163aSDimitry Andric BA.Addr->removeMember(PA, *this);
1500050e163aSDimitry Andric }
1501050e163aSDimitry Andric }
1502050e163aSDimitry Andric
1503050e163aSDimitry Andric // For a given reference node TA in an instruction node IA, connect the
1504050e163aSDimitry Andric // reaching def of TA to the appropriate def node. Create any shadow nodes
1505050e163aSDimitry Andric // as appropriate.
1506050e163aSDimitry Andric template <typename T>
linkRefUp(Instr IA,NodeAddr<T> TA,DefStack & DS)15077fa27ce4SDimitry Andric void DataFlowGraph::linkRefUp(Instr IA, NodeAddr<T> TA, DefStack &DS) {
1508050e163aSDimitry Andric if (DS.empty())
1509050e163aSDimitry Andric return;
1510b915e9e0SDimitry Andric RegisterRef RR = TA.Addr->getRegRef(*this);
1511050e163aSDimitry Andric NodeAddr<T> TAP;
1512050e163aSDimitry Andric
1513050e163aSDimitry Andric // References from the def stack that have been examined so far.
15147fa27ce4SDimitry Andric RegisterAggr Defs(getPRI());
1515050e163aSDimitry Andric
1516050e163aSDimitry Andric for (auto I = DS.top(), E = DS.bottom(); I != E; I.down()) {
1517b915e9e0SDimitry Andric RegisterRef QR = I->Addr->getRegRef(*this);
1518b915e9e0SDimitry Andric
1519050e163aSDimitry Andric // Skip all defs that are aliased to any of the defs that we have already
1520b915e9e0SDimitry Andric // seen. If this completes a cover of RR, stop the stack traversal.
1521b915e9e0SDimitry Andric bool Alias = Defs.hasAliasOf(QR);
1522b915e9e0SDimitry Andric bool Cover = Defs.insert(QR).hasCoverOf(RR);
1523b915e9e0SDimitry Andric if (Alias) {
1524b915e9e0SDimitry Andric if (Cover)
1525050e163aSDimitry Andric break;
1526050e163aSDimitry Andric continue;
1527050e163aSDimitry Andric }
1528b915e9e0SDimitry Andric
1529050e163aSDimitry Andric // The reaching def.
15307fa27ce4SDimitry Andric Def RDA = *I;
1531050e163aSDimitry Andric
1532050e163aSDimitry Andric // Pick the reached node.
1533050e163aSDimitry Andric if (TAP.Id == 0) {
1534050e163aSDimitry Andric TAP = TA;
1535050e163aSDimitry Andric } else {
1536050e163aSDimitry Andric // Mark the existing ref as "shadow" and create a new shadow.
1537050e163aSDimitry Andric TAP.Addr->setFlags(TAP.Addr->getFlags() | NodeAttrs::Shadow);
1538050e163aSDimitry Andric TAP = getNextShadow(IA, TAP, true);
1539050e163aSDimitry Andric }
1540050e163aSDimitry Andric
1541050e163aSDimitry Andric // Create the link.
1542050e163aSDimitry Andric TAP.Addr->linkToDef(TAP.Id, RDA);
1543050e163aSDimitry Andric
1544b915e9e0SDimitry Andric if (Cover)
1545050e163aSDimitry Andric break;
1546050e163aSDimitry Andric }
1547050e163aSDimitry Andric }
1548050e163aSDimitry Andric
1549050e163aSDimitry Andric // Create data-flow links for all reference nodes in the statement node SA.
155071d5a254SDimitry Andric template <typename Predicate>
linkStmtRefs(DefStackMap & DefM,Stmt SA,Predicate P)15517fa27ce4SDimitry Andric void DataFlowGraph::linkStmtRefs(DefStackMap &DefM, Stmt SA, Predicate P) {
1552b915e9e0SDimitry Andric #ifndef NDEBUG
15537fa27ce4SDimitry Andric RegisterSet Defs(getPRI());
1554b915e9e0SDimitry Andric #endif
1555050e163aSDimitry Andric
1556050e163aSDimitry Andric // Link all nodes (upwards in the data-flow) with their reaching defs.
15577fa27ce4SDimitry Andric for (Ref RA : SA.Addr->members_if(P, *this)) {
1558050e163aSDimitry Andric uint16_t Kind = RA.Addr->getKind();
1559050e163aSDimitry Andric assert(Kind == NodeAttrs::Def || Kind == NodeAttrs::Use);
1560b915e9e0SDimitry Andric RegisterRef RR = RA.Addr->getRegRef(*this);
1561b915e9e0SDimitry Andric #ifndef NDEBUG
1562b915e9e0SDimitry Andric // Do not expect multiple defs of the same reference.
1563b915e9e0SDimitry Andric assert(Kind != NodeAttrs::Def || !Defs.count(RR));
1564050e163aSDimitry Andric Defs.insert(RR);
1565b915e9e0SDimitry Andric #endif
1566050e163aSDimitry Andric
1567b915e9e0SDimitry Andric auto F = DefM.find(RR.Reg);
1568050e163aSDimitry Andric if (F == DefM.end())
1569050e163aSDimitry Andric continue;
1570050e163aSDimitry Andric DefStack &DS = F->second;
1571050e163aSDimitry Andric if (Kind == NodeAttrs::Use)
1572050e163aSDimitry Andric linkRefUp<UseNode *>(SA, RA, DS);
1573050e163aSDimitry Andric else if (Kind == NodeAttrs::Def)
1574050e163aSDimitry Andric linkRefUp<DefNode *>(SA, RA, DS);
1575050e163aSDimitry Andric else
1576050e163aSDimitry Andric llvm_unreachable("Unexpected node in instruction");
1577050e163aSDimitry Andric }
1578050e163aSDimitry Andric }
1579050e163aSDimitry Andric
1580050e163aSDimitry Andric // Create data-flow links for all instructions in the block node BA. This
1581050e163aSDimitry Andric // will include updating any phi nodes in BA.
linkBlockRefs(DefStackMap & DefM,Block BA)15827fa27ce4SDimitry Andric void DataFlowGraph::linkBlockRefs(DefStackMap &DefM, Block BA) {
1583050e163aSDimitry Andric // Push block delimiters.
1584050e163aSDimitry Andric markBlock(BA.Id, DefM);
1585050e163aSDimitry Andric
15867fa27ce4SDimitry Andric auto IsClobber = [](Ref RA) -> bool {
158771d5a254SDimitry Andric return IsDef(RA) && (RA.Addr->getFlags() & NodeAttrs::Clobbering);
158871d5a254SDimitry Andric };
15897fa27ce4SDimitry Andric auto IsNoClobber = [](Ref RA) -> bool {
159071d5a254SDimitry Andric return IsDef(RA) && !(RA.Addr->getFlags() & NodeAttrs::Clobbering);
159171d5a254SDimitry Andric };
159271d5a254SDimitry Andric
159301095a5dSDimitry Andric assert(BA.Addr && "block node address is needed to create a data-flow link");
1594050e163aSDimitry Andric // For each non-phi instruction in the block, link all the defs and uses
1595050e163aSDimitry Andric // to their reaching defs. For any member of the block (including phis),
1596050e163aSDimitry Andric // push the defs on the corresponding stacks.
15977fa27ce4SDimitry Andric for (Instr IA : BA.Addr->members(*this)) {
1598050e163aSDimitry Andric // Ignore phi nodes here. They will be linked part by part from the
1599050e163aSDimitry Andric // predecessors.
160071d5a254SDimitry Andric if (IA.Addr->getKind() == NodeAttrs::Stmt) {
160171d5a254SDimitry Andric linkStmtRefs(DefM, IA, IsUse);
160271d5a254SDimitry Andric linkStmtRefs(DefM, IA, IsClobber);
160371d5a254SDimitry Andric }
1604050e163aSDimitry Andric
1605050e163aSDimitry Andric // Push the definitions on the stack.
160671d5a254SDimitry Andric pushClobbers(IA, DefM);
160771d5a254SDimitry Andric
160871d5a254SDimitry Andric if (IA.Addr->getKind() == NodeAttrs::Stmt)
160971d5a254SDimitry Andric linkStmtRefs(DefM, IA, IsNoClobber);
161071d5a254SDimitry Andric
1611050e163aSDimitry Andric pushDefs(IA, DefM);
1612050e163aSDimitry Andric }
1613050e163aSDimitry Andric
1614050e163aSDimitry Andric // Recursively process all children in the dominator tree.
1615050e163aSDimitry Andric MachineDomTreeNode *N = MDT.getNode(BA.Addr->getCode());
16164b4fe385SDimitry Andric for (auto *I : *N) {
1617050e163aSDimitry Andric MachineBasicBlock *SB = I->getBlock();
16187fa27ce4SDimitry Andric Block SBA = findBlock(SB);
1619050e163aSDimitry Andric linkBlockRefs(DefM, SBA);
1620050e163aSDimitry Andric }
1621050e163aSDimitry Andric
1622050e163aSDimitry Andric // Link the phi uses from the successor blocks.
16237fa27ce4SDimitry Andric auto IsUseForBA = [BA](Node NA) -> bool {
1624050e163aSDimitry Andric if (NA.Addr->getKind() != NodeAttrs::Use)
1625050e163aSDimitry Andric return false;
1626050e163aSDimitry Andric assert(NA.Addr->getFlags() & NodeAttrs::PhiRef);
16277fa27ce4SDimitry Andric return PhiUse(NA).Addr->getPredecessor() == BA.Id;
1628050e163aSDimitry Andric };
1629b915e9e0SDimitry Andric
16307fa27ce4SDimitry Andric RegisterAggr EHLiveIns = getLandingPadLiveIns();
1631050e163aSDimitry Andric MachineBasicBlock *MBB = BA.Addr->getCode();
1632b915e9e0SDimitry Andric
1633b915e9e0SDimitry Andric for (MachineBasicBlock *SB : MBB->successors()) {
1634b915e9e0SDimitry Andric bool IsEHPad = SB->isEHPad();
16357fa27ce4SDimitry Andric Block SBA = findBlock(SB);
16367fa27ce4SDimitry Andric for (Instr IA : SBA.Addr->members_if(IsPhi, *this)) {
1637b915e9e0SDimitry Andric // Do not link phi uses for landing pad live-ins.
1638b915e9e0SDimitry Andric if (IsEHPad) {
1639b915e9e0SDimitry Andric // Find what register this phi is for.
16407fa27ce4SDimitry Andric Ref RA = IA.Addr->getFirstMember(*this);
1641b915e9e0SDimitry Andric assert(RA.Id != 0);
16427fa27ce4SDimitry Andric if (EHLiveIns.hasCoverOf(RA.Addr->getRegRef(*this)))
1643b915e9e0SDimitry Andric continue;
1644b915e9e0SDimitry Andric }
1645050e163aSDimitry Andric // Go over each phi use associated with MBB, and link it.
1646050e163aSDimitry Andric for (auto U : IA.Addr->members_if(IsUseForBA, *this)) {
16477fa27ce4SDimitry Andric PhiUse PUA = U;
1648b915e9e0SDimitry Andric RegisterRef RR = PUA.Addr->getRegRef(*this);
1649b915e9e0SDimitry Andric linkRefUp<UseNode *>(IA, PUA, DefM[RR.Reg]);
1650050e163aSDimitry Andric }
1651050e163aSDimitry Andric }
1652050e163aSDimitry Andric }
1653050e163aSDimitry Andric
1654050e163aSDimitry Andric // Pop all defs from this block from the definition stacks.
1655050e163aSDimitry Andric releaseBlock(BA.Id, DefM);
1656050e163aSDimitry Andric }
1657050e163aSDimitry Andric
1658050e163aSDimitry Andric // Remove the use node UA from any data-flow and structural links.
unlinkUseDF(Use UA)16597fa27ce4SDimitry Andric void DataFlowGraph::unlinkUseDF(Use UA) {
1660050e163aSDimitry Andric NodeId RD = UA.Addr->getReachingDef();
1661050e163aSDimitry Andric NodeId Sib = UA.Addr->getSibling();
1662050e163aSDimitry Andric
1663050e163aSDimitry Andric if (RD == 0) {
1664050e163aSDimitry Andric assert(Sib == 0);
1665050e163aSDimitry Andric return;
1666050e163aSDimitry Andric }
1667050e163aSDimitry Andric
1668050e163aSDimitry Andric auto RDA = addr<DefNode *>(RD);
1669050e163aSDimitry Andric auto TA = addr<UseNode *>(RDA.Addr->getReachedUse());
1670050e163aSDimitry Andric if (TA.Id == UA.Id) {
1671050e163aSDimitry Andric RDA.Addr->setReachedUse(Sib);
1672050e163aSDimitry Andric return;
1673050e163aSDimitry Andric }
1674050e163aSDimitry Andric
1675050e163aSDimitry Andric while (TA.Id != 0) {
1676050e163aSDimitry Andric NodeId S = TA.Addr->getSibling();
1677050e163aSDimitry Andric if (S == UA.Id) {
1678050e163aSDimitry Andric TA.Addr->setSibling(UA.Addr->getSibling());
1679050e163aSDimitry Andric return;
1680050e163aSDimitry Andric }
1681050e163aSDimitry Andric TA = addr<UseNode *>(S);
1682050e163aSDimitry Andric }
1683050e163aSDimitry Andric }
1684050e163aSDimitry Andric
1685050e163aSDimitry Andric // Remove the def node DA from any data-flow and structural links.
unlinkDefDF(Def DA)16867fa27ce4SDimitry Andric void DataFlowGraph::unlinkDefDF(Def DA) {
1687050e163aSDimitry Andric //
1688050e163aSDimitry Andric // RD
1689050e163aSDimitry Andric // | reached
1690050e163aSDimitry Andric // | def
1691050e163aSDimitry Andric // :
1692050e163aSDimitry Andric // .
1693050e163aSDimitry Andric // +----+
1694050e163aSDimitry Andric // ... -- | DA | -- ... -- 0 : sibling chain of DA
1695050e163aSDimitry Andric // +----+
1696050e163aSDimitry Andric // | | reached
1697050e163aSDimitry Andric // | : def
1698050e163aSDimitry Andric // | .
1699050e163aSDimitry Andric // | ... : Siblings (defs)
1700050e163aSDimitry Andric // |
1701050e163aSDimitry Andric // : reached
1702050e163aSDimitry Andric // . use
1703050e163aSDimitry Andric // ... : sibling chain of reached uses
1704050e163aSDimitry Andric
1705050e163aSDimitry Andric NodeId RD = DA.Addr->getReachingDef();
1706050e163aSDimitry Andric
1707050e163aSDimitry Andric // Visit all siblings of the reached def and reset their reaching defs.
1708050e163aSDimitry Andric // Also, defs reached by DA are now "promoted" to being reached by RD,
1709050e163aSDimitry Andric // so all of them will need to be spliced into the sibling chain where
1710050e163aSDimitry Andric // DA belongs.
1711050e163aSDimitry Andric auto getAllNodes = [this](NodeId N) -> NodeList {
1712050e163aSDimitry Andric NodeList Res;
1713050e163aSDimitry Andric while (N) {
1714050e163aSDimitry Andric auto RA = addr<RefNode *>(N);
1715050e163aSDimitry Andric // Keep the nodes in the exact sibling order.
1716050e163aSDimitry Andric Res.push_back(RA);
1717050e163aSDimitry Andric N = RA.Addr->getSibling();
1718050e163aSDimitry Andric }
1719050e163aSDimitry Andric return Res;
1720050e163aSDimitry Andric };
1721050e163aSDimitry Andric NodeList ReachedDefs = getAllNodes(DA.Addr->getReachedDef());
1722050e163aSDimitry Andric NodeList ReachedUses = getAllNodes(DA.Addr->getReachedUse());
1723050e163aSDimitry Andric
1724050e163aSDimitry Andric if (RD == 0) {
17257fa27ce4SDimitry Andric for (Ref I : ReachedDefs)
1726050e163aSDimitry Andric I.Addr->setSibling(0);
17277fa27ce4SDimitry Andric for (Ref I : ReachedUses)
1728050e163aSDimitry Andric I.Addr->setSibling(0);
1729050e163aSDimitry Andric }
17307fa27ce4SDimitry Andric for (Def I : ReachedDefs)
1731050e163aSDimitry Andric I.Addr->setReachingDef(RD);
17327fa27ce4SDimitry Andric for (Use I : ReachedUses)
1733050e163aSDimitry Andric I.Addr->setReachingDef(RD);
1734050e163aSDimitry Andric
1735050e163aSDimitry Andric NodeId Sib = DA.Addr->getSibling();
1736050e163aSDimitry Andric if (RD == 0) {
1737050e163aSDimitry Andric assert(Sib == 0);
1738050e163aSDimitry Andric return;
1739050e163aSDimitry Andric }
1740050e163aSDimitry Andric
1741050e163aSDimitry Andric // Update the reaching def node and remove DA from the sibling list.
1742050e163aSDimitry Andric auto RDA = addr<DefNode *>(RD);
1743050e163aSDimitry Andric auto TA = addr<DefNode *>(RDA.Addr->getReachedDef());
1744050e163aSDimitry Andric if (TA.Id == DA.Id) {
1745050e163aSDimitry Andric // If DA is the first reached def, just update the RD's reached def
1746050e163aSDimitry Andric // to the DA's sibling.
1747050e163aSDimitry Andric RDA.Addr->setReachedDef(Sib);
1748050e163aSDimitry Andric } else {
1749050e163aSDimitry Andric // Otherwise, traverse the sibling list of the reached defs and remove
1750050e163aSDimitry Andric // DA from it.
1751050e163aSDimitry Andric while (TA.Id != 0) {
1752050e163aSDimitry Andric NodeId S = TA.Addr->getSibling();
1753050e163aSDimitry Andric if (S == DA.Id) {
1754050e163aSDimitry Andric TA.Addr->setSibling(Sib);
1755050e163aSDimitry Andric break;
1756050e163aSDimitry Andric }
1757050e163aSDimitry Andric TA = addr<DefNode *>(S);
1758050e163aSDimitry Andric }
1759050e163aSDimitry Andric }
1760050e163aSDimitry Andric
1761050e163aSDimitry Andric // Splice the DA's reached defs into the RDA's reached def chain.
1762050e163aSDimitry Andric if (!ReachedDefs.empty()) {
17637fa27ce4SDimitry Andric auto Last = Def(ReachedDefs.back());
1764050e163aSDimitry Andric Last.Addr->setSibling(RDA.Addr->getReachedDef());
1765050e163aSDimitry Andric RDA.Addr->setReachedDef(ReachedDefs.front().Id);
1766050e163aSDimitry Andric }
1767050e163aSDimitry Andric // Splice the DA's reached uses into the RDA's reached use chain.
1768050e163aSDimitry Andric if (!ReachedUses.empty()) {
17697fa27ce4SDimitry Andric auto Last = Use(ReachedUses.back());
1770050e163aSDimitry Andric Last.Addr->setSibling(RDA.Addr->getReachedUse());
1771050e163aSDimitry Andric RDA.Addr->setReachedUse(ReachedUses.front().Id);
1772050e163aSDimitry Andric }
1773050e163aSDimitry Andric }
17747fa27ce4SDimitry Andric
isTracked(RegisterRef RR) const17757fa27ce4SDimitry Andric bool DataFlowGraph::isTracked(RegisterRef RR) const {
17767fa27ce4SDimitry Andric return !disjoint(getPRI().getUnits(RR), TrackedUnits);
17777fa27ce4SDimitry Andric }
17787fa27ce4SDimitry Andric
hasUntrackedRef(Stmt S,bool IgnoreReserved) const17797fa27ce4SDimitry Andric bool DataFlowGraph::hasUntrackedRef(Stmt S, bool IgnoreReserved) const {
17807fa27ce4SDimitry Andric SmallVector<MachineOperand *> Ops;
17817fa27ce4SDimitry Andric
17827fa27ce4SDimitry Andric for (Ref R : S.Addr->members(*this)) {
17837fa27ce4SDimitry Andric Ops.push_back(&R.Addr->getOp());
17847fa27ce4SDimitry Andric RegisterRef RR = R.Addr->getRegRef(*this);
17857fa27ce4SDimitry Andric if (IgnoreReserved && RR.isReg() && ReservedRegs[RR.idx()])
17867fa27ce4SDimitry Andric continue;
17877fa27ce4SDimitry Andric if (!isTracked(RR))
17887fa27ce4SDimitry Andric return true;
17897fa27ce4SDimitry Andric }
17907fa27ce4SDimitry Andric for (const MachineOperand &Op : S.Addr->getCode()->operands()) {
17917fa27ce4SDimitry Andric if (!Op.isReg() && !Op.isRegMask())
17927fa27ce4SDimitry Andric continue;
17934df029ccSDimitry Andric if (!llvm::is_contained(Ops, &Op))
17947fa27ce4SDimitry Andric return true;
17957fa27ce4SDimitry Andric }
17967fa27ce4SDimitry Andric return false;
17977fa27ce4SDimitry Andric }
17987fa27ce4SDimitry Andric
17997fa27ce4SDimitry Andric } // end namespace llvm::rdf
1800