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/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"
47 INITIALIZE_PASS_BEGIN(PEI
, "prologepilog",
48 "Prologue/Epilogue Insertion", false, false)
49 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo
)
50 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree
)
51 INITIALIZE_PASS_END(PEI
, "prologepilog",
52 "Prologue/Epilogue Insertion", false, false)
54 STATISTIC(NumVirtualFrameRegs
, "Number of virtual frame regs encountered");
55 STATISTIC(NumScavengedRegs
, "Number of frame index regs scavenged");
57 /// createPrologEpilogCodeInserter - This function returns a pass that inserts
58 /// prolog and epilog code, and eliminates abstract frame references.
60 FunctionPass
*llvm::createPrologEpilogCodeInserter() { return new PEI(); }
62 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
63 /// frame indexes with appropriate references.
65 bool 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
);
71 // Calculate the MaxCallFrameSize and AdjustsStack variables for the
72 // function's frame information. Also eliminates call frame pseudo
74 calculateCallsInformation(Fn
);
76 // Allow the target machine to make some adjustments to the function
77 // e.g. UsedPhysRegs before calculateCalleeSavedRegisters.
78 TRI
->processFunctionBeforeCalleeSavedScan(Fn
, RS
);
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
);
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
88 // - Without shink wrapping (default), place all spills in the
89 // entry block, all restores in return blocks.
90 placeCSRSpillsAndRestores(Fn
);
92 // Add the code to save and restore the callee saved registers
93 if (!F
->hasFnAttr(Attribute::Naked
))
94 insertCSRSpillsAndRestores(Fn
);
96 // Allow the target machine to make final modifications to the function
97 // before the frame layout is finalized.
98 TRI
->processFunctionBeforeFrameFinalized(Fn
);
100 // Calculate actual frame offsets for all abstract stack objects...
101 calculateFrameObjectOffsets(Fn
);
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
);
111 // Replace all MO_FrameIndex operands with physical register references
112 // and actual offsets.
114 replaceFrameIndices(Fn
);
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
119 if (TRI
->requiresRegisterScavenging(Fn
) && FrameIndexVirtualScavenging
)
120 scavengeFrameVirtualRegs(Fn
);
128 void PEI::getAnalysisUsage(AnalysisUsage
&AU
) const {
129 AU
.setPreservesCFG();
130 if (ShrinkWrapping
|| ShrinkWrapFunc
!= "") {
131 AU
.addRequired
<MachineLoopInfo
>();
132 AU
.addRequired
<MachineDominatorTree
>();
134 AU
.addPreserved
<MachineLoopInfo
>();
135 AU
.addPreserved
<MachineDominatorTree
>();
136 MachineFunctionPass::getAnalysisUsage(AU
);
140 /// calculateCallsInformation - Calculate the MaxCallFrameSize and AdjustsStack
141 /// variables for the function's frame information and eliminate call frame
142 /// pseudo instructions.
143 void PEI::calculateCallsInformation(MachineFunction
&Fn
) {
144 const TargetRegisterInfo
*RegInfo
= Fn
.getTarget().getRegisterInfo();
145 MachineFrameInfo
*MFI
= Fn
.getFrameInfo();
147 unsigned MaxCallFrameSize
= 0;
148 bool AdjustsStack
= MFI
->adjustsStack();
150 // Get the function call frame set-up and tear-down instruction opcode
151 int FrameSetupOpcode
= RegInfo
->getCallFrameSetupOpcode();
152 int FrameDestroyOpcode
= RegInfo
->getCallFrameDestroyOpcode();
154 // Early exit for targets which have no call frame setup/destroy pseudo
156 if (FrameSetupOpcode
== -1 && FrameDestroyOpcode
== -1)
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
;
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())
176 MFI
->setAdjustsStack(AdjustsStack
);
177 MFI
->setMaxCallFrameSize(MaxCallFrameSize
);
179 for (std::vector
<MachineBasicBlock::iterator
>::iterator
180 i
= FrameSDOps
.begin(), e
= FrameSDOps
.end(); i
!= e
; ++i
) {
181 MachineBasicBlock::iterator I
= *i
;
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
);
193 /// calculateCalleeSavedRegisters - Scan the function for modified callee saved
195 void PEI::calculateCalleeSavedRegisters(MachineFunction
&Fn
) {
196 const TargetRegisterInfo
*RegInfo
= Fn
.getTarget().getRegisterInfo();
197 const TargetFrameInfo
*TFI
= Fn
.getTarget().getFrameInfo();
198 MachineFrameInfo
*MFI
= Fn
.getFrameInfo();
200 // Get the callee saved register list...
201 const unsigned *CSRegs
= RegInfo
->getCalleeSavedRegs(&Fn
);
203 // These are used to keep track the callee-save area. Initialize them.
204 MinCSFrameIndex
= INT_MAX
;
207 // Early exit for targets which have no callee saved registers.
208 if (CSRegs
== 0 || CSRegs
[0] == 0)
211 // In Naked functions we aren't going to save any registers.
212 if (Fn
.getFunction()->hasFnAttr(Attribute::Naked
))
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
));
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
));
233 return; // Early exit if no callee saved registers are modified!
235 unsigned NumFixedSpillSlots
;
236 const TargetFrameInfo::SpillSlot
*FixedSpillSlots
=
237 TFI
->getCalleeSavedSpillSlots(NumFixedSpillSlots
);
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
);
247 if (RegInfo
->hasReservedSpillSlot(Fn
, Reg
, FrameIdx
)) {
248 I
->setFrameIdx(FrameIdx
);
252 // Check to see if this physreg must be spilled to a particular stack slot
254 const TargetFrameInfo::SpillSlot
*FixedSlot
= FixedSpillSlots
;
255 while (FixedSlot
!= FixedSpillSlots
+NumFixedSpillSlots
&&
256 FixedSlot
->Reg
!= Reg
)
259 if (FixedSlot
== FixedSpillSlots
+ NumFixedSpillSlots
) {
260 // Nope, just spill it anywhere convenient.
261 unsigned Align
= RC
->getAlignment();
262 unsigned StackAlign
= TFI
->getStackAlignment();
264 // We may not be able to satisfy the desired alignment specification of
265 // the TargetRegisterClass if the stack alignment is smaller. Use the
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
;
272 // Spill it to the stack where we must.
273 FrameIdx
= MFI
->CreateFixedObject(RC
->getSize(), FixedSlot
->Offset
, true);
276 I
->setFrameIdx(FrameIdx
);
279 MFI
->setCalleeSavedInfo(CSI
);
282 /// insertCSRSpillsAndRestores - Insert spill and restore code for
283 /// callee saved registers used in the function, handling shrink wrapping.
285 void PEI::insertCSRSpillsAndRestores(MachineFunction
&Fn
) {
286 // Get callee saved register information.
287 MachineFrameInfo
*MFI
= Fn
.getFrameInfo();
288 const std::vector
<CalleeSavedInfo
> &CSI
= MFI
->getCalleeSavedInfo();
290 MFI
->setCalleeSavedInfoValid(true);
292 // Early exit if no callee saved registers are modified!
296 const TargetInstrInfo
&TII
= *Fn
.getTarget().getInstrInfo();
297 const TargetRegisterInfo
*TRI
= Fn
.getTarget().getRegisterInfo();
298 MachineBasicBlock::iterator I
;
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());
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
);
317 // Restore using target interface.
318 for (unsigned ri
= 0, re
= ReturnBlocks
.size(); ri
!= re
; ++ri
) {
319 MachineBasicBlock
* MBB
= ReturnBlocks
[ri
];
322 // Skip over all terminator instructions, which are part of the return
324 MachineBasicBlock::iterator I2
= I
;
325 while (I2
!= MBB
->begin() && (--I2
)->getDesc().isTerminator())
328 bool AtStart
= I
== MBB
->begin();
329 MachineBasicBlock::iterator BeforeI
= I
;
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(),
342 assert(I
!= MBB
->begin() &&
343 "loadRegFromStackSlot didn't insert any code!");
344 // Insert in reverse order. loadRegFromStackSlot can insert
345 // multiple instructions.
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
;
369 for (CSRegSet::iterator RI
= save
.begin(),
370 RE
= save
.end(); RI
!= RE
; ++RI
) {
371 blockCSI
.push_back(CSI
[*RI
]);
373 assert(blockCSI
.size() > 0 &&
374 "Could not collect callee saved register info");
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());
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
,
389 blockCSI
[i
].getFrameIdx(),
394 for (CSRegBlockMap::iterator BI
= CSRRestore
.begin(),
395 BE
= CSRRestore
.end(); BI
!= BE
; ++BI
) {
396 MachineBasicBlock
* MBB
= BI
->first
;
397 CSRegSet restore
= BI
->second
;
403 for (CSRegSet::iterator RI
= restore
.begin(),
404 RE
= restore
.end(); RI
!= RE
; ++RI
) {
405 blockCSI
.push_back(CSI
[*RI
]);
407 assert(blockCSI
.size() > 0 &&
408 "Could not find callee saved register info");
410 // If MBB is empty and needs restores, insert at the _beginning_.
417 // Skip over all terminator instructions, which are part of the
419 if (! I
->getDesc().isTerminator()) {
422 MachineBasicBlock::iterator I2
= I
;
423 while (I2
!= MBB
->begin() && (--I2
)->getDesc().isTerminator())
428 bool AtStart
= I
== MBB
->begin();
429 MachineBasicBlock::iterator BeforeI
= I
;
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(),
441 assert(I
!= MBB
->begin() &&
442 "loadRegFromStackSlot didn't insert any code!");
443 // Insert in reverse order. loadRegFromStackSlot can insert
444 // multiple instructions.
455 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
457 AdjustStackOffset(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.
462 Offset
+= MFI
->getObjectSize(FrameIdx
);
464 unsigned Align
= MFI
->getObjectAlignment(FrameIdx
);
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
);
470 // Adjust to alignment boundary.
471 Offset
= (Offset
+ Align
- 1) / Align
* Align
;
473 if (StackGrowsDown
) {
474 DEBUG(dbgs() << "alloc FI(" << FrameIdx
<< ") at SP[" << -Offset
<< "]\n");
475 MFI
->setObjectOffset(FrameIdx
, -Offset
); // Set the computed offset
477 DEBUG(dbgs() << "alloc FI(" << FrameIdx
<< ") at SP[" << Offset
<< "]\n");
478 MFI
->setObjectOffset(FrameIdx
, Offset
);
479 Offset
+= MFI
->getObjectSize(FrameIdx
);
483 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
484 /// abstract stack objects.
486 void PEI::calculateFrameObjectOffsets(MachineFunction
&Fn
) {
487 const TargetFrameInfo
&TFI
= *Fn
.getTarget().getFrameInfo();
489 bool StackGrowsDown
=
490 TFI
.getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown
;
492 // Loop over all of the stack objects, assigning sequential addresses...
493 MachineFrameInfo
*MFI
= Fn
.getFrameInfo();
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();
500 LocalAreaOffset
= -LocalAreaOffset
;
501 assert(LocalAreaOffset
>= 0
502 && "Local area offset should be in direction of stack growth");
503 int64_t Offset
= LocalAreaOffset
;
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
) {
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
);
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
);
522 if (FixedOff
> Offset
) Offset
= FixedOff
;
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
);
533 unsigned Align
= MFI
->getObjectAlignment(i
);
534 // Adjust to alignment boundary
535 Offset
= (Offset
+Align
-1)/Align
*Align
;
537 MFI
->setObjectOffset(i
, -Offset
); // Set the computed offset
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
;
546 MFI
->setObjectOffset(i
, Offset
);
547 Offset
+= MFI
->getObjectSize(i
);
551 unsigned MaxAlign
= MFI
->getMaxAlignment();
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();
559 AdjustStackOffset(MFI
, SFI
, StackGrowsDown
, Offset
, MaxAlign
);
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();
569 // Adjust to alignment boundary.
570 Offset
= (Offset
+ Align
- 1) / Align
* Align
;
572 DEBUG(dbgs() << "Local frame base offset: " << Offset
<< "\n");
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[" <<
580 MFI
->setObjectOffset(Entry
.first
, FIOffset
);
582 // Allocate the local block
583 Offset
+= MFI
->getLocalFrameSize();
585 MaxAlign
= std::max(Align
, MaxAlign
);
588 // Make sure that the stack protector comes before the local variables on the
590 SmallSet
<int, 16> LargeStackObjs
;
591 if (MFI
->getStackProtectorIndex() >= 0) {
592 AdjustStackOffset(MFI
, MFI
->getStackProtectorIndex(), StackGrowsDown
,
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())
600 if (i
>= MinCSFrameIndex
&& i
<= MaxCSFrameIndex
)
602 if (RS
&& (int)i
== RS
->getScavengingFrameIndex())
604 if (MFI
->isDeadObjectIndex(i
))
606 if (MFI
->getStackProtectorIndex() == (int)i
)
608 if (!MFI
->MayNeedStackProtector(i
))
611 AdjustStackOffset(MFI
, i
, StackGrowsDown
, Offset
, MaxAlign
);
612 LargeStackObjs
.insert(i
);
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())
622 if (i
>= MinCSFrameIndex
&& i
<= MaxCSFrameIndex
)
624 if (RS
&& (int)i
== RS
->getScavengingFrameIndex())
626 if (MFI
->isDeadObjectIndex(i
))
628 if (MFI
->getStackProtectorIndex() == (int)i
)
630 if (LargeStackObjs
.count(i
))
633 AdjustStackOffset(MFI
, i
, StackGrowsDown
, Offset
, MaxAlign
);
636 // Make sure the special register scavenging spill slot is closest to the
638 if (RS
&& (!RegInfo
->hasFP(Fn
) || RegInfo
->needsStackRealignment(Fn
))) {
639 int SFI
= RS
->getScavengingFrameIndex();
641 AdjustStackOffset(MFI
, SFI
, StackGrowsDown
, Offset
, MaxAlign
);
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();
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
657 if (MFI
->adjustsStack() || MFI
->hasVarSizedObjects() ||
658 (RegInfo
->needsStackRealignment(Fn
) && MFI
->getObjectIndexEnd() != 0))
659 StackAlign
= TFI
.getStackAlignment();
661 StackAlign
= TFI
.getTransientStackAlignment();
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
);
670 // Update frame info to pretend that this is part of the stack...
671 MFI
->setStackSize(Offset
- LocalAreaOffset
);
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.
678 void PEI::insertPrologEpilogCode(MachineFunction
&Fn
) {
679 const TargetRegisterInfo
*TRI
= Fn
.getTarget().getRegisterInfo();
681 // Add prologue to the function...
682 TRI
->emitPrologue(Fn
);
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
);
692 /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
693 /// register references and actual offsets.
695 void PEI::replaceFrameIndices(MachineFunction
&Fn
) {
696 if (!Fn
.getFrameInfo()->hasStackObjects()) return; // Nothing to do?
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();
707 for (MachineFunction::iterator BB
= Fn
.begin(),
708 E
= Fn
.end(); BB
!= E
; ++BB
) {
710 int SPAdjCount
= 0; // frame setup / destroy count.
712 int SPAdj
= 0; // SP offset due to call frame setup / destroy.
713 if (RS
&& !FrameIndexVirtualScavenging
) RS
->enterBasicBlock(BB
);
715 for (MachineBasicBlock::iterator I
= BB
->begin(); I
!= BB
->end(); ) {
717 if (I
->getOpcode() == FrameSetupOpcode
||
718 I
->getOpcode() == FrameDestroyOpcode
) {
720 // Track whether we see even pairs of them
721 SPAdjCount
+= I
->getOpcode() == FrameSetupOpcode
? 1 : -1;
723 // Remember how much SP has been adjusted to create the call
725 int Size
= I
->getOperand(0).getImm();
727 if ((!StackGrowsDown
&& I
->getOpcode() == FrameSetupOpcode
) ||
728 (StackGrowsDown
&& I
->getOpcode() == FrameDestroyOpcode
))
733 MachineBasicBlock::iterator PrevI
= BB
->end();
734 if (I
!= BB
->begin()) PrevI
= prior(I
);
735 TRI
.eliminateCallFramePseudoInstr(Fn
, *BB
, I
);
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.
741 I
= llvm::next(PrevI
);
745 MachineInstr
*MI
= I
;
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
;
759 // If this instruction has a FrameIndex operand, we need to
760 // use that target machine register info object to eliminate
762 TRI
.eliminateFrameIndex(MI
, SPAdj
,
763 FrameIndexVirtualScavenging
? NULL
: RS
);
765 // Reset the iterator if we were at the beginning of the BB.
775 if (DoIncr
&& I
!= BB
->end()) ++I
;
777 // Update register states.
778 if (RS
&& !FrameIndexVirtualScavenging
&& MI
) RS
->forward(MI
);
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?");
791 /// scavengeFrameVirtualRegs - Replace all frame index virtual registers
792 /// with physical registers. Use the register scavenger to find an
793 /// appropriate register to use.
794 void 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
);
800 unsigned VirtReg
= 0;
801 unsigned ScratchReg
= 0;
804 // The instruction stream may change in the loop, so check BB->end()
806 for (MachineBasicBlock::iterator I
= BB
->begin(); I
!= BB
->end(); ) {
807 MachineInstr
*MI
= I
;
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();
815 if (!TargetRegisterInfo::isVirtualRegister(Reg
))
818 ++NumVirtualFrameRegs
;
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
828 const TargetRegisterClass
*RC
= Fn
.getRegInfo().getRegClass(Reg
);
829 ScratchReg
= RS
->scavengeRegister(RC
, I
, SPAdj
);
832 // Replace this reference to the virtual register with the
834 assert (ScratchReg
&& "Missing scratch register!");
835 MI
->getOperand(i
).setReg(ScratchReg
);