blob: 3f7e5cf052f279fad0d9697ac6959c06c5714926 [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>
Puyan Lotfi6ea89b42018-04-16 08:12:15 +000046 CanonicalizeFunctionNumber("canon-nth-function", cl::Hidden, cl::init(~0u),
47 cl::value_desc("N"),
48 cl::desc("Function number to canonicalize."));
Puyan Lotfia521c4ac52017-11-02 23:37:32 +000049
Puyan Lotfi6ea89b42018-04-16 08:12:15 +000050static cl::opt<unsigned> CanonicalizeBasicBlockNumber(
51 "canon-nth-basicblock", cl::Hidden, cl::init(~0u), cl::value_desc("N"),
52 cl::desc("BasicBlock number to canonicalize."));
Puyan Lotfia521c4ac52017-11-02 23:37:32 +000053
54namespace {
55
56class MIRCanonicalizer : public MachineFunctionPass {
57public:
58 static char ID;
59 MIRCanonicalizer() : MachineFunctionPass(ID) {}
60
61 StringRef getPassName() const override {
62 return "Rename register operands in a canonical ordering.";
63 }
64
65 void getAnalysisUsage(AnalysisUsage &AU) const override {
66 AU.setPreservesCFG();
67 MachineFunctionPass::getAnalysisUsage(AU);
68 }
69
70 bool runOnMachineFunction(MachineFunction &MF) override;
71};
72
73} // end anonymous namespace
74
75enum VRType { RSE_Reg = 0, RSE_FrameIndex, RSE_NewCandidate };
76class TypedVReg {
77 VRType type;
78 unsigned reg;
79
80public:
81 TypedVReg(unsigned reg) : type(RSE_Reg), reg(reg) {}
82 TypedVReg(VRType type) : type(type), reg(~0U) {
83 assert(type != RSE_Reg && "Expected a non-register type.");
84 }
85
Puyan Lotfi6ea89b42018-04-16 08:12:15 +000086 bool isReg() const { return type == RSE_Reg; }
87 bool isFrameIndex() const { return type == RSE_FrameIndex; }
88 bool isCandidate() const { return type == RSE_NewCandidate; }
Puyan Lotfia521c4ac52017-11-02 23:37:32 +000089
90 VRType getType() const { return type; }
91 unsigned getReg() const {
92 assert(this->isReg() && "Expected a virtual or physical register.");
93 return reg;
94 }
95};
96
97char MIRCanonicalizer::ID;
98
99char &llvm::MIRCanonicalizerID = MIRCanonicalizer::ID;
100
101INITIALIZE_PASS_BEGIN(MIRCanonicalizer, "mir-canonicalizer",
Craig Topper666e23b2017-11-03 18:02:46 +0000102 "Rename Register Operands Canonically", false, false)
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000103
104INITIALIZE_PASS_END(MIRCanonicalizer, "mir-canonicalizer",
Craig Topper666e23b2017-11-03 18:02:46 +0000105 "Rename Register Operands Canonically", false, false)
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000106
107static std::vector<MachineBasicBlock *> GetRPOList(MachineFunction &MF) {
108 ReversePostOrderTraversal<MachineBasicBlock *> RPOT(&*MF.begin());
109 std::vector<MachineBasicBlock *> RPOList;
110 for (auto MBB : RPOT) {
111 RPOList.push_back(MBB);
112 }
113
114 return RPOList;
115}
116
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000117static bool
118rescheduleLexographically(std::vector<MachineInstr *> instructions,
119 MachineBasicBlock *MBB,
120 std::function<MachineBasicBlock::iterator()> getPos) {
121
122 bool Changed = false;
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000123 std::map<std::string, MachineInstr *> StringInstrMap;
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000124
125 for (auto *II : instructions) {
126 std::string S;
127 raw_string_ostream OS(S);
128 II->print(OS);
129 OS.flush();
130
131 // Trim the assignment, or start from the begining in the case of a store.
132 const size_t i = S.find("=");
133 StringInstrMap.insert({(i == std::string::npos) ? S : S.substr(i), II});
134 }
135
136 for (auto &II : StringInstrMap) {
137
138 DEBUG({
139 dbgs() << "Splicing ";
140 II.second->dump();
141 dbgs() << " right before: ";
142 getPos()->dump();
143 });
144
145 Changed = true;
146 MBB->splice(getPos(), MBB, II.second);
147 }
148
149 return Changed;
150}
151
152static bool rescheduleCanonically(unsigned &PseudoIdempotentInstCount,
153 MachineBasicBlock *MBB) {
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000154
155 bool Changed = false;
156
157 // Calculates the distance of MI from the begining of its parent BB.
158 auto getInstrIdx = [](const MachineInstr &MI) {
159 unsigned i = 0;
160 for (auto &CurMI : *MI.getParent()) {
161 if (&CurMI == &MI)
162 return i;
163 i++;
164 }
165 return ~0U;
166 };
167
168 // Pre-Populate vector of instructions to reschedule so that we don't
169 // clobber the iterator.
170 std::vector<MachineInstr *> Instructions;
171 for (auto &MI : *MBB) {
172 Instructions.push_back(&MI);
173 }
174
Puyan Lotfi26c504f2018-04-05 00:08:15 +0000175 std::map<MachineInstr *, std::vector<MachineInstr *>> MultiUsers;
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000176 std::vector<MachineInstr *> PseudoIdempotentInstructions;
177 std::vector<unsigned> PhysRegDefs;
178 for (auto *II : Instructions) {
179 for (unsigned i = 1; i < II->getNumOperands(); i++) {
180 MachineOperand &MO = II->getOperand(i);
181 if (!MO.isReg())
182 continue;
183
184 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
185 continue;
186
187 if (!MO.isDef())
188 continue;
189
190 PhysRegDefs.push_back(MO.getReg());
191 }
192 }
193
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000194 for (auto *II : Instructions) {
195 if (II->getNumOperands() == 0)
196 continue;
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000197 if (II->mayLoadOrStore())
198 continue;
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000199
200 MachineOperand &MO = II->getOperand(0);
201 if (!MO.isReg() || !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
202 continue;
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000203 if (!MO.isDef())
204 continue;
205
206 bool IsPseudoIdempotent = true;
207 for (unsigned i = 1; i < II->getNumOperands(); i++) {
208
209 if (II->getOperand(i).isImm()) {
210 continue;
211 }
212
213 if (II->getOperand(i).isReg()) {
214 if (!TargetRegisterInfo::isVirtualRegister(II->getOperand(i).getReg()))
215 if (llvm::find(PhysRegDefs, II->getOperand(i).getReg()) ==
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000216 PhysRegDefs.end()) {
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000217 continue;
218 }
219 }
220
221 IsPseudoIdempotent = false;
222 break;
223 }
224
225 if (IsPseudoIdempotent) {
226 PseudoIdempotentInstructions.push_back(II);
227 continue;
228 }
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000229
230 DEBUG(dbgs() << "Operand " << 0 << " of "; II->dump(); MO.dump(););
231
232 MachineInstr *Def = II;
233 unsigned Distance = ~0U;
234 MachineInstr *UseToBringDefCloserTo = nullptr;
235 MachineRegisterInfo *MRI = &MBB->getParent()->getRegInfo();
236 for (auto &UO : MRI->use_nodbg_operands(MO.getReg())) {
237 MachineInstr *UseInst = UO.getParent();
238
239 const unsigned DefLoc = getInstrIdx(*Def);
240 const unsigned UseLoc = getInstrIdx(*UseInst);
241 const unsigned Delta = (UseLoc - DefLoc);
242
243 if (UseInst->getParent() != Def->getParent())
244 continue;
245 if (DefLoc >= UseLoc)
246 continue;
247
248 if (Delta < Distance) {
249 Distance = Delta;
250 UseToBringDefCloserTo = UseInst;
251 }
252 }
253
254 const auto BBE = MBB->instr_end();
255 MachineBasicBlock::iterator DefI = BBE;
256 MachineBasicBlock::iterator UseI = BBE;
257
258 for (auto BBI = MBB->instr_begin(); BBI != BBE; ++BBI) {
259
260 if (DefI != BBE && UseI != BBE)
261 break;
262
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000263 if (&*BBI == Def) {
264 DefI = BBI;
265 continue;
266 }
267
268 if (&*BBI == UseToBringDefCloserTo) {
269 UseI = BBI;
270 continue;
271 }
272 }
273
274 if (DefI == BBE || UseI == BBE)
275 continue;
276
277 DEBUG({
278 dbgs() << "Splicing ";
279 DefI->dump();
280 dbgs() << " right before: ";
281 UseI->dump();
282 });
283
Puyan Lotfi26c504f2018-04-05 00:08:15 +0000284 MultiUsers[UseToBringDefCloserTo].push_back(Def);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000285 Changed = true;
286 MBB->splice(UseI, MBB, DefI);
287 }
288
Puyan Lotfi26c504f2018-04-05 00:08:15 +0000289 // Sort the defs for users of multiple defs lexographically.
290 for (const auto &E : MultiUsers) {
291
292 auto UseI =
293 std::find_if(MBB->instr_begin(), MBB->instr_end(),
294 [&](MachineInstr &MI) -> bool { return &MI == E.first; });
295
296 if (UseI == MBB->instr_end())
297 continue;
298
299 DEBUG(dbgs() << "Rescheduling Multi-Use Instructions Lexographically.";);
300 Changed |= rescheduleLexographically(
301 E.second, MBB, [&]() -> MachineBasicBlock::iterator { return UseI; });
302 }
303
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000304 PseudoIdempotentInstCount = PseudoIdempotentInstructions.size();
305 DEBUG(dbgs() << "Rescheduling Idempotent Instructions Lexographically.";);
306 Changed |= rescheduleLexographically(
307 PseudoIdempotentInstructions, MBB,
308 [&]() -> MachineBasicBlock::iterator { return MBB->begin(); });
309
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000310 return Changed;
311}
312
Puyan Lotfi14b6637e2018-04-16 09:03:03 +0000313bool propagateLocalCopies(MachineBasicBlock *MBB) {
314 bool Changed = false;
315 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
316
317 std::vector<MachineInstr *> Copies;
318 for (MachineInstr &MI : MBB->instrs()) {
319 if (MI.isCopy())
320 Copies.push_back(&MI);
321 }
322
323 for (MachineInstr *MI : Copies) {
324
325 if (!MI->getOperand(0).isReg())
326 continue;
327 if (!MI->getOperand(1).isReg())
328 continue;
329
330 const unsigned Dst = MI->getOperand(0).getReg();
331 const unsigned Src = MI->getOperand(1).getReg();
332
333 if (!TargetRegisterInfo::isVirtualRegister(Dst))
334 continue;
335 if (!TargetRegisterInfo::isVirtualRegister(Src))
336 continue;
337 if (MRI.getRegClass(Dst) != MRI.getRegClass(Src))
338 continue;
339
340 for (auto UI = MRI.use_begin(Dst); UI != MRI.use_end(); ++UI) {
341 MachineOperand *MO = &*UI;
342 MO->setReg(Src);
343 Changed = true;
344 }
345
346 MI->eraseFromParent();
347 }
348
349 return Changed;
350}
351
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000352/// Here we find our candidates. What makes an interesting candidate?
353/// An candidate for a canonicalization tree root is normally any kind of
354/// instruction that causes side effects such as a store to memory or a copy to
355/// a physical register or a return instruction. We use these as an expression
356/// tree root that we walk inorder to build a canonical walk which should result
357/// in canoncal vreg renaming.
358static std::vector<MachineInstr *> populateCandidates(MachineBasicBlock *MBB) {
359 std::vector<MachineInstr *> Candidates;
360 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
361
362 for (auto II = MBB->begin(), IE = MBB->end(); II != IE; ++II) {
363 MachineInstr *MI = &*II;
364
365 bool DoesMISideEffect = false;
366
367 if (MI->getNumOperands() > 0 && MI->getOperand(0).isReg()) {
368 const unsigned Dst = MI->getOperand(0).getReg();
369 DoesMISideEffect |= !TargetRegisterInfo::isVirtualRegister(Dst);
370
371 for (auto UI = MRI.use_begin(Dst); UI != MRI.use_end(); ++UI) {
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000372 if (DoesMISideEffect)
373 break;
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000374 DoesMISideEffect |= (UI->getParent()->getParent() != MI->getParent());
375 }
376 }
377
378 if (!MI->mayStore() && !MI->isBranch() && !DoesMISideEffect)
379 continue;
380
381 DEBUG(dbgs() << "Found Candidate: "; MI->dump(););
382 Candidates.push_back(MI);
383 }
384
385 return Candidates;
386}
387
Benjamin Kramer51ebcaa2017-11-24 14:55:41 +0000388static void doCandidateWalk(std::vector<TypedVReg> &VRegs,
389 std::queue<TypedVReg> &RegQueue,
390 std::vector<MachineInstr *> &VisitedMIs,
391 const MachineBasicBlock *MBB) {
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000392
393 const MachineFunction &MF = *MBB->getParent();
394 const MachineRegisterInfo &MRI = MF.getRegInfo();
395
396 while (!RegQueue.empty()) {
397
398 auto TReg = RegQueue.front();
399 RegQueue.pop();
400
401 if (TReg.isFrameIndex()) {
402 DEBUG(dbgs() << "Popping frame index.\n";);
403 VRegs.push_back(TypedVReg(RSE_FrameIndex));
404 continue;
405 }
406
407 assert(TReg.isReg() && "Expected vreg or physreg.");
408 unsigned Reg = TReg.getReg();
409
410 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
411 DEBUG({
412 dbgs() << "Popping vreg ";
413 MRI.def_begin(Reg)->dump();
414 dbgs() << "\n";
415 });
416
417 if (!llvm::any_of(VRegs, [&](const TypedVReg &TR) {
418 return TR.isReg() && TR.getReg() == Reg;
419 })) {
420 VRegs.push_back(TypedVReg(Reg));
421 }
422 } else {
423 DEBUG(dbgs() << "Popping physreg.\n";);
424 VRegs.push_back(TypedVReg(Reg));
425 continue;
426 }
427
428 for (auto RI = MRI.def_begin(Reg), RE = MRI.def_end(); RI != RE; ++RI) {
429 MachineInstr *Def = RI->getParent();
430
431 if (Def->getParent() != MBB)
432 continue;
433
434 if (llvm::any_of(VisitedMIs,
435 [&](const MachineInstr *VMI) { return Def == VMI; })) {
436 break;
437 }
438
439 DEBUG({
440 dbgs() << "\n========================\n";
441 dbgs() << "Visited MI: ";
442 Def->dump();
443 dbgs() << "BB Name: " << Def->getParent()->getName() << "\n";
444 dbgs() << "\n========================\n";
445 });
446 VisitedMIs.push_back(Def);
447 for (unsigned I = 1, E = Def->getNumOperands(); I != E; ++I) {
448
449 MachineOperand &MO = Def->getOperand(I);
450 if (MO.isFI()) {
451 DEBUG(dbgs() << "Pushing frame index.\n";);
452 RegQueue.push(TypedVReg(RSE_FrameIndex));
453 }
454
455 if (!MO.isReg())
456 continue;
457 RegQueue.push(TypedVReg(MO.getReg()));
458 }
459 }
460 }
461}
462
Puyan Lotfid6f73132018-04-05 00:27:15 +0000463class NamedVRegCursor {
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000464
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000465private:
466 MachineRegisterInfo &MRI;
467 unsigned virtualVRegNumber;
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000468
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000469public:
470 NamedVRegCursor(MachineRegisterInfo &MRI) : MRI(MRI) {
471 unsigned VRegGapIndex = 0;
472 const unsigned VR_GAP = (++VRegGapIndex * 1000);
Puyan Lotfid6f73132018-04-05 00:27:15 +0000473
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000474 unsigned I = MRI.createIncompleteVirtualRegister();
475 const unsigned E = (((I + VR_GAP) / VR_GAP) + 1) * VR_GAP;
Puyan Lotfid6f73132018-04-05 00:27:15 +0000476
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000477 virtualVRegNumber = E;
478 }
Puyan Lotfid6f73132018-04-05 00:27:15 +0000479
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000480 void SkipVRegs() {
481 unsigned VRegGapIndex = 1;
482 const unsigned VR_GAP = (++VRegGapIndex * 1000);
Puyan Lotfid6f73132018-04-05 00:27:15 +0000483
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000484 unsigned I = virtualVRegNumber;
485 const unsigned E = (((I + VR_GAP) / VR_GAP) + 1) * VR_GAP;
Puyan Lotfid6f73132018-04-05 00:27:15 +0000486
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000487 virtualVRegNumber = E;
488 }
Puyan Lotfid6f73132018-04-05 00:27:15 +0000489
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000490 unsigned getVirtualVReg() const { return virtualVRegNumber; }
Puyan Lotfid6f73132018-04-05 00:27:15 +0000491
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000492 unsigned incrementVirtualVReg(unsigned incr = 1) {
493 virtualVRegNumber += incr;
494 return virtualVRegNumber;
495 }
Puyan Lotfid6f73132018-04-05 00:27:15 +0000496
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000497 unsigned createVirtualRegister(const TargetRegisterClass *RC) {
498 std::string S;
499 raw_string_ostream OS(S);
500 OS << "namedVReg" << (virtualVRegNumber & ~0x80000000);
501 OS.flush();
502 virtualVRegNumber++;
Puyan Lotfid6f73132018-04-05 00:27:15 +0000503
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000504 return MRI.createVirtualRegister(RC, OS.str());
505 }
Puyan Lotfid6f73132018-04-05 00:27:15 +0000506};
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000507
508static std::map<unsigned, unsigned>
509GetVRegRenameMap(const std::vector<TypedVReg> &VRegs,
510 const std::vector<unsigned> &renamedInOtherBB,
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000511 MachineRegisterInfo &MRI, NamedVRegCursor &NVC) {
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000512 std::map<unsigned, unsigned> VRegRenameMap;
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000513 bool FirstCandidate = true;
514
515 for (auto &vreg : VRegs) {
516 if (vreg.isFrameIndex()) {
517 // We skip one vreg for any frame index because there is a good chance
518 // (especially when comparing SelectionDAG to GlobalISel generated MIR)
519 // that in the other file we are just getting an incoming vreg that comes
520 // from a copy from a frame index. So it's safe to skip by one.
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000521 unsigned LastRenameReg = NVC.incrementVirtualVReg();
522 (void)LastRenameReg;
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000523 DEBUG(dbgs() << "Skipping rename for FI " << LastRenameReg << "\n";);
524 continue;
525 } else if (vreg.isCandidate()) {
526
527 // After the first candidate, for every subsequent candidate, we skip mod
528 // 10 registers so that the candidates are more likely to start at the
529 // same vreg number making it more likely that the canonical walk from the
530 // candidate insruction. We don't need to skip from the first candidate of
531 // the BasicBlock because we already skip ahead several vregs for each BB.
Puyan Lotfid6f73132018-04-05 00:27:15 +0000532 unsigned LastRenameReg = NVC.getVirtualVReg();
533 if (FirstCandidate)
534 NVC.incrementVirtualVReg(LastRenameReg % 10);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000535 FirstCandidate = false;
536 continue;
537 } else if (!TargetRegisterInfo::isVirtualRegister(vreg.getReg())) {
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000538 unsigned LastRenameReg = NVC.incrementVirtualVReg();
539 (void)LastRenameReg;
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000540 DEBUG({
541 dbgs() << "Skipping rename for Phys Reg " << LastRenameReg << "\n";
542 });
543 continue;
544 }
545
546 auto Reg = vreg.getReg();
547 if (llvm::find(renamedInOtherBB, Reg) != renamedInOtherBB.end()) {
548 DEBUG(dbgs() << "Vreg " << Reg << " already renamed in other BB.\n";);
549 continue;
550 }
551
Puyan Lotfid6f73132018-04-05 00:27:15 +0000552 auto Rename = NVC.createVirtualRegister(MRI.getRegClass(Reg));
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000553
554 if (VRegRenameMap.find(Reg) == VRegRenameMap.end()) {
555 DEBUG(dbgs() << "Mapping vreg ";);
556 if (MRI.reg_begin(Reg) != MRI.reg_end()) {
557 DEBUG(auto foo = &*MRI.reg_begin(Reg); foo->dump(););
558 } else {
559 DEBUG(dbgs() << Reg;);
560 }
561 DEBUG(dbgs() << " to ";);
562 if (MRI.reg_begin(Rename) != MRI.reg_end()) {
563 DEBUG(auto foo = &*MRI.reg_begin(Rename); foo->dump(););
564 } else {
565 DEBUG(dbgs() << Rename;);
566 }
567 DEBUG(dbgs() << "\n";);
568
569 VRegRenameMap.insert(std::pair<unsigned, unsigned>(Reg, Rename));
570 }
571 }
572
573 return VRegRenameMap;
574}
575
576static bool doVRegRenaming(std::vector<unsigned> &RenamedInOtherBB,
577 const std::map<unsigned, unsigned> &VRegRenameMap,
578 MachineRegisterInfo &MRI) {
579 bool Changed = false;
580 for (auto I = VRegRenameMap.begin(), E = VRegRenameMap.end(); I != E; ++I) {
581
582 auto VReg = I->first;
583 auto Rename = I->second;
584
585 RenamedInOtherBB.push_back(Rename);
586
587 std::vector<MachineOperand *> RenameMOs;
588 for (auto &MO : MRI.reg_operands(VReg)) {
589 RenameMOs.push_back(&MO);
590 }
591
592 for (auto *MO : RenameMOs) {
593 Changed = true;
594 MO->setReg(Rename);
595
596 if (!MO->isDef())
597 MO->setIsKill(false);
598 }
599 }
600
601 return Changed;
602}
603
604static bool doDefKillClear(MachineBasicBlock *MBB) {
605 bool Changed = false;
606
607 for (auto &MI : *MBB) {
608 for (auto &MO : MI.operands()) {
609 if (!MO.isReg())
610 continue;
611 if (!MO.isDef() && MO.isKill()) {
612 Changed = true;
613 MO.setIsKill(false);
614 }
615
616 if (MO.isDef() && MO.isDead()) {
617 Changed = true;
618 MO.setIsDead(false);
619 }
620 }
621 }
622
623 return Changed;
624}
625
626static bool runOnBasicBlock(MachineBasicBlock *MBB,
627 std::vector<StringRef> &bbNames,
628 std::vector<unsigned> &renamedInOtherBB,
Puyan Lotfid6f73132018-04-05 00:27:15 +0000629 unsigned &basicBlockNum, unsigned &VRegGapIndex,
630 NamedVRegCursor &NVC) {
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000631
632 if (CanonicalizeBasicBlockNumber != ~0U) {
633 if (CanonicalizeBasicBlockNumber != basicBlockNum++)
634 return false;
635 DEBUG(dbgs() << "\n Canonicalizing BasicBlock " << MBB->getName() << "\n";);
636 }
637
638 if (llvm::find(bbNames, MBB->getName()) != bbNames.end()) {
639 DEBUG({
640 dbgs() << "Found potentially duplicate BasicBlocks: " << MBB->getName()
641 << "\n";
642 });
643 return false;
644 }
645
646 DEBUG({
647 dbgs() << "\n\n NEW BASIC BLOCK: " << MBB->getName() << " \n\n";
648 dbgs() << "\n\n================================================\n\n";
649 });
650
651 bool Changed = false;
652 MachineFunction &MF = *MBB->getParent();
653 MachineRegisterInfo &MRI = MF.getRegInfo();
654
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000655 bbNames.push_back(MBB->getName());
656 DEBUG(dbgs() << "\n\n NEW BASIC BLOCK: " << MBB->getName() << "\n\n";);
657
Puyan Lotfi14b6637e2018-04-16 09:03:03 +0000658 DEBUG(dbgs() << "MBB Before Canonical Copy Propagation:\n"; MBB->dump(););
659 Changed |= propagateLocalCopies(MBB);
660 DEBUG(dbgs() << "MBB After Canonical Copy Propagation:\n"; MBB->dump(););
661
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000662 DEBUG(dbgs() << "MBB Before Scheduling:\n"; MBB->dump(););
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000663 unsigned IdempotentInstCount = 0;
664 Changed |= rescheduleCanonically(IdempotentInstCount, MBB);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000665 DEBUG(dbgs() << "MBB After Scheduling:\n"; MBB->dump(););
666
667 std::vector<MachineInstr *> Candidates = populateCandidates(MBB);
668 std::vector<MachineInstr *> VisitedMIs;
669 std::copy(Candidates.begin(), Candidates.end(),
670 std::back_inserter(VisitedMIs));
671
672 std::vector<TypedVReg> VRegs;
673 for (auto candidate : Candidates) {
674 VRegs.push_back(TypedVReg(RSE_NewCandidate));
675
676 std::queue<TypedVReg> RegQueue;
677
678 // Here we walk the vreg operands of a non-root node along our walk.
679 // The root nodes are the original candidates (stores normally).
680 // These are normally not the root nodes (except for the case of copies to
681 // physical registers).
682 for (unsigned i = 1; i < candidate->getNumOperands(); i++) {
683 if (candidate->mayStore() || candidate->isBranch())
684 break;
685
686 MachineOperand &MO = candidate->getOperand(i);
687 if (!(MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())))
688 continue;
689
690 DEBUG(dbgs() << "Enqueue register"; MO.dump(); dbgs() << "\n";);
691 RegQueue.push(TypedVReg(MO.getReg()));
692 }
693
694 // Here we walk the root candidates. We start from the 0th operand because
695 // the root is normally a store to a vreg.
696 for (unsigned i = 0; i < candidate->getNumOperands(); i++) {
697
698 if (!candidate->mayStore() && !candidate->isBranch())
699 break;
700
701 MachineOperand &MO = candidate->getOperand(i);
702
703 // TODO: Do we want to only add vregs here?
704 if (!MO.isReg() && !MO.isFI())
705 continue;
706
707 DEBUG(dbgs() << "Enqueue Reg/FI"; MO.dump(); dbgs() << "\n";);
708
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000709 RegQueue.push(MO.isReg() ? TypedVReg(MO.getReg())
710 : TypedVReg(RSE_FrameIndex));
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000711 }
712
713 doCandidateWalk(VRegs, RegQueue, VisitedMIs, MBB);
714 }
715
716 // If we have populated no vregs to rename then bail.
717 // The rest of this function does the vreg remaping.
718 if (VRegs.size() == 0)
719 return Changed;
720
Puyan Lotfid6f73132018-04-05 00:27:15 +0000721 auto VRegRenameMap = GetVRegRenameMap(VRegs, renamedInOtherBB, MRI, NVC);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000722 Changed |= doVRegRenaming(renamedInOtherBB, VRegRenameMap, MRI);
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000723
724 // Here we renumber the def vregs for the idempotent instructions from the top
725 // of the MachineBasicBlock so that they are named in the order that we sorted
726 // them alphabetically. Eventually we wont need SkipVRegs because we will use
727 // named vregs instead.
Puyan Lotfid6f73132018-04-05 00:27:15 +0000728 NVC.SkipVRegs();
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000729
730 auto MII = MBB->begin();
731 for (unsigned i = 0; i < IdempotentInstCount && MII != MBB->end(); ++i) {
732 MachineInstr &MI = *MII++;
733 Changed = true;
734 unsigned vRegToRename = MI.getOperand(0).getReg();
Puyan Lotfid6f73132018-04-05 00:27:15 +0000735 auto Rename = NVC.createVirtualRegister(MRI.getRegClass(vRegToRename));
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000736
737 std::vector<MachineOperand *> RenameMOs;
738 for (auto &MO : MRI.reg_operands(vRegToRename)) {
739 RenameMOs.push_back(&MO);
740 }
741
742 for (auto *MO : RenameMOs) {
743 MO->setReg(Rename);
744 }
745 }
746
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000747 Changed |= doDefKillClear(MBB);
748
749 DEBUG(dbgs() << "Updated MachineBasicBlock:\n"; MBB->dump(); dbgs() << "\n";);
750 DEBUG(dbgs() << "\n\n================================================\n\n");
751 return Changed;
752}
753
754bool MIRCanonicalizer::runOnMachineFunction(MachineFunction &MF) {
755
756 static unsigned functionNum = 0;
757 if (CanonicalizeFunctionNumber != ~0U) {
758 if (CanonicalizeFunctionNumber != functionNum++)
759 return false;
760 DEBUG(dbgs() << "\n Canonicalizing Function " << MF.getName() << "\n";);
761 }
762
763 // we need a valid vreg to create a vreg type for skipping all those
764 // stray vreg numbers so reach alignment/canonical vreg values.
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000765 std::vector<MachineBasicBlock *> RPOList = GetRPOList(MF);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000766
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000767 DEBUG(dbgs() << "\n\n NEW MACHINE FUNCTION: " << MF.getName() << " \n\n";
768 dbgs() << "\n\n================================================\n\n";
769 dbgs() << "Total Basic Blocks: " << RPOList.size() << "\n";
770 for (auto MBB
771 : RPOList) { dbgs() << MBB->getName() << "\n"; } dbgs()
772 << "\n\n================================================\n\n";);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000773
774 std::vector<StringRef> BBNames;
775 std::vector<unsigned> RenamedInOtherBB;
776
777 unsigned GapIdx = 0;
778 unsigned BBNum = 0;
779
780 bool Changed = false;
781
Puyan Lotfid6f73132018-04-05 00:27:15 +0000782 MachineRegisterInfo &MRI = MF.getRegInfo();
783 NamedVRegCursor NVC(MRI);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000784 for (auto MBB : RPOList)
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000785 Changed |=
786 runOnBasicBlock(MBB, BBNames, RenamedInOtherBB, BBNum, GapIdx, NVC);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000787
788 return Changed;
789}