1 //===-- HexagonFrameLowering.cpp - Define frame lowering ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #include "HexagonFrameLowering.h"
13 #include "HexagonInstrInfo.h"
14 #include "HexagonMachineFunctionInfo.h"
15 #include "HexagonRegisterInfo.h"
16 #include "HexagonSubtarget.h"
17 #include "HexagonTargetMachine.h"
18 #include "llvm/ADT/BitVector.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/CodeGen/AsmPrinter.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 #include "llvm/CodeGen/RegisterScavenging.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/Type.h"
30 #include "llvm/MC/MCAsmInfo.h"
31 #include "llvm/MC/MachineLocation.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Target/TargetInstrInfo.h"
34 #include "llvm/Target/TargetMachine.h"
35 #include "llvm/Target/TargetOptions.h"
39 static cl::opt<bool> DisableDeallocRet(
40 "disable-hexagon-dealloc-ret",
42 cl::desc("Disable Dealloc Return for Hexagon target"));
44 /// determineFrameLayout - Determine the size of the frame and maximum call
46 void HexagonFrameLowering::determineFrameLayout(MachineFunction &MF) const {
47 MachineFrameInfo *MFI = MF.getFrameInfo();
49 // Get the number of bytes to allocate from the FrameInfo.
50 unsigned FrameSize = MFI->getStackSize();
52 // Get the alignments provided by the target.
53 unsigned TargetAlign =
54 MF.getSubtarget().getFrameLowering()->getStackAlignment();
55 // Get the maximum call frame size of all the calls.
56 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
58 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
59 // that allocations will be aligned.
60 if (MFI->hasVarSizedObjects())
61 maxCallFrameSize = RoundUpToAlignment(maxCallFrameSize, TargetAlign);
63 // Update maximum call frame size.
64 MFI->setMaxCallFrameSize(maxCallFrameSize);
66 // Include call frame size in total.
67 FrameSize += maxCallFrameSize;
69 // Make sure the frame is aligned.
70 FrameSize = RoundUpToAlignment(FrameSize, TargetAlign);
73 MFI->setStackSize(FrameSize);
77 void HexagonFrameLowering::emitPrologue(MachineFunction &MF) const {
78 MachineBasicBlock &MBB = MF.front();
79 MachineFrameInfo *MFI = MF.getFrameInfo();
80 MachineBasicBlock::iterator MBBI = MBB.begin();
81 const HexagonRegisterInfo *QRI =
82 MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
83 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
84 determineFrameLayout(MF);
86 // Get the number of bytes to allocate from the FrameInfo.
87 int NumBytes = (int) MFI->getStackSize();
89 // LLVM expects allocframe not to be the first instruction in the
91 MachineBasicBlock::iterator InsertPt = MBB.begin();
94 // ALLOCA adjust regs. Iterate over ADJDYNALLOC nodes and change the offset.
96 HexagonMachineFunctionInfo *FuncInfo =
97 MF.getInfo<HexagonMachineFunctionInfo>();
98 const std::vector<MachineInstr*>& AdjustRegs =
99 FuncInfo->getAllocaAdjustInsts();
100 for (std::vector<MachineInstr*>::const_iterator i = AdjustRegs.begin(),
101 e = AdjustRegs.end();
103 MachineInstr* MI = *i;
104 assert((MI->getOpcode() == Hexagon::ADJDYNALLOC) &&
105 "Expected adjust alloca node");
107 MachineOperand& MO = MI->getOperand(2);
108 assert(MO.isImm() && "Expected immediate");
109 MO.setImm(MFI->getMaxCallFrameSize());
113 // Only insert ALLOCFRAME if we need to.
116 // Check for overflow.
117 // Hexagon_TODO: Ugh! hardcoding. Is there an API that can be used?
118 const int ALLOCFRAME_MAX = 16384;
119 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
121 if (NumBytes >= ALLOCFRAME_MAX) {
122 // Emit allocframe(#0).
123 BuildMI(MBB, InsertPt, dl, TII.get(Hexagon::S2_allocframe)).addImm(0);
125 // Subtract offset from frame pointer.
126 BuildMI(MBB, InsertPt, dl, TII.get(Hexagon::CONST32_Int_Real),
127 HEXAGON_RESERVED_REG_1).addImm(NumBytes);
128 BuildMI(MBB, InsertPt, dl, TII.get(Hexagon::A2_sub),
129 QRI->getStackRegister()).
130 addReg(QRI->getStackRegister()).
131 addReg(HEXAGON_RESERVED_REG_1);
133 BuildMI(MBB, InsertPt, dl, TII.get(Hexagon::S2_allocframe)).addImm(NumBytes);
137 // Returns true if MBB has a machine instructions that indicates a tail call
139 bool HexagonFrameLowering::hasTailCall(MachineBasicBlock &MBB) const {
140 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
141 unsigned RetOpcode = MBBI->getOpcode();
143 return RetOpcode == Hexagon::TCRETURNtg || RetOpcode == Hexagon::TCRETURNtext;
146 void HexagonFrameLowering::emitEpilogue(MachineFunction &MF,
147 MachineBasicBlock &MBB) const {
148 MachineBasicBlock::iterator MBBI = std::prev(MBB.end());
149 DebugLoc dl = MBBI->getDebugLoc();
151 // Only insert deallocframe if we need to. Also at -O0. See comment
152 // in emitPrologue above.
154 if (hasFP(MF) || MF.getTarget().getOptLevel() == CodeGenOpt::None) {
155 MachineBasicBlock::iterator MBBI = std::prev(MBB.end());
156 MachineBasicBlock::iterator MBBI_end = MBB.end();
158 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
160 if (MBBI->getOpcode() == Hexagon::EH_RETURN_JMPR) {
161 assert(MBBI->getOperand(0).isReg() && "Offset should be in register!");
162 BuildMI(MBB, MBBI, dl, TII.get(Hexagon::L2_deallocframe));
163 BuildMI(MBB, MBBI, dl, TII.get(Hexagon::A2_add),
164 Hexagon::R29).addReg(Hexagon::R29).addReg(Hexagon::R28);
167 // Replace 'jumpr r31' instruction with dealloc_return for V4 and higher
169 if (MF.getSubtarget<HexagonSubtarget>().hasV4TOps() &&
170 MBBI->getOpcode() == Hexagon::JMPret && !DisableDeallocRet) {
171 // Check for RESTORE_DEALLOC_RET_JMP_V4 call. Don't emit an extra DEALLOC
172 // instruction if we encounter it.
173 MachineBasicBlock::iterator BeforeJMPR =
174 MBB.begin() == MBBI ? MBBI : std::prev(MBBI);
175 if (BeforeJMPR != MBBI &&
176 BeforeJMPR->getOpcode() == Hexagon::RESTORE_DEALLOC_RET_JMP_V4) {
177 // Remove the JMPR node.
182 // Add dealloc_return.
183 MachineInstrBuilder MIB =
184 BuildMI(MBB, MBBI_end, dl, TII.get(Hexagon::L4_return));
185 // Transfer the function live-out registers.
186 MIB->copyImplicitOps(*MBB.getParent(), &*MBBI);
187 // Remove the JUMPR node.
189 } else { // Add deallocframe for V2 and V3, and V4 tail calls.
190 // Check for RESTORE_DEALLOC_BEFORE_TAILCALL_V4. We don't need an extra
191 // DEALLOCFRAME instruction after it.
192 MachineBasicBlock::iterator Term = MBB.getFirstTerminator();
193 MachineBasicBlock::iterator I =
194 Term == MBB.begin() ? MBB.end() : std::prev(Term);
195 if (I != MBB.end() &&
196 I->getOpcode() == Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4)
199 BuildMI(MBB, MBBI, dl, TII.get(Hexagon::L2_deallocframe));
204 bool HexagonFrameLowering::hasFP(const MachineFunction &MF) const {
205 const MachineFrameInfo *MFI = MF.getFrameInfo();
206 const HexagonMachineFunctionInfo *FuncInfo =
207 MF.getInfo<HexagonMachineFunctionInfo>();
208 return (MFI->hasCalls() || (MFI->getStackSize() > 0) ||
209 FuncInfo->hasClobberLR() );
213 unsigned uniqueSuperReg(unsigned Reg, const TargetRegisterInfo *TRI) {
214 MCSuperRegIterator SRI(Reg, TRI);
215 assert(SRI.isValid() && "Expected a superreg");
216 unsigned SuperReg = *SRI;
218 assert(!SRI.isValid() && "Expected exactly one superreg");
223 HexagonFrameLowering::spillCalleeSavedRegisters(
224 MachineBasicBlock &MBB,
225 MachineBasicBlock::iterator MI,
226 const std::vector<CalleeSavedInfo> &CSI,
227 const TargetRegisterInfo *TRI) const {
228 MachineFunction *MF = MBB.getParent();
229 const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
235 // We can only schedule double loads if we spill contiguous callee-saved regs
236 // For instance, we cannot scheduled double-word loads if we spill r24,
238 // Hexagon_TODO: We can try to double-word align odd registers for -O2 and
240 bool ContiguousRegs = true;
242 for (unsigned i = 0; i < CSI.size(); ++i) {
243 unsigned Reg = CSI[i].getReg();
246 // Check if we can use a double-word store.
248 unsigned SuperReg = uniqueSuperReg(Reg, TRI);
249 bool CanUseDblStore = false;
250 const TargetRegisterClass* SuperRegClass = nullptr;
252 if (ContiguousRegs && (i < CSI.size()-1)) {
253 unsigned SuperRegNext = uniqueSuperReg(CSI[i+1].getReg(), TRI);
254 SuperRegClass = TRI->getMinimalPhysRegClass(SuperReg);
255 CanUseDblStore = (SuperRegNext == SuperReg);
259 if (CanUseDblStore) {
260 TII.storeRegToStackSlot(MBB, MI, SuperReg, true,
261 CSI[i+1].getFrameIdx(), SuperRegClass, TRI);
262 MBB.addLiveIn(SuperReg);
265 // Cannot use a double-word store.
266 ContiguousRegs = false;
267 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
268 TII.storeRegToStackSlot(MBB, MI, Reg, true, CSI[i].getFrameIdx(), RC,
277 bool HexagonFrameLowering::restoreCalleeSavedRegisters(
278 MachineBasicBlock &MBB,
279 MachineBasicBlock::iterator MI,
280 const std::vector<CalleeSavedInfo> &CSI,
281 const TargetRegisterInfo *TRI) const {
283 MachineFunction *MF = MBB.getParent();
284 const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
290 // We can only schedule double loads if we spill contiguous callee-saved regs
291 // For instance, we cannot scheduled double-word loads if we spill r24,
293 // Hexagon_TODO: We can try to double-word align odd registers for -O2 and
295 bool ContiguousRegs = true;
297 for (unsigned i = 0; i < CSI.size(); ++i) {
298 unsigned Reg = CSI[i].getReg();
301 // Check if we can use a double-word load.
303 unsigned SuperReg = uniqueSuperReg(Reg, TRI);
304 const TargetRegisterClass* SuperRegClass = nullptr;
305 bool CanUseDblLoad = false;
306 if (ContiguousRegs && (i < CSI.size()-1)) {
307 unsigned SuperRegNext = uniqueSuperReg(CSI[i+1].getReg(), TRI);
308 SuperRegClass = TRI->getMinimalPhysRegClass(SuperReg);
309 CanUseDblLoad = (SuperRegNext == SuperReg);
314 TII.loadRegFromStackSlot(MBB, MI, SuperReg, CSI[i+1].getFrameIdx(),
316 MBB.addLiveIn(SuperReg);
319 // Cannot use a double-word load.
320 ContiguousRegs = false;
321 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
322 TII.loadRegFromStackSlot(MBB, MI, Reg, CSI[i].getFrameIdx(), RC, TRI);
329 void HexagonFrameLowering::
330 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
331 MachineBasicBlock::iterator I) const {
332 MachineInstr &MI = *I;
334 if (MI.getOpcode() == Hexagon::ADJCALLSTACKDOWN) {
335 // Hexagon_TODO: add code
336 } else if (MI.getOpcode() == Hexagon::ADJCALLSTACKUP) {
337 // Hexagon_TODO: add code
339 llvm_unreachable("Cannot handle this call frame pseudo instruction");
344 int HexagonFrameLowering::getFrameIndexOffset(const MachineFunction &MF,
346 return MF.getFrameInfo()->getObjectOffset(FI);