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