1 //===-- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass is responsible for finalizing the functions frame layout, saving
11 // callee saved registers, and for emitting prolog & epilog code for the
14 // This pass must be run after register allocation. After this pass is
15 // executed, it is illegal to construct MO_FrameIndex operands.
17 // This pass provides an optional shrink wrapping variant of prolog/epilog
18 // insertion, enabled via --shrink-wrap. See ShrinkWrapping.cpp.
20 //===----------------------------------------------------------------------===//
22 #include "PrologEpilogInserter.h"
23 #include "llvm/CodeGen/MachineDominators.h"
24 #include "llvm/CodeGen/MachineLoopInfo.h"
25 #include "llvm/CodeGen/MachineInstr.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineModuleInfo.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/CodeGen/RegisterScavenging.h"
30 #include "llvm/Target/TargetMachine.h"
31 #include "llvm/Target/TargetRegisterInfo.h"
32 #include "llvm/Target/TargetFrameInfo.h"
33 #include "llvm/Target/TargetInstrInfo.h"
34 #include "llvm/Support/Compiler.h"
35 #include "llvm/ADT/STLExtras.h"
42 static RegisterPass<PEI>
43 X("prologepilog", "Prologue/Epilogue Insertion");
45 /// createPrologEpilogCodeInserter - This function returns a pass that inserts
46 /// prolog and epilog code, and eliminates abstract frame references.
48 FunctionPass *llvm::createPrologEpilogCodeInserter() { return new PEI(); }
50 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
51 /// frame indexes with appropriate references.
53 bool PEI::runOnMachineFunction(MachineFunction &Fn) {
54 const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
55 RS = TRI->requiresRegisterScavenging(Fn) ? new RegScavenger() : NULL;
57 // Get MachineModuleInfo so that we can track the construction of the
59 if (MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>())
60 Fn.getFrameInfo()->setMachineModuleInfo(MMI);
62 // Calculate the MaxCallFrameSize and HasCalls variables for the function's
63 // frame information. Also eliminates call frame pseudo instructions.
64 calculateCallsInformation(Fn);
66 // Allow the target machine to make some adjustments to the function
67 // e.g. UsedPhysRegs before calculateCalleeSavedRegisters.
68 TRI->processFunctionBeforeCalleeSavedScan(Fn, RS);
70 // Scan the function for modified callee saved registers and insert spill code
71 // for any callee saved registers that are modified.
72 calculateCalleeSavedRegisters(Fn);
74 // Determine placement of CSR spill/restore code:
75 // - with shrink wrapping, place spills and restores to tightly
76 // enclose regions in the Machine CFG of the function where
77 // they are used. Without shrink wrapping
78 // - default (no shrink wrapping), place all spills in the
79 // entry block, all restores in return blocks.
80 placeCSRSpillsAndRestores(Fn);
82 // Add the code to save and restore the callee saved registers
83 insertCSRSpillsAndRestores(Fn);
85 // Allow the target machine to make final modifications to the function
86 // before the frame layout is finalized.
87 TRI->processFunctionBeforeFrameFinalized(Fn);
89 // Calculate actual frame offsets for all abstract stack objects...
90 calculateFrameObjectOffsets(Fn);
92 // Add prolog and epilog code to the function. This function is required
93 // to align the stack frame as necessary for any stack variables or
94 // called functions. Because of this, calculateCalleeSavedRegisters
95 // must be called before this function in order to set the HasCalls
96 // and MaxCallFrameSize variables.
97 insertPrologEpilogCode(Fn);
99 // Replace all MO_FrameIndex operands with physical register references
100 // and actual offsets.
102 replaceFrameIndices(Fn);
110 void PEI::getAnalysisUsage(AnalysisUsage &AU) const {
111 AU.setPreservesCFG();
112 if (ShrinkWrapping || ShrinkWrapFunc != "") {
113 AU.addRequired<MachineLoopInfo>();
114 AU.addRequired<MachineDominatorTree>();
116 AU.addPreserved<MachineLoopInfo>();
117 AU.addPreserved<MachineDominatorTree>();
118 MachineFunctionPass::getAnalysisUsage(AU);
122 /// calculateCallsInformation - Calculate the MaxCallFrameSize and HasCalls
123 /// variables for the function's frame information and eliminate call frame
124 /// pseudo instructions.
125 void PEI::calculateCallsInformation(MachineFunction &Fn) {
126 const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
128 unsigned MaxCallFrameSize = 0;
129 bool HasCalls = false;
131 // Get the function call frame set-up and tear-down instruction opcode
132 int FrameSetupOpcode = RegInfo->getCallFrameSetupOpcode();
133 int FrameDestroyOpcode = RegInfo->getCallFrameDestroyOpcode();
135 // Early exit for targets which have no call frame setup/destroy pseudo
137 if (FrameSetupOpcode == -1 && FrameDestroyOpcode == -1)
140 std::vector<MachineBasicBlock::iterator> FrameSDOps;
141 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB)
142 for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
143 if (I->getOpcode() == FrameSetupOpcode ||
144 I->getOpcode() == FrameDestroyOpcode) {
145 assert(I->getNumOperands() >= 1 && "Call Frame Setup/Destroy Pseudo"
146 " instructions should have a single immediate argument!");
147 unsigned Size = I->getOperand(0).getImm();
148 if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
150 FrameSDOps.push_back(I);
151 } else if (I->getOpcode() == TargetInstrInfo::INLINEASM) {
152 // An InlineAsm might be a call; assume it is to get the stack frame
153 // aligned correctly for calls.
157 MachineFrameInfo *FFI = Fn.getFrameInfo();
158 FFI->setHasCalls(HasCalls);
159 FFI->setMaxCallFrameSize(MaxCallFrameSize);
161 for (std::vector<MachineBasicBlock::iterator>::iterator
162 i = FrameSDOps.begin(), e = FrameSDOps.end(); i != e; ++i) {
163 MachineBasicBlock::iterator I = *i;
165 // If call frames are not being included as part of the stack frame, and
166 // there is no dynamic allocation (therefore referencing frame slots off
167 // sp), leave the pseudo ops alone. We'll eliminate them later.
168 if (RegInfo->hasReservedCallFrame(Fn) || RegInfo->hasFP(Fn))
169 RegInfo->eliminateCallFramePseudoInstr(Fn, *I->getParent(), I);
174 /// calculateCalleeSavedRegisters - Scan the function for modified callee saved
176 void PEI::calculateCalleeSavedRegisters(MachineFunction &Fn) {
177 const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
178 const TargetFrameInfo *TFI = Fn.getTarget().getFrameInfo();
179 MachineFrameInfo *FFI = Fn.getFrameInfo();
181 // Get the callee saved register list...
182 const unsigned *CSRegs = RegInfo->getCalleeSavedRegs(&Fn);
184 // These are used to keep track the callee-save area. Initialize them.
185 MinCSFrameIndex = INT_MAX;
188 // Early exit for targets which have no callee saved registers.
189 if (CSRegs == 0 || CSRegs[0] == 0)
192 // Figure out which *callee saved* registers are modified by the current
193 // function, thus needing to be saved and restored in the prolog/epilog.
194 const TargetRegisterClass * const *CSRegClasses =
195 RegInfo->getCalleeSavedRegClasses(&Fn);
197 std::vector<CalleeSavedInfo> CSI;
198 for (unsigned i = 0; CSRegs[i]; ++i) {
199 unsigned Reg = CSRegs[i];
200 if (Fn.getRegInfo().isPhysRegUsed(Reg)) {
201 // If the reg is modified, save it!
202 CSI.push_back(CalleeSavedInfo(Reg, CSRegClasses[i]));
204 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
205 *AliasSet; ++AliasSet) { // Check alias registers too.
206 if (Fn.getRegInfo().isPhysRegUsed(*AliasSet)) {
207 CSI.push_back(CalleeSavedInfo(Reg, CSRegClasses[i]));
215 return; // Early exit if no callee saved registers are modified!
217 unsigned NumFixedSpillSlots;
218 const std::pair<unsigned,int> *FixedSpillSlots =
219 TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots);
221 // Now that we know which registers need to be saved and restored, allocate
222 // stack slots for them.
223 for (std::vector<CalleeSavedInfo>::iterator
224 I = CSI.begin(), E = CSI.end(); I != E; ++I) {
225 unsigned Reg = I->getReg();
226 const TargetRegisterClass *RC = I->getRegClass();
229 if (RegInfo->hasReservedSpillSlot(Fn, Reg, FrameIdx)) {
230 I->setFrameIdx(FrameIdx);
234 // Check to see if this physreg must be spilled to a particular stack slot
236 const std::pair<unsigned,int> *FixedSlot = FixedSpillSlots;
237 while (FixedSlot != FixedSpillSlots+NumFixedSpillSlots &&
238 FixedSlot->first != Reg)
241 if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) {
242 // Nope, just spill it anywhere convenient.
243 unsigned Align = RC->getAlignment();
244 unsigned StackAlign = TFI->getStackAlignment();
246 // We may not be able to satisfy the desired alignment specification of
247 // the TargetRegisterClass if the stack alignment is smaller. Use the
249 Align = std::min(Align, StackAlign);
250 FrameIdx = FFI->CreateStackObject(RC->getSize(), Align);
251 if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx;
252 if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx;
254 // Spill it to the stack where we must.
255 FrameIdx = FFI->CreateFixedObject(RC->getSize(), FixedSlot->second);
258 I->setFrameIdx(FrameIdx);
261 FFI->setCalleeSavedInfo(CSI);
264 /// insertCSRSpillsAndRestores - Insert spill and restore code for
265 /// callee saved registers used in the function, handling shrink wrapping.
267 void PEI::insertCSRSpillsAndRestores(MachineFunction &Fn) {
268 // Get callee saved register information.
269 MachineFrameInfo *FFI = Fn.getFrameInfo();
270 const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
272 // Early exit if no callee saved registers are modified!
276 const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
277 MachineBasicBlock::iterator I;
279 if (! ShrinkWrapThisFunction) {
280 // Spill using target interface.
281 I = EntryBlock->begin();
282 if (!TII.spillCalleeSavedRegisters(*EntryBlock, I, CSI)) {
283 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
284 // Add the callee-saved register as live-in.
285 // It's killed at the spill.
286 EntryBlock->addLiveIn(CSI[i].getReg());
288 // Insert the spill to the stack frame.
289 TII.storeRegToStackSlot(*EntryBlock, I, CSI[i].getReg(), true,
290 CSI[i].getFrameIdx(), CSI[i].getRegClass());
294 // Restore using target interface.
295 for (unsigned ri = 0, re = ReturnBlocks.size(); ri != re; ++ri) {
296 MachineBasicBlock* MBB = ReturnBlocks[ri];
299 // Skip over all terminator instructions, which are part of the return
301 MachineBasicBlock::iterator I2 = I;
302 while (I2 != MBB->begin() && (--I2)->getDesc().isTerminator())
305 bool AtStart = I == MBB->begin();
306 MachineBasicBlock::iterator BeforeI = I;
310 // Restore all registers immediately before the return and any
311 // terminators that preceed it.
312 if (!TII.restoreCalleeSavedRegisters(*MBB, I, CSI)) {
313 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
314 TII.loadRegFromStackSlot(*MBB, I, CSI[i].getReg(),
315 CSI[i].getFrameIdx(),
316 CSI[i].getRegClass());
317 assert(I != MBB->begin() &&
318 "loadRegFromStackSlot didn't insert any code!");
319 // Insert in reverse order. loadRegFromStackSlot can insert
320 // multiple instructions.
334 std::vector<CalleeSavedInfo> blockCSI;
335 for (CSRegBlockMap::iterator BI = CSRSave.begin(),
336 BE = CSRSave.end(); BI != BE; ++BI) {
337 MachineBasicBlock* MBB = BI->first;
338 CSRegSet save = BI->second;
344 for (CSRegSet::iterator RI = save.begin(),
345 RE = save.end(); RI != RE; ++RI) {
346 blockCSI.push_back(CSI[*RI]);
348 assert(blockCSI.size() > 0 &&
349 "Could not collect callee saved register info");
353 // When shrink wrapping, use stack slot stores/loads.
354 for (unsigned i = 0, e = blockCSI.size(); i != e; ++i) {
355 // Add the callee-saved register as live-in.
356 // It's killed at the spill.
357 MBB->addLiveIn(blockCSI[i].getReg());
359 // Insert the spill to the stack frame.
360 TII.storeRegToStackSlot(*MBB, I, blockCSI[i].getReg(),
362 blockCSI[i].getFrameIdx(),
363 blockCSI[i].getRegClass());
367 for (CSRegBlockMap::iterator BI = CSRRestore.begin(),
368 BE = CSRRestore.end(); BI != BE; ++BI) {
369 MachineBasicBlock* MBB = BI->first;
370 CSRegSet restore = BI->second;
376 for (CSRegSet::iterator RI = restore.begin(),
377 RE = restore.end(); RI != RE; ++RI) {
378 blockCSI.push_back(CSI[*RI]);
380 assert(blockCSI.size() > 0 &&
381 "Could not find callee saved register info");
383 // If MBB is empty and needs restores, insert at the _beginning_.
390 // Skip over all terminator instructions, which are part of the
392 if (! I->getDesc().isTerminator()) {
395 MachineBasicBlock::iterator I2 = I;
396 while (I2 != MBB->begin() && (--I2)->getDesc().isTerminator())
401 bool AtStart = I == MBB->begin();
402 MachineBasicBlock::iterator BeforeI = I;
406 // Restore all registers immediately before the return and any
407 // terminators that preceed it.
408 for (unsigned i = 0, e = blockCSI.size(); i != e; ++i) {
409 TII.loadRegFromStackSlot(*MBB, I, blockCSI[i].getReg(),
410 blockCSI[i].getFrameIdx(),
411 blockCSI[i].getRegClass());
412 assert(I != MBB->begin() &&
413 "loadRegFromStackSlot didn't insert any code!");
414 // Insert in reverse order. loadRegFromStackSlot can insert
415 // multiple instructions.
426 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
428 AdjustStackOffset(MachineFrameInfo *FFI, int FrameIdx,
429 bool StackGrowsDown, int64_t &Offset,
430 unsigned &MaxAlign) {
431 // If stack grows down, we need to add size of find the lowest address of the
434 Offset += FFI->getObjectSize(FrameIdx);
436 unsigned Align = FFI->getObjectAlignment(FrameIdx);
438 // If the alignment of this object is greater than that of the stack, then
439 // increase the stack alignment to match.
440 MaxAlign = std::max(MaxAlign, Align);
442 // Adjust to alignment boundary.
443 Offset = (Offset + Align - 1) / Align * Align;
445 if (StackGrowsDown) {
446 FFI->setObjectOffset(FrameIdx, -Offset); // Set the computed offset
448 FFI->setObjectOffset(FrameIdx, Offset);
449 Offset += FFI->getObjectSize(FrameIdx);
453 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
454 /// abstract stack objects.
456 void PEI::calculateFrameObjectOffsets(MachineFunction &Fn) {
457 const TargetFrameInfo &TFI = *Fn.getTarget().getFrameInfo();
459 bool StackGrowsDown =
460 TFI.getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
462 // Loop over all of the stack objects, assigning sequential addresses...
463 MachineFrameInfo *FFI = Fn.getFrameInfo();
465 unsigned MaxAlign = FFI->getMaxAlignment();
467 // Start at the beginning of the local area.
468 // The Offset is the distance from the stack top in the direction
469 // of stack growth -- so it's always nonnegative.
470 int64_t Offset = TFI.getOffsetOfLocalArea();
474 && "Local area offset should be in direction of stack growth");
476 // If there are fixed sized objects that are preallocated in the local area,
477 // non-fixed objects can't be allocated right at the start of local area.
478 // We currently don't support filling in holes in between fixed sized
479 // objects, so we adjust 'Offset' to point to the end of last fixed sized
480 // preallocated object.
481 for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
483 if (StackGrowsDown) {
484 // The maximum distance from the stack pointer is at lower address of
485 // the object -- which is given by offset. For down growing stack
486 // the offset is negative, so we negate the offset to get the distance.
487 FixedOff = -FFI->getObjectOffset(i);
489 // The maximum distance from the start pointer is at the upper
490 // address of the object.
491 FixedOff = FFI->getObjectOffset(i) + FFI->getObjectSize(i);
493 if (FixedOff > Offset) Offset = FixedOff;
496 // First assign frame offsets to stack objects that are used to spill
497 // callee saved registers.
498 if (StackGrowsDown) {
499 for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) {
500 // If stack grows down, we need to add size of find the lowest
501 // address of the object.
502 Offset += FFI->getObjectSize(i);
504 unsigned Align = FFI->getObjectAlignment(i);
505 // If the alignment of this object is greater than that of the stack,
506 // then increase the stack alignment to match.
507 MaxAlign = std::max(MaxAlign, Align);
508 // Adjust to alignment boundary
509 Offset = (Offset+Align-1)/Align*Align;
511 FFI->setObjectOffset(i, -Offset); // Set the computed offset
514 int MaxCSFI = MaxCSFrameIndex, MinCSFI = MinCSFrameIndex;
515 for (int i = MaxCSFI; i >= MinCSFI ; --i) {
516 unsigned Align = FFI->getObjectAlignment(i);
517 // If the alignment of this object is greater than that of the stack,
518 // then increase the stack alignment to match.
519 MaxAlign = std::max(MaxAlign, Align);
520 // Adjust to alignment boundary
521 Offset = (Offset+Align-1)/Align*Align;
523 FFI->setObjectOffset(i, Offset);
524 Offset += FFI->getObjectSize(i);
528 // Make sure the special register scavenging spill slot is closest to the
529 // frame pointer if a frame pointer is required.
530 const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
531 if (RS && RegInfo->hasFP(Fn)) {
532 int SFI = RS->getScavengingFrameIndex();
534 AdjustStackOffset(FFI, SFI, StackGrowsDown, Offset, MaxAlign);
537 // Make sure that the stack protector comes before the local variables on the
539 if (FFI->getStackProtectorIndex() >= 0)
540 AdjustStackOffset(FFI, FFI->getStackProtectorIndex(), StackGrowsDown,
543 // Then assign frame offsets to stack objects that are not used to spill
544 // callee saved registers.
545 for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
546 if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
548 if (RS && (int)i == RS->getScavengingFrameIndex())
550 if (FFI->isDeadObjectIndex(i))
552 if (FFI->getStackProtectorIndex() == (int)i)
555 AdjustStackOffset(FFI, i, StackGrowsDown, Offset, MaxAlign);
558 // Make sure the special register scavenging spill slot is closest to the
560 if (RS && !RegInfo->hasFP(Fn)) {
561 int SFI = RS->getScavengingFrameIndex();
563 AdjustStackOffset(FFI, SFI, StackGrowsDown, Offset, MaxAlign);
566 // Round up the size to a multiple of the alignment, but only if there are
567 // calls or alloca's in the function. This ensures that any calls to
568 // subroutines have their stack frames suitable aligned.
569 // Also do this if we need runtime alignment of the stack. In this case
570 // offsets will be relative to SP not FP; round up the stack size so this
572 if (!RegInfo->targetHandlesStackFrameRounding() &&
573 (FFI->hasCalls() || FFI->hasVarSizedObjects() ||
574 (RegInfo->needsStackRealignment(Fn) &&
575 FFI->getObjectIndexEnd() != 0))) {
576 // If we have reserved argument space for call sites in the function
577 // immediately on entry to the current function, count it as part of the
578 // overall stack size.
579 if (RegInfo->hasReservedCallFrame(Fn))
580 Offset += FFI->getMaxCallFrameSize();
582 unsigned AlignMask = std::max(TFI.getStackAlignment(),MaxAlign) - 1;
583 Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
586 // Update frame info to pretend that this is part of the stack...
587 FFI->setStackSize(Offset+TFI.getOffsetOfLocalArea());
589 // Remember the required stack alignment in case targets need it to perform
590 // dynamic stack alignment.
591 FFI->setMaxAlignment(MaxAlign);
595 /// insertPrologEpilogCode - Scan the function for modified callee saved
596 /// registers, insert spill code for these callee saved registers, then add
597 /// prolog and epilog code to the function.
599 void PEI::insertPrologEpilogCode(MachineFunction &Fn) {
600 const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
602 // Add prologue to the function...
603 TRI->emitPrologue(Fn);
605 // Add epilogue to restore the callee-save registers in each exiting block
606 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
607 // If last instruction is a return instruction, add an epilogue
608 if (!I->empty() && I->back().getDesc().isReturn())
609 TRI->emitEpilogue(Fn, *I);
614 /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
615 /// register references and actual offsets.
617 void PEI::replaceFrameIndices(MachineFunction &Fn) {
618 if (!Fn.getFrameInfo()->hasStackObjects()) return; // Nothing to do?
620 const TargetMachine &TM = Fn.getTarget();
621 assert(TM.getRegisterInfo() && "TM::getRegisterInfo() must be implemented!");
622 const TargetRegisterInfo &TRI = *TM.getRegisterInfo();
623 const TargetFrameInfo *TFI = TM.getFrameInfo();
624 bool StackGrowsDown =
625 TFI->getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
626 int FrameSetupOpcode = TRI.getCallFrameSetupOpcode();
627 int FrameDestroyOpcode = TRI.getCallFrameDestroyOpcode();
629 for (MachineFunction::iterator BB = Fn.begin(),
630 E = Fn.end(); BB != E; ++BB) {
631 int SPAdj = 0; // SP offset due to call frame setup / destroy.
632 if (RS) RS->enterBasicBlock(BB);
634 for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
635 if (I->getOpcode() == TargetInstrInfo::DECLARE) {
641 if (I->getOpcode() == FrameSetupOpcode ||
642 I->getOpcode() == FrameDestroyOpcode) {
643 // Remember how much SP has been adjusted to create the call
645 int Size = I->getOperand(0).getImm();
647 if ((!StackGrowsDown && I->getOpcode() == FrameSetupOpcode) ||
648 (StackGrowsDown && I->getOpcode() == FrameDestroyOpcode))
653 MachineBasicBlock::iterator PrevI = BB->end();
654 if (I != BB->begin()) PrevI = prior(I);
655 TRI.eliminateCallFramePseudoInstr(Fn, *BB, I);
657 // Visit the instructions created by eliminateCallFramePseudoInstr().
658 if (PrevI == BB->end())
659 I = BB->begin(); // The replaced instr was the first in the block.
665 MachineInstr *MI = I;
667 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
668 if (MI->getOperand(i).isFI()) {
669 // Some instructions (e.g. inline asm instructions) can have
670 // multiple frame indices and/or cause eliminateFrameIndex
671 // to insert more than one instruction. We need the register
672 // scavenger to go through all of these instructions so that
673 // it can update its register information. We keep the
674 // iterator at the point before insertion so that we can
675 // revisit them in full.
676 bool AtBeginning = (I == BB->begin());
677 if (!AtBeginning) --I;
679 // If this instruction has a FrameIndex operand, we need to
680 // use that target machine register info object to eliminate
683 TRI.eliminateFrameIndex(MI, SPAdj, RS);
685 // Reset the iterator if we were at the beginning of the BB.
695 if (DoIncr && I != BB->end()) ++I;
697 // Update register states.
698 if (RS && MI) RS->forward(MI);
701 assert(SPAdj == 0 && "Unbalanced call frame setup / destroy pairs?");