Change allowsUnalignedMemoryAccesses to take type argument since some targets
[llvm/avr.git] / lib / CodeGen / PrologEpilogInserter.cpp
blob608e4c5763a5c40fee1531b6fccfa15b626317d8
1 //===-- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function --===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass is responsible for finalizing the functions frame layout, saving
11 // callee saved registers, and for emitting prolog & epilog code for the
12 // function.
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 #include "PrologEpilogInserter.h"
23 #include "llvm/CodeGen/MachineDominators.h"
24 #include "llvm/CodeGen/MachineLoopInfo.h"
25 #include "llvm/CodeGen/MachineInstr.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineModuleInfo.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/Compiler.h"
35 #include "llvm/ADT/STLExtras.h"
36 #include <climits>
38 using namespace llvm;
40 char PEI::ID = 0;
42 static RegisterPass<PEI>
43 X("prologepilog", "Prologue/Epilogue Insertion");
45 /// createPrologEpilogCodeInserter - This function returns a pass that inserts
46 /// prolog and epilog code, and eliminates abstract frame references.
47 ///
48 FunctionPass *llvm::createPrologEpilogCodeInserter() { return new PEI(); }
50 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
51 /// frame indexes with appropriate references.
52 ///
53 bool PEI::runOnMachineFunction(MachineFunction &Fn) {
54 const Function* F = Fn.getFunction();
55 const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
56 RS = TRI->requiresRegisterScavenging(Fn) ? new RegScavenger() : NULL;
58 // Get MachineModuleInfo so that we can track the construction of the
59 // frame.
60 if (MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>())
61 Fn.getFrameInfo()->setMachineModuleInfo(MMI);
63 // Calculate the MaxCallFrameSize and HasCalls variables for the function's
64 // frame information. Also eliminates call frame pseudo instructions.
65 calculateCallsInformation(Fn);
67 // Allow the target machine to make some adjustments to the function
68 // e.g. UsedPhysRegs before calculateCalleeSavedRegisters.
69 TRI->processFunctionBeforeCalleeSavedScan(Fn, RS);
71 // Scan the function for modified callee saved registers and insert spill code
72 // for any callee saved registers that are modified.
73 calculateCalleeSavedRegisters(Fn);
75 // Determine placement of CSR spill/restore code:
76 // - with shrink wrapping, place spills and restores to tightly
77 // enclose regions in the Machine CFG of the function where
78 // they are used. Without shrink wrapping
79 // - default (no shrink wrapping), place all spills in the
80 // entry block, all restores in return blocks.
81 placeCSRSpillsAndRestores(Fn);
83 // Add the code to save and restore the callee saved registers
84 if (!F->hasFnAttr(Attribute::Naked))
85 insertCSRSpillsAndRestores(Fn);
87 // Allow the target machine to make final modifications to the function
88 // before the frame layout is finalized.
89 TRI->processFunctionBeforeFrameFinalized(Fn);
91 // Calculate actual frame offsets for all abstract stack objects...
92 calculateFrameObjectOffsets(Fn);
94 // Add prolog and epilog code to the function. This function is required
95 // to align the stack frame as necessary for any stack variables or
96 // called functions. Because of this, calculateCalleeSavedRegisters
97 // must be called before this function in order to set the HasCalls
98 // and MaxCallFrameSize variables.
99 if (!F->hasFnAttr(Attribute::Naked))
100 insertPrologEpilogCode(Fn);
102 // Replace all MO_FrameIndex operands with physical register references
103 // and actual offsets.
105 replaceFrameIndices(Fn);
107 delete RS;
108 clearAllSets();
109 return true;
112 #if 0
113 void PEI::getAnalysisUsage(AnalysisUsage &AU) const {
114 AU.setPreservesCFG();
115 if (ShrinkWrapping || ShrinkWrapFunc != "") {
116 AU.addRequired<MachineLoopInfo>();
117 AU.addRequired<MachineDominatorTree>();
119 AU.addPreserved<MachineLoopInfo>();
120 AU.addPreserved<MachineDominatorTree>();
121 MachineFunctionPass::getAnalysisUsage(AU);
123 #endif
125 /// calculateCallsInformation - Calculate the MaxCallFrameSize and HasCalls
126 /// variables for the function's frame information and eliminate call frame
127 /// pseudo instructions.
128 void PEI::calculateCallsInformation(MachineFunction &Fn) {
129 const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
131 unsigned MaxCallFrameSize = 0;
132 bool HasCalls = false;
134 // Get the function call frame set-up and tear-down instruction opcode
135 int FrameSetupOpcode = RegInfo->getCallFrameSetupOpcode();
136 int FrameDestroyOpcode = RegInfo->getCallFrameDestroyOpcode();
138 // Early exit for targets which have no call frame setup/destroy pseudo
139 // instructions.
140 if (FrameSetupOpcode == -1 && FrameDestroyOpcode == -1)
141 return;
143 std::vector<MachineBasicBlock::iterator> FrameSDOps;
144 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB)
145 for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
146 if (I->getOpcode() == FrameSetupOpcode ||
147 I->getOpcode() == FrameDestroyOpcode) {
148 assert(I->getNumOperands() >= 1 && "Call Frame Setup/Destroy Pseudo"
149 " instructions should have a single immediate argument!");
150 unsigned Size = I->getOperand(0).getImm();
151 if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
152 HasCalls = true;
153 FrameSDOps.push_back(I);
154 } else if (I->getOpcode() == TargetInstrInfo::INLINEASM) {
155 // An InlineAsm might be a call; assume it is to get the stack frame
156 // aligned correctly for calls.
157 HasCalls = true;
160 MachineFrameInfo *FFI = Fn.getFrameInfo();
161 FFI->setHasCalls(HasCalls);
162 FFI->setMaxCallFrameSize(MaxCallFrameSize);
164 for (std::vector<MachineBasicBlock::iterator>::iterator
165 i = FrameSDOps.begin(), e = FrameSDOps.end(); i != e; ++i) {
166 MachineBasicBlock::iterator I = *i;
168 // If call frames are not being included as part of the stack frame, and
169 // there is no dynamic allocation (therefore referencing frame slots off
170 // sp), leave the pseudo ops alone. We'll eliminate them later.
171 if (RegInfo->hasReservedCallFrame(Fn) || RegInfo->hasFP(Fn))
172 RegInfo->eliminateCallFramePseudoInstr(Fn, *I->getParent(), I);
177 /// calculateCalleeSavedRegisters - Scan the function for modified callee saved
178 /// registers.
179 void PEI::calculateCalleeSavedRegisters(MachineFunction &Fn) {
180 const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
181 const TargetFrameInfo *TFI = Fn.getTarget().getFrameInfo();
182 MachineFrameInfo *FFI = Fn.getFrameInfo();
184 // Get the callee saved register list...
185 const unsigned *CSRegs = RegInfo->getCalleeSavedRegs(&Fn);
187 // These are used to keep track the callee-save area. Initialize them.
188 MinCSFrameIndex = INT_MAX;
189 MaxCSFrameIndex = 0;
191 // Early exit for targets which have no callee saved registers.
192 if (CSRegs == 0 || CSRegs[0] == 0)
193 return;
195 // Figure out which *callee saved* registers are modified by the current
196 // function, thus needing to be saved and restored in the prolog/epilog.
197 const TargetRegisterClass * const *CSRegClasses =
198 RegInfo->getCalleeSavedRegClasses(&Fn);
200 std::vector<CalleeSavedInfo> CSI;
201 for (unsigned i = 0; CSRegs[i]; ++i) {
202 unsigned Reg = CSRegs[i];
203 if (Fn.getRegInfo().isPhysRegUsed(Reg)) {
204 // If the reg is modified, save it!
205 CSI.push_back(CalleeSavedInfo(Reg, CSRegClasses[i]));
206 } else {
207 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
208 *AliasSet; ++AliasSet) { // Check alias registers too.
209 if (Fn.getRegInfo().isPhysRegUsed(*AliasSet)) {
210 CSI.push_back(CalleeSavedInfo(Reg, CSRegClasses[i]));
211 break;
217 if (CSI.empty())
218 return; // Early exit if no callee saved registers are modified!
220 unsigned NumFixedSpillSlots;
221 const std::pair<unsigned,int> *FixedSpillSlots =
222 TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots);
224 // Now that we know which registers need to be saved and restored, allocate
225 // stack slots for them.
226 for (std::vector<CalleeSavedInfo>::iterator
227 I = CSI.begin(), E = CSI.end(); I != E; ++I) {
228 unsigned Reg = I->getReg();
229 const TargetRegisterClass *RC = I->getRegClass();
231 int FrameIdx;
232 if (RegInfo->hasReservedSpillSlot(Fn, Reg, FrameIdx)) {
233 I->setFrameIdx(FrameIdx);
234 continue;
237 // Check to see if this physreg must be spilled to a particular stack slot
238 // on this target.
239 const std::pair<unsigned,int> *FixedSlot = FixedSpillSlots;
240 while (FixedSlot != FixedSpillSlots+NumFixedSpillSlots &&
241 FixedSlot->first != Reg)
242 ++FixedSlot;
244 if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) {
245 // Nope, just spill it anywhere convenient.
246 unsigned Align = RC->getAlignment();
247 unsigned StackAlign = TFI->getStackAlignment();
249 // We may not be able to satisfy the desired alignment specification of
250 // the TargetRegisterClass if the stack alignment is smaller. Use the
251 // min.
252 Align = std::min(Align, StackAlign);
253 FrameIdx = FFI->CreateStackObject(RC->getSize(), Align);
254 if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx;
255 if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx;
256 } else {
257 // Spill it to the stack where we must.
258 FrameIdx = FFI->CreateFixedObject(RC->getSize(), FixedSlot->second);
261 I->setFrameIdx(FrameIdx);
264 FFI->setCalleeSavedInfo(CSI);
267 /// insertCSRSpillsAndRestores - Insert spill and restore code for
268 /// callee saved registers used in the function, handling shrink wrapping.
270 void PEI::insertCSRSpillsAndRestores(MachineFunction &Fn) {
271 // Get callee saved register information.
272 MachineFrameInfo *FFI = Fn.getFrameInfo();
273 const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
275 FFI->setCalleeSavedInfoValid(true);
277 // Early exit if no callee saved registers are modified!
278 if (CSI.empty())
279 return;
281 const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
282 MachineBasicBlock::iterator I;
284 if (! ShrinkWrapThisFunction) {
285 // Spill using target interface.
286 I = EntryBlock->begin();
287 if (!TII.spillCalleeSavedRegisters(*EntryBlock, I, CSI)) {
288 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
289 // Add the callee-saved register as live-in.
290 // It's killed at the spill.
291 EntryBlock->addLiveIn(CSI[i].getReg());
293 // Insert the spill to the stack frame.
294 TII.storeRegToStackSlot(*EntryBlock, I, CSI[i].getReg(), true,
295 CSI[i].getFrameIdx(), CSI[i].getRegClass());
299 // Restore using target interface.
300 for (unsigned ri = 0, re = ReturnBlocks.size(); ri != re; ++ri) {
301 MachineBasicBlock* MBB = ReturnBlocks[ri];
302 I = MBB->end(); --I;
304 // Skip over all terminator instructions, which are part of the return
305 // sequence.
306 MachineBasicBlock::iterator I2 = I;
307 while (I2 != MBB->begin() && (--I2)->getDesc().isTerminator())
308 I = I2;
310 bool AtStart = I == MBB->begin();
311 MachineBasicBlock::iterator BeforeI = I;
312 if (!AtStart)
313 --BeforeI;
315 // Restore all registers immediately before the return and any
316 // terminators that preceed it.
317 if (!TII.restoreCalleeSavedRegisters(*MBB, I, CSI)) {
318 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
319 TII.loadRegFromStackSlot(*MBB, I, CSI[i].getReg(),
320 CSI[i].getFrameIdx(),
321 CSI[i].getRegClass());
322 assert(I != MBB->begin() &&
323 "loadRegFromStackSlot didn't insert any code!");
324 // Insert in reverse order. loadRegFromStackSlot can insert
325 // multiple instructions.
326 if (AtStart)
327 I = MBB->begin();
328 else {
329 I = BeforeI;
330 ++I;
335 return;
338 // Insert spills.
339 std::vector<CalleeSavedInfo> blockCSI;
340 for (CSRegBlockMap::iterator BI = CSRSave.begin(),
341 BE = CSRSave.end(); BI != BE; ++BI) {
342 MachineBasicBlock* MBB = BI->first;
343 CSRegSet save = BI->second;
345 if (save.empty())
346 continue;
348 blockCSI.clear();
349 for (CSRegSet::iterator RI = save.begin(),
350 RE = save.end(); RI != RE; ++RI) {
351 blockCSI.push_back(CSI[*RI]);
353 assert(blockCSI.size() > 0 &&
354 "Could not collect callee saved register info");
356 I = MBB->begin();
358 // When shrink wrapping, use stack slot stores/loads.
359 for (unsigned i = 0, e = blockCSI.size(); i != e; ++i) {
360 // Add the callee-saved register as live-in.
361 // It's killed at the spill.
362 MBB->addLiveIn(blockCSI[i].getReg());
364 // Insert the spill to the stack frame.
365 TII.storeRegToStackSlot(*MBB, I, blockCSI[i].getReg(),
366 true,
367 blockCSI[i].getFrameIdx(),
368 blockCSI[i].getRegClass());
372 for (CSRegBlockMap::iterator BI = CSRRestore.begin(),
373 BE = CSRRestore.end(); BI != BE; ++BI) {
374 MachineBasicBlock* MBB = BI->first;
375 CSRegSet restore = BI->second;
377 if (restore.empty())
378 continue;
380 blockCSI.clear();
381 for (CSRegSet::iterator RI = restore.begin(),
382 RE = restore.end(); RI != RE; ++RI) {
383 blockCSI.push_back(CSI[*RI]);
385 assert(blockCSI.size() > 0 &&
386 "Could not find callee saved register info");
388 // If MBB is empty and needs restores, insert at the _beginning_.
389 if (MBB->empty()) {
390 I = MBB->begin();
391 } else {
392 I = MBB->end();
393 --I;
395 // Skip over all terminator instructions, which are part of the
396 // return sequence.
397 if (! I->getDesc().isTerminator()) {
398 ++I;
399 } else {
400 MachineBasicBlock::iterator I2 = I;
401 while (I2 != MBB->begin() && (--I2)->getDesc().isTerminator())
402 I = I2;
406 bool AtStart = I == MBB->begin();
407 MachineBasicBlock::iterator BeforeI = I;
408 if (!AtStart)
409 --BeforeI;
411 // Restore all registers immediately before the return and any
412 // terminators that preceed it.
413 for (unsigned i = 0, e = blockCSI.size(); i != e; ++i) {
414 TII.loadRegFromStackSlot(*MBB, I, blockCSI[i].getReg(),
415 blockCSI[i].getFrameIdx(),
416 blockCSI[i].getRegClass());
417 assert(I != MBB->begin() &&
418 "loadRegFromStackSlot didn't insert any code!");
419 // Insert in reverse order. loadRegFromStackSlot can insert
420 // multiple instructions.
421 if (AtStart)
422 I = MBB->begin();
423 else {
424 I = BeforeI;
425 ++I;
431 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
432 static inline void
433 AdjustStackOffset(MachineFrameInfo *FFI, int FrameIdx,
434 bool StackGrowsDown, int64_t &Offset,
435 unsigned &MaxAlign) {
436 // If stack grows down, we need to add size of find the lowest address of the
437 // object.
438 if (StackGrowsDown)
439 Offset += FFI->getObjectSize(FrameIdx);
441 unsigned Align = FFI->getObjectAlignment(FrameIdx);
443 // If the alignment of this object is greater than that of the stack, then
444 // increase the stack alignment to match.
445 MaxAlign = std::max(MaxAlign, Align);
447 // Adjust to alignment boundary.
448 Offset = (Offset + Align - 1) / Align * Align;
450 if (StackGrowsDown) {
451 FFI->setObjectOffset(FrameIdx, -Offset); // Set the computed offset
452 } else {
453 FFI->setObjectOffset(FrameIdx, Offset);
454 Offset += FFI->getObjectSize(FrameIdx);
458 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
459 /// abstract stack objects.
461 void PEI::calculateFrameObjectOffsets(MachineFunction &Fn) {
462 const TargetFrameInfo &TFI = *Fn.getTarget().getFrameInfo();
464 bool StackGrowsDown =
465 TFI.getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
467 // Loop over all of the stack objects, assigning sequential addresses...
468 MachineFrameInfo *FFI = Fn.getFrameInfo();
470 unsigned MaxAlign = FFI->getMaxAlignment();
472 // Start at the beginning of the local area.
473 // The Offset is the distance from the stack top in the direction
474 // of stack growth -- so it's always nonnegative.
475 int64_t Offset = TFI.getOffsetOfLocalArea();
476 if (StackGrowsDown)
477 Offset = -Offset;
478 assert(Offset >= 0
479 && "Local area offset should be in direction of stack growth");
481 // If there are fixed sized objects that are preallocated in the local area,
482 // non-fixed objects can't be allocated right at the start of local area.
483 // We currently don't support filling in holes in between fixed sized
484 // objects, so we adjust 'Offset' to point to the end of last fixed sized
485 // preallocated object.
486 for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
487 int64_t FixedOff;
488 if (StackGrowsDown) {
489 // The maximum distance from the stack pointer is at lower address of
490 // the object -- which is given by offset. For down growing stack
491 // the offset is negative, so we negate the offset to get the distance.
492 FixedOff = -FFI->getObjectOffset(i);
493 } else {
494 // The maximum distance from the start pointer is at the upper
495 // address of the object.
496 FixedOff = FFI->getObjectOffset(i) + FFI->getObjectSize(i);
498 if (FixedOff > Offset) Offset = FixedOff;
501 // First assign frame offsets to stack objects that are used to spill
502 // callee saved registers.
503 if (StackGrowsDown) {
504 for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) {
505 // If stack grows down, we need to add size of find the lowest
506 // address of the object.
507 Offset += FFI->getObjectSize(i);
509 unsigned Align = FFI->getObjectAlignment(i);
510 // If the alignment of this object is greater than that of the stack,
511 // then increase the stack alignment to match.
512 MaxAlign = std::max(MaxAlign, Align);
513 // Adjust to alignment boundary
514 Offset = (Offset+Align-1)/Align*Align;
516 FFI->setObjectOffset(i, -Offset); // Set the computed offset
518 } else {
519 int MaxCSFI = MaxCSFrameIndex, MinCSFI = MinCSFrameIndex;
520 for (int i = MaxCSFI; i >= MinCSFI ; --i) {
521 unsigned Align = FFI->getObjectAlignment(i);
522 // If the alignment of this object is greater than that of the stack,
523 // then increase the stack alignment to match.
524 MaxAlign = std::max(MaxAlign, Align);
525 // Adjust to alignment boundary
526 Offset = (Offset+Align-1)/Align*Align;
528 FFI->setObjectOffset(i, Offset);
529 Offset += FFI->getObjectSize(i);
533 // Make sure the special register scavenging spill slot is closest to the
534 // frame pointer if a frame pointer is required.
535 const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
536 if (RS && RegInfo->hasFP(Fn)) {
537 int SFI = RS->getScavengingFrameIndex();
538 if (SFI >= 0)
539 AdjustStackOffset(FFI, SFI, StackGrowsDown, Offset, MaxAlign);
542 // Make sure that the stack protector comes before the local variables on the
543 // stack.
544 if (FFI->getStackProtectorIndex() >= 0)
545 AdjustStackOffset(FFI, FFI->getStackProtectorIndex(), StackGrowsDown,
546 Offset, MaxAlign);
548 // Then assign frame offsets to stack objects that are not used to spill
549 // callee saved registers.
550 for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
551 if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
552 continue;
553 if (RS && (int)i == RS->getScavengingFrameIndex())
554 continue;
555 if (FFI->isDeadObjectIndex(i))
556 continue;
557 if (FFI->getStackProtectorIndex() == (int)i)
558 continue;
560 AdjustStackOffset(FFI, i, StackGrowsDown, Offset, MaxAlign);
563 // Make sure the special register scavenging spill slot is closest to the
564 // stack pointer.
565 if (RS && !RegInfo->hasFP(Fn)) {
566 int SFI = RS->getScavengingFrameIndex();
567 if (SFI >= 0)
568 AdjustStackOffset(FFI, SFI, StackGrowsDown, Offset, MaxAlign);
571 // Round up the size to a multiple of the alignment, but only if there are
572 // calls or alloca's in the function. This ensures that any calls to
573 // subroutines have their stack frames suitable aligned.
574 // Also do this if we need runtime alignment of the stack. In this case
575 // offsets will be relative to SP not FP; round up the stack size so this
576 // works.
577 if (!RegInfo->targetHandlesStackFrameRounding() &&
578 (FFI->hasCalls() || FFI->hasVarSizedObjects() ||
579 (RegInfo->needsStackRealignment(Fn) &&
580 FFI->getObjectIndexEnd() != 0))) {
581 // If we have reserved argument space for call sites in the function
582 // immediately on entry to the current function, count it as part of the
583 // overall stack size.
584 if (RegInfo->hasReservedCallFrame(Fn))
585 Offset += FFI->getMaxCallFrameSize();
587 unsigned AlignMask = std::max(TFI.getStackAlignment(),MaxAlign) - 1;
588 Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
591 // Update frame info to pretend that this is part of the stack...
592 FFI->setStackSize(Offset+TFI.getOffsetOfLocalArea());
594 // Remember the required stack alignment in case targets need it to perform
595 // dynamic stack alignment.
596 FFI->setMaxAlignment(MaxAlign);
600 /// insertPrologEpilogCode - Scan the function for modified callee saved
601 /// registers, insert spill code for these callee saved registers, then add
602 /// prolog and epilog code to the function.
604 void PEI::insertPrologEpilogCode(MachineFunction &Fn) {
605 const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
607 // Add prologue to the function...
608 TRI->emitPrologue(Fn);
610 // Add epilogue to restore the callee-save registers in each exiting block
611 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
612 // If last instruction is a return instruction, add an epilogue
613 if (!I->empty() && I->back().getDesc().isReturn())
614 TRI->emitEpilogue(Fn, *I);
619 /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
620 /// register references and actual offsets.
622 void PEI::replaceFrameIndices(MachineFunction &Fn) {
623 if (!Fn.getFrameInfo()->hasStackObjects()) return; // Nothing to do?
625 const TargetMachine &TM = Fn.getTarget();
626 assert(TM.getRegisterInfo() && "TM::getRegisterInfo() must be implemented!");
627 const TargetRegisterInfo &TRI = *TM.getRegisterInfo();
628 const TargetFrameInfo *TFI = TM.getFrameInfo();
629 bool StackGrowsDown =
630 TFI->getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
631 int FrameSetupOpcode = TRI.getCallFrameSetupOpcode();
632 int FrameDestroyOpcode = TRI.getCallFrameDestroyOpcode();
634 for (MachineFunction::iterator BB = Fn.begin(),
635 E = Fn.end(); BB != E; ++BB) {
636 int SPAdj = 0; // SP offset due to call frame setup / destroy.
637 if (RS) RS->enterBasicBlock(BB);
639 for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
640 if (I->getOpcode() == TargetInstrInfo::DECLARE) {
641 // Ignore it.
642 ++I;
643 continue;
646 if (I->getOpcode() == FrameSetupOpcode ||
647 I->getOpcode() == FrameDestroyOpcode) {
648 // Remember how much SP has been adjusted to create the call
649 // frame.
650 int Size = I->getOperand(0).getImm();
652 if ((!StackGrowsDown && I->getOpcode() == FrameSetupOpcode) ||
653 (StackGrowsDown && I->getOpcode() == FrameDestroyOpcode))
654 Size = -Size;
656 SPAdj += Size;
658 MachineBasicBlock::iterator PrevI = BB->end();
659 if (I != BB->begin()) PrevI = prior(I);
660 TRI.eliminateCallFramePseudoInstr(Fn, *BB, I);
662 // Visit the instructions created by eliminateCallFramePseudoInstr().
663 if (PrevI == BB->end())
664 I = BB->begin(); // The replaced instr was the first in the block.
665 else
666 I = next(PrevI);
667 continue;
670 MachineInstr *MI = I;
671 bool DoIncr = true;
672 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
673 if (MI->getOperand(i).isFI()) {
674 // Some instructions (e.g. inline asm instructions) can have
675 // multiple frame indices and/or cause eliminateFrameIndex
676 // to insert more than one instruction. We need the register
677 // scavenger to go through all of these instructions so that
678 // it can update its register information. We keep the
679 // iterator at the point before insertion so that we can
680 // revisit them in full.
681 bool AtBeginning = (I == BB->begin());
682 if (!AtBeginning) --I;
684 // If this instruction has a FrameIndex operand, we need to
685 // use that target machine register info object to eliminate
686 // it.
688 TRI.eliminateFrameIndex(MI, SPAdj, RS);
690 // Reset the iterator if we were at the beginning of the BB.
691 if (AtBeginning) {
692 I = BB->begin();
693 DoIncr = false;
696 MI = 0;
697 break;
700 if (DoIncr && I != BB->end()) ++I;
702 // Update register states.
703 if (RS && MI) RS->forward(MI);
706 assert(SPAdj == 0 && "Unbalanced call frame setup / destroy pairs?");