PrologEpilogInserter.cpp 40 KB

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