LLVM 23.0.0git
RDFLiveness.cpp
Go to the documentation of this file.
1//===- RDFLiveness.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// Computation of the liveness information from the data-flow graph.
10//
11// The main functionality of this code is to compute block live-in
12// information. With the live-in information in place, the placement
13// of kill flags can also be recalculated.
14//
15// The block live-in calculation is based on the ideas from the following
16// publication:
17//
18// Dibyendu Das, Ramakrishna Upadrasta, Benoit Dupont de Dinechin.
19// "Efficient Liveness Computation Using Merge Sets and DJ-Graphs."
20// ACM Transactions on Architecture and Code Optimization, Association for
21// Computing Machinery, 2012, ACM TACO Special Issue on "High-Performance
22// and Embedded Architectures and Compilers", 8 (4),
23// <10.1145/2086696.2086706>. <hal-00647369>
24//
25#include "llvm/ADT/BitVector.h"
26#include "llvm/ADT/DenseMap.h"
27#include "llvm/ADT/STLExtras.h"
28#include "llvm/ADT/SetVector.h"
29#include "llvm/ADT/SmallSet.h"
39#include "llvm/MC/LaneBitmask.h"
44#include <algorithm>
45#include <cassert>
46#include <cstdint>
47#include <iterator>
48#include <map>
49#include <unordered_map>
50#include <utility>
51#include <vector>
52
53using namespace llvm;
54
55static cl::opt<unsigned> MaxRecNest("rdf-liveness-max-rec", cl::init(25),
57 cl::desc("Maximum recursion level"));
58
59namespace llvm::rdf {
60
62 OS << '{';
63 for (const auto &I : P.Obj) {
64 OS << ' ' << printReg(I.first, &P.G.getTRI()) << '{';
65 for (auto J = I.second.begin(), E = I.second.end(); J != E;) {
66 OS << Print(J->first, P.G) << PrintLaneMaskShort(J->second);
67 if (++J != E)
68 OS << ',';
69 }
70 OS << '}';
71 }
72 OS << " }";
73 return OS;
74}
75
76// The order in the returned sequence is the order of reaching defs in the
77// upward traversal: the first def is the closest to the given reference RefA,
78// the next one is further up, and so on.
79// The list ends at a reaching phi def, or when the reference from RefA is
80// covered by the defs in the list (see FullChain).
81// This function provides two modes of operation:
82// (1) Returning the sequence of reaching defs for a particular reference
83// node. This sequence will terminate at the first phi node [1].
84// (2) Returning a partial sequence of reaching defs, where the final goal
85// is to traverse past phi nodes to the actual defs arising from the code
86// itself.
87// In mode (2), the register reference for which the search was started
88// may be different from the reference node RefA, for which this call was
89// made, hence the argument RefRR, which holds the original register.
90// Also, some definitions may have already been encountered in a previous
91// call that will influence register covering. The register references
92// already defined are passed in through DefRRs.
93// In mode (1), the "continuation" considerations do not apply, and the
94// RefRR is the same as the register in RefA, and the set DefRRs is empty.
95//
96// [1] It is possible for multiple phi nodes to be included in the returned
97// sequence:
98// SubA = phi ...
99// SubB = phi ...
100// ... = SuperAB(rdef:SubA), SuperAB"(rdef:SubB)
101// However, these phi nodes are independent from one another in terms of
102// the data-flow.
103
105 NodeAddr<RefNode *> RefA, bool TopShadows,
106 bool FullChain,
107 const RegisterAggr &DefRRs) {
108 NodeList RDefs; // Return value.
111
112 // Dead defs will be treated as if they were live, since they are actually
113 // on the data-flow path. They cannot be ignored because even though they
114 // do not generate meaningful values, they still modify registers.
115
116 // If the reference is undefined, there is nothing to do.
117 if (RefA.Addr->getFlags() & NodeAttrs::Undef)
118 return RDefs;
119
120 // The initial queue should not have reaching defs for shadows. The
121 // whole point of a shadow is that it will have a reaching def that
122 // is not aliased to the reaching defs of the related shadows.
123 NodeId Start = RefA.Id;
124 auto SNA = DFG.addr<RefNode *>(Start);
125 if (NodeId RD = SNA.Addr->getReachingDef())
126 DefQ.insert(RD);
127 if (TopShadows) {
128 for (auto S : DFG.getRelatedRefs(RefA.Addr->getOwner(DFG), RefA))
129 if (NodeId RD = NodeAddr<RefNode *>(S).Addr->getReachingDef())
130 DefQ.insert(RD);
131 }
132
133 // Collect all the reaching defs, going up until a phi node is encountered,
134 // or there are no more reaching defs. From this set, the actual set of
135 // reaching defs will be selected.
136 // The traversal upwards must go on until a covering def is encountered.
137 // It is possible that a collection of non-covering (individually) defs
138 // will be sufficient, but keep going until a covering one is found.
139 for (unsigned i = 0; i < DefQ.size(); ++i) {
140 auto TA = DFG.addr<DefNode *>(DefQ[i]);
141 if (TA.Addr->getFlags() & NodeAttrs::PhiRef)
142 continue;
143 // Stop at the covering/overwriting def of the initial register reference.
144 RegisterRef RR = TA.Addr->getRegRef(DFG);
145 if (!DFG.IsPreservingDef(TA))
146 if (RegisterAggr::isCoverOf(RR, RefRR, PRI))
147 continue;
148 // Get the next level of reaching defs. This will include multiple
149 // reaching defs for shadows.
150 for (auto S : DFG.getRelatedRefs(TA.Addr->getOwner(DFG), TA))
151 if (NodeId RD = NodeAddr<RefNode *>(S).Addr->getReachingDef())
152 DefQ.insert(RD);
153 // Don't visit sibling defs. They share the same reaching def (which
154 // will be visited anyway), but they define something not aliased to
155 // this ref.
156 }
157
158 // Return the MachineBasicBlock containing a given instruction.
159 auto Block = [this](NodeAddr<InstrNode *> IA) -> MachineBasicBlock * {
160 if (IA.Addr->getKind() == NodeAttrs::Stmt)
161 return NodeAddr<StmtNode *>(IA).Addr->getCode()->getParent();
162 assert(IA.Addr->getKind() == NodeAttrs::Phi);
163 NodeAddr<PhiNode *> PA = IA;
164 NodeAddr<BlockNode *> BA = PA.Addr->getOwner(DFG);
165 return BA.Addr->getCode();
166 };
167
169
170 // Remove all non-phi defs that are not aliased to RefRR, and separate
171 // the the remaining defs into buckets for containing blocks.
172 std::map<NodeId, NodeAddr<InstrNode *>> Owners;
173 std::map<MachineBasicBlock *, SmallVector<NodeId, 32>> Blocks;
174 for (NodeId N : DefQ) {
175 auto TA = DFG.addr<DefNode *>(N);
176 bool IsPhi = TA.Addr->getFlags() & NodeAttrs::PhiRef;
177 if (!IsPhi && !PRI.alias(RefRR, TA.Addr->getRegRef(DFG)))
178 continue;
179 Defs.insert(TA.Id);
180 NodeAddr<InstrNode *> IA = TA.Addr->getOwner(DFG);
181 Owners[TA.Id] = IA;
182 Blocks[Block(IA)].push_back(IA.Id);
183 }
184
185 auto Precedes = [this, &OrdMap](NodeId A, NodeId B) {
186 if (A == B)
187 return false;
188 NodeAddr<InstrNode *> OA = DFG.addr<InstrNode *>(A);
189 NodeAddr<InstrNode *> OB = DFG.addr<InstrNode *>(B);
190 bool StmtA = OA.Addr->getKind() == NodeAttrs::Stmt;
191 bool StmtB = OB.Addr->getKind() == NodeAttrs::Stmt;
192 if (StmtA && StmtB) {
193 const MachineInstr *InA = NodeAddr<StmtNode *>(OA).Addr->getCode();
194 const MachineInstr *InB = NodeAddr<StmtNode *>(OB).Addr->getCode();
195 assert(InA->getParent() == InB->getParent());
196 auto FA = OrdMap.find(InA);
197 if (FA != OrdMap.end())
198 return FA->second < OrdMap.find(InB)->second;
199 const MachineBasicBlock *BB = InA->getParent();
200 for (auto It = BB->begin(), E = BB->end(); It != E; ++It) {
201 if (It == InA->getIterator())
202 return true;
203 if (It == InB->getIterator())
204 return false;
205 }
206 llvm_unreachable("InA and InB should be in the same block");
207 }
208 // One of them is a phi node.
209 if (!StmtA && !StmtB) {
210 // Both are phis, which are unordered. Break the tie by id numbers.
211 return A < B;
212 }
213 // Only one of them is a phi. Phis always precede statements.
214 return !StmtA;
215 };
216
217 auto GetOrder = [&OrdMap](MachineBasicBlock &B) {
218 uint32_t Pos = 0;
219 for (MachineInstr &In : B)
220 OrdMap.insert({&In, ++Pos});
221 };
222
223 // For each block, sort the nodes in it.
224 std::vector<MachineBasicBlock *> TmpBB;
225 for (auto &Bucket : Blocks) {
226 TmpBB.push_back(Bucket.first);
227 if (Bucket.second.size() > 2)
228 GetOrder(*Bucket.first);
229 llvm::sort(Bucket.second, Precedes);
230 }
231
232 // Sort the blocks with respect to dominance.
233 llvm::sort(TmpBB,
234 [this](auto A, auto B) { return MDT.properlyDominates(A, B); });
235
236 std::vector<NodeId> TmpInst;
237 for (MachineBasicBlock *MBB : llvm::reverse(TmpBB)) {
238 auto &Bucket = Blocks[MBB];
239 TmpInst.insert(TmpInst.end(), Bucket.rbegin(), Bucket.rend());
240 }
241
242 // The vector is a list of instructions, so that defs coming from
243 // the same instruction don't need to be artificially ordered.
244 // Then, when computing the initial segment, and iterating over an
245 // instruction, pick the defs that contribute to the covering (i.e. is
246 // not covered by previously added defs). Check the defs individually,
247 // i.e. first check each def if is covered or not (without adding them
248 // to the tracking set), and then add all the selected ones.
249
250 // The reason for this is this example:
251 // *d1<A>, *d2<B>, ... Assume A and B are aliased (can happen in phi nodes).
252 // *d3<C> If A \incl BuC, and B \incl AuC, then *d2 would be
253 // covered if we added A first, and A would be covered
254 // if we added B first.
255 // In this example we want both A and B, because we don't want to give
256 // either one priority over the other, since they belong to the same
257 // statement.
258
259 RegisterAggr RRs(DefRRs);
260
261 auto DefInSet = [&Defs](NodeAddr<RefNode *> TA) -> bool {
262 return TA.Addr->getKind() == NodeAttrs::Def && Defs.count(TA.Id);
263 };
264
265 for (NodeId T : TmpInst) {
266 if (!FullChain && RRs.hasCoverOf(RefRR))
267 break;
268 auto TA = DFG.addr<InstrNode *>(T);
269 bool IsPhi = DFG.IsCode<NodeAttrs::Phi>(TA);
270 NodeList Ds;
271 for (NodeAddr<DefNode *> DA : TA.Addr->members_if(DefInSet, DFG)) {
272 RegisterRef QR = DA.Addr->getRegRef(DFG);
273 // Add phi defs even if they are covered by subsequent defs. This is
274 // for cases where the reached use is not covered by any of the defs
275 // encountered so far: the phi def is needed to expose the liveness
276 // of that use to the entry of the block.
277 // Example:
278 // phi d1<R3>(,d2,), ... Phi def d1 is covered by d2.
279 // d2<R3>(d1,,u3), ...
280 // ..., u3<D1>(d2) This use needs to be live on entry.
281 if (FullChain || IsPhi || !RRs.hasCoverOf(QR))
282 Ds.push_back(DA);
283 }
284 llvm::append_range(RDefs, Ds);
285 for (NodeAddr<DefNode *> DA : Ds) {
286 // When collecting a full chain of definitions, do not consider phi
287 // defs to actually define a register.
288 uint16_t Flags = DA.Addr->getFlags();
289 if (!FullChain || !(Flags & NodeAttrs::PhiRef))
290 if (!(Flags & NodeAttrs::Preserving)) // Don't care about Undef here.
291 RRs.insert(DA.Addr->getRegRef(DFG));
292 }
293 }
294
295 auto DeadP = [](const NodeAddr<DefNode *> DA) -> bool {
296 return DA.Addr->getFlags() & NodeAttrs::Dead;
297 };
298 llvm::erase_if(RDefs, DeadP);
299
300 return RDefs;
301}
302
303std::pair<NodeSet, bool>
305 NodeSet &Visited, const NodeSet &Defs) {
306 return getAllReachingDefsRecImpl(RefRR, RefA, Visited, Defs, 0, MaxRecNest);
307}
308
309std::pair<NodeSet, bool>
310Liveness::getAllReachingDefsRecImpl(RegisterRef RefRR, NodeAddr<RefNode *> RefA,
311 NodeSet &Visited, const NodeSet &Defs,
312 unsigned Nest, unsigned MaxNest) {
313 if (Nest > MaxNest)
314 return {NodeSet(), false};
315 // Collect all defined registers. Do not consider phis to be defining
316 // anything, only collect "real" definitions.
317 RegisterAggr DefRRs(PRI);
318 for (NodeId D : Defs) {
319 const auto DA = DFG.addr<const DefNode *>(D);
320 if (!(DA.Addr->getFlags() & NodeAttrs::PhiRef))
321 DefRRs.insert(DA.Addr->getRegRef(DFG));
322 }
323
324 NodeList RDs = getAllReachingDefs(RefRR, RefA, false, true, DefRRs);
325 if (RDs.empty())
326 return {Defs, true};
327
328 // Make a copy of the preexisting definitions and add the newly found ones.
329 NodeSet TmpDefs = Defs;
330 for (NodeAddr<NodeBase *> R : RDs)
331 TmpDefs.insert(R.Id);
332
333 NodeSet Result = Defs;
334
335 for (NodeAddr<DefNode *> DA : RDs) {
336 Result.insert(DA.Id);
337 if (!(DA.Addr->getFlags() & NodeAttrs::PhiRef))
338 continue;
339 NodeAddr<PhiNode *> PA = DA.Addr->getOwner(DFG);
340 if (!Visited.insert(PA.Id).second)
341 continue;
342 // Go over all phi uses and get the reaching defs for each use.
343 for (auto U : PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG)) {
344 const auto &T = getAllReachingDefsRecImpl(RefRR, U, Visited, TmpDefs,
345 Nest + 1, MaxNest);
346 if (!T.second)
347 return {T.first, false};
348 Result.insert(T.first.begin(), T.first.end());
349 }
350 }
351
352 return {Result, true};
353}
354
355/// Find the nearest ref node aliased to RefRR, going upwards in the data
356/// flow, starting from the instruction immediately preceding Inst.
359 NodeAddr<BlockNode *> BA = IA.Addr->getOwner(DFG);
360 NodeList Ins = BA.Addr->members(DFG);
361 NodeId FindId = IA.Id;
362 auto E = Ins.rend();
363 auto B =
364 std::find_if(Ins.rbegin(), E, [FindId](const NodeAddr<InstrNode *> T) {
365 return T.Id == FindId;
366 });
367 // Do not scan IA (which is what B would point to).
368 if (B != E)
369 ++B;
370
371 do {
372 // Process the range of instructions from B to E.
373 for (NodeAddr<InstrNode *> I : make_range(B, E)) {
374 NodeList Refs = I.Addr->members(DFG);
376 // Scan all the refs in I aliased to RefRR, and return the one that
377 // is the closest to the output of I, i.e. def > clobber > use.
378 for (NodeAddr<RefNode *> R : Refs) {
379 if (!PRI.alias(R.Addr->getRegRef(DFG), RefRR))
380 continue;
381 if (DFG.IsDef(R)) {
382 // If it's a non-clobbering def, just return it.
383 if (!(R.Addr->getFlags() & NodeAttrs::Clobbering))
384 return R;
385 Clob = R;
386 } else {
387 Use = R;
388 }
389 }
390 if (Clob.Id != 0)
391 return Clob;
392 if (Use.Id != 0)
393 return Use;
394 }
395
396 // Go up to the immediate dominator, if any.
397 MachineBasicBlock *BB = BA.Addr->getCode();
399 if (MachineDomTreeNode *N = MDT.getNode(BB)) {
400 if ((N = N->getIDom()))
401 BA = DFG.findBlock(N->getBlock());
402 }
403 if (!BA.Id)
404 break;
405
406 Ins = BA.Addr->members(DFG);
407 B = Ins.rbegin();
408 E = Ins.rend();
409 } while (true);
410
411 return NodeAddr<RefNode *>();
412}
413
415 const RegisterAggr &DefRRs) {
417
418 // If the original register is already covered by all the intervening
419 // defs, no more uses can be reached.
420 if (DefRRs.hasCoverOf(RefRR))
421 return Uses;
422
423 // Add all directly reached uses.
424 // If the def is dead, it does not provide a value for any use.
425 bool IsDead = DefA.Addr->getFlags() & NodeAttrs::Dead;
426 NodeId U = !IsDead ? DefA.Addr->getReachedUse() : 0;
427 while (U != 0) {
428 auto UA = DFG.addr<UseNode *>(U);
429 if (!(UA.Addr->getFlags() & NodeAttrs::Undef)) {
430 RegisterRef UR = UA.Addr->getRegRef(DFG);
431 if (PRI.alias(RefRR, UR) && !DefRRs.hasCoverOf(UR))
432 Uses.insert(U);
433 }
434 U = UA.Addr->getSibling();
435 }
436
437 // Traverse all reached defs. This time dead defs cannot be ignored.
438 for (NodeId D = DefA.Addr->getReachedDef(), NextD; D != 0; D = NextD) {
439 auto DA = DFG.addr<DefNode *>(D);
440 NextD = DA.Addr->getSibling();
441 RegisterRef DR = DA.Addr->getRegRef(DFG);
442 // If this def is already covered, it cannot reach anything new.
443 // Similarly, skip it if it is not aliased to the interesting register.
444 if (DefRRs.hasCoverOf(DR) || !PRI.alias(RefRR, DR))
445 continue;
446 NodeSet T;
447 if (DFG.IsPreservingDef(DA)) {
448 // If it is a preserving def, do not update the set of intervening defs.
449 T = getAllReachedUses(RefRR, DA, DefRRs);
450 } else {
451 RegisterAggr NewDefRRs = DefRRs;
452 NewDefRRs.insert(DR);
453 T = getAllReachedUses(RefRR, DA, NewDefRRs);
454 }
455 Uses.insert(T.begin(), T.end());
456 }
457 return Uses;
458}
459
461 RealUseMap.clear();
462
463 NodeList Phis;
464 NodeAddr<FuncNode *> FA = DFG.getFunc();
465 NodeList Blocks = FA.Addr->members(DFG);
466 for (NodeAddr<BlockNode *> BA : Blocks) {
467 auto Ps = BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG);
468 llvm::append_range(Phis, Ps);
469 }
470
471 // phi use -> (map: reaching phi -> set of registers defined in between)
472 std::map<NodeId, std::map<NodeId, RegisterAggr>> PhiUp;
473 std::vector<NodeId> PhiUQ; // Work list of phis for upward propagation.
474 DenseMap<NodeId, RegisterAggr> PhiDRs; // Phi -> registers defined by it.
475
476 // Go over all phis.
477 for (NodeAddr<PhiNode *> PhiA : Phis) {
478 // Go over all defs and collect the reached uses that are non-phi uses
479 // (i.e. the "real uses").
480 RefMap &RealUses = RealUseMap[PhiA.Id];
481 NodeList PhiRefs = PhiA.Addr->members(DFG);
482
483 // Have a work queue of defs whose reached uses need to be found.
484 // For each def, add to the queue all reached (non-phi) defs.
486 NodeSet PhiDefs;
487 RegisterAggr DRs(PRI);
488 for (NodeAddr<RefNode *> R : PhiRefs) {
489 if (!DFG.IsRef<NodeAttrs::Def>(R))
490 continue;
491 DRs.insert(R.Addr->getRegRef(DFG));
492 DefQ.insert(R.Id);
493 PhiDefs.insert(R.Id);
494 }
495 PhiDRs.insert(std::make_pair(PhiA.Id, DRs));
496
497 // Collect the super-set of all possible reached uses. This set will
498 // contain all uses reached from this phi, either directly from the
499 // phi defs, or (recursively) via non-phi defs reached by the phi defs.
500 // This set of uses will later be trimmed to only contain these uses that
501 // are actually reached by the phi defs.
502 for (unsigned i = 0; i < DefQ.size(); ++i) {
503 NodeAddr<DefNode *> DA = DFG.addr<DefNode *>(DefQ[i]);
504 // Visit all reached uses. Phi defs should not really have the "dead"
505 // flag set, but check it anyway for consistency.
506 bool IsDead = DA.Addr->getFlags() & NodeAttrs::Dead;
507 NodeId UN = !IsDead ? DA.Addr->getReachedUse() : 0;
508 while (UN != 0) {
509 NodeAddr<UseNode *> A = DFG.addr<UseNode *>(UN);
510 uint16_t F = A.Addr->getFlags();
511 if ((F & (NodeAttrs::Undef | NodeAttrs::PhiRef)) == 0) {
512 RegisterRef R = A.Addr->getRegRef(DFG);
513 RealUses[R.Id].insert({A.Id, R.Mask});
514 }
515 UN = A.Addr->getSibling();
516 }
517 // Visit all reached defs, and add them to the queue. These defs may
518 // override some of the uses collected here, but that will be handled
519 // later.
520 NodeId DN = DA.Addr->getReachedDef();
521 while (DN != 0) {
522 NodeAddr<DefNode *> A = DFG.addr<DefNode *>(DN);
523 for (auto T : DFG.getRelatedRefs(A.Addr->getOwner(DFG), A)) {
524 uint16_t Flags = NodeAddr<DefNode *>(T).Addr->getFlags();
525 // Must traverse the reached-def chain. Consider:
526 // def(D0) -> def(R0) -> def(R0) -> use(D0)
527 // The reachable use of D0 passes through a def of R0.
528 if (!(Flags & NodeAttrs::PhiRef))
529 DefQ.insert(T.Id);
530 }
531 DN = A.Addr->getSibling();
532 }
533 }
534 // Filter out these uses that appear to be reachable, but really
535 // are not. For example:
536 //
537 // R1:0 = d1
538 // = R1:0 u2 Reached by d1.
539 // R0 = d3
540 // = R1:0 u4 Still reached by d1: indirectly through
541 // the def d3.
542 // R1 = d5
543 // = R1:0 u6 Not reached by d1 (covered collectively
544 // by d3 and d5), but following reached
545 // defs and uses from d1 will lead here.
546 for (auto UI = RealUses.begin(), UE = RealUses.end(); UI != UE;) {
547 // For each reached register UI->first, there is a set UI->second, of
548 // uses of it. For each such use, check if it is reached by this phi,
549 // i.e. check if the set of its reaching uses intersects the set of
550 // this phi's defs.
551 NodeRefSet Uses = UI->second;
552 UI->second.clear();
553 for (std::pair<NodeId, LaneBitmask> I : Uses) {
554 auto UA = DFG.addr<UseNode *>(I.first);
555 // Undef flag is checked above.
556 assert((UA.Addr->getFlags() & NodeAttrs::Undef) == 0);
557 RegisterRef UseR(UI->first, I.second); // Ref from Uses
558 // R = intersection of the ref from the phi and the ref from Uses
559 RegisterRef R = PhiDRs.at(PhiA.Id).intersectWith(UseR);
560 if (!R)
561 continue;
562 // Calculate the exposed part of the reached use.
563 RegisterAggr Covered(PRI);
564 for (NodeAddr<DefNode *> DA : getAllReachingDefs(R, UA)) {
565 if (PhiDefs.count(DA.Id))
566 break;
567 Covered.insert(DA.Addr->getRegRef(DFG));
568 }
569 if (RegisterRef RC = Covered.clearIn(R)) {
570 // We are updating the map for register UI->first, so we need
571 // to map RC to be expressed in terms of that register.
572 RegisterRef S = PRI.mapTo(RC, UI->first);
573 UI->second.insert({I.first, S.Mask});
574 }
575 }
576 ++UI;
577 }
578 // Drop the entries whose use sets ended up empty.
579 emptify(RealUses);
580
581 // If this phi reaches some "real" uses, add it to the queue for upward
582 // propagation.
583 if (!RealUses.empty())
584 PhiUQ.push_back(PhiA.Id);
585
586 // Go over all phi uses and check if the reaching def is another phi.
587 // Collect the phis that are among the reaching defs of these uses.
588 // While traversing the list of reaching defs for each phi use, accumulate
589 // the set of registers defined between this phi (PhiA) and the owner phi
590 // of the reaching def.
591 NodeSet SeenUses;
592
593 for (auto I : PhiRefs) {
594 if (!DFG.IsRef<NodeAttrs::Use>(I) || SeenUses.count(I.Id))
595 continue;
597 if (PUA.Addr->getReachingDef() == 0)
598 continue;
599
600 RegisterRef UR = PUA.Addr->getRegRef(DFG);
601 NodeList Ds = getAllReachingDefs(UR, PUA, true, false, NoRegs);
602 RegisterAggr DefRRs(PRI);
603
604 for (NodeAddr<DefNode *> D : Ds) {
605 if (D.Addr->getFlags() & NodeAttrs::PhiRef) {
606 NodeId RP = D.Addr->getOwner(DFG).Id;
607 auto [F, Inserted] = PhiUp[PUA.Id].try_emplace(RP, DefRRs);
608 if (!Inserted)
609 F->second.insert(DefRRs);
610 }
611 DefRRs.insert(D.Addr->getRegRef(DFG));
612 }
613
614 for (NodeAddr<PhiUseNode *> T : DFG.getRelatedRefs(PhiA, PUA))
615 SeenUses.insert(T.Id);
616 }
617 }
618
619 if (Trace) {
620 dbgs() << "Phi-up-to-phi map with intervening defs:\n";
621 for (auto I : PhiUp) {
622 dbgs() << "phi " << Print(I.first, DFG) << " -> {";
623 for (auto R : I.second)
624 dbgs() << ' ' << Print(R.first, DFG) << Print(R.second, DFG);
625 dbgs() << " }\n";
626 }
627 }
628
629 // Propagate the reached registers up in the phi chain.
630 //
631 // The following type of situation needs careful handling:
632 //
633 // phi d1<R1:0> (1)
634 // |
635 // ... d2<R1>
636 // |
637 // phi u3<R1:0> (2)
638 // |
639 // ... u4<R1>
640 //
641 // The phi node (2) defines a register pair R1:0, and reaches a "real"
642 // use u4 of just R1. The same phi node is also known to reach (upwards)
643 // the phi node (1). However, the use u4 is not reached by phi (1),
644 // because of the intervening definition d2 of R1. The data flow between
645 // phis (1) and (2) is restricted to R1:0 minus R1, i.e. R0.
646 //
647 // When propagating uses up the phi chains, get the all reaching defs
648 // for a given phi use, and traverse the list until the propagated ref
649 // is covered, or until reaching the final phi. Only assume that the
650 // reference reaches the phi in the latter case.
651
652 // The operation "clearIn" can be expensive. For a given set of intervening
653 // defs, cache the result of subtracting these defs from a given register
654 // ref.
655 using RefHash = std::hash<RegisterRef>;
656 using RefEqual = RegisterRefEqualTo;
657 using SubMap =
658 std::unordered_map<RegisterRef, RegisterRef, RefHash, RefEqual>;
659 std::unordered_map<RegisterAggr, SubMap> Subs;
660 auto ClearIn = [](RegisterRef RR, const RegisterAggr &Mid, SubMap &SM) {
661 if (Mid.empty())
662 return RR;
663 auto F = SM.find(RR);
664 if (F != SM.end())
665 return F->second;
666 RegisterRef S = Mid.clearIn(RR);
667 SM.insert({RR, S});
668 return S;
669 };
670
671 // Go over all phis.
672 for (unsigned i = 0; i < PhiUQ.size(); ++i) {
673 auto PA = DFG.addr<PhiNode *>(PhiUQ[i]);
674 NodeList PUs = PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG);
675 // Make a copy of RealUseMap[PA.Id] to avoid iterator invalidation.
676 // The inner loop may insert new entries into RealUseMap (via operator[]
677 // on line "RealUseMap[P.first]"), which can trigger a DenseMap rehash
678 // and invalidate any references/iterators into the map.
679 RefMap RUM = RealUseMap[PA.Id];
680
681 for (NodeAddr<UseNode *> UA : PUs) {
682 std::map<NodeId, RegisterAggr> &PUM = PhiUp[UA.Id];
683 RegisterRef UR = UA.Addr->getRegRef(DFG);
684 for (const std::pair<const NodeId, RegisterAggr> &P : PUM) {
685 bool Changed = false;
686 const RegisterAggr &MidDefs = P.second;
687 // Collect the set PropUp of uses that are reached by the current
688 // phi PA, and are not covered by any intervening def between the
689 // currently visited use UA and the upward phi P.
690
691 if (MidDefs.hasCoverOf(UR))
692 continue;
693 SubMap &SM = Subs.try_emplace(MidDefs, 1, RefHash(), RefEqual(PRI))
694 .first->second;
695
696 // General algorithm:
697 // for each (R,U) : U is use node of R, U is reached by PA
698 // if MidDefs does not cover (R,U)
699 // then add (R-MidDefs,U) to RealUseMap[P]
700 //
701 for (const auto &T : RUM) {
702 RegisterRef R(T.first);
703 // The current phi (PA) could be a phi for a regmask. It could
704 // reach a whole variety of uses that are not related to the
705 // specific upward phi (P.first).
706 const RegisterAggr &DRs = PhiDRs.at(P.first);
707 if (!DRs.hasAliasOf(R))
708 continue;
709 R = PRI.mapTo(DRs.intersectWith(R), T.first);
710 for (std::pair<NodeId, LaneBitmask> V : T.second) {
711 LaneBitmask M = R.Mask & V.second;
712 if (M.none())
713 continue;
714 if (RegisterRef SS = ClearIn(RegisterRef(R.Id, M), MidDefs, SM)) {
715 NodeRefSet &RS = RealUseMap[P.first][SS.Id];
716 Changed |= RS.insert({V.first, SS.Mask}).second;
717 }
718 }
719 }
720
721 if (Changed)
722 PhiUQ.push_back(P.first);
723 }
724 }
725 }
726
727 if (Trace) {
728 dbgs() << "Real use map:\n";
729 for (auto I : RealUseMap) {
730 dbgs() << "phi " << Print(I.first, DFG);
731 NodeAddr<PhiNode *> PA = DFG.addr<PhiNode *>(I.first);
732 NodeList Ds = PA.Addr->members_if(DFG.IsRef<NodeAttrs::Def>, DFG);
733 if (!Ds.empty()) {
734 RegisterRef RR = NodeAddr<DefNode *>(Ds[0]).Addr->getRegRef(DFG);
735 dbgs() << '<' << Print(RR, DFG) << '>';
736 } else {
737 dbgs() << "<noreg>";
738 }
739 dbgs() << " -> " << Print(I.second, DFG) << '\n';
740 }
741 }
742}
743
745 // Populate the node-to-block map. This speeds up the calculations
746 // significantly.
747 NBMap.clear();
748 for (NodeAddr<BlockNode *> BA : DFG.getFunc().Addr->members(DFG)) {
749 MachineBasicBlock *BB = BA.Addr->getCode();
750 for (NodeAddr<InstrNode *> IA : BA.Addr->members(DFG)) {
751 for (NodeAddr<RefNode *> RA : IA.Addr->members(DFG))
752 NBMap.insert(std::make_pair(RA.Id, BB));
753 NBMap.insert(std::make_pair(IA.Id, BB));
754 }
755 }
756
757 MachineFunction &MF = DFG.getMF();
758
759 // Compute IDF first, then the inverse.
760 decltype(IIDF) IDF;
761 for (MachineBasicBlock &B : MF) {
762 auto F1 = MDF.find(&B);
763 if (F1 == MDF.end())
764 continue;
766 for (unsigned i = 0; i < IDFB.size(); ++i) {
767 auto F2 = MDF.find(IDFB[i]);
768 if (F2 != MDF.end())
769 IDFB.insert_range(F2->second);
770 }
771 // Add B to the IDF(B). This will put B in the IIDF(B).
772 IDFB.insert(&B);
773 IDF[&B].insert(IDFB.begin(), IDFB.end());
774 }
775
776 for (auto I : IDF)
777 for (auto *S : I.second)
778 IIDF[S].insert(I.first);
779
781
782 NodeAddr<FuncNode *> FA = DFG.getFunc();
783 NodeList Blocks = FA.Addr->members(DFG);
784
785 // Build the phi live-on-entry map.
786 for (NodeAddr<BlockNode *> BA : Blocks) {
787 MachineBasicBlock *MB = BA.Addr->getCode();
788 RefMap &LON = PhiLON[MB];
789 for (auto P : BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG)) {
790 for (const RefMap::value_type &S : RealUseMap[P.Id])
791 LON[S.first].insert(S.second.begin(), S.second.end());
792 }
793 }
794
795 if (Trace) {
796 dbgs() << "Phi live-on-entry map:\n";
797 for (auto &I : PhiLON)
798 dbgs() << "block #" << I.first->getNumber() << " -> "
799 << Print(I.second, DFG) << '\n';
800 }
801
802 // Build the phi live-on-exit map. Each phi node has some set of reached
803 // "real" uses. Propagate this set backwards into the block predecessors
804 // through the reaching defs of the corresponding phi uses.
805 for (NodeAddr<BlockNode *> BA : Blocks) {
806 NodeList Phis = BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG);
807 for (NodeAddr<PhiNode *> PA : Phis) {
808 RefMap &RUs = RealUseMap[PA.Id];
809 if (RUs.empty())
810 continue;
811
812 NodeSet SeenUses;
813 for (auto U : PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG)) {
814 if (!SeenUses.insert(U.Id).second)
815 continue;
817 if (PUA.Addr->getReachingDef() == 0)
818 continue;
819
820 // Each phi has some set (possibly empty) of reached "real" uses,
821 // that is, uses that are part of the compiled program. Such a use
822 // may be located in some farther block, but following a chain of
823 // reaching defs will eventually lead to this phi.
824 // Any chain of reaching defs may fork at a phi node, but there
825 // will be a path upwards that will lead to this phi. Now, this
826 // chain will need to fork at this phi, since some of the reached
827 // uses may have definitions joining in from multiple predecessors.
828 // For each reached "real" use, identify the set of reaching defs
829 // coming from each predecessor P, and add them to PhiLOX[P].
830 //
831 auto PrA = DFG.addr<BlockNode *>(PUA.Addr->getPredecessor());
832 RefMap &LOX = PhiLOX[PrA.Addr->getCode()];
833
834 for (const auto &RS : RUs) {
835 // We need to visit each individual use.
836 for (std::pair<NodeId, LaneBitmask> P : RS.second) {
837 // Create a register ref corresponding to the use, and find
838 // all reaching defs starting from the phi use, and treating
839 // all related shadows as a single use cluster.
840 RegisterRef S(RS.first, P.second);
841 NodeList Ds = getAllReachingDefs(S, PUA, true, false, NoRegs);
842 for (NodeAddr<DefNode *> D : Ds) {
843 // Calculate the mask corresponding to the visited def.
844 RegisterAggr TA(PRI);
845 TA.insert(D.Addr->getRegRef(DFG)).intersect(S);
846 LaneBitmask TM = TA.makeRegRef().Mask;
847 LOX[S.Id].insert({D.Id, TM});
848 }
849 }
850 }
851
852 for (NodeAddr<PhiUseNode *> T : DFG.getRelatedRefs(PA, PUA))
853 SeenUses.insert(T.Id);
854 } // for U : phi uses
855 } // for P : Phis
856 } // for B : Blocks
857
858 if (Trace) {
859 dbgs() << "Phi live-on-exit map:\n";
860 for (auto &I : PhiLOX)
861 dbgs() << "block #" << I.first->getNumber() << " -> "
862 << Print(I.second, DFG) << '\n';
863 }
864
865 RefMap LiveIn;
866 traverse(&MF.front(), LiveIn);
867
868 // Add function live-ins to the live-in set of the function entry block.
869 LiveMap[&MF.front()].insert(DFG.getLiveIns());
870
871 if (Trace) {
872 // Dump the liveness map
873 for (MachineBasicBlock &B : MF) {
874 std::vector<RegisterRef> LV;
875 for (const MachineBasicBlock::RegisterMaskPair &LI : B.liveins())
876 LV.push_back(RegisterRef(LI.PhysReg, LI.LaneMask));
877 llvm::sort(LV, RegisterRefLess(PRI));
878 dbgs() << printMBBReference(B) << "\t rec = {";
879 for (auto I : LV)
880 dbgs() << ' ' << Print(I, DFG);
881 dbgs() << " }\n";
882 // dbgs() << "\tcomp = " << Print(LiveMap[&B], DFG) << '\n';
883
884 LV.clear();
885 for (RegisterRef RR : LiveMap[&B].refs())
886 LV.push_back(RR);
887 llvm::sort(LV, RegisterRefLess(PRI));
888 dbgs() << "\tcomp = {";
889 for (auto I : LV)
890 dbgs() << ' ' << Print(I, DFG);
891 dbgs() << " }\n";
892 }
893 }
894}
895
897 for (auto &B : DFG.getMF()) {
898 // Remove all live-ins.
899 std::vector<MCRegister> T;
900 for (const MachineBasicBlock::RegisterMaskPair &LI : B.liveins())
901 T.push_back(LI.PhysReg);
902 for (auto I : T)
903 B.removeLiveIn(I);
904 // Add the newly computed live-ins.
905 const RegisterAggr &LiveIns = LiveMap[&B];
906 for (RegisterRef R : LiveIns.refs())
907 B.addLiveIn({R.asMCReg(), R.Mask});
908 }
909}
910
912 for (auto &B : DFG.getMF())
913 resetKills(&B);
914}
915
917 auto CopyLiveIns = [this](MachineBasicBlock *B, BitVector &LV) -> void {
918 for (auto I : B->liveins()) {
919 MCSubRegIndexIterator S(I.PhysReg, &TRI);
920 if (!S.isValid()) {
921 LV.set(I.PhysReg.id());
922 continue;
923 }
924 do {
925 LaneBitmask M = TRI.getSubRegIndexLaneMask(S.getSubRegIndex());
926 if ((M & I.LaneMask).any())
927 LV.set(S.getSubReg());
928 ++S;
929 } while (S.isValid());
930 }
931 };
932
933 BitVector LiveIn(TRI.getNumRegs()), Live(TRI.getNumRegs());
934 CopyLiveIns(B, LiveIn);
935 for (auto *SI : B->successors())
936 CopyLiveIns(SI, Live);
937
938 for (MachineInstr &MI : llvm::reverse(*B)) {
939 if (MI.isDebugInstr())
940 continue;
941
942 MI.clearKillInfo();
943 for (auto &Op : MI.all_defs()) {
944 // An implicit def of a super-register may not necessarily start a
945 // live range of it, since an implicit use could be used to keep parts
946 // of it live. Instead of analyzing the implicit operands, ignore
947 // implicit defs.
948 if (Op.isImplicit())
949 continue;
950 Register R = Op.getReg();
951 if (!R.isPhysical())
952 continue;
953 for (MCPhysReg SR : TRI.subregs_inclusive(R))
954 Live.reset(SR);
955 }
956 for (auto &Op : MI.all_uses()) {
957 if (Op.isUndef())
958 continue;
959 Register R = Op.getReg();
960 if (!R.isPhysical())
961 continue;
962 bool IsLive = false;
963 for (MCRegAliasIterator AR(R, &TRI, true); AR.isValid(); ++AR) {
964 if (!Live[(*AR).id()])
965 continue;
966 IsLive = true;
967 break;
968 }
969 if (!IsLive)
970 Op.setIsKill(true);
971 for (MCPhysReg SR : TRI.subregs_inclusive(R))
972 Live.set(SR);
973 }
974 }
975}
976
977// Helper function to obtain the basic block containing the reaching def
978// of the given use.
979MachineBasicBlock *Liveness::getBlockWithRef(NodeId RN) const {
980 auto F = NBMap.find(RN);
981 if (F != NBMap.end())
982 return F->second;
983 llvm_unreachable("Node id not in map");
984}
985
986void Liveness::traverse(MachineBasicBlock *B, RefMap &LiveIn) {
987 // The LiveIn map, for each (physical) register, contains the set of live
988 // reaching defs of that register that are live on entry to the associated
989 // block.
990
991 // The summary of the traversal algorithm:
992 //
993 // R is live-in in B, if there exists a U(R), such that rdef(R) dom B
994 // and (U \in IDF(B) or B dom U).
995 //
996 // for (C : children) {
997 // LU = {}
998 // traverse(C, LU)
999 // LiveUses += LU
1000 // }
1001 //
1002 // LiveUses -= Defs(B);
1003 // LiveUses += UpwardExposedUses(B);
1004 // for (C : IIDF[B])
1005 // for (U : LiveUses)
1006 // if (Rdef(U) dom C)
1007 // C.addLiveIn(U)
1008 //
1009
1010 // Go up the dominator tree (depth-first).
1011 MachineDomTreeNode *N = MDT.getNode(B);
1012 for (auto *I : *N) {
1013 RefMap L;
1014 MachineBasicBlock *SB = I->getBlock();
1015 traverse(SB, L);
1016
1017 for (auto S : L)
1018 LiveIn[S.first].insert(S.second.begin(), S.second.end());
1019 }
1020
1021 if (Trace) {
1022 dbgs() << "\n-- " << printMBBReference(*B) << ": " << __func__
1023 << " after recursion into: {";
1024 for (auto *I : *N)
1025 dbgs() << ' ' << I->getBlock()->getNumber();
1026 dbgs() << " }\n";
1027 dbgs() << " LiveIn: " << Print(LiveIn, DFG) << '\n';
1028 dbgs() << " Local: " << Print(LiveMap[B], DFG) << '\n';
1029 }
1030
1031 // Add reaching defs of phi uses that are live on exit from this block.
1032 RefMap &PUs = PhiLOX[B];
1033 for (auto &S : PUs)
1034 LiveIn[S.first].insert(S.second.begin(), S.second.end());
1035
1036 if (Trace) {
1037 dbgs() << "after LOX\n";
1038 dbgs() << " LiveIn: " << Print(LiveIn, DFG) << '\n';
1039 dbgs() << " Local: " << Print(LiveMap[B], DFG) << '\n';
1040 }
1041
1042 // The LiveIn map at this point has all defs that are live-on-exit from B,
1043 // as if they were live-on-entry to B. First, we need to filter out all
1044 // defs that are present in this block. Then we will add reaching defs of
1045 // all upward-exposed uses.
1046
1047 // To filter out the defs, first make a copy of LiveIn, and then re-populate
1048 // LiveIn with the defs that should remain.
1049 RefMap LiveInCopy = LiveIn;
1050 LiveIn.clear();
1051
1052 for (const auto &LE : LiveInCopy) {
1053 RegisterRef LRef(LE.first);
1054 NodeRefSet &NewDefs = LiveIn[LRef.Id]; // To be filled.
1055 const NodeRefSet &OldDefs = LE.second;
1056 for (NodeRef OR : OldDefs) {
1057 // R is a def node that was live-on-exit
1058 auto DA = DFG.addr<DefNode *>(OR.first);
1059 NodeAddr<InstrNode *> IA = DA.Addr->getOwner(DFG);
1060 NodeAddr<BlockNode *> BA = IA.Addr->getOwner(DFG);
1061 if (B != BA.Addr->getCode()) {
1062 // Defs from a different block need to be preserved. Defs from this
1063 // block will need to be processed further, except for phi defs, the
1064 // liveness of which is handled through the PhiLON/PhiLOX maps.
1065 NewDefs.insert(OR);
1066 continue;
1067 }
1068
1069 // Defs from this block need to stop the liveness from being
1070 // propagated upwards. This only applies to non-preserving defs,
1071 // and to the parts of the register actually covered by those defs.
1072 // (Note that phi defs should always be preserving.)
1073 RegisterAggr RRs(PRI);
1074 LRef.Mask = OR.second;
1075
1076 if (!DFG.IsPreservingDef(DA)) {
1077 assert(!(IA.Addr->getFlags() & NodeAttrs::Phi));
1078 // DA is a non-phi def that is live-on-exit from this block, and
1079 // that is also located in this block. LRef is a register ref
1080 // whose use this def reaches. If DA covers LRef, then no part
1081 // of LRef is exposed upwards.A
1082 if (RRs.insert(DA.Addr->getRegRef(DFG)).hasCoverOf(LRef))
1083 continue;
1084 }
1085
1086 // DA itself was not sufficient to cover LRef. In general, it is
1087 // the last in a chain of aliased defs before the exit from this block.
1088 // There could be other defs in this block that are a part of that
1089 // chain. Check that now: accumulate the registers from these defs,
1090 // and if they all together cover LRef, it is not live-on-entry.
1091 for (NodeAddr<DefNode *> TA : getAllReachingDefs(DA)) {
1092 // DefNode -> InstrNode -> BlockNode.
1093 NodeAddr<InstrNode *> ITA = TA.Addr->getOwner(DFG);
1094 NodeAddr<BlockNode *> BTA = ITA.Addr->getOwner(DFG);
1095 // Reaching defs are ordered in the upward direction.
1096 if (BTA.Addr->getCode() != B) {
1097 // We have reached past the beginning of B, and the accumulated
1098 // registers are not covering LRef. The first def from the
1099 // upward chain will be live.
1100 // Subtract all accumulated defs (RRs) from LRef.
1101 RegisterRef T = RRs.clearIn(LRef);
1102 assert(T);
1103 NewDefs.insert({TA.Id, T.Mask});
1104 break;
1105 }
1106
1107 // TA is in B. Only add this def to the accumulated cover if it is
1108 // not preserving.
1109 if (!(TA.Addr->getFlags() & NodeAttrs::Preserving))
1110 RRs.insert(TA.Addr->getRegRef(DFG));
1111 // If this is enough to cover LRef, then stop.
1112 if (RRs.hasCoverOf(LRef))
1113 break;
1114 }
1115 }
1116 }
1117
1118 emptify(LiveIn);
1119
1120 if (Trace) {
1121 dbgs() << "after defs in block\n";
1122 dbgs() << " LiveIn: " << Print(LiveIn, DFG) << '\n';
1123 dbgs() << " Local: " << Print(LiveMap[B], DFG) << '\n';
1124 }
1125
1126 // Scan the block for upward-exposed uses and add them to the tracking set.
1127 for (auto I : DFG.getFunc().Addr->findBlock(B, DFG).Addr->members(DFG)) {
1128 NodeAddr<InstrNode *> IA = I;
1129 if (IA.Addr->getKind() != NodeAttrs::Stmt)
1130 continue;
1131 for (NodeAddr<UseNode *> UA : IA.Addr->members_if(DFG.IsUse, DFG)) {
1132 if (UA.Addr->getFlags() & NodeAttrs::Undef)
1133 continue;
1134 RegisterRef RR = UA.Addr->getRegRef(DFG);
1135 for (NodeAddr<DefNode *> D : getAllReachingDefs(UA))
1136 if (getBlockWithRef(D.Id) != B)
1137 LiveIn[RR.Id].insert({D.Id, RR.Mask});
1138 }
1139 }
1140
1141 if (Trace) {
1142 dbgs() << "after uses in block\n";
1143 dbgs() << " LiveIn: " << Print(LiveIn, DFG) << '\n';
1144 dbgs() << " Local: " << Print(LiveMap[B], DFG) << '\n';
1145 }
1146
1147 // Phi uses should not be propagated up the dominator tree, since they
1148 // are not dominated by their corresponding reaching defs.
1149 RegisterAggr &Local = LiveMap[B];
1150 RefMap &LON = PhiLON[B];
1151 for (auto &R : LON) {
1152 LaneBitmask M;
1153 for (auto P : R.second)
1154 M |= P.second;
1155 Local.insert(RegisterRef(R.first, M));
1156 }
1157
1158 if (Trace) {
1159 dbgs() << "after phi uses in block\n";
1160 dbgs() << " LiveIn: " << Print(LiveIn, DFG) << '\n';
1161 dbgs() << " Local: " << Print(Local, DFG) << '\n';
1162 }
1163
1164 for (auto *C : IIDF[B]) {
1165 RegisterAggr &LiveC = LiveMap[C];
1166 for (const auto &S : LiveIn)
1167 for (auto R : S.second)
1168 if (MDT.properlyDominates(getBlockWithRef(R.first), C))
1169 LiveC.insert(RegisterRef(S.first, R.second));
1170 }
1171}
1172
1173void Liveness::emptify(RefMap &M) {
1174 M.remove_if([](const auto &P) { return P.second.empty(); });
1175}
1176
1177} // namespace llvm::rdf
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file defines the DenseMap class.
IRTranslator LLVM IR MI
A common definition of LaneBitmask for use in TableGen and CodeGen.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define T
static constexpr unsigned SM(unsigned Version)
#define P(N)
static cl::opt< unsigned > MaxRecNest("rdf-liveness-max-rec", cl::init(25), cl::Hidden, cl::desc("Maximum recursion level"))
Remove Loads Into Fake Uses
bool IsDead
SI optimize exec mask operations pre RA
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallSet class.
BitVector & reset()
Reset all bits in the bitvector.
Definition BitVector.h:409
BitVector & set()
Set all bits in the bitvector.
Definition BitVector.h:366
ValueT & at(const_arg_type_t< KeyT > Val)
Return the entry for the specified key, or abort if no such entry exists.
Definition DenseMap.h:270
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:225
bool empty() const
Definition DenseMap.h:173
iterator begin()
Definition DenseMap.h:139
iterator end()
Definition DenseMap.h:143
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:286
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
MCRegAliasIterator enumerates all registers aliasing Reg.
Iterator that enumerates the sub-registers of a Reg and the associated sub-register indices.
bool isValid() const
Returns true if this iterator is not yet at the end.
unsigned getSubRegIndex() const
Returns sub-register index of the current sub-register.
MCRegister getSubReg() const
Returns current sub-register.
const MachineBasicBlock & front() const
void insert(iterator MBBI, MachineBasicBlock *MBB)
Representation of each machine instruction.
const MachineBasicBlock * getParent() const
bool insert(SUnit *SU)
Wrapper class representing virtual and physical registers.
Definition Register.h:20
A vector that has set insertion semantics.
Definition SetVector.h:57
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:103
void insert_range(Range &&R)
Definition SetVector.h:176
iterator end()
Get an iterator to the end of the SetVector.
Definition SetVector.h:112
iterator begin()
Get an iterator to the beginning of the SetVector.
Definition SetVector.h:106
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:151
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:134
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition SmallSet.h:176
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
self_iterator getIterator()
Definition ilist_node.h:123
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
initializer< Ty > init(const Ty &Val)
NodeAddr< BlockNode * > Block
Definition RDFGraph.h:392
Print(const T &, const DataFlowGraph &) -> Print< T >
uint32_t NodeId
Definition RDFGraph.h:262
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
LLVM_ABI raw_ostream & operator<<(raw_ostream &OS, const Print< RegisterRef > &P)
Definition RDFGraph.cpp:44
std::set< NodeId > NodeSet
Definition RDFGraph.h:551
SmallVector< Node, 4 > NodeList
Definition RDFGraph.h:550
This is an optimization pass for GlobalISel generic memory operations.
constexpr from_range_t from_range
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2207
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1635
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
DomTreeNodeBase< MachineBasicBlock > MachineDomTreeNode
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
DWARFExpression::Operation Op
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2191
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
#define N
Pair of physical register and lane mask.
NodeAddr< T > addr(NodeId N) const
Definition RDFGraph.h:692
LLVM_ABI NodeList getAllReachingDefs(RegisterRef RefRR, NodeAddr< RefNode * > RefA, bool TopShadows, bool FullChain, const RegisterAggr &DefRRs)
std::unordered_set< NodeRef > NodeRefSet
Definition RDFLiveness.h:58
LLVM_ABI NodeAddr< RefNode * > getNearestAliasedRef(RegisterRef RefRR, NodeAddr< InstrNode * > IA)
Find the nearest ref node aliased to RefRR, going upwards in the data flow, starting from the instruc...
LLVM_ABI void resetKills()
LLVM_ABI void resetLiveIns()
DenseMap< RegisterId, NodeRefSet > RefMap
Definition RDFLiveness.h:59
detail::NodeRef NodeRef
Definition RDFLiveness.h:57
LLVM_ABI void computePhiInfo()
LLVM_ABI std::pair< NodeSet, bool > getAllReachingDefsRec(RegisterRef RefRR, NodeAddr< RefNode * > RefA, NodeSet &Visited, const NodeSet &Defs)
LLVM_ABI void computeLiveIns()
LLVM_ABI NodeSet getAllReachedUses(RegisterRef RefRR, NodeAddr< DefNode * > DefA, const RegisterAggr &DefRRs)
NodeId getSibling() const
Definition RDFGraph.h:569
iterator_range< ref_iterator > refs() const
LLVM_ABI RegisterAggr & insert(RegisterRef RR)
LLVM_ABI RegisterRef clearIn(RegisterRef RR) const
LLVM_ABI bool hasAliasOf(RegisterRef RR) const
LLVM_ABI RegisterRef intersectWith(RegisterRef RR) const
LLVM_ABI bool hasCoverOf(RegisterRef RR) const
static bool isCoverOf(RegisterRef RA, RegisterRef RB, const PhysicalRegisterInfo &PRI)