Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 1 | //===-------------- MIRCanonicalizer.cpp - MIR Canonicalizer --------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 6 | // |
| 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 Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 29 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 30 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/Passes.h" |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 33 | |
| 34 | #include <queue> |
| 35 | |
| 36 | using namespace llvm; |
| 37 | |
| 38 | namespace llvm { |
| 39 | extern char &MIRCanonicalizerID; |
| 40 | } // namespace llvm |
| 41 | |
| 42 | #define DEBUG_TYPE "mir-canonicalizer" |
| 43 | |
| 44 | static cl::opt<unsigned> |
Puyan Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 45 | CanonicalizeFunctionNumber("canon-nth-function", cl::Hidden, cl::init(~0u), |
| 46 | cl::value_desc("N"), |
| 47 | cl::desc("Function number to canonicalize.")); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 48 | |
Puyan Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 49 | static 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 Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 52 | |
| 53 | namespace { |
| 54 | |
| 55 | class MIRCanonicalizer : public MachineFunctionPass { |
| 56 | public: |
| 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 | |
| 74 | enum VRType { RSE_Reg = 0, RSE_FrameIndex, RSE_NewCandidate }; |
| 75 | class TypedVReg { |
| 76 | VRType type; |
| 77 | unsigned reg; |
| 78 | |
| 79 | public: |
| 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 Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 85 | bool isReg() const { return type == RSE_Reg; } |
| 86 | bool isFrameIndex() const { return type == RSE_FrameIndex; } |
| 87 | bool isCandidate() const { return type == RSE_NewCandidate; } |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 88 | |
| 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 | |
| 96 | char MIRCanonicalizer::ID; |
| 97 | |
| 98 | char &llvm::MIRCanonicalizerID = MIRCanonicalizer::ID; |
| 99 | |
| 100 | INITIALIZE_PASS_BEGIN(MIRCanonicalizer, "mir-canonicalizer", |
Craig Topper | 666e23b | 2017-11-03 18:02:46 +0000 | [diff] [blame] | 101 | "Rename Register Operands Canonically", false, false) |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 102 | |
| 103 | INITIALIZE_PASS_END(MIRCanonicalizer, "mir-canonicalizer", |
Craig Topper | 666e23b | 2017-11-03 18:02:46 +0000 | [diff] [blame] | 104 | "Rename Register Operands Canonically", false, false) |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 105 | |
| 106 | static std::vector<MachineBasicBlock *> GetRPOList(MachineFunction &MF) { |
Puyan Lotfi | daaecf9 | 2019-05-30 21:37:25 +0000 | [diff] [blame] | 107 | if (MF.empty()) |
| 108 | return {}; |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 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 | |
Puyan Lotfi | 57c4f38 | 2018-03-31 05:48:51 +0000 | [diff] [blame] | 118 | static bool |
| 119 | rescheduleLexographically(std::vector<MachineInstr *> instructions, |
| 120 | MachineBasicBlock *MBB, |
| 121 | std::function<MachineBasicBlock::iterator()> getPos) { |
| 122 | |
| 123 | bool Changed = false; |
Puyan Lotfi | 380a6f5 | 2018-05-13 06:07:20 +0000 | [diff] [blame] | 124 | using StringInstrPair = std::pair<std::string, MachineInstr *>; |
| 125 | std::vector<StringInstrPair> StringInstrMap; |
Puyan Lotfi | 57c4f38 | 2018-03-31 05:48:51 +0000 | [diff] [blame] | 126 | |
| 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 Lotfi | 380a6f5 | 2018-05-13 06:07:20 +0000 | [diff] [blame] | 135 | StringInstrMap.push_back({(i == std::string::npos) ? S : S.substr(i), II}); |
Puyan Lotfi | 57c4f38 | 2018-03-31 05:48:51 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 138 | llvm::sort(StringInstrMap, |
| 139 | [](const StringInstrPair &a, const StringInstrPair &b) -> bool { |
| 140 | return (a.first < b.first); |
| 141 | }); |
Puyan Lotfi | 380a6f5 | 2018-05-13 06:07:20 +0000 | [diff] [blame] | 142 | |
Puyan Lotfi | 57c4f38 | 2018-03-31 05:48:51 +0000 | [diff] [blame] | 143 | for (auto &II : StringInstrMap) { |
| 144 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 145 | LLVM_DEBUG({ |
Puyan Lotfi | 57c4f38 | 2018-03-31 05:48:51 +0000 | [diff] [blame] | 146 | 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 | |
| 159 | static bool rescheduleCanonically(unsigned &PseudoIdempotentInstCount, |
| 160 | MachineBasicBlock *MBB) { |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 161 | |
| 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 Lotfi | 26c504f | 2018-04-05 00:08:15 +0000 | [diff] [blame] | 182 | std::map<MachineInstr *, std::vector<MachineInstr *>> MultiUsers; |
Puyan Lotfi | 4d89462 | 2019-06-11 00:00:25 +0000 | [diff] [blame] | 183 | std::map<unsigned, MachineInstr *> MultiUserLookup; |
| 184 | unsigned UseToBringDefCloserToCount = 0; |
Puyan Lotfi | 57c4f38 | 2018-03-31 05:48:51 +0000 | [diff] [blame] | 185 | std::vector<MachineInstr *> PseudoIdempotentInstructions; |
| 186 | std::vector<unsigned> PhysRegDefs; |
| 187 | for (auto *II : Instructions) { |
| 188 | for (unsigned i = 1; i < II->getNumOperands(); i++) { |
| 189 | MachineOperand &MO = II->getOperand(i); |
| 190 | if (!MO.isReg()) |
| 191 | continue; |
| 192 | |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 193 | if (Register::isVirtualRegister(MO.getReg())) |
Puyan Lotfi | 57c4f38 | 2018-03-31 05:48:51 +0000 | [diff] [blame] | 194 | continue; |
| 195 | |
| 196 | if (!MO.isDef()) |
| 197 | continue; |
| 198 | |
| 199 | PhysRegDefs.push_back(MO.getReg()); |
| 200 | } |
| 201 | } |
| 202 | |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 203 | for (auto *II : Instructions) { |
| 204 | if (II->getNumOperands() == 0) |
| 205 | continue; |
Puyan Lotfi | 57c4f38 | 2018-03-31 05:48:51 +0000 | [diff] [blame] | 206 | if (II->mayLoadOrStore()) |
| 207 | continue; |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 208 | |
| 209 | MachineOperand &MO = II->getOperand(0); |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 210 | if (!MO.isReg() || !Register::isVirtualRegister(MO.getReg())) |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 211 | continue; |
Puyan Lotfi | 57c4f38 | 2018-03-31 05:48:51 +0000 | [diff] [blame] | 212 | if (!MO.isDef()) |
| 213 | continue; |
| 214 | |
| 215 | bool IsPseudoIdempotent = true; |
| 216 | for (unsigned i = 1; i < II->getNumOperands(); i++) { |
| 217 | |
| 218 | if (II->getOperand(i).isImm()) { |
| 219 | continue; |
| 220 | } |
| 221 | |
| 222 | if (II->getOperand(i).isReg()) { |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 223 | if (!Register::isVirtualRegister(II->getOperand(i).getReg())) |
Puyan Lotfi | 57c4f38 | 2018-03-31 05:48:51 +0000 | [diff] [blame] | 224 | if (llvm::find(PhysRegDefs, II->getOperand(i).getReg()) == |
Puyan Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 225 | PhysRegDefs.end()) { |
Puyan Lotfi | 57c4f38 | 2018-03-31 05:48:51 +0000 | [diff] [blame] | 226 | continue; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | IsPseudoIdempotent = false; |
| 231 | break; |
| 232 | } |
| 233 | |
| 234 | if (IsPseudoIdempotent) { |
| 235 | PseudoIdempotentInstructions.push_back(II); |
| 236 | continue; |
| 237 | } |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 238 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 239 | LLVM_DEBUG(dbgs() << "Operand " << 0 << " of "; II->dump(); MO.dump();); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 240 | |
| 241 | MachineInstr *Def = II; |
| 242 | unsigned Distance = ~0U; |
| 243 | MachineInstr *UseToBringDefCloserTo = nullptr; |
| 244 | MachineRegisterInfo *MRI = &MBB->getParent()->getRegInfo(); |
| 245 | for (auto &UO : MRI->use_nodbg_operands(MO.getReg())) { |
| 246 | MachineInstr *UseInst = UO.getParent(); |
| 247 | |
| 248 | const unsigned DefLoc = getInstrIdx(*Def); |
| 249 | const unsigned UseLoc = getInstrIdx(*UseInst); |
| 250 | const unsigned Delta = (UseLoc - DefLoc); |
| 251 | |
| 252 | if (UseInst->getParent() != Def->getParent()) |
| 253 | continue; |
| 254 | if (DefLoc >= UseLoc) |
| 255 | continue; |
| 256 | |
| 257 | if (Delta < Distance) { |
| 258 | Distance = Delta; |
| 259 | UseToBringDefCloserTo = UseInst; |
Puyan Lotfi | 4d89462 | 2019-06-11 00:00:25 +0000 | [diff] [blame] | 260 | MultiUserLookup[UseToBringDefCloserToCount++] = UseToBringDefCloserTo; |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | const auto BBE = MBB->instr_end(); |
| 265 | MachineBasicBlock::iterator DefI = BBE; |
| 266 | MachineBasicBlock::iterator UseI = BBE; |
| 267 | |
| 268 | for (auto BBI = MBB->instr_begin(); BBI != BBE; ++BBI) { |
| 269 | |
| 270 | if (DefI != BBE && UseI != BBE) |
| 271 | break; |
| 272 | |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 273 | if (&*BBI == Def) { |
| 274 | DefI = BBI; |
| 275 | continue; |
| 276 | } |
| 277 | |
| 278 | if (&*BBI == UseToBringDefCloserTo) { |
| 279 | UseI = BBI; |
| 280 | continue; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | if (DefI == BBE || UseI == BBE) |
| 285 | continue; |
| 286 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 287 | LLVM_DEBUG({ |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 288 | dbgs() << "Splicing "; |
| 289 | DefI->dump(); |
| 290 | dbgs() << " right before: "; |
| 291 | UseI->dump(); |
| 292 | }); |
| 293 | |
Puyan Lotfi | 26c504f | 2018-04-05 00:08:15 +0000 | [diff] [blame] | 294 | MultiUsers[UseToBringDefCloserTo].push_back(Def); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 295 | Changed = true; |
| 296 | MBB->splice(UseI, MBB, DefI); |
| 297 | } |
| 298 | |
Puyan Lotfi | 26c504f | 2018-04-05 00:08:15 +0000 | [diff] [blame] | 299 | // Sort the defs for users of multiple defs lexographically. |
Puyan Lotfi | 4d89462 | 2019-06-11 00:00:25 +0000 | [diff] [blame] | 300 | for (const auto &E : MultiUserLookup) { |
Puyan Lotfi | 26c504f | 2018-04-05 00:08:15 +0000 | [diff] [blame] | 301 | |
| 302 | auto UseI = |
| 303 | std::find_if(MBB->instr_begin(), MBB->instr_end(), |
Puyan Lotfi | 4d89462 | 2019-06-11 00:00:25 +0000 | [diff] [blame] | 304 | [&](MachineInstr &MI) -> bool { return &MI == E.second; }); |
Puyan Lotfi | 26c504f | 2018-04-05 00:08:15 +0000 | [diff] [blame] | 305 | |
| 306 | if (UseI == MBB->instr_end()) |
| 307 | continue; |
| 308 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 309 | LLVM_DEBUG( |
| 310 | dbgs() << "Rescheduling Multi-Use Instructions Lexographically.";); |
Puyan Lotfi | 26c504f | 2018-04-05 00:08:15 +0000 | [diff] [blame] | 311 | Changed |= rescheduleLexographically( |
Puyan Lotfi | 4d89462 | 2019-06-11 00:00:25 +0000 | [diff] [blame] | 312 | MultiUsers[E.second], MBB, |
| 313 | [&]() -> MachineBasicBlock::iterator { return UseI; }); |
Puyan Lotfi | 26c504f | 2018-04-05 00:08:15 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Puyan Lotfi | 57c4f38 | 2018-03-31 05:48:51 +0000 | [diff] [blame] | 316 | PseudoIdempotentInstCount = PseudoIdempotentInstructions.size(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 317 | LLVM_DEBUG( |
| 318 | dbgs() << "Rescheduling Idempotent Instructions Lexographically.";); |
Puyan Lotfi | 57c4f38 | 2018-03-31 05:48:51 +0000 | [diff] [blame] | 319 | Changed |= rescheduleLexographically( |
| 320 | PseudoIdempotentInstructions, MBB, |
| 321 | [&]() -> MachineBasicBlock::iterator { return MBB->begin(); }); |
| 322 | |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 323 | return Changed; |
| 324 | } |
| 325 | |
Benjamin Kramer | 651d0bf | 2018-05-15 21:26:47 +0000 | [diff] [blame] | 326 | static bool propagateLocalCopies(MachineBasicBlock *MBB) { |
Puyan Lotfi | 14b6637e | 2018-04-16 09:03:03 +0000 | [diff] [blame] | 327 | bool Changed = false; |
| 328 | MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); |
| 329 | |
| 330 | std::vector<MachineInstr *> Copies; |
| 331 | for (MachineInstr &MI : MBB->instrs()) { |
| 332 | if (MI.isCopy()) |
| 333 | Copies.push_back(&MI); |
| 334 | } |
| 335 | |
| 336 | for (MachineInstr *MI : Copies) { |
| 337 | |
| 338 | if (!MI->getOperand(0).isReg()) |
| 339 | continue; |
| 340 | if (!MI->getOperand(1).isReg()) |
| 341 | continue; |
| 342 | |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame^] | 343 | const Register Dst = MI->getOperand(0).getReg(); |
| 344 | const Register Src = MI->getOperand(1).getReg(); |
Puyan Lotfi | 14b6637e | 2018-04-16 09:03:03 +0000 | [diff] [blame] | 345 | |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 346 | if (!Register::isVirtualRegister(Dst)) |
Puyan Lotfi | 14b6637e | 2018-04-16 09:03:03 +0000 | [diff] [blame] | 347 | continue; |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 348 | if (!Register::isVirtualRegister(Src)) |
Puyan Lotfi | 14b6637e | 2018-04-16 09:03:03 +0000 | [diff] [blame] | 349 | continue; |
Puyan Lotfi | 2a90140 | 2019-05-31 04:49:58 +0000 | [diff] [blame] | 350 | // Not folding COPY instructions if regbankselect has not set the RCs. |
| 351 | // Why are we only considering Register Classes? Because the verifier |
| 352 | // sometimes gets upset if the register classes don't match even if the |
| 353 | // types do. A future patch might add COPY folding for matching types in |
| 354 | // pre-registerbankselect code. |
| 355 | if (!MRI.getRegClassOrNull(Dst)) |
| 356 | continue; |
Puyan Lotfi | 14b6637e | 2018-04-16 09:03:03 +0000 | [diff] [blame] | 357 | if (MRI.getRegClass(Dst) != MRI.getRegClass(Src)) |
| 358 | continue; |
| 359 | |
Puyan Lotfi | 2a90140 | 2019-05-31 04:49:58 +0000 | [diff] [blame] | 360 | std::vector<MachineOperand *> Uses; |
| 361 | for (auto UI = MRI.use_begin(Dst); UI != MRI.use_end(); ++UI) |
| 362 | Uses.push_back(&*UI); |
| 363 | for (auto *MO : Uses) |
Puyan Lotfi | 14b6637e | 2018-04-16 09:03:03 +0000 | [diff] [blame] | 364 | MO->setReg(Src); |
Puyan Lotfi | 14b6637e | 2018-04-16 09:03:03 +0000 | [diff] [blame] | 365 | |
Puyan Lotfi | 2a90140 | 2019-05-31 04:49:58 +0000 | [diff] [blame] | 366 | Changed = true; |
Puyan Lotfi | 14b6637e | 2018-04-16 09:03:03 +0000 | [diff] [blame] | 367 | MI->eraseFromParent(); |
| 368 | } |
| 369 | |
| 370 | return Changed; |
| 371 | } |
| 372 | |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 373 | /// Here we find our candidates. What makes an interesting candidate? |
| 374 | /// An candidate for a canonicalization tree root is normally any kind of |
| 375 | /// instruction that causes side effects such as a store to memory or a copy to |
| 376 | /// a physical register or a return instruction. We use these as an expression |
| 377 | /// tree root that we walk inorder to build a canonical walk which should result |
| 378 | /// in canoncal vreg renaming. |
| 379 | static std::vector<MachineInstr *> populateCandidates(MachineBasicBlock *MBB) { |
| 380 | std::vector<MachineInstr *> Candidates; |
| 381 | MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); |
| 382 | |
| 383 | for (auto II = MBB->begin(), IE = MBB->end(); II != IE; ++II) { |
| 384 | MachineInstr *MI = &*II; |
| 385 | |
| 386 | bool DoesMISideEffect = false; |
| 387 | |
| 388 | if (MI->getNumOperands() > 0 && MI->getOperand(0).isReg()) { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame^] | 389 | const Register Dst = MI->getOperand(0).getReg(); |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 390 | DoesMISideEffect |= !Register::isVirtualRegister(Dst); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 391 | |
| 392 | for (auto UI = MRI.use_begin(Dst); UI != MRI.use_end(); ++UI) { |
Puyan Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 393 | if (DoesMISideEffect) |
| 394 | break; |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 395 | DoesMISideEffect |= (UI->getParent()->getParent() != MI->getParent()); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | if (!MI->mayStore() && !MI->isBranch() && !DoesMISideEffect) |
| 400 | continue; |
| 401 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 402 | LLVM_DEBUG(dbgs() << "Found Candidate: "; MI->dump();); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 403 | Candidates.push_back(MI); |
| 404 | } |
| 405 | |
| 406 | return Candidates; |
| 407 | } |
| 408 | |
Benjamin Kramer | 51ebcaa | 2017-11-24 14:55:41 +0000 | [diff] [blame] | 409 | static void doCandidateWalk(std::vector<TypedVReg> &VRegs, |
| 410 | std::queue<TypedVReg> &RegQueue, |
| 411 | std::vector<MachineInstr *> &VisitedMIs, |
| 412 | const MachineBasicBlock *MBB) { |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 413 | |
| 414 | const MachineFunction &MF = *MBB->getParent(); |
| 415 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 416 | |
| 417 | while (!RegQueue.empty()) { |
| 418 | |
| 419 | auto TReg = RegQueue.front(); |
| 420 | RegQueue.pop(); |
| 421 | |
| 422 | if (TReg.isFrameIndex()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 423 | LLVM_DEBUG(dbgs() << "Popping frame index.\n";); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 424 | VRegs.push_back(TypedVReg(RSE_FrameIndex)); |
| 425 | continue; |
| 426 | } |
| 427 | |
| 428 | assert(TReg.isReg() && "Expected vreg or physreg."); |
| 429 | unsigned Reg = TReg.getReg(); |
| 430 | |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 431 | if (Register::isVirtualRegister(Reg)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 432 | LLVM_DEBUG({ |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 433 | dbgs() << "Popping vreg "; |
| 434 | MRI.def_begin(Reg)->dump(); |
| 435 | dbgs() << "\n"; |
| 436 | }); |
| 437 | |
| 438 | if (!llvm::any_of(VRegs, [&](const TypedVReg &TR) { |
| 439 | return TR.isReg() && TR.getReg() == Reg; |
| 440 | })) { |
| 441 | VRegs.push_back(TypedVReg(Reg)); |
| 442 | } |
| 443 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 444 | LLVM_DEBUG(dbgs() << "Popping physreg.\n";); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 445 | VRegs.push_back(TypedVReg(Reg)); |
| 446 | continue; |
| 447 | } |
| 448 | |
| 449 | for (auto RI = MRI.def_begin(Reg), RE = MRI.def_end(); RI != RE; ++RI) { |
| 450 | MachineInstr *Def = RI->getParent(); |
| 451 | |
| 452 | if (Def->getParent() != MBB) |
| 453 | continue; |
| 454 | |
| 455 | if (llvm::any_of(VisitedMIs, |
| 456 | [&](const MachineInstr *VMI) { return Def == VMI; })) { |
| 457 | break; |
| 458 | } |
| 459 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 460 | LLVM_DEBUG({ |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 461 | dbgs() << "\n========================\n"; |
| 462 | dbgs() << "Visited MI: "; |
| 463 | Def->dump(); |
| 464 | dbgs() << "BB Name: " << Def->getParent()->getName() << "\n"; |
| 465 | dbgs() << "\n========================\n"; |
| 466 | }); |
| 467 | VisitedMIs.push_back(Def); |
| 468 | for (unsigned I = 1, E = Def->getNumOperands(); I != E; ++I) { |
| 469 | |
| 470 | MachineOperand &MO = Def->getOperand(I); |
| 471 | if (MO.isFI()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 472 | LLVM_DEBUG(dbgs() << "Pushing frame index.\n";); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 473 | RegQueue.push(TypedVReg(RSE_FrameIndex)); |
| 474 | } |
| 475 | |
| 476 | if (!MO.isReg()) |
| 477 | continue; |
| 478 | RegQueue.push(TypedVReg(MO.getReg())); |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
Benjamin Kramer | 651d0bf | 2018-05-15 21:26:47 +0000 | [diff] [blame] | 484 | namespace { |
Puyan Lotfi | d6f7313 | 2018-04-05 00:27:15 +0000 | [diff] [blame] | 485 | class NamedVRegCursor { |
Puyan Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 486 | MachineRegisterInfo &MRI; |
| 487 | unsigned virtualVRegNumber; |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 488 | |
Puyan Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 489 | public: |
Puyan Lotfi | 0d63cef | 2019-05-31 06:02:38 +0000 | [diff] [blame] | 490 | NamedVRegCursor(MachineRegisterInfo &MRI) : MRI(MRI), virtualVRegNumber(0) {} |
Puyan Lotfi | d6f7313 | 2018-04-05 00:27:15 +0000 | [diff] [blame] | 491 | |
Puyan Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 492 | void SkipVRegs() { |
| 493 | unsigned VRegGapIndex = 1; |
Puyan Lotfi | 0d63cef | 2019-05-31 06:02:38 +0000 | [diff] [blame] | 494 | if (!virtualVRegNumber) { |
| 495 | VRegGapIndex = 0; |
| 496 | virtualVRegNumber = MRI.createIncompleteVirtualRegister(); |
| 497 | } |
Puyan Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 498 | const unsigned VR_GAP = (++VRegGapIndex * 1000); |
Puyan Lotfi | d6f7313 | 2018-04-05 00:27:15 +0000 | [diff] [blame] | 499 | |
Puyan Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 500 | unsigned I = virtualVRegNumber; |
| 501 | const unsigned E = (((I + VR_GAP) / VR_GAP) + 1) * VR_GAP; |
Puyan Lotfi | d6f7313 | 2018-04-05 00:27:15 +0000 | [diff] [blame] | 502 | |
Puyan Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 503 | virtualVRegNumber = E; |
| 504 | } |
Puyan Lotfi | d6f7313 | 2018-04-05 00:27:15 +0000 | [diff] [blame] | 505 | |
Puyan Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 506 | unsigned getVirtualVReg() const { return virtualVRegNumber; } |
Puyan Lotfi | d6f7313 | 2018-04-05 00:27:15 +0000 | [diff] [blame] | 507 | |
Puyan Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 508 | unsigned incrementVirtualVReg(unsigned incr = 1) { |
| 509 | virtualVRegNumber += incr; |
| 510 | return virtualVRegNumber; |
| 511 | } |
Puyan Lotfi | d6f7313 | 2018-04-05 00:27:15 +0000 | [diff] [blame] | 512 | |
Puyan Lotfi | 0f4446b | 2019-05-30 18:06:28 +0000 | [diff] [blame] | 513 | unsigned createVirtualRegister(unsigned VReg) { |
Puyan Lotfi | 0d63cef | 2019-05-31 06:02:38 +0000 | [diff] [blame] | 514 | if (!virtualVRegNumber) |
| 515 | SkipVRegs(); |
Puyan Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 516 | std::string S; |
| 517 | raw_string_ostream OS(S); |
| 518 | OS << "namedVReg" << (virtualVRegNumber & ~0x80000000); |
| 519 | OS.flush(); |
| 520 | virtualVRegNumber++; |
Puyan Lotfi | 0f4446b | 2019-05-30 18:06:28 +0000 | [diff] [blame] | 521 | if (auto RC = MRI.getRegClassOrNull(VReg)) |
| 522 | return MRI.createVirtualRegister(RC, OS.str()); |
| 523 | return MRI.createGenericVirtualRegister(MRI.getType(VReg), OS.str()); |
Puyan Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 524 | } |
Puyan Lotfi | d6f7313 | 2018-04-05 00:27:15 +0000 | [diff] [blame] | 525 | }; |
Benjamin Kramer | 651d0bf | 2018-05-15 21:26:47 +0000 | [diff] [blame] | 526 | } // namespace |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 527 | |
| 528 | static std::map<unsigned, unsigned> |
| 529 | GetVRegRenameMap(const std::vector<TypedVReg> &VRegs, |
| 530 | const std::vector<unsigned> &renamedInOtherBB, |
Puyan Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 531 | MachineRegisterInfo &MRI, NamedVRegCursor &NVC) { |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 532 | std::map<unsigned, unsigned> VRegRenameMap; |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 533 | bool FirstCandidate = true; |
| 534 | |
| 535 | for (auto &vreg : VRegs) { |
| 536 | if (vreg.isFrameIndex()) { |
| 537 | // We skip one vreg for any frame index because there is a good chance |
| 538 | // (especially when comparing SelectionDAG to GlobalISel generated MIR) |
| 539 | // that in the other file we are just getting an incoming vreg that comes |
| 540 | // from a copy from a frame index. So it's safe to skip by one. |
Puyan Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 541 | unsigned LastRenameReg = NVC.incrementVirtualVReg(); |
| 542 | (void)LastRenameReg; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 543 | LLVM_DEBUG(dbgs() << "Skipping rename for FI " << LastRenameReg << "\n";); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 544 | continue; |
| 545 | } else if (vreg.isCandidate()) { |
| 546 | |
| 547 | // After the first candidate, for every subsequent candidate, we skip mod |
| 548 | // 10 registers so that the candidates are more likely to start at the |
| 549 | // same vreg number making it more likely that the canonical walk from the |
| 550 | // candidate insruction. We don't need to skip from the first candidate of |
| 551 | // the BasicBlock because we already skip ahead several vregs for each BB. |
Puyan Lotfi | d6f7313 | 2018-04-05 00:27:15 +0000 | [diff] [blame] | 552 | unsigned LastRenameReg = NVC.getVirtualVReg(); |
| 553 | if (FirstCandidate) |
| 554 | NVC.incrementVirtualVReg(LastRenameReg % 10); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 555 | FirstCandidate = false; |
| 556 | continue; |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 557 | } else if (!Register::isVirtualRegister(vreg.getReg())) { |
Puyan Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 558 | unsigned LastRenameReg = NVC.incrementVirtualVReg(); |
| 559 | (void)LastRenameReg; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 560 | LLVM_DEBUG({ |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 561 | dbgs() << "Skipping rename for Phys Reg " << LastRenameReg << "\n"; |
| 562 | }); |
| 563 | continue; |
| 564 | } |
| 565 | |
| 566 | auto Reg = vreg.getReg(); |
| 567 | if (llvm::find(renamedInOtherBB, Reg) != renamedInOtherBB.end()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 568 | LLVM_DEBUG(dbgs() << "Vreg " << Reg |
| 569 | << " already renamed in other BB.\n";); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 570 | continue; |
| 571 | } |
| 572 | |
Puyan Lotfi | 0f4446b | 2019-05-30 18:06:28 +0000 | [diff] [blame] | 573 | auto Rename = NVC.createVirtualRegister(Reg); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 574 | |
| 575 | if (VRegRenameMap.find(Reg) == VRegRenameMap.end()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 576 | LLVM_DEBUG(dbgs() << "Mapping vreg ";); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 577 | if (MRI.reg_begin(Reg) != MRI.reg_end()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 578 | LLVM_DEBUG(auto foo = &*MRI.reg_begin(Reg); foo->dump();); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 579 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 580 | LLVM_DEBUG(dbgs() << Reg;); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 581 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 582 | LLVM_DEBUG(dbgs() << " to ";); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 583 | if (MRI.reg_begin(Rename) != MRI.reg_end()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 584 | LLVM_DEBUG(auto foo = &*MRI.reg_begin(Rename); foo->dump();); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 585 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 586 | LLVM_DEBUG(dbgs() << Rename;); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 587 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 588 | LLVM_DEBUG(dbgs() << "\n";); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 589 | |
| 590 | VRegRenameMap.insert(std::pair<unsigned, unsigned>(Reg, Rename)); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | return VRegRenameMap; |
| 595 | } |
| 596 | |
| 597 | static bool doVRegRenaming(std::vector<unsigned> &RenamedInOtherBB, |
| 598 | const std::map<unsigned, unsigned> &VRegRenameMap, |
| 599 | MachineRegisterInfo &MRI) { |
| 600 | bool Changed = false; |
| 601 | for (auto I = VRegRenameMap.begin(), E = VRegRenameMap.end(); I != E; ++I) { |
| 602 | |
| 603 | auto VReg = I->first; |
| 604 | auto Rename = I->second; |
| 605 | |
| 606 | RenamedInOtherBB.push_back(Rename); |
| 607 | |
| 608 | std::vector<MachineOperand *> RenameMOs; |
| 609 | for (auto &MO : MRI.reg_operands(VReg)) { |
| 610 | RenameMOs.push_back(&MO); |
| 611 | } |
| 612 | |
| 613 | for (auto *MO : RenameMOs) { |
| 614 | Changed = true; |
| 615 | MO->setReg(Rename); |
| 616 | |
| 617 | if (!MO->isDef()) |
| 618 | MO->setIsKill(false); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | return Changed; |
| 623 | } |
| 624 | |
| 625 | static bool doDefKillClear(MachineBasicBlock *MBB) { |
| 626 | bool Changed = false; |
| 627 | |
| 628 | for (auto &MI : *MBB) { |
| 629 | for (auto &MO : MI.operands()) { |
| 630 | if (!MO.isReg()) |
| 631 | continue; |
| 632 | if (!MO.isDef() && MO.isKill()) { |
| 633 | Changed = true; |
| 634 | MO.setIsKill(false); |
| 635 | } |
| 636 | |
| 637 | if (MO.isDef() && MO.isDead()) { |
| 638 | Changed = true; |
| 639 | MO.setIsDead(false); |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | return Changed; |
| 645 | } |
| 646 | |
| 647 | static bool runOnBasicBlock(MachineBasicBlock *MBB, |
| 648 | std::vector<StringRef> &bbNames, |
| 649 | std::vector<unsigned> &renamedInOtherBB, |
Puyan Lotfi | d6f7313 | 2018-04-05 00:27:15 +0000 | [diff] [blame] | 650 | unsigned &basicBlockNum, unsigned &VRegGapIndex, |
| 651 | NamedVRegCursor &NVC) { |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 652 | |
| 653 | if (CanonicalizeBasicBlockNumber != ~0U) { |
| 654 | if (CanonicalizeBasicBlockNumber != basicBlockNum++) |
| 655 | return false; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 656 | LLVM_DEBUG(dbgs() << "\n Canonicalizing BasicBlock " << MBB->getName() |
| 657 | << "\n";); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | if (llvm::find(bbNames, MBB->getName()) != bbNames.end()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 661 | LLVM_DEBUG({ |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 662 | dbgs() << "Found potentially duplicate BasicBlocks: " << MBB->getName() |
| 663 | << "\n"; |
| 664 | }); |
| 665 | return false; |
| 666 | } |
| 667 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 668 | LLVM_DEBUG({ |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 669 | dbgs() << "\n\n NEW BASIC BLOCK: " << MBB->getName() << " \n\n"; |
| 670 | dbgs() << "\n\n================================================\n\n"; |
| 671 | }); |
| 672 | |
| 673 | bool Changed = false; |
| 674 | MachineFunction &MF = *MBB->getParent(); |
| 675 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 676 | |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 677 | bbNames.push_back(MBB->getName()); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 678 | LLVM_DEBUG(dbgs() << "\n\n NEW BASIC BLOCK: " << MBB->getName() << "\n\n";); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 679 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 680 | LLVM_DEBUG(dbgs() << "MBB Before Canonical Copy Propagation:\n"; |
| 681 | MBB->dump();); |
Puyan Lotfi | 14b6637e | 2018-04-16 09:03:03 +0000 | [diff] [blame] | 682 | Changed |= propagateLocalCopies(MBB); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 683 | LLVM_DEBUG(dbgs() << "MBB After Canonical Copy Propagation:\n"; MBB->dump();); |
Puyan Lotfi | 14b6637e | 2018-04-16 09:03:03 +0000 | [diff] [blame] | 684 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 685 | LLVM_DEBUG(dbgs() << "MBB Before Scheduling:\n"; MBB->dump();); |
Puyan Lotfi | 57c4f38 | 2018-03-31 05:48:51 +0000 | [diff] [blame] | 686 | unsigned IdempotentInstCount = 0; |
| 687 | Changed |= rescheduleCanonically(IdempotentInstCount, MBB); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 688 | LLVM_DEBUG(dbgs() << "MBB After Scheduling:\n"; MBB->dump();); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 689 | |
| 690 | std::vector<MachineInstr *> Candidates = populateCandidates(MBB); |
| 691 | std::vector<MachineInstr *> VisitedMIs; |
Fangrui Song | 7570932 | 2018-11-17 01:44:25 +0000 | [diff] [blame] | 692 | llvm::copy(Candidates, std::back_inserter(VisitedMIs)); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 693 | |
| 694 | std::vector<TypedVReg> VRegs; |
| 695 | for (auto candidate : Candidates) { |
| 696 | VRegs.push_back(TypedVReg(RSE_NewCandidate)); |
| 697 | |
| 698 | std::queue<TypedVReg> RegQueue; |
| 699 | |
| 700 | // Here we walk the vreg operands of a non-root node along our walk. |
| 701 | // The root nodes are the original candidates (stores normally). |
| 702 | // These are normally not the root nodes (except for the case of copies to |
| 703 | // physical registers). |
| 704 | for (unsigned i = 1; i < candidate->getNumOperands(); i++) { |
| 705 | if (candidate->mayStore() || candidate->isBranch()) |
| 706 | break; |
| 707 | |
| 708 | MachineOperand &MO = candidate->getOperand(i); |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 709 | if (!(MO.isReg() && Register::isVirtualRegister(MO.getReg()))) |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 710 | continue; |
| 711 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 712 | LLVM_DEBUG(dbgs() << "Enqueue register"; MO.dump(); dbgs() << "\n";); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 713 | RegQueue.push(TypedVReg(MO.getReg())); |
| 714 | } |
| 715 | |
| 716 | // Here we walk the root candidates. We start from the 0th operand because |
| 717 | // the root is normally a store to a vreg. |
| 718 | for (unsigned i = 0; i < candidate->getNumOperands(); i++) { |
| 719 | |
| 720 | if (!candidate->mayStore() && !candidate->isBranch()) |
| 721 | break; |
| 722 | |
| 723 | MachineOperand &MO = candidate->getOperand(i); |
| 724 | |
| 725 | // TODO: Do we want to only add vregs here? |
| 726 | if (!MO.isReg() && !MO.isFI()) |
| 727 | continue; |
| 728 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 729 | LLVM_DEBUG(dbgs() << "Enqueue Reg/FI"; MO.dump(); dbgs() << "\n";); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 730 | |
Puyan Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 731 | RegQueue.push(MO.isReg() ? TypedVReg(MO.getReg()) |
| 732 | : TypedVReg(RSE_FrameIndex)); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | doCandidateWalk(VRegs, RegQueue, VisitedMIs, MBB); |
| 736 | } |
| 737 | |
| 738 | // If we have populated no vregs to rename then bail. |
| 739 | // The rest of this function does the vreg remaping. |
| 740 | if (VRegs.size() == 0) |
| 741 | return Changed; |
| 742 | |
Puyan Lotfi | d6f7313 | 2018-04-05 00:27:15 +0000 | [diff] [blame] | 743 | auto VRegRenameMap = GetVRegRenameMap(VRegs, renamedInOtherBB, MRI, NVC); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 744 | Changed |= doVRegRenaming(renamedInOtherBB, VRegRenameMap, MRI); |
Puyan Lotfi | 57c4f38 | 2018-03-31 05:48:51 +0000 | [diff] [blame] | 745 | |
| 746 | // Here we renumber the def vregs for the idempotent instructions from the top |
| 747 | // of the MachineBasicBlock so that they are named in the order that we sorted |
| 748 | // them alphabetically. Eventually we wont need SkipVRegs because we will use |
| 749 | // named vregs instead. |
Puyan Lotfi | 3ea6b24 | 2019-05-31 17:34:25 +0000 | [diff] [blame] | 750 | if (IdempotentInstCount) |
| 751 | NVC.SkipVRegs(); |
Puyan Lotfi | 57c4f38 | 2018-03-31 05:48:51 +0000 | [diff] [blame] | 752 | |
| 753 | auto MII = MBB->begin(); |
| 754 | for (unsigned i = 0; i < IdempotentInstCount && MII != MBB->end(); ++i) { |
| 755 | MachineInstr &MI = *MII++; |
| 756 | Changed = true; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame^] | 757 | Register vRegToRename = MI.getOperand(0).getReg(); |
Puyan Lotfi | 0f4446b | 2019-05-30 18:06:28 +0000 | [diff] [blame] | 758 | auto Rename = NVC.createVirtualRegister(vRegToRename); |
Puyan Lotfi | 57c4f38 | 2018-03-31 05:48:51 +0000 | [diff] [blame] | 759 | |
| 760 | std::vector<MachineOperand *> RenameMOs; |
| 761 | for (auto &MO : MRI.reg_operands(vRegToRename)) { |
| 762 | RenameMOs.push_back(&MO); |
| 763 | } |
| 764 | |
| 765 | for (auto *MO : RenameMOs) { |
| 766 | MO->setReg(Rename); |
| 767 | } |
| 768 | } |
| 769 | |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 770 | Changed |= doDefKillClear(MBB); |
| 771 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 772 | LLVM_DEBUG(dbgs() << "Updated MachineBasicBlock:\n"; MBB->dump(); |
| 773 | dbgs() << "\n";); |
| 774 | LLVM_DEBUG( |
| 775 | dbgs() << "\n\n================================================\n\n"); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 776 | return Changed; |
| 777 | } |
| 778 | |
| 779 | bool MIRCanonicalizer::runOnMachineFunction(MachineFunction &MF) { |
| 780 | |
| 781 | static unsigned functionNum = 0; |
| 782 | if (CanonicalizeFunctionNumber != ~0U) { |
| 783 | if (CanonicalizeFunctionNumber != functionNum++) |
| 784 | return false; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 785 | LLVM_DEBUG(dbgs() << "\n Canonicalizing Function " << MF.getName() |
| 786 | << "\n";); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | // we need a valid vreg to create a vreg type for skipping all those |
| 790 | // stray vreg numbers so reach alignment/canonical vreg values. |
Puyan Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 791 | std::vector<MachineBasicBlock *> RPOList = GetRPOList(MF); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 792 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 793 | LLVM_DEBUG( |
| 794 | dbgs() << "\n\n NEW MACHINE FUNCTION: " << MF.getName() << " \n\n"; |
| 795 | dbgs() << "\n\n================================================\n\n"; |
| 796 | dbgs() << "Total Basic Blocks: " << RPOList.size() << "\n"; |
| 797 | for (auto MBB |
| 798 | : RPOList) { dbgs() << MBB->getName() << "\n"; } dbgs() |
| 799 | << "\n\n================================================\n\n";); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 800 | |
| 801 | std::vector<StringRef> BBNames; |
| 802 | std::vector<unsigned> RenamedInOtherBB; |
| 803 | |
| 804 | unsigned GapIdx = 0; |
| 805 | unsigned BBNum = 0; |
| 806 | |
| 807 | bool Changed = false; |
| 808 | |
Puyan Lotfi | d6f7313 | 2018-04-05 00:27:15 +0000 | [diff] [blame] | 809 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 810 | NamedVRegCursor NVC(MRI); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 811 | for (auto MBB : RPOList) |
Puyan Lotfi | 6ea89b4 | 2018-04-16 08:12:15 +0000 | [diff] [blame] | 812 | Changed |= |
| 813 | runOnBasicBlock(MBB, BBNames, RenamedInOtherBB, BBNum, GapIdx, NVC); |
Puyan Lotfi | a521c4ac5 | 2017-11-02 23:37:32 +0000 | [diff] [blame] | 814 | |
| 815 | return Changed; |
| 816 | } |