Merge branch 'master' into msp430
[llvm/msp430.git] / lib / CodeGen / PrologEpilogInserter.cpp
blob9e7ad6752a73d188388834e6f06c182bc76ef163
1 //===-- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function --===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass is responsible for finalizing the functions frame layout, saving
11 // callee saved registers, and for emitting prolog & epilog code for the
12 // function.
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"
36 #include <climits>
38 using namespace llvm;
40 char PEI::ID = 0;
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.
47 ///
48 FunctionPass *llvm::createPrologEpilogCodeInserter() { return new PEI(); }
50 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
51 /// frame indexes with appropriate references.
52 ///
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
58 // frame.
59 if (MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>())
60 Fn.getFrameInfo()->setMachineModuleInfo(MMI);
62 // Allow the target machine to make some adjustments to the function
63 // e.g. UsedPhysRegs before calculateCalleeSavedRegisters.
64 TRI->processFunctionBeforeCalleeSavedScan(Fn, RS);
66 // Scan the function for modified callee saved registers and insert spill
67 // code for any callee saved registers that are modified. Also calculate
68 // the MaxCallFrameSize and HasCalls variables for the function's frame
69 // information and eliminates call frame pseudo instructions.
70 calculateCalleeSavedRegisters(Fn);
72 // Determine placement of CSR spill/restore code:
73 // - with shrink wrapping, place spills and restores to tightly
74 // enclose regions in the Machine CFG of the function where
75 // they are used. Without shrink wrapping
76 // - default (no shrink wrapping), place all spills in the
77 // entry block, all restores in return blocks.
78 placeCSRSpillsAndRestores(Fn);
80 // Add the code to save and restore the callee saved registers
81 insertCSRSpillsAndRestores(Fn);
83 // Allow the target machine to make final modifications to the function
84 // before the frame layout is finalized.
85 TRI->processFunctionBeforeFrameFinalized(Fn);
87 // Calculate actual frame offsets for all abstract stack objects...
88 calculateFrameObjectOffsets(Fn);
90 // Add prolog and epilog code to the function. This function is required
91 // to align the stack frame as necessary for any stack variables or
92 // called functions. Because of this, calculateCalleeSavedRegisters
93 // must be called before this function in order to set the HasCalls
94 // and MaxCallFrameSize variables.
95 insertPrologEpilogCode(Fn);
97 // Replace all MO_FrameIndex operands with physical register references
98 // and actual offsets.
100 replaceFrameIndices(Fn);
102 delete RS;
103 clearAllSets();
104 return true;
107 #if 0
108 void PEI::getAnalysisUsage(AnalysisUsage &AU) const {
109 AU.setPreservesCFG();
110 if (ShrinkWrapping || ShrinkWrapFunc != "") {
111 AU.addRequired<MachineLoopInfo>();
112 AU.addRequired<MachineDominatorTree>();
114 AU.addPreserved<MachineLoopInfo>();
115 AU.addPreserved<MachineDominatorTree>();
116 MachineFunctionPass::getAnalysisUsage(AU);
118 #endif
120 /// calculateCalleeSavedRegisters - Scan the function for modified callee saved
121 /// registers. Also calculate the MaxCallFrameSize and HasCalls variables for
122 /// the function's frame information and eliminates call frame pseudo
123 /// instructions.
125 void PEI::calculateCalleeSavedRegisters(MachineFunction &Fn) {
126 const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
127 const TargetFrameInfo *TFI = Fn.getTarget().getFrameInfo();
129 // Get the callee saved register list...
130 const unsigned *CSRegs = RegInfo->getCalleeSavedRegs(&Fn);
132 // Get the function call frame set-up and tear-down instruction opcode
133 int FrameSetupOpcode = RegInfo->getCallFrameSetupOpcode();
134 int FrameDestroyOpcode = RegInfo->getCallFrameDestroyOpcode();
136 // These are used to keep track the callee-save area. Initialize them.
137 MinCSFrameIndex = INT_MAX;
138 MaxCSFrameIndex = 0;
140 // Early exit for targets which have no callee saved registers and no call
141 // frame setup/destroy pseudo instructions.
142 if ((CSRegs == 0 || CSRegs[0] == 0) &&
143 FrameSetupOpcode == -1 && FrameDestroyOpcode == -1)
144 return;
146 unsigned MaxCallFrameSize = 0;
147 bool HasCalls = false;
149 std::vector<MachineBasicBlock::iterator> FrameSDOps;
150 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB)
151 for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
152 if (I->getOpcode() == FrameSetupOpcode ||
153 I->getOpcode() == FrameDestroyOpcode) {
154 assert(I->getNumOperands() >= 1 && "Call Frame Setup/Destroy Pseudo"
155 " instructions should have a single immediate argument!");
156 unsigned Size = I->getOperand(0).getImm();
157 if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
158 HasCalls = true;
159 FrameSDOps.push_back(I);
162 MachineFrameInfo *FFI = Fn.getFrameInfo();
163 FFI->setHasCalls(HasCalls);
164 FFI->setMaxCallFrameSize(MaxCallFrameSize);
166 for (unsigned i = 0, e = FrameSDOps.size(); i != e; ++i) {
167 MachineBasicBlock::iterator I = FrameSDOps[i];
168 // If call frames are not being included as part of the stack frame,
169 // and there is no dynamic allocation (therefore referencing frame slots
170 // off sp), leave the pseudo ops alone. We'll eliminate them later.
171 if (RegInfo->hasReservedCallFrame(Fn) || RegInfo->hasFP(Fn))
172 RegInfo->eliminateCallFramePseudoInstr(Fn, *I->getParent(), I);
175 // Now figure out which *callee saved* registers are modified by the current
176 // function, thus needing to be saved and restored in the prolog/epilog.
178 const TargetRegisterClass* const *CSRegClasses =
179 RegInfo->getCalleeSavedRegClasses(&Fn);
180 std::vector<CalleeSavedInfo> CSI;
181 for (unsigned i = 0; CSRegs[i]; ++i) {
182 unsigned Reg = CSRegs[i];
183 if (Fn.getRegInfo().isPhysRegUsed(Reg)) {
184 // If the reg is modified, save it!
185 CSI.push_back(CalleeSavedInfo(Reg, CSRegClasses[i]));
186 } else {
187 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
188 *AliasSet; ++AliasSet) { // Check alias registers too.
189 if (Fn.getRegInfo().isPhysRegUsed(*AliasSet)) {
190 CSI.push_back(CalleeSavedInfo(Reg, CSRegClasses[i]));
191 break;
197 if (CSI.empty())
198 return; // Early exit if no callee saved registers are modified!
200 unsigned NumFixedSpillSlots;
201 const std::pair<unsigned,int> *FixedSpillSlots =
202 TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots);
204 // Now that we know which registers need to be saved and restored, allocate
205 // stack slots for them.
206 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
207 unsigned Reg = CSI[i].getReg();
208 const TargetRegisterClass *RC = CSI[i].getRegClass();
210 // Check to see if this physreg must be spilled to a particular stack slot
211 // on this target.
212 const std::pair<unsigned,int> *FixedSlot = FixedSpillSlots;
213 while (FixedSlot != FixedSpillSlots+NumFixedSpillSlots &&
214 FixedSlot->first != Reg)
215 ++FixedSlot;
217 int FrameIdx;
218 if (FixedSlot == FixedSpillSlots+NumFixedSpillSlots) {
219 // Nope, just spill it anywhere convenient.
220 unsigned Align = RC->getAlignment();
221 unsigned StackAlign = TFI->getStackAlignment();
222 // We may not be able to sastify the desired alignment specification of
223 // the TargetRegisterClass if the stack alignment is smaller.
224 // Use the min.
225 Align = std::min(Align, StackAlign);
226 FrameIdx = FFI->CreateStackObject(RC->getSize(), Align);
227 if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx;
228 if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx;
229 } else {
230 // Spill it to the stack where we must.
231 FrameIdx = FFI->CreateFixedObject(RC->getSize(), FixedSlot->second);
233 CSI[i].setFrameIdx(FrameIdx);
236 FFI->setCalleeSavedInfo(CSI);
239 /// insertCSRSpillsAndRestores - Insert spill and restore code for
240 /// callee saved registers used in the function, handling shrink wrapping.
242 void PEI::insertCSRSpillsAndRestores(MachineFunction &Fn) {
243 // Get callee saved register information.
244 MachineFrameInfo *FFI = Fn.getFrameInfo();
245 const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
247 // Early exit if no callee saved registers are modified!
248 if (CSI.empty())
249 return;
251 const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
252 MachineBasicBlock::iterator I;
254 if (! ShrinkWrapThisFunction) {
255 // Spill using target interface.
256 I = EntryBlock->begin();
257 if (!TII.spillCalleeSavedRegisters(*EntryBlock, I, CSI)) {
258 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
259 // Add the callee-saved register as live-in.
260 // It's killed at the spill.
261 EntryBlock->addLiveIn(CSI[i].getReg());
263 // Insert the spill to the stack frame.
264 TII.storeRegToStackSlot(*EntryBlock, I, CSI[i].getReg(), true,
265 CSI[i].getFrameIdx(), CSI[i].getRegClass());
269 // Restore using target interface.
270 for (unsigned ri = 0, re = ReturnBlocks.size(); ri != re; ++ri) {
271 MachineBasicBlock* MBB = ReturnBlocks[ri];
272 I = MBB->end(); --I;
274 // Skip over all terminator instructions, which are part of the return
275 // sequence.
276 MachineBasicBlock::iterator I2 = I;
277 while (I2 != MBB->begin() && (--I2)->getDesc().isTerminator())
278 I = I2;
280 bool AtStart = I == MBB->begin();
281 MachineBasicBlock::iterator BeforeI = I;
282 if (!AtStart)
283 --BeforeI;
285 // Restore all registers immediately before the return and any
286 // terminators that preceed it.
287 if (!TII.restoreCalleeSavedRegisters(*MBB, I, CSI)) {
288 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
289 TII.loadRegFromStackSlot(*MBB, I, CSI[i].getReg(),
290 CSI[i].getFrameIdx(),
291 CSI[i].getRegClass());
292 assert(I != MBB->begin() &&
293 "loadRegFromStackSlot didn't insert any code!");
294 // Insert in reverse order. loadRegFromStackSlot can insert
295 // multiple instructions.
296 if (AtStart)
297 I = MBB->begin();
298 else {
299 I = BeforeI;
300 ++I;
305 return;
308 // Insert spills.
309 std::vector<CalleeSavedInfo> blockCSI;
310 for (CSRegBlockMap::iterator BI = CSRSave.begin(),
311 BE = CSRSave.end(); BI != BE; ++BI) {
312 MachineBasicBlock* MBB = BI->first;
313 CSRegSet save = BI->second;
315 if (save.empty())
316 continue;
318 blockCSI.clear();
319 for (CSRegSet::iterator RI = save.begin(),
320 RE = save.end(); RI != RE; ++RI) {
321 blockCSI.push_back(CSI[*RI]);
323 assert(blockCSI.size() > 0 &&
324 "Could not collect callee saved register info");
326 I = MBB->begin();
328 // When shrink wrapping, use stack slot stores/loads.
329 for (unsigned i = 0, e = blockCSI.size(); i != e; ++i) {
330 // Add the callee-saved register as live-in.
331 // It's killed at the spill.
332 MBB->addLiveIn(blockCSI[i].getReg());
334 // Insert the spill to the stack frame.
335 TII.storeRegToStackSlot(*MBB, I, blockCSI[i].getReg(),
336 true,
337 blockCSI[i].getFrameIdx(),
338 blockCSI[i].getRegClass());
342 for (CSRegBlockMap::iterator BI = CSRRestore.begin(),
343 BE = CSRRestore.end(); BI != BE; ++BI) {
344 MachineBasicBlock* MBB = BI->first;
345 CSRegSet restore = BI->second;
347 if (restore.empty())
348 continue;
350 blockCSI.clear();
351 for (CSRegSet::iterator RI = restore.begin(),
352 RE = restore.end(); RI != RE; ++RI) {
353 blockCSI.push_back(CSI[*RI]);
355 assert(blockCSI.size() > 0 &&
356 "Could not find callee saved register info");
358 // If MBB is empty and needs restores, insert at the _beginning_.
359 if (MBB->empty()) {
360 I = MBB->begin();
361 } else {
362 I = MBB->end();
363 --I;
365 // Skip over all terminator instructions, which are part of the
366 // return sequence.
367 if (! I->getDesc().isTerminator()) {
368 ++I;
369 } else {
370 MachineBasicBlock::iterator I2 = I;
371 while (I2 != MBB->begin() && (--I2)->getDesc().isTerminator())
372 I = I2;
376 bool AtStart = I == MBB->begin();
377 MachineBasicBlock::iterator BeforeI = I;
378 if (!AtStart)
379 --BeforeI;
381 // Restore all registers immediately before the return and any
382 // terminators that preceed it.
383 for (unsigned i = 0, e = blockCSI.size(); i != e; ++i) {
384 TII.loadRegFromStackSlot(*MBB, I, blockCSI[i].getReg(),
385 blockCSI[i].getFrameIdx(),
386 blockCSI[i].getRegClass());
387 assert(I != MBB->begin() &&
388 "loadRegFromStackSlot didn't insert any code!");
389 // Insert in reverse order. loadRegFromStackSlot can insert
390 // multiple instructions.
391 if (AtStart)
392 I = MBB->begin();
393 else {
394 I = BeforeI;
395 ++I;
401 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
402 static inline void
403 AdjustStackOffset(MachineFrameInfo *FFI, int FrameIdx,
404 bool StackGrowsDown, int64_t &Offset,
405 unsigned &MaxAlign) {
406 // If stack grows down, we need to add size of find the lowest address of the
407 // object.
408 if (StackGrowsDown)
409 Offset += FFI->getObjectSize(FrameIdx);
411 unsigned Align = FFI->getObjectAlignment(FrameIdx);
413 // If the alignment of this object is greater than that of the stack, then
414 // increase the stack alignment to match.
415 MaxAlign = std::max(MaxAlign, Align);
417 // Adjust to alignment boundary.
418 Offset = (Offset + Align - 1) / Align * Align;
420 if (StackGrowsDown) {
421 FFI->setObjectOffset(FrameIdx, -Offset); // Set the computed offset
422 } else {
423 FFI->setObjectOffset(FrameIdx, Offset);
424 Offset += FFI->getObjectSize(FrameIdx);
428 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
429 /// abstract stack objects.
431 void PEI::calculateFrameObjectOffsets(MachineFunction &Fn) {
432 const TargetFrameInfo &TFI = *Fn.getTarget().getFrameInfo();
434 bool StackGrowsDown =
435 TFI.getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
437 // Loop over all of the stack objects, assigning sequential addresses...
438 MachineFrameInfo *FFI = Fn.getFrameInfo();
440 unsigned MaxAlign = FFI->getMaxAlignment();
442 // Start at the beginning of the local area.
443 // The Offset is the distance from the stack top in the direction
444 // of stack growth -- so it's always nonnegative.
445 int64_t Offset = TFI.getOffsetOfLocalArea();
446 if (StackGrowsDown)
447 Offset = -Offset;
448 assert(Offset >= 0
449 && "Local area offset should be in direction of stack growth");
451 // If there are fixed sized objects that are preallocated in the local area,
452 // non-fixed objects can't be allocated right at the start of local area.
453 // We currently don't support filling in holes in between fixed sized
454 // objects, so we adjust 'Offset' to point to the end of last fixed sized
455 // preallocated object.
456 for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
457 int64_t FixedOff;
458 if (StackGrowsDown) {
459 // The maximum distance from the stack pointer is at lower address of
460 // the object -- which is given by offset. For down growing stack
461 // the offset is negative, so we negate the offset to get the distance.
462 FixedOff = -FFI->getObjectOffset(i);
463 } else {
464 // The maximum distance from the start pointer is at the upper
465 // address of the object.
466 FixedOff = FFI->getObjectOffset(i) + FFI->getObjectSize(i);
468 if (FixedOff > Offset) Offset = FixedOff;
471 // First assign frame offsets to stack objects that are used to spill
472 // callee saved registers.
473 if (StackGrowsDown) {
474 for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) {
475 // If stack grows down, we need to add size of find the lowest
476 // address of the object.
477 Offset += FFI->getObjectSize(i);
479 unsigned Align = FFI->getObjectAlignment(i);
480 // If the alignment of this object is greater than that of the stack,
481 // then increase the stack alignment to match.
482 MaxAlign = std::max(MaxAlign, Align);
483 // Adjust to alignment boundary
484 Offset = (Offset+Align-1)/Align*Align;
486 FFI->setObjectOffset(i, -Offset); // Set the computed offset
488 } else {
489 int MaxCSFI = MaxCSFrameIndex, MinCSFI = MinCSFrameIndex;
490 for (int i = MaxCSFI; i >= MinCSFI ; --i) {
491 unsigned Align = FFI->getObjectAlignment(i);
492 // If the alignment of this object is greater than that of the stack,
493 // then increase the stack alignment to match.
494 MaxAlign = std::max(MaxAlign, Align);
495 // Adjust to alignment boundary
496 Offset = (Offset+Align-1)/Align*Align;
498 FFI->setObjectOffset(i, Offset);
499 Offset += FFI->getObjectSize(i);
503 // Make sure the special register scavenging spill slot is closest to the
504 // frame pointer if a frame pointer is required.
505 const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
506 if (RS && RegInfo->hasFP(Fn)) {
507 int SFI = RS->getScavengingFrameIndex();
508 if (SFI >= 0)
509 AdjustStackOffset(FFI, SFI, StackGrowsDown, Offset, MaxAlign);
512 // Make sure that the stack protector comes before the local variables on the
513 // stack.
514 if (FFI->getStackProtectorIndex() >= 0)
515 AdjustStackOffset(FFI, FFI->getStackProtectorIndex(), StackGrowsDown,
516 Offset, MaxAlign);
518 // Then assign frame offsets to stack objects that are not used to spill
519 // callee saved registers.
520 for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
521 if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
522 continue;
523 if (RS && (int)i == RS->getScavengingFrameIndex())
524 continue;
525 if (FFI->isDeadObjectIndex(i))
526 continue;
527 if (FFI->getStackProtectorIndex() == (int)i)
528 continue;
530 AdjustStackOffset(FFI, i, StackGrowsDown, Offset, MaxAlign);
533 // Make sure the special register scavenging spill slot is closest to the
534 // stack pointer.
535 if (RS && !RegInfo->hasFP(Fn)) {
536 int SFI = RS->getScavengingFrameIndex();
537 if (SFI >= 0)
538 AdjustStackOffset(FFI, SFI, StackGrowsDown, Offset, MaxAlign);
541 // Round up the size to a multiple of the alignment, but only if there are
542 // calls or alloca's in the function. This ensures that any calls to
543 // subroutines have their stack frames suitable aligned.
544 // Also do this if we need runtime alignment of the stack. In this case
545 // offsets will be relative to SP not FP; round up the stack size so this
546 // works.
547 if (!RegInfo->targetHandlesStackFrameRounding() &&
548 (FFI->hasCalls() || FFI->hasVarSizedObjects() ||
549 (RegInfo->needsStackRealignment(Fn) &&
550 FFI->getObjectIndexEnd() != 0))) {
551 // If we have reserved argument space for call sites in the function
552 // immediately on entry to the current function, count it as part of the
553 // overall stack size.
554 if (RegInfo->hasReservedCallFrame(Fn))
555 Offset += FFI->getMaxCallFrameSize();
557 unsigned AlignMask = std::max(TFI.getStackAlignment(),MaxAlign) - 1;
558 Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
561 // Update frame info to pretend that this is part of the stack...
562 FFI->setStackSize(Offset+TFI.getOffsetOfLocalArea());
564 // Remember the required stack alignment in case targets need it to perform
565 // dynamic stack alignment.
566 FFI->setMaxAlignment(MaxAlign);
570 /// insertPrologEpilogCode - Scan the function for modified callee saved
571 /// registers, insert spill code for these callee saved registers, then add
572 /// prolog and epilog code to the function.
574 void PEI::insertPrologEpilogCode(MachineFunction &Fn) {
575 const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
577 // Add prologue to the function...
578 TRI->emitPrologue(Fn);
580 // Add epilogue to restore the callee-save registers in each exiting block
581 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
582 // If last instruction is a return instruction, add an epilogue
583 if (!I->empty() && I->back().getDesc().isReturn())
584 TRI->emitEpilogue(Fn, *I);
589 /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
590 /// register references and actual offsets.
592 void PEI::replaceFrameIndices(MachineFunction &Fn) {
593 if (!Fn.getFrameInfo()->hasStackObjects()) return; // Nothing to do?
595 const TargetMachine &TM = Fn.getTarget();
596 assert(TM.getRegisterInfo() && "TM::getRegisterInfo() must be implemented!");
597 const TargetRegisterInfo &TRI = *TM.getRegisterInfo();
598 const TargetFrameInfo *TFI = TM.getFrameInfo();
599 bool StackGrowsDown =
600 TFI->getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
601 int FrameSetupOpcode = TRI.getCallFrameSetupOpcode();
602 int FrameDestroyOpcode = TRI.getCallFrameDestroyOpcode();
604 for (MachineFunction::iterator BB = Fn.begin(),
605 E = Fn.end(); BB != E; ++BB) {
606 int SPAdj = 0; // SP offset due to call frame setup / destroy.
607 if (RS) RS->enterBasicBlock(BB);
609 for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
610 if (I->getOpcode() == TargetInstrInfo::DECLARE) {
611 // Ignore it.
612 ++I;
613 continue;
616 if (I->getOpcode() == FrameSetupOpcode ||
617 I->getOpcode() == FrameDestroyOpcode) {
618 // Remember how much SP has been adjusted to create the call
619 // frame.
620 int Size = I->getOperand(0).getImm();
622 if ((!StackGrowsDown && I->getOpcode() == FrameSetupOpcode) ||
623 (StackGrowsDown && I->getOpcode() == FrameDestroyOpcode))
624 Size = -Size;
626 SPAdj += Size;
628 MachineBasicBlock::iterator PrevI = BB->end();
629 if (I != BB->begin()) PrevI = prior(I);
630 TRI.eliminateCallFramePseudoInstr(Fn, *BB, I);
632 // Visit the instructions created by eliminateCallFramePseudoInstr().
633 if (PrevI == BB->end())
634 I = BB->begin(); // The replaced instr was the first in the block.
635 else
636 I = next(PrevI);
637 continue;
640 MachineInstr *MI = I;
641 bool DoIncr = true;
642 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
643 if (MI->getOperand(i).isFI()) {
644 // Some instructions (e.g. inline asm instructions) can have
645 // multiple frame indices and/or cause eliminateFrameIndex
646 // to insert more than one instruction. We need the register
647 // scavenger to go through all of these instructions so that
648 // it can update its register information. We keep the
649 // iterator at the point before insertion so that we can
650 // revisit them in full.
651 bool AtBeginning = (I == BB->begin());
652 if (!AtBeginning) --I;
654 // If this instruction has a FrameIndex operand, we need to
655 // use that target machine register info object to eliminate
656 // it.
658 TRI.eliminateFrameIndex(MI, SPAdj, RS);
660 // Reset the iterator if we were at the beginning of the BB.
661 if (AtBeginning) {
662 I = BB->begin();
663 DoIncr = false;
666 MI = 0;
667 break;
670 if (DoIncr && I != BB->end()) ++I;
672 // Update register states.
673 if (RS && MI) RS->forward(MI);
676 assert(SPAdj == 0 && "Unbalanced call frame setup / destroy pairs?");