1 //====- BlackfinFrameLowering.cpp - Blackfin Frame Information --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the Blackfin implementation of TargetFrameLowering class.
12 //===----------------------------------------------------------------------===//
14 #include "BlackfinFrameLowering.h"
15 #include "BlackfinInstrInfo.h"
16 #include "llvm/CodeGen/MachineFrameInfo.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/RegisterScavenging.h"
20 #include "llvm/Target/TargetOptions.h"
25 // 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.
28 bool BlackfinFrameLowering::hasFP(const MachineFunction
&MF
) const {
29 const MachineFrameInfo
*MFI
= MF
.getFrameInfo();
30 return DisableFramePointerElim(MF
) ||
31 MFI
->adjustsStack() || MFI
->hasVarSizedObjects();
34 // Emit a prologue that sets up a stack frame.
35 // On function entry, R0-R2 and P0 may hold arguments.
36 // R3, P1, and P2 may be used as scratch registers
37 void BlackfinFrameLowering::emitPrologue(MachineFunction
&MF
) const {
38 MachineBasicBlock
&MBB
= MF
.front(); // Prolog goes in entry BB
39 MachineBasicBlock::iterator MBBI
= MBB
.begin();
40 MachineFrameInfo
*MFI
= MF
.getFrameInfo();
41 const BlackfinRegisterInfo
*RegInfo
=
42 static_cast<const BlackfinRegisterInfo
*>(MF
.getTarget().getRegisterInfo());
43 const BlackfinInstrInfo
&TII
=
44 *static_cast<const BlackfinInstrInfo
*>(MF
.getTarget().getInstrInfo());
46 DebugLoc dl
= MBBI
!= MBB
.end() ? MBBI
->getDebugLoc() : DebugLoc();
48 int FrameSize
= MFI
->getStackSize();
50 FrameSize
= (FrameSize
+3) & ~3;
51 MFI
->setStackSize(FrameSize
);
55 assert(!MFI
->adjustsStack() &&
56 "FP elimination on a non-leaf function is not supported");
57 RegInfo
->adjustRegister(MBB
, MBBI
, dl
, BF::SP
, BF::P1
, -FrameSize
);
61 // emit a LINK instruction
62 if (FrameSize
<= 0x3ffff) {
63 BuildMI(MBB
, MBBI
, dl
, TII
.get(BF::LINK
)).addImm(FrameSize
);
67 // Frame is too big, do a manual LINK:
73 BuildMI(MBB
, MBBI
, dl
, TII
.get(BF::PUSH
))
74 .addReg(BF::RETS
, RegState::Kill
);
75 BuildMI(MBB
, MBBI
, dl
, TII
.get(BF::PUSH
))
76 .addReg(BF::FP
, RegState::Kill
);
77 BuildMI(MBB
, MBBI
, dl
, TII
.get(BF::MOVE
), BF::FP
)
79 RegInfo
->loadConstant(MBB
, MBBI
, dl
, BF::P1
, -FrameSize
);
80 BuildMI(MBB
, MBBI
, dl
, TII
.get(BF::ADDpp
), BF::SP
)
81 .addReg(BF::SP
, RegState::Kill
)
82 .addReg(BF::P1
, RegState::Kill
);
86 void BlackfinFrameLowering::emitEpilogue(MachineFunction
&MF
,
87 MachineBasicBlock
&MBB
) const {
88 MachineFrameInfo
*MFI
= MF
.getFrameInfo();
89 const BlackfinRegisterInfo
*RegInfo
=
90 static_cast<const BlackfinRegisterInfo
*>(MF
.getTarget().getRegisterInfo());
91 const BlackfinInstrInfo
&TII
=
92 *static_cast<const BlackfinInstrInfo
*>(MF
.getTarget().getInstrInfo());
93 MachineBasicBlock::iterator MBBI
= MBB
.getLastNonDebugInstr();
94 DebugLoc dl
= MBBI
->getDebugLoc();
96 int FrameSize
= MFI
->getStackSize();
97 assert(FrameSize
%4 == 0 && "Misaligned frame size");
100 assert(!MFI
->adjustsStack() &&
101 "FP elimination on a non-leaf function is not supported");
102 RegInfo
->adjustRegister(MBB
, MBBI
, dl
, BF::SP
, BF::P1
, FrameSize
);
106 // emit an UNLINK instruction
107 BuildMI(MBB
, MBBI
, dl
, TII
.get(BF::UNLINK
));
110 void BlackfinFrameLowering::
111 processFunctionBeforeCalleeSavedScan(MachineFunction
&MF
,
112 RegScavenger
*RS
) const {
113 MachineFrameInfo
*MFI
= MF
.getFrameInfo();
114 const BlackfinRegisterInfo
*RegInfo
=
115 static_cast<const BlackfinRegisterInfo
*>(MF
.getTarget().getRegisterInfo());
116 const TargetRegisterClass
*RC
= BF::DPRegisterClass
;
118 if (RegInfo
->requiresRegisterScavenging(MF
)) {
119 // Reserve a slot close to SP or frame pointer.
120 RS
->setScavengingFrameIndex(MFI
->CreateStackObject(RC
->getSize(),