413ce91ead21d00cac0cbd99b4dbce6661dbea26
[oota-llvm.git] / lib / CodeGen / PrologEpilogInserter.cpp
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.
13 //
14 // This pass must be run after register allocation.  After this pass is
15 // executed, it is illegal to construct MO_FrameIndex operands.
16 //
17 // This pass provides an optional shrink wrapping variant of prolog/epilog
18 // insertion, enabled via --shrink-wrap. See ShrinkWrapping.cpp.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #define DEBUG_TYPE "pei"
23 #include "PrologEpilogInserter.h"
24 #include "llvm/ADT/IndexedMap.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/SmallSet.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/CodeGen/MachineDominators.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/CodeGen/MachineLoopInfo.h"
32 #include "llvm/CodeGen/MachineRegisterInfo.h"
33 #include "llvm/CodeGen/RegisterScavenging.h"
34 #include "llvm/IR/InlineAsm.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Compiler.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Target/TargetFrameLowering.h"
39 #include "llvm/Target/TargetInstrInfo.h"
40 #include "llvm/Target/TargetMachine.h"
41 #include "llvm/Target/TargetRegisterInfo.h"
42 #include <climits>
43
44 using namespace llvm;
45
46 char PEI::ID = 0;
47 char &llvm::PrologEpilogCodeInserterID = PEI::ID;
48
49 INITIALIZE_PASS_BEGIN(PEI, "prologepilog",
50                 "Prologue/Epilogue Insertion", false, false)
51 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
52 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
53 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
54 INITIALIZE_PASS_END(PEI, "prologepilog",
55                     "Prologue/Epilogue Insertion & Frame Finalization",
56                     false, false)
57
58 STATISTIC(NumVirtualFrameRegs, "Number of virtual frame regs encountered");
59 STATISTIC(NumScavengedRegs, "Number of frame index regs scavenged");
60 STATISTIC(NumBytesStackSpace,
61           "Number of bytes used for stack in all functions");
62
63 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
64 /// frame indexes with appropriate references.
65 ///
66 bool PEI::runOnMachineFunction(MachineFunction &Fn) {
67   const Function* F = Fn.getFunction();
68   const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
69   const TargetFrameLowering *TFI = Fn.getTarget().getFrameLowering();
70
71   assert(!Fn.getRegInfo().getNumVirtRegs() && "Regalloc must assign all vregs");
72
73   RS = TRI->requiresRegisterScavenging(Fn) ? new RegScavenger() : NULL;
74   FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(Fn);
75
76   // Calculate the MaxCallFrameSize and AdjustsStack variables for the
77   // function's frame information. Also eliminates call frame pseudo
78   // instructions.
79   calculateCallsInformation(Fn);
80
81   // Allow the target machine to make some adjustments to the function
82   // e.g. UsedPhysRegs before calculateCalleeSavedRegisters.
83   TFI->processFunctionBeforeCalleeSavedScan(Fn, RS);
84
85   // Scan the function for modified callee saved registers and insert spill code
86   // for any callee saved registers that are modified.
87   calculateCalleeSavedRegisters(Fn);
88
89   // Determine placement of CSR spill/restore code:
90   //  - With shrink wrapping, place spills and restores to tightly
91   //    enclose regions in the Machine CFG of the function where
92   //    they are used.
93   //  - Without shink wrapping (default), place all spills in the
94   //    entry block, all restores in return blocks.
95   placeCSRSpillsAndRestores(Fn);
96
97   // Add the code to save and restore the callee saved registers
98   if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
99                                        Attribute::Naked))
100     insertCSRSpillsAndRestores(Fn);
101
102   // Allow the target machine to make final modifications to the function
103   // before the frame layout is finalized.
104   TFI->processFunctionBeforeFrameFinalized(Fn, RS);
105
106   // Calculate actual frame offsets for all abstract stack objects...
107   calculateFrameObjectOffsets(Fn);
108
109   // Add prolog and epilog code to the function.  This function is required
110   // to align the stack frame as necessary for any stack variables or
111   // called functions.  Because of this, calculateCalleeSavedRegisters()
112   // must be called before this function in order to set the AdjustsStack
113   // and MaxCallFrameSize variables.
114   if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
115                                        Attribute::Naked))
116     insertPrologEpilogCode(Fn);
117
118   // Replace all MO_FrameIndex operands with physical register references
119   // and actual offsets.
120   //
121   replaceFrameIndices(Fn);
122
123   // If register scavenging is needed, as we've enabled doing it as a
124   // post-pass, scavenge the virtual registers that frame index elimiation
125   // inserted.
126   if (TRI->requiresRegisterScavenging(Fn) && FrameIndexVirtualScavenging)
127     scavengeFrameVirtualRegs(Fn);
128
129   // Clear any vregs created by virtual scavenging.
130   Fn.getRegInfo().clearVirtRegs();
131
132   delete RS;
133   clearAllSets();
134   return true;
135 }
136
137 /// calculateCallsInformation - Calculate the MaxCallFrameSize and AdjustsStack
138 /// variables for the function's frame information and eliminate call frame
139 /// pseudo instructions.
140 void PEI::calculateCallsInformation(MachineFunction &Fn) {
141   const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
142   const TargetFrameLowering *TFI = Fn.getTarget().getFrameLowering();
143   MachineFrameInfo *MFI = Fn.getFrameInfo();
144
145   unsigned MaxCallFrameSize = 0;
146   bool AdjustsStack = MFI->adjustsStack();
147
148   // Get the function call frame set-up and tear-down instruction opcode
149   int FrameSetupOpcode   = TII.getCallFrameSetupOpcode();
150   int FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
151
152   // Early exit for targets which have no call frame setup/destroy pseudo
153   // instructions.
154   if (FrameSetupOpcode == -1 && FrameDestroyOpcode == -1)
155     return;
156
157   std::vector<MachineBasicBlock::iterator> FrameSDOps;
158   for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB)
159     for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
160       if (I->getOpcode() == FrameSetupOpcode ||
161           I->getOpcode() == FrameDestroyOpcode) {
162         assert(I->getNumOperands() >= 1 && "Call Frame Setup/Destroy Pseudo"
163                " instructions should have a single immediate argument!");
164         unsigned Size = I->getOperand(0).getImm();
165         if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
166         AdjustsStack = true;
167         FrameSDOps.push_back(I);
168       } else if (I->isInlineAsm()) {
169         // Some inline asm's need a stack frame, as indicated by operand 1.
170         unsigned ExtraInfo = I->getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
171         if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
172           AdjustsStack = true;
173       }
174
175   MFI->setAdjustsStack(AdjustsStack);
176   MFI->setMaxCallFrameSize(MaxCallFrameSize);
177
178   for (std::vector<MachineBasicBlock::iterator>::iterator
179          i = FrameSDOps.begin(), e = FrameSDOps.end(); i != e; ++i) {
180     MachineBasicBlock::iterator I = *i;
181
182     // If call frames are not being included as part of the stack frame, and
183     // the target doesn't indicate otherwise, remove the call frame pseudos
184     // here. The sub/add sp instruction pairs are still inserted, but we don't
185     // need to track the SP adjustment for frame index elimination.
186     if (TFI->canSimplifyCallFramePseudos(Fn))
187       TFI->eliminateCallFramePseudoInstr(Fn, *I->getParent(), I);
188   }
189 }
190
191
192 /// calculateCalleeSavedRegisters - Scan the function for modified callee saved
193 /// registers.
194 void PEI::calculateCalleeSavedRegisters(MachineFunction &F) {
195   const TargetRegisterInfo *RegInfo = F.getTarget().getRegisterInfo();
196   const TargetFrameLowering *TFI = F.getTarget().getFrameLowering();
197   MachineFrameInfo *MFI = F.getFrameInfo();
198
199   // Get the callee saved register list...
200   const uint16_t *CSRegs = RegInfo->getCalleeSavedRegs(&F);
201
202   // These are used to keep track the callee-save area. Initialize them.
203   MinCSFrameIndex = INT_MAX;
204   MaxCSFrameIndex = 0;
205
206   // Early exit for targets which have no callee saved registers.
207   if (CSRegs == 0 || CSRegs[0] == 0)
208     return;
209
210   // In Naked functions we aren't going to save any registers.
211   if (F.getFunction()->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
212                                                     Attribute::Naked))
213     return;
214
215   std::vector<CalleeSavedInfo> CSI;
216   for (unsigned i = 0; CSRegs[i]; ++i) {
217     unsigned Reg = CSRegs[i];
218     if (F.getRegInfo().isPhysRegUsed(Reg)) {
219       // If the reg is modified, save it!
220       CSI.push_back(CalleeSavedInfo(Reg));
221     }
222   }
223
224   if (CSI.empty())
225     return;   // Early exit if no callee saved registers are modified!
226
227   unsigned NumFixedSpillSlots;
228   const TargetFrameLowering::SpillSlot *FixedSpillSlots =
229     TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots);
230
231   // Now that we know which registers need to be saved and restored, allocate
232   // stack slots for them.
233   for (std::vector<CalleeSavedInfo>::iterator
234          I = CSI.begin(), E = CSI.end(); I != E; ++I) {
235     unsigned Reg = I->getReg();
236     const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg);
237
238     int FrameIdx;
239     if (RegInfo->hasReservedSpillSlot(F, Reg, FrameIdx)) {
240       I->setFrameIdx(FrameIdx);
241       continue;
242     }
243
244     // Check to see if this physreg must be spilled to a particular stack slot
245     // on this target.
246     const TargetFrameLowering::SpillSlot *FixedSlot = FixedSpillSlots;
247     while (FixedSlot != FixedSpillSlots+NumFixedSpillSlots &&
248            FixedSlot->Reg != Reg)
249       ++FixedSlot;
250
251     if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) {
252       // Nope, just spill it anywhere convenient.
253       unsigned Align = RC->getAlignment();
254       unsigned StackAlign = TFI->getStackAlignment();
255
256       // We may not be able to satisfy the desired alignment specification of
257       // the TargetRegisterClass if the stack alignment is smaller. Use the
258       // min.
259       Align = std::min(Align, StackAlign);
260       FrameIdx = MFI->CreateStackObject(RC->getSize(), Align, true);
261       if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx;
262       if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx;
263     } else {
264       // Spill it to the stack where we must.
265       FrameIdx = MFI->CreateFixedObject(RC->getSize(), FixedSlot->Offset, true);
266     }
267
268     I->setFrameIdx(FrameIdx);
269   }
270
271   MFI->setCalleeSavedInfo(CSI);
272 }
273
274 /// insertCSRSpillsAndRestores - Insert spill and restore code for
275 /// callee saved registers used in the function, handling shrink wrapping.
276 ///
277 void PEI::insertCSRSpillsAndRestores(MachineFunction &Fn) {
278   // Get callee saved register information.
279   MachineFrameInfo *MFI = Fn.getFrameInfo();
280   const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
281
282   MFI->setCalleeSavedInfoValid(true);
283
284   // Early exit if no callee saved registers are modified!
285   if (CSI.empty())
286     return;
287
288   const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
289   const TargetFrameLowering *TFI = Fn.getTarget().getFrameLowering();
290   const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
291   MachineBasicBlock::iterator I;
292
293   if (!ShrinkWrapThisFunction) {
294     // Spill using target interface.
295     I = EntryBlock->begin();
296     if (!TFI->spillCalleeSavedRegisters(*EntryBlock, I, CSI, TRI)) {
297       for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
298         // Add the callee-saved register as live-in.
299         // It's killed at the spill.
300         EntryBlock->addLiveIn(CSI[i].getReg());
301
302         // Insert the spill to the stack frame.
303         unsigned Reg = CSI[i].getReg();
304         const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
305         TII.storeRegToStackSlot(*EntryBlock, I, Reg, true,
306                                 CSI[i].getFrameIdx(), RC, TRI);
307       }
308     }
309
310     // Restore using target interface.
311     for (unsigned ri = 0, re = ReturnBlocks.size(); ri != re; ++ri) {
312       MachineBasicBlock* MBB = ReturnBlocks[ri];
313       I = MBB->end(); --I;
314
315       // Skip over all terminator instructions, which are part of the return
316       // sequence.
317       MachineBasicBlock::iterator I2 = I;
318       while (I2 != MBB->begin() && (--I2)->isTerminator())
319         I = I2;
320
321       bool AtStart = I == MBB->begin();
322       MachineBasicBlock::iterator BeforeI = I;
323       if (!AtStart)
324         --BeforeI;
325
326       // Restore all registers immediately before the return and any
327       // terminators that precede it.
328       if (!TFI->restoreCalleeSavedRegisters(*MBB, I, CSI, TRI)) {
329         for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
330           unsigned Reg = CSI[i].getReg();
331           const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
332           TII.loadRegFromStackSlot(*MBB, I, Reg,
333                                    CSI[i].getFrameIdx(),
334                                    RC, TRI);
335           assert(I != MBB->begin() &&
336                  "loadRegFromStackSlot didn't insert any code!");
337           // Insert in reverse order.  loadRegFromStackSlot can insert
338           // multiple instructions.
339           if (AtStart)
340             I = MBB->begin();
341           else {
342             I = BeforeI;
343             ++I;
344           }
345         }
346       }
347     }
348     return;
349   }
350
351   // Insert spills.
352   std::vector<CalleeSavedInfo> blockCSI;
353   for (CSRegBlockMap::iterator BI = CSRSave.begin(),
354          BE = CSRSave.end(); BI != BE; ++BI) {
355     MachineBasicBlock* MBB = BI->first;
356     CSRegSet save = BI->second;
357
358     if (save.empty())
359       continue;
360
361     blockCSI.clear();
362     for (CSRegSet::iterator RI = save.begin(),
363            RE = save.end(); RI != RE; ++RI) {
364       blockCSI.push_back(CSI[*RI]);
365     }
366     assert(blockCSI.size() > 0 &&
367            "Could not collect callee saved register info");
368
369     I = MBB->begin();
370
371     // When shrink wrapping, use stack slot stores/loads.
372     for (unsigned i = 0, e = blockCSI.size(); i != e; ++i) {
373       // Add the callee-saved register as live-in.
374       // It's killed at the spill.
375       MBB->addLiveIn(blockCSI[i].getReg());
376
377       // Insert the spill to the stack frame.
378       unsigned Reg = blockCSI[i].getReg();
379       const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
380       TII.storeRegToStackSlot(*MBB, I, Reg,
381                               true,
382                               blockCSI[i].getFrameIdx(),
383                               RC, TRI);
384     }
385   }
386
387   for (CSRegBlockMap::iterator BI = CSRRestore.begin(),
388          BE = CSRRestore.end(); BI != BE; ++BI) {
389     MachineBasicBlock* MBB = BI->first;
390     CSRegSet restore = BI->second;
391
392     if (restore.empty())
393       continue;
394
395     blockCSI.clear();
396     for (CSRegSet::iterator RI = restore.begin(),
397            RE = restore.end(); RI != RE; ++RI) {
398       blockCSI.push_back(CSI[*RI]);
399     }
400     assert(blockCSI.size() > 0 &&
401            "Could not find callee saved register info");
402
403     // If MBB is empty and needs restores, insert at the _beginning_.
404     if (MBB->empty()) {
405       I = MBB->begin();
406     } else {
407       I = MBB->end();
408       --I;
409
410       // Skip over all terminator instructions, which are part of the
411       // return sequence.
412       if (! I->isTerminator()) {
413         ++I;
414       } else {
415         MachineBasicBlock::iterator I2 = I;
416         while (I2 != MBB->begin() && (--I2)->isTerminator())
417           I = I2;
418       }
419     }
420
421     bool AtStart = I == MBB->begin();
422     MachineBasicBlock::iterator BeforeI = I;
423     if (!AtStart)
424       --BeforeI;
425
426     // Restore all registers immediately before the return and any
427     // terminators that precede it.
428     for (unsigned i = 0, e = blockCSI.size(); i != e; ++i) {
429       unsigned Reg = blockCSI[i].getReg();
430       const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
431       TII.loadRegFromStackSlot(*MBB, I, Reg,
432                                blockCSI[i].getFrameIdx(),
433                                RC, TRI);
434       assert(I != MBB->begin() &&
435              "loadRegFromStackSlot didn't insert any code!");
436       // Insert in reverse order.  loadRegFromStackSlot can insert
437       // multiple instructions.
438       if (AtStart)
439         I = MBB->begin();
440       else {
441         I = BeforeI;
442         ++I;
443       }
444     }
445   }
446 }
447
448 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
449 static inline void
450 AdjustStackOffset(MachineFrameInfo *MFI, int FrameIdx,
451                   bool StackGrowsDown, int64_t &Offset,
452                   unsigned &MaxAlign) {
453   // If the stack grows down, add the object size to find the lowest address.
454   if (StackGrowsDown)
455     Offset += MFI->getObjectSize(FrameIdx);
456
457   unsigned Align = MFI->getObjectAlignment(FrameIdx);
458
459   // If the alignment of this object is greater than that of the stack, then
460   // increase the stack alignment to match.
461   MaxAlign = std::max(MaxAlign, Align);
462
463   // Adjust to alignment boundary.
464   Offset = (Offset + Align - 1) / Align * Align;
465
466   if (StackGrowsDown) {
467     DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset << "]\n");
468     MFI->setObjectOffset(FrameIdx, -Offset); // Set the computed offset
469   } else {
470     DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset << "]\n");
471     MFI->setObjectOffset(FrameIdx, Offset);
472     Offset += MFI->getObjectSize(FrameIdx);
473   }
474 }
475
476 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
477 /// abstract stack objects.
478 ///
479 void PEI::calculateFrameObjectOffsets(MachineFunction &Fn) {
480   const TargetFrameLowering &TFI = *Fn.getTarget().getFrameLowering();
481
482   bool StackGrowsDown =
483     TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
484
485   // Loop over all of the stack objects, assigning sequential addresses...
486   MachineFrameInfo *MFI = Fn.getFrameInfo();
487
488   // Start at the beginning of the local area.
489   // The Offset is the distance from the stack top in the direction
490   // of stack growth -- so it's always nonnegative.
491   int LocalAreaOffset = TFI.getOffsetOfLocalArea();
492   if (StackGrowsDown)
493     LocalAreaOffset = -LocalAreaOffset;
494   assert(LocalAreaOffset >= 0
495          && "Local area offset should be in direction of stack growth");
496   int64_t Offset = LocalAreaOffset;
497
498   // If there are fixed sized objects that are preallocated in the local area,
499   // non-fixed objects can't be allocated right at the start of local area.
500   // We currently don't support filling in holes in between fixed sized
501   // objects, so we adjust 'Offset' to point to the end of last fixed sized
502   // preallocated object.
503   for (int i = MFI->getObjectIndexBegin(); i != 0; ++i) {
504     int64_t FixedOff;
505     if (StackGrowsDown) {
506       // The maximum distance from the stack pointer is at lower address of
507       // the object -- which is given by offset. For down growing stack
508       // the offset is negative, so we negate the offset to get the distance.
509       FixedOff = -MFI->getObjectOffset(i);
510     } else {
511       // The maximum distance from the start pointer is at the upper
512       // address of the object.
513       FixedOff = MFI->getObjectOffset(i) + MFI->getObjectSize(i);
514     }
515     if (FixedOff > Offset) Offset = FixedOff;
516   }
517
518   // First assign frame offsets to stack objects that are used to spill
519   // callee saved registers.
520   if (StackGrowsDown) {
521     for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) {
522       // If the stack grows down, we need to add the size to find the lowest
523       // address of the object.
524       Offset += MFI->getObjectSize(i);
525
526       unsigned Align = MFI->getObjectAlignment(i);
527       // Adjust to alignment boundary
528       Offset = (Offset+Align-1)/Align*Align;
529
530       MFI->setObjectOffset(i, -Offset);        // Set the computed offset
531     }
532   } else {
533     int MaxCSFI = MaxCSFrameIndex, MinCSFI = MinCSFrameIndex;
534     for (int i = MaxCSFI; i >= MinCSFI ; --i) {
535       unsigned Align = MFI->getObjectAlignment(i);
536       // Adjust to alignment boundary
537       Offset = (Offset+Align-1)/Align*Align;
538
539       MFI->setObjectOffset(i, Offset);
540       Offset += MFI->getObjectSize(i);
541     }
542   }
543
544   unsigned MaxAlign = MFI->getMaxAlignment();
545
546   // Make sure the special register scavenging spill slot is closest to the
547   // frame pointer if a frame pointer is required.
548   const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
549   if (RS && TFI.hasFP(Fn) && RegInfo->useFPForScavengingIndex(Fn) &&
550       !RegInfo->needsStackRealignment(Fn)) {
551     SmallVector<int, 2> SFIs;
552     RS->getScavengingFrameIndices(SFIs);
553     for (SmallVector<int, 2>::iterator I = SFIs.begin(),
554          IE = SFIs.end(); I != IE; ++I)
555       AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign);
556   }
557
558   // FIXME: Once this is working, then enable flag will change to a target
559   // check for whether the frame is large enough to want to use virtual
560   // frame index registers. Functions which don't want/need this optimization
561   // will continue to use the existing code path.
562   if (MFI->getUseLocalStackAllocationBlock()) {
563     unsigned Align = MFI->getLocalFrameMaxAlign();
564
565     // Adjust to alignment boundary.
566     Offset = (Offset + Align - 1) / Align * Align;
567
568     DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
569
570     // Resolve offsets for objects in the local block.
571     for (unsigned i = 0, e = MFI->getLocalFrameObjectCount(); i != e; ++i) {
572       std::pair<int, int64_t> Entry = MFI->getLocalFrameObjectMap(i);
573       int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
574       DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" <<
575             FIOffset << "]\n");
576       MFI->setObjectOffset(Entry.first, FIOffset);
577     }
578     // Allocate the local block
579     Offset += MFI->getLocalFrameSize();
580
581     MaxAlign = std::max(Align, MaxAlign);
582   }
583
584   // Make sure that the stack protector comes before the local variables on the
585   // stack.
586   SmallSet<int, 16> LargeStackObjs;
587   if (MFI->getStackProtectorIndex() >= 0) {
588     AdjustStackOffset(MFI, MFI->getStackProtectorIndex(), StackGrowsDown,
589                       Offset, MaxAlign);
590
591     // Assign large stack objects first.
592     for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
593       if (MFI->isObjectPreAllocated(i) &&
594           MFI->getUseLocalStackAllocationBlock())
595         continue;
596       if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
597         continue;
598       if (RS && RS->isScavengingFrameIndex((int)i))
599         continue;
600       if (MFI->isDeadObjectIndex(i))
601         continue;
602       if (MFI->getStackProtectorIndex() == (int)i)
603         continue;
604       if (!MFI->MayNeedStackProtector(i))
605         continue;
606
607       AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign);
608       LargeStackObjs.insert(i);
609     }
610   }
611
612   // Then assign frame offsets to stack objects that are not used to spill
613   // callee saved registers.
614   for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
615     if (MFI->isObjectPreAllocated(i) &&
616         MFI->getUseLocalStackAllocationBlock())
617       continue;
618     if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
619       continue;
620     if (RS && RS->isScavengingFrameIndex((int)i))
621       continue;
622     if (MFI->isDeadObjectIndex(i))
623       continue;
624     if (MFI->getStackProtectorIndex() == (int)i)
625       continue;
626     if (LargeStackObjs.count(i))
627       continue;
628
629     AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign);
630   }
631
632   // Make sure the special register scavenging spill slot is closest to the
633   // stack pointer.
634   if (RS && (!TFI.hasFP(Fn) || RegInfo->needsStackRealignment(Fn) ||
635              !RegInfo->useFPForScavengingIndex(Fn))) {
636     SmallVector<int, 2> SFIs;
637     RS->getScavengingFrameIndices(SFIs);
638     for (SmallVector<int, 2>::iterator I = SFIs.begin(),
639          IE = SFIs.end(); I != IE; ++I)
640       AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign);
641   }
642
643   if (!TFI.targetHandlesStackFrameRounding()) {
644     // If we have reserved argument space for call sites in the function
645     // immediately on entry to the current function, count it as part of the
646     // overall stack size.
647     if (MFI->adjustsStack() && TFI.hasReservedCallFrame(Fn))
648       Offset += MFI->getMaxCallFrameSize();
649
650     // Round up the size to a multiple of the alignment.  If the function has
651     // any calls or alloca's, align to the target's StackAlignment value to
652     // ensure that the callee's frame or the alloca data is suitably aligned;
653     // otherwise, for leaf functions, align to the TransientStackAlignment
654     // value.
655     unsigned StackAlign;
656     if (MFI->adjustsStack() || MFI->hasVarSizedObjects() ||
657         (RegInfo->needsStackRealignment(Fn) && MFI->getObjectIndexEnd() != 0))
658       StackAlign = TFI.getStackAlignment();
659     else
660       StackAlign = TFI.getTransientStackAlignment();
661
662     // If the frame pointer is eliminated, all frame offsets will be relative to
663     // SP not FP. Align to MaxAlign so this works.
664     StackAlign = std::max(StackAlign, MaxAlign);
665     unsigned AlignMask = StackAlign - 1;
666     Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
667   }
668
669   // Update frame info to pretend that this is part of the stack...
670   int64_t StackSize = Offset - LocalAreaOffset;
671   MFI->setStackSize(StackSize);
672   NumBytesStackSpace += StackSize;
673 }
674
675 /// insertPrologEpilogCode - Scan the function for modified callee saved
676 /// registers, insert spill code for these callee saved registers, then add
677 /// prolog and epilog code to the function.
678 ///
679 void PEI::insertPrologEpilogCode(MachineFunction &Fn) {
680   const TargetFrameLowering &TFI = *Fn.getTarget().getFrameLowering();
681
682   // Add prologue to the function...
683   TFI.emitPrologue(Fn);
684
685   // Add epilogue to restore the callee-save registers in each exiting block
686   for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
687     // If last instruction is a return instruction, add an epilogue
688     if (!I->empty() && I->back().isReturn())
689       TFI.emitEpilogue(Fn, *I);
690   }
691
692   // Emit additional code that is required to support segmented stacks, if
693   // we've been asked for it.  This, when linked with a runtime with support
694   // for segmented stacks (libgcc is one), will result in allocating stack
695   // space in small chunks instead of one large contiguous block.
696   if (Fn.getTarget().Options.EnableSegmentedStacks)
697     TFI.adjustForSegmentedStacks(Fn);
698
699   // Emit additional code that is required to explicitly handle the stack in
700   // HiPE native code (if needed) when loaded in the Erlang/OTP runtime. The
701   // approach is rather similar to that of Segmented Stacks, but it uses a
702   // different conditional check and another BIF for allocating more stack
703   // space.
704   if (Fn.getFunction()->getCallingConv() == CallingConv::HiPE)
705     TFI.adjustForHiPEPrologue(Fn);
706 }
707
708 /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
709 /// register references and actual offsets.
710 ///
711 void PEI::replaceFrameIndices(MachineFunction &Fn) {
712   if (!Fn.getFrameInfo()->hasStackObjects()) return; // Nothing to do?
713
714   const TargetMachine &TM = Fn.getTarget();
715   assert(TM.getRegisterInfo() && "TM::getRegisterInfo() must be implemented!");
716   const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
717   const TargetRegisterInfo &TRI = *TM.getRegisterInfo();
718   const TargetFrameLowering *TFI = TM.getFrameLowering();
719   bool StackGrowsDown =
720     TFI->getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
721   int FrameSetupOpcode   = TII.getCallFrameSetupOpcode();
722   int FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
723
724   for (MachineFunction::iterator BB = Fn.begin(),
725          E = Fn.end(); BB != E; ++BB) {
726 #ifndef NDEBUG
727     int SPAdjCount = 0; // frame setup / destroy count.
728 #endif
729     int SPAdj = 0;  // SP offset due to call frame setup / destroy.
730     if (RS && !FrameIndexVirtualScavenging) RS->enterBasicBlock(BB);
731
732     for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
733
734       if (I->getOpcode() == FrameSetupOpcode ||
735           I->getOpcode() == FrameDestroyOpcode) {
736 #ifndef NDEBUG
737         // Track whether we see even pairs of them
738         SPAdjCount += I->getOpcode() == FrameSetupOpcode ? 1 : -1;
739 #endif
740         // Remember how much SP has been adjusted to create the call
741         // frame.
742         int Size = I->getOperand(0).getImm();
743
744         if ((!StackGrowsDown && I->getOpcode() == FrameSetupOpcode) ||
745             (StackGrowsDown && I->getOpcode() == FrameDestroyOpcode))
746           Size = -Size;
747
748         SPAdj += Size;
749
750         MachineBasicBlock::iterator PrevI = BB->end();
751         if (I != BB->begin()) PrevI = prior(I);
752         TFI->eliminateCallFramePseudoInstr(Fn, *BB, I);
753
754         // Visit the instructions created by eliminateCallFramePseudoInstr().
755         if (PrevI == BB->end())
756           I = BB->begin();     // The replaced instr was the first in the block.
757         else
758           I = llvm::next(PrevI);
759         continue;
760       }
761
762       MachineInstr *MI = I;
763       bool DoIncr = true;
764       for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
765         if (!MI->getOperand(i).isFI())
766             continue;
767
768         // Some instructions (e.g. inline asm instructions) can have
769         // multiple frame indices and/or cause eliminateFrameIndex
770         // to insert more than one instruction. We need the register
771         // scavenger to go through all of these instructions so that
772         // it can update its register information. We keep the
773         // iterator at the point before insertion so that we can
774         // revisit them in full.
775         bool AtBeginning = (I == BB->begin());
776         if (!AtBeginning) --I;
777
778         // If this instruction has a FrameIndex operand, we need to
779         // use that target machine register info object to eliminate
780         // it.
781         TRI.eliminateFrameIndex(MI, SPAdj, i,
782                                 FrameIndexVirtualScavenging ?  NULL : RS);
783
784         // Reset the iterator if we were at the beginning of the BB.
785         if (AtBeginning) {
786           I = BB->begin();
787           DoIncr = false;
788         }
789
790         MI = 0;
791         break;
792       }
793
794       if (DoIncr && I != BB->end()) ++I;
795
796       // Update register states.
797       if (RS && !FrameIndexVirtualScavenging && MI) RS->forward(MI);
798     }
799
800     // If we have evenly matched pairs of frame setup / destroy instructions,
801     // make sure the adjustments come out to zero. If we don't have matched
802     // pairs, we can't be sure the missing bit isn't in another basic block
803     // due to a custom inserter playing tricks, so just asserting SPAdj==0
804     // isn't sufficient. See tMOVCC on Thumb1, for example.
805     assert((SPAdjCount || SPAdj == 0) &&
806            "Unbalanced call frame setup / destroy pairs?");
807   }
808 }
809
810 /// scavengeFrameVirtualRegs - Replace all frame index virtual registers
811 /// with physical registers. Use the register scavenger to find an
812 /// appropriate register to use.
813 ///
814 /// FIXME: Iterating over the instruction stream is unnecessary. We can simply
815 /// iterate over the vreg use list, which at this point only contains machine
816 /// operands for which eliminateFrameIndex need a new scratch reg.
817 void PEI::scavengeFrameVirtualRegs(MachineFunction &Fn) {
818   // Run through the instructions and find any virtual registers.
819   for (MachineFunction::iterator BB = Fn.begin(),
820        E = Fn.end(); BB != E; ++BB) {
821     RS->enterBasicBlock(BB);
822
823     unsigned VirtReg = 0;
824     unsigned ScratchReg = 0;
825     int SPAdj = 0;
826
827     // The instruction stream may change in the loop, so check BB->end()
828     // directly.
829     for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
830       MachineInstr *MI = I;
831       for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
832         if (MI->getOperand(i).isReg()) {
833           MachineOperand &MO = MI->getOperand(i);
834           unsigned Reg = MO.getReg();
835           if (Reg == 0)
836             continue;
837           if (!TargetRegisterInfo::isVirtualRegister(Reg))
838             continue;
839
840           ++NumVirtualFrameRegs;
841
842           // Have we already allocated a scratch register for this virtual?
843           if (Reg != VirtReg) {
844             // When we first encounter a new virtual register, it
845             // must be a definition.
846             assert(MI->getOperand(i).isDef() &&
847                    "frame index virtual missing def!");
848             // Scavenge a new scratch register
849             VirtReg = Reg;
850             const TargetRegisterClass *RC = Fn.getRegInfo().getRegClass(Reg);
851             ScratchReg = RS->scavengeRegister(RC, I, SPAdj);
852             ++NumScavengedRegs;
853           }
854           // Replace this reference to the virtual register with the
855           // scratch register.
856           assert (ScratchReg && "Missing scratch register!");
857           MI->getOperand(i).setReg(ScratchReg);
858
859         }
860       }
861       RS->forward(I);
862       ++I;
863     }
864   }
865 }