PrologEpilogInserter.cpp 34 KB

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