116c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikov//===-- llvm/Target/TargetFrameLowering.h ---------------------------*- C++ -*-===//
234695381d626485a560594f162701088079589dfMisha Brukman//
36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//                     The LLVM Compiler Infrastructure
46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
734695381d626485a560594f162701088079589dfMisha Brukman//
86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//===----------------------------------------------------------------------===//
9035dfbe7f2d109008d2d62d9f2a67efb477a7ab6Chris Lattner//
10fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner// Interface to describe the layout of a stack frame on the target machine.
11035dfbe7f2d109008d2d62d9f2a67efb477a7ab6Chris Lattner//
12035dfbe7f2d109008d2d62d9f2a67efb477a7ab6Chris Lattner//===----------------------------------------------------------------------===//
137cffcaaa48656fa6f90b041cc5f056304addef32Vikram S. Adve
1416c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikov#ifndef LLVM_TARGET_TARGETFRAMELOWERING_H
1516c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikov#define LLVM_TARGET_TARGETFRAMELOWERING_H
167cffcaaa48656fa6f90b041cc5f056304addef32Vikram S. Adve
17cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov#include "llvm/CodeGen/MachineBasicBlock.h"
1809431e187f9abd45af08461df728204ee9731fd7Chris Lattner#include <utility>
19d9e3385ced2dc887e2fe8e1c071bd2611e4d3edeAnton Korobeynikov#include <vector>
2009431e187f9abd45af08461df728204ee9731fd7Chris Lattner
21d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
22cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  class BitVector;
23cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov  class CalleeSavedInfo;
2433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov  class MachineFunction;
2594c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov  class RegScavenger;
26d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
27c07c0df634d31849ac659234e4718b57ddacb882Chris Lattner/// Information about stack frame layout on the target.  It holds the direction
28c07c0df634d31849ac659234e4718b57ddacb882Chris Lattner/// of stack growth, the known stack alignment on entry to each function, and
29c07c0df634d31849ac659234e4718b57ddacb882Chris Lattner/// the offset to the locals area.
30c07c0df634d31849ac659234e4718b57ddacb882Chris Lattner///
31c07c0df634d31849ac659234e4718b57ddacb882Chris Lattner/// The offset to the local area is the offset from the stack pointer on
32c07c0df634d31849ac659234e4718b57ddacb882Chris Lattner/// function entry to the first location where function data (local variables,
33c07c0df634d31849ac659234e4718b57ddacb882Chris Lattner/// spill locations) can be stored.
3416c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikovclass TargetFrameLowering {
351fca5ff62bb2ecb5bfc8974f4dbfc56e9d3ca721Chris Lattnerpublic:
36fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner  enum StackDirection {
37fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner    StackGrowsUp,        // Adding to the stack increases the stack address
38fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner    StackGrowsDown       // Adding to the stack decreases the stack address
39fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner  };
408ff95de83cbe85d939535d2f4fb5f9b2b721081aTilmann Scheller
418ff95de83cbe85d939535d2f4fb5f9b2b721081aTilmann Scheller  // Maps a callee saved register to a stack slot with a fixed offset.
428ff95de83cbe85d939535d2f4fb5f9b2b721081aTilmann Scheller  struct SpillSlot {
438ff95de83cbe85d939535d2f4fb5f9b2b721081aTilmann Scheller    unsigned Reg;
448ff95de83cbe85d939535d2f4fb5f9b2b721081aTilmann Scheller    int Offset; // Offset relative to stack pointer on function entry.
458ff95de83cbe85d939535d2f4fb5f9b2b721081aTilmann Scheller  };
46fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattnerprivate:
47fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner  StackDirection StackDir;
48fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner  unsigned StackAlignment;
490035f9c3b9982eeef098b608fceb7572df969b3eBob Wilson  unsigned TransientStackAlignment;
50fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner  int LocalAreaOffset;
5169261a644298bff1497d46c8cd38d688670f307bManman Ren  bool StackRealignable;
527cffcaaa48656fa6f90b041cc5f056304addef32Vikram S. Advepublic:
5316c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikov  TargetFrameLowering(StackDirection D, unsigned StackAl, int LAO,
5469261a644298bff1497d46c8cd38d688670f307bManman Ren                      unsigned TransAl = 1, bool StackReal = true)
550035f9c3b9982eeef098b608fceb7572df969b3eBob Wilson    : StackDir(D), StackAlignment(StackAl), TransientStackAlignment(TransAl),
5669261a644298bff1497d46c8cd38d688670f307bManman Ren      LocalAreaOffset(LAO), StackRealignable(StackReal) {}
57fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner
5816c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikov  virtual ~TargetFrameLowering();
59a1aad3b0eda2a516f663cb5f9f8c4c9bbb6ef224Reid Spencer
60fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner  // These methods return information that describes the abstract stack layout
61fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner  // of the target machine.
62fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner
63fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner  /// getStackGrowthDirection - Return the direction the stack grows
64fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner  ///
65fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner  StackDirection getStackGrowthDirection() const { return StackDir; }
66fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner
670035f9c3b9982eeef098b608fceb7572df969b3eBob Wilson  /// getStackAlignment - This method returns the number of bytes to which the
680035f9c3b9982eeef098b608fceb7572df969b3eBob Wilson  /// stack pointer must be aligned on entry to a function.  Typically, this
690035f9c3b9982eeef098b608fceb7572df969b3eBob Wilson  /// is the largest alignment for any data object in the target.
70fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner  ///
71fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner  unsigned getStackAlignment() const { return StackAlignment; }
72fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner
73cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  /// alignSPAdjust - This method aligns the stack adjustment to the correct
74cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  /// alignment.
75cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  ///
76cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  int alignSPAdjust(int SPAdj) const {
77cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    if (SPAdj < 0) {
78cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      SPAdj = -RoundUpToAlignment(-SPAdj, StackAlignment);
79cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    } else {
80cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      SPAdj = RoundUpToAlignment(SPAdj, StackAlignment);
81cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    }
82cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    return SPAdj;
83cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  }
84cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
850035f9c3b9982eeef098b608fceb7572df969b3eBob Wilson  /// getTransientStackAlignment - This method returns the number of bytes to
860035f9c3b9982eeef098b608fceb7572df969b3eBob Wilson  /// which the stack pointer must be aligned at all times, even between
870035f9c3b9982eeef098b608fceb7572df969b3eBob Wilson  /// calls.
880035f9c3b9982eeef098b608fceb7572df969b3eBob Wilson  ///
890035f9c3b9982eeef098b608fceb7572df969b3eBob Wilson  unsigned getTransientStackAlignment() const {
900035f9c3b9982eeef098b608fceb7572df969b3eBob Wilson    return TransientStackAlignment;
910035f9c3b9982eeef098b608fceb7572df969b3eBob Wilson  }
920035f9c3b9982eeef098b608fceb7572df969b3eBob Wilson
9369261a644298bff1497d46c8cd38d688670f307bManman Ren  /// isStackRealignable - This method returns whether the stack can be
9469261a644298bff1497d46c8cd38d688670f307bManman Ren  /// realigned.
9569261a644298bff1497d46c8cd38d688670f307bManman Ren  bool isStackRealignable() const {
9669261a644298bff1497d46c8cd38d688670f307bManman Ren    return StackRealignable;
9769261a644298bff1497d46c8cd38d688670f307bManman Ren  }
9869261a644298bff1497d46c8cd38d688670f307bManman Ren
99cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  /// Return the skew that has to be applied to stack alignment under
100cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  /// certain conditions (e.g. stack was adjusted before function \p MF
101cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  /// was called).
102cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  virtual unsigned getStackAlignmentSkew(const MachineFunction &MF) const;
103cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
104fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner  /// getOffsetOfLocalArea - This method returns the offset of the local area
105fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner  /// from the stack pointer on entrance to a function.
106fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner  ///
107fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner  int getOffsetOfLocalArea() const { return LocalAreaOffset; }
108fd529d2e4aed9a455d539962248fda8583d6ba8eChris Lattner
1097271ac2c0318043688ddc8686dd23777dca62c59Richard Sandiford  /// isFPCloseToIncomingSP - Return true if the frame pointer is close to
1107271ac2c0318043688ddc8686dd23777dca62c59Richard Sandiford  /// the incoming stack pointer, false if it is close to the post-prologue
1117271ac2c0318043688ddc8686dd23777dca62c59Richard Sandiford  /// stack pointer.
1127271ac2c0318043688ddc8686dd23777dca62c59Richard Sandiford  virtual bool isFPCloseToIncomingSP() const { return true; }
1137271ac2c0318043688ddc8686dd23777dca62c59Richard Sandiford
114c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  /// assignCalleeSavedSpillSlots - Allows target to override spill slot
115c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  /// assignment logic.  If implemented, assignCalleeSavedSpillSlots() should
116c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  /// assign frame slots to all CSI entries and return true.  If this method
117c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  /// returns false, spill slots will be assigned using generic implementation.
118c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  /// assignCalleeSavedSpillSlots() may add, delete or rearrange elements of
119c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  /// CSI.
120c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  virtual bool
121c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  assignCalleeSavedSpillSlots(MachineFunction &MF,
122c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines                              const TargetRegisterInfo *TRI,
123c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines                              std::vector<CalleeSavedInfo> &CSI) const {
124c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines    return false;
125c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  }
126c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
1270098b3e2b69e527ddcf2ebad7a3081898fa3b4f0Evan Cheng  /// getCalleeSavedSpillSlots - This method returns a pointer to an array of
1280098b3e2b69e527ddcf2ebad7a3081898fa3b4f0Evan Cheng  /// pairs, that contains an entry for each callee saved register that must be
12909431e187f9abd45af08461df728204ee9731fd7Chris Lattner  /// spilled to a particular stack location if it is spilled.
13009431e187f9abd45af08461df728204ee9731fd7Chris Lattner  ///
13109431e187f9abd45af08461df728204ee9731fd7Chris Lattner  /// Each entry in this array contains a <register,offset> pair, indicating the
13209431e187f9abd45af08461df728204ee9731fd7Chris Lattner  /// fixed offset from the incoming stack pointer that each register should be
1338ff95de83cbe85d939535d2f4fb5f9b2b721081aTilmann Scheller  /// spilled at. If a register is not listed here, the code generator is
13409431e187f9abd45af08461df728204ee9731fd7Chris Lattner  /// allowed to spill it anywhere it chooses.
13534695381d626485a560594f162701088079589dfMisha Brukman  ///
1368ff95de83cbe85d939535d2f4fb5f9b2b721081aTilmann Scheller  virtual const SpillSlot *
1370098b3e2b69e527ddcf2ebad7a3081898fa3b4f0Evan Cheng  getCalleeSavedSpillSlots(unsigned &NumEntries) const {
13809431e187f9abd45af08461df728204ee9731fd7Chris Lattner    NumEntries = 0;
139dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return nullptr;
14009431e187f9abd45af08461df728204ee9731fd7Chris Lattner  }
14133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov
14233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov  /// targetHandlesStackFrameRounding - Returns true if the target is
14333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov  /// responsible for rounding up the stack frame (probably at emitPrologue
14433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov  /// time).
14533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov  virtual bool targetHandlesStackFrameRounding() const {
14633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov    return false;
14733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov  }
14833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov
149cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  /// Returns true if the target will correctly handle shrink wrapping.
150cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  virtual bool enableShrinkWrapping(const MachineFunction &MF) const {
151cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    return false;
152cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  }
153cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
15433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov  /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
15533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov  /// the function.
1566948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  virtual void emitPrologue(MachineFunction &MF,
1576948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar                            MachineBasicBlock &MBB) const = 0;
15833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov  virtual void emitEpilogue(MachineFunction &MF,
15933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov                            MachineBasicBlock &MBB) const = 0;
160d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov
161cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  /// Replace a StackProbe stub (if any) with the actual probe code inline
162cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  virtual void inlineStackProbe(MachineFunction &MF,
163cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                                MachineBasicBlock &PrologueMBB) const {}
164cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
16576927d758657b3a511c73467ec5a7288795c1513Rafael Espindola  /// Adjust the prologue to have the function use segmented stacks. This works
16676927d758657b3a511c73467ec5a7288795c1513Rafael Espindola  /// by adding a check even before the "normal" function prologue.
1676948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  virtual void adjustForSegmentedStacks(MachineFunction &MF,
1686948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar                                        MachineBasicBlock &PrologueMBB) const {}
16976927d758657b3a511c73467ec5a7288795c1513Rafael Espindola
17098fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer  /// Adjust the prologue to add Erlang Run-Time System (ERTS) specific code in
17198fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer  /// the assembly prologue to explicitly handle the stack.
1726948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  virtual void adjustForHiPEPrologue(MachineFunction &MF,
1736948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar                                     MachineBasicBlock &PrologueMBB) const {}
17498fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer
175ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  /// Adjust the prologue to add an allocation at a fixed offset from the frame
176ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  /// pointer.
1776948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  virtual void
1786948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  adjustForFrameAllocatePrologue(MachineFunction &MF,
1796948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar                                 MachineBasicBlock &PrologueMBB) const {}
180ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
181cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov  /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee
182cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov  /// saved registers and returns true if it isn't possible / profitable to do
183cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov  /// so by issuing a series of store instructions via
184cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov  /// storeRegToStackSlot(). Returns false otherwise.
185cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov  virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
186cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov                                         MachineBasicBlock::iterator MI,
187cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov                                        const std::vector<CalleeSavedInfo> &CSI,
188cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov                                         const TargetRegisterInfo *TRI) const {
189cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov    return false;
190cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov  }
191cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov
192cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov  /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee
193cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov  /// saved registers and returns true if it isn't possible / profitable to do
194cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov  /// so by issuing a series of load instructions via loadRegToStackSlot().
195cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov  /// Returns false otherwise.
196cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov  virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
197cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov                                           MachineBasicBlock::iterator MI,
198cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov                                        const std::vector<CalleeSavedInfo> &CSI,
199cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov                                        const TargetRegisterInfo *TRI) const {
200cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov    return false;
201cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov  }
202cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov
2036948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// Return true if the target needs to disable frame pointer elimination.
2046948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  virtual bool noFramePointerElim(const MachineFunction &MF) const;
2056948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
206d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov  /// hasFP - Return true if the specified function should have a dedicated
207d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov  /// frame pointer register. For most targets this is true only if the function
208d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov  /// has variable sized allocas or if frame pointer elimination is disabled.
209d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov  virtual bool hasFP(const MachineFunction &MF) const = 0;
210d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov
211d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov  /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
212d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov  /// not required, we reserve argument space for call sites in the function
213d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov  /// immediately on entry to the current function. This eliminates the need for
214d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov  /// add/sub sp brackets around call sites. Returns true if the call frame is
215d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov  /// included as part of the stack frame.
216d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov  virtual bool hasReservedCallFrame(const MachineFunction &MF) const {
217d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov    return !hasFP(MF);
218d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov  }
219d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov
220d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov  /// canSimplifyCallFramePseudos - When possible, it's best to simplify the
221d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov  /// call frame pseudo ops before doing frame index elimination. This is
222d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov  /// possible only when frame index references between the pseudos won't
223d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov  /// need adjusting for the call frame adjustments. Normally, that's true
224d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov  /// if the function has a reserved call frame or a frame pointer. Some
225d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov  /// targets (Thumb2, for example) may have more complicated criteria,
226d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov  /// however, and can override this behavior.
227d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov  virtual bool canSimplifyCallFramePseudos(const MachineFunction &MF) const {
228d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov    return hasReservedCallFrame(MF) || hasFP(MF);
229d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov  }
230d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov
231ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  // needsFrameIndexResolution - Do we need to perform FI resolution for
232ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  // this function. Normally, this is required only when the function
233ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  // has any stack objects. However, targets may want to override this.
234ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  virtual bool needsFrameIndexResolution(const MachineFunction &MF) const;
235ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
23682f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov  /// getFrameIndexReference - This method should return the base register
23782f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov  /// and offset used to reference a frame index location. The offset is
23882f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov  /// returned directly, and the base register is returned via FrameReg.
23982f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov  virtual int getFrameIndexReference(const MachineFunction &MF, int FI,
24082f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov                                     unsigned &FrameReg) const;
24194c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov
242ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  /// Same as above, except that the 'base register' will always be RSP, not
243cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  /// RBP on x86. This is generally used for emitting statepoint or EH tables
244cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  /// that use offsets from RSP.
245ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  /// TODO: This should really be a parameterizable choice.
246ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  virtual int getFrameIndexReferenceFromSP(const MachineFunction &MF, int FI,
247cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                                           unsigned &FrameReg) const {
248ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    // default to calling normal version, we override this on x86 only
249ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    llvm_unreachable("unimplemented for non-x86");
250ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return 0;
251ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  }
252ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
253cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  /// This method determines which of the registers reported by
254cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  /// TargetRegisterInfo::getCalleeSavedRegs() should actually get saved.
255cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  /// The default implementation checks populates the \p SavedRegs bitset with
256cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  /// all registers which are modified in the function, targets may override
257cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  /// this function to save additional registers.
258cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  /// This method also sets up the register scavenger ensuring there is a free
259cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  /// register or a frameindex available.
260cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
261cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                                    RegScavenger *RS = nullptr) const;
26294c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov
26394c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov  /// processFunctionBeforeFrameFinalized - This method is called immediately
26494c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov  /// before the specified function's frame layout (MF.getFrameInfo()) is
26594c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov  /// finalized.  Once the frame is finalized, MO_FrameIndex operands are
26694c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov  /// replaced with direct constants.  This method is optional.
26794c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov  ///
2683080d23fde4981835d8a7faf46c152441fadb11fHal Finkel  virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF,
269dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                                             RegScavenger *RS = nullptr) const {
27094c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov  }
271700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky
272cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  virtual unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const {
273cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    report_fatal_error("WinEH not implemented for this target");
274cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  }
275cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
276700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky  /// eliminateCallFramePseudoInstr - This method is called during prolog/epilog
277700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky  /// code insertion to eliminate call frame setup and destroy pseudo
278700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky  /// instructions (but only if the Target is using them).  It is responsible
279700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky  /// for eliminating these instructions, replacing them with concrete
280700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky  /// instructions.  This method need only be implemented if using call frame
281700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky  /// setup/destroy pseudo instructions.
282700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky  ///
283700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky  virtual void
284700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky  eliminateCallFramePseudoInstr(MachineFunction &MF,
285700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky                                MachineBasicBlock &MBB,
286700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky                                MachineBasicBlock::iterator MI) const {
287700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky    llvm_unreachable("Call Frame Pseudo Instructions do not exist on this "
288700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky                     "target!");
289700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky  }
2906948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
2916948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// Check whether or not the given \p MBB can be used as a prologue
2926948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// for the target.
2936948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// The prologue will be inserted first in this basic block.
2946948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// This method is used by the shrink-wrapping pass to decide if
2956948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// \p MBB will be correctly handled by the target.
2966948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// As soon as the target enable shrink-wrapping without overriding
2976948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// this method, we assume that each basic block is a valid
2986948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// prologue.
2996948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  virtual bool canUseAsPrologue(const MachineBasicBlock &MBB) const {
3006948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    return true;
3016948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  }
3026948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
3036948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// Check whether or not the given \p MBB can be used as a epilogue
3046948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// for the target.
3056948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// The epilogue will be inserted before the first terminator of that block.
3066948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// This method is used by the shrink-wrapping pass to decide if
3076948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// \p MBB will be correctly handled by the target.
3086948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// As soon as the target enable shrink-wrapping without overriding
3096948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// this method, we assume that each basic block is a valid
3106948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// epilogue.
3116948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  virtual bool canUseAsEpilogue(const MachineBasicBlock &MBB) const {
3126948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    return true;
3136948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  }
3147cffcaaa48656fa6f90b041cc5f056304addef32Vikram S. Adve};
3157cffcaaa48656fa6f90b041cc5f056304addef32Vikram S. Adve
316d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
317d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
3187cffcaaa48656fa6f90b041cc5f056304addef32Vikram S. Adve#endif
319