TargetFrameLoweringImpl.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //===- TargetFrameLoweringImpl.cpp - Implement target frame interface ------==//
  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. // Implements the layout of a stack frame on the target machine.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/TargetFrameLowering.h"
  13. #include "llvm/ADT/BitVector.h"
  14. #include "llvm/CodeGen/MachineFrameInfo.h"
  15. #include "llvm/CodeGen/MachineFunction.h"
  16. #include "llvm/CodeGen/MachineRegisterInfo.h"
  17. #include "llvm/CodeGen/TargetRegisterInfo.h"
  18. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  19. #include "llvm/IR/Attributes.h"
  20. #include "llvm/IR/CallingConv.h"
  21. #include "llvm/IR/Function.h"
  22. #include "llvm/IR/InstrTypes.h"
  23. #include "llvm/MC/MCRegisterInfo.h"
  24. #include "llvm/Support/Compiler.h"
  25. #include "llvm/Target/TargetMachine.h"
  26. #include "llvm/Target/TargetOptions.h"
  27. using namespace llvm;
  28. TargetFrameLowering::~TargetFrameLowering() = default;
  29. bool TargetFrameLowering::enableCalleeSaveSkip(const MachineFunction &MF) const {
  30. assert(MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
  31. MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
  32. !MF.getFunction().hasFnAttribute(Attribute::UWTable));
  33. return false;
  34. }
  35. /// Returns the displacement from the frame register to the stack
  36. /// frame of the specified index, along with the frame register used
  37. /// (in output arg FrameReg). This is the default implementation which
  38. /// is overridden for some targets.
  39. StackOffset
  40. TargetFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
  41. Register &FrameReg) const {
  42. const MachineFrameInfo &MFI = MF.getFrameInfo();
  43. const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
  44. // By default, assume all frame indices are referenced via whatever
  45. // getFrameRegister() says. The target can override this if it's doing
  46. // something different.
  47. FrameReg = RI->getFrameRegister(MF);
  48. return StackOffset::getFixed(MFI.getObjectOffset(FI) + MFI.getStackSize() -
  49. getOffsetOfLocalArea() +
  50. MFI.getOffsetAdjustment());
  51. }
  52. bool TargetFrameLowering::needsFrameIndexResolution(
  53. const MachineFunction &MF) const {
  54. return MF.getFrameInfo().hasStackObjects();
  55. }
  56. void TargetFrameLowering::getCalleeSaves(const MachineFunction &MF,
  57. BitVector &CalleeSaves) const {
  58. const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
  59. CalleeSaves.resize(TRI.getNumRegs());
  60. const MachineFrameInfo &MFI = MF.getFrameInfo();
  61. if (!MFI.isCalleeSavedInfoValid())
  62. return;
  63. for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
  64. CalleeSaves.set(Info.getReg());
  65. }
  66. void TargetFrameLowering::determineCalleeSaves(MachineFunction &MF,
  67. BitVector &SavedRegs,
  68. RegScavenger *RS) const {
  69. const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
  70. // Resize before the early returns. Some backends expect that
  71. // SavedRegs.size() == TRI.getNumRegs() after this call even if there are no
  72. // saved registers.
  73. SavedRegs.resize(TRI.getNumRegs());
  74. // When interprocedural register allocation is enabled caller saved registers
  75. // are preferred over callee saved registers.
  76. if (MF.getTarget().Options.EnableIPRA &&
  77. isSafeForNoCSROpt(MF.getFunction()) &&
  78. isProfitableForNoCSROpt(MF.getFunction()))
  79. return;
  80. // Get the callee saved register list...
  81. const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs();
  82. // Early exit if there are no callee saved registers.
  83. if (!CSRegs || CSRegs[0] == 0)
  84. return;
  85. // In Naked functions we aren't going to save any registers.
  86. if (MF.getFunction().hasFnAttribute(Attribute::Naked))
  87. return;
  88. // Noreturn+nounwind functions never restore CSR, so no saves are needed.
  89. // Purely noreturn functions may still return through throws, so those must
  90. // save CSR for caller exception handlers.
  91. //
  92. // If the function uses longjmp to break out of its current path of
  93. // execution we do not need the CSR spills either: setjmp stores all CSRs
  94. // it was called with into the jmp_buf, which longjmp then restores.
  95. if (MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
  96. MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
  97. !MF.getFunction().hasFnAttribute(Attribute::UWTable) &&
  98. enableCalleeSaveSkip(MF))
  99. return;
  100. // Functions which call __builtin_unwind_init get all their registers saved.
  101. bool CallsUnwindInit = MF.callsUnwindInit();
  102. const MachineRegisterInfo &MRI = MF.getRegInfo();
  103. for (unsigned i = 0; CSRegs[i]; ++i) {
  104. unsigned Reg = CSRegs[i];
  105. if (CallsUnwindInit || MRI.isPhysRegModified(Reg))
  106. SavedRegs.set(Reg);
  107. }
  108. }
  109. unsigned TargetFrameLowering::getStackAlignmentSkew(
  110. const MachineFunction &MF) const {
  111. // When HHVM function is called, the stack is skewed as the return address
  112. // is removed from the stack before we enter the function.
  113. if (LLVM_UNLIKELY(MF.getFunction().getCallingConv() == CallingConv::HHVM))
  114. return MF.getTarget().getAllocaPointerSize();
  115. return 0;
  116. }
  117. bool TargetFrameLowering::allocateScavengingFrameIndexesNearIncomingSP(
  118. const MachineFunction &MF) const {
  119. if (!hasFP(MF))
  120. return false;
  121. const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
  122. return RegInfo->useFPForScavengingIndex(MF) &&
  123. !RegInfo->hasStackRealignment(MF);
  124. }
  125. bool TargetFrameLowering::isSafeForNoCSROpt(const Function &F) {
  126. if (!F.hasLocalLinkage() || F.hasAddressTaken() ||
  127. !F.hasFnAttribute(Attribute::NoRecurse))
  128. return false;
  129. // Function should not be optimized as tail call.
  130. for (const User *U : F.users())
  131. if (auto *CB = dyn_cast<CallBase>(U))
  132. if (CB->isTailCall())
  133. return false;
  134. return true;
  135. }
  136. int TargetFrameLowering::getInitialCFAOffset(const MachineFunction &MF) const {
  137. llvm_unreachable("getInitialCFAOffset() not implemented!");
  138. }
  139. Register
  140. TargetFrameLowering::getInitialCFARegister(const MachineFunction &MF) const {
  141. llvm_unreachable("getInitialCFARegister() not implemented!");
  142. }
  143. TargetFrameLowering::DwarfFrameBase
  144. TargetFrameLowering::getDwarfFrameBase(const MachineFunction &MF) const {
  145. const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
  146. return DwarfFrameBase{DwarfFrameBase::Register, {RI->getFrameRegister(MF)}};
  147. }