PrologEpilogInserter.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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. // This pass provides an optional shrink wrapping variant of prolog/epilog
  18. // insertion, enabled via --shrink-wrap. See ShrinkWrapping.cpp.
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #define DEBUG_TYPE "pei"
  22. #include "PrologEpilogInserter.h"
  23. #include "llvm/InlineAsm.h"
  24. #include "llvm/CodeGen/MachineDominators.h"
  25. #include "llvm/CodeGen/MachineLoopInfo.h"
  26. #include "llvm/CodeGen/MachineInstr.h"
  27. #include "llvm/CodeGen/MachineFrameInfo.h"
  28. #include "llvm/CodeGen/MachineRegisterInfo.h"
  29. #include "llvm/CodeGen/RegisterScavenging.h"
  30. #include "llvm/Target/TargetMachine.h"
  31. #include "llvm/Target/TargetOptions.h"
  32. #include "llvm/Target/TargetRegisterInfo.h"
  33. #include "llvm/Target/TargetFrameLowering.h"
  34. #include "llvm/Target/TargetInstrInfo.h"
  35. #include "llvm/Support/CommandLine.h"
  36. #include "llvm/Support/Compiler.h"
  37. #include "llvm/Support/Debug.h"
  38. #include "llvm/ADT/IndexedMap.h"
  39. #include "llvm/ADT/SmallSet.h"
  40. #include "llvm/ADT/Statistic.h"
  41. #include "llvm/ADT/STLExtras.h"
  42. #include <climits>
  43. using namespace llvm;
  44. char PEI::ID = 0;
  45. char &llvm::PrologEpilogCodeInserterID = PEI::ID;
  46. INITIALIZE_PASS_BEGIN(PEI, "prologepilog",
  47. "Prologue/Epilogue Insertion", false, false)
  48. INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
  49. INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
  50. INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
  51. INITIALIZE_PASS_END(PEI, "prologepilog",
  52. "Prologue/Epilogue Insertion & Frame Finalization",
  53. false, false)
  54. STATISTIC(NumVirtualFrameRegs, "Number of virtual frame regs encountered");
  55. STATISTIC(NumScavengedRegs, "Number of frame index regs scavenged");
  56. STATISTIC(NumBytesStackSpace,
  57. "Number of bytes used for stack in all functions");
  58. /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
  59. /// frame indexes with appropriate references.
  60. ///
  61. bool PEI::runOnMachineFunction(MachineFunction &Fn) {
  62. const Function* F = Fn.getFunction();
  63. const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
  64. const TargetFrameLowering *TFI = Fn.getTarget().getFrameLowering();
  65. assert(!Fn.getRegInfo().getNumVirtRegs() && "Regalloc must assign all vregs");
  66. RS = TRI->requiresRegisterScavenging(Fn) ? new RegScavenger() : NULL;
  67. FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(Fn);
  68. // Calculate the MaxCallFrameSize and AdjustsStack variables for the
  69. // function's frame information. Also eliminates call frame pseudo
  70. // instructions.
  71. calculateCallsInformation(Fn);
  72. // Allow the target machine to make some adjustments to the function
  73. // e.g. UsedPhysRegs before calculateCalleeSavedRegisters.
  74. TFI->processFunctionBeforeCalleeSavedScan(Fn, RS);
  75. // Scan the function for modified callee saved registers and insert spill code
  76. // for any callee saved registers that are modified.
  77. calculateCalleeSavedRegisters(Fn);
  78. // Determine placement of CSR spill/restore code:
  79. // - With shrink wrapping, place spills and restores to tightly
  80. // enclose regions in the Machine CFG of the function where
  81. // they are used.
  82. // - Without shink wrapping (default), place all spills in the
  83. // entry block, all restores in return blocks.
  84. placeCSRSpillsAndRestores(Fn);
  85. // Add the code to save and restore the callee saved registers
  86. if (!F->getFnAttributes().hasNakedAttr())
  87. insertCSRSpillsAndRestores(Fn);
  88. // Allow the target machine to make final modifications to the function
  89. // before the frame layout is finalized.
  90. TFI->processFunctionBeforeFrameFinalized(Fn);
  91. // Calculate actual frame offsets for all abstract stack objects...
  92. calculateFrameObjectOffsets(Fn);
  93. // Add prolog and epilog code to the function. This function is required
  94. // to align the stack frame as necessary for any stack variables or
  95. // called functions. Because of this, calculateCalleeSavedRegisters()
  96. // must be called before this function in order to set the AdjustsStack
  97. // and MaxCallFrameSize variables.
  98. if (!F->getFnAttributes().hasNakedAttr())
  99. insertPrologEpilogCode(Fn);
  100. // Replace all MO_FrameIndex operands with physical register references
  101. // and actual offsets.
  102. //
  103. replaceFrameIndices(Fn);
  104. // If register scavenging is needed, as we've enabled doing it as a
  105. // post-pass, scavenge the virtual registers that frame index elimiation
  106. // inserted.
  107. if (TRI->requiresRegisterScavenging(Fn) && FrameIndexVirtualScavenging)
  108. scavengeFrameVirtualRegs(Fn);
  109. // Clear any vregs created by virtual scavenging.
  110. Fn.getRegInfo().clearVirtRegs();
  111. delete RS;
  112. clearAllSets();
  113. return true;
  114. }
  115. #if 0
  116. void PEI::getAnalysisUsage(AnalysisUsage &AU) const {
  117. AU.setPreservesCFG();
  118. if (ShrinkWrapping || ShrinkWrapFunc != "") {
  119. AU.addRequired<MachineLoopInfo>();
  120. AU.addRequired<MachineDominatorTree>();
  121. }
  122. AU.addPreserved<MachineLoopInfo>();
  123. AU.addPreserved<MachineDominatorTree>();
  124. MachineFunctionPass::getAnalysisUsage(AU);
  125. }
  126. #endif
  127. /// calculateCallsInformation - Calculate the MaxCallFrameSize and AdjustsStack
  128. /// variables for the function's frame information and eliminate call frame
  129. /// pseudo instructions.
  130. void PEI::calculateCallsInformation(MachineFunction &Fn) {
  131. const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
  132. const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
  133. const TargetFrameLowering *TFI = Fn.getTarget().getFrameLowering();
  134. MachineFrameInfo *MFI = Fn.getFrameInfo();
  135. unsigned MaxCallFrameSize = 0;
  136. bool AdjustsStack = MFI->adjustsStack();
  137. // Get the function call frame set-up and tear-down instruction opcode
  138. int FrameSetupOpcode = TII.getCallFrameSetupOpcode();
  139. int FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
  140. // Early exit for targets which have no call frame setup/destroy pseudo
  141. // instructions.
  142. if (FrameSetupOpcode == -1 && FrameDestroyOpcode == -1)
  143. return;
  144. std::vector<MachineBasicBlock::iterator> FrameSDOps;
  145. for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB)
  146. for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
  147. if (I->getOpcode() == FrameSetupOpcode ||
  148. I->getOpcode() == FrameDestroyOpcode) {
  149. assert(I->getNumOperands() >= 1 && "Call Frame Setup/Destroy Pseudo"
  150. " instructions should have a single immediate argument!");
  151. unsigned Size = I->getOperand(0).getImm();
  152. if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
  153. AdjustsStack = true;
  154. FrameSDOps.push_back(I);
  155. } else if (I->isInlineAsm()) {
  156. // Some inline asm's need a stack frame, as indicated by operand 1.
  157. unsigned ExtraInfo = I->getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
  158. if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
  159. AdjustsStack = true;
  160. }
  161. MFI->setAdjustsStack(AdjustsStack);
  162. MFI->setMaxCallFrameSize(MaxCallFrameSize);
  163. for (std::vector<MachineBasicBlock::iterator>::iterator
  164. i = FrameSDOps.begin(), e = FrameSDOps.end(); i != e; ++i) {
  165. MachineBasicBlock::iterator I = *i;
  166. // If call frames are not being included as part of the stack frame, and
  167. // the target doesn't indicate otherwise, remove the call frame pseudos
  168. // here. The sub/add sp instruction pairs are still inserted, but we don't
  169. // need to track the SP adjustment for frame index elimination.
  170. if (TFI->canSimplifyCallFramePseudos(Fn))
  171. RegInfo->eliminateCallFramePseudoInstr(Fn, *I->getParent(), I);
  172. }
  173. }
  174. /// calculateCalleeSavedRegisters - Scan the function for modified callee saved
  175. /// registers.
  176. void PEI::calculateCalleeSavedRegisters(MachineFunction &Fn) {
  177. const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
  178. const TargetFrameLowering *TFI = Fn.getTarget().getFrameLowering();
  179. MachineFrameInfo *MFI = Fn.getFrameInfo();
  180. // Get the callee saved register list...
  181. const uint16_t *CSRegs = RegInfo->getCalleeSavedRegs(&Fn);
  182. // These are used to keep track the callee-save area. Initialize them.
  183. MinCSFrameIndex = INT_MAX;
  184. MaxCSFrameIndex = 0;
  185. // Early exit for targets which have no callee saved registers.
  186. if (CSRegs == 0 || CSRegs[0] == 0)
  187. return;
  188. // In Naked functions we aren't going to save any registers.
  189. if (Fn.getFunction()->getFnAttributes().hasNakedAttr())
  190. return;
  191. std::vector<CalleeSavedInfo> CSI;
  192. for (unsigned i = 0; CSRegs[i]; ++i) {
  193. unsigned Reg = CSRegs[i];
  194. if (Fn.getRegInfo().isPhysRegOrOverlapUsed(Reg)) {
  195. // If the reg is modified, save it!
  196. CSI.push_back(CalleeSavedInfo(Reg));
  197. }
  198. }
  199. if (CSI.empty())
  200. return; // Early exit if no callee saved registers are modified!
  201. unsigned NumFixedSpillSlots;
  202. const TargetFrameLowering::SpillSlot *FixedSpillSlots =
  203. TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots);
  204. // Now that we know which registers need to be saved and restored, allocate
  205. // stack slots for them.
  206. for (std::vector<CalleeSavedInfo>::iterator
  207. I = CSI.begin(), E = CSI.end(); I != E; ++I) {
  208. unsigned Reg = I->getReg();
  209. const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg);
  210. int FrameIdx;
  211. if (RegInfo->hasReservedSpillSlot(Fn, Reg, FrameIdx)) {
  212. I->setFrameIdx(FrameIdx);
  213. continue;
  214. }
  215. // Check to see if this physreg must be spilled to a particular stack slot
  216. // on this target.
  217. const TargetFrameLowering::SpillSlot *FixedSlot = FixedSpillSlots;
  218. while (FixedSlot != FixedSpillSlots+NumFixedSpillSlots &&
  219. FixedSlot->Reg != Reg)
  220. ++FixedSlot;
  221. if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) {
  222. // Nope, just spill it anywhere convenient.
  223. unsigned Align = RC->getAlignment();
  224. unsigned StackAlign = TFI->getStackAlignment();
  225. // We may not be able to satisfy the desired alignment specification of
  226. // the TargetRegisterClass if the stack alignment is smaller. Use the
  227. // min.
  228. Align = std::min(Align, StackAlign);
  229. FrameIdx = MFI->CreateStackObject(RC->getSize(), Align, true);
  230. if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx;
  231. if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx;
  232. } else {
  233. // Spill it to the stack where we must.
  234. FrameIdx = MFI->CreateFixedObject(RC->getSize(), FixedSlot->Offset, true);
  235. }
  236. I->setFrameIdx(FrameIdx);
  237. }
  238. MFI->setCalleeSavedInfo(CSI);
  239. }
  240. /// insertCSRSpillsAndRestores - Insert spill and restore code for
  241. /// callee saved registers used in the function, handling shrink wrapping.
  242. ///
  243. void PEI::insertCSRSpillsAndRestores(MachineFunction &Fn) {
  244. // Get callee saved register information.
  245. MachineFrameInfo *MFI = Fn.getFrameInfo();
  246. const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
  247. MFI->setCalleeSavedInfoValid(true);
  248. // Early exit if no callee saved registers are modified!
  249. if (CSI.empty())
  250. return;
  251. const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
  252. const TargetFrameLowering *TFI = Fn.getTarget().getFrameLowering();
  253. const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
  254. MachineBasicBlock::iterator I;
  255. if (!ShrinkWrapThisFunction) {
  256. // Spill using target interface.
  257. I = EntryBlock->begin();
  258. if (!TFI->spillCalleeSavedRegisters(*EntryBlock, I, CSI, TRI)) {
  259. for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
  260. // Add the callee-saved register as live-in.
  261. // It's killed at the spill.
  262. EntryBlock->addLiveIn(CSI[i].getReg());
  263. // Insert the spill to the stack frame.
  264. unsigned Reg = CSI[i].getReg();
  265. const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
  266. TII.storeRegToStackSlot(*EntryBlock, I, Reg, true,
  267. CSI[i].getFrameIdx(), RC, TRI);
  268. }
  269. }
  270. // Restore using target interface.
  271. for (unsigned ri = 0, re = ReturnBlocks.size(); ri != re; ++ri) {
  272. MachineBasicBlock* MBB = ReturnBlocks[ri];
  273. I = MBB->end(); --I;
  274. // Skip over all terminator instructions, which are part of the return
  275. // sequence.
  276. MachineBasicBlock::iterator I2 = I;
  277. while (I2 != MBB->begin() && (--I2)->isTerminator())
  278. I = I2;
  279. bool AtStart = I == MBB->begin();
  280. MachineBasicBlock::iterator BeforeI = I;
  281. if (!AtStart)
  282. --BeforeI;
  283. // Restore all registers immediately before the return and any
  284. // terminators that precede it.
  285. if (!TFI->restoreCalleeSavedRegisters(*MBB, I, CSI, TRI)) {
  286. for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
  287. unsigned Reg = CSI[i].getReg();
  288. const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
  289. TII.loadRegFromStackSlot(*MBB, I, Reg,
  290. CSI[i].getFrameIdx(),
  291. RC, TRI);
  292. assert(I != MBB->begin() &&
  293. "loadRegFromStackSlot didn't insert any code!");
  294. // Insert in reverse order. loadRegFromStackSlot can insert
  295. // multiple instructions.
  296. if (AtStart)
  297. I = MBB->begin();
  298. else {
  299. I = BeforeI;
  300. ++I;
  301. }
  302. }
  303. }
  304. }
  305. return;
  306. }
  307. // Insert spills.
  308. std::vector<CalleeSavedInfo> blockCSI;
  309. for (CSRegBlockMap::iterator BI = CSRSave.begin(),
  310. BE = CSRSave.end(); BI != BE; ++BI) {
  311. MachineBasicBlock* MBB = BI->first;
  312. CSRegSet save = BI->second;
  313. if (save.empty())
  314. continue;
  315. blockCSI.clear();
  316. for (CSRegSet::iterator RI = save.begin(),
  317. RE = save.end(); RI != RE; ++RI) {
  318. blockCSI.push_back(CSI[*RI]);
  319. }
  320. assert(blockCSI.size() > 0 &&
  321. "Could not collect callee saved register info");
  322. I = MBB->begin();
  323. // When shrink wrapping, use stack slot stores/loads.
  324. for (unsigned i = 0, e = blockCSI.size(); i != e; ++i) {
  325. // Add the callee-saved register as live-in.
  326. // It's killed at the spill.
  327. MBB->addLiveIn(blockCSI[i].getReg());
  328. // Insert the spill to the stack frame.
  329. unsigned Reg = blockCSI[i].getReg();
  330. const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
  331. TII.storeRegToStackSlot(*MBB, I, Reg,
  332. true,
  333. blockCSI[i].getFrameIdx(),
  334. RC, TRI);
  335. }
  336. }
  337. for (CSRegBlockMap::iterator BI = CSRRestore.begin(),
  338. BE = CSRRestore.end(); BI != BE; ++BI) {
  339. MachineBasicBlock* MBB = BI->first;
  340. CSRegSet restore = BI->second;
  341. if (restore.empty())
  342. continue;
  343. blockCSI.clear();
  344. for (CSRegSet::iterator RI = restore.begin(),
  345. RE = restore.end(); RI != RE; ++RI) {
  346. blockCSI.push_back(CSI[*RI]);
  347. }
  348. assert(blockCSI.size() > 0 &&
  349. "Could not find callee saved register info");
  350. // If MBB is empty and needs restores, insert at the _beginning_.
  351. if (MBB->empty()) {
  352. I = MBB->begin();
  353. } else {
  354. I = MBB->end();
  355. --I;
  356. // Skip over all terminator instructions, which are part of the
  357. // return sequence.
  358. if (! I->isTerminator()) {
  359. ++I;
  360. } else {
  361. MachineBasicBlock::iterator I2 = I;
  362. while (I2 != MBB->begin() && (--I2)->isTerminator())
  363. I = I2;
  364. }
  365. }
  366. bool AtStart = I == MBB->begin();
  367. MachineBasicBlock::iterator BeforeI = I;
  368. if (!AtStart)
  369. --BeforeI;
  370. // Restore all registers immediately before the return and any
  371. // terminators that precede it.
  372. for (unsigned i = 0, e = blockCSI.size(); i != e; ++i) {
  373. unsigned Reg = blockCSI[i].getReg();
  374. const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
  375. TII.loadRegFromStackSlot(*MBB, I, Reg,
  376. blockCSI[i].getFrameIdx(),
  377. RC, TRI);
  378. assert(I != MBB->begin() &&
  379. "loadRegFromStackSlot didn't insert any code!");
  380. // Insert in reverse order. loadRegFromStackSlot can insert
  381. // multiple instructions.
  382. if (AtStart)
  383. I = MBB->begin();
  384. else {
  385. I = BeforeI;
  386. ++I;
  387. }
  388. }
  389. }
  390. }
  391. /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
  392. static inline void
  393. AdjustStackOffset(MachineFrameInfo *MFI, int FrameIdx,
  394. bool StackGrowsDown, int64_t &Offset,
  395. unsigned &MaxAlign) {
  396. // If the stack grows down, add the object size to find the lowest address.
  397. if (StackGrowsDown)
  398. Offset += MFI->getObjectSize(FrameIdx);
  399. unsigned Align = MFI->getObjectAlignment(FrameIdx);
  400. // If the alignment of this object is greater than that of the stack, then
  401. // increase the stack alignment to match.
  402. MaxAlign = std::max(MaxAlign, Align);
  403. // Adjust to alignment boundary.
  404. Offset = (Offset + Align - 1) / Align * Align;
  405. if (StackGrowsDown) {
  406. DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset << "]\n");
  407. MFI->setObjectOffset(FrameIdx, -Offset); // Set the computed offset
  408. } else {
  409. DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset << "]\n");
  410. MFI->setObjectOffset(FrameIdx, Offset);
  411. Offset += MFI->getObjectSize(FrameIdx);
  412. }
  413. }
  414. /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
  415. /// abstract stack objects.
  416. ///
  417. void PEI::calculateFrameObjectOffsets(MachineFunction &Fn) {
  418. const TargetFrameLowering &TFI = *Fn.getTarget().getFrameLowering();
  419. bool StackGrowsDown =
  420. TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
  421. // Loop over all of the stack objects, assigning sequential addresses...
  422. MachineFrameInfo *MFI = Fn.getFrameInfo();
  423. // Start at the beginning of the local area.
  424. // The Offset is the distance from the stack top in the direction
  425. // of stack growth -- so it's always nonnegative.
  426. int LocalAreaOffset = TFI.getOffsetOfLocalArea();
  427. if (StackGrowsDown)
  428. LocalAreaOffset = -LocalAreaOffset;
  429. assert(LocalAreaOffset >= 0
  430. && "Local area offset should be in direction of stack growth");
  431. int64_t Offset = LocalAreaOffset;
  432. // If there are fixed sized objects that are preallocated in the local area,
  433. // non-fixed objects can't be allocated right at the start of local area.
  434. // We currently don't support filling in holes in between fixed sized
  435. // objects, so we adjust 'Offset' to point to the end of last fixed sized
  436. // preallocated object.
  437. for (int i = MFI->getObjectIndexBegin(); i != 0; ++i) {
  438. int64_t FixedOff;
  439. if (StackGrowsDown) {
  440. // The maximum distance from the stack pointer is at lower address of
  441. // the object -- which is given by offset. For down growing stack
  442. // the offset is negative, so we negate the offset to get the distance.
  443. FixedOff = -MFI->getObjectOffset(i);
  444. } else {
  445. // The maximum distance from the start pointer is at the upper
  446. // address of the object.
  447. FixedOff = MFI->getObjectOffset(i) + MFI->getObjectSize(i);
  448. }
  449. if (FixedOff > Offset) Offset = FixedOff;
  450. }
  451. // First assign frame offsets to stack objects that are used to spill
  452. // callee saved registers.
  453. if (StackGrowsDown) {
  454. for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) {
  455. // If the stack grows down, we need to add the size to find the lowest
  456. // address of the object.
  457. Offset += MFI->getObjectSize(i);
  458. unsigned Align = MFI->getObjectAlignment(i);
  459. // Adjust to alignment boundary
  460. Offset = (Offset+Align-1)/Align*Align;
  461. MFI->setObjectOffset(i, -Offset); // Set the computed offset
  462. }
  463. } else {
  464. int MaxCSFI = MaxCSFrameIndex, MinCSFI = MinCSFrameIndex;
  465. for (int i = MaxCSFI; i >= MinCSFI ; --i) {
  466. unsigned Align = MFI->getObjectAlignment(i);
  467. // Adjust to alignment boundary
  468. Offset = (Offset+Align-1)/Align*Align;
  469. MFI->setObjectOffset(i, Offset);
  470. Offset += MFI->getObjectSize(i);
  471. }
  472. }
  473. unsigned MaxAlign = MFI->getMaxAlignment();
  474. // Make sure the special register scavenging spill slot is closest to the
  475. // frame pointer if a frame pointer is required.
  476. const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
  477. if (RS && TFI.hasFP(Fn) && RegInfo->useFPForScavengingIndex(Fn) &&
  478. !RegInfo->needsStackRealignment(Fn)) {
  479. int SFI = RS->getScavengingFrameIndex();
  480. if (SFI >= 0)
  481. AdjustStackOffset(MFI, SFI, StackGrowsDown, Offset, MaxAlign);
  482. }
  483. // FIXME: Once this is working, then enable flag will change to a target
  484. // check for whether the frame is large enough to want to use virtual
  485. // frame index registers. Functions which don't want/need this optimization
  486. // will continue to use the existing code path.
  487. if (MFI->getUseLocalStackAllocationBlock()) {
  488. unsigned Align = MFI->getLocalFrameMaxAlign();
  489. // Adjust to alignment boundary.
  490. Offset = (Offset + Align - 1) / Align * Align;
  491. DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
  492. // Resolve offsets for objects in the local block.
  493. for (unsigned i = 0, e = MFI->getLocalFrameObjectCount(); i != e; ++i) {
  494. std::pair<int, int64_t> Entry = MFI->getLocalFrameObjectMap(i);
  495. int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
  496. DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" <<
  497. FIOffset << "]\n");
  498. MFI->setObjectOffset(Entry.first, FIOffset);
  499. }
  500. // Allocate the local block
  501. Offset += MFI->getLocalFrameSize();
  502. MaxAlign = std::max(Align, MaxAlign);
  503. }
  504. // Make sure that the stack protector comes before the local variables on the
  505. // stack.
  506. SmallSet<int, 16> LargeStackObjs;
  507. if (MFI->getStackProtectorIndex() >= 0) {
  508. AdjustStackOffset(MFI, MFI->getStackProtectorIndex(), StackGrowsDown,
  509. Offset, MaxAlign);
  510. // Assign large stack objects first.
  511. for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
  512. if (MFI->isObjectPreAllocated(i) &&
  513. MFI->getUseLocalStackAllocationBlock())
  514. continue;
  515. if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
  516. continue;
  517. if (RS && (int)i == RS->getScavengingFrameIndex())
  518. continue;
  519. if (MFI->isDeadObjectIndex(i))
  520. continue;
  521. if (MFI->getStackProtectorIndex() == (int)i)
  522. continue;
  523. if (!MFI->MayNeedStackProtector(i))
  524. continue;
  525. AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign);
  526. LargeStackObjs.insert(i);
  527. }
  528. }
  529. // Then assign frame offsets to stack objects that are not used to spill
  530. // callee saved registers.
  531. for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
  532. if (MFI->isObjectPreAllocated(i) &&
  533. MFI->getUseLocalStackAllocationBlock())
  534. continue;
  535. if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
  536. continue;
  537. if (RS && (int)i == RS->getScavengingFrameIndex())
  538. continue;
  539. if (MFI->isDeadObjectIndex(i))
  540. continue;
  541. if (MFI->getStackProtectorIndex() == (int)i)
  542. continue;
  543. if (LargeStackObjs.count(i))
  544. continue;
  545. AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign);
  546. }
  547. // Make sure the special register scavenging spill slot is closest to the
  548. // stack pointer.
  549. if (RS && (!TFI.hasFP(Fn) || RegInfo->needsStackRealignment(Fn) ||
  550. !RegInfo->useFPForScavengingIndex(Fn))) {
  551. int SFI = RS->getScavengingFrameIndex();
  552. if (SFI >= 0)
  553. AdjustStackOffset(MFI, SFI, StackGrowsDown, Offset, MaxAlign);
  554. }
  555. if (!TFI.targetHandlesStackFrameRounding()) {
  556. // If we have reserved argument space for call sites in the function
  557. // immediately on entry to the current function, count it as part of the
  558. // overall stack size.
  559. if (MFI->adjustsStack() && TFI.hasReservedCallFrame(Fn))
  560. Offset += MFI->getMaxCallFrameSize();
  561. // Round up the size to a multiple of the alignment. If the function has
  562. // any calls or alloca's, align to the target's StackAlignment value to
  563. // ensure that the callee's frame or the alloca data is suitably aligned;
  564. // otherwise, for leaf functions, align to the TransientStackAlignment
  565. // value.
  566. unsigned StackAlign;
  567. if (MFI->adjustsStack() || MFI->hasVarSizedObjects() ||
  568. (RegInfo->needsStackRealignment(Fn) && MFI->getObjectIndexEnd() != 0))
  569. StackAlign = TFI.getStackAlignment();
  570. else
  571. StackAlign = TFI.getTransientStackAlignment();
  572. // If the frame pointer is eliminated, all frame offsets will be relative to
  573. // SP not FP. Align to MaxAlign so this works.
  574. StackAlign = std::max(StackAlign, MaxAlign);
  575. unsigned AlignMask = StackAlign - 1;
  576. Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
  577. }
  578. // Update frame info to pretend that this is part of the stack...
  579. int64_t StackSize = Offset - LocalAreaOffset;
  580. MFI->setStackSize(StackSize);
  581. NumBytesStackSpace += StackSize;
  582. }
  583. /// insertPrologEpilogCode - Scan the function for modified callee saved
  584. /// registers, insert spill code for these callee saved registers, then add
  585. /// prolog and epilog code to the function.
  586. ///
  587. void PEI::insertPrologEpilogCode(MachineFunction &Fn) {
  588. const TargetFrameLowering &TFI = *Fn.getTarget().getFrameLowering();
  589. // Add prologue to the function...
  590. TFI.emitPrologue(Fn);
  591. // Add epilogue to restore the callee-save registers in each exiting block
  592. for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
  593. // If last instruction is a return instruction, add an epilogue
  594. if (!I->empty() && I->back().isReturn())
  595. TFI.emitEpilogue(Fn, *I);
  596. }
  597. // Emit additional code that is required to support segmented stacks, if
  598. // we've been asked for it. This, when linked with a runtime with support
  599. // for segmented stacks (libgcc is one), will result in allocating stack
  600. // space in small chunks instead of one large contiguous block.
  601. if (Fn.getTarget().Options.EnableSegmentedStacks)
  602. TFI.adjustForSegmentedStacks(Fn);
  603. }
  604. /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
  605. /// register references and actual offsets.
  606. ///
  607. void PEI::replaceFrameIndices(MachineFunction &Fn) {
  608. if (!Fn.getFrameInfo()->hasStackObjects()) return; // Nothing to do?
  609. const TargetMachine &TM = Fn.getTarget();
  610. assert(TM.getRegisterInfo() && "TM::getRegisterInfo() must be implemented!");
  611. const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
  612. const TargetRegisterInfo &TRI = *TM.getRegisterInfo();
  613. const TargetFrameLowering *TFI = TM.getFrameLowering();
  614. bool StackGrowsDown =
  615. TFI->getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
  616. int FrameSetupOpcode = TII.getCallFrameSetupOpcode();
  617. int FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
  618. for (MachineFunction::iterator BB = Fn.begin(),
  619. E = Fn.end(); BB != E; ++BB) {
  620. #ifndef NDEBUG
  621. int SPAdjCount = 0; // frame setup / destroy count.
  622. #endif
  623. int SPAdj = 0; // SP offset due to call frame setup / destroy.
  624. if (RS && !FrameIndexVirtualScavenging) RS->enterBasicBlock(BB);
  625. for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
  626. if (I->getOpcode() == FrameSetupOpcode ||
  627. I->getOpcode() == FrameDestroyOpcode) {
  628. #ifndef NDEBUG
  629. // Track whether we see even pairs of them
  630. SPAdjCount += I->getOpcode() == FrameSetupOpcode ? 1 : -1;
  631. #endif
  632. // Remember how much SP has been adjusted to create the call
  633. // frame.
  634. int Size = I->getOperand(0).getImm();
  635. if ((!StackGrowsDown && I->getOpcode() == FrameSetupOpcode) ||
  636. (StackGrowsDown && I->getOpcode() == FrameDestroyOpcode))
  637. Size = -Size;
  638. SPAdj += Size;
  639. MachineBasicBlock::iterator PrevI = BB->end();
  640. if (I != BB->begin()) PrevI = prior(I);
  641. TRI.eliminateCallFramePseudoInstr(Fn, *BB, I);
  642. // Visit the instructions created by eliminateCallFramePseudoInstr().
  643. if (PrevI == BB->end())
  644. I = BB->begin(); // The replaced instr was the first in the block.
  645. else
  646. I = llvm::next(PrevI);
  647. continue;
  648. }
  649. MachineInstr *MI = I;
  650. bool DoIncr = true;
  651. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
  652. if (MI->getOperand(i).isFI()) {
  653. // Some instructions (e.g. inline asm instructions) can have
  654. // multiple frame indices and/or cause eliminateFrameIndex
  655. // to insert more than one instruction. We need the register
  656. // scavenger to go through all of these instructions so that
  657. // it can update its register information. We keep the
  658. // iterator at the point before insertion so that we can
  659. // revisit them in full.
  660. bool AtBeginning = (I == BB->begin());
  661. if (!AtBeginning) --I;
  662. // If this instruction has a FrameIndex operand, we need to
  663. // use that target machine register info object to eliminate
  664. // it.
  665. TRI.eliminateFrameIndex(MI, SPAdj,
  666. FrameIndexVirtualScavenging ? NULL : RS);
  667. // Reset the iterator if we were at the beginning of the BB.
  668. if (AtBeginning) {
  669. I = BB->begin();
  670. DoIncr = false;
  671. }
  672. MI = 0;
  673. break;
  674. }
  675. if (DoIncr && I != BB->end()) ++I;
  676. // Update register states.
  677. if (RS && !FrameIndexVirtualScavenging && MI) RS->forward(MI);
  678. }
  679. // If we have evenly matched pairs of frame setup / destroy instructions,
  680. // make sure the adjustments come out to zero. If we don't have matched
  681. // pairs, we can't be sure the missing bit isn't in another basic block
  682. // due to a custom inserter playing tricks, so just asserting SPAdj==0
  683. // isn't sufficient. See tMOVCC on Thumb1, for example.
  684. assert((SPAdjCount || SPAdj == 0) &&
  685. "Unbalanced call frame setup / destroy pairs?");
  686. }
  687. }
  688. /// scavengeFrameVirtualRegs - Replace all frame index virtual registers
  689. /// with physical registers. Use the register scavenger to find an
  690. /// appropriate register to use.
  691. ///
  692. /// FIXME: Iterating over the instruction stream is unnecessary. We can simply
  693. /// iterate over the vreg use list, which at this point only contains machine
  694. /// operands for which eliminateFrameIndex need a new scratch reg.
  695. void PEI::scavengeFrameVirtualRegs(MachineFunction &Fn) {
  696. // Run through the instructions and find any virtual registers.
  697. for (MachineFunction::iterator BB = Fn.begin(),
  698. E = Fn.end(); BB != E; ++BB) {
  699. RS->enterBasicBlock(BB);
  700. unsigned VirtReg = 0;
  701. unsigned ScratchReg = 0;
  702. int SPAdj = 0;
  703. // The instruction stream may change in the loop, so check BB->end()
  704. // directly.
  705. for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
  706. MachineInstr *MI = I;
  707. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  708. if (MI->getOperand(i).isReg()) {
  709. MachineOperand &MO = MI->getOperand(i);
  710. unsigned Reg = MO.getReg();
  711. if (Reg == 0)
  712. continue;
  713. if (!TargetRegisterInfo::isVirtualRegister(Reg))
  714. continue;
  715. ++NumVirtualFrameRegs;
  716. // Have we already allocated a scratch register for this virtual?
  717. if (Reg != VirtReg) {
  718. // When we first encounter a new virtual register, it
  719. // must be a definition.
  720. assert(MI->getOperand(i).isDef() &&
  721. "frame index virtual missing def!");
  722. // Scavenge a new scratch register
  723. VirtReg = Reg;
  724. const TargetRegisterClass *RC = Fn.getRegInfo().getRegClass(Reg);
  725. ScratchReg = RS->scavengeRegister(RC, I, SPAdj);
  726. ++NumScavengedRegs;
  727. }
  728. // Replace this reference to the virtual register with the
  729. // scratch register.
  730. assert (ScratchReg && "Missing scratch register!");
  731. MI->getOperand(i).setReg(ScratchReg);
  732. }
  733. }
  734. RS->forward(I);
  735. ++I;
  736. }
  737. }
  738. }