MachineFrameInfo.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //===-- MachineFrameInfo.cpp ---------------------------------------------===//
  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. /// \file Implements MachineFrameInfo that manages the stack frame.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/MachineFrameInfo.h"
  14. #include "llvm/ADT/BitVector.h"
  15. #include "llvm/CodeGen/MachineFunction.h"
  16. #include "llvm/CodeGen/MachineRegisterInfo.h"
  17. #include "llvm/CodeGen/TargetFrameLowering.h"
  18. #include "llvm/CodeGen/TargetInstrInfo.h"
  19. #include "llvm/CodeGen/TargetRegisterInfo.h"
  20. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  21. #include "llvm/Config/llvm-config.h"
  22. #include "llvm/Support/Debug.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. #include <cassert>
  25. #define DEBUG_TYPE "codegen"
  26. using namespace llvm;
  27. void MachineFrameInfo::ensureMaxAlignment(unsigned Align) {
  28. if (!StackRealignable)
  29. assert(Align <= StackAlignment &&
  30. "For targets without stack realignment, Align is out of limit!");
  31. if (MaxAlignment < Align) MaxAlignment = Align;
  32. }
  33. /// Clamp the alignment if requested and emit a warning.
  34. static inline unsigned clampStackAlignment(bool ShouldClamp, unsigned Align,
  35. unsigned StackAlign) {
  36. if (!ShouldClamp || Align <= StackAlign)
  37. return Align;
  38. DEBUG(dbgs() << "Warning: requested alignment " << Align
  39. << " exceeds the stack alignment " << StackAlign
  40. << " when stack realignment is off" << '\n');
  41. return StackAlign;
  42. }
  43. int MachineFrameInfo::CreateStackObject(uint64_t Size, unsigned Alignment,
  44. bool IsSpillSlot,
  45. const AllocaInst *Alloca,
  46. uint8_t StackID) {
  47. assert(Size != 0 && "Cannot allocate zero size stack objects!");
  48. Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
  49. Objects.push_back(StackObject(Size, Alignment, 0, false, IsSpillSlot, Alloca,
  50. !IsSpillSlot, StackID));
  51. int Index = (int)Objects.size() - NumFixedObjects - 1;
  52. assert(Index >= 0 && "Bad frame index!");
  53. ensureMaxAlignment(Alignment);
  54. return Index;
  55. }
  56. int MachineFrameInfo::CreateSpillStackObject(uint64_t Size,
  57. unsigned Alignment) {
  58. Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
  59. CreateStackObject(Size, Alignment, true);
  60. int Index = (int)Objects.size() - NumFixedObjects - 1;
  61. ensureMaxAlignment(Alignment);
  62. return Index;
  63. }
  64. int MachineFrameInfo::CreateVariableSizedObject(unsigned Alignment,
  65. const AllocaInst *Alloca) {
  66. HasVarSizedObjects = true;
  67. Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
  68. Objects.push_back(StackObject(0, Alignment, 0, false, false, Alloca, true));
  69. ensureMaxAlignment(Alignment);
  70. return (int)Objects.size()-NumFixedObjects-1;
  71. }
  72. int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
  73. bool IsImmutable, bool IsAliased) {
  74. assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
  75. // The alignment of the frame index can be determined from its offset from
  76. // the incoming frame position. If the frame object is at offset 32 and
  77. // the stack is guaranteed to be 16-byte aligned, then we know that the
  78. // object is 16-byte aligned. Note that unlike the non-fixed case, if the
  79. // stack needs realignment, we can't assume that the stack will in fact be
  80. // aligned.
  81. unsigned Alignment = MinAlign(SPOffset, ForcedRealign ? 1 : StackAlignment);
  82. Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
  83. Objects.insert(Objects.begin(),
  84. StackObject(Size, Alignment, SPOffset, IsImmutable,
  85. /*isSpillSlot=*/false, /*Alloca=*/nullptr,
  86. IsAliased));
  87. return -++NumFixedObjects;
  88. }
  89. int MachineFrameInfo::CreateFixedSpillStackObject(uint64_t Size,
  90. int64_t SPOffset,
  91. bool IsImmutable) {
  92. unsigned Alignment = MinAlign(SPOffset, ForcedRealign ? 1 : StackAlignment);
  93. Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
  94. Objects.insert(Objects.begin(),
  95. StackObject(Size, Alignment, SPOffset, IsImmutable,
  96. /*IsSpillSlot=*/true, /*Alloca=*/nullptr,
  97. /*IsAliased=*/false));
  98. return -++NumFixedObjects;
  99. }
  100. BitVector MachineFrameInfo::getPristineRegs(const MachineFunction &MF) const {
  101. const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
  102. BitVector BV(TRI->getNumRegs());
  103. // Before CSI is calculated, no registers are considered pristine. They can be
  104. // freely used and PEI will make sure they are saved.
  105. if (!isCalleeSavedInfoValid())
  106. return BV;
  107. const MachineRegisterInfo &MRI = MF.getRegInfo();
  108. for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR;
  109. ++CSR)
  110. BV.set(*CSR);
  111. // Saved CSRs are not pristine.
  112. for (auto &I : getCalleeSavedInfo())
  113. for (MCSubRegIterator S(I.getReg(), TRI, true); S.isValid(); ++S)
  114. BV.reset(*S);
  115. return BV;
  116. }
  117. unsigned MachineFrameInfo::estimateStackSize(const MachineFunction &MF) const {
  118. const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
  119. const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
  120. unsigned MaxAlign = getMaxAlignment();
  121. int Offset = 0;
  122. // This code is very, very similar to PEI::calculateFrameObjectOffsets().
  123. // It really should be refactored to share code. Until then, changes
  124. // should keep in mind that there's tight coupling between the two.
  125. for (int i = getObjectIndexBegin(); i != 0; ++i) {
  126. int FixedOff = -getObjectOffset(i);
  127. if (FixedOff > Offset) Offset = FixedOff;
  128. }
  129. for (unsigned i = 0, e = getObjectIndexEnd(); i != e; ++i) {
  130. if (isDeadObjectIndex(i))
  131. continue;
  132. Offset += getObjectSize(i);
  133. unsigned Align = getObjectAlignment(i);
  134. // Adjust to alignment boundary
  135. Offset = (Offset+Align-1)/Align*Align;
  136. MaxAlign = std::max(Align, MaxAlign);
  137. }
  138. if (adjustsStack() && TFI->hasReservedCallFrame(MF))
  139. Offset += getMaxCallFrameSize();
  140. // Round up the size to a multiple of the alignment. If the function has
  141. // any calls or alloca's, align to the target's StackAlignment value to
  142. // ensure that the callee's frame or the alloca data is suitably aligned;
  143. // otherwise, for leaf functions, align to the TransientStackAlignment
  144. // value.
  145. unsigned StackAlign;
  146. if (adjustsStack() || hasVarSizedObjects() ||
  147. (RegInfo->needsStackRealignment(MF) && getObjectIndexEnd() != 0))
  148. StackAlign = TFI->getStackAlignment();
  149. else
  150. StackAlign = TFI->getTransientStackAlignment();
  151. // If the frame pointer is eliminated, all frame offsets will be relative to
  152. // SP not FP. Align to MaxAlign so this works.
  153. StackAlign = std::max(StackAlign, MaxAlign);
  154. unsigned AlignMask = StackAlign - 1;
  155. Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
  156. return (unsigned)Offset;
  157. }
  158. void MachineFrameInfo::computeMaxCallFrameSize(const MachineFunction &MF) {
  159. const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
  160. unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
  161. unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
  162. assert(FrameSetupOpcode != ~0u && FrameDestroyOpcode != ~0u &&
  163. "Can only compute MaxCallFrameSize if Setup/Destroy opcode are known");
  164. MaxCallFrameSize = 0;
  165. for (const MachineBasicBlock &MBB : MF) {
  166. for (const MachineInstr &MI : MBB) {
  167. unsigned Opcode = MI.getOpcode();
  168. if (Opcode == FrameSetupOpcode || Opcode == FrameDestroyOpcode) {
  169. unsigned Size = TII.getFrameSize(MI);
  170. MaxCallFrameSize = std::max(MaxCallFrameSize, Size);
  171. AdjustsStack = true;
  172. } else if (MI.isInlineAsm()) {
  173. // Some inline asm's need a stack frame, as indicated by operand 1.
  174. unsigned ExtraInfo = MI.getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
  175. if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
  176. AdjustsStack = true;
  177. }
  178. }
  179. }
  180. }
  181. void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
  182. if (Objects.empty()) return;
  183. const TargetFrameLowering *FI = MF.getSubtarget().getFrameLowering();
  184. int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
  185. OS << "Frame Objects:\n";
  186. for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
  187. const StackObject &SO = Objects[i];
  188. OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
  189. if (SO.StackID != 0)
  190. OS << "id=" << static_cast<unsigned>(SO.StackID) << ' ';
  191. if (SO.Size == ~0ULL) {
  192. OS << "dead\n";
  193. continue;
  194. }
  195. if (SO.Size == 0)
  196. OS << "variable sized";
  197. else
  198. OS << "size=" << SO.Size;
  199. OS << ", align=" << SO.Alignment;
  200. if (i < NumFixedObjects)
  201. OS << ", fixed";
  202. if (i < NumFixedObjects || SO.SPOffset != -1) {
  203. int64_t Off = SO.SPOffset - ValOffset;
  204. OS << ", at location [SP";
  205. if (Off > 0)
  206. OS << "+" << Off;
  207. else if (Off < 0)
  208. OS << Off;
  209. OS << "]";
  210. }
  211. OS << "\n";
  212. }
  213. }
  214. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  215. LLVM_DUMP_METHOD void MachineFrameInfo::dump(const MachineFunction &MF) const {
  216. print(MF, dbgs());
  217. }
  218. #endif