TargetFrameLoweringImpl.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //===- TargetFrameLoweringImpl.cpp - Implement target frame interface ------==//
  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. // Implements the layout of a stack frame on the target machine.
  11. //
  12. //===----------------------------------------------------------------------===//
  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/TargetFrameLowering.h"
  18. #include "llvm/CodeGen/TargetRegisterInfo.h"
  19. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  20. #include "llvm/IR/Attributes.h"
  21. #include "llvm/IR/CallingConv.h"
  22. #include "llvm/IR/Function.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. int TargetFrameLowering::getFrameIndexReference(const MachineFunction &MF,
  40. int FI, unsigned &FrameReg) const {
  41. const MachineFrameInfo &MFI = MF.getFrameInfo();
  42. const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
  43. // By default, assume all frame indices are referenced via whatever
  44. // getFrameRegister() says. The target can override this if it's doing
  45. // something different.
  46. FrameReg = RI->getFrameRegister(MF);
  47. return MFI.getObjectOffset(FI) + MFI.getStackSize() -
  48. getOffsetOfLocalArea() + MFI.getOffsetAdjustment();
  49. }
  50. bool TargetFrameLowering::needsFrameIndexResolution(
  51. const MachineFunction &MF) const {
  52. return MF.getFrameInfo().hasStackObjects();
  53. }
  54. void TargetFrameLowering::determineCalleeSaves(MachineFunction &MF,
  55. BitVector &SavedRegs,
  56. RegScavenger *RS) const {
  57. const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
  58. // Resize before the early returns. Some backends expect that
  59. // SavedRegs.size() == TRI.getNumRegs() after this call even if there are no
  60. // saved registers.
  61. SavedRegs.resize(TRI.getNumRegs());
  62. // When interprocedural register allocation is enabled caller saved registers
  63. // are preferred over callee saved registers.
  64. if (MF.getTarget().Options.EnableIPRA && isSafeForNoCSROpt(MF.getFunction()))
  65. return;
  66. // Get the callee saved register list...
  67. const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs();
  68. // Early exit if there are no callee saved registers.
  69. if (!CSRegs || CSRegs[0] == 0)
  70. return;
  71. // In Naked functions we aren't going to save any registers.
  72. if (MF.getFunction().hasFnAttribute(Attribute::Naked))
  73. return;
  74. // Noreturn+nounwind functions never restore CSR, so no saves are needed.
  75. // Purely noreturn functions may still return through throws, so those must
  76. // save CSR for caller exception handlers.
  77. //
  78. // If the function uses longjmp to break out of its current path of
  79. // execution we do not need the CSR spills either: setjmp stores all CSRs
  80. // it was called with into the jmp_buf, which longjmp then restores.
  81. if (MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
  82. MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
  83. !MF.getFunction().hasFnAttribute(Attribute::UWTable) &&
  84. enableCalleeSaveSkip(MF))
  85. return;
  86. // Functions which call __builtin_unwind_init get all their registers saved.
  87. bool CallsUnwindInit = MF.callsUnwindInit();
  88. const MachineRegisterInfo &MRI = MF.getRegInfo();
  89. for (unsigned i = 0; CSRegs[i]; ++i) {
  90. unsigned Reg = CSRegs[i];
  91. if (CallsUnwindInit || MRI.isPhysRegModified(Reg))
  92. SavedRegs.set(Reg);
  93. }
  94. }
  95. unsigned TargetFrameLowering::getStackAlignmentSkew(
  96. const MachineFunction &MF) const {
  97. // When HHVM function is called, the stack is skewed as the return address
  98. // is removed from the stack before we enter the function.
  99. if (LLVM_UNLIKELY(MF.getFunction().getCallingConv() == CallingConv::HHVM))
  100. return MF.getTarget().getAllocaPointerSize();
  101. return 0;
  102. }
  103. int TargetFrameLowering::getInitialCFAOffset(const MachineFunction &MF) const {
  104. llvm_unreachable("getInitialCFAOffset() not implemented!");
  105. }
  106. unsigned TargetFrameLowering::getInitialCFARegister(const MachineFunction &MF)
  107. const {
  108. llvm_unreachable("getInitialCFARegister() not implemented!");
  109. }