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.
15 //
16 // Basic Usage:
17 //
18 // llc -o - -run-pass mir-canonicalizer example.mir
19 //
20 // Reorders instructions canonically.
21 // Renames virtual register operands canonically.
22 // Strips certain MIR artifacts (optionally).
23 //
24 //===----------------------------------------------------------------------===//
25 
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"
33 
34 #include <queue>
35 
36 using namespace llvm;
37 
38 namespace llvm {
39 extern char &MIRCanonicalizerID;
40 } // namespace llvm
41 
42 #define DEBUG_TYPE "mir-canonicalizer"
43 
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."));
48 
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."));
52 
53 namespace {
54 
55 class MIRCanonicalizer : public MachineFunctionPass {
56 public:
57   static char ID;
58   MIRCanonicalizer() : MachineFunctionPass(ID) {}
59 
60   StringRef getPassName() const override {
61     return "Rename register operands in a canonical ordering.";
62   }
63 
64   void getAnalysisUsage(AnalysisUsage &AU) const override {
65     AU.setPreservesCFG();
66     MachineFunctionPass::getAnalysisUsage(AU);
67   }
68 
69   bool runOnMachineFunction(MachineFunction &MF) override;
70 };
71 
72 } // end anonymous namespace
73 
74 enum VRType { RSE_Reg = 0, RSE_FrameIndex, RSE_NewCandidate };
75 class TypedVReg {
76   VRType type;
77   unsigned reg;
78 
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.");
83   }
84 
85   bool isReg() const { return type == RSE_Reg; }
86   bool isFrameIndex() const { return type == RSE_FrameIndex; }
87   bool isCandidate() const { return type == RSE_NewCandidate; }
88 
89   VRType getType() const { return type; }
90   unsigned getReg() const {
91     assert(this->isReg() && "Expected a virtual or physical register.");
92     return reg;
93   }
94 };
95 
96 char MIRCanonicalizer::ID;
97 
98 char &llvm::MIRCanonicalizerID = MIRCanonicalizer::ID;
99 
100 INITIALIZE_PASS_BEGIN(MIRCanonicalizer, "mir-canonicalizer",
101                       "Rename Register Operands Canonically", false, false)
102 
103 INITIALIZE_PASS_END(MIRCanonicalizer, "mir-canonicalizer",
104                     "Rename Register Operands Canonically", false, false)
105 
106 static std::vector<MachineBasicBlock *> GetRPOList(MachineFunction &MF) {
107   if (MF.empty())
108     return {};
109   ReversePostOrderTraversal<MachineBasicBlock *> RPOT(&*MF.begin());
110   std::vector<MachineBasicBlock *> RPOList;
111   for (auto MBB : RPOT) {
112     RPOList.push_back(MBB);
113   }
114 
115   return RPOList;
116 }
117 
118 static bool
119 rescheduleLexographically(std::vector<MachineInstr *> instructions,
120                           MachineBasicBlock *MBB,
121                           std::function<MachineBasicBlock::iterator()> getPos) {
122 
123   bool Changed = false;
124   using StringInstrPair = std::pair<std::string, MachineInstr *>;
125   std::vector<StringInstrPair> StringInstrMap;
126 
127   for (auto *II : instructions) {
128     std::string S;
129     raw_string_ostream OS(S);
130     II->print(OS);
131     OS.flush();
132 
133     // Trim the assignment, or start from the begining in the case of a store.
134     const size_t i = S.find("=");
135     StringInstrMap.push_back({(i == std::string::npos) ? S : S.substr(i), II});
136   }
137 
138   llvm::sort(StringInstrMap,
139              [](const StringInstrPair &a, const StringInstrPair &b) -> bool {
140                return (a.first < b.first);
141              });
142 
143   for (auto &II : StringInstrMap) {
144 
145     LLVM_DEBUG({
146       dbgs() << "Splicing ";
147       II.second->dump();
148       dbgs() << " right before: ";
149       getPos()->dump();
150     });
151 
152     Changed = true;
153     MBB->splice(getPos(), MBB, II.second);
154   }
155 
156   return Changed;
157 }
158 
159 static bool rescheduleCanonically(unsigned &PseudoIdempotentInstCount,
160                                   MachineBasicBlock *MBB) {
161 
162   bool Changed = false;
163 
164   // Calculates the distance of MI from the begining of its parent BB.
165   auto getInstrIdx = [](const MachineInstr &MI) {
166     unsigned i = 0;
167     for (auto &CurMI : *MI.getParent()) {
168       if (&CurMI == &MI)
169         return i;
170       i++;
171     }
172     return ~0U;
173   };
174 
175   // Pre-Populate vector of instructions to reschedule so that we don't
176   // clobber the iterator.
177   std::vector<MachineInstr *> Instructions;
178   for (auto &MI : *MBB) {
179     Instructions.push_back(&MI);
180   }
181 
182   std::map<MachineInstr *, std::vector<MachineInstr *>> MultiUsers;
183   std::vector<MachineInstr *> PseudoIdempotentInstructions;
184   std::vector<unsigned> PhysRegDefs;
185   for (auto *II : Instructions) {
186     for (unsigned i = 1; i < II->getNumOperands(); i++) {
187       MachineOperand &MO = II->getOperand(i);
188       if (!MO.isReg())
189         continue;
190 
191       if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
192         continue;
193 
194       if (!MO.isDef())
195         continue;
196 
197       PhysRegDefs.push_back(MO.getReg());
198     }
199   }
200 
201   for (auto *II : Instructions) {
202     if (II->getNumOperands() == 0)
203       continue;
204     if (II->mayLoadOrStore())
205       continue;
206 
207     MachineOperand &MO = II->getOperand(0);
208     if (!MO.isReg() || !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
209       continue;
210     if (!MO.isDef())
211       continue;
212 
213     bool IsPseudoIdempotent = true;
214     for (unsigned i = 1; i < II->getNumOperands(); i++) {
215 
216       if (II->getOperand(i).isImm()) {
217         continue;
218       }
219 
220       if (II->getOperand(i).isReg()) {
221         if (!TargetRegisterInfo::isVirtualRegister(II->getOperand(i).getReg()))
222           if (llvm::find(PhysRegDefs, II->getOperand(i).getReg()) ==
223               PhysRegDefs.end()) {
224             continue;
225           }
226       }
227 
228       IsPseudoIdempotent = false;
229       break;
230     }
231 
232     if (IsPseudoIdempotent) {
233       PseudoIdempotentInstructions.push_back(II);
234       continue;
235     }
236 
237     LLVM_DEBUG(dbgs() << "Operand " << 0 << " of "; II->dump(); MO.dump(););
238 
239     MachineInstr *Def = II;
240     unsigned Distance = ~0U;
241     MachineInstr *UseToBringDefCloserTo = nullptr;
242     MachineRegisterInfo *MRI = &MBB->getParent()->getRegInfo();
243     for (auto &UO : MRI->use_nodbg_operands(MO.getReg())) {
244       MachineInstr *UseInst = UO.getParent();
245 
246       const unsigned DefLoc = getInstrIdx(*Def);
247       const unsigned UseLoc = getInstrIdx(*UseInst);
248       const unsigned Delta = (UseLoc - DefLoc);
249 
250       if (UseInst->getParent() != Def->getParent())
251         continue;
252       if (DefLoc >= UseLoc)
253         continue;
254 
255       if (Delta < Distance) {
256         Distance = Delta;
257         UseToBringDefCloserTo = UseInst;
258       }
259     }
260 
261     const auto BBE = MBB->instr_end();
262     MachineBasicBlock::iterator DefI = BBE;
263     MachineBasicBlock::iterator UseI = BBE;
264 
265     for (auto BBI = MBB->instr_begin(); BBI != BBE; ++BBI) {
266 
267       if (DefI != BBE && UseI != BBE)
268         break;
269 
270       if (&*BBI == Def) {
271         DefI = BBI;
272         continue;
273       }
274 
275       if (&*BBI == UseToBringDefCloserTo) {
276         UseI = BBI;
277         continue;
278       }
279     }
280 
281     if (DefI == BBE || UseI == BBE)
282       continue;
283 
284     LLVM_DEBUG({
285       dbgs() << "Splicing ";
286       DefI->dump();
287       dbgs() << " right before: ";
288       UseI->dump();
289     });
290 
291     MultiUsers[UseToBringDefCloserTo].push_back(Def);
292     Changed = true;
293     MBB->splice(UseI, MBB, DefI);
294   }
295 
296   // Sort the defs for users of multiple defs lexographically.
297   for (const auto &E : MultiUsers) {
298 
299     auto UseI =
300         std::find_if(MBB->instr_begin(), MBB->instr_end(),
301                      [&](MachineInstr &MI) -> bool { return &MI == E.first; });
302 
303     if (UseI == MBB->instr_end())
304       continue;
305 
306     LLVM_DEBUG(
307         dbgs() << "Rescheduling Multi-Use Instructions Lexographically.";);
308     Changed |= rescheduleLexographically(
309         E.second, MBB, [&]() -> MachineBasicBlock::iterator { return UseI; });
310   }
311 
312   PseudoIdempotentInstCount = PseudoIdempotentInstructions.size();
313   LLVM_DEBUG(
314       dbgs() << "Rescheduling Idempotent Instructions Lexographically.";);
315   Changed |= rescheduleLexographically(
316       PseudoIdempotentInstructions, MBB,
317       [&]() -> MachineBasicBlock::iterator { return MBB->begin(); });
318 
319   return Changed;
320 }
321 
322 static bool propagateLocalCopies(MachineBasicBlock *MBB) {
323   bool Changed = false;
324   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
325 
326   std::vector<MachineInstr *> Copies;
327   for (MachineInstr &MI : MBB->instrs()) {
328     if (MI.isCopy())
329       Copies.push_back(&MI);
330   }
331 
332   for (MachineInstr *MI : Copies) {
333 
334     if (!MI->getOperand(0).isReg())
335       continue;
336     if (!MI->getOperand(1).isReg())
337       continue;
338 
339     const unsigned Dst = MI->getOperand(0).getReg();
340     const unsigned Src = MI->getOperand(1).getReg();
341 
342     if (!TargetRegisterInfo::isVirtualRegister(Dst))
343       continue;
344     if (!TargetRegisterInfo::isVirtualRegister(Src))
345       continue;
346     if (MRI.getRegClass(Dst) != MRI.getRegClass(Src))
347       continue;
348 
349     for (auto UI = MRI.use_begin(Dst); UI != MRI.use_end(); ++UI) {
350       MachineOperand *MO = &*UI;
351       MO->setReg(Src);
352       Changed = true;
353     }
354 
355     MI->eraseFromParent();
356   }
357 
358   return Changed;
359 }
360 
361 /// Here we find our candidates. What makes an interesting candidate?
362 /// An candidate for a canonicalization tree root is normally any kind of
363 /// instruction that causes side effects such as a store to memory or a copy to
364 /// a physical register or a return instruction. We use these as an expression
365 /// tree root that we walk inorder to build a canonical walk which should result
366 /// in canoncal vreg renaming.
367 static std::vector<MachineInstr *> populateCandidates(MachineBasicBlock *MBB) {
368   std::vector<MachineInstr *> Candidates;
369   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
370 
371   for (auto II = MBB->begin(), IE = MBB->end(); II != IE; ++II) {
372     MachineInstr *MI = &*II;
373 
374     bool DoesMISideEffect = false;
375 
376     if (MI->getNumOperands() > 0 && MI->getOperand(0).isReg()) {
377       const unsigned Dst = MI->getOperand(0).getReg();
378       DoesMISideEffect |= !TargetRegisterInfo::isVirtualRegister(Dst);
379 
380       for (auto UI = MRI.use_begin(Dst); UI != MRI.use_end(); ++UI) {
381         if (DoesMISideEffect)
382           break;
383         DoesMISideEffect |= (UI->getParent()->getParent() != MI->getParent());
384       }
385     }
386 
387     if (!MI->mayStore() && !MI->isBranch() && !DoesMISideEffect)
388       continue;
389 
390     LLVM_DEBUG(dbgs() << "Found Candidate:  "; MI->dump(););
391     Candidates.push_back(MI);
392   }
393 
394   return Candidates;
395 }
396 
397 static void doCandidateWalk(std::vector<TypedVReg> &VRegs,
398                             std::queue<TypedVReg> &RegQueue,
399                             std::vector<MachineInstr *> &VisitedMIs,
400                             const MachineBasicBlock *MBB) {
401 
402   const MachineFunction &MF = *MBB->getParent();
403   const MachineRegisterInfo &MRI = MF.getRegInfo();
404 
405   while (!RegQueue.empty()) {
406 
407     auto TReg = RegQueue.front();
408     RegQueue.pop();
409 
410     if (TReg.isFrameIndex()) {
411       LLVM_DEBUG(dbgs() << "Popping frame index.\n";);
412       VRegs.push_back(TypedVReg(RSE_FrameIndex));
413       continue;
414     }
415 
416     assert(TReg.isReg() && "Expected vreg or physreg.");
417     unsigned Reg = TReg.getReg();
418 
419     if (TargetRegisterInfo::isVirtualRegister(Reg)) {
420       LLVM_DEBUG({
421         dbgs() << "Popping vreg ";
422         MRI.def_begin(Reg)->dump();
423         dbgs() << "\n";
424       });
425 
426       if (!llvm::any_of(VRegs, [&](const TypedVReg &TR) {
427             return TR.isReg() && TR.getReg() == Reg;
428           })) {
429         VRegs.push_back(TypedVReg(Reg));
430       }
431     } else {
432       LLVM_DEBUG(dbgs() << "Popping physreg.\n";);
433       VRegs.push_back(TypedVReg(Reg));
434       continue;
435     }
436 
437     for (auto RI = MRI.def_begin(Reg), RE = MRI.def_end(); RI != RE; ++RI) {
438       MachineInstr *Def = RI->getParent();
439 
440       if (Def->getParent() != MBB)
441         continue;
442 
443       if (llvm::any_of(VisitedMIs,
444                        [&](const MachineInstr *VMI) { return Def == VMI; })) {
445         break;
446       }
447 
448       LLVM_DEBUG({
449         dbgs() << "\n========================\n";
450         dbgs() << "Visited MI: ";
451         Def->dump();
452         dbgs() << "BB Name: " << Def->getParent()->getName() << "\n";
453         dbgs() << "\n========================\n";
454       });
455       VisitedMIs.push_back(Def);
456       for (unsigned I = 1, E = Def->getNumOperands(); I != E; ++I) {
457 
458         MachineOperand &MO = Def->getOperand(I);
459         if (MO.isFI()) {
460           LLVM_DEBUG(dbgs() << "Pushing frame index.\n";);
461           RegQueue.push(TypedVReg(RSE_FrameIndex));
462         }
463 
464         if (!MO.isReg())
465           continue;
466         RegQueue.push(TypedVReg(MO.getReg()));
467       }
468     }
469   }
470 }
471 
472 namespace {
473 class NamedVRegCursor {
474   MachineRegisterInfo &MRI;
475   unsigned virtualVRegNumber;
476 
477 public:
478   NamedVRegCursor(MachineRegisterInfo &MRI) : MRI(MRI) {
479     unsigned VRegGapIndex = 0;
480     const unsigned VR_GAP = (++VRegGapIndex * 1000);
481 
482     unsigned I = MRI.createIncompleteVirtualRegister();
483     const unsigned E = (((I + VR_GAP) / VR_GAP) + 1) * VR_GAP;
484 
485     virtualVRegNumber = E;
486   }
487 
488   void SkipVRegs() {
489     unsigned VRegGapIndex = 1;
490     const unsigned VR_GAP = (++VRegGapIndex * 1000);
491 
492     unsigned I = virtualVRegNumber;
493     const unsigned E = (((I + VR_GAP) / VR_GAP) + 1) * VR_GAP;
494 
495     virtualVRegNumber = E;
496   }
497 
498   unsigned getVirtualVReg() const { return virtualVRegNumber; }
499 
500   unsigned incrementVirtualVReg(unsigned incr = 1) {
501     virtualVRegNumber += incr;
502     return virtualVRegNumber;
503   }
504 
505   unsigned createVirtualRegister(unsigned VReg) {
506     std::string S;
507     raw_string_ostream OS(S);
508     OS << "namedVReg" << (virtualVRegNumber & ~0x80000000);
509     OS.flush();
510     virtualVRegNumber++;
511     if (auto RC = MRI.getRegClassOrNull(VReg))
512       return MRI.createVirtualRegister(RC, OS.str());
513     return MRI.createGenericVirtualRegister(MRI.getType(VReg), OS.str());
514   }
515 };
516 } // namespace
517 
518 static std::map<unsigned, unsigned>
519 GetVRegRenameMap(const std::vector<TypedVReg> &VRegs,
520                  const std::vector<unsigned> &renamedInOtherBB,
521                  MachineRegisterInfo &MRI, NamedVRegCursor &NVC) {
522   std::map<unsigned, unsigned> VRegRenameMap;
523   bool FirstCandidate = true;
524 
525   for (auto &vreg : VRegs) {
526     if (vreg.isFrameIndex()) {
527       // We skip one vreg for any frame index because there is a good chance
528       // (especially when comparing SelectionDAG to GlobalISel generated MIR)
529       // that in the other file we are just getting an incoming vreg that comes
530       // from a copy from a frame index. So it's safe to skip by one.
531       unsigned LastRenameReg = NVC.incrementVirtualVReg();
532       (void)LastRenameReg;
533       LLVM_DEBUG(dbgs() << "Skipping rename for FI " << LastRenameReg << "\n";);
534       continue;
535     } else if (vreg.isCandidate()) {
536 
537       // After the first candidate, for every subsequent candidate, we skip mod
538       // 10 registers so that the candidates are more likely to start at the
539       // same vreg number making it more likely that the canonical walk from the
540       // candidate insruction. We don't need to skip from the first candidate of
541       // the BasicBlock because we already skip ahead several vregs for each BB.
542       unsigned LastRenameReg = NVC.getVirtualVReg();
543       if (FirstCandidate)
544         NVC.incrementVirtualVReg(LastRenameReg % 10);
545       FirstCandidate = false;
546       continue;
547     } else if (!TargetRegisterInfo::isVirtualRegister(vreg.getReg())) {
548       unsigned LastRenameReg = NVC.incrementVirtualVReg();
549       (void)LastRenameReg;
550       LLVM_DEBUG({
551         dbgs() << "Skipping rename for Phys Reg " << LastRenameReg << "\n";
552       });
553       continue;
554     }
555 
556     auto Reg = vreg.getReg();
557     if (llvm::find(renamedInOtherBB, Reg) != renamedInOtherBB.end()) {
558       LLVM_DEBUG(dbgs() << "Vreg " << Reg
559                         << " already renamed in other BB.\n";);
560       continue;
561     }
562 
563     auto Rename = NVC.createVirtualRegister(Reg);
564 
565     if (VRegRenameMap.find(Reg) == VRegRenameMap.end()) {
566       LLVM_DEBUG(dbgs() << "Mapping vreg ";);
567       if (MRI.reg_begin(Reg) != MRI.reg_end()) {
568         LLVM_DEBUG(auto foo = &*MRI.reg_begin(Reg); foo->dump(););
569       } else {
570         LLVM_DEBUG(dbgs() << Reg;);
571       }
572       LLVM_DEBUG(dbgs() << " to ";);
573       if (MRI.reg_begin(Rename) != MRI.reg_end()) {
574         LLVM_DEBUG(auto foo = &*MRI.reg_begin(Rename); foo->dump(););
575       } else {
576         LLVM_DEBUG(dbgs() << Rename;);
577       }
578       LLVM_DEBUG(dbgs() << "\n";);
579 
580       VRegRenameMap.insert(std::pair<unsigned, unsigned>(Reg, Rename));
581     }
582   }
583 
584   return VRegRenameMap;
585 }
586 
587 static bool doVRegRenaming(std::vector<unsigned> &RenamedInOtherBB,
588                            const std::map<unsigned, unsigned> &VRegRenameMap,
589                            MachineRegisterInfo &MRI) {
590   bool Changed = false;
591   for (auto I = VRegRenameMap.begin(), E = VRegRenameMap.end(); I != E; ++I) {
592 
593     auto VReg = I->first;
594     auto Rename = I->second;
595 
596     RenamedInOtherBB.push_back(Rename);
597 
598     std::vector<MachineOperand *> RenameMOs;
599     for (auto &MO : MRI.reg_operands(VReg)) {
600       RenameMOs.push_back(&MO);
601     }
602 
603     for (auto *MO : RenameMOs) {
604       Changed = true;
605       MO->setReg(Rename);
606 
607       if (!MO->isDef())
608         MO->setIsKill(false);
609     }
610   }
611 
612   return Changed;
613 }
614 
615 static bool doDefKillClear(MachineBasicBlock *MBB) {
616   bool Changed = false;
617 
618   for (auto &MI : *MBB) {
619     for (auto &MO : MI.operands()) {
620       if (!MO.isReg())
621         continue;
622       if (!MO.isDef() && MO.isKill()) {
623         Changed = true;
624         MO.setIsKill(false);
625       }
626 
627       if (MO.isDef() && MO.isDead()) {
628         Changed = true;
629         MO.setIsDead(false);
630       }
631     }
632   }
633 
634   return Changed;
635 }
636 
637 static bool runOnBasicBlock(MachineBasicBlock *MBB,
638                             std::vector<StringRef> &bbNames,
639                             std::vector<unsigned> &renamedInOtherBB,
640                             unsigned &basicBlockNum, unsigned &VRegGapIndex,
641                             NamedVRegCursor &NVC) {
642 
643   if (CanonicalizeBasicBlockNumber != ~0U) {
644     if (CanonicalizeBasicBlockNumber != basicBlockNum++)
645       return false;
646     LLVM_DEBUG(dbgs() << "\n Canonicalizing BasicBlock " << MBB->getName()
647                       << "\n";);
648   }
649 
650   if (llvm::find(bbNames, MBB->getName()) != bbNames.end()) {
651     LLVM_DEBUG({
652       dbgs() << "Found potentially duplicate BasicBlocks: " << MBB->getName()
653              << "\n";
654     });
655     return false;
656   }
657 
658   LLVM_DEBUG({
659     dbgs() << "\n\n  NEW BASIC BLOCK: " << MBB->getName() << "  \n\n";
660     dbgs() << "\n\n================================================\n\n";
661   });
662 
663   bool Changed = false;
664   MachineFunction &MF = *MBB->getParent();
665   MachineRegisterInfo &MRI = MF.getRegInfo();
666 
667   bbNames.push_back(MBB->getName());
668   LLVM_DEBUG(dbgs() << "\n\n NEW BASIC BLOCK: " << MBB->getName() << "\n\n";);
669 
670   LLVM_DEBUG(dbgs() << "MBB Before Canonical Copy Propagation:\n";
671              MBB->dump(););
672   Changed |= propagateLocalCopies(MBB);
673   LLVM_DEBUG(dbgs() << "MBB After Canonical Copy Propagation:\n"; MBB->dump(););
674 
675   LLVM_DEBUG(dbgs() << "MBB Before Scheduling:\n"; MBB->dump(););
676   unsigned IdempotentInstCount = 0;
677   Changed |= rescheduleCanonically(IdempotentInstCount, MBB);
678   LLVM_DEBUG(dbgs() << "MBB After Scheduling:\n"; MBB->dump(););
679 
680   std::vector<MachineInstr *> Candidates = populateCandidates(MBB);
681   std::vector<MachineInstr *> VisitedMIs;
682   llvm::copy(Candidates, std::back_inserter(VisitedMIs));
683 
684   std::vector<TypedVReg> VRegs;
685   for (auto candidate : Candidates) {
686     VRegs.push_back(TypedVReg(RSE_NewCandidate));
687 
688     std::queue<TypedVReg> RegQueue;
689 
690     // Here we walk the vreg operands of a non-root node along our walk.
691     // The root nodes are the original candidates (stores normally).
692     // These are normally not the root nodes (except for the case of copies to
693     // physical registers).
694     for (unsigned i = 1; i < candidate->getNumOperands(); i++) {
695       if (candidate->mayStore() || candidate->isBranch())
696         break;
697 
698       MachineOperand &MO = candidate->getOperand(i);
699       if (!(MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())))
700         continue;
701 
702       LLVM_DEBUG(dbgs() << "Enqueue register"; MO.dump(); dbgs() << "\n";);
703       RegQueue.push(TypedVReg(MO.getReg()));
704     }
705 
706     // Here we walk the root candidates. We start from the 0th operand because
707     // the root is normally a store to a vreg.
708     for (unsigned i = 0; i < candidate->getNumOperands(); i++) {
709 
710       if (!candidate->mayStore() && !candidate->isBranch())
711         break;
712 
713       MachineOperand &MO = candidate->getOperand(i);
714 
715       // TODO: Do we want to only add vregs here?
716       if (!MO.isReg() && !MO.isFI())
717         continue;
718 
719       LLVM_DEBUG(dbgs() << "Enqueue Reg/FI"; MO.dump(); dbgs() << "\n";);
720 
721       RegQueue.push(MO.isReg() ? TypedVReg(MO.getReg())
722                                : TypedVReg(RSE_FrameIndex));
723     }
724 
725     doCandidateWalk(VRegs, RegQueue, VisitedMIs, MBB);
726   }
727 
728   // If we have populated no vregs to rename then bail.
729   // The rest of this function does the vreg remaping.
730   if (VRegs.size() == 0)
731     return Changed;
732 
733   auto VRegRenameMap = GetVRegRenameMap(VRegs, renamedInOtherBB, MRI, NVC);
734   Changed |= doVRegRenaming(renamedInOtherBB, VRegRenameMap, MRI);
735 
736   // Here we renumber the def vregs for the idempotent instructions from the top
737   // of the MachineBasicBlock so that they are named in the order that we sorted
738   // them alphabetically. Eventually we wont need SkipVRegs because we will use
739   // named vregs instead.
740   NVC.SkipVRegs();
741 
742   auto MII = MBB->begin();
743   for (unsigned i = 0; i < IdempotentInstCount && MII != MBB->end(); ++i) {
744     MachineInstr &MI = *MII++;
745     Changed = true;
746     unsigned vRegToRename = MI.getOperand(0).getReg();
747     auto Rename = NVC.createVirtualRegister(vRegToRename);
748 
749     std::vector<MachineOperand *> RenameMOs;
750     for (auto &MO : MRI.reg_operands(vRegToRename)) {
751       RenameMOs.push_back(&MO);
752     }
753 
754     for (auto *MO : RenameMOs) {
755       MO->setReg(Rename);
756     }
757   }
758 
759   Changed |= doDefKillClear(MBB);
760 
761   LLVM_DEBUG(dbgs() << "Updated MachineBasicBlock:\n"; MBB->dump();
762              dbgs() << "\n";);
763   LLVM_DEBUG(
764       dbgs() << "\n\n================================================\n\n");
765   return Changed;
766 }
767 
768 bool MIRCanonicalizer::runOnMachineFunction(MachineFunction &MF) {
769 
770   static unsigned functionNum = 0;
771   if (CanonicalizeFunctionNumber != ~0U) {
772     if (CanonicalizeFunctionNumber != functionNum++)
773       return false;
774     LLVM_DEBUG(dbgs() << "\n Canonicalizing Function " << MF.getName()
775                       << "\n";);
776   }
777 
778   // we need a valid vreg to create a vreg type for skipping all those
779   // stray vreg numbers so reach alignment/canonical vreg values.
780   std::vector<MachineBasicBlock *> RPOList = GetRPOList(MF);
781 
782   LLVM_DEBUG(
783       dbgs() << "\n\n  NEW MACHINE FUNCTION: " << MF.getName() << "  \n\n";
784       dbgs() << "\n\n================================================\n\n";
785       dbgs() << "Total Basic Blocks: " << RPOList.size() << "\n";
786       for (auto MBB
787            : RPOList) { dbgs() << MBB->getName() << "\n"; } dbgs()
788       << "\n\n================================================\n\n";);
789 
790   std::vector<StringRef> BBNames;
791   std::vector<unsigned> RenamedInOtherBB;
792 
793   unsigned GapIdx = 0;
794   unsigned BBNum = 0;
795 
796   bool Changed = false;
797 
798   MachineRegisterInfo &MRI = MF.getRegInfo();
799   NamedVRegCursor NVC(MRI);
800   for (auto MBB : RPOList)
801     Changed |=
802         runOnBasicBlock(MBB, BBNames, RenamedInOtherBB, BBNum, GapIdx, NVC);
803 
804   return Changed;
805 }
806