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