PrologEpilogInserter.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/ADT/BitVector.h"
  19. #include "llvm/ADT/DepthFirstIterator.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/MachineLoopInfo.h"
  34. #include "llvm/CodeGen/MachineModuleInfo.h"
  35. #include "llvm/CodeGen/MachineOperand.h"
  36. #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
  37. #include "llvm/CodeGen/MachineRegisterInfo.h"
  38. #include "llvm/CodeGen/RegisterScavenging.h"
  39. #include "llvm/CodeGen/TargetFrameLowering.h"
  40. #include "llvm/CodeGen/TargetInstrInfo.h"
  41. #include "llvm/CodeGen/TargetOpcodes.h"
  42. #include "llvm/CodeGen/TargetRegisterInfo.h"
  43. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  44. #include "llvm/CodeGen/WinEHFuncInfo.h"
  45. #include "llvm/IR/Attributes.h"
  46. #include "llvm/IR/CallingConv.h"
  47. #include "llvm/IR/DebugInfoMetadata.h"
  48. #include "llvm/IR/DiagnosticInfo.h"
  49. #include "llvm/IR/Function.h"
  50. #include "llvm/IR/InlineAsm.h"
  51. #include "llvm/IR/LLVMContext.h"
  52. #include "llvm/MC/MCRegisterInfo.h"
  53. #include "llvm/Pass.h"
  54. #include "llvm/Support/CodeGen.h"
  55. #include "llvm/Support/CommandLine.h"
  56. #include "llvm/Support/Debug.h"
  57. #include "llvm/Support/ErrorHandling.h"
  58. #include "llvm/Support/MathExtras.h"
  59. #include "llvm/Support/raw_ostream.h"
  60. #include "llvm/Target/TargetMachine.h"
  61. #include "llvm/Target/TargetOptions.h"
  62. #include <algorithm>
  63. #include <cassert>
  64. #include <cstdint>
  65. #include <functional>
  66. #include <limits>
  67. #include <utility>
  68. #include <vector>
  69. using namespace llvm;
  70. #define DEBUG_TYPE "prologepilog"
  71. using MBBVector = SmallVector<MachineBasicBlock *, 4>;
  72. STATISTIC(NumLeafFuncWithSpills, "Number of leaf functions with CSRs");
  73. STATISTIC(NumFuncSeen, "Number of functions seen in PEI");
  74. namespace {
  75. class PEI : public MachineFunctionPass {
  76. public:
  77. static char ID;
  78. PEI() : MachineFunctionPass(ID) {
  79. initializePEIPass(*PassRegistry::getPassRegistry());
  80. }
  81. void getAnalysisUsage(AnalysisUsage &AU) const override;
  82. /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
  83. /// frame indexes with appropriate references.
  84. bool runOnMachineFunction(MachineFunction &MF) override;
  85. private:
  86. RegScavenger *RS;
  87. // MinCSFrameIndex, MaxCSFrameIndex - Keeps the range of callee saved
  88. // stack frame indexes.
  89. unsigned MinCSFrameIndex = std::numeric_limits<unsigned>::max();
  90. unsigned MaxCSFrameIndex = 0;
  91. // Save and Restore blocks of the current function. Typically there is a
  92. // single save block, unless Windows EH funclets are involved.
  93. MBBVector SaveBlocks;
  94. MBBVector RestoreBlocks;
  95. // Flag to control whether to use the register scavenger to resolve
  96. // frame index materialization registers. Set according to
  97. // TRI->requiresFrameIndexScavenging() for the current function.
  98. bool FrameIndexVirtualScavenging;
  99. // Flag to control whether the scavenger should be passed even though
  100. // FrameIndexVirtualScavenging is used.
  101. bool FrameIndexEliminationScavenging;
  102. // Emit remarks.
  103. MachineOptimizationRemarkEmitter *ORE = nullptr;
  104. void calculateCallFrameInfo(MachineFunction &MF);
  105. void calculateSaveRestoreBlocks(MachineFunction &MF);
  106. void spillCalleeSavedRegs(MachineFunction &MF);
  107. void calculateFrameObjectOffsets(MachineFunction &MF);
  108. void replaceFrameIndices(MachineFunction &MF);
  109. void replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
  110. int &SPAdj);
  111. void insertPrologEpilogCode(MachineFunction &MF);
  112. };
  113. } // end anonymous namespace
  114. char PEI::ID = 0;
  115. char &llvm::PrologEpilogCodeInserterID = PEI::ID;
  116. static cl::opt<unsigned>
  117. WarnStackSize("warn-stack-size", cl::Hidden, cl::init((unsigned)-1),
  118. cl::desc("Warn for stack size bigger than the given"
  119. " number"));
  120. INITIALIZE_PASS_BEGIN(PEI, DEBUG_TYPE, "Prologue/Epilogue Insertion", false,
  121. false)
  122. INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
  123. INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
  124. INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass)
  125. INITIALIZE_PASS_END(PEI, DEBUG_TYPE,
  126. "Prologue/Epilogue Insertion & Frame Finalization", false,
  127. false)
  128. MachineFunctionPass *llvm::createPrologEpilogInserterPass() {
  129. return new PEI();
  130. }
  131. STATISTIC(NumBytesStackSpace,
  132. "Number of bytes used for stack in all functions");
  133. void PEI::getAnalysisUsage(AnalysisUsage &AU) const {
  134. AU.setPreservesCFG();
  135. AU.addPreserved<MachineLoopInfo>();
  136. AU.addPreserved<MachineDominatorTree>();
  137. AU.addRequired<MachineOptimizationRemarkEmitterPass>();
  138. MachineFunctionPass::getAnalysisUsage(AU);
  139. }
  140. /// StackObjSet - A set of stack object indexes
  141. using StackObjSet = SmallSetVector<int, 8>;
  142. using SavedDbgValuesMap =
  143. SmallDenseMap<MachineBasicBlock *, SmallVector<MachineInstr *, 4>, 4>;
  144. /// Stash DBG_VALUEs that describe parameters and which are placed at the start
  145. /// of the block. Later on, after the prologue code has been emitted, the
  146. /// stashed DBG_VALUEs will be reinserted at the start of the block.
  147. static void stashEntryDbgValues(MachineBasicBlock &MBB,
  148. SavedDbgValuesMap &EntryDbgValues) {
  149. SmallVector<const MachineInstr *, 4> FrameIndexValues;
  150. for (auto &MI : MBB) {
  151. if (!MI.isDebugInstr())
  152. break;
  153. if (!MI.isDebugValue() || !MI.getDebugVariable()->isParameter())
  154. continue;
  155. if (MI.getOperand(0).isFI()) {
  156. // We can only emit valid locations for frame indices after the frame
  157. // setup, so do not stash away them.
  158. FrameIndexValues.push_back(&MI);
  159. continue;
  160. }
  161. const DILocalVariable *Var = MI.getDebugVariable();
  162. const DIExpression *Expr = MI.getDebugExpression();
  163. auto Overlaps = [Var, Expr](const MachineInstr *DV) {
  164. return Var == DV->getDebugVariable() &&
  165. Expr->fragmentsOverlap(DV->getDebugExpression());
  166. };
  167. // See if the debug value overlaps with any preceding debug value that will
  168. // not be stashed. If that is the case, then we can't stash this value, as
  169. // we would then reorder the values at reinsertion.
  170. if (llvm::none_of(FrameIndexValues, Overlaps))
  171. EntryDbgValues[&MBB].push_back(&MI);
  172. }
  173. // Remove stashed debug values from the block.
  174. if (EntryDbgValues.count(&MBB))
  175. for (auto *MI : EntryDbgValues[&MBB])
  176. MI->removeFromParent();
  177. }
  178. /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
  179. /// frame indexes with appropriate references.
  180. bool PEI::runOnMachineFunction(MachineFunction &MF) {
  181. NumFuncSeen++;
  182. const Function &F = MF.getFunction();
  183. const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
  184. const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
  185. RS = TRI->requiresRegisterScavenging(MF) ? new RegScavenger() : nullptr;
  186. FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(MF);
  187. ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
  188. // Calculate the MaxCallFrameSize and AdjustsStack variables for the
  189. // function's frame information. Also eliminates call frame pseudo
  190. // instructions.
  191. calculateCallFrameInfo(MF);
  192. // Determine placement of CSR spill/restore code and prolog/epilog code:
  193. // place all spills in the entry block, all restores in return blocks.
  194. calculateSaveRestoreBlocks(MF);
  195. // Stash away DBG_VALUEs that should not be moved by insertion of prolog code.
  196. SavedDbgValuesMap EntryDbgValues;
  197. for (MachineBasicBlock *SaveBlock : SaveBlocks)
  198. stashEntryDbgValues(*SaveBlock, EntryDbgValues);
  199. // Handle CSR spilling and restoring, for targets that need it.
  200. if (MF.getTarget().usesPhysRegsForPEI())
  201. spillCalleeSavedRegs(MF);
  202. // Allow the target machine to make final modifications to the function
  203. // before the frame layout is finalized.
  204. TFI->processFunctionBeforeFrameFinalized(MF, RS);
  205. // Calculate actual frame offsets for all abstract stack objects...
  206. calculateFrameObjectOffsets(MF);
  207. // Add prolog and epilog code to the function. This function is required
  208. // to align the stack frame as necessary for any stack variables or
  209. // called functions. Because of this, calculateCalleeSavedRegisters()
  210. // must be called before this function in order to set the AdjustsStack
  211. // and MaxCallFrameSize variables.
  212. if (!F.hasFnAttribute(Attribute::Naked))
  213. insertPrologEpilogCode(MF);
  214. // Reinsert stashed debug values at the start of the entry blocks.
  215. for (auto &I : EntryDbgValues)
  216. I.first->insert(I.first->begin(), I.second.begin(), I.second.end());
  217. // Replace all MO_FrameIndex operands with physical register references
  218. // and actual offsets.
  219. //
  220. replaceFrameIndices(MF);
  221. // If register scavenging is needed, as we've enabled doing it as a
  222. // post-pass, scavenge the virtual registers that frame index elimination
  223. // inserted.
  224. if (TRI->requiresRegisterScavenging(MF) && FrameIndexVirtualScavenging)
  225. scavengeFrameVirtualRegs(MF, *RS);
  226. // Warn on stack size when we exceeds the given limit.
  227. MachineFrameInfo &MFI = MF.getFrameInfo();
  228. uint64_t StackSize = MFI.getStackSize();
  229. if (WarnStackSize.getNumOccurrences() > 0 && WarnStackSize < StackSize) {
  230. DiagnosticInfoStackSize DiagStackSize(F, StackSize);
  231. F.getContext().diagnose(DiagStackSize);
  232. }
  233. ORE->emit([&]() {
  234. return MachineOptimizationRemarkAnalysis(DEBUG_TYPE, "StackSize",
  235. MF.getFunction().getSubprogram(),
  236. &MF.front())
  237. << ore::NV("NumStackBytes", StackSize) << " stack bytes in function";
  238. });
  239. delete RS;
  240. SaveBlocks.clear();
  241. RestoreBlocks.clear();
  242. MFI.setSavePoint(nullptr);
  243. MFI.setRestorePoint(nullptr);
  244. return true;
  245. }
  246. /// Calculate the MaxCallFrameSize and AdjustsStack
  247. /// variables for the function's frame information and eliminate call frame
  248. /// pseudo instructions.
  249. void PEI::calculateCallFrameInfo(MachineFunction &MF) {
  250. const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
  251. const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
  252. MachineFrameInfo &MFI = MF.getFrameInfo();
  253. unsigned MaxCallFrameSize = 0;
  254. bool AdjustsStack = MFI.adjustsStack();
  255. // Get the function call frame set-up and tear-down instruction opcode
  256. unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
  257. unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
  258. // Early exit for targets which have no call frame setup/destroy pseudo
  259. // instructions.
  260. if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
  261. return;
  262. std::vector<MachineBasicBlock::iterator> FrameSDOps;
  263. for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
  264. for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
  265. if (TII.isFrameInstr(*I)) {
  266. unsigned Size = TII.getFrameSize(*I);
  267. if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
  268. AdjustsStack = true;
  269. FrameSDOps.push_back(I);
  270. } else if (I->isInlineAsm()) {
  271. // Some inline asm's need a stack frame, as indicated by operand 1.
  272. unsigned ExtraInfo = I->getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
  273. if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
  274. AdjustsStack = true;
  275. }
  276. assert(!MFI.isMaxCallFrameSizeComputed() ||
  277. (MFI.getMaxCallFrameSize() == MaxCallFrameSize &&
  278. MFI.adjustsStack() == AdjustsStack));
  279. MFI.setAdjustsStack(AdjustsStack);
  280. MFI.setMaxCallFrameSize(MaxCallFrameSize);
  281. for (std::vector<MachineBasicBlock::iterator>::iterator
  282. i = FrameSDOps.begin(), e = FrameSDOps.end(); i != e; ++i) {
  283. MachineBasicBlock::iterator I = *i;
  284. // If call frames are not being included as part of the stack frame, and
  285. // the target doesn't indicate otherwise, remove the call frame pseudos
  286. // here. The sub/add sp instruction pairs are still inserted, but we don't
  287. // need to track the SP adjustment for frame index elimination.
  288. if (TFI->canSimplifyCallFramePseudos(MF))
  289. TFI->eliminateCallFramePseudoInstr(MF, *I->getParent(), I);
  290. }
  291. }
  292. /// Compute the sets of entry and return blocks for saving and restoring
  293. /// callee-saved registers, and placing prolog and epilog code.
  294. void PEI::calculateSaveRestoreBlocks(MachineFunction &MF) {
  295. const MachineFrameInfo &MFI = MF.getFrameInfo();
  296. // Even when we do not change any CSR, we still want to insert the
  297. // prologue and epilogue of the function.
  298. // So set the save points for those.
  299. // Use the points found by shrink-wrapping, if any.
  300. if (MFI.getSavePoint()) {
  301. SaveBlocks.push_back(MFI.getSavePoint());
  302. assert(MFI.getRestorePoint() && "Both restore and save must be set");
  303. MachineBasicBlock *RestoreBlock = MFI.getRestorePoint();
  304. // If RestoreBlock does not have any successor and is not a return block
  305. // then the end point is unreachable and we do not need to insert any
  306. // epilogue.
  307. if (!RestoreBlock->succ_empty() || RestoreBlock->isReturnBlock())
  308. RestoreBlocks.push_back(RestoreBlock);
  309. return;
  310. }
  311. // Save refs to entry and return blocks.
  312. SaveBlocks.push_back(&MF.front());
  313. for (MachineBasicBlock &MBB : MF) {
  314. if (MBB.isEHFuncletEntry())
  315. SaveBlocks.push_back(&MBB);
  316. if (MBB.isReturnBlock())
  317. RestoreBlocks.push_back(&MBB);
  318. }
  319. }
  320. static void assignCalleeSavedSpillSlots(MachineFunction &F,
  321. const BitVector &SavedRegs,
  322. unsigned &MinCSFrameIndex,
  323. unsigned &MaxCSFrameIndex) {
  324. if (SavedRegs.empty())
  325. return;
  326. const TargetRegisterInfo *RegInfo = F.getSubtarget().getRegisterInfo();
  327. const MCPhysReg *CSRegs = F.getRegInfo().getCalleeSavedRegs();
  328. std::vector<CalleeSavedInfo> CSI;
  329. for (unsigned i = 0; CSRegs[i]; ++i) {
  330. unsigned Reg = CSRegs[i];
  331. if (SavedRegs.test(Reg))
  332. CSI.push_back(CalleeSavedInfo(Reg));
  333. }
  334. const TargetFrameLowering *TFI = F.getSubtarget().getFrameLowering();
  335. MachineFrameInfo &MFI = F.getFrameInfo();
  336. if (!TFI->assignCalleeSavedSpillSlots(F, RegInfo, CSI)) {
  337. // If target doesn't implement this, use generic code.
  338. if (CSI.empty())
  339. return; // Early exit if no callee saved registers are modified!
  340. unsigned NumFixedSpillSlots;
  341. const TargetFrameLowering::SpillSlot *FixedSpillSlots =
  342. TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots);
  343. // Now that we know which registers need to be saved and restored, allocate
  344. // stack slots for them.
  345. for (auto &CS : CSI) {
  346. // If the target has spilled this register to another register, we don't
  347. // need to allocate a stack slot.
  348. if (CS.isSpilledToReg())
  349. continue;
  350. unsigned Reg = CS.getReg();
  351. const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg);
  352. int FrameIdx;
  353. if (RegInfo->hasReservedSpillSlot(F, Reg, FrameIdx)) {
  354. CS.setFrameIdx(FrameIdx);
  355. continue;
  356. }
  357. // Check to see if this physreg must be spilled to a particular stack slot
  358. // on this target.
  359. const TargetFrameLowering::SpillSlot *FixedSlot = FixedSpillSlots;
  360. while (FixedSlot != FixedSpillSlots + NumFixedSpillSlots &&
  361. FixedSlot->Reg != Reg)
  362. ++FixedSlot;
  363. unsigned Size = RegInfo->getSpillSize(*RC);
  364. if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) {
  365. // Nope, just spill it anywhere convenient.
  366. unsigned Align = RegInfo->getSpillAlignment(*RC);
  367. unsigned StackAlign = TFI->getStackAlignment();
  368. // We may not be able to satisfy the desired alignment specification of
  369. // the TargetRegisterClass if the stack alignment is smaller. Use the
  370. // min.
  371. Align = std::min(Align, StackAlign);
  372. FrameIdx = MFI.CreateStackObject(Size, Align, true);
  373. if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx;
  374. if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx;
  375. } else {
  376. // Spill it to the stack where we must.
  377. FrameIdx = MFI.CreateFixedSpillStackObject(Size, FixedSlot->Offset);
  378. }
  379. CS.setFrameIdx(FrameIdx);
  380. }
  381. }
  382. MFI.setCalleeSavedInfo(CSI);
  383. }
  384. /// Helper function to update the liveness information for the callee-saved
  385. /// registers.
  386. static void updateLiveness(MachineFunction &MF) {
  387. MachineFrameInfo &MFI = MF.getFrameInfo();
  388. // Visited will contain all the basic blocks that are in the region
  389. // where the callee saved registers are alive:
  390. // - Anything that is not Save or Restore -> LiveThrough.
  391. // - Save -> LiveIn.
  392. // - Restore -> LiveOut.
  393. // The live-out is not attached to the block, so no need to keep
  394. // Restore in this set.
  395. SmallPtrSet<MachineBasicBlock *, 8> Visited;
  396. SmallVector<MachineBasicBlock *, 8> WorkList;
  397. MachineBasicBlock *Entry = &MF.front();
  398. MachineBasicBlock *Save = MFI.getSavePoint();
  399. if (!Save)
  400. Save = Entry;
  401. if (Entry != Save) {
  402. WorkList.push_back(Entry);
  403. Visited.insert(Entry);
  404. }
  405. Visited.insert(Save);
  406. MachineBasicBlock *Restore = MFI.getRestorePoint();
  407. if (Restore)
  408. // By construction Restore cannot be visited, otherwise it
  409. // means there exists a path to Restore that does not go
  410. // through Save.
  411. WorkList.push_back(Restore);
  412. while (!WorkList.empty()) {
  413. const MachineBasicBlock *CurBB = WorkList.pop_back_val();
  414. // By construction, the region that is after the save point is
  415. // dominated by the Save and post-dominated by the Restore.
  416. if (CurBB == Save && Save != Restore)
  417. continue;
  418. // Enqueue all the successors not already visited.
  419. // Those are by construction either before Save or after Restore.
  420. for (MachineBasicBlock *SuccBB : CurBB->successors())
  421. if (Visited.insert(SuccBB).second)
  422. WorkList.push_back(SuccBB);
  423. }
  424. const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
  425. MachineRegisterInfo &MRI = MF.getRegInfo();
  426. for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
  427. for (MachineBasicBlock *MBB : Visited) {
  428. MCPhysReg Reg = CSI[i].getReg();
  429. // Add the callee-saved register as live-in.
  430. // It's killed at the spill.
  431. if (!MRI.isReserved(Reg) && !MBB->isLiveIn(Reg))
  432. MBB->addLiveIn(Reg);
  433. }
  434. // If callee-saved register is spilled to another register rather than
  435. // spilling to stack, the destination register has to be marked as live for
  436. // each MBB between the prologue and epilogue so that it is not clobbered
  437. // before it is reloaded in the epilogue. The Visited set contains all
  438. // blocks outside of the region delimited by prologue/epilogue.
  439. if (CSI[i].isSpilledToReg()) {
  440. for (MachineBasicBlock &MBB : MF) {
  441. if (Visited.count(&MBB))
  442. continue;
  443. MCPhysReg DstReg = CSI[i].getDstReg();
  444. if (!MBB.isLiveIn(DstReg))
  445. MBB.addLiveIn(DstReg);
  446. }
  447. }
  448. }
  449. }
  450. /// Insert restore code for the callee-saved registers used in the function.
  451. static void insertCSRSaves(MachineBasicBlock &SaveBlock,
  452. ArrayRef<CalleeSavedInfo> CSI) {
  453. MachineFunction &MF = *SaveBlock.getParent();
  454. const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
  455. const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
  456. const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
  457. MachineBasicBlock::iterator I = SaveBlock.begin();
  458. if (!TFI->spillCalleeSavedRegisters(SaveBlock, I, CSI, TRI)) {
  459. for (const CalleeSavedInfo &CS : CSI) {
  460. // Insert the spill to the stack frame.
  461. unsigned Reg = CS.getReg();
  462. const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
  463. TII.storeRegToStackSlot(SaveBlock, I, Reg, true, CS.getFrameIdx(), RC,
  464. TRI);
  465. }
  466. }
  467. }
  468. /// Insert restore code for the callee-saved registers used in the function.
  469. static void insertCSRRestores(MachineBasicBlock &RestoreBlock,
  470. std::vector<CalleeSavedInfo> &CSI) {
  471. MachineFunction &MF = *RestoreBlock.getParent();
  472. const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
  473. const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
  474. const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
  475. // Restore all registers immediately before the return and any
  476. // terminators that precede it.
  477. MachineBasicBlock::iterator I = RestoreBlock.getFirstTerminator();
  478. if (!TFI->restoreCalleeSavedRegisters(RestoreBlock, I, CSI, TRI)) {
  479. for (const CalleeSavedInfo &CI : reverse(CSI)) {
  480. unsigned Reg = CI.getReg();
  481. const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
  482. TII.loadRegFromStackSlot(RestoreBlock, I, Reg, CI.getFrameIdx(), RC, TRI);
  483. assert(I != RestoreBlock.begin() &&
  484. "loadRegFromStackSlot didn't insert any code!");
  485. // Insert in reverse order. loadRegFromStackSlot can insert
  486. // multiple instructions.
  487. }
  488. }
  489. }
  490. void PEI::spillCalleeSavedRegs(MachineFunction &MF) {
  491. // We can't list this requirement in getRequiredProperties because some
  492. // targets (WebAssembly) use virtual registers past this point, and the pass
  493. // pipeline is set up without giving the passes a chance to look at the
  494. // TargetMachine.
  495. // FIXME: Find a way to express this in getRequiredProperties.
  496. assert(MF.getProperties().hasProperty(
  497. MachineFunctionProperties::Property::NoVRegs));
  498. const Function &F = MF.getFunction();
  499. const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
  500. MachineFrameInfo &MFI = MF.getFrameInfo();
  501. MinCSFrameIndex = std::numeric_limits<unsigned>::max();
  502. MaxCSFrameIndex = 0;
  503. // Determine which of the registers in the callee save list should be saved.
  504. BitVector SavedRegs;
  505. TFI->determineCalleeSaves(MF, SavedRegs, RS);
  506. // Assign stack slots for any callee-saved registers that must be spilled.
  507. assignCalleeSavedSpillSlots(MF, SavedRegs, MinCSFrameIndex, MaxCSFrameIndex);
  508. // Add the code to save and restore the callee saved registers.
  509. if (!F.hasFnAttribute(Attribute::Naked)) {
  510. MFI.setCalleeSavedInfoValid(true);
  511. std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
  512. if (!CSI.empty()) {
  513. if (!MFI.hasCalls())
  514. NumLeafFuncWithSpills++;
  515. for (MachineBasicBlock *SaveBlock : SaveBlocks) {
  516. insertCSRSaves(*SaveBlock, CSI);
  517. // Update the live-in information of all the blocks up to the save
  518. // point.
  519. updateLiveness(MF);
  520. }
  521. for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
  522. insertCSRRestores(*RestoreBlock, CSI);
  523. }
  524. }
  525. }
  526. /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
  527. static inline void
  528. AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx,
  529. bool StackGrowsDown, int64_t &Offset,
  530. unsigned &MaxAlign, unsigned Skew) {
  531. // If the stack grows down, add the object size to find the lowest address.
  532. if (StackGrowsDown)
  533. Offset += MFI.getObjectSize(FrameIdx);
  534. unsigned Align = MFI.getObjectAlignment(FrameIdx);
  535. // If the alignment of this object is greater than that of the stack, then
  536. // increase the stack alignment to match.
  537. MaxAlign = std::max(MaxAlign, Align);
  538. // Adjust to alignment boundary.
  539. Offset = alignTo(Offset, Align, Skew);
  540. if (StackGrowsDown) {
  541. LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset
  542. << "]\n");
  543. MFI.setObjectOffset(FrameIdx, -Offset); // Set the computed offset
  544. } else {
  545. LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset
  546. << "]\n");
  547. MFI.setObjectOffset(FrameIdx, Offset);
  548. Offset += MFI.getObjectSize(FrameIdx);
  549. }
  550. }
  551. /// Compute which bytes of fixed and callee-save stack area are unused and keep
  552. /// track of them in StackBytesFree.
  553. static inline void
  554. computeFreeStackSlots(MachineFrameInfo &MFI, bool StackGrowsDown,
  555. unsigned MinCSFrameIndex, unsigned MaxCSFrameIndex,
  556. int64_t FixedCSEnd, BitVector &StackBytesFree) {
  557. // Avoid undefined int64_t -> int conversion below in extreme case.
  558. if (FixedCSEnd > std::numeric_limits<int>::max())
  559. return;
  560. StackBytesFree.resize(FixedCSEnd, true);
  561. SmallVector<int, 16> AllocatedFrameSlots;
  562. // Add fixed objects.
  563. for (int i = MFI.getObjectIndexBegin(); i != 0; ++i)
  564. // StackSlot scavenging is only implemented for the default stack.
  565. if (MFI.getStackID(i) == 0)
  566. AllocatedFrameSlots.push_back(i);
  567. // Add callee-save objects.
  568. for (int i = MinCSFrameIndex; i <= (int)MaxCSFrameIndex; ++i)
  569. if (MFI.getStackID(i) == 0)
  570. AllocatedFrameSlots.push_back(i);
  571. for (int i : AllocatedFrameSlots) {
  572. // These are converted from int64_t, but they should always fit in int
  573. // because of the FixedCSEnd check above.
  574. int ObjOffset = MFI.getObjectOffset(i);
  575. int ObjSize = MFI.getObjectSize(i);
  576. int ObjStart, ObjEnd;
  577. if (StackGrowsDown) {
  578. // ObjOffset is negative when StackGrowsDown is true.
  579. ObjStart = -ObjOffset - ObjSize;
  580. ObjEnd = -ObjOffset;
  581. } else {
  582. ObjStart = ObjOffset;
  583. ObjEnd = ObjOffset + ObjSize;
  584. }
  585. // Ignore fixed holes that are in the previous stack frame.
  586. if (ObjEnd > 0)
  587. StackBytesFree.reset(ObjStart, ObjEnd);
  588. }
  589. }
  590. /// Assign frame object to an unused portion of the stack in the fixed stack
  591. /// object range. Return true if the allocation was successful.
  592. static inline bool scavengeStackSlot(MachineFrameInfo &MFI, int FrameIdx,
  593. bool StackGrowsDown, unsigned MaxAlign,
  594. BitVector &StackBytesFree) {
  595. if (MFI.isVariableSizedObjectIndex(FrameIdx))
  596. return false;
  597. if (StackBytesFree.none()) {
  598. // clear it to speed up later scavengeStackSlot calls to
  599. // StackBytesFree.none()
  600. StackBytesFree.clear();
  601. return false;
  602. }
  603. unsigned ObjAlign = MFI.getObjectAlignment(FrameIdx);
  604. if (ObjAlign > MaxAlign)
  605. return false;
  606. int64_t ObjSize = MFI.getObjectSize(FrameIdx);
  607. int FreeStart;
  608. for (FreeStart = StackBytesFree.find_first(); FreeStart != -1;
  609. FreeStart = StackBytesFree.find_next(FreeStart)) {
  610. // Check that free space has suitable alignment.
  611. unsigned ObjStart = StackGrowsDown ? FreeStart + ObjSize : FreeStart;
  612. if (alignTo(ObjStart, ObjAlign) != ObjStart)
  613. continue;
  614. if (FreeStart + ObjSize > StackBytesFree.size())
  615. return false;
  616. bool AllBytesFree = true;
  617. for (unsigned Byte = 0; Byte < ObjSize; ++Byte)
  618. if (!StackBytesFree.test(FreeStart + Byte)) {
  619. AllBytesFree = false;
  620. break;
  621. }
  622. if (AllBytesFree)
  623. break;
  624. }
  625. if (FreeStart == -1)
  626. return false;
  627. if (StackGrowsDown) {
  628. int ObjStart = -(FreeStart + ObjSize);
  629. LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP["
  630. << ObjStart << "]\n");
  631. MFI.setObjectOffset(FrameIdx, ObjStart);
  632. } else {
  633. LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP["
  634. << FreeStart << "]\n");
  635. MFI.setObjectOffset(FrameIdx, FreeStart);
  636. }
  637. StackBytesFree.reset(FreeStart, FreeStart + ObjSize);
  638. return true;
  639. }
  640. /// AssignProtectedObjSet - Helper function to assign large stack objects (i.e.,
  641. /// those required to be close to the Stack Protector) to stack offsets.
  642. static void
  643. AssignProtectedObjSet(const StackObjSet &UnassignedObjs,
  644. SmallSet<int, 16> &ProtectedObjs,
  645. MachineFrameInfo &MFI, bool StackGrowsDown,
  646. int64_t &Offset, unsigned &MaxAlign, unsigned Skew) {
  647. for (StackObjSet::const_iterator I = UnassignedObjs.begin(),
  648. E = UnassignedObjs.end(); I != E; ++I) {
  649. int i = *I;
  650. AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign, Skew);
  651. ProtectedObjs.insert(i);
  652. }
  653. }
  654. /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
  655. /// abstract stack objects.
  656. void PEI::calculateFrameObjectOffsets(MachineFunction &MF) {
  657. const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
  658. bool StackGrowsDown =
  659. TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
  660. // Loop over all of the stack objects, assigning sequential addresses...
  661. MachineFrameInfo &MFI = MF.getFrameInfo();
  662. // Start at the beginning of the local area.
  663. // The Offset is the distance from the stack top in the direction
  664. // of stack growth -- so it's always nonnegative.
  665. int LocalAreaOffset = TFI.getOffsetOfLocalArea();
  666. if (StackGrowsDown)
  667. LocalAreaOffset = -LocalAreaOffset;
  668. assert(LocalAreaOffset >= 0
  669. && "Local area offset should be in direction of stack growth");
  670. int64_t Offset = LocalAreaOffset;
  671. // Skew to be applied to alignment.
  672. unsigned Skew = TFI.getStackAlignmentSkew(MF);
  673. #ifdef EXPENSIVE_CHECKS
  674. for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i)
  675. if (!MFI.isDeadObjectIndex(i) && MFI.getStackID(i) == 0)
  676. assert(MFI.getObjectAlignment(i) <= MFI.getMaxAlignment() &&
  677. "MaxAlignment is invalid");
  678. #endif
  679. // If there are fixed sized objects that are preallocated in the local area,
  680. // non-fixed objects can't be allocated right at the start of local area.
  681. // Adjust 'Offset' to point to the end of last fixed sized preallocated
  682. // object.
  683. for (int i = MFI.getObjectIndexBegin(); i != 0; ++i) {
  684. if (MFI.getStackID(i)) // Only allocate objects on the default stack.
  685. continue;
  686. int64_t FixedOff;
  687. if (StackGrowsDown) {
  688. // The maximum distance from the stack pointer is at lower address of
  689. // the object -- which is given by offset. For down growing stack
  690. // the offset is negative, so we negate the offset to get the distance.
  691. FixedOff = -MFI.getObjectOffset(i);
  692. } else {
  693. // The maximum distance from the start pointer is at the upper
  694. // address of the object.
  695. FixedOff = MFI.getObjectOffset(i) + MFI.getObjectSize(i);
  696. }
  697. if (FixedOff > Offset) Offset = FixedOff;
  698. }
  699. // First assign frame offsets to stack objects that are used to spill
  700. // callee saved registers.
  701. if (StackGrowsDown) {
  702. for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) {
  703. if (MFI.getStackID(i)) // Only allocate objects on the default stack.
  704. continue;
  705. // If the stack grows down, we need to add the size to find the lowest
  706. // address of the object.
  707. Offset += MFI.getObjectSize(i);
  708. unsigned Align = MFI.getObjectAlignment(i);
  709. // Adjust to alignment boundary
  710. Offset = alignTo(Offset, Align, Skew);
  711. LLVM_DEBUG(dbgs() << "alloc FI(" << i << ") at SP[" << -Offset << "]\n");
  712. MFI.setObjectOffset(i, -Offset); // Set the computed offset
  713. }
  714. } else if (MaxCSFrameIndex >= MinCSFrameIndex) {
  715. // Be careful about underflow in comparisons agains MinCSFrameIndex.
  716. for (unsigned i = MaxCSFrameIndex; i != MinCSFrameIndex - 1; --i) {
  717. if (MFI.getStackID(i)) // Only allocate objects on the default stack.
  718. continue;
  719. if (MFI.isDeadObjectIndex(i))
  720. continue;
  721. unsigned Align = MFI.getObjectAlignment(i);
  722. // Adjust to alignment boundary
  723. Offset = alignTo(Offset, Align, Skew);
  724. LLVM_DEBUG(dbgs() << "alloc FI(" << i << ") at SP[" << Offset << "]\n");
  725. MFI.setObjectOffset(i, Offset);
  726. Offset += MFI.getObjectSize(i);
  727. }
  728. }
  729. // FixedCSEnd is the stack offset to the end of the fixed and callee-save
  730. // stack area.
  731. int64_t FixedCSEnd = Offset;
  732. unsigned MaxAlign = MFI.getMaxAlignment();
  733. // Make sure the special register scavenging spill slot is closest to the
  734. // incoming stack pointer if a frame pointer is required and is closer
  735. // to the incoming rather than the final stack pointer.
  736. const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
  737. bool EarlyScavengingSlots = (TFI.hasFP(MF) &&
  738. TFI.isFPCloseToIncomingSP() &&
  739. RegInfo->useFPForScavengingIndex(MF) &&
  740. !RegInfo->needsStackRealignment(MF));
  741. if (RS && EarlyScavengingSlots) {
  742. SmallVector<int, 2> SFIs;
  743. RS->getScavengingFrameIndices(SFIs);
  744. for (SmallVectorImpl<int>::iterator I = SFIs.begin(),
  745. IE = SFIs.end(); I != IE; ++I)
  746. AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign, Skew);
  747. }
  748. // FIXME: Once this is working, then enable flag will change to a target
  749. // check for whether the frame is large enough to want to use virtual
  750. // frame index registers. Functions which don't want/need this optimization
  751. // will continue to use the existing code path.
  752. if (MFI.getUseLocalStackAllocationBlock()) {
  753. unsigned Align = MFI.getLocalFrameMaxAlign();
  754. // Adjust to alignment boundary.
  755. Offset = alignTo(Offset, Align, Skew);
  756. LLVM_DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
  757. // Resolve offsets for objects in the local block.
  758. for (unsigned i = 0, e = MFI.getLocalFrameObjectCount(); i != e; ++i) {
  759. std::pair<int, int64_t> Entry = MFI.getLocalFrameObjectMap(i);
  760. int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
  761. LLVM_DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" << FIOffset
  762. << "]\n");
  763. MFI.setObjectOffset(Entry.first, FIOffset);
  764. }
  765. // Allocate the local block
  766. Offset += MFI.getLocalFrameSize();
  767. MaxAlign = std::max(Align, MaxAlign);
  768. }
  769. // Retrieve the Exception Handler registration node.
  770. int EHRegNodeFrameIndex = std::numeric_limits<int>::max();
  771. if (const WinEHFuncInfo *FuncInfo = MF.getWinEHFuncInfo())
  772. EHRegNodeFrameIndex = FuncInfo->EHRegNodeFrameIndex;
  773. // Make sure that the stack protector comes before the local variables on the
  774. // stack.
  775. SmallSet<int, 16> ProtectedObjs;
  776. if (MFI.getStackProtectorIndex() >= 0) {
  777. StackObjSet LargeArrayObjs;
  778. StackObjSet SmallArrayObjs;
  779. StackObjSet AddrOfObjs;
  780. AdjustStackOffset(MFI, MFI.getStackProtectorIndex(), StackGrowsDown,
  781. Offset, MaxAlign, Skew);
  782. // Assign large stack objects first.
  783. for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
  784. if (MFI.isObjectPreAllocated(i) &&
  785. MFI.getUseLocalStackAllocationBlock())
  786. continue;
  787. if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
  788. continue;
  789. if (RS && RS->isScavengingFrameIndex((int)i))
  790. continue;
  791. if (MFI.isDeadObjectIndex(i))
  792. continue;
  793. if (MFI.getStackProtectorIndex() == (int)i ||
  794. EHRegNodeFrameIndex == (int)i)
  795. continue;
  796. if (MFI.getStackID(i)) // Only allocate objects on the default stack.
  797. continue;
  798. switch (MFI.getObjectSSPLayout(i)) {
  799. case MachineFrameInfo::SSPLK_None:
  800. continue;
  801. case MachineFrameInfo::SSPLK_SmallArray:
  802. SmallArrayObjs.insert(i);
  803. continue;
  804. case MachineFrameInfo::SSPLK_AddrOf:
  805. AddrOfObjs.insert(i);
  806. continue;
  807. case MachineFrameInfo::SSPLK_LargeArray:
  808. LargeArrayObjs.insert(i);
  809. continue;
  810. }
  811. llvm_unreachable("Unexpected SSPLayoutKind.");
  812. }
  813. AssignProtectedObjSet(LargeArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
  814. Offset, MaxAlign, Skew);
  815. AssignProtectedObjSet(SmallArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
  816. Offset, MaxAlign, Skew);
  817. AssignProtectedObjSet(AddrOfObjs, ProtectedObjs, MFI, StackGrowsDown,
  818. Offset, MaxAlign, Skew);
  819. }
  820. SmallVector<int, 8> ObjectsToAllocate;
  821. // Then prepare to assign frame offsets to stack objects that are not used to
  822. // spill callee saved registers.
  823. for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
  824. if (MFI.isObjectPreAllocated(i) && MFI.getUseLocalStackAllocationBlock())
  825. continue;
  826. if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
  827. continue;
  828. if (RS && RS->isScavengingFrameIndex((int)i))
  829. continue;
  830. if (MFI.isDeadObjectIndex(i))
  831. continue;
  832. if (MFI.getStackProtectorIndex() == (int)i ||
  833. EHRegNodeFrameIndex == (int)i)
  834. continue;
  835. if (ProtectedObjs.count(i))
  836. continue;
  837. if (MFI.getStackID(i)) // Only allocate objects on the default stack.
  838. continue;
  839. // Add the objects that we need to allocate to our working set.
  840. ObjectsToAllocate.push_back(i);
  841. }
  842. // Allocate the EH registration node first if one is present.
  843. if (EHRegNodeFrameIndex != std::numeric_limits<int>::max())
  844. AdjustStackOffset(MFI, EHRegNodeFrameIndex, StackGrowsDown, Offset,
  845. MaxAlign, Skew);
  846. // Give the targets a chance to order the objects the way they like it.
  847. if (MF.getTarget().getOptLevel() != CodeGenOpt::None &&
  848. MF.getTarget().Options.StackSymbolOrdering)
  849. TFI.orderFrameObjects(MF, ObjectsToAllocate);
  850. // Keep track of which bytes in the fixed and callee-save range are used so we
  851. // can use the holes when allocating later stack objects. Only do this if
  852. // stack protector isn't being used and the target requests it and we're
  853. // optimizing.
  854. BitVector StackBytesFree;
  855. if (!ObjectsToAllocate.empty() &&
  856. MF.getTarget().getOptLevel() != CodeGenOpt::None &&
  857. MFI.getStackProtectorIndex() < 0 && TFI.enableStackSlotScavenging(MF))
  858. computeFreeStackSlots(MFI, StackGrowsDown, MinCSFrameIndex, MaxCSFrameIndex,
  859. FixedCSEnd, StackBytesFree);
  860. // Now walk the objects and actually assign base offsets to them.
  861. for (auto &Object : ObjectsToAllocate)
  862. if (!scavengeStackSlot(MFI, Object, StackGrowsDown, MaxAlign,
  863. StackBytesFree))
  864. AdjustStackOffset(MFI, Object, StackGrowsDown, Offset, MaxAlign, Skew);
  865. // Make sure the special register scavenging spill slot is closest to the
  866. // stack pointer.
  867. if (RS && !EarlyScavengingSlots) {
  868. SmallVector<int, 2> SFIs;
  869. RS->getScavengingFrameIndices(SFIs);
  870. for (SmallVectorImpl<int>::iterator I = SFIs.begin(),
  871. IE = SFIs.end(); I != IE; ++I)
  872. AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign, Skew);
  873. }
  874. if (!TFI.targetHandlesStackFrameRounding()) {
  875. // If we have reserved argument space for call sites in the function
  876. // immediately on entry to the current function, count it as part of the
  877. // overall stack size.
  878. if (MFI.adjustsStack() && TFI.hasReservedCallFrame(MF))
  879. Offset += MFI.getMaxCallFrameSize();
  880. // Round up the size to a multiple of the alignment. If the function has
  881. // any calls or alloca's, align to the target's StackAlignment value to
  882. // ensure that the callee's frame or the alloca data is suitably aligned;
  883. // otherwise, for leaf functions, align to the TransientStackAlignment
  884. // value.
  885. unsigned StackAlign;
  886. if (MFI.adjustsStack() || MFI.hasVarSizedObjects() ||
  887. (RegInfo->needsStackRealignment(MF) && MFI.getObjectIndexEnd() != 0))
  888. StackAlign = TFI.getStackAlignment();
  889. else
  890. StackAlign = TFI.getTransientStackAlignment();
  891. // If the frame pointer is eliminated, all frame offsets will be relative to
  892. // SP not FP. Align to MaxAlign so this works.
  893. StackAlign = std::max(StackAlign, MaxAlign);
  894. Offset = alignTo(Offset, StackAlign, Skew);
  895. }
  896. // Update frame info to pretend that this is part of the stack...
  897. int64_t StackSize = Offset - LocalAreaOffset;
  898. MFI.setStackSize(StackSize);
  899. NumBytesStackSpace += StackSize;
  900. }
  901. /// insertPrologEpilogCode - Scan the function for modified callee saved
  902. /// registers, insert spill code for these callee saved registers, then add
  903. /// prolog and epilog code to the function.
  904. void PEI::insertPrologEpilogCode(MachineFunction &MF) {
  905. const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
  906. // Add prologue to the function...
  907. for (MachineBasicBlock *SaveBlock : SaveBlocks)
  908. TFI.emitPrologue(MF, *SaveBlock);
  909. // Add epilogue to restore the callee-save registers in each exiting block.
  910. for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
  911. TFI.emitEpilogue(MF, *RestoreBlock);
  912. for (MachineBasicBlock *SaveBlock : SaveBlocks)
  913. TFI.inlineStackProbe(MF, *SaveBlock);
  914. // Emit additional code that is required to support segmented stacks, if
  915. // we've been asked for it. This, when linked with a runtime with support
  916. // for segmented stacks (libgcc is one), will result in allocating stack
  917. // space in small chunks instead of one large contiguous block.
  918. if (MF.shouldSplitStack()) {
  919. for (MachineBasicBlock *SaveBlock : SaveBlocks)
  920. TFI.adjustForSegmentedStacks(MF, *SaveBlock);
  921. // Record that there are split-stack functions, so we will emit a
  922. // special section to tell the linker.
  923. MF.getMMI().setHasSplitStack(true);
  924. } else
  925. MF.getMMI().setHasNosplitStack(true);
  926. // Emit additional code that is required to explicitly handle the stack in
  927. // HiPE native code (if needed) when loaded in the Erlang/OTP runtime. The
  928. // approach is rather similar to that of Segmented Stacks, but it uses a
  929. // different conditional check and another BIF for allocating more stack
  930. // space.
  931. if (MF.getFunction().getCallingConv() == CallingConv::HiPE)
  932. for (MachineBasicBlock *SaveBlock : SaveBlocks)
  933. TFI.adjustForHiPEPrologue(MF, *SaveBlock);
  934. }
  935. /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
  936. /// register references and actual offsets.
  937. void PEI::replaceFrameIndices(MachineFunction &MF) {
  938. const auto &ST = MF.getSubtarget();
  939. const TargetFrameLowering &TFI = *ST.getFrameLowering();
  940. if (!TFI.needsFrameIndexResolution(MF))
  941. return;
  942. const TargetRegisterInfo *TRI = ST.getRegisterInfo();
  943. // Allow the target to determine this after knowing the frame size.
  944. FrameIndexEliminationScavenging = (RS && !FrameIndexVirtualScavenging) ||
  945. TRI->requiresFrameIndexReplacementScavenging(MF);
  946. // Store SPAdj at exit of a basic block.
  947. SmallVector<int, 8> SPState;
  948. SPState.resize(MF.getNumBlockIDs());
  949. df_iterator_default_set<MachineBasicBlock*> Reachable;
  950. // Iterate over the reachable blocks in DFS order.
  951. for (auto DFI = df_ext_begin(&MF, Reachable), DFE = df_ext_end(&MF, Reachable);
  952. DFI != DFE; ++DFI) {
  953. int SPAdj = 0;
  954. // Check the exit state of the DFS stack predecessor.
  955. if (DFI.getPathLength() >= 2) {
  956. MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
  957. assert(Reachable.count(StackPred) &&
  958. "DFS stack predecessor is already visited.\n");
  959. SPAdj = SPState[StackPred->getNumber()];
  960. }
  961. MachineBasicBlock *BB = *DFI;
  962. replaceFrameIndices(BB, MF, SPAdj);
  963. SPState[BB->getNumber()] = SPAdj;
  964. }
  965. // Handle the unreachable blocks.
  966. for (auto &BB : MF) {
  967. if (Reachable.count(&BB))
  968. // Already handled in DFS traversal.
  969. continue;
  970. int SPAdj = 0;
  971. replaceFrameIndices(&BB, MF, SPAdj);
  972. }
  973. }
  974. void PEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
  975. int &SPAdj) {
  976. assert(MF.getSubtarget().getRegisterInfo() &&
  977. "getRegisterInfo() must be implemented!");
  978. const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
  979. const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
  980. const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
  981. if (RS && FrameIndexEliminationScavenging)
  982. RS->enterBasicBlock(*BB);
  983. bool InsideCallSequence = false;
  984. for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
  985. if (TII.isFrameInstr(*I)) {
  986. InsideCallSequence = TII.isFrameSetup(*I);
  987. SPAdj += TII.getSPAdjust(*I);
  988. I = TFI->eliminateCallFramePseudoInstr(MF, *BB, I);
  989. continue;
  990. }
  991. MachineInstr &MI = *I;
  992. bool DoIncr = true;
  993. bool DidFinishLoop = true;
  994. for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
  995. if (!MI.getOperand(i).isFI())
  996. continue;
  997. // Frame indices in debug values are encoded in a target independent
  998. // way with simply the frame index and offset rather than any
  999. // target-specific addressing mode.
  1000. if (MI.isDebugValue()) {
  1001. assert(i == 0 && "Frame indices can only appear as the first "
  1002. "operand of a DBG_VALUE machine instruction");
  1003. unsigned Reg;
  1004. int64_t Offset =
  1005. TFI->getFrameIndexReference(MF, MI.getOperand(0).getIndex(), Reg);
  1006. MI.getOperand(0).ChangeToRegister(Reg, false /*isDef*/);
  1007. MI.getOperand(0).setIsDebug();
  1008. auto *DIExpr = DIExpression::prepend(MI.getDebugExpression(),
  1009. DIExpression::NoDeref, Offset);
  1010. MI.getOperand(3).setMetadata(DIExpr);
  1011. continue;
  1012. }
  1013. // TODO: This code should be commoned with the code for
  1014. // PATCHPOINT. There's no good reason for the difference in
  1015. // implementation other than historical accident. The only
  1016. // remaining difference is the unconditional use of the stack
  1017. // pointer as the base register.
  1018. if (MI.getOpcode() == TargetOpcode::STATEPOINT) {
  1019. assert((!MI.isDebugValue() || i == 0) &&
  1020. "Frame indicies can only appear as the first operand of a "
  1021. "DBG_VALUE machine instruction");
  1022. unsigned Reg;
  1023. MachineOperand &Offset = MI.getOperand(i + 1);
  1024. int refOffset = TFI->getFrameIndexReferencePreferSP(
  1025. MF, MI.getOperand(i).getIndex(), Reg, /*IgnoreSPUpdates*/ false);
  1026. Offset.setImm(Offset.getImm() + refOffset + SPAdj);
  1027. MI.getOperand(i).ChangeToRegister(Reg, false /*isDef*/);
  1028. continue;
  1029. }
  1030. // Some instructions (e.g. inline asm instructions) can have
  1031. // multiple frame indices and/or cause eliminateFrameIndex
  1032. // to insert more than one instruction. We need the register
  1033. // scavenger to go through all of these instructions so that
  1034. // it can update its register information. We keep the
  1035. // iterator at the point before insertion so that we can
  1036. // revisit them in full.
  1037. bool AtBeginning = (I == BB->begin());
  1038. if (!AtBeginning) --I;
  1039. // If this instruction has a FrameIndex operand, we need to
  1040. // use that target machine register info object to eliminate
  1041. // it.
  1042. TRI.eliminateFrameIndex(MI, SPAdj, i,
  1043. FrameIndexEliminationScavenging ? RS : nullptr);
  1044. // Reset the iterator if we were at the beginning of the BB.
  1045. if (AtBeginning) {
  1046. I = BB->begin();
  1047. DoIncr = false;
  1048. }
  1049. DidFinishLoop = false;
  1050. break;
  1051. }
  1052. // If we are looking at a call sequence, we need to keep track of
  1053. // the SP adjustment made by each instruction in the sequence.
  1054. // This includes both the frame setup/destroy pseudos (handled above),
  1055. // as well as other instructions that have side effects w.r.t the SP.
  1056. // Note that this must come after eliminateFrameIndex, because
  1057. // if I itself referred to a frame index, we shouldn't count its own
  1058. // adjustment.
  1059. if (DidFinishLoop && InsideCallSequence)
  1060. SPAdj += TII.getSPAdjust(MI);
  1061. if (DoIncr && I != BB->end()) ++I;
  1062. // Update register states.
  1063. if (RS && FrameIndexEliminationScavenging && DidFinishLoop)
  1064. RS->forward(MI);
  1065. }
  1066. }