10b57cec5SDimitry Andric //===- llvm/CodeGen/VirtRegMap.cpp - Virtual Register Map -----------------===// 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 implements the VirtRegMap class. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric // It also contains implementations of the Spiller interface, which, given a 120b57cec5SDimitry Andric // virtual register map and a machine function, eliminates all virtual 130b57cec5SDimitry Andric // references by replacing them with physical register references - adding spill 140b57cec5SDimitry Andric // code as necessary. 150b57cec5SDimitry Andric // 160b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 170b57cec5SDimitry Andric 180b57cec5SDimitry Andric #include "llvm/CodeGen/VirtRegMap.h" 190b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 200b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 21*0fca6ea1SDimitry Andric #include "llvm/CodeGen/LiveDebugVariables.h" 220b57cec5SDimitry Andric #include "llvm/CodeGen/LiveInterval.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/LiveStacks.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 280b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 300b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 310b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 320b57cec5SDimitry Andric #include "llvm/CodeGen/SlotIndexes.h" 33fe6060f1SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h" 340b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 350b57cec5SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h" 360b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 370b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 380b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h" 390b57cec5SDimitry Andric #include "llvm/MC/LaneBitmask.h" 400b57cec5SDimitry Andric #include "llvm/Pass.h" 410b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 420b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 430b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 440b57cec5SDimitry Andric #include <cassert> 450b57cec5SDimitry Andric #include <iterator> 460b57cec5SDimitry Andric #include <utility> 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric using namespace llvm; 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric #define DEBUG_TYPE "regalloc" 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric STATISTIC(NumSpillSlots, "Number of spill slots allocated"); 530b57cec5SDimitry Andric STATISTIC(NumIdCopies, "Number of identity moves eliminated after rewriting"); 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 560b57cec5SDimitry Andric // VirtRegMap implementation 570b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric char VirtRegMap::ID = 0; 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric INITIALIZE_PASS(VirtRegMap, "virtregmap", "Virtual Register Map", false, false) 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric bool VirtRegMap::runOnMachineFunction(MachineFunction &mf) { 640b57cec5SDimitry Andric MRI = &mf.getRegInfo(); 650b57cec5SDimitry Andric TII = mf.getSubtarget().getInstrInfo(); 660b57cec5SDimitry Andric TRI = mf.getSubtarget().getRegisterInfo(); 670b57cec5SDimitry Andric MF = &mf; 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric Virt2PhysMap.clear(); 700b57cec5SDimitry Andric Virt2StackSlotMap.clear(); 710b57cec5SDimitry Andric Virt2SplitMap.clear(); 72e8d8bef9SDimitry Andric Virt2ShapeMap.clear(); 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric grow(); 750b57cec5SDimitry Andric return false; 760b57cec5SDimitry Andric } 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric void VirtRegMap::grow() { 790b57cec5SDimitry Andric unsigned NumRegs = MF->getRegInfo().getNumVirtRegs(); 800b57cec5SDimitry Andric Virt2PhysMap.resize(NumRegs); 810b57cec5SDimitry Andric Virt2StackSlotMap.resize(NumRegs); 820b57cec5SDimitry Andric Virt2SplitMap.resize(NumRegs); 830b57cec5SDimitry Andric } 840b57cec5SDimitry Andric 858bcb0991SDimitry Andric void VirtRegMap::assignVirt2Phys(Register virtReg, MCPhysReg physReg) { 868bcb0991SDimitry Andric assert(virtReg.isVirtual() && Register::isPhysicalRegister(physReg)); 878bcb0991SDimitry Andric assert(Virt2PhysMap[virtReg.id()] == NO_PHYS_REG && 880b57cec5SDimitry Andric "attempt to assign physical register to already mapped " 890b57cec5SDimitry Andric "virtual register"); 900b57cec5SDimitry Andric assert(!getRegInfo().isReserved(physReg) && 910b57cec5SDimitry Andric "Attempt to map virtReg to a reserved physReg"); 928bcb0991SDimitry Andric Virt2PhysMap[virtReg.id()] = physReg; 930b57cec5SDimitry Andric } 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric unsigned VirtRegMap::createSpillSlot(const TargetRegisterClass *RC) { 960b57cec5SDimitry Andric unsigned Size = TRI->getSpillSize(*RC); 975ffd83dbSDimitry Andric Align Alignment = TRI->getSpillAlign(*RC); 98fe6060f1SDimitry Andric // Set preferred alignment if we are still able to realign the stack 99fe6060f1SDimitry Andric auto &ST = MF->getSubtarget(); 100fe6060f1SDimitry Andric Align CurrentAlign = ST.getFrameLowering()->getStackAlign(); 101fe6060f1SDimitry Andric if (Alignment > CurrentAlign && !ST.getRegisterInfo()->canRealignStack(*MF)) { 102fe6060f1SDimitry Andric Alignment = CurrentAlign; 103fe6060f1SDimitry Andric } 1045ffd83dbSDimitry Andric int SS = MF->getFrameInfo().CreateSpillStackObject(Size, Alignment); 1050b57cec5SDimitry Andric ++NumSpillSlots; 1060b57cec5SDimitry Andric return SS; 1070b57cec5SDimitry Andric } 1080b57cec5SDimitry Andric 109fe6060f1SDimitry Andric bool VirtRegMap::hasPreferredPhys(Register VirtReg) const { 1108bcb0991SDimitry Andric Register Hint = MRI->getSimpleHint(VirtReg); 1118bcb0991SDimitry Andric if (!Hint.isValid()) 1120b57cec5SDimitry Andric return false; 1138bcb0991SDimitry Andric if (Hint.isVirtual()) 1140b57cec5SDimitry Andric Hint = getPhys(Hint); 115e8d8bef9SDimitry Andric return Register(getPhys(VirtReg)) == Hint; 1160b57cec5SDimitry Andric } 1170b57cec5SDimitry Andric 118fe6060f1SDimitry Andric bool VirtRegMap::hasKnownPreference(Register VirtReg) const { 11906c3fb27SDimitry Andric std::pair<unsigned, Register> Hint = MRI->getRegAllocationHint(VirtReg); 12006c3fb27SDimitry Andric if (Hint.second.isPhysical()) 1210b57cec5SDimitry Andric return true; 12206c3fb27SDimitry Andric if (Hint.second.isVirtual()) 1230b57cec5SDimitry Andric return hasPhys(Hint.second); 1240b57cec5SDimitry Andric return false; 1250b57cec5SDimitry Andric } 1260b57cec5SDimitry Andric 1278bcb0991SDimitry Andric int VirtRegMap::assignVirt2StackSlot(Register virtReg) { 1288bcb0991SDimitry Andric assert(virtReg.isVirtual()); 1298bcb0991SDimitry Andric assert(Virt2StackSlotMap[virtReg.id()] == NO_STACK_SLOT && 1300b57cec5SDimitry Andric "attempt to assign stack slot to already spilled register"); 1310b57cec5SDimitry Andric const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtReg); 1328bcb0991SDimitry Andric return Virt2StackSlotMap[virtReg.id()] = createSpillSlot(RC); 1330b57cec5SDimitry Andric } 1340b57cec5SDimitry Andric 1358bcb0991SDimitry Andric void VirtRegMap::assignVirt2StackSlot(Register virtReg, int SS) { 1368bcb0991SDimitry Andric assert(virtReg.isVirtual()); 1378bcb0991SDimitry Andric assert(Virt2StackSlotMap[virtReg.id()] == NO_STACK_SLOT && 1380b57cec5SDimitry Andric "attempt to assign stack slot to already spilled register"); 1390b57cec5SDimitry Andric assert((SS >= 0 || 1400b57cec5SDimitry Andric (SS >= MF->getFrameInfo().getObjectIndexBegin())) && 1410b57cec5SDimitry Andric "illegal fixed frame index"); 1428bcb0991SDimitry Andric Virt2StackSlotMap[virtReg.id()] = SS; 1430b57cec5SDimitry Andric } 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric void VirtRegMap::print(raw_ostream &OS, const Module*) const { 1460b57cec5SDimitry Andric OS << "********** REGISTER MAP **********\n"; 1470b57cec5SDimitry Andric for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 148bdd1243dSDimitry Andric Register Reg = Register::index2VirtReg(i); 1490b57cec5SDimitry Andric if (Virt2PhysMap[Reg] != (unsigned)VirtRegMap::NO_PHYS_REG) { 1500b57cec5SDimitry Andric OS << '[' << printReg(Reg, TRI) << " -> " 1510b57cec5SDimitry Andric << printReg(Virt2PhysMap[Reg], TRI) << "] " 1520b57cec5SDimitry Andric << TRI->getRegClassName(MRI->getRegClass(Reg)) << "\n"; 1530b57cec5SDimitry Andric } 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 157bdd1243dSDimitry Andric Register Reg = Register::index2VirtReg(i); 1580b57cec5SDimitry Andric if (Virt2StackSlotMap[Reg] != VirtRegMap::NO_STACK_SLOT) { 1590b57cec5SDimitry Andric OS << '[' << printReg(Reg, TRI) << " -> fi#" << Virt2StackSlotMap[Reg] 1600b57cec5SDimitry Andric << "] " << TRI->getRegClassName(MRI->getRegClass(Reg)) << "\n"; 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric } 1630b57cec5SDimitry Andric OS << '\n'; 1640b57cec5SDimitry Andric } 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1670b57cec5SDimitry Andric LLVM_DUMP_METHOD void VirtRegMap::dump() const { 1680b57cec5SDimitry Andric print(dbgs()); 1690b57cec5SDimitry Andric } 1700b57cec5SDimitry Andric #endif 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1730b57cec5SDimitry Andric // VirtRegRewriter 1740b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1750b57cec5SDimitry Andric // 1760b57cec5SDimitry Andric // The VirtRegRewriter is the last of the register allocator passes. 1770b57cec5SDimitry Andric // It rewrites virtual registers to physical registers as specified in the 1780b57cec5SDimitry Andric // VirtRegMap analysis. It also updates live-in information on basic blocks 1790b57cec5SDimitry Andric // according to LiveIntervals. 1800b57cec5SDimitry Andric // 1810b57cec5SDimitry Andric namespace { 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric class VirtRegRewriter : public MachineFunctionPass { 18406c3fb27SDimitry Andric MachineFunction *MF = nullptr; 18506c3fb27SDimitry Andric const TargetRegisterInfo *TRI = nullptr; 18606c3fb27SDimitry Andric const TargetInstrInfo *TII = nullptr; 18706c3fb27SDimitry Andric MachineRegisterInfo *MRI = nullptr; 18806c3fb27SDimitry Andric SlotIndexes *Indexes = nullptr; 18906c3fb27SDimitry Andric LiveIntervals *LIS = nullptr; 19006c3fb27SDimitry Andric VirtRegMap *VRM = nullptr; 19106c3fb27SDimitry Andric LiveDebugVariables *DebugVars = nullptr; 192fe6060f1SDimitry Andric DenseSet<Register> RewriteRegs; 193fe6060f1SDimitry Andric bool ClearVirtRegs; 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric void rewrite(); 1960b57cec5SDimitry Andric void addMBBLiveIns(); 1970b57cec5SDimitry Andric bool readsUndefSubreg(const MachineOperand &MO) const; 198fe6060f1SDimitry Andric void addLiveInsForSubRanges(const LiveInterval &LI, MCRegister PhysReg) const; 199fe6060f1SDimitry Andric void handleIdentityCopy(MachineInstr &MI); 2000b57cec5SDimitry Andric void expandCopyBundle(MachineInstr &MI) const; 201e8d8bef9SDimitry Andric bool subRegLiveThrough(const MachineInstr &MI, MCRegister SuperPhysReg) const; 2020b57cec5SDimitry Andric 2030b57cec5SDimitry Andric public: 2040b57cec5SDimitry Andric static char ID; 205fe6060f1SDimitry Andric VirtRegRewriter(bool ClearVirtRegs_ = true) : 206fe6060f1SDimitry Andric MachineFunctionPass(ID), 207fe6060f1SDimitry Andric ClearVirtRegs(ClearVirtRegs_) {} 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override; 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction&) override; 2120b57cec5SDimitry Andric 2130b57cec5SDimitry Andric MachineFunctionProperties getSetProperties() const override { 214fe6060f1SDimitry Andric if (ClearVirtRegs) { 2150b57cec5SDimitry Andric return MachineFunctionProperties().set( 2160b57cec5SDimitry Andric MachineFunctionProperties::Property::NoVRegs); 2170b57cec5SDimitry Andric } 218fe6060f1SDimitry Andric 219fe6060f1SDimitry Andric return MachineFunctionProperties(); 220fe6060f1SDimitry Andric } 2210b57cec5SDimitry Andric }; 2220b57cec5SDimitry Andric 2230b57cec5SDimitry Andric } // end anonymous namespace 2240b57cec5SDimitry Andric 2250b57cec5SDimitry Andric char VirtRegRewriter::ID = 0; 2260b57cec5SDimitry Andric 2270b57cec5SDimitry Andric char &llvm::VirtRegRewriterID = VirtRegRewriter::ID; 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(VirtRegRewriter, "virtregrewriter", 2300b57cec5SDimitry Andric "Virtual Register Rewriter", false, false) 231*0fca6ea1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass) 232*0fca6ea1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass) 2330b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables) 2340b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LiveStacks) 2350b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(VirtRegMap) 2360b57cec5SDimitry Andric INITIALIZE_PASS_END(VirtRegRewriter, "virtregrewriter", 2370b57cec5SDimitry Andric "Virtual Register Rewriter", false, false) 2380b57cec5SDimitry Andric 2390b57cec5SDimitry Andric void VirtRegRewriter::getAnalysisUsage(AnalysisUsage &AU) const { 2400b57cec5SDimitry Andric AU.setPreservesCFG(); 241*0fca6ea1SDimitry Andric AU.addRequired<LiveIntervalsWrapperPass>(); 242*0fca6ea1SDimitry Andric AU.addPreserved<LiveIntervalsWrapperPass>(); 243*0fca6ea1SDimitry Andric AU.addRequired<SlotIndexesWrapperPass>(); 244*0fca6ea1SDimitry Andric AU.addPreserved<SlotIndexesWrapperPass>(); 2450b57cec5SDimitry Andric AU.addRequired<LiveDebugVariables>(); 2460b57cec5SDimitry Andric AU.addRequired<LiveStacks>(); 2470b57cec5SDimitry Andric AU.addPreserved<LiveStacks>(); 2480b57cec5SDimitry Andric AU.addRequired<VirtRegMap>(); 249fe6060f1SDimitry Andric 250fe6060f1SDimitry Andric if (!ClearVirtRegs) 251fe6060f1SDimitry Andric AU.addPreserved<LiveDebugVariables>(); 252fe6060f1SDimitry Andric 2530b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 2540b57cec5SDimitry Andric } 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric bool VirtRegRewriter::runOnMachineFunction(MachineFunction &fn) { 2570b57cec5SDimitry Andric MF = &fn; 2580b57cec5SDimitry Andric TRI = MF->getSubtarget().getRegisterInfo(); 2590b57cec5SDimitry Andric TII = MF->getSubtarget().getInstrInfo(); 2600b57cec5SDimitry Andric MRI = &MF->getRegInfo(); 261*0fca6ea1SDimitry Andric Indexes = &getAnalysis<SlotIndexesWrapperPass>().getSI(); 262*0fca6ea1SDimitry Andric LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS(); 2630b57cec5SDimitry Andric VRM = &getAnalysis<VirtRegMap>(); 2645f757f3fSDimitry Andric DebugVars = &getAnalysis<LiveDebugVariables>(); 2650b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "********** REWRITE VIRTUAL REGISTERS **********\n" 2660b57cec5SDimitry Andric << "********** Function: " << MF->getName() << '\n'); 2670b57cec5SDimitry Andric LLVM_DEBUG(VRM->dump()); 2680b57cec5SDimitry Andric 2690b57cec5SDimitry Andric // Add kill flags while we still have virtual registers. 2700b57cec5SDimitry Andric LIS->addKillFlags(VRM); 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric // Live-in lists on basic blocks are required for physregs. 2730b57cec5SDimitry Andric addMBBLiveIns(); 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric // Rewrite virtual registers. 2760b57cec5SDimitry Andric rewrite(); 2770b57cec5SDimitry Andric 2785f757f3fSDimitry Andric if (ClearVirtRegs) { 2790b57cec5SDimitry Andric // Write out new DBG_VALUE instructions. 280fe6060f1SDimitry Andric 281fe6060f1SDimitry Andric // We only do this if ClearVirtRegs is specified since this should be the 282fe6060f1SDimitry Andric // final run of the pass and we don't want to emit them multiple times. 283fe6060f1SDimitry Andric DebugVars->emitDebugValues(VRM); 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric // All machine operands and other references to virtual registers have been 2860b57cec5SDimitry Andric // replaced. Remove the virtual registers and release all the transient data. 2870b57cec5SDimitry Andric VRM->clearAllVirt(); 2880b57cec5SDimitry Andric MRI->clearVirtRegs(); 289fe6060f1SDimitry Andric } 290fe6060f1SDimitry Andric 2910b57cec5SDimitry Andric return true; 2920b57cec5SDimitry Andric } 2930b57cec5SDimitry Andric 2940b57cec5SDimitry Andric void VirtRegRewriter::addLiveInsForSubRanges(const LiveInterval &LI, 295fe6060f1SDimitry Andric MCRegister PhysReg) const { 2960b57cec5SDimitry Andric assert(!LI.empty()); 2970b57cec5SDimitry Andric assert(LI.hasSubRanges()); 2980b57cec5SDimitry Andric 2990b57cec5SDimitry Andric using SubRangeIteratorPair = 3000b57cec5SDimitry Andric std::pair<const LiveInterval::SubRange *, LiveInterval::const_iterator>; 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric SmallVector<SubRangeIteratorPair, 4> SubRanges; 3030b57cec5SDimitry Andric SlotIndex First; 3040b57cec5SDimitry Andric SlotIndex Last; 3050b57cec5SDimitry Andric for (const LiveInterval::SubRange &SR : LI.subranges()) { 3060b57cec5SDimitry Andric SubRanges.push_back(std::make_pair(&SR, SR.begin())); 3070b57cec5SDimitry Andric if (!First.isValid() || SR.segments.front().start < First) 3080b57cec5SDimitry Andric First = SR.segments.front().start; 3090b57cec5SDimitry Andric if (!Last.isValid() || SR.segments.back().end > Last) 3100b57cec5SDimitry Andric Last = SR.segments.back().end; 3110b57cec5SDimitry Andric } 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric // Check all mbb start positions between First and Last while 3145f757f3fSDimitry Andric // simultaneously advancing an iterator for each subrange. 3155f757f3fSDimitry Andric for (SlotIndexes::MBBIndexIterator MBBI = Indexes->getMBBLowerBound(First); 3160b57cec5SDimitry Andric MBBI != Indexes->MBBIndexEnd() && MBBI->first <= Last; ++MBBI) { 3170b57cec5SDimitry Andric SlotIndex MBBBegin = MBBI->first; 3180b57cec5SDimitry Andric // Advance all subrange iterators so that their end position is just 3190b57cec5SDimitry Andric // behind MBBBegin (or the iterator is at the end). 3200b57cec5SDimitry Andric LaneBitmask LaneMask; 3210b57cec5SDimitry Andric for (auto &RangeIterPair : SubRanges) { 3220b57cec5SDimitry Andric const LiveInterval::SubRange *SR = RangeIterPair.first; 3230b57cec5SDimitry Andric LiveInterval::const_iterator &SRI = RangeIterPair.second; 3240b57cec5SDimitry Andric while (SRI != SR->end() && SRI->end <= MBBBegin) 3250b57cec5SDimitry Andric ++SRI; 3260b57cec5SDimitry Andric if (SRI == SR->end()) 3270b57cec5SDimitry Andric continue; 3280b57cec5SDimitry Andric if (SRI->start <= MBBBegin) 3290b57cec5SDimitry Andric LaneMask |= SR->LaneMask; 3300b57cec5SDimitry Andric } 3310b57cec5SDimitry Andric if (LaneMask.none()) 3320b57cec5SDimitry Andric continue; 3330b57cec5SDimitry Andric MachineBasicBlock *MBB = MBBI->second; 3340b57cec5SDimitry Andric MBB->addLiveIn(PhysReg, LaneMask); 3350b57cec5SDimitry Andric } 3360b57cec5SDimitry Andric } 3370b57cec5SDimitry Andric 3380b57cec5SDimitry Andric // Compute MBB live-in lists from virtual register live ranges and their 3390b57cec5SDimitry Andric // assignments. 3400b57cec5SDimitry Andric void VirtRegRewriter::addMBBLiveIns() { 3410b57cec5SDimitry Andric for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) { 3428bcb0991SDimitry Andric Register VirtReg = Register::index2VirtReg(Idx); 3430b57cec5SDimitry Andric if (MRI->reg_nodbg_empty(VirtReg)) 3440b57cec5SDimitry Andric continue; 3450b57cec5SDimitry Andric LiveInterval &LI = LIS->getInterval(VirtReg); 3460b57cec5SDimitry Andric if (LI.empty() || LIS->intervalIsInOneMBB(LI)) 3470b57cec5SDimitry Andric continue; 3480b57cec5SDimitry Andric // This is a virtual register that is live across basic blocks. Its 3490b57cec5SDimitry Andric // assigned PhysReg must be marked as live-in to those blocks. 3508bcb0991SDimitry Andric Register PhysReg = VRM->getPhys(VirtReg); 351fe6060f1SDimitry Andric if (PhysReg == VirtRegMap::NO_PHYS_REG) { 352fe6060f1SDimitry Andric // There may be no physical register assigned if only some register 353fe6060f1SDimitry Andric // classes were already allocated. 354fe6060f1SDimitry Andric assert(!ClearVirtRegs && "Unmapped virtual register"); 355fe6060f1SDimitry Andric continue; 356fe6060f1SDimitry Andric } 3570b57cec5SDimitry Andric 3580b57cec5SDimitry Andric if (LI.hasSubRanges()) { 3590b57cec5SDimitry Andric addLiveInsForSubRanges(LI, PhysReg); 3600b57cec5SDimitry Andric } else { 3610b57cec5SDimitry Andric // Go over MBB begin positions and see if we have segments covering them. 3620b57cec5SDimitry Andric // The following works because segments and the MBBIndex list are both 3630b57cec5SDimitry Andric // sorted by slot indexes. 3640b57cec5SDimitry Andric SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(); 3650b57cec5SDimitry Andric for (const auto &Seg : LI) { 3665f757f3fSDimitry Andric I = Indexes->getMBBLowerBound(I, Seg.start); 3670b57cec5SDimitry Andric for (; I != Indexes->MBBIndexEnd() && I->first < Seg.end; ++I) { 3680b57cec5SDimitry Andric MachineBasicBlock *MBB = I->second; 3690b57cec5SDimitry Andric MBB->addLiveIn(PhysReg); 3700b57cec5SDimitry Andric } 3710b57cec5SDimitry Andric } 3720b57cec5SDimitry Andric } 3730b57cec5SDimitry Andric } 3740b57cec5SDimitry Andric 3750b57cec5SDimitry Andric // Sort and unique MBB LiveIns as we've not checked if SubReg/PhysReg were in 3760b57cec5SDimitry Andric // each MBB's LiveIns set before calling addLiveIn on them. 3770b57cec5SDimitry Andric for (MachineBasicBlock &MBB : *MF) 3780b57cec5SDimitry Andric MBB.sortUniqueLiveIns(); 3790b57cec5SDimitry Andric } 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric /// Returns true if the given machine operand \p MO only reads undefined lanes. 3820b57cec5SDimitry Andric /// The function only works for use operands with a subregister set. 3830b57cec5SDimitry Andric bool VirtRegRewriter::readsUndefSubreg(const MachineOperand &MO) const { 3840b57cec5SDimitry Andric // Shortcut if the operand is already marked undef. 3850b57cec5SDimitry Andric if (MO.isUndef()) 3860b57cec5SDimitry Andric return true; 3870b57cec5SDimitry Andric 3888bcb0991SDimitry Andric Register Reg = MO.getReg(); 3890b57cec5SDimitry Andric const LiveInterval &LI = LIS->getInterval(Reg); 3900b57cec5SDimitry Andric const MachineInstr &MI = *MO.getParent(); 3910b57cec5SDimitry Andric SlotIndex BaseIndex = LIS->getInstructionIndex(MI); 3920b57cec5SDimitry Andric // This code is only meant to handle reading undefined subregisters which 3930b57cec5SDimitry Andric // we couldn't properly detect before. 3940b57cec5SDimitry Andric assert(LI.liveAt(BaseIndex) && 3950b57cec5SDimitry Andric "Reads of completely dead register should be marked undef already"); 3960b57cec5SDimitry Andric unsigned SubRegIdx = MO.getSubReg(); 3970b57cec5SDimitry Andric assert(SubRegIdx != 0 && LI.hasSubRanges()); 3980b57cec5SDimitry Andric LaneBitmask UseMask = TRI->getSubRegIndexLaneMask(SubRegIdx); 3990b57cec5SDimitry Andric // See if any of the relevant subregister liveranges is defined at this point. 4000b57cec5SDimitry Andric for (const LiveInterval::SubRange &SR : LI.subranges()) { 4010b57cec5SDimitry Andric if ((SR.LaneMask & UseMask).any() && SR.liveAt(BaseIndex)) 4020b57cec5SDimitry Andric return false; 4030b57cec5SDimitry Andric } 4040b57cec5SDimitry Andric return true; 4050b57cec5SDimitry Andric } 4060b57cec5SDimitry Andric 407fe6060f1SDimitry Andric void VirtRegRewriter::handleIdentityCopy(MachineInstr &MI) { 4080b57cec5SDimitry Andric if (!MI.isIdentityCopy()) 4090b57cec5SDimitry Andric return; 4100b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Identity copy: " << MI); 4110b57cec5SDimitry Andric ++NumIdCopies; 4120b57cec5SDimitry Andric 413fe6060f1SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 414fe6060f1SDimitry Andric 415fe6060f1SDimitry Andric // We may have deferred allocation of the virtual register, and the rewrite 416fe6060f1SDimitry Andric // regs code doesn't handle the liveness update. 417fe6060f1SDimitry Andric if (DstReg.isVirtual()) 418fe6060f1SDimitry Andric return; 419fe6060f1SDimitry Andric 420fe6060f1SDimitry Andric RewriteRegs.insert(DstReg); 421fe6060f1SDimitry Andric 4220b57cec5SDimitry Andric // Copies like: 4230b57cec5SDimitry Andric // %r0 = COPY undef %r0 4240b57cec5SDimitry Andric // %al = COPY %al, implicit-def %eax 4250b57cec5SDimitry Andric // give us additional liveness information: The target (super-)register 4260b57cec5SDimitry Andric // must not be valid before this point. Replace the COPY with a KILL 4270b57cec5SDimitry Andric // instruction to maintain this information. 4280b57cec5SDimitry Andric if (MI.getOperand(1).isUndef() || MI.getNumOperands() > 2) { 4290b57cec5SDimitry Andric MI.setDesc(TII->get(TargetOpcode::KILL)); 4300b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " replace by: " << MI); 4310b57cec5SDimitry Andric return; 4320b57cec5SDimitry Andric } 4330b57cec5SDimitry Andric 4340b57cec5SDimitry Andric if (Indexes) 4350b57cec5SDimitry Andric Indexes->removeSingleMachineInstrFromMaps(MI); 4360b57cec5SDimitry Andric MI.eraseFromBundle(); 4370b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " deleted.\n"); 4380b57cec5SDimitry Andric } 4390b57cec5SDimitry Andric 4400b57cec5SDimitry Andric /// The liverange splitting logic sometimes produces bundles of copies when 4410b57cec5SDimitry Andric /// subregisters are involved. Expand these into a sequence of copy instructions 4420b57cec5SDimitry Andric /// after processing the last in the bundle. Does not update LiveIntervals 4430b57cec5SDimitry Andric /// which we shouldn't need for this instruction anymore. 4440b57cec5SDimitry Andric void VirtRegRewriter::expandCopyBundle(MachineInstr &MI) const { 445e8d8bef9SDimitry Andric if (!MI.isCopy() && !MI.isKill()) 4460b57cec5SDimitry Andric return; 4470b57cec5SDimitry Andric 4480b57cec5SDimitry Andric if (MI.isBundledWithPred() && !MI.isBundledWithSucc()) { 4490b57cec5SDimitry Andric SmallVector<MachineInstr *, 2> MIs({&MI}); 4500b57cec5SDimitry Andric 451e8d8bef9SDimitry Andric // Only do this when the complete bundle is made out of COPYs and KILLs. 4520b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 4530b57cec5SDimitry Andric for (MachineBasicBlock::reverse_instr_iterator I = 4540b57cec5SDimitry Andric std::next(MI.getReverseIterator()), E = MBB.instr_rend(); 4550b57cec5SDimitry Andric I != E && I->isBundledWithSucc(); ++I) { 456e8d8bef9SDimitry Andric if (!I->isCopy() && !I->isKill()) 4570b57cec5SDimitry Andric return; 4580b57cec5SDimitry Andric MIs.push_back(&*I); 4590b57cec5SDimitry Andric } 4600b57cec5SDimitry Andric MachineInstr *FirstMI = MIs.back(); 4610b57cec5SDimitry Andric 4620b57cec5SDimitry Andric auto anyRegsAlias = [](const MachineInstr *Dst, 4630b57cec5SDimitry Andric ArrayRef<MachineInstr *> Srcs, 4640b57cec5SDimitry Andric const TargetRegisterInfo *TRI) { 4650b57cec5SDimitry Andric for (const MachineInstr *Src : Srcs) 4660b57cec5SDimitry Andric if (Src != Dst) 4670b57cec5SDimitry Andric if (TRI->regsOverlap(Dst->getOperand(0).getReg(), 4680b57cec5SDimitry Andric Src->getOperand(1).getReg())) 4690b57cec5SDimitry Andric return true; 4700b57cec5SDimitry Andric return false; 4710b57cec5SDimitry Andric }; 4720b57cec5SDimitry Andric 4730b57cec5SDimitry Andric // If any of the destination registers in the bundle of copies alias any of 4740b57cec5SDimitry Andric // the source registers, try to schedule the instructions to avoid any 4750b57cec5SDimitry Andric // clobbering. 4760b57cec5SDimitry Andric for (int E = MIs.size(), PrevE = E; E > 1; PrevE = E) { 4770b57cec5SDimitry Andric for (int I = E; I--; ) 478bdd1243dSDimitry Andric if (!anyRegsAlias(MIs[I], ArrayRef(MIs).take_front(E), TRI)) { 4790b57cec5SDimitry Andric if (I + 1 != E) 4800b57cec5SDimitry Andric std::swap(MIs[I], MIs[E - 1]); 4810b57cec5SDimitry Andric --E; 4820b57cec5SDimitry Andric } 4830b57cec5SDimitry Andric if (PrevE == E) { 4840b57cec5SDimitry Andric MF->getFunction().getContext().emitError( 4850b57cec5SDimitry Andric "register rewriting failed: cycle in copy bundle"); 4860b57cec5SDimitry Andric break; 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric } 4890b57cec5SDimitry Andric 4900b57cec5SDimitry Andric MachineInstr *BundleStart = FirstMI; 4910b57cec5SDimitry Andric for (MachineInstr *BundledMI : llvm::reverse(MIs)) { 4920b57cec5SDimitry Andric // If instruction is in the middle of the bundle, move it before the 4930b57cec5SDimitry Andric // bundle starts, otherwise, just unbundle it. When we get to the last 4940b57cec5SDimitry Andric // instruction, the bundle will have been completely undone. 4950b57cec5SDimitry Andric if (BundledMI != BundleStart) { 4960b57cec5SDimitry Andric BundledMI->removeFromBundle(); 497e8d8bef9SDimitry Andric MBB.insert(BundleStart, BundledMI); 4980b57cec5SDimitry Andric } else if (BundledMI->isBundledWithSucc()) { 4990b57cec5SDimitry Andric BundledMI->unbundleFromSucc(); 5000b57cec5SDimitry Andric BundleStart = &*std::next(BundledMI->getIterator()); 5010b57cec5SDimitry Andric } 5020b57cec5SDimitry Andric 5030b57cec5SDimitry Andric if (Indexes && BundledMI != FirstMI) 5040b57cec5SDimitry Andric Indexes->insertMachineInstrInMaps(*BundledMI); 5050b57cec5SDimitry Andric } 5060b57cec5SDimitry Andric } 5070b57cec5SDimitry Andric } 5080b57cec5SDimitry Andric 5090b57cec5SDimitry Andric /// Check whether (part of) \p SuperPhysReg is live through \p MI. 5100b57cec5SDimitry Andric /// \pre \p MI defines a subregister of a virtual register that 5110b57cec5SDimitry Andric /// has been assigned to \p SuperPhysReg. 5120b57cec5SDimitry Andric bool VirtRegRewriter::subRegLiveThrough(const MachineInstr &MI, 513e8d8bef9SDimitry Andric MCRegister SuperPhysReg) const { 5140b57cec5SDimitry Andric SlotIndex MIIndex = LIS->getInstructionIndex(MI); 5150b57cec5SDimitry Andric SlotIndex BeforeMIUses = MIIndex.getBaseIndex(); 5160b57cec5SDimitry Andric SlotIndex AfterMIDefs = MIIndex.getBoundaryIndex(); 51706c3fb27SDimitry Andric for (MCRegUnit Unit : TRI->regunits(SuperPhysReg)) { 51806c3fb27SDimitry Andric const LiveRange &UnitRange = LIS->getRegUnit(Unit); 5190b57cec5SDimitry Andric // If the regunit is live both before and after MI, 5200b57cec5SDimitry Andric // we assume it is live through. 5210b57cec5SDimitry Andric // Generally speaking, this is not true, because something like 5220b57cec5SDimitry Andric // "RU = op RU" would match that description. 5230b57cec5SDimitry Andric // However, we know that we are trying to assess whether 5240b57cec5SDimitry Andric // a def of a virtual reg, vreg, is live at the same time of RU. 5250b57cec5SDimitry Andric // If we are in the "RU = op RU" situation, that means that vreg 5260b57cec5SDimitry Andric // is defined at the same time as RU (i.e., "vreg, RU = op RU"). 5270b57cec5SDimitry Andric // Thus, vreg and RU interferes and vreg cannot be assigned to 5280b57cec5SDimitry Andric // SuperPhysReg. Therefore, this situation cannot happen. 5290b57cec5SDimitry Andric if (UnitRange.liveAt(AfterMIDefs) && UnitRange.liveAt(BeforeMIUses)) 5300b57cec5SDimitry Andric return true; 5310b57cec5SDimitry Andric } 5320b57cec5SDimitry Andric return false; 5330b57cec5SDimitry Andric } 5340b57cec5SDimitry Andric 5350b57cec5SDimitry Andric void VirtRegRewriter::rewrite() { 5360b57cec5SDimitry Andric bool NoSubRegLiveness = !MRI->subRegLivenessEnabled(); 5378bcb0991SDimitry Andric SmallVector<Register, 8> SuperDeads; 5388bcb0991SDimitry Andric SmallVector<Register, 8> SuperDefs; 5398bcb0991SDimitry Andric SmallVector<Register, 8> SuperKills; 5400b57cec5SDimitry Andric 5410b57cec5SDimitry Andric for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end(); 5420b57cec5SDimitry Andric MBBI != MBBE; ++MBBI) { 5430b57cec5SDimitry Andric LLVM_DEBUG(MBBI->print(dbgs(), Indexes)); 544349cc55cSDimitry Andric for (MachineInstr &MI : llvm::make_early_inc_range(MBBI->instrs())) { 545349cc55cSDimitry Andric for (MachineOperand &MO : MI.operands()) { 5460b57cec5SDimitry Andric // Make sure MRI knows about registers clobbered by regmasks. 5470b57cec5SDimitry Andric if (MO.isRegMask()) 5480b57cec5SDimitry Andric MRI->addPhysRegsUsedFromRegMask(MO.getRegMask()); 5490b57cec5SDimitry Andric 5508bcb0991SDimitry Andric if (!MO.isReg() || !MO.getReg().isVirtual()) 5510b57cec5SDimitry Andric continue; 5528bcb0991SDimitry Andric Register VirtReg = MO.getReg(); 553e8d8bef9SDimitry Andric MCRegister PhysReg = VRM->getPhys(VirtReg); 554fe6060f1SDimitry Andric if (PhysReg == VirtRegMap::NO_PHYS_REG) 555fe6060f1SDimitry Andric continue; 556fe6060f1SDimitry Andric 557fe6060f1SDimitry Andric assert(Register(PhysReg).isPhysical()); 558fe6060f1SDimitry Andric 559fe6060f1SDimitry Andric RewriteRegs.insert(PhysReg); 5600b57cec5SDimitry Andric assert(!MRI->isReserved(PhysReg) && "Reserved register assignment"); 5610b57cec5SDimitry Andric 5620b57cec5SDimitry Andric // Preserve semantics of sub-register operands. 5630b57cec5SDimitry Andric unsigned SubReg = MO.getSubReg(); 5640b57cec5SDimitry Andric if (SubReg != 0) { 5650b57cec5SDimitry Andric if (NoSubRegLiveness || !MRI->shouldTrackSubRegLiveness(VirtReg)) { 5660b57cec5SDimitry Andric // A virtual register kill refers to the whole register, so we may 5670b57cec5SDimitry Andric // have to add implicit killed operands for the super-register. A 5680b57cec5SDimitry Andric // partial redef always kills and redefines the super-register. 5690b57cec5SDimitry Andric if ((MO.readsReg() && (MO.isDef() || MO.isKill())) || 570349cc55cSDimitry Andric (MO.isDef() && subRegLiveThrough(MI, PhysReg))) 5710b57cec5SDimitry Andric SuperKills.push_back(PhysReg); 5720b57cec5SDimitry Andric 5730b57cec5SDimitry Andric if (MO.isDef()) { 5740b57cec5SDimitry Andric // Also add implicit defs for the super-register. 5750b57cec5SDimitry Andric if (MO.isDead()) 5760b57cec5SDimitry Andric SuperDeads.push_back(PhysReg); 5770b57cec5SDimitry Andric else 5780b57cec5SDimitry Andric SuperDefs.push_back(PhysReg); 5790b57cec5SDimitry Andric } 5800b57cec5SDimitry Andric } else { 5810b57cec5SDimitry Andric if (MO.isUse()) { 5820b57cec5SDimitry Andric if (readsUndefSubreg(MO)) 5830b57cec5SDimitry Andric // We need to add an <undef> flag if the subregister is 5840b57cec5SDimitry Andric // completely undefined (and we are not adding super-register 5850b57cec5SDimitry Andric // defs). 5860b57cec5SDimitry Andric MO.setIsUndef(true); 5870b57cec5SDimitry Andric } else if (!MO.isDead()) { 5880b57cec5SDimitry Andric assert(MO.isDef()); 5890b57cec5SDimitry Andric } 5900b57cec5SDimitry Andric } 5910b57cec5SDimitry Andric 5920b57cec5SDimitry Andric // The def undef and def internal flags only make sense for 5930b57cec5SDimitry Andric // sub-register defs, and we are substituting a full physreg. An 5940b57cec5SDimitry Andric // implicit killed operand from the SuperKills list will represent the 5950b57cec5SDimitry Andric // partial read of the super-register. 5960b57cec5SDimitry Andric if (MO.isDef()) { 5970b57cec5SDimitry Andric MO.setIsUndef(false); 5980b57cec5SDimitry Andric MO.setIsInternalRead(false); 5990b57cec5SDimitry Andric } 6000b57cec5SDimitry Andric 6010b57cec5SDimitry Andric // PhysReg operands cannot have subregister indexes. 6020b57cec5SDimitry Andric PhysReg = TRI->getSubReg(PhysReg, SubReg); 6038bcb0991SDimitry Andric assert(PhysReg.isValid() && "Invalid SubReg for physical register"); 6040b57cec5SDimitry Andric MO.setSubReg(0); 6050b57cec5SDimitry Andric } 6060b57cec5SDimitry Andric // Rewrite. Note we could have used MachineOperand::substPhysReg(), but 6070b57cec5SDimitry Andric // we need the inlining here. 6080b57cec5SDimitry Andric MO.setReg(PhysReg); 6090b57cec5SDimitry Andric MO.setIsRenamable(true); 6100b57cec5SDimitry Andric } 6110b57cec5SDimitry Andric 6120b57cec5SDimitry Andric // Add any missing super-register kills after rewriting the whole 6130b57cec5SDimitry Andric // instruction. 6140b57cec5SDimitry Andric while (!SuperKills.empty()) 615349cc55cSDimitry Andric MI.addRegisterKilled(SuperKills.pop_back_val(), TRI, true); 6160b57cec5SDimitry Andric 6170b57cec5SDimitry Andric while (!SuperDeads.empty()) 618349cc55cSDimitry Andric MI.addRegisterDead(SuperDeads.pop_back_val(), TRI, true); 6190b57cec5SDimitry Andric 6200b57cec5SDimitry Andric while (!SuperDefs.empty()) 621349cc55cSDimitry Andric MI.addRegisterDefined(SuperDefs.pop_back_val(), TRI); 6220b57cec5SDimitry Andric 623349cc55cSDimitry Andric LLVM_DEBUG(dbgs() << "> " << MI); 6240b57cec5SDimitry Andric 625349cc55cSDimitry Andric expandCopyBundle(MI); 6260b57cec5SDimitry Andric 6270b57cec5SDimitry Andric // We can remove identity copies right now. 628349cc55cSDimitry Andric handleIdentityCopy(MI); 6290b57cec5SDimitry Andric } 6300b57cec5SDimitry Andric } 631fe6060f1SDimitry Andric 632fe6060f1SDimitry Andric if (LIS) { 633fe6060f1SDimitry Andric // Don't bother maintaining accurate LiveIntervals for registers which were 634fe6060f1SDimitry Andric // already allocated. 635fe6060f1SDimitry Andric for (Register PhysReg : RewriteRegs) { 63606c3fb27SDimitry Andric for (MCRegUnit Unit : TRI->regunits(PhysReg)) { 63706c3fb27SDimitry Andric LIS->removeRegUnit(Unit); 638fe6060f1SDimitry Andric } 639fe6060f1SDimitry Andric } 640fe6060f1SDimitry Andric } 641fe6060f1SDimitry Andric 642fe6060f1SDimitry Andric RewriteRegs.clear(); 643fe6060f1SDimitry Andric } 644fe6060f1SDimitry Andric 645fe6060f1SDimitry Andric FunctionPass *llvm::createVirtRegRewriter(bool ClearVirtRegs) { 646fe6060f1SDimitry Andric return new VirtRegRewriter(ClearVirtRegs); 6470b57cec5SDimitry Andric } 648