TargetFrameLoweringImpl.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/MachineModuleInfo.h"
  17. #include "llvm/CodeGen/MachineRegisterInfo.h"
  18. #include "llvm/CodeGen/TargetPassConfig.h"
  19. #include "llvm/IR/CallingConv.h"
  20. #include "llvm/IR/Function.h"
  21. #include "llvm/Target/TargetFrameLowering.h"
  22. #include "llvm/Target/TargetRegisterInfo.h"
  23. #include "llvm/Target/TargetSubtargetInfo.h"
  24. #include <cstdlib>
  25. using namespace llvm;
  26. TargetFrameLowering::~TargetFrameLowering() {
  27. }
  28. /// The default implementation just looks at attribute "no-frame-pointer-elim".
  29. bool TargetFrameLowering::noFramePointerElim(const MachineFunction &MF) const {
  30. auto Attr = MF.getFunction()->getFnAttribute("no-frame-pointer-elim");
  31. return Attr.getValueAsString() == "true";
  32. }
  33. /// Returns the displacement from the frame register to the stack
  34. /// frame of the specified index, along with the frame register used
  35. /// (in output arg FrameReg). This is the default implementation which
  36. /// is overridden for some targets.
  37. int TargetFrameLowering::getFrameIndexReference(const MachineFunction &MF,
  38. int FI, unsigned &FrameReg) const {
  39. const MachineFrameInfo &MFI = MF.getFrameInfo();
  40. const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
  41. // By default, assume all frame indices are referenced via whatever
  42. // getFrameRegister() says. The target can override this if it's doing
  43. // something different.
  44. FrameReg = RI->getFrameRegister(MF);
  45. return MFI.getObjectOffset(FI) + MFI.getStackSize() -
  46. getOffsetOfLocalArea() + MFI.getOffsetAdjustment();
  47. }
  48. bool TargetFrameLowering::needsFrameIndexResolution(
  49. const MachineFunction &MF) const {
  50. return MF.getFrameInfo().hasStackObjects();
  51. }
  52. void TargetFrameLowering::determineCalleeSaves(MachineFunction &MF,
  53. BitVector &SavedRegs,
  54. RegScavenger *RS) const {
  55. const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
  56. // Resize before the early returns. Some backends expect that
  57. // SavedRegs.size() == TRI.getNumRegs() after this call even if there are no
  58. // saved registers.
  59. SavedRegs.resize(TRI.getNumRegs());
  60. // When interprocedural register allocation is enabled caller saved registers
  61. // are preferred over callee saved registers.
  62. if (MF.getTarget().Options.EnableIPRA && isSafeForNoCSROpt(MF.getFunction()))
  63. return;
  64. // Get the callee saved register list...
  65. const MCPhysReg *CSRegs = TRI.getCalleeSavedRegs(&MF);
  66. // Early exit if there are no callee saved registers.
  67. if (!CSRegs || CSRegs[0] == 0)
  68. return;
  69. // In Naked functions we aren't going to save any registers.
  70. if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
  71. return;
  72. // Functions which call __builtin_unwind_init get all their registers saved.
  73. bool CallsUnwindInit = MF.getMMI().callsUnwindInit();
  74. const MachineRegisterInfo &MRI = MF.getRegInfo();
  75. for (unsigned i = 0; CSRegs[i]; ++i) {
  76. unsigned Reg = CSRegs[i];
  77. if (CallsUnwindInit || MRI.isPhysRegModified(Reg))
  78. SavedRegs.set(Reg);
  79. }
  80. }
  81. unsigned TargetFrameLowering::getStackAlignmentSkew(
  82. const MachineFunction &MF) const {
  83. // When HHVM function is called, the stack is skewed as the return address
  84. // is removed from the stack before we enter the function.
  85. if (LLVM_UNLIKELY(MF.getFunction()->getCallingConv() == CallingConv::HHVM))
  86. return MF.getTarget().getPointerSize();
  87. return 0;
  88. }