forked from Hoernchen/Epiphany
-
Notifications
You must be signed in to change notification settings - Fork 2
/
EpiphanyFrameLowering.cpp
374 lines (318 loc) · 13.7 KB
/
EpiphanyFrameLowering.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//===- EpiphanyFrameLowering.cpp - Epiphany Frame Information ---------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains the Epiphany implementation of TargetFrameLowering class.
//
//===----------------------------------------------------------------------===//
//
// e-gcc creates the following stack
//
// | | Higher address
// |-----------------------------------|
// | |
// | arguments passed on the stack |
// | |
// |-----------------------------------| <- prev_fp + 2
// | prev_lr |
// | prev_fp |
// |-----------------------------------| <- prev_sp, fp
// | |
// | callee-saved registers |
// | |
// |-----------------------------------|
// | |
// | local variables |
// | |
// |-----------------------------------|
// | 2 empty bytes |
// | for lr/fp |
// |-----------------------------------| <- sp
// | | Lower address
//
#include "EpiphanyFrameLowering.h"
#include "EpiphanyMachineFunction.h"
#include "EpiphanyInstrInfo.h"
#include "EpiphanySubtarget.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/RegisterScavenging.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Target/TargetOptions.h"
using namespace llvm;
#define DEBUG_TYPE "frame-info"
// Prologue should save the original FP and LR, and adjust fp into position
// LR and FP are neighbors, so we can use 64-bit store/load
// strd lr, [sp], -offset
// add fp, offset
//@emitPrologue {
void EpiphanyFrameLowering::emitPrologue(MachineFunction &MF,
MachineBasicBlock &MBB) const {
assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
MachineFrameInfo &MFI = MF.getFrameInfo();
MachineModuleInfo &MMI = MF.getMMI();
const EpiphanyInstrInfo &TII = *STI.getInstrInfo();
MachineBasicBlock::iterator MBBI = MBB.begin();
DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
unsigned SP = Epiphany::SP;
unsigned LR = Epiphany::LR;
unsigned FP = Epiphany::FP;
unsigned STRi64_pmd = Epiphany::STRi64_pmd;
unsigned ADDri_r32 = Epiphany::ADDri_r32;
unsigned CFIIndex;
// First, compute final stack size.
uint64_t StackSize = MFI.getStackSize();
// If we have calls, we should allocate 16 bytes to store LR and FP by the callee func
if (MF.getFrameInfo().hasCalls()) {
StackSize += 16;
}
// No need to allocate space on the stack.
if (StackSize == 0 && !MFI.adjustsStack()) return;
const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
// Create label for prologue
MCSymbol *FrameLabel = MF.getContext().createTempSymbol();
// if framepointer enabled, set it to point to the stack pointer.
if (hasFP(MF)) {
// Save old LR and FP to stack
BuildMI(MBB, MBBI, DL, TII.get(STRi64_pmd), SP).addReg(LR).addReg(SP).addImm(-StackSize).setMIFlag(MachineInstr::FrameSetup);
// Adjust FP
BuildMI(MBB, MBBI, DL, TII.get(ADDri_r32), FP).addReg(SP).addImm(StackSize).setMIFlag(MachineInstr::FrameSetup);
// emit ".cfi_def_cfa_register $fp"
CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(FrameLabel, MRI->getDwarfRegNum(FP, true)));
BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)).addCFIIndex(CFIIndex).setMIFlags(MachineInstr::FrameSetup);
} else {
// Just adjust SP if no frame present
TII.adjustStackPtr(SP, -StackSize, MBB, MBBI);
}
// emit ".cfi_def_cfa_offset StackSize"
CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(FrameLabel, -StackSize));
BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)).addCFIIndex(CFIIndex).setMIFlags(MachineInstr::FrameSetup);
const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
// Spill all callee-saves
if (!CSI.empty()) {
// Find the instruction past the last instruction that saves a callee-saved
// register to the stack.
for (unsigned i = 0; i < CSI.size(); ++i)
++MBBI;
// Iterate over list of callee-saved registers and emit .cfi_offset
// directives.
DEBUG(dbgs() << "\nCallee-saved regs spilled in prologue\n");
for (auto I : CSI) {
int64_t Offset = MFI.getObjectOffset(I.getFrameIdx()) - getOffsetOfLocalArea();
unsigned Reg = I.getReg();
// Reg is in CPURegs.
DEBUG(dbgs() << Reg << "\n");
CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(FrameLabel, MRI->getDwarfRegNum(Reg, true), Offset));
BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)).addCFIIndex(CFIIndex).setMIFlags(MachineInstr::FrameSetup);
}
}
}
//}
//@emitEpilogue {
void EpiphanyFrameLowering::emitEpilogue(MachineFunction &MF,
MachineBasicBlock &MBB) const {
MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
MachineFrameInfo &MFI = MF.getFrameInfo();
const EpiphanyInstrInfo &TII = *STI.getInstrInfo();
DebugLoc dl = MBBI->getDebugLoc();
unsigned SP = Epiphany::SP;
unsigned LR = Epiphany::LR;
unsigned LDRi64 = Epiphany::LDRi64;
// Get the number of bytes from FrameInfo
uint64_t StackSize = MFI.getStackSize();
if (MF.getFrameInfo().hasCalls()) {
StackSize += 16;
}
if (!StackSize)
return;
// if framepointer enabled, set it to point to the stack pointer.
if (hasFP(MF)) {
// Restore old LR and FP from SP + offset
BuildMI(MBB, MBBI, dl, TII.get(LDRi64), LR).addReg(SP).addImm(StackSize).setMIFlag(MachineInstr::FrameDestroy);
}
// Adjust stack.
TII.adjustStackPtr(SP, StackSize, MBB, MBBI);
}
//}
/// getFrameIndexReference - Provide a base+offset reference to an FI slot for
/// debug info. It's the same as what we use for resolving the code-gen
/// references for now. FIXME: This can go wrong when references are
/// SP-relative and simple call frames aren't used.
int EpiphanyFrameLowering::getFrameIndexReference(const MachineFunction &MF,
int FI, unsigned &FrameReg) const {
const MachineFrameInfo &MFI = MF.getFrameInfo();
if (hasFP(MF)) {
const auto *RegInfo = static_cast<const EpiphanyRegisterInfo *>(
MF.getSubtarget().getRegisterInfo());
FrameReg = RegInfo->getFrameRegister(MF);
//return MFI.getObjectOffset(FI) + 16;
return MFI.getObjectOffset(FI);
} else {
FrameReg = Epiphany::SP;
return MFI.getObjectOffset(FI) + MFI.getStackSize();
}
}
static void setAliasRegs(MachineFunction &MF, BitVector &SavedRegs, unsigned Reg) {
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
SavedRegs.set(*AI);
}
// This method is called immediately before PrologEpilogInserter scans the
// physical registers used to determine what callee saved registers should be
// spilled. This method is optional.
void EpiphanyFrameLowering::determineCalleeSaves(MachineFunction &MF,
BitVector &SavedRegs,
RegScavenger *RS) const {
//@determineCalleeSaves-body
TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
const auto *RegInfo = static_cast<const EpiphanyRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
DEBUG(dbgs() << "*** determineCalleeSaves\nUsed CSRs:";
for (int Reg = SavedRegs.find_first(); Reg != -1;
Reg = SavedRegs.find_next(Reg))
dbgs() << ' ' << PrintReg(Reg, RegInfo);
dbgs() << "\n";);
}
bool EpiphanyFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
const MachineFrameInfo &MFI = MF.getFrameInfo();
// Reserve call frame if the size of the maximum call frame fits into 16-bit
// immediate field and there are no variable sized objects on the stack.
// Make sure the second register scavenger spill slot can be accessed with one
// instruction.
return isInt<16>(MFI.getMaxCallFrameSize() + getStackAlignment()) &&
!MFI.hasVarSizedObjects();
}
/// Assign callee-saved regs to frame indexes
///
/// \param MF Machine function
/// \param TRI Register info
/// \param CSI Callee-save regs info
/// \return True if success
bool EpiphanyFrameLowering::assignCalleeSavedSpillSlots(MachineFunction &MF,
const TargetRegisterInfo *TRI, std::vector<CalleeSavedInfo> &CSI) const {
// TODO: Probably this method can be moved completely into reimplemented determineCalleeSaves()
// as it not only assigns but also redefines paired regs
if (CSI.empty())
return true; // Early exit if no callee saved registers are modified!
MachineFrameInfo &MFI = MF.getFrameInfo();
unsigned NumFixedSpillSlots;
const TargetFrameLowering::SpillSlot *FixedSpillSlots = getCalleeSavedSpillSlots(NumFixedSpillSlots);
// Now that we know which registers need to be saved and restored, allocate
// stack slots for them.
for (auto CS = CSI.begin(); CS != CSI.end(); ++CS) {
unsigned Reg = CS->getReg();
if (Reg == Epiphany::LR) {
DEBUG(dbgs() << "Erasing LR from CSI, it will be handled by prologue/epilogue inserters\n");
CSI.erase(CS--);
continue;
}
int FrameIdx;
const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
if (TRI->hasReservedSpillSlot(MF, Reg, FrameIdx)) {
CS->setFrameIdx(FrameIdx);
continue;
}
// Check to see if this physreg must be spilled to a particular stack slot on this target.
const TargetFrameLowering::SpillSlot *FixedSlot = FixedSpillSlots;
const TargetFrameLowering::SpillSlot *LastFixedSlot = FixedSlot + NumFixedSpillSlots;
while (FixedSlot != LastFixedSlot && FixedSlot->Reg != Reg) {
++FixedSlot;
}
if (FixedSlot != LastFixedSlot) {
// Spill it to the stack where we must and bail out
FrameIdx = MFI.CreateFixedSpillStackObject(RC->getSize(), FixedSlot->Offset);
CS->setFrameIdx(FrameIdx);
continue;
} else {
// Nope, just spill it anywhere convenient.
unsigned Align = RC->getAlignment();
unsigned StackAlign = getStackAlignment();
// Check if this index can be paired
unsigned sra = 0, srb = 0;
auto Next = CS;
Next++;
if (Next != CSI.end()) {
unsigned CurrentReg = CS->getReg();
unsigned NextReg = Next->getReg();
// Getting target class
const TargetRegisterClass *TRC = TRI->getMinimalPhysRegClass(CurrentReg) == &Epiphany::GPR32RegClass ||
TRI->getMinimalPhysRegClass(CurrentReg) == &Epiphany::GPR16RegClass
? &Epiphany::GPR64RegClass
: &Epiphany::FPR64RegClass;
// Check if we can find superreg for paired regs
sra = TRI->getMatchingSuperReg(CurrentReg, Epiphany::isub_lo, TRC);
srb = TRI->getMatchingSuperReg(NextReg, Epiphany::isub_hi, TRC);
if ((!sra || !srb) || sra != srb) {
srb = TRI->getMatchingSuperReg(CurrentReg, Epiphany::isub_hi, TRC);
sra = TRI->getMatchingSuperReg(NextReg, Epiphany::isub_lo, TRC);
}
// Check if pair was formed
if ((sra && srb) && sra == srb) {
// Remove subregs and set superreg as Callee-saved
CSI.erase(CS--, ++Next);
CSI.emplace_back(sra);
continue;
}
}
// If unable to pair for some reason - just assign to the next frame index
Align = std::min(Align, StackAlign);
FrameIdx = MFI.CreateStackObject(RC->getSize(), Align, true);
CS->setFrameIdx(FrameIdx);
}
}
return true;
}
// hasFP - Returns true if the specified function should have a dedicated frame
// pointer register.
bool EpiphanyFrameLowering::hasFP(const MachineFunction &MF) const {
const MachineFrameInfo &MFI = MF.getFrameInfo();
const TargetRegisterInfo *TRI = STI.getRegisterInfo();
DEBUG(
if (MF.getTarget().Options.DisableFramePointerElim(MF)) {
dbgs() << "\nHas FP: DisableFramePointerElim set\n";
}
if (TRI->needsStackRealignment(MF)) {
dbgs() << "\nHas FP: Stack realign needed\n";
}
if (MFI.hasVarSizedObjects()) {
dbgs() << "\nHas FP: Has var sized objects\n";
}
if (MFI.isFrameAddressTaken()) {
dbgs() << "\nHas FP: Frame address taken\n";
};);
return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
TRI->needsStackRealignment(MF) ||
MFI.hasVarSizedObjects() ||
MFI.isFrameAddressTaken());
}
/// Set local frame max alignment to 8, used by EpiphanyLoadStoreOptimizer
void EpiphanyFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
RegScavenger *RS) const {
MachineFrameInfo &MFI = MF.getFrameInfo();
MFI.setLocalFrameMaxAlign(8);
}
/// Eliminate pseudo ADJCALLSTACKUP/ADJCALLSTACKDOWN instructions
/// See EpiphanyInstrInfo.td and EpiphanyInstrInfo.cpp
MachineBasicBlock::iterator EpiphanyFrameLowering::eliminateCallFramePseudoInstr(
MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator I) const {
unsigned SP = Epiphany::SP;
if (!hasReservedCallFrame(MF)) {
// Keep positive if adjusting up, negate if down
int64_t Amount = I->getOperand(0).getImm();
if (I->getOpcode() == Epiphany::ADJCALLSTACKDOWN) {
Amount = -Amount;
}
// Issue adjustment commands
STI.getInstrInfo()->adjustStackPtr(SP, Amount, MBB, I);
}
return MBB.erase(I);
}