10b57cec5SDimitry Andric //===- llvm/CodeGen/MachineRegisterInfo.h -----------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file defines the MachineRegisterInfo class. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #ifndef LLVM_CODEGEN_MACHINEREGISTERINFO_H 140b57cec5SDimitry Andric #define LLVM_CODEGEN_MACHINEREGISTERINFO_H 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 170b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h" 180b57cec5SDimitry Andric #include "llvm/ADT/IndexedMap.h" 190b57cec5SDimitry Andric #include "llvm/ADT/PointerUnion.h" 20bdd1243dSDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 210b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 220b57cec5SDimitry Andric #include "llvm/ADT/StringSet.h" 230b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBundle.h" 270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 2881ad6265SDimitry Andric #include "llvm/CodeGen/RegisterBank.h" 290b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 300b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 310b57cec5SDimitry Andric #include "llvm/MC/LaneBitmask.h" 320b57cec5SDimitry Andric #include <cassert> 330b57cec5SDimitry Andric #include <cstddef> 340b57cec5SDimitry Andric #include <cstdint> 350b57cec5SDimitry Andric #include <iterator> 360b57cec5SDimitry Andric #include <memory> 370b57cec5SDimitry Andric #include <utility> 380b57cec5SDimitry Andric #include <vector> 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric namespace llvm { 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric class PSetIterator; 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric /// Convenient type to represent either a register class or a register bank. 450b57cec5SDimitry Andric using RegClassOrRegBank = 460b57cec5SDimitry Andric PointerUnion<const TargetRegisterClass *, const RegisterBank *>; 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric /// MachineRegisterInfo - Keep track of information for virtual and physical 490b57cec5SDimitry Andric /// registers, including vreg register classes, use/def chains for registers, 500b57cec5SDimitry Andric /// etc. 510b57cec5SDimitry Andric class MachineRegisterInfo { 520b57cec5SDimitry Andric public: 530b57cec5SDimitry Andric class Delegate { 540b57cec5SDimitry Andric virtual void anchor(); 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric public: 570b57cec5SDimitry Andric virtual ~Delegate() = default; 580b57cec5SDimitry Andric 595ffd83dbSDimitry Andric virtual void MRI_NoteNewVirtualRegister(Register Reg) = 0; 6006c3fb27SDimitry Andric virtual void MRI_NoteCloneVirtualRegister(Register NewReg, 61bdd1243dSDimitry Andric Register SrcReg) { 62bdd1243dSDimitry Andric MRI_NoteNewVirtualRegister(NewReg); 63bdd1243dSDimitry Andric } 640b57cec5SDimitry Andric }; 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric private: 670b57cec5SDimitry Andric MachineFunction *MF; 68bdd1243dSDimitry Andric SmallPtrSet<Delegate *, 1> TheDelegates; 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric /// True if subregister liveness is tracked. 710b57cec5SDimitry Andric const bool TracksSubRegLiveness; 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric /// VRegInfo - Information we keep for each virtual register. 740b57cec5SDimitry Andric /// 750b57cec5SDimitry Andric /// Each element in this list contains the register class of the vreg and the 760b57cec5SDimitry Andric /// start of the use/def list for the register. 770b57cec5SDimitry Andric IndexedMap<std::pair<RegClassOrRegBank, MachineOperand *>, 780b57cec5SDimitry Andric VirtReg2IndexFunctor> 790b57cec5SDimitry Andric VRegInfo; 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric /// Map for recovering vreg name from vreg number. 820b57cec5SDimitry Andric /// This map is used by the MIR Printer. 830b57cec5SDimitry Andric IndexedMap<std::string, VirtReg2IndexFunctor> VReg2Name; 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric /// StringSet that is used to unique vreg names. 860b57cec5SDimitry Andric StringSet<> VRegNames; 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric /// The flag is true upon \p UpdatedCSRs initialization 890b57cec5SDimitry Andric /// and false otherwise. 901fd87a68SDimitry Andric bool IsUpdatedCSRsInitialized = false; 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric /// Contains the updated callee saved register list. 930b57cec5SDimitry Andric /// As opposed to the static list defined in register info, 940b57cec5SDimitry Andric /// all registers that were disabled are removed from the list. 950b57cec5SDimitry Andric SmallVector<MCPhysReg, 16> UpdatedCSRs; 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric /// RegAllocHints - This vector records register allocation hints for 980b57cec5SDimitry Andric /// virtual registers. For each virtual register, it keeps a pair of hint 990b57cec5SDimitry Andric /// type and hints vector making up the allocation hints. Only the first 1000b57cec5SDimitry Andric /// hint may be target specific, and in that case this is reflected by the 1010b57cec5SDimitry Andric /// first member of the pair being non-zero. If the hinted register is 1020b57cec5SDimitry Andric /// virtual, it means the allocator should prefer the physical register 1030b57cec5SDimitry Andric /// allocated to it if any. 10406c3fb27SDimitry Andric IndexedMap<std::pair<unsigned, SmallVector<Register, 4>>, 10506c3fb27SDimitry Andric VirtReg2IndexFunctor> 10606c3fb27SDimitry Andric RegAllocHints; 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric /// PhysRegUseDefLists - This is an array of the head of the use/def list for 1090b57cec5SDimitry Andric /// physical registers. 1100b57cec5SDimitry Andric std::unique_ptr<MachineOperand *[]> PhysRegUseDefLists; 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric /// getRegUseDefListHead - Return the head pointer for the register use/def 1130b57cec5SDimitry Andric /// list for the specified virtual or physical register. 1148bcb0991SDimitry Andric MachineOperand *&getRegUseDefListHead(Register RegNo) { 1158bcb0991SDimitry Andric if (RegNo.isVirtual()) 1168bcb0991SDimitry Andric return VRegInfo[RegNo.id()].second; 1178bcb0991SDimitry Andric return PhysRegUseDefLists[RegNo.id()]; 1180b57cec5SDimitry Andric } 1190b57cec5SDimitry Andric 1208bcb0991SDimitry Andric MachineOperand *getRegUseDefListHead(Register RegNo) const { 1218bcb0991SDimitry Andric if (RegNo.isVirtual()) 1228bcb0991SDimitry Andric return VRegInfo[RegNo.id()].second; 1238bcb0991SDimitry Andric return PhysRegUseDefLists[RegNo.id()]; 1240b57cec5SDimitry Andric } 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric /// Get the next element in the use-def chain. 1270b57cec5SDimitry Andric static MachineOperand *getNextOperandForReg(const MachineOperand *MO) { 1280b57cec5SDimitry Andric assert(MO && MO->isReg() && "This is not a register operand!"); 1290b57cec5SDimitry Andric return MO->Contents.Reg.Next; 1300b57cec5SDimitry Andric } 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric /// UsedPhysRegMask - Additional used physregs including aliases. 1330b57cec5SDimitry Andric /// This bit vector represents all the registers clobbered by function calls. 1340b57cec5SDimitry Andric BitVector UsedPhysRegMask; 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric /// ReservedRegs - This is a bit vector of reserved registers. The target 1370b57cec5SDimitry Andric /// may change its mind about which registers should be reserved. This 1380b57cec5SDimitry Andric /// vector is the frozen set of reserved registers when register allocation 1390b57cec5SDimitry Andric /// started. 1400b57cec5SDimitry Andric BitVector ReservedRegs; 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric using VRegToTypeMap = IndexedMap<LLT, VirtReg2IndexFunctor>; 1430b57cec5SDimitry Andric /// Map generic virtual registers to their low-level type. 1440b57cec5SDimitry Andric VRegToTypeMap VRegToType; 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric /// Keep track of the physical registers that are live in to the function. 1470b57cec5SDimitry Andric /// Live in values are typically arguments in registers. LiveIn values are 1480b57cec5SDimitry Andric /// allowed to have virtual registers associated with them, stored in the 1490b57cec5SDimitry Andric /// second element. 1505ffd83dbSDimitry Andric std::vector<std::pair<MCRegister, Register>> LiveIns; 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric public: 1530b57cec5SDimitry Andric explicit MachineRegisterInfo(MachineFunction *MF); 1540b57cec5SDimitry Andric MachineRegisterInfo(const MachineRegisterInfo &) = delete; 1550b57cec5SDimitry Andric MachineRegisterInfo &operator=(const MachineRegisterInfo &) = delete; 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric const TargetRegisterInfo *getTargetRegisterInfo() const { 1580b57cec5SDimitry Andric return MF->getSubtarget().getRegisterInfo(); 1590b57cec5SDimitry Andric } 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric void resetDelegate(Delegate *delegate) { 1620b57cec5SDimitry Andric // Ensure another delegate does not take over unless the current 163bdd1243dSDimitry Andric // delegate first unattaches itself. 164bdd1243dSDimitry Andric assert(TheDelegates.count(delegate) && 165bdd1243dSDimitry Andric "Only an existing delegate can perform reset!"); 166bdd1243dSDimitry Andric TheDelegates.erase(delegate); 1670b57cec5SDimitry Andric } 1680b57cec5SDimitry Andric 169bdd1243dSDimitry Andric void addDelegate(Delegate *delegate) { 170bdd1243dSDimitry Andric assert(delegate && !TheDelegates.count(delegate) && 171bdd1243dSDimitry Andric "Attempted to add null delegate, or to change it without " 1720b57cec5SDimitry Andric "first resetting it!"); 1730b57cec5SDimitry Andric 174bdd1243dSDimitry Andric TheDelegates.insert(delegate); 175bdd1243dSDimitry Andric } 176bdd1243dSDimitry Andric 177bdd1243dSDimitry Andric void noteNewVirtualRegister(Register Reg) { 178bdd1243dSDimitry Andric for (auto *TheDelegate : TheDelegates) 179bdd1243dSDimitry Andric TheDelegate->MRI_NoteNewVirtualRegister(Reg); 180bdd1243dSDimitry Andric } 181bdd1243dSDimitry Andric 182bdd1243dSDimitry Andric void noteCloneVirtualRegister(Register NewReg, Register SrcReg) { 183bdd1243dSDimitry Andric for (auto *TheDelegate : TheDelegates) 18406c3fb27SDimitry Andric TheDelegate->MRI_NoteCloneVirtualRegister(NewReg, SrcReg); 1850b57cec5SDimitry Andric } 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric //===--------------------------------------------------------------------===// 1880b57cec5SDimitry Andric // Function State 1890b57cec5SDimitry Andric //===--------------------------------------------------------------------===// 1900b57cec5SDimitry Andric 1910b57cec5SDimitry Andric // isSSA - Returns true when the machine function is in SSA form. Early 1920b57cec5SDimitry Andric // passes require the machine function to be in SSA form where every virtual 1930b57cec5SDimitry Andric // register has a single defining instruction. 1940b57cec5SDimitry Andric // 1950b57cec5SDimitry Andric // The TwoAddressInstructionPass and PHIElimination passes take the machine 1960b57cec5SDimitry Andric // function out of SSA form when they introduce multiple defs per virtual 1970b57cec5SDimitry Andric // register. 1980b57cec5SDimitry Andric bool isSSA() const { 1990b57cec5SDimitry Andric return MF->getProperties().hasProperty( 2000b57cec5SDimitry Andric MachineFunctionProperties::Property::IsSSA); 2010b57cec5SDimitry Andric } 2020b57cec5SDimitry Andric 2030b57cec5SDimitry Andric // leaveSSA - Indicates that the machine function is no longer in SSA form. 2040b57cec5SDimitry Andric void leaveSSA() { 2050b57cec5SDimitry Andric MF->getProperties().reset(MachineFunctionProperties::Property::IsSSA); 2060b57cec5SDimitry Andric } 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric /// tracksLiveness - Returns true when tracking register liveness accurately. 2090b57cec5SDimitry Andric /// (see MachineFUnctionProperties::Property description for details) 2100b57cec5SDimitry Andric bool tracksLiveness() const { 2110b57cec5SDimitry Andric return MF->getProperties().hasProperty( 2120b57cec5SDimitry Andric MachineFunctionProperties::Property::TracksLiveness); 2130b57cec5SDimitry Andric } 2140b57cec5SDimitry Andric 2150b57cec5SDimitry Andric /// invalidateLiveness - Indicates that register liveness is no longer being 2160b57cec5SDimitry Andric /// tracked accurately. 2170b57cec5SDimitry Andric /// 2180b57cec5SDimitry Andric /// This should be called by late passes that invalidate the liveness 2190b57cec5SDimitry Andric /// information. 2200b57cec5SDimitry Andric void invalidateLiveness() { 2210b57cec5SDimitry Andric MF->getProperties().reset( 2220b57cec5SDimitry Andric MachineFunctionProperties::Property::TracksLiveness); 2230b57cec5SDimitry Andric } 2240b57cec5SDimitry Andric 2250b57cec5SDimitry Andric /// Returns true if liveness for register class @p RC should be tracked at 2260b57cec5SDimitry Andric /// the subregister level. 2270b57cec5SDimitry Andric bool shouldTrackSubRegLiveness(const TargetRegisterClass &RC) const { 2280b57cec5SDimitry Andric return subRegLivenessEnabled() && RC.HasDisjunctSubRegs; 2290b57cec5SDimitry Andric } 2308bcb0991SDimitry Andric bool shouldTrackSubRegLiveness(Register VReg) const { 2318bcb0991SDimitry Andric assert(VReg.isVirtual() && "Must pass a VReg"); 2325f757f3fSDimitry Andric const TargetRegisterClass *RC = getRegClassOrNull(VReg); 2335f757f3fSDimitry Andric return LLVM_LIKELY(RC) ? shouldTrackSubRegLiveness(*RC) : false; 2340b57cec5SDimitry Andric } 2350b57cec5SDimitry Andric bool subRegLivenessEnabled() const { 2360b57cec5SDimitry Andric return TracksSubRegLiveness; 2370b57cec5SDimitry Andric } 2380b57cec5SDimitry Andric 2390b57cec5SDimitry Andric //===--------------------------------------------------------------------===// 2400b57cec5SDimitry Andric // Register Info 2410b57cec5SDimitry Andric //===--------------------------------------------------------------------===// 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric /// Returns true if the updated CSR list was initialized and false otherwise. 2440b57cec5SDimitry Andric bool isUpdatedCSRsInitialized() const { return IsUpdatedCSRsInitialized; } 2450b57cec5SDimitry Andric 2460b57cec5SDimitry Andric /// Disables the register from the list of CSRs. 2470b57cec5SDimitry Andric /// I.e. the register will not appear as part of the CSR mask. 2480b57cec5SDimitry Andric /// \see UpdatedCalleeSavedRegs. 2495ffd83dbSDimitry Andric void disableCalleeSavedRegister(MCRegister Reg); 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric /// Returns list of callee saved registers. 2520b57cec5SDimitry Andric /// The function returns the updated CSR list (after taking into account 2530b57cec5SDimitry Andric /// registers that are disabled from the CSR list). 2540b57cec5SDimitry Andric const MCPhysReg *getCalleeSavedRegs() const; 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric /// Sets the updated Callee Saved Registers list. 2570b57cec5SDimitry Andric /// Notice that it will override ant previously disabled/saved CSRs. 2580b57cec5SDimitry Andric void setCalleeSavedRegs(ArrayRef<MCPhysReg> CSRs); 2590b57cec5SDimitry Andric 2600b57cec5SDimitry Andric // Strictly for use by MachineInstr.cpp. 2610b57cec5SDimitry Andric void addRegOperandToUseList(MachineOperand *MO); 2620b57cec5SDimitry Andric 2630b57cec5SDimitry Andric // Strictly for use by MachineInstr.cpp. 2640b57cec5SDimitry Andric void removeRegOperandFromUseList(MachineOperand *MO); 2650b57cec5SDimitry Andric 2660b57cec5SDimitry Andric // Strictly for use by MachineInstr.cpp. 2670b57cec5SDimitry Andric void moveOperands(MachineOperand *Dst, MachineOperand *Src, unsigned NumOps); 2680b57cec5SDimitry Andric 2690b57cec5SDimitry Andric /// Verify the sanity of the use list for Reg. 2705ffd83dbSDimitry Andric void verifyUseList(Register Reg) const; 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric /// Verify the use list of all registers. 2730b57cec5SDimitry Andric void verifyUseLists() const; 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric /// reg_begin/reg_end - Provide iteration support to walk over all definitions 2760b57cec5SDimitry Andric /// and uses of a register within the MachineFunction that corresponds to this 2770b57cec5SDimitry Andric /// MachineRegisterInfo object. 2780b57cec5SDimitry Andric template<bool Uses, bool Defs, bool SkipDebug, 2790b57cec5SDimitry Andric bool ByOperand, bool ByInstr, bool ByBundle> 2800b57cec5SDimitry Andric class defusechain_iterator; 2810b57cec5SDimitry Andric template<bool Uses, bool Defs, bool SkipDebug, 2820b57cec5SDimitry Andric bool ByOperand, bool ByInstr, bool ByBundle> 2830b57cec5SDimitry Andric class defusechain_instr_iterator; 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric // Make it a friend so it can access getNextOperandForReg(). 2860b57cec5SDimitry Andric template<bool, bool, bool, bool, bool, bool> 2870b57cec5SDimitry Andric friend class defusechain_iterator; 2880b57cec5SDimitry Andric template<bool, bool, bool, bool, bool, bool> 2890b57cec5SDimitry Andric friend class defusechain_instr_iterator; 2900b57cec5SDimitry Andric 2910b57cec5SDimitry Andric /// reg_iterator/reg_begin/reg_end - Walk all defs and uses of the specified 2920b57cec5SDimitry Andric /// register. 2930b57cec5SDimitry Andric using reg_iterator = 2940b57cec5SDimitry Andric defusechain_iterator<true, true, false, true, false, false>; 2955ffd83dbSDimitry Andric reg_iterator reg_begin(Register RegNo) const { 2960b57cec5SDimitry Andric return reg_iterator(getRegUseDefListHead(RegNo)); 2970b57cec5SDimitry Andric } 2980b57cec5SDimitry Andric static reg_iterator reg_end() { return reg_iterator(nullptr); } 2990b57cec5SDimitry Andric 3005ffd83dbSDimitry Andric inline iterator_range<reg_iterator> reg_operands(Register Reg) const { 3010b57cec5SDimitry Andric return make_range(reg_begin(Reg), reg_end()); 3020b57cec5SDimitry Andric } 3030b57cec5SDimitry Andric 3040b57cec5SDimitry Andric /// reg_instr_iterator/reg_instr_begin/reg_instr_end - Walk all defs and uses 3050b57cec5SDimitry Andric /// of the specified register, stepping by MachineInstr. 3060b57cec5SDimitry Andric using reg_instr_iterator = 3070b57cec5SDimitry Andric defusechain_instr_iterator<true, true, false, false, true, false>; 3085ffd83dbSDimitry Andric reg_instr_iterator reg_instr_begin(Register RegNo) const { 3090b57cec5SDimitry Andric return reg_instr_iterator(getRegUseDefListHead(RegNo)); 3100b57cec5SDimitry Andric } 3110b57cec5SDimitry Andric static reg_instr_iterator reg_instr_end() { 3120b57cec5SDimitry Andric return reg_instr_iterator(nullptr); 3130b57cec5SDimitry Andric } 3140b57cec5SDimitry Andric 3150b57cec5SDimitry Andric inline iterator_range<reg_instr_iterator> 3165ffd83dbSDimitry Andric reg_instructions(Register Reg) const { 3170b57cec5SDimitry Andric return make_range(reg_instr_begin(Reg), reg_instr_end()); 3180b57cec5SDimitry Andric } 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andric /// reg_bundle_iterator/reg_bundle_begin/reg_bundle_end - Walk all defs and uses 3210b57cec5SDimitry Andric /// of the specified register, stepping by bundle. 3220b57cec5SDimitry Andric using reg_bundle_iterator = 3230b57cec5SDimitry Andric defusechain_instr_iterator<true, true, false, false, false, true>; 3245ffd83dbSDimitry Andric reg_bundle_iterator reg_bundle_begin(Register RegNo) const { 3250b57cec5SDimitry Andric return reg_bundle_iterator(getRegUseDefListHead(RegNo)); 3260b57cec5SDimitry Andric } 3270b57cec5SDimitry Andric static reg_bundle_iterator reg_bundle_end() { 3280b57cec5SDimitry Andric return reg_bundle_iterator(nullptr); 3290b57cec5SDimitry Andric } 3300b57cec5SDimitry Andric 3315ffd83dbSDimitry Andric inline iterator_range<reg_bundle_iterator> reg_bundles(Register Reg) const { 3320b57cec5SDimitry Andric return make_range(reg_bundle_begin(Reg), reg_bundle_end()); 3330b57cec5SDimitry Andric } 3340b57cec5SDimitry Andric 3350b57cec5SDimitry Andric /// reg_empty - Return true if there are no instructions using or defining the 3360b57cec5SDimitry Andric /// specified register (it may be live-in). 3375ffd83dbSDimitry Andric bool reg_empty(Register RegNo) const { return reg_begin(RegNo) == reg_end(); } 3380b57cec5SDimitry Andric 3390b57cec5SDimitry Andric /// reg_nodbg_iterator/reg_nodbg_begin/reg_nodbg_end - Walk all defs and uses 3400b57cec5SDimitry Andric /// of the specified register, skipping those marked as Debug. 3410b57cec5SDimitry Andric using reg_nodbg_iterator = 3420b57cec5SDimitry Andric defusechain_iterator<true, true, true, true, false, false>; 3438bcb0991SDimitry Andric reg_nodbg_iterator reg_nodbg_begin(Register RegNo) const { 3440b57cec5SDimitry Andric return reg_nodbg_iterator(getRegUseDefListHead(RegNo)); 3450b57cec5SDimitry Andric } 3460b57cec5SDimitry Andric static reg_nodbg_iterator reg_nodbg_end() { 3470b57cec5SDimitry Andric return reg_nodbg_iterator(nullptr); 3480b57cec5SDimitry Andric } 3490b57cec5SDimitry Andric 3500b57cec5SDimitry Andric inline iterator_range<reg_nodbg_iterator> 3515ffd83dbSDimitry Andric reg_nodbg_operands(Register Reg) const { 3520b57cec5SDimitry Andric return make_range(reg_nodbg_begin(Reg), reg_nodbg_end()); 3530b57cec5SDimitry Andric } 3540b57cec5SDimitry Andric 3550b57cec5SDimitry Andric /// reg_instr_nodbg_iterator/reg_instr_nodbg_begin/reg_instr_nodbg_end - Walk 3560b57cec5SDimitry Andric /// all defs and uses of the specified register, stepping by MachineInstr, 3570b57cec5SDimitry Andric /// skipping those marked as Debug. 3580b57cec5SDimitry Andric using reg_instr_nodbg_iterator = 3590b57cec5SDimitry Andric defusechain_instr_iterator<true, true, true, false, true, false>; 3605ffd83dbSDimitry Andric reg_instr_nodbg_iterator reg_instr_nodbg_begin(Register RegNo) const { 3610b57cec5SDimitry Andric return reg_instr_nodbg_iterator(getRegUseDefListHead(RegNo)); 3620b57cec5SDimitry Andric } 3630b57cec5SDimitry Andric static reg_instr_nodbg_iterator reg_instr_nodbg_end() { 3640b57cec5SDimitry Andric return reg_instr_nodbg_iterator(nullptr); 3650b57cec5SDimitry Andric } 3660b57cec5SDimitry Andric 3670b57cec5SDimitry Andric inline iterator_range<reg_instr_nodbg_iterator> 3685ffd83dbSDimitry Andric reg_nodbg_instructions(Register Reg) const { 3690b57cec5SDimitry Andric return make_range(reg_instr_nodbg_begin(Reg), reg_instr_nodbg_end()); 3700b57cec5SDimitry Andric } 3710b57cec5SDimitry Andric 3720b57cec5SDimitry Andric /// reg_bundle_nodbg_iterator/reg_bundle_nodbg_begin/reg_bundle_nodbg_end - Walk 3730b57cec5SDimitry Andric /// all defs and uses of the specified register, stepping by bundle, 3740b57cec5SDimitry Andric /// skipping those marked as Debug. 3750b57cec5SDimitry Andric using reg_bundle_nodbg_iterator = 3760b57cec5SDimitry Andric defusechain_instr_iterator<true, true, true, false, false, true>; 3775ffd83dbSDimitry Andric reg_bundle_nodbg_iterator reg_bundle_nodbg_begin(Register RegNo) const { 3780b57cec5SDimitry Andric return reg_bundle_nodbg_iterator(getRegUseDefListHead(RegNo)); 3790b57cec5SDimitry Andric } 3800b57cec5SDimitry Andric static reg_bundle_nodbg_iterator reg_bundle_nodbg_end() { 3810b57cec5SDimitry Andric return reg_bundle_nodbg_iterator(nullptr); 3820b57cec5SDimitry Andric } 3830b57cec5SDimitry Andric 3840b57cec5SDimitry Andric inline iterator_range<reg_bundle_nodbg_iterator> 3855ffd83dbSDimitry Andric reg_nodbg_bundles(Register Reg) const { 3860b57cec5SDimitry Andric return make_range(reg_bundle_nodbg_begin(Reg), reg_bundle_nodbg_end()); 3870b57cec5SDimitry Andric } 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric /// reg_nodbg_empty - Return true if the only instructions using or defining 3900b57cec5SDimitry Andric /// Reg are Debug instructions. 3918bcb0991SDimitry Andric bool reg_nodbg_empty(Register RegNo) const { 3920b57cec5SDimitry Andric return reg_nodbg_begin(RegNo) == reg_nodbg_end(); 3930b57cec5SDimitry Andric } 3940b57cec5SDimitry Andric 3950b57cec5SDimitry Andric /// def_iterator/def_begin/def_end - Walk all defs of the specified register. 3960b57cec5SDimitry Andric using def_iterator = 3970b57cec5SDimitry Andric defusechain_iterator<false, true, false, true, false, false>; 3985ffd83dbSDimitry Andric def_iterator def_begin(Register RegNo) const { 3990b57cec5SDimitry Andric return def_iterator(getRegUseDefListHead(RegNo)); 4000b57cec5SDimitry Andric } 4010b57cec5SDimitry Andric static def_iterator def_end() { return def_iterator(nullptr); } 4020b57cec5SDimitry Andric 4035ffd83dbSDimitry Andric inline iterator_range<def_iterator> def_operands(Register Reg) const { 4040b57cec5SDimitry Andric return make_range(def_begin(Reg), def_end()); 4050b57cec5SDimitry Andric } 4060b57cec5SDimitry Andric 4070b57cec5SDimitry Andric /// def_instr_iterator/def_instr_begin/def_instr_end - Walk all defs of the 4080b57cec5SDimitry Andric /// specified register, stepping by MachineInst. 4090b57cec5SDimitry Andric using def_instr_iterator = 4100b57cec5SDimitry Andric defusechain_instr_iterator<false, true, false, false, true, false>; 4115ffd83dbSDimitry Andric def_instr_iterator def_instr_begin(Register RegNo) const { 4120b57cec5SDimitry Andric return def_instr_iterator(getRegUseDefListHead(RegNo)); 4130b57cec5SDimitry Andric } 4140b57cec5SDimitry Andric static def_instr_iterator def_instr_end() { 4150b57cec5SDimitry Andric return def_instr_iterator(nullptr); 4160b57cec5SDimitry Andric } 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric inline iterator_range<def_instr_iterator> 4195ffd83dbSDimitry Andric def_instructions(Register Reg) const { 4200b57cec5SDimitry Andric return make_range(def_instr_begin(Reg), def_instr_end()); 4210b57cec5SDimitry Andric } 4220b57cec5SDimitry Andric 4230b57cec5SDimitry Andric /// def_bundle_iterator/def_bundle_begin/def_bundle_end - Walk all defs of the 4240b57cec5SDimitry Andric /// specified register, stepping by bundle. 4250b57cec5SDimitry Andric using def_bundle_iterator = 4260b57cec5SDimitry Andric defusechain_instr_iterator<false, true, false, false, false, true>; 4275ffd83dbSDimitry Andric def_bundle_iterator def_bundle_begin(Register RegNo) const { 4280b57cec5SDimitry Andric return def_bundle_iterator(getRegUseDefListHead(RegNo)); 4290b57cec5SDimitry Andric } 4300b57cec5SDimitry Andric static def_bundle_iterator def_bundle_end() { 4310b57cec5SDimitry Andric return def_bundle_iterator(nullptr); 4320b57cec5SDimitry Andric } 4330b57cec5SDimitry Andric 4345ffd83dbSDimitry Andric inline iterator_range<def_bundle_iterator> def_bundles(Register Reg) const { 4350b57cec5SDimitry Andric return make_range(def_bundle_begin(Reg), def_bundle_end()); 4360b57cec5SDimitry Andric } 4370b57cec5SDimitry Andric 4380b57cec5SDimitry Andric /// def_empty - Return true if there are no instructions defining the 4390b57cec5SDimitry Andric /// specified register (it may be live-in). 4405ffd83dbSDimitry Andric bool def_empty(Register RegNo) const { return def_begin(RegNo) == def_end(); } 4410b57cec5SDimitry Andric 4425ffd83dbSDimitry Andric StringRef getVRegName(Register Reg) const { 4430b57cec5SDimitry Andric return VReg2Name.inBounds(Reg) ? StringRef(VReg2Name[Reg]) : ""; 4440b57cec5SDimitry Andric } 4450b57cec5SDimitry Andric 4465ffd83dbSDimitry Andric void insertVRegByName(StringRef Name, Register Reg) { 44706c3fb27SDimitry Andric assert((Name.empty() || !VRegNames.contains(Name)) && 4480b57cec5SDimitry Andric "Named VRegs Must be Unique."); 4490b57cec5SDimitry Andric if (!Name.empty()) { 4500b57cec5SDimitry Andric VRegNames.insert(Name); 4510b57cec5SDimitry Andric VReg2Name.grow(Reg); 4520b57cec5SDimitry Andric VReg2Name[Reg] = Name.str(); 4530b57cec5SDimitry Andric } 4540b57cec5SDimitry Andric } 4550b57cec5SDimitry Andric 4560b57cec5SDimitry Andric /// Return true if there is exactly one operand defining the specified 4570b57cec5SDimitry Andric /// register. 4585ffd83dbSDimitry Andric bool hasOneDef(Register RegNo) const { 459e8d8bef9SDimitry Andric return hasSingleElement(def_operands(RegNo)); 460e8d8bef9SDimitry Andric } 461e8d8bef9SDimitry Andric 462e8d8bef9SDimitry Andric /// Returns the defining operand if there is exactly one operand defining the 463e8d8bef9SDimitry Andric /// specified register, otherwise nullptr. 464e8d8bef9SDimitry Andric MachineOperand *getOneDef(Register Reg) const { 465e8d8bef9SDimitry Andric def_iterator DI = def_begin(Reg); 466e8d8bef9SDimitry Andric if (DI == def_end()) // No defs. 467e8d8bef9SDimitry Andric return nullptr; 468e8d8bef9SDimitry Andric 469e8d8bef9SDimitry Andric def_iterator OneDef = DI; 470e8d8bef9SDimitry Andric if (++DI == def_end()) 471e8d8bef9SDimitry Andric return &*OneDef; 472e8d8bef9SDimitry Andric return nullptr; // Multiple defs. 4730b57cec5SDimitry Andric } 4740b57cec5SDimitry Andric 4750b57cec5SDimitry Andric /// use_iterator/use_begin/use_end - Walk all uses of the specified register. 4760b57cec5SDimitry Andric using use_iterator = 4770b57cec5SDimitry Andric defusechain_iterator<true, false, false, true, false, false>; 4785ffd83dbSDimitry Andric use_iterator use_begin(Register RegNo) const { 4790b57cec5SDimitry Andric return use_iterator(getRegUseDefListHead(RegNo)); 4800b57cec5SDimitry Andric } 4810b57cec5SDimitry Andric static use_iterator use_end() { return use_iterator(nullptr); } 4820b57cec5SDimitry Andric 4835ffd83dbSDimitry Andric inline iterator_range<use_iterator> use_operands(Register Reg) const { 4840b57cec5SDimitry Andric return make_range(use_begin(Reg), use_end()); 4850b57cec5SDimitry Andric } 4860b57cec5SDimitry Andric 4870b57cec5SDimitry Andric /// use_instr_iterator/use_instr_begin/use_instr_end - Walk all uses of the 4880b57cec5SDimitry Andric /// specified register, stepping by MachineInstr. 4890b57cec5SDimitry Andric using use_instr_iterator = 4900b57cec5SDimitry Andric defusechain_instr_iterator<true, false, false, false, true, false>; 4915ffd83dbSDimitry Andric use_instr_iterator use_instr_begin(Register RegNo) const { 4920b57cec5SDimitry Andric return use_instr_iterator(getRegUseDefListHead(RegNo)); 4930b57cec5SDimitry Andric } 4940b57cec5SDimitry Andric static use_instr_iterator use_instr_end() { 4950b57cec5SDimitry Andric return use_instr_iterator(nullptr); 4960b57cec5SDimitry Andric } 4970b57cec5SDimitry Andric 4980b57cec5SDimitry Andric inline iterator_range<use_instr_iterator> 4995ffd83dbSDimitry Andric use_instructions(Register Reg) const { 5000b57cec5SDimitry Andric return make_range(use_instr_begin(Reg), use_instr_end()); 5010b57cec5SDimitry Andric } 5020b57cec5SDimitry Andric 5030b57cec5SDimitry Andric /// use_bundle_iterator/use_bundle_begin/use_bundle_end - Walk all uses of the 5040b57cec5SDimitry Andric /// specified register, stepping by bundle. 5050b57cec5SDimitry Andric using use_bundle_iterator = 5060b57cec5SDimitry Andric defusechain_instr_iterator<true, false, false, false, false, true>; 5075ffd83dbSDimitry Andric use_bundle_iterator use_bundle_begin(Register RegNo) const { 5080b57cec5SDimitry Andric return use_bundle_iterator(getRegUseDefListHead(RegNo)); 5090b57cec5SDimitry Andric } 5100b57cec5SDimitry Andric static use_bundle_iterator use_bundle_end() { 5110b57cec5SDimitry Andric return use_bundle_iterator(nullptr); 5120b57cec5SDimitry Andric } 5130b57cec5SDimitry Andric 5145ffd83dbSDimitry Andric inline iterator_range<use_bundle_iterator> use_bundles(Register Reg) const { 5150b57cec5SDimitry Andric return make_range(use_bundle_begin(Reg), use_bundle_end()); 5160b57cec5SDimitry Andric } 5170b57cec5SDimitry Andric 5180b57cec5SDimitry Andric /// use_empty - Return true if there are no instructions using the specified 5190b57cec5SDimitry Andric /// register. 5205ffd83dbSDimitry Andric bool use_empty(Register RegNo) const { return use_begin(RegNo) == use_end(); } 5210b57cec5SDimitry Andric 5220b57cec5SDimitry Andric /// hasOneUse - Return true if there is exactly one instruction using the 5230b57cec5SDimitry Andric /// specified register. 5245ffd83dbSDimitry Andric bool hasOneUse(Register RegNo) const { 525e8d8bef9SDimitry Andric return hasSingleElement(use_operands(RegNo)); 5260b57cec5SDimitry Andric } 5270b57cec5SDimitry Andric 5280b57cec5SDimitry Andric /// use_nodbg_iterator/use_nodbg_begin/use_nodbg_end - Walk all uses of the 5290b57cec5SDimitry Andric /// specified register, skipping those marked as Debug. 5300b57cec5SDimitry Andric using use_nodbg_iterator = 5310b57cec5SDimitry Andric defusechain_iterator<true, false, true, true, false, false>; 5325ffd83dbSDimitry Andric use_nodbg_iterator use_nodbg_begin(Register RegNo) const { 5330b57cec5SDimitry Andric return use_nodbg_iterator(getRegUseDefListHead(RegNo)); 5340b57cec5SDimitry Andric } 5350b57cec5SDimitry Andric static use_nodbg_iterator use_nodbg_end() { 5360b57cec5SDimitry Andric return use_nodbg_iterator(nullptr); 5370b57cec5SDimitry Andric } 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andric inline iterator_range<use_nodbg_iterator> 5405ffd83dbSDimitry Andric use_nodbg_operands(Register Reg) const { 5410b57cec5SDimitry Andric return make_range(use_nodbg_begin(Reg), use_nodbg_end()); 5420b57cec5SDimitry Andric } 5430b57cec5SDimitry Andric 5440b57cec5SDimitry Andric /// use_instr_nodbg_iterator/use_instr_nodbg_begin/use_instr_nodbg_end - Walk 5450b57cec5SDimitry Andric /// all uses of the specified register, stepping by MachineInstr, skipping 5460b57cec5SDimitry Andric /// those marked as Debug. 5470b57cec5SDimitry Andric using use_instr_nodbg_iterator = 5480b57cec5SDimitry Andric defusechain_instr_iterator<true, false, true, false, true, false>; 5495ffd83dbSDimitry Andric use_instr_nodbg_iterator use_instr_nodbg_begin(Register RegNo) const { 5500b57cec5SDimitry Andric return use_instr_nodbg_iterator(getRegUseDefListHead(RegNo)); 5510b57cec5SDimitry Andric } 5520b57cec5SDimitry Andric static use_instr_nodbg_iterator use_instr_nodbg_end() { 5530b57cec5SDimitry Andric return use_instr_nodbg_iterator(nullptr); 5540b57cec5SDimitry Andric } 5550b57cec5SDimitry Andric 5560b57cec5SDimitry Andric inline iterator_range<use_instr_nodbg_iterator> 5575ffd83dbSDimitry Andric use_nodbg_instructions(Register Reg) const { 5580b57cec5SDimitry Andric return make_range(use_instr_nodbg_begin(Reg), use_instr_nodbg_end()); 5590b57cec5SDimitry Andric } 5600b57cec5SDimitry Andric 5610b57cec5SDimitry Andric /// use_bundle_nodbg_iterator/use_bundle_nodbg_begin/use_bundle_nodbg_end - Walk 5620b57cec5SDimitry Andric /// all uses of the specified register, stepping by bundle, skipping 5630b57cec5SDimitry Andric /// those marked as Debug. 5640b57cec5SDimitry Andric using use_bundle_nodbg_iterator = 5650b57cec5SDimitry Andric defusechain_instr_iterator<true, false, true, false, false, true>; 5665ffd83dbSDimitry Andric use_bundle_nodbg_iterator use_bundle_nodbg_begin(Register RegNo) const { 5670b57cec5SDimitry Andric return use_bundle_nodbg_iterator(getRegUseDefListHead(RegNo)); 5680b57cec5SDimitry Andric } 5690b57cec5SDimitry Andric static use_bundle_nodbg_iterator use_bundle_nodbg_end() { 5700b57cec5SDimitry Andric return use_bundle_nodbg_iterator(nullptr); 5710b57cec5SDimitry Andric } 5720b57cec5SDimitry Andric 5730b57cec5SDimitry Andric inline iterator_range<use_bundle_nodbg_iterator> 5745ffd83dbSDimitry Andric use_nodbg_bundles(Register Reg) const { 5750b57cec5SDimitry Andric return make_range(use_bundle_nodbg_begin(Reg), use_bundle_nodbg_end()); 5760b57cec5SDimitry Andric } 5770b57cec5SDimitry Andric 5780b57cec5SDimitry Andric /// use_nodbg_empty - Return true if there are no non-Debug instructions 5790b57cec5SDimitry Andric /// using the specified register. 5805ffd83dbSDimitry Andric bool use_nodbg_empty(Register RegNo) const { 5810b57cec5SDimitry Andric return use_nodbg_begin(RegNo) == use_nodbg_end(); 5820b57cec5SDimitry Andric } 5830b57cec5SDimitry Andric 5840b57cec5SDimitry Andric /// hasOneNonDBGUse - Return true if there is exactly one non-Debug 5850b57cec5SDimitry Andric /// use of the specified register. 5865ffd83dbSDimitry Andric bool hasOneNonDBGUse(Register RegNo) const; 5870b57cec5SDimitry Andric 5880b57cec5SDimitry Andric /// hasOneNonDBGUse - Return true if there is exactly one non-Debug 5890b57cec5SDimitry Andric /// instruction using the specified register. Said instruction may have 5900b57cec5SDimitry Andric /// multiple uses. 5915ffd83dbSDimitry Andric bool hasOneNonDBGUser(Register RegNo) const; 5920b57cec5SDimitry Andric 593bdd1243dSDimitry Andric 594bdd1243dSDimitry Andric /// hasAtMostUses - Return true if the given register has at most \p MaxUsers 595bdd1243dSDimitry Andric /// non-debug user instructions. 596bdd1243dSDimitry Andric bool hasAtMostUserInstrs(Register Reg, unsigned MaxUsers) const; 597bdd1243dSDimitry Andric 5980b57cec5SDimitry Andric /// replaceRegWith - Replace all instances of FromReg with ToReg in the 5990b57cec5SDimitry Andric /// machine function. This is like llvm-level X->replaceAllUsesWith(Y), 6000b57cec5SDimitry Andric /// except that it also changes any definitions of the register as well. 6010b57cec5SDimitry Andric /// 6020b57cec5SDimitry Andric /// Note that it is usually necessary to first constrain ToReg's register 6030b57cec5SDimitry Andric /// class and register bank to match the FromReg constraints using one of the 6040b57cec5SDimitry Andric /// methods: 6050b57cec5SDimitry Andric /// 6060b57cec5SDimitry Andric /// constrainRegClass(ToReg, getRegClass(FromReg)) 6070b57cec5SDimitry Andric /// constrainRegAttrs(ToReg, FromReg) 6080b57cec5SDimitry Andric /// RegisterBankInfo::constrainGenericRegister(ToReg, 6090b57cec5SDimitry Andric /// *MRI.getRegClass(FromReg), MRI) 6100b57cec5SDimitry Andric /// 6110b57cec5SDimitry Andric /// These functions will return a falsy result if the virtual registers have 6120b57cec5SDimitry Andric /// incompatible constraints. 6130b57cec5SDimitry Andric /// 6140b57cec5SDimitry Andric /// Note that if ToReg is a physical register the function will replace and 6150b57cec5SDimitry Andric /// apply sub registers to ToReg in order to obtain a final/proper physical 6160b57cec5SDimitry Andric /// register. 6175ffd83dbSDimitry Andric void replaceRegWith(Register FromReg, Register ToReg); 6180b57cec5SDimitry Andric 6190b57cec5SDimitry Andric /// getVRegDef - Return the machine instr that defines the specified virtual 6200b57cec5SDimitry Andric /// register or null if none is found. This assumes that the code is in SSA 6210b57cec5SDimitry Andric /// form, so there should only be one definition. 6225ffd83dbSDimitry Andric MachineInstr *getVRegDef(Register Reg) const; 6230b57cec5SDimitry Andric 6240b57cec5SDimitry Andric /// getUniqueVRegDef - Return the unique machine instr that defines the 6250b57cec5SDimitry Andric /// specified virtual register or null if none is found. If there are 6260b57cec5SDimitry Andric /// multiple definitions or no definition, return null. 6275ffd83dbSDimitry Andric MachineInstr *getUniqueVRegDef(Register Reg) const; 6280b57cec5SDimitry Andric 6290b57cec5SDimitry Andric /// clearKillFlags - Iterate over all the uses of the given register and 6300b57cec5SDimitry Andric /// clear the kill flag from the MachineOperand. This function is used by 6310b57cec5SDimitry Andric /// optimization passes which extend register lifetimes and need only 6320b57cec5SDimitry Andric /// preserve conservative kill flag information. 6335ffd83dbSDimitry Andric void clearKillFlags(Register Reg) const; 6340b57cec5SDimitry Andric 6355ffd83dbSDimitry Andric void dumpUses(Register RegNo) const; 6360b57cec5SDimitry Andric 6370b57cec5SDimitry Andric /// Returns true if PhysReg is unallocatable and constant throughout the 6380b57cec5SDimitry Andric /// function. Writing to a constant register has no effect. 6395ffd83dbSDimitry Andric bool isConstantPhysReg(MCRegister PhysReg) const; 6400b57cec5SDimitry Andric 6410b57cec5SDimitry Andric /// Get an iterator over the pressure sets affected by the given physical or 6420b57cec5SDimitry Andric /// virtual register. If RegUnit is physical, it must be a register unit (from 6430b57cec5SDimitry Andric /// MCRegUnitIterator). 644e8d8bef9SDimitry Andric PSetIterator getPressureSets(Register RegUnit) const; 6450b57cec5SDimitry Andric 6460b57cec5SDimitry Andric //===--------------------------------------------------------------------===// 6470b57cec5SDimitry Andric // Virtual Register Info 6480b57cec5SDimitry Andric //===--------------------------------------------------------------------===// 6490b57cec5SDimitry Andric 6500b57cec5SDimitry Andric /// Return the register class of the specified virtual register. 6510b57cec5SDimitry Andric /// This shouldn't be used directly unless \p Reg has a register class. 6520b57cec5SDimitry Andric /// \see getRegClassOrNull when this might happen. 6538bcb0991SDimitry Andric const TargetRegisterClass *getRegClass(Register Reg) const { 65406c3fb27SDimitry Andric assert(isa<const TargetRegisterClass *>(VRegInfo[Reg.id()].first) && 6550b57cec5SDimitry Andric "Register class not set, wrong accessor"); 65606c3fb27SDimitry Andric return cast<const TargetRegisterClass *>(VRegInfo[Reg.id()].first); 6570b57cec5SDimitry Andric } 6580b57cec5SDimitry Andric 6590b57cec5SDimitry Andric /// Return the register class of \p Reg, or null if Reg has not been assigned 6600b57cec5SDimitry Andric /// a register class yet. 6610b57cec5SDimitry Andric /// 6620b57cec5SDimitry Andric /// \note A null register class can only happen when these two 6630b57cec5SDimitry Andric /// conditions are met: 6640b57cec5SDimitry Andric /// 1. Generic virtual registers are created. 6650b57cec5SDimitry Andric /// 2. The machine function has not completely been through the 6660b57cec5SDimitry Andric /// instruction selection process. 6670b57cec5SDimitry Andric /// None of this condition is possible without GlobalISel for now. 6680b57cec5SDimitry Andric /// In other words, if GlobalISel is not used or if the query happens after 6690b57cec5SDimitry Andric /// the select pass, using getRegClass is safe. 6705ffd83dbSDimitry Andric const TargetRegisterClass *getRegClassOrNull(Register Reg) const { 6710b57cec5SDimitry Andric const RegClassOrRegBank &Val = VRegInfo[Reg].first; 67206c3fb27SDimitry Andric return dyn_cast_if_present<const TargetRegisterClass *>(Val); 6730b57cec5SDimitry Andric } 6740b57cec5SDimitry Andric 6750b57cec5SDimitry Andric /// Return the register bank of \p Reg, or null if Reg has not been assigned 6760b57cec5SDimitry Andric /// a register bank or has been assigned a register class. 6770b57cec5SDimitry Andric /// \note It is possible to get the register bank from the register class via 6780b57cec5SDimitry Andric /// RegisterBankInfo::getRegBankFromRegClass. 6795ffd83dbSDimitry Andric const RegisterBank *getRegBankOrNull(Register Reg) const { 6800b57cec5SDimitry Andric const RegClassOrRegBank &Val = VRegInfo[Reg].first; 68106c3fb27SDimitry Andric return dyn_cast_if_present<const RegisterBank *>(Val); 6820b57cec5SDimitry Andric } 6830b57cec5SDimitry Andric 6840b57cec5SDimitry Andric /// Return the register bank or register class of \p Reg. 6850b57cec5SDimitry Andric /// \note Before the register bank gets assigned (i.e., before the 6860b57cec5SDimitry Andric /// RegBankSelect pass) \p Reg may not have either. 6875ffd83dbSDimitry Andric const RegClassOrRegBank &getRegClassOrRegBank(Register Reg) const { 6880b57cec5SDimitry Andric return VRegInfo[Reg].first; 6890b57cec5SDimitry Andric } 6900b57cec5SDimitry Andric 6910b57cec5SDimitry Andric /// setRegClass - Set the register class of the specified virtual register. 6925ffd83dbSDimitry Andric void setRegClass(Register Reg, const TargetRegisterClass *RC); 6930b57cec5SDimitry Andric 6940b57cec5SDimitry Andric /// Set the register bank to \p RegBank for \p Reg. 6955ffd83dbSDimitry Andric void setRegBank(Register Reg, const RegisterBank &RegBank); 6960b57cec5SDimitry Andric 6975ffd83dbSDimitry Andric void setRegClassOrRegBank(Register Reg, 6980b57cec5SDimitry Andric const RegClassOrRegBank &RCOrRB){ 6990b57cec5SDimitry Andric VRegInfo[Reg].first = RCOrRB; 7000b57cec5SDimitry Andric } 7010b57cec5SDimitry Andric 7020b57cec5SDimitry Andric /// constrainRegClass - Constrain the register class of the specified virtual 7030b57cec5SDimitry Andric /// register to be a common subclass of RC and the current register class, 7040b57cec5SDimitry Andric /// but only if the new class has at least MinNumRegs registers. Return the 7050b57cec5SDimitry Andric /// new register class, or NULL if no such class exists. 7060b57cec5SDimitry Andric /// This should only be used when the constraint is known to be trivial, like 7070b57cec5SDimitry Andric /// GR32 -> GR32_NOSP. Beware of increasing register pressure. 7080b57cec5SDimitry Andric /// 7090b57cec5SDimitry Andric /// \note Assumes that the register has a register class assigned. 7100b57cec5SDimitry Andric /// Use RegisterBankInfo::constrainGenericRegister in GlobalISel's 7110b57cec5SDimitry Andric /// InstructionSelect pass and constrainRegAttrs in every other pass, 7120b57cec5SDimitry Andric /// including non-select passes of GlobalISel, instead. 7135ffd83dbSDimitry Andric const TargetRegisterClass *constrainRegClass(Register Reg, 7140b57cec5SDimitry Andric const TargetRegisterClass *RC, 7150b57cec5SDimitry Andric unsigned MinNumRegs = 0); 7160b57cec5SDimitry Andric 7170b57cec5SDimitry Andric /// Constrain the register class or the register bank of the virtual register 7180b57cec5SDimitry Andric /// \p Reg (and low-level type) to be a common subclass or a common bank of 7190b57cec5SDimitry Andric /// both registers provided respectively (and a common low-level type). Do 7200b57cec5SDimitry Andric /// nothing if any of the attributes (classes, banks, or low-level types) of 7210b57cec5SDimitry Andric /// the registers are deemed incompatible, or if the resulting register will 7220b57cec5SDimitry Andric /// have a class smaller than before and of size less than \p MinNumRegs. 7230b57cec5SDimitry Andric /// Return true if such register attributes exist, false otherwise. 7240b57cec5SDimitry Andric /// 7250b57cec5SDimitry Andric /// \note Use this method instead of constrainRegClass and 7260b57cec5SDimitry Andric /// RegisterBankInfo::constrainGenericRegister everywhere but SelectionDAG 7270b57cec5SDimitry Andric /// ISel / FastISel and GlobalISel's InstructionSelect pass respectively. 7285ffd83dbSDimitry Andric bool constrainRegAttrs(Register Reg, Register ConstrainingReg, 7290b57cec5SDimitry Andric unsigned MinNumRegs = 0); 7300b57cec5SDimitry Andric 7310b57cec5SDimitry Andric /// recomputeRegClass - Try to find a legal super-class of Reg's register 7320b57cec5SDimitry Andric /// class that still satisfies the constraints from the instructions using 7330b57cec5SDimitry Andric /// Reg. Returns true if Reg was upgraded. 7340b57cec5SDimitry Andric /// 7350b57cec5SDimitry Andric /// This method can be used after constraints have been removed from a 7360b57cec5SDimitry Andric /// virtual register, for example after removing instructions or splitting 7370b57cec5SDimitry Andric /// the live range. 7385ffd83dbSDimitry Andric bool recomputeRegClass(Register Reg); 7390b57cec5SDimitry Andric 7400b57cec5SDimitry Andric /// createVirtualRegister - Create and return a new virtual register in the 7410b57cec5SDimitry Andric /// function with the specified register class. 7420b57cec5SDimitry Andric Register createVirtualRegister(const TargetRegisterClass *RegClass, 7430b57cec5SDimitry Andric StringRef Name = ""); 7440b57cec5SDimitry Andric 745*0fca6ea1SDimitry Andric /// All attributes(register class or bank and low-level type) a virtual 746*0fca6ea1SDimitry Andric /// register can have. 747*0fca6ea1SDimitry Andric struct VRegAttrs { 748*0fca6ea1SDimitry Andric RegClassOrRegBank RCOrRB; 749*0fca6ea1SDimitry Andric LLT Ty; 750*0fca6ea1SDimitry Andric }; 751*0fca6ea1SDimitry Andric 752*0fca6ea1SDimitry Andric /// Returns register class or bank and low level type of \p Reg. Always safe 753*0fca6ea1SDimitry Andric /// to use. Special values are returned when \p Reg does not have some of the 754*0fca6ea1SDimitry Andric /// attributes. 755*0fca6ea1SDimitry Andric VRegAttrs getVRegAttrs(Register Reg) { 756*0fca6ea1SDimitry Andric return {getRegClassOrRegBank(Reg), getType(Reg)}; 757*0fca6ea1SDimitry Andric } 758*0fca6ea1SDimitry Andric 759*0fca6ea1SDimitry Andric /// Create and return a new virtual register in the function with the 760*0fca6ea1SDimitry Andric /// specified register attributes(register class or bank and low level type). 761*0fca6ea1SDimitry Andric Register createVirtualRegister(VRegAttrs RegAttr, StringRef Name = ""); 762*0fca6ea1SDimitry Andric 7630b57cec5SDimitry Andric /// Create and return a new virtual register in the function with the same 7640b57cec5SDimitry Andric /// attributes as the given register. 7650b57cec5SDimitry Andric Register cloneVirtualRegister(Register VReg, StringRef Name = ""); 7660b57cec5SDimitry Andric 7670b57cec5SDimitry Andric /// Get the low-level type of \p Reg or LLT{} if Reg is not a generic 7680b57cec5SDimitry Andric /// (target independent) virtual register. 7695ffd83dbSDimitry Andric LLT getType(Register Reg) const { 770bdd1243dSDimitry Andric if (Reg.isVirtual() && VRegToType.inBounds(Reg)) 7710b57cec5SDimitry Andric return VRegToType[Reg]; 7720b57cec5SDimitry Andric return LLT{}; 7730b57cec5SDimitry Andric } 7740b57cec5SDimitry Andric 7750b57cec5SDimitry Andric /// Set the low-level type of \p VReg to \p Ty. 7765ffd83dbSDimitry Andric void setType(Register VReg, LLT Ty); 7770b57cec5SDimitry Andric 7780b57cec5SDimitry Andric /// Create and return a new generic virtual register with low-level 7790b57cec5SDimitry Andric /// type \p Ty. 7800b57cec5SDimitry Andric Register createGenericVirtualRegister(LLT Ty, StringRef Name = ""); 7810b57cec5SDimitry Andric 7820b57cec5SDimitry Andric /// Remove all types associated to virtual registers (after instruction 7830b57cec5SDimitry Andric /// selection and constraining of all generic virtual registers). 7840b57cec5SDimitry Andric void clearVirtRegTypes(); 7850b57cec5SDimitry Andric 7860b57cec5SDimitry Andric /// Creates a new virtual register that has no register class, register bank 7870b57cec5SDimitry Andric /// or size assigned yet. This is only allowed to be used 7880b57cec5SDimitry Andric /// temporarily while constructing machine instructions. Most operations are 7890b57cec5SDimitry Andric /// undefined on an incomplete register until one of setRegClass(), 7900b57cec5SDimitry Andric /// setRegBank() or setSize() has been called on it. 7915ffd83dbSDimitry Andric Register createIncompleteVirtualRegister(StringRef Name = ""); 7920b57cec5SDimitry Andric 7930b57cec5SDimitry Andric /// getNumVirtRegs - Return the number of virtual registers created. 7940b57cec5SDimitry Andric unsigned getNumVirtRegs() const { return VRegInfo.size(); } 7950b57cec5SDimitry Andric 7960b57cec5SDimitry Andric /// clearVirtRegs - Remove all virtual registers (after physreg assignment). 7970b57cec5SDimitry Andric void clearVirtRegs(); 7980b57cec5SDimitry Andric 7990b57cec5SDimitry Andric /// setRegAllocationHint - Specify a register allocation hint for the 8000b57cec5SDimitry Andric /// specified virtual register. This is typically used by target, and in case 8010b57cec5SDimitry Andric /// of an earlier hint it will be overwritten. 8025ffd83dbSDimitry Andric void setRegAllocationHint(Register VReg, unsigned Type, Register PrefReg) { 8035ffd83dbSDimitry Andric assert(VReg.isVirtual()); 8040b57cec5SDimitry Andric RegAllocHints[VReg].first = Type; 8050b57cec5SDimitry Andric RegAllocHints[VReg].second.clear(); 8060b57cec5SDimitry Andric RegAllocHints[VReg].second.push_back(PrefReg); 8070b57cec5SDimitry Andric } 8080b57cec5SDimitry Andric 8090b57cec5SDimitry Andric /// addRegAllocationHint - Add a register allocation hint to the hints 8100b57cec5SDimitry Andric /// vector for VReg. 8115ffd83dbSDimitry Andric void addRegAllocationHint(Register VReg, Register PrefReg) { 812bdd1243dSDimitry Andric assert(VReg.isVirtual()); 8130b57cec5SDimitry Andric RegAllocHints[VReg].second.push_back(PrefReg); 8140b57cec5SDimitry Andric } 8150b57cec5SDimitry Andric 8160b57cec5SDimitry Andric /// Specify the preferred (target independent) register allocation hint for 8170b57cec5SDimitry Andric /// the specified virtual register. 8185ffd83dbSDimitry Andric void setSimpleHint(Register VReg, Register PrefReg) { 8190b57cec5SDimitry Andric setRegAllocationHint(VReg, /*Type=*/0, PrefReg); 8200b57cec5SDimitry Andric } 8210b57cec5SDimitry Andric 8225ffd83dbSDimitry Andric void clearSimpleHint(Register VReg) { 8235ffd83dbSDimitry Andric assert (!RegAllocHints[VReg].first && 8240b57cec5SDimitry Andric "Expected to clear a non-target hint!"); 8250b57cec5SDimitry Andric RegAllocHints[VReg].second.clear(); 8260b57cec5SDimitry Andric } 8270b57cec5SDimitry Andric 8280b57cec5SDimitry Andric /// getRegAllocationHint - Return the register allocation hint for the 8290b57cec5SDimitry Andric /// specified virtual register. If there are many hints, this returns the 8300b57cec5SDimitry Andric /// one with the greatest weight. 83106c3fb27SDimitry Andric std::pair<unsigned, Register> getRegAllocationHint(Register VReg) const { 8328bcb0991SDimitry Andric assert(VReg.isVirtual()); 8335ffd83dbSDimitry Andric Register BestHint = (RegAllocHints[VReg.id()].second.size() ? 8345ffd83dbSDimitry Andric RegAllocHints[VReg.id()].second[0] : Register()); 83506c3fb27SDimitry Andric return {RegAllocHints[VReg.id()].first, BestHint}; 8360b57cec5SDimitry Andric } 8370b57cec5SDimitry Andric 8380b57cec5SDimitry Andric /// getSimpleHint - same as getRegAllocationHint except it will only return 8390b57cec5SDimitry Andric /// a target independent hint. 8408bcb0991SDimitry Andric Register getSimpleHint(Register VReg) const { 8418bcb0991SDimitry Andric assert(VReg.isVirtual()); 84206c3fb27SDimitry Andric std::pair<unsigned, Register> Hint = getRegAllocationHint(VReg); 8435ffd83dbSDimitry Andric return Hint.first ? Register() : Hint.second; 8440b57cec5SDimitry Andric } 8450b57cec5SDimitry Andric 8460b57cec5SDimitry Andric /// getRegAllocationHints - Return a reference to the vector of all 8470b57cec5SDimitry Andric /// register allocation hints for VReg. 84806c3fb27SDimitry Andric const std::pair<unsigned, SmallVector<Register, 4>> & 84906c3fb27SDimitry Andric getRegAllocationHints(Register VReg) const { 8505ffd83dbSDimitry Andric assert(VReg.isVirtual()); 8510b57cec5SDimitry Andric return RegAllocHints[VReg]; 8520b57cec5SDimitry Andric } 8530b57cec5SDimitry Andric 8540b57cec5SDimitry Andric /// markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the 8550b57cec5SDimitry Andric /// specified register as undefined which causes the DBG_VALUE to be 8560b57cec5SDimitry Andric /// deleted during LiveDebugVariables analysis. 8575ffd83dbSDimitry Andric void markUsesInDebugValueAsUndef(Register Reg) const; 8580b57cec5SDimitry Andric 859349cc55cSDimitry Andric /// updateDbgUsersToReg - Update a collection of debug instructions 8608bcb0991SDimitry Andric /// to refer to the designated register. 861fe6060f1SDimitry Andric void updateDbgUsersToReg(MCRegister OldReg, MCRegister NewReg, 8628bcb0991SDimitry Andric ArrayRef<MachineInstr *> Users) const { 863349cc55cSDimitry Andric // If this operand is a register, check whether it overlaps with OldReg. 864349cc55cSDimitry Andric // If it does, replace with NewReg. 86581ad6265SDimitry Andric auto UpdateOp = [this, &NewReg, &OldReg](MachineOperand &Op) { 86681ad6265SDimitry Andric if (Op.isReg() && 86781ad6265SDimitry Andric getTargetRegisterInfo()->regsOverlap(Op.getReg(), OldReg)) 868fe6060f1SDimitry Andric Op.setReg(NewReg); 869349cc55cSDimitry Andric }; 870349cc55cSDimitry Andric 871349cc55cSDimitry Andric // Iterate through (possibly several) operands to DBG_VALUEs and update 872349cc55cSDimitry Andric // each. For DBG_PHIs, only one operand will be present. 873349cc55cSDimitry Andric for (MachineInstr *MI : Users) { 874349cc55cSDimitry Andric if (MI->isDebugValue()) { 875349cc55cSDimitry Andric for (auto &Op : MI->debug_operands()) 876349cc55cSDimitry Andric UpdateOp(Op); 877fe6060f1SDimitry Andric assert(MI->hasDebugOperandForReg(NewReg) && 878fe6060f1SDimitry Andric "Expected debug value to have some overlap with OldReg"); 879349cc55cSDimitry Andric } else if (MI->isDebugPHI()) { 880349cc55cSDimitry Andric UpdateOp(MI->getOperand(0)); 881349cc55cSDimitry Andric } else { 882349cc55cSDimitry Andric llvm_unreachable("Non-DBG_VALUE, Non-DBG_PHI debug instr updated"); 883349cc55cSDimitry Andric } 8848bcb0991SDimitry Andric } 8858bcb0991SDimitry Andric } 8868bcb0991SDimitry Andric 8870b57cec5SDimitry Andric /// Return true if the specified register is modified in this function. 8880b57cec5SDimitry Andric /// This checks that no defining machine operands exist for the register or 8890b57cec5SDimitry Andric /// any of its aliases. Definitions found on functions marked noreturn are 8900b57cec5SDimitry Andric /// ignored, to consider them pass 'true' for optional parameter 8910b57cec5SDimitry Andric /// SkipNoReturnDef. The register is also considered modified when it is set 8920b57cec5SDimitry Andric /// in the UsedPhysRegMask. 8935ffd83dbSDimitry Andric bool isPhysRegModified(MCRegister PhysReg, bool SkipNoReturnDef = false) const; 8940b57cec5SDimitry Andric 8950b57cec5SDimitry Andric /// Return true if the specified register is modified or read in this 8960b57cec5SDimitry Andric /// function. This checks that no machine operands exist for the register or 897fe6060f1SDimitry Andric /// any of its aliases. If SkipRegMaskTest is false, the register is 898fe6060f1SDimitry Andric /// considered used when it is set in the UsedPhysRegMask. 899fe6060f1SDimitry Andric bool isPhysRegUsed(MCRegister PhysReg, bool SkipRegMaskTest = false) const; 9000b57cec5SDimitry Andric 9010b57cec5SDimitry Andric /// addPhysRegsUsedFromRegMask - Mark any registers not in RegMask as used. 9020b57cec5SDimitry Andric /// This corresponds to the bit mask attached to register mask operands. 9030b57cec5SDimitry Andric void addPhysRegsUsedFromRegMask(const uint32_t *RegMask) { 9040b57cec5SDimitry Andric UsedPhysRegMask.setBitsNotInMask(RegMask); 9050b57cec5SDimitry Andric } 9060b57cec5SDimitry Andric 9070b57cec5SDimitry Andric const BitVector &getUsedPhysRegsMask() const { return UsedPhysRegMask; } 9080b57cec5SDimitry Andric 9090b57cec5SDimitry Andric //===--------------------------------------------------------------------===// 9100b57cec5SDimitry Andric // Reserved Register Info 9110b57cec5SDimitry Andric //===--------------------------------------------------------------------===// 9120b57cec5SDimitry Andric // 9130b57cec5SDimitry Andric // The set of reserved registers must be invariant during register 9140b57cec5SDimitry Andric // allocation. For example, the target cannot suddenly decide it needs a 9150b57cec5SDimitry Andric // frame pointer when the register allocator has already used the frame 9160b57cec5SDimitry Andric // pointer register for something else. 9170b57cec5SDimitry Andric // 9180b57cec5SDimitry Andric // These methods can be used by target hooks like hasFP() to avoid changing 9190b57cec5SDimitry Andric // the reserved register set during register allocation. 9200b57cec5SDimitry Andric 9210b57cec5SDimitry Andric /// freezeReservedRegs - Called by the register allocator to freeze the set 9220b57cec5SDimitry Andric /// of reserved registers before allocation begins. 923*0fca6ea1SDimitry Andric void freezeReservedRegs(); 9240b57cec5SDimitry Andric 925bdd1243dSDimitry Andric /// reserveReg -- Mark a register as reserved so checks like isAllocatable 926bdd1243dSDimitry Andric /// will not suggest using it. This should not be used during the middle 927bdd1243dSDimitry Andric /// of a function walk, or when liveness info is available. 928bdd1243dSDimitry Andric void reserveReg(MCRegister PhysReg, const TargetRegisterInfo *TRI) { 929bdd1243dSDimitry Andric assert(reservedRegsFrozen() && 930bdd1243dSDimitry Andric "Reserved registers haven't been frozen yet. "); 931bdd1243dSDimitry Andric MCRegAliasIterator R(PhysReg, TRI, true); 932bdd1243dSDimitry Andric 933bdd1243dSDimitry Andric for (; R.isValid(); ++R) 934bdd1243dSDimitry Andric ReservedRegs.set(*R); 935bdd1243dSDimitry Andric } 936bdd1243dSDimitry Andric 9370b57cec5SDimitry Andric /// reservedRegsFrozen - Returns true after freezeReservedRegs() was called 9380b57cec5SDimitry Andric /// to ensure the set of reserved registers stays constant. 9390b57cec5SDimitry Andric bool reservedRegsFrozen() const { 9400b57cec5SDimitry Andric return !ReservedRegs.empty(); 9410b57cec5SDimitry Andric } 9420b57cec5SDimitry Andric 9430b57cec5SDimitry Andric /// canReserveReg - Returns true if PhysReg can be used as a reserved 9440b57cec5SDimitry Andric /// register. Any register can be reserved before freezeReservedRegs() is 9450b57cec5SDimitry Andric /// called. 9465ffd83dbSDimitry Andric bool canReserveReg(MCRegister PhysReg) const { 9470b57cec5SDimitry Andric return !reservedRegsFrozen() || ReservedRegs.test(PhysReg); 9480b57cec5SDimitry Andric } 9490b57cec5SDimitry Andric 9500b57cec5SDimitry Andric /// getReservedRegs - Returns a reference to the frozen set of reserved 9510b57cec5SDimitry Andric /// registers. This method should always be preferred to calling 9520b57cec5SDimitry Andric /// TRI::getReservedRegs() when possible. 9530b57cec5SDimitry Andric const BitVector &getReservedRegs() const { 9540b57cec5SDimitry Andric assert(reservedRegsFrozen() && 9550b57cec5SDimitry Andric "Reserved registers haven't been frozen yet. " 9560b57cec5SDimitry Andric "Use TRI::getReservedRegs()."); 9570b57cec5SDimitry Andric return ReservedRegs; 9580b57cec5SDimitry Andric } 9590b57cec5SDimitry Andric 9600b57cec5SDimitry Andric /// isReserved - Returns true when PhysReg is a reserved register. 9610b57cec5SDimitry Andric /// 9620b57cec5SDimitry Andric /// Reserved registers may belong to an allocatable register class, but the 9630b57cec5SDimitry Andric /// target has explicitly requested that they are not used. 964e8d8bef9SDimitry Andric bool isReserved(MCRegister PhysReg) const { 9658bcb0991SDimitry Andric return getReservedRegs().test(PhysReg.id()); 9660b57cec5SDimitry Andric } 9670b57cec5SDimitry Andric 9680b57cec5SDimitry Andric /// Returns true when the given register unit is considered reserved. 9690b57cec5SDimitry Andric /// 9700b57cec5SDimitry Andric /// Register units are considered reserved when for at least one of their 9710b57cec5SDimitry Andric /// root registers, the root register and all super registers are reserved. 9720b57cec5SDimitry Andric /// This currently iterates the register hierarchy and may be slower than 9730b57cec5SDimitry Andric /// expected. 9740b57cec5SDimitry Andric bool isReservedRegUnit(unsigned Unit) const; 9750b57cec5SDimitry Andric 9760b57cec5SDimitry Andric /// isAllocatable - Returns true when PhysReg belongs to an allocatable 9770b57cec5SDimitry Andric /// register class and it hasn't been reserved. 9780b57cec5SDimitry Andric /// 9790b57cec5SDimitry Andric /// Allocatable registers may show up in the allocation order of some virtual 9800b57cec5SDimitry Andric /// register, so a register allocator needs to track its liveness and 9810b57cec5SDimitry Andric /// availability. 9825ffd83dbSDimitry Andric bool isAllocatable(MCRegister PhysReg) const { 9830b57cec5SDimitry Andric return getTargetRegisterInfo()->isInAllocatableClass(PhysReg) && 9840b57cec5SDimitry Andric !isReserved(PhysReg); 9850b57cec5SDimitry Andric } 9860b57cec5SDimitry Andric 9870b57cec5SDimitry Andric //===--------------------------------------------------------------------===// 9880b57cec5SDimitry Andric // LiveIn Management 9890b57cec5SDimitry Andric //===--------------------------------------------------------------------===// 9900b57cec5SDimitry Andric 9910b57cec5SDimitry Andric /// addLiveIn - Add the specified register as a live-in. Note that it 9920b57cec5SDimitry Andric /// is an error to add the same register to the same set more than once. 9935ffd83dbSDimitry Andric void addLiveIn(MCRegister Reg, Register vreg = Register()) { 9940b57cec5SDimitry Andric LiveIns.push_back(std::make_pair(Reg, vreg)); 9950b57cec5SDimitry Andric } 9960b57cec5SDimitry Andric 9970b57cec5SDimitry Andric // Iteration support for the live-ins set. It's kept in sorted order 9980b57cec5SDimitry Andric // by register number. 9990b57cec5SDimitry Andric using livein_iterator = 10005ffd83dbSDimitry Andric std::vector<std::pair<MCRegister,Register>>::const_iterator; 10010b57cec5SDimitry Andric livein_iterator livein_begin() const { return LiveIns.begin(); } 10020b57cec5SDimitry Andric livein_iterator livein_end() const { return LiveIns.end(); } 10030b57cec5SDimitry Andric bool livein_empty() const { return LiveIns.empty(); } 10040b57cec5SDimitry Andric 10055ffd83dbSDimitry Andric ArrayRef<std::pair<MCRegister, Register>> liveins() const { 10060b57cec5SDimitry Andric return LiveIns; 10070b57cec5SDimitry Andric } 10080b57cec5SDimitry Andric 10095ffd83dbSDimitry Andric bool isLiveIn(Register Reg) const; 10100b57cec5SDimitry Andric 10110b57cec5SDimitry Andric /// getLiveInPhysReg - If VReg is a live-in virtual register, return the 10120b57cec5SDimitry Andric /// corresponding live-in physical register. 10135ffd83dbSDimitry Andric MCRegister getLiveInPhysReg(Register VReg) const; 10140b57cec5SDimitry Andric 10150b57cec5SDimitry Andric /// getLiveInVirtReg - If PReg is a live-in physical register, return the 1016349cc55cSDimitry Andric /// corresponding live-in virtual register. 10175ffd83dbSDimitry Andric Register getLiveInVirtReg(MCRegister PReg) const; 10180b57cec5SDimitry Andric 10190b57cec5SDimitry Andric /// EmitLiveInCopies - Emit copies to initialize livein virtual registers 10200b57cec5SDimitry Andric /// into the given entry block. 10210b57cec5SDimitry Andric void EmitLiveInCopies(MachineBasicBlock *EntryMBB, 10220b57cec5SDimitry Andric const TargetRegisterInfo &TRI, 10230b57cec5SDimitry Andric const TargetInstrInfo &TII); 10240b57cec5SDimitry Andric 10250b57cec5SDimitry Andric /// Returns a mask covering all bits that can appear in lane masks of 10260b57cec5SDimitry Andric /// subregisters of the virtual register @p Reg. 10275ffd83dbSDimitry Andric LaneBitmask getMaxLaneMaskForVReg(Register Reg) const; 10280b57cec5SDimitry Andric 10290b57cec5SDimitry Andric /// defusechain_iterator - This class provides iterator support for machine 10300b57cec5SDimitry Andric /// operands in the function that use or define a specific register. If 10310b57cec5SDimitry Andric /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it 10320b57cec5SDimitry Andric /// returns defs. If neither are true then you are silly and it always 10330b57cec5SDimitry Andric /// returns end(). If SkipDebug is true it skips uses marked Debug 10340b57cec5SDimitry Andric /// when incrementing. 1035fe6060f1SDimitry Andric template <bool ReturnUses, bool ReturnDefs, bool SkipDebug, bool ByOperand, 1036fe6060f1SDimitry Andric bool ByInstr, bool ByBundle> 1037fe6060f1SDimitry Andric class defusechain_iterator { 10380b57cec5SDimitry Andric friend class MachineRegisterInfo; 10390b57cec5SDimitry Andric 1040fe6060f1SDimitry Andric public: 1041fe6060f1SDimitry Andric using iterator_category = std::forward_iterator_tag; 1042fe6060f1SDimitry Andric using value_type = MachineOperand; 1043fe6060f1SDimitry Andric using difference_type = std::ptrdiff_t; 1044fe6060f1SDimitry Andric using pointer = value_type *; 1045fe6060f1SDimitry Andric using reference = value_type &; 1046fe6060f1SDimitry Andric 1047fe6060f1SDimitry Andric private: 10480b57cec5SDimitry Andric MachineOperand *Op = nullptr; 10490b57cec5SDimitry Andric 10500b57cec5SDimitry Andric explicit defusechain_iterator(MachineOperand *op) : Op(op) { 10510b57cec5SDimitry Andric // If the first node isn't one we're interested in, advance to one that 10520b57cec5SDimitry Andric // we are interested in. 10530b57cec5SDimitry Andric if (op) { 10540b57cec5SDimitry Andric if ((!ReturnUses && op->isUse()) || 10550b57cec5SDimitry Andric (!ReturnDefs && op->isDef()) || 10560b57cec5SDimitry Andric (SkipDebug && op->isDebug())) 10570b57cec5SDimitry Andric advance(); 10580b57cec5SDimitry Andric } 10590b57cec5SDimitry Andric } 10600b57cec5SDimitry Andric 10610b57cec5SDimitry Andric void advance() { 10620b57cec5SDimitry Andric assert(Op && "Cannot increment end iterator!"); 10630b57cec5SDimitry Andric Op = getNextOperandForReg(Op); 10640b57cec5SDimitry Andric 10650b57cec5SDimitry Andric // All defs come before the uses, so stop def_iterator early. 10660b57cec5SDimitry Andric if (!ReturnUses) { 10670b57cec5SDimitry Andric if (Op) { 10680b57cec5SDimitry Andric if (Op->isUse()) 10690b57cec5SDimitry Andric Op = nullptr; 10700b57cec5SDimitry Andric else 10710b57cec5SDimitry Andric assert(!Op->isDebug() && "Can't have debug defs"); 10720b57cec5SDimitry Andric } 10730b57cec5SDimitry Andric } else { 10740b57cec5SDimitry Andric // If this is an operand we don't care about, skip it. 10750b57cec5SDimitry Andric while (Op && ((!ReturnDefs && Op->isDef()) || 10760b57cec5SDimitry Andric (SkipDebug && Op->isDebug()))) 10770b57cec5SDimitry Andric Op = getNextOperandForReg(Op); 10780b57cec5SDimitry Andric } 10790b57cec5SDimitry Andric } 10800b57cec5SDimitry Andric 10810b57cec5SDimitry Andric public: 10820b57cec5SDimitry Andric defusechain_iterator() = default; 10830b57cec5SDimitry Andric 10840b57cec5SDimitry Andric bool operator==(const defusechain_iterator &x) const { 10850b57cec5SDimitry Andric return Op == x.Op; 10860b57cec5SDimitry Andric } 10870b57cec5SDimitry Andric bool operator!=(const defusechain_iterator &x) const { 10880b57cec5SDimitry Andric return !operator==(x); 10890b57cec5SDimitry Andric } 10900b57cec5SDimitry Andric 10910b57cec5SDimitry Andric /// atEnd - return true if this iterator is equal to reg_end() on the value. 10920b57cec5SDimitry Andric bool atEnd() const { return Op == nullptr; } 10930b57cec5SDimitry Andric 10940b57cec5SDimitry Andric // Iterator traversal: forward iteration only 10950b57cec5SDimitry Andric defusechain_iterator &operator++() { // Preincrement 10960b57cec5SDimitry Andric assert(Op && "Cannot increment end iterator!"); 10970b57cec5SDimitry Andric if (ByOperand) 10980b57cec5SDimitry Andric advance(); 10990b57cec5SDimitry Andric else if (ByInstr) { 11000b57cec5SDimitry Andric MachineInstr *P = Op->getParent(); 11010b57cec5SDimitry Andric do { 11020b57cec5SDimitry Andric advance(); 11030b57cec5SDimitry Andric } while (Op && Op->getParent() == P); 11040b57cec5SDimitry Andric } else if (ByBundle) { 11050b57cec5SDimitry Andric MachineBasicBlock::instr_iterator P = 11060b57cec5SDimitry Andric getBundleStart(Op->getParent()->getIterator()); 11070b57cec5SDimitry Andric do { 11080b57cec5SDimitry Andric advance(); 11090b57cec5SDimitry Andric } while (Op && getBundleStart(Op->getParent()->getIterator()) == P); 11100b57cec5SDimitry Andric } 11110b57cec5SDimitry Andric 11120b57cec5SDimitry Andric return *this; 11130b57cec5SDimitry Andric } 11140b57cec5SDimitry Andric defusechain_iterator operator++(int) { // Postincrement 11150b57cec5SDimitry Andric defusechain_iterator tmp = *this; ++*this; return tmp; 11160b57cec5SDimitry Andric } 11170b57cec5SDimitry Andric 11180b57cec5SDimitry Andric /// getOperandNo - Return the operand # of this MachineOperand in its 11190b57cec5SDimitry Andric /// MachineInstr. 11200b57cec5SDimitry Andric unsigned getOperandNo() const { 11210b57cec5SDimitry Andric assert(Op && "Cannot dereference end iterator!"); 11220b57cec5SDimitry Andric return Op - &Op->getParent()->getOperand(0); 11230b57cec5SDimitry Andric } 11240b57cec5SDimitry Andric 11250b57cec5SDimitry Andric // Retrieve a reference to the current operand. 11260b57cec5SDimitry Andric MachineOperand &operator*() const { 11270b57cec5SDimitry Andric assert(Op && "Cannot dereference end iterator!"); 11280b57cec5SDimitry Andric return *Op; 11290b57cec5SDimitry Andric } 11300b57cec5SDimitry Andric 11310b57cec5SDimitry Andric MachineOperand *operator->() const { 11320b57cec5SDimitry Andric assert(Op && "Cannot dereference end iterator!"); 11330b57cec5SDimitry Andric return Op; 11340b57cec5SDimitry Andric } 11350b57cec5SDimitry Andric }; 11360b57cec5SDimitry Andric 11370b57cec5SDimitry Andric /// defusechain_iterator - This class provides iterator support for machine 11380b57cec5SDimitry Andric /// operands in the function that use or define a specific register. If 11390b57cec5SDimitry Andric /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it 11400b57cec5SDimitry Andric /// returns defs. If neither are true then you are silly and it always 11410b57cec5SDimitry Andric /// returns end(). If SkipDebug is true it skips uses marked Debug 11420b57cec5SDimitry Andric /// when incrementing. 1143fe6060f1SDimitry Andric template <bool ReturnUses, bool ReturnDefs, bool SkipDebug, bool ByOperand, 1144fe6060f1SDimitry Andric bool ByInstr, bool ByBundle> 1145fe6060f1SDimitry Andric class defusechain_instr_iterator { 11460b57cec5SDimitry Andric friend class MachineRegisterInfo; 11470b57cec5SDimitry Andric 1148fe6060f1SDimitry Andric public: 1149fe6060f1SDimitry Andric using iterator_category = std::forward_iterator_tag; 1150fe6060f1SDimitry Andric using value_type = MachineInstr; 1151fe6060f1SDimitry Andric using difference_type = std::ptrdiff_t; 1152fe6060f1SDimitry Andric using pointer = value_type *; 1153fe6060f1SDimitry Andric using reference = value_type &; 1154fe6060f1SDimitry Andric 1155fe6060f1SDimitry Andric private: 11560b57cec5SDimitry Andric MachineOperand *Op = nullptr; 11570b57cec5SDimitry Andric 11580b57cec5SDimitry Andric explicit defusechain_instr_iterator(MachineOperand *op) : Op(op) { 11590b57cec5SDimitry Andric // If the first node isn't one we're interested in, advance to one that 11600b57cec5SDimitry Andric // we are interested in. 11610b57cec5SDimitry Andric if (op) { 11620b57cec5SDimitry Andric if ((!ReturnUses && op->isUse()) || 11630b57cec5SDimitry Andric (!ReturnDefs && op->isDef()) || 11640b57cec5SDimitry Andric (SkipDebug && op->isDebug())) 11650b57cec5SDimitry Andric advance(); 11660b57cec5SDimitry Andric } 11670b57cec5SDimitry Andric } 11680b57cec5SDimitry Andric 11690b57cec5SDimitry Andric void advance() { 11700b57cec5SDimitry Andric assert(Op && "Cannot increment end iterator!"); 11710b57cec5SDimitry Andric Op = getNextOperandForReg(Op); 11720b57cec5SDimitry Andric 11730b57cec5SDimitry Andric // All defs come before the uses, so stop def_iterator early. 11740b57cec5SDimitry Andric if (!ReturnUses) { 11750b57cec5SDimitry Andric if (Op) { 11760b57cec5SDimitry Andric if (Op->isUse()) 11770b57cec5SDimitry Andric Op = nullptr; 11780b57cec5SDimitry Andric else 11790b57cec5SDimitry Andric assert(!Op->isDebug() && "Can't have debug defs"); 11800b57cec5SDimitry Andric } 11810b57cec5SDimitry Andric } else { 11820b57cec5SDimitry Andric // If this is an operand we don't care about, skip it. 11830b57cec5SDimitry Andric while (Op && ((!ReturnDefs && Op->isDef()) || 11840b57cec5SDimitry Andric (SkipDebug && Op->isDebug()))) 11850b57cec5SDimitry Andric Op = getNextOperandForReg(Op); 11860b57cec5SDimitry Andric } 11870b57cec5SDimitry Andric } 11880b57cec5SDimitry Andric 11890b57cec5SDimitry Andric public: 11900b57cec5SDimitry Andric defusechain_instr_iterator() = default; 11910b57cec5SDimitry Andric 11920b57cec5SDimitry Andric bool operator==(const defusechain_instr_iterator &x) const { 11930b57cec5SDimitry Andric return Op == x.Op; 11940b57cec5SDimitry Andric } 11950b57cec5SDimitry Andric bool operator!=(const defusechain_instr_iterator &x) const { 11960b57cec5SDimitry Andric return !operator==(x); 11970b57cec5SDimitry Andric } 11980b57cec5SDimitry Andric 11990b57cec5SDimitry Andric /// atEnd - return true if this iterator is equal to reg_end() on the value. 12000b57cec5SDimitry Andric bool atEnd() const { return Op == nullptr; } 12010b57cec5SDimitry Andric 12020b57cec5SDimitry Andric // Iterator traversal: forward iteration only 12030b57cec5SDimitry Andric defusechain_instr_iterator &operator++() { // Preincrement 12040b57cec5SDimitry Andric assert(Op && "Cannot increment end iterator!"); 12050b57cec5SDimitry Andric if (ByOperand) 12060b57cec5SDimitry Andric advance(); 12070b57cec5SDimitry Andric else if (ByInstr) { 12080b57cec5SDimitry Andric MachineInstr *P = Op->getParent(); 12090b57cec5SDimitry Andric do { 12100b57cec5SDimitry Andric advance(); 12110b57cec5SDimitry Andric } while (Op && Op->getParent() == P); 12120b57cec5SDimitry Andric } else if (ByBundle) { 12130b57cec5SDimitry Andric MachineBasicBlock::instr_iterator P = 12140b57cec5SDimitry Andric getBundleStart(Op->getParent()->getIterator()); 12150b57cec5SDimitry Andric do { 12160b57cec5SDimitry Andric advance(); 12170b57cec5SDimitry Andric } while (Op && getBundleStart(Op->getParent()->getIterator()) == P); 12180b57cec5SDimitry Andric } 12190b57cec5SDimitry Andric 12200b57cec5SDimitry Andric return *this; 12210b57cec5SDimitry Andric } 12220b57cec5SDimitry Andric defusechain_instr_iterator operator++(int) { // Postincrement 12230b57cec5SDimitry Andric defusechain_instr_iterator tmp = *this; ++*this; return tmp; 12240b57cec5SDimitry Andric } 12250b57cec5SDimitry Andric 12260b57cec5SDimitry Andric // Retrieve a reference to the current operand. 12270b57cec5SDimitry Andric MachineInstr &operator*() const { 12280b57cec5SDimitry Andric assert(Op && "Cannot dereference end iterator!"); 12290b57cec5SDimitry Andric if (ByBundle) 12300b57cec5SDimitry Andric return *getBundleStart(Op->getParent()->getIterator()); 12310b57cec5SDimitry Andric return *Op->getParent(); 12320b57cec5SDimitry Andric } 12330b57cec5SDimitry Andric 12340b57cec5SDimitry Andric MachineInstr *operator->() const { return &operator*(); } 12350b57cec5SDimitry Andric }; 12360b57cec5SDimitry Andric }; 12370b57cec5SDimitry Andric 12380b57cec5SDimitry Andric /// Iterate over the pressure sets affected by the given physical or virtual 12390b57cec5SDimitry Andric /// register. If Reg is physical, it must be a register unit (from 12400b57cec5SDimitry Andric /// MCRegUnitIterator). 12410b57cec5SDimitry Andric class PSetIterator { 12420b57cec5SDimitry Andric const int *PSet = nullptr; 12430b57cec5SDimitry Andric unsigned Weight = 0; 12440b57cec5SDimitry Andric 12450b57cec5SDimitry Andric public: 12460b57cec5SDimitry Andric PSetIterator() = default; 12470b57cec5SDimitry Andric 1248e8d8bef9SDimitry Andric PSetIterator(Register RegUnit, const MachineRegisterInfo *MRI) { 12490b57cec5SDimitry Andric const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo(); 1250e8d8bef9SDimitry Andric if (RegUnit.isVirtual()) { 12510b57cec5SDimitry Andric const TargetRegisterClass *RC = MRI->getRegClass(RegUnit); 12520b57cec5SDimitry Andric PSet = TRI->getRegClassPressureSets(RC); 12530b57cec5SDimitry Andric Weight = TRI->getRegClassWeight(RC).RegWeight; 1254e8d8bef9SDimitry Andric } else { 12550b57cec5SDimitry Andric PSet = TRI->getRegUnitPressureSets(RegUnit); 12560b57cec5SDimitry Andric Weight = TRI->getRegUnitWeight(RegUnit); 12570b57cec5SDimitry Andric } 12580b57cec5SDimitry Andric if (*PSet == -1) 12590b57cec5SDimitry Andric PSet = nullptr; 12600b57cec5SDimitry Andric } 12610b57cec5SDimitry Andric 12620b57cec5SDimitry Andric bool isValid() const { return PSet; } 12630b57cec5SDimitry Andric 12640b57cec5SDimitry Andric unsigned getWeight() const { return Weight; } 12650b57cec5SDimitry Andric 12660b57cec5SDimitry Andric unsigned operator*() const { return *PSet; } 12670b57cec5SDimitry Andric 12680b57cec5SDimitry Andric void operator++() { 12690b57cec5SDimitry Andric assert(isValid() && "Invalid PSetIterator."); 12700b57cec5SDimitry Andric ++PSet; 12710b57cec5SDimitry Andric if (*PSet == -1) 12720b57cec5SDimitry Andric PSet = nullptr; 12730b57cec5SDimitry Andric } 12740b57cec5SDimitry Andric }; 12750b57cec5SDimitry Andric 1276e8d8bef9SDimitry Andric inline PSetIterator 1277e8d8bef9SDimitry Andric MachineRegisterInfo::getPressureSets(Register RegUnit) const { 12780b57cec5SDimitry Andric return PSetIterator(RegUnit, this); 12790b57cec5SDimitry Andric } 12800b57cec5SDimitry Andric 12810b57cec5SDimitry Andric } // end namespace llvm 12820b57cec5SDimitry Andric 12830b57cec5SDimitry Andric #endif // LLVM_CODEGEN_MACHINEREGISTERINFO_H 1284