PrologEpilogInserter.cpp revision 263508
1181053Srwatson//===-- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function --===// 2180701Srwatson// 3155192Srwatson// The LLVM Compiler Infrastructure 4155192Srwatson// 5155192Srwatson// This file is distributed under the University of Illinois Open Source 6155192Srwatson// License. See LICENSE.TXT for details. 7155192Srwatson// 8155192Srwatson//===----------------------------------------------------------------------===// 9155192Srwatson// 10155192Srwatson// This pass is responsible for finalizing the functions frame layout, saving 11155192Srwatson// callee saved registers, and for emitting prolog & epilog code for the 12155192Srwatson// function. 13180701Srwatson// 14155192Srwatson// This pass must be run after register allocation. After this pass is 15155192Srwatson// executed, it is illegal to construct MO_FrameIndex operands. 16155192Srwatson// 17155192Srwatson//===----------------------------------------------------------------------===// 18155192Srwatson 19155192Srwatson#define DEBUG_TYPE "pei" 20155192Srwatson#include "PrologEpilogInserter.h" 21155192Srwatson#include "llvm/ADT/IndexedMap.h" 22155192Srwatson#include "llvm/ADT/STLExtras.h" 23155192Srwatson#include "llvm/ADT/SmallSet.h" 24155192Srwatson#include "llvm/ADT/Statistic.h" 25155192Srwatson#include "llvm/CodeGen/MachineDominators.h" 26155192Srwatson#include "llvm/CodeGen/MachineFrameInfo.h" 27155192Srwatson#include "llvm/CodeGen/MachineInstr.h" 28155192Srwatson#include "llvm/CodeGen/MachineLoopInfo.h" 29155192Srwatson#include "llvm/CodeGen/MachineModuleInfo.h" 30178186Srwatson#include "llvm/CodeGen/MachineRegisterInfo.h" 31178186Srwatson#include "llvm/CodeGen/RegisterScavenging.h" 32178186Srwatson#include "llvm/IR/InlineAsm.h" 33155192Srwatson#include "llvm/Support/CommandLine.h" 34155192Srwatson#include "llvm/Support/Compiler.h" 35155192Srwatson#include "llvm/Support/Debug.h" 36155192Srwatson#include "llvm/Support/raw_ostream.h" 37155192Srwatson#include "llvm/Target/TargetFrameLowering.h" 38155192Srwatson#include "llvm/Target/TargetInstrInfo.h" 39155192Srwatson#include "llvm/Target/TargetMachine.h" 40155192Srwatson#include "llvm/Target/TargetRegisterInfo.h" 41155192Srwatson#include <climits> 42159277Srwatson 43155192Srwatsonusing namespace llvm; 44155192Srwatson 45155192Srwatsonchar PEI::ID = 0; 46155192Srwatsonchar &llvm::PrologEpilogCodeInserterID = PEI::ID; 47155192Srwatson 48155192Srwatsonstatic cl::opt<unsigned> 49155192SrwatsonWarnStackSize("warn-stack-size", cl::Hidden, cl::init((unsigned)-1), 50155192Srwatson cl::desc("Warn for stack size bigger than the given" 51155192Srwatson " number")); 52155192Srwatson 53155192SrwatsonINITIALIZE_PASS_BEGIN(PEI, "prologepilog", 54155192Srwatson "Prologue/Epilogue Insertion", false, false) 55170196SrwatsonINITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) 56170196SrwatsonINITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) 57170196SrwatsonINITIALIZE_PASS_DEPENDENCY(TargetPassConfig) 58170196SrwatsonINITIALIZE_PASS_END(PEI, "prologepilog", 59155192Srwatson "Prologue/Epilogue Insertion & Frame Finalization", 60155192Srwatson false, false) 61155192Srwatson 62155192SrwatsonSTATISTIC(NumScavengedRegs, "Number of frame index regs scavenged"); 63155192SrwatsonSTATISTIC(NumBytesStackSpace, 64170585Srwatson "Number of bytes used for stack in all functions"); 65155192Srwatson 66155192Srwatsonvoid PEI::getAnalysisUsage(AnalysisUsage &AU) const { 67155192Srwatson AU.setPreservesCFG(); 68155192Srwatson AU.addPreserved<MachineLoopInfo>(); 69155192Srwatson AU.addPreserved<MachineDominatorTree>(); 70155192Srwatson AU.addRequired<TargetPassConfig>(); 71155192Srwatson MachineFunctionPass::getAnalysisUsage(AU); 72155192Srwatson} 73155192Srwatson 74155192Srwatsonbool PEI::isReturnBlock(MachineBasicBlock* MBB) { 75155192Srwatson return (MBB && !MBB->empty() && MBB->back().isReturn()); 76155192Srwatson} 77155192Srwatson 78155192Srwatson/// Compute the set of return blocks 79155192Srwatsonvoid PEI::calculateSets(MachineFunction &Fn) { 80155192Srwatson // Sets used to compute spill, restore placement sets. 81155192Srwatson const std::vector<CalleeSavedInfo> &CSI = 82155192Srwatson Fn.getFrameInfo()->getCalleeSavedInfo(); 83155192Srwatson 84155192Srwatson // If no CSRs used, we are done. 85155192Srwatson if (CSI.empty()) 86155192Srwatson return; 87155192Srwatson 88155192Srwatson // Save refs to entry and return blocks. 89155192Srwatson EntryBlock = Fn.begin(); 90155192Srwatson for (MachineFunction::iterator MBB = Fn.begin(), E = Fn.end(); 91155192Srwatson MBB != E; ++MBB) 92155192Srwatson if (isReturnBlock(MBB)) 93155192Srwatson ReturnBlocks.push_back(MBB); 94155192Srwatson 95155192Srwatson return; 96155192Srwatson} 97155192Srwatson 98155192Srwatson/// runOnMachineFunction - Insert prolog/epilog code and replace abstract 99155192Srwatson/// frame indexes with appropriate references. 100155192Srwatson/// 101155192Srwatsonbool PEI::runOnMachineFunction(MachineFunction &Fn) { 102155192Srwatson const Function* F = Fn.getFunction(); 103155192Srwatson const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo(); 104195925Srwatson const TargetFrameLowering *TFI = Fn.getTarget().getFrameLowering(); 105195925Srwatson 106195925Srwatson assert(!Fn.getRegInfo().getNumVirtRegs() && "Regalloc must assign all vregs"); 107195925Srwatson 108195925Srwatson RS = TRI->requiresRegisterScavenging(Fn) ? new RegScavenger() : NULL; 109195925Srwatson FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(Fn); 110195925Srwatson 111195925Srwatson // Calculate the MaxCallFrameSize and AdjustsStack variables for the 112195925Srwatson // function's frame information. Also eliminates call frame pseudo 113195925Srwatson // instructions. 114195925Srwatson calculateCallsInformation(Fn); 115195925Srwatson 116195925Srwatson // Allow the target machine to make some adjustments to the function 117195925Srwatson // e.g. UsedPhysRegs before calculateCalleeSavedRegisters. 118195925Srwatson TFI->processFunctionBeforeCalleeSavedScan(Fn, RS); 119195925Srwatson 120195925Srwatson // Scan the function for modified callee saved registers and insert spill code 121195925Srwatson // for any callee saved registers that are modified. 122195925Srwatson calculateCalleeSavedRegisters(Fn); 123195925Srwatson 124195925Srwatson // Determine placement of CSR spill/restore code: 125195925Srwatson // place all spills in the entry block, all restores in return blocks. 126195925Srwatson calculateSets(Fn); 127195925Srwatson 128195925Srwatson // Add the code to save and restore the callee saved registers 129195925Srwatson if (!F->hasFnAttribute(Attribute::Naked)) 130155192Srwatson insertCSRSpillsAndRestores(Fn); 131155192Srwatson 132155192Srwatson // Allow the target machine to make final modifications to the function 133155192Srwatson // before the frame layout is finalized. 134155192Srwatson TFI->processFunctionBeforeFrameFinalized(Fn, RS); 135155192Srwatson 136155192Srwatson // Calculate actual frame offsets for all abstract stack objects... 137155192Srwatson calculateFrameObjectOffsets(Fn); 138155192Srwatson 139155192Srwatson // Add prolog and epilog code to the function. This function is required 140155192Srwatson // to align the stack frame as necessary for any stack variables or 141155192Srwatson // called functions. Because of this, calculateCalleeSavedRegisters() 142155192Srwatson // must be called before this function in order to set the AdjustsStack 143155192Srwatson // and MaxCallFrameSize variables. 144155192Srwatson if (!F->hasFnAttribute(Attribute::Naked)) 145155192Srwatson insertPrologEpilogCode(Fn); 146155192Srwatson 147155192Srwatson // Replace all MO_FrameIndex operands with physical register references 148155192Srwatson // and actual offsets. 149155192Srwatson // 150155192Srwatson replaceFrameIndices(Fn); 151155192Srwatson 152155192Srwatson // If register scavenging is needed, as we've enabled doing it as a 153155192Srwatson // post-pass, scavenge the virtual registers that frame index elimiation 154155192Srwatson // inserted. 155155192Srwatson if (TRI->requiresRegisterScavenging(Fn) && FrameIndexVirtualScavenging) 156155192Srwatson scavengeFrameVirtualRegs(Fn); 157155192Srwatson 158155192Srwatson // Clear any vregs created by virtual scavenging. 159155192Srwatson Fn.getRegInfo().clearVirtRegs(); 160155192Srwatson 161155192Srwatson // Warn on stack size when we exceeds the given limit. 162155192Srwatson MachineFrameInfo *MFI = Fn.getFrameInfo(); 163155192Srwatson if (WarnStackSize.getNumOccurrences() > 0 && 164155192Srwatson WarnStackSize < MFI->getStackSize()) 165155192Srwatson errs() << "warning: Stack size limit exceeded (" << MFI->getStackSize() 166155192Srwatson << ") in " << Fn.getName() << ".\n"; 167155192Srwatson 168155192Srwatson delete RS; 169155192Srwatson ReturnBlocks.clear(); 170155192Srwatson return true; 171155192Srwatson} 172155192Srwatson 173155192Srwatson/// calculateCallsInformation - Calculate the MaxCallFrameSize and AdjustsStack 174155192Srwatson/// variables for the function's frame information and eliminate call frame 175155192Srwatson/// pseudo instructions. 176155192Srwatsonvoid PEI::calculateCallsInformation(MachineFunction &Fn) { 177155192Srwatson const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo(); 178155192Srwatson const TargetFrameLowering *TFI = Fn.getTarget().getFrameLowering(); 179155192Srwatson MachineFrameInfo *MFI = Fn.getFrameInfo(); 180155192Srwatson 181155192Srwatson unsigned MaxCallFrameSize = 0; 182155192Srwatson bool AdjustsStack = MFI->adjustsStack(); 183155192Srwatson 184155192Srwatson // Get the function call frame set-up and tear-down instruction opcode 185155192Srwatson int FrameSetupOpcode = TII.getCallFrameSetupOpcode(); 186155192Srwatson int FrameDestroyOpcode = TII.getCallFrameDestroyOpcode(); 187155192Srwatson 188155192Srwatson // Early exit for targets which have no call frame setup/destroy pseudo 189155192Srwatson // instructions. 190155192Srwatson if (FrameSetupOpcode == -1 && FrameDestroyOpcode == -1) 191155192Srwatson return; 192155192Srwatson 193155192Srwatson std::vector<MachineBasicBlock::iterator> FrameSDOps; 194155192Srwatson for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) 195155192Srwatson for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) 196155192Srwatson if (I->getOpcode() == FrameSetupOpcode || 197155192Srwatson I->getOpcode() == FrameDestroyOpcode) { 198155192Srwatson assert(I->getNumOperands() >= 1 && "Call Frame Setup/Destroy Pseudo" 199155192Srwatson " instructions should have a single immediate argument!"); 200155192Srwatson unsigned Size = I->getOperand(0).getImm(); 201155192Srwatson if (Size > MaxCallFrameSize) MaxCallFrameSize = Size; 202155192Srwatson AdjustsStack = true; 203155192Srwatson FrameSDOps.push_back(I); 204155192Srwatson } else if (I->isInlineAsm()) { 205155192Srwatson // Some inline asm's need a stack frame, as indicated by operand 1. 206155192Srwatson unsigned ExtraInfo = I->getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 207155192Srwatson if (ExtraInfo & InlineAsm::Extra_IsAlignStack) 208155192Srwatson AdjustsStack = true; 209155192Srwatson } 210155192Srwatson 211155192Srwatson MFI->setAdjustsStack(AdjustsStack); 212155192Srwatson MFI->setMaxCallFrameSize(MaxCallFrameSize); 213155192Srwatson 214155192Srwatson for (std::vector<MachineBasicBlock::iterator>::iterator 215155192Srwatson i = FrameSDOps.begin(), e = FrameSDOps.end(); i != e; ++i) { 216155192Srwatson MachineBasicBlock::iterator I = *i; 217155192Srwatson 218155192Srwatson // If call frames are not being included as part of the stack frame, and 219155192Srwatson // the target doesn't indicate otherwise, remove the call frame pseudos 220155192Srwatson // here. The sub/add sp instruction pairs are still inserted, but we don't 221155192Srwatson // need to track the SP adjustment for frame index elimination. 222155192Srwatson if (TFI->canSimplifyCallFramePseudos(Fn)) 223155192Srwatson TFI->eliminateCallFramePseudoInstr(Fn, *I->getParent(), I); 224155192Srwatson } 225155192Srwatson} 226155192Srwatson 227155192Srwatson 228155192Srwatson/// calculateCalleeSavedRegisters - Scan the function for modified callee saved 229155192Srwatson/// registers. 230155192Srwatsonvoid PEI::calculateCalleeSavedRegisters(MachineFunction &F) { 231155192Srwatson const TargetRegisterInfo *RegInfo = F.getTarget().getRegisterInfo(); 232155192Srwatson const TargetFrameLowering *TFI = F.getTarget().getFrameLowering(); 233155192Srwatson MachineFrameInfo *MFI = F.getFrameInfo(); 234155192Srwatson 235155192Srwatson // Get the callee saved register list... 236155192Srwatson const uint16_t *CSRegs = RegInfo->getCalleeSavedRegs(&F); 237155192Srwatson 238155192Srwatson // These are used to keep track the callee-save area. Initialize them. 239155192Srwatson MinCSFrameIndex = INT_MAX; 240155192Srwatson MaxCSFrameIndex = 0; 241155192Srwatson 242155192Srwatson // Early exit for targets which have no callee saved registers. 243155192Srwatson if (CSRegs == 0 || CSRegs[0] == 0) 244155192Srwatson return; 245155192Srwatson 246155192Srwatson // In Naked functions we aren't going to save any registers. 247155192Srwatson if (F.getFunction()->hasFnAttribute(Attribute::Naked)) 248155192Srwatson return; 249155192Srwatson 250155192Srwatson std::vector<CalleeSavedInfo> CSI; 251155192Srwatson for (unsigned i = 0; CSRegs[i]; ++i) { 252155192Srwatson unsigned Reg = CSRegs[i]; 253155192Srwatson // Functions which call __builtin_unwind_init get all their registers saved. 254155192Srwatson if (F.getRegInfo().isPhysRegUsed(Reg) || F.getMMI().callsUnwindInit()) { 255155192Srwatson // If the reg is modified, save it! 256155192Srwatson CSI.push_back(CalleeSavedInfo(Reg)); 257155192Srwatson } 258155192Srwatson } 259155192Srwatson 260155192Srwatson if (CSI.empty()) 261155192Srwatson return; // Early exit if no callee saved registers are modified! 262180699Srwatson 263155192Srwatson unsigned NumFixedSpillSlots; 264155192Srwatson const TargetFrameLowering::SpillSlot *FixedSpillSlots = 265202143Sbrooks TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots); 266202143Sbrooks 267195177Ssson // Now that we know which registers need to be saved and restored, allocate 268155192Srwatson // stack slots for them. 269155192Srwatson for (std::vector<CalleeSavedInfo>::iterator 270155192Srwatson I = CSI.begin(), E = CSI.end(); I != E; ++I) { 271155192Srwatson unsigned Reg = I->getReg(); 272195177Ssson const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg); 273195177Ssson 274195177Ssson int FrameIdx; 275195177Ssson if (RegInfo->hasReservedSpillSlot(F, Reg, FrameIdx)) { 276155192Srwatson I->setFrameIdx(FrameIdx); 277155192Srwatson continue; 278155192Srwatson } 279155192Srwatson 280155192Srwatson // Check to see if this physreg must be spilled to a particular stack slot 281155192Srwatson // on this target. 282155192Srwatson const TargetFrameLowering::SpillSlot *FixedSlot = FixedSpillSlots; 283155192Srwatson while (FixedSlot != FixedSpillSlots+NumFixedSpillSlots && 284155192Srwatson FixedSlot->Reg != Reg) 285155192Srwatson ++FixedSlot; 286155192Srwatson 287155192Srwatson if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) { 288155192Srwatson // Nope, just spill it anywhere convenient. 289155192Srwatson unsigned Align = RC->getAlignment(); 290155192Srwatson unsigned StackAlign = TFI->getStackAlignment(); 291155192Srwatson 292155192Srwatson // We may not be able to satisfy the desired alignment specification of 293155192Srwatson // the TargetRegisterClass if the stack alignment is smaller. Use the 294155192Srwatson // min. 295155192Srwatson Align = std::min(Align, StackAlign); 296155192Srwatson FrameIdx = MFI->CreateStackObject(RC->getSize(), Align, true); 297155192Srwatson if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx; 298155192Srwatson if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx; 299155192Srwatson } else { 300155192Srwatson // Spill it to the stack where we must. 301155192Srwatson FrameIdx = MFI->CreateFixedObject(RC->getSize(), FixedSlot->Offset, true); 302155192Srwatson } 303155192Srwatson 304155192Srwatson I->setFrameIdx(FrameIdx); 305155192Srwatson } 306155192Srwatson 307155192Srwatson MFI->setCalleeSavedInfo(CSI); 308155192Srwatson} 309155192Srwatson 310155192Srwatson/// insertCSRSpillsAndRestores - Insert spill and restore code for 311155192Srwatson/// callee saved registers used in the function. 312155192Srwatson/// 313155192Srwatsonvoid PEI::insertCSRSpillsAndRestores(MachineFunction &Fn) { 314155192Srwatson // Get callee saved register information. 315155192Srwatson MachineFrameInfo *MFI = Fn.getFrameInfo(); 316155192Srwatson const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); 317155192Srwatson 318155192Srwatson MFI->setCalleeSavedInfoValid(true); 319155192Srwatson 320155192Srwatson // Early exit if no callee saved registers are modified! 321155192Srwatson if (CSI.empty()) 322155192Srwatson return; 323155192Srwatson 324155192Srwatson const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo(); 325155192Srwatson const TargetFrameLowering *TFI = Fn.getTarget().getFrameLowering(); 326155192Srwatson const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo(); 327155192Srwatson MachineBasicBlock::iterator I; 328155192Srwatson 329155192Srwatson // Spill using target interface. 330155192Srwatson I = EntryBlock->begin(); 331155192Srwatson if (!TFI->spillCalleeSavedRegisters(*EntryBlock, I, CSI, TRI)) { 332155192Srwatson for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 333155192Srwatson // Add the callee-saved register as live-in. 334155192Srwatson // It's killed at the spill. 335155192Srwatson EntryBlock->addLiveIn(CSI[i].getReg()); 336155192Srwatson 337155192Srwatson // Insert the spill to the stack frame. 338155192Srwatson unsigned Reg = CSI[i].getReg(); 339155192Srwatson const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); 340155192Srwatson TII.storeRegToStackSlot(*EntryBlock, I, Reg, true, CSI[i].getFrameIdx(), 341155192Srwatson RC, TRI); 342155192Srwatson } 343155192Srwatson } 344155192Srwatson 345155192Srwatson // Restore using target interface. 346155192Srwatson for (unsigned ri = 0, re = ReturnBlocks.size(); ri != re; ++ri) { 347155192Srwatson MachineBasicBlock *MBB = ReturnBlocks[ri]; 348155192Srwatson I = MBB->end(); 349155192Srwatson --I; 350155192Srwatson 351155192Srwatson // Skip over all terminator instructions, which are part of the return 352155192Srwatson // sequence. 353155192Srwatson MachineBasicBlock::iterator I2 = I; 354155192Srwatson while (I2 != MBB->begin() && (--I2)->isTerminator()) 355155192Srwatson I = I2; 356155192Srwatson 357155192Srwatson bool AtStart = I == MBB->begin(); 358155192Srwatson MachineBasicBlock::iterator BeforeI = I; 359155192Srwatson if (!AtStart) 360155192Srwatson --BeforeI; 361155192Srwatson 362155192Srwatson // Restore all registers immediately before the return and any 363155192Srwatson // terminators that precede it. 364155192Srwatson if (!TFI->restoreCalleeSavedRegisters(*MBB, I, CSI, TRI)) { 365155192Srwatson for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 366155192Srwatson unsigned Reg = CSI[i].getReg(); 367155192Srwatson const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); 368155192Srwatson TII.loadRegFromStackSlot(*MBB, I, Reg, CSI[i].getFrameIdx(), RC, TRI); 369155192Srwatson assert(I != MBB->begin() && 370155192Srwatson "loadRegFromStackSlot didn't insert any code!"); 371155192Srwatson // Insert in reverse order. loadRegFromStackSlot can insert 372155192Srwatson // multiple instructions. 373155192Srwatson if (AtStart) 374155192Srwatson I = MBB->begin(); 375155192Srwatson else { 376155192Srwatson I = BeforeI; 377155192Srwatson ++I; 378155192Srwatson } 379155192Srwatson } 380155192Srwatson } 381155192Srwatson } 382155192Srwatson} 383155192Srwatson 384155192Srwatson/// AdjustStackOffset - Helper function used to adjust the stack frame offset. 385155192Srwatsonstatic inline void 386155192SrwatsonAdjustStackOffset(MachineFrameInfo *MFI, int FrameIdx, 387155192Srwatson bool StackGrowsDown, int64_t &Offset, 388155192Srwatson unsigned &MaxAlign) { 389155192Srwatson // If the stack grows down, add the object size to find the lowest address. 390155192Srwatson if (StackGrowsDown) 391155192Srwatson Offset += MFI->getObjectSize(FrameIdx); 392184948Srwatson 393155192Srwatson unsigned Align = MFI->getObjectAlignment(FrameIdx); 394160086Srwatson 395160086Srwatson // If the alignment of this object is greater than that of the stack, then 396160086Srwatson // increase the stack alignment to match. 397160086Srwatson MaxAlign = std::max(MaxAlign, Align); 398155192Srwatson 399160086Srwatson // Adjust to alignment boundary. 400155192Srwatson Offset = (Offset + Align - 1) / Align * Align; 401155192Srwatson 402184948Srwatson if (StackGrowsDown) { 403184948Srwatson DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset << "]\n"); 404184948Srwatson MFI->setObjectOffset(FrameIdx, -Offset); // Set the computed offset 405184948Srwatson } else { 406184948Srwatson DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset << "]\n"); 407184948Srwatson MFI->setObjectOffset(FrameIdx, Offset); 408184948Srwatson Offset += MFI->getObjectSize(FrameIdx); 409184948Srwatson } 410159277Srwatson} 411155192Srwatson 412168688Scsjp/// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the 413155192Srwatson/// abstract stack objects. 414155192Srwatson/// 415155192Srwatsonvoid PEI::calculateFrameObjectOffsets(MachineFunction &Fn) { 416155192Srwatson const TargetFrameLowering &TFI = *Fn.getTarget().getFrameLowering(); 417155192Srwatson 418155192Srwatson bool StackGrowsDown = 419155192Srwatson TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown; 420155192Srwatson 421155192Srwatson // Loop over all of the stack objects, assigning sequential addresses... 422155192Srwatson MachineFrameInfo *MFI = Fn.getFrameInfo(); 423155192Srwatson 424155192Srwatson // Start at the beginning of the local area. 425155192Srwatson // The Offset is the distance from the stack top in the direction 426155192Srwatson // of stack growth -- so it's always nonnegative. 427155192Srwatson int LocalAreaOffset = TFI.getOffsetOfLocalArea(); 428155192Srwatson if (StackGrowsDown) 429155192Srwatson LocalAreaOffset = -LocalAreaOffset; 430155192Srwatson assert(LocalAreaOffset >= 0 431155192Srwatson && "Local area offset should be in direction of stack growth"); 432156889Srwatson int64_t Offset = LocalAreaOffset; 433155192Srwatson 434155192Srwatson // If there are fixed sized objects that are preallocated in the local area, 435155192Srwatson // non-fixed objects can't be allocated right at the start of local area. 436155192Srwatson // We currently don't support filling in holes in between fixed sized 437155192Srwatson // objects, so we adjust 'Offset' to point to the end of last fixed sized 438155192Srwatson // preallocated object. 439155192Srwatson for (int i = MFI->getObjectIndexBegin(); i != 0; ++i) { 440155192Srwatson int64_t FixedOff; 441155192Srwatson if (StackGrowsDown) { 442155192Srwatson // The maximum distance from the stack pointer is at lower address of 443155192Srwatson // the object -- which is given by offset. For down growing stack 444247667Spjd // the offset is negative, so we negate the offset to get the distance. 445155192Srwatson FixedOff = -MFI->getObjectOffset(i); 446155192Srwatson } else { 447155192Srwatson // The maximum distance from the start pointer is at the upper 448160086Srwatson // address of the object. 449160086Srwatson FixedOff = MFI->getObjectOffset(i) + MFI->getObjectSize(i); 450160086Srwatson } 451155192Srwatson if (FixedOff > Offset) Offset = FixedOff; 452160086Srwatson } 453155192Srwatson 454155192Srwatson // First assign frame offsets to stack objects that are used to spill 455164011Scsjp // callee saved registers. 456160086Srwatson if (StackGrowsDown) { 457155192Srwatson for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) { 458155192Srwatson // If the stack grows down, we need to add the size to find the lowest 459155192Srwatson // address of the object. 460155192Srwatson Offset += MFI->getObjectSize(i); 461155192Srwatson 462155192Srwatson unsigned Align = MFI->getObjectAlignment(i); 463155192Srwatson // Adjust to alignment boundary 464155192Srwatson Offset = (Offset+Align-1)/Align*Align; 465155192Srwatson 466247667Spjd MFI->setObjectOffset(i, -Offset); // Set the computed offset 467247667Spjd } 468247667Spjd } else { 469243726Spjd int MaxCSFI = MaxCSFrameIndex, MinCSFI = MinCSFrameIndex; 470155192Srwatson for (int i = MaxCSFI; i >= MinCSFI ; --i) { 471155192Srwatson unsigned Align = MFI->getObjectAlignment(i); 472155192Srwatson // Adjust to alignment boundary 473155192Srwatson Offset = (Offset+Align-1)/Align*Align; 474155192Srwatson 475155192Srwatson MFI->setObjectOffset(i, Offset); 476155192Srwatson Offset += MFI->getObjectSize(i); 477155192Srwatson } 478155192Srwatson } 479155192Srwatson 480155192Srwatson unsigned MaxAlign = MFI->getMaxAlignment(); 481155192Srwatson 482155192Srwatson // Make sure the special register scavenging spill slot is closest to the 483155192Srwatson // incoming stack pointer if a frame pointer is required and is closer 484155192Srwatson // to the incoming rather than the final stack pointer. 485155192Srwatson const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo(); 486155192Srwatson bool EarlyScavengingSlots = (TFI.hasFP(Fn) && 487155192Srwatson TFI.isFPCloseToIncomingSP() && 488155192Srwatson RegInfo->useFPForScavengingIndex(Fn) && 489155192Srwatson !RegInfo->needsStackRealignment(Fn)); 490155192Srwatson if (RS && EarlyScavengingSlots) { 491155192Srwatson SmallVector<int, 2> SFIs; 492155192Srwatson RS->getScavengingFrameIndices(SFIs); 493155192Srwatson for (SmallVectorImpl<int>::iterator I = SFIs.begin(), 494155192Srwatson IE = SFIs.end(); I != IE; ++I) 495155192Srwatson AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign); 496155192Srwatson } 497155192Srwatson 498155192Srwatson // FIXME: Once this is working, then enable flag will change to a target 499155192Srwatson // check for whether the frame is large enough to want to use virtual 500155192Srwatson // frame index registers. Functions which don't want/need this optimization 501155192Srwatson // will continue to use the existing code path. 502155192Srwatson if (MFI->getUseLocalStackAllocationBlock()) { 503155192Srwatson unsigned Align = MFI->getLocalFrameMaxAlign(); 504155192Srwatson 505155192Srwatson // Adjust to alignment boundary. 506155192Srwatson Offset = (Offset + Align - 1) / Align * Align; 507155192Srwatson 508171066Scsjp DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n"); 509171066Scsjp 510171066Scsjp // Resolve offsets for objects in the local block. 511171066Scsjp for (unsigned i = 0, e = MFI->getLocalFrameObjectCount(); i != e; ++i) { 512171066Scsjp std::pair<int, int64_t> Entry = MFI->getLocalFrameObjectMap(i); 513171066Scsjp int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second; 514171066Scsjp DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" << 515171066Scsjp FIOffset << "]\n"); 516171066Scsjp MFI->setObjectOffset(Entry.first, FIOffset); 517171066Scsjp } 518171066Scsjp // Allocate the local block 519171066Scsjp Offset += MFI->getLocalFrameSize(); 520171066Scsjp 521171066Scsjp MaxAlign = std::max(Align, MaxAlign); 522171066Scsjp } 523171066Scsjp 524171066Scsjp // Make sure that the stack protector comes before the local variables on the 525171066Scsjp // stack. 526171066Scsjp SmallSet<int, 16> LargeStackObjs; 527171066Scsjp if (MFI->getStackProtectorIndex() >= 0) { 528171066Scsjp AdjustStackOffset(MFI, MFI->getStackProtectorIndex(), StackGrowsDown, 529171066Scsjp Offset, MaxAlign); 530155192Srwatson 531155192Srwatson // Assign large stack objects first. 532155192Srwatson for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) { 533155192Srwatson if (MFI->isObjectPreAllocated(i) && 534160086Srwatson MFI->getUseLocalStackAllocationBlock()) 535160086Srwatson continue; 536155192Srwatson if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex) 537155192Srwatson continue; 538155192Srwatson if (RS && RS->isScavengingFrameIndex((int)i)) 539155192Srwatson continue; 540155192Srwatson if (MFI->isDeadObjectIndex(i)) 541155192Srwatson continue; 542155192Srwatson if (MFI->getStackProtectorIndex() == (int)i) 543155192Srwatson continue; 544155192Srwatson if (!MFI->MayNeedStackProtector(i)) 545155192Srwatson continue; 546155192Srwatson 547155192Srwatson AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign); 548155192Srwatson LargeStackObjs.insert(i); 549155192Srwatson } 550155192Srwatson } 551155192Srwatson 552155192Srwatson // Then assign frame offsets to stack objects that are not used to spill 553155192Srwatson // callee saved registers. 554155192Srwatson for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) { 555155192Srwatson if (MFI->isObjectPreAllocated(i) && 556155192Srwatson MFI->getUseLocalStackAllocationBlock()) 557155192Srwatson continue; 558155192Srwatson if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex) 559155192Srwatson continue; 560155192Srwatson if (RS && RS->isScavengingFrameIndex((int)i)) 561155192Srwatson continue; 562155192Srwatson if (MFI->isDeadObjectIndex(i)) 563155192Srwatson continue; 564155192Srwatson if (MFI->getStackProtectorIndex() == (int)i) 565155192Srwatson continue; 566155192Srwatson if (LargeStackObjs.count(i)) 567155192Srwatson continue; 568155192Srwatson 569155192Srwatson AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign); 570155192Srwatson } 571155192Srwatson 572155192Srwatson // Make sure the special register scavenging spill slot is closest to the 573155192Srwatson // stack pointer. 574155192Srwatson if (RS && !EarlyScavengingSlots) { 575155192Srwatson SmallVector<int, 2> SFIs; 576155192Srwatson RS->getScavengingFrameIndices(SFIs); 577155192Srwatson for (SmallVectorImpl<int>::iterator I = SFIs.begin(), 578155192Srwatson IE = SFIs.end(); I != IE; ++I) 579155192Srwatson AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign); 580155192Srwatson } 581155192Srwatson 582155192Srwatson if (!TFI.targetHandlesStackFrameRounding()) { 583155192Srwatson // If we have reserved argument space for call sites in the function 584155192Srwatson // immediately on entry to the current function, count it as part of the 585155192Srwatson // overall stack size. 586156889Srwatson if (MFI->adjustsStack() && TFI.hasReservedCallFrame(Fn)) 587156889Srwatson Offset += MFI->getMaxCallFrameSize(); 588155192Srwatson 589155192Srwatson // Round up the size to a multiple of the alignment. If the function has 590155192Srwatson // any calls or alloca's, align to the target's StackAlignment value to 591155192Srwatson // ensure that the callee's frame or the alloca data is suitably aligned; 592155192Srwatson // otherwise, for leaf functions, align to the TransientStackAlignment 593155192Srwatson // value. 594155192Srwatson unsigned StackAlign; 595155192Srwatson if (MFI->adjustsStack() || MFI->hasVarSizedObjects() || 596155192Srwatson (RegInfo->needsStackRealignment(Fn) && MFI->getObjectIndexEnd() != 0)) 597155192Srwatson StackAlign = TFI.getStackAlignment(); 598155192Srwatson else 599155192Srwatson StackAlign = TFI.getTransientStackAlignment(); 600155192Srwatson 601155192Srwatson // If the frame pointer is eliminated, all frame offsets will be relative to 602155192Srwatson // SP not FP. Align to MaxAlign so this works. 603155192Srwatson StackAlign = std::max(StackAlign, MaxAlign); 604155192Srwatson unsigned AlignMask = StackAlign - 1; 605155192Srwatson Offset = (Offset + AlignMask) & ~uint64_t(AlignMask); 606155192Srwatson } 607155192Srwatson 608155192Srwatson // Update frame info to pretend that this is part of the stack... 609155192Srwatson int64_t StackSize = Offset - LocalAreaOffset; 610155192Srwatson MFI->setStackSize(StackSize); 611155192Srwatson NumBytesStackSpace += StackSize; 612155192Srwatson} 613155192Srwatson 614155192Srwatson/// insertPrologEpilogCode - Scan the function for modified callee saved 615155192Srwatson/// registers, insert spill code for these callee saved registers, then add 616155192Srwatson/// prolog and epilog code to the function. 617155192Srwatson/// 618155192Srwatsonvoid PEI::insertPrologEpilogCode(MachineFunction &Fn) { 619155192Srwatson const TargetFrameLowering &TFI = *Fn.getTarget().getFrameLowering(); 620155192Srwatson 621155192Srwatson // Add prologue to the function... 622155192Srwatson TFI.emitPrologue(Fn); 623155192Srwatson 624155192Srwatson // Add epilogue to restore the callee-save registers in each exiting block 625155192Srwatson for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) { 626155192Srwatson // If last instruction is a return instruction, add an epilogue 627155192Srwatson if (!I->empty() && I->back().isReturn()) 628155192Srwatson TFI.emitEpilogue(Fn, *I); 629155192Srwatson } 630155192Srwatson 631155192Srwatson // Emit additional code that is required to support segmented stacks, if 632155192Srwatson // we've been asked for it. This, when linked with a runtime with support 633155192Srwatson // for segmented stacks (libgcc is one), will result in allocating stack 634155192Srwatson // space in small chunks instead of one large contiguous block. 635155192Srwatson if (Fn.getTarget().Options.EnableSegmentedStacks) 636155192Srwatson TFI.adjustForSegmentedStacks(Fn); 637155192Srwatson 638155192Srwatson // Emit additional code that is required to explicitly handle the stack in 639155192Srwatson // HiPE native code (if needed) when loaded in the Erlang/OTP runtime. The 640155192Srwatson // approach is rather similar to that of Segmented Stacks, but it uses a 641156889Srwatson // different conditional check and another BIF for allocating more stack 642156889Srwatson // space. 643155192Srwatson if (Fn.getFunction()->getCallingConv() == CallingConv::HiPE) 644155192Srwatson TFI.adjustForHiPEPrologue(Fn); 645155192Srwatson} 646155192Srwatson 647155192Srwatson/// replaceFrameIndices - Replace all MO_FrameIndex operands with physical 648155192Srwatson/// register references and actual offsets. 649155192Srwatson/// 650155192Srwatsonvoid PEI::replaceFrameIndices(MachineFunction &Fn) { 651155192Srwatson if (!Fn.getFrameInfo()->hasStackObjects()) return; // Nothing to do? 652155192Srwatson 653155192Srwatson // Store SPAdj at exit of a basic block. 654155192Srwatson SmallVector<int, 8> SPState; 655155192Srwatson SPState.resize(Fn.getNumBlockIDs()); 656155192Srwatson SmallPtrSet<MachineBasicBlock*, 8> Reachable; 657155192Srwatson 658160086Srwatson // Iterate over the reachable blocks in DFS order. 659160086Srwatson for (df_ext_iterator<MachineFunction*, SmallPtrSet<MachineBasicBlock*, 8> > 660160086Srwatson DFI = df_ext_begin(&Fn, Reachable), DFE = df_ext_end(&Fn, Reachable); 661160086Srwatson DFI != DFE; ++DFI) { 662155192Srwatson int SPAdj = 0; 663155192Srwatson // Check the exit state of the DFS stack predecessor. 664155192Srwatson if (DFI.getPathLength() >= 2) { 665155192Srwatson MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2); 666155192Srwatson assert(Reachable.count(StackPred) && 667155192Srwatson "DFS stack predecessor is already visited.\n"); 668155192Srwatson SPAdj = SPState[StackPred->getNumber()]; 669184661Sjhb } 670195926Srwatson MachineBasicBlock *BB = *DFI; 671175294Sattilio replaceFrameIndices(BB, Fn, SPAdj); 672155192Srwatson SPState[BB->getNumber()] = SPAdj; 673155192Srwatson } 674155192Srwatson 675155192Srwatson // Handle the unreachable blocks. 676155192Srwatson for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { 677166845Srwatson if (Reachable.count(BB)) 678155192Srwatson // Already handled in DFS traversal. 679156889Srwatson continue; 680155192Srwatson int SPAdj = 0; 681156889Srwatson replaceFrameIndices(BB, Fn, SPAdj); 682155192Srwatson } 683156889Srwatson} 684166845Srwatson 685155192Srwatsonvoid PEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &Fn, 686178322Srwatson int &SPAdj) { 687155192Srwatson const TargetMachine &TM = Fn.getTarget(); 688156889Srwatson assert(TM.getRegisterInfo() && "TM::getRegisterInfo() must be implemented!"); 689155192Srwatson const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo(); 690156889Srwatson const TargetRegisterInfo &TRI = *TM.getRegisterInfo(); 691155192Srwatson const TargetFrameLowering *TFI = TM.getFrameLowering(); 692156889Srwatson bool StackGrowsDown = 693155192Srwatson TFI->getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown; 694156889Srwatson int FrameSetupOpcode = TII.getCallFrameSetupOpcode(); 695178322Srwatson int FrameDestroyOpcode = TII.getCallFrameDestroyOpcode(); 696155192Srwatson 697155192Srwatson if (RS && !FrameIndexVirtualScavenging) RS->enterBasicBlock(BB); 698155192Srwatson 699155192Srwatson for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) { 700155192Srwatson 701155192Srwatson if (I->getOpcode() == FrameSetupOpcode || 702155192Srwatson I->getOpcode() == FrameDestroyOpcode) { 703155192Srwatson // Remember how much SP has been adjusted to create the call 704155192Srwatson // frame. 705155192Srwatson int Size = I->getOperand(0).getImm(); 706156889Srwatson 707156889Srwatson if ((!StackGrowsDown && I->getOpcode() == FrameSetupOpcode) || 708180703Srwatson (StackGrowsDown && I->getOpcode() == FrameDestroyOpcode)) 709180703Srwatson Size = -Size; 710180703Srwatson 711155192Srwatson SPAdj += Size; 712195939Srwatson 713243726Spjd MachineBasicBlock::iterator PrevI = BB->end(); 714195939Srwatson if (I != BB->begin()) PrevI = prior(I); 715195939Srwatson TFI->eliminateCallFramePseudoInstr(Fn, *BB, I); 716195939Srwatson 717195939Srwatson // Visit the instructions created by eliminateCallFramePseudoInstr(). 718243726Spjd if (PrevI == BB->end()) 719195939Srwatson I = BB->begin(); // The replaced instr was the first in the block. 720195939Srwatson else 721155192Srwatson I = llvm::next(PrevI); 722243726Spjd continue; 723155192Srwatson } 724155192Srwatson 725155192Srwatson MachineInstr *MI = I; 726160086Srwatson bool DoIncr = true; 727160086Srwatson for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 728160086Srwatson if (!MI->getOperand(i).isFI()) 729160086Srwatson continue; 730243726Spjd 731195939Srwatson // Frame indicies in debug values are encoded in a target independent 732195939Srwatson // way with simply the frame index and offset rather than any 733155192Srwatson // target-specific addressing mode. 734195939Srwatson if (MI->isDebugValue()) { 735243726Spjd assert(i == 0 && "Frame indicies can only appear as the first " 736195939Srwatson "operand of a DBG_VALUE machine instruction"); 737195939Srwatson unsigned Reg; 738155192Srwatson MachineOperand &Offset = MI->getOperand(1); 739195939Srwatson Offset.setImm(Offset.getImm() + 740195939Srwatson TFI->getFrameIndexReference( 741195939Srwatson Fn, MI->getOperand(0).getIndex(), Reg)); 742155192Srwatson MI->getOperand(0).ChangeToRegister(Reg, false /*isDef*/); 743243726Spjd continue; 744195939Srwatson } 745155192Srwatson 746155192Srwatson // Some instructions (e.g. inline asm instructions) can have 747155192Srwatson // multiple frame indices and/or cause eliminateFrameIndex 748156889Srwatson // to insert more than one instruction. We need the register 749156889Srwatson // scavenger to go through all of these instructions so that 750155192Srwatson // it can update its register information. We keep the 751155192Srwatson // iterator at the point before insertion so that we can 752155192Srwatson // revisit them in full. 753155192Srwatson bool AtBeginning = (I == BB->begin()); 754170196Srwatson if (!AtBeginning) --I; 755170196Srwatson 756170196Srwatson // If this instruction has a FrameIndex operand, we need to 757170196Srwatson // use that target machine register info object to eliminate 758170196Srwatson // it. 759170196Srwatson TRI.eliminateFrameIndex(MI, SPAdj, i, 760170196Srwatson FrameIndexVirtualScavenging ? NULL : RS); 761155192Srwatson 762155192Srwatson // Reset the iterator if we were at the beginning of the BB. 763155192Srwatson if (AtBeginning) { 764155192Srwatson I = BB->begin(); 765195926Srwatson DoIncr = false; 766195926Srwatson } 767155192Srwatson 768155192Srwatson MI = 0; 769155192Srwatson break; 770155192Srwatson } 771155192Srwatson 772155192Srwatson if (DoIncr && I != BB->end()) ++I; 773182371Sattilio 774155192Srwatson // Update register states. 775155192Srwatson if (RS && !FrameIndexVirtualScavenging && MI) RS->forward(MI); 776195926Srwatson } 777155192Srwatson} 778155192Srwatson 779155192Srwatson/// scavengeFrameVirtualRegs - Replace all frame index virtual registers 780155192Srwatson/// with physical registers. Use the register scavenger to find an 781155192Srwatson/// appropriate register to use. 782155192Srwatson/// 783155192Srwatson/// FIXME: Iterating over the instruction stream is unnecessary. We can simply 784155192Srwatson/// iterate over the vreg use list, which at this point only contains machine 785155192Srwatson/// operands for which eliminateFrameIndex need a new scratch reg. 786195926Srwatsonvoid PEI::scavengeFrameVirtualRegs(MachineFunction &Fn) { 787195926Srwatson // Run through the instructions and find any virtual registers. 788195926Srwatson for (MachineFunction::iterator BB = Fn.begin(), 789195926Srwatson E = Fn.end(); BB != E; ++BB) { 790195926Srwatson RS->enterBasicBlock(BB); 791195926Srwatson 792195926Srwatson int SPAdj = 0; 793195926Srwatson 794195926Srwatson // The instruction stream may change in the loop, so check BB->end() 795195926Srwatson // directly. 796195926Srwatson for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) { 797195926Srwatson // We might end up here again with a NULL iterator if we scavenged a 798195926Srwatson // register for which we inserted spill code for definition by what was 799195926Srwatson // originally the first instruction in BB. 800195926Srwatson if (I == MachineBasicBlock::iterator(NULL)) 801195926Srwatson I = BB->begin(); 802155192Srwatson 803195926Srwatson MachineInstr *MI = I; 804195926Srwatson MachineBasicBlock::iterator J = llvm::next(I); 805195926Srwatson MachineBasicBlock::iterator P = I == BB->begin() ? 806195926Srwatson MachineBasicBlock::iterator(NULL) : llvm::prior(I); 807195926Srwatson 808195926Srwatson // RS should process this instruction before we might scavenge at this 809195926Srwatson // location. This is because we might be replacing a virtual register 810195926Srwatson // defined by this instruction, and if so, registers killed by this 811195926Srwatson // instruction are available, and defined registers are not. 812195926Srwatson RS->forward(I); 813195926Srwatson 814195926Srwatson for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 815195926Srwatson if (MI->getOperand(i).isReg()) { 816195926Srwatson MachineOperand &MO = MI->getOperand(i); 817195926Srwatson unsigned Reg = MO.getReg(); 818155192Srwatson if (Reg == 0) 819155192Srwatson continue; 820155192Srwatson if (!TargetRegisterInfo::isVirtualRegister(Reg)) 821155192Srwatson continue; 822161813Swsalamon 823161813Swsalamon // When we first encounter a new virtual register, it 824161813Swsalamon // must be a definition. 825161813Swsalamon assert(MI->getOperand(i).isDef() && 826161813Swsalamon "frame index virtual missing def!"); 827161813Swsalamon // Scavenge a new scratch register 828161813Swsalamon const TargetRegisterClass *RC = Fn.getRegInfo().getRegClass(Reg); 829161813Swsalamon unsigned ScratchReg = RS->scavengeRegister(RC, J, SPAdj); 830161813Swsalamon 831161813Swsalamon ++NumScavengedRegs; 832161813Swsalamon 833161813Swsalamon // Replace this reference to the virtual register with the 834161813Swsalamon // scratch register. 835161813Swsalamon assert (ScratchReg && "Missing scratch register!"); 836161813Swsalamon Fn.getRegInfo().replaceRegWith(Reg, ScratchReg); 837161813Swsalamon 838161813Swsalamon // Because this instruction was processed by the RS before this 839161813Swsalamon // register was allocated, make sure that the RS now records the 840161813Swsalamon // register as being used. 841161813Swsalamon RS->setUsed(ScratchReg); 842161813Swsalamon } 843161813Swsalamon } 844161813Swsalamon 845161813Swsalamon // If the scavenger needed to use one of its spill slots, the 846161813Swsalamon // spill code will have been inserted in between I and J. This is a 847161813Swsalamon // problem because we need the spill code before I: Move I to just 848161813Swsalamon // prior to J. 849161813Swsalamon if (I != llvm::prior(J)) { 850161813Swsalamon BB->splice(J, BB, I); 851161813Swsalamon 852161813Swsalamon // Before we move I, we need to prepare the RS to visit I again. 853161813Swsalamon // Specifically, RS will assert if it sees uses of registers that 854161813Swsalamon // it believes are undefined. Because we have already processed 855161813Swsalamon // register kills in I, when it visits I again, it will believe that 856161813Swsalamon // those registers are undefined. To avoid this situation, unprocess 857161813Swsalamon // the instruction I. 858161813Swsalamon assert(RS->getCurrentPosition() == I && 859161813Swsalamon "The register scavenger has an unexpected position"); 860161813Swsalamon I = P; 861161813Swsalamon RS->unprocess(P); 862161813Swsalamon } else 863224181Sjonathan ++I; 864255219Spjd } 865224181Sjonathan } 866224181Sjonathan} 867224181Sjonathan