/src/llvm-project/llvm/lib/CodeGen/RDFGraph.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- RDFGraph.cpp -------------------------------------------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // Target-independent, SSA-based data flow graph for register data flow (RDF). |
10 | | // |
11 | | #include "llvm/ADT/BitVector.h" |
12 | | #include "llvm/ADT/STLExtras.h" |
13 | | #include "llvm/ADT/SetVector.h" |
14 | | #include "llvm/CodeGen/MachineBasicBlock.h" |
15 | | #include "llvm/CodeGen/MachineDominanceFrontier.h" |
16 | | #include "llvm/CodeGen/MachineDominators.h" |
17 | | #include "llvm/CodeGen/MachineFunction.h" |
18 | | #include "llvm/CodeGen/MachineInstr.h" |
19 | | #include "llvm/CodeGen/MachineOperand.h" |
20 | | #include "llvm/CodeGen/MachineRegisterInfo.h" |
21 | | #include "llvm/CodeGen/RDFGraph.h" |
22 | | #include "llvm/CodeGen/RDFRegisters.h" |
23 | | #include "llvm/CodeGen/TargetInstrInfo.h" |
24 | | #include "llvm/CodeGen/TargetLowering.h" |
25 | | #include "llvm/CodeGen/TargetRegisterInfo.h" |
26 | | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
27 | | #include "llvm/IR/Function.h" |
28 | | #include "llvm/MC/LaneBitmask.h" |
29 | | #include "llvm/MC/MCInstrDesc.h" |
30 | | #include "llvm/Support/ErrorHandling.h" |
31 | | #include "llvm/Support/raw_ostream.h" |
32 | | #include <algorithm> |
33 | | #include <cassert> |
34 | | #include <cstdint> |
35 | | #include <cstring> |
36 | | #include <iterator> |
37 | | #include <set> |
38 | | #include <utility> |
39 | | #include <vector> |
40 | | |
41 | | // Printing functions. Have them here first, so that the rest of the code |
42 | | // can use them. |
43 | | namespace llvm::rdf { |
44 | | |
45 | 0 | raw_ostream &operator<<(raw_ostream &OS, const Print<RegisterRef> &P) { |
46 | 0 | P.G.getPRI().print(OS, P.Obj); |
47 | 0 | return OS; |
48 | 0 | } |
49 | | |
50 | 0 | raw_ostream &operator<<(raw_ostream &OS, const Print<NodeId> &P) { |
51 | 0 | if (P.Obj == 0) |
52 | 0 | return OS << "null"; |
53 | 0 | auto NA = P.G.addr<NodeBase *>(P.Obj); |
54 | 0 | uint16_t Attrs = NA.Addr->getAttrs(); |
55 | 0 | uint16_t Kind = NodeAttrs::kind(Attrs); |
56 | 0 | uint16_t Flags = NodeAttrs::flags(Attrs); |
57 | 0 | switch (NodeAttrs::type(Attrs)) { |
58 | 0 | case NodeAttrs::Code: |
59 | 0 | switch (Kind) { |
60 | 0 | case NodeAttrs::Func: |
61 | 0 | OS << 'f'; |
62 | 0 | break; |
63 | 0 | case NodeAttrs::Block: |
64 | 0 | OS << 'b'; |
65 | 0 | break; |
66 | 0 | case NodeAttrs::Stmt: |
67 | 0 | OS << 's'; |
68 | 0 | break; |
69 | 0 | case NodeAttrs::Phi: |
70 | 0 | OS << 'p'; |
71 | 0 | break; |
72 | 0 | default: |
73 | 0 | OS << "c?"; |
74 | 0 | break; |
75 | 0 | } |
76 | 0 | break; |
77 | 0 | case NodeAttrs::Ref: |
78 | 0 | if (Flags & NodeAttrs::Undef) |
79 | 0 | OS << '/'; |
80 | 0 | if (Flags & NodeAttrs::Dead) |
81 | 0 | OS << '\\'; |
82 | 0 | if (Flags & NodeAttrs::Preserving) |
83 | 0 | OS << '+'; |
84 | 0 | if (Flags & NodeAttrs::Clobbering) |
85 | 0 | OS << '~'; |
86 | 0 | switch (Kind) { |
87 | 0 | case NodeAttrs::Use: |
88 | 0 | OS << 'u'; |
89 | 0 | break; |
90 | 0 | case NodeAttrs::Def: |
91 | 0 | OS << 'd'; |
92 | 0 | break; |
93 | 0 | case NodeAttrs::Block: |
94 | 0 | OS << 'b'; |
95 | 0 | break; |
96 | 0 | default: |
97 | 0 | OS << "r?"; |
98 | 0 | break; |
99 | 0 | } |
100 | 0 | break; |
101 | 0 | default: |
102 | 0 | OS << '?'; |
103 | 0 | break; |
104 | 0 | } |
105 | 0 | OS << P.Obj; |
106 | 0 | if (Flags & NodeAttrs::Shadow) |
107 | 0 | OS << '"'; |
108 | 0 | return OS; |
109 | 0 | } |
110 | | |
111 | | static void printRefHeader(raw_ostream &OS, const Ref RA, |
112 | 0 | const DataFlowGraph &G) { |
113 | 0 | OS << Print(RA.Id, G) << '<' << Print(RA.Addr->getRegRef(G), G) << '>'; |
114 | 0 | if (RA.Addr->getFlags() & NodeAttrs::Fixed) |
115 | 0 | OS << '!'; |
116 | 0 | } |
117 | | |
118 | 0 | raw_ostream &operator<<(raw_ostream &OS, const Print<Def> &P) { |
119 | 0 | printRefHeader(OS, P.Obj, P.G); |
120 | 0 | OS << '('; |
121 | 0 | if (NodeId N = P.Obj.Addr->getReachingDef()) |
122 | 0 | OS << Print(N, P.G); |
123 | 0 | OS << ','; |
124 | 0 | if (NodeId N = P.Obj.Addr->getReachedDef()) |
125 | 0 | OS << Print(N, P.G); |
126 | 0 | OS << ','; |
127 | 0 | if (NodeId N = P.Obj.Addr->getReachedUse()) |
128 | 0 | OS << Print(N, P.G); |
129 | 0 | OS << "):"; |
130 | 0 | if (NodeId N = P.Obj.Addr->getSibling()) |
131 | 0 | OS << Print(N, P.G); |
132 | 0 | return OS; |
133 | 0 | } |
134 | | |
135 | 0 | raw_ostream &operator<<(raw_ostream &OS, const Print<Use> &P) { |
136 | 0 | printRefHeader(OS, P.Obj, P.G); |
137 | 0 | OS << '('; |
138 | 0 | if (NodeId N = P.Obj.Addr->getReachingDef()) |
139 | 0 | OS << Print(N, P.G); |
140 | 0 | OS << "):"; |
141 | 0 | if (NodeId N = P.Obj.Addr->getSibling()) |
142 | 0 | OS << Print(N, P.G); |
143 | 0 | return OS; |
144 | 0 | } |
145 | | |
146 | 0 | raw_ostream &operator<<(raw_ostream &OS, const Print<PhiUse> &P) { |
147 | 0 | printRefHeader(OS, P.Obj, P.G); |
148 | 0 | OS << '('; |
149 | 0 | if (NodeId N = P.Obj.Addr->getReachingDef()) |
150 | 0 | OS << Print(N, P.G); |
151 | 0 | OS << ','; |
152 | 0 | if (NodeId N = P.Obj.Addr->getPredecessor()) |
153 | 0 | OS << Print(N, P.G); |
154 | 0 | OS << "):"; |
155 | 0 | if (NodeId N = P.Obj.Addr->getSibling()) |
156 | 0 | OS << Print(N, P.G); |
157 | 0 | return OS; |
158 | 0 | } |
159 | | |
160 | 0 | raw_ostream &operator<<(raw_ostream &OS, const Print<Ref> &P) { |
161 | 0 | switch (P.Obj.Addr->getKind()) { |
162 | 0 | case NodeAttrs::Def: |
163 | 0 | OS << PrintNode<DefNode *>(P.Obj, P.G); |
164 | 0 | break; |
165 | 0 | case NodeAttrs::Use: |
166 | 0 | if (P.Obj.Addr->getFlags() & NodeAttrs::PhiRef) |
167 | 0 | OS << PrintNode<PhiUseNode *>(P.Obj, P.G); |
168 | 0 | else |
169 | 0 | OS << PrintNode<UseNode *>(P.Obj, P.G); |
170 | 0 | break; |
171 | 0 | } |
172 | 0 | return OS; |
173 | 0 | } |
174 | | |
175 | 0 | raw_ostream &operator<<(raw_ostream &OS, const Print<NodeList> &P) { |
176 | 0 | unsigned N = P.Obj.size(); |
177 | 0 | for (auto I : P.Obj) { |
178 | 0 | OS << Print(I.Id, P.G); |
179 | 0 | if (--N) |
180 | 0 | OS << ' '; |
181 | 0 | } |
182 | 0 | return OS; |
183 | 0 | } |
184 | | |
185 | 0 | raw_ostream &operator<<(raw_ostream &OS, const Print<NodeSet> &P) { |
186 | 0 | unsigned N = P.Obj.size(); |
187 | 0 | for (auto I : P.Obj) { |
188 | 0 | OS << Print(I, P.G); |
189 | 0 | if (--N) |
190 | 0 | OS << ' '; |
191 | 0 | } |
192 | 0 | return OS; |
193 | 0 | } |
194 | | |
195 | | namespace { |
196 | | |
197 | | template <typename T> struct PrintListV { |
198 | 0 | PrintListV(const NodeList &L, const DataFlowGraph &G) : List(L), G(G) {} |
199 | | |
200 | | using Type = T; |
201 | | const NodeList &List; |
202 | | const DataFlowGraph &G; |
203 | | }; |
204 | | |
205 | | template <typename T> |
206 | 0 | raw_ostream &operator<<(raw_ostream &OS, const PrintListV<T> &P) { |
207 | 0 | unsigned N = P.List.size(); |
208 | 0 | for (NodeAddr<T> A : P.List) { |
209 | 0 | OS << PrintNode<T>(A, P.G); |
210 | 0 | if (--N) |
211 | 0 | OS << ", "; |
212 | 0 | } |
213 | 0 | return OS; |
214 | 0 | } |
215 | | |
216 | | } // end anonymous namespace |
217 | | |
218 | 0 | raw_ostream &operator<<(raw_ostream &OS, const Print<Phi> &P) { |
219 | 0 | OS << Print(P.Obj.Id, P.G) << ": phi [" |
220 | 0 | << PrintListV<RefNode *>(P.Obj.Addr->members(P.G), P.G) << ']'; |
221 | 0 | return OS; |
222 | 0 | } |
223 | | |
224 | 0 | raw_ostream &operator<<(raw_ostream &OS, const Print<Stmt> &P) { |
225 | 0 | const MachineInstr &MI = *P.Obj.Addr->getCode(); |
226 | 0 | unsigned Opc = MI.getOpcode(); |
227 | 0 | OS << Print(P.Obj.Id, P.G) << ": " << P.G.getTII().getName(Opc); |
228 | | // Print the target for calls and branches (for readability). |
229 | 0 | if (MI.isCall() || MI.isBranch()) { |
230 | 0 | MachineInstr::const_mop_iterator T = |
231 | 0 | llvm::find_if(MI.operands(), [](const MachineOperand &Op) -> bool { |
232 | 0 | return Op.isMBB() || Op.isGlobal() || Op.isSymbol(); |
233 | 0 | }); |
234 | 0 | if (T != MI.operands_end()) { |
235 | 0 | OS << ' '; |
236 | 0 | if (T->isMBB()) |
237 | 0 | OS << printMBBReference(*T->getMBB()); |
238 | 0 | else if (T->isGlobal()) |
239 | 0 | OS << T->getGlobal()->getName(); |
240 | 0 | else if (T->isSymbol()) |
241 | 0 | OS << T->getSymbolName(); |
242 | 0 | } |
243 | 0 | } |
244 | 0 | OS << " [" << PrintListV<RefNode *>(P.Obj.Addr->members(P.G), P.G) << ']'; |
245 | 0 | return OS; |
246 | 0 | } |
247 | | |
248 | 0 | raw_ostream &operator<<(raw_ostream &OS, const Print<Instr> &P) { |
249 | 0 | switch (P.Obj.Addr->getKind()) { |
250 | 0 | case NodeAttrs::Phi: |
251 | 0 | OS << PrintNode<PhiNode *>(P.Obj, P.G); |
252 | 0 | break; |
253 | 0 | case NodeAttrs::Stmt: |
254 | 0 | OS << PrintNode<StmtNode *>(P.Obj, P.G); |
255 | 0 | break; |
256 | 0 | default: |
257 | 0 | OS << "instr? " << Print(P.Obj.Id, P.G); |
258 | 0 | break; |
259 | 0 | } |
260 | 0 | return OS; |
261 | 0 | } |
262 | | |
263 | 0 | raw_ostream &operator<<(raw_ostream &OS, const Print<Block> &P) { |
264 | 0 | MachineBasicBlock *BB = P.Obj.Addr->getCode(); |
265 | 0 | unsigned NP = BB->pred_size(); |
266 | 0 | std::vector<int> Ns; |
267 | 0 | auto PrintBBs = [&OS](std::vector<int> Ns) -> void { |
268 | 0 | unsigned N = Ns.size(); |
269 | 0 | for (int I : Ns) { |
270 | 0 | OS << "%bb." << I; |
271 | 0 | if (--N) |
272 | 0 | OS << ", "; |
273 | 0 | } |
274 | 0 | }; |
275 | |
|
276 | 0 | OS << Print(P.Obj.Id, P.G) << ": --- " << printMBBReference(*BB) |
277 | 0 | << " --- preds(" << NP << "): "; |
278 | 0 | for (MachineBasicBlock *B : BB->predecessors()) |
279 | 0 | Ns.push_back(B->getNumber()); |
280 | 0 | PrintBBs(Ns); |
281 | |
|
282 | 0 | unsigned NS = BB->succ_size(); |
283 | 0 | OS << " succs(" << NS << "): "; |
284 | 0 | Ns.clear(); |
285 | 0 | for (MachineBasicBlock *B : BB->successors()) |
286 | 0 | Ns.push_back(B->getNumber()); |
287 | 0 | PrintBBs(Ns); |
288 | 0 | OS << '\n'; |
289 | |
|
290 | 0 | for (auto I : P.Obj.Addr->members(P.G)) |
291 | 0 | OS << PrintNode<InstrNode *>(I, P.G) << '\n'; |
292 | 0 | return OS; |
293 | 0 | } |
294 | | |
295 | 0 | raw_ostream &operator<<(raw_ostream &OS, const Print<Func> &P) { |
296 | 0 | OS << "DFG dump:[\n" |
297 | 0 | << Print(P.Obj.Id, P.G) |
298 | 0 | << ": Function: " << P.Obj.Addr->getCode()->getName() << '\n'; |
299 | 0 | for (auto I : P.Obj.Addr->members(P.G)) |
300 | 0 | OS << PrintNode<BlockNode *>(I, P.G) << '\n'; |
301 | 0 | OS << "]\n"; |
302 | 0 | return OS; |
303 | 0 | } |
304 | | |
305 | 0 | raw_ostream &operator<<(raw_ostream &OS, const Print<RegisterSet> &P) { |
306 | 0 | OS << '{'; |
307 | 0 | for (auto I : P.Obj) |
308 | 0 | OS << ' ' << Print(I, P.G); |
309 | 0 | OS << " }"; |
310 | 0 | return OS; |
311 | 0 | } |
312 | | |
313 | 0 | raw_ostream &operator<<(raw_ostream &OS, const Print<RegisterAggr> &P) { |
314 | 0 | OS << P.Obj; |
315 | 0 | return OS; |
316 | 0 | } |
317 | | |
318 | | raw_ostream &operator<<(raw_ostream &OS, |
319 | 0 | const Print<DataFlowGraph::DefStack> &P) { |
320 | 0 | for (auto I = P.Obj.top(), E = P.Obj.bottom(); I != E;) { |
321 | 0 | OS << Print(I->Id, P.G) << '<' << Print(I->Addr->getRegRef(P.G), P.G) |
322 | 0 | << '>'; |
323 | 0 | I.down(); |
324 | 0 | if (I != E) |
325 | 0 | OS << ' '; |
326 | 0 | } |
327 | 0 | return OS; |
328 | 0 | } |
329 | | |
330 | | // Node allocation functions. |
331 | | // |
332 | | // Node allocator is like a slab memory allocator: it allocates blocks of |
333 | | // memory in sizes that are multiples of the size of a node. Each block has |
334 | | // the same size. Nodes are allocated from the currently active block, and |
335 | | // when it becomes full, a new one is created. |
336 | | // There is a mapping scheme between node id and its location in a block, |
337 | | // and within that block is described in the header file. |
338 | | // |
339 | 16.9k | void NodeAllocator::startNewBlock() { |
340 | 16.9k | void *T = MemPool.Allocate(NodesPerBlock * NodeMemSize, NodeMemSize); |
341 | 16.9k | char *P = static_cast<char *>(T); |
342 | 16.9k | Blocks.push_back(P); |
343 | | // Check if the block index is still within the allowed range, i.e. less |
344 | | // than 2^N, where N is the number of bits in NodeId for the block index. |
345 | | // BitsPerIndex is the number of bits per node index. |
346 | 16.9k | assert((Blocks.size() < ((size_t)1 << (8 * sizeof(NodeId) - BitsPerIndex))) && |
347 | 16.9k | "Out of bits for block index"); |
348 | 0 | ActiveEnd = P; |
349 | 16.9k | } |
350 | | |
351 | 3.99M | bool NodeAllocator::needNewBlock() { |
352 | 3.99M | if (Blocks.empty()) |
353 | 16.9k | return true; |
354 | | |
355 | 3.97M | char *ActiveBegin = Blocks.back(); |
356 | 3.97M | uint32_t Index = (ActiveEnd - ActiveBegin) / NodeMemSize; |
357 | 3.97M | return Index >= NodesPerBlock; |
358 | 3.99M | } |
359 | | |
360 | 3.99M | Node NodeAllocator::New() { |
361 | 3.99M | if (needNewBlock()) |
362 | 16.9k | startNewBlock(); |
363 | | |
364 | 3.99M | uint32_t ActiveB = Blocks.size() - 1; |
365 | 3.99M | uint32_t Index = (ActiveEnd - Blocks[ActiveB]) / NodeMemSize; |
366 | 3.99M | Node NA = {reinterpret_cast<NodeBase *>(ActiveEnd), makeId(ActiveB, Index)}; |
367 | 3.99M | ActiveEnd += NodeMemSize; |
368 | 3.99M | return NA; |
369 | 3.99M | } |
370 | | |
371 | 1.32M | NodeId NodeAllocator::id(const NodeBase *P) const { |
372 | 1.32M | uintptr_t A = reinterpret_cast<uintptr_t>(P); |
373 | 1.32M | for (unsigned i = 0, n = Blocks.size(); i != n; ++i) { |
374 | 1.32M | uintptr_t B = reinterpret_cast<uintptr_t>(Blocks[i]); |
375 | 1.32M | if (A < B || A >= B + NodesPerBlock * NodeMemSize) |
376 | 0 | continue; |
377 | 1.32M | uint32_t Idx = (A - B) / NodeMemSize; |
378 | 1.32M | return makeId(i, Idx); |
379 | 1.32M | } |
380 | 0 | llvm_unreachable("Invalid node address"); |
381 | 0 | } |
382 | | |
383 | 16.9k | void NodeAllocator::clear() { |
384 | 16.9k | MemPool.Reset(); |
385 | 16.9k | Blocks.clear(); |
386 | 16.9k | ActiveEnd = nullptr; |
387 | 16.9k | } |
388 | | |
389 | | // Insert node NA after "this" in the circular chain. |
390 | 2.63M | void NodeBase::append(Node NA) { |
391 | 2.63M | NodeId Nx = Next; |
392 | | // If NA is already "next", do nothing. |
393 | 2.63M | if (Next != NA.Id) { |
394 | 2.63M | Next = NA.Id; |
395 | 2.63M | NA.Addr->Next = Nx; |
396 | 2.63M | } |
397 | 2.63M | } |
398 | | |
399 | | // Fundamental node manipulator functions. |
400 | | |
401 | | // Obtain the register reference from a reference node. |
402 | 19.2M | RegisterRef RefNode::getRegRef(const DataFlowGraph &G) const { |
403 | 19.2M | assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref); |
404 | 19.2M | if (NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef) |
405 | 862k | return G.unpack(RefData.PR); |
406 | 18.4M | assert(RefData.Op != nullptr); |
407 | 0 | return G.makeRegRef(*RefData.Op); |
408 | 19.2M | } |
409 | | |
410 | | // Set the register reference in the reference node directly (for references |
411 | | // in phi nodes). |
412 | 128k | void RefNode::setRegRef(RegisterRef RR, DataFlowGraph &G) { |
413 | 128k | assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref); |
414 | 0 | assert(NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef); |
415 | 0 | RefData.PR = G.pack(RR); |
416 | 128k | } |
417 | | |
418 | | // Set the register reference in the reference node based on a machine |
419 | | // operand (for references in statement nodes). |
420 | 2.45M | void RefNode::setRegRef(MachineOperand *Op, DataFlowGraph &G) { |
421 | 2.45M | assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref); |
422 | 0 | assert(!(NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef)); |
423 | 0 | (void)G; |
424 | 2.45M | RefData.Op = Op; |
425 | 2.45M | } |
426 | | |
427 | | // Get the owner of a given reference node. |
428 | 1.70M | Node RefNode::getOwner(const DataFlowGraph &G) { |
429 | 1.70M | Node NA = G.addr<NodeBase *>(getNext()); |
430 | | |
431 | 3.46M | while (NA.Addr != this) { |
432 | 3.46M | if (NA.Addr->getType() == NodeAttrs::Code) |
433 | 1.70M | return NA; |
434 | 1.76M | NA = G.addr<NodeBase *>(NA.Addr->getNext()); |
435 | 1.76M | } |
436 | 0 | llvm_unreachable("No owner in circular list"); |
437 | 0 | } |
438 | | |
439 | | // Connect the def node to the reaching def node. |
440 | 787k | void DefNode::linkToDef(NodeId Self, Def DA) { |
441 | 787k | RefData.RD = DA.Id; |
442 | 787k | RefData.Sib = DA.Addr->getReachedDef(); |
443 | 787k | DA.Addr->setReachedDef(Self); |
444 | 787k | } |
445 | | |
446 | | // Connect the use node to the reaching def node. |
447 | 1.47M | void UseNode::linkToDef(NodeId Self, Def DA) { |
448 | 1.47M | RefData.RD = DA.Id; |
449 | 1.47M | RefData.Sib = DA.Addr->getReachedUse(); |
450 | 1.47M | DA.Addr->setReachedUse(Self); |
451 | 1.47M | } |
452 | | |
453 | | // Get the first member of the code node. |
454 | 12.3M | Node CodeNode::getFirstMember(const DataFlowGraph &G) const { |
455 | 12.3M | if (CodeData.FirstM == 0) |
456 | 362k | return Node(); |
457 | 11.9M | return G.addr<NodeBase *>(CodeData.FirstM); |
458 | 12.3M | } |
459 | | |
460 | | // Get the last member of the code node. |
461 | 3.85M | Node CodeNode::getLastMember(const DataFlowGraph &G) const { |
462 | 3.85M | if (CodeData.LastM == 0) |
463 | 1.32M | return Node(); |
464 | 2.53M | return G.addr<NodeBase *>(CodeData.LastM); |
465 | 3.85M | } |
466 | | |
467 | | // Add node NA at the end of the member list of the given code node. |
468 | 3.85M | void CodeNode::addMember(Node NA, const DataFlowGraph &G) { |
469 | 3.85M | Node ML = getLastMember(G); |
470 | 3.85M | if (ML.Id != 0) { |
471 | 2.53M | ML.Addr->append(NA); |
472 | 2.53M | } else { |
473 | 1.32M | CodeData.FirstM = NA.Id; |
474 | 1.32M | NodeId Self = G.id(this); |
475 | 1.32M | NA.Addr->setNext(Self); |
476 | 1.32M | } |
477 | 3.85M | CodeData.LastM = NA.Id; |
478 | 3.85M | } |
479 | | |
480 | | // Add node NA after member node MA in the given code node. |
481 | 102k | void CodeNode::addMemberAfter(Node MA, Node NA, const DataFlowGraph &G) { |
482 | 102k | MA.Addr->append(NA); |
483 | 102k | if (CodeData.LastM == MA.Id) |
484 | 30.8k | CodeData.LastM = NA.Id; |
485 | 102k | } |
486 | | |
487 | | // Remove member node NA from the given code node. |
488 | 52.7k | void CodeNode::removeMember(Node NA, const DataFlowGraph &G) { |
489 | 52.7k | Node MA = getFirstMember(G); |
490 | 52.7k | assert(MA.Id != 0); |
491 | | |
492 | | // Special handling if the member to remove is the first member. |
493 | 52.7k | if (MA.Id == NA.Id) { |
494 | 24.1k | if (CodeData.LastM == MA.Id) { |
495 | | // If it is the only member, set both first and last to 0. |
496 | 12.9k | CodeData.FirstM = CodeData.LastM = 0; |
497 | 12.9k | } else { |
498 | | // Otherwise, advance the first member. |
499 | 11.2k | CodeData.FirstM = MA.Addr->getNext(); |
500 | 11.2k | } |
501 | 24.1k | return; |
502 | 24.1k | } |
503 | | |
504 | 31.0k | while (MA.Addr != this) { |
505 | 31.0k | NodeId MX = MA.Addr->getNext(); |
506 | 31.0k | if (MX == NA.Id) { |
507 | 28.5k | MA.Addr->setNext(NA.Addr->getNext()); |
508 | | // If the member to remove happens to be the last one, update the |
509 | | // LastM indicator. |
510 | 28.5k | if (CodeData.LastM == NA.Id) |
511 | 12.7k | CodeData.LastM = MA.Id; |
512 | 28.5k | return; |
513 | 28.5k | } |
514 | 2.45k | MA = G.addr<NodeBase *>(MX); |
515 | 2.45k | } |
516 | 0 | llvm_unreachable("No such member"); |
517 | 0 | } |
518 | | |
519 | | // Return the list of all members of the code node. |
520 | 2.35M | NodeList CodeNode::members(const DataFlowGraph &G) const { |
521 | 8.29M | static auto True = [](Node) -> bool { return true; }; |
522 | 2.35M | return members_if(True, G); |
523 | 2.35M | } |
524 | | |
525 | | // Return the owner of the given instr node. |
526 | 112k | Node InstrNode::getOwner(const DataFlowGraph &G) { |
527 | 112k | Node NA = G.addr<NodeBase *>(getNext()); |
528 | | |
529 | 5.90M | while (NA.Addr != this) { |
530 | 5.90M | assert(NA.Addr->getType() == NodeAttrs::Code); |
531 | 5.90M | if (NA.Addr->getKind() == NodeAttrs::Block) |
532 | 112k | return NA; |
533 | 5.79M | NA = G.addr<NodeBase *>(NA.Addr->getNext()); |
534 | 5.79M | } |
535 | 0 | llvm_unreachable("No owner in circular list"); |
536 | 0 | } |
537 | | |
538 | | // Add the phi node PA to the given block node. |
539 | 61.4k | void BlockNode::addPhi(Phi PA, const DataFlowGraph &G) { |
540 | 61.4k | Node M = getFirstMember(G); |
541 | 61.4k | if (M.Id == 0) { |
542 | 0 | addMember(PA, G); |
543 | 0 | return; |
544 | 0 | } |
545 | | |
546 | 61.4k | assert(M.Addr->getType() == NodeAttrs::Code); |
547 | 61.4k | if (M.Addr->getKind() == NodeAttrs::Stmt) { |
548 | | // If the first member of the block is a statement, insert the phi as |
549 | | // the first member. |
550 | 12.7k | CodeData.FirstM = PA.Id; |
551 | 12.7k | PA.Addr->setNext(M.Id); |
552 | 48.6k | } else { |
553 | | // If the first member is a phi, find the last phi, and append PA to it. |
554 | 48.6k | assert(M.Addr->getKind() == NodeAttrs::Phi); |
555 | 0 | Node MN = M; |
556 | 218k | do { |
557 | 218k | M = MN; |
558 | 218k | MN = G.addr<NodeBase *>(M.Addr->getNext()); |
559 | 218k | assert(MN.Addr->getType() == NodeAttrs::Code); |
560 | 218k | } while (MN.Addr->getKind() == NodeAttrs::Phi); |
561 | | |
562 | | // M is the last phi. |
563 | 0 | addMemberAfter(M, PA, G); |
564 | 48.6k | } |
565 | 61.4k | } |
566 | | |
567 | | // Find the block node corresponding to the machine basic block BB in the |
568 | | // given func node. |
569 | | Block FuncNode::findBlock(const MachineBasicBlock *BB, |
570 | 21.1k | const DataFlowGraph &G) const { |
571 | 41.6k | auto EqBB = [BB](Node NA) -> bool { return Block(NA).Addr->getCode() == BB; }; |
572 | 21.1k | NodeList Ms = members_if(EqBB, G); |
573 | 21.1k | if (!Ms.empty()) |
574 | 21.1k | return Ms[0]; |
575 | 0 | return Block(); |
576 | 21.1k | } |
577 | | |
578 | | // Get the block node for the entry block in the given function. |
579 | 16.9k | Block FuncNode::getEntryBlock(const DataFlowGraph &G) { |
580 | 16.9k | MachineBasicBlock *EntryB = &getCode()->front(); |
581 | 16.9k | return findBlock(EntryB, G); |
582 | 16.9k | } |
583 | | |
584 | | // Target operand information. |
585 | | // |
586 | | |
587 | | // For a given instruction, check if there are any bits of RR that can remain |
588 | | // unchanged across this def. |
589 | | bool TargetOperandInfo::isPreserving(const MachineInstr &In, |
590 | 994k | unsigned OpNum) const { |
591 | 994k | return TII.isPredicated(In); |
592 | 994k | } |
593 | | |
594 | | // Check if the definition of RR produces an unspecified value. |
595 | | bool TargetOperandInfo::isClobbering(const MachineInstr &In, |
596 | 994k | unsigned OpNum) const { |
597 | 994k | const MachineOperand &Op = In.getOperand(OpNum); |
598 | 994k | if (Op.isRegMask()) |
599 | 0 | return true; |
600 | 994k | assert(Op.isReg()); |
601 | 994k | if (In.isCall()) |
602 | 72.0k | if (Op.isDef() && Op.isDead()) |
603 | 28.8k | return true; |
604 | 965k | return false; |
605 | 994k | } |
606 | | |
607 | | // Check if the given instruction specifically requires |
608 | | bool TargetOperandInfo::isFixedReg(const MachineInstr &In, |
609 | 2.45M | unsigned OpNum) const { |
610 | 2.45M | if (In.isCall() || In.isReturn() || In.isInlineAsm()) |
611 | 173k | return true; |
612 | | // Check for a tail call. |
613 | 2.27M | if (In.isBranch()) |
614 | 5.33k | for (const MachineOperand &O : In.operands()) |
615 | 14.6k | if (O.isGlobal() || O.isSymbol()) |
616 | 0 | return true; |
617 | | |
618 | 2.27M | const MCInstrDesc &D = In.getDesc(); |
619 | 2.27M | if (D.implicit_defs().empty() && D.implicit_uses().empty()) |
620 | 1.70M | return false; |
621 | 576k | const MachineOperand &Op = In.getOperand(OpNum); |
622 | | // If there is a sub-register, treat the operand as non-fixed. Currently, |
623 | | // fixed registers are those that are listed in the descriptor as implicit |
624 | | // uses or defs, and those lists do not allow sub-registers. |
625 | 576k | if (Op.getSubReg() != 0) |
626 | 0 | return false; |
627 | 576k | Register Reg = Op.getReg(); |
628 | 576k | ArrayRef<MCPhysReg> ImpOps = |
629 | 576k | Op.isDef() ? D.implicit_defs() : D.implicit_uses(); |
630 | 576k | return is_contained(ImpOps, Reg); |
631 | 576k | } |
632 | | |
633 | | // |
634 | | // The data flow graph construction. |
635 | | // |
636 | | |
637 | | DataFlowGraph::DataFlowGraph(MachineFunction &mf, const TargetInstrInfo &tii, |
638 | | const TargetRegisterInfo &tri, |
639 | | const MachineDominatorTree &mdt, |
640 | | const MachineDominanceFrontier &mdf) |
641 | | : DefaultTOI(std::make_unique<TargetOperandInfo>(tii)), MF(mf), TII(tii), |
642 | | TRI(tri), PRI(tri, mf), MDT(mdt), MDF(mdf), TOI(*DefaultTOI), |
643 | 16.9k | LiveIns(PRI) {} |
644 | | |
645 | | DataFlowGraph::DataFlowGraph(MachineFunction &mf, const TargetInstrInfo &tii, |
646 | | const TargetRegisterInfo &tri, |
647 | | const MachineDominatorTree &mdt, |
648 | | const MachineDominanceFrontier &mdf, |
649 | | const TargetOperandInfo &toi) |
650 | | : MF(mf), TII(tii), TRI(tri), PRI(tri, mf), MDT(mdt), MDF(mdf), TOI(toi), |
651 | 0 | LiveIns(PRI) {} |
652 | | |
653 | | // The implementation of the definition stack. |
654 | | // Each register reference has its own definition stack. In particular, |
655 | | // for a register references "Reg" and "Reg:subreg" will each have their |
656 | | // own definition stacks. |
657 | | |
658 | | // Construct a stack iterator. |
659 | | DataFlowGraph::DefStack::Iterator::Iterator(const DataFlowGraph::DefStack &S, |
660 | | bool Top) |
661 | 12.3M | : DS(S) { |
662 | 12.3M | if (!Top) { |
663 | | // Initialize to bottom. |
664 | 6.14M | Pos = 0; |
665 | 6.14M | return; |
666 | 6.14M | } |
667 | | // Initialize to the top, i.e. top-most non-delimiter (or 0, if empty). |
668 | 6.19M | Pos = DS.Stack.size(); |
669 | 7.37M | while (Pos > 0 && DS.isDelimiter(DS.Stack[Pos - 1])) |
670 | 1.17M | Pos--; |
671 | 6.19M | } |
672 | | |
673 | | // Return the size of the stack, including block delimiters. |
674 | 0 | unsigned DataFlowGraph::DefStack::size() const { |
675 | 0 | unsigned S = 0; |
676 | 0 | for (auto I = top(), E = bottom(); I != E; I.down()) |
677 | 0 | S++; |
678 | 0 | return S; |
679 | 0 | } |
680 | | |
681 | | // Remove the top entry from the stack. Remove all intervening delimiters |
682 | | // so that after this, the stack is either empty, or the top of the stack |
683 | | // is a non-delimiter. |
684 | 0 | void DataFlowGraph::DefStack::pop() { |
685 | 0 | assert(!empty()); |
686 | 0 | unsigned P = nextDown(Stack.size()); |
687 | 0 | Stack.resize(P); |
688 | 0 | } |
689 | | |
690 | | // Push a delimiter for block node N on the stack. |
691 | 1.67M | void DataFlowGraph::DefStack::start_block(NodeId N) { |
692 | 1.67M | assert(N != 0); |
693 | 0 | Stack.push_back(Def(nullptr, N)); |
694 | 1.67M | } |
695 | | |
696 | | // Remove all nodes from the top of the stack, until the delimited for |
697 | | // block node N is encountered. Remove the delimiter as well. In effect, |
698 | | // this will remove from the stack all definitions from block N. |
699 | 8.10M | void DataFlowGraph::DefStack::clear_block(NodeId N) { |
700 | 8.10M | assert(N != 0); |
701 | 0 | unsigned P = Stack.size(); |
702 | 26.8M | while (P > 0) { |
703 | 20.3M | bool Found = isDelimiter(Stack[P - 1], N); |
704 | 20.3M | P--; |
705 | 20.3M | if (Found) |
706 | 1.67M | break; |
707 | 20.3M | } |
708 | | // This will also remove the delimiter, if found. |
709 | 8.10M | Stack.resize(P); |
710 | 8.10M | } |
711 | | |
712 | | // Move the stack iterator up by one. |
713 | 0 | unsigned DataFlowGraph::DefStack::nextUp(unsigned P) const { |
714 | | // Get the next valid position after P (skipping all delimiters). |
715 | | // The input position P does not have to point to a non-delimiter. |
716 | 0 | unsigned SS = Stack.size(); |
717 | 0 | bool IsDelim; |
718 | 0 | assert(P < SS); |
719 | 0 | do { |
720 | 0 | P++; |
721 | 0 | IsDelim = isDelimiter(Stack[P - 1]); |
722 | 0 | } while (P < SS && IsDelim); |
723 | 0 | assert(!IsDelim); |
724 | 0 | return P; |
725 | 0 | } |
726 | | |
727 | | // Move the stack iterator down by one. |
728 | 92.2k | unsigned DataFlowGraph::DefStack::nextDown(unsigned P) const { |
729 | | // Get the preceding valid position before P (skipping all delimiters). |
730 | | // The input position P does not have to point to a non-delimiter. |
731 | 92.2k | assert(P > 0 && P <= Stack.size()); |
732 | 0 | bool IsDelim = isDelimiter(Stack[P - 1]); |
733 | 92.8k | do { |
734 | 92.8k | if (--P == 0) |
735 | 3.81k | break; |
736 | 88.9k | IsDelim = isDelimiter(Stack[P - 1]); |
737 | 88.9k | } while (P > 0 && IsDelim); |
738 | 0 | assert(!IsDelim); |
739 | 0 | return P; |
740 | 92.2k | } |
741 | | |
742 | | // Register information. |
743 | | |
744 | 40.2k | RegisterAggr DataFlowGraph::getLandingPadLiveIns() const { |
745 | 40.2k | RegisterAggr LR(getPRI()); |
746 | 40.2k | const Function &F = MF.getFunction(); |
747 | 40.2k | const Constant *PF = F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr; |
748 | 40.2k | const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering(); |
749 | 40.2k | if (RegisterId R = TLI.getExceptionPointerRegister(PF)) |
750 | 40.2k | LR.insert(RegisterRef(R)); |
751 | 40.2k | if (!isFuncletEHPersonality(classifyEHPersonality(PF))) { |
752 | 40.2k | if (RegisterId R = TLI.getExceptionSelectorRegister(PF)) |
753 | 40.2k | LR.insert(RegisterRef(R)); |
754 | 40.2k | } |
755 | 40.2k | return LR; |
756 | 40.2k | } |
757 | | |
758 | | // Node management functions. |
759 | | |
760 | | // Get the pointer to the node with the id N. |
761 | 61.1M | NodeBase *DataFlowGraph::ptr(NodeId N) const { |
762 | 61.1M | if (N == 0) |
763 | 0 | return nullptr; |
764 | 61.1M | return Memory.ptr(N); |
765 | 61.1M | } |
766 | | |
767 | | // Get the id of the node at the address P. |
768 | 1.32M | NodeId DataFlowGraph::id(const NodeBase *P) const { |
769 | 1.32M | if (P == nullptr) |
770 | 0 | return 0; |
771 | 1.32M | return Memory.id(P); |
772 | 1.32M | } |
773 | | |
774 | | // Allocate a new node and set the attributes to Attrs. |
775 | 3.99M | Node DataFlowGraph::newNode(uint16_t Attrs) { |
776 | 3.99M | Node P = Memory.New(); |
777 | 3.99M | P.Addr->init(); |
778 | 3.99M | P.Addr->setAttrs(Attrs); |
779 | 3.99M | return P; |
780 | 3.99M | } |
781 | | |
782 | | // Make a copy of the given node B, except for the data-flow links, which |
783 | | // are set to 0. |
784 | 53.8k | Node DataFlowGraph::cloneNode(const Node B) { |
785 | 53.8k | Node NA = newNode(0); |
786 | 53.8k | memcpy(NA.Addr, B.Addr, sizeof(NodeBase)); |
787 | | // Ref nodes need to have the data-flow links reset. |
788 | 53.8k | if (NA.Addr->getType() == NodeAttrs::Ref) { |
789 | 53.8k | Ref RA = NA; |
790 | 53.8k | RA.Addr->setReachingDef(0); |
791 | 53.8k | RA.Addr->setSibling(0); |
792 | 53.8k | if (NA.Addr->getKind() == NodeAttrs::Def) { |
793 | 21.8k | Def DA = NA; |
794 | 21.8k | DA.Addr->setReachedDef(0); |
795 | 21.8k | DA.Addr->setReachedUse(0); |
796 | 21.8k | } |
797 | 53.8k | } |
798 | 53.8k | return NA; |
799 | 53.8k | } |
800 | | |
801 | | // Allocation routines for specific node types/kinds. |
802 | | |
803 | 1.45M | Use DataFlowGraph::newUse(Instr Owner, MachineOperand &Op, uint16_t Flags) { |
804 | 1.45M | Use UA = newNode(NodeAttrs::Ref | NodeAttrs::Use | Flags); |
805 | 1.45M | UA.Addr->setRegRef(&Op, *this); |
806 | 1.45M | return UA; |
807 | 1.45M | } |
808 | | |
809 | | PhiUse DataFlowGraph::newPhiUse(Phi Owner, RegisterRef RR, Block PredB, |
810 | 67.1k | uint16_t Flags) { |
811 | 67.1k | PhiUse PUA = newNode(NodeAttrs::Ref | NodeAttrs::Use | Flags); |
812 | 67.1k | assert(Flags & NodeAttrs::PhiRef); |
813 | 0 | PUA.Addr->setRegRef(RR, *this); |
814 | 67.1k | PUA.Addr->setPredecessor(PredB.Id); |
815 | 67.1k | return PUA; |
816 | 67.1k | } |
817 | | |
818 | 994k | Def DataFlowGraph::newDef(Instr Owner, MachineOperand &Op, uint16_t Flags) { |
819 | 994k | Def DA = newNode(NodeAttrs::Ref | NodeAttrs::Def | Flags); |
820 | 994k | DA.Addr->setRegRef(&Op, *this); |
821 | 994k | return DA; |
822 | 994k | } |
823 | | |
824 | 61.4k | Def DataFlowGraph::newDef(Instr Owner, RegisterRef RR, uint16_t Flags) { |
825 | 61.4k | Def DA = newNode(NodeAttrs::Ref | NodeAttrs::Def | Flags); |
826 | 61.4k | assert(Flags & NodeAttrs::PhiRef); |
827 | 0 | DA.Addr->setRegRef(RR, *this); |
828 | 61.4k | return DA; |
829 | 61.4k | } |
830 | | |
831 | 61.4k | Phi DataFlowGraph::newPhi(Block Owner) { |
832 | 61.4k | Phi PA = newNode(NodeAttrs::Code | NodeAttrs::Phi); |
833 | 61.4k | Owner.Addr->addPhi(PA, *this); |
834 | 61.4k | return PA; |
835 | 61.4k | } |
836 | | |
837 | 1.25M | Stmt DataFlowGraph::newStmt(Block Owner, MachineInstr *MI) { |
838 | 1.25M | Stmt SA = newNode(NodeAttrs::Code | NodeAttrs::Stmt); |
839 | 1.25M | SA.Addr->setCode(MI); |
840 | 1.25M | Owner.Addr->addMember(SA, *this); |
841 | 1.25M | return SA; |
842 | 1.25M | } |
843 | | |
844 | 23.2k | Block DataFlowGraph::newBlock(Func Owner, MachineBasicBlock *BB) { |
845 | 23.2k | Block BA = newNode(NodeAttrs::Code | NodeAttrs::Block); |
846 | 23.2k | BA.Addr->setCode(BB); |
847 | 23.2k | Owner.Addr->addMember(BA, *this); |
848 | 23.2k | return BA; |
849 | 23.2k | } |
850 | | |
851 | 16.9k | Func DataFlowGraph::newFunc(MachineFunction *MF) { |
852 | 16.9k | Func FA = newNode(NodeAttrs::Code | NodeAttrs::Func); |
853 | 16.9k | FA.Addr->setCode(MF); |
854 | 16.9k | return FA; |
855 | 16.9k | } |
856 | | |
857 | | // Build the data flow graph. |
858 | 16.9k | void DataFlowGraph::build(const Config &config) { |
859 | 16.9k | reset(); |
860 | 16.9k | BuildCfg = config; |
861 | 16.9k | MachineRegisterInfo &MRI = MF.getRegInfo(); |
862 | 16.9k | ReservedRegs = MRI.getReservedRegs(); |
863 | 16.9k | bool SkipReserved = BuildCfg.Options & BuildOptions::OmitReserved; |
864 | | |
865 | 6.24M | auto Insert = [](auto &Set, auto &&Range) { |
866 | 6.24M | Set.insert(Range.begin(), Range.end()); |
867 | 6.24M | }; |
868 | | |
869 | 16.9k | if (BuildCfg.TrackRegs.empty()) { |
870 | 16.9k | std::set<RegisterId> BaseSet; |
871 | 16.9k | if (BuildCfg.Classes.empty()) { |
872 | | // Insert every register. |
873 | 6.76M | for (unsigned R = 0, E = getPRI().getTRI().getNumRegs(); R != E; ++R) |
874 | 6.74M | BaseSet.insert(R); |
875 | 16.9k | } else { |
876 | 0 | for (const TargetRegisterClass *RC : BuildCfg.Classes) { |
877 | 0 | for (MCPhysReg R : *RC) |
878 | 0 | BaseSet.insert(R); |
879 | 0 | } |
880 | 0 | } |
881 | 6.74M | for (RegisterId R : BaseSet) { |
882 | 6.74M | if (SkipReserved && ReservedRegs[R]) |
883 | 502k | continue; |
884 | 6.24M | Insert(TrackedUnits, getPRI().getUnits(RegisterRef(R))); |
885 | 6.24M | } |
886 | 16.9k | } else { |
887 | | // Track set in Config overrides everything. |
888 | 0 | for (unsigned R : BuildCfg.TrackRegs) { |
889 | 0 | if (SkipReserved && ReservedRegs[R]) |
890 | 0 | continue; |
891 | 0 | Insert(TrackedUnits, getPRI().getUnits(RegisterRef(R))); |
892 | 0 | } |
893 | 0 | } |
894 | | |
895 | 16.9k | TheFunc = newFunc(&MF); |
896 | | |
897 | 16.9k | if (MF.empty()) |
898 | 0 | return; |
899 | | |
900 | 23.2k | for (MachineBasicBlock &B : MF) { |
901 | 23.2k | Block BA = newBlock(TheFunc, &B); |
902 | 23.2k | BlockNodes.insert(std::make_pair(&B, BA)); |
903 | 1.25M | for (MachineInstr &I : B) { |
904 | 1.25M | if (I.isDebugInstr()) |
905 | 0 | continue; |
906 | 1.25M | buildStmt(BA, I); |
907 | 1.25M | } |
908 | 23.2k | } |
909 | | |
910 | 16.9k | Block EA = TheFunc.Addr->getEntryBlock(*this); |
911 | 16.9k | NodeList Blocks = TheFunc.Addr->members(*this); |
912 | | |
913 | | // Collect function live-ins and entry block live-ins. |
914 | 16.9k | MachineBasicBlock &EntryB = *EA.Addr->getCode(); |
915 | 16.9k | assert(EntryB.pred_empty() && "Function entry block has predecessors"); |
916 | 0 | for (std::pair<unsigned, unsigned> P : MRI.liveins()) |
917 | 22.3k | LiveIns.insert(RegisterRef(P.first)); |
918 | 16.9k | if (MRI.tracksLiveness()) { |
919 | 16.9k | for (auto I : EntryB.liveins()) |
920 | 23.1k | LiveIns.insert(RegisterRef(I.PhysReg, I.LaneMask)); |
921 | 16.9k | } |
922 | | |
923 | | // Add function-entry phi nodes for the live-in registers. |
924 | 29.4k | for (RegisterRef RR : LiveIns.refs()) { |
925 | 29.4k | if (RR.isReg() && !isTracked(RR)) // isReg is likely guaranteed |
926 | 0 | continue; |
927 | 29.4k | Phi PA = newPhi(EA); |
928 | 29.4k | uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving; |
929 | 29.4k | Def DA = newDef(PA, RR, PhiFlags); |
930 | 29.4k | PA.Addr->addMember(DA, *this); |
931 | 29.4k | } |
932 | | |
933 | | // Add phis for landing pads. |
934 | | // Landing pads, unlike usual backs blocks, are not entered through |
935 | | // branches in the program, or fall-throughs from other blocks. They |
936 | | // are entered from the exception handling runtime and target's ABI |
937 | | // may define certain registers as defined on entry to such a block. |
938 | 16.9k | RegisterAggr EHRegs = getLandingPadLiveIns(); |
939 | 16.9k | if (!EHRegs.empty()) { |
940 | 23.2k | for (Block BA : Blocks) { |
941 | 23.2k | const MachineBasicBlock &B = *BA.Addr->getCode(); |
942 | 23.2k | if (!B.isEHPad()) |
943 | 23.2k | continue; |
944 | | |
945 | | // Prepare a list of NodeIds of the block's predecessors. |
946 | 0 | NodeList Preds; |
947 | 0 | for (MachineBasicBlock *PB : B.predecessors()) |
948 | 0 | Preds.push_back(findBlock(PB)); |
949 | | |
950 | | // Build phi nodes for each live-in. |
951 | 0 | for (RegisterRef RR : EHRegs.refs()) { |
952 | 0 | if (RR.isReg() && !isTracked(RR)) |
953 | 0 | continue; |
954 | 0 | Phi PA = newPhi(BA); |
955 | 0 | uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving; |
956 | | // Add def: |
957 | 0 | Def DA = newDef(PA, RR, PhiFlags); |
958 | 0 | PA.Addr->addMember(DA, *this); |
959 | | // Add uses (no reaching defs for phi uses): |
960 | 0 | for (Block PBA : Preds) { |
961 | 0 | PhiUse PUA = newPhiUse(PA, RR, PBA); |
962 | 0 | PA.Addr->addMember(PUA, *this); |
963 | 0 | } |
964 | 0 | } |
965 | 0 | } |
966 | 16.9k | } |
967 | | |
968 | | // Build a map "PhiM" which will contain, for each block, the set |
969 | | // of references that will require phi definitions in that block. |
970 | 16.9k | BlockRefsMap PhiM(getPRI()); |
971 | 16.9k | for (Block BA : Blocks) |
972 | 23.2k | recordDefsForDF(PhiM, BA); |
973 | 16.9k | for (Block BA : Blocks) |
974 | 23.2k | buildPhis(PhiM, BA); |
975 | | |
976 | | // Link all the refs. This will recursively traverse the dominator tree. |
977 | 16.9k | DefStackMap DM; |
978 | 16.9k | linkBlockRefs(DM, EA); |
979 | | |
980 | | // Finally, remove all unused phi nodes. |
981 | 16.9k | if (!(BuildCfg.Options & BuildOptions::KeepDeadPhis)) |
982 | 19 | removeUnusedPhis(); |
983 | 16.9k | } |
984 | | |
985 | 18.5M | RegisterRef DataFlowGraph::makeRegRef(unsigned Reg, unsigned Sub) const { |
986 | 18.5M | assert(RegisterRef::isRegId(Reg) || RegisterRef::isMaskId(Reg)); |
987 | 0 | assert(Reg != 0); |
988 | 18.5M | if (Sub != 0) |
989 | 0 | Reg = TRI.getSubReg(Reg, Sub); |
990 | 18.5M | return RegisterRef(Reg); |
991 | 18.5M | } |
992 | | |
993 | 18.9M | RegisterRef DataFlowGraph::makeRegRef(const MachineOperand &Op) const { |
994 | 18.9M | assert(Op.isReg() || Op.isRegMask()); |
995 | 18.9M | if (Op.isReg()) |
996 | 18.5M | return makeRegRef(Op.getReg(), Op.getSubReg()); |
997 | 474k | return RegisterRef(getPRI().getRegMaskId(Op.getRegMask()), |
998 | 474k | LaneBitmask::getAll()); |
999 | 18.9M | } |
1000 | | |
1001 | | // For each stack in the map DefM, push the delimiter for block B on it. |
1002 | 34.9k | void DataFlowGraph::markBlock(NodeId B, DefStackMap &DefM) { |
1003 | | // Push block delimiters. |
1004 | 34.9k | for (auto &P : DefM) |
1005 | 1.67M | P.second.start_block(B); |
1006 | 34.9k | } |
1007 | | |
1008 | | // Remove all definitions coming from block B from each stack in DefM. |
1009 | 34.9k | void DataFlowGraph::releaseBlock(NodeId B, DefStackMap &DefM) { |
1010 | | // Pop all defs from this block from the definition stack. Defs that were |
1011 | | // added to the map during the traversal of instructions will not have a |
1012 | | // delimiter, but for those, the whole stack will be emptied. |
1013 | 34.9k | for (auto &P : DefM) |
1014 | 8.10M | P.second.clear_block(B); |
1015 | | |
1016 | | // Finally, remove empty stacks from the map. |
1017 | 8.13M | for (auto I = DefM.begin(), E = DefM.end(), NextI = I; I != E; I = NextI) { |
1018 | 8.10M | NextI = std::next(I); |
1019 | | // This preserves the validity of iterators other than I. |
1020 | 8.10M | if (I->second.empty()) |
1021 | 6.42M | DefM.erase(I); |
1022 | 8.10M | } |
1023 | 34.9k | } |
1024 | | |
1025 | | // Push all definitions from the instruction node IA to an appropriate |
1026 | | // stack in DefM. |
1027 | 655k | void DataFlowGraph::pushAllDefs(Instr IA, DefStackMap &DefM) { |
1028 | 655k | pushClobbers(IA, DefM); |
1029 | 655k | pushDefs(IA, DefM); |
1030 | 655k | } |
1031 | | |
1032 | | // Push all definitions from the instruction node IA to an appropriate |
1033 | | // stack in DefM. |
1034 | 1.97M | void DataFlowGraph::pushClobbers(Instr IA, DefStackMap &DefM) { |
1035 | 1.97M | NodeSet Visited; |
1036 | 1.97M | std::set<RegisterId> Defined; |
1037 | | |
1038 | | // The important objectives of this function are: |
1039 | | // - to be able to handle instructions both while the graph is being |
1040 | | // constructed, and after the graph has been constructed, and |
1041 | | // - maintain proper ordering of definitions on the stack for each |
1042 | | // register reference: |
1043 | | // - if there are two or more related defs in IA (i.e. coming from |
1044 | | // the same machine operand), then only push one def on the stack, |
1045 | | // - if there are multiple unrelated defs of non-overlapping |
1046 | | // subregisters of S, then the stack for S will have both (in an |
1047 | | // unspecified order), but the order does not matter from the data- |
1048 | | // -flow perspective. |
1049 | | |
1050 | 1.97M | for (Def DA : IA.Addr->members_if(IsDef, *this)) { |
1051 | 1.54M | if (Visited.count(DA.Id)) |
1052 | 0 | continue; |
1053 | 1.54M | if (!(DA.Addr->getFlags() & NodeAttrs::Clobbering)) |
1054 | 1.49M | continue; |
1055 | | |
1056 | 43.1k | NodeList Rel = getRelatedRefs(IA, DA); |
1057 | 43.1k | Def PDA = Rel.front(); |
1058 | 43.1k | RegisterRef RR = PDA.Addr->getRegRef(*this); |
1059 | | |
1060 | | // Push the definition on the stack for the register and all aliases. |
1061 | | // The def stack traversal in linkNodeUp will check the exact aliasing. |
1062 | 43.1k | DefM[RR.Reg].push(DA); |
1063 | 43.1k | Defined.insert(RR.Reg); |
1064 | 16.3M | for (RegisterId A : getPRI().getAliasSet(RR.Reg)) { |
1065 | 16.3M | if (RegisterRef::isRegId(A) && !isTracked(RegisterRef(A))) |
1066 | 1.15M | continue; |
1067 | | // Check that we don't push the same def twice. |
1068 | 15.2M | assert(A != RR.Reg); |
1069 | 15.2M | if (!Defined.count(A)) |
1070 | 15.2M | DefM[A].push(DA); |
1071 | 15.2M | } |
1072 | | // Mark all the related defs as visited. |
1073 | 43.1k | for (Node T : Rel) |
1074 | 43.1k | Visited.insert(T.Id); |
1075 | 43.1k | } |
1076 | 1.97M | } |
1077 | | |
1078 | | // Push all definitions from the instruction node IA to an appropriate |
1079 | | // stack in DefM. |
1080 | 1.97M | void DataFlowGraph::pushDefs(Instr IA, DefStackMap &DefM) { |
1081 | 1.97M | NodeSet Visited; |
1082 | 1.97M | #ifndef NDEBUG |
1083 | 1.97M | std::set<RegisterId> Defined; |
1084 | 1.97M | #endif |
1085 | | |
1086 | | // The important objectives of this function are: |
1087 | | // - to be able to handle instructions both while the graph is being |
1088 | | // constructed, and after the graph has been constructed, and |
1089 | | // - maintain proper ordering of definitions on the stack for each |
1090 | | // register reference: |
1091 | | // - if there are two or more related defs in IA (i.e. coming from |
1092 | | // the same machine operand), then only push one def on the stack, |
1093 | | // - if there are multiple unrelated defs of non-overlapping |
1094 | | // subregisters of S, then the stack for S will have both (in an |
1095 | | // unspecified order), but the order does not matter from the data- |
1096 | | // -flow perspective. |
1097 | | |
1098 | 1.97M | for (Def DA : IA.Addr->members_if(IsDef, *this)) { |
1099 | 1.56M | if (Visited.count(DA.Id)) |
1100 | 32.8k | continue; |
1101 | 1.53M | if (DA.Addr->getFlags() & NodeAttrs::Clobbering) |
1102 | 43.1k | continue; |
1103 | | |
1104 | 1.48M | NodeList Rel = getRelatedRefs(IA, DA); |
1105 | 1.48M | Def PDA = Rel.front(); |
1106 | 1.48M | RegisterRef RR = PDA.Addr->getRegRef(*this); |
1107 | 1.48M | #ifndef NDEBUG |
1108 | | // Assert if the register is defined in two or more unrelated defs. |
1109 | | // This could happen if there are two or more def operands defining it. |
1110 | 1.48M | if (!Defined.insert(RR.Reg).second) { |
1111 | 0 | MachineInstr *MI = Stmt(IA).Addr->getCode(); |
1112 | 0 | dbgs() << "Multiple definitions of register: " << Print(RR, *this) |
1113 | 0 | << " in\n " << *MI << "in " << printMBBReference(*MI->getParent()) |
1114 | 0 | << '\n'; |
1115 | 0 | llvm_unreachable(nullptr); |
1116 | 0 | } |
1117 | 1.48M | #endif |
1118 | | // Push the definition on the stack for the register and all aliases. |
1119 | | // The def stack traversal in linkNodeUp will check the exact aliasing. |
1120 | 1.48M | DefM[RR.Reg].push(DA); |
1121 | 1.97M | for (RegisterId A : getPRI().getAliasSet(RR.Reg)) { |
1122 | 1.97M | if (RegisterRef::isRegId(A) && !isTracked(RegisterRef(A))) |
1123 | 0 | continue; |
1124 | | // Check that we don't push the same def twice. |
1125 | 1.97M | assert(A != RR.Reg); |
1126 | 0 | DefM[A].push(DA); |
1127 | 1.97M | } |
1128 | | // Mark all the related defs as visited. |
1129 | 1.48M | for (Node T : Rel) |
1130 | 1.52M | Visited.insert(T.Id); |
1131 | 1.48M | } |
1132 | 1.97M | } |
1133 | | |
1134 | | // Return the list of all reference nodes related to RA, including RA itself. |
1135 | | // See "getNextRelated" for the meaning of a "related reference". |
1136 | 2.21M | NodeList DataFlowGraph::getRelatedRefs(Instr IA, Ref RA) const { |
1137 | 2.21M | assert(IA.Id != 0 && RA.Id != 0); |
1138 | | |
1139 | 0 | NodeList Refs; |
1140 | 2.21M | NodeId Start = RA.Id; |
1141 | 2.26M | do { |
1142 | 2.26M | Refs.push_back(RA); |
1143 | 2.26M | RA = getNextRelated(IA, RA); |
1144 | 2.26M | } while (RA.Id != 0 && RA.Id != Start); |
1145 | 2.21M | return Refs; |
1146 | 2.21M | } |
1147 | | |
1148 | | // Clear all information in the graph. |
1149 | 16.9k | void DataFlowGraph::reset() { |
1150 | 16.9k | Memory.clear(); |
1151 | 16.9k | BlockNodes.clear(); |
1152 | 16.9k | TrackedUnits.clear(); |
1153 | 16.9k | ReservedRegs.clear(); |
1154 | 16.9k | TheFunc = Func(); |
1155 | 16.9k | } |
1156 | | |
1157 | | // Return the next reference node in the instruction node IA that is related |
1158 | | // to RA. Conceptually, two reference nodes are related if they refer to the |
1159 | | // same instance of a register access, but differ in flags or other minor |
1160 | | // characteristics. Specific examples of related nodes are shadow reference |
1161 | | // nodes. |
1162 | | // Return the equivalent of nullptr if there are no more related references. |
1163 | 2.31M | Ref DataFlowGraph::getNextRelated(Instr IA, Ref RA) const { |
1164 | 2.31M | assert(IA.Id != 0 && RA.Id != 0); |
1165 | | |
1166 | 274k | auto IsRelated = [this, RA](Ref TA) -> bool { |
1167 | 274k | if (TA.Addr->getKind() != RA.Addr->getKind()) |
1168 | 188k | return false; |
1169 | 85.1k | if (!getPRI().equal_to(TA.Addr->getRegRef(*this), |
1170 | 85.1k | RA.Addr->getRegRef(*this))) { |
1171 | 0 | return false; |
1172 | 0 | } |
1173 | 85.1k | return true; |
1174 | 85.1k | }; |
1175 | | |
1176 | 2.31M | RegisterRef RR = RA.Addr->getRegRef(*this); |
1177 | 2.31M | if (IA.Addr->getKind() == NodeAttrs::Stmt) { |
1178 | 2.13M | auto Cond = [&IsRelated, RA](Ref TA) -> bool { |
1179 | 191k | return IsRelated(TA) && &RA.Addr->getOp() == &TA.Addr->getOp(); |
1180 | 191k | }; |
1181 | 2.13M | return RA.Addr->getNextRef(RR, Cond, true, *this); |
1182 | 2.13M | } |
1183 | | |
1184 | 177k | assert(IA.Addr->getKind() == NodeAttrs::Phi); |
1185 | 82.3k | auto Cond = [&IsRelated, RA](Ref TA) -> bool { |
1186 | 82.3k | if (!IsRelated(TA)) |
1187 | 46.3k | return false; |
1188 | 35.9k | if (TA.Addr->getKind() != NodeAttrs::Use) |
1189 | 0 | return true; |
1190 | | // For phi uses, compare predecessor blocks. |
1191 | 35.9k | return PhiUse(TA).Addr->getPredecessor() == |
1192 | 35.9k | PhiUse(RA).Addr->getPredecessor(); |
1193 | 35.9k | }; |
1194 | 177k | return RA.Addr->getNextRef(RR, Cond, true, *this); |
1195 | 2.31M | } |
1196 | | |
1197 | | // Find the next node related to RA in IA that satisfies condition P. |
1198 | | // If such a node was found, return a pair where the second element is the |
1199 | | // located node. If such a node does not exist, return a pair where the |
1200 | | // first element is the element after which such a node should be inserted, |
1201 | | // and the second element is a null-address. |
1202 | | template <typename Predicate> |
1203 | | std::pair<Ref, Ref> DataFlowGraph::locateNextRef(Instr IA, Ref RA, |
1204 | 53.8k | Predicate P) const { |
1205 | 53.8k | assert(IA.Id != 0 && RA.Id != 0); |
1206 | | |
1207 | 0 | Ref NA; |
1208 | 53.8k | NodeId Start = RA.Id; |
1209 | 53.8k | while (true) { |
1210 | 53.8k | NA = getNextRelated(IA, RA); |
1211 | 53.8k | if (NA.Id == 0 || NA.Id == Start) |
1212 | 53.8k | break; |
1213 | 0 | if (P(NA)) |
1214 | 0 | break; |
1215 | 0 | RA = NA; |
1216 | 0 | } |
1217 | | |
1218 | 53.8k | if (NA.Id != 0 && NA.Id != Start) |
1219 | 0 | return std::make_pair(RA, NA); |
1220 | 53.8k | return std::make_pair(RA, Ref()); |
1221 | 53.8k | } |
1222 | | |
1223 | | // Get the next shadow node in IA corresponding to RA, and optionally create |
1224 | | // such a node if it does not exist. |
1225 | 53.8k | Ref DataFlowGraph::getNextShadow(Instr IA, Ref RA, bool Create) { |
1226 | 53.8k | assert(IA.Id != 0 && RA.Id != 0); |
1227 | | |
1228 | 0 | uint16_t Flags = RA.Addr->getFlags() | NodeAttrs::Shadow; |
1229 | 53.8k | auto IsShadow = [Flags](Ref TA) -> bool { |
1230 | 0 | return TA.Addr->getFlags() == Flags; |
1231 | 0 | }; |
1232 | 53.8k | auto Loc = locateNextRef(IA, RA, IsShadow); |
1233 | 53.8k | if (Loc.second.Id != 0 || !Create) |
1234 | 0 | return Loc.second; |
1235 | | |
1236 | | // Create a copy of RA and mark is as shadow. |
1237 | 53.8k | Ref NA = cloneNode(RA); |
1238 | 53.8k | NA.Addr->setFlags(Flags | NodeAttrs::Shadow); |
1239 | 53.8k | IA.Addr->addMemberAfter(Loc.first, NA, *this); |
1240 | 53.8k | return NA; |
1241 | 53.8k | } |
1242 | | |
1243 | | // Create a new statement node in the block node BA that corresponds to |
1244 | | // the machine instruction MI. |
1245 | 1.25M | void DataFlowGraph::buildStmt(Block BA, MachineInstr &In) { |
1246 | 1.25M | Stmt SA = newStmt(BA, &In); |
1247 | | |
1248 | 1.25M | auto isCall = [](const MachineInstr &In) -> bool { |
1249 | 1.25M | if (In.isCall()) |
1250 | 28.7k | return true; |
1251 | | // Is tail call? |
1252 | 1.22M | if (In.isBranch()) { |
1253 | 20.0k | for (const MachineOperand &Op : In.operands()) |
1254 | 57.3k | if (Op.isGlobal() || Op.isSymbol()) |
1255 | 0 | return true; |
1256 | | // Assume indirect branches are calls. This is for the purpose of |
1257 | | // keeping implicit operands, and so it won't hurt on intra-function |
1258 | | // indirect branches. |
1259 | 20.0k | if (In.isIndirectBranch()) |
1260 | 14.6k | return true; |
1261 | 20.0k | } |
1262 | 1.21M | return false; |
1263 | 1.22M | }; |
1264 | | |
1265 | 1.25M | auto isDefUndef = [this](const MachineInstr &In, RegisterRef DR) -> bool { |
1266 | | // This instruction defines DR. Check if there is a use operand that |
1267 | | // would make DR live on entry to the instruction. |
1268 | 256k | for (const MachineOperand &Op : In.all_uses()) { |
1269 | 256k | if (Op.getReg() == 0 || Op.isUndef()) |
1270 | 119 | continue; |
1271 | 256k | RegisterRef UR = makeRegRef(Op); |
1272 | 256k | if (getPRI().alias(DR, UR)) |
1273 | 85.0k | return false; |
1274 | 256k | } |
1275 | 86.2k | return true; |
1276 | 171k | }; |
1277 | | |
1278 | 1.25M | bool IsCall = isCall(In); |
1279 | 1.25M | unsigned NumOps = In.getNumOperands(); |
1280 | | |
1281 | | // Avoid duplicate implicit defs. This will not detect cases of implicit |
1282 | | // defs that define registers that overlap, but it is not clear how to |
1283 | | // interpret that in the absence of explicit defs. Overlapping explicit |
1284 | | // defs are likely illegal already. |
1285 | 1.25M | BitVector DoneDefs(TRI.getNumRegs()); |
1286 | | // Process explicit defs first. |
1287 | 5.43M | for (unsigned OpN = 0; OpN < NumOps; ++OpN) { |
1288 | 4.18M | MachineOperand &Op = In.getOperand(OpN); |
1289 | 4.18M | if (!Op.isReg() || !Op.isDef() || Op.isImplicit()) |
1290 | 3.34M | continue; |
1291 | 839k | Register R = Op.getReg(); |
1292 | 839k | if (!R || !R.isPhysical() || !isTracked(RegisterRef(R))) |
1293 | 1.44k | continue; |
1294 | 838k | uint16_t Flags = NodeAttrs::None; |
1295 | 838k | if (TOI.isPreserving(In, OpN)) { |
1296 | 169k | Flags |= NodeAttrs::Preserving; |
1297 | | // If the def is preserving, check if it is also undefined. |
1298 | 169k | if (isDefUndef(In, makeRegRef(Op))) |
1299 | 84.9k | Flags |= NodeAttrs::Undef; |
1300 | 169k | } |
1301 | 838k | if (TOI.isClobbering(In, OpN)) |
1302 | 0 | Flags |= NodeAttrs::Clobbering; |
1303 | 838k | if (TOI.isFixedReg(In, OpN)) |
1304 | 0 | Flags |= NodeAttrs::Fixed; |
1305 | 838k | if (IsCall && Op.isDead()) |
1306 | 0 | Flags |= NodeAttrs::Dead; |
1307 | 838k | Def DA = newDef(SA, Op, Flags); |
1308 | 838k | SA.Addr->addMember(DA, *this); |
1309 | 838k | assert(!DoneDefs.test(R)); |
1310 | 0 | DoneDefs.set(R); |
1311 | 838k | } |
1312 | | |
1313 | | // Process reg-masks (as clobbers). |
1314 | 1.25M | BitVector DoneClobbers(TRI.getNumRegs()); |
1315 | 5.43M | for (unsigned OpN = 0; OpN < NumOps; ++OpN) { |
1316 | 4.18M | MachineOperand &Op = In.getOperand(OpN); |
1317 | 4.18M | if (!Op.isRegMask()) |
1318 | 4.15M | continue; |
1319 | 28.7k | uint16_t Flags = NodeAttrs::Clobbering | NodeAttrs::Fixed | NodeAttrs::Dead; |
1320 | 28.7k | Def DA = newDef(SA, Op, Flags); |
1321 | 28.7k | SA.Addr->addMember(DA, *this); |
1322 | | // Record all clobbered registers in DoneDefs. |
1323 | 28.7k | const uint32_t *RM = Op.getRegMask(); |
1324 | 11.4M | for (unsigned i = 1, e = TRI.getNumRegs(); i != e; ++i) { |
1325 | 11.4M | if (!isTracked(RegisterRef(i))) |
1326 | 578k | continue; |
1327 | 10.8M | if (!(RM[i / 32] & (1u << (i % 32)))) |
1328 | 10.3M | DoneClobbers.set(i); |
1329 | 10.8M | } |
1330 | 28.7k | } |
1331 | | |
1332 | | // Process implicit defs, skipping those that have already been added |
1333 | | // as explicit. |
1334 | 5.43M | for (unsigned OpN = 0; OpN < NumOps; ++OpN) { |
1335 | 4.18M | MachineOperand &Op = In.getOperand(OpN); |
1336 | 4.18M | if (!Op.isReg() || !Op.isDef() || !Op.isImplicit()) |
1337 | 3.89M | continue; |
1338 | 282k | Register R = Op.getReg(); |
1339 | 282k | if (!R || !R.isPhysical() || !isTracked(RegisterRef(R)) || DoneDefs.test(R)) |
1340 | 126k | continue; |
1341 | 155k | RegisterRef RR = makeRegRef(Op); |
1342 | 155k | uint16_t Flags = NodeAttrs::None; |
1343 | 155k | if (TOI.isPreserving(In, OpN)) { |
1344 | 1.31k | Flags |= NodeAttrs::Preserving; |
1345 | | // If the def is preserving, check if it is also undefined. |
1346 | 1.31k | if (isDefUndef(In, RR)) |
1347 | 1.31k | Flags |= NodeAttrs::Undef; |
1348 | 1.31k | } |
1349 | 155k | if (TOI.isClobbering(In, OpN)) |
1350 | 28.8k | Flags |= NodeAttrs::Clobbering; |
1351 | 155k | if (TOI.isFixedReg(In, OpN)) |
1352 | 155k | Flags |= NodeAttrs::Fixed; |
1353 | 155k | if (IsCall && Op.isDead()) { |
1354 | 36.1k | if (DoneClobbers.test(R)) |
1355 | 28.8k | continue; |
1356 | 7.31k | Flags |= NodeAttrs::Dead; |
1357 | 7.31k | } |
1358 | 126k | Def DA = newDef(SA, Op, Flags); |
1359 | 126k | SA.Addr->addMember(DA, *this); |
1360 | 126k | DoneDefs.set(R); |
1361 | 126k | } |
1362 | | |
1363 | 5.43M | for (unsigned OpN = 0; OpN < NumOps; ++OpN) { |
1364 | 4.18M | MachineOperand &Op = In.getOperand(OpN); |
1365 | 4.18M | if (!Op.isReg() || !Op.isUse()) |
1366 | 2.53M | continue; |
1367 | 1.64M | Register R = Op.getReg(); |
1368 | 1.64M | if (!R || !R.isPhysical() || !isTracked(RegisterRef(R))) |
1369 | 191k | continue; |
1370 | 1.45M | uint16_t Flags = NodeAttrs::None; |
1371 | 1.45M | if (Op.isUndef()) |
1372 | 970 | Flags |= NodeAttrs::Undef; |
1373 | 1.45M | if (TOI.isFixedReg(In, OpN)) |
1374 | 263k | Flags |= NodeAttrs::Fixed; |
1375 | 1.45M | Use UA = newUse(SA, Op, Flags); |
1376 | 1.45M | SA.Addr->addMember(UA, *this); |
1377 | 1.45M | } |
1378 | 1.25M | } |
1379 | | |
1380 | | // Scan all defs in the block node BA and record in PhiM the locations of |
1381 | | // phi nodes corresponding to these defs. |
1382 | 23.2k | void DataFlowGraph::recordDefsForDF(BlockRefsMap &PhiM, Block BA) { |
1383 | | // Check all defs from block BA and record them in each block in BA's |
1384 | | // iterated dominance frontier. This information will later be used to |
1385 | | // create phi nodes. |
1386 | 23.2k | MachineBasicBlock *BB = BA.Addr->getCode(); |
1387 | 23.2k | assert(BB); |
1388 | 0 | auto DFLoc = MDF.find(BB); |
1389 | 23.2k | if (DFLoc == MDF.end() || DFLoc->second.empty()) |
1390 | 19.2k | return; |
1391 | | |
1392 | | // Traverse all instructions in the block and collect the set of all |
1393 | | // defined references. For each reference there will be a phi created |
1394 | | // in the block's iterated dominance frontier. |
1395 | | // This is done to make sure that each defined reference gets only one |
1396 | | // phi node, even if it is defined multiple times. |
1397 | 4.08k | RegisterAggr Defs(getPRI()); |
1398 | 72.1k | for (Instr IA : BA.Addr->members(*this)) { |
1399 | 72.1k | for (Ref RA : IA.Addr->members_if(IsDef, *this)) { |
1400 | 54.2k | RegisterRef RR = RA.Addr->getRegRef(*this); |
1401 | 54.2k | if (RR.isReg() && isTracked(RR)) |
1402 | 51.4k | Defs.insert(RR); |
1403 | 54.2k | } |
1404 | 72.1k | } |
1405 | | |
1406 | | // Calculate the iterated dominance frontier of BB. |
1407 | 4.08k | const MachineDominanceFrontier::DomSetType &DF = DFLoc->second; |
1408 | 4.08k | SetVector<MachineBasicBlock *> IDF(DF.begin(), DF.end()); |
1409 | 8.17k | for (unsigned i = 0; i < IDF.size(); ++i) { |
1410 | 4.09k | auto F = MDF.find(IDF[i]); |
1411 | 4.09k | if (F != MDF.end()) |
1412 | 4.09k | IDF.insert(F->second.begin(), F->second.end()); |
1413 | 4.09k | } |
1414 | | |
1415 | | // Finally, add the set of defs to each block in the iterated dominance |
1416 | | // frontier. |
1417 | 4.09k | for (auto *DB : IDF) { |
1418 | 4.09k | Block DBA = findBlock(DB); |
1419 | 4.09k | PhiM[DBA.Id].insert(Defs); |
1420 | 4.09k | } |
1421 | 4.08k | } |
1422 | | |
1423 | | // Given the locations of phi nodes in the map PhiM, create the phi nodes |
1424 | | // that are located in the block node BA. |
1425 | 23.2k | void DataFlowGraph::buildPhis(BlockRefsMap &PhiM, Block BA) { |
1426 | | // Check if this blocks has any DF defs, i.e. if there are any defs |
1427 | | // that this block is in the iterated dominance frontier of. |
1428 | 23.2k | auto HasDF = PhiM.find(BA.Id); |
1429 | 23.2k | if (HasDF == PhiM.end() || HasDF->second.empty()) |
1430 | 19.7k | return; |
1431 | | |
1432 | | // Prepare a list of NodeIds of the block's predecessors. |
1433 | 3.55k | NodeList Preds; |
1434 | 3.55k | const MachineBasicBlock *MBB = BA.Addr->getCode(); |
1435 | 3.55k | for (MachineBasicBlock *PB : MBB->predecessors()) |
1436 | 7.47k | Preds.push_back(findBlock(PB)); |
1437 | | |
1438 | 3.55k | const RegisterAggr &Defs = PhiM[BA.Id]; |
1439 | 3.55k | uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving; |
1440 | | |
1441 | 31.9k | for (RegisterRef RR : Defs.refs()) { |
1442 | 31.9k | Phi PA = newPhi(BA); |
1443 | 31.9k | PA.Addr->addMember(newDef(PA, RR, PhiFlags), *this); |
1444 | | |
1445 | | // Add phi uses. |
1446 | 67.1k | for (Block PBA : Preds) { |
1447 | 67.1k | PA.Addr->addMember(newPhiUse(PA, RR, PBA), *this); |
1448 | 67.1k | } |
1449 | 31.9k | } |
1450 | 3.55k | } |
1451 | | |
1452 | | // Remove any unneeded phi nodes that were created during the build process. |
1453 | 19 | void DataFlowGraph::removeUnusedPhis() { |
1454 | | // This will remove unused phis, i.e. phis where each def does not reach |
1455 | | // any uses or other defs. This will not detect or remove circular phi |
1456 | | // chains that are otherwise dead. Unused/dead phis are created during |
1457 | | // the build process and this function is intended to remove these cases |
1458 | | // that are easily determinable to be unnecessary. |
1459 | | |
1460 | 19 | SetVector<NodeId> PhiQ; |
1461 | 19 | for (Block BA : TheFunc.Addr->members(*this)) { |
1462 | 19 | for (auto P : BA.Addr->members_if(IsPhi, *this)) |
1463 | 0 | PhiQ.insert(P.Id); |
1464 | 19 | } |
1465 | | |
1466 | 19 | static auto HasUsedDef = [](NodeList &Ms) -> bool { |
1467 | 0 | for (Node M : Ms) { |
1468 | 0 | if (M.Addr->getKind() != NodeAttrs::Def) |
1469 | 0 | continue; |
1470 | 0 | Def DA = M; |
1471 | 0 | if (DA.Addr->getReachedDef() != 0 || DA.Addr->getReachedUse() != 0) |
1472 | 0 | return true; |
1473 | 0 | } |
1474 | 0 | return false; |
1475 | 0 | }; |
1476 | | |
1477 | | // Any phi, if it is removed, may affect other phis (make them dead). |
1478 | | // For each removed phi, collect the potentially affected phis and add |
1479 | | // them back to the queue. |
1480 | 19 | while (!PhiQ.empty()) { |
1481 | 0 | auto PA = addr<PhiNode *>(PhiQ[0]); |
1482 | 0 | PhiQ.remove(PA.Id); |
1483 | 0 | NodeList Refs = PA.Addr->members(*this); |
1484 | 0 | if (HasUsedDef(Refs)) |
1485 | 0 | continue; |
1486 | 0 | for (Ref RA : Refs) { |
1487 | 0 | if (NodeId RD = RA.Addr->getReachingDef()) { |
1488 | 0 | auto RDA = addr<DefNode *>(RD); |
1489 | 0 | Instr OA = RDA.Addr->getOwner(*this); |
1490 | 0 | if (IsPhi(OA)) |
1491 | 0 | PhiQ.insert(OA.Id); |
1492 | 0 | } |
1493 | 0 | if (RA.Addr->isDef()) |
1494 | 0 | unlinkDef(RA, true); |
1495 | 0 | else |
1496 | 0 | unlinkUse(RA, true); |
1497 | 0 | } |
1498 | 0 | Block BA = PA.Addr->getOwner(*this); |
1499 | 0 | BA.Addr->removeMember(PA, *this); |
1500 | 0 | } |
1501 | 19 | } |
1502 | | |
1503 | | // For a given reference node TA in an instruction node IA, connect the |
1504 | | // reaching def of TA to the appropriate def node. Create any shadow nodes |
1505 | | // as appropriate. |
1506 | | template <typename T> |
1507 | 2.21M | void DataFlowGraph::linkRefUp(Instr IA, NodeAddr<T> TA, DefStack &DS) { |
1508 | 2.21M | if (DS.empty()) |
1509 | 9.12k | return; |
1510 | 2.20M | RegisterRef RR = TA.Addr->getRegRef(*this); |
1511 | 2.20M | NodeAddr<T> TAP; |
1512 | | |
1513 | | // References from the def stack that have been examined so far. |
1514 | 2.20M | RegisterAggr Defs(getPRI()); |
1515 | | |
1516 | 2.29M | for (auto I = DS.top(), E = DS.bottom(); I != E; I.down()) { |
1517 | 2.29M | RegisterRef QR = I->Addr->getRegRef(*this); |
1518 | | |
1519 | | // Skip all defs that are aliased to any of the defs that we have already |
1520 | | // seen. If this completes a cover of RR, stop the stack traversal. |
1521 | 2.29M | bool Alias = Defs.hasAliasOf(QR); |
1522 | 2.29M | bool Cover = Defs.insert(QR).hasCoverOf(RR); |
1523 | 2.29M | if (Alias) { |
1524 | 34.6k | if (Cover) |
1525 | 9.43k | break; |
1526 | 25.2k | continue; |
1527 | 34.6k | } |
1528 | | |
1529 | | // The reaching def. |
1530 | 2.25M | Def RDA = *I; |
1531 | | |
1532 | | // Pick the reached node. |
1533 | 2.25M | if (TAP.Id == 0) { |
1534 | 2.20M | TAP = TA; |
1535 | 2.20M | } else { |
1536 | | // Mark the existing ref as "shadow" and create a new shadow. |
1537 | 53.8k | TAP.Addr->setFlags(TAP.Addr->getFlags() | NodeAttrs::Shadow); |
1538 | 53.8k | TAP = getNextShadow(IA, TAP, true); |
1539 | 53.8k | } |
1540 | | |
1541 | | // Create the link. |
1542 | 2.25M | TAP.Addr->linkToDef(TAP.Id, RDA); |
1543 | | |
1544 | 2.25M | if (Cover) |
1545 | 2.19M | break; |
1546 | 2.25M | } |
1547 | 2.20M | } void llvm::rdf::DataFlowGraph::linkRefUp<llvm::rdf::DefNode*>(llvm::rdf::NodeAddr<llvm::rdf::InstrNode*>, llvm::rdf::NodeAddr<llvm::rdf::DefNode*>, llvm::rdf::DataFlowGraph::DefStack&) Line | Count | Source | 1507 | 765k | void DataFlowGraph::linkRefUp(Instr IA, NodeAddr<T> TA, DefStack &DS) { | 1508 | 765k | if (DS.empty()) | 1509 | 0 | return; | 1510 | 765k | RegisterRef RR = TA.Addr->getRegRef(*this); | 1511 | 765k | NodeAddr<T> TAP; | 1512 | | | 1513 | | // References from the def stack that have been examined so far. | 1514 | 765k | RegisterAggr Defs(getPRI()); | 1515 | | | 1516 | 810k | for (auto I = DS.top(), E = DS.bottom(); I != E; I.down()) { | 1517 | 806k | RegisterRef QR = I->Addr->getRegRef(*this); | 1518 | | | 1519 | | // Skip all defs that are aliased to any of the defs that we have already | 1520 | | // seen. If this completes a cover of RR, stop the stack traversal. | 1521 | 806k | bool Alias = Defs.hasAliasOf(QR); | 1522 | 806k | bool Cover = Defs.insert(QR).hasCoverOf(RR); | 1523 | 806k | if (Alias) { | 1524 | 18.9k | if (Cover) | 1525 | 7.40k | break; | 1526 | 11.5k | continue; | 1527 | 18.9k | } | 1528 | | | 1529 | | // The reaching def. | 1530 | 787k | Def RDA = *I; | 1531 | | | 1532 | | // Pick the reached node. | 1533 | 787k | if (TAP.Id == 0) { | 1534 | 765k | TAP = TA; | 1535 | 765k | } else { | 1536 | | // Mark the existing ref as "shadow" and create a new shadow. | 1537 | 21.8k | TAP.Addr->setFlags(TAP.Addr->getFlags() | NodeAttrs::Shadow); | 1538 | 21.8k | TAP = getNextShadow(IA, TAP, true); | 1539 | 21.8k | } | 1540 | | | 1541 | | // Create the link. | 1542 | 787k | TAP.Addr->linkToDef(TAP.Id, RDA); | 1543 | | | 1544 | 787k | if (Cover) | 1545 | 754k | break; | 1546 | 787k | } | 1547 | 765k | } |
void llvm::rdf::DataFlowGraph::linkRefUp<llvm::rdf::UseNode*>(llvm::rdf::NodeAddr<llvm::rdf::InstrNode*>, llvm::rdf::NodeAddr<llvm::rdf::UseNode*>, llvm::rdf::DataFlowGraph::DefStack&) Line | Count | Source | 1507 | 1.44M | void DataFlowGraph::linkRefUp(Instr IA, NodeAddr<T> TA, DefStack &DS) { | 1508 | 1.44M | if (DS.empty()) | 1509 | 9.12k | return; | 1510 | 1.43M | RegisterRef RR = TA.Addr->getRegRef(*this); | 1511 | 1.43M | NodeAddr<T> TAP; | 1512 | | | 1513 | | // References from the def stack that have been examined so far. | 1514 | 1.43M | RegisterAggr Defs(getPRI()); | 1515 | | | 1516 | 1.48M | for (auto I = DS.top(), E = DS.bottom(); I != E; I.down()) { | 1517 | 1.48M | RegisterRef QR = I->Addr->getRegRef(*this); | 1518 | | | 1519 | | // Skip all defs that are aliased to any of the defs that we have already | 1520 | | // seen. If this completes a cover of RR, stop the stack traversal. | 1521 | 1.48M | bool Alias = Defs.hasAliasOf(QR); | 1522 | 1.48M | bool Cover = Defs.insert(QR).hasCoverOf(RR); | 1523 | 1.48M | if (Alias) { | 1524 | 15.6k | if (Cover) | 1525 | 2.02k | break; | 1526 | 13.6k | continue; | 1527 | 15.6k | } | 1528 | | | 1529 | | // The reaching def. | 1530 | 1.47M | Def RDA = *I; | 1531 | | | 1532 | | // Pick the reached node. | 1533 | 1.47M | if (TAP.Id == 0) { | 1534 | 1.43M | TAP = TA; | 1535 | 1.43M | } else { | 1536 | | // Mark the existing ref as "shadow" and create a new shadow. | 1537 | 31.9k | TAP.Addr->setFlags(TAP.Addr->getFlags() | NodeAttrs::Shadow); | 1538 | 31.9k | TAP = getNextShadow(IA, TAP, true); | 1539 | 31.9k | } | 1540 | | | 1541 | | // Create the link. | 1542 | 1.47M | TAP.Addr->linkToDef(TAP.Id, RDA); | 1543 | | | 1544 | 1.47M | if (Cover) | 1545 | 1.43M | break; | 1546 | 1.47M | } | 1547 | 1.43M | } |
|
1548 | | |
1549 | | // Create data-flow links for all reference nodes in the statement node SA. |
1550 | | template <typename Predicate> |
1551 | 3.76M | void DataFlowGraph::linkStmtRefs(DefStackMap &DefM, Stmt SA, Predicate P) { |
1552 | 3.76M | #ifndef NDEBUG |
1553 | 3.76M | RegisterSet Defs(getPRI()); |
1554 | 3.76M | #endif |
1555 | | |
1556 | | // Link all nodes (upwards in the data-flow) with their reaching defs. |
1557 | 3.76M | for (Ref RA : SA.Addr->members_if(P, *this)) { |
1558 | 2.45M | uint16_t Kind = RA.Addr->getKind(); |
1559 | 2.45M | assert(Kind == NodeAttrs::Def || Kind == NodeAttrs::Use); |
1560 | 0 | RegisterRef RR = RA.Addr->getRegRef(*this); |
1561 | 2.45M | #ifndef NDEBUG |
1562 | | // Do not expect multiple defs of the same reference. |
1563 | 2.45M | assert(Kind != NodeAttrs::Def || !Defs.count(RR)); |
1564 | 0 | Defs.insert(RR); |
1565 | 2.45M | #endif |
1566 | | |
1567 | 2.45M | auto F = DefM.find(RR.Reg); |
1568 | 2.45M | if (F == DefM.end()) |
1569 | 305k | continue; |
1570 | 2.14M | DefStack &DS = F->second; |
1571 | 2.14M | if (Kind == NodeAttrs::Use) |
1572 | 1.38M | linkRefUp<UseNode *>(SA, RA, DS); |
1573 | 765k | else if (Kind == NodeAttrs::Def) |
1574 | 765k | linkRefUp<DefNode *>(SA, RA, DS); |
1575 | 0 | else |
1576 | 0 | llvm_unreachable("Unexpected node in instruction"); |
1577 | 2.14M | } |
1578 | 3.76M | } void llvm::rdf::DataFlowGraph::linkStmtRefs<bool (*)(llvm::rdf::NodeAddr<llvm::rdf::NodeBase*>)>(std::__1::unordered_map<unsigned int, llvm::rdf::DataFlowGraph::DefStack, std::__1::hash<unsigned int>, std::__1::equal_to<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, llvm::rdf::DataFlowGraph::DefStack> > >&, llvm::rdf::NodeAddr<llvm::rdf::StmtNode*>, bool (*)(llvm::rdf::NodeAddr<llvm::rdf::NodeBase*>)) Line | Count | Source | 1551 | 1.25M | void DataFlowGraph::linkStmtRefs(DefStackMap &DefM, Stmt SA, Predicate P) { | 1552 | 1.25M | #ifndef NDEBUG | 1553 | 1.25M | RegisterSet Defs(getPRI()); | 1554 | 1.25M | #endif | 1555 | | | 1556 | | // Link all nodes (upwards in the data-flow) with their reaching defs. | 1557 | 1.45M | for (Ref RA : SA.Addr->members_if(P, *this)) { | 1558 | 1.45M | uint16_t Kind = RA.Addr->getKind(); | 1559 | 1.45M | assert(Kind == NodeAttrs::Def || Kind == NodeAttrs::Use); | 1560 | 0 | RegisterRef RR = RA.Addr->getRegRef(*this); | 1561 | 1.45M | #ifndef NDEBUG | 1562 | | // Do not expect multiple defs of the same reference. | 1563 | 1.45M | assert(Kind != NodeAttrs::Def || !Defs.count(RR)); | 1564 | 0 | Defs.insert(RR); | 1565 | 1.45M | #endif | 1566 | | | 1567 | 1.45M | auto F = DefM.find(RR.Reg); | 1568 | 1.45M | if (F == DefM.end()) | 1569 | 76.6k | continue; | 1570 | 1.38M | DefStack &DS = F->second; | 1571 | 1.38M | if (Kind == NodeAttrs::Use) | 1572 | 1.38M | linkRefUp<UseNode *>(SA, RA, DS); | 1573 | 0 | else if (Kind == NodeAttrs::Def) | 1574 | 0 | linkRefUp<DefNode *>(SA, RA, DS); | 1575 | 0 | else | 1576 | 0 | llvm_unreachable("Unexpected node in instruction"); | 1577 | 1.38M | } | 1578 | 1.25M | } |
RDFGraph.cpp:void llvm::rdf::DataFlowGraph::linkStmtRefs<llvm::rdf::DataFlowGraph::linkBlockRefs(std::__1::unordered_map<unsigned int, llvm::rdf::DataFlowGraph::DefStack, std::__1::hash<unsigned int>, std::__1::equal_to<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, llvm::rdf::DataFlowGraph::DefStack> > >&, llvm::rdf::NodeAddr<llvm::rdf::BlockNode*>)::$_12>(std::__1::unordered_map<unsigned int, llvm::rdf::DataFlowGraph::DefStack, std::__1::hash<unsigned int>, std::__1::equal_to<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, llvm::rdf::DataFlowGraph::DefStack> > >&, llvm::rdf::NodeAddr<llvm::rdf::StmtNode*>, llvm::rdf::DataFlowGraph::linkBlockRefs(std::__1::unordered_map<unsigned int, llvm::rdf::DataFlowGraph::DefStack, std::__1::hash<unsigned int>, std::__1::equal_to<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, llvm::rdf::DataFlowGraph::DefStack> > >&, llvm::rdf::NodeAddr<llvm::rdf::BlockNode*>)::$_12) Line | Count | Source | 1551 | 1.25M | void DataFlowGraph::linkStmtRefs(DefStackMap &DefM, Stmt SA, Predicate P) { | 1552 | 1.25M | #ifndef NDEBUG | 1553 | 1.25M | RegisterSet Defs(getPRI()); | 1554 | 1.25M | #endif | 1555 | | | 1556 | | // Link all nodes (upwards in the data-flow) with their reaching defs. | 1557 | 1.25M | for (Ref RA : SA.Addr->members_if(P, *this)) { | 1558 | 28.7k | uint16_t Kind = RA.Addr->getKind(); | 1559 | 28.7k | assert(Kind == NodeAttrs::Def || Kind == NodeAttrs::Use); | 1560 | 0 | RegisterRef RR = RA.Addr->getRegRef(*this); | 1561 | 28.7k | #ifndef NDEBUG | 1562 | | // Do not expect multiple defs of the same reference. | 1563 | 28.7k | assert(Kind != NodeAttrs::Def || !Defs.count(RR)); | 1564 | 0 | Defs.insert(RR); | 1565 | 28.7k | #endif | 1566 | | | 1567 | 28.7k | auto F = DefM.find(RR.Reg); | 1568 | 28.7k | if (F == DefM.end()) | 1569 | 11.3k | continue; | 1570 | 17.4k | DefStack &DS = F->second; | 1571 | 17.4k | if (Kind == NodeAttrs::Use) | 1572 | 0 | linkRefUp<UseNode *>(SA, RA, DS); | 1573 | 17.4k | else if (Kind == NodeAttrs::Def) | 1574 | 17.4k | linkRefUp<DefNode *>(SA, RA, DS); | 1575 | 0 | else | 1576 | 0 | llvm_unreachable("Unexpected node in instruction"); | 1577 | 17.4k | } | 1578 | 1.25M | } |
RDFGraph.cpp:void llvm::rdf::DataFlowGraph::linkStmtRefs<llvm::rdf::DataFlowGraph::linkBlockRefs(std::__1::unordered_map<unsigned int, llvm::rdf::DataFlowGraph::DefStack, std::__1::hash<unsigned int>, std::__1::equal_to<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, llvm::rdf::DataFlowGraph::DefStack> > >&, llvm::rdf::NodeAddr<llvm::rdf::BlockNode*>)::$_13>(std::__1::unordered_map<unsigned int, llvm::rdf::DataFlowGraph::DefStack, std::__1::hash<unsigned int>, std::__1::equal_to<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, llvm::rdf::DataFlowGraph::DefStack> > >&, llvm::rdf::NodeAddr<llvm::rdf::StmtNode*>, llvm::rdf::DataFlowGraph::linkBlockRefs(std::__1::unordered_map<unsigned int, llvm::rdf::DataFlowGraph::DefStack, std::__1::hash<unsigned int>, std::__1::equal_to<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, llvm::rdf::DataFlowGraph::DefStack> > >&, llvm::rdf::NodeAddr<llvm::rdf::BlockNode*>)::$_13) Line | Count | Source | 1551 | 1.25M | void DataFlowGraph::linkStmtRefs(DefStackMap &DefM, Stmt SA, Predicate P) { | 1552 | 1.25M | #ifndef NDEBUG | 1553 | 1.25M | RegisterSet Defs(getPRI()); | 1554 | 1.25M | #endif | 1555 | | | 1556 | | // Link all nodes (upwards in the data-flow) with their reaching defs. | 1557 | 1.25M | for (Ref RA : SA.Addr->members_if(P, *this)) { | 1558 | 965k | uint16_t Kind = RA.Addr->getKind(); | 1559 | 965k | assert(Kind == NodeAttrs::Def || Kind == NodeAttrs::Use); | 1560 | 0 | RegisterRef RR = RA.Addr->getRegRef(*this); | 1561 | 965k | #ifndef NDEBUG | 1562 | | // Do not expect multiple defs of the same reference. | 1563 | 965k | assert(Kind != NodeAttrs::Def || !Defs.count(RR)); | 1564 | 0 | Defs.insert(RR); | 1565 | 965k | #endif | 1566 | | | 1567 | 965k | auto F = DefM.find(RR.Reg); | 1568 | 965k | if (F == DefM.end()) | 1569 | 217k | continue; | 1570 | 748k | DefStack &DS = F->second; | 1571 | 748k | if (Kind == NodeAttrs::Use) | 1572 | 0 | linkRefUp<UseNode *>(SA, RA, DS); | 1573 | 748k | else if (Kind == NodeAttrs::Def) | 1574 | 748k | linkRefUp<DefNode *>(SA, RA, DS); | 1575 | 0 | else | 1576 | 0 | llvm_unreachable("Unexpected node in instruction"); | 1577 | 748k | } | 1578 | 1.25M | } |
|
1579 | | |
1580 | | // Create data-flow links for all instructions in the block node BA. This |
1581 | | // will include updating any phi nodes in BA. |
1582 | 23.2k | void DataFlowGraph::linkBlockRefs(DefStackMap &DefM, Block BA) { |
1583 | | // Push block delimiters. |
1584 | 23.2k | markBlock(BA.Id, DefM); |
1585 | | |
1586 | 2.48M | auto IsClobber = [](Ref RA) -> bool { |
1587 | 2.48M | return IsDef(RA) && (RA.Addr->getFlags() & NodeAttrs::Clobbering); |
1588 | 2.48M | }; |
1589 | 2.48M | auto IsNoClobber = [](Ref RA) -> bool { |
1590 | 2.48M | return IsDef(RA) && !(RA.Addr->getFlags() & NodeAttrs::Clobbering); |
1591 | 2.48M | }; |
1592 | | |
1593 | 23.2k | assert(BA.Addr && "block node address is needed to create a data-flow link"); |
1594 | | // For each non-phi instruction in the block, link all the defs and uses |
1595 | | // to their reaching defs. For any member of the block (including phis), |
1596 | | // push the defs on the corresponding stacks. |
1597 | 1.31M | for (Instr IA : BA.Addr->members(*this)) { |
1598 | | // Ignore phi nodes here. They will be linked part by part from the |
1599 | | // predecessors. |
1600 | 1.31M | if (IA.Addr->getKind() == NodeAttrs::Stmt) { |
1601 | 1.25M | linkStmtRefs(DefM, IA, IsUse); |
1602 | 1.25M | linkStmtRefs(DefM, IA, IsClobber); |
1603 | 1.25M | } |
1604 | | |
1605 | | // Push the definitions on the stack. |
1606 | 1.31M | pushClobbers(IA, DefM); |
1607 | | |
1608 | 1.31M | if (IA.Addr->getKind() == NodeAttrs::Stmt) |
1609 | 1.25M | linkStmtRefs(DefM, IA, IsNoClobber); |
1610 | | |
1611 | 1.31M | pushDefs(IA, DefM); |
1612 | 1.31M | } |
1613 | | |
1614 | | // Recursively process all children in the dominator tree. |
1615 | 23.2k | MachineDomTreeNode *N = MDT.getNode(BA.Addr->getCode()); |
1616 | 23.2k | for (auto *I : *N) { |
1617 | 6.33k | MachineBasicBlock *SB = I->getBlock(); |
1618 | 6.33k | Block SBA = findBlock(SB); |
1619 | 6.33k | linkBlockRefs(DefM, SBA); |
1620 | 6.33k | } |
1621 | | |
1622 | | // Link the phi uses from the successor blocks. |
1623 | 211k | auto IsUseForBA = [BA](Node NA) -> bool { |
1624 | 211k | if (NA.Addr->getKind() != NodeAttrs::Use) |
1625 | 67.1k | return false; |
1626 | 144k | assert(NA.Addr->getFlags() & NodeAttrs::PhiRef); |
1627 | 0 | return PhiUse(NA).Addr->getPredecessor() == BA.Id; |
1628 | 211k | }; |
1629 | | |
1630 | 23.2k | RegisterAggr EHLiveIns = getLandingPadLiveIns(); |
1631 | 23.2k | MachineBasicBlock *MBB = BA.Addr->getCode(); |
1632 | | |
1633 | 23.2k | for (MachineBasicBlock *SB : MBB->successors()) { |
1634 | 10.4k | bool IsEHPad = SB->isEHPad(); |
1635 | 10.4k | Block SBA = findBlock(SB); |
1636 | 67.1k | for (Instr IA : SBA.Addr->members_if(IsPhi, *this)) { |
1637 | | // Do not link phi uses for landing pad live-ins. |
1638 | 67.1k | if (IsEHPad) { |
1639 | | // Find what register this phi is for. |
1640 | 0 | Ref RA = IA.Addr->getFirstMember(*this); |
1641 | 0 | assert(RA.Id != 0); |
1642 | 0 | if (EHLiveIns.hasCoverOf(RA.Addr->getRegRef(*this))) |
1643 | 0 | continue; |
1644 | 0 | } |
1645 | | // Go over each phi use associated with MBB, and link it. |
1646 | 67.1k | for (auto U : IA.Addr->members_if(IsUseForBA, *this)) { |
1647 | 67.1k | PhiUse PUA = U; |
1648 | 67.1k | RegisterRef RR = PUA.Addr->getRegRef(*this); |
1649 | 67.1k | linkRefUp<UseNode *>(IA, PUA, DefM[RR.Reg]); |
1650 | 67.1k | } |
1651 | 67.1k | } |
1652 | 10.4k | } |
1653 | | |
1654 | | // Pop all defs from this block from the definition stacks. |
1655 | 23.2k | releaseBlock(BA.Id, DefM); |
1656 | 23.2k | } |
1657 | | |
1658 | | // Remove the use node UA from any data-flow and structural links. |
1659 | 26.8k | void DataFlowGraph::unlinkUseDF(Use UA) { |
1660 | 26.8k | NodeId RD = UA.Addr->getReachingDef(); |
1661 | 26.8k | NodeId Sib = UA.Addr->getSibling(); |
1662 | | |
1663 | 26.8k | if (RD == 0) { |
1664 | 3.24k | assert(Sib == 0); |
1665 | 0 | return; |
1666 | 3.24k | } |
1667 | | |
1668 | 23.5k | auto RDA = addr<DefNode *>(RD); |
1669 | 23.5k | auto TA = addr<UseNode *>(RDA.Addr->getReachedUse()); |
1670 | 23.5k | if (TA.Id == UA.Id) { |
1671 | 19.8k | RDA.Addr->setReachedUse(Sib); |
1672 | 19.8k | return; |
1673 | 19.8k | } |
1674 | | |
1675 | 5.51k | while (TA.Id != 0) { |
1676 | 5.51k | NodeId S = TA.Addr->getSibling(); |
1677 | 5.51k | if (S == UA.Id) { |
1678 | 3.73k | TA.Addr->setSibling(UA.Addr->getSibling()); |
1679 | 3.73k | return; |
1680 | 3.73k | } |
1681 | 1.78k | TA = addr<UseNode *>(S); |
1682 | 1.78k | } |
1683 | 3.73k | } |
1684 | | |
1685 | | // Remove the def node DA from any data-flow and structural links. |
1686 | 12.9k | void DataFlowGraph::unlinkDefDF(Def DA) { |
1687 | | // |
1688 | | // RD |
1689 | | // | reached |
1690 | | // | def |
1691 | | // : |
1692 | | // . |
1693 | | // +----+ |
1694 | | // ... -- | DA | -- ... -- 0 : sibling chain of DA |
1695 | | // +----+ |
1696 | | // | | reached |
1697 | | // | : def |
1698 | | // | . |
1699 | | // | ... : Siblings (defs) |
1700 | | // | |
1701 | | // : reached |
1702 | | // . use |
1703 | | // ... : sibling chain of reached uses |
1704 | | |
1705 | 12.9k | NodeId RD = DA.Addr->getReachingDef(); |
1706 | | |
1707 | | // Visit all siblings of the reached def and reset their reaching defs. |
1708 | | // Also, defs reached by DA are now "promoted" to being reached by RD, |
1709 | | // so all of them will need to be spliced into the sibling chain where |
1710 | | // DA belongs. |
1711 | 25.9k | auto getAllNodes = [this](NodeId N) -> NodeList { |
1712 | 25.9k | NodeList Res; |
1713 | 37.6k | while (N) { |
1714 | 11.6k | auto RA = addr<RefNode *>(N); |
1715 | | // Keep the nodes in the exact sibling order. |
1716 | 11.6k | Res.push_back(RA); |
1717 | 11.6k | N = RA.Addr->getSibling(); |
1718 | 11.6k | } |
1719 | 25.9k | return Res; |
1720 | 25.9k | }; |
1721 | 12.9k | NodeList ReachedDefs = getAllNodes(DA.Addr->getReachedDef()); |
1722 | 12.9k | NodeList ReachedUses = getAllNodes(DA.Addr->getReachedUse()); |
1723 | | |
1724 | 12.9k | if (RD == 0) { |
1725 | 12.9k | for (Ref I : ReachedDefs) |
1726 | 11.6k | I.Addr->setSibling(0); |
1727 | 12.9k | for (Ref I : ReachedUses) |
1728 | 0 | I.Addr->setSibling(0); |
1729 | 12.9k | } |
1730 | 12.9k | for (Def I : ReachedDefs) |
1731 | 11.6k | I.Addr->setReachingDef(RD); |
1732 | 12.9k | for (Use I : ReachedUses) |
1733 | 0 | I.Addr->setReachingDef(RD); |
1734 | | |
1735 | 12.9k | NodeId Sib = DA.Addr->getSibling(); |
1736 | 12.9k | if (RD == 0) { |
1737 | 12.9k | assert(Sib == 0); |
1738 | 0 | return; |
1739 | 12.9k | } |
1740 | | |
1741 | | // Update the reaching def node and remove DA from the sibling list. |
1742 | 14 | auto RDA = addr<DefNode *>(RD); |
1743 | 14 | auto TA = addr<DefNode *>(RDA.Addr->getReachedDef()); |
1744 | 14 | if (TA.Id == DA.Id) { |
1745 | | // If DA is the first reached def, just update the RD's reached def |
1746 | | // to the DA's sibling. |
1747 | 13 | RDA.Addr->setReachedDef(Sib); |
1748 | 13 | } else { |
1749 | | // Otherwise, traverse the sibling list of the reached defs and remove |
1750 | | // DA from it. |
1751 | 11 | while (TA.Id != 0) { |
1752 | 11 | NodeId S = TA.Addr->getSibling(); |
1753 | 11 | if (S == DA.Id) { |
1754 | 1 | TA.Addr->setSibling(Sib); |
1755 | 1 | break; |
1756 | 1 | } |
1757 | 10 | TA = addr<DefNode *>(S); |
1758 | 10 | } |
1759 | 1 | } |
1760 | | |
1761 | | // Splice the DA's reached defs into the RDA's reached def chain. |
1762 | 14 | if (!ReachedDefs.empty()) { |
1763 | 5 | auto Last = Def(ReachedDefs.back()); |
1764 | 5 | Last.Addr->setSibling(RDA.Addr->getReachedDef()); |
1765 | 5 | RDA.Addr->setReachedDef(ReachedDefs.front().Id); |
1766 | 5 | } |
1767 | | // Splice the DA's reached uses into the RDA's reached use chain. |
1768 | 14 | if (!ReachedUses.empty()) { |
1769 | 0 | auto Last = Use(ReachedUses.back()); |
1770 | 0 | Last.Addr->setSibling(RDA.Addr->getReachedUse()); |
1771 | 0 | RDA.Addr->setReachedUse(ReachedUses.front().Id); |
1772 | 0 | } |
1773 | 14 | } |
1774 | | |
1775 | 33.3M | bool DataFlowGraph::isTracked(RegisterRef RR) const { |
1776 | 33.3M | return !disjoint(getPRI().getUnits(RR), TrackedUnits); |
1777 | 33.3M | } |
1778 | | |
1779 | 358k | bool DataFlowGraph::hasUntrackedRef(Stmt S, bool IgnoreReserved) const { |
1780 | 358k | SmallVector<MachineOperand *> Ops; |
1781 | | |
1782 | 734k | for (Ref R : S.Addr->members(*this)) { |
1783 | 734k | Ops.push_back(&R.Addr->getOp()); |
1784 | 734k | RegisterRef RR = R.Addr->getRegRef(*this); |
1785 | 734k | if (IgnoreReserved && RR.isReg() && ReservedRegs[RR.idx()]) |
1786 | 0 | continue; |
1787 | 734k | if (!isTracked(RR)) |
1788 | 0 | return true; |
1789 | 734k | } |
1790 | 1.06M | for (const MachineOperand &Op : S.Addr->getCode()->operands()) { |
1791 | 1.06M | if (!Op.isReg() && !Op.isRegMask()) |
1792 | 347k | continue; |
1793 | 712k | if (!llvm::is_contained(Ops, &Op)) |
1794 | 0 | return true; |
1795 | 712k | } |
1796 | 358k | return false; |
1797 | 358k | } |
1798 | | |
1799 | | } // end namespace llvm::rdf |