PrologEpilogInserter.cpp 43 KB

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