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 implements a shrink wrapping variant of prolog/epilog insertion:
18 // - Places callee saved register (CSR) spills and restores in the CFG to
19 // tightly surround uses so that execution paths that do not use CSRs do not
20 // pay the spill/restore penalty.
22 // - Avoiding placment of spills/restores in loops: if a CSR is used inside a
23 // loop(nest), the spills are placed in the loop preheader, and restores are
24 // placed in the loop exit nodes (the successors of the loop _exiting_ nodes).
26 // - Covering paths without CSR uses: e.g. if a restore is placed in a join
27 // block, a matching spill is added to the end of all immediate predecessor
28 // blocks that are not reached by a spill. Similarly for saves placed in
31 // Shrink wrapping uses an analysis similar to the one in GVNPRE to determine
32 // which basic blocks require callee-saved register save/restore code.
34 // This pass uses MachineDominators and MachineLoopInfo. Loop information
35 // is used to prevent shrink wrapping of callee-saved register save/restore
38 //===----------------------------------------------------------------------===//
40 #define DEBUG_TYPE "shrink-wrap"
42 #include "llvm/CodeGen/Passes.h"
43 #include "llvm/CodeGen/MachineDominators.h"
44 #include "llvm/CodeGen/MachineLoopInfo.h"
45 #include "llvm/CodeGen/MachineFunctionPass.h"
46 #include "llvm/CodeGen/MachineInstr.h"
47 #include "llvm/CodeGen/MachineFrameInfo.h"
48 #include "llvm/CodeGen/MachineModuleInfo.h"
49 #include "llvm/CodeGen/MachineRegisterInfo.h"
50 #include "llvm/CodeGen/RegisterScavenging.h"
51 #include "llvm/Target/TargetMachine.h"
52 #include "llvm/Target/TargetRegisterInfo.h"
53 #include "llvm/Target/TargetFrameInfo.h"
54 #include "llvm/Target/TargetInstrInfo.h"
55 #include "llvm/ADT/SparseBitVector.h"
56 #include "llvm/ADT/DenseMap.h"
57 #include "llvm/ADT/PostOrderIterator.h"
58 #include "llvm/ADT/Statistic.h"
59 #include "llvm/Support/CommandLine.h"
60 #include "llvm/Support/Compiler.h"
61 #include "llvm/Support/Debug.h"
62 #include "llvm/ADT/STLExtras.h"
70 ShrinkWrapping("shrink-wrap",
71 cl::desc("Apply shrink wrapping to callee-saved register spills/restores"));
74 struct VISIBILITY_HIDDEN PEI
: public MachineFunctionPass
{
76 PEI() : MachineFunctionPass(&ID
) {}
78 const char *getPassName() const {
79 return "Prolog/Epilog Insertion & Frame Finalization";
82 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
85 AU
.addRequired
<MachineLoopInfo
>();
86 AU
.addRequired
<MachineDominatorTree
>();
88 AU
.addPreserved
<MachineLoopInfo
>();
89 AU
.addPreserved
<MachineDominatorTree
>();
90 MachineFunctionPass::getAnalysisUsage(AU
);
93 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
94 /// frame indexes with appropriate references.
96 bool runOnMachineFunction(MachineFunction
&Fn
) {
97 const TargetRegisterInfo
*TRI
= Fn
.getTarget().getRegisterInfo();
98 RS
= TRI
->requiresRegisterScavenging(Fn
) ? new RegScavenger() : NULL
;
100 // Get MachineModuleInfo so that we can track the construction of the
102 if (MachineModuleInfo
*MMI
= getAnalysisIfAvailable
<MachineModuleInfo
>())
103 Fn
.getFrameInfo()->setMachineModuleInfo(MMI
);
105 // Allow the target machine to make some adjustments to the function
106 // e.g. UsedPhysRegs before calculateCalleeSavedRegisters.
107 TRI
->processFunctionBeforeCalleeSavedScan(Fn
, RS
);
109 // Scan the function for modified callee saved registers and insert spill
110 // code for any callee saved registers that are modified. Also calculate
111 // the MaxCallFrameSize and HasCalls variables for the function's frame
112 // information and eliminates call frame pseudo instructions.
113 calculateCalleeSavedRegisters(Fn
);
115 // Determine placement of CSR spill/restore code:
116 // - with shrink wrapping, place spills and restores to tightly
117 // enclose regions in the Machine CFG of the function where
118 // they are used. Without shrink wrapping
119 // - default (no shrink wrapping), place all spills in the
120 // entry block, all restores in return blocks.
121 placeCSRSpillsAndRestores(Fn
);
123 // Add the code to save and restore the callee saved registers
124 insertCSRSpillsAndRestores(Fn
);
126 // Allow the target machine to make final modifications to the function
127 // before the frame layout is finalized.
128 TRI
->processFunctionBeforeFrameFinalized(Fn
);
130 // Calculate actual frame offsets for all of the abstract stack objects...
131 calculateFrameObjectOffsets(Fn
);
133 // Add prolog and epilog code to the function. This function is required
134 // to align the stack frame as necessary for any stack variables or
135 // called functions. Because of this, calculateCalleeSavedRegisters
136 // must be called before this function in order to set the HasCalls
137 // and MaxCallFrameSize variables.
138 insertPrologEpilogCode(Fn
);
140 // Replace all MO_FrameIndex operands with physical register references
141 // and actual offsets.
143 replaceFrameIndices(Fn
);
152 // MinCSFrameIndex, MaxCSFrameIndex - Keeps the range of callee saved
153 // stack frame indexes.
154 unsigned MinCSFrameIndex
, MaxCSFrameIndex
;
156 // Analysis info for spill/restore placement.
157 // "CSR": "callee saved register".
159 // CSRegSet contains indices into the Callee Saved Register Info
160 // vector built by calculateCalleeSavedRegisters() and accessed
161 // via MF.getFrameInfo()->getCalleeSavedInfo().
162 typedef SparseBitVector
<> CSRegSet
;
164 // CSRegBlockMap maps MachineBasicBlocks to sets of callee
165 // saved register indices.
166 typedef DenseMap
<MachineBasicBlock
*, CSRegSet
> CSRegBlockMap
;
168 // Set and maps for computing CSR spill/restore placement:
169 // used in function (UsedCSRegs)
170 // used in a basic block (CSRUsed)
171 // anticipatable in a basic block (Antic{In,Out})
172 // available in a basic block (Avail{In,Out})
173 // to be spilled at the entry to a basic block (CSRSave)
174 // to be restored at the end of a basic block (CSRRestore)
177 CSRegBlockMap CSRUsed
;
178 CSRegBlockMap AnticIn
, AnticOut
;
179 CSRegBlockMap AvailIn
, AvailOut
;
180 CSRegBlockMap CSRSave
;
181 CSRegBlockMap CSRRestore
;
183 // Entry and return blocks of the current function.
184 MachineBasicBlock
* EntryBlock
;
185 SmallVector
<MachineBasicBlock
*, 4> ReturnBlocks
;
187 // Flag to control shrink wrapping per-function:
188 // may choose to skip shrink wrapping for certain
190 bool ShrinkWrapThisFunction
;
192 bool calculateSets(MachineFunction
&Fn
);
193 void calculateAnticAvail(MachineFunction
&Fn
);
194 MachineBasicBlock
* moveSpillsOutOfLoops(MachineFunction
&Fn
,
195 MachineBasicBlock
* MBB
);
196 void addRestoresForSBranchBlock(MachineFunction
&Fn
,
197 MachineBasicBlock
* MBB
);
198 void moveRestoresOutOfLoops(MachineFunction
& Fn
,
199 MachineBasicBlock
* MBB
,
200 std::vector
<MachineBasicBlock
*>& SBLKS
);
201 void addSavesForRJoinBlocks(MachineFunction
& Fn
,
202 std::vector
<MachineBasicBlock
*>& SBLKS
);
203 void placeSpillsAndRestores(MachineFunction
&Fn
);
204 void placeCSRSpillsAndRestores(MachineFunction
&Fn
);
205 void calculateCalleeSavedRegisters(MachineFunction
&Fn
);
206 void insertCSRSpillsAndRestores(MachineFunction
&Fn
);
207 void calculateFrameObjectOffsets(MachineFunction
&Fn
);
208 void replaceFrameIndices(MachineFunction
&Fn
);
209 void insertPrologEpilogCode(MachineFunction
&Fn
);
211 // Initialize all shrink wrapping data.
212 void initShrinkWrappingInfo() {
222 if (! ReturnBlocks
.empty())
223 ReturnBlocks
.clear();
224 ShrinkWrapThisFunction
= ShrinkWrapping
;
227 // Convienences for dealing with machine loops.
228 MachineBasicBlock
* getTopLevelLoopPreheader(MachineLoop
* LP
) {
229 assert(LP
&& "Machine loop is NULL.");
230 MachineBasicBlock
* PHDR
= LP
->getLoopPreheader();
231 MachineLoop
* PLP
= LP
->getParentLoop();
233 PHDR
= PLP
->getLoopPreheader();
234 PLP
= PLP
->getParentLoop();
239 MachineLoop
* getTopLevelLoopParent(MachineLoop
*LP
) {
242 MachineLoop
* PLP
= LP
->getParentLoop();
245 PLP
= PLP
->getParentLoop();
251 // Debugging methods.
252 static std::string
getBasicBlockName(const MachineBasicBlock
* MBB
) {
253 std::ostringstream name
;
255 if (MBB
->getBasicBlock())
256 name
<< MBB
->getBasicBlock()->getName();
258 name
<< "_MBB_" << MBB
->getNumber();
263 static std::string
stringifyCSRegSet(const CSRegSet
& s
,
264 MachineFunction
&Fn
) {
265 const TargetRegisterInfo
* TRI
= Fn
.getTarget().getRegisterInfo();
266 const std::vector
<CalleeSavedInfo
> CSI
=
267 Fn
.getFrameInfo()->getCalleeSavedInfo();
269 std::ostringstream srep
;
270 if (CSI
.size() == 0) {
275 CSRegSet::iterator I
= s
.begin(), E
= s
.end();
277 unsigned reg
= CSI
[*I
].getReg();
278 srep
<< TRI
->getName(reg
);
279 for (++I
; I
!= E
; ++I
) {
280 reg
= CSI
[*I
].getReg();
282 srep
<< TRI
->getName(reg
);
289 static void dumpSet(const CSRegSet
& s
, MachineFunction
&Fn
) {
290 DOUT
<< stringifyCSRegSet(s
, Fn
) << "\n";
298 /// createPrologEpilogCodeInserter - This function returns a pass that inserts
299 /// prolog and epilog code, and eliminates abstract frame references.
301 FunctionPass
*llvm::createPrologEpilogCodeInserter() { return new PEI(); }
304 /// placeCSRSpillsAndRestores - determine which MBBs of the function
305 /// need save, restore code for callee-saved registers by doing a DF analysis
306 /// similar to the one used in code motion (GVNPRE). This produces maps of MBBs
307 /// to sets of registers (CSRs) for saves and restores. MachineLoopInfo
308 /// is used to ensure that CSR save/restore code is not placed inside loops.
309 /// This function computes the maps of MBBs -> CSRs to spill and restore
310 /// in CSRSave, CSRRestore.
312 /// If shrink wrapping is not being performed, place all spills in
313 /// the entry block, all restores in return blocks. In this case,
314 /// CSRSave has a single mapping, CSRRestore has mappings for each
317 void PEI::placeCSRSpillsAndRestores(MachineFunction
&Fn
) {
320 DOUT
<< "Place CSR spills/restores for "
321 << Fn
.getFunction()->getName() << "\n";
324 initShrinkWrappingInfo();
326 if (calculateSets(Fn
))
327 placeSpillsAndRestores(Fn
);
330 /// calculateAnticAvail - helper for computing the data flow
331 /// sets required for determining spill/restore placements.
333 void PEI::calculateAnticAvail(MachineFunction
&Fn
) {
335 // Calulate Antic{In,Out} and Avail{In,Out} iteratively on the MCFG.
337 unsigned iterations
= 0;
340 for (MachineFunction::iterator MBBI
= Fn
.begin(), MBBE
= Fn
.end();
341 MBBI
!= MBBE
; ++MBBI
) {
342 MachineBasicBlock
* MBB
= MBBI
;
344 // AnticOut[MBB] = INTERSECT(AnticIn[S] for S in SUCC(MBB))
345 MachineBasicBlock::succ_iterator SI
= MBB
->succ_begin(),
346 SE
= MBB
->succ_end();
348 CSRegSet prevAnticOut
= AnticOut
[MBB
];
349 MachineBasicBlock
* SUCC
= *SI
;
350 AnticOut
[MBB
] = AnticIn
[SUCC
];
351 for (++SI
; SI
!= SE
; ++SI
) {
353 AnticOut
[MBB
] &= AnticIn
[SUCC
];
355 if (prevAnticOut
!= AnticOut
[MBB
])
358 // AnticIn[MBB] = CSRUsed[MBB] | AnticOut[MBB];
359 CSRegSet prevAnticIn
= AnticIn
[MBB
];
360 AnticIn
[MBB
] = CSRUsed
[MBB
] | AnticOut
[MBB
];
361 if (prevAnticIn
|= AnticIn
[MBB
])
364 // AvailIn[MBB] = INTERSECT(AvailOut[S] for S in PRED(MBB))
365 MachineBasicBlock::pred_iterator PI
= MBB
->pred_begin(),
366 PE
= MBB
->pred_end();
368 CSRegSet prevAvailIn
= AvailIn
[MBB
];
369 MachineBasicBlock
* PRED
= *PI
;
370 AvailIn
[MBB
] = AvailOut
[PRED
];
371 for (++PI
; PI
!= PE
; ++PI
) {
373 AvailIn
[MBB
] &= AvailOut
[PRED
];
375 if (prevAvailIn
!= AvailIn
[MBB
])
378 // AvailOut[MBB] = CSRUsed[MBB] | AvailIn[MBB];
379 CSRegSet prevAvailOut
= AvailOut
[MBB
];
380 AvailOut
[MBB
] = CSRUsed
[MBB
] | AvailIn
[MBB
];
381 if (prevAvailOut
|= AvailOut
[MBB
])
388 AnticIn
[EntryBlock
].clear();
389 AnticOut
[EntryBlock
].clear();
392 DOUT
<< "-----------------------------------------------------------\n";
393 DOUT
<< "iterations = " << iterations
<< "\n";
394 DOUT
<< "-----------------------------------------------------------\n";
395 DOUT
<< "MBB | ANTIC_IN | ANTIC_OUT | AVAIL_IN | AVAIL_OUT\n";
396 DOUT
<< "-----------------------------------------------------------\n";
397 for (MachineFunction::iterator MBBI
= Fn
.begin(), MBBE
= Fn
.end();
398 MBBI
!= MBBE
; ++MBBI
) {
399 MachineBasicBlock
* MBB
= MBBI
;
401 DOUT
<< getBasicBlockName(MBB
) << " | "
402 << stringifyCSRegSet(AnticIn
[MBB
], Fn
)
404 << stringifyCSRegSet(AnticOut
[MBB
], Fn
)
406 << stringifyCSRegSet(AvailIn
[MBB
], Fn
)
408 << stringifyCSRegSet(AvailOut
[MBB
], Fn
)
414 /// calculateSets - helper function for placeCSRSpillsAndRestores,
415 /// collect the CSRs used in this function, develop the DF sets that
416 /// describe the minimal regions in the Machine CFG around which spills,
417 /// restores must be placed.
419 /// This function decides if shrink wrapping should actually be done:
420 /// if all CSR uses are in the entry block, no shrink wrapping is possible,
421 /// so ShrinkWrapping is turned off (for the current function) and the
422 /// function returns false.
424 bool PEI::calculateSets(MachineFunction
&Fn
) {
426 // Sets used to compute spill, restore placement sets.
427 const std::vector
<CalleeSavedInfo
> CSI
=
428 Fn
.getFrameInfo()->getCalleeSavedInfo();
430 // If no CSRs used, we are done.
433 DOUT
<< Fn
.getFunction()->getName()
434 << " uses no callee-saved registers.\n";
440 DOUT
<< "-----------------------------------------------------------\n";
443 const TargetRegisterInfo
*TRI
= Fn
.getTarget().getRegisterInfo();
444 bool allCSRUsesInEntryBlock
= true;
446 // Initialize UsedCSRegs set, CSRUsed map.
447 // At the same time, put entry block directly into
448 // CSRSave, CSRRestore sets if any CSRs are used.
450 // Quick exit option (not implemented):
451 // Given N CSR uses in entry block,
452 // revert to default behavior, skip the placement
453 // step and put all saves in entry, restores in
456 // Set up entry and return blocks.
457 EntryBlock
= Fn
.begin();
458 for (MachineFunction::iterator MBB
= Fn
.begin(), E
= Fn
.end();
460 if (!MBB
->empty() && MBB
->back().getDesc().isReturn())
461 ReturnBlocks
.push_back(MBB
);
463 // TODO -- check for a use of a CSR in each imm. successor of EntryBlock,
464 // do not shrink wrap this function if this is the case.
466 // If not shrink wrapping (this function) at this point, set bits in
467 // CSR{Save,Restore}[] and UsedCSRegs, then return.
468 if (! ShrinkWrapThisFunction
) {
469 for (unsigned inx
= 0, e
= CSI
.size(); inx
!= e
; ++inx
) {
471 CSRSave
[EntryBlock
].set(inx
);
472 for (unsigned ri
= 0, re
= ReturnBlocks
.size(); ri
!= re
; ++ri
)
473 CSRRestore
[ReturnBlocks
[ri
]].set(inx
);
478 // Walk instructions in all MBBs, create basic sets, choose
479 // whether or not to shrink wrap this function.
480 for (MachineFunction::iterator MBBI
= Fn
.begin(), MBBE
= Fn
.end();
481 MBBI
!= MBBE
; ++MBBI
) {
482 MachineBasicBlock
* MBB
= MBBI
;
483 for (MachineBasicBlock::iterator I
= MBB
->begin(); I
!= MBB
->end(); ++I
) {
484 for (unsigned inx
= 0, e
= CSI
.size(); inx
!= e
; ++inx
) {
485 unsigned Reg
= CSI
[inx
].getReg();
486 // If instruction I reads or modifies Reg, add it to UsedCSRegs,
487 // CSRUsed map for the current block.
488 for (unsigned opInx
= 0, opEnd
= I
->getNumOperands();
489 opInx
!= opEnd
; ++opInx
) {
490 const MachineOperand
&MO
= I
->getOperand(opInx
);
491 if (! (MO
.isReg() && (MO
.isUse() || MO
.isDef())))
493 unsigned MOReg
= MO
.getReg();
497 (TargetRegisterInfo::isPhysicalRegister(MOReg
) &&
498 TargetRegisterInfo::isPhysicalRegister(Reg
) &&
499 TRI
->isSubRegister(MOReg
, Reg
))) {
500 // CSR Reg is defined/used in block MBB.
502 CSRUsed
[MBB
].set(inx
);
503 // Short-circuit analysis for entry, return blocks:
504 // if a CSR is used in the entry block, add it directly
505 // to CSRSave[EntryBlock] and to CSRRestore[R] for R
506 // in ReturnBlocks. Note CSR uses in non-entry blocks.
507 if (ShrinkWrapThisFunction
) {
508 if (MBB
== EntryBlock
) {
509 CSRSave
[MBB
].set(inx
);
510 for (unsigned ri
= 0, re
= ReturnBlocks
.size(); ri
!= re
; ++ri
)
511 CSRRestore
[ReturnBlocks
[ri
]].set(inx
);
513 allCSRUsesInEntryBlock
= false;
515 // Not shrink wrapping => ensure saves/restores are correctly
516 // added for entry, return blocks.
517 CSRSave
[EntryBlock
].set(inx
);
518 for (unsigned ri
= 0, re
= ReturnBlocks
.size(); ri
!= re
; ++ri
)
519 CSRRestore
[ReturnBlocks
[ri
]].set(inx
);
526 DOUT
<< "CSRUsed[" << getBasicBlockName(MBB
) << "] = "
527 << stringifyCSRegSet(CSRUsed
[MBB
], Fn
) << "\n";
532 DOUT
<< "UsedCSRegs = " << stringifyCSRegSet(UsedCSRegs
, Fn
) << "\n";
536 // 1. Not asked to do shrink wrapping => just "place" all spills(restores)
537 // in the entry(return) block(s), already done above.
538 // 2. All CSR uses in entry block => same as case 1, but say we will
539 // not shrink wrap the current function.
540 ShrinkWrapThisFunction
= (ShrinkWrapping
&&
541 ShrinkWrapThisFunction
&&
542 ! allCSRUsesInEntryBlock
);
543 if (! ShrinkWrapThisFunction
) {
547 calculateAnticAvail(Fn
);
552 /// moveSpillsOutOfLoops - helper for placeSpillsAndRestores() which
553 /// relocates a spill from a subgraph in a loop to the loop preheader.
554 /// Returns the MBB to which saves have been moved, or the given MBB
555 /// if it is a branch point.
557 MachineBasicBlock
* PEI::moveSpillsOutOfLoops(MachineFunction
&Fn
,
558 MachineBasicBlock
* MBB
) {
559 if (MBB
== 0 || CSRSave
[MBB
].empty())
562 // Block to which saves are moved.
563 MachineBasicBlock
* DEST
= 0;
564 MachineLoopInfo
&LI
= getAnalysis
<MachineLoopInfo
>();
566 if (MachineLoop
* LP
= LI
.getLoopFor(MBB
)) {
567 MachineBasicBlock
* LPH
= getTopLevelLoopPreheader(LP
);
568 assert(LPH
&& "Loop has no top level preheader?");
571 DOUT
<< "Moving saves of "
572 << stringifyCSRegSet(CSRSave
[MBB
], Fn
)
573 << " from " << getBasicBlockName(MBB
)
574 << " to " << getBasicBlockName(LPH
) << "\n";
576 // Add CSRegSet from MBB to LPH, empty out MBB's CSRegSet.
577 CSRSave
[LPH
] |= CSRSave
[MBB
];
578 // If saves moved to entry block, add restores to returns.
579 if (LPH
== EntryBlock
) {
580 for (unsigned i
= 0, e
= ReturnBlocks
.size(); i
!= e
; ++i
)
581 CSRRestore
[ReturnBlocks
[i
]] |= CSRSave
[MBB
];
583 // Remember where we moved the save so we can add
584 // restores on successor paths if necessary.
585 if (LPH
->succ_size() > 1)
588 CSRSave
[MBB
].clear();
589 } else if (MBB
->succ_size() > 1)
594 /// addRestoresForSBranchBlock - helper for placeSpillsAndRestores() which
595 /// adds restores of CSRs saved in branch point MBBs to the front of any
596 /// successor blocks connected to regions with no uses of the saved CSRs.
598 void PEI::addRestoresForSBranchBlock(MachineFunction
&Fn
,
599 MachineBasicBlock
* MBB
) {
601 if (MBB
== 0 || CSRSave
[MBB
].empty() || MBB
->succ_size() < 2)
604 // Add restores of CSRs saved in branch point MBBs to the
605 // front of any succ blocks flowing into regions that
606 // have no uses of MBB's CSRs.
607 bool hasCSRUses
= false;
608 for (MachineBasicBlock::succ_iterator SI
= MBB
->succ_begin(),
609 SE
= MBB
->succ_end(); SI
!= SE
; ++SI
) {
610 MachineBasicBlock
* SUCC
= *SI
;
611 bool needsRestore
= false;
612 if (CSRUsed
[SUCC
].intersects(CSRSave
[MBB
])) {
617 for (df_iterator
<MachineBasicBlock
*> BI
= df_begin(SUCC
),
618 BE
= df_end(SUCC
); BI
!= BE
; ++BI
) {
619 MachineBasicBlock
* SBB
= *BI
;
620 if (CSRUsed
[SBB
].intersects(CSRSave
[MBB
])) {
622 needsRestore
= false;
626 // Additional restores are needed for SUCC iff there is at least
627 // one CSR use reachable from the successors of MBB and there
628 // are no uses in or below SUCC.
629 if (needsRestore
&& hasCSRUses
) {
631 DOUT
<< "MBB " << getBasicBlockName(MBB
)
632 << " needs a restore on path to successor "
633 << getBasicBlockName(SUCC
) << "\n";
635 // Add restores to SUCC for all CSRs saved in MBB...
636 CSRRestore
[SUCC
] = CSRSave
[MBB
];
641 /// moveRestoresOutOfLoops - helper for placeSpillsAndRestores() which
642 /// relocates restores from a subgraph in a loop to the loop exit blocks.
643 /// This function records the MBBs to which restores have been moved in
644 /// SBLKS. If no restores are moved, SBLKS contains the input MBB if it
645 /// is a join point in the Machine CFG.
647 void PEI::moveRestoresOutOfLoops(MachineFunction
& Fn
,
648 MachineBasicBlock
* MBB
,
649 std::vector
<MachineBasicBlock
*>& SBLKS
) {
652 if (MBB
== 0 || CSRRestore
[MBB
].empty())
655 MachineLoopInfo
&LI
= getAnalysis
<MachineLoopInfo
>();
657 if (MachineLoop
* LP
= LI
.getLoopFor(MBB
)) {
658 LP
= getTopLevelLoopParent(LP
);
659 assert(LP
&& "Loop with no top level parent?");
661 SmallVector
<MachineBasicBlock
*, 4> exitBlocks
;
663 LP
->getExitBlocks(exitBlocks
);
664 assert(exitBlocks
.size() > 0 &&
665 "Loop has no top level exit blocks?");
666 for (unsigned i
= 0, e
= exitBlocks
.size(); i
!= e
; ++i
) {
667 MachineBasicBlock
* EXB
= exitBlocks
[i
];
670 DOUT
<< "Moving restores of "
671 << stringifyCSRegSet(CSRRestore
[MBB
], Fn
)
672 << " from " << getBasicBlockName(MBB
)
673 << " to " << getBasicBlockName(EXB
) << "\n";
676 // Add CSRegSet from MBB to LPE, empty out MBB's CSRegSet.
677 CSRRestore
[EXB
] |= CSRRestore
[MBB
];
678 if (EXB
->pred_size() > 1)
679 SBLKS
.push_back(EXB
);
681 CSRRestore
[MBB
].clear();
682 } else if (MBB
->pred_size() > 1)
683 SBLKS
.push_back(MBB
);
686 /// addSavesForRJoinBlocks - Add saves of CSRs restored in join point MBBs
687 /// to the ends of any pred blocks that flow into MBB from regions that
688 /// have no uses of MBB's CSRs.
690 void PEI::addSavesForRJoinBlocks(MachineFunction
& Fn
,
691 std::vector
<MachineBasicBlock
*>& SBLKS
) {
696 for (unsigned i
= 0, e
= SBLKS
.size(); i
!= e
; ++i
) {
697 MachineBasicBlock
* MBB
= SBLKS
[i
];
698 if (MBB
->pred_size() > 1) {
699 bool needsSave
= false;
700 for (MachineBasicBlock::pred_iterator PI
= MBB
->pred_begin(),
701 PE
= MBB
->pred_end(); PI
!= PE
; ++PI
) {
702 MachineBasicBlock
* PRED
= *PI
;
704 // Walk back up in the CFG from the preds of MBB, look for
705 // a block that uses any CSR that is restored in MBB.
706 if (CSRUsed
[PRED
].intersects(CSRRestore
[MBB
]))
709 for (idf_iterator
<MachineBasicBlock
*> PPI
= idf_begin(PRED
),
710 PPE
= idf_end(PRED
); PPI
!= PPE
; ++PPI
) {
711 MachineBasicBlock
* PBB
= *PPI
;
712 if (CSRUsed
[PBB
].intersects(CSRRestore
[MBB
])) {
718 // Add saves to PRED for all CSRs restored in MBB...
720 DOUT
<< "MBB " << getBasicBlockName(MBB
)
721 << " needs a save on path from predecessor "
722 << getBasicBlockName(PRED
) << "\n";
724 CSRSave
[PRED
] = CSRRestore
[MBB
];
731 /// placeSpillsAndRestores - decide which MBBs need spills, restores
734 void PEI::placeSpillsAndRestores(MachineFunction
&Fn
) {
737 DOUT
<< "-----------------------------------------------------------\n";
740 // Calculate CSR{Save,Restore} using Antic, Avail on the Machine-CFG.
741 for (MachineFunction::iterator MBBI
= Fn
.begin(), MBBE
= Fn
.end();
742 MBBI
!= MBBE
; ++MBBI
) {
743 MachineBasicBlock
* MBB
= MBBI
;
744 // Entry block saves are recorded in UsedCSRegs pass above.
745 if (MBB
!= EntryBlock
) {
746 // Intersect (CSRegs - AnticIn[P]) for all predecessors P of MBB
747 CSRegSet anticInPreds
;
748 MachineBasicBlock::pred_iterator PI
= MBB
->pred_begin(),
749 PE
= MBB
->pred_end();
751 MachineBasicBlock
* PRED
= *PI
;
752 anticInPreds
= UsedCSRegs
- AnticIn
[PRED
];
753 for (++PI
; PI
!= PE
; ++PI
) {
757 anticInPreds
&= (UsedCSRegs
- AnticIn
[PRED
]);
760 // CSRSave[MBB] = (AnticIn[MBB] - AvailIn[MBB]) & anticInPreds
761 CSRSave
[MBB
] = (AnticIn
[MBB
] - AvailIn
[MBB
]) & anticInPreds
;
763 // Remove the CSRs that are saved in the entry block
764 if (! CSRSave
[MBB
].empty() && ! CSRSave
[EntryBlock
].empty())
765 CSRSave
[MBB
] = CSRSave
[MBB
] - CSRSave
[EntryBlock
];
767 // Move saves inside loops to the preheaders of the outermost
768 // containing loops, add restores to blocks reached by saves
769 // placed at branch points where necessary.
770 if (MachineBasicBlock
* DESTBB
= moveSpillsOutOfLoops(Fn
, MBB
)) {
771 // Add restores to blocks reached by saves placed at branch
772 // points where necessary.
773 addRestoresForSBranchBlock(Fn
, DESTBB
);
778 if (! CSRSave
[MBB
].empty())
779 DOUT
<< "SAVE[" << getBasicBlockName(MBB
) << "] = "
780 << stringifyCSRegSet(CSRSave
[MBB
], Fn
) << "\n";
783 // Compute CSRRestore, which may already be set for return blocks.
784 if (! CSRRestore
[MBB
].empty() || MBB
->pred_size() == 0)
787 // Intersect (CSRegs - AvailOut[S]) for all successors S of MBB
788 CSRegSet availOutSucc
;
789 MachineBasicBlock::succ_iterator SI
= MBB
->succ_begin(),
790 SE
= MBB
->succ_end();
792 MachineBasicBlock
* SUCC
= *SI
;
793 availOutSucc
= UsedCSRegs
- AvailOut
[SUCC
];
794 for (++SI
; SI
!= SE
; ++SI
) {
798 availOutSucc
&= (UsedCSRegs
- AvailOut
[SUCC
]);
800 } else if (! CSRUsed
[MBB
].empty()) {
801 // Take care of uses in return blocks (which have no successors).
802 availOutSucc
= UsedCSRegs
;
804 // CSRRestore[MBB] = (AvailOut[MBB] - AnticOut[MBB]) & availOutSucc
805 CSRRestore
[MBB
] = (AvailOut
[MBB
] - AnticOut
[MBB
]) & availOutSucc
;
807 // Remove the CSRs that are restored in the return blocks.
808 // Lest this be confusing, note that:
809 // CSRSave[EntryBlock] == CSRRestore[B] for all B in ReturnBlocks.
810 if (! CSRRestore
[MBB
].empty() && ! CSRSave
[EntryBlock
].empty())
811 CSRRestore
[MBB
] = CSRRestore
[MBB
] - CSRSave
[EntryBlock
];
813 // Move restores inside loops to the exits of the outermost (top level)
815 std::vector
<MachineBasicBlock
*> saveBlocks
;
816 moveRestoresOutOfLoops(Fn
, MBB
, saveBlocks
);
818 // Add saves of CSRs restored in join point MBBs to the ends
819 // of any pred blocks that flow into MBB from regions that
820 // have no uses of MBB's CSRs.
821 addSavesForRJoinBlocks(Fn
, saveBlocks
);
824 if (! CSRRestore
[MBB
].empty())
825 DOUT
<< "RESTORE[" << getBasicBlockName(MBB
) << "] = "
826 << stringifyCSRegSet(CSRRestore
[MBB
], Fn
) << "\n";
831 DOUT
<< "-----------------------------------------------------------\n";
832 DOUT
<< "Final SAVE, RESTORE:\n";
833 DOUT
<< "-----------------------------------------------------------\n";
834 for (MachineFunction::iterator MBB
= Fn
.begin(), E
= Fn
.end();
836 if (! CSRSave
[MBB
].empty()) {
837 DOUT
<< "SAVE[" << getBasicBlockName(MBB
) << "] = "
838 << stringifyCSRegSet(CSRSave
[MBB
], Fn
);
839 if (CSRRestore
[MBB
].empty())
842 if (! CSRRestore
[MBB
].empty()) {
843 if (! CSRSave
[MBB
].empty())
845 DOUT
<< "RESTORE[" << getBasicBlockName(MBB
) << "] = "
846 << stringifyCSRegSet(CSRRestore
[MBB
], Fn
) << "\n";
852 /// calculateCalleeSavedRegisters - Scan the function for modified callee saved
853 /// registers. Also calculate the MaxCallFrameSize and HasCalls variables for
854 /// the function's frame information and eliminates call frame pseudo
857 void PEI::calculateCalleeSavedRegisters(MachineFunction
&Fn
) {
858 const TargetRegisterInfo
*RegInfo
= Fn
.getTarget().getRegisterInfo();
859 const TargetFrameInfo
*TFI
= Fn
.getTarget().getFrameInfo();
861 // Get the callee saved register list...
862 const unsigned *CSRegs
= RegInfo
->getCalleeSavedRegs(&Fn
);
864 // Get the function call frame set-up and tear-down instruction opcode
865 int FrameSetupOpcode
= RegInfo
->getCallFrameSetupOpcode();
866 int FrameDestroyOpcode
= RegInfo
->getCallFrameDestroyOpcode();
868 // These are used to keep track the callee-save area. Initialize them.
869 MinCSFrameIndex
= INT_MAX
;
872 // Early exit for targets which have no callee saved registers and no call
873 // frame setup/destroy pseudo instructions.
874 if ((CSRegs
== 0 || CSRegs
[0] == 0) &&
875 FrameSetupOpcode
== -1 && FrameDestroyOpcode
== -1)
878 unsigned MaxCallFrameSize
= 0;
879 bool HasCalls
= false;
881 std::vector
<MachineBasicBlock::iterator
> FrameSDOps
;
882 for (MachineFunction::iterator BB
= Fn
.begin(), E
= Fn
.end(); BB
!= E
; ++BB
)
883 for (MachineBasicBlock::iterator I
= BB
->begin(); I
!= BB
->end(); ++I
)
884 if (I
->getOpcode() == FrameSetupOpcode
||
885 I
->getOpcode() == FrameDestroyOpcode
) {
886 assert(I
->getNumOperands() >= 1 && "Call Frame Setup/Destroy Pseudo"
887 " instructions should have a single immediate argument!");
888 unsigned Size
= I
->getOperand(0).getImm();
889 if (Size
> MaxCallFrameSize
) MaxCallFrameSize
= Size
;
891 FrameSDOps
.push_back(I
);
894 MachineFrameInfo
*FFI
= Fn
.getFrameInfo();
895 FFI
->setHasCalls(HasCalls
);
896 FFI
->setMaxCallFrameSize(MaxCallFrameSize
);
898 for (unsigned i
= 0, e
= FrameSDOps
.size(); i
!= e
; ++i
) {
899 MachineBasicBlock::iterator I
= FrameSDOps
[i
];
900 // If call frames are not being included as part of the stack frame,
901 // and there is no dynamic allocation (therefore referencing frame slots
902 // off sp), leave the pseudo ops alone. We'll eliminate them later.
903 if (RegInfo
->hasReservedCallFrame(Fn
) || RegInfo
->hasFP(Fn
))
904 RegInfo
->eliminateCallFramePseudoInstr(Fn
, *I
->getParent(), I
);
907 // Now figure out which *callee saved* registers are modified by the current
908 // function, thus needing to be saved and restored in the prolog/epilog.
910 const TargetRegisterClass
* const *CSRegClasses
=
911 RegInfo
->getCalleeSavedRegClasses(&Fn
);
912 std::vector
<CalleeSavedInfo
> CSI
;
913 for (unsigned i
= 0; CSRegs
[i
]; ++i
) {
914 unsigned Reg
= CSRegs
[i
];
915 if (Fn
.getRegInfo().isPhysRegUsed(Reg
)) {
916 // If the reg is modified, save it!
917 CSI
.push_back(CalleeSavedInfo(Reg
, CSRegClasses
[i
]));
919 for (const unsigned *AliasSet
= RegInfo
->getAliasSet(Reg
);
920 *AliasSet
; ++AliasSet
) { // Check alias registers too.
921 if (Fn
.getRegInfo().isPhysRegUsed(*AliasSet
)) {
922 CSI
.push_back(CalleeSavedInfo(Reg
, CSRegClasses
[i
]));
930 return; // Early exit if no callee saved registers are modified!
932 unsigned NumFixedSpillSlots
;
933 const std::pair
<unsigned,int> *FixedSpillSlots
=
934 TFI
->getCalleeSavedSpillSlots(NumFixedSpillSlots
);
936 // Now that we know which registers need to be saved and restored, allocate
937 // stack slots for them.
938 for (unsigned i
= 0, e
= CSI
.size(); i
!= e
; ++i
) {
939 unsigned Reg
= CSI
[i
].getReg();
940 const TargetRegisterClass
*RC
= CSI
[i
].getRegClass();
942 // Check to see if this physreg must be spilled to a particular stack slot
944 const std::pair
<unsigned,int> *FixedSlot
= FixedSpillSlots
;
945 while (FixedSlot
!= FixedSpillSlots
+NumFixedSpillSlots
&&
946 FixedSlot
->first
!= Reg
)
950 if (FixedSlot
== FixedSpillSlots
+NumFixedSpillSlots
) {
951 // Nope, just spill it anywhere convenient.
952 unsigned Align
= RC
->getAlignment();
953 unsigned StackAlign
= TFI
->getStackAlignment();
954 // We may not be able to sastify the desired alignment specification of
955 // the TargetRegisterClass if the stack alignment is smaller.
957 Align
= std::min(Align
, StackAlign
);
958 FrameIdx
= FFI
->CreateStackObject(RC
->getSize(), Align
);
959 if ((unsigned)FrameIdx
< MinCSFrameIndex
) MinCSFrameIndex
= FrameIdx
;
960 if ((unsigned)FrameIdx
> MaxCSFrameIndex
) MaxCSFrameIndex
= FrameIdx
;
962 // Spill it to the stack where we must.
963 FrameIdx
= FFI
->CreateFixedObject(RC
->getSize(), FixedSlot
->second
);
965 CSI
[i
].setFrameIdx(FrameIdx
);
968 FFI
->setCalleeSavedInfo(CSI
);
971 /// insertCSRSpillsAndRestores - Insert spill and restore code for
972 /// callee saved registers used in the function, handling shrink wrapping.
974 void PEI::insertCSRSpillsAndRestores(MachineFunction
&Fn
) {
975 // Get callee saved register information.
976 MachineFrameInfo
*FFI
= Fn
.getFrameInfo();
977 const std::vector
<CalleeSavedInfo
> &CSI
= FFI
->getCalleeSavedInfo();
979 // Early exit if no callee saved registers are modified!
983 const TargetInstrInfo
&TII
= *Fn
.getTarget().getInstrInfo();
984 MachineBasicBlock::iterator I
;
985 std::vector
<CalleeSavedInfo
> blockCSI
;
988 DOUT
<< "Inserting spill/restore code for CSRs in function "
989 << Fn
.getFunction()->getName() << "\n";
993 for (CSRegBlockMap::iterator
994 BI
= CSRSave
.begin(), BE
= CSRSave
.end(); BI
!= BE
; ++BI
) {
995 MachineBasicBlock
* MBB
= BI
->first
;
996 CSRegSet save
= BI
->second
;
1001 if (! ShrinkWrapThisFunction
) {
1002 // Spill using target interface.
1004 if (!TII
.spillCalleeSavedRegisters(*MBB
, I
, CSI
)) {
1005 for (unsigned i
= 0, e
= CSI
.size(); i
!= e
; ++i
) {
1006 // Add the callee-saved register as live-in. It's killed at the spill.
1007 MBB
->addLiveIn(CSI
[i
].getReg());
1009 // Insert the spill to the stack frame.
1010 TII
.storeRegToStackSlot(*MBB
, I
, CSI
[i
].getReg(), true,
1011 CSI
[i
].getFrameIdx(), CSI
[i
].getRegClass());
1016 DOUT
<< "CSRSave[" << getBasicBlockName(MBB
) << "] = "
1017 << stringifyCSRegSet(save
, Fn
) << "\n";
1021 for (CSRegSet::iterator RI
= save
.begin(),
1022 RE
= save
.end(); RI
!= RE
; ++RI
) {
1023 blockCSI
.push_back(CSI
[*RI
]);
1025 assert(blockCSI
.size() > 0 &&
1026 "Could not collect callee saved register info");
1028 // If MBB has no uses of CSRs being saved, this means saves
1029 // must be inserted at the _end_.
1030 if (! MBB
->empty() && ! CSRUsed
[MBB
].intersects(save
)) {
1033 if (I
->getDesc().isCall()) {
1036 MachineBasicBlock::iterator I2
= I
;
1037 while (I2
!= MBB
->begin() && (--I2
)->getDesc().isTerminator())
1044 // When shrink wrapping, use stack slot stores/loads.
1045 for (unsigned i
= 0, e
= blockCSI
.size(); i
!= e
; ++i
) {
1046 // Add the callee-saved register as live-in.
1047 // It's killed at the spill.
1048 MBB
->addLiveIn(blockCSI
[i
].getReg());
1050 // Insert the spill to the stack frame.
1051 TII
.storeRegToStackSlot(*MBB
, I
, blockCSI
[i
].getReg(),
1053 blockCSI
[i
].getFrameIdx(),
1054 blockCSI
[i
].getRegClass());
1058 // Use CSRRestore to add code to restore the callee-saved registers in
1060 for (CSRegBlockMap::iterator
1061 BI
= CSRRestore
.begin(), BE
= CSRRestore
.end(); BI
!= BE
; ++BI
) {
1062 MachineBasicBlock
* MBB
= BI
->first
;
1063 CSRegSet restore
= BI
->second
;
1065 if (restore
.empty())
1067 if (! ShrinkWrapThisFunction
) {
1068 // Restore using target interface.
1069 I
= MBB
->end(); --I
;
1071 // Skip over all terminator instructions, which are part of the return
1073 MachineBasicBlock::iterator I2
= I
;
1074 while (I2
!= MBB
->begin() && (--I2
)->getDesc().isTerminator())
1077 bool AtStart
= I
== MBB
->begin();
1078 MachineBasicBlock::iterator BeforeI
= I
;
1082 // Restore all registers immediately before the return and any
1083 // terminators that preceed it.
1084 if (!TII
.restoreCalleeSavedRegisters(*MBB
, I
, CSI
)) {
1085 for (unsigned i
= 0, e
= CSI
.size(); i
!= e
; ++i
) {
1086 TII
.loadRegFromStackSlot(*MBB
, I
, CSI
[i
].getReg(),
1087 CSI
[i
].getFrameIdx(),
1088 CSI
[i
].getRegClass());
1089 assert(I
!= MBB
->begin() &&
1090 "loadRegFromStackSlot didn't insert any code!");
1091 // Insert in reverse order. loadRegFromStackSlot can insert
1092 // multiple instructions.
1103 DOUT
<< "CSRRestore[" << getBasicBlockName(MBB
) << "] = "
1104 << stringifyCSRegSet(restore
, Fn
) << "\n";
1108 for (CSRegSet::iterator RI
= restore
.begin(),
1109 RE
= restore
.end(); RI
!= RE
; ++RI
) {
1110 blockCSI
.push_back(CSI
[*RI
]);
1112 assert(blockCSI
.size() > 0 &&
1113 "Could not find callee saved register info");
1115 // If MBB uses no CSRs but has restores, this means
1116 // it must have restores inserted at the _beginning_.
1117 // N.B. -- not necessary if edge splitting done.
1118 if (MBB
->empty() || ! CSRUsed
[MBB
].intersects(restore
)) {
1124 // EXP iff spill/restore implemented with push/pop:
1125 // append restore to block unless it ends in a
1126 // barrier terminator instruction.
1128 // Skip over all terminator instructions, which are part of the
1130 if (I
->getDesc().isCall()) {
1133 MachineBasicBlock::iterator I2
= I
;
1134 while (I2
!= MBB
->begin() && (--I2
)->getDesc().isTerminator())
1139 bool AtStart
= I
== MBB
->begin();
1140 MachineBasicBlock::iterator BeforeI
= I
;
1145 if (! MBB
->empty() && ! CSRUsed
[MBB
].intersects(restore
)) {
1146 MachineInstr
* MI
= BeforeI
;
1147 DOUT
<< "adding restore after ";
1150 DOUT
<< "adding restore to beginning of "
1151 << getBasicBlockName(MBB
) << "\n";
1155 // Restore all registers immediately before the return and any
1156 // terminators that preceed it.
1157 for (unsigned i
= 0, e
= blockCSI
.size(); i
!= e
; ++i
) {
1158 TII
.loadRegFromStackSlot(*MBB
, I
, blockCSI
[i
].getReg(),
1159 blockCSI
[i
].getFrameIdx(),
1160 blockCSI
[i
].getRegClass());
1161 assert(I
!= MBB
->begin() &&
1162 "loadRegFromStackSlot didn't insert any code!");
1163 // Insert in reverse order. loadRegFromStackSlot can insert
1164 // multiple instructions.
1176 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
1178 AdjustStackOffset(MachineFrameInfo
*FFI
, int FrameIdx
,
1179 bool StackGrowsDown
, int64_t &Offset
,
1180 unsigned &MaxAlign
) {
1181 // If stack grows down, we need to add size of find the lowest address of the
1184 Offset
+= FFI
->getObjectSize(FrameIdx
);
1186 unsigned Align
= FFI
->getObjectAlignment(FrameIdx
);
1188 // If the alignment of this object is greater than that of the stack, then
1189 // increase the stack alignment to match.
1190 MaxAlign
= std::max(MaxAlign
, Align
);
1192 // Adjust to alignment boundary.
1193 Offset
= (Offset
+ Align
- 1) / Align
* Align
;
1195 if (StackGrowsDown
) {
1196 FFI
->setObjectOffset(FrameIdx
, -Offset
); // Set the computed offset
1198 FFI
->setObjectOffset(FrameIdx
, Offset
);
1199 Offset
+= FFI
->getObjectSize(FrameIdx
);
1203 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
1204 /// abstract stack objects.
1206 void PEI::calculateFrameObjectOffsets(MachineFunction
&Fn
) {
1207 const TargetFrameInfo
&TFI
= *Fn
.getTarget().getFrameInfo();
1209 bool StackGrowsDown
=
1210 TFI
.getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown
;
1212 // Loop over all of the stack objects, assigning sequential addresses...
1213 MachineFrameInfo
*FFI
= Fn
.getFrameInfo();
1215 unsigned MaxAlign
= FFI
->getMaxAlignment();
1217 // Start at the beginning of the local area.
1218 // The Offset is the distance from the stack top in the direction
1219 // of stack growth -- so it's always nonnegative.
1220 int64_t Offset
= TFI
.getOffsetOfLocalArea();
1224 && "Local area offset should be in direction of stack growth");
1226 // If there are fixed sized objects that are preallocated in the local area,
1227 // non-fixed objects can't be allocated right at the start of local area.
1228 // We currently don't support filling in holes in between fixed sized
1229 // objects, so we adjust 'Offset' to point to the end of last fixed sized
1230 // preallocated object.
1231 for (int i
= FFI
->getObjectIndexBegin(); i
!= 0; ++i
) {
1233 if (StackGrowsDown
) {
1234 // The maximum distance from the stack pointer is at lower address of
1235 // the object -- which is given by offset. For down growing stack
1236 // the offset is negative, so we negate the offset to get the distance.
1237 FixedOff
= -FFI
->getObjectOffset(i
);
1239 // The maximum distance from the start pointer is at the upper
1240 // address of the object.
1241 FixedOff
= FFI
->getObjectOffset(i
) + FFI
->getObjectSize(i
);
1243 if (FixedOff
> Offset
) Offset
= FixedOff
;
1246 // First assign frame offsets to stack objects that are used to spill
1247 // callee saved registers.
1248 if (StackGrowsDown
) {
1249 for (unsigned i
= MinCSFrameIndex
; i
<= MaxCSFrameIndex
; ++i
) {
1250 // If stack grows down, we need to add size of find the lowest
1251 // address of the object.
1252 Offset
+= FFI
->getObjectSize(i
);
1254 unsigned Align
= FFI
->getObjectAlignment(i
);
1255 // If the alignment of this object is greater than that of the stack,
1256 // then increase the stack alignment to match.
1257 MaxAlign
= std::max(MaxAlign
, Align
);
1258 // Adjust to alignment boundary
1259 Offset
= (Offset
+Align
-1)/Align
*Align
;
1261 FFI
->setObjectOffset(i
, -Offset
); // Set the computed offset
1264 int MaxCSFI
= MaxCSFrameIndex
, MinCSFI
= MinCSFrameIndex
;
1265 for (int i
= MaxCSFI
; i
>= MinCSFI
; --i
) {
1266 unsigned Align
= FFI
->getObjectAlignment(i
);
1267 // If the alignment of this object is greater than that of the stack,
1268 // then increase the stack alignment to match.
1269 MaxAlign
= std::max(MaxAlign
, Align
);
1270 // Adjust to alignment boundary
1271 Offset
= (Offset
+Align
-1)/Align
*Align
;
1273 FFI
->setObjectOffset(i
, Offset
);
1274 Offset
+= FFI
->getObjectSize(i
);
1278 // Make sure the special register scavenging spill slot is closest to the
1279 // frame pointer if a frame pointer is required.
1280 const TargetRegisterInfo
*RegInfo
= Fn
.getTarget().getRegisterInfo();
1281 if (RS
&& RegInfo
->hasFP(Fn
)) {
1282 int SFI
= RS
->getScavengingFrameIndex();
1284 AdjustStackOffset(FFI
, SFI
, StackGrowsDown
, Offset
, MaxAlign
);
1287 // Make sure that the stack protector comes before the local variables on the
1289 if (FFI
->getStackProtectorIndex() >= 0)
1290 AdjustStackOffset(FFI
, FFI
->getStackProtectorIndex(), StackGrowsDown
,
1293 // Then assign frame offsets to stack objects that are not used to spill
1294 // callee saved registers.
1295 for (unsigned i
= 0, e
= FFI
->getObjectIndexEnd(); i
!= e
; ++i
) {
1296 if (i
>= MinCSFrameIndex
&& i
<= MaxCSFrameIndex
)
1298 if (RS
&& (int)i
== RS
->getScavengingFrameIndex())
1300 if (FFI
->isDeadObjectIndex(i
))
1302 if (FFI
->getStackProtectorIndex() == (int)i
)
1305 AdjustStackOffset(FFI
, i
, StackGrowsDown
, Offset
, MaxAlign
);
1308 // Make sure the special register scavenging spill slot is closest to the
1310 if (RS
&& !RegInfo
->hasFP(Fn
)) {
1311 int SFI
= RS
->getScavengingFrameIndex();
1313 AdjustStackOffset(FFI
, SFI
, StackGrowsDown
, Offset
, MaxAlign
);
1316 // Round up the size to a multiple of the alignment, but only if there are
1317 // calls or alloca's in the function. This ensures that any calls to
1318 // subroutines have their stack frames suitable aligned.
1319 // Also do this if we need runtime alignment of the stack. In this case
1320 // offsets will be relative to SP not FP; round up the stack size so this
1322 if (!RegInfo
->targetHandlesStackFrameRounding() &&
1323 (FFI
->hasCalls() || FFI
->hasVarSizedObjects() ||
1324 (RegInfo
->needsStackRealignment(Fn
) &&
1325 FFI
->getObjectIndexEnd() != 0))) {
1326 // If we have reserved argument space for call sites in the function
1327 // immediately on entry to the current function, count it as part of the
1328 // overall stack size.
1329 if (RegInfo
->hasReservedCallFrame(Fn
))
1330 Offset
+= FFI
->getMaxCallFrameSize();
1332 unsigned AlignMask
= std::max(TFI
.getStackAlignment(),MaxAlign
) - 1;
1333 Offset
= (Offset
+ AlignMask
) & ~uint64_t(AlignMask
);
1336 // Update frame info to pretend that this is part of the stack...
1337 FFI
->setStackSize(Offset
+TFI
.getOffsetOfLocalArea());
1339 // Remember the required stack alignment in case targets need it to perform
1340 // dynamic stack alignment.
1341 FFI
->setMaxAlignment(MaxAlign
);
1345 /// insertPrologEpilogCode - Scan the function for modified callee saved
1346 /// registers, insert spill code for these callee saved registers, then add
1347 /// prolog and epilog code to the function.
1349 void PEI::insertPrologEpilogCode(MachineFunction
&Fn
) {
1350 const TargetRegisterInfo
*TRI
= Fn
.getTarget().getRegisterInfo();
1352 // Add prologue to the function...
1353 TRI
->emitPrologue(Fn
);
1355 // Add epilogue to restore the callee-save registers in each exiting block
1356 for (MachineFunction::iterator I
= Fn
.begin(), E
= Fn
.end(); I
!= E
; ++I
) {
1357 // If last instruction is a return instruction, add an epilogue
1358 if (!I
->empty() && I
->back().getDesc().isReturn())
1359 TRI
->emitEpilogue(Fn
, *I
);
1364 /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
1365 /// register references and actual offsets.
1367 void PEI::replaceFrameIndices(MachineFunction
&Fn
) {
1368 if (!Fn
.getFrameInfo()->hasStackObjects()) return; // Nothing to do?
1370 const TargetMachine
&TM
= Fn
.getTarget();
1371 assert(TM
.getRegisterInfo() && "TM::getRegisterInfo() must be implemented!");
1372 const TargetRegisterInfo
&TRI
= *TM
.getRegisterInfo();
1373 const TargetFrameInfo
*TFI
= TM
.getFrameInfo();
1374 bool StackGrowsDown
=
1375 TFI
->getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown
;
1376 int FrameSetupOpcode
= TRI
.getCallFrameSetupOpcode();
1377 int FrameDestroyOpcode
= TRI
.getCallFrameDestroyOpcode();
1379 for (MachineFunction::iterator BB
= Fn
.begin(),
1380 E
= Fn
.end(); BB
!= E
; ++BB
) {
1381 int SPAdj
= 0; // SP offset due to call frame setup / destroy.
1382 if (RS
) RS
->enterBasicBlock(BB
);
1384 for (MachineBasicBlock::iterator I
= BB
->begin(); I
!= BB
->end(); ) {
1385 if (I
->getOpcode() == TargetInstrInfo::DECLARE
) {
1391 if (I
->getOpcode() == FrameSetupOpcode
||
1392 I
->getOpcode() == FrameDestroyOpcode
) {
1393 // Remember how much SP has been adjusted to create the call
1395 int Size
= I
->getOperand(0).getImm();
1397 if ((!StackGrowsDown
&& I
->getOpcode() == FrameSetupOpcode
) ||
1398 (StackGrowsDown
&& I
->getOpcode() == FrameDestroyOpcode
))
1403 MachineBasicBlock::iterator PrevI
= BB
->end();
1404 if (I
!= BB
->begin()) PrevI
= prior(I
);
1405 TRI
.eliminateCallFramePseudoInstr(Fn
, *BB
, I
);
1407 // Visit the instructions created by eliminateCallFramePseudoInstr().
1408 if (PrevI
== BB
->end())
1409 I
= BB
->begin(); // The replaced instr was the first in the block.
1415 MachineInstr
*MI
= I
;
1417 for (unsigned i
= 0, e
= MI
->getNumOperands(); i
!= e
; ++i
)
1418 if (MI
->getOperand(i
).isFI()) {
1419 // Some instructions (e.g. inline asm instructions) can have
1420 // multiple frame indices and/or cause eliminateFrameIndex
1421 // to insert more than one instruction. We need the register
1422 // scavenger to go through all of these instructions so that
1423 // it can update its register information. We keep the
1424 // iterator at the point before insertion so that we can
1425 // revisit them in full.
1426 bool AtBeginning
= (I
== BB
->begin());
1427 if (!AtBeginning
) --I
;
1429 // If this instruction has a FrameIndex operand, we need to
1430 // use that target machine register info object to eliminate
1433 TRI
.eliminateFrameIndex(MI
, SPAdj
, RS
);
1435 // Reset the iterator if we were at the beginning of the BB.
1445 if (DoIncr
&& I
!= BB
->end()) ++I
;
1447 // Update register states.
1448 if (RS
&& MI
) RS
->forward(MI
);
1451 assert(SPAdj
== 0 && "Unbalanced call frame setup / destroy pairs?");