TargetFrameLoweringImpl.cpp 6.7 KB

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