blob: 0b0984d2f7774f8809525f64dcbcf117a3c81ed1 [file] [log] [blame]
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001//====- BlackfinFrameLowering.cpp - Blackfin Frame Information --*- C++ -*-===//
Anton Korobeynikov33464912010-11-15 00:06:54 +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//
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010// This file contains the Blackfin implementation of TargetFrameLowering class.
Anton Korobeynikov33464912010-11-15 00:06:54 +000011//
12//===----------------------------------------------------------------------===//
13
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000014#include "BlackfinFrameLowering.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000015#include "BlackfinInstrInfo.h"
16#include "llvm/CodeGen/MachineFrameInfo.h"
17#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +000019#include "llvm/CodeGen/RegisterScavenging.h"
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000020#include "llvm/Target/TargetOptions.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000021
22using namespace llvm;
23
24
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000025// hasFP - Return true if the specified function should have a dedicated frame
26// pointer register. This is true if the function has variable sized allocas or
27// if frame pointer elimination is disabled.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000028bool BlackfinFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000029 const MachineFrameInfo *MFI = MF.getFrameInfo();
30 return DisableFramePointerElim(MF) ||
31 MFI->adjustsStack() || MFI->hasVarSizedObjects();
32}
33
Jakob Stoklund Olesen7b5fdc72011-06-03 22:45:18 +000034// Always reserve a call frame. We dont have enough registers to adjust SP.
35bool BlackfinFrameLowering::
36hasReservedCallFrame(const MachineFunction &MF) const {
37 return true;
38}
39
Anton Korobeynikov33464912010-11-15 00:06:54 +000040// Emit a prologue that sets up a stack frame.
41// On function entry, R0-R2 and P0 may hold arguments.
42// R3, P1, and P2 may be used as scratch registers
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000043void BlackfinFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +000044 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
45 MachineBasicBlock::iterator MBBI = MBB.begin();
46 MachineFrameInfo *MFI = MF.getFrameInfo();
47 const BlackfinRegisterInfo *RegInfo =
48 static_cast<const BlackfinRegisterInfo*>(MF.getTarget().getRegisterInfo());
49 const BlackfinInstrInfo &TII =
50 *static_cast<const BlackfinInstrInfo*>(MF.getTarget().getInstrInfo());
51
52 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
53
54 int FrameSize = MFI->getStackSize();
55 if (FrameSize%4) {
56 FrameSize = (FrameSize+3) & ~3;
57 MFI->setStackSize(FrameSize);
58 }
59
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000060 if (!hasFP(MF)) {
Anton Korobeynikov33464912010-11-15 00:06:54 +000061 assert(!MFI->adjustsStack() &&
62 "FP elimination on a non-leaf function is not supported");
63 RegInfo->adjustRegister(MBB, MBBI, dl, BF::SP, BF::P1, -FrameSize);
64 return;
65 }
66
67 // emit a LINK instruction
68 if (FrameSize <= 0x3ffff) {
69 BuildMI(MBB, MBBI, dl, TII.get(BF::LINK)).addImm(FrameSize);
70 return;
71 }
72
73 // Frame is too big, do a manual LINK:
74 // [--SP] = RETS;
75 // [--SP] = FP;
76 // FP = SP;
77 // P1 = -FrameSize;
78 // SP = SP + P1;
79 BuildMI(MBB, MBBI, dl, TII.get(BF::PUSH))
80 .addReg(BF::RETS, RegState::Kill);
81 BuildMI(MBB, MBBI, dl, TII.get(BF::PUSH))
82 .addReg(BF::FP, RegState::Kill);
83 BuildMI(MBB, MBBI, dl, TII.get(BF::MOVE), BF::FP)
84 .addReg(BF::SP);
85 RegInfo->loadConstant(MBB, MBBI, dl, BF::P1, -FrameSize);
86 BuildMI(MBB, MBBI, dl, TII.get(BF::ADDpp), BF::SP)
87 .addReg(BF::SP, RegState::Kill)
88 .addReg(BF::P1, RegState::Kill);
89
90}
91
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000092void BlackfinFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikov33464912010-11-15 00:06:54 +000093 MachineBasicBlock &MBB) const {
94 MachineFrameInfo *MFI = MF.getFrameInfo();
95 const BlackfinRegisterInfo *RegInfo =
96 static_cast<const BlackfinRegisterInfo*>(MF.getTarget().getRegisterInfo());
97 const BlackfinInstrInfo &TII =
98 *static_cast<const BlackfinInstrInfo*>(MF.getTarget().getInstrInfo());
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +000099 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000100 DebugLoc dl = MBBI->getDebugLoc();
101
102 int FrameSize = MFI->getStackSize();
103 assert(FrameSize%4 == 0 && "Misaligned frame size");
104
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000105 if (!hasFP(MF)) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000106 assert(!MFI->adjustsStack() &&
107 "FP elimination on a non-leaf function is not supported");
108 RegInfo->adjustRegister(MBB, MBBI, dl, BF::SP, BF::P1, FrameSize);
109 return;
110 }
111
112 // emit an UNLINK instruction
113 BuildMI(MBB, MBBI, dl, TII.get(BF::UNLINK));
114}
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000115
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000116void BlackfinFrameLowering::
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000117processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
118 RegScavenger *RS) const {
119 MachineFrameInfo *MFI = MF.getFrameInfo();
120 const BlackfinRegisterInfo *RegInfo =
121 static_cast<const BlackfinRegisterInfo*>(MF.getTarget().getRegisterInfo());
122 const TargetRegisterClass *RC = BF::DPRegisterClass;
123
124 if (RegInfo->requiresRegisterScavenging(MF)) {
125 // Reserve a slot close to SP or frame pointer.
126 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
127 RC->getAlignment(),
128 false));
129 }
130}