1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===-- llvm/CodeGen/Rewriter.cpp - Rewriter -----------------------------===// 2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The LLVM Compiler Infrastructure 4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source 6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details. 7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===// 9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define DEBUG_TYPE "virtregrewriter" 11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "VirtRegRewriter.h" 12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "VirtRegMap.h" 13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Function.h" 14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/LiveIntervalAnalysis.h" 15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineFrameInfo.h" 16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineInstrBuilder.h" 17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineRegisterInfo.h" 18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/CommandLine.h" 19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/Debug.h" 20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/ErrorHandling.h" 21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/raw_ostream.h" 22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetInstrInfo.h" 23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetLowering.h" 24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/DepthFirstIterator.h" 2519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/ADT/SmallSet.h" 26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/Statistic.h" 27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm; 28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumDSE , "Number of dead stores elided"); 30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumDSS , "Number of dead spill slots removed"); 31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumCommutes, "Number of instructions commuted"); 32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumDRM , "Number of re-materializable defs elided"); 33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumStores , "Number of stores added"); 34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumPSpills , "Number of physical register spills"); 3519bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSTATISTIC(NumOmitted , "Number of reloads omitted"); 36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumAvoided , "Number of reloads deemed unnecessary"); 37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumCopified, "Number of available reloads turned into copies"); 38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumReMats , "Number of re-materialization"); 39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumLoads , "Number of loads added"); 40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumReused , "Number of values reused"); 41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumDCE , "Number of copies elided"); 42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumSUnfold , "Number of stores unfolded"); 43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumModRefUnfold, "Number of modref unfolded"); 44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace { 46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman enum RewriterName { local, trivial }; 47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic cl::opt<RewriterName> 50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanRewriterOpt("rewriter", 51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman cl::desc("Rewriter to use (default=local)"), 52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman cl::Prefix, 53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman cl::values(clEnumVal(local, "local rewriter"), 54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman clEnumVal(trivial, "trivial rewriter"), 55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman clEnumValEnd), 56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman cl::init(local)); 57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic cl::opt<bool> 59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanScheduleSpills("schedule-spills", 60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman cl::desc("Schedule spill code"), 61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman cl::init(false)); 62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanVirtRegRewriter::~VirtRegRewriter() {} 64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// substitutePhysReg - Replace virtual register in MachineOperand with a 66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// physical register. Do the right thing with the sub-register index. 67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Note that operands may be added, so the MO reference is no longer valid. 68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void substitutePhysReg(MachineOperand &MO, unsigned Reg, 69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterInfo &TRI) { 7019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (MO.getSubReg()) { 7119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MO.substPhysReg(Reg, TRI); 7219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 7319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Any kill flags apply to the full virtual register, so they also apply to 7419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // the full physical register. 7519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // We assume that partial defs have already been decorated with a super-reg 7619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // <imp-def> operand by LiveIntervals. 77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr &MI = *MO.getParent(); 7819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (MO.isUse() && !MO.isUndef() && 7919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman (MO.isKill() || MI.isRegTiedToDefOperand(&MO-&MI.getOperand(0)))) 80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MI.addRegisterKilled(Reg, &TRI, /*AddIfNotFound=*/ true); 81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else { 82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MO.setReg(Reg); 83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace { 87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// This class is intended for use with the new spilling framework only. It 89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// rewrites vreg def/uses to use the assigned preg, but does not insert any 90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// spill code. 91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstruct TrivialRewriter : public VirtRegRewriter { 92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM, 94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveIntervals* LIs) { 95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "********** REWRITE MACHINE CODE **********\n"); 96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "********** Function: " 97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman << MF.getFunction()->getName() << '\n'); 98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "**** Machine Instrs" 99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman << "(NOTE! Does not include spills and reloads!) ****\n"); 100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(MF.dump()); 101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineRegisterInfo *mri = &MF.getRegInfo(); 103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterInfo *tri = MF.getTarget().getRegisterInfo(); 104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool changed = false; 106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (LiveIntervals::iterator liItr = LIs->begin(), liEnd = LIs->end(); 108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman liItr != liEnd; ++liItr) { 109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const LiveInterval *li = liItr->second; 111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned reg = li->reg; 112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TargetRegisterInfo::isPhysicalRegister(reg)) { 114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!li->empty()) 115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman mri->setPhysRegUsed(reg); 116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman else { 118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!VRM.hasPhys(reg)) 119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned pReg = VRM.getPhys(reg); 121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman mri->setPhysRegUsed(pReg); 122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Copy the register use-list before traversing it. 123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<std::pair<MachineInstr*, unsigned>, 32> reglist; 124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (MachineRegisterInfo::reg_iterator I = mri->reg_begin(reg), 125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman E = mri->reg_end(); I != E; ++I) 126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman reglist.push_back(std::make_pair(&*I, I.getOperandNo())); 127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned N=0; N != reglist.size(); ++N) 128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman substitutePhysReg(reglist[N].first->getOperand(reglist[N].second), 129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman pReg, *tri); 130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman changed |= !reglist.empty(); 131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "**** Post Machine Instrs ****\n"); 135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(MF.dump()); 136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return changed; 138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ************************************************************************ // 145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace { 147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// AvailableSpills - As the local rewriter is scanning and rewriting an MBB 149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// from top down, keep track of which spill slots or remat are available in 150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// each register. 151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// 152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Note that not all physregs are created equal here. In particular, some 153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// physregs are reloads that we are allowed to clobber or ignore at any time. 154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Other physregs are values that the register allocated program is using 155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// that we cannot CHANGE, but we can read if we like. We keep track of this 156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// on a per-stack-slot / remat id basis as the low bit in the value of the 157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// SpillSlotsAvailable entries. The predicate 'canClobberPhysReg()' checks 158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// this bit and addAvailable sets it if. 159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass AvailableSpills { 160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterInfo *TRI; 161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetInstrInfo *TII; 162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // SpillSlotsOrReMatsAvailable - This map keeps track of all of the spilled 164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // or remat'ed virtual register values that are still available, due to 165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // being loaded or stored to, but not invalidated yet. 166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::map<int, unsigned> SpillSlotsOrReMatsAvailable; 167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // PhysRegsAvailable - This is the inverse of SpillSlotsOrReMatsAvailable, 169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // indicating which stack slot values are currently held by a physreg. This 170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // is used to invalidate entries in SpillSlotsOrReMatsAvailable when a 171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // physreg is modified. 172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::multimap<unsigned, int> PhysRegsAvailable; 173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void disallowClobberPhysRegOnly(unsigned PhysReg); 175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void ClobberPhysRegOnly(unsigned PhysReg); 177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic: 178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AvailableSpills(const TargetRegisterInfo *tri, const TargetInstrInfo *tii) 179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman : TRI(tri), TII(tii) { 180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// clear - Reset the state. 183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void clear() { 184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SpillSlotsOrReMatsAvailable.clear(); 185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman PhysRegsAvailable.clear(); 186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterInfo *getRegInfo() const { return TRI; } 189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// getSpillSlotOrReMatPhysReg - If the specified stack slot or remat is 191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// available in a physical register, return that PhysReg, otherwise 192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// return 0. 193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned getSpillSlotOrReMatPhysReg(int Slot) const { 194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::map<int, unsigned>::const_iterator I = 195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SpillSlotsOrReMatsAvailable.find(Slot); 196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (I != SpillSlotsOrReMatsAvailable.end()) { 197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return I->second >> 1; // Remove the CanClobber bit. 198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return 0; 200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// addAvailable - Mark that the specified stack slot / remat is available 203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// in the specified physreg. If CanClobber is true, the physreg can be 204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// modified at any time without changing the semantics of the program. 205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void addAvailable(int SlotOrReMat, unsigned Reg, bool CanClobber = true) { 206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this stack slot is thought to be available in some other physreg, 207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // remove its record. 208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ModifyStackSlotOrReMat(SlotOrReMat); 209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman PhysRegsAvailable.insert(std::make_pair(Reg, SlotOrReMat)); 211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SpillSlotsOrReMatsAvailable[SlotOrReMat]= (Reg << 1) | 212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman (unsigned)CanClobber; 213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SlotOrReMat > VirtRegMap::MAX_STACK_SLOT) 215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "Remembering RM#" 216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman << SlotOrReMat-VirtRegMap::MAX_STACK_SLOT-1); 217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman else 218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "Remembering SS#" << SlotOrReMat); 21919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DEBUG(dbgs() << " in physreg " << TRI->getName(Reg) 22019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman << (CanClobber ? " canclobber" : "") << "\n"); 221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// canClobberPhysRegForSS - Return true if the spiller is allowed to change 224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// the value of the specified stackslot register if it desires. The 225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// specified stack slot must be available in a physreg for this query to 226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// make sense. 227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool canClobberPhysRegForSS(int SlotOrReMat) const { 228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(SpillSlotsOrReMatsAvailable.count(SlotOrReMat) && 229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Value not available!"); 230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return SpillSlotsOrReMatsAvailable.find(SlotOrReMat)->second & 1; 231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// canClobberPhysReg - Return true if the spiller is allowed to clobber the 234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// physical register where values for some stack slot(s) might be 235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// available. 236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool canClobberPhysReg(unsigned PhysReg) const { 237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::multimap<unsigned, int>::const_iterator I = 238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman PhysRegsAvailable.lower_bound(PhysReg); 239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman while (I != PhysRegsAvailable.end() && I->first == PhysReg) { 240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int SlotOrReMat = I->second; 241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman I++; 242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!canClobberPhysRegForSS(SlotOrReMat)) 243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// disallowClobberPhysReg - Unset the CanClobber bit of the specified 249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// stackslot register. The register is still available but is no longer 250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// allowed to be modifed. 251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void disallowClobberPhysReg(unsigned PhysReg); 252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// ClobberPhysReg - This is called when the specified physreg changes 254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// value. We use this to invalidate any info about stuff that lives in 255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// it and any of its aliases. 256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void ClobberPhysReg(unsigned PhysReg); 257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// ModifyStackSlotOrReMat - This method is called when the value in a stack 259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// slot changes. This removes information about which register the 260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// previous value for this slot lives in (as the previous value is dead 261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// now). 262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void ModifyStackSlotOrReMat(int SlotOrReMat); 263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 26419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman /// ClobberSharingStackSlots - When a register mapped to a stack slot changes, 26519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman /// other stack slots sharing the same register are no longer valid. 26619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman void ClobberSharingStackSlots(int StackSlot); 26719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// AddAvailableRegsToLiveIn - Availability information is being kept coming 269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// into the specified MBB. Add available physical registers as potential 270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// live-in's. If they are reused in the MBB, they will be added to the 271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// live-in set to make register scavenger and post-allocation scheduler. 272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void AddAvailableRegsToLiveIn(MachineBasicBlock &MBB, BitVector &RegKills, 273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps); 274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ************************************************************************ // 279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Given a location where a reload of a spilled register or a remat of 281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// a constant is to be inserted, attempt to find a safe location to 282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// insert the load at an earlier point in the basic-block, to hide 283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// latency of the load and to avoid address-generation interlock 284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// issues. 285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic MachineBasicBlock::iterator 286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanComputeReloadLoc(MachineBasicBlock::iterator const InsertLoc, 287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock::iterator const Begin, 288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned PhysReg, 289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterInfo *TRI, 290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool DoReMat, 291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int SSorRMId, 292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetInstrInfo *TII, 293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const MachineFunction &MF) 294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman{ 295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!ScheduleSpills) 296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return InsertLoc; 297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Spill backscheduling is of primary interest to addresses, so 299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // don't do anything if the register isn't in the register class 300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // used for pointers. 301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetLowering *TL = MF.getTarget().getTargetLowering(); 303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!TL->isTypeLegal(TL->getPointerTy())) 30519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Believe it or not, this is true on 16-bit targets like PIC16. 306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return InsertLoc; 307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass *ptrRegClass = 309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TL->getRegClassFor(TL->getPointerTy()); 310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!ptrRegClass->contains(PhysReg)) 311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return InsertLoc; 312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Scan upwards through the preceding instructions. If an instruction doesn't 314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // reference the stack slot or the register we're loading, we can 315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // backschedule the reload up past it. 316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock::iterator NewInsertLoc = InsertLoc; 317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman while (NewInsertLoc != Begin) { 318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock::iterator Prev = prior(NewInsertLoc); 319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0; i < Prev->getNumOperands(); ++i) { 320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineOperand &Op = Prev->getOperand(i); 321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!DoReMat && Op.isFI() && Op.getIndex() == SSorRMId) 322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman goto stop; 323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Prev->findRegisterUseOperandIdx(PhysReg) != -1 || 325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Prev->findRegisterDefOperand(PhysReg)) 326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman goto stop; 327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (const unsigned *Alias = TRI->getAliasSet(PhysReg); *Alias; ++Alias) 328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Prev->findRegisterUseOperandIdx(*Alias) != -1 || 329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Prev->findRegisterDefOperand(*Alias)) 330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman goto stop; 331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NewInsertLoc = Prev; 332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstop:; 334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If we made it to the beginning of the block, turn around and move back 336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // down just past any existing reloads. They're likely to be reloads/remats 337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // for instructions earlier than what our current reload/remat is for, so 338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // they should be scheduled earlier. 339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (NewInsertLoc == Begin) { 340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int FrameIdx; 341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman while (InsertLoc != NewInsertLoc && 342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman (TII->isLoadFromStackSlot(NewInsertLoc, FrameIdx) || 343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TII->isTriviallyReMaterializable(NewInsertLoc))) 344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NewInsertLoc; 345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return NewInsertLoc; 348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace { 351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ReusedOp - For each reused operand, we keep track of a bit of information, 353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// in case we need to rollback upon processing a new operand. See comments 354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// below. 355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstruct ReusedOp { 356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // The MachineInstr operand that reused an available value. 357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Operand; 358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // StackSlotOrReMat - The spill slot or remat id of the value being reused. 360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned StackSlotOrReMat; 361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // PhysRegReused - The physical register the value was available in. 363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned PhysRegReused; 364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // AssignedPhysReg - The physreg that was assigned for use by the reload. 366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned AssignedPhysReg; 367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // VirtReg - The virtual register itself. 369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned VirtReg; 370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ReusedOp(unsigned o, unsigned ss, unsigned prr, unsigned apr, 372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned vreg) 373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman : Operand(o), StackSlotOrReMat(ss), PhysRegReused(prr), 374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AssignedPhysReg(apr), VirtReg(vreg) {} 375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ReuseInfo - This maintains a collection of ReuseOp's for each operand that 378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// is reused instead of reloaded. 379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass ReuseInfo { 380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr &MI; 381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<ReusedOp> Reuses; 382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector PhysRegsClobbered; 383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic: 384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ReuseInfo(MachineInstr &mi, const TargetRegisterInfo *tri) : MI(mi) { 385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman PhysRegsClobbered.resize(tri->getNumRegs()); 386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool hasReuses() const { 389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return !Reuses.empty(); 390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// addReuse - If we choose to reuse a virtual register that is already 393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// available instead of reloading it, remember that we did so. 394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void addReuse(unsigned OpNo, unsigned StackSlotOrReMat, 395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned PhysRegReused, unsigned AssignedPhysReg, 396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned VirtReg) { 397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the reload is to the assigned register anyway, no undo will be 398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // required. 399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (PhysRegReused == AssignedPhysReg) return; 400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Otherwise, remember this. 402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Reuses.push_back(ReusedOp(OpNo, StackSlotOrReMat, PhysRegReused, 403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AssignedPhysReg, VirtReg)); 404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void markClobbered(unsigned PhysReg) { 407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman PhysRegsClobbered.set(PhysReg); 408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isClobbered(unsigned PhysReg) const { 411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return PhysRegsClobbered.test(PhysReg); 412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// GetRegForReload - We are about to emit a reload into PhysReg. If there 415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// is some other operand that is using the specified register, either pick 416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// a new register to use, or evict the previous reload and use this reg. 417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned GetRegForReload(const TargetRegisterClass *RC, unsigned PhysReg, 418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineFunction &MF, MachineInstr *MI, 419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AvailableSpills &Spills, 420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineInstr*> &MaybeDeadStores, 421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallSet<unsigned, 8> &Rejected, 422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector &RegKills, 423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps, 424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VirtRegMap &VRM); 425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// GetRegForReload - Helper for the above GetRegForReload(). Add a 427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 'Rejected' set to remember which registers have been considered and 428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// rejected for the reload. This avoids infinite looping in case like 429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// this: 430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// t1 := op t2, t3 431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// t2 <- assigned r0 for use by the reload but ended up reuse r1 432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// t3 <- assigned r1 for use by the reload but ended up reuse r0 433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// t1 <- desires r1 434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// sees r1 is taken by t2, tries t2's reload register r0 435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// sees r0 is taken by t3, tries t3's reload register r1 436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// sees r1 is taken by t2, tries t2's reload register r0 ... 437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned GetRegForReload(unsigned VirtReg, unsigned PhysReg, MachineInstr *MI, 438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AvailableSpills &Spills, 439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineInstr*> &MaybeDeadStores, 440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector &RegKills, 441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps, 442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VirtRegMap &VRM) { 443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallSet<unsigned, 8> Rejected; 444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineFunction &MF = *MI->getParent()->getParent(); 445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass* RC = MF.getRegInfo().getRegClass(VirtReg); 446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return GetRegForReload(RC, PhysReg, MF, MI, Spills, MaybeDeadStores, 447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Rejected, RegKills, KillOps, VRM); 448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ****************** // 454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Utility Functions // 455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ****************** // 456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// findSinglePredSuccessor - Return via reference a vector of machine basic 458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// blocks each of which is a successor of the specified BB and has no other 459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// predecessor. 460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void findSinglePredSuccessor(MachineBasicBlock *MBB, 461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVectorImpl<MachineBasicBlock *> &Succs){ 462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), 463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SE = MBB->succ_end(); SI != SE; ++SI) { 464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock *SuccMBB = *SI; 465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SuccMBB->pred_size() == 1) 466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Succs.push_back(SuccMBB); 467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 47019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// ResurrectConfirmedKill - Helper for ResurrectKill. This register is killed 47119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// but not re-defined and it's being reused. Remove the kill flag for the 47219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// register and unset the kill's marker and last kill operand. 47319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic void ResurrectConfirmedKill(unsigned Reg, const TargetRegisterInfo* TRI, 47419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman BitVector &RegKills, 47519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman std::vector<MachineOperand*> &KillOps) { 47619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DEBUG(dbgs() << "Resurrect " << TRI->getName(Reg) << "\n"); 47719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 47819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MachineOperand *KillOp = KillOps[Reg]; 47919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman KillOp->setIsKill(false); 48019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // KillOps[Reg] might be a def of a super-register. 48119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned KReg = KillOp->getReg(); 48219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (!RegKills[KReg]) 48319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return; 48419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 48519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman assert(KillOps[KReg]->getParent() == KillOp->getParent() && 48619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman "invalid superreg kill flags"); 48719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman KillOps[KReg] = NULL; 48819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman RegKills.reset(KReg); 48919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 49019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If it's a def of a super-register. Its other sub-regsters are no 49119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // longer killed as well. 49219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman for (const unsigned *SR = TRI->getSubRegisters(KReg); *SR; ++SR) { 49319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DEBUG(dbgs() << " Resurrect subreg " << TRI->getName(*SR) << "\n"); 49419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 49519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman assert(KillOps[*SR]->getParent() == KillOp->getParent() && 49619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman "invalid subreg kill flags"); 49719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman KillOps[*SR] = NULL; 49819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman RegKills.reset(*SR); 49919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 50019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 50119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 50219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// ResurrectKill - Invalidate kill info associated with a previous MI. An 50319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// optimization may have decided that it's safe to reuse a previously killed 50419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// register. If we fail to erase the invalid kill flags, then the register 50519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// scavenger may later clobber the register used by this MI. Note that this 50619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// must be done even if this MI is being deleted! Consider: 50719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 50819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// USE $r1 (vreg1) <kill> 50919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// ... 51019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// $r1(vreg3) = COPY $r1 (vreg2) 51119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 51219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// RegAlloc has smartly assigned all three vregs to the same physreg. Initially 51319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// vreg1's only use is a kill. The rewriter doesn't know it should be live 51419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// until it rewrites vreg2. At that points it sees that the copy is dead and 51519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// deletes it. However, deleting the copy implicitly forwards liveness of $r1 51619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// (it's copy coalescing). We must resurrect $r1 by removing the kill flag at 51719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// vreg1 before deleting the copy. 51819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic void ResurrectKill(MachineInstr &MI, unsigned Reg, 51919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman const TargetRegisterInfo* TRI, BitVector &RegKills, 52019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman std::vector<MachineOperand*> &KillOps) { 52119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (RegKills[Reg] && KillOps[Reg]->getParent() != &MI) { 52219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ResurrectConfirmedKill(Reg, TRI, RegKills, KillOps); 52319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return; 52419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 52519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // No previous kill for this reg. Check for subreg kills as well. 52619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // d4 = 52719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // store d4, fi#0 52819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // ... 52919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // = s8<kill> 53019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // ... 53119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // = d4 <avoiding reload> 53219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman for (const unsigned *SR = TRI->getSubRegisters(Reg); *SR; ++SR) { 53319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned SReg = *SR; 53419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (RegKills[SReg] && KillOps[SReg]->getParent() != &MI) 53519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ResurrectConfirmedKill(SReg, TRI, RegKills, KillOps); 536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// InvalidateKills - MI is going to be deleted. If any of its operands are 540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// marked kill, then invalidate the information. 541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void InvalidateKills(MachineInstr &MI, 542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterInfo* TRI, 543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector &RegKills, 544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps, 545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<unsigned, 2> *KillRegs = NULL) { 546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineOperand &MO = MI.getOperand(i); 548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!MO.isReg() || !MO.isUse() || !MO.isKill() || MO.isUndef()) 549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Reg = MO.getReg(); 551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TargetRegisterInfo::isVirtualRegister(Reg)) 552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (KillRegs) 554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman KillRegs->push_back(Reg); 555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(Reg < KillOps.size()); 556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (KillOps[Reg] == &MO) { 55719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // This operand was the kill, now no longer. 558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman KillOps[Reg] = NULL; 559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RegKills.reset(Reg); 560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (const unsigned *SR = TRI->getSubRegisters(Reg); *SR; ++SR) { 561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (RegKills[*SR]) { 56219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman assert(KillOps[*SR] == &MO && "bad subreg kill flags"); 563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman KillOps[*SR] = NULL; 564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RegKills.reset(*SR); 565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 56819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman else { 56919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // This operand may have reused a previously killed reg. Keep it live in 57019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // case it continues to be used after erasing this instruction. 57119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ResurrectKill(MI, Reg, TRI, RegKills, KillOps); 57219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// InvalidateRegDef - If the def operand of the specified def MI is now dead 577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// (since its spill instruction is removed), mark it isDead. Also checks if 578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the def MI has other definition operands that are not dead. Returns it by 579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// reference. 580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool InvalidateRegDef(MachineBasicBlock::iterator I, 581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr &NewDef, unsigned Reg, 582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool &HasLiveDef, 583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterInfo *TRI) { 584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Due to remat, it's possible this reg isn't being reused. That is, 585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // the def of this reg (by prev MI) is now dead. 586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *DefMI = I; 587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineOperand *DefOp = NULL; 588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = DefMI->getNumOperands(); i != e; ++i) { 589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineOperand &MO = DefMI->getOperand(i); 590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!MO.isReg() || !MO.isDef() || !MO.isKill() || MO.isUndef()) 591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MO.getReg() == Reg) 593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DefOp = &MO; 594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman else if (!MO.isDead()) 595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman HasLiveDef = true; 596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!DefOp) 598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool FoundUse = false, Done = false; 601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock::iterator E = &NewDef; 602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++I; ++E; 603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (; !Done && I != E; ++I) { 604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *NMI = I; 605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned j = 0, ee = NMI->getNumOperands(); j != ee; ++j) { 606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineOperand &MO = NMI->getOperand(j); 607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!MO.isReg() || MO.getReg() == 0 || 608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman (MO.getReg() != Reg && !TRI->isSubRegister(Reg, MO.getReg()))) 609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MO.isUse()) 611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman FoundUse = true; 612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Done = true; // Stop after scanning all the operands of this MI. 613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!FoundUse) { 616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Def is dead! 617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DefOp->setIsDead(); 618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// UpdateKills - Track and update kill info. If a MI reads a register that is 624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// marked kill, then it must be due to register reuse. Transfer the kill info 625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// over. 626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void UpdateKills(MachineInstr &MI, const TargetRegisterInfo* TRI, 627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector &RegKills, 628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps) { 629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // These do not affect kill info at all. 630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MI.isDebugValue()) 631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return; 632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineOperand &MO = MI.getOperand(i); 634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!MO.isReg() || !MO.isUse() || MO.isUndef()) 635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Reg = MO.getReg(); 637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Reg == 0) 638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 64019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // This operand may have reused a previously killed reg. Keep it live. 64119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ResurrectKill(MI, Reg, TRI, RegKills, KillOps); 642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MO.isKill()) { 644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RegKills.set(Reg); 645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman KillOps[Reg] = &MO; 646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (const unsigned *SR = TRI->getSubRegisters(Reg); *SR; ++SR) { 647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RegKills.set(*SR); 648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman KillOps[*SR] = &MO; 649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const MachineOperand &MO = MI.getOperand(i); 655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!MO.isReg() || !MO.getReg() || !MO.isDef()) 656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Reg = MO.getReg(); 658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RegKills.reset(Reg); 659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman KillOps[Reg] = NULL; 660894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // It also defines (or partially define) aliases. 661894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (const unsigned *SR = TRI->getSubRegisters(Reg); *SR; ++SR) { 662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RegKills.reset(*SR); 663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman KillOps[*SR] = NULL; 664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (const unsigned *SR = TRI->getSuperRegisters(Reg); *SR; ++SR) { 666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RegKills.reset(*SR); 667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman KillOps[*SR] = NULL; 668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 67219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// ReMaterialize - Re-materialize definition for Reg targeting DestReg. 673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// 674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void ReMaterialize(MachineBasicBlock &MBB, 675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock::iterator &MII, 676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned DestReg, unsigned Reg, 677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetInstrInfo *TII, 678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterInfo *TRI, 679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VirtRegMap &VRM) { 680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *ReMatDefMI = VRM.getReMaterializedMI(Reg); 681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef NDEBUG 68219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman const MCInstrDesc &MCID = ReMatDefMI->getDesc(); 68319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman assert(MCID.getNumDefs() == 1 && 684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Don't know how to remat instructions that define > 1 values!"); 685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TII->reMaterialize(MBB, MII, DestReg, 0, ReMatDefMI, *TRI); 687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *NewMI = prior(MII); 688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = NewMI->getNumOperands(); i != e; ++i) { 689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineOperand &MO = NewMI->getOperand(i); 690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!MO.isReg() || MO.getReg() == 0) 691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned VirtReg = MO.getReg(); 693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TargetRegisterInfo::isPhysicalRegister(VirtReg)) 694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(MO.isUse()); 696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Phys = VRM.getPhys(VirtReg); 697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(Phys && "Virtual register is not assigned a register?"); 698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman substitutePhysReg(MO, Phys, *TRI); 699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumReMats; 701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// findSuperReg - Find the SubReg's super-register of given register class 704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// where its SubIdx sub-register is SubReg. 705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic unsigned findSuperReg(const TargetRegisterClass *RC, unsigned SubReg, 706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned SubIdx, const TargetRegisterInfo *TRI) { 707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end(); 708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman I != E; ++I) { 709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Reg = *I; 710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TRI->getSubReg(Reg, SubIdx) == SubReg) 711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return Reg; 712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return 0; 714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ******************************** // 717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Available Spills Implementation // 718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ******************************** // 719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// disallowClobberPhysRegOnly - Unset the CanClobber bit of the specified 721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// stackslot register. The register is still available but is no longer 722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// allowed to be modifed. 723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AvailableSpills::disallowClobberPhysRegOnly(unsigned PhysReg) { 724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::multimap<unsigned, int>::iterator I = 725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman PhysRegsAvailable.lower_bound(PhysReg); 726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman while (I != PhysRegsAvailable.end() && I->first == PhysReg) { 727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int SlotOrReMat = I->second; 728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman I++; 729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert((SpillSlotsOrReMatsAvailable[SlotOrReMat] >> 1) == PhysReg && 730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Bidirectional map mismatch!"); 731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SpillSlotsOrReMatsAvailable[SlotOrReMat] &= ~1; 732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "PhysReg " << TRI->getName(PhysReg) 733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman << " copied, it is available for use but can no longer be modified\n"); 734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// disallowClobberPhysReg - Unset the CanClobber bit of the specified 738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// stackslot register and its aliases. The register and its aliases may 739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// still available but is no longer allowed to be modifed. 740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AvailableSpills::disallowClobberPhysReg(unsigned PhysReg) { 741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (const unsigned *AS = TRI->getAliasSet(PhysReg); *AS; ++AS) 742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman disallowClobberPhysRegOnly(*AS); 743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman disallowClobberPhysRegOnly(PhysReg); 744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ClobberPhysRegOnly - This is called when the specified physreg changes 747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// value. We use this to invalidate any info about stuff we thing lives in it. 748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AvailableSpills::ClobberPhysRegOnly(unsigned PhysReg) { 749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::multimap<unsigned, int>::iterator I = 750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman PhysRegsAvailable.lower_bound(PhysReg); 751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman while (I != PhysRegsAvailable.end() && I->first == PhysReg) { 752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int SlotOrReMat = I->second; 753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman PhysRegsAvailable.erase(I++); 754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert((SpillSlotsOrReMatsAvailable[SlotOrReMat] >> 1) == PhysReg && 755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Bidirectional map mismatch!"); 756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SpillSlotsOrReMatsAvailable.erase(SlotOrReMat); 757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "PhysReg " << TRI->getName(PhysReg) 758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman << " clobbered, invalidating "); 759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SlotOrReMat > VirtRegMap::MAX_STACK_SLOT) 760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "RM#" << SlotOrReMat-VirtRegMap::MAX_STACK_SLOT-1 <<"\n"); 761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman else 762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "SS#" << SlotOrReMat << "\n"); 763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ClobberPhysReg - This is called when the specified physreg changes 767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// value. We use this to invalidate any info about stuff we thing lives in 768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// it and any of its aliases. 769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AvailableSpills::ClobberPhysReg(unsigned PhysReg) { 770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (const unsigned *AS = TRI->getAliasSet(PhysReg); *AS; ++AS) 771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ClobberPhysRegOnly(*AS); 772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ClobberPhysRegOnly(PhysReg); 773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// AddAvailableRegsToLiveIn - Availability information is being kept coming 776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// into the specified MBB. Add available physical registers as potential 777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// live-in's. If they are reused in the MBB, they will be added to the 778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// live-in set to make register scavenger and post-allocation scheduler. 779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AvailableSpills::AddAvailableRegsToLiveIn(MachineBasicBlock &MBB, 780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector &RegKills, 781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps) { 782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::set<unsigned> NotAvailable; 783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (std::multimap<unsigned, int>::iterator 784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman I = PhysRegsAvailable.begin(), E = PhysRegsAvailable.end(); 785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman I != E; ++I) { 786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Reg = I->first; 787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass* RC = TRI->getMinimalPhysRegClass(Reg); 788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // FIXME: A temporary workaround. We can't reuse available value if it's 789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // not safe to move the def of the virtual register's class. e.g. 790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // X86::RFP* register classes. Do not add it as a live-in. 791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!TII->isSafeToMoveRegClassDefs(RC)) 792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // This is no longer available. 793894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NotAvailable.insert(Reg); 794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman else { 795894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MBB.addLiveIn(Reg); 79619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (RegKills[Reg]) 79719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ResurrectConfirmedKill(Reg, TRI, RegKills, KillOps); 798894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 799894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Skip over the same register. 801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::multimap<unsigned, int>::iterator NI = llvm::next(I); 802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman while (NI != E && NI->first == Reg) { 803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++I; 804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NI; 805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (std::set<unsigned>::iterator I = NotAvailable.begin(), 809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman E = NotAvailable.end(); I != E; ++I) { 810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ClobberPhysReg(*I); 811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (const unsigned *SubRegs = TRI->getSubRegisters(*I); 812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman *SubRegs; ++SubRegs) 813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ClobberPhysReg(*SubRegs); 814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ModifyStackSlotOrReMat - This method is called when the value in a stack 818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// slot changes. This removes information about which register the previous 819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// value for this slot lives in (as the previous value is dead now). 820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AvailableSpills::ModifyStackSlotOrReMat(int SlotOrReMat) { 821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::map<int, unsigned>::iterator It = 822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SpillSlotsOrReMatsAvailable.find(SlotOrReMat); 823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (It == SpillSlotsOrReMatsAvailable.end()) return; 824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Reg = It->second >> 1; 825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SpillSlotsOrReMatsAvailable.erase(It); 826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // This register may hold the value of multiple stack slots, only remove this 828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // stack slot from the set of values the register contains. 829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::multimap<unsigned, int>::iterator I = PhysRegsAvailable.lower_bound(Reg); 830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (; ; ++I) { 831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(I != PhysRegsAvailable.end() && I->first == Reg && 832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Map inverse broken!"); 833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (I->second == SlotOrReMat) break; 834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 835894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman PhysRegsAvailable.erase(I); 836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 83819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid AvailableSpills::ClobberSharingStackSlots(int StackSlot) { 83919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman std::map<int, unsigned>::iterator It = 84019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman SpillSlotsOrReMatsAvailable.find(StackSlot); 84119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (It == SpillSlotsOrReMatsAvailable.end()) return; 84219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned Reg = It->second >> 1; 84319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 84419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Erase entries in PhysRegsAvailable for other stack slots. 84519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman std::multimap<unsigned, int>::iterator I = PhysRegsAvailable.lower_bound(Reg); 84619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman while (I != PhysRegsAvailable.end() && I->first == Reg) { 84719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman std::multimap<unsigned, int>::iterator NextI = llvm::next(I); 84819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (I->second != StackSlot) { 84919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DEBUG(dbgs() << "Clobbered sharing SS#" << I->second << " in " 85019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman << PrintReg(Reg, TRI) << '\n'); 85119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman SpillSlotsOrReMatsAvailable.erase(I->second); 85219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman PhysRegsAvailable.erase(I); 85319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 85419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman I = NextI; 85519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 85619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 85719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ************************** // 859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Reuse Info Implementation // 860894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ************************** // 861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// GetRegForReload - We are about to emit a reload into PhysReg. If there 863894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// is some other operand that is using the specified register, either pick 864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// a new register to use, or evict the previous reload and use this reg. 865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned ReuseInfo::GetRegForReload(const TargetRegisterClass *RC, 866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned PhysReg, 867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineFunction &MF, 868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *MI, AvailableSpills &Spills, 869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineInstr*> &MaybeDeadStores, 870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallSet<unsigned, 8> &Rejected, 871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector &RegKills, 872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps, 873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VirtRegMap &VRM) { 874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetInstrInfo* TII = MF.getTarget().getInstrInfo(); 875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterInfo *TRI = Spills.getRegInfo(); 876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Reuses.empty()) return PhysReg; // This is most often empty. 878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned ro = 0, e = Reuses.size(); ro != e; ++ro) { 880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ReusedOp &Op = Reuses[ro]; 881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If we find some other reuse that was supposed to use this register 882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // exactly for its reload, we can change this reload to use ITS reload 883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // register. That is, unless its reload register has already been 884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // considered and subsequently rejected because it has also been reused 885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // by another operand. 886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Op.PhysRegReused == PhysReg && 887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Rejected.count(Op.AssignedPhysReg) == 0 && 888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RC->contains(Op.AssignedPhysReg)) { 889894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Yup, use the reload register that we didn't use before. 890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned NewReg = Op.AssignedPhysReg; 891894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Rejected.insert(PhysReg); 892894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return GetRegForReload(RC, NewReg, MF, MI, Spills, MaybeDeadStores, 893894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Rejected, RegKills, KillOps, VRM); 894894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else { 895894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Otherwise, we might also have a problem if a previously reused 896894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // value aliases the new register. If so, codegen the previous reload 897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // and use this one. 898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned PRRU = Op.PhysRegReused; 899894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TRI->regsOverlap(PRRU, PhysReg)) { 900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Okay, we found out that an alias of a reused register 901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // was used. This isn't good because it means we have 902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // to undo a previous reuse. 903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock *MBB = MI->getParent(); 904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass *AliasRC = 905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MBB->getParent()->getRegInfo().getRegClass(Op.VirtReg); 906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Copy Op out of the vector and remove it, we're going to insert an 908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // explicit load for it. 909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ReusedOp NewOp = Op; 910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Reuses.erase(Reuses.begin()+ro); 911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // MI may be using only a sub-register of PhysRegUsed. 913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned RealPhysRegUsed = MI->getOperand(NewOp.Operand).getReg(); 914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned SubIdx = 0; 915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(TargetRegisterInfo::isPhysicalRegister(RealPhysRegUsed) && 916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "A reuse cannot be a virtual register"); 917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (PRRU != RealPhysRegUsed) { 918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // What was the sub-register index? 919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SubIdx = TRI->getSubRegIndex(PRRU, RealPhysRegUsed); 920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(SubIdx && 921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Operand physreg is not a sub-register of PhysRegUsed"); 922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Ok, we're going to try to reload the assigned physreg into the 925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // slot that we were supposed to in the first place. However, that 926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // register could hold a reuse. Check to see if it conflicts or 927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // would prefer us to use a different register. 928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned NewPhysReg = GetRegForReload(RC, NewOp.AssignedPhysReg, 929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MF, MI, Spills, MaybeDeadStores, 930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Rejected, RegKills, KillOps, VRM); 931894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool DoReMat = NewOp.StackSlotOrReMat > VirtRegMap::MAX_STACK_SLOT; 933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int SSorRMId = DoReMat 934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ? VRM.getReMatId(NewOp.VirtReg) : (int) NewOp.StackSlotOrReMat; 935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Back-schedule reloads and remats. 937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock::iterator InsertLoc = 938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ComputeReloadLoc(MI, MBB->begin(), PhysReg, TRI, 939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DoReMat, SSorRMId, TII, MF); 940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (DoReMat) { 942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ReMaterialize(*MBB, InsertLoc, NewPhysReg, NewOp.VirtReg, TII, 943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TRI, VRM); 944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else { 945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TII->loadRegFromStackSlot(*MBB, InsertLoc, NewPhysReg, 946894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NewOp.StackSlotOrReMat, AliasRC, TRI); 947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *LoadMI = prior(InsertLoc); 948894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM.addSpillSlotUse(NewOp.StackSlotOrReMat, LoadMI); 949894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Any stores to this stack slot are not dead anymore. 950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MaybeDeadStores[NewOp.StackSlotOrReMat] = NULL; 951894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumLoads; 952894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 953894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.ClobberPhysReg(NewPhysReg); 954894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.ClobberPhysReg(NewOp.PhysRegReused); 955894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 956894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned RReg = SubIdx ? TRI->getSubReg(NewPhysReg, SubIdx) :NewPhysReg; 957894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MI->getOperand(NewOp.Operand).setReg(RReg); 958894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MI->getOperand(NewOp.Operand).setSubReg(0); 959894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 960894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.addAvailable(NewOp.StackSlotOrReMat, NewPhysReg); 961894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman UpdateKills(*prior(InsertLoc), TRI, RegKills, KillOps); 962894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << '\t' << *prior(InsertLoc)); 963894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 964894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "Reuse undone!\n"); 965894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman --NumReused; 966894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 967894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Finally, PhysReg is now available, go ahead and use it. 968894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return PhysReg; 969894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 970894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 971894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 972894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return PhysReg; 973894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 975894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ************************************************************************ // 976894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// FoldsStackSlotModRef - Return true if the specified MI folds the specified 978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// stack slot mod/ref. It also checks if it's possible to unfold the 979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// instruction by having it define a specified physical register instead. 980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool FoldsStackSlotModRef(MachineInstr &MI, int SS, unsigned PhysReg, 981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetInstrInfo *TII, 982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterInfo *TRI, 983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VirtRegMap &VRM) { 984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (VRM.hasEmergencySpills(&MI) || VRM.isSpillPt(&MI)) 985894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool Found = false; 988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VirtRegMap::MI2VirtMapTy::const_iterator I, End; 989894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ++I) { 990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned VirtReg = I->second.first; 991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VirtRegMap::ModRef MR = I->second.second; 992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MR & VirtRegMap::isModRef) 993894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (VRM.getStackSlot(VirtReg) == SS) { 994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Found= TII->getOpcodeAfterMemoryUnfold(MI.getOpcode(), true, true) != 0; 995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!Found) 999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1000894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Does the instruction uses a register that overlaps the scratch register? 1002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 1003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineOperand &MO = MI.getOperand(i); 1004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!MO.isReg() || MO.getReg() == 0) 1005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 1006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Reg = MO.getReg(); 1007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TargetRegisterInfo::isVirtualRegister(Reg)) { 1008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!VRM.hasPhys(Reg)) 1009894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 1010894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Reg = VRM.getPhys(Reg); 1011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TRI->regsOverlap(PhysReg, Reg)) 1013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 1016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// FindFreeRegister - Find a free register of a given register class by looking 1019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// at (at most) the last two machine instructions. 1020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic unsigned FindFreeRegister(MachineBasicBlock::iterator MII, 1021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock &MBB, 1022894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass *RC, 1023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterInfo *TRI, 1024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector &AllocatableRegs) { 1025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector Defs(TRI->getNumRegs()); 1026894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector Uses(TRI->getNumRegs()); 1027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<unsigned, 4> LocalUses; 1028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<unsigned, 4> Kills; 1029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Take a look at 2 instructions at most. 1031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Count = 0; 1032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman while (Count < 2) { 1033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MII == MBB.begin()) 1034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 1035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *PrevMI = prior(MII); 1036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MII = PrevMI; 1037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (PrevMI->isDebugValue()) 1039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; // Skip over dbg_value instructions. 1040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++Count; 1041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = PrevMI->getNumOperands(); i != e; ++i) { 1043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineOperand &MO = PrevMI->getOperand(i); 1044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!MO.isReg() || MO.getReg() == 0) 1045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 1046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Reg = MO.getReg(); 1047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MO.isDef()) { 1048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Defs.set(Reg); 1049894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) 1050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Defs.set(*AS); 1051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else { 1052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LocalUses.push_back(Reg); 1053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MO.isKill() && AllocatableRegs[Reg]) 1054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Kills.push_back(Reg); 1055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = Kills.size(); i != e; ++i) { 1059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Kill = Kills[i]; 1060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!Defs[Kill] && !Uses[Kill] && 1061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RC->contains(Kill)) 1062894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return Kill; 1063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = LocalUses.size(); i != e; ++i) { 1065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Reg = LocalUses[i]; 1066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Uses.set(Reg); 1067894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) 1068894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Uses.set(*AS); 1069894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1070894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1071894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return 0; 1073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1075894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic 1076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AssignPhysToVirtReg(MachineInstr *MI, unsigned VirtReg, unsigned PhysReg, 1077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterInfo &TRI) { 1078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 1079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineOperand &MO = MI->getOperand(i); 1080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MO.isReg() && MO.getReg() == VirtReg) 1081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman substitutePhysReg(MO, PhysReg, TRI); 1082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace { 1086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstruct RefSorter { 1088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool operator()(const std::pair<MachineInstr*, int> &A, 1089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const std::pair<MachineInstr*, int> &B) { 1090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return A.second < B.second; 1091894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 1093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ***************************** // 1095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Local Spiller Implementation // 1096894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ***************************** // 1097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass LocalRewriter : public VirtRegRewriter { 1099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineRegisterInfo *MRI; 1100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterInfo *TRI; 1101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetInstrInfo *TII; 1102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VirtRegMap *VRM; 110319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman LiveIntervals *LIs; 1104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector AllocatableRegs; 1105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DenseMap<MachineInstr*, unsigned> DistanceMap; 1106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DenseMap<int, SmallVector<MachineInstr*,4> > Slot2DbgValues; 1107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock *MBB; // Basic block currently being processed. 1109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic: 1111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM, 1113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveIntervals* LIs); 1114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprivate: 111619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman void EraseInstr(MachineInstr *MI) { 111719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman VRM->RemoveMachineInstrFromMaps(MI); 111819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman LIs->RemoveMachineInstrFromMaps(MI); 111919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MI->eraseFromParent(); 112019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 1121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool OptimizeByUnfold2(unsigned VirtReg, int SS, 1123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock::iterator &MII, 1124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineInstr*> &MaybeDeadStores, 1125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AvailableSpills &Spills, 1126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector &RegKills, 1127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps); 1128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool OptimizeByUnfold(MachineBasicBlock::iterator &MII, 1130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineInstr*> &MaybeDeadStores, 1131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AvailableSpills &Spills, 1132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector &RegKills, 1133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps); 1134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool CommuteToFoldReload(MachineBasicBlock::iterator &MII, 1136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned VirtReg, unsigned SrcReg, int SS, 1137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AvailableSpills &Spills, 1138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector &RegKills, 1139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps, 1140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterInfo *TRI); 1141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void SpillRegToStackSlot(MachineBasicBlock::iterator &MII, 1143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int Idx, unsigned PhysReg, int StackSlot, 1144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass *RC, 1145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isAvailable, MachineInstr *&LastStore, 1146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AvailableSpills &Spills, 1147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallSet<MachineInstr*, 4> &ReMatDefs, 1148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector &RegKills, 1149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps); 1150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void TransferDeadness(unsigned Reg, BitVector &RegKills, 1152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps); 1153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool InsertEmergencySpills(MachineInstr *MI); 1155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool InsertRestores(MachineInstr *MI, 1157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AvailableSpills &Spills, 1158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector &RegKills, 1159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps); 1160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool InsertSpills(MachineInstr *MI); 1162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 116319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman void ProcessUses(MachineInstr &MI, AvailableSpills &Spills, 116419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman std::vector<MachineInstr*> &MaybeDeadStores, 116519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman BitVector &RegKills, 116619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ReuseInfo &ReusedOperands, 116719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman std::vector<MachineOperand*> &KillOps); 116819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void RewriteMBB(LiveIntervals *LIs, 1170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AvailableSpills &Spills, BitVector &RegKills, 1171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps); 1172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 1173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool LocalRewriter::runOnMachineFunction(MachineFunction &MF, VirtRegMap &vrm, 117619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman LiveIntervals* lis) { 1177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MRI = &MF.getRegInfo(); 1178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TRI = MF.getTarget().getRegisterInfo(); 1179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TII = MF.getTarget().getInstrInfo(); 1180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM = &vrm; 118119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman LIs = lis; 1182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AllocatableRegs = TRI->getAllocatableSet(MF); 1183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "\n**** Local spiller rewriting function '" 1184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman << MF.getFunction()->getName() << "':\n"); 1185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "**** Machine Instrs (NOTE! Does not include spills and" 1186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman " reloads!) ****\n"); 118719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DEBUG(MF.print(dbgs(), LIs->getSlotIndexes())); 1188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Spills - Keep track of which spilled values are available in physregs 1190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // so that we can choose to reuse the physregs instead of emitting 1191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // reloads. This is usually refreshed per basic block. 1192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AvailableSpills Spills(TRI, TII); 1193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Keep track of kill information. 1195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector RegKills(TRI->getNumRegs()); 1196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> KillOps; 1197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman KillOps.resize(TRI->getNumRegs(), NULL); 1198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // SingleEntrySuccs - Successor blocks which have a single predecessor. 1200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<MachineBasicBlock*, 4> SinglePredSuccs; 1201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallPtrSet<MachineBasicBlock*,16> EarlyVisited; 1202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Traverse the basic blocks depth first. 1204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock *Entry = MF.begin(); 1205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallPtrSet<MachineBasicBlock*,16> Visited; 1206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (df_ext_iterator<MachineBasicBlock*, 1207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallPtrSet<MachineBasicBlock*,16> > 1208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited); 1209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DFI != E; ++DFI) { 1210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MBB = *DFI; 1211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!EarlyVisited.count(MBB)) 1212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RewriteMBB(LIs, Spills, RegKills, KillOps); 1213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this MBB is the only predecessor of a successor. Keep the 1215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // availability information and visit it next. 1216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman do { 1217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Keep visiting single predecessor successor as long as possible. 1218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SinglePredSuccs.clear(); 1219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman findSinglePredSuccessor(MBB, SinglePredSuccs); 1220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SinglePredSuccs.empty()) 1221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MBB = 0; 1222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman else { 1223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // FIXME: More than one successors, each of which has MBB has 1224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // the only predecessor. 1225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MBB = SinglePredSuccs[0]; 1226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!Visited.count(MBB) && EarlyVisited.insert(MBB)) { 1227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.AddAvailableRegsToLiveIn(*MBB, RegKills, KillOps); 1228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RewriteMBB(LIs, Spills, RegKills, KillOps); 1229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } while (MBB); 1232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Clear the availability info. 1234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.clear(); 1235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "**** Post Machine Instrs ****\n"); 123819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DEBUG(MF.print(dbgs(), LIs->getSlotIndexes())); 1239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Mark unused spill slots. 1241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineFrameInfo *MFI = MF.getFrameInfo(); 1242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int SS = VRM->getLowSpillSlot(); 1243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SS != VirtRegMap::NO_STACK_SLOT) { 1244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (int e = VRM->getHighSpillSlot(); SS <= e; ++SS) { 1245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<MachineInstr*, 4> &DbgValues = Slot2DbgValues[SS]; 1246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!VRM->isSpillSlotUsed(SS)) { 1247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MFI->RemoveStackObject(SS); 1248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned j = 0, ee = DbgValues.size(); j != ee; ++j) { 1249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *DVMI = DbgValues[j]; 1250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "Removing debug info referencing FI#" << SS << '\n'); 125119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman EraseInstr(DVMI); 1252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumDSS; 1254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DbgValues.clear(); 1256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Slot2DbgValues.clear(); 1259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 1261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// OptimizeByUnfold2 - Unfold a series of load / store folding instructions if 1264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// a scratch register is available. 1265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// xorq %r12<kill>, %r13 1266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// addq %rax, -184(%rbp) 1267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// addq %r13, -184(%rbp) 1268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ==> 1269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// xorq %r12<kill>, %r13 1270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// movq -184(%rbp), %r12 1271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// addq %rax, %r12 1272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// addq %r13, %r12 1273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// movq %r12, -184(%rbp) 1274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool LocalRewriter:: 1275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanOptimizeByUnfold2(unsigned VirtReg, int SS, 1276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock::iterator &MII, 1277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineInstr*> &MaybeDeadStores, 1278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AvailableSpills &Spills, 1279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector &RegKills, 1280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps) { 1281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock::iterator NextMII = llvm::next(MII); 1283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Skip over dbg_value instructions. 1284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman while (NextMII != MBB->end() && NextMII->isDebugValue()) 1285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NextMII = llvm::next(NextMII); 1286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (NextMII == MBB->end()) 1287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TII->getOpcodeAfterMemoryUnfold(MII->getOpcode(), true, true) == 0) 1290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Now let's see if the last couple of instructions happens to have freed up 1293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // a register. 1294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass* RC = MRI->getRegClass(VirtReg); 1295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned PhysReg = FindFreeRegister(MII, *MBB, RC, TRI, AllocatableRegs); 1296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!PhysReg) 1297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineFunction &MF = *MBB->getParent(); 1300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TRI = MF.getTarget().getRegisterInfo(); 1301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr &MI = *MII; 1302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!FoldsStackSlotModRef(MI, SS, PhysReg, TII, TRI, *VRM)) 1303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the next instruction also folds the same SS modref and can be unfoled, 1306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // then it's worthwhile to issue a load from SS into the free register and 1307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // then unfold these instructions. 1308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!FoldsStackSlotModRef(*NextMII, SS, PhysReg, TII, TRI, *VRM)) 1309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Back-schedule reloads and remats. 1312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ComputeReloadLoc(MII, MBB->begin(), PhysReg, TRI, false, SS, TII, MF); 1313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Load from SS to the spare physical register. 1315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TII->loadRegFromStackSlot(*MBB, MII, PhysReg, SS, RC, TRI); 1316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // This invalidates Phys. 1317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.ClobberPhysReg(PhysReg); 1318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Remember it's available. 1319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.addAvailable(SS, PhysReg); 1320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MaybeDeadStores[SS] = NULL; 1321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Unfold current MI. 1323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<MachineInstr*, 4> NewMIs; 1324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!TII->unfoldMemoryOperand(MF, &MI, VirtReg, false, false, NewMIs)) 1325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman llvm_unreachable("Unable unfold the load / store folding instruction!"); 1326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(NewMIs.size() == 1); 1327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AssignPhysToVirtReg(NewMIs[0], VirtReg, PhysReg, *TRI); 1328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->transferRestorePts(&MI, NewMIs[0]); 1329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MII = MBB->insert(MII, NewMIs[0]); 1330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman InvalidateKills(MI, TRI, RegKills, KillOps); 133119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman EraseInstr(&MI); 1332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumModRefUnfold; 1333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Unfold next instructions that fold the same SS. 1335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman do { 1336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr &NextMI = *NextMII; 1337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NextMII = llvm::next(NextMII); 1338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NewMIs.clear(); 1339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!TII->unfoldMemoryOperand(MF, &NextMI, VirtReg, false, false, NewMIs)) 1340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman llvm_unreachable("Unable unfold the load / store folding instruction!"); 1341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(NewMIs.size() == 1); 1342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AssignPhysToVirtReg(NewMIs[0], VirtReg, PhysReg, *TRI); 1343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->transferRestorePts(&NextMI, NewMIs[0]); 1344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MBB->insert(NextMII, NewMIs[0]); 1345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman InvalidateKills(NextMI, TRI, RegKills, KillOps); 134619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman EraseInstr(&NextMI); 1347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumModRefUnfold; 1348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Skip over dbg_value instructions. 1349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman while (NextMII != MBB->end() && NextMII->isDebugValue()) 1350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NextMII = llvm::next(NextMII); 1351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (NextMII == MBB->end()) 1352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 1353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } while (FoldsStackSlotModRef(*NextMII, SS, PhysReg, TII, TRI, *VRM)); 1354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Store the value back into SS. 1356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TII->storeRegToStackSlot(*MBB, NextMII, PhysReg, true, SS, RC, TRI); 1357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *StoreMI = prior(NextMII); 1358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->addSpillSlotUse(SS, StoreMI); 1359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->virtFolded(VirtReg, StoreMI, VirtRegMap::isMod); 1360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 1362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// OptimizeByUnfold - Turn a store folding instruction into a load folding 1365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// instruction. e.g. 1366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// xorl %edi, %eax 1367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// movl %eax, -32(%ebp) 1368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// movl -36(%ebp), %eax 1369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// orl %eax, -32(%ebp) 1370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ==> 1371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// xorl %edi, %eax 1372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// orl -36(%ebp), %eax 1373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// mov %eax, -32(%ebp) 1374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// This enables unfolding optimization for a subsequent instruction which will 1375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// also eliminate the newly introduced store instruction. 1376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool LocalRewriter:: 1377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanOptimizeByUnfold(MachineBasicBlock::iterator &MII, 1378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineInstr*> &MaybeDeadStores, 1379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AvailableSpills &Spills, 1380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector &RegKills, 1381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps) { 1382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineFunction &MF = *MBB->getParent(); 1383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr &MI = *MII; 1384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned UnfoldedOpc = 0; 1385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned UnfoldPR = 0; 1386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned UnfoldVR = 0; 1387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int FoldedSS = VirtRegMap::NO_STACK_SLOT; 1388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VirtRegMap::MI2VirtMapTy::const_iterator I, End; 1389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (tie(I, End) = VRM->getFoldedVirts(&MI); I != End; ) { 1390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Only transform a MI that folds a single register. 1391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (UnfoldedOpc) 1392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman UnfoldVR = I->second.first; 1394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VirtRegMap::ModRef MR = I->second.second; 1395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // MI2VirtMap be can updated which invalidate the iterator. 1396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Increment the iterator first. 1397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++I; 1398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (VRM->isAssignedReg(UnfoldVR)) 1399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 1400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this reference is not a use, any previous store is now dead. 1401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Otherwise, the store to this stack slot is not dead anymore. 1402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman FoldedSS = VRM->getStackSlot(UnfoldVR); 1403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr* DeadStore = MaybeDeadStores[FoldedSS]; 1404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (DeadStore && (MR & VirtRegMap::isModRef)) { 1405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(FoldedSS); 1406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!PhysReg || !DeadStore->readsRegister(PhysReg)) 1407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 1408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman UnfoldPR = PhysReg; 1409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman UnfoldedOpc = TII->getOpcodeAfterMemoryUnfold(MI.getOpcode(), 1410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman false, true); 1411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!UnfoldedOpc) { 1415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!UnfoldVR) 1416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Look for other unfolding opportunities. 1419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return OptimizeByUnfold2(UnfoldVR, FoldedSS, MII, MaybeDeadStores, Spills, 1420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RegKills, KillOps); 1421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 1424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineOperand &MO = MI.getOperand(i); 1425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!MO.isReg() || MO.getReg() == 0 || !MO.isUse()) 1426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 1427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned VirtReg = MO.getReg(); 1428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TargetRegisterInfo::isPhysicalRegister(VirtReg) || MO.getSubReg()) 1429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 1430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (VRM->isAssignedReg(VirtReg)) { 1431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned PhysReg = VRM->getPhys(VirtReg); 1432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (PhysReg && TRI->regsOverlap(PhysReg, UnfoldPR)) 1433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else if (VRM->isReMaterialized(VirtReg)) 1435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 1436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int SS = VRM->getStackSlot(VirtReg); 1437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS); 1438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (PhysReg) { 1439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TRI->regsOverlap(PhysReg, UnfoldPR)) 1440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 1442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (VRM->hasPhys(VirtReg)) { 1444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman PhysReg = VRM->getPhys(VirtReg); 1445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!TRI->regsOverlap(PhysReg, UnfoldPR)) 1446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 1447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Ok, we'll need to reload the value into a register which makes 1450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // it impossible to perform the store unfolding optimization later. 1451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Let's see if it is possible to fold the load if the store is 1452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // unfolded. This allows us to perform the store unfolding 1453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // optimization. 1454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<MachineInstr*, 4> NewMIs; 1455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TII->unfoldMemoryOperand(MF, &MI, UnfoldVR, false, false, NewMIs)) { 1456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(NewMIs.size() == 1); 1457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *NewMI = NewMIs.back(); 1458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MBB->insert(MII, NewMI); 1459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NewMIs.clear(); 1460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int Idx = NewMI->findRegisterUseOperandIdx(VirtReg, false); 1461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(Idx != -1); 1462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<unsigned, 1> Ops; 1463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Ops.push_back(Idx); 1464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *FoldedMI = TII->foldMemoryOperand(NewMI, Ops, SS); 1465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NewMI->eraseFromParent(); 1466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (FoldedMI) { 1467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->addSpillSlotUse(SS, FoldedMI); 1468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!VRM->hasPhys(UnfoldVR)) 1469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->assignVirt2Phys(UnfoldVR, UnfoldPR); 1470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->virtFolded(VirtReg, FoldedMI, VirtRegMap::isRef); 1471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MII = FoldedMI; 1472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman InvalidateKills(MI, TRI, RegKills, KillOps); 147319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman EraseInstr(&MI); 1474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 1475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CommuteChangesDestination - We are looking for r0 = op r1, r2 and 1483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// where SrcReg is r1 and it is tied to r0. Return true if after 1484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// commuting this instruction it will be r0 = op r2, r1. 1485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool CommuteChangesDestination(MachineInstr *DefMI, 148619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman const MCInstrDesc &MCID, 1487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned SrcReg, 1488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetInstrInfo *TII, 1489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned &DstIdx) { 149019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (MCID.getNumDefs() != 1 && MCID.getNumOperands() != 3) 1491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!DefMI->getOperand(1).isReg() || 1493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DefMI->getOperand(1).getReg() != SrcReg) 1494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned DefIdx; 1496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!DefMI->isRegTiedToDefOperand(1, &DefIdx) || DefIdx != 0) 1497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned SrcIdx1, SrcIdx2; 1499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!TII->findCommutedOpIndices(DefMI, SrcIdx1, SrcIdx2)) 1500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SrcIdx1 == 1 && SrcIdx2 == 2) { 1502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DstIdx = 2; 1503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 1504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CommuteToFoldReload - 1509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Look for 1510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// r1 = load fi#1 1511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// r1 = op r1, r2<kill> 1512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// store r1, fi#1 1513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// 1514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// If op is commutable and r2 is killed, then we can xform these to 1515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// r2 = op r2, fi#1 1516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// store r2, fi#1 1517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool LocalRewriter:: 1518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanCommuteToFoldReload(MachineBasicBlock::iterator &MII, 1519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned VirtReg, unsigned SrcReg, int SS, 1520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AvailableSpills &Spills, 1521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector &RegKills, 1522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps, 1523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterInfo *TRI) { 1524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MII == MBB->begin() || !MII->killsRegister(SrcReg)) 1525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr &MI = *MII; 1528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock::iterator DefMII = prior(MII); 1529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *DefMI = DefMII; 153019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman const MCInstrDesc &MCID = DefMI->getDesc(); 1531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned NewDstIdx; 1532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (DefMII != MBB->begin() && 153319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MCID.isCommutable() && 153419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman CommuteChangesDestination(DefMI, MCID, SrcReg, TII, NewDstIdx)) { 1535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineOperand &NewDstMO = DefMI->getOperand(NewDstIdx); 1536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned NewReg = NewDstMO.getReg(); 1537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!NewDstMO.isKill() || TRI->regsOverlap(NewReg, SrcReg)) 1538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *ReloadMI = prior(DefMII); 1540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int FrameIdx; 1541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned DestReg = TII->isLoadFromStackSlot(ReloadMI, FrameIdx); 1542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (DestReg != SrcReg || FrameIdx != SS) 1543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int UseIdx = DefMI->findRegisterUseOperandIdx(DestReg, false); 1545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (UseIdx == -1) 1546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned DefIdx; 1548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!MI.isRegTiedToDefOperand(UseIdx, &DefIdx)) 1549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(DefMI->getOperand(DefIdx).isReg() && 1551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DefMI->getOperand(DefIdx).getReg() == SrcReg); 1552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Now commute def instruction. 1554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *CommutedMI = TII->commuteInstruction(DefMI, true); 1555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!CommutedMI) 1556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MBB->insert(MII, CommutedMI); 1558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<unsigned, 1> Ops; 1559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Ops.push_back(NewDstIdx); 1560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *FoldedMI = TII->foldMemoryOperand(CommutedMI, Ops, SS); 1561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Not needed since foldMemoryOperand returns new MI. 1562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CommutedMI->eraseFromParent(); 1563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!FoldedMI) 1564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->addSpillSlotUse(SS, FoldedMI); 1567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->virtFolded(VirtReg, FoldedMI, VirtRegMap::isRef); 1568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Insert new def MI and spill MI. 1569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass* RC = MRI->getRegClass(VirtReg); 1570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TII->storeRegToStackSlot(*MBB, &MI, NewReg, true, SS, RC, TRI); 1571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MII = prior(MII); 1572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *StoreMI = MII; 1573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->addSpillSlotUse(SS, StoreMI); 1574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->virtFolded(VirtReg, StoreMI, VirtRegMap::isMod); 1575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MII = FoldedMI; // Update MII to backtrack. 1576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Delete all 3 old instructions. 1578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman InvalidateKills(*ReloadMI, TRI, RegKills, KillOps); 157919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman EraseInstr(ReloadMI); 1580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman InvalidateKills(*DefMI, TRI, RegKills, KillOps); 158119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman EraseInstr(DefMI); 1582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman InvalidateKills(MI, TRI, RegKills, KillOps); 158319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman EraseInstr(&MI); 1584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If NewReg was previously holding value of some SS, it's now clobbered. 1586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // This has to be done now because it's a physical register. When this 1587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // instruction is re-visited, it's ignored. 1588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.ClobberPhysReg(NewReg); 1589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumCommutes; 1591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 1592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// SpillRegToStackSlot - Spill a register to a specified stack slot. Check if 1598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the last store to the same slot is now dead. If so, remove the last store. 1599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid LocalRewriter:: 1600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSpillRegToStackSlot(MachineBasicBlock::iterator &MII, 1601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int Idx, unsigned PhysReg, int StackSlot, 1602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass *RC, 1603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isAvailable, MachineInstr *&LastStore, 1604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AvailableSpills &Spills, 1605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallSet<MachineInstr*, 4> &ReMatDefs, 1606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector &RegKills, 1607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps) { 1608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock::iterator oldNextMII = llvm::next(MII); 1610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TII->storeRegToStackSlot(*MBB, llvm::next(MII), PhysReg, true, StackSlot, RC, 1611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TRI); 1612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *StoreMI = prior(oldNextMII); 1613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->addSpillSlotUse(StackSlot, StoreMI); 1614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "Store:\t" << *StoreMI); 1615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If there is a dead store to this stack slot, nuke it now. 1617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (LastStore) { 1618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "Removed dead store:\t" << *LastStore); 1619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumDSE; 1620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<unsigned, 2> KillRegs; 1621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman InvalidateKills(*LastStore, TRI, RegKills, KillOps, &KillRegs); 1622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock::iterator PrevMII = LastStore; 1623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool CheckDef = PrevMII != MBB->begin(); 1624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (CheckDef) 1625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman --PrevMII; 162619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman EraseInstr(LastStore); 1627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (CheckDef) { 1628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Look at defs of killed registers on the store. Mark the defs 1629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // as dead since the store has been deleted and they aren't 1630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // being reused. 1631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned j = 0, ee = KillRegs.size(); j != ee; ++j) { 1632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool HasOtherDef = false; 1633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (InvalidateRegDef(PrevMII, *MII, KillRegs[j], HasOtherDef, TRI)) { 1634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *DeadDef = PrevMII; 1635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ReMatDefs.count(DeadDef) && !HasOtherDef) { 1636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // FIXME: This assumes a remat def does not have side effects. 163719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman EraseInstr(DeadDef); 1638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumDRM; 1639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Allow for multi-instruction spill sequences, as on PPC Altivec. Presume 1646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // the last of multiple instructions is the actual store. 1647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LastStore = prior(oldNextMII); 1648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the stack slot value was previously available in some other 1650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // register, change it now. Otherwise, make the register available, 1651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // in PhysReg. 1652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.ModifyStackSlotOrReMat(StackSlot); 1653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.ClobberPhysReg(PhysReg); 1654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.addAvailable(StackSlot, PhysReg, isAvailable); 1655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumStores; 1656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isSafeToDelete - Return true if this instruction doesn't produce any side 1659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// effect and all of its defs are dead. 1660894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isSafeToDelete(MachineInstr &MI) { 166119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman const MCInstrDesc &MCID = MI.getDesc(); 166219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (MCID.mayLoad() || MCID.mayStore() || MCID.isTerminator() || 166319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MCID.isCall() || MCID.isBarrier() || MCID.isReturn() || 166419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MI.isLabel() || MI.isDebugValue() || 166519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MI.hasUnmodeledSideEffects()) 166619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return false; 166719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 166819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Technically speaking inline asm without side effects and no defs can still 166919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // be deleted. But there is so much bad inline asm code out there, we should 167019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // let them be. 167119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (MI.isInlineAsm()) 1672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 167319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 1675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineOperand &MO = MI.getOperand(i); 1676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!MO.isReg() || !MO.getReg()) 1677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 1678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MO.isDef() && !MO.isDead()) 1679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MO.isUse() && MO.isKill()) 1681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // FIXME: We can't remove kill markers or else the scavenger will assert. 1682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // An alternative is to add a ADD pseudo instruction to replace kill 1683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // markers. 1684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 1687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// TransferDeadness - A identity copy definition is dead and it's being 1690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// removed. Find the last def or use and mark it as dead / kill. 1691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid LocalRewriter:: 1692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanTransferDeadness(unsigned Reg, BitVector &RegKills, 1693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps) { 1694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallPtrSet<MachineInstr*, 4> Seens; 1695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<std::pair<MachineInstr*, int>,8> Refs; 1696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (MachineRegisterInfo::reg_iterator RI = MRI->reg_begin(Reg), 1697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RE = MRI->reg_end(); RI != RE; ++RI) { 1698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *UDMI = &*RI; 1699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (UDMI->isDebugValue() || UDMI->getParent() != MBB) 1700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 1701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UDMI); 1702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (DI == DistanceMap.end()) 1703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 1704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Seens.insert(UDMI)) 1705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Refs.push_back(std::make_pair(UDMI, DI->second)); 1706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Refs.empty()) 1709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return; 1710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::sort(Refs.begin(), Refs.end(), RefSorter()); 1711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman while (!Refs.empty()) { 1713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *LastUDMI = Refs.back().first; 1714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Refs.pop_back(); 1715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineOperand *LastUD = NULL; 1717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = LastUDMI->getNumOperands(); i != e; ++i) { 1718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineOperand &MO = LastUDMI->getOperand(i); 1719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!MO.isReg() || MO.getReg() != Reg) 1720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 1721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!LastUD || (LastUD->isUse() && MO.isDef())) 1722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LastUD = &MO; 1723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (LastUDMI->isRegTiedToDefOperand(i)) 1724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 1725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (LastUD->isDef()) { 1727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the instruction has no side effect, delete it and propagate 1728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // backward further. Otherwise, mark is dead and we are done. 1729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!isSafeToDelete(*LastUDMI)) { 1730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LastUD->setIsDead(); 1731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 1732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 173319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman EraseInstr(LastUDMI); 1734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else { 1735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LastUD->setIsKill(); 1736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RegKills.set(Reg); 1737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman KillOps[Reg] = LastUD; 1738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 1739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// InsertEmergencySpills - Insert emergency spills before MI if requested by 1744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// VRM. Return true if spills were inserted. 1745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool LocalRewriter::InsertEmergencySpills(MachineInstr *MI) { 1746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!VRM->hasEmergencySpills(MI)) 1747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock::iterator MII = MI; 1749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallSet<int, 4> UsedSS; 1750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<unsigned> &EmSpills = VRM->getEmergencySpills(MI); 1751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = EmSpills.size(); i != e; ++i) { 1752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned PhysReg = EmSpills[i]; 1753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(PhysReg); 1754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(RC && "Unable to determine register class!"); 1755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int SS = VRM->getEmergencySpillSlot(RC); 1756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (UsedSS.count(SS)) 1757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman llvm_unreachable("Need to spill more than one physical registers!"); 1758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman UsedSS.insert(SS); 1759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TII->storeRegToStackSlot(*MBB, MII, PhysReg, true, SS, RC, TRI); 1760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *StoreMI = prior(MII); 1761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->addSpillSlotUse(SS, StoreMI); 1762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Back-schedule reloads and remats. 1764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock::iterator InsertLoc = 1765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ComputeReloadLoc(llvm::next(MII), MBB->begin(), PhysReg, TRI, false, SS, 1766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TII, *MBB->getParent()); 1767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TII->loadRegFromStackSlot(*MBB, InsertLoc, PhysReg, SS, RC, TRI); 1769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *LoadMI = prior(InsertLoc); 1771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->addSpillSlotUse(SS, LoadMI); 1772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumPSpills; 1773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DistanceMap.insert(std::make_pair(LoadMI, DistanceMap.size())); 1774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 1776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// InsertRestores - Restore registers before MI is requested by VRM. Return 1779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// true is any instructions were inserted. 1780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool LocalRewriter::InsertRestores(MachineInstr *MI, 1781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AvailableSpills &Spills, 1782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector &RegKills, 1783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps) { 1784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!VRM->isRestorePt(MI)) 1785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock::iterator MII = MI; 1787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<unsigned> &RestoreRegs = VRM->getRestorePtRestores(MI); 1788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = RestoreRegs.size(); i != e; ++i) { 1789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned VirtReg = RestoreRegs[e-i-1]; // Reverse order. 1790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!VRM->getPreSplitReg(VirtReg)) 1791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; // Split interval spilled again. 1792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Phys = VRM->getPhys(VirtReg); 1793894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MRI->setPhysRegUsed(Phys); 1794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1795894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Check if the value being restored if available. If so, it must be 1796894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // from a predecessor BB that fallthrough into this BB. We do not 1797894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // expect: 1798894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // BB1: 1799894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // r1 = load fi#1 1800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // ... 1801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // = r1<kill> 1802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // ... # r1 not clobbered 1803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // ... 1804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // = load fi#1 1805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool DoReMat = VRM->isReMaterialized(VirtReg); 1806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int SSorRMId = DoReMat 1807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ? VRM->getReMatId(VirtReg) : VRM->getStackSlot(VirtReg); 1808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned InReg = Spills.getSpillSlotOrReMatPhysReg(SSorRMId); 1809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (InReg == Phys) { 1810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the value is already available in the expected register, save 1811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // a reload / remat. 1812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SSorRMId) 1813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "Reusing RM#" 1814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman << SSorRMId-VirtRegMap::MAX_STACK_SLOT-1); 1815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman else 1816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "Reusing SS#" << SSorRMId); 1817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << " from physreg " 181819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman << TRI->getName(InReg) << " for " << PrintReg(VirtReg) 181919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman <<" instead of reloading into physreg " 1820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman << TRI->getName(Phys) << '\n'); 182119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 182219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Reusing a physreg may resurrect it. But we expect ProcessUses to update 182319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // the kill flags for the current instruction after processing it. 182419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumOmitted; 1826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 1827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else if (InReg && InReg != Phys) { 1828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SSorRMId) 1829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "Reusing RM#" 1830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman << SSorRMId-VirtRegMap::MAX_STACK_SLOT-1); 1831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman else 1832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "Reusing SS#" << SSorRMId); 1833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << " from physreg " 183419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman << TRI->getName(InReg) << " for " << PrintReg(VirtReg) 183519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman <<" by copying it into physreg " 1836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman << TRI->getName(Phys) << '\n'); 1837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the reloaded / remat value is available in another register, 1839894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // copy it to the desired register. 1840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Back-schedule reloads and remats. 1842894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock::iterator InsertLoc = 1843894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ComputeReloadLoc(MII, MBB->begin(), Phys, TRI, DoReMat, SSorRMId, TII, 1844894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman *MBB->getParent()); 1845894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *CopyMI = BuildMI(*MBB, InsertLoc, MI->getDebugLoc(), 1846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TII->get(TargetOpcode::COPY), Phys) 1847894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman .addReg(InReg, RegState::Kill); 1848894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // This invalidates Phys. 1850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.ClobberPhysReg(Phys); 1851894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Remember it's available. 1852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.addAvailable(SSorRMId, Phys); 1853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CopyMI->setAsmPrinterFlag(MachineInstr::ReloadReuse); 1855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman UpdateKills(*CopyMI, TRI, RegKills, KillOps); 1856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1857894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << '\t' << *CopyMI); 1858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumCopified; 1859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 1860894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Back-schedule reloads and remats. 1863894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock::iterator InsertLoc = 1864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ComputeReloadLoc(MII, MBB->begin(), Phys, TRI, DoReMat, SSorRMId, TII, 1865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman *MBB->getParent()); 1866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (VRM->isReMaterialized(VirtReg)) { 1868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ReMaterialize(*MBB, InsertLoc, Phys, VirtReg, TII, TRI, *VRM); 1869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else { 1870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass* RC = MRI->getRegClass(VirtReg); 1871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TII->loadRegFromStackSlot(*MBB, InsertLoc, Phys, SSorRMId, RC, TRI); 1872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *LoadMI = prior(InsertLoc); 1873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->addSpillSlotUse(SSorRMId, LoadMI); 1874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumLoads; 1875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DistanceMap.insert(std::make_pair(LoadMI, DistanceMap.size())); 1876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // This invalidates Phys. 1879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.ClobberPhysReg(Phys); 1880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Remember it's available. 1881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.addAvailable(SSorRMId, Phys); 1882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman UpdateKills(*prior(InsertLoc), TRI, RegKills, KillOps); 1884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << '\t' << *prior(MII)); 1885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 1887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 188919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// InsertSpills - Insert spills after MI if requested by VRM. Return 1890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// true if spills were inserted. 1891894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool LocalRewriter::InsertSpills(MachineInstr *MI) { 1892894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!VRM->isSpillPt(MI)) 1893894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1894894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock::iterator MII = MI; 1895894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<std::pair<unsigned,bool> > &SpillRegs = 1896894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->getSpillPtSpills(MI); 1897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = SpillRegs.size(); i != e; ++i) { 1898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned VirtReg = SpillRegs[i].first; 1899894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isKill = SpillRegs[i].second; 1900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!VRM->getPreSplitReg(VirtReg)) 1901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; // Split interval spilled again. 1902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass *RC = MRI->getRegClass(VirtReg); 1903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Phys = VRM->getPhys(VirtReg); 1904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int StackSlot = VRM->getStackSlot(VirtReg); 1905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock::iterator oldNextMII = llvm::next(MII); 1906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TII->storeRegToStackSlot(*MBB, llvm::next(MII), Phys, isKill, StackSlot, 1907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RC, TRI); 1908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *StoreMI = prior(oldNextMII); 1909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->addSpillSlotUse(StackSlot, StoreMI); 1910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "Store:\t" << *StoreMI); 1911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->virtFolded(VirtReg, StoreMI, VirtRegMap::isMod); 1912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 1914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 191719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// ProcessUses - Process all of MI's spilled operands and all available 191819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// operands. 191919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid LocalRewriter::ProcessUses(MachineInstr &MI, AvailableSpills &Spills, 192019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman std::vector<MachineInstr*> &MaybeDeadStores, 192119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman BitVector &RegKills, 192219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ReuseInfo &ReusedOperands, 192319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman std::vector<MachineOperand*> &KillOps) { 192419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Clear kill info. 192519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman SmallSet<unsigned, 2> KilledMIRegs; 192619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman SmallVector<unsigned, 4> VirtUseOps; 192719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 192819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MachineOperand &MO = MI.getOperand(i); 192919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (!MO.isReg() || MO.getReg() == 0) 193019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman continue; // Ignore non-register operands. 193119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 193219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned VirtReg = MO.getReg(); 193319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 193419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (TargetRegisterInfo::isPhysicalRegister(VirtReg)) { 193519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Ignore physregs for spilling, but remember that it is used by this 193619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // function. 193719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MRI->setPhysRegUsed(VirtReg); 193819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman continue; 193919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 194019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 194119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // We want to process implicit virtual register uses first. 194219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (MO.isImplicit()) 194319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If the virtual register is implicitly defined, emit a implicit_def 194419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // before so scavenger knows it's "defined". 194519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // FIXME: This is a horrible hack done the by register allocator to 194619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // remat a definition with virtual register operand. 194719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman VirtUseOps.insert(VirtUseOps.begin(), i); 194819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman else 194919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman VirtUseOps.push_back(i); 195019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 195119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // A partial def causes problems because the same operand both reads and 195219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // writes the register. This rewriter is designed to rewrite uses and defs 195319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // separately, so a partial def would already have been rewritten to a 195419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // physreg by the time we get to processing defs. 195519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Add an implicit use operand to model the partial def. 195619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (MO.isDef() && MO.getSubReg() && MI.readsVirtualRegister(VirtReg) && 195719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MI.findRegisterUseOperandIdx(VirtReg) == -1) { 195819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman VirtUseOps.insert(VirtUseOps.begin(), MI.getNumOperands()); 195919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MI.addOperand(MachineOperand::CreateReg(VirtReg, 196019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman false, // isDef 196119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman true)); // isImplicit 196219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DEBUG(dbgs() << "Partial redef: " << MI); 196319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 196419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 196519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 196619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Process all of the spilled uses and all non spilled reg references. 196719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman SmallVector<int, 2> PotentialDeadStoreSlots; 196819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman KilledMIRegs.clear(); 196919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman for (unsigned j = 0, e = VirtUseOps.size(); j != e; ++j) { 197019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned i = VirtUseOps[j]; 197119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned VirtReg = MI.getOperand(i).getReg(); 197219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && 197319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman "Not a virtual register?"); 197419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 197519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned SubIdx = MI.getOperand(i).getSubReg(); 197619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (VRM->isAssignedReg(VirtReg)) { 197719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // This virtual register was assigned a physreg! 197819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned Phys = VRM->getPhys(VirtReg); 197919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MRI->setPhysRegUsed(Phys); 198019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (MI.getOperand(i).isDef()) 198119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ReusedOperands.markClobbered(Phys); 198219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman substitutePhysReg(MI.getOperand(i), Phys, *TRI); 198319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (VRM->isImplicitlyDefined(VirtReg)) 198419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // FIXME: Is this needed? 198519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman BuildMI(*MBB, &MI, MI.getDebugLoc(), 198619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman TII->get(TargetOpcode::IMPLICIT_DEF), Phys); 198719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman continue; 198819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 198919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 199019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // This virtual register is now known to be a spilled value. 199119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (!MI.getOperand(i).isUse()) 199219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman continue; // Handle defs in the loop below (handle use&def here though) 199319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 199419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman bool AvoidReload = MI.getOperand(i).isUndef(); 199519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Check if it is defined by an implicit def. It should not be spilled. 199619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Note, this is for correctness reason. e.g. 199719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // 8 %reg1024<def> = IMPLICIT_DEF 199819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2 199919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // The live range [12, 14) are not part of the r1024 live interval since 200019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // it's defined by an implicit def. It will not conflicts with live 200119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // interval of r1025. Now suppose both registers are spilled, you can 200219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // easily see a situation where both registers are reloaded before 200319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // the INSERT_SUBREG and both target registers that would overlap. 200419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman bool DoReMat = VRM->isReMaterialized(VirtReg); 200519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman int SSorRMId = DoReMat 200619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ? VRM->getReMatId(VirtReg) : VRM->getStackSlot(VirtReg); 200719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman int ReuseSlot = SSorRMId; 200819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 200919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Check to see if this stack slot is available. 201019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SSorRMId); 201119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 201219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If this is a sub-register use, make sure the reuse register is in the 201319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // right register class. For example, for x86 not all of the 32-bit 201419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // registers have accessible sub-registers. 201519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Similarly so for EXTRACT_SUBREG. Consider this: 201619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // EDI = op 201719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // MOV32_mr fi#1, EDI 201819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // ... 201919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // = EXTRACT_SUBREG fi#1 202019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // fi#1 is available in EDI, but it cannot be reused because it's not in 202119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // the right register file. 202219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (PhysReg && !AvoidReload && SubIdx) { 202319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman const TargetRegisterClass* RC = MRI->getRegClass(VirtReg); 202419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (!RC->contains(PhysReg)) 202519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman PhysReg = 0; 202619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 202719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 202819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (PhysReg && !AvoidReload) { 202919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // This spilled operand might be part of a two-address operand. If this 203019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // is the case, then changing it will necessarily require changing the 203119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // def part of the instruction as well. However, in some cases, we 203219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // aren't allowed to modify the reused register. If none of these cases 203319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // apply, reuse it. 203419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman bool CanReuse = true; 203519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman bool isTied = MI.isRegTiedToDefOperand(i); 203619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (isTied) { 203719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Okay, we have a two address operand. We can reuse this physreg as 203819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // long as we are allowed to clobber the value and there isn't an 203919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // earlier def that has already clobbered the physreg. 204019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman CanReuse = !ReusedOperands.isClobbered(PhysReg) && 204119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Spills.canClobberPhysReg(PhysReg); 204219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 204319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If this is an asm, and a PhysReg alias is used elsewhere as an 204419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // earlyclobber operand, we can't also use it as an input. 204519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (MI.isInlineAsm()) { 204619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman for (unsigned k = 0, e = MI.getNumOperands(); k != e; ++k) { 204719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MachineOperand &MOk = MI.getOperand(k); 204819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (MOk.isReg() && MOk.isEarlyClobber() && 204919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman TRI->regsOverlap(MOk.getReg(), PhysReg)) { 205019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman CanReuse = false; 205119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DEBUG(dbgs() << "Not reusing physreg " << TRI->getName(PhysReg) 205219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman << " for " << PrintReg(VirtReg) << ": " << MOk 205319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman << '\n'); 205419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman break; 205519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 205619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 205719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 205819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 205919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (CanReuse) { 206019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If this stack slot value is already available, reuse it! 206119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (ReuseSlot > VirtRegMap::MAX_STACK_SLOT) 206219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DEBUG(dbgs() << "Reusing RM#" 206319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman << ReuseSlot-VirtRegMap::MAX_STACK_SLOT-1); 206419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman else 206519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DEBUG(dbgs() << "Reusing SS#" << ReuseSlot); 206619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DEBUG(dbgs() << " from physreg " 206719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman << TRI->getName(PhysReg) << " for " << PrintReg(VirtReg) 206819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman << " instead of reloading into " 206919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman << PrintReg(VRM->getPhys(VirtReg), TRI) << '\n'); 207019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg; 207119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MI.getOperand(i).setReg(RReg); 207219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MI.getOperand(i).setSubReg(0); 207319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 207419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Reusing a physreg may resurrect it. But we expect ProcessUses to 207519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // update the kill flags for the current instr after processing it. 207619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 207719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // The only technical detail we have is that we don't know that 207819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // PhysReg won't be clobbered by a reloaded stack slot that occurs 207919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // later in the instruction. In particular, consider 'op V1, V2'. 208019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If V1 is available in physreg R0, we would choose to reuse it 208119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // here, instead of reloading it into the register the allocator 208219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // indicated (say R1). However, V2 might have to be reloaded 208319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // later, and it might indicate that it needs to live in R0. When 208419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // this occurs, we need to have information available that 208519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // indicates it is safe to use R1 for the reload instead of R0. 208619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // 208719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // To further complicate matters, we might conflict with an alias, 208819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // or R0 and R1 might not be compatible with each other. In this 208919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // case, we actually insert a reload for V1 in R1, ensuring that 209019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // we can get at R0 or its alias. 209119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ReusedOperands.addReuse(i, ReuseSlot, PhysReg, 209219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman VRM->getPhys(VirtReg), VirtReg); 209319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (isTied) 209419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Only mark it clobbered if this is a use&def operand. 209519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ReusedOperands.markClobbered(PhysReg); 209619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ++NumReused; 209719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 209819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (MI.getOperand(i).isKill() && 209919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ReuseSlot <= VirtRegMap::MAX_STACK_SLOT) { 210019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 210119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // The store of this spilled value is potentially dead, but we 210219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // won't know for certain until we've confirmed that the re-use 210319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // above is valid, which means waiting until the other operands 210419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // are processed. For now we just track the spill slot, we'll 210519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // remove it after the other operands are processed if valid. 210619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 210719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman PotentialDeadStoreSlots.push_back(ReuseSlot); 210819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 210919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 211019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Mark is isKill if it's there no other uses of the same virtual 211119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // register and it's not a two-address operand. IsKill will be 211219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // unset if reg is reused. 211319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (!isTied && KilledMIRegs.count(VirtReg) == 0) { 211419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MI.getOperand(i).setIsKill(); 211519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman KilledMIRegs.insert(VirtReg); 211619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 211719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman continue; 211819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } // CanReuse 211919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 212019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Otherwise we have a situation where we have a two-address instruction 212119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // whose mod/ref operand needs to be reloaded. This reload is already 212219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // available in some register "PhysReg", but if we used PhysReg as the 212319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // operand to our 2-addr instruction, the instruction would modify 212419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // PhysReg. This isn't cool if something later uses PhysReg and expects 212519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // to get its initial value. 212619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // 212719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // To avoid this problem, and to avoid doing a load right after a store, 212819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // we emit a copy from PhysReg into the designated register for this 212919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // operand. 213019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // 213119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // This case also applies to an earlyclobber'd PhysReg. 213219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned DesignatedReg = VRM->getPhys(VirtReg); 213319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman assert(DesignatedReg && "Must map virtreg to physreg!"); 213419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 213519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Note that, if we reused a register for a previous operand, the 213619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // register we want to reload into might not actually be 213719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // available. If this occurs, use the register indicated by the 213819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // reuser. 213919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (ReusedOperands.hasReuses()) 214019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DesignatedReg = ReusedOperands. 214119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman GetRegForReload(VirtReg, DesignatedReg, &MI, Spills, 214219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MaybeDeadStores, RegKills, KillOps, *VRM); 214319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 214419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If the mapped designated register is actually the physreg we have 214519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // incoming, we don't need to inserted a dead copy. 214619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (DesignatedReg == PhysReg) { 214719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If this stack slot value is already available, reuse it! 214819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (ReuseSlot > VirtRegMap::MAX_STACK_SLOT) 214919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DEBUG(dbgs() << "Reusing RM#" 215019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman << ReuseSlot-VirtRegMap::MAX_STACK_SLOT-1); 215119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman else 215219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DEBUG(dbgs() << "Reusing SS#" << ReuseSlot); 215319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DEBUG(dbgs() << " from physreg " << TRI->getName(PhysReg) 215419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman << " for " << PrintReg(VirtReg) 215519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman << " instead of reloading into same physreg.\n"); 215619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg; 215719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MI.getOperand(i).setReg(RReg); 215819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MI.getOperand(i).setSubReg(0); 215919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ReusedOperands.markClobbered(RReg); 216019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ++NumReused; 216119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman continue; 216219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 216319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 216419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MRI->setPhysRegUsed(DesignatedReg); 216519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ReusedOperands.markClobbered(DesignatedReg); 216619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 216719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Back-schedule reloads and remats. 216819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MachineBasicBlock::iterator InsertLoc = 216919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ComputeReloadLoc(&MI, MBB->begin(), PhysReg, TRI, DoReMat, 217019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman SSorRMId, TII, *MBB->getParent()); 217119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MachineInstr *CopyMI = BuildMI(*MBB, InsertLoc, MI.getDebugLoc(), 217219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman TII->get(TargetOpcode::COPY), 217319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DesignatedReg).addReg(PhysReg); 217419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman CopyMI->setAsmPrinterFlag(MachineInstr::ReloadReuse); 217519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman UpdateKills(*CopyMI, TRI, RegKills, KillOps); 217619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 217719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // This invalidates DesignatedReg. 217819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Spills.ClobberPhysReg(DesignatedReg); 217919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 218019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Spills.addAvailable(ReuseSlot, DesignatedReg); 218119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned RReg = 218219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman SubIdx ? TRI->getSubReg(DesignatedReg, SubIdx) : DesignatedReg; 218319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MI.getOperand(i).setReg(RReg); 218419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MI.getOperand(i).setSubReg(0); 218519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DEBUG(dbgs() << '\t' << *prior(InsertLoc)); 218619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ++NumReused; 218719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman continue; 218819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } // if (PhysReg) 218919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 219019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Otherwise, reload it and remember that we have it. 219119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman PhysReg = VRM->getPhys(VirtReg); 219219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman assert(PhysReg && "Must map virtreg to physreg!"); 219319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 219419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Note that, if we reused a register for a previous operand, the 219519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // register we want to reload into might not actually be 219619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // available. If this occurs, use the register indicated by the 219719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // reuser. 219819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (ReusedOperands.hasReuses()) 219919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman PhysReg = ReusedOperands.GetRegForReload(VirtReg, PhysReg, &MI, 220019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Spills, MaybeDeadStores, RegKills, KillOps, *VRM); 220119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 220219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MRI->setPhysRegUsed(PhysReg); 220319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ReusedOperands.markClobbered(PhysReg); 220419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (AvoidReload) 220519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ++NumAvoided; 220619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman else { 220719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Back-schedule reloads and remats. 220819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MachineBasicBlock::iterator InsertLoc = 220919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ComputeReloadLoc(MI, MBB->begin(), PhysReg, TRI, DoReMat, 221019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman SSorRMId, TII, *MBB->getParent()); 221119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 221219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (DoReMat) { 221319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ReMaterialize(*MBB, InsertLoc, PhysReg, VirtReg, TII, TRI, *VRM); 221419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } else { 221519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman const TargetRegisterClass* RC = MRI->getRegClass(VirtReg); 221619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman TII->loadRegFromStackSlot(*MBB, InsertLoc, PhysReg, SSorRMId, RC,TRI); 221719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MachineInstr *LoadMI = prior(InsertLoc); 221819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman VRM->addSpillSlotUse(SSorRMId, LoadMI); 221919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ++NumLoads; 222019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DistanceMap.insert(std::make_pair(LoadMI, DistanceMap.size())); 222119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 222219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // This invalidates PhysReg. 222319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Spills.ClobberPhysReg(PhysReg); 222419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 222519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Any stores to this stack slot are not dead anymore. 222619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (!DoReMat) 222719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MaybeDeadStores[SSorRMId] = NULL; 222819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Spills.addAvailable(SSorRMId, PhysReg); 222919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Assumes this is the last use. IsKill will be unset if reg is reused 223019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // unless it's a two-address operand. 223119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (!MI.isRegTiedToDefOperand(i) && 223219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman KilledMIRegs.count(VirtReg) == 0) { 223319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MI.getOperand(i).setIsKill(); 223419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman KilledMIRegs.insert(VirtReg); 223519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 223619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 223719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman UpdateKills(*prior(InsertLoc), TRI, RegKills, KillOps); 223819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DEBUG(dbgs() << '\t' << *prior(InsertLoc)); 223919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 224019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg; 224119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MI.getOperand(i).setReg(RReg); 224219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MI.getOperand(i).setSubReg(0); 224319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 224419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 224519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Ok - now we can remove stores that have been confirmed dead. 224619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman for (unsigned j = 0, e = PotentialDeadStoreSlots.size(); j != e; ++j) { 224719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // This was the last use and the spilled value is still available 224819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // for reuse. That means the spill was unnecessary! 224919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman int PDSSlot = PotentialDeadStoreSlots[j]; 225019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MachineInstr* DeadStore = MaybeDeadStores[PDSSlot]; 225119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (DeadStore) { 225219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DEBUG(dbgs() << "Removed dead store:\t" << *DeadStore); 225319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman InvalidateKills(*DeadStore, TRI, RegKills, KillOps); 225419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman EraseInstr(DeadStore); 225519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman MaybeDeadStores[PDSSlot] = NULL; 225619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ++NumDSE; 225719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 225819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 225919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 226019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 2261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// rewriteMBB - Keep track of which spills are available even after the 2262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// register allocator is done with them. If possible, avoid reloading vregs. 2263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid 2264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanLocalRewriter::RewriteMBB(LiveIntervals *LIs, 2265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AvailableSpills &Spills, BitVector &RegKills, 2266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineOperand*> &KillOps) { 2267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "\n**** Local spiller rewriting MBB '" 2269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman << MBB->getName() << "':\n"); 2270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineFunction &MF = *MBB->getParent(); 2272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // MaybeDeadStores - When we need to write a value back into a stack slot, 2274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // keep track of the inserted store. If the stack slot value is never read 2275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // (because the value was used from some available register, for example), and 2276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // subsequently stored to, the original store is dead. This map keeps track 2277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // of inserted stores that are not used. If we see a subsequent store to the 2278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // same stack slot, the original store is deleted. 2279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<MachineInstr*> MaybeDeadStores; 2280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MaybeDeadStores.resize(MF.getFrameInfo()->getObjectIndexEnd(), NULL); 2281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // ReMatDefs - These are rematerializable def MIs which are not deleted. 2283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallSet<MachineInstr*, 4> ReMatDefs; 2284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Keep track of the registers we have already spilled in case there are 2286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // multiple defs of the same register in MI. 2287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallSet<unsigned, 8> SpilledMIRegs; 2288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RegKills.reset(); 2290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman KillOps.clear(); 2291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman KillOps.resize(TRI->getNumRegs(), NULL); 2292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DistanceMap.clear(); 2294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (MachineBasicBlock::iterator MII = MBB->begin(), E = MBB->end(); 2295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MII != E; ) { 2296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock::iterator NextMII = llvm::next(MII); 2297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (OptimizeByUnfold(MII, MaybeDeadStores, Spills, RegKills, KillOps)) 2299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NextMII = llvm::next(MII); 2300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (InsertEmergencySpills(MII)) 2302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NextMII = llvm::next(MII); 2303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman InsertRestores(MII, Spills, RegKills, KillOps); 2305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (InsertSpills(MII)) 2307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NextMII = llvm::next(MII); 2308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool Erased = false; 2310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool BackTracked = false; 2311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr &MI = *MII; 2312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Remember DbgValue's which reference stack slots. 2314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MI.isDebugValue() && MI.getOperand(0).isFI()) 2315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Slot2DbgValues[MI.getOperand(0).getIndex()].push_back(&MI); 2316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// ReusedOperands - Keep track of operand reuse in case we need to undo 2318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// reuse. 2319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ReuseInfo ReusedOperands(MI, TRI); 2320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 232119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ProcessUses(MI, Spills, MaybeDeadStores, RegKills, ReusedOperands, KillOps); 2322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << '\t' << MI); 2324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If we have folded references to memory operands, make sure we clear all 2327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // physical registers that may contain the value of the spilled virtual 2328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // register 2329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Copy the folded virts to a small vector, we may change MI2VirtMap. 2331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<std::pair<unsigned, VirtRegMap::ModRef>, 4> FoldedVirts; 2332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // C++0x FTW! 2333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (std::pair<VirtRegMap::MI2VirtMapTy::const_iterator, 2334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VirtRegMap::MI2VirtMapTy::const_iterator> FVRange = 2335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->getFoldedVirts(&MI); 2336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman FVRange.first != FVRange.second; ++FVRange.first) 2337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman FoldedVirts.push_back(FVRange.first->second); 2338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallSet<int, 2> FoldedSS; 2340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned FVI = 0, FVE = FoldedVirts.size(); FVI != FVE; ++FVI) { 2341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned VirtReg = FoldedVirts[FVI].first; 2342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VirtRegMap::ModRef MR = FoldedVirts[FVI].second; 234319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DEBUG(dbgs() << "Folded " << PrintReg(VirtReg) << " MR: " << MR); 2344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int SS = VRM->getStackSlot(VirtReg); 2346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SS == VirtRegMap::NO_STACK_SLOT) 2347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 2348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman FoldedSS.insert(SS); 2349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << " - StackSlot: " << SS << "\n"); 2350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this folded instruction is just a use, check to see if it's a 2352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // straight load from the virt reg slot. 2353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if ((MR & VirtRegMap::isRef) && !(MR & VirtRegMap::isMod)) { 2354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int FrameIdx; 2355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx); 2356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (DestReg && FrameIdx == SS) { 2357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this spill slot is available, turn it into a copy (or nothing) 2358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // instead of leaving it as a load! 2359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (unsigned InReg = Spills.getSpillSlotOrReMatPhysReg(SS)) { 2360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "Promoted Load To Copy: " << MI); 2361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (DestReg != InReg) { 2362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineOperand *DefMO = MI.findRegisterDefOperand(DestReg); 2363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *CopyMI = BuildMI(*MBB, &MI, MI.getDebugLoc(), 2364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TII->get(TargetOpcode::COPY)) 2365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman .addReg(DestReg, RegState::Define, DefMO->getSubReg()) 2366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman .addReg(InReg, RegState::Kill); 2367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Revisit the copy so we make sure to notice the effects of the 2368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // operation on the destreg (either needing to RA it if it's 2369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // virtual or needing to clobber any values if it's physical). 2370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NextMII = CopyMI; 2371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NextMII->setAsmPrinterFlag(MachineInstr::ReloadReuse); 2372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BackTracked = true; 2373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else { 2374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "Removing now-noop copy: " << MI); 237519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // InvalidateKills resurrects any prior kill of the copy's source 237619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // allowing the source reg to be reused in place of the copy. 2377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.disallowClobberPhysReg(InReg); 2378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman InvalidateKills(MI, TRI, RegKills, KillOps); 238119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman EraseInstr(&MI); 2382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Erased = true; 2383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman goto ProcessNextInst; 2384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else { 2386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS); 2387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<MachineInstr*, 4> NewMIs; 2388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (PhysReg && 2389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TII->unfoldMemoryOperand(MF, &MI, PhysReg, false, false, NewMIs)){ 2390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MBB->insert(MII, NewMIs[0]); 2391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman InvalidateKills(MI, TRI, RegKills, KillOps); 239219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman EraseInstr(&MI); 2393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Erased = true; 2394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman --NextMII; // backtrack to the unfolded instruction. 2395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BackTracked = true; 2396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman goto ProcessNextInst; 2397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this reference is not a use, any previous store is now dead. 2402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Otherwise, the store to this stack slot is not dead anymore. 2403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr* DeadStore = MaybeDeadStores[SS]; 2404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (DeadStore) { 2405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isDead = !(MR & VirtRegMap::isRef); 2406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *NewStore = NULL; 2407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MR & VirtRegMap::isModRef) { 2408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS); 2409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<MachineInstr*, 4> NewMIs; 2410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We can reuse this physreg as long as we are allowed to clobber 2411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // the value and there isn't an earlier def that has already clobbered 2412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // the physreg. 2413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (PhysReg && 2414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman !ReusedOperands.isClobbered(PhysReg) && 2415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.canClobberPhysReg(PhysReg) && 2416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman !TII->isStoreToStackSlot(&MI, SS)) { // Not profitable! 2417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineOperand *KillOpnd = 2418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DeadStore->findRegisterUseOperand(PhysReg, true); 2419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Note, if the store is storing a sub-register, it's possible the 2420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // super-register is needed below. 2421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (KillOpnd && !KillOpnd->getSubReg() && 2422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TII->unfoldMemoryOperand(MF, &MI, PhysReg, false, true,NewMIs)){ 2423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MBB->insert(MII, NewMIs[0]); 2424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NewStore = NewMIs[1]; 2425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MBB->insert(MII, NewStore); 2426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->addSpillSlotUse(SS, NewStore); 2427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman InvalidateKills(MI, TRI, RegKills, KillOps); 242819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman EraseInstr(&MI); 2429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Erased = true; 2430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman --NextMII; 2431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman --NextMII; // backtrack to the unfolded instruction. 2432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BackTracked = true; 2433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman isDead = true; 2434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumSUnfold; 2435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (isDead) { // Previous store is dead. 2440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If we get here, the store is dead, nuke it now. 2441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "Removed dead store:\t" << *DeadStore); 2442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman InvalidateKills(*DeadStore, TRI, RegKills, KillOps); 244319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman EraseInstr(DeadStore); 2444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!NewStore) 2445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumDSE; 2446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MaybeDeadStores[SS] = NULL; 2449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (NewStore) { 2450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Treat this store as a spill merged into a copy. That makes the 2451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // stack slot value available. 2452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VRM->virtFolded(VirtReg, NewStore, VirtRegMap::isMod); 2453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman goto ProcessNextInst; 2454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the spill slot value is available, and this is a new definition of 2458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // the value, the value is not available anymore. 2459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MR & VirtRegMap::isMod) { 2460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Notice that the value in this stack slot has been modified. 2461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.ModifyStackSlotOrReMat(SS); 2462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this is *just* a mod of the value, check to see if this is just a 2464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // store to the spill slot (i.e. the spill got merged into the copy). If 2465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // so, realize that the vreg is available now, and add the store to the 2466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // MaybeDeadStore info. 2467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int StackSlot; 2468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!(MR & VirtRegMap::isRef)) { 2469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (unsigned SrcReg = TII->isStoreToStackSlot(&MI, StackSlot)) { 2470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(TargetRegisterInfo::isPhysicalRegister(SrcReg) && 2471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Src hasn't been allocated yet?"); 2472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (CommuteToFoldReload(MII, VirtReg, SrcReg, StackSlot, 2474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills, RegKills, KillOps, TRI)) { 2475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NextMII = llvm::next(MII); 2476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BackTracked = true; 2477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman goto ProcessNextInst; 2478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Okay, this is certainly a store of SrcReg to [StackSlot]. Mark 2481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // this as a potentially dead store in case there is a subsequent 2482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // store into the stack slot without a read from it. 2483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MaybeDeadStores[StackSlot] = &MI; 2484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the stack slot value was previously available in some other 2486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // register, change it now. Otherwise, make the register 2487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // available in PhysReg. 2488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.addAvailable(StackSlot, SrcReg, MI.killsRegister(SrcReg)); 2489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Process all of the spilled defs. 2495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SpilledMIRegs.clear(); 2496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 2497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineOperand &MO = MI.getOperand(i); 2498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!(MO.isReg() && MO.getReg() && MO.isDef())) 2499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 2500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned VirtReg = MO.getReg(); 2502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!TargetRegisterInfo::isVirtualRegister(VirtReg)) { 2503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Check to see if this is a noop copy. If so, eliminate the 2504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // instruction before considering the dest reg to be changed. 2505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Also check if it's copying from an "undef", if so, we can't 2506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // eliminate this or else the undef marker is lost and it will 2507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // confuses the scavenger. This is extremely rare. 2508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MI.isIdentityCopy() && !MI.getOperand(1).isUndef() && 2509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MI.getNumOperands() == 2) { 2510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumDCE; 2511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "Removing now-noop copy: " << MI); 2512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<unsigned, 2> KillRegs; 2513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman InvalidateKills(MI, TRI, RegKills, KillOps, &KillRegs); 2514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MO.isDead() && !KillRegs.empty()) { 2515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Source register or an implicit super/sub-register use is killed. 2516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(TRI->regsOverlap(KillRegs[0], MI.getOperand(0).getReg())); 2517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Last def is now dead. 2518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TransferDeadness(MI.getOperand(1).getReg(), RegKills, KillOps); 2519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 252019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman EraseInstr(&MI); 2521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Erased = true; 2522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.disallowClobberPhysReg(VirtReg); 2523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman goto ProcessNextInst; 2524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If it's not a no-op copy, it clobbers the value in the destreg. 2527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.ClobberPhysReg(VirtReg); 2528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ReusedOperands.markClobbered(VirtReg); 2529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Check to see if this instruction is a load from a stack slot into 2531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // a register. If so, this provides the stack slot value in the reg. 2532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int FrameIdx; 2533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx)) { 2534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(DestReg == VirtReg && "Unknown load situation!"); 2535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If it is a folded reference, then it's not safe to clobber. 2537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool Folded = FoldedSS.count(FrameIdx); 2538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Otherwise, if it wasn't available, remember that it is now! 2539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills.addAvailable(FrameIdx, DestReg, !Folded); 2540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman goto ProcessNextInst; 2541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 2544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned SubIdx = MO.getSubReg(); 2547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool DoReMat = VRM->isReMaterialized(VirtReg); 2548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (DoReMat) 2549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ReMatDefs.insert(&MI); 2550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // The only vregs left are stack slot definitions. 2552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int StackSlot = VRM->getStackSlot(VirtReg); 2553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass *RC = MRI->getRegClass(VirtReg); 2554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this def is part of a two-address operand, make sure to execute 2556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // the store from the correct physical register. 2557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned PhysReg; 2558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned TiedOp; 2559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MI.isRegTiedToUseOperand(i, &TiedOp)) { 2560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman PhysReg = MI.getOperand(TiedOp).getReg(); 2561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SubIdx) { 2562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned SuperReg = findSuperReg(RC, PhysReg, SubIdx, TRI); 2563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(SuperReg && TRI->getSubReg(SuperReg, SubIdx) == PhysReg && 2564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Can't find corresponding super-register!"); 2565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman PhysReg = SuperReg; 2566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else { 2568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman PhysReg = VRM->getPhys(VirtReg); 2569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ReusedOperands.isClobbered(PhysReg)) { 2570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Another def has taken the assigned physreg. It must have been a 2571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // use&def which got it due to reuse. Undo the reuse! 2572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman PhysReg = ReusedOperands.GetRegForReload(VirtReg, PhysReg, &MI, 2573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Spills, MaybeDeadStores, RegKills, KillOps, *VRM); 2574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 257719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If StackSlot is available in a register that also holds other stack 257819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // slots, clobber those stack slots now. 257919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Spills.ClobberSharingStackSlots(StackSlot); 258019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 2581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(PhysReg && "VR not assigned a physical register?"); 2582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MRI->setPhysRegUsed(PhysReg); 2583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg; 2584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ReusedOperands.markClobbered(RReg); 2585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MI.getOperand(i).setReg(RReg); 2586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MI.getOperand(i).setSubReg(0); 2587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!MO.isDead() && SpilledMIRegs.insert(VirtReg)) { 2589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *&LastStore = MaybeDeadStores[StackSlot]; 2590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SpillRegToStackSlot(MII, -1, PhysReg, StackSlot, RC, true, 2591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LastStore, Spills, ReMatDefs, RegKills, KillOps); 2592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NextMII = llvm::next(MII); 2593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Check to see if this is a noop copy. If so, eliminate the 2595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // instruction before considering the dest reg to be changed. 2596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MI.isIdentityCopy()) { 2597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumDCE; 2598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "Removing now-noop copy: " << MI); 2599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman InvalidateKills(MI, TRI, RegKills, KillOps); 260019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman EraseInstr(&MI); 2601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Erased = true; 2602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman UpdateKills(*LastStore, TRI, RegKills, KillOps); 2603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman goto ProcessNextInst; 2604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ProcessNextInst: 2608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Delete dead instructions without side effects. 2609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!Erased && !BackTracked && isSafeToDelete(MI)) { 2610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman InvalidateKills(MI, TRI, RegKills, KillOps); 261119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman EraseInstr(&MI); 2612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Erased = true; 2613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!Erased) 2615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DistanceMap.insert(std::make_pair(&MI, DistanceMap.size())); 2616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!Erased && !BackTracked) { 2617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (MachineBasicBlock::iterator II = &MI; II != NextMII; ++II) 2618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman UpdateKills(*II, TRI, RegKills, KillOps); 2619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MII = NextMII; 2621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 2624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 2625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanllvm::VirtRegRewriter* llvm::createVirtRegRewriter() { 2626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman switch (RewriterOpt) { 2627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman default: llvm_unreachable("Unreachable!"); 2628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case local: 2629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return new LocalRewriter(); 2630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case trivial: 2631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return new TrivialRewriter(); 2632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 2633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 2634