1 //===-------------- MIRCanonicalizer.cpp - MIR Canonicalizer --------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // The purpose of this pass is to employ a canonical code transformation so
10 // that code compiled with slightly different IR passes can be diffed more
11 // effectively than otherwise. This is done by renaming vregs in a given
12 // LiveRange in a canonical way. This pass also does a pseudo-scheduling to
13 // move defs closer to their use inorder to reduce diffs caused by slightly
14 // different schedules.
18 // llc -o - -run-pass mir-canonicalizer example.mir
20 // Reorders instructions canonically.
21 // Renames virtual register operands canonically.
22 // Strips certain MIR artifacts (optionally).
24 //===----------------------------------------------------------------------===//
26 #include "llvm/ADT/PostOrderIterator.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/CodeGen/MachineFunctionPass.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/Passes.h"
32 #include "llvm/Support/raw_ostream.h"
39 extern char &MIRCanonicalizerID
;
42 #define DEBUG_TYPE "mir-canonicalizer"
44 static cl::opt
<unsigned>
45 CanonicalizeFunctionNumber("canon-nth-function", cl::Hidden
, cl::init(~0u),
47 cl::desc("Function number to canonicalize."));
49 static cl::opt
<unsigned> CanonicalizeBasicBlockNumber(
50 "canon-nth-basicblock", cl::Hidden
, cl::init(~0u), cl::value_desc("N"),
51 cl::desc("BasicBlock number to canonicalize."));
55 class MIRCanonicalizer
: public MachineFunctionPass
{
58 MIRCanonicalizer() : MachineFunctionPass(ID
) {}
60 StringRef
getPassName() const override
{
61 return "Rename register operands in a canonical ordering.";
64 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
66 MachineFunctionPass::getAnalysisUsage(AU
);
69 bool runOnMachineFunction(MachineFunction
&MF
) override
;
72 } // end anonymous namespace
74 enum VRType
{ RSE_Reg
= 0, RSE_FrameIndex
, RSE_NewCandidate
};
80 TypedVReg(unsigned reg
) : type(RSE_Reg
), reg(reg
) {}
81 TypedVReg(VRType type
) : type(type
), reg(~0U) {
82 assert(type
!= RSE_Reg
&& "Expected a non-register type.");
85 bool isReg() const { return type
== RSE_Reg
; }
86 bool isFrameIndex() const { return type
== RSE_FrameIndex
; }
87 bool isCandidate() const { return type
== RSE_NewCandidate
; }
89 VRType
getType() const { return type
; }
90 unsigned getReg() const {
91 assert(this->isReg() && "Expected a virtual or physical register.");
96 char MIRCanonicalizer::ID
;
98 char &llvm::MIRCanonicalizerID
= MIRCanonicalizer::ID
;
100 INITIALIZE_PASS_BEGIN(MIRCanonicalizer
, "mir-canonicalizer",
101 "Rename Register Operands Canonically", false, false)
103 INITIALIZE_PASS_END(MIRCanonicalizer
, "mir-canonicalizer",
104 "Rename Register Operands Canonically", false, false)
106 static std::vector
<MachineBasicBlock
*> GetRPOList(MachineFunction
&MF
) {
107 ReversePostOrderTraversal
<MachineBasicBlock
*> RPOT(&*MF
.begin());
108 std::vector
<MachineBasicBlock
*> RPOList
;
109 for (auto MBB
: RPOT
) {
110 RPOList
.push_back(MBB
);
117 rescheduleLexographically(std::vector
<MachineInstr
*> instructions
,
118 MachineBasicBlock
*MBB
,
119 std::function
<MachineBasicBlock::iterator()> getPos
) {
121 bool Changed
= false;
122 using StringInstrPair
= std::pair
<std::string
, MachineInstr
*>;
123 std::vector
<StringInstrPair
> StringInstrMap
;
125 for (auto *II
: instructions
) {
127 raw_string_ostream
OS(S
);
131 // Trim the assignment, or start from the begining in the case of a store.
132 const size_t i
= S
.find("=");
133 StringInstrMap
.push_back({(i
== std::string::npos
) ? S
: S
.substr(i
), II
});
136 llvm::sort(StringInstrMap
,
137 [](const StringInstrPair
&a
, const StringInstrPair
&b
) -> bool {
138 return (a
.first
< b
.first
);
141 for (auto &II
: StringInstrMap
) {
144 dbgs() << "Splicing ";
146 dbgs() << " right before: ";
151 MBB
->splice(getPos(), MBB
, II
.second
);
157 static bool rescheduleCanonically(unsigned &PseudoIdempotentInstCount
,
158 MachineBasicBlock
*MBB
) {
160 bool Changed
= false;
162 // Calculates the distance of MI from the begining of its parent BB.
163 auto getInstrIdx
= [](const MachineInstr
&MI
) {
165 for (auto &CurMI
: *MI
.getParent()) {
173 // Pre-Populate vector of instructions to reschedule so that we don't
174 // clobber the iterator.
175 std::vector
<MachineInstr
*> Instructions
;
176 for (auto &MI
: *MBB
) {
177 Instructions
.push_back(&MI
);
180 std::map
<MachineInstr
*, std::vector
<MachineInstr
*>> MultiUsers
;
181 std::vector
<MachineInstr
*> PseudoIdempotentInstructions
;
182 std::vector
<unsigned> PhysRegDefs
;
183 for (auto *II
: Instructions
) {
184 for (unsigned i
= 1; i
< II
->getNumOperands(); i
++) {
185 MachineOperand
&MO
= II
->getOperand(i
);
189 if (TargetRegisterInfo::isVirtualRegister(MO
.getReg()))
195 PhysRegDefs
.push_back(MO
.getReg());
199 for (auto *II
: Instructions
) {
200 if (II
->getNumOperands() == 0)
202 if (II
->mayLoadOrStore())
205 MachineOperand
&MO
= II
->getOperand(0);
206 if (!MO
.isReg() || !TargetRegisterInfo::isVirtualRegister(MO
.getReg()))
211 bool IsPseudoIdempotent
= true;
212 for (unsigned i
= 1; i
< II
->getNumOperands(); i
++) {
214 if (II
->getOperand(i
).isImm()) {
218 if (II
->getOperand(i
).isReg()) {
219 if (!TargetRegisterInfo::isVirtualRegister(II
->getOperand(i
).getReg()))
220 if (llvm::find(PhysRegDefs
, II
->getOperand(i
).getReg()) ==
226 IsPseudoIdempotent
= false;
230 if (IsPseudoIdempotent
) {
231 PseudoIdempotentInstructions
.push_back(II
);
235 LLVM_DEBUG(dbgs() << "Operand " << 0 << " of "; II
->dump(); MO
.dump(););
237 MachineInstr
*Def
= II
;
238 unsigned Distance
= ~0U;
239 MachineInstr
*UseToBringDefCloserTo
= nullptr;
240 MachineRegisterInfo
*MRI
= &MBB
->getParent()->getRegInfo();
241 for (auto &UO
: MRI
->use_nodbg_operands(MO
.getReg())) {
242 MachineInstr
*UseInst
= UO
.getParent();
244 const unsigned DefLoc
= getInstrIdx(*Def
);
245 const unsigned UseLoc
= getInstrIdx(*UseInst
);
246 const unsigned Delta
= (UseLoc
- DefLoc
);
248 if (UseInst
->getParent() != Def
->getParent())
250 if (DefLoc
>= UseLoc
)
253 if (Delta
< Distance
) {
255 UseToBringDefCloserTo
= UseInst
;
259 const auto BBE
= MBB
->instr_end();
260 MachineBasicBlock::iterator DefI
= BBE
;
261 MachineBasicBlock::iterator UseI
= BBE
;
263 for (auto BBI
= MBB
->instr_begin(); BBI
!= BBE
; ++BBI
) {
265 if (DefI
!= BBE
&& UseI
!= BBE
)
273 if (&*BBI
== UseToBringDefCloserTo
) {
279 if (DefI
== BBE
|| UseI
== BBE
)
283 dbgs() << "Splicing ";
285 dbgs() << " right before: ";
289 MultiUsers
[UseToBringDefCloserTo
].push_back(Def
);
291 MBB
->splice(UseI
, MBB
, DefI
);
294 // Sort the defs for users of multiple defs lexographically.
295 for (const auto &E
: MultiUsers
) {
298 std::find_if(MBB
->instr_begin(), MBB
->instr_end(),
299 [&](MachineInstr
&MI
) -> bool { return &MI
== E
.first
; });
301 if (UseI
== MBB
->instr_end())
305 dbgs() << "Rescheduling Multi-Use Instructions Lexographically.";);
306 Changed
|= rescheduleLexographically(
307 E
.second
, MBB
, [&]() -> MachineBasicBlock::iterator
{ return UseI
; });
310 PseudoIdempotentInstCount
= PseudoIdempotentInstructions
.size();
312 dbgs() << "Rescheduling Idempotent Instructions Lexographically.";);
313 Changed
|= rescheduleLexographically(
314 PseudoIdempotentInstructions
, MBB
,
315 [&]() -> MachineBasicBlock::iterator
{ return MBB
->begin(); });
320 static bool propagateLocalCopies(MachineBasicBlock
*MBB
) {
321 bool Changed
= false;
322 MachineRegisterInfo
&MRI
= MBB
->getParent()->getRegInfo();
324 std::vector
<MachineInstr
*> Copies
;
325 for (MachineInstr
&MI
: MBB
->instrs()) {
327 Copies
.push_back(&MI
);
330 for (MachineInstr
*MI
: Copies
) {
332 if (!MI
->getOperand(0).isReg())
334 if (!MI
->getOperand(1).isReg())
337 const unsigned Dst
= MI
->getOperand(0).getReg();
338 const unsigned Src
= MI
->getOperand(1).getReg();
340 if (!TargetRegisterInfo::isVirtualRegister(Dst
))
342 if (!TargetRegisterInfo::isVirtualRegister(Src
))
344 if (MRI
.getRegClass(Dst
) != MRI
.getRegClass(Src
))
347 for (auto UI
= MRI
.use_begin(Dst
); UI
!= MRI
.use_end(); ++UI
) {
348 MachineOperand
*MO
= &*UI
;
353 MI
->eraseFromParent();
359 /// Here we find our candidates. What makes an interesting candidate?
360 /// An candidate for a canonicalization tree root is normally any kind of
361 /// instruction that causes side effects such as a store to memory or a copy to
362 /// a physical register or a return instruction. We use these as an expression
363 /// tree root that we walk inorder to build a canonical walk which should result
364 /// in canoncal vreg renaming.
365 static std::vector
<MachineInstr
*> populateCandidates(MachineBasicBlock
*MBB
) {
366 std::vector
<MachineInstr
*> Candidates
;
367 MachineRegisterInfo
&MRI
= MBB
->getParent()->getRegInfo();
369 for (auto II
= MBB
->begin(), IE
= MBB
->end(); II
!= IE
; ++II
) {
370 MachineInstr
*MI
= &*II
;
372 bool DoesMISideEffect
= false;
374 if (MI
->getNumOperands() > 0 && MI
->getOperand(0).isReg()) {
375 const unsigned Dst
= MI
->getOperand(0).getReg();
376 DoesMISideEffect
|= !TargetRegisterInfo::isVirtualRegister(Dst
);
378 for (auto UI
= MRI
.use_begin(Dst
); UI
!= MRI
.use_end(); ++UI
) {
379 if (DoesMISideEffect
)
381 DoesMISideEffect
|= (UI
->getParent()->getParent() != MI
->getParent());
385 if (!MI
->mayStore() && !MI
->isBranch() && !DoesMISideEffect
)
388 LLVM_DEBUG(dbgs() << "Found Candidate: "; MI
->dump(););
389 Candidates
.push_back(MI
);
395 static void doCandidateWalk(std::vector
<TypedVReg
> &VRegs
,
396 std::queue
<TypedVReg
> &RegQueue
,
397 std::vector
<MachineInstr
*> &VisitedMIs
,
398 const MachineBasicBlock
*MBB
) {
400 const MachineFunction
&MF
= *MBB
->getParent();
401 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
403 while (!RegQueue
.empty()) {
405 auto TReg
= RegQueue
.front();
408 if (TReg
.isFrameIndex()) {
409 LLVM_DEBUG(dbgs() << "Popping frame index.\n";);
410 VRegs
.push_back(TypedVReg(RSE_FrameIndex
));
414 assert(TReg
.isReg() && "Expected vreg or physreg.");
415 unsigned Reg
= TReg
.getReg();
417 if (TargetRegisterInfo::isVirtualRegister(Reg
)) {
419 dbgs() << "Popping vreg ";
420 MRI
.def_begin(Reg
)->dump();
424 if (!llvm::any_of(VRegs
, [&](const TypedVReg
&TR
) {
425 return TR
.isReg() && TR
.getReg() == Reg
;
427 VRegs
.push_back(TypedVReg(Reg
));
430 LLVM_DEBUG(dbgs() << "Popping physreg.\n";);
431 VRegs
.push_back(TypedVReg(Reg
));
435 for (auto RI
= MRI
.def_begin(Reg
), RE
= MRI
.def_end(); RI
!= RE
; ++RI
) {
436 MachineInstr
*Def
= RI
->getParent();
438 if (Def
->getParent() != MBB
)
441 if (llvm::any_of(VisitedMIs
,
442 [&](const MachineInstr
*VMI
) { return Def
== VMI
; })) {
447 dbgs() << "\n========================\n";
448 dbgs() << "Visited MI: ";
450 dbgs() << "BB Name: " << Def
->getParent()->getName() << "\n";
451 dbgs() << "\n========================\n";
453 VisitedMIs
.push_back(Def
);
454 for (unsigned I
= 1, E
= Def
->getNumOperands(); I
!= E
; ++I
) {
456 MachineOperand
&MO
= Def
->getOperand(I
);
458 LLVM_DEBUG(dbgs() << "Pushing frame index.\n";);
459 RegQueue
.push(TypedVReg(RSE_FrameIndex
));
464 RegQueue
.push(TypedVReg(MO
.getReg()));
471 class NamedVRegCursor
{
472 MachineRegisterInfo
&MRI
;
473 unsigned virtualVRegNumber
;
476 NamedVRegCursor(MachineRegisterInfo
&MRI
) : MRI(MRI
) {
477 unsigned VRegGapIndex
= 0;
478 const unsigned VR_GAP
= (++VRegGapIndex
* 1000);
480 unsigned I
= MRI
.createIncompleteVirtualRegister();
481 const unsigned E
= (((I
+ VR_GAP
) / VR_GAP
) + 1) * VR_GAP
;
483 virtualVRegNumber
= E
;
487 unsigned VRegGapIndex
= 1;
488 const unsigned VR_GAP
= (++VRegGapIndex
* 1000);
490 unsigned I
= virtualVRegNumber
;
491 const unsigned E
= (((I
+ VR_GAP
) / VR_GAP
) + 1) * VR_GAP
;
493 virtualVRegNumber
= E
;
496 unsigned getVirtualVReg() const { return virtualVRegNumber
; }
498 unsigned incrementVirtualVReg(unsigned incr
= 1) {
499 virtualVRegNumber
+= incr
;
500 return virtualVRegNumber
;
503 unsigned createVirtualRegister(const TargetRegisterClass
*RC
) {
505 raw_string_ostream
OS(S
);
506 OS
<< "namedVReg" << (virtualVRegNumber
& ~0x80000000);
510 return MRI
.createVirtualRegister(RC
, OS
.str());
515 static std::map
<unsigned, unsigned>
516 GetVRegRenameMap(const std::vector
<TypedVReg
> &VRegs
,
517 const std::vector
<unsigned> &renamedInOtherBB
,
518 MachineRegisterInfo
&MRI
, NamedVRegCursor
&NVC
) {
519 std::map
<unsigned, unsigned> VRegRenameMap
;
520 bool FirstCandidate
= true;
522 for (auto &vreg
: VRegs
) {
523 if (vreg
.isFrameIndex()) {
524 // We skip one vreg for any frame index because there is a good chance
525 // (especially when comparing SelectionDAG to GlobalISel generated MIR)
526 // that in the other file we are just getting an incoming vreg that comes
527 // from a copy from a frame index. So it's safe to skip by one.
528 unsigned LastRenameReg
= NVC
.incrementVirtualVReg();
530 LLVM_DEBUG(dbgs() << "Skipping rename for FI " << LastRenameReg
<< "\n";);
532 } else if (vreg
.isCandidate()) {
534 // After the first candidate, for every subsequent candidate, we skip mod
535 // 10 registers so that the candidates are more likely to start at the
536 // same vreg number making it more likely that the canonical walk from the
537 // candidate insruction. We don't need to skip from the first candidate of
538 // the BasicBlock because we already skip ahead several vregs for each BB.
539 unsigned LastRenameReg
= NVC
.getVirtualVReg();
541 NVC
.incrementVirtualVReg(LastRenameReg
% 10);
542 FirstCandidate
= false;
544 } else if (!TargetRegisterInfo::isVirtualRegister(vreg
.getReg())) {
545 unsigned LastRenameReg
= NVC
.incrementVirtualVReg();
548 dbgs() << "Skipping rename for Phys Reg " << LastRenameReg
<< "\n";
553 auto Reg
= vreg
.getReg();
554 if (llvm::find(renamedInOtherBB
, Reg
) != renamedInOtherBB
.end()) {
555 LLVM_DEBUG(dbgs() << "Vreg " << Reg
556 << " already renamed in other BB.\n";);
560 auto Rename
= NVC
.createVirtualRegister(MRI
.getRegClass(Reg
));
562 if (VRegRenameMap
.find(Reg
) == VRegRenameMap
.end()) {
563 LLVM_DEBUG(dbgs() << "Mapping vreg ";);
564 if (MRI
.reg_begin(Reg
) != MRI
.reg_end()) {
565 LLVM_DEBUG(auto foo
= &*MRI
.reg_begin(Reg
); foo
->dump(););
567 LLVM_DEBUG(dbgs() << Reg
;);
569 LLVM_DEBUG(dbgs() << " to ";);
570 if (MRI
.reg_begin(Rename
) != MRI
.reg_end()) {
571 LLVM_DEBUG(auto foo
= &*MRI
.reg_begin(Rename
); foo
->dump(););
573 LLVM_DEBUG(dbgs() << Rename
;);
575 LLVM_DEBUG(dbgs() << "\n";);
577 VRegRenameMap
.insert(std::pair
<unsigned, unsigned>(Reg
, Rename
));
581 return VRegRenameMap
;
584 static bool doVRegRenaming(std::vector
<unsigned> &RenamedInOtherBB
,
585 const std::map
<unsigned, unsigned> &VRegRenameMap
,
586 MachineRegisterInfo
&MRI
) {
587 bool Changed
= false;
588 for (auto I
= VRegRenameMap
.begin(), E
= VRegRenameMap
.end(); I
!= E
; ++I
) {
590 auto VReg
= I
->first
;
591 auto Rename
= I
->second
;
593 RenamedInOtherBB
.push_back(Rename
);
595 std::vector
<MachineOperand
*> RenameMOs
;
596 for (auto &MO
: MRI
.reg_operands(VReg
)) {
597 RenameMOs
.push_back(&MO
);
600 for (auto *MO
: RenameMOs
) {
605 MO
->setIsKill(false);
612 static bool doDefKillClear(MachineBasicBlock
*MBB
) {
613 bool Changed
= false;
615 for (auto &MI
: *MBB
) {
616 for (auto &MO
: MI
.operands()) {
619 if (!MO
.isDef() && MO
.isKill()) {
624 if (MO
.isDef() && MO
.isDead()) {
634 static bool runOnBasicBlock(MachineBasicBlock
*MBB
,
635 std::vector
<StringRef
> &bbNames
,
636 std::vector
<unsigned> &renamedInOtherBB
,
637 unsigned &basicBlockNum
, unsigned &VRegGapIndex
,
638 NamedVRegCursor
&NVC
) {
640 if (CanonicalizeBasicBlockNumber
!= ~0U) {
641 if (CanonicalizeBasicBlockNumber
!= basicBlockNum
++)
643 LLVM_DEBUG(dbgs() << "\n Canonicalizing BasicBlock " << MBB
->getName()
647 if (llvm::find(bbNames
, MBB
->getName()) != bbNames
.end()) {
649 dbgs() << "Found potentially duplicate BasicBlocks: " << MBB
->getName()
656 dbgs() << "\n\n NEW BASIC BLOCK: " << MBB
->getName() << " \n\n";
657 dbgs() << "\n\n================================================\n\n";
660 bool Changed
= false;
661 MachineFunction
&MF
= *MBB
->getParent();
662 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
664 bbNames
.push_back(MBB
->getName());
665 LLVM_DEBUG(dbgs() << "\n\n NEW BASIC BLOCK: " << MBB
->getName() << "\n\n";);
667 LLVM_DEBUG(dbgs() << "MBB Before Canonical Copy Propagation:\n";
669 Changed
|= propagateLocalCopies(MBB
);
670 LLVM_DEBUG(dbgs() << "MBB After Canonical Copy Propagation:\n"; MBB
->dump(););
672 LLVM_DEBUG(dbgs() << "MBB Before Scheduling:\n"; MBB
->dump(););
673 unsigned IdempotentInstCount
= 0;
674 Changed
|= rescheduleCanonically(IdempotentInstCount
, MBB
);
675 LLVM_DEBUG(dbgs() << "MBB After Scheduling:\n"; MBB
->dump(););
677 std::vector
<MachineInstr
*> Candidates
= populateCandidates(MBB
);
678 std::vector
<MachineInstr
*> VisitedMIs
;
679 llvm::copy(Candidates
, std::back_inserter(VisitedMIs
));
681 std::vector
<TypedVReg
> VRegs
;
682 for (auto candidate
: Candidates
) {
683 VRegs
.push_back(TypedVReg(RSE_NewCandidate
));
685 std::queue
<TypedVReg
> RegQueue
;
687 // Here we walk the vreg operands of a non-root node along our walk.
688 // The root nodes are the original candidates (stores normally).
689 // These are normally not the root nodes (except for the case of copies to
690 // physical registers).
691 for (unsigned i
= 1; i
< candidate
->getNumOperands(); i
++) {
692 if (candidate
->mayStore() || candidate
->isBranch())
695 MachineOperand
&MO
= candidate
->getOperand(i
);
696 if (!(MO
.isReg() && TargetRegisterInfo::isVirtualRegister(MO
.getReg())))
699 LLVM_DEBUG(dbgs() << "Enqueue register"; MO
.dump(); dbgs() << "\n";);
700 RegQueue
.push(TypedVReg(MO
.getReg()));
703 // Here we walk the root candidates. We start from the 0th operand because
704 // the root is normally a store to a vreg.
705 for (unsigned i
= 0; i
< candidate
->getNumOperands(); i
++) {
707 if (!candidate
->mayStore() && !candidate
->isBranch())
710 MachineOperand
&MO
= candidate
->getOperand(i
);
712 // TODO: Do we want to only add vregs here?
713 if (!MO
.isReg() && !MO
.isFI())
716 LLVM_DEBUG(dbgs() << "Enqueue Reg/FI"; MO
.dump(); dbgs() << "\n";);
718 RegQueue
.push(MO
.isReg() ? TypedVReg(MO
.getReg())
719 : TypedVReg(RSE_FrameIndex
));
722 doCandidateWalk(VRegs
, RegQueue
, VisitedMIs
, MBB
);
725 // If we have populated no vregs to rename then bail.
726 // The rest of this function does the vreg remaping.
727 if (VRegs
.size() == 0)
730 auto VRegRenameMap
= GetVRegRenameMap(VRegs
, renamedInOtherBB
, MRI
, NVC
);
731 Changed
|= doVRegRenaming(renamedInOtherBB
, VRegRenameMap
, MRI
);
733 // Here we renumber the def vregs for the idempotent instructions from the top
734 // of the MachineBasicBlock so that they are named in the order that we sorted
735 // them alphabetically. Eventually we wont need SkipVRegs because we will use
736 // named vregs instead.
739 auto MII
= MBB
->begin();
740 for (unsigned i
= 0; i
< IdempotentInstCount
&& MII
!= MBB
->end(); ++i
) {
741 MachineInstr
&MI
= *MII
++;
743 unsigned vRegToRename
= MI
.getOperand(0).getReg();
744 auto Rename
= NVC
.createVirtualRegister(MRI
.getRegClass(vRegToRename
));
746 std::vector
<MachineOperand
*> RenameMOs
;
747 for (auto &MO
: MRI
.reg_operands(vRegToRename
)) {
748 RenameMOs
.push_back(&MO
);
751 for (auto *MO
: RenameMOs
) {
756 Changed
|= doDefKillClear(MBB
);
758 LLVM_DEBUG(dbgs() << "Updated MachineBasicBlock:\n"; MBB
->dump();
761 dbgs() << "\n\n================================================\n\n");
765 bool MIRCanonicalizer::runOnMachineFunction(MachineFunction
&MF
) {
767 static unsigned functionNum
= 0;
768 if (CanonicalizeFunctionNumber
!= ~0U) {
769 if (CanonicalizeFunctionNumber
!= functionNum
++)
771 LLVM_DEBUG(dbgs() << "\n Canonicalizing Function " << MF
.getName()
775 // we need a valid vreg to create a vreg type for skipping all those
776 // stray vreg numbers so reach alignment/canonical vreg values.
777 std::vector
<MachineBasicBlock
*> RPOList
= GetRPOList(MF
);
780 dbgs() << "\n\n NEW MACHINE FUNCTION: " << MF
.getName() << " \n\n";
781 dbgs() << "\n\n================================================\n\n";
782 dbgs() << "Total Basic Blocks: " << RPOList
.size() << "\n";
784 : RPOList
) { dbgs() << MBB
->getName() << "\n"; } dbgs()
785 << "\n\n================================================\n\n";);
787 std::vector
<StringRef
> BBNames
;
788 std::vector
<unsigned> RenamedInOtherBB
;
793 bool Changed
= false;
795 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
796 NamedVRegCursor
NVC(MRI
);
797 for (auto MBB
: RPOList
)
799 runOnBasicBlock(MBB
, BBNames
, RenamedInOtherBB
, BBNum
, GapIdx
, NVC
);