PrologEpilogInserter.cpp 50 KB

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