PrologEpilogInserter.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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/CommandLine.h"
  34. #include "llvm/Support/Compiler.h"
  35. #include "llvm/ADT/IndexedMap.h"
  36. #include "llvm/ADT/STLExtras.h"
  37. #include <climits>
  38. using namespace llvm;
  39. char PEI::ID = 0;
  40. static RegisterPass<PEI>
  41. X("prologepilog", "Prologue/Epilogue Insertion");
  42. /// createPrologEpilogCodeInserter - This function returns a pass that inserts
  43. /// prolog and epilog code, and eliminates abstract frame references.
  44. ///
  45. FunctionPass *llvm::createPrologEpilogCodeInserter() { return new PEI(); }
  46. /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
  47. /// frame indexes with appropriate references.
  48. ///
  49. bool PEI::runOnMachineFunction(MachineFunction &Fn) {
  50. const Function* F = Fn.getFunction();
  51. const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
  52. RS = TRI->requiresRegisterScavenging(Fn) ? new RegScavenger() : NULL;
  53. FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(Fn);
  54. // Get MachineModuleInfo so that we can track the construction of the
  55. // frame.
  56. if (MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>())
  57. Fn.getFrameInfo()->setMachineModuleInfo(MMI);
  58. // Calculate the MaxCallFrameSize and HasCalls variables for the function's
  59. // frame information. Also eliminates call frame pseudo instructions.
  60. calculateCallsInformation(Fn);
  61. // Allow the target machine to make some adjustments to the function
  62. // e.g. UsedPhysRegs before calculateCalleeSavedRegisters.
  63. TRI->processFunctionBeforeCalleeSavedScan(Fn, RS);
  64. // Scan the function for modified callee saved registers and insert spill code
  65. // for any callee saved registers that are modified.
  66. calculateCalleeSavedRegisters(Fn);
  67. // Determine placement of CSR spill/restore code:
  68. // - with shrink wrapping, place spills and restores to tightly
  69. // enclose regions in the Machine CFG of the function where
  70. // they are used. Without shrink wrapping
  71. // - default (no shrink wrapping), place all spills in the
  72. // entry block, all restores in return blocks.
  73. placeCSRSpillsAndRestores(Fn);
  74. // Add the code to save and restore the callee saved registers
  75. if (!F->hasFnAttr(Attribute::Naked))
  76. insertCSRSpillsAndRestores(Fn);
  77. // Allow the target machine to make final modifications to the function
  78. // before the frame layout is finalized.
  79. TRI->processFunctionBeforeFrameFinalized(Fn);
  80. // Calculate actual frame offsets for all abstract stack objects...
  81. calculateFrameObjectOffsets(Fn);
  82. // Add prolog and epilog code to the function. This function is required
  83. // to align the stack frame as necessary for any stack variables or
  84. // called functions. Because of this, calculateCalleeSavedRegisters
  85. // must be called before this function in order to set the HasCalls
  86. // and MaxCallFrameSize variables.
  87. if (!F->hasFnAttr(Attribute::Naked))
  88. insertPrologEpilogCode(Fn);
  89. // Replace all MO_FrameIndex operands with physical register references
  90. // and actual offsets.
  91. //
  92. replaceFrameIndices(Fn);
  93. // If register scavenging is needed, as we've enabled doing it as a
  94. // post-pass, scavenge the virtual registers that frame index elimiation
  95. // inserted.
  96. if (TRI->requiresRegisterScavenging(Fn) && FrameIndexVirtualScavenging)
  97. scavengeFrameVirtualRegs(Fn);
  98. delete RS;
  99. clearAllSets();
  100. return true;
  101. }
  102. #if 0
  103. void PEI::getAnalysisUsage(AnalysisUsage &AU) const {
  104. AU.setPreservesCFG();
  105. if (ShrinkWrapping || ShrinkWrapFunc != "") {
  106. AU.addRequired<MachineLoopInfo>();
  107. AU.addRequired<MachineDominatorTree>();
  108. }
  109. AU.addPreserved<MachineLoopInfo>();
  110. AU.addPreserved<MachineDominatorTree>();
  111. MachineFunctionPass::getAnalysisUsage(AU);
  112. }
  113. #endif
  114. /// calculateCallsInformation - Calculate the MaxCallFrameSize and HasCalls
  115. /// variables for the function's frame information and eliminate call frame
  116. /// pseudo instructions.
  117. void PEI::calculateCallsInformation(MachineFunction &Fn) {
  118. const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
  119. MachineFrameInfo *FFI = Fn.getFrameInfo();
  120. unsigned MaxCallFrameSize = 0;
  121. bool HasCalls = FFI->hasCalls();
  122. // Get the function call frame set-up and tear-down instruction opcode
  123. int FrameSetupOpcode = RegInfo->getCallFrameSetupOpcode();
  124. int FrameDestroyOpcode = RegInfo->getCallFrameDestroyOpcode();
  125. // Early exit for targets which have no call frame setup/destroy pseudo
  126. // instructions.
  127. if (FrameSetupOpcode == -1 && FrameDestroyOpcode == -1)
  128. return;
  129. std::vector<MachineBasicBlock::iterator> FrameSDOps;
  130. for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB)
  131. for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
  132. if (I->getOpcode() == FrameSetupOpcode ||
  133. I->getOpcode() == FrameDestroyOpcode) {
  134. assert(I->getNumOperands() >= 1 && "Call Frame Setup/Destroy Pseudo"
  135. " instructions should have a single immediate argument!");
  136. unsigned Size = I->getOperand(0).getImm();
  137. if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
  138. HasCalls = true;
  139. FrameSDOps.push_back(I);
  140. } else if (I->isInlineAsm()) {
  141. // An InlineAsm might be a call; assume it is to get the stack frame
  142. // aligned correctly for calls.
  143. HasCalls = true;
  144. }
  145. FFI->setHasCalls(HasCalls);
  146. FFI->setMaxCallFrameSize(MaxCallFrameSize);
  147. for (std::vector<MachineBasicBlock::iterator>::iterator
  148. i = FrameSDOps.begin(), e = FrameSDOps.end(); i != e; ++i) {
  149. MachineBasicBlock::iterator I = *i;
  150. // If call frames are not being included as part of the stack frame, and
  151. // there is no dynamic allocation (therefore referencing frame slots off
  152. // sp), leave the pseudo ops alone. We'll eliminate them later.
  153. if (RegInfo->hasReservedCallFrame(Fn) || RegInfo->hasFP(Fn))
  154. RegInfo->eliminateCallFramePseudoInstr(Fn, *I->getParent(), I);
  155. }
  156. }
  157. /// calculateCalleeSavedRegisters - Scan the function for modified callee saved
  158. /// registers.
  159. void PEI::calculateCalleeSavedRegisters(MachineFunction &Fn) {
  160. const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
  161. const TargetFrameInfo *TFI = Fn.getTarget().getFrameInfo();
  162. MachineFrameInfo *FFI = Fn.getFrameInfo();
  163. // Get the callee saved register list...
  164. const unsigned *CSRegs = RegInfo->getCalleeSavedRegs(&Fn);
  165. // These are used to keep track the callee-save area. Initialize them.
  166. MinCSFrameIndex = INT_MAX;
  167. MaxCSFrameIndex = 0;
  168. // Early exit for targets which have no callee saved registers.
  169. if (CSRegs == 0 || CSRegs[0] == 0)
  170. return;
  171. // Figure out which *callee saved* registers are modified by the current
  172. // function, thus needing to be saved and restored in the prolog/epilog.
  173. const TargetRegisterClass * const *CSRegClasses =
  174. RegInfo->getCalleeSavedRegClasses(&Fn);
  175. std::vector<CalleeSavedInfo> CSI;
  176. for (unsigned i = 0; CSRegs[i]; ++i) {
  177. unsigned Reg = CSRegs[i];
  178. if (Fn.getRegInfo().isPhysRegUsed(Reg)) {
  179. // If the reg is modified, save it!
  180. CSI.push_back(CalleeSavedInfo(Reg, CSRegClasses[i]));
  181. } else {
  182. for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
  183. *AliasSet; ++AliasSet) { // Check alias registers too.
  184. if (Fn.getRegInfo().isPhysRegUsed(*AliasSet)) {
  185. CSI.push_back(CalleeSavedInfo(Reg, CSRegClasses[i]));
  186. break;
  187. }
  188. }
  189. }
  190. }
  191. if (CSI.empty())
  192. return; // Early exit if no callee saved registers are modified!
  193. unsigned NumFixedSpillSlots;
  194. const TargetFrameInfo::SpillSlot *FixedSpillSlots =
  195. TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots);
  196. // Now that we know which registers need to be saved and restored, allocate
  197. // stack slots for them.
  198. for (std::vector<CalleeSavedInfo>::iterator
  199. I = CSI.begin(), E = CSI.end(); I != E; ++I) {
  200. unsigned Reg = I->getReg();
  201. const TargetRegisterClass *RC = I->getRegClass();
  202. int FrameIdx;
  203. if (RegInfo->hasReservedSpillSlot(Fn, Reg, FrameIdx)) {
  204. I->setFrameIdx(FrameIdx);
  205. continue;
  206. }
  207. // Check to see if this physreg must be spilled to a particular stack slot
  208. // on this target.
  209. const TargetFrameInfo::SpillSlot *FixedSlot = FixedSpillSlots;
  210. while (FixedSlot != FixedSpillSlots+NumFixedSpillSlots &&
  211. FixedSlot->Reg != Reg)
  212. ++FixedSlot;
  213. if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) {
  214. // Nope, just spill it anywhere convenient.
  215. unsigned Align = RC->getAlignment();
  216. unsigned StackAlign = TFI->getStackAlignment();
  217. // We may not be able to satisfy the desired alignment specification of
  218. // the TargetRegisterClass if the stack alignment is smaller. Use the
  219. // min.
  220. Align = std::min(Align, StackAlign);
  221. FrameIdx = FFI->CreateStackObject(RC->getSize(), Align, true);
  222. if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx;
  223. if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx;
  224. } else {
  225. // Spill it to the stack where we must.
  226. FrameIdx = FFI->CreateFixedObject(RC->getSize(), FixedSlot->Offset,
  227. true, false);
  228. }
  229. I->setFrameIdx(FrameIdx);
  230. }
  231. FFI->setCalleeSavedInfo(CSI);
  232. }
  233. /// insertCSRSpillsAndRestores - Insert spill and restore code for
  234. /// callee saved registers used in the function, handling shrink wrapping.
  235. ///
  236. void PEI::insertCSRSpillsAndRestores(MachineFunction &Fn) {
  237. // Get callee saved register information.
  238. MachineFrameInfo *FFI = Fn.getFrameInfo();
  239. const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
  240. FFI->setCalleeSavedInfoValid(true);
  241. // Early exit if no callee saved registers are modified!
  242. if (CSI.empty())
  243. return;
  244. const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
  245. MachineBasicBlock::iterator I;
  246. if (! ShrinkWrapThisFunction) {
  247. // Spill using target interface.
  248. I = EntryBlock->begin();
  249. if (!TII.spillCalleeSavedRegisters(*EntryBlock, I, CSI)) {
  250. for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
  251. // Add the callee-saved register as live-in.
  252. // It's killed at the spill.
  253. EntryBlock->addLiveIn(CSI[i].getReg());
  254. // Insert the spill to the stack frame.
  255. TII.storeRegToStackSlot(*EntryBlock, I, CSI[i].getReg(), true,
  256. CSI[i].getFrameIdx(), CSI[i].getRegClass());
  257. }
  258. }
  259. // Restore using target interface.
  260. for (unsigned ri = 0, re = ReturnBlocks.size(); ri != re; ++ri) {
  261. MachineBasicBlock* MBB = ReturnBlocks[ri];
  262. I = MBB->end(); --I;
  263. // Skip over all terminator instructions, which are part of the return
  264. // sequence.
  265. MachineBasicBlock::iterator I2 = I;
  266. while (I2 != MBB->begin() && (--I2)->getDesc().isTerminator())
  267. I = I2;
  268. bool AtStart = I == MBB->begin();
  269. MachineBasicBlock::iterator BeforeI = I;
  270. if (!AtStart)
  271. --BeforeI;
  272. // Restore all registers immediately before the return and any
  273. // terminators that preceed it.
  274. if (!TII.restoreCalleeSavedRegisters(*MBB, I, CSI)) {
  275. for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
  276. TII.loadRegFromStackSlot(*MBB, I, CSI[i].getReg(),
  277. CSI[i].getFrameIdx(),
  278. CSI[i].getRegClass());
  279. assert(I != MBB->begin() &&
  280. "loadRegFromStackSlot didn't insert any code!");
  281. // Insert in reverse order. loadRegFromStackSlot can insert
  282. // multiple instructions.
  283. if (AtStart)
  284. I = MBB->begin();
  285. else {
  286. I = BeforeI;
  287. ++I;
  288. }
  289. }
  290. }
  291. }
  292. return;
  293. }
  294. // Insert spills.
  295. std::vector<CalleeSavedInfo> blockCSI;
  296. for (CSRegBlockMap::iterator BI = CSRSave.begin(),
  297. BE = CSRSave.end(); BI != BE; ++BI) {
  298. MachineBasicBlock* MBB = BI->first;
  299. CSRegSet save = BI->second;
  300. if (save.empty())
  301. continue;
  302. blockCSI.clear();
  303. for (CSRegSet::iterator RI = save.begin(),
  304. RE = save.end(); RI != RE; ++RI) {
  305. blockCSI.push_back(CSI[*RI]);
  306. }
  307. assert(blockCSI.size() > 0 &&
  308. "Could not collect callee saved register info");
  309. I = MBB->begin();
  310. // When shrink wrapping, use stack slot stores/loads.
  311. for (unsigned i = 0, e = blockCSI.size(); i != e; ++i) {
  312. // Add the callee-saved register as live-in.
  313. // It's killed at the spill.
  314. MBB->addLiveIn(blockCSI[i].getReg());
  315. // Insert the spill to the stack frame.
  316. TII.storeRegToStackSlot(*MBB, I, blockCSI[i].getReg(),
  317. true,
  318. blockCSI[i].getFrameIdx(),
  319. blockCSI[i].getRegClass());
  320. }
  321. }
  322. for (CSRegBlockMap::iterator BI = CSRRestore.begin(),
  323. BE = CSRRestore.end(); BI != BE; ++BI) {
  324. MachineBasicBlock* MBB = BI->first;
  325. CSRegSet restore = BI->second;
  326. if (restore.empty())
  327. continue;
  328. blockCSI.clear();
  329. for (CSRegSet::iterator RI = restore.begin(),
  330. RE = restore.end(); RI != RE; ++RI) {
  331. blockCSI.push_back(CSI[*RI]);
  332. }
  333. assert(blockCSI.size() > 0 &&
  334. "Could not find callee saved register info");
  335. // If MBB is empty and needs restores, insert at the _beginning_.
  336. if (MBB->empty()) {
  337. I = MBB->begin();
  338. } else {
  339. I = MBB->end();
  340. --I;
  341. // Skip over all terminator instructions, which are part of the
  342. // return sequence.
  343. if (! I->getDesc().isTerminator()) {
  344. ++I;
  345. } else {
  346. MachineBasicBlock::iterator I2 = I;
  347. while (I2 != MBB->begin() && (--I2)->getDesc().isTerminator())
  348. I = I2;
  349. }
  350. }
  351. bool AtStart = I == MBB->begin();
  352. MachineBasicBlock::iterator BeforeI = I;
  353. if (!AtStart)
  354. --BeforeI;
  355. // Restore all registers immediately before the return and any
  356. // terminators that preceed it.
  357. for (unsigned i = 0, e = blockCSI.size(); i != e; ++i) {
  358. TII.loadRegFromStackSlot(*MBB, I, blockCSI[i].getReg(),
  359. blockCSI[i].getFrameIdx(),
  360. blockCSI[i].getRegClass());
  361. assert(I != MBB->begin() &&
  362. "loadRegFromStackSlot didn't insert any code!");
  363. // Insert in reverse order. loadRegFromStackSlot can insert
  364. // multiple instructions.
  365. if (AtStart)
  366. I = MBB->begin();
  367. else {
  368. I = BeforeI;
  369. ++I;
  370. }
  371. }
  372. }
  373. }
  374. /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
  375. static inline void
  376. AdjustStackOffset(MachineFrameInfo *FFI, int FrameIdx,
  377. bool StackGrowsDown, int64_t &Offset,
  378. unsigned &MaxAlign) {
  379. // If the stack grows down, add the object size to find the lowest address.
  380. if (StackGrowsDown)
  381. Offset += FFI->getObjectSize(FrameIdx);
  382. unsigned Align = FFI->getObjectAlignment(FrameIdx);
  383. // If the alignment of this object is greater than that of the stack, then
  384. // increase the stack alignment to match.
  385. MaxAlign = std::max(MaxAlign, Align);
  386. // Adjust to alignment boundary.
  387. Offset = (Offset + Align - 1) / Align * Align;
  388. if (StackGrowsDown) {
  389. FFI->setObjectOffset(FrameIdx, -Offset); // Set the computed offset
  390. } else {
  391. FFI->setObjectOffset(FrameIdx, Offset);
  392. Offset += FFI->getObjectSize(FrameIdx);
  393. }
  394. }
  395. /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
  396. /// abstract stack objects.
  397. ///
  398. void PEI::calculateFrameObjectOffsets(MachineFunction &Fn) {
  399. const TargetFrameInfo &TFI = *Fn.getTarget().getFrameInfo();
  400. bool StackGrowsDown =
  401. TFI.getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
  402. // Loop over all of the stack objects, assigning sequential addresses...
  403. MachineFrameInfo *FFI = Fn.getFrameInfo();
  404. unsigned MaxAlign = 1;
  405. // Start at the beginning of the local area.
  406. // The Offset is the distance from the stack top in the direction
  407. // of stack growth -- so it's always nonnegative.
  408. int LocalAreaOffset = TFI.getOffsetOfLocalArea();
  409. if (StackGrowsDown)
  410. LocalAreaOffset = -LocalAreaOffset;
  411. assert(LocalAreaOffset >= 0
  412. && "Local area offset should be in direction of stack growth");
  413. int64_t Offset = LocalAreaOffset;
  414. // If there are fixed sized objects that are preallocated in the local area,
  415. // non-fixed objects can't be allocated right at the start of local area.
  416. // We currently don't support filling in holes in between fixed sized
  417. // objects, so we adjust 'Offset' to point to the end of last fixed sized
  418. // preallocated object.
  419. for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
  420. int64_t FixedOff;
  421. if (StackGrowsDown) {
  422. // The maximum distance from the stack pointer is at lower address of
  423. // the object -- which is given by offset. For down growing stack
  424. // the offset is negative, so we negate the offset to get the distance.
  425. FixedOff = -FFI->getObjectOffset(i);
  426. } else {
  427. // The maximum distance from the start pointer is at the upper
  428. // address of the object.
  429. FixedOff = FFI->getObjectOffset(i) + FFI->getObjectSize(i);
  430. }
  431. if (FixedOff > Offset) Offset = FixedOff;
  432. }
  433. // First assign frame offsets to stack objects that are used to spill
  434. // callee saved registers.
  435. if (StackGrowsDown) {
  436. for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) {
  437. // If stack grows down, we need to add size of find the lowest
  438. // address of the object.
  439. Offset += FFI->getObjectSize(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); // Set the computed offset
  447. }
  448. } else {
  449. int MaxCSFI = MaxCSFrameIndex, MinCSFI = MinCSFrameIndex;
  450. for (int i = MaxCSFI; i >= MinCSFI ; --i) {
  451. unsigned Align = FFI->getObjectAlignment(i);
  452. // If the alignment of this object is greater than that of the stack,
  453. // then increase the stack alignment to match.
  454. MaxAlign = std::max(MaxAlign, Align);
  455. // Adjust to alignment boundary
  456. Offset = (Offset+Align-1)/Align*Align;
  457. FFI->setObjectOffset(i, Offset);
  458. Offset += FFI->getObjectSize(i);
  459. }
  460. }
  461. // Make sure the special register scavenging spill slot is closest to the
  462. // frame pointer if a frame pointer is required.
  463. const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
  464. if (RS && RegInfo->hasFP(Fn) && !RegInfo->needsStackRealignment(Fn)) {
  465. int SFI = RS->getScavengingFrameIndex();
  466. if (SFI >= 0)
  467. AdjustStackOffset(FFI, SFI, StackGrowsDown, Offset, MaxAlign);
  468. }
  469. // Make sure that the stack protector comes before the local variables on the
  470. // stack.
  471. if (FFI->getStackProtectorIndex() >= 0)
  472. AdjustStackOffset(FFI, FFI->getStackProtectorIndex(), StackGrowsDown,
  473. Offset, MaxAlign);
  474. // Then assign frame offsets to stack objects that are not used to spill
  475. // callee saved registers.
  476. for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
  477. if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
  478. continue;
  479. if (RS && (int)i == RS->getScavengingFrameIndex())
  480. continue;
  481. if (FFI->isDeadObjectIndex(i))
  482. continue;
  483. if (FFI->getStackProtectorIndex() == (int)i)
  484. continue;
  485. AdjustStackOffset(FFI, i, StackGrowsDown, Offset, MaxAlign);
  486. }
  487. // Make sure the special register scavenging spill slot is closest to the
  488. // stack pointer.
  489. if (RS && (!RegInfo->hasFP(Fn) || RegInfo->needsStackRealignment(Fn))) {
  490. int SFI = RS->getScavengingFrameIndex();
  491. if (SFI >= 0)
  492. AdjustStackOffset(FFI, SFI, StackGrowsDown, Offset, MaxAlign);
  493. }
  494. if (!RegInfo->targetHandlesStackFrameRounding()) {
  495. // If we have reserved argument space for call sites in the function
  496. // immediately on entry to the current function, count it as part of the
  497. // overall stack size.
  498. if (FFI->hasCalls() && RegInfo->hasReservedCallFrame(Fn))
  499. Offset += FFI->getMaxCallFrameSize();
  500. // Round up the size to a multiple of the alignment. If the function has
  501. // any calls or alloca's, align to the target's StackAlignment value to
  502. // ensure that the callee's frame or the alloca data is suitably aligned;
  503. // otherwise, for leaf functions, align to the TransientStackAlignment
  504. // value.
  505. unsigned StackAlign;
  506. if (FFI->hasCalls() || FFI->hasVarSizedObjects() ||
  507. (RegInfo->needsStackRealignment(Fn) && FFI->getObjectIndexEnd() != 0))
  508. StackAlign = TFI.getStackAlignment();
  509. else
  510. StackAlign = TFI.getTransientStackAlignment();
  511. // If the frame pointer is eliminated, all frame offsets will be relative
  512. // to SP not FP; align to MaxAlign so this works.
  513. StackAlign = std::max(StackAlign, MaxAlign);
  514. unsigned AlignMask = StackAlign - 1;
  515. Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
  516. }
  517. // Update frame info to pretend that this is part of the stack...
  518. FFI->setStackSize(Offset - LocalAreaOffset);
  519. // Remember the required stack alignment in case targets need it to perform
  520. // dynamic stack alignment.
  521. if (MaxAlign > FFI->getMaxAlignment())
  522. FFI->setMaxAlignment(MaxAlign);
  523. }
  524. /// insertPrologEpilogCode - Scan the function for modified callee saved
  525. /// registers, insert spill code for these callee saved registers, then add
  526. /// prolog and epilog code to the function.
  527. ///
  528. void PEI::insertPrologEpilogCode(MachineFunction &Fn) {
  529. const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
  530. // Add prologue to the function...
  531. TRI->emitPrologue(Fn);
  532. // Add epilogue to restore the callee-save registers in each exiting block
  533. for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
  534. // If last instruction is a return instruction, add an epilogue
  535. if (!I->empty() && I->back().getDesc().isReturn())
  536. TRI->emitEpilogue(Fn, *I);
  537. }
  538. }
  539. /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
  540. /// register references and actual offsets.
  541. ///
  542. void PEI::replaceFrameIndices(MachineFunction &Fn) {
  543. if (!Fn.getFrameInfo()->hasStackObjects()) return; // Nothing to do?
  544. const TargetMachine &TM = Fn.getTarget();
  545. assert(TM.getRegisterInfo() && "TM::getRegisterInfo() must be implemented!");
  546. const TargetRegisterInfo &TRI = *TM.getRegisterInfo();
  547. const TargetFrameInfo *TFI = TM.getFrameInfo();
  548. bool StackGrowsDown =
  549. TFI->getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
  550. int FrameSetupOpcode = TRI.getCallFrameSetupOpcode();
  551. int FrameDestroyOpcode = TRI.getCallFrameDestroyOpcode();
  552. for (MachineFunction::iterator BB = Fn.begin(),
  553. E = Fn.end(); BB != E; ++BB) {
  554. int SPAdj = 0; // SP offset due to call frame setup / destroy.
  555. if (RS && !FrameIndexVirtualScavenging) RS->enterBasicBlock(BB);
  556. for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
  557. if (I->getOpcode() == FrameSetupOpcode ||
  558. I->getOpcode() == FrameDestroyOpcode) {
  559. // Remember how much SP has been adjusted to create the call
  560. // frame.
  561. int Size = I->getOperand(0).getImm();
  562. if ((!StackGrowsDown && I->getOpcode() == FrameSetupOpcode) ||
  563. (StackGrowsDown && I->getOpcode() == FrameDestroyOpcode))
  564. Size = -Size;
  565. SPAdj += Size;
  566. MachineBasicBlock::iterator PrevI = BB->end();
  567. if (I != BB->begin()) PrevI = prior(I);
  568. TRI.eliminateCallFramePseudoInstr(Fn, *BB, I);
  569. // Visit the instructions created by eliminateCallFramePseudoInstr().
  570. if (PrevI == BB->end())
  571. I = BB->begin(); // The replaced instr was the first in the block.
  572. else
  573. I = llvm::next(PrevI);
  574. continue;
  575. }
  576. MachineInstr *MI = I;
  577. bool DoIncr = true;
  578. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
  579. if (MI->getOperand(i).isFI()) {
  580. // Some instructions (e.g. inline asm instructions) can have
  581. // multiple frame indices and/or cause eliminateFrameIndex
  582. // to insert more than one instruction. We need the register
  583. // scavenger to go through all of these instructions so that
  584. // it can update its register information. We keep the
  585. // iterator at the point before insertion so that we can
  586. // revisit them in full.
  587. bool AtBeginning = (I == BB->begin());
  588. if (!AtBeginning) --I;
  589. // If this instruction has a FrameIndex operand, we need to
  590. // use that target machine register info object to eliminate
  591. // it.
  592. int Value;
  593. unsigned VReg =
  594. TRI.eliminateFrameIndex(MI, SPAdj, &Value,
  595. FrameIndexVirtualScavenging ? NULL : RS);
  596. if (VReg) {
  597. assert (FrameIndexVirtualScavenging &&
  598. "Not scavenging, but virtual returned from "
  599. "eliminateFrameIndex()!");
  600. FrameConstantRegMap[VReg] = FrameConstantEntry(Value, SPAdj);
  601. }
  602. // Reset the iterator if we were at the beginning of the BB.
  603. if (AtBeginning) {
  604. I = BB->begin();
  605. DoIncr = false;
  606. }
  607. MI = 0;
  608. break;
  609. }
  610. if (DoIncr && I != BB->end()) ++I;
  611. // Update register states.
  612. if (RS && !FrameIndexVirtualScavenging && MI) RS->forward(MI);
  613. }
  614. assert(SPAdj == 0 && "Unbalanced call frame setup / destroy pairs?");
  615. }
  616. }
  617. /// findLastUseReg - find the killing use of the specified register within
  618. /// the instruciton range. Return the operand number of the kill in Operand.
  619. static MachineBasicBlock::iterator
  620. findLastUseReg(MachineBasicBlock::iterator I, MachineBasicBlock::iterator ME,
  621. unsigned Reg) {
  622. // Scan forward to find the last use of this virtual register
  623. for (++I; I != ME; ++I) {
  624. MachineInstr *MI = I;
  625. bool isDefInsn = false;
  626. bool isKillInsn = false;
  627. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
  628. if (MI->getOperand(i).isReg()) {
  629. unsigned OpReg = MI->getOperand(i).getReg();
  630. if (OpReg == 0 || !TargetRegisterInfo::isVirtualRegister(OpReg))
  631. continue;
  632. assert (OpReg == Reg
  633. && "overlapping use of scavenged index register!");
  634. // If this is the killing use, we have a candidate.
  635. if (MI->getOperand(i).isKill())
  636. isKillInsn = true;
  637. else if (MI->getOperand(i).isDef())
  638. isDefInsn = true;
  639. }
  640. if (isKillInsn && !isDefInsn)
  641. return I;
  642. }
  643. // If we hit the end of the basic block, there was no kill of
  644. // the virtual register, which is wrong.
  645. assert (0 && "scavenged index register never killed!");
  646. return ME;
  647. }
  648. /// scavengeFrameVirtualRegs - Replace all frame index virtual registers
  649. /// with physical registers. Use the register scavenger to find an
  650. /// appropriate register to use.
  651. void PEI::scavengeFrameVirtualRegs(MachineFunction &Fn) {
  652. // Run through the instructions and find any virtual registers.
  653. for (MachineFunction::iterator BB = Fn.begin(),
  654. E = Fn.end(); BB != E; ++BB) {
  655. RS->enterBasicBlock(BB);
  656. // FIXME: The logic flow in this function is still too convoluted.
  657. // It needs a cleanup refactoring. Do that in preparation for tracking
  658. // more than one scratch register value and using ranges to find
  659. // available scratch registers.
  660. unsigned CurrentVirtReg = 0;
  661. unsigned CurrentScratchReg = 0;
  662. bool havePrevValue = false;
  663. int PrevValue = 0;
  664. MachineInstr *PrevLastUseMI = NULL;
  665. unsigned PrevLastUseOp = 0;
  666. bool trackingCurrentValue = false;
  667. int SPAdj = 0;
  668. int Value = 0;
  669. // The instruction stream may change in the loop, so check BB->end()
  670. // directly.
  671. for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
  672. MachineInstr *MI = I;
  673. bool isDefInsn = false;
  674. bool isKillInsn = false;
  675. bool clobbersScratchReg = false;
  676. bool DoIncr = true;
  677. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  678. if (MI->getOperand(i).isReg()) {
  679. MachineOperand &MO = MI->getOperand(i);
  680. unsigned Reg = MO.getReg();
  681. if (Reg == 0)
  682. continue;
  683. if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
  684. // If we have a previous scratch reg, check and see if anything
  685. // here kills whatever value is in there.
  686. if (Reg == CurrentScratchReg) {
  687. if (MO.isUse()) {
  688. // Two-address operands implicitly kill
  689. if (MO.isKill() || MI->isRegTiedToDefOperand(i))
  690. clobbersScratchReg = true;
  691. } else {
  692. assert (MO.isDef());
  693. clobbersScratchReg = true;
  694. }
  695. }
  696. continue;
  697. }
  698. // If this is a def, remember that this insn defines the value.
  699. // This lets us properly consider insns which re-use the scratch
  700. // register, such as r2 = sub r2, #imm, in the middle of the
  701. // scratch range.
  702. if (MO.isDef())
  703. isDefInsn = true;
  704. // Have we already allocated a scratch register for this virtual?
  705. if (Reg != CurrentVirtReg) {
  706. // When we first encounter a new virtual register, it
  707. // must be a definition.
  708. assert(MI->getOperand(i).isDef() &&
  709. "frame index virtual missing def!");
  710. // We can't have nested virtual register live ranges because
  711. // there's only a guarantee of one scavenged register at a time.
  712. assert (CurrentVirtReg == 0 &&
  713. "overlapping frame index virtual registers!");
  714. // If the target gave us information about what's in the register,
  715. // we can use that to re-use scratch regs.
  716. DenseMap<unsigned, FrameConstantEntry>::iterator Entry =
  717. FrameConstantRegMap.find(Reg);
  718. trackingCurrentValue = Entry != FrameConstantRegMap.end();
  719. if (trackingCurrentValue) {
  720. SPAdj = (*Entry).second.second;
  721. Value = (*Entry).second.first;
  722. } else
  723. SPAdj = Value = 0;
  724. // If the scratch register from the last allocation is still
  725. // available, see if the value matches. If it does, just re-use it.
  726. if (trackingCurrentValue && havePrevValue && PrevValue == Value) {
  727. // FIXME: This assumes that the instructions in the live range
  728. // for the virtual register are exclusively for the purpose
  729. // of populating the value in the register. That's reasonable
  730. // for these frame index registers, but it's still a very, very
  731. // strong assumption. rdar://7322732. Better would be to
  732. // explicitly check each instruction in the range for references
  733. // to the virtual register. Only delete those insns that
  734. // touch the virtual register.
  735. // Find the last use of the new virtual register. Remove all
  736. // instruction between here and there, and update the current
  737. // instruction to reference the last use insn instead.
  738. MachineBasicBlock::iterator LastUseMI =
  739. findLastUseReg(I, BB->end(), Reg);
  740. // Remove all instructions up 'til the last use, since they're
  741. // just calculating the value we already have.
  742. BB->erase(I, LastUseMI);
  743. I = LastUseMI;
  744. // Extend the live range of the scratch register
  745. PrevLastUseMI->getOperand(PrevLastUseOp).setIsKill(false);
  746. RS->setUsed(CurrentScratchReg);
  747. CurrentVirtReg = Reg;
  748. // We deleted the instruction we were scanning the operands of.
  749. // Jump back to the instruction iterator loop. Don't increment
  750. // past this instruction since we updated the iterator already.
  751. DoIncr = false;
  752. break;
  753. }
  754. // Scavenge a new scratch register
  755. CurrentVirtReg = Reg;
  756. const TargetRegisterClass *RC = Fn.getRegInfo().getRegClass(Reg);
  757. CurrentScratchReg = RS->FindUnusedReg(RC);
  758. if (CurrentScratchReg == 0)
  759. // No register is "free". Scavenge a register.
  760. CurrentScratchReg = RS->scavengeRegister(RC, I, SPAdj);
  761. PrevValue = Value;
  762. }
  763. // replace this reference to the virtual register with the
  764. // scratch register.
  765. assert (CurrentScratchReg && "Missing scratch register!");
  766. MI->getOperand(i).setReg(CurrentScratchReg);
  767. if (MI->getOperand(i).isKill()) {
  768. isKillInsn = true;
  769. PrevLastUseOp = i;
  770. PrevLastUseMI = MI;
  771. }
  772. }
  773. }
  774. // If this is the last use of the scratch, stop tracking it. The
  775. // last use will be a kill operand in an instruction that does
  776. // not also define the scratch register.
  777. if (isKillInsn && !isDefInsn) {
  778. CurrentVirtReg = 0;
  779. havePrevValue = trackingCurrentValue;
  780. }
  781. // Similarly, notice if instruction clobbered the value in the
  782. // register we're tracking for possible later reuse. This is noted
  783. // above, but enforced here since the value is still live while we
  784. // process the rest of the operands of the instruction.
  785. if (clobbersScratchReg) {
  786. havePrevValue = false;
  787. CurrentScratchReg = 0;
  788. }
  789. if (DoIncr) {
  790. RS->forward(I);
  791. ++I;
  792. }
  793. }
  794. }
  795. }