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