1a521c4acSPuyan Lotfi //===-------------- MIRCanonicalizer.cpp - MIR Canonicalizer --------------===//
2a521c4acSPuyan Lotfi //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a521c4acSPuyan Lotfi //
7a521c4acSPuyan Lotfi //===----------------------------------------------------------------------===//
8a521c4acSPuyan Lotfi //
9a521c4acSPuyan Lotfi // The purpose of this pass is to employ a canonical code transformation so
10a521c4acSPuyan Lotfi // that code compiled with slightly different IR passes can be diffed more
11a521c4acSPuyan Lotfi // effectively than otherwise. This is done by renaming vregs in a given
12a521c4acSPuyan Lotfi // LiveRange in a canonical way. This pass also does a pseudo-scheduling to
13a521c4acSPuyan Lotfi // move defs closer to their use inorder to reduce diffs caused by slightly
14a521c4acSPuyan Lotfi // different schedules.
15a521c4acSPuyan Lotfi //
16a521c4acSPuyan Lotfi // Basic Usage:
17a521c4acSPuyan Lotfi //
18a521c4acSPuyan Lotfi // llc -o - -run-pass mir-canonicalizer example.mir
19a521c4acSPuyan Lotfi //
20a521c4acSPuyan Lotfi // Reorders instructions canonically.
21a521c4acSPuyan Lotfi // Renames virtual register operands canonically.
22a521c4acSPuyan Lotfi // Strips certain MIR artifacts (optionally).
23a521c4acSPuyan Lotfi //
24a521c4acSPuyan Lotfi //===----------------------------------------------------------------------===//
25a521c4acSPuyan Lotfi
26028061d4SPuyan Lotfi #include "MIRVRegNamerUtils.h"
27a521c4acSPuyan Lotfi #include "llvm/ADT/PostOrderIterator.h"
28a521c4acSPuyan Lotfi #include "llvm/ADT/STLExtras.h"
29a521c4acSPuyan Lotfi #include "llvm/CodeGen/MachineFunctionPass.h"
30a521c4acSPuyan Lotfi #include "llvm/CodeGen/MachineRegisterInfo.h"
3105da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
32989f1c72Sserge-sans-paille #include "llvm/Pass.h"
331d7b4136SReid Kleckner #include "llvm/Support/Debug.h"
34a521c4acSPuyan Lotfi #include "llvm/Support/raw_ostream.h"
35a521c4acSPuyan Lotfi
36a521c4acSPuyan Lotfi using namespace llvm;
37a521c4acSPuyan Lotfi
38a521c4acSPuyan Lotfi #define DEBUG_TYPE "mir-canonicalizer"
39a521c4acSPuyan Lotfi
40a521c4acSPuyan Lotfi static cl::opt<unsigned>
41a521c4acSPuyan Lotfi CanonicalizeFunctionNumber("canon-nth-function", cl::Hidden, cl::init(~0u),
42a521c4acSPuyan Lotfi cl::value_desc("N"),
43a521c4acSPuyan Lotfi cl::desc("Function number to canonicalize."));
44a521c4acSPuyan Lotfi
45a521c4acSPuyan Lotfi namespace {
46a521c4acSPuyan Lotfi
47a521c4acSPuyan Lotfi class MIRCanonicalizer : public MachineFunctionPass {
48a521c4acSPuyan Lotfi public:
49a521c4acSPuyan Lotfi static char ID;
MIRCanonicalizer()50a521c4acSPuyan Lotfi MIRCanonicalizer() : MachineFunctionPass(ID) {}
51a521c4acSPuyan Lotfi
getPassName() const52a521c4acSPuyan Lotfi StringRef getPassName() const override {
53a521c4acSPuyan Lotfi return "Rename register operands in a canonical ordering.";
54a521c4acSPuyan Lotfi }
55a521c4acSPuyan Lotfi
getAnalysisUsage(AnalysisUsage & AU) const56a521c4acSPuyan Lotfi void getAnalysisUsage(AnalysisUsage &AU) const override {
57a521c4acSPuyan Lotfi AU.setPreservesCFG();
58a521c4acSPuyan Lotfi MachineFunctionPass::getAnalysisUsage(AU);
59a521c4acSPuyan Lotfi }
60a521c4acSPuyan Lotfi
61a521c4acSPuyan Lotfi bool runOnMachineFunction(MachineFunction &MF) override;
62a521c4acSPuyan Lotfi };
63a521c4acSPuyan Lotfi
64a521c4acSPuyan Lotfi } // end anonymous namespace
65a521c4acSPuyan Lotfi
66a521c4acSPuyan Lotfi char MIRCanonicalizer::ID;
67a521c4acSPuyan Lotfi
68a521c4acSPuyan Lotfi char &llvm::MIRCanonicalizerID = MIRCanonicalizer::ID;
69a521c4acSPuyan Lotfi
70a521c4acSPuyan Lotfi INITIALIZE_PASS_BEGIN(MIRCanonicalizer, "mir-canonicalizer",
71666e23b5SCraig Topper "Rename Register Operands Canonically", false, false)
72a521c4acSPuyan Lotfi
73a521c4acSPuyan Lotfi INITIALIZE_PASS_END(MIRCanonicalizer, "mir-canonicalizer",
74666e23b5SCraig Topper "Rename Register Operands Canonically", false, false)
75a521c4acSPuyan Lotfi
GetRPOList(MachineFunction & MF)76a521c4acSPuyan Lotfi static std::vector<MachineBasicBlock *> GetRPOList(MachineFunction &MF) {
77daaecf98SPuyan Lotfi if (MF.empty())
78daaecf98SPuyan Lotfi return {};
79a521c4acSPuyan Lotfi ReversePostOrderTraversal<MachineBasicBlock *> RPOT(&*MF.begin());
80a521c4acSPuyan Lotfi std::vector<MachineBasicBlock *> RPOList;
81c5c4dbd2SKazu Hirata append_range(RPOList, RPOT);
82a521c4acSPuyan Lotfi
83a521c4acSPuyan Lotfi return RPOList;
84a521c4acSPuyan Lotfi }
85a521c4acSPuyan Lotfi
8657c4f38cSPuyan Lotfi static bool
rescheduleLexographically(std::vector<MachineInstr * > instructions,MachineBasicBlock * MBB,std::function<MachineBasicBlock::iterator ()> getPos)8757c4f38cSPuyan Lotfi rescheduleLexographically(std::vector<MachineInstr *> instructions,
8857c4f38cSPuyan Lotfi MachineBasicBlock *MBB,
8957c4f38cSPuyan Lotfi std::function<MachineBasicBlock::iterator()> getPos) {
9057c4f38cSPuyan Lotfi
9157c4f38cSPuyan Lotfi bool Changed = false;
92380a6f55SPuyan Lotfi using StringInstrPair = std::pair<std::string, MachineInstr *>;
93380a6f55SPuyan Lotfi std::vector<StringInstrPair> StringInstrMap;
9457c4f38cSPuyan Lotfi
9557c4f38cSPuyan Lotfi for (auto *II : instructions) {
9657c4f38cSPuyan Lotfi std::string S;
9757c4f38cSPuyan Lotfi raw_string_ostream OS(S);
9857c4f38cSPuyan Lotfi II->print(OS);
9957c4f38cSPuyan Lotfi OS.flush();
10057c4f38cSPuyan Lotfi
101abada503SFilipe Cabecinhas // Trim the assignment, or start from the beginning in the case of a store.
102c70f3686SFangrui Song const size_t i = S.find('=');
103380a6f55SPuyan Lotfi StringInstrMap.push_back({(i == std::string::npos) ? S : S.substr(i), II});
10457c4f38cSPuyan Lotfi }
10557c4f38cSPuyan Lotfi
1064969a692SKazu Hirata llvm::sort(StringInstrMap, llvm::less_first());
107380a6f55SPuyan Lotfi
10857c4f38cSPuyan Lotfi for (auto &II : StringInstrMap) {
10957c4f38cSPuyan Lotfi
110d34e60caSNicola Zaghen LLVM_DEBUG({
11157c4f38cSPuyan Lotfi dbgs() << "Splicing ";
11257c4f38cSPuyan Lotfi II.second->dump();
11357c4f38cSPuyan Lotfi dbgs() << " right before: ";
11457c4f38cSPuyan Lotfi getPos()->dump();
11557c4f38cSPuyan Lotfi });
11657c4f38cSPuyan Lotfi
11757c4f38cSPuyan Lotfi Changed = true;
11857c4f38cSPuyan Lotfi MBB->splice(getPos(), MBB, II.second);
11957c4f38cSPuyan Lotfi }
12057c4f38cSPuyan Lotfi
12157c4f38cSPuyan Lotfi return Changed;
12257c4f38cSPuyan Lotfi }
12357c4f38cSPuyan Lotfi
rescheduleCanonically(unsigned & PseudoIdempotentInstCount,MachineBasicBlock * MBB)12457c4f38cSPuyan Lotfi static bool rescheduleCanonically(unsigned &PseudoIdempotentInstCount,
12557c4f38cSPuyan Lotfi MachineBasicBlock *MBB) {
126a521c4acSPuyan Lotfi
127a521c4acSPuyan Lotfi bool Changed = false;
128a521c4acSPuyan Lotfi
129abada503SFilipe Cabecinhas // Calculates the distance of MI from the beginning of its parent BB.
130a521c4acSPuyan Lotfi auto getInstrIdx = [](const MachineInstr &MI) {
131a521c4acSPuyan Lotfi unsigned i = 0;
132*9e6d1f4bSKazu Hirata for (const auto &CurMI : *MI.getParent()) {
133a521c4acSPuyan Lotfi if (&CurMI == &MI)
134a521c4acSPuyan Lotfi return i;
135a521c4acSPuyan Lotfi i++;
136a521c4acSPuyan Lotfi }
137a521c4acSPuyan Lotfi return ~0U;
138a521c4acSPuyan Lotfi };
139a521c4acSPuyan Lotfi
140a521c4acSPuyan Lotfi // Pre-Populate vector of instructions to reschedule so that we don't
141a521c4acSPuyan Lotfi // clobber the iterator.
142a521c4acSPuyan Lotfi std::vector<MachineInstr *> Instructions;
143a521c4acSPuyan Lotfi for (auto &MI : *MBB) {
144a521c4acSPuyan Lotfi Instructions.push_back(&MI);
145a521c4acSPuyan Lotfi }
146a521c4acSPuyan Lotfi
14726c504feSPuyan Lotfi std::map<MachineInstr *, std::vector<MachineInstr *>> MultiUsers;
1484d89462aSPuyan Lotfi std::map<unsigned, MachineInstr *> MultiUserLookup;
1494d89462aSPuyan Lotfi unsigned UseToBringDefCloserToCount = 0;
15057c4f38cSPuyan Lotfi std::vector<MachineInstr *> PseudoIdempotentInstructions;
15157c4f38cSPuyan Lotfi std::vector<unsigned> PhysRegDefs;
15257c4f38cSPuyan Lotfi for (auto *II : Instructions) {
15357c4f38cSPuyan Lotfi for (unsigned i = 1; i < II->getNumOperands(); i++) {
15457c4f38cSPuyan Lotfi MachineOperand &MO = II->getOperand(i);
15557c4f38cSPuyan Lotfi if (!MO.isReg())
15657c4f38cSPuyan Lotfi continue;
15757c4f38cSPuyan Lotfi
1582bea69bfSDaniel Sanders if (Register::isVirtualRegister(MO.getReg()))
15957c4f38cSPuyan Lotfi continue;
16057c4f38cSPuyan Lotfi
16157c4f38cSPuyan Lotfi if (!MO.isDef())
16257c4f38cSPuyan Lotfi continue;
16357c4f38cSPuyan Lotfi
16457c4f38cSPuyan Lotfi PhysRegDefs.push_back(MO.getReg());
16557c4f38cSPuyan Lotfi }
16657c4f38cSPuyan Lotfi }
16757c4f38cSPuyan Lotfi
168a521c4acSPuyan Lotfi for (auto *II : Instructions) {
169a521c4acSPuyan Lotfi if (II->getNumOperands() == 0)
170a521c4acSPuyan Lotfi continue;
17157c4f38cSPuyan Lotfi if (II->mayLoadOrStore())
17257c4f38cSPuyan Lotfi continue;
173a521c4acSPuyan Lotfi
174a521c4acSPuyan Lotfi MachineOperand &MO = II->getOperand(0);
1752bea69bfSDaniel Sanders if (!MO.isReg() || !Register::isVirtualRegister(MO.getReg()))
176a521c4acSPuyan Lotfi continue;
17757c4f38cSPuyan Lotfi if (!MO.isDef())
17857c4f38cSPuyan Lotfi continue;
17957c4f38cSPuyan Lotfi
18057c4f38cSPuyan Lotfi bool IsPseudoIdempotent = true;
18157c4f38cSPuyan Lotfi for (unsigned i = 1; i < II->getNumOperands(); i++) {
18257c4f38cSPuyan Lotfi
18357c4f38cSPuyan Lotfi if (II->getOperand(i).isImm()) {
18457c4f38cSPuyan Lotfi continue;
18557c4f38cSPuyan Lotfi }
18657c4f38cSPuyan Lotfi
18757c4f38cSPuyan Lotfi if (II->getOperand(i).isReg()) {
1882bea69bfSDaniel Sanders if (!Register::isVirtualRegister(II->getOperand(i).getReg()))
1892583d8ebSKazu Hirata if (!llvm::is_contained(PhysRegDefs, II->getOperand(i).getReg())) {
19057c4f38cSPuyan Lotfi continue;
19157c4f38cSPuyan Lotfi }
19257c4f38cSPuyan Lotfi }
19357c4f38cSPuyan Lotfi
19457c4f38cSPuyan Lotfi IsPseudoIdempotent = false;
19557c4f38cSPuyan Lotfi break;
19657c4f38cSPuyan Lotfi }
19757c4f38cSPuyan Lotfi
19857c4f38cSPuyan Lotfi if (IsPseudoIdempotent) {
19957c4f38cSPuyan Lotfi PseudoIdempotentInstructions.push_back(II);
20057c4f38cSPuyan Lotfi continue;
20157c4f38cSPuyan Lotfi }
202a521c4acSPuyan Lotfi
203d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Operand " << 0 << " of "; II->dump(); MO.dump(););
204a521c4acSPuyan Lotfi
205a521c4acSPuyan Lotfi MachineInstr *Def = II;
206a521c4acSPuyan Lotfi unsigned Distance = ~0U;
207a521c4acSPuyan Lotfi MachineInstr *UseToBringDefCloserTo = nullptr;
208a521c4acSPuyan Lotfi MachineRegisterInfo *MRI = &MBB->getParent()->getRegInfo();
209a521c4acSPuyan Lotfi for (auto &UO : MRI->use_nodbg_operands(MO.getReg())) {
210a521c4acSPuyan Lotfi MachineInstr *UseInst = UO.getParent();
211a521c4acSPuyan Lotfi
212a521c4acSPuyan Lotfi const unsigned DefLoc = getInstrIdx(*Def);
213a521c4acSPuyan Lotfi const unsigned UseLoc = getInstrIdx(*UseInst);
214a521c4acSPuyan Lotfi const unsigned Delta = (UseLoc - DefLoc);
215a521c4acSPuyan Lotfi
216a521c4acSPuyan Lotfi if (UseInst->getParent() != Def->getParent())
217a521c4acSPuyan Lotfi continue;
218a521c4acSPuyan Lotfi if (DefLoc >= UseLoc)
219a521c4acSPuyan Lotfi continue;
220a521c4acSPuyan Lotfi
221a521c4acSPuyan Lotfi if (Delta < Distance) {
222a521c4acSPuyan Lotfi Distance = Delta;
223a521c4acSPuyan Lotfi UseToBringDefCloserTo = UseInst;
2244d89462aSPuyan Lotfi MultiUserLookup[UseToBringDefCloserToCount++] = UseToBringDefCloserTo;
225a521c4acSPuyan Lotfi }
226a521c4acSPuyan Lotfi }
227a521c4acSPuyan Lotfi
228a521c4acSPuyan Lotfi const auto BBE = MBB->instr_end();
229a521c4acSPuyan Lotfi MachineBasicBlock::iterator DefI = BBE;
230a521c4acSPuyan Lotfi MachineBasicBlock::iterator UseI = BBE;
231a521c4acSPuyan Lotfi
232a521c4acSPuyan Lotfi for (auto BBI = MBB->instr_begin(); BBI != BBE; ++BBI) {
233a521c4acSPuyan Lotfi
234a521c4acSPuyan Lotfi if (DefI != BBE && UseI != BBE)
235a521c4acSPuyan Lotfi break;
236a521c4acSPuyan Lotfi
237a521c4acSPuyan Lotfi if (&*BBI == Def) {
238a521c4acSPuyan Lotfi DefI = BBI;
239a521c4acSPuyan Lotfi continue;
240a521c4acSPuyan Lotfi }
241a521c4acSPuyan Lotfi
242a521c4acSPuyan Lotfi if (&*BBI == UseToBringDefCloserTo) {
243a521c4acSPuyan Lotfi UseI = BBI;
244a521c4acSPuyan Lotfi continue;
245a521c4acSPuyan Lotfi }
246a521c4acSPuyan Lotfi }
247a521c4acSPuyan Lotfi
248a521c4acSPuyan Lotfi if (DefI == BBE || UseI == BBE)
249a521c4acSPuyan Lotfi continue;
250a521c4acSPuyan Lotfi
251d34e60caSNicola Zaghen LLVM_DEBUG({
252a521c4acSPuyan Lotfi dbgs() << "Splicing ";
253a521c4acSPuyan Lotfi DefI->dump();
254a521c4acSPuyan Lotfi dbgs() << " right before: ";
255a521c4acSPuyan Lotfi UseI->dump();
256a521c4acSPuyan Lotfi });
257a521c4acSPuyan Lotfi
25826c504feSPuyan Lotfi MultiUsers[UseToBringDefCloserTo].push_back(Def);
259a521c4acSPuyan Lotfi Changed = true;
260a521c4acSPuyan Lotfi MBB->splice(UseI, MBB, DefI);
261a521c4acSPuyan Lotfi }
262a521c4acSPuyan Lotfi
26326c504feSPuyan Lotfi // Sort the defs for users of multiple defs lexographically.
2644d89462aSPuyan Lotfi for (const auto &E : MultiUserLookup) {
26526c504feSPuyan Lotfi
2669850d3b1SKazu Hirata auto UseI = llvm::find_if(MBB->instrs(), [&](MachineInstr &MI) -> bool {
2679850d3b1SKazu Hirata return &MI == E.second;
2689850d3b1SKazu Hirata });
26926c504feSPuyan Lotfi
27026c504feSPuyan Lotfi if (UseI == MBB->instr_end())
27126c504feSPuyan Lotfi continue;
27226c504feSPuyan Lotfi
273d34e60caSNicola Zaghen LLVM_DEBUG(
274d34e60caSNicola Zaghen dbgs() << "Rescheduling Multi-Use Instructions Lexographically.";);
27526c504feSPuyan Lotfi Changed |= rescheduleLexographically(
2764d89462aSPuyan Lotfi MultiUsers[E.second], MBB,
2774d89462aSPuyan Lotfi [&]() -> MachineBasicBlock::iterator { return UseI; });
27826c504feSPuyan Lotfi }
27926c504feSPuyan Lotfi
28057c4f38cSPuyan Lotfi PseudoIdempotentInstCount = PseudoIdempotentInstructions.size();
281d34e60caSNicola Zaghen LLVM_DEBUG(
282d34e60caSNicola Zaghen dbgs() << "Rescheduling Idempotent Instructions Lexographically.";);
28357c4f38cSPuyan Lotfi Changed |= rescheduleLexographically(
28457c4f38cSPuyan Lotfi PseudoIdempotentInstructions, MBB,
28557c4f38cSPuyan Lotfi [&]() -> MachineBasicBlock::iterator { return MBB->begin(); });
28657c4f38cSPuyan Lotfi
287a521c4acSPuyan Lotfi return Changed;
288a521c4acSPuyan Lotfi }
289a521c4acSPuyan Lotfi
propagateLocalCopies(MachineBasicBlock * MBB)290651d0bf9SBenjamin Kramer static bool propagateLocalCopies(MachineBasicBlock *MBB) {
29114b6637eSPuyan Lotfi bool Changed = false;
29214b6637eSPuyan Lotfi MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
29314b6637eSPuyan Lotfi
29414b6637eSPuyan Lotfi std::vector<MachineInstr *> Copies;
29514b6637eSPuyan Lotfi for (MachineInstr &MI : MBB->instrs()) {
29614b6637eSPuyan Lotfi if (MI.isCopy())
29714b6637eSPuyan Lotfi Copies.push_back(&MI);
29814b6637eSPuyan Lotfi }
29914b6637eSPuyan Lotfi
30014b6637eSPuyan Lotfi for (MachineInstr *MI : Copies) {
30114b6637eSPuyan Lotfi
30214b6637eSPuyan Lotfi if (!MI->getOperand(0).isReg())
30314b6637eSPuyan Lotfi continue;
30414b6637eSPuyan Lotfi if (!MI->getOperand(1).isReg())
30514b6637eSPuyan Lotfi continue;
30614b6637eSPuyan Lotfi
3070c476111SDaniel Sanders const Register Dst = MI->getOperand(0).getReg();
3080c476111SDaniel Sanders const Register Src = MI->getOperand(1).getReg();
30914b6637eSPuyan Lotfi
3102bea69bfSDaniel Sanders if (!Register::isVirtualRegister(Dst))
31114b6637eSPuyan Lotfi continue;
3122bea69bfSDaniel Sanders if (!Register::isVirtualRegister(Src))
31314b6637eSPuyan Lotfi continue;
3142a901401SPuyan Lotfi // Not folding COPY instructions if regbankselect has not set the RCs.
3152a901401SPuyan Lotfi // Why are we only considering Register Classes? Because the verifier
3162a901401SPuyan Lotfi // sometimes gets upset if the register classes don't match even if the
3172a901401SPuyan Lotfi // types do. A future patch might add COPY folding for matching types in
3182a901401SPuyan Lotfi // pre-registerbankselect code.
3192a901401SPuyan Lotfi if (!MRI.getRegClassOrNull(Dst))
3202a901401SPuyan Lotfi continue;
32114b6637eSPuyan Lotfi if (MRI.getRegClass(Dst) != MRI.getRegClass(Src))
32214b6637eSPuyan Lotfi continue;
32314b6637eSPuyan Lotfi
3242a901401SPuyan Lotfi std::vector<MachineOperand *> Uses;
3252ca45adfSKazu Hirata for (MachineOperand &MO : MRI.use_operands(Dst))
3262ca45adfSKazu Hirata Uses.push_back(&MO);
3272a901401SPuyan Lotfi for (auto *MO : Uses)
32814b6637eSPuyan Lotfi MO->setReg(Src);
32914b6637eSPuyan Lotfi
3302a901401SPuyan Lotfi Changed = true;
33114b6637eSPuyan Lotfi MI->eraseFromParent();
33214b6637eSPuyan Lotfi }
33314b6637eSPuyan Lotfi
33414b6637eSPuyan Lotfi return Changed;
33514b6637eSPuyan Lotfi }
33614b6637eSPuyan Lotfi
doDefKillClear(MachineBasicBlock * MBB)337a521c4acSPuyan Lotfi static bool doDefKillClear(MachineBasicBlock *MBB) {
338a521c4acSPuyan Lotfi bool Changed = false;
339a521c4acSPuyan Lotfi
340a521c4acSPuyan Lotfi for (auto &MI : *MBB) {
341a521c4acSPuyan Lotfi for (auto &MO : MI.operands()) {
342a521c4acSPuyan Lotfi if (!MO.isReg())
343a521c4acSPuyan Lotfi continue;
344a521c4acSPuyan Lotfi if (!MO.isDef() && MO.isKill()) {
345a521c4acSPuyan Lotfi Changed = true;
346a521c4acSPuyan Lotfi MO.setIsKill(false);
347a521c4acSPuyan Lotfi }
348a521c4acSPuyan Lotfi
349a521c4acSPuyan Lotfi if (MO.isDef() && MO.isDead()) {
350a521c4acSPuyan Lotfi Changed = true;
351a521c4acSPuyan Lotfi MO.setIsDead(false);
352a521c4acSPuyan Lotfi }
353a521c4acSPuyan Lotfi }
354a521c4acSPuyan Lotfi }
355a521c4acSPuyan Lotfi
356a521c4acSPuyan Lotfi return Changed;
357a521c4acSPuyan Lotfi }
358a521c4acSPuyan Lotfi
runOnBasicBlock(MachineBasicBlock * MBB,unsigned BasicBlockNum,VRegRenamer & Renamer)359a521c4acSPuyan Lotfi static bool runOnBasicBlock(MachineBasicBlock *MBB,
360fdc6f4b9SPuyan Lotfi unsigned BasicBlockNum, VRegRenamer &Renamer) {
361d34e60caSNicola Zaghen LLVM_DEBUG({
362a521c4acSPuyan Lotfi dbgs() << "\n\n NEW BASIC BLOCK: " << MBB->getName() << " \n\n";
363a521c4acSPuyan Lotfi dbgs() << "\n\n================================================\n\n";
364a521c4acSPuyan Lotfi });
365a521c4acSPuyan Lotfi
366a521c4acSPuyan Lotfi bool Changed = false;
367a521c4acSPuyan Lotfi
368d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\n\n NEW BASIC BLOCK: " << MBB->getName() << "\n\n";);
369a521c4acSPuyan Lotfi
370d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "MBB Before Canonical Copy Propagation:\n";
371d34e60caSNicola Zaghen MBB->dump(););
37214b6637eSPuyan Lotfi Changed |= propagateLocalCopies(MBB);
373d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "MBB After Canonical Copy Propagation:\n"; MBB->dump(););
37414b6637eSPuyan Lotfi
375d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "MBB Before Scheduling:\n"; MBB->dump(););
37657c4f38cSPuyan Lotfi unsigned IdempotentInstCount = 0;
37757c4f38cSPuyan Lotfi Changed |= rescheduleCanonically(IdempotentInstCount, MBB);
378d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "MBB After Scheduling:\n"; MBB->dump(););
379a521c4acSPuyan Lotfi
380fdc6f4b9SPuyan Lotfi Changed |= Renamer.renameVRegs(MBB, BasicBlockNum);
38157c4f38cSPuyan Lotfi
382fdc6f4b9SPuyan Lotfi // TODO: Consider dropping this. Dropping kill defs is probably not
383fdc6f4b9SPuyan Lotfi // semantically sound.
384a521c4acSPuyan Lotfi Changed |= doDefKillClear(MBB);
385a521c4acSPuyan Lotfi
386d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Updated MachineBasicBlock:\n"; MBB->dump();
387d34e60caSNicola Zaghen dbgs() << "\n";);
388d34e60caSNicola Zaghen LLVM_DEBUG(
389d34e60caSNicola Zaghen dbgs() << "\n\n================================================\n\n");
390a521c4acSPuyan Lotfi return Changed;
391a521c4acSPuyan Lotfi }
392a521c4acSPuyan Lotfi
runOnMachineFunction(MachineFunction & MF)393a521c4acSPuyan Lotfi bool MIRCanonicalizer::runOnMachineFunction(MachineFunction &MF) {
394a521c4acSPuyan Lotfi
395a521c4acSPuyan Lotfi static unsigned functionNum = 0;
396a521c4acSPuyan Lotfi if (CanonicalizeFunctionNumber != ~0U) {
397a521c4acSPuyan Lotfi if (CanonicalizeFunctionNumber != functionNum++)
398a521c4acSPuyan Lotfi return false;
399d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\n Canonicalizing Function " << MF.getName()
400d34e60caSNicola Zaghen << "\n";);
401a521c4acSPuyan Lotfi }
402a521c4acSPuyan Lotfi
403a521c4acSPuyan Lotfi // we need a valid vreg to create a vreg type for skipping all those
404a521c4acSPuyan Lotfi // stray vreg numbers so reach alignment/canonical vreg values.
405a521c4acSPuyan Lotfi std::vector<MachineBasicBlock *> RPOList = GetRPOList(MF);
406a521c4acSPuyan Lotfi
407d34e60caSNicola Zaghen LLVM_DEBUG(
408d34e60caSNicola Zaghen dbgs() << "\n\n NEW MACHINE FUNCTION: " << MF.getName() << " \n\n";
409a521c4acSPuyan Lotfi dbgs() << "\n\n================================================\n\n";
410a521c4acSPuyan Lotfi dbgs() << "Total Basic Blocks: " << RPOList.size() << "\n";
4116ea89b40SPuyan Lotfi for (auto MBB
4126ea89b40SPuyan Lotfi : RPOList) { dbgs() << MBB->getName() << "\n"; } dbgs()
4136ea89b40SPuyan Lotfi << "\n\n================================================\n\n";);
414a521c4acSPuyan Lotfi
415a521c4acSPuyan Lotfi unsigned BBNum = 0;
416a521c4acSPuyan Lotfi bool Changed = false;
417d6f7313cSPuyan Lotfi MachineRegisterInfo &MRI = MF.getRegInfo();
41872768685SAditya Nandakumar VRegRenamer Renamer(MRI);
419*9e6d1f4bSKazu Hirata for (auto *MBB : RPOList)
420fdc6f4b9SPuyan Lotfi Changed |= runOnBasicBlock(MBB, BBNum++, Renamer);
421a521c4acSPuyan Lotfi
422a521c4acSPuyan Lotfi return Changed;
423a521c4acSPuyan Lotfi }
424