PrologEpilogInserter.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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. #include "PrologEpilogInserter.h"
  22. #include "llvm/CodeGen/MachineDominators.h"
  23. #include "llvm/CodeGen/MachineLoopInfo.h"
  24. #include "llvm/CodeGen/MachineInstr.h"
  25. #include "llvm/CodeGen/MachineFrameInfo.h"
  26. #include "llvm/CodeGen/MachineModuleInfo.h"
  27. #include "llvm/CodeGen/MachineRegisterInfo.h"
  28. #include "llvm/CodeGen/RegisterScavenging.h"
  29. #include "llvm/Target/TargetMachine.h"
  30. #include "llvm/Target/TargetRegisterInfo.h"
  31. #include "llvm/Target/TargetFrameInfo.h"
  32. #include "llvm/Target/TargetInstrInfo.h"
  33. #include "llvm/Support/Compiler.h"
  34. #include "llvm/ADT/STLExtras.h"
  35. #include <climits>
  36. using namespace llvm;
  37. char PEI::ID = 0;
  38. static RegisterPass<PEI>
  39. X("prologepilog", "Prologue/Epilogue Insertion");
  40. /// createPrologEpilogCodeInserter - This function returns a pass that inserts
  41. /// prolog and epilog code, and eliminates abstract frame references.
  42. ///
  43. FunctionPass *llvm::createPrologEpilogCodeInserter() { return new PEI(); }
  44. /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
  45. /// frame indexes with appropriate references.
  46. ///
  47. bool PEI::runOnMachineFunction(MachineFunction &Fn) {
  48. const Function* F = Fn.getFunction();
  49. const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
  50. RS = TRI->requiresRegisterScavenging(Fn) ? new RegScavenger() : NULL;
  51. // Get MachineModuleInfo so that we can track the construction of the
  52. // frame.
  53. if (MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>())
  54. Fn.getFrameInfo()->setMachineModuleInfo(MMI);
  55. // Calculate the MaxCallFrameSize and HasCalls variables for the function's
  56. // frame information. Also eliminates call frame pseudo instructions.
  57. calculateCallsInformation(Fn);
  58. // Allow the target machine to make some adjustments to the function
  59. // e.g. UsedPhysRegs before calculateCalleeSavedRegisters.
  60. TRI->processFunctionBeforeCalleeSavedScan(Fn, RS);
  61. // Scan the function for modified callee saved registers and insert spill code
  62. // for any callee saved registers that are modified.
  63. calculateCalleeSavedRegisters(Fn);
  64. // Determine placement of CSR spill/restore code:
  65. // - with shrink wrapping, place spills and restores to tightly
  66. // enclose regions in the Machine CFG of the function where
  67. // they are used. Without shrink wrapping
  68. // - default (no shrink wrapping), place all spills in the
  69. // entry block, all restores in return blocks.
  70. placeCSRSpillsAndRestores(Fn);
  71. // Add the code to save and restore the callee saved registers
  72. if (!F->hasFnAttr(Attribute::Naked))
  73. insertCSRSpillsAndRestores(Fn);
  74. // Allow the target machine to make final modifications to the function
  75. // before the frame layout is finalized.
  76. TRI->processFunctionBeforeFrameFinalized(Fn);
  77. // Calculate actual frame offsets for all abstract stack objects...
  78. calculateFrameObjectOffsets(Fn);
  79. // Add prolog and epilog code to the function. This function is required
  80. // to align the stack frame as necessary for any stack variables or
  81. // called functions. Because of this, calculateCalleeSavedRegisters
  82. // must be called before this function in order to set the HasCalls
  83. // and MaxCallFrameSize variables.
  84. if (!F->hasFnAttr(Attribute::Naked))
  85. insertPrologEpilogCode(Fn);
  86. // Replace all MO_FrameIndex operands with physical register references
  87. // and actual offsets.
  88. //
  89. replaceFrameIndices(Fn);
  90. delete RS;
  91. clearAllSets();
  92. return true;
  93. }
  94. #if 0
  95. void PEI::getAnalysisUsage(AnalysisUsage &AU) const {
  96. if (ShrinkWrapping || ShrinkWrapFunc != "") {
  97. AU.addRequired<MachineLoopInfo>();
  98. AU.addRequired<MachineDominatorTree>();
  99. }
  100. AU.addPreserved<MachineLoopInfo>();
  101. AU.addPreserved<MachineDominatorTree>();
  102. MachineFunctionPass::getAnalysisUsage(AU);
  103. }
  104. #endif
  105. /// calculateCallsInformation - Calculate the MaxCallFrameSize and HasCalls
  106. /// variables for the function's frame information and eliminate call frame
  107. /// pseudo instructions.
  108. void PEI::calculateCallsInformation(MachineFunction &Fn) {
  109. const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
  110. unsigned MaxCallFrameSize = 0;
  111. bool HasCalls = false;
  112. // Get the function call frame set-up and tear-down instruction opcode
  113. int FrameSetupOpcode = RegInfo->getCallFrameSetupOpcode();
  114. int FrameDestroyOpcode = RegInfo->getCallFrameDestroyOpcode();
  115. // Early exit for targets which have no call frame setup/destroy pseudo
  116. // instructions.
  117. if (FrameSetupOpcode == -1 && FrameDestroyOpcode == -1)
  118. return;
  119. std::vector<MachineBasicBlock::iterator> FrameSDOps;
  120. for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB)
  121. for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
  122. if (I->getOpcode() == FrameSetupOpcode ||
  123. I->getOpcode() == FrameDestroyOpcode) {
  124. assert(I->getNumOperands() >= 1 && "Call Frame Setup/Destroy Pseudo"
  125. " instructions should have a single immediate argument!");
  126. unsigned Size = I->getOperand(0).getImm();
  127. if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
  128. HasCalls = true;
  129. FrameSDOps.push_back(I);
  130. } else if (I->getOpcode() == TargetInstrInfo::INLINEASM) {
  131. // An InlineAsm might be a call; assume it is to get the stack frame
  132. // aligned correctly for calls.
  133. HasCalls = true;
  134. }
  135. MachineFrameInfo *FFI = Fn.getFrameInfo();
  136. FFI->setHasCalls(HasCalls);
  137. FFI->setMaxCallFrameSize(MaxCallFrameSize);
  138. for (std::vector<MachineBasicBlock::iterator>::iterator
  139. i = FrameSDOps.begin(), e = FrameSDOps.end(); i != e; ++i) {
  140. MachineBasicBlock::iterator I = *i;
  141. // If call frames are not being included as part of the stack frame, and
  142. // there is no dynamic allocation (therefore referencing frame slots off
  143. // sp), leave the pseudo ops alone. We'll eliminate them later.
  144. if (RegInfo->hasReservedCallFrame(Fn) || RegInfo->hasFP(Fn))
  145. RegInfo->eliminateCallFramePseudoInstr(Fn, *I->getParent(), I);
  146. }
  147. }
  148. /// calculateCalleeSavedRegisters - Scan the function for modified callee saved
  149. /// registers.
  150. void PEI::calculateCalleeSavedRegisters(MachineFunction &Fn) {
  151. const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
  152. const TargetFrameInfo *TFI = Fn.getTarget().getFrameInfo();
  153. MachineFrameInfo *FFI = Fn.getFrameInfo();
  154. // Get the callee saved register list...
  155. const unsigned *CSRegs = RegInfo->getCalleeSavedRegs(&Fn);
  156. // These are used to keep track the callee-save area. Initialize them.
  157. MinCSFrameIndex = INT_MAX;
  158. MaxCSFrameIndex = 0;
  159. // Early exit for targets which have no callee saved registers.
  160. if (CSRegs == 0 || CSRegs[0] == 0)
  161. return;
  162. // Figure out which *callee saved* registers are modified by the current
  163. // function, thus needing to be saved and restored in the prolog/epilog.
  164. const TargetRegisterClass * const *CSRegClasses =
  165. RegInfo->getCalleeSavedRegClasses(&Fn);
  166. std::vector<CalleeSavedInfo> CSI;
  167. for (unsigned i = 0; CSRegs[i]; ++i) {
  168. unsigned Reg = CSRegs[i];
  169. if (Fn.getRegInfo().isPhysRegUsed(Reg)) {
  170. // If the reg is modified, save it!
  171. CSI.push_back(CalleeSavedInfo(Reg, CSRegClasses[i]));
  172. } else {
  173. for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
  174. *AliasSet; ++AliasSet) { // Check alias registers too.
  175. if (Fn.getRegInfo().isPhysRegUsed(*AliasSet)) {
  176. CSI.push_back(CalleeSavedInfo(Reg, CSRegClasses[i]));
  177. break;
  178. }
  179. }
  180. }
  181. }
  182. if (CSI.empty())
  183. return; // Early exit if no callee saved registers are modified!
  184. unsigned NumFixedSpillSlots;
  185. const std::pair<unsigned,int> *FixedSpillSlots =
  186. TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots);
  187. // Now that we know which registers need to be saved and restored, allocate
  188. // stack slots for them.
  189. for (std::vector<CalleeSavedInfo>::iterator
  190. I = CSI.begin(), E = CSI.end(); I != E; ++I) {
  191. unsigned Reg = I->getReg();
  192. const TargetRegisterClass *RC = I->getRegClass();
  193. int FrameIdx;
  194. if (RegInfo->hasReservedSpillSlot(Fn, Reg, FrameIdx)) {
  195. I->setFrameIdx(FrameIdx);
  196. continue;
  197. }
  198. // Check to see if this physreg must be spilled to a particular stack slot
  199. // on this target.
  200. const std::pair<unsigned,int> *FixedSlot = FixedSpillSlots;
  201. while (FixedSlot != FixedSpillSlots+NumFixedSpillSlots &&
  202. FixedSlot->first != Reg)
  203. ++FixedSlot;
  204. if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) {
  205. // Nope, just spill it anywhere convenient.
  206. unsigned Align = RC->getAlignment();
  207. unsigned StackAlign = TFI->getStackAlignment();
  208. // We may not be able to satisfy the desired alignment specification of
  209. // the TargetRegisterClass if the stack alignment is smaller. Use the
  210. // min.
  211. Align = std::min(Align, StackAlign);
  212. FrameIdx = FFI->CreateStackObject(RC->getSize(), Align);
  213. if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx;
  214. if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx;
  215. } else {
  216. // Spill it to the stack where we must.
  217. FrameIdx = FFI->CreateFixedObject(RC->getSize(), FixedSlot->second);
  218. }
  219. I->setFrameIdx(FrameIdx);
  220. }
  221. FFI->setCalleeSavedInfo(CSI);
  222. }
  223. /// insertCSRSpillsAndRestores - Insert spill and restore code for
  224. /// callee saved registers used in the function, handling shrink wrapping.
  225. ///
  226. void PEI::insertCSRSpillsAndRestores(MachineFunction &Fn) {
  227. // Get callee saved register information.
  228. MachineFrameInfo *FFI = Fn.getFrameInfo();
  229. const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
  230. // Early exit if no callee saved registers are modified!
  231. if (CSI.empty())
  232. return;
  233. const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
  234. MachineBasicBlock::iterator I;
  235. if (! ShrinkWrapThisFunction) {
  236. // Spill using target interface.
  237. I = EntryBlock->begin();
  238. if (!TII.spillCalleeSavedRegisters(*EntryBlock, I, CSI)) {
  239. for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
  240. // Add the callee-saved register as live-in.
  241. // It's killed at the spill.
  242. EntryBlock->addLiveIn(CSI[i].getReg());
  243. // Insert the spill to the stack frame.
  244. TII.storeRegToStackSlot(*EntryBlock, I, CSI[i].getReg(), true,
  245. CSI[i].getFrameIdx(), CSI[i].getRegClass());
  246. }
  247. }
  248. // Restore using target interface.
  249. for (unsigned ri = 0, re = ReturnBlocks.size(); ri != re; ++ri) {
  250. MachineBasicBlock* MBB = ReturnBlocks[ri];
  251. I = MBB->end(); --I;
  252. // Skip over all terminator instructions, which are part of the return
  253. // sequence.
  254. MachineBasicBlock::iterator I2 = I;
  255. while (I2 != MBB->begin() && (--I2)->getDesc().isTerminator())
  256. I = I2;
  257. bool AtStart = I == MBB->begin();
  258. MachineBasicBlock::iterator BeforeI = I;
  259. if (!AtStart)
  260. --BeforeI;
  261. // Restore all registers immediately before the return and any
  262. // terminators that preceed it.
  263. if (!TII.restoreCalleeSavedRegisters(*MBB, I, CSI)) {
  264. for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
  265. TII.loadRegFromStackSlot(*MBB, I, CSI[i].getReg(),
  266. CSI[i].getFrameIdx(),
  267. CSI[i].getRegClass());
  268. assert(I != MBB->begin() &&
  269. "loadRegFromStackSlot didn't insert any code!");
  270. // Insert in reverse order. loadRegFromStackSlot can insert
  271. // multiple instructions.
  272. if (AtStart)
  273. I = MBB->begin();
  274. else {
  275. I = BeforeI;
  276. ++I;
  277. }
  278. }
  279. }
  280. }
  281. return;
  282. }
  283. // Insert spills.
  284. std::vector<CalleeSavedInfo> blockCSI;
  285. for (CSRegBlockMap::iterator BI = CSRSave.begin(),
  286. BE = CSRSave.end(); BI != BE; ++BI) {
  287. MachineBasicBlock* MBB = BI->first;
  288. CSRegSet save = BI->second;
  289. if (save.empty())
  290. continue;
  291. blockCSI.clear();
  292. for (CSRegSet::iterator RI = save.begin(),
  293. RE = save.end(); RI != RE; ++RI) {
  294. blockCSI.push_back(CSI[*RI]);
  295. }
  296. assert(blockCSI.size() > 0 &&
  297. "Could not collect callee saved register info");
  298. I = MBB->begin();
  299. // When shrink wrapping, use stack slot stores/loads.
  300. for (unsigned i = 0, e = blockCSI.size(); i != e; ++i) {
  301. // Add the callee-saved register as live-in.
  302. // It's killed at the spill.
  303. MBB->addLiveIn(blockCSI[i].getReg());
  304. // Insert the spill to the stack frame.
  305. TII.storeRegToStackSlot(*MBB, I, blockCSI[i].getReg(),
  306. true,
  307. blockCSI[i].getFrameIdx(),
  308. blockCSI[i].getRegClass());
  309. }
  310. }
  311. for (CSRegBlockMap::iterator BI = CSRRestore.begin(),
  312. BE = CSRRestore.end(); BI != BE; ++BI) {
  313. MachineBasicBlock* MBB = BI->first;
  314. CSRegSet restore = BI->second;
  315. if (restore.empty())
  316. continue;
  317. blockCSI.clear();
  318. for (CSRegSet::iterator RI = restore.begin(),
  319. RE = restore.end(); RI != RE; ++RI) {
  320. blockCSI.push_back(CSI[*RI]);
  321. }
  322. assert(blockCSI.size() > 0 &&
  323. "Could not find callee saved register info");
  324. // If MBB is empty and needs restores, insert at the _beginning_.
  325. if (MBB->empty()) {
  326. I = MBB->begin();
  327. } else {
  328. I = MBB->end();
  329. --I;
  330. // Skip over all terminator instructions, which are part of the
  331. // return sequence.
  332. if (! I->getDesc().isTerminator()) {
  333. ++I;
  334. } else {
  335. MachineBasicBlock::iterator I2 = I;
  336. while (I2 != MBB->begin() && (--I2)->getDesc().isTerminator())
  337. I = I2;
  338. }
  339. }
  340. bool AtStart = I == MBB->begin();
  341. MachineBasicBlock::iterator BeforeI = I;
  342. if (!AtStart)
  343. --BeforeI;
  344. // Restore all registers immediately before the return and any
  345. // terminators that preceed it.
  346. for (unsigned i = 0, e = blockCSI.size(); i != e; ++i) {
  347. TII.loadRegFromStackSlot(*MBB, I, blockCSI[i].getReg(),
  348. blockCSI[i].getFrameIdx(),
  349. blockCSI[i].getRegClass());
  350. assert(I != MBB->begin() &&
  351. "loadRegFromStackSlot didn't insert any code!");
  352. // Insert in reverse order. loadRegFromStackSlot can insert
  353. // multiple instructions.
  354. if (AtStart)
  355. I = MBB->begin();
  356. else {
  357. I = BeforeI;
  358. ++I;
  359. }
  360. }
  361. }
  362. }
  363. /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
  364. static inline void
  365. AdjustStackOffset(MachineFrameInfo *FFI, int FrameIdx,
  366. bool StackGrowsDown, int64_t &Offset,
  367. unsigned &MaxAlign) {
  368. // If stack grows down, we need to add size of find the lowest address of the
  369. // object.
  370. if (StackGrowsDown)
  371. Offset += FFI->getObjectSize(FrameIdx);
  372. unsigned Align = FFI->getObjectAlignment(FrameIdx);
  373. // If the alignment of this object is greater than that of the stack, then
  374. // increase the stack alignment to match.
  375. MaxAlign = std::max(MaxAlign, Align);
  376. // Adjust to alignment boundary.
  377. Offset = (Offset + Align - 1) / Align * Align;
  378. if (StackGrowsDown) {
  379. FFI->setObjectOffset(FrameIdx, -Offset); // Set the computed offset
  380. } else {
  381. FFI->setObjectOffset(FrameIdx, Offset);
  382. Offset += FFI->getObjectSize(FrameIdx);
  383. }
  384. }
  385. /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
  386. /// abstract stack objects.
  387. ///
  388. void PEI::calculateFrameObjectOffsets(MachineFunction &Fn) {
  389. const TargetFrameInfo &TFI = *Fn.getTarget().getFrameInfo();
  390. bool StackGrowsDown =
  391. TFI.getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
  392. // Loop over all of the stack objects, assigning sequential addresses...
  393. MachineFrameInfo *FFI = Fn.getFrameInfo();
  394. unsigned MaxAlign = FFI->getMaxAlignment();
  395. // Start at the beginning of the local area.
  396. // The Offset is the distance from the stack top in the direction
  397. // of stack growth -- so it's always nonnegative.
  398. int64_t Offset = TFI.getOffsetOfLocalArea();
  399. if (StackGrowsDown)
  400. Offset = -Offset;
  401. assert(Offset >= 0
  402. && "Local area offset should be in direction of stack growth");
  403. // If there are fixed sized objects that are preallocated in the local area,
  404. // non-fixed objects can't be allocated right at the start of local area.
  405. // We currently don't support filling in holes in between fixed sized
  406. // objects, so we adjust 'Offset' to point to the end of last fixed sized
  407. // preallocated object.
  408. for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
  409. int64_t FixedOff;
  410. if (StackGrowsDown) {
  411. // The maximum distance from the stack pointer is at lower address of
  412. // the object -- which is given by offset. For down growing stack
  413. // the offset is negative, so we negate the offset to get the distance.
  414. FixedOff = -FFI->getObjectOffset(i);
  415. } else {
  416. // The maximum distance from the start pointer is at the upper
  417. // address of the object.
  418. FixedOff = FFI->getObjectOffset(i) + FFI->getObjectSize(i);
  419. }
  420. if (FixedOff > Offset) Offset = FixedOff;
  421. }
  422. // First assign frame offsets to stack objects that are used to spill
  423. // callee saved registers.
  424. if (StackGrowsDown) {
  425. for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) {
  426. // If stack grows down, we need to add size of find the lowest
  427. // address of the object.
  428. Offset += FFI->getObjectSize(i);
  429. unsigned Align = FFI->getObjectAlignment(i);
  430. // If the alignment of this object is greater than that of the stack,
  431. // then increase the stack alignment to match.
  432. MaxAlign = std::max(MaxAlign, Align);
  433. // Adjust to alignment boundary
  434. Offset = (Offset+Align-1)/Align*Align;
  435. FFI->setObjectOffset(i, -Offset); // Set the computed offset
  436. }
  437. } else {
  438. int MaxCSFI = MaxCSFrameIndex, MinCSFI = MinCSFrameIndex;
  439. for (int i = MaxCSFI; i >= MinCSFI ; --i) {
  440. unsigned Align = FFI->getObjectAlignment(i);
  441. // If the alignment of this object is greater than that of the stack,
  442. // then increase the stack alignment to match.
  443. MaxAlign = std::max(MaxAlign, Align);
  444. // Adjust to alignment boundary
  445. Offset = (Offset+Align-1)/Align*Align;
  446. FFI->setObjectOffset(i, Offset);
  447. Offset += FFI->getObjectSize(i);
  448. }
  449. }
  450. // Make sure the special register scavenging spill slot is closest to the
  451. // frame pointer if a frame pointer is required.
  452. const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
  453. if (RS && RegInfo->hasFP(Fn)) {
  454. int SFI = RS->getScavengingFrameIndex();
  455. if (SFI >= 0)
  456. AdjustStackOffset(FFI, SFI, StackGrowsDown, Offset, MaxAlign);
  457. }
  458. // Make sure that the stack protector comes before the local variables on the
  459. // stack.
  460. if (FFI->getStackProtectorIndex() >= 0)
  461. AdjustStackOffset(FFI, FFI->getStackProtectorIndex(), StackGrowsDown,
  462. Offset, MaxAlign);
  463. // Then assign frame offsets to stack objects that are not used to spill
  464. // callee saved registers.
  465. for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
  466. if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
  467. continue;
  468. if (RS && (int)i == RS->getScavengingFrameIndex())
  469. continue;
  470. if (FFI->isDeadObjectIndex(i))
  471. continue;
  472. if (FFI->getStackProtectorIndex() == (int)i)
  473. continue;
  474. AdjustStackOffset(FFI, i, StackGrowsDown, Offset, MaxAlign);
  475. }
  476. // Make sure the special register scavenging spill slot is closest to the
  477. // stack pointer.
  478. if (RS && !RegInfo->hasFP(Fn)) {
  479. int SFI = RS->getScavengingFrameIndex();
  480. if (SFI >= 0)
  481. AdjustStackOffset(FFI, SFI, StackGrowsDown, Offset, MaxAlign);
  482. }
  483. // Round up the size to a multiple of the alignment, but only if there are
  484. // calls or alloca's in the function. This ensures that any calls to
  485. // subroutines have their stack frames suitable aligned.
  486. // Also do this if we need runtime alignment of the stack. In this case
  487. // offsets will be relative to SP not FP; round up the stack size so this
  488. // works.
  489. if (!RegInfo->targetHandlesStackFrameRounding() &&
  490. (FFI->hasCalls() || FFI->hasVarSizedObjects() ||
  491. (RegInfo->needsStackRealignment(Fn) &&
  492. FFI->getObjectIndexEnd() != 0))) {
  493. // If we have reserved argument space for call sites in the function
  494. // immediately on entry to the current function, count it as part of the
  495. // overall stack size.
  496. if (RegInfo->hasReservedCallFrame(Fn))
  497. Offset += FFI->getMaxCallFrameSize();
  498. unsigned AlignMask = std::max(TFI.getStackAlignment(),MaxAlign) - 1;
  499. Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
  500. }
  501. // Update frame info to pretend that this is part of the stack...
  502. FFI->setStackSize(Offset+TFI.getOffsetOfLocalArea());
  503. // Remember the required stack alignment in case targets need it to perform
  504. // dynamic stack alignment.
  505. FFI->setMaxAlignment(MaxAlign);
  506. }
  507. /// insertPrologEpilogCode - Scan the function for modified callee saved
  508. /// registers, insert spill code for these callee saved registers, then add
  509. /// prolog and epilog code to the function.
  510. ///
  511. void PEI::insertPrologEpilogCode(MachineFunction &Fn) {
  512. const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
  513. // Add prologue to the function...
  514. TRI->emitPrologue(Fn);
  515. // Add epilogue to restore the callee-save registers in each exiting block
  516. for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
  517. // If last instruction is a return instruction, add an epilogue
  518. if (!I->empty() && I->back().getDesc().isReturn())
  519. TRI->emitEpilogue(Fn, *I);
  520. }
  521. }
  522. /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
  523. /// register references and actual offsets.
  524. ///
  525. void PEI::replaceFrameIndices(MachineFunction &Fn) {
  526. if (!Fn.getFrameInfo()->hasStackObjects()) return; // Nothing to do?
  527. const TargetMachine &TM = Fn.getTarget();
  528. assert(TM.getRegisterInfo() && "TM::getRegisterInfo() must be implemented!");
  529. const TargetRegisterInfo &TRI = *TM.getRegisterInfo();
  530. const TargetFrameInfo *TFI = TM.getFrameInfo();
  531. bool StackGrowsDown =
  532. TFI->getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
  533. int FrameSetupOpcode = TRI.getCallFrameSetupOpcode();
  534. int FrameDestroyOpcode = TRI.getCallFrameDestroyOpcode();
  535. for (MachineFunction::iterator BB = Fn.begin(),
  536. E = Fn.end(); BB != E; ++BB) {
  537. int SPAdj = 0; // SP offset due to call frame setup / destroy.
  538. if (RS) RS->enterBasicBlock(BB);
  539. for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
  540. if (I->getOpcode() == TargetInstrInfo::DECLARE) {
  541. // Ignore it.
  542. ++I;
  543. continue;
  544. }
  545. if (I->getOpcode() == FrameSetupOpcode ||
  546. I->getOpcode() == FrameDestroyOpcode) {
  547. // Remember how much SP has been adjusted to create the call
  548. // frame.
  549. int Size = I->getOperand(0).getImm();
  550. if ((!StackGrowsDown && I->getOpcode() == FrameSetupOpcode) ||
  551. (StackGrowsDown && I->getOpcode() == FrameDestroyOpcode))
  552. Size = -Size;
  553. SPAdj += Size;
  554. MachineBasicBlock::iterator PrevI = BB->end();
  555. if (I != BB->begin()) PrevI = prior(I);
  556. TRI.eliminateCallFramePseudoInstr(Fn, *BB, I);
  557. // Visit the instructions created by eliminateCallFramePseudoInstr().
  558. if (PrevI == BB->end())
  559. I = BB->begin(); // The replaced instr was the first in the block.
  560. else
  561. I = next(PrevI);
  562. continue;
  563. }
  564. MachineInstr *MI = I;
  565. bool DoIncr = true;
  566. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
  567. if (MI->getOperand(i).isFI()) {
  568. // Some instructions (e.g. inline asm instructions) can have
  569. // multiple frame indices and/or cause eliminateFrameIndex
  570. // to insert more than one instruction. We need the register
  571. // scavenger to go through all of these instructions so that
  572. // it can update its register information. We keep the
  573. // iterator at the point before insertion so that we can
  574. // revisit them in full.
  575. bool AtBeginning = (I == BB->begin());
  576. if (!AtBeginning) --I;
  577. // If this instruction has a FrameIndex operand, we need to
  578. // use that target machine register info object to eliminate
  579. // it.
  580. TRI.eliminateFrameIndex(MI, SPAdj, RS);
  581. // Reset the iterator if we were at the beginning of the BB.
  582. if (AtBeginning) {
  583. I = BB->begin();
  584. DoIncr = false;
  585. }
  586. MI = 0;
  587. break;
  588. }
  589. if (DoIncr && I != BB->end()) ++I;
  590. // Update register states.
  591. if (RS && MI) RS->forward(MI);
  592. }
  593. assert(SPAdj == 0 && "Unbalanced call frame setup / destroy pairs?");
  594. }
  595. }