Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / CodeGen / MIRCanonicalizerPass.cpp
blobe7db863e1f9f827d99d55096875ef10f9fdbd1be
1 //===-------------- MIRCanonicalizer.cpp - MIR Canonicalizer --------------===//
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 // 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.
16 // Basic Usage:
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"
34 #include <queue>
36 using namespace llvm;
38 namespace llvm {
39 extern char &MIRCanonicalizerID;
40 } // namespace llvm
42 #define DEBUG_TYPE "mir-canonicalizer"
44 static cl::opt<unsigned>
45 CanonicalizeFunctionNumber("canon-nth-function", cl::Hidden, cl::init(~0u),
46 cl::value_desc("N"),
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."));
53 namespace {
55 class MIRCanonicalizer : public MachineFunctionPass {
56 public:
57 static char ID;
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 {
65 AU.setPreservesCFG();
66 MachineFunctionPass::getAnalysisUsage(AU);
69 bool runOnMachineFunction(MachineFunction &MF) override;
72 } // end anonymous namespace
74 enum VRType { RSE_Reg = 0, RSE_FrameIndex, RSE_NewCandidate };
75 class TypedVReg {
76 VRType type;
77 unsigned reg;
79 public:
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.");
92 return reg;
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);
113 return RPOList;
116 static bool
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) {
126 std::string S;
127 raw_string_ostream OS(S);
128 II->print(OS);
129 OS.flush();
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) {
143 LLVM_DEBUG({
144 dbgs() << "Splicing ";
145 II.second->dump();
146 dbgs() << " right before: ";
147 getPos()->dump();
150 Changed = true;
151 MBB->splice(getPos(), MBB, II.second);
154 return Changed;
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) {
164 unsigned i = 0;
165 for (auto &CurMI : *MI.getParent()) {
166 if (&CurMI == &MI)
167 return i;
168 i++;
170 return ~0U;
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);
186 if (!MO.isReg())
187 continue;
189 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
190 continue;
192 if (!MO.isDef())
193 continue;
195 PhysRegDefs.push_back(MO.getReg());
199 for (auto *II : Instructions) {
200 if (II->getNumOperands() == 0)
201 continue;
202 if (II->mayLoadOrStore())
203 continue;
205 MachineOperand &MO = II->getOperand(0);
206 if (!MO.isReg() || !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
207 continue;
208 if (!MO.isDef())
209 continue;
211 bool IsPseudoIdempotent = true;
212 for (unsigned i = 1; i < II->getNumOperands(); i++) {
214 if (II->getOperand(i).isImm()) {
215 continue;
218 if (II->getOperand(i).isReg()) {
219 if (!TargetRegisterInfo::isVirtualRegister(II->getOperand(i).getReg()))
220 if (llvm::find(PhysRegDefs, II->getOperand(i).getReg()) ==
221 PhysRegDefs.end()) {
222 continue;
226 IsPseudoIdempotent = false;
227 break;
230 if (IsPseudoIdempotent) {
231 PseudoIdempotentInstructions.push_back(II);
232 continue;
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())
249 continue;
250 if (DefLoc >= UseLoc)
251 continue;
253 if (Delta < Distance) {
254 Distance = Delta;
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)
266 break;
268 if (&*BBI == Def) {
269 DefI = BBI;
270 continue;
273 if (&*BBI == UseToBringDefCloserTo) {
274 UseI = BBI;
275 continue;
279 if (DefI == BBE || UseI == BBE)
280 continue;
282 LLVM_DEBUG({
283 dbgs() << "Splicing ";
284 DefI->dump();
285 dbgs() << " right before: ";
286 UseI->dump();
289 MultiUsers[UseToBringDefCloserTo].push_back(Def);
290 Changed = true;
291 MBB->splice(UseI, MBB, DefI);
294 // Sort the defs for users of multiple defs lexographically.
295 for (const auto &E : MultiUsers) {
297 auto UseI =
298 std::find_if(MBB->instr_begin(), MBB->instr_end(),
299 [&](MachineInstr &MI) -> bool { return &MI == E.first; });
301 if (UseI == MBB->instr_end())
302 continue;
304 LLVM_DEBUG(
305 dbgs() << "Rescheduling Multi-Use Instructions Lexographically.";);
306 Changed |= rescheduleLexographically(
307 E.second, MBB, [&]() -> MachineBasicBlock::iterator { return UseI; });
310 PseudoIdempotentInstCount = PseudoIdempotentInstructions.size();
311 LLVM_DEBUG(
312 dbgs() << "Rescheduling Idempotent Instructions Lexographically.";);
313 Changed |= rescheduleLexographically(
314 PseudoIdempotentInstructions, MBB,
315 [&]() -> MachineBasicBlock::iterator { return MBB->begin(); });
317 return Changed;
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()) {
326 if (MI.isCopy())
327 Copies.push_back(&MI);
330 for (MachineInstr *MI : Copies) {
332 if (!MI->getOperand(0).isReg())
333 continue;
334 if (!MI->getOperand(1).isReg())
335 continue;
337 const unsigned Dst = MI->getOperand(0).getReg();
338 const unsigned Src = MI->getOperand(1).getReg();
340 if (!TargetRegisterInfo::isVirtualRegister(Dst))
341 continue;
342 if (!TargetRegisterInfo::isVirtualRegister(Src))
343 continue;
344 if (MRI.getRegClass(Dst) != MRI.getRegClass(Src))
345 continue;
347 for (auto UI = MRI.use_begin(Dst); UI != MRI.use_end(); ++UI) {
348 MachineOperand *MO = &*UI;
349 MO->setReg(Src);
350 Changed = true;
353 MI->eraseFromParent();
356 return Changed;
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)
380 break;
381 DoesMISideEffect |= (UI->getParent()->getParent() != MI->getParent());
385 if (!MI->mayStore() && !MI->isBranch() && !DoesMISideEffect)
386 continue;
388 LLVM_DEBUG(dbgs() << "Found Candidate: "; MI->dump(););
389 Candidates.push_back(MI);
392 return Candidates;
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();
406 RegQueue.pop();
408 if (TReg.isFrameIndex()) {
409 LLVM_DEBUG(dbgs() << "Popping frame index.\n";);
410 VRegs.push_back(TypedVReg(RSE_FrameIndex));
411 continue;
414 assert(TReg.isReg() && "Expected vreg or physreg.");
415 unsigned Reg = TReg.getReg();
417 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
418 LLVM_DEBUG({
419 dbgs() << "Popping vreg ";
420 MRI.def_begin(Reg)->dump();
421 dbgs() << "\n";
424 if (!llvm::any_of(VRegs, [&](const TypedVReg &TR) {
425 return TR.isReg() && TR.getReg() == Reg;
426 })) {
427 VRegs.push_back(TypedVReg(Reg));
429 } else {
430 LLVM_DEBUG(dbgs() << "Popping physreg.\n";);
431 VRegs.push_back(TypedVReg(Reg));
432 continue;
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)
439 continue;
441 if (llvm::any_of(VisitedMIs,
442 [&](const MachineInstr *VMI) { return Def == VMI; })) {
443 break;
446 LLVM_DEBUG({
447 dbgs() << "\n========================\n";
448 dbgs() << "Visited MI: ";
449 Def->dump();
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);
457 if (MO.isFI()) {
458 LLVM_DEBUG(dbgs() << "Pushing frame index.\n";);
459 RegQueue.push(TypedVReg(RSE_FrameIndex));
462 if (!MO.isReg())
463 continue;
464 RegQueue.push(TypedVReg(MO.getReg()));
470 namespace {
471 class NamedVRegCursor {
472 MachineRegisterInfo &MRI;
473 unsigned virtualVRegNumber;
475 public:
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;
486 void SkipVRegs() {
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) {
504 std::string S;
505 raw_string_ostream OS(S);
506 OS << "namedVReg" << (virtualVRegNumber & ~0x80000000);
507 OS.flush();
508 virtualVRegNumber++;
510 return MRI.createVirtualRegister(RC, OS.str());
513 } // namespace
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();
529 (void)LastRenameReg;
530 LLVM_DEBUG(dbgs() << "Skipping rename for FI " << LastRenameReg << "\n";);
531 continue;
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();
540 if (FirstCandidate)
541 NVC.incrementVirtualVReg(LastRenameReg % 10);
542 FirstCandidate = false;
543 continue;
544 } else if (!TargetRegisterInfo::isVirtualRegister(vreg.getReg())) {
545 unsigned LastRenameReg = NVC.incrementVirtualVReg();
546 (void)LastRenameReg;
547 LLVM_DEBUG({
548 dbgs() << "Skipping rename for Phys Reg " << LastRenameReg << "\n";
550 continue;
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";);
557 continue;
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(););
566 } else {
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(););
572 } else {
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) {
601 Changed = true;
602 MO->setReg(Rename);
604 if (!MO->isDef())
605 MO->setIsKill(false);
609 return Changed;
612 static bool doDefKillClear(MachineBasicBlock *MBB) {
613 bool Changed = false;
615 for (auto &MI : *MBB) {
616 for (auto &MO : MI.operands()) {
617 if (!MO.isReg())
618 continue;
619 if (!MO.isDef() && MO.isKill()) {
620 Changed = true;
621 MO.setIsKill(false);
624 if (MO.isDef() && MO.isDead()) {
625 Changed = true;
626 MO.setIsDead(false);
631 return Changed;
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++)
642 return false;
643 LLVM_DEBUG(dbgs() << "\n Canonicalizing BasicBlock " << MBB->getName()
644 << "\n";);
647 if (llvm::find(bbNames, MBB->getName()) != bbNames.end()) {
648 LLVM_DEBUG({
649 dbgs() << "Found potentially duplicate BasicBlocks: " << MBB->getName()
650 << "\n";
652 return false;
655 LLVM_DEBUG({
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";
668 MBB->dump(););
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())
693 break;
695 MachineOperand &MO = candidate->getOperand(i);
696 if (!(MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())))
697 continue;
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())
708 break;
710 MachineOperand &MO = candidate->getOperand(i);
712 // TODO: Do we want to only add vregs here?
713 if (!MO.isReg() && !MO.isFI())
714 continue;
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)
728 return Changed;
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.
737 NVC.SkipVRegs();
739 auto MII = MBB->begin();
740 for (unsigned i = 0; i < IdempotentInstCount && MII != MBB->end(); ++i) {
741 MachineInstr &MI = *MII++;
742 Changed = true;
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) {
752 MO->setReg(Rename);
756 Changed |= doDefKillClear(MBB);
758 LLVM_DEBUG(dbgs() << "Updated MachineBasicBlock:\n"; MBB->dump();
759 dbgs() << "\n";);
760 LLVM_DEBUG(
761 dbgs() << "\n\n================================================\n\n");
762 return Changed;
765 bool MIRCanonicalizer::runOnMachineFunction(MachineFunction &MF) {
767 static unsigned functionNum = 0;
768 if (CanonicalizeFunctionNumber != ~0U) {
769 if (CanonicalizeFunctionNumber != functionNum++)
770 return false;
771 LLVM_DEBUG(dbgs() << "\n Canonicalizing Function " << MF.getName()
772 << "\n";);
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);
779 LLVM_DEBUG(
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";
783 for (auto MBB
784 : RPOList) { dbgs() << MBB->getName() << "\n"; } dbgs()
785 << "\n\n================================================\n\n";);
787 std::vector<StringRef> BBNames;
788 std::vector<unsigned> RenamedInOtherBB;
790 unsigned GapIdx = 0;
791 unsigned BBNum = 0;
793 bool Changed = false;
795 MachineRegisterInfo &MRI = MF.getRegInfo();
796 NamedVRegCursor NVC(MRI);
797 for (auto MBB : RPOList)
798 Changed |=
799 runOnBasicBlock(MBB, BBNames, RenamedInOtherBB, BBNum, GapIdx, NVC);
801 return Changed;