PrologEpilogInserter.cpp 43 KB

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