1 2 //===------------ MIRVRegNamerUtils.h - MIR VReg Renaming Utilities -------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // The purpose of these utilities is to abstract out parts of the MIRCanon pass 11 // that are responsible for renaming virtual registers with the purpose of 12 // sharing code with a MIRVRegNamer pass that could be the analog of the 13 // opt -instnamer pass. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #ifndef LLVM_LIB_CODEGEN_MIRVREGNAMERUTILS_H 18 #define LLVM_LIB_CODEGEN_MIRVREGNAMERUTILS_H 19 20 #include "llvm/ADT/PostOrderIterator.h" 21 #include "llvm/ADT/STLExtras.h" 22 #include "llvm/CodeGen/MachineFunctionPass.h" 23 #include "llvm/CodeGen/MachineInstrBuilder.h" 24 #include "llvm/CodeGen/MachineRegisterInfo.h" 25 #include "llvm/CodeGen/Passes.h" 26 #include "llvm/Support/raw_ostream.h" 27 28 #include <queue> 29 30 namespace llvm { 31 32 /// NamedVRegCursor - The cursor is an object that keeps track of what the next 33 /// vreg name should be. It does book keeping to determine when to skip the 34 /// index value and by how much, or if the next vreg name should be an increment 35 /// from the previous. 36 class NamedVRegCursor { 37 MachineRegisterInfo &MRI; 38 39 /// virtualVRegNumber - Book keeping of the last vreg position. 40 unsigned virtualVRegNumber; 41 42 /// SkipGapSize - Used to calculate a modulo amount to skip by after every 43 /// sequence of instructions starting from a given side-effecting 44 /// MachineInstruction for a given MachineBasicBlock. The general idea is that 45 /// for a given program compiled with two different opt pipelines, there 46 /// shouldn't be greater than SkipGapSize difference in how many vregs are in 47 /// play between the two and for every def-use graph of vregs we rename we 48 /// will round up to the next SkipGapSize'th number so that we have a high 49 /// change of landing on the same name for two given matching side-effects 50 /// for the two compilation outcomes. 51 const unsigned SkipGapSize; 52 53 /// RenamedInOtherBB - VRegs that we already renamed: ie breadcrumbs. 54 std::vector<Register> RenamedInOtherBB; 55 56 public: 57 NamedVRegCursor() = delete; 58 /// 1000 for the SkipGapSize was a good heuristic at the time of the writing 59 /// of the MIRCanonicalizerPass. Adjust as needed. 60 NamedVRegCursor(MachineRegisterInfo &MRI, unsigned SkipGapSize = 1000) 61 : MRI(MRI), virtualVRegNumber(0), SkipGapSize(SkipGapSize) {} 62 63 /// SkipGapSize - Skips modulo a gap value of indices. Indices are used to 64 /// produce the next vreg name. 65 void skipVRegs(); 66 67 unsigned getVirtualVReg() const { return virtualVRegNumber; } 68 69 /// incrementVirtualVReg - This increments an index value that us used to 70 /// create a new vreg name. This is not a Register. 71 unsigned incrementVirtualVReg(unsigned incr = 1) { 72 virtualVRegNumber += incr; 73 return virtualVRegNumber; 74 } 75 76 /// createVirtualRegister - Given an existing vreg, create a named vreg to 77 /// take its place. 78 unsigned createVirtualRegister(unsigned VReg); 79 80 /// renameVRegs - For a given MachineBasicBlock, scan for side-effecting 81 /// instructions, walk the def-use from each side-effecting root (in sorted 82 /// root order) and rename the encountered vregs in the def-use graph in a 83 /// canonical ordering. This method maintains book keeping for which vregs 84 /// were already renamed in RenamedInOtherBB. 85 // @return changed 86 bool renameVRegs(MachineBasicBlock *MBB); 87 }; 88 89 } // namespace llvm 90 91 #endif 92