1 //===-- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass is responsible for finalizing the functions frame layout, saving
11 // callee saved registers, and for emitting prolog & epilog code for the
14 // This pass must be run after register allocation. After this pass is
15 // executed, it is illegal to construct MO_FrameIndex operands.
17 // This pass provides an optional shrink wrapping variant of prolog/epilog
18 // insertion, enabled via --shrink-wrap. See ShrinkWrapping.cpp.
20 //===----------------------------------------------------------------------===//
22 #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"
48 char &llvm::PrologEpilogCodeInserterID
= PEI::ID
;
50 INITIALIZE_PASS_BEGIN(PEI
, "prologepilog",
51 "Prologue/Epilogue Insertion", false, false)
52 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo
)
53 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree
)
54 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig
)
55 INITIALIZE_PASS_END(PEI
, "prologepilog",
56 "Prologue/Epilogue Insertion & Frame Finalization",
59 STATISTIC(NumVirtualFrameRegs
, "Number of virtual frame regs encountered");
60 STATISTIC(NumScavengedRegs
, "Number of frame index regs scavenged");
61 STATISTIC(NumBytesStackSpace
,
62 "Number of bytes used for stack in all functions");
64 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
65 /// frame indexes with appropriate references.
67 bool PEI::runOnMachineFunction(MachineFunction
&Fn
) {
68 const Function
* F
= Fn
.getFunction();
69 const TargetRegisterInfo
*TRI
= Fn
.getTarget().getRegisterInfo();
70 const TargetFrameLowering
*TFI
= Fn
.getTarget().getFrameLowering();
72 assert(!Fn
.getRegInfo().getNumVirtRegs() && "Regalloc must assign all vregs");
74 RS
= TRI
->requiresRegisterScavenging(Fn
) ? new RegScavenger() : NULL
;
75 FrameIndexVirtualScavenging
= TRI
->requiresFrameIndexScavenging(Fn
);
77 // Calculate the MaxCallFrameSize and AdjustsStack variables for the
78 // function's frame information. Also eliminates call frame pseudo
80 calculateCallsInformation(Fn
);
82 // Allow the target machine to make some adjustments to the function
83 // e.g. UsedPhysRegs before calculateCalleeSavedRegisters.
84 TFI
->processFunctionBeforeCalleeSavedScan(Fn
, RS
);
86 // Scan the function for modified callee saved registers and insert spill code
87 // for any callee saved registers that are modified.
88 calculateCalleeSavedRegisters(Fn
);
90 // Determine placement of CSR spill/restore code:
91 // - With shrink wrapping, place spills and restores to tightly
92 // enclose regions in the Machine CFG of the function where
94 // - Without shink wrapping (default), place all spills in the
95 // entry block, all restores in return blocks.
96 placeCSRSpillsAndRestores(Fn
);
98 // Add the code to save and restore the callee saved registers
99 if (!F
->getFnAttributes().hasNakedAttr())
100 insertCSRSpillsAndRestores(Fn
);
102 // Allow the target machine to make final modifications to the function
103 // before the frame layout is finalized.
104 TFI
->processFunctionBeforeFrameFinalized(Fn
);
106 // Calculate actual frame offsets for all abstract stack objects...
107 calculateFrameObjectOffsets(Fn
);
109 // Add prolog and epilog code to the function. This function is required
110 // to align the stack frame as necessary for any stack variables or
111 // called functions. Because of this, calculateCalleeSavedRegisters()
112 // must be called before this function in order to set the AdjustsStack
113 // and MaxCallFrameSize variables.
114 if (!F
->getFnAttributes().hasNakedAttr())
115 insertPrologEpilogCode(Fn
);
117 // Replace all MO_FrameIndex operands with physical register references
118 // and actual offsets.
120 replaceFrameIndices(Fn
);
122 // If register scavenging is needed, as we've enabled doing it as a
123 // post-pass, scavenge the virtual registers that frame index elimiation
125 if (TRI
->requiresRegisterScavenging(Fn
) && FrameIndexVirtualScavenging
)
126 scavengeFrameVirtualRegs(Fn
);
128 // Clear any vregs created by virtual scavenging.
129 Fn
.getRegInfo().clearVirtRegs();
137 void PEI::getAnalysisUsage(AnalysisUsage
&AU
) const {
138 AU
.setPreservesCFG();
139 if (ShrinkWrapping
|| ShrinkWrapFunc
!= "") {
140 AU
.addRequired
<MachineLoopInfo
>();
141 AU
.addRequired
<MachineDominatorTree
>();
143 AU
.addPreserved
<MachineLoopInfo
>();
144 AU
.addPreserved
<MachineDominatorTree
>();
145 MachineFunctionPass::getAnalysisUsage(AU
);
149 /// calculateCallsInformation - Calculate the MaxCallFrameSize and AdjustsStack
150 /// variables for the function's frame information and eliminate call frame
151 /// pseudo instructions.
152 void PEI::calculateCallsInformation(MachineFunction
&Fn
) {
153 const TargetRegisterInfo
*RegInfo
= Fn
.getTarget().getRegisterInfo();
154 const TargetInstrInfo
&TII
= *Fn
.getTarget().getInstrInfo();
155 const TargetFrameLowering
*TFI
= Fn
.getTarget().getFrameLowering();
156 MachineFrameInfo
*MFI
= Fn
.getFrameInfo();
158 unsigned MaxCallFrameSize
= 0;
159 bool AdjustsStack
= MFI
->adjustsStack();
161 // Get the function call frame set-up and tear-down instruction opcode
162 int FrameSetupOpcode
= TII
.getCallFrameSetupOpcode();
163 int FrameDestroyOpcode
= TII
.getCallFrameDestroyOpcode();
165 // Early exit for targets which have no call frame setup/destroy pseudo
167 if (FrameSetupOpcode
== -1 && FrameDestroyOpcode
== -1)
170 std::vector
<MachineBasicBlock::iterator
> FrameSDOps
;
171 for (MachineFunction::iterator BB
= Fn
.begin(), E
= Fn
.end(); BB
!= E
; ++BB
)
172 for (MachineBasicBlock::iterator I
= BB
->begin(); I
!= BB
->end(); ++I
)
173 if (I
->getOpcode() == FrameSetupOpcode
||
174 I
->getOpcode() == FrameDestroyOpcode
) {
175 assert(I
->getNumOperands() >= 1 && "Call Frame Setup/Destroy Pseudo"
176 " instructions should have a single immediate argument!");
177 unsigned Size
= I
->getOperand(0).getImm();
178 if (Size
> MaxCallFrameSize
) MaxCallFrameSize
= Size
;
180 FrameSDOps
.push_back(I
);
181 } else if (I
->isInlineAsm()) {
182 // Some inline asm's need a stack frame, as indicated by operand 1.
183 unsigned ExtraInfo
= I
->getOperand(InlineAsm::MIOp_ExtraInfo
).getImm();
184 if (ExtraInfo
& InlineAsm::Extra_IsAlignStack
)
188 MFI
->setAdjustsStack(AdjustsStack
);
189 MFI
->setMaxCallFrameSize(MaxCallFrameSize
);
191 for (std::vector
<MachineBasicBlock::iterator
>::iterator
192 i
= FrameSDOps
.begin(), e
= FrameSDOps
.end(); i
!= e
; ++i
) {
193 MachineBasicBlock::iterator I
= *i
;
195 // If call frames are not being included as part of the stack frame, and
196 // the target doesn't indicate otherwise, remove the call frame pseudos
197 // here. The sub/add sp instruction pairs are still inserted, but we don't
198 // need to track the SP adjustment for frame index elimination.
199 if (TFI
->canSimplifyCallFramePseudos(Fn
))
200 RegInfo
->eliminateCallFramePseudoInstr(Fn
, *I
->getParent(), I
);
205 /// calculateCalleeSavedRegisters - Scan the function for modified callee saved
207 void PEI::calculateCalleeSavedRegisters(MachineFunction
&Fn
) {
208 const TargetRegisterInfo
*RegInfo
= Fn
.getTarget().getRegisterInfo();
209 const TargetFrameLowering
*TFI
= Fn
.getTarget().getFrameLowering();
210 MachineFrameInfo
*MFI
= Fn
.getFrameInfo();
212 // Get the callee saved register list...
213 const uint16_t *CSRegs
= RegInfo
->getCalleeSavedRegs(&Fn
);
215 // These are used to keep track the callee-save area. Initialize them.
216 MinCSFrameIndex
= INT_MAX
;
219 // Early exit for targets which have no callee saved registers.
220 if (CSRegs
== 0 || CSRegs
[0] == 0)
223 // In Naked functions we aren't going to save any registers.
224 if (Fn
.getFunction()->getFnAttributes().hasNakedAttr())
227 std::vector
<CalleeSavedInfo
> CSI
;
228 for (unsigned i
= 0; CSRegs
[i
]; ++i
) {
229 unsigned Reg
= CSRegs
[i
];
230 if (Fn
.getRegInfo().isPhysRegOrOverlapUsed(Reg
)) {
231 // If the reg is modified, save it!
232 CSI
.push_back(CalleeSavedInfo(Reg
));
237 return; // Early exit if no callee saved registers are modified!
239 unsigned NumFixedSpillSlots
;
240 const TargetFrameLowering::SpillSlot
*FixedSpillSlots
=
241 TFI
->getCalleeSavedSpillSlots(NumFixedSpillSlots
);
243 // Now that we know which registers need to be saved and restored, allocate
244 // stack slots for them.
245 for (std::vector
<CalleeSavedInfo
>::iterator
246 I
= CSI
.begin(), E
= CSI
.end(); I
!= E
; ++I
) {
247 unsigned Reg
= I
->getReg();
248 const TargetRegisterClass
*RC
= RegInfo
->getMinimalPhysRegClass(Reg
);
251 if (RegInfo
->hasReservedSpillSlot(Fn
, Reg
, FrameIdx
)) {
252 I
->setFrameIdx(FrameIdx
);
256 // Check to see if this physreg must be spilled to a particular stack slot
258 const TargetFrameLowering::SpillSlot
*FixedSlot
= FixedSpillSlots
;
259 while (FixedSlot
!= FixedSpillSlots
+NumFixedSpillSlots
&&
260 FixedSlot
->Reg
!= Reg
)
263 if (FixedSlot
== FixedSpillSlots
+ NumFixedSpillSlots
) {
264 // Nope, just spill it anywhere convenient.
265 unsigned Align
= RC
->getAlignment();
266 unsigned StackAlign
= TFI
->getStackAlignment();
268 // We may not be able to satisfy the desired alignment specification of
269 // the TargetRegisterClass if the stack alignment is smaller. Use the
271 Align
= std::min(Align
, StackAlign
);
272 FrameIdx
= MFI
->CreateStackObject(RC
->getSize(), Align
, true);
273 if ((unsigned)FrameIdx
< MinCSFrameIndex
) MinCSFrameIndex
= FrameIdx
;
274 if ((unsigned)FrameIdx
> MaxCSFrameIndex
) MaxCSFrameIndex
= FrameIdx
;
276 // Spill it to the stack where we must.
277 FrameIdx
= MFI
->CreateFixedObject(RC
->getSize(), FixedSlot
->Offset
, true);
280 I
->setFrameIdx(FrameIdx
);
283 MFI
->setCalleeSavedInfo(CSI
);
286 /// insertCSRSpillsAndRestores - Insert spill and restore code for
287 /// callee saved registers used in the function, handling shrink wrapping.
289 void PEI::insertCSRSpillsAndRestores(MachineFunction
&Fn
) {
290 // Get callee saved register information.
291 MachineFrameInfo
*MFI
= Fn
.getFrameInfo();
292 const std::vector
<CalleeSavedInfo
> &CSI
= MFI
->getCalleeSavedInfo();
294 MFI
->setCalleeSavedInfoValid(true);
296 // Early exit if no callee saved registers are modified!
300 const TargetInstrInfo
&TII
= *Fn
.getTarget().getInstrInfo();
301 const TargetFrameLowering
*TFI
= Fn
.getTarget().getFrameLowering();
302 const TargetRegisterInfo
*TRI
= Fn
.getTarget().getRegisterInfo();
303 MachineBasicBlock::iterator I
;
305 if (!ShrinkWrapThisFunction
) {
306 // Spill using target interface.
307 I
= EntryBlock
->begin();
308 if (!TFI
->spillCalleeSavedRegisters(*EntryBlock
, I
, CSI
, TRI
)) {
309 for (unsigned i
= 0, e
= CSI
.size(); i
!= e
; ++i
) {
310 // Add the callee-saved register as live-in.
311 // It's killed at the spill.
312 EntryBlock
->addLiveIn(CSI
[i
].getReg());
314 // Insert the spill to the stack frame.
315 unsigned Reg
= CSI
[i
].getReg();
316 const TargetRegisterClass
*RC
= TRI
->getMinimalPhysRegClass(Reg
);
317 TII
.storeRegToStackSlot(*EntryBlock
, I
, Reg
, true,
318 CSI
[i
].getFrameIdx(), RC
, TRI
);
322 // Restore using target interface.
323 for (unsigned ri
= 0, re
= ReturnBlocks
.size(); ri
!= re
; ++ri
) {
324 MachineBasicBlock
* MBB
= ReturnBlocks
[ri
];
327 // Skip over all terminator instructions, which are part of the return
329 MachineBasicBlock::iterator I2
= I
;
330 while (I2
!= MBB
->begin() && (--I2
)->isTerminator())
333 bool AtStart
= I
== MBB
->begin();
334 MachineBasicBlock::iterator BeforeI
= I
;
338 // Restore all registers immediately before the return and any
339 // terminators that precede it.
340 if (!TFI
->restoreCalleeSavedRegisters(*MBB
, I
, CSI
, TRI
)) {
341 for (unsigned i
= 0, e
= CSI
.size(); i
!= e
; ++i
) {
342 unsigned Reg
= CSI
[i
].getReg();
343 const TargetRegisterClass
*RC
= TRI
->getMinimalPhysRegClass(Reg
);
344 TII
.loadRegFromStackSlot(*MBB
, I
, Reg
,
345 CSI
[i
].getFrameIdx(),
347 assert(I
!= MBB
->begin() &&
348 "loadRegFromStackSlot didn't insert any code!");
349 // Insert in reverse order. loadRegFromStackSlot can insert
350 // multiple instructions.
364 std::vector
<CalleeSavedInfo
> blockCSI
;
365 for (CSRegBlockMap::iterator BI
= CSRSave
.begin(),
366 BE
= CSRSave
.end(); BI
!= BE
; ++BI
) {
367 MachineBasicBlock
* MBB
= BI
->first
;
368 CSRegSet save
= BI
->second
;
374 for (CSRegSet::iterator RI
= save
.begin(),
375 RE
= save
.end(); RI
!= RE
; ++RI
) {
376 blockCSI
.push_back(CSI
[*RI
]);
378 assert(blockCSI
.size() > 0 &&
379 "Could not collect callee saved register info");
383 // When shrink wrapping, use stack slot stores/loads.
384 for (unsigned i
= 0, e
= blockCSI
.size(); i
!= e
; ++i
) {
385 // Add the callee-saved register as live-in.
386 // It's killed at the spill.
387 MBB
->addLiveIn(blockCSI
[i
].getReg());
389 // Insert the spill to the stack frame.
390 unsigned Reg
= blockCSI
[i
].getReg();
391 const TargetRegisterClass
*RC
= TRI
->getMinimalPhysRegClass(Reg
);
392 TII
.storeRegToStackSlot(*MBB
, I
, Reg
,
394 blockCSI
[i
].getFrameIdx(),
399 for (CSRegBlockMap::iterator BI
= CSRRestore
.begin(),
400 BE
= CSRRestore
.end(); BI
!= BE
; ++BI
) {
401 MachineBasicBlock
* MBB
= BI
->first
;
402 CSRegSet restore
= BI
->second
;
408 for (CSRegSet::iterator RI
= restore
.begin(),
409 RE
= restore
.end(); RI
!= RE
; ++RI
) {
410 blockCSI
.push_back(CSI
[*RI
]);
412 assert(blockCSI
.size() > 0 &&
413 "Could not find callee saved register info");
415 // If MBB is empty and needs restores, insert at the _beginning_.
422 // Skip over all terminator instructions, which are part of the
424 if (! I
->isTerminator()) {
427 MachineBasicBlock::iterator I2
= I
;
428 while (I2
!= MBB
->begin() && (--I2
)->isTerminator())
433 bool AtStart
= I
== MBB
->begin();
434 MachineBasicBlock::iterator BeforeI
= I
;
438 // Restore all registers immediately before the return and any
439 // terminators that precede it.
440 for (unsigned i
= 0, e
= blockCSI
.size(); i
!= e
; ++i
) {
441 unsigned Reg
= blockCSI
[i
].getReg();
442 const TargetRegisterClass
*RC
= TRI
->getMinimalPhysRegClass(Reg
);
443 TII
.loadRegFromStackSlot(*MBB
, I
, Reg
,
444 blockCSI
[i
].getFrameIdx(),
446 assert(I
!= MBB
->begin() &&
447 "loadRegFromStackSlot didn't insert any code!");
448 // Insert in reverse order. loadRegFromStackSlot can insert
449 // multiple instructions.
460 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
462 AdjustStackOffset(MachineFrameInfo
*MFI
, int FrameIdx
,
463 bool StackGrowsDown
, int64_t &Offset
,
464 unsigned &MaxAlign
) {
465 // If the stack grows down, add the object size to find the lowest address.
467 Offset
+= MFI
->getObjectSize(FrameIdx
);
469 unsigned Align
= MFI
->getObjectAlignment(FrameIdx
);
471 // If the alignment of this object is greater than that of the stack, then
472 // increase the stack alignment to match.
473 MaxAlign
= std::max(MaxAlign
, Align
);
475 // Adjust to alignment boundary.
476 Offset
= (Offset
+ Align
- 1) / Align
* Align
;
478 if (StackGrowsDown
) {
479 DEBUG(dbgs() << "alloc FI(" << FrameIdx
<< ") at SP[" << -Offset
<< "]\n");
480 MFI
->setObjectOffset(FrameIdx
, -Offset
); // Set the computed offset
482 DEBUG(dbgs() << "alloc FI(" << FrameIdx
<< ") at SP[" << Offset
<< "]\n");
483 MFI
->setObjectOffset(FrameIdx
, Offset
);
484 Offset
+= MFI
->getObjectSize(FrameIdx
);
488 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
489 /// abstract stack objects.
491 void PEI::calculateFrameObjectOffsets(MachineFunction
&Fn
) {
492 const TargetFrameLowering
&TFI
= *Fn
.getTarget().getFrameLowering();
494 bool StackGrowsDown
=
495 TFI
.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown
;
497 // Loop over all of the stack objects, assigning sequential addresses...
498 MachineFrameInfo
*MFI
= Fn
.getFrameInfo();
500 // Start at the beginning of the local area.
501 // The Offset is the distance from the stack top in the direction
502 // of stack growth -- so it's always nonnegative.
503 int LocalAreaOffset
= TFI
.getOffsetOfLocalArea();
505 LocalAreaOffset
= -LocalAreaOffset
;
506 assert(LocalAreaOffset
>= 0
507 && "Local area offset should be in direction of stack growth");
508 int64_t Offset
= LocalAreaOffset
;
510 // If there are fixed sized objects that are preallocated in the local area,
511 // non-fixed objects can't be allocated right at the start of local area.
512 // We currently don't support filling in holes in between fixed sized
513 // objects, so we adjust 'Offset' to point to the end of last fixed sized
514 // preallocated object.
515 for (int i
= MFI
->getObjectIndexBegin(); i
!= 0; ++i
) {
517 if (StackGrowsDown
) {
518 // The maximum distance from the stack pointer is at lower address of
519 // the object -- which is given by offset. For down growing stack
520 // the offset is negative, so we negate the offset to get the distance.
521 FixedOff
= -MFI
->getObjectOffset(i
);
523 // The maximum distance from the start pointer is at the upper
524 // address of the object.
525 FixedOff
= MFI
->getObjectOffset(i
) + MFI
->getObjectSize(i
);
527 if (FixedOff
> Offset
) Offset
= FixedOff
;
530 // First assign frame offsets to stack objects that are used to spill
531 // callee saved registers.
532 if (StackGrowsDown
) {
533 for (unsigned i
= MinCSFrameIndex
; i
<= MaxCSFrameIndex
; ++i
) {
534 // If the stack grows down, we need to add the size to find the lowest
535 // address of the object.
536 Offset
+= MFI
->getObjectSize(i
);
538 unsigned Align
= MFI
->getObjectAlignment(i
);
539 // Adjust to alignment boundary
540 Offset
= (Offset
+Align
-1)/Align
*Align
;
542 MFI
->setObjectOffset(i
, -Offset
); // Set the computed offset
545 int MaxCSFI
= MaxCSFrameIndex
, MinCSFI
= MinCSFrameIndex
;
546 for (int i
= MaxCSFI
; i
>= MinCSFI
; --i
) {
547 unsigned Align
= MFI
->getObjectAlignment(i
);
548 // Adjust to alignment boundary
549 Offset
= (Offset
+Align
-1)/Align
*Align
;
551 MFI
->setObjectOffset(i
, Offset
);
552 Offset
+= MFI
->getObjectSize(i
);
556 unsigned MaxAlign
= MFI
->getMaxAlignment();
558 // Make sure the special register scavenging spill slot is closest to the
559 // frame pointer if a frame pointer is required.
560 const TargetRegisterInfo
*RegInfo
= Fn
.getTarget().getRegisterInfo();
561 if (RS
&& TFI
.hasFP(Fn
) && RegInfo
->useFPForScavengingIndex(Fn
) &&
562 !RegInfo
->needsStackRealignment(Fn
)) {
563 int SFI
= RS
->getScavengingFrameIndex();
565 AdjustStackOffset(MFI
, SFI
, StackGrowsDown
, Offset
, MaxAlign
);
568 // FIXME: Once this is working, then enable flag will change to a target
569 // check for whether the frame is large enough to want to use virtual
570 // frame index registers. Functions which don't want/need this optimization
571 // will continue to use the existing code path.
572 if (MFI
->getUseLocalStackAllocationBlock()) {
573 unsigned Align
= MFI
->getLocalFrameMaxAlign();
575 // Adjust to alignment boundary.
576 Offset
= (Offset
+ Align
- 1) / Align
* Align
;
578 DEBUG(dbgs() << "Local frame base offset: " << Offset
<< "\n");
580 // Resolve offsets for objects in the local block.
581 for (unsigned i
= 0, e
= MFI
->getLocalFrameObjectCount(); i
!= e
; ++i
) {
582 std::pair
<int, int64_t> Entry
= MFI
->getLocalFrameObjectMap(i
);
583 int64_t FIOffset
= (StackGrowsDown
? -Offset
: Offset
) + Entry
.second
;
584 DEBUG(dbgs() << "alloc FI(" << Entry
.first
<< ") at SP[" <<
586 MFI
->setObjectOffset(Entry
.first
, FIOffset
);
588 // Allocate the local block
589 Offset
+= MFI
->getLocalFrameSize();
591 MaxAlign
= std::max(Align
, MaxAlign
);
594 // Make sure that the stack protector comes before the local variables on the
596 SmallSet
<int, 16> LargeStackObjs
;
597 if (MFI
->getStackProtectorIndex() >= 0) {
598 AdjustStackOffset(MFI
, MFI
->getStackProtectorIndex(), StackGrowsDown
,
601 // Assign large stack objects first.
602 for (unsigned i
= 0, e
= MFI
->getObjectIndexEnd(); i
!= e
; ++i
) {
603 if (MFI
->isObjectPreAllocated(i
) &&
604 MFI
->getUseLocalStackAllocationBlock())
606 if (i
>= MinCSFrameIndex
&& i
<= MaxCSFrameIndex
)
608 if (RS
&& (int)i
== RS
->getScavengingFrameIndex())
610 if (MFI
->isDeadObjectIndex(i
))
612 if (MFI
->getStackProtectorIndex() == (int)i
)
614 if (!MFI
->MayNeedStackProtector(i
))
617 AdjustStackOffset(MFI
, i
, StackGrowsDown
, Offset
, MaxAlign
);
618 LargeStackObjs
.insert(i
);
622 // Then assign frame offsets to stack objects that are not used to spill
623 // callee saved registers.
624 for (unsigned i
= 0, e
= MFI
->getObjectIndexEnd(); i
!= e
; ++i
) {
625 if (MFI
->isObjectPreAllocated(i
) &&
626 MFI
->getUseLocalStackAllocationBlock())
628 if (i
>= MinCSFrameIndex
&& i
<= MaxCSFrameIndex
)
630 if (RS
&& (int)i
== RS
->getScavengingFrameIndex())
632 if (MFI
->isDeadObjectIndex(i
))
634 if (MFI
->getStackProtectorIndex() == (int)i
)
636 if (LargeStackObjs
.count(i
))
639 AdjustStackOffset(MFI
, i
, StackGrowsDown
, Offset
, MaxAlign
);
642 // Make sure the special register scavenging spill slot is closest to the
644 if (RS
&& (!TFI
.hasFP(Fn
) || RegInfo
->needsStackRealignment(Fn
) ||
645 !RegInfo
->useFPForScavengingIndex(Fn
))) {
646 int SFI
= RS
->getScavengingFrameIndex();
648 AdjustStackOffset(MFI
, SFI
, StackGrowsDown
, Offset
, MaxAlign
);
651 if (!TFI
.targetHandlesStackFrameRounding()) {
652 // If we have reserved argument space for call sites in the function
653 // immediately on entry to the current function, count it as part of the
654 // overall stack size.
655 if (MFI
->adjustsStack() && TFI
.hasReservedCallFrame(Fn
))
656 Offset
+= MFI
->getMaxCallFrameSize();
658 // Round up the size to a multiple of the alignment. If the function has
659 // any calls or alloca's, align to the target's StackAlignment value to
660 // ensure that the callee's frame or the alloca data is suitably aligned;
661 // otherwise, for leaf functions, align to the TransientStackAlignment
664 if (MFI
->adjustsStack() || MFI
->hasVarSizedObjects() ||
665 (RegInfo
->needsStackRealignment(Fn
) && MFI
->getObjectIndexEnd() != 0))
666 StackAlign
= TFI
.getStackAlignment();
668 StackAlign
= TFI
.getTransientStackAlignment();
670 // If the frame pointer is eliminated, all frame offsets will be relative to
671 // SP not FP. Align to MaxAlign so this works.
672 StackAlign
= std::max(StackAlign
, MaxAlign
);
673 unsigned AlignMask
= StackAlign
- 1;
674 Offset
= (Offset
+ AlignMask
) & ~uint64_t(AlignMask
);
677 // Update frame info to pretend that this is part of the stack...
678 int64_t StackSize
= Offset
- LocalAreaOffset
;
679 MFI
->setStackSize(StackSize
);
680 NumBytesStackSpace
+= StackSize
;
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.
687 void PEI::insertPrologEpilogCode(MachineFunction
&Fn
) {
688 const TargetFrameLowering
&TFI
= *Fn
.getTarget().getFrameLowering();
690 // Add prologue to the function...
691 TFI
.emitPrologue(Fn
);
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().isReturn())
697 TFI
.emitEpilogue(Fn
, *I
);
700 // Emit additional code that is required to support segmented stacks, if
701 // we've been asked for it. This, when linked with a runtime with support
702 // for segmented stacks (libgcc is one), will result in allocating stack
703 // space in small chunks instead of one large contiguous block.
704 if (Fn
.getTarget().Options
.EnableSegmentedStacks
)
705 TFI
.adjustForSegmentedStacks(Fn
);
708 /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
709 /// register references and actual offsets.
711 void PEI::replaceFrameIndices(MachineFunction
&Fn
) {
712 if (!Fn
.getFrameInfo()->hasStackObjects()) return; // Nothing to do?
714 const TargetMachine
&TM
= Fn
.getTarget();
715 assert(TM
.getRegisterInfo() && "TM::getRegisterInfo() must be implemented!");
716 const TargetInstrInfo
&TII
= *Fn
.getTarget().getInstrInfo();
717 const TargetRegisterInfo
&TRI
= *TM
.getRegisterInfo();
718 const TargetFrameLowering
*TFI
= TM
.getFrameLowering();
719 bool StackGrowsDown
=
720 TFI
->getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown
;
721 int FrameSetupOpcode
= TII
.getCallFrameSetupOpcode();
722 int FrameDestroyOpcode
= TII
.getCallFrameDestroyOpcode();
724 for (MachineFunction::iterator BB
= Fn
.begin(),
725 E
= Fn
.end(); BB
!= E
; ++BB
) {
727 int SPAdjCount
= 0; // frame setup / destroy count.
729 int SPAdj
= 0; // SP offset due to call frame setup / destroy.
730 if (RS
&& !FrameIndexVirtualScavenging
) RS
->enterBasicBlock(BB
);
732 for (MachineBasicBlock::iterator I
= BB
->begin(); I
!= BB
->end(); ) {
734 if (I
->getOpcode() == FrameSetupOpcode
||
735 I
->getOpcode() == FrameDestroyOpcode
) {
737 // Track whether we see even pairs of them
738 SPAdjCount
+= I
->getOpcode() == FrameSetupOpcode
? 1 : -1;
740 // Remember how much SP has been adjusted to create the call
742 int Size
= I
->getOperand(0).getImm();
744 if ((!StackGrowsDown
&& I
->getOpcode() == FrameSetupOpcode
) ||
745 (StackGrowsDown
&& I
->getOpcode() == FrameDestroyOpcode
))
750 MachineBasicBlock::iterator PrevI
= BB
->end();
751 if (I
!= BB
->begin()) PrevI
= prior(I
);
752 TRI
.eliminateCallFramePseudoInstr(Fn
, *BB
, I
);
754 // Visit the instructions created by eliminateCallFramePseudoInstr().
755 if (PrevI
== BB
->end())
756 I
= BB
->begin(); // The replaced instr was the first in the block.
758 I
= llvm::next(PrevI
);
762 MachineInstr
*MI
= I
;
764 for (unsigned i
= 0, e
= MI
->getNumOperands(); i
!= e
; ++i
)
765 if (MI
->getOperand(i
).isFI()) {
766 // Some instructions (e.g. inline asm instructions) can have
767 // multiple frame indices and/or cause eliminateFrameIndex
768 // to insert more than one instruction. We need the register
769 // scavenger to go through all of these instructions so that
770 // it can update its register information. We keep the
771 // iterator at the point before insertion so that we can
772 // revisit them in full.
773 bool AtBeginning
= (I
== BB
->begin());
774 if (!AtBeginning
) --I
;
776 // If this instruction has a FrameIndex operand, we need to
777 // use that target machine register info object to eliminate
779 TRI
.eliminateFrameIndex(MI
, SPAdj
,
780 FrameIndexVirtualScavenging
? NULL
: RS
);
782 // Reset the iterator if we were at the beginning of the BB.
792 if (DoIncr
&& I
!= BB
->end()) ++I
;
794 // Update register states.
795 if (RS
&& !FrameIndexVirtualScavenging
&& MI
) RS
->forward(MI
);
798 // If we have evenly matched pairs of frame setup / destroy instructions,
799 // make sure the adjustments come out to zero. If we don't have matched
800 // pairs, we can't be sure the missing bit isn't in another basic block
801 // due to a custom inserter playing tricks, so just asserting SPAdj==0
802 // isn't sufficient. See tMOVCC on Thumb1, for example.
803 assert((SPAdjCount
|| SPAdj
== 0) &&
804 "Unbalanced call frame setup / destroy pairs?");
808 /// scavengeFrameVirtualRegs - Replace all frame index virtual registers
809 /// with physical registers. Use the register scavenger to find an
810 /// appropriate register to use.
812 /// FIXME: Iterating over the instruction stream is unnecessary. We can simply
813 /// iterate over the vreg use list, which at this point only contains machine
814 /// operands for which eliminateFrameIndex need a new scratch reg.
815 void PEI::scavengeFrameVirtualRegs(MachineFunction
&Fn
) {
816 // Run through the instructions and find any virtual registers.
817 for (MachineFunction::iterator BB
= Fn
.begin(),
818 E
= Fn
.end(); BB
!= E
; ++BB
) {
819 RS
->enterBasicBlock(BB
);
821 unsigned VirtReg
= 0;
822 unsigned ScratchReg
= 0;
825 // The instruction stream may change in the loop, so check BB->end()
827 for (MachineBasicBlock::iterator I
= BB
->begin(); I
!= BB
->end(); ) {
828 MachineInstr
*MI
= I
;
829 for (unsigned i
= 0, e
= MI
->getNumOperands(); i
!= e
; ++i
) {
830 if (MI
->getOperand(i
).isReg()) {
831 MachineOperand
&MO
= MI
->getOperand(i
);
832 unsigned Reg
= MO
.getReg();
835 if (!TargetRegisterInfo::isVirtualRegister(Reg
))
838 ++NumVirtualFrameRegs
;
840 // Have we already allocated a scratch register for this virtual?
841 if (Reg
!= VirtReg
) {
842 // When we first encounter a new virtual register, it
843 // must be a definition.
844 assert(MI
->getOperand(i
).isDef() &&
845 "frame index virtual missing def!");
846 // Scavenge a new scratch register
848 const TargetRegisterClass
*RC
= Fn
.getRegInfo().getRegClass(Reg
);
849 ScratchReg
= RS
->scavengeRegister(RC
, I
, SPAdj
);
852 // Replace this reference to the virtual register with the
854 assert (ScratchReg
&& "Missing scratch register!");
855 MI
->getOperand(i
).setReg(ScratchReg
);