MachineFrameInfo.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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(llvm::Align Align) {
  27. if (!StackRealignable)
  28. assert(Align <= StackAlignment &&
  29. "For targets without stack realignment, Align is out of limit!");
  30. if (MaxAlignment < Align) MaxAlignment = Align;
  31. }
  32. /// Clamp the alignment if requested and emit a warning.
  33. static inline llvm::Align clampStackAlignment(bool ShouldClamp,
  34. llvm::Align Align,
  35. llvm::Align StackAlign) {
  36. if (!ShouldClamp || Align <= StackAlign)
  37. return Align;
  38. LLVM_DEBUG(dbgs() << "Warning: requested alignment " << Align.value()
  39. << " exceeds the stack alignment " << StackAlign.value()
  40. << " when stack realignment is off" << '\n');
  41. return StackAlign;
  42. }
  43. int MachineFrameInfo::CreateStackObject(uint64_t Size, llvm::Align 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. if (StackID == 0)
  54. ensureMaxAlignment(Alignment);
  55. return Index;
  56. }
  57. int MachineFrameInfo::CreateSpillStackObject(uint64_t Size,
  58. llvm::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(llvm::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. llvm::Align Alignment = commonAlignment(
  83. ForcedRealign ? llvm::Align::None() : 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. llvm::Align Alignment = commonAlignment(
  95. ForcedRealign ? llvm::Align::None() : 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 (auto &I : getCalleeSavedInfo())
  116. for (MCSubRegIterator S(I.getReg(), TRI, true); S.isValid(); ++S)
  117. BV.reset(*S);
  118. return BV;
  119. }
  120. unsigned MachineFrameInfo::estimateStackSize(const MachineFunction &MF) const {
  121. const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
  122. const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
  123. unsigned MaxAlign = getMaxAlignment();
  124. int 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. int 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. unsigned Align = getObjectAlignment(i);
  141. // Adjust to alignment boundary
  142. Offset = (Offset+Align-1)/Align*Align;
  143. MaxAlign = std::max(Align, 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. unsigned StackAlign;
  153. if (adjustsStack() || hasVarSizedObjects() ||
  154. (RegInfo->needsStackRealignment(MF) && getObjectIndexEnd() != 0))
  155. StackAlign = TFI->getStackAlignment();
  156. else
  157. StackAlign = TFI->getTransientStackAlignment();
  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. unsigned AlignMask = StackAlign - 1;
  162. Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
  163. return (unsigned)Offset;
  164. }
  165. void MachineFrameInfo::computeMaxCallFrameSize(const MachineFunction &MF) {
  166. const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
  167. unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
  168. unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
  169. assert(FrameSetupOpcode != ~0u && FrameDestroyOpcode != ~0u &&
  170. "Can only compute MaxCallFrameSize if Setup/Destroy opcode are known");
  171. MaxCallFrameSize = 0;
  172. for (const MachineBasicBlock &MBB : MF) {
  173. for (const MachineInstr &MI : MBB) {
  174. unsigned Opcode = MI.getOpcode();
  175. if (Opcode == FrameSetupOpcode || Opcode == FrameDestroyOpcode) {
  176. unsigned Size = TII.getFrameSize(MI);
  177. MaxCallFrameSize = std::max(MaxCallFrameSize, Size);
  178. AdjustsStack = true;
  179. } else if (MI.isInlineAsm()) {
  180. // Some inline asm's need a stack frame, as indicated by operand 1.
  181. unsigned ExtraInfo = MI.getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
  182. if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
  183. AdjustsStack = true;
  184. }
  185. }
  186. }
  187. }
  188. void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
  189. if (Objects.empty()) return;
  190. const TargetFrameLowering *FI = MF.getSubtarget().getFrameLowering();
  191. int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
  192. OS << "Frame Objects:\n";
  193. for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
  194. const StackObject &SO = Objects[i];
  195. OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
  196. if (SO.StackID != 0)
  197. OS << "id=" << static_cast<unsigned>(SO.StackID) << ' ';
  198. if (SO.Size == ~0ULL) {
  199. OS << "dead\n";
  200. continue;
  201. }
  202. if (SO.Size == 0)
  203. OS << "variable sized";
  204. else
  205. OS << "size=" << SO.Size;
  206. OS << ", align=" << SO.Alignment.value();
  207. if (i < NumFixedObjects)
  208. OS << ", fixed";
  209. if (i < NumFixedObjects || SO.SPOffset != -1) {
  210. int64_t Off = SO.SPOffset - ValOffset;
  211. OS << ", at location [SP";
  212. if (Off > 0)
  213. OS << "+" << Off;
  214. else if (Off < 0)
  215. OS << Off;
  216. OS << "]";
  217. }
  218. OS << "\n";
  219. }
  220. }
  221. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  222. LLVM_DUMP_METHOD void MachineFrameInfo::dump(const MachineFunction &MF) const {
  223. print(MF, dbgs());
  224. }
  225. #endif