blob: 9ef097c97e760589767000b8b730fde963c96274 [file] [log] [blame]
Puyan Lotfia521c4ac52017-11-02 23:37:32 +00001//===-------------- 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"
Puyan Lotfia521c4ac52017-11-02 23:37:32 +000029#include "llvm/CodeGen/MachineFunctionPass.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
31#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000032#include "llvm/CodeGen/Passes.h"
Puyan Lotfia521c4ac52017-11-02 23:37:32 +000033#include "llvm/Support/raw_ostream.h"
Puyan Lotfia521c4ac52017-11-02 23:37:32 +000034
35#include <queue>
36
37using namespace llvm;
38
39namespace llvm {
40extern char &MIRCanonicalizerID;
41} // namespace llvm
42
43#define DEBUG_TYPE "mir-canonicalizer"
44
45static cl::opt<unsigned>
46CanonicalizeFunctionNumber("canon-nth-function", cl::Hidden, cl::init(~0u),
47 cl::value_desc("N"),
48 cl::desc("Function number to canonicalize."));
49
50static cl::opt<unsigned>
51CanonicalizeBasicBlockNumber("canon-nth-basicblock", cl::Hidden, cl::init(~0u),
52 cl::value_desc("N"),
53 cl::desc("BasicBlock number to canonicalize."));
54
55namespace {
56
57class MIRCanonicalizer : public MachineFunctionPass {
58public:
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
76enum VRType { RSE_Reg = 0, RSE_FrameIndex, RSE_NewCandidate };
77class TypedVReg {
78 VRType type;
79 unsigned reg;
80
81public:
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
98char MIRCanonicalizer::ID;
99
100char &llvm::MIRCanonicalizerID = MIRCanonicalizer::ID;
101
102INITIALIZE_PASS_BEGIN(MIRCanonicalizer, "mir-canonicalizer",
Craig Topper666e23b2017-11-03 18:02:46 +0000103 "Rename Register Operands Canonically", false, false)
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000104
105INITIALIZE_PASS_END(MIRCanonicalizer, "mir-canonicalizer",
Craig Topper666e23b2017-11-03 18:02:46 +0000106 "Rename Register Operands Canonically", false, false)
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000107
108static 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.
120static 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
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000134static bool
135rescheduleLexographically(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
169static bool rescheduleCanonically(unsigned &PseudoIdempotentInstCount,
170 MachineBasicBlock *MBB) {
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000171
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
Puyan Lotfi26c504f2018-04-05 00:08:15 +0000192 std::map<MachineInstr *, std::vector<MachineInstr *>> MultiUsers;
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000193 std::vector<MachineInstr *> PseudoIdempotentInstructions;
194 std::vector<unsigned> PhysRegDefs;
195 for (auto *II : Instructions) {
196 for (unsigned i = 1; i < II->getNumOperands(); i++) {
197 MachineOperand &MO = II->getOperand(i);
198 if (!MO.isReg())
199 continue;
200
201 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
202 continue;
203
204 if (!MO.isDef())
205 continue;
206
207 PhysRegDefs.push_back(MO.getReg());
208 }
209 }
210
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000211 for (auto *II : Instructions) {
212 if (II->getNumOperands() == 0)
213 continue;
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000214 if (II->mayLoadOrStore())
215 continue;
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000216
217 MachineOperand &MO = II->getOperand(0);
218 if (!MO.isReg() || !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
219 continue;
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000220 if (!MO.isDef())
221 continue;
222
223 bool IsPseudoIdempotent = true;
224 for (unsigned i = 1; i < II->getNumOperands(); i++) {
225
226 if (II->getOperand(i).isImm()) {
227 continue;
228 }
229
230 if (II->getOperand(i).isReg()) {
231 if (!TargetRegisterInfo::isVirtualRegister(II->getOperand(i).getReg()))
232 if (llvm::find(PhysRegDefs, II->getOperand(i).getReg()) ==
233 PhysRegDefs.end()) {
234 continue;
235 }
236 }
237
238 IsPseudoIdempotent = false;
239 break;
240 }
241
242 if (IsPseudoIdempotent) {
243 PseudoIdempotentInstructions.push_back(II);
244 continue;
245 }
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000246
247 DEBUG(dbgs() << "Operand " << 0 << " of "; II->dump(); MO.dump(););
248
249 MachineInstr *Def = II;
250 unsigned Distance = ~0U;
251 MachineInstr *UseToBringDefCloserTo = nullptr;
252 MachineRegisterInfo *MRI = &MBB->getParent()->getRegInfo();
253 for (auto &UO : MRI->use_nodbg_operands(MO.getReg())) {
254 MachineInstr *UseInst = UO.getParent();
255
256 const unsigned DefLoc = getInstrIdx(*Def);
257 const unsigned UseLoc = getInstrIdx(*UseInst);
258 const unsigned Delta = (UseLoc - DefLoc);
259
260 if (UseInst->getParent() != Def->getParent())
261 continue;
262 if (DefLoc >= UseLoc)
263 continue;
264
265 if (Delta < Distance) {
266 Distance = Delta;
267 UseToBringDefCloserTo = UseInst;
268 }
269 }
270
271 const auto BBE = MBB->instr_end();
272 MachineBasicBlock::iterator DefI = BBE;
273 MachineBasicBlock::iterator UseI = BBE;
274
275 for (auto BBI = MBB->instr_begin(); BBI != BBE; ++BBI) {
276
277 if (DefI != BBE && UseI != BBE)
278 break;
279
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000280 if (&*BBI == Def) {
281 DefI = BBI;
282 continue;
283 }
284
285 if (&*BBI == UseToBringDefCloserTo) {
286 UseI = BBI;
287 continue;
288 }
289 }
290
291 if (DefI == BBE || UseI == BBE)
292 continue;
293
294 DEBUG({
295 dbgs() << "Splicing ";
296 DefI->dump();
297 dbgs() << " right before: ";
298 UseI->dump();
299 });
300
Puyan Lotfi26c504f2018-04-05 00:08:15 +0000301 MultiUsers[UseToBringDefCloserTo].push_back(Def);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000302 Changed = true;
303 MBB->splice(UseI, MBB, DefI);
304 }
305
Puyan Lotfi26c504f2018-04-05 00:08:15 +0000306 // Sort the defs for users of multiple defs lexographically.
307 for (const auto &E : MultiUsers) {
308
309 auto UseI =
310 std::find_if(MBB->instr_begin(), MBB->instr_end(),
311 [&](MachineInstr &MI) -> bool { return &MI == E.first; });
312
313 if (UseI == MBB->instr_end())
314 continue;
315
316 DEBUG(dbgs() << "Rescheduling Multi-Use Instructions Lexographically.";);
317 Changed |= rescheduleLexographically(
318 E.second, MBB, [&]() -> MachineBasicBlock::iterator { return UseI; });
319 }
320
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000321 PseudoIdempotentInstCount = PseudoIdempotentInstructions.size();
322 DEBUG(dbgs() << "Rescheduling Idempotent Instructions Lexographically.";);
323 Changed |= rescheduleLexographically(
324 PseudoIdempotentInstructions, MBB,
325 [&]() -> MachineBasicBlock::iterator { return MBB->begin(); });
326
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000327 return Changed;
328}
329
330/// Here we find our candidates. What makes an interesting candidate?
331/// An candidate for a canonicalization tree root is normally any kind of
332/// instruction that causes side effects such as a store to memory or a copy to
333/// a physical register or a return instruction. We use these as an expression
334/// tree root that we walk inorder to build a canonical walk which should result
335/// in canoncal vreg renaming.
336static std::vector<MachineInstr *> populateCandidates(MachineBasicBlock *MBB) {
337 std::vector<MachineInstr *> Candidates;
338 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
339
340 for (auto II = MBB->begin(), IE = MBB->end(); II != IE; ++II) {
341 MachineInstr *MI = &*II;
342
343 bool DoesMISideEffect = false;
344
345 if (MI->getNumOperands() > 0 && MI->getOperand(0).isReg()) {
346 const unsigned Dst = MI->getOperand(0).getReg();
347 DoesMISideEffect |= !TargetRegisterInfo::isVirtualRegister(Dst);
348
349 for (auto UI = MRI.use_begin(Dst); UI != MRI.use_end(); ++UI) {
350 if (DoesMISideEffect) break;
351 DoesMISideEffect |= (UI->getParent()->getParent() != MI->getParent());
352 }
353 }
354
355 if (!MI->mayStore() && !MI->isBranch() && !DoesMISideEffect)
356 continue;
357
358 DEBUG(dbgs() << "Found Candidate: "; MI->dump(););
359 Candidates.push_back(MI);
360 }
361
362 return Candidates;
363}
364
Benjamin Kramer51ebcaa2017-11-24 14:55:41 +0000365static void doCandidateWalk(std::vector<TypedVReg> &VRegs,
366 std::queue<TypedVReg> &RegQueue,
367 std::vector<MachineInstr *> &VisitedMIs,
368 const MachineBasicBlock *MBB) {
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000369
370 const MachineFunction &MF = *MBB->getParent();
371 const MachineRegisterInfo &MRI = MF.getRegInfo();
372
373 while (!RegQueue.empty()) {
374
375 auto TReg = RegQueue.front();
376 RegQueue.pop();
377
378 if (TReg.isFrameIndex()) {
379 DEBUG(dbgs() << "Popping frame index.\n";);
380 VRegs.push_back(TypedVReg(RSE_FrameIndex));
381 continue;
382 }
383
384 assert(TReg.isReg() && "Expected vreg or physreg.");
385 unsigned Reg = TReg.getReg();
386
387 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
388 DEBUG({
389 dbgs() << "Popping vreg ";
390 MRI.def_begin(Reg)->dump();
391 dbgs() << "\n";
392 });
393
394 if (!llvm::any_of(VRegs, [&](const TypedVReg &TR) {
395 return TR.isReg() && TR.getReg() == Reg;
396 })) {
397 VRegs.push_back(TypedVReg(Reg));
398 }
399 } else {
400 DEBUG(dbgs() << "Popping physreg.\n";);
401 VRegs.push_back(TypedVReg(Reg));
402 continue;
403 }
404
405 for (auto RI = MRI.def_begin(Reg), RE = MRI.def_end(); RI != RE; ++RI) {
406 MachineInstr *Def = RI->getParent();
407
408 if (Def->getParent() != MBB)
409 continue;
410
411 if (llvm::any_of(VisitedMIs,
412 [&](const MachineInstr *VMI) { return Def == VMI; })) {
413 break;
414 }
415
416 DEBUG({
417 dbgs() << "\n========================\n";
418 dbgs() << "Visited MI: ";
419 Def->dump();
420 dbgs() << "BB Name: " << Def->getParent()->getName() << "\n";
421 dbgs() << "\n========================\n";
422 });
423 VisitedMIs.push_back(Def);
424 for (unsigned I = 1, E = Def->getNumOperands(); I != E; ++I) {
425
426 MachineOperand &MO = Def->getOperand(I);
427 if (MO.isFI()) {
428 DEBUG(dbgs() << "Pushing frame index.\n";);
429 RegQueue.push(TypedVReg(RSE_FrameIndex));
430 }
431
432 if (!MO.isReg())
433 continue;
434 RegQueue.push(TypedVReg(MO.getReg()));
435 }
436 }
437 }
438}
439
440// TODO: Work to remove this in the future. One day when we have named vregs
441// we should be able to form the canonical name based on some characteristic
442// we see in that point of the expression tree (like if we were to name based
443// on some sort of value numbering scheme).
444static void SkipVRegs(unsigned &VRegGapIndex, MachineRegisterInfo &MRI,
445 const TargetRegisterClass *RC) {
446 const unsigned VR_GAP = (++VRegGapIndex * 1000);
447
448 DEBUG({
449 dbgs() << "Adjusting per-BB VR_GAP for BB" << VRegGapIndex << " to "
450 << VR_GAP << "\n";
451 });
452
453 unsigned I = MRI.createVirtualRegister(RC);
454 const unsigned E = (((I + VR_GAP) / VR_GAP) + 1) * VR_GAP;
455 while (I != E) {
456 I = MRI.createVirtualRegister(RC);
457 }
458}
459
460static std::map<unsigned, unsigned>
461GetVRegRenameMap(const std::vector<TypedVReg> &VRegs,
462 const std::vector<unsigned> &renamedInOtherBB,
463 MachineRegisterInfo &MRI,
464 const TargetRegisterClass *RC) {
465 std::map<unsigned, unsigned> VRegRenameMap;
466 unsigned LastRenameReg = MRI.createVirtualRegister(RC);
467 bool FirstCandidate = true;
468
469 for (auto &vreg : VRegs) {
470 if (vreg.isFrameIndex()) {
471 // We skip one vreg for any frame index because there is a good chance
472 // (especially when comparing SelectionDAG to GlobalISel generated MIR)
473 // that in the other file we are just getting an incoming vreg that comes
474 // from a copy from a frame index. So it's safe to skip by one.
475 LastRenameReg = MRI.createVirtualRegister(RC);
476 DEBUG(dbgs() << "Skipping rename for FI " << LastRenameReg << "\n";);
477 continue;
478 } else if (vreg.isCandidate()) {
479
480 // After the first candidate, for every subsequent candidate, we skip mod
481 // 10 registers so that the candidates are more likely to start at the
482 // same vreg number making it more likely that the canonical walk from the
483 // candidate insruction. We don't need to skip from the first candidate of
484 // the BasicBlock because we already skip ahead several vregs for each BB.
485 while (LastRenameReg % 10) {
486 if (!FirstCandidate) break;
487 LastRenameReg = MRI.createVirtualRegister(RC);
488
489 DEBUG({
490 dbgs() << "Skipping rename for new candidate " << LastRenameReg
491 << "\n";
492 });
493 }
494 FirstCandidate = false;
495 continue;
496 } else if (!TargetRegisterInfo::isVirtualRegister(vreg.getReg())) {
497 LastRenameReg = MRI.createVirtualRegister(RC);
498 DEBUG({
499 dbgs() << "Skipping rename for Phys Reg " << LastRenameReg << "\n";
500 });
501 continue;
502 }
503
504 auto Reg = vreg.getReg();
505 if (llvm::find(renamedInOtherBB, Reg) != renamedInOtherBB.end()) {
506 DEBUG(dbgs() << "Vreg " << Reg << " already renamed in other BB.\n";);
507 continue;
508 }
509
510 auto Rename = MRI.createVirtualRegister(MRI.getRegClass(Reg));
511 LastRenameReg = Rename;
512
513 if (VRegRenameMap.find(Reg) == VRegRenameMap.end()) {
514 DEBUG(dbgs() << "Mapping vreg ";);
515 if (MRI.reg_begin(Reg) != MRI.reg_end()) {
516 DEBUG(auto foo = &*MRI.reg_begin(Reg); foo->dump(););
517 } else {
518 DEBUG(dbgs() << Reg;);
519 }
520 DEBUG(dbgs() << " to ";);
521 if (MRI.reg_begin(Rename) != MRI.reg_end()) {
522 DEBUG(auto foo = &*MRI.reg_begin(Rename); foo->dump(););
523 } else {
524 DEBUG(dbgs() << Rename;);
525 }
526 DEBUG(dbgs() << "\n";);
527
528 VRegRenameMap.insert(std::pair<unsigned, unsigned>(Reg, Rename));
529 }
530 }
531
532 return VRegRenameMap;
533}
534
535static bool doVRegRenaming(std::vector<unsigned> &RenamedInOtherBB,
536 const std::map<unsigned, unsigned> &VRegRenameMap,
537 MachineRegisterInfo &MRI) {
538 bool Changed = false;
539 for (auto I = VRegRenameMap.begin(), E = VRegRenameMap.end(); I != E; ++I) {
540
541 auto VReg = I->first;
542 auto Rename = I->second;
543
544 RenamedInOtherBB.push_back(Rename);
545
546 std::vector<MachineOperand *> RenameMOs;
547 for (auto &MO : MRI.reg_operands(VReg)) {
548 RenameMOs.push_back(&MO);
549 }
550
551 for (auto *MO : RenameMOs) {
552 Changed = true;
553 MO->setReg(Rename);
554
555 if (!MO->isDef())
556 MO->setIsKill(false);
557 }
558 }
559
560 return Changed;
561}
562
563static bool doDefKillClear(MachineBasicBlock *MBB) {
564 bool Changed = false;
565
566 for (auto &MI : *MBB) {
567 for (auto &MO : MI.operands()) {
568 if (!MO.isReg())
569 continue;
570 if (!MO.isDef() && MO.isKill()) {
571 Changed = true;
572 MO.setIsKill(false);
573 }
574
575 if (MO.isDef() && MO.isDead()) {
576 Changed = true;
577 MO.setIsDead(false);
578 }
579 }
580 }
581
582 return Changed;
583}
584
585static bool runOnBasicBlock(MachineBasicBlock *MBB,
586 std::vector<StringRef> &bbNames,
587 std::vector<unsigned> &renamedInOtherBB,
588 unsigned &basicBlockNum, unsigned &VRegGapIndex) {
589
590 if (CanonicalizeBasicBlockNumber != ~0U) {
591 if (CanonicalizeBasicBlockNumber != basicBlockNum++)
592 return false;
593 DEBUG(dbgs() << "\n Canonicalizing BasicBlock " << MBB->getName() << "\n";);
594 }
595
596 if (llvm::find(bbNames, MBB->getName()) != bbNames.end()) {
597 DEBUG({
598 dbgs() << "Found potentially duplicate BasicBlocks: " << MBB->getName()
599 << "\n";
600 });
601 return false;
602 }
603
604 DEBUG({
605 dbgs() << "\n\n NEW BASIC BLOCK: " << MBB->getName() << " \n\n";
606 dbgs() << "\n\n================================================\n\n";
607 });
608
609 bool Changed = false;
610 MachineFunction &MF = *MBB->getParent();
611 MachineRegisterInfo &MRI = MF.getRegInfo();
612
613 const unsigned DummyVReg = GetDummyVReg(MF);
614 const TargetRegisterClass *DummyRC =
615 (DummyVReg == ~0U) ? nullptr : MRI.getRegClass(DummyVReg);
616 if (!DummyRC) return false;
617
618 bbNames.push_back(MBB->getName());
619 DEBUG(dbgs() << "\n\n NEW BASIC BLOCK: " << MBB->getName() << "\n\n";);
620
621 DEBUG(dbgs() << "MBB Before Scheduling:\n"; MBB->dump(););
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000622 unsigned IdempotentInstCount = 0;
623 Changed |= rescheduleCanonically(IdempotentInstCount, MBB);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000624 DEBUG(dbgs() << "MBB After Scheduling:\n"; MBB->dump(););
625
626 std::vector<MachineInstr *> Candidates = populateCandidates(MBB);
627 std::vector<MachineInstr *> VisitedMIs;
628 std::copy(Candidates.begin(), Candidates.end(),
629 std::back_inserter(VisitedMIs));
630
631 std::vector<TypedVReg> VRegs;
632 for (auto candidate : Candidates) {
633 VRegs.push_back(TypedVReg(RSE_NewCandidate));
634
635 std::queue<TypedVReg> RegQueue;
636
637 // Here we walk the vreg operands of a non-root node along our walk.
638 // The root nodes are the original candidates (stores normally).
639 // These are normally not the root nodes (except for the case of copies to
640 // physical registers).
641 for (unsigned i = 1; i < candidate->getNumOperands(); i++) {
642 if (candidate->mayStore() || candidate->isBranch())
643 break;
644
645 MachineOperand &MO = candidate->getOperand(i);
646 if (!(MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())))
647 continue;
648
649 DEBUG(dbgs() << "Enqueue register"; MO.dump(); dbgs() << "\n";);
650 RegQueue.push(TypedVReg(MO.getReg()));
651 }
652
653 // Here we walk the root candidates. We start from the 0th operand because
654 // the root is normally a store to a vreg.
655 for (unsigned i = 0; i < candidate->getNumOperands(); i++) {
656
657 if (!candidate->mayStore() && !candidate->isBranch())
658 break;
659
660 MachineOperand &MO = candidate->getOperand(i);
661
662 // TODO: Do we want to only add vregs here?
663 if (!MO.isReg() && !MO.isFI())
664 continue;
665
666 DEBUG(dbgs() << "Enqueue Reg/FI"; MO.dump(); dbgs() << "\n";);
667
668 RegQueue.push(MO.isReg() ? TypedVReg(MO.getReg()) :
669 TypedVReg(RSE_FrameIndex));
670 }
671
672 doCandidateWalk(VRegs, RegQueue, VisitedMIs, MBB);
673 }
674
675 // If we have populated no vregs to rename then bail.
676 // The rest of this function does the vreg remaping.
677 if (VRegs.size() == 0)
678 return Changed;
679
680 // Skip some vregs, so we can recon where we'll land next.
681 SkipVRegs(VRegGapIndex, MRI, DummyRC);
682
683 auto VRegRenameMap = GetVRegRenameMap(VRegs, renamedInOtherBB, MRI, DummyRC);
684 Changed |= doVRegRenaming(renamedInOtherBB, VRegRenameMap, MRI);
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000685
686 // Here we renumber the def vregs for the idempotent instructions from the top
687 // of the MachineBasicBlock so that they are named in the order that we sorted
688 // them alphabetically. Eventually we wont need SkipVRegs because we will use
689 // named vregs instead.
690 unsigned gap = 1;
691 SkipVRegs(gap, MRI, DummyRC);
692
693 auto MII = MBB->begin();
694 for (unsigned i = 0; i < IdempotentInstCount && MII != MBB->end(); ++i) {
695 MachineInstr &MI = *MII++;
696 Changed = true;
697 unsigned vRegToRename = MI.getOperand(0).getReg();
698 auto Rename = MRI.createVirtualRegister(MRI.getRegClass(vRegToRename));
699
700 std::vector<MachineOperand *> RenameMOs;
701 for (auto &MO : MRI.reg_operands(vRegToRename)) {
702 RenameMOs.push_back(&MO);
703 }
704
705 for (auto *MO : RenameMOs) {
706 MO->setReg(Rename);
707 }
708 }
709
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000710 Changed |= doDefKillClear(MBB);
711
712 DEBUG(dbgs() << "Updated MachineBasicBlock:\n"; MBB->dump(); dbgs() << "\n";);
713 DEBUG(dbgs() << "\n\n================================================\n\n");
714 return Changed;
715}
716
717bool MIRCanonicalizer::runOnMachineFunction(MachineFunction &MF) {
718
719 static unsigned functionNum = 0;
720 if (CanonicalizeFunctionNumber != ~0U) {
721 if (CanonicalizeFunctionNumber != functionNum++)
722 return false;
723 DEBUG(dbgs() << "\n Canonicalizing Function " << MF.getName() << "\n";);
724 }
725
726 // we need a valid vreg to create a vreg type for skipping all those
727 // stray vreg numbers so reach alignment/canonical vreg values.
728 std::vector<MachineBasicBlock*> RPOList = GetRPOList(MF);
729
730 DEBUG(
731 dbgs() << "\n\n NEW MACHINE FUNCTION: " << MF.getName() << " \n\n";
732 dbgs() << "\n\n================================================\n\n";
733 dbgs() << "Total Basic Blocks: " << RPOList.size() << "\n";
734 for (auto MBB : RPOList) {
735 dbgs() << MBB->getName() << "\n";
736 }
737 dbgs() << "\n\n================================================\n\n";
738 );
739
740 std::vector<StringRef> BBNames;
741 std::vector<unsigned> RenamedInOtherBB;
742
743 unsigned GapIdx = 0;
744 unsigned BBNum = 0;
745
746 bool Changed = false;
747
748 for (auto MBB : RPOList)
749 Changed |= runOnBasicBlock(MBB, BBNames, RenamedInOtherBB, BBNum, GapIdx);
750
751 return Changed;
752}
753