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