blob: 650240e60fef96a1ba6b7df61859db25c12324f0 [file] [log] [blame]
Puyan Lotfia521c4ac52017-11-02 23:37:32 +00001//===-------------- MIRCanonicalizer.cpp - MIR Canonicalizer --------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Puyan Lotfia521c4ac52017-11-02 23:37:32 +00006//
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"
Puyan Lotfia521c4ac52017-11-02 23:37:32 +000028#include "llvm/CodeGen/MachineFunctionPass.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000031#include "llvm/CodeGen/Passes.h"
Puyan Lotfia521c4ac52017-11-02 23:37:32 +000032#include "llvm/Support/raw_ostream.h"
Puyan Lotfia521c4ac52017-11-02 23:37:32 +000033
34#include <queue>
35
36using namespace llvm;
37
38namespace llvm {
39extern char &MIRCanonicalizerID;
40} // namespace llvm
41
42#define DEBUG_TYPE "mir-canonicalizer"
43
44static cl::opt<unsigned>
Puyan Lotfi6ea89b42018-04-16 08:12:15 +000045 CanonicalizeFunctionNumber("canon-nth-function", cl::Hidden, cl::init(~0u),
46 cl::value_desc("N"),
47 cl::desc("Function number to canonicalize."));
Puyan Lotfia521c4ac52017-11-02 23:37:32 +000048
Puyan Lotfi6ea89b42018-04-16 08:12:15 +000049static cl::opt<unsigned> CanonicalizeBasicBlockNumber(
50 "canon-nth-basicblock", cl::Hidden, cl::init(~0u), cl::value_desc("N"),
51 cl::desc("BasicBlock number to canonicalize."));
Puyan Lotfia521c4ac52017-11-02 23:37:32 +000052
53namespace {
54
55class MIRCanonicalizer : public MachineFunctionPass {
56public:
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
74enum VRType { RSE_Reg = 0, RSE_FrameIndex, RSE_NewCandidate };
75class TypedVReg {
76 VRType type;
77 unsigned reg;
78
79public:
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
Puyan Lotfi6ea89b42018-04-16 08:12:15 +000085 bool isReg() const { return type == RSE_Reg; }
86 bool isFrameIndex() const { return type == RSE_FrameIndex; }
87 bool isCandidate() const { return type == RSE_NewCandidate; }
Puyan Lotfia521c4ac52017-11-02 23:37:32 +000088
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
96char MIRCanonicalizer::ID;
97
98char &llvm::MIRCanonicalizerID = MIRCanonicalizer::ID;
99
100INITIALIZE_PASS_BEGIN(MIRCanonicalizer, "mir-canonicalizer",
Craig Topper666e23b2017-11-03 18:02:46 +0000101 "Rename Register Operands Canonically", false, false)
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000102
103INITIALIZE_PASS_END(MIRCanonicalizer, "mir-canonicalizer",
Craig Topper666e23b2017-11-03 18:02:46 +0000104 "Rename Register Operands Canonically", false, false)
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000105
106static std::vector<MachineBasicBlock *> GetRPOList(MachineFunction &MF) {
Puyan Lotfidaaecf92019-05-30 21:37:25 +0000107 if (MF.empty())
108 return {};
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000109 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
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000118static bool
119rescheduleLexographically(std::vector<MachineInstr *> instructions,
120 MachineBasicBlock *MBB,
121 std::function<MachineBasicBlock::iterator()> getPos) {
122
123 bool Changed = false;
Puyan Lotfi380a6f52018-05-13 06:07:20 +0000124 using StringInstrPair = std::pair<std::string, MachineInstr *>;
125 std::vector<StringInstrPair> StringInstrMap;
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000126
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("=");
Puyan Lotfi380a6f52018-05-13 06:07:20 +0000135 StringInstrMap.push_back({(i == std::string::npos) ? S : S.substr(i), II});
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000136 }
137
Fangrui Song0cac7262018-09-27 02:13:45 +0000138 llvm::sort(StringInstrMap,
139 [](const StringInstrPair &a, const StringInstrPair &b) -> bool {
140 return (a.first < b.first);
141 });
Puyan Lotfi380a6f52018-05-13 06:07:20 +0000142
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000143 for (auto &II : StringInstrMap) {
144
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000145 LLVM_DEBUG({
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000146 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
159static bool rescheduleCanonically(unsigned &PseudoIdempotentInstCount,
160 MachineBasicBlock *MBB) {
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000161
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
Puyan Lotfi26c504f2018-04-05 00:08:15 +0000182 std::map<MachineInstr *, std::vector<MachineInstr *>> MultiUsers;
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000183 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
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000201 for (auto *II : Instructions) {
202 if (II->getNumOperands() == 0)
203 continue;
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000204 if (II->mayLoadOrStore())
205 continue;
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000206
207 MachineOperand &MO = II->getOperand(0);
208 if (!MO.isReg() || !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
209 continue;
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000210 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()) ==
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000223 PhysRegDefs.end()) {
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000224 continue;
225 }
226 }
227
228 IsPseudoIdempotent = false;
229 break;
230 }
231
232 if (IsPseudoIdempotent) {
233 PseudoIdempotentInstructions.push_back(II);
234 continue;
235 }
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000236
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000237 LLVM_DEBUG(dbgs() << "Operand " << 0 << " of "; II->dump(); MO.dump(););
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000238
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
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000270 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
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000284 LLVM_DEBUG({
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000285 dbgs() << "Splicing ";
286 DefI->dump();
287 dbgs() << " right before: ";
288 UseI->dump();
289 });
290
Puyan Lotfi26c504f2018-04-05 00:08:15 +0000291 MultiUsers[UseToBringDefCloserTo].push_back(Def);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000292 Changed = true;
293 MBB->splice(UseI, MBB, DefI);
294 }
295
Puyan Lotfi26c504f2018-04-05 00:08:15 +0000296 // 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
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000306 LLVM_DEBUG(
307 dbgs() << "Rescheduling Multi-Use Instructions Lexographically.";);
Puyan Lotfi26c504f2018-04-05 00:08:15 +0000308 Changed |= rescheduleLexographically(
309 E.second, MBB, [&]() -> MachineBasicBlock::iterator { return UseI; });
310 }
311
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000312 PseudoIdempotentInstCount = PseudoIdempotentInstructions.size();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000313 LLVM_DEBUG(
314 dbgs() << "Rescheduling Idempotent Instructions Lexographically.";);
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000315 Changed |= rescheduleLexographically(
316 PseudoIdempotentInstructions, MBB,
317 [&]() -> MachineBasicBlock::iterator { return MBB->begin(); });
318
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000319 return Changed;
320}
321
Benjamin Kramer651d0bf2018-05-15 21:26:47 +0000322static bool propagateLocalCopies(MachineBasicBlock *MBB) {
Puyan Lotfi14b6637e2018-04-16 09:03:03 +0000323 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
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000361/// 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.
367static 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) {
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000381 if (DoesMISideEffect)
382 break;
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000383 DoesMISideEffect |= (UI->getParent()->getParent() != MI->getParent());
384 }
385 }
386
387 if (!MI->mayStore() && !MI->isBranch() && !DoesMISideEffect)
388 continue;
389
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000390 LLVM_DEBUG(dbgs() << "Found Candidate: "; MI->dump(););
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000391 Candidates.push_back(MI);
392 }
393
394 return Candidates;
395}
396
Benjamin Kramer51ebcaa2017-11-24 14:55:41 +0000397static void doCandidateWalk(std::vector<TypedVReg> &VRegs,
398 std::queue<TypedVReg> &RegQueue,
399 std::vector<MachineInstr *> &VisitedMIs,
400 const MachineBasicBlock *MBB) {
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000401
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()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000411 LLVM_DEBUG(dbgs() << "Popping frame index.\n";);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000412 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)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000420 LLVM_DEBUG({
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000421 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 {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000432 LLVM_DEBUG(dbgs() << "Popping physreg.\n";);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000433 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
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000448 LLVM_DEBUG({
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000449 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()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000460 LLVM_DEBUG(dbgs() << "Pushing frame index.\n";);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000461 RegQueue.push(TypedVReg(RSE_FrameIndex));
462 }
463
464 if (!MO.isReg())
465 continue;
466 RegQueue.push(TypedVReg(MO.getReg()));
467 }
468 }
469 }
470}
471
Benjamin Kramer651d0bf2018-05-15 21:26:47 +0000472namespace {
Puyan Lotfid6f73132018-04-05 00:27:15 +0000473class NamedVRegCursor {
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000474 MachineRegisterInfo &MRI;
475 unsigned virtualVRegNumber;
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000476
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000477public:
478 NamedVRegCursor(MachineRegisterInfo &MRI) : MRI(MRI) {
479 unsigned VRegGapIndex = 0;
480 const unsigned VR_GAP = (++VRegGapIndex * 1000);
Puyan Lotfid6f73132018-04-05 00:27:15 +0000481
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000482 unsigned I = MRI.createIncompleteVirtualRegister();
483 const unsigned E = (((I + VR_GAP) / VR_GAP) + 1) * VR_GAP;
Puyan Lotfid6f73132018-04-05 00:27:15 +0000484
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000485 virtualVRegNumber = E;
486 }
Puyan Lotfid6f73132018-04-05 00:27:15 +0000487
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000488 void SkipVRegs() {
489 unsigned VRegGapIndex = 1;
490 const unsigned VR_GAP = (++VRegGapIndex * 1000);
Puyan Lotfid6f73132018-04-05 00:27:15 +0000491
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000492 unsigned I = virtualVRegNumber;
493 const unsigned E = (((I + VR_GAP) / VR_GAP) + 1) * VR_GAP;
Puyan Lotfid6f73132018-04-05 00:27:15 +0000494
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000495 virtualVRegNumber = E;
496 }
Puyan Lotfid6f73132018-04-05 00:27:15 +0000497
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000498 unsigned getVirtualVReg() const { return virtualVRegNumber; }
Puyan Lotfid6f73132018-04-05 00:27:15 +0000499
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000500 unsigned incrementVirtualVReg(unsigned incr = 1) {
501 virtualVRegNumber += incr;
502 return virtualVRegNumber;
503 }
Puyan Lotfid6f73132018-04-05 00:27:15 +0000504
Puyan Lotfi0f4446b2019-05-30 18:06:28 +0000505 unsigned createVirtualRegister(unsigned VReg) {
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000506 std::string S;
507 raw_string_ostream OS(S);
508 OS << "namedVReg" << (virtualVRegNumber & ~0x80000000);
509 OS.flush();
510 virtualVRegNumber++;
Puyan Lotfi0f4446b2019-05-30 18:06:28 +0000511 if (auto RC = MRI.getRegClassOrNull(VReg))
512 return MRI.createVirtualRegister(RC, OS.str());
513 return MRI.createGenericVirtualRegister(MRI.getType(VReg), OS.str());
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000514 }
Puyan Lotfid6f73132018-04-05 00:27:15 +0000515};
Benjamin Kramer651d0bf2018-05-15 21:26:47 +0000516} // namespace
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000517
518static std::map<unsigned, unsigned>
519GetVRegRenameMap(const std::vector<TypedVReg> &VRegs,
520 const std::vector<unsigned> &renamedInOtherBB,
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000521 MachineRegisterInfo &MRI, NamedVRegCursor &NVC) {
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000522 std::map<unsigned, unsigned> VRegRenameMap;
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000523 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.
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000531 unsigned LastRenameReg = NVC.incrementVirtualVReg();
532 (void)LastRenameReg;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000533 LLVM_DEBUG(dbgs() << "Skipping rename for FI " << LastRenameReg << "\n";);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000534 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.
Puyan Lotfid6f73132018-04-05 00:27:15 +0000542 unsigned LastRenameReg = NVC.getVirtualVReg();
543 if (FirstCandidate)
544 NVC.incrementVirtualVReg(LastRenameReg % 10);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000545 FirstCandidate = false;
546 continue;
547 } else if (!TargetRegisterInfo::isVirtualRegister(vreg.getReg())) {
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000548 unsigned LastRenameReg = NVC.incrementVirtualVReg();
549 (void)LastRenameReg;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000550 LLVM_DEBUG({
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000551 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()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000558 LLVM_DEBUG(dbgs() << "Vreg " << Reg
559 << " already renamed in other BB.\n";);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000560 continue;
561 }
562
Puyan Lotfi0f4446b2019-05-30 18:06:28 +0000563 auto Rename = NVC.createVirtualRegister(Reg);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000564
565 if (VRegRenameMap.find(Reg) == VRegRenameMap.end()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000566 LLVM_DEBUG(dbgs() << "Mapping vreg ";);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000567 if (MRI.reg_begin(Reg) != MRI.reg_end()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000568 LLVM_DEBUG(auto foo = &*MRI.reg_begin(Reg); foo->dump(););
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000569 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000570 LLVM_DEBUG(dbgs() << Reg;);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000571 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000572 LLVM_DEBUG(dbgs() << " to ";);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000573 if (MRI.reg_begin(Rename) != MRI.reg_end()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000574 LLVM_DEBUG(auto foo = &*MRI.reg_begin(Rename); foo->dump(););
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000575 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000576 LLVM_DEBUG(dbgs() << Rename;);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000577 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000578 LLVM_DEBUG(dbgs() << "\n";);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000579
580 VRegRenameMap.insert(std::pair<unsigned, unsigned>(Reg, Rename));
581 }
582 }
583
584 return VRegRenameMap;
585}
586
587static 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
615static 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
637static bool runOnBasicBlock(MachineBasicBlock *MBB,
638 std::vector<StringRef> &bbNames,
639 std::vector<unsigned> &renamedInOtherBB,
Puyan Lotfid6f73132018-04-05 00:27:15 +0000640 unsigned &basicBlockNum, unsigned &VRegGapIndex,
641 NamedVRegCursor &NVC) {
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000642
643 if (CanonicalizeBasicBlockNumber != ~0U) {
644 if (CanonicalizeBasicBlockNumber != basicBlockNum++)
645 return false;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000646 LLVM_DEBUG(dbgs() << "\n Canonicalizing BasicBlock " << MBB->getName()
647 << "\n";);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000648 }
649
650 if (llvm::find(bbNames, MBB->getName()) != bbNames.end()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000651 LLVM_DEBUG({
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000652 dbgs() << "Found potentially duplicate BasicBlocks: " << MBB->getName()
653 << "\n";
654 });
655 return false;
656 }
657
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000658 LLVM_DEBUG({
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000659 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
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000667 bbNames.push_back(MBB->getName());
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000668 LLVM_DEBUG(dbgs() << "\n\n NEW BASIC BLOCK: " << MBB->getName() << "\n\n";);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000669
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000670 LLVM_DEBUG(dbgs() << "MBB Before Canonical Copy Propagation:\n";
671 MBB->dump(););
Puyan Lotfi14b6637e2018-04-16 09:03:03 +0000672 Changed |= propagateLocalCopies(MBB);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000673 LLVM_DEBUG(dbgs() << "MBB After Canonical Copy Propagation:\n"; MBB->dump(););
Puyan Lotfi14b6637e2018-04-16 09:03:03 +0000674
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000675 LLVM_DEBUG(dbgs() << "MBB Before Scheduling:\n"; MBB->dump(););
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000676 unsigned IdempotentInstCount = 0;
677 Changed |= rescheduleCanonically(IdempotentInstCount, MBB);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000678 LLVM_DEBUG(dbgs() << "MBB After Scheduling:\n"; MBB->dump(););
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000679
680 std::vector<MachineInstr *> Candidates = populateCandidates(MBB);
681 std::vector<MachineInstr *> VisitedMIs;
Fangrui Song75709322018-11-17 01:44:25 +0000682 llvm::copy(Candidates, std::back_inserter(VisitedMIs));
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000683
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
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000702 LLVM_DEBUG(dbgs() << "Enqueue register"; MO.dump(); dbgs() << "\n";);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000703 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
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000719 LLVM_DEBUG(dbgs() << "Enqueue Reg/FI"; MO.dump(); dbgs() << "\n";);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000720
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000721 RegQueue.push(MO.isReg() ? TypedVReg(MO.getReg())
722 : TypedVReg(RSE_FrameIndex));
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000723 }
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
Puyan Lotfid6f73132018-04-05 00:27:15 +0000733 auto VRegRenameMap = GetVRegRenameMap(VRegs, renamedInOtherBB, MRI, NVC);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000734 Changed |= doVRegRenaming(renamedInOtherBB, VRegRenameMap, MRI);
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000735
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.
Puyan Lotfid6f73132018-04-05 00:27:15 +0000740 NVC.SkipVRegs();
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000741
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();
Puyan Lotfi0f4446b2019-05-30 18:06:28 +0000747 auto Rename = NVC.createVirtualRegister(vRegToRename);
Puyan Lotfi57c4f382018-03-31 05:48:51 +0000748
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
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000759 Changed |= doDefKillClear(MBB);
760
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000761 LLVM_DEBUG(dbgs() << "Updated MachineBasicBlock:\n"; MBB->dump();
762 dbgs() << "\n";);
763 LLVM_DEBUG(
764 dbgs() << "\n\n================================================\n\n");
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000765 return Changed;
766}
767
768bool MIRCanonicalizer::runOnMachineFunction(MachineFunction &MF) {
769
770 static unsigned functionNum = 0;
771 if (CanonicalizeFunctionNumber != ~0U) {
772 if (CanonicalizeFunctionNumber != functionNum++)
773 return false;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000774 LLVM_DEBUG(dbgs() << "\n Canonicalizing Function " << MF.getName()
775 << "\n";);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000776 }
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.
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000780 std::vector<MachineBasicBlock *> RPOList = GetRPOList(MF);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000781
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000782 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";);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000789
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
Puyan Lotfid6f73132018-04-05 00:27:15 +0000798 MachineRegisterInfo &MRI = MF.getRegInfo();
799 NamedVRegCursor NVC(MRI);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000800 for (auto MBB : RPOList)
Puyan Lotfi6ea89b42018-04-16 08:12:15 +0000801 Changed |=
802 runOnBasicBlock(MBB, BBNames, RenamedInOtherBB, BBNum, GapIdx, NVC);
Puyan Lotfia521c4ac52017-11-02 23:37:32 +0000803
804 return Changed;
805}