1 //===- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function ---===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This pass is responsible for finalizing the functions frame layout, saving 10 // callee saved registers, and for emitting prolog & epilog code for the 11 // function. 12 // 13 // This pass must be run after register allocation. After this pass is 14 // executed, it is illegal to construct MO_FrameIndex operands. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "llvm/ADT/ArrayRef.h" 19 #include "llvm/ADT/BitVector.h" 20 #include "llvm/ADT/STLExtras.h" 21 #include "llvm/ADT/SetVector.h" 22 #include "llvm/ADT/SmallPtrSet.h" 23 #include "llvm/ADT/SmallSet.h" 24 #include "llvm/ADT/SmallVector.h" 25 #include "llvm/ADT/Statistic.h" 26 #include "llvm/Analysis/OptimizationRemarkEmitter.h" 27 #include "llvm/CodeGen/MachineBasicBlock.h" 28 #include "llvm/CodeGen/MachineDominators.h" 29 #include "llvm/CodeGen/MachineFrameInfo.h" 30 #include "llvm/CodeGen/MachineFunction.h" 31 #include "llvm/CodeGen/MachineFunctionPass.h" 32 #include "llvm/CodeGen/MachineInstr.h" 33 #include "llvm/CodeGen/MachineInstrBuilder.h" 34 #include "llvm/CodeGen/MachineLoopInfo.h" 35 #include "llvm/CodeGen/MachineModuleInfo.h" 36 #include "llvm/CodeGen/MachineOperand.h" 37 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h" 38 #include "llvm/CodeGen/MachineRegisterInfo.h" 39 #include "llvm/CodeGen/RegisterScavenging.h" 40 #include "llvm/CodeGen/TargetFrameLowering.h" 41 #include "llvm/CodeGen/TargetInstrInfo.h" 42 #include "llvm/CodeGen/TargetOpcodes.h" 43 #include "llvm/CodeGen/TargetRegisterInfo.h" 44 #include "llvm/CodeGen/TargetSubtargetInfo.h" 45 #include "llvm/CodeGen/WinEHFuncInfo.h" 46 #include "llvm/IR/Attributes.h" 47 #include "llvm/IR/CallingConv.h" 48 #include "llvm/IR/DebugInfoMetadata.h" 49 #include "llvm/IR/DiagnosticInfo.h" 50 #include "llvm/IR/Function.h" 51 #include "llvm/IR/InlineAsm.h" 52 #include "llvm/IR/LLVMContext.h" 53 #include "llvm/InitializePasses.h" 54 #include "llvm/MC/MCRegisterInfo.h" 55 #include "llvm/Pass.h" 56 #include "llvm/Support/CodeGen.h" 57 #include "llvm/Support/Debug.h" 58 #include "llvm/Support/ErrorHandling.h" 59 #include "llvm/Support/FormatVariadic.h" 60 #include "llvm/Support/raw_ostream.h" 61 #include "llvm/Target/TargetMachine.h" 62 #include "llvm/Target/TargetOptions.h" 63 #include <algorithm> 64 #include <cassert> 65 #include <cstdint> 66 #include <functional> 67 #include <limits> 68 #include <utility> 69 #include <vector> 70 71 using namespace llvm; 72 73 #define DEBUG_TYPE "prologepilog" 74 75 using MBBVector = SmallVector<MachineBasicBlock *, 4>; 76 77 STATISTIC(NumLeafFuncWithSpills, "Number of leaf functions with CSRs"); 78 STATISTIC(NumFuncSeen, "Number of functions seen in PEI"); 79 80 81 namespace { 82 83 class PEI : public MachineFunctionPass { 84 public: 85 static char ID; 86 87 PEI() : MachineFunctionPass(ID) { 88 initializePEIPass(*PassRegistry::getPassRegistry()); 89 } 90 91 void getAnalysisUsage(AnalysisUsage &AU) const override; 92 93 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract 94 /// frame indexes with appropriate references. 95 bool runOnMachineFunction(MachineFunction &MF) override; 96 97 private: 98 RegScavenger *RS = nullptr; 99 100 // MinCSFrameIndex, MaxCSFrameIndex - Keeps the range of callee saved 101 // stack frame indexes. 102 unsigned MinCSFrameIndex = std::numeric_limits<unsigned>::max(); 103 unsigned MaxCSFrameIndex = 0; 104 105 // Save and Restore blocks of the current function. Typically there is a 106 // single save block, unless Windows EH funclets are involved. 107 MBBVector SaveBlocks; 108 MBBVector RestoreBlocks; 109 110 // Flag to control whether to use the register scavenger to resolve 111 // frame index materialization registers. Set according to 112 // TRI->requiresFrameIndexScavenging() for the current function. 113 bool FrameIndexVirtualScavenging = false; 114 115 // Flag to control whether the scavenger should be passed even though 116 // FrameIndexVirtualScavenging is used. 117 bool FrameIndexEliminationScavenging = false; 118 119 // Emit remarks. 120 MachineOptimizationRemarkEmitter *ORE = nullptr; 121 122 void calculateCallFrameInfo(MachineFunction &MF); 123 void calculateSaveRestoreBlocks(MachineFunction &MF); 124 void spillCalleeSavedRegs(MachineFunction &MF); 125 126 void calculateFrameObjectOffsets(MachineFunction &MF); 127 void replaceFrameIndices(MachineFunction &MF); 128 void replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF, 129 int &SPAdj); 130 // Frame indices in debug values are encoded in a target independent 131 // way with simply the frame index and offset rather than any 132 // target-specific addressing mode. 133 bool replaceFrameIndexDebugInstr(MachineFunction &MF, MachineInstr &MI, 134 unsigned OpIdx, int SPAdj = 0); 135 // Does same as replaceFrameIndices but using the backward MIR walk and 136 // backward register scavenger walk. 137 void replaceFrameIndicesBackward(MachineFunction &MF); 138 void replaceFrameIndicesBackward(MachineBasicBlock *BB, MachineFunction &MF, 139 int &SPAdj); 140 141 void insertPrologEpilogCode(MachineFunction &MF); 142 void insertZeroCallUsedRegs(MachineFunction &MF); 143 }; 144 145 } // end anonymous namespace 146 147 char PEI::ID = 0; 148 149 char &llvm::PrologEpilogCodeInserterID = PEI::ID; 150 151 INITIALIZE_PASS_BEGIN(PEI, DEBUG_TYPE, "Prologue/Epilogue Insertion", false, 152 false) 153 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) 154 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) 155 INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass) 156 INITIALIZE_PASS_END(PEI, DEBUG_TYPE, 157 "Prologue/Epilogue Insertion & Frame Finalization", false, 158 false) 159 160 MachineFunctionPass *llvm::createPrologEpilogInserterPass() { 161 return new PEI(); 162 } 163 164 STATISTIC(NumBytesStackSpace, 165 "Number of bytes used for stack in all functions"); 166 167 void PEI::getAnalysisUsage(AnalysisUsage &AU) const { 168 AU.setPreservesCFG(); 169 AU.addPreserved<MachineLoopInfo>(); 170 AU.addPreserved<MachineDominatorTree>(); 171 AU.addRequired<MachineOptimizationRemarkEmitterPass>(); 172 MachineFunctionPass::getAnalysisUsage(AU); 173 } 174 175 /// StackObjSet - A set of stack object indexes 176 using StackObjSet = SmallSetVector<int, 8>; 177 178 using SavedDbgValuesMap = 179 SmallDenseMap<MachineBasicBlock *, SmallVector<MachineInstr *, 4>, 4>; 180 181 /// Stash DBG_VALUEs that describe parameters and which are placed at the start 182 /// of the block. Later on, after the prologue code has been emitted, the 183 /// stashed DBG_VALUEs will be reinserted at the start of the block. 184 static void stashEntryDbgValues(MachineBasicBlock &MBB, 185 SavedDbgValuesMap &EntryDbgValues) { 186 SmallVector<const MachineInstr *, 4> FrameIndexValues; 187 188 for (auto &MI : MBB) { 189 if (!MI.isDebugInstr()) 190 break; 191 if (!MI.isDebugValue() || !MI.getDebugVariable()->isParameter()) 192 continue; 193 if (any_of(MI.debug_operands(), 194 [](const MachineOperand &MO) { return MO.isFI(); })) { 195 // We can only emit valid locations for frame indices after the frame 196 // setup, so do not stash away them. 197 FrameIndexValues.push_back(&MI); 198 continue; 199 } 200 const DILocalVariable *Var = MI.getDebugVariable(); 201 const DIExpression *Expr = MI.getDebugExpression(); 202 auto Overlaps = [Var, Expr](const MachineInstr *DV) { 203 return Var == DV->getDebugVariable() && 204 Expr->fragmentsOverlap(DV->getDebugExpression()); 205 }; 206 // See if the debug value overlaps with any preceding debug value that will 207 // not be stashed. If that is the case, then we can't stash this value, as 208 // we would then reorder the values at reinsertion. 209 if (llvm::none_of(FrameIndexValues, Overlaps)) 210 EntryDbgValues[&MBB].push_back(&MI); 211 } 212 213 // Remove stashed debug values from the block. 214 if (EntryDbgValues.count(&MBB)) 215 for (auto *MI : EntryDbgValues[&MBB]) 216 MI->removeFromParent(); 217 } 218 219 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract 220 /// frame indexes with appropriate references. 221 bool PEI::runOnMachineFunction(MachineFunction &MF) { 222 NumFuncSeen++; 223 const Function &F = MF.getFunction(); 224 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 225 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering(); 226 227 RS = TRI->requiresRegisterScavenging(MF) ? new RegScavenger() : nullptr; 228 FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(MF); 229 ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE(); 230 231 // Calculate the MaxCallFrameSize and AdjustsStack variables for the 232 // function's frame information. Also eliminates call frame pseudo 233 // instructions. 234 calculateCallFrameInfo(MF); 235 236 // Determine placement of CSR spill/restore code and prolog/epilog code: 237 // place all spills in the entry block, all restores in return blocks. 238 calculateSaveRestoreBlocks(MF); 239 240 // Stash away DBG_VALUEs that should not be moved by insertion of prolog code. 241 SavedDbgValuesMap EntryDbgValues; 242 for (MachineBasicBlock *SaveBlock : SaveBlocks) 243 stashEntryDbgValues(*SaveBlock, EntryDbgValues); 244 245 // Handle CSR spilling and restoring, for targets that need it. 246 if (MF.getTarget().usesPhysRegsForValues()) 247 spillCalleeSavedRegs(MF); 248 249 // Allow the target machine to make final modifications to the function 250 // before the frame layout is finalized. 251 TFI->processFunctionBeforeFrameFinalized(MF, RS); 252 253 // Calculate actual frame offsets for all abstract stack objects... 254 calculateFrameObjectOffsets(MF); 255 256 // Add prolog and epilog code to the function. This function is required 257 // to align the stack frame as necessary for any stack variables or 258 // called functions. Because of this, calculateCalleeSavedRegisters() 259 // must be called before this function in order to set the AdjustsStack 260 // and MaxCallFrameSize variables. 261 if (!F.hasFnAttribute(Attribute::Naked)) 262 insertPrologEpilogCode(MF); 263 264 // Reinsert stashed debug values at the start of the entry blocks. 265 for (auto &I : EntryDbgValues) 266 I.first->insert(I.first->begin(), I.second.begin(), I.second.end()); 267 268 // Allow the target machine to make final modifications to the function 269 // before the frame layout is finalized. 270 TFI->processFunctionBeforeFrameIndicesReplaced(MF, RS); 271 272 // Replace all MO_FrameIndex operands with physical register references 273 // and actual offsets. 274 if (TFI->needsFrameIndexResolution(MF)) { 275 // Allow the target to determine this after knowing the frame size. 276 FrameIndexEliminationScavenging = 277 (RS && !FrameIndexVirtualScavenging) || 278 TRI->requiresFrameIndexReplacementScavenging(MF); 279 280 if (TRI->supportsBackwardScavenger()) 281 replaceFrameIndicesBackward(MF); 282 else 283 replaceFrameIndices(MF); 284 } 285 286 // If register scavenging is needed, as we've enabled doing it as a 287 // post-pass, scavenge the virtual registers that frame index elimination 288 // inserted. 289 if (TRI->requiresRegisterScavenging(MF) && FrameIndexVirtualScavenging) 290 scavengeFrameVirtualRegs(MF, *RS); 291 292 // Warn on stack size when we exceeds the given limit. 293 MachineFrameInfo &MFI = MF.getFrameInfo(); 294 uint64_t StackSize = MFI.getStackSize(); 295 296 unsigned Threshold = UINT_MAX; 297 if (MF.getFunction().hasFnAttribute("warn-stack-size")) { 298 bool Failed = MF.getFunction() 299 .getFnAttribute("warn-stack-size") 300 .getValueAsString() 301 .getAsInteger(10, Threshold); 302 // Verifier should have caught this. 303 assert(!Failed && "Invalid warn-stack-size fn attr value"); 304 (void)Failed; 305 } 306 uint64_t UnsafeStackSize = MFI.getUnsafeStackSize(); 307 if (MF.getFunction().hasFnAttribute(Attribute::SafeStack)) 308 StackSize += UnsafeStackSize; 309 310 if (StackSize > Threshold) { 311 DiagnosticInfoStackSize DiagStackSize(F, StackSize, Threshold, DS_Warning); 312 F.getContext().diagnose(DiagStackSize); 313 int64_t SpillSize = 0; 314 for (int Idx = MFI.getObjectIndexBegin(), End = MFI.getObjectIndexEnd(); 315 Idx != End; ++Idx) { 316 if (MFI.isSpillSlotObjectIndex(Idx)) 317 SpillSize += MFI.getObjectSize(Idx); 318 } 319 320 [[maybe_unused]] float SpillPct = 321 static_cast<float>(SpillSize) / static_cast<float>(StackSize); 322 LLVM_DEBUG( 323 dbgs() << formatv("{0}/{1} ({3:P}) spills, {2}/{1} ({4:P}) variables", 324 SpillSize, StackSize, StackSize - SpillSize, SpillPct, 325 1.0f - SpillPct)); 326 if (UnsafeStackSize != 0) { 327 LLVM_DEBUG(dbgs() << formatv(", {0}/{2} ({1:P}) unsafe stack", 328 UnsafeStackSize, 329 static_cast<float>(UnsafeStackSize) / 330 static_cast<float>(StackSize), 331 StackSize)); 332 } 333 LLVM_DEBUG(dbgs() << "\n"); 334 } 335 336 ORE->emit([&]() { 337 return MachineOptimizationRemarkAnalysis(DEBUG_TYPE, "StackSize", 338 MF.getFunction().getSubprogram(), 339 &MF.front()) 340 << ore::NV("NumStackBytes", StackSize) << " stack bytes in function"; 341 }); 342 343 delete RS; 344 SaveBlocks.clear(); 345 RestoreBlocks.clear(); 346 MFI.setSavePoint(nullptr); 347 MFI.setRestorePoint(nullptr); 348 return true; 349 } 350 351 /// Calculate the MaxCallFrameSize and AdjustsStack 352 /// variables for the function's frame information and eliminate call frame 353 /// pseudo instructions. 354 void PEI::calculateCallFrameInfo(MachineFunction &MF) { 355 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); 356 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering(); 357 MachineFrameInfo &MFI = MF.getFrameInfo(); 358 359 unsigned MaxCallFrameSize = 0; 360 bool AdjustsStack = MFI.adjustsStack(); 361 362 // Get the function call frame set-up and tear-down instruction opcode 363 unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode(); 364 unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode(); 365 366 // Early exit for targets which have no call frame setup/destroy pseudo 367 // instructions. 368 if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u) 369 return; 370 371 std::vector<MachineBasicBlock::iterator> FrameSDOps; 372 for (MachineBasicBlock &BB : MF) 373 for (MachineBasicBlock::iterator I = BB.begin(); I != BB.end(); ++I) 374 if (TII.isFrameInstr(*I)) { 375 unsigned Size = TII.getFrameSize(*I); 376 if (Size > MaxCallFrameSize) MaxCallFrameSize = Size; 377 AdjustsStack = true; 378 FrameSDOps.push_back(I); 379 } else if (I->isInlineAsm()) { 380 // Some inline asm's need a stack frame, as indicated by operand 1. 381 unsigned ExtraInfo = I->getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 382 if (ExtraInfo & InlineAsm::Extra_IsAlignStack) 383 AdjustsStack = true; 384 } 385 386 assert(!MFI.isMaxCallFrameSizeComputed() || 387 (MFI.getMaxCallFrameSize() >= MaxCallFrameSize && 388 !(AdjustsStack && !MFI.adjustsStack()))); 389 MFI.setAdjustsStack(AdjustsStack); 390 MFI.setMaxCallFrameSize(MaxCallFrameSize); 391 392 if (TFI->canSimplifyCallFramePseudos(MF)) { 393 // If call frames are not being included as part of the stack frame, and 394 // the target doesn't indicate otherwise, remove the call frame pseudos 395 // here. The sub/add sp instruction pairs are still inserted, but we don't 396 // need to track the SP adjustment for frame index elimination. 397 for (MachineBasicBlock::iterator I : FrameSDOps) 398 TFI->eliminateCallFramePseudoInstr(MF, *I->getParent(), I); 399 400 // We can't track the call frame size after call frame pseudos have been 401 // eliminated. Set it to zero everywhere to keep MachineVerifier happy. 402 for (MachineBasicBlock &MBB : MF) 403 MBB.setCallFrameSize(0); 404 } 405 } 406 407 /// Compute the sets of entry and return blocks for saving and restoring 408 /// callee-saved registers, and placing prolog and epilog code. 409 void PEI::calculateSaveRestoreBlocks(MachineFunction &MF) { 410 const MachineFrameInfo &MFI = MF.getFrameInfo(); 411 412 // Even when we do not change any CSR, we still want to insert the 413 // prologue and epilogue of the function. 414 // So set the save points for those. 415 416 // Use the points found by shrink-wrapping, if any. 417 if (MFI.getSavePoint()) { 418 SaveBlocks.push_back(MFI.getSavePoint()); 419 assert(MFI.getRestorePoint() && "Both restore and save must be set"); 420 MachineBasicBlock *RestoreBlock = MFI.getRestorePoint(); 421 // If RestoreBlock does not have any successor and is not a return block 422 // then the end point is unreachable and we do not need to insert any 423 // epilogue. 424 if (!RestoreBlock->succ_empty() || RestoreBlock->isReturnBlock()) 425 RestoreBlocks.push_back(RestoreBlock); 426 return; 427 } 428 429 // Save refs to entry and return blocks. 430 SaveBlocks.push_back(&MF.front()); 431 for (MachineBasicBlock &MBB : MF) { 432 if (MBB.isEHFuncletEntry()) 433 SaveBlocks.push_back(&MBB); 434 if (MBB.isReturnBlock()) 435 RestoreBlocks.push_back(&MBB); 436 } 437 } 438 439 static void assignCalleeSavedSpillSlots(MachineFunction &F, 440 const BitVector &SavedRegs, 441 unsigned &MinCSFrameIndex, 442 unsigned &MaxCSFrameIndex) { 443 if (SavedRegs.empty()) 444 return; 445 446 const TargetRegisterInfo *RegInfo = F.getSubtarget().getRegisterInfo(); 447 const MCPhysReg *CSRegs = F.getRegInfo().getCalleeSavedRegs(); 448 BitVector CSMask(SavedRegs.size()); 449 450 for (unsigned i = 0; CSRegs[i]; ++i) 451 CSMask.set(CSRegs[i]); 452 453 std::vector<CalleeSavedInfo> CSI; 454 for (unsigned i = 0; CSRegs[i]; ++i) { 455 unsigned Reg = CSRegs[i]; 456 if (SavedRegs.test(Reg)) { 457 bool SavedSuper = false; 458 for (const MCPhysReg &SuperReg : RegInfo->superregs(Reg)) { 459 // Some backends set all aliases for some registers as saved, such as 460 // Mips's $fp, so they appear in SavedRegs but not CSRegs. 461 if (SavedRegs.test(SuperReg) && CSMask.test(SuperReg)) { 462 SavedSuper = true; 463 break; 464 } 465 } 466 467 if (!SavedSuper) 468 CSI.push_back(CalleeSavedInfo(Reg)); 469 } 470 } 471 472 const TargetFrameLowering *TFI = F.getSubtarget().getFrameLowering(); 473 MachineFrameInfo &MFI = F.getFrameInfo(); 474 if (!TFI->assignCalleeSavedSpillSlots(F, RegInfo, CSI, MinCSFrameIndex, 475 MaxCSFrameIndex)) { 476 // If target doesn't implement this, use generic code. 477 478 if (CSI.empty()) 479 return; // Early exit if no callee saved registers are modified! 480 481 unsigned NumFixedSpillSlots; 482 const TargetFrameLowering::SpillSlot *FixedSpillSlots = 483 TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots); 484 485 // Now that we know which registers need to be saved and restored, allocate 486 // stack slots for them. 487 for (auto &CS : CSI) { 488 // If the target has spilled this register to another register, we don't 489 // need to allocate a stack slot. 490 if (CS.isSpilledToReg()) 491 continue; 492 493 unsigned Reg = CS.getReg(); 494 const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg); 495 496 int FrameIdx; 497 if (RegInfo->hasReservedSpillSlot(F, Reg, FrameIdx)) { 498 CS.setFrameIdx(FrameIdx); 499 continue; 500 } 501 502 // Check to see if this physreg must be spilled to a particular stack slot 503 // on this target. 504 const TargetFrameLowering::SpillSlot *FixedSlot = FixedSpillSlots; 505 while (FixedSlot != FixedSpillSlots + NumFixedSpillSlots && 506 FixedSlot->Reg != Reg) 507 ++FixedSlot; 508 509 unsigned Size = RegInfo->getSpillSize(*RC); 510 if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) { 511 // Nope, just spill it anywhere convenient. 512 Align Alignment = RegInfo->getSpillAlign(*RC); 513 // We may not be able to satisfy the desired alignment specification of 514 // the TargetRegisterClass if the stack alignment is smaller. Use the 515 // min. 516 Alignment = std::min(Alignment, TFI->getStackAlign()); 517 FrameIdx = MFI.CreateStackObject(Size, Alignment, true); 518 if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx; 519 if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx; 520 } else { 521 // Spill it to the stack where we must. 522 FrameIdx = MFI.CreateFixedSpillStackObject(Size, FixedSlot->Offset); 523 } 524 525 CS.setFrameIdx(FrameIdx); 526 } 527 } 528 529 MFI.setCalleeSavedInfo(CSI); 530 } 531 532 /// Helper function to update the liveness information for the callee-saved 533 /// registers. 534 static void updateLiveness(MachineFunction &MF) { 535 MachineFrameInfo &MFI = MF.getFrameInfo(); 536 // Visited will contain all the basic blocks that are in the region 537 // where the callee saved registers are alive: 538 // - Anything that is not Save or Restore -> LiveThrough. 539 // - Save -> LiveIn. 540 // - Restore -> LiveOut. 541 // The live-out is not attached to the block, so no need to keep 542 // Restore in this set. 543 SmallPtrSet<MachineBasicBlock *, 8> Visited; 544 SmallVector<MachineBasicBlock *, 8> WorkList; 545 MachineBasicBlock *Entry = &MF.front(); 546 MachineBasicBlock *Save = MFI.getSavePoint(); 547 548 if (!Save) 549 Save = Entry; 550 551 if (Entry != Save) { 552 WorkList.push_back(Entry); 553 Visited.insert(Entry); 554 } 555 Visited.insert(Save); 556 557 MachineBasicBlock *Restore = MFI.getRestorePoint(); 558 if (Restore) 559 // By construction Restore cannot be visited, otherwise it 560 // means there exists a path to Restore that does not go 561 // through Save. 562 WorkList.push_back(Restore); 563 564 while (!WorkList.empty()) { 565 const MachineBasicBlock *CurBB = WorkList.pop_back_val(); 566 // By construction, the region that is after the save point is 567 // dominated by the Save and post-dominated by the Restore. 568 if (CurBB == Save && Save != Restore) 569 continue; 570 // Enqueue all the successors not already visited. 571 // Those are by construction either before Save or after Restore. 572 for (MachineBasicBlock *SuccBB : CurBB->successors()) 573 if (Visited.insert(SuccBB).second) 574 WorkList.push_back(SuccBB); 575 } 576 577 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo(); 578 579 MachineRegisterInfo &MRI = MF.getRegInfo(); 580 for (const CalleeSavedInfo &I : CSI) { 581 for (MachineBasicBlock *MBB : Visited) { 582 MCPhysReg Reg = I.getReg(); 583 // Add the callee-saved register as live-in. 584 // It's killed at the spill. 585 if (!MRI.isReserved(Reg) && !MBB->isLiveIn(Reg)) 586 MBB->addLiveIn(Reg); 587 } 588 // If callee-saved register is spilled to another register rather than 589 // spilling to stack, the destination register has to be marked as live for 590 // each MBB between the prologue and epilogue so that it is not clobbered 591 // before it is reloaded in the epilogue. The Visited set contains all 592 // blocks outside of the region delimited by prologue/epilogue. 593 if (I.isSpilledToReg()) { 594 for (MachineBasicBlock &MBB : MF) { 595 if (Visited.count(&MBB)) 596 continue; 597 MCPhysReg DstReg = I.getDstReg(); 598 if (!MBB.isLiveIn(DstReg)) 599 MBB.addLiveIn(DstReg); 600 } 601 } 602 } 603 } 604 605 /// Insert spill code for the callee-saved registers used in the function. 606 static void insertCSRSaves(MachineBasicBlock &SaveBlock, 607 ArrayRef<CalleeSavedInfo> CSI) { 608 MachineFunction &MF = *SaveBlock.getParent(); 609 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); 610 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering(); 611 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 612 613 MachineBasicBlock::iterator I = SaveBlock.begin(); 614 if (!TFI->spillCalleeSavedRegisters(SaveBlock, I, CSI, TRI)) { 615 for (const CalleeSavedInfo &CS : CSI) { 616 // Insert the spill to the stack frame. 617 unsigned Reg = CS.getReg(); 618 619 if (CS.isSpilledToReg()) { 620 BuildMI(SaveBlock, I, DebugLoc(), 621 TII.get(TargetOpcode::COPY), CS.getDstReg()) 622 .addReg(Reg, getKillRegState(true)); 623 } else { 624 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); 625 TII.storeRegToStackSlot(SaveBlock, I, Reg, true, CS.getFrameIdx(), RC, 626 TRI, Register()); 627 } 628 } 629 } 630 } 631 632 /// Insert restore code for the callee-saved registers used in the function. 633 static void insertCSRRestores(MachineBasicBlock &RestoreBlock, 634 std::vector<CalleeSavedInfo> &CSI) { 635 MachineFunction &MF = *RestoreBlock.getParent(); 636 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); 637 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering(); 638 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 639 640 // Restore all registers immediately before the return and any 641 // terminators that precede it. 642 MachineBasicBlock::iterator I = RestoreBlock.getFirstTerminator(); 643 644 if (!TFI->restoreCalleeSavedRegisters(RestoreBlock, I, CSI, TRI)) { 645 for (const CalleeSavedInfo &CI : reverse(CSI)) { 646 unsigned Reg = CI.getReg(); 647 if (CI.isSpilledToReg()) { 648 BuildMI(RestoreBlock, I, DebugLoc(), TII.get(TargetOpcode::COPY), Reg) 649 .addReg(CI.getDstReg(), getKillRegState(true)); 650 } else { 651 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); 652 TII.loadRegFromStackSlot(RestoreBlock, I, Reg, CI.getFrameIdx(), RC, 653 TRI, Register()); 654 assert(I != RestoreBlock.begin() && 655 "loadRegFromStackSlot didn't insert any code!"); 656 // Insert in reverse order. loadRegFromStackSlot can insert 657 // multiple instructions. 658 } 659 } 660 } 661 } 662 663 void PEI::spillCalleeSavedRegs(MachineFunction &MF) { 664 // We can't list this requirement in getRequiredProperties because some 665 // targets (WebAssembly) use virtual registers past this point, and the pass 666 // pipeline is set up without giving the passes a chance to look at the 667 // TargetMachine. 668 // FIXME: Find a way to express this in getRequiredProperties. 669 assert(MF.getProperties().hasProperty( 670 MachineFunctionProperties::Property::NoVRegs)); 671 672 const Function &F = MF.getFunction(); 673 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering(); 674 MachineFrameInfo &MFI = MF.getFrameInfo(); 675 MinCSFrameIndex = std::numeric_limits<unsigned>::max(); 676 MaxCSFrameIndex = 0; 677 678 // Determine which of the registers in the callee save list should be saved. 679 BitVector SavedRegs; 680 TFI->determineCalleeSaves(MF, SavedRegs, RS); 681 682 // Assign stack slots for any callee-saved registers that must be spilled. 683 assignCalleeSavedSpillSlots(MF, SavedRegs, MinCSFrameIndex, MaxCSFrameIndex); 684 685 // Add the code to save and restore the callee saved registers. 686 if (!F.hasFnAttribute(Attribute::Naked)) { 687 MFI.setCalleeSavedInfoValid(true); 688 689 std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo(); 690 if (!CSI.empty()) { 691 if (!MFI.hasCalls()) 692 NumLeafFuncWithSpills++; 693 694 for (MachineBasicBlock *SaveBlock : SaveBlocks) 695 insertCSRSaves(*SaveBlock, CSI); 696 697 // Update the live-in information of all the blocks up to the save point. 698 updateLiveness(MF); 699 700 for (MachineBasicBlock *RestoreBlock : RestoreBlocks) 701 insertCSRRestores(*RestoreBlock, CSI); 702 } 703 } 704 } 705 706 /// AdjustStackOffset - Helper function used to adjust the stack frame offset. 707 static inline void AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx, 708 bool StackGrowsDown, int64_t &Offset, 709 Align &MaxAlign) { 710 // If the stack grows down, add the object size to find the lowest address. 711 if (StackGrowsDown) 712 Offset += MFI.getObjectSize(FrameIdx); 713 714 Align Alignment = MFI.getObjectAlign(FrameIdx); 715 716 // If the alignment of this object is greater than that of the stack, then 717 // increase the stack alignment to match. 718 MaxAlign = std::max(MaxAlign, Alignment); 719 720 // Adjust to alignment boundary. 721 Offset = alignTo(Offset, Alignment); 722 723 if (StackGrowsDown) { 724 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset 725 << "]\n"); 726 MFI.setObjectOffset(FrameIdx, -Offset); // Set the computed offset 727 } else { 728 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset 729 << "]\n"); 730 MFI.setObjectOffset(FrameIdx, Offset); 731 Offset += MFI.getObjectSize(FrameIdx); 732 } 733 } 734 735 /// Compute which bytes of fixed and callee-save stack area are unused and keep 736 /// track of them in StackBytesFree. 737 static inline void 738 computeFreeStackSlots(MachineFrameInfo &MFI, bool StackGrowsDown, 739 unsigned MinCSFrameIndex, unsigned MaxCSFrameIndex, 740 int64_t FixedCSEnd, BitVector &StackBytesFree) { 741 // Avoid undefined int64_t -> int conversion below in extreme case. 742 if (FixedCSEnd > std::numeric_limits<int>::max()) 743 return; 744 745 StackBytesFree.resize(FixedCSEnd, true); 746 747 SmallVector<int, 16> AllocatedFrameSlots; 748 // Add fixed objects. 749 for (int i = MFI.getObjectIndexBegin(); i != 0; ++i) 750 // StackSlot scavenging is only implemented for the default stack. 751 if (MFI.getStackID(i) == TargetStackID::Default) 752 AllocatedFrameSlots.push_back(i); 753 // Add callee-save objects if there are any. 754 if (MinCSFrameIndex <= MaxCSFrameIndex) { 755 for (int i = MinCSFrameIndex; i <= (int)MaxCSFrameIndex; ++i) 756 if (MFI.getStackID(i) == TargetStackID::Default) 757 AllocatedFrameSlots.push_back(i); 758 } 759 760 for (int i : AllocatedFrameSlots) { 761 // These are converted from int64_t, but they should always fit in int 762 // because of the FixedCSEnd check above. 763 int ObjOffset = MFI.getObjectOffset(i); 764 int ObjSize = MFI.getObjectSize(i); 765 int ObjStart, ObjEnd; 766 if (StackGrowsDown) { 767 // ObjOffset is negative when StackGrowsDown is true. 768 ObjStart = -ObjOffset - ObjSize; 769 ObjEnd = -ObjOffset; 770 } else { 771 ObjStart = ObjOffset; 772 ObjEnd = ObjOffset + ObjSize; 773 } 774 // Ignore fixed holes that are in the previous stack frame. 775 if (ObjEnd > 0) 776 StackBytesFree.reset(ObjStart, ObjEnd); 777 } 778 } 779 780 /// Assign frame object to an unused portion of the stack in the fixed stack 781 /// object range. Return true if the allocation was successful. 782 static inline bool scavengeStackSlot(MachineFrameInfo &MFI, int FrameIdx, 783 bool StackGrowsDown, Align MaxAlign, 784 BitVector &StackBytesFree) { 785 if (MFI.isVariableSizedObjectIndex(FrameIdx)) 786 return false; 787 788 if (StackBytesFree.none()) { 789 // clear it to speed up later scavengeStackSlot calls to 790 // StackBytesFree.none() 791 StackBytesFree.clear(); 792 return false; 793 } 794 795 Align ObjAlign = MFI.getObjectAlign(FrameIdx); 796 if (ObjAlign > MaxAlign) 797 return false; 798 799 int64_t ObjSize = MFI.getObjectSize(FrameIdx); 800 int FreeStart; 801 for (FreeStart = StackBytesFree.find_first(); FreeStart != -1; 802 FreeStart = StackBytesFree.find_next(FreeStart)) { 803 804 // Check that free space has suitable alignment. 805 unsigned ObjStart = StackGrowsDown ? FreeStart + ObjSize : FreeStart; 806 if (alignTo(ObjStart, ObjAlign) != ObjStart) 807 continue; 808 809 if (FreeStart + ObjSize > StackBytesFree.size()) 810 return false; 811 812 bool AllBytesFree = true; 813 for (unsigned Byte = 0; Byte < ObjSize; ++Byte) 814 if (!StackBytesFree.test(FreeStart + Byte)) { 815 AllBytesFree = false; 816 break; 817 } 818 if (AllBytesFree) 819 break; 820 } 821 822 if (FreeStart == -1) 823 return false; 824 825 if (StackGrowsDown) { 826 int ObjStart = -(FreeStart + ObjSize); 827 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP[" 828 << ObjStart << "]\n"); 829 MFI.setObjectOffset(FrameIdx, ObjStart); 830 } else { 831 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP[" 832 << FreeStart << "]\n"); 833 MFI.setObjectOffset(FrameIdx, FreeStart); 834 } 835 836 StackBytesFree.reset(FreeStart, FreeStart + ObjSize); 837 return true; 838 } 839 840 /// AssignProtectedObjSet - Helper function to assign large stack objects (i.e., 841 /// those required to be close to the Stack Protector) to stack offsets. 842 static void AssignProtectedObjSet(const StackObjSet &UnassignedObjs, 843 SmallSet<int, 16> &ProtectedObjs, 844 MachineFrameInfo &MFI, bool StackGrowsDown, 845 int64_t &Offset, Align &MaxAlign) { 846 847 for (int i : UnassignedObjs) { 848 AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign); 849 ProtectedObjs.insert(i); 850 } 851 } 852 853 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the 854 /// abstract stack objects. 855 void PEI::calculateFrameObjectOffsets(MachineFunction &MF) { 856 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering(); 857 858 bool StackGrowsDown = 859 TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown; 860 861 // Loop over all of the stack objects, assigning sequential addresses... 862 MachineFrameInfo &MFI = MF.getFrameInfo(); 863 864 // Start at the beginning of the local area. 865 // The Offset is the distance from the stack top in the direction 866 // of stack growth -- so it's always nonnegative. 867 int LocalAreaOffset = TFI.getOffsetOfLocalArea(); 868 if (StackGrowsDown) 869 LocalAreaOffset = -LocalAreaOffset; 870 assert(LocalAreaOffset >= 0 871 && "Local area offset should be in direction of stack growth"); 872 int64_t Offset = LocalAreaOffset; 873 874 #ifdef EXPENSIVE_CHECKS 875 for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) 876 if (!MFI.isDeadObjectIndex(i) && 877 MFI.getStackID(i) == TargetStackID::Default) 878 assert(MFI.getObjectAlign(i) <= MFI.getMaxAlign() && 879 "MaxAlignment is invalid"); 880 #endif 881 882 // If there are fixed sized objects that are preallocated in the local area, 883 // non-fixed objects can't be allocated right at the start of local area. 884 // Adjust 'Offset' to point to the end of last fixed sized preallocated 885 // object. 886 for (int i = MFI.getObjectIndexBegin(); i != 0; ++i) { 887 // Only allocate objects on the default stack. 888 if (MFI.getStackID(i) != TargetStackID::Default) 889 continue; 890 891 int64_t FixedOff; 892 if (StackGrowsDown) { 893 // The maximum distance from the stack pointer is at lower address of 894 // the object -- which is given by offset. For down growing stack 895 // the offset is negative, so we negate the offset to get the distance. 896 FixedOff = -MFI.getObjectOffset(i); 897 } else { 898 // The maximum distance from the start pointer is at the upper 899 // address of the object. 900 FixedOff = MFI.getObjectOffset(i) + MFI.getObjectSize(i); 901 } 902 if (FixedOff > Offset) Offset = FixedOff; 903 } 904 905 Align MaxAlign = MFI.getMaxAlign(); 906 // First assign frame offsets to stack objects that are used to spill 907 // callee saved registers. 908 if (MaxCSFrameIndex >= MinCSFrameIndex) { 909 for (unsigned i = 0; i <= MaxCSFrameIndex - MinCSFrameIndex; ++i) { 910 unsigned FrameIndex = 911 StackGrowsDown ? MinCSFrameIndex + i : MaxCSFrameIndex - i; 912 913 // Only allocate objects on the default stack. 914 if (MFI.getStackID(FrameIndex) != TargetStackID::Default) 915 continue; 916 917 // TODO: should this just be if (MFI.isDeadObjectIndex(FrameIndex)) 918 if (!StackGrowsDown && MFI.isDeadObjectIndex(FrameIndex)) 919 continue; 920 921 AdjustStackOffset(MFI, FrameIndex, StackGrowsDown, Offset, MaxAlign); 922 } 923 } 924 925 assert(MaxAlign == MFI.getMaxAlign() && 926 "MFI.getMaxAlign should already account for all callee-saved " 927 "registers without a fixed stack slot"); 928 929 // FixedCSEnd is the stack offset to the end of the fixed and callee-save 930 // stack area. 931 int64_t FixedCSEnd = Offset; 932 933 // Make sure the special register scavenging spill slot is closest to the 934 // incoming stack pointer if a frame pointer is required and is closer 935 // to the incoming rather than the final stack pointer. 936 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo(); 937 bool EarlyScavengingSlots = TFI.allocateScavengingFrameIndexesNearIncomingSP(MF); 938 if (RS && EarlyScavengingSlots) { 939 SmallVector<int, 2> SFIs; 940 RS->getScavengingFrameIndices(SFIs); 941 for (int SFI : SFIs) 942 AdjustStackOffset(MFI, SFI, StackGrowsDown, Offset, MaxAlign); 943 } 944 945 // FIXME: Once this is working, then enable flag will change to a target 946 // check for whether the frame is large enough to want to use virtual 947 // frame index registers. Functions which don't want/need this optimization 948 // will continue to use the existing code path. 949 if (MFI.getUseLocalStackAllocationBlock()) { 950 Align Alignment = MFI.getLocalFrameMaxAlign(); 951 952 // Adjust to alignment boundary. 953 Offset = alignTo(Offset, Alignment); 954 955 LLVM_DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n"); 956 957 // Resolve offsets for objects in the local block. 958 for (unsigned i = 0, e = MFI.getLocalFrameObjectCount(); i != e; ++i) { 959 std::pair<int, int64_t> Entry = MFI.getLocalFrameObjectMap(i); 960 int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second; 961 LLVM_DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" << FIOffset 962 << "]\n"); 963 MFI.setObjectOffset(Entry.first, FIOffset); 964 } 965 // Allocate the local block 966 Offset += MFI.getLocalFrameSize(); 967 968 MaxAlign = std::max(Alignment, MaxAlign); 969 } 970 971 // Retrieve the Exception Handler registration node. 972 int EHRegNodeFrameIndex = std::numeric_limits<int>::max(); 973 if (const WinEHFuncInfo *FuncInfo = MF.getWinEHFuncInfo()) 974 EHRegNodeFrameIndex = FuncInfo->EHRegNodeFrameIndex; 975 976 // Make sure that the stack protector comes before the local variables on the 977 // stack. 978 SmallSet<int, 16> ProtectedObjs; 979 if (MFI.hasStackProtectorIndex()) { 980 int StackProtectorFI = MFI.getStackProtectorIndex(); 981 StackObjSet LargeArrayObjs; 982 StackObjSet SmallArrayObjs; 983 StackObjSet AddrOfObjs; 984 985 // If we need a stack protector, we need to make sure that 986 // LocalStackSlotPass didn't already allocate a slot for it. 987 // If we are told to use the LocalStackAllocationBlock, the stack protector 988 // is expected to be already pre-allocated. 989 if (MFI.getStackID(StackProtectorFI) != TargetStackID::Default) { 990 // If the stack protector isn't on the default stack then it's up to the 991 // target to set the stack offset. 992 assert(MFI.getObjectOffset(StackProtectorFI) != 0 && 993 "Offset of stack protector on non-default stack expected to be " 994 "already set."); 995 assert(!MFI.isObjectPreAllocated(MFI.getStackProtectorIndex()) && 996 "Stack protector on non-default stack expected to not be " 997 "pre-allocated by LocalStackSlotPass."); 998 } else if (!MFI.getUseLocalStackAllocationBlock()) { 999 AdjustStackOffset(MFI, StackProtectorFI, StackGrowsDown, Offset, 1000 MaxAlign); 1001 } else if (!MFI.isObjectPreAllocated(MFI.getStackProtectorIndex())) { 1002 llvm_unreachable( 1003 "Stack protector not pre-allocated by LocalStackSlotPass."); 1004 } 1005 1006 // Assign large stack objects first. 1007 for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) { 1008 if (MFI.isObjectPreAllocated(i) && MFI.getUseLocalStackAllocationBlock()) 1009 continue; 1010 if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex) 1011 continue; 1012 if (RS && RS->isScavengingFrameIndex((int)i)) 1013 continue; 1014 if (MFI.isDeadObjectIndex(i)) 1015 continue; 1016 if (StackProtectorFI == (int)i || EHRegNodeFrameIndex == (int)i) 1017 continue; 1018 // Only allocate objects on the default stack. 1019 if (MFI.getStackID(i) != TargetStackID::Default) 1020 continue; 1021 1022 switch (MFI.getObjectSSPLayout(i)) { 1023 case MachineFrameInfo::SSPLK_None: 1024 continue; 1025 case MachineFrameInfo::SSPLK_SmallArray: 1026 SmallArrayObjs.insert(i); 1027 continue; 1028 case MachineFrameInfo::SSPLK_AddrOf: 1029 AddrOfObjs.insert(i); 1030 continue; 1031 case MachineFrameInfo::SSPLK_LargeArray: 1032 LargeArrayObjs.insert(i); 1033 continue; 1034 } 1035 llvm_unreachable("Unexpected SSPLayoutKind."); 1036 } 1037 1038 // We expect **all** the protected stack objects to be pre-allocated by 1039 // LocalStackSlotPass. If it turns out that PEI still has to allocate some 1040 // of them, we may end up messing up the expected order of the objects. 1041 if (MFI.getUseLocalStackAllocationBlock() && 1042 !(LargeArrayObjs.empty() && SmallArrayObjs.empty() && 1043 AddrOfObjs.empty())) 1044 llvm_unreachable("Found protected stack objects not pre-allocated by " 1045 "LocalStackSlotPass."); 1046 1047 AssignProtectedObjSet(LargeArrayObjs, ProtectedObjs, MFI, StackGrowsDown, 1048 Offset, MaxAlign); 1049 AssignProtectedObjSet(SmallArrayObjs, ProtectedObjs, MFI, StackGrowsDown, 1050 Offset, MaxAlign); 1051 AssignProtectedObjSet(AddrOfObjs, ProtectedObjs, MFI, StackGrowsDown, 1052 Offset, MaxAlign); 1053 } 1054 1055 SmallVector<int, 8> ObjectsToAllocate; 1056 1057 // Then prepare to assign frame offsets to stack objects that are not used to 1058 // spill callee saved registers. 1059 for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) { 1060 if (MFI.isObjectPreAllocated(i) && MFI.getUseLocalStackAllocationBlock()) 1061 continue; 1062 if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex) 1063 continue; 1064 if (RS && RS->isScavengingFrameIndex((int)i)) 1065 continue; 1066 if (MFI.isDeadObjectIndex(i)) 1067 continue; 1068 if (MFI.getStackProtectorIndex() == (int)i || EHRegNodeFrameIndex == (int)i) 1069 continue; 1070 if (ProtectedObjs.count(i)) 1071 continue; 1072 // Only allocate objects on the default stack. 1073 if (MFI.getStackID(i) != TargetStackID::Default) 1074 continue; 1075 1076 // Add the objects that we need to allocate to our working set. 1077 ObjectsToAllocate.push_back(i); 1078 } 1079 1080 // Allocate the EH registration node first if one is present. 1081 if (EHRegNodeFrameIndex != std::numeric_limits<int>::max()) 1082 AdjustStackOffset(MFI, EHRegNodeFrameIndex, StackGrowsDown, Offset, 1083 MaxAlign); 1084 1085 // Give the targets a chance to order the objects the way they like it. 1086 if (MF.getTarget().getOptLevel() != CodeGenOpt::None && 1087 MF.getTarget().Options.StackSymbolOrdering) 1088 TFI.orderFrameObjects(MF, ObjectsToAllocate); 1089 1090 // Keep track of which bytes in the fixed and callee-save range are used so we 1091 // can use the holes when allocating later stack objects. Only do this if 1092 // stack protector isn't being used and the target requests it and we're 1093 // optimizing. 1094 BitVector StackBytesFree; 1095 if (!ObjectsToAllocate.empty() && 1096 MF.getTarget().getOptLevel() != CodeGenOpt::None && 1097 MFI.getStackProtectorIndex() < 0 && TFI.enableStackSlotScavenging(MF)) 1098 computeFreeStackSlots(MFI, StackGrowsDown, MinCSFrameIndex, MaxCSFrameIndex, 1099 FixedCSEnd, StackBytesFree); 1100 1101 // Now walk the objects and actually assign base offsets to them. 1102 for (auto &Object : ObjectsToAllocate) 1103 if (!scavengeStackSlot(MFI, Object, StackGrowsDown, MaxAlign, 1104 StackBytesFree)) 1105 AdjustStackOffset(MFI, Object, StackGrowsDown, Offset, MaxAlign); 1106 1107 // Make sure the special register scavenging spill slot is closest to the 1108 // stack pointer. 1109 if (RS && !EarlyScavengingSlots) { 1110 SmallVector<int, 2> SFIs; 1111 RS->getScavengingFrameIndices(SFIs); 1112 for (int SFI : SFIs) 1113 AdjustStackOffset(MFI, SFI, StackGrowsDown, Offset, MaxAlign); 1114 } 1115 1116 if (!TFI.targetHandlesStackFrameRounding()) { 1117 // If we have reserved argument space for call sites in the function 1118 // immediately on entry to the current function, count it as part of the 1119 // overall stack size. 1120 if (MFI.adjustsStack() && TFI.hasReservedCallFrame(MF)) 1121 Offset += MFI.getMaxCallFrameSize(); 1122 1123 // Round up the size to a multiple of the alignment. If the function has 1124 // any calls or alloca's, align to the target's StackAlignment value to 1125 // ensure that the callee's frame or the alloca data is suitably aligned; 1126 // otherwise, for leaf functions, align to the TransientStackAlignment 1127 // value. 1128 Align StackAlign; 1129 if (MFI.adjustsStack() || MFI.hasVarSizedObjects() || 1130 (RegInfo->hasStackRealignment(MF) && MFI.getObjectIndexEnd() != 0)) 1131 StackAlign = TFI.getStackAlign(); 1132 else 1133 StackAlign = TFI.getTransientStackAlign(); 1134 1135 // If the frame pointer is eliminated, all frame offsets will be relative to 1136 // SP not FP. Align to MaxAlign so this works. 1137 StackAlign = std::max(StackAlign, MaxAlign); 1138 int64_t OffsetBeforeAlignment = Offset; 1139 Offset = alignTo(Offset, StackAlign); 1140 1141 // If we have increased the offset to fulfill the alignment constrants, 1142 // then the scavenging spill slots may become harder to reach from the 1143 // stack pointer, float them so they stay close. 1144 if (StackGrowsDown && OffsetBeforeAlignment != Offset && RS && 1145 !EarlyScavengingSlots) { 1146 SmallVector<int, 2> SFIs; 1147 RS->getScavengingFrameIndices(SFIs); 1148 LLVM_DEBUG(if (!SFIs.empty()) llvm::dbgs() 1149 << "Adjusting emergency spill slots!\n";); 1150 int64_t Delta = Offset - OffsetBeforeAlignment; 1151 for (int SFI : SFIs) { 1152 LLVM_DEBUG(llvm::dbgs() 1153 << "Adjusting offset of emergency spill slot #" << SFI 1154 << " from " << MFI.getObjectOffset(SFI);); 1155 MFI.setObjectOffset(SFI, MFI.getObjectOffset(SFI) - Delta); 1156 LLVM_DEBUG(llvm::dbgs() << " to " << MFI.getObjectOffset(SFI) << "\n";); 1157 } 1158 } 1159 } 1160 1161 // Update frame info to pretend that this is part of the stack... 1162 int64_t StackSize = Offset - LocalAreaOffset; 1163 MFI.setStackSize(StackSize); 1164 NumBytesStackSpace += StackSize; 1165 } 1166 1167 /// insertPrologEpilogCode - Scan the function for modified callee saved 1168 /// registers, insert spill code for these callee saved registers, then add 1169 /// prolog and epilog code to the function. 1170 void PEI::insertPrologEpilogCode(MachineFunction &MF) { 1171 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering(); 1172 1173 // Add prologue to the function... 1174 for (MachineBasicBlock *SaveBlock : SaveBlocks) 1175 TFI.emitPrologue(MF, *SaveBlock); 1176 1177 // Add epilogue to restore the callee-save registers in each exiting block. 1178 for (MachineBasicBlock *RestoreBlock : RestoreBlocks) 1179 TFI.emitEpilogue(MF, *RestoreBlock); 1180 1181 // Zero call used registers before restoring callee-saved registers. 1182 insertZeroCallUsedRegs(MF); 1183 1184 for (MachineBasicBlock *SaveBlock : SaveBlocks) 1185 TFI.inlineStackProbe(MF, *SaveBlock); 1186 1187 // Emit additional code that is required to support segmented stacks, if 1188 // we've been asked for it. This, when linked with a runtime with support 1189 // for segmented stacks (libgcc is one), will result in allocating stack 1190 // space in small chunks instead of one large contiguous block. 1191 if (MF.shouldSplitStack()) { 1192 for (MachineBasicBlock *SaveBlock : SaveBlocks) 1193 TFI.adjustForSegmentedStacks(MF, *SaveBlock); 1194 } 1195 1196 // Emit additional code that is required to explicitly handle the stack in 1197 // HiPE native code (if needed) when loaded in the Erlang/OTP runtime. The 1198 // approach is rather similar to that of Segmented Stacks, but it uses a 1199 // different conditional check and another BIF for allocating more stack 1200 // space. 1201 if (MF.getFunction().getCallingConv() == CallingConv::HiPE) 1202 for (MachineBasicBlock *SaveBlock : SaveBlocks) 1203 TFI.adjustForHiPEPrologue(MF, *SaveBlock); 1204 } 1205 1206 /// insertZeroCallUsedRegs - Zero out call used registers. 1207 void PEI::insertZeroCallUsedRegs(MachineFunction &MF) { 1208 const Function &F = MF.getFunction(); 1209 1210 if (!F.hasFnAttribute("zero-call-used-regs")) 1211 return; 1212 1213 using namespace ZeroCallUsedRegs; 1214 1215 ZeroCallUsedRegsKind ZeroRegsKind = 1216 StringSwitch<ZeroCallUsedRegsKind>( 1217 F.getFnAttribute("zero-call-used-regs").getValueAsString()) 1218 .Case("skip", ZeroCallUsedRegsKind::Skip) 1219 .Case("used-gpr-arg", ZeroCallUsedRegsKind::UsedGPRArg) 1220 .Case("used-gpr", ZeroCallUsedRegsKind::UsedGPR) 1221 .Case("used-arg", ZeroCallUsedRegsKind::UsedArg) 1222 .Case("used", ZeroCallUsedRegsKind::Used) 1223 .Case("all-gpr-arg", ZeroCallUsedRegsKind::AllGPRArg) 1224 .Case("all-gpr", ZeroCallUsedRegsKind::AllGPR) 1225 .Case("all-arg", ZeroCallUsedRegsKind::AllArg) 1226 .Case("all", ZeroCallUsedRegsKind::All); 1227 1228 if (ZeroRegsKind == ZeroCallUsedRegsKind::Skip) 1229 return; 1230 1231 const bool OnlyGPR = static_cast<unsigned>(ZeroRegsKind) & ONLY_GPR; 1232 const bool OnlyUsed = static_cast<unsigned>(ZeroRegsKind) & ONLY_USED; 1233 const bool OnlyArg = static_cast<unsigned>(ZeroRegsKind) & ONLY_ARG; 1234 1235 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 1236 const BitVector AllocatableSet(TRI.getAllocatableSet(MF)); 1237 1238 // Mark all used registers. 1239 BitVector UsedRegs(TRI.getNumRegs()); 1240 if (OnlyUsed) 1241 for (const MachineBasicBlock &MBB : MF) 1242 for (const MachineInstr &MI : MBB) { 1243 // skip debug instructions 1244 if (MI.isDebugInstr()) 1245 continue; 1246 1247 for (const MachineOperand &MO : MI.operands()) { 1248 if (!MO.isReg()) 1249 continue; 1250 1251 MCRegister Reg = MO.getReg(); 1252 if (AllocatableSet[Reg] && !MO.isImplicit() && 1253 (MO.isDef() || MO.isUse())) 1254 UsedRegs.set(Reg); 1255 } 1256 } 1257 1258 // Get a list of registers that are used. 1259 BitVector LiveIns(TRI.getNumRegs()); 1260 for (const MachineBasicBlock::RegisterMaskPair &LI : MF.front().liveins()) 1261 LiveIns.set(LI.PhysReg); 1262 1263 BitVector RegsToZero(TRI.getNumRegs()); 1264 for (MCRegister Reg : AllocatableSet.set_bits()) { 1265 // Skip over fixed registers. 1266 if (TRI.isFixedRegister(MF, Reg)) 1267 continue; 1268 1269 // Want only general purpose registers. 1270 if (OnlyGPR && !TRI.isGeneralPurposeRegister(MF, Reg)) 1271 continue; 1272 1273 // Want only used registers. 1274 if (OnlyUsed && !UsedRegs[Reg]) 1275 continue; 1276 1277 // Want only registers used for arguments. 1278 if (OnlyArg) { 1279 if (OnlyUsed) { 1280 if (!LiveIns[Reg]) 1281 continue; 1282 } else if (!TRI.isArgumentRegister(MF, Reg)) { 1283 continue; 1284 } 1285 } 1286 1287 RegsToZero.set(Reg); 1288 } 1289 1290 // Don't clear registers that are live when leaving the function. 1291 for (const MachineBasicBlock &MBB : MF) 1292 for (const MachineInstr &MI : MBB.terminators()) { 1293 if (!MI.isReturn()) 1294 continue; 1295 1296 for (const auto &MO : MI.operands()) { 1297 if (!MO.isReg()) 1298 continue; 1299 1300 MCRegister Reg = MO.getReg(); 1301 if (!Reg) 1302 continue; 1303 1304 // This picks up sibling registers (e.q. %al -> %ah). 1305 for (MCRegUnit Unit : TRI.regunits(Reg)) 1306 RegsToZero.reset(Unit); 1307 1308 for (MCPhysReg SReg : TRI.sub_and_superregs_inclusive(Reg)) 1309 RegsToZero.reset(SReg); 1310 } 1311 } 1312 1313 // Don't need to clear registers that are used/clobbered by terminating 1314 // instructions. 1315 for (const MachineBasicBlock &MBB : MF) { 1316 if (!MBB.isReturnBlock()) 1317 continue; 1318 1319 MachineBasicBlock::const_iterator MBBI = MBB.getFirstTerminator(); 1320 for (MachineBasicBlock::const_iterator I = MBBI, E = MBB.end(); I != E; 1321 ++I) { 1322 for (const MachineOperand &MO : I->operands()) { 1323 if (!MO.isReg()) 1324 continue; 1325 1326 MCRegister Reg = MO.getReg(); 1327 if (!Reg) 1328 continue; 1329 1330 for (const MCPhysReg Reg : TRI.sub_and_superregs_inclusive(Reg)) 1331 RegsToZero.reset(Reg); 1332 } 1333 } 1334 } 1335 1336 // Don't clear registers that must be preserved. 1337 for (const MCPhysReg *CSRegs = TRI.getCalleeSavedRegs(&MF); 1338 MCPhysReg CSReg = *CSRegs; ++CSRegs) 1339 for (MCRegister Reg : TRI.sub_and_superregs_inclusive(CSReg)) 1340 RegsToZero.reset(Reg); 1341 1342 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering(); 1343 for (MachineBasicBlock &MBB : MF) 1344 if (MBB.isReturnBlock()) 1345 TFI.emitZeroCallUsedRegs(RegsToZero, MBB); 1346 } 1347 1348 /// Replace all FrameIndex operands with physical register references and actual 1349 /// offsets. 1350 void PEI::replaceFrameIndicesBackward(MachineFunction &MF) { 1351 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering(); 1352 1353 for (auto &MBB : MF) { 1354 int SPAdj = 0; 1355 if (!MBB.succ_empty()) { 1356 // Get the SP adjustment for the end of MBB from the start of any of its 1357 // successors. They should all be the same. 1358 assert(all_of(MBB.successors(), [&MBB](const MachineBasicBlock *Succ) { 1359 return Succ->getCallFrameSize() == 1360 (*MBB.succ_begin())->getCallFrameSize(); 1361 })); 1362 const MachineBasicBlock &FirstSucc = **MBB.succ_begin(); 1363 SPAdj = TFI.alignSPAdjust(FirstSucc.getCallFrameSize()); 1364 if (TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsUp) 1365 SPAdj = -SPAdj; 1366 } 1367 1368 replaceFrameIndicesBackward(&MBB, MF, SPAdj); 1369 1370 // We can't track the call frame size after call frame pseudos have been 1371 // eliminated. Set it to zero everywhere to keep MachineVerifier happy. 1372 MBB.setCallFrameSize(0); 1373 } 1374 } 1375 1376 /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical 1377 /// register references and actual offsets. 1378 void PEI::replaceFrameIndices(MachineFunction &MF) { 1379 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering(); 1380 1381 for (auto &MBB : MF) { 1382 int SPAdj = TFI.alignSPAdjust(MBB.getCallFrameSize()); 1383 if (TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsUp) 1384 SPAdj = -SPAdj; 1385 1386 replaceFrameIndices(&MBB, MF, SPAdj); 1387 1388 // We can't track the call frame size after call frame pseudos have been 1389 // eliminated. Set it to zero everywhere to keep MachineVerifier happy. 1390 MBB.setCallFrameSize(0); 1391 } 1392 } 1393 1394 bool PEI::replaceFrameIndexDebugInstr(MachineFunction &MF, MachineInstr &MI, 1395 unsigned OpIdx, int SPAdj) { 1396 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering(); 1397 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 1398 if (MI.isDebugValue()) { 1399 1400 MachineOperand &Op = MI.getOperand(OpIdx); 1401 assert(MI.isDebugOperand(&Op) && 1402 "Frame indices can only appear as a debug operand in a DBG_VALUE*" 1403 " machine instruction"); 1404 Register Reg; 1405 unsigned FrameIdx = Op.getIndex(); 1406 unsigned Size = MF.getFrameInfo().getObjectSize(FrameIdx); 1407 1408 StackOffset Offset = TFI->getFrameIndexReference(MF, FrameIdx, Reg); 1409 Op.ChangeToRegister(Reg, false /*isDef*/); 1410 1411 const DIExpression *DIExpr = MI.getDebugExpression(); 1412 1413 // If we have a direct DBG_VALUE, and its location expression isn't 1414 // currently complex, then adding an offset will morph it into a 1415 // complex location that is interpreted as being a memory address. 1416 // This changes a pointer-valued variable to dereference that pointer, 1417 // which is incorrect. Fix by adding DW_OP_stack_value. 1418 1419 if (MI.isNonListDebugValue()) { 1420 unsigned PrependFlags = DIExpression::ApplyOffset; 1421 if (!MI.isIndirectDebugValue() && !DIExpr->isComplex()) 1422 PrependFlags |= DIExpression::StackValue; 1423 1424 // If we have DBG_VALUE that is indirect and has a Implicit location 1425 // expression need to insert a deref before prepending a Memory 1426 // location expression. Also after doing this we change the DBG_VALUE 1427 // to be direct. 1428 if (MI.isIndirectDebugValue() && DIExpr->isImplicit()) { 1429 SmallVector<uint64_t, 2> Ops = {dwarf::DW_OP_deref_size, Size}; 1430 bool WithStackValue = true; 1431 DIExpr = DIExpression::prependOpcodes(DIExpr, Ops, WithStackValue); 1432 // Make the DBG_VALUE direct. 1433 MI.getDebugOffset().ChangeToRegister(0, false); 1434 } 1435 DIExpr = TRI.prependOffsetExpression(DIExpr, PrependFlags, Offset); 1436 } else { 1437 // The debug operand at DebugOpIndex was a frame index at offset 1438 // `Offset`; now the operand has been replaced with the frame 1439 // register, we must add Offset with `register x, plus Offset`. 1440 unsigned DebugOpIndex = MI.getDebugOperandIndex(&Op); 1441 SmallVector<uint64_t, 3> Ops; 1442 TRI.getOffsetOpcodes(Offset, Ops); 1443 DIExpr = DIExpression::appendOpsToArg(DIExpr, Ops, DebugOpIndex); 1444 } 1445 MI.getDebugExpressionOp().setMetadata(DIExpr); 1446 return true; 1447 } 1448 1449 if (MI.isDebugPHI()) { 1450 // Allow stack ref to continue onwards. 1451 return true; 1452 } 1453 1454 // TODO: This code should be commoned with the code for 1455 // PATCHPOINT. There's no good reason for the difference in 1456 // implementation other than historical accident. The only 1457 // remaining difference is the unconditional use of the stack 1458 // pointer as the base register. 1459 if (MI.getOpcode() == TargetOpcode::STATEPOINT) { 1460 assert((!MI.isDebugValue() || OpIdx == 0) && 1461 "Frame indicies can only appear as the first operand of a " 1462 "DBG_VALUE machine instruction"); 1463 Register Reg; 1464 MachineOperand &Offset = MI.getOperand(OpIdx + 1); 1465 StackOffset refOffset = TFI->getFrameIndexReferencePreferSP( 1466 MF, MI.getOperand(OpIdx).getIndex(), Reg, /*IgnoreSPUpdates*/ false); 1467 assert(!refOffset.getScalable() && 1468 "Frame offsets with a scalable component are not supported"); 1469 Offset.setImm(Offset.getImm() + refOffset.getFixed() + SPAdj); 1470 MI.getOperand(OpIdx).ChangeToRegister(Reg, false /*isDef*/); 1471 return true; 1472 } 1473 return false; 1474 } 1475 1476 void PEI::replaceFrameIndicesBackward(MachineBasicBlock *BB, 1477 MachineFunction &MF, int &SPAdj) { 1478 assert(MF.getSubtarget().getRegisterInfo() && 1479 "getRegisterInfo() must be implemented!"); 1480 1481 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); 1482 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 1483 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering(); 1484 1485 RegScavenger *LocalRS = FrameIndexEliminationScavenging ? RS : nullptr; 1486 if (LocalRS) 1487 LocalRS->enterBasicBlockEnd(*BB); 1488 1489 for (MachineBasicBlock::iterator I = BB->end(); I != BB->begin();) { 1490 MachineInstr &MI = *std::prev(I); 1491 1492 if (TII.isFrameInstr(MI)) { 1493 SPAdj -= TII.getSPAdjust(MI); 1494 TFI.eliminateCallFramePseudoInstr(MF, *BB, &MI); 1495 continue; 1496 } 1497 1498 // Step backwards to get the liveness state at (immedately after) MI. 1499 if (LocalRS) 1500 LocalRS->backward(MI); 1501 1502 bool RemovedMI = false; 1503 for (const auto &[Idx, Op] : enumerate(MI.operands())) { 1504 if (!Op.isFI()) 1505 continue; 1506 1507 if (replaceFrameIndexDebugInstr(MF, MI, Idx, SPAdj)) 1508 continue; 1509 1510 // Eliminate this FrameIndex operand. 1511 RemovedMI = TRI.eliminateFrameIndex(MI, SPAdj, Idx, LocalRS); 1512 if (RemovedMI) 1513 break; 1514 } 1515 1516 // Refresh the scavenger's internal iterator in case MI was removed or more 1517 // instructions were inserted after it. 1518 if (LocalRS) 1519 LocalRS->skipTo(std::prev(I)); 1520 1521 if (!RemovedMI) 1522 --I; 1523 } 1524 } 1525 1526 void PEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF, 1527 int &SPAdj) { 1528 assert(MF.getSubtarget().getRegisterInfo() && 1529 "getRegisterInfo() must be implemented!"); 1530 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); 1531 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 1532 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering(); 1533 1534 if (RS && FrameIndexEliminationScavenging) 1535 RS->enterBasicBlock(*BB); 1536 1537 bool InsideCallSequence = false; 1538 1539 for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) { 1540 if (TII.isFrameInstr(*I)) { 1541 InsideCallSequence = TII.isFrameSetup(*I); 1542 SPAdj += TII.getSPAdjust(*I); 1543 I = TFI->eliminateCallFramePseudoInstr(MF, *BB, I); 1544 continue; 1545 } 1546 1547 MachineInstr &MI = *I; 1548 bool DoIncr = true; 1549 bool DidFinishLoop = true; 1550 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 1551 if (!MI.getOperand(i).isFI()) 1552 continue; 1553 1554 if (replaceFrameIndexDebugInstr(MF, MI, i, SPAdj)) 1555 continue; 1556 1557 // Some instructions (e.g. inline asm instructions) can have 1558 // multiple frame indices and/or cause eliminateFrameIndex 1559 // to insert more than one instruction. We need the register 1560 // scavenger to go through all of these instructions so that 1561 // it can update its register information. We keep the 1562 // iterator at the point before insertion so that we can 1563 // revisit them in full. 1564 bool AtBeginning = (I == BB->begin()); 1565 if (!AtBeginning) --I; 1566 1567 // If this instruction has a FrameIndex operand, we need to 1568 // use that target machine register info object to eliminate 1569 // it. 1570 TRI.eliminateFrameIndex(MI, SPAdj, i, 1571 FrameIndexEliminationScavenging ? RS : nullptr); 1572 1573 // Reset the iterator if we were at the beginning of the BB. 1574 if (AtBeginning) { 1575 I = BB->begin(); 1576 DoIncr = false; 1577 } 1578 1579 DidFinishLoop = false; 1580 break; 1581 } 1582 1583 // If we are looking at a call sequence, we need to keep track of 1584 // the SP adjustment made by each instruction in the sequence. 1585 // This includes both the frame setup/destroy pseudos (handled above), 1586 // as well as other instructions that have side effects w.r.t the SP. 1587 // Note that this must come after eliminateFrameIndex, because 1588 // if I itself referred to a frame index, we shouldn't count its own 1589 // adjustment. 1590 if (DidFinishLoop && InsideCallSequence) 1591 SPAdj += TII.getSPAdjust(MI); 1592 1593 if (DoIncr && I != BB->end()) ++I; 1594 1595 // Update register states. 1596 if (RS && FrameIndexEliminationScavenging && DidFinishLoop) 1597 RS->forward(MI); 1598 } 1599 } 1600