blob: 296edbe1effb4667ac899d1dc7fc08f7c28c6589 [file] [log] [blame]
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +00001//==- HexagonFrameLowering.h - Define frame lowering for Hexagon -*- C++ -*-==//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
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
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000010#ifndef LLVM_LIB_TARGET_HEXAGON_HEXAGONFRAMELOWERING_H
11#define LLVM_LIB_TARGET_HEXAGON_HEXAGONFRAMELOWERING_H
Tony Linthicum1213a7a2011-12-12 21:14:40 +000012
13#include "Hexagon.h"
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +000014#include "HexagonBlockRanges.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000015#include "llvm/ADT/STLExtras.h"
16#include "llvm/CodeGen/MachineBasicBlock.h"
17#include "llvm/CodeGen/MachineFrameInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000018#include "llvm/Target/TargetFrameLowering.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000019#include <vector>
Tony Linthicum1213a7a2011-12-12 21:14:40 +000020
21namespace llvm {
22
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000023class BitVector;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000024class HexagonInstrInfo;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +000025class HexagonRegisterInfo;
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000026class MachineFunction;
27class MachineInstr;
28class MachineRegisterInfo;
29class TargetRegisterClass;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000030
Tony Linthicum1213a7a2011-12-12 21:14:40 +000031class HexagonFrameLowering : public TargetFrameLowering {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000032public:
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000033 explicit HexagonFrameLowering()
34 : TargetFrameLowering(StackGrowsDown, 8, 0, 1, true) {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +000035
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +000036 // All of the prolog/epilog functionality, including saving and restoring
37 // callee-saved registers is handled in emitPrologue. This is to have the
38 // logic for shrink-wrapping in one place.
Quentin Colombet61b305e2015-05-05 17:38:16 +000039 void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const
40 override;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +000041 void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const
42 override {}
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000043
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +000044 bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
45 MachineBasicBlock::iterator MI, const std::vector<CalleeSavedInfo> &CSI,
46 const TargetRegisterInfo *TRI) const override {
47 return true;
48 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000049
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +000050 bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Krzysztof Parzyszekbea30c62017-08-10 16:17:32 +000051 MachineBasicBlock::iterator MI, std::vector<CalleeSavedInfo> &CSI,
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +000052 const TargetRegisterInfo *TRI) const override {
53 return true;
54 }
55
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +000056 bool hasReservedCallFrame(const MachineFunction &MF) const override {
57 // We always reserve call frame as a part of the initial stack allocation.
58 return true;
59 }
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000060
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +000061 bool canSimplifyCallFramePseudos(const MachineFunction &MF) const override {
62 // Override this function to avoid calling hasFP before CSI is set
63 // (the default implementation calls hasFP).
64 return true;
65 }
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000066
Hans Wennborge1a2e902016-03-31 18:33:38 +000067 MachineBasicBlock::iterator
68 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
69 MachineBasicBlock::iterator I) const override;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +000070 void processFunctionBeforeFrameFinalized(MachineFunction &MF,
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +000071 RegScavenger *RS = nullptr) const override;
Matthias Braun02564862015-07-14 17:17:13 +000072 void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +000073 RegScavenger *RS) const override;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +000074
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000075 bool targetHandlesStackFrameRounding() const override {
76 return true;
77 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000078
James Y Knight5567baf2015-08-15 02:32:35 +000079 int getFrameIndexReference(const MachineFunction &MF, int FI,
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +000080 unsigned &FrameReg) const override;
Craig Topper906c2cd2014-04-29 07:58:16 +000081 bool hasFP(const MachineFunction &MF) const override;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000082
83 const SpillSlot *getCalleeSavedSpillSlots(unsigned &NumEntries)
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +000084 const override {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000085 static const SpillSlot Offsets[] = {
86 { Hexagon::R17, -4 }, { Hexagon::R16, -8 }, { Hexagon::D8, -8 },
87 { Hexagon::R19, -12 }, { Hexagon::R18, -16 }, { Hexagon::D9, -16 },
88 { Hexagon::R21, -20 }, { Hexagon::R20, -24 }, { Hexagon::D10, -24 },
89 { Hexagon::R23, -28 }, { Hexagon::R22, -32 }, { Hexagon::D11, -32 },
90 { Hexagon::R25, -36 }, { Hexagon::R24, -40 }, { Hexagon::D12, -40 },
91 { Hexagon::R27, -44 }, { Hexagon::R26, -48 }, { Hexagon::D13, -48 }
92 };
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000093 NumEntries = array_lengthof(Offsets);
94 return Offsets;
95 }
96
97 bool assignCalleeSavedSpillSlots(MachineFunction &MF,
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +000098 const TargetRegisterInfo *TRI, std::vector<CalleeSavedInfo> &CSI)
99 const override;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000100
101 bool needsAligna(const MachineFunction &MF) const;
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000102 const MachineInstr *getAlignaInstr(const MachineFunction &MF) const;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000103
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000104 void insertCFIInstructions(MachineFunction &MF) const;
105
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000106private:
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +0000107 using CSIVect = std::vector<CalleeSavedInfo>;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000108
109 void expandAlloca(MachineInstr *AI, const HexagonInstrInfo &TII,
110 unsigned SP, unsigned CF) const;
Krzysztof Parzyszekc9d4caa2016-03-24 20:20:07 +0000111 void insertPrologueInBlock(MachineBasicBlock &MBB, bool PrologueStubs) const;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000112 void insertEpilogueInBlock(MachineBasicBlock &MBB) const;
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000113 void insertAllocframe(MachineBasicBlock &MBB,
114 MachineBasicBlock::iterator InsertPt, unsigned NumBytes) const;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000115 bool insertCSRSpillsInBlock(MachineBasicBlock &MBB, const CSIVect &CSI,
Krzysztof Parzyszekc9d4caa2016-03-24 20:20:07 +0000116 const HexagonRegisterInfo &HRI, bool &PrologueStubs) const;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000117 bool insertCSRRestoresInBlock(MachineBasicBlock &MBB, const CSIVect &CSI,
118 const HexagonRegisterInfo &HRI) const;
Krzysztof Parzyszeka34d6392016-07-27 16:26:39 +0000119 void updateEntryPaths(MachineFunction &MF, MachineBasicBlock &SaveB) const;
120 bool updateExitPaths(MachineBasicBlock &MBB, MachineBasicBlock &RestoreB,
Krzysztof Parzyszekda0b9a92016-05-26 19:44:28 +0000121 BitVector &DoneT, BitVector &DoneF, BitVector &Path) const;
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000122 void insertCFIInstructionsAt(MachineBasicBlock &MBB,
123 MachineBasicBlock::iterator At) const;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000124
125 void adjustForCalleeSavedRegsSpillCall(MachineFunction &MF) const;
Krzysztof Parzyszek996ad1f2016-02-12 18:19:53 +0000126
127 bool expandCopy(MachineBasicBlock &B, MachineBasicBlock::iterator It,
128 MachineRegisterInfo &MRI, const HexagonInstrInfo &HII,
129 SmallVectorImpl<unsigned> &NewRegs) const;
130 bool expandStoreInt(MachineBasicBlock &B, MachineBasicBlock::iterator It,
131 MachineRegisterInfo &MRI, const HexagonInstrInfo &HII,
132 SmallVectorImpl<unsigned> &NewRegs) const;
133 bool expandLoadInt(MachineBasicBlock &B, MachineBasicBlock::iterator It,
134 MachineRegisterInfo &MRI, const HexagonInstrInfo &HII,
135 SmallVectorImpl<unsigned> &NewRegs) const;
136 bool expandStoreVecPred(MachineBasicBlock &B, MachineBasicBlock::iterator It,
137 MachineRegisterInfo &MRI, const HexagonInstrInfo &HII,
138 SmallVectorImpl<unsigned> &NewRegs) const;
139 bool expandLoadVecPred(MachineBasicBlock &B, MachineBasicBlock::iterator It,
140 MachineRegisterInfo &MRI, const HexagonInstrInfo &HII,
141 SmallVectorImpl<unsigned> &NewRegs) const;
142 bool expandStoreVec2(MachineBasicBlock &B, MachineBasicBlock::iterator It,
143 MachineRegisterInfo &MRI, const HexagonInstrInfo &HII,
144 SmallVectorImpl<unsigned> &NewRegs) const;
145 bool expandLoadVec2(MachineBasicBlock &B, MachineBasicBlock::iterator It,
146 MachineRegisterInfo &MRI, const HexagonInstrInfo &HII,
147 SmallVectorImpl<unsigned> &NewRegs) const;
148 bool expandStoreVec(MachineBasicBlock &B, MachineBasicBlock::iterator It,
149 MachineRegisterInfo &MRI, const HexagonInstrInfo &HII,
150 SmallVectorImpl<unsigned> &NewRegs) const;
151 bool expandLoadVec(MachineBasicBlock &B, MachineBasicBlock::iterator It,
152 MachineRegisterInfo &MRI, const HexagonInstrInfo &HII,
153 SmallVectorImpl<unsigned> &NewRegs) const;
154 bool expandSpillMacros(MachineFunction &MF,
155 SmallVectorImpl<unsigned> &NewRegs) const;
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000156
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +0000157 unsigned findPhysReg(MachineFunction &MF, HexagonBlockRanges::IndexRange &FIR,
158 HexagonBlockRanges::InstrIndexMap &IndexMap,
159 HexagonBlockRanges::RegToRangeMap &DeadMap,
160 const TargetRegisterClass *RC) const;
161 void optimizeSpillSlots(MachineFunction &MF,
162 SmallVectorImpl<unsigned> &VRegs) const;
163
Krzysztof Parzyszek876a19d2015-04-23 16:05:39 +0000164 void findShrunkPrologEpilog(MachineFunction &MF, MachineBasicBlock *&PrologB,
165 MachineBasicBlock *&EpilogB) const;
166
Krzysztof Parzyszeke8e754d2016-04-25 17:49:44 +0000167 void addCalleeSaveRegistersAsImpOperand(MachineInstr *MI, const CSIVect &CSI,
168 bool IsDef, bool IsKill) const;
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000169 bool shouldInlineCSR(const MachineFunction &MF, const CSIVect &CSI) const;
170 bool useSpillFunction(const MachineFunction &MF, const CSIVect &CSI) const;
171 bool useRestoreFunction(const MachineFunction &MF, const CSIVect &CSI) const;
Krzysztof Parzyszekddafa2c2016-08-01 17:15:30 +0000172 bool mayOverflowFrameOffset(MachineFunction &MF) const;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000173};
174
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000175} // end namespace llvm
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000176
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000177#endif // LLVM_LIB_TARGET_HEXAGON_HEXAGONFRAMELOWERING_H