MachineFrameInfo.cpp 9.8 KB

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