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