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