1 //===- LiveRegUnits.cpp - Register Unit Set -------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 /// \file This file imlements the LiveRegUnits set. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/CodeGen/LiveRegUnits.h" 15 #include "llvm/CodeGen/MachineBasicBlock.h" 16 #include "llvm/CodeGen/MachineFrameInfo.h" 17 #include "llvm/CodeGen/MachineFunction.h" 18 #include "llvm/CodeGen/MachineInstrBundle.h" 19 #include "llvm/CodeGen/MachineOperand.h" 20 #include "llvm/MC/MCRegisterInfo.h" 21 #include "llvm/Target/TargetRegisterInfo.h" 22 23 using namespace llvm; 24 25 void LiveRegUnits::removeRegsNotPreserved(const uint32_t *RegMask) { 26 for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) { 27 for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) { 28 if (MachineOperand::clobbersPhysReg(RegMask, *RootReg)) 29 Units.reset(U); 30 } 31 } 32 } 33 34 void LiveRegUnits::addRegsInMask(const uint32_t *RegMask) { 35 for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) { 36 for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) { 37 if (MachineOperand::clobbersPhysReg(RegMask, *RootReg)) 38 Units.set(U); 39 } 40 } 41 } 42 43 void LiveRegUnits::stepBackward(const MachineInstr &MI) { 44 // Remove defined registers and regmask kills from the set. 45 for (ConstMIBundleOperands O(MI); O.isValid(); ++O) { 46 if (O->isReg()) { 47 if (!O->isDef()) 48 continue; 49 unsigned Reg = O->getReg(); 50 if (!TargetRegisterInfo::isPhysicalRegister(Reg)) 51 continue; 52 removeReg(Reg); 53 } else if (O->isRegMask()) 54 removeRegsNotPreserved(O->getRegMask()); 55 } 56 57 // Add uses to the set. 58 for (ConstMIBundleOperands O(MI); O.isValid(); ++O) { 59 if (!O->isReg() || !O->readsReg()) 60 continue; 61 unsigned Reg = O->getReg(); 62 if (!TargetRegisterInfo::isPhysicalRegister(Reg)) 63 continue; 64 addReg(Reg); 65 } 66 } 67 68 void LiveRegUnits::accumulateBackward(const MachineInstr &MI) { 69 // Add defs, uses and regmask clobbers to the set. 70 for (ConstMIBundleOperands O(MI); O.isValid(); ++O) { 71 if (O->isReg()) { 72 unsigned Reg = O->getReg(); 73 if (!TargetRegisterInfo::isPhysicalRegister(Reg)) 74 continue; 75 if (!O->isDef() && !O->readsReg()) 76 continue; 77 addReg(Reg); 78 } else if (O->isRegMask()) 79 addRegsInMask(O->getRegMask()); 80 } 81 } 82 83 /// Add live-in registers of basic block \p MBB to \p LiveUnits. 84 static void addLiveIns(LiveRegUnits &LiveUnits, const MachineBasicBlock &MBB) { 85 for (const auto &LI : MBB.liveins()) 86 LiveUnits.addRegMasked(LI.PhysReg, LI.LaneMask); 87 } 88 89 static void addLiveOuts(LiveRegUnits &LiveUnits, const MachineBasicBlock &MBB) { 90 // To get the live-outs we simply merge the live-ins of all successors. 91 for (const MachineBasicBlock *Succ : MBB.successors()) 92 addLiveIns(LiveUnits, *Succ); 93 } 94 95 /// Add pristine registers to the given \p LiveUnits. This function removes 96 /// actually saved callee save registers when \p InPrologueEpilogue is false. 97 static void removeSavedRegs(LiveRegUnits &LiveUnits, const MachineFunction &MF, 98 const MachineFrameInfo &MFI, 99 const TargetRegisterInfo &TRI) { 100 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo()) 101 LiveUnits.removeReg(Info.getReg()); 102 } 103 104 void LiveRegUnits::addLiveOuts(const MachineBasicBlock &MBB) { 105 const MachineFunction &MF = *MBB.getParent(); 106 const MachineFrameInfo &MFI = MF.getFrameInfo(); 107 if (MFI.isCalleeSavedInfoValid()) { 108 for (const MCPhysReg *I = TRI->getCalleeSavedRegs(&MF); *I; ++I) 109 addReg(*I); 110 if (!MBB.isReturnBlock()) 111 removeSavedRegs(*this, MF, MFI, *TRI); 112 } 113 ::addLiveOuts(*this, MBB); 114 } 115 116 void LiveRegUnits::addLiveIns(const MachineBasicBlock &MBB) { 117 const MachineFunction &MF = *MBB.getParent(); 118 const MachineFrameInfo &MFI = MF.getFrameInfo(); 119 if (MFI.isCalleeSavedInfoValid()) { 120 for (const MCPhysReg *I = TRI->getCalleeSavedRegs(&MF); *I; ++I) 121 addReg(*I); 122 if (&MBB != &MF.front()) 123 removeSavedRegs(*this, MF, MFI, *TRI); 124 } 125 ::addLiveIns(*this, MBB); 126 } 127