PrologEpilogInserter.cpp 22 KB

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