PrologEpilogInserter.cpp 47 KB

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