PrologEpilogInserter.cpp revision 9fb3e7fa74ab8c36787ecb2b66d2028a09fa70b6
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/CodeGen/MachineDominators.h"
25#include "llvm/CodeGen/MachineLoopInfo.h"
26#include "llvm/CodeGen/MachineInstr.h"
27#include "llvm/CodeGen/MachineFrameInfo.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/CommandLine.h"
35#include "llvm/Support/Compiler.h"
36#include "llvm/Support/Debug.h"
37#include "llvm/ADT/IndexedMap.h"
38#include "llvm/ADT/SmallSet.h"
39#include "llvm/ADT/Statistic.h"
40#include "llvm/ADT/STLExtras.h"
41#include <climits>
42
43using namespace llvm;
44
45char PEI::ID = 0;
46
47INITIALIZE_PASS_BEGIN(PEI, "prologepilog",
48                "Prologue/Epilogue Insertion", false, false)
49INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
50INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
51INITIALIZE_PASS_END(PEI, "prologepilog",
52                "Prologue/Epilogue Insertion", false, false)
53
54STATISTIC(NumVirtualFrameRegs, "Number of virtual frame regs encountered");
55STATISTIC(NumScavengedRegs, "Number of frame index regs scavenged");
56
57/// createPrologEpilogCodeInserter - This function returns a pass that inserts
58/// prolog and epilog code, and eliminates abstract frame references.
59///
60FunctionPass *llvm::createPrologEpilogCodeInserter() { return new PEI(); }
61
62/// runOnMachineFunction - Insert prolog/epilog code and replace abstract
63/// frame indexes with appropriate references.
64///
65bool PEI::runOnMachineFunction(MachineFunction &Fn) {
66  const Function* F = Fn.getFunction();
67  const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
68  RS = TRI->requiresRegisterScavenging(Fn) ? new RegScavenger() : NULL;
69  FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(Fn);
70
71  // Calculate the MaxCallFrameSize and AdjustsStack variables for the
72  // function's frame information. Also eliminates call frame pseudo
73  // instructions.
74  calculateCallsInformation(Fn);
75
76  // Allow the target machine to make some adjustments to the function
77  // e.g. UsedPhysRegs before calculateCalleeSavedRegisters.
78  TRI->processFunctionBeforeCalleeSavedScan(Fn, RS);
79
80  // Scan the function for modified callee saved registers and insert spill code
81  // for any callee saved registers that are modified.
82  calculateCalleeSavedRegisters(Fn);
83
84  // Determine placement of CSR spill/restore code:
85  //  - With shrink wrapping, place spills and restores to tightly
86  //    enclose regions in the Machine CFG of the function where
87  //    they are used.
88  //  - Without shink wrapping (default), place all spills in the
89  //    entry block, all restores in return blocks.
90  placeCSRSpillsAndRestores(Fn);
91
92  // Add the code to save and restore the callee saved registers
93  if (!F->hasFnAttr(Attribute::Naked))
94    insertCSRSpillsAndRestores(Fn);
95
96  // Allow the target machine to make final modifications to the function
97  // before the frame layout is finalized.
98  TRI->processFunctionBeforeFrameFinalized(Fn);
99
100  // Calculate actual frame offsets for all abstract stack objects...
101  calculateFrameObjectOffsets(Fn);
102
103  // Add prolog and epilog code to the function.  This function is required
104  // to align the stack frame as necessary for any stack variables or
105  // called functions.  Because of this, calculateCalleeSavedRegisters()
106  // must be called before this function in order to set the AdjustsStack
107  // and MaxCallFrameSize variables.
108  if (!F->hasFnAttr(Attribute::Naked))
109    insertPrologEpilogCode(Fn);
110
111  // Replace all MO_FrameIndex operands with physical register references
112  // and actual offsets.
113  //
114  replaceFrameIndices(Fn);
115
116  // If register scavenging is needed, as we've enabled doing it as a
117  // post-pass, scavenge the virtual registers that frame index elimiation
118  // inserted.
119  if (TRI->requiresRegisterScavenging(Fn) && FrameIndexVirtualScavenging)
120    scavengeFrameVirtualRegs(Fn);
121
122  delete RS;
123  clearAllSets();
124  return true;
125}
126
127#if 0
128void PEI::getAnalysisUsage(AnalysisUsage &AU) const {
129  AU.setPreservesCFG();
130  if (ShrinkWrapping || ShrinkWrapFunc != "") {
131    AU.addRequired<MachineLoopInfo>();
132    AU.addRequired<MachineDominatorTree>();
133  }
134  AU.addPreserved<MachineLoopInfo>();
135  AU.addPreserved<MachineDominatorTree>();
136  MachineFunctionPass::getAnalysisUsage(AU);
137}
138#endif
139
140/// calculateCallsInformation - Calculate the MaxCallFrameSize and AdjustsStack
141/// variables for the function's frame information and eliminate call frame
142/// pseudo instructions.
143void PEI::calculateCallsInformation(MachineFunction &Fn) {
144  const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
145  MachineFrameInfo *MFI = Fn.getFrameInfo();
146
147  unsigned MaxCallFrameSize = 0;
148  bool AdjustsStack = MFI->adjustsStack();
149
150  // Get the function call frame set-up and tear-down instruction opcode
151  int FrameSetupOpcode   = RegInfo->getCallFrameSetupOpcode();
152  int FrameDestroyOpcode = RegInfo->getCallFrameDestroyOpcode();
153
154  // Early exit for targets which have no call frame setup/destroy pseudo
155  // instructions.
156  if (FrameSetupOpcode == -1 && FrameDestroyOpcode == -1)
157    return;
158
159  std::vector<MachineBasicBlock::iterator> FrameSDOps;
160  for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB)
161    for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
162      if (I->getOpcode() == FrameSetupOpcode ||
163          I->getOpcode() == FrameDestroyOpcode) {
164        assert(I->getNumOperands() >= 1 && "Call Frame Setup/Destroy Pseudo"
165               " instructions should have a single immediate argument!");
166        unsigned Size = I->getOperand(0).getImm();
167        if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
168        AdjustsStack = true;
169        FrameSDOps.push_back(I);
170      } else if (I->isInlineAsm()) {
171        // Some inline asm's need a stack frame, as indicated by operand 1.
172        if (I->getOperand(1).getImm())
173          AdjustsStack = true;
174      }
175
176  MFI->setAdjustsStack(AdjustsStack);
177  MFI->setMaxCallFrameSize(MaxCallFrameSize);
178
179  for (std::vector<MachineBasicBlock::iterator>::iterator
180         i = FrameSDOps.begin(), e = FrameSDOps.end(); i != e; ++i) {
181    MachineBasicBlock::iterator I = *i;
182
183    // If call frames are not being included as part of the stack frame, and
184    // the target doesn't indicate otherwise, remove the call frame pseudos
185    // here. The sub/add sp instruction pairs are still inserted, but we don't
186    // need to track the SP adjustment for frame index elimination.
187    if (RegInfo->canSimplifyCallFramePseudos(Fn))
188      RegInfo->eliminateCallFramePseudoInstr(Fn, *I->getParent(), I);
189  }
190}
191
192
193/// calculateCalleeSavedRegisters - Scan the function for modified callee saved
194/// registers.
195void PEI::calculateCalleeSavedRegisters(MachineFunction &Fn) {
196  const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
197  const TargetFrameInfo *TFI = Fn.getTarget().getFrameInfo();
198  MachineFrameInfo *MFI = Fn.getFrameInfo();
199
200  // Get the callee saved register list...
201  const unsigned *CSRegs = RegInfo->getCalleeSavedRegs(&Fn);
202
203  // These are used to keep track the callee-save area. Initialize them.
204  MinCSFrameIndex = INT_MAX;
205  MaxCSFrameIndex = 0;
206
207  // Early exit for targets which have no callee saved registers.
208  if (CSRegs == 0 || CSRegs[0] == 0)
209    return;
210
211  // In Naked functions we aren't going to save any registers.
212  if (Fn.getFunction()->hasFnAttr(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 (Fn.getRegInfo().isPhysRegUsed(Reg)) {
219      // If the reg is modified, save it!
220      CSI.push_back(CalleeSavedInfo(Reg));
221    } else {
222      for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
223           *AliasSet; ++AliasSet) {  // Check alias registers too.
224        if (Fn.getRegInfo().isPhysRegUsed(*AliasSet)) {
225          CSI.push_back(CalleeSavedInfo(Reg));
226          break;
227        }
228      }
229    }
230  }
231
232  if (CSI.empty())
233    return;   // Early exit if no callee saved registers are modified!
234
235  unsigned NumFixedSpillSlots;
236  const TargetFrameInfo::SpillSlot *FixedSpillSlots =
237    TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots);
238
239  // Now that we know which registers need to be saved and restored, allocate
240  // stack slots for them.
241  for (std::vector<CalleeSavedInfo>::iterator
242         I = CSI.begin(), E = CSI.end(); I != E; ++I) {
243    unsigned Reg = I->getReg();
244    const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg);
245
246    int FrameIdx;
247    if (RegInfo->hasReservedSpillSlot(Fn, Reg, FrameIdx)) {
248      I->setFrameIdx(FrameIdx);
249      continue;
250    }
251
252    // Check to see if this physreg must be spilled to a particular stack slot
253    // on this target.
254    const TargetFrameInfo::SpillSlot *FixedSlot = FixedSpillSlots;
255    while (FixedSlot != FixedSpillSlots+NumFixedSpillSlots &&
256           FixedSlot->Reg != Reg)
257      ++FixedSlot;
258
259    if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) {
260      // Nope, just spill it anywhere convenient.
261      unsigned Align = RC->getAlignment();
262      unsigned StackAlign = TFI->getStackAlignment();
263
264      // We may not be able to satisfy the desired alignment specification of
265      // the TargetRegisterClass if the stack alignment is smaller. Use the
266      // min.
267      Align = std::min(Align, StackAlign);
268      FrameIdx = MFI->CreateStackObject(RC->getSize(), Align, true);
269      if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx;
270      if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx;
271    } else {
272      // Spill it to the stack where we must.
273      FrameIdx = MFI->CreateFixedObject(RC->getSize(), FixedSlot->Offset, true);
274    }
275
276    I->setFrameIdx(FrameIdx);
277  }
278
279  MFI->setCalleeSavedInfo(CSI);
280}
281
282/// insertCSRSpillsAndRestores - Insert spill and restore code for
283/// callee saved registers used in the function, handling shrink wrapping.
284///
285void PEI::insertCSRSpillsAndRestores(MachineFunction &Fn) {
286  // Get callee saved register information.
287  MachineFrameInfo *MFI = Fn.getFrameInfo();
288  const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
289
290  MFI->setCalleeSavedInfoValid(true);
291
292  // Early exit if no callee saved registers are modified!
293  if (CSI.empty())
294    return;
295
296  const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
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 (!TII.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)->getDesc().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 preceed it.
335      if (!TII.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->getDesc().isTerminator()) {
420        ++I;
421      } else {
422        MachineBasicBlock::iterator I2 = I;
423        while (I2 != MBB->begin() && (--I2)->getDesc().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 preceed 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 TargetFrameInfo &TFI = *Fn.getTarget().getFrameInfo();
488
489  bool StackGrowsDown =
490    TFI.getStackGrowthDirection() == TargetFrameInfo::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 && RegInfo->hasFP(Fn) && !RegInfo->needsStackRealignment(Fn)) {
557    int SFI = RS->getScavengingFrameIndex();
558    if (SFI >= 0)
559      AdjustStackOffset(MFI, SFI, StackGrowsDown, Offset, MaxAlign);
560  }
561
562  // FIXME: Once this is working, then enable flag will change to a target
563  // check for whether the frame is large enough to want to use virtual
564  // frame index registers. Functions which don't want/need this optimization
565  // will continue to use the existing code path.
566  if (MFI->getUseLocalStackAllocationBlock()) {
567    unsigned Align = MFI->getLocalFrameMaxAlign();
568
569    // Adjust to alignment boundary.
570    Offset = (Offset + Align - 1) / Align * Align;
571
572    DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
573
574    // Resolve offsets for objects in the local block.
575    for (unsigned i = 0, e = MFI->getLocalFrameObjectCount(); i != e; ++i) {
576      std::pair<int, int64_t> Entry = MFI->getLocalFrameObjectMap(i);
577      int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
578      DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" <<
579            FIOffset << "]\n");
580      MFI->setObjectOffset(Entry.first, FIOffset);
581    }
582    // Allocate the local block
583    Offset += MFI->getLocalFrameSize();
584
585    MaxAlign = std::max(Align, MaxAlign);
586  }
587
588  // Make sure that the stack protector comes before the local variables on the
589  // stack.
590  SmallSet<int, 16> LargeStackObjs;
591  if (MFI->getStackProtectorIndex() >= 0) {
592    AdjustStackOffset(MFI, MFI->getStackProtectorIndex(), StackGrowsDown,
593                      Offset, MaxAlign);
594
595    // Assign large stack objects first.
596    for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
597      if (MFI->isObjectPreAllocated(i) &&
598          MFI->getUseLocalStackAllocationBlock())
599        continue;
600      if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
601        continue;
602      if (RS && (int)i == RS->getScavengingFrameIndex())
603        continue;
604      if (MFI->isDeadObjectIndex(i))
605        continue;
606      if (MFI->getStackProtectorIndex() == (int)i)
607        continue;
608      if (!MFI->MayNeedStackProtector(i))
609        continue;
610
611      AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign);
612      LargeStackObjs.insert(i);
613    }
614  }
615
616  // Then assign frame offsets to stack objects that are not used to spill
617  // callee saved registers.
618  for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
619    if (MFI->isObjectPreAllocated(i) &&
620        MFI->getUseLocalStackAllocationBlock())
621      continue;
622    if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
623      continue;
624    if (RS && (int)i == RS->getScavengingFrameIndex())
625      continue;
626    if (MFI->isDeadObjectIndex(i))
627      continue;
628    if (MFI->getStackProtectorIndex() == (int)i)
629      continue;
630    if (LargeStackObjs.count(i))
631      continue;
632
633    AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign);
634  }
635
636  // Make sure the special register scavenging spill slot is closest to the
637  // stack pointer.
638  if (RS && (!RegInfo->hasFP(Fn) || RegInfo->needsStackRealignment(Fn))) {
639    int SFI = RS->getScavengingFrameIndex();
640    if (SFI >= 0)
641      AdjustStackOffset(MFI, SFI, StackGrowsDown, Offset, MaxAlign);
642  }
643
644  if (!RegInfo->targetHandlesStackFrameRounding()) {
645    // If we have reserved argument space for call sites in the function
646    // immediately on entry to the current function, count it as part of the
647    // overall stack size.
648    if (MFI->adjustsStack() && RegInfo->hasReservedCallFrame(Fn))
649      Offset += MFI->getMaxCallFrameSize();
650
651    // Round up the size to a multiple of the alignment.  If the function has
652    // any calls or alloca's, align to the target's StackAlignment value to
653    // ensure that the callee's frame or the alloca data is suitably aligned;
654    // otherwise, for leaf functions, align to the TransientStackAlignment
655    // value.
656    unsigned StackAlign;
657    if (MFI->adjustsStack() || MFI->hasVarSizedObjects() ||
658        (RegInfo->needsStackRealignment(Fn) && MFI->getObjectIndexEnd() != 0))
659      StackAlign = TFI.getStackAlignment();
660    else
661      StackAlign = TFI.getTransientStackAlignment();
662
663    // If the frame pointer is eliminated, all frame offsets will be relative to
664    // SP not FP. Align to MaxAlign so this works.
665    StackAlign = std::max(StackAlign, MaxAlign);
666    unsigned AlignMask = StackAlign - 1;
667    Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
668  }
669
670  // Update frame info to pretend that this is part of the stack...
671  MFI->setStackSize(Offset - LocalAreaOffset);
672}
673
674/// insertPrologEpilogCode - Scan the function for modified callee saved
675/// registers, insert spill code for these callee saved registers, then add
676/// prolog and epilog code to the function.
677///
678void PEI::insertPrologEpilogCode(MachineFunction &Fn) {
679  const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
680
681  // Add prologue to the function...
682  TRI->emitPrologue(Fn);
683
684  // Add epilogue to restore the callee-save registers in each exiting block
685  for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
686    // If last instruction is a return instruction, add an epilogue
687    if (!I->empty() && I->back().getDesc().isReturn())
688      TRI->emitEpilogue(Fn, *I);
689  }
690}
691
692/// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
693/// register references and actual offsets.
694///
695void PEI::replaceFrameIndices(MachineFunction &Fn) {
696  if (!Fn.getFrameInfo()->hasStackObjects()) return; // Nothing to do?
697
698  const TargetMachine &TM = Fn.getTarget();
699  assert(TM.getRegisterInfo() && "TM::getRegisterInfo() must be implemented!");
700  const TargetRegisterInfo &TRI = *TM.getRegisterInfo();
701  const TargetFrameInfo *TFI = TM.getFrameInfo();
702  bool StackGrowsDown =
703    TFI->getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
704  int FrameSetupOpcode   = TRI.getCallFrameSetupOpcode();
705  int FrameDestroyOpcode = TRI.getCallFrameDestroyOpcode();
706
707  for (MachineFunction::iterator BB = Fn.begin(),
708         E = Fn.end(); BB != E; ++BB) {
709#ifndef NDEBUG
710    int SPAdjCount = 0; // frame setup / destroy count.
711#endif
712    int SPAdj = 0;  // SP offset due to call frame setup / destroy.
713    if (RS && !FrameIndexVirtualScavenging) RS->enterBasicBlock(BB);
714
715    for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
716
717      if (I->getOpcode() == FrameSetupOpcode ||
718          I->getOpcode() == FrameDestroyOpcode) {
719#ifndef NDEBUG
720        // Track whether we see even pairs of them
721        SPAdjCount += I->getOpcode() == FrameSetupOpcode ? 1 : -1;
722#endif
723        // Remember how much SP has been adjusted to create the call
724        // frame.
725        int Size = I->getOperand(0).getImm();
726
727        if ((!StackGrowsDown && I->getOpcode() == FrameSetupOpcode) ||
728            (StackGrowsDown && I->getOpcode() == FrameDestroyOpcode))
729          Size = -Size;
730
731        SPAdj += Size;
732
733        MachineBasicBlock::iterator PrevI = BB->end();
734        if (I != BB->begin()) PrevI = prior(I);
735        TRI.eliminateCallFramePseudoInstr(Fn, *BB, I);
736
737        // Visit the instructions created by eliminateCallFramePseudoInstr().
738        if (PrevI == BB->end())
739          I = BB->begin();     // The replaced instr was the first in the block.
740        else
741          I = llvm::next(PrevI);
742        continue;
743      }
744
745      MachineInstr *MI = I;
746      bool DoIncr = true;
747      for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
748        if (MI->getOperand(i).isFI()) {
749          // Some instructions (e.g. inline asm instructions) can have
750          // multiple frame indices and/or cause eliminateFrameIndex
751          // to insert more than one instruction. We need the register
752          // scavenger to go through all of these instructions so that
753          // it can update its register information. We keep the
754          // iterator at the point before insertion so that we can
755          // revisit them in full.
756          bool AtBeginning = (I == BB->begin());
757          if (!AtBeginning) --I;
758
759          // If this instruction has a FrameIndex operand, we need to
760          // use that target machine register info object to eliminate
761          // it.
762            TRI.eliminateFrameIndex(MI, SPAdj,
763                                    FrameIndexVirtualScavenging ?  NULL : RS);
764
765          // Reset the iterator if we were at the beginning of the BB.
766          if (AtBeginning) {
767            I = BB->begin();
768            DoIncr = false;
769          }
770
771          MI = 0;
772          break;
773        }
774
775      if (DoIncr && I != BB->end()) ++I;
776
777      // Update register states.
778      if (RS && !FrameIndexVirtualScavenging && MI) RS->forward(MI);
779    }
780
781    // If we have evenly matched pairs of frame setup / destroy instructions,
782    // make sure the adjustments come out to zero. If we don't have matched
783    // pairs, we can't be sure the missing bit isn't in another basic block
784    // due to a custom inserter playing tricks, so just asserting SPAdj==0
785    // isn't sufficient. See tMOVCC on Thumb1, for example.
786    assert((SPAdjCount || SPAdj == 0) &&
787           "Unbalanced call frame setup / destroy pairs?");
788  }
789}
790
791/// scavengeFrameVirtualRegs - Replace all frame index virtual registers
792/// with physical registers. Use the register scavenger to find an
793/// appropriate register to use.
794void PEI::scavengeFrameVirtualRegs(MachineFunction &Fn) {
795  // Run through the instructions and find any virtual registers.
796  for (MachineFunction::iterator BB = Fn.begin(),
797       E = Fn.end(); BB != E; ++BB) {
798    RS->enterBasicBlock(BB);
799
800    unsigned VirtReg = 0;
801    unsigned ScratchReg = 0;
802    int SPAdj = 0;
803
804    // The instruction stream may change in the loop, so check BB->end()
805    // directly.
806    for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
807      MachineInstr *MI = I;
808      bool DoIncr = true;
809      for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
810        if (MI->getOperand(i).isReg()) {
811          MachineOperand &MO = MI->getOperand(i);
812          unsigned Reg = MO.getReg();
813          if (Reg == 0)
814            continue;
815          if (!TargetRegisterInfo::isVirtualRegister(Reg))
816            continue;
817
818          ++NumVirtualFrameRegs;
819
820          // Have we already allocated a scratch register for this virtual?
821          if (Reg != VirtReg) {
822            // When we first encounter a new virtual register, it
823            // must be a definition.
824            assert(MI->getOperand(i).isDef() &&
825                   "frame index virtual missing def!");
826            // Scavenge a new scratch register
827            VirtReg = Reg;
828            const TargetRegisterClass *RC = Fn.getRegInfo().getRegClass(Reg);
829            ScratchReg = RS->scavengeRegister(RC, I, SPAdj);
830            ++NumScavengedRegs;
831          }
832          // Replace this reference to the virtual register with the
833          // scratch register.
834          assert (ScratchReg && "Missing scratch register!");
835          MI->getOperand(i).setReg(ScratchReg);
836
837        }
838      }
839      if (DoIncr) {
840        RS->forward(I);
841        ++I;
842      }
843    }
844  }
845}
846