LLVM 20.0.0git
ReachingDefAnalysis.cpp
Go to the documentation of this file.
1//===---- ReachingDefAnalysis.cpp - Reaching Def Analysis ---*- C++ -*-----===//
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
11#include "llvm/ADT/SmallSet.h"
15#include "llvm/Support/Debug.h"
16
17using namespace llvm;
18
19#define DEBUG_TYPE "reaching-defs-analysis"
20
22INITIALIZE_PASS(ReachingDefAnalysis, DEBUG_TYPE, "ReachingDefAnalysis", false,
23 true)
24
25static bool isValidReg(const MachineOperand &MO) {
26 return MO.isReg() && MO.getReg();
27}
28
29static bool isValidRegUse(const MachineOperand &MO) {
30 return isValidReg(MO) && MO.isUse();
31}
32
33static bool isValidRegUseOf(const MachineOperand &MO, MCRegister Reg,
34 const TargetRegisterInfo *TRI) {
35 if (!isValidRegUse(MO))
36 return false;
37 return TRI->regsOverlap(MO.getReg(), Reg);
38}
39
40static bool isValidRegDef(const MachineOperand &MO) {
41 return isValidReg(MO) && MO.isDef();
42}
43
44static bool isValidRegDefOf(const MachineOperand &MO, MCRegister Reg,
45 const TargetRegisterInfo *TRI) {
46 if (!isValidRegDef(MO))
47 return false;
48 return TRI->regsOverlap(MO.getReg(), Reg);
49}
50
51void ReachingDefAnalysis::enterBasicBlock(MachineBasicBlock *MBB) {
52 unsigned MBBNumber = MBB->getNumber();
53 assert(MBBNumber < MBBReachingDefs.numBlockIDs() &&
54 "Unexpected basic block number.");
55 MBBReachingDefs.startBasicBlock(MBBNumber, NumRegUnits);
56
57 // Reset instruction counter in each basic block.
58 CurInstr = 0;
59
60 // Set up LiveRegs to represent registers entering MBB.
61 // Default values are 'nothing happened a long time ago'.
62 if (LiveRegs.empty())
63 LiveRegs.assign(NumRegUnits, ReachingDefDefaultVal);
64
65 // This is the entry block.
66 if (MBB->pred_empty()) {
67 for (const auto &LI : MBB->liveins()) {
68 for (MCRegUnit Unit : TRI->regunits(LI.PhysReg)) {
69 // Treat function live-ins as if they were defined just before the first
70 // instruction. Usually, function arguments are set up immediately
71 // before the call.
72 if (LiveRegs[Unit] != -1) {
73 LiveRegs[Unit] = -1;
74 MBBReachingDefs.append(MBBNumber, Unit, -1);
75 }
76 }
77 }
78 LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << ": entry\n");
79 return;
80 }
81
82 // Try to coalesce live-out registers from predecessors.
84 assert(unsigned(pred->getNumber()) < MBBOutRegsInfos.size() &&
85 "Should have pre-allocated MBBInfos for all MBBs");
86 const LiveRegsDefInfo &Incoming = MBBOutRegsInfos[pred->getNumber()];
87 // Incoming is null if this is a backedge from a BB
88 // we haven't processed yet
89 if (Incoming.empty())
90 continue;
91
92 // Find the most recent reaching definition from a predecessor.
93 for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit)
94 LiveRegs[Unit] = std::max(LiveRegs[Unit], Incoming[Unit]);
95 }
96
97 // Insert the most recent reaching definition we found.
98 for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit)
99 if (LiveRegs[Unit] != ReachingDefDefaultVal)
100 MBBReachingDefs.append(MBBNumber, Unit, LiveRegs[Unit]);
101}
102
103void ReachingDefAnalysis::leaveBasicBlock(MachineBasicBlock *MBB) {
104 assert(!LiveRegs.empty() && "Must enter basic block first.");
105 unsigned MBBNumber = MBB->getNumber();
106 assert(MBBNumber < MBBOutRegsInfos.size() &&
107 "Unexpected basic block number.");
108 // Save register clearances at end of MBB - used by enterBasicBlock().
109 MBBOutRegsInfos[MBBNumber] = LiveRegs;
110
111 // While processing the basic block, we kept `Def` relative to the start
112 // of the basic block for convenience. However, future use of this information
113 // only cares about the clearance from the end of the block, so adjust
114 // everything to be relative to the end of the basic block.
115 for (int &OutLiveReg : MBBOutRegsInfos[MBBNumber])
116 if (OutLiveReg != ReachingDefDefaultVal)
117 OutLiveReg -= CurInstr;
118 LiveRegs.clear();
119}
120
121void ReachingDefAnalysis::processDefs(MachineInstr *MI) {
122 assert(!MI->isDebugInstr() && "Won't process debug instructions");
123
124 unsigned MBBNumber = MI->getParent()->getNumber();
125 assert(MBBNumber < MBBReachingDefs.numBlockIDs() &&
126 "Unexpected basic block number.");
127
128 for (auto &MO : MI->operands()) {
129 if (!isValidRegDef(MO))
130 continue;
131 for (MCRegUnit Unit : TRI->regunits(MO.getReg().asMCReg())) {
132 // This instruction explicitly defines the current reg unit.
133 LLVM_DEBUG(dbgs() << printRegUnit(Unit, TRI) << ":\t" << CurInstr << '\t'
134 << *MI);
135
136 // How many instructions since this reg unit was last written?
137 if (LiveRegs[Unit] != CurInstr) {
138 LiveRegs[Unit] = CurInstr;
139 MBBReachingDefs.append(MBBNumber, Unit, CurInstr);
140 }
141 }
142 }
143 InstIds[MI] = CurInstr;
144 ++CurInstr;
145}
146
147void ReachingDefAnalysis::reprocessBasicBlock(MachineBasicBlock *MBB) {
148 unsigned MBBNumber = MBB->getNumber();
149 assert(MBBNumber < MBBReachingDefs.numBlockIDs() &&
150 "Unexpected basic block number.");
151
152 // Count number of non-debug instructions for end of block adjustment.
153 auto NonDbgInsts =
155 int NumInsts = std::distance(NonDbgInsts.begin(), NonDbgInsts.end());
156
157 // When reprocessing a block, the only thing we need to do is check whether
158 // there is now a more recent incoming reaching definition from a predecessor.
160 assert(unsigned(pred->getNumber()) < MBBOutRegsInfos.size() &&
161 "Should have pre-allocated MBBInfos for all MBBs");
162 const LiveRegsDefInfo &Incoming = MBBOutRegsInfos[pred->getNumber()];
163 // Incoming may be empty for dead predecessors.
164 if (Incoming.empty())
165 continue;
166
167 for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit) {
168 int Def = Incoming[Unit];
169 if (Def == ReachingDefDefaultVal)
170 continue;
171
172 auto Defs = MBBReachingDefs.defs(MBBNumber, Unit);
173 if (!Defs.empty() && Defs.front() < 0) {
174 if (Defs.front() >= Def)
175 continue;
176
177 // Update existing reaching def from predecessor to a more recent one.
178 MBBReachingDefs.replaceFront(MBBNumber, Unit, Def);
179 } else {
180 // Insert new reaching def from predecessor.
181 MBBReachingDefs.prepend(MBBNumber, Unit, Def);
182 }
183
184 // Update reaching def at end of BB. Keep in mind that these are
185 // adjusted relative to the end of the basic block.
186 if (MBBOutRegsInfos[MBBNumber][Unit] < Def - NumInsts)
187 MBBOutRegsInfos[MBBNumber][Unit] = Def - NumInsts;
188 }
189 }
190}
191
192void ReachingDefAnalysis::processBasicBlock(
193 const LoopTraversal::TraversedMBBInfo &TraversedMBB) {
194 MachineBasicBlock *MBB = TraversedMBB.MBB;
196 << (!TraversedMBB.IsDone ? ": incomplete\n"
197 : ": all preds known\n"));
198
199 if (!TraversedMBB.PrimaryPass) {
200 // Reprocess MBB that is part of a loop.
201 reprocessBasicBlock(MBB);
202 return;
203 }
204
205 enterBasicBlock(MBB);
206 for (MachineInstr &MI :
208 processDefs(&MI);
209 leaveBasicBlock(MBB);
210}
211
213 MF = &mf;
214 TRI = MF->getSubtarget().getRegisterInfo();
215 LLVM_DEBUG(dbgs() << "********** REACHING DEFINITION ANALYSIS **********\n");
216 init();
217 traverse();
218 return false;
219}
220
222 // Clear the internal vectors.
223 MBBOutRegsInfos.clear();
224 MBBReachingDefs.clear();
225 InstIds.clear();
226 LiveRegs.clear();
227}
228
231 init();
232 traverse();
233}
234
236 NumRegUnits = TRI->getNumRegUnits();
237 MBBReachingDefs.init(MF->getNumBlockIDs());
238 // Initialize the MBBOutRegsInfos
239 MBBOutRegsInfos.resize(MF->getNumBlockIDs());
240 LoopTraversal Traversal;
241 TraversedMBBOrder = Traversal.traverse(*MF);
242}
243
245 // Traverse the basic blocks.
246 for (LoopTraversal::TraversedMBBInfo TraversedMBB : TraversedMBBOrder)
247 processBasicBlock(TraversedMBB);
248#ifndef NDEBUG
249 // Make sure reaching defs are sorted and unique.
250 for (unsigned MBBNumber = 0, NumBlockIDs = MF->getNumBlockIDs();
251 MBBNumber != NumBlockIDs; ++MBBNumber) {
252 for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit) {
253 int LastDef = ReachingDefDefaultVal;
254 for (int Def : MBBReachingDefs.defs(MBBNumber, Unit)) {
255 assert(Def > LastDef && "Defs must be sorted and unique");
256 LastDef = Def;
257 }
258 }
259 }
260#endif
261}
262
264 MCRegister Reg) const {
265 assert(InstIds.count(MI) && "Unexpected machine instuction.");
266 int InstId = InstIds.lookup(MI);
267 int DefRes = ReachingDefDefaultVal;
268 unsigned MBBNumber = MI->getParent()->getNumber();
269 assert(MBBNumber < MBBReachingDefs.numBlockIDs() &&
270 "Unexpected basic block number.");
271 int LatestDef = ReachingDefDefaultVal;
272 for (MCRegUnit Unit : TRI->regunits(Reg)) {
273 for (int Def : MBBReachingDefs.defs(MBBNumber, Unit)) {
274 if (Def >= InstId)
275 break;
276 DefRes = Def;
277 }
278 LatestDef = std::max(LatestDef, DefRes);
279 }
280 return LatestDef;
281}
282
283MachineInstr *ReachingDefAnalysis::getReachingLocalMIDef(MachineInstr *MI,
284 MCRegister Reg) const {
285 return hasLocalDefBefore(MI, Reg)
286 ? getInstFromId(MI->getParent(), getReachingDef(MI, Reg))
287 : nullptr;
288}
289
291 MCRegister Reg) const {
292 MachineBasicBlock *ParentA = A->getParent();
293 MachineBasicBlock *ParentB = B->getParent();
294 if (ParentA != ParentB)
295 return false;
296
297 return getReachingDef(A, Reg) == getReachingDef(B, Reg);
298}
299
300MachineInstr *ReachingDefAnalysis::getInstFromId(MachineBasicBlock *MBB,
301 int InstId) const {
302 assert(static_cast<size_t>(MBB->getNumber()) <
303 MBBReachingDefs.numBlockIDs() &&
304 "Unexpected basic block number.");
305 assert(InstId < static_cast<int>(MBB->size()) &&
306 "Unexpected instruction id.");
307
308 if (InstId < 0)
309 return nullptr;
310
311 for (auto &MI : *MBB) {
312 auto F = InstIds.find(&MI);
313 if (F != InstIds.end() && F->second == InstId)
314 return &MI;
315 }
316
317 return nullptr;
318}
319
321 assert(InstIds.count(MI) && "Unexpected machine instuction.");
322 return InstIds.lookup(MI) - getReachingDef(MI, Reg);
323}
324
326 MCRegister Reg) const {
327 return getReachingDef(MI, Reg) >= 0;
328}
329
331 MCRegister Reg,
332 InstSet &Uses) const {
333 MachineBasicBlock *MBB = Def->getParent();
335 while (++MI != MBB->end()) {
336 if (MI->isDebugInstr())
337 continue;
338
339 // If/when we find a new reaching def, we know that there's no more uses
340 // of 'Def'.
341 if (getReachingLocalMIDef(&*MI, Reg) != Def)
342 return;
343
344 for (auto &MO : MI->operands()) {
345 if (!isValidRegUseOf(MO, Reg, TRI))
346 continue;
347
348 Uses.insert(&*MI);
349 if (MO.isKill())
350 return;
351 }
352 }
353}
354
356 InstSet &Uses) const {
357 for (MachineInstr &MI :
359 for (auto &MO : MI.operands()) {
360 if (!isValidRegUseOf(MO, Reg, TRI))
361 continue;
362 if (getReachingDef(&MI, Reg) >= 0)
363 return false;
364 Uses.insert(&MI);
365 }
366 }
367 auto Last = MBB->getLastNonDebugInstr();
368 if (Last == MBB->end())
369 return true;
370 return isReachingDefLiveOut(&*Last, Reg);
371}
372
374 InstSet &Uses) const {
375 MachineBasicBlock *MBB = MI->getParent();
376
377 // Collect the uses that each def touches within the block.
379
380 // Handle live-out values.
381 if (auto *LiveOut = getLocalLiveOutMIDef(MI->getParent(), Reg)) {
382 if (LiveOut != MI)
383 return;
384
387 while (!ToVisit.empty()) {
389 if (Visited.count(MBB) || !MBB->isLiveIn(Reg))
390 continue;
391 if (getLiveInUses(MBB, Reg, Uses))
392 llvm::append_range(ToVisit, MBB->successors());
393 Visited.insert(MBB);
394 }
395 }
396}
397
399 MCRegister Reg,
400 InstSet &Defs) const {
401 if (auto *Def = getUniqueReachingMIDef(MI, Reg)) {
402 Defs.insert(Def);
403 return;
404 }
405
406 for (auto *MBB : MI->getParent()->predecessors())
407 getLiveOuts(MBB, Reg, Defs);
408}
409
411 InstSet &Defs) const {
413 getLiveOuts(MBB, Reg, Defs, VisitedBBs);
414}
415
417 InstSet &Defs,
418 BlockSet &VisitedBBs) const {
419 if (VisitedBBs.count(MBB))
420 return;
421
422 VisitedBBs.insert(MBB);
423 LiveRegUnits LiveRegs(*TRI);
424 LiveRegs.addLiveOuts(*MBB);
425 if (LiveRegs.available(Reg))
426 return;
427
428 if (auto *Def = getLocalLiveOutMIDef(MBB, Reg))
429 Defs.insert(Def);
430 else
431 for (auto *Pred : MBB->predecessors())
432 getLiveOuts(Pred, Reg, Defs, VisitedBBs);
433}
434
437 MCRegister Reg) const {
438 // If there's a local def before MI, return it.
439 MachineInstr *LocalDef = getReachingLocalMIDef(MI, Reg);
440 if (LocalDef && InstIds.lookup(LocalDef) < InstIds.lookup(MI))
441 return LocalDef;
442
444 MachineBasicBlock *Parent = MI->getParent();
445 for (auto *Pred : Parent->predecessors())
446 getLiveOuts(Pred, Reg, Incoming);
447
448 // Check that we have a single incoming value and that it does not
449 // come from the same block as MI - since it would mean that the def
450 // is executed after MI.
451 if (Incoming.size() == 1 && (*Incoming.begin())->getParent() != Parent)
452 return *Incoming.begin();
453 return nullptr;
454}
455
457 unsigned Idx) const {
458 assert(MI->getOperand(Idx).isReg() && "Expected register operand");
459 return getUniqueReachingMIDef(MI, MI->getOperand(Idx).getReg());
460}
461
463 MachineOperand &MO) const {
464 assert(MO.isReg() && "Expected register operand");
465 return getUniqueReachingMIDef(MI, MO.getReg());
466}
467
469 MCRegister Reg) const {
470 MachineBasicBlock *MBB = MI->getParent();
471 LiveRegUnits LiveRegs(*TRI);
472 LiveRegs.addLiveOuts(*MBB);
473
474 // Yes if the register is live out of the basic block.
475 if (!LiveRegs.available(Reg))
476 return true;
477
478 // Walk backwards through the block to see if the register is live at some
479 // point.
480 for (MachineInstr &Last :
482 LiveRegs.stepBackward(Last);
483 if (!LiveRegs.available(Reg))
484 return InstIds.lookup(&Last) > InstIds.lookup(MI);
485 }
486 return false;
487}
488
490 MCRegister Reg) const {
491 MachineBasicBlock *MBB = MI->getParent();
492 auto Last = MBB->getLastNonDebugInstr();
493 if (Last != MBB->end() &&
494 getReachingDef(MI, Reg) != getReachingDef(&*Last, Reg))
495 return true;
496
497 if (auto *Def = getLocalLiveOutMIDef(MBB, Reg))
498 return Def == getReachingLocalMIDef(MI, Reg);
499
500 return false;
501}
502
504 MCRegister Reg) const {
505 MachineBasicBlock *MBB = MI->getParent();
506 LiveRegUnits LiveRegs(*TRI);
507 LiveRegs.addLiveOuts(*MBB);
508 if (LiveRegs.available(Reg))
509 return false;
510
511 auto Last = MBB->getLastNonDebugInstr();
512 int Def = getReachingDef(MI, Reg);
513 if (Last != MBB->end() && getReachingDef(&*Last, Reg) != Def)
514 return false;
515
516 // Finally check that the last instruction doesn't redefine the register.
517 for (auto &MO : Last->operands())
518 if (isValidRegDefOf(MO, Reg, TRI))
519 return false;
520
521 return true;
522}
523
525 MCRegister Reg) const {
526 LiveRegUnits LiveRegs(*TRI);
527 LiveRegs.addLiveOuts(*MBB);
528 if (LiveRegs.available(Reg))
529 return nullptr;
530
531 auto Last = MBB->getLastNonDebugInstr();
532 if (Last == MBB->end())
533 return nullptr;
534
535 int Def = getReachingDef(&*Last, Reg);
536 for (auto &MO : Last->operands())
537 if (isValidRegDefOf(MO, Reg, TRI))
538 return &*Last;
539
540 return Def < 0 ? nullptr : getInstFromId(MBB, Def);
541}
542
544 return MI.mayLoadOrStore() || MI.mayRaiseFPException() ||
545 MI.hasUnmodeledSideEffects() || MI.isTerminator() ||
546 MI.isCall() || MI.isBarrier() || MI.isBranch() || MI.isReturn();
547}
548
549// Can we safely move 'From' to just before 'To'? To satisfy this, 'From' must
550// not define a register that is used by any instructions, after and including,
551// 'To'. These instructions also must not redefine any of Froms operands.
552template<typename Iterator>
553bool ReachingDefAnalysis::isSafeToMove(MachineInstr *From,
554 MachineInstr *To) const {
555 if (From->getParent() != To->getParent() || From == To)
556 return false;
557
558 SmallSet<int, 2> Defs;
559 // First check that From would compute the same value if moved.
560 for (auto &MO : From->operands()) {
561 if (!isValidReg(MO))
562 continue;
563 if (MO.isDef())
564 Defs.insert(MO.getReg());
565 else if (!hasSameReachingDef(From, To, MO.getReg()))
566 return false;
567 }
568
569 // Now walk checking that the rest of the instructions will compute the same
570 // value and that we're not overwriting anything. Don't move the instruction
571 // past any memory, control-flow or other ambiguous instructions.
572 for (auto I = ++Iterator(From), E = Iterator(To); I != E; ++I) {
573 if (mayHaveSideEffects(*I))
574 return false;
575 for (auto &MO : I->operands())
576 if (MO.isReg() && MO.getReg() && Defs.count(MO.getReg()))
577 return false;
578 }
579 return true;
580}
581
583 MachineInstr *To) const {
584 using Iterator = MachineBasicBlock::iterator;
585 // Walk forwards until we find the instruction.
586 for (auto I = Iterator(From), E = From->getParent()->end(); I != E; ++I)
587 if (&*I == To)
588 return isSafeToMove<Iterator>(From, To);
589 return false;
590}
591
593 MachineInstr *To) const {
595 // Walk backwards until we find the instruction.
596 for (auto I = Iterator(From), E = From->getParent()->rend(); I != E; ++I)
597 if (&*I == To)
598 return isSafeToMove<Iterator>(From, To);
599 return false;
600}
601
603 InstSet &ToRemove) const {
606 return isSafeToRemove(MI, Visited, ToRemove, Ignore);
607}
608
609bool
611 InstSet &Ignore) const {
613 return isSafeToRemove(MI, Visited, ToRemove, Ignore);
614}
615
616bool
618 InstSet &ToRemove, InstSet &Ignore) const {
619 if (Visited.count(MI) || Ignore.count(MI))
620 return true;
621 else if (mayHaveSideEffects(*MI)) {
622 // Unless told to ignore the instruction, don't remove anything which has
623 // side effects.
624 return false;
625 }
626
627 Visited.insert(MI);
628 for (auto &MO : MI->operands()) {
629 if (!isValidRegDef(MO))
630 continue;
631
633 getGlobalUses(MI, MO.getReg(), Uses);
634
635 for (auto *I : Uses) {
636 if (Ignore.count(I) || ToRemove.count(I))
637 continue;
638 if (!isSafeToRemove(I, Visited, ToRemove, Ignore))
639 return false;
640 }
641 }
642 ToRemove.insert(MI);
643 return true;
644}
645
647 InstSet &Dead) const {
648 Dead.insert(MI);
649 auto IsDead = [this, &Dead](MachineInstr *Def, MCRegister Reg) {
650 if (mayHaveSideEffects(*Def))
651 return false;
652
653 unsigned LiveDefs = 0;
654 for (auto &MO : Def->operands()) {
655 if (!isValidRegDef(MO))
656 continue;
657 if (!MO.isDead())
658 ++LiveDefs;
659 }
660
661 if (LiveDefs > 1)
662 return false;
663
665 getGlobalUses(Def, Reg, Uses);
666 return llvm::set_is_subset(Uses, Dead);
667 };
668
669 for (auto &MO : MI->operands()) {
670 if (!isValidRegUse(MO))
671 continue;
672 if (MachineInstr *Def = getMIOperand(MI, MO))
673 if (IsDead(Def, MO.getReg()))
674 collectKilledOperands(Def, Dead);
675 }
676}
677
679 MCRegister Reg) const {
681 return isSafeToDefRegAt(MI, Reg, Ignore);
682}
683
685 InstSet &Ignore) const {
686 // Check for any uses of the register after MI.
687 if (isRegUsedAfter(MI, Reg)) {
688 if (auto *Def = getReachingLocalMIDef(MI, Reg)) {
690 getGlobalUses(Def, Reg, Uses);
692 return false;
693 } else
694 return false;
695 }
696
697 MachineBasicBlock *MBB = MI->getParent();
698 // Check for any defs after MI.
699 if (isRegDefinedAfter(MI, Reg)) {
701 for (auto E = MBB->end(); I != E; ++I) {
702 if (Ignore.count(&*I))
703 continue;
704 for (auto &MO : I->operands())
705 if (isValidRegDefOf(MO, Reg, TRI))
706 return false;
707 }
708 }
709 return true;
710}
aarch64 promote const
ReachingDefAnalysis InstSet & ToRemove
ReachingDefAnalysis InstSet InstSet & Ignore
MachineBasicBlock & MBB
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define LLVM_DEBUG(...)
Definition: Debug.h:106
hexagon gen pred
IRTranslator LLVM IR MI
A set of register units.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
unsigned const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
static bool isValidRegUseOf(const MachineOperand &MO, MCRegister Reg, const TargetRegisterInfo *TRI)
static bool mayHaveSideEffects(MachineInstr &MI)
static bool isValidRegDef(const MachineOperand &MO)
static bool isValidRegUse(const MachineOperand &MO)
static bool isValidRegDefOf(const MachineOperand &MO, MCRegister Reg, const TargetRegisterInfo *TRI)
#define DEBUG_TYPE
Remove Loads Into Fake Uses
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool IsDead
This file defines generic set operations that may be used on set's of different types,...
This file defines the SmallSet class.
A set of register units used to track register liveness.
Definition: LiveRegUnits.h:30
bool available(MCPhysReg Reg) const
Returns true if no part of physical register Reg is live.
Definition: LiveRegUnits.h:116
void stepBackward(const MachineInstr &MI)
Updates liveness when stepping backwards over the instruction MI.
void addLiveOuts(const MachineBasicBlock &MBB)
Adds registers living out of block MBB.
This class provides the basic blocks traversal order used by passes like ReachingDefAnalysis and Exec...
Definition: LoopTraversal.h:65
TraversalOrder traverse(MachineFunction &MF)
void append(unsigned MBBNumber, unsigned Unit, int Def)
ArrayRef< ReachingDef > defs(unsigned MBBNumber, unsigned Unit) const
void replaceFront(unsigned MBBNumber, unsigned Unit, int Def)
void init(unsigned NumBlockIDs)
void prepend(unsigned MBBNumber, unsigned Unit, int Def)
void startBasicBlock(unsigned MBBNumber, unsigned NumRegUnits)
unsigned getNumRegUnits() const
Return the number of (native) register units in the target.
iterator_range< MCRegUnitIterator > regunits(MCRegister Reg) const
Returns an iterator range over all regunits for Reg.
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
instr_iterator instr_begin()
iterator_range< livein_iterator > liveins() const
reverse_instr_iterator instr_rbegin()
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
reverse_instr_iterator instr_rend()
iterator getLastNonDebugInstr(bool SkipPseudoOp=true)
Returns an iterator to the last non-debug instruction in the basic block, or end().
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
instr_iterator instr_end()
iterator_range< succ_iterator > successors()
iterator_range< pred_iterator > predecessors()
MachineInstrBundleIterator< MachineInstr > iterator
bool isLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
Representation of each machine instruction.
Definition: MachineInstr.h:69
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:347
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
This class provides the reaching def analysis.
int getClearance(MachineInstr *MI, MCRegister Reg) const
Provides the clearance - the number of instructions since the closest reaching def instuction of Reg ...
void traverse()
Traverse the machine function, mapping definitions.
bool isSafeToMoveForwards(MachineInstr *From, MachineInstr *To) const
Return whether From can be moved forwards to just before To.
bool isReachingDefLiveOut(MachineInstr *MI, MCRegister Reg) const
Return whether the reaching def for MI also is live out of its parent block.
bool isRegUsedAfter(MachineInstr *MI, MCRegister Reg) const
Return whether the given register is used after MI, whether it's a local use or a live out.
bool getLiveInUses(MachineBasicBlock *MBB, MCRegister Reg, InstSet &Uses) const
For the given block, collect the instructions that use the live-in value of the provided register.
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
int getReachingDef(MachineInstr *MI, MCRegister Reg) const
Provides the instruction id of the closest reaching def instruction of Reg that reaches MI,...
bool isSafeToRemove(MachineInstr *MI, InstSet &ToRemove) const
Return whether removing this instruction will have no effect on the program, returning the redundant ...
MachineInstr * getMIOperand(MachineInstr *MI, unsigned Idx) const
If a single MachineInstr creates the reaching definition, for MIs operand at Idx, then return it.
void reset()
Re-run the analysis.
MachineInstr * getUniqueReachingMIDef(MachineInstr *MI, MCRegister Reg) const
If a single MachineInstr creates the reaching definition, then return it.
bool hasSameReachingDef(MachineInstr *A, MachineInstr *B, MCRegister Reg) const
Return whether A and B use the same def of Reg.
void init()
Initialize data structures.
MachineInstr * getLocalLiveOutMIDef(MachineBasicBlock *MBB, MCRegister Reg) const
Return the local MI that produces the live out value for Reg, or nullptr for a non-live out or non-lo...
bool hasLocalDefBefore(MachineInstr *MI, MCRegister Reg) const
Provide whether the register has been defined in the same basic block as, and before,...
void collectKilledOperands(MachineInstr *MI, InstSet &Dead) const
Assuming MI is dead, recursively search the incoming operands which are killed by MI and collect thos...
bool isSafeToMoveBackwards(MachineInstr *From, MachineInstr *To) const
Return whether From can be moved backwards to just after To.
bool isSafeToDefRegAt(MachineInstr *MI, MCRegister Reg) const
Return whether a MachineInstr could be inserted at MI and safely define the given register without af...
void getLiveOuts(MachineBasicBlock *MBB, MCRegister Reg, InstSet &Defs, BlockSet &VisitedBBs) const
Search MBB for a definition of Reg and insert it into Defs.
bool isRegDefinedAfter(MachineInstr *MI, MCRegister Reg) const
Return whether the given register is defined after MI.
void getReachingLocalUses(MachineInstr *MI, MCRegister Reg, InstSet &Uses) const
Provides the uses, in the same block as MI, of register that MI defines.
void getGlobalUses(MachineInstr *MI, MCRegister Reg, InstSet &Uses) const
Collect the users of the value stored in Reg, which is defined by MI.
void getGlobalReachingDefs(MachineInstr *MI, MCRegister Reg, InstSet &Defs) const
Collect all possible definitions of the value stored in Reg, which is used by MI.
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:363
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:452
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:384
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:519
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:132
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:175
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:181
bool empty() const
Definition: SmallVector.h:81
size_t size() const
Definition: SmallVector.h:78
void resize(size_type N)
Definition: SmallVector.h:638
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
NodeAddr< DefNode * > Def
Definition: RDFGraph.h:384
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Printable printRegUnit(unsigned Unit, const TargetRegisterInfo *TRI)
Create Printable object to print register units on a raw_ostream.
bool set_is_subset(const S1Ty &S1, const S2Ty &S2)
set_is_subset(A, B) - Return true iff A in B
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2115
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
auto instructionsWithoutDebug(IterT It, IterT End, bool SkipPseudoOp=true)
Construct a range iterator which begins at It and moves forwards until End is reached,...
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...
MachineBasicBlock * MBB
The basic block.
Definition: LoopTraversal.h:89
bool IsDone
True if the block that is ready for its final round of processing.
Definition: LoopTraversal.h:95
bool PrimaryPass
True if this is the first time we process the basic block.
Definition: LoopTraversal.h:92