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