PrologEpilogInserter.cpp 39 KB

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