blob: 918fb07f57b8e8075d806586665415879b47c7b5 [file] [log] [blame]
Anton Korobeynikov33464912010-11-15 00:06:54 +00001//=======- PPCFrameInfo.cpp - PPC Frame Information ------------*- C++ -*-====//
2//
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//
10// This file contains the PPC implementation of TargetFrameInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "PPCFrameInfo.h"
15#include "PPCInstrInfo.h"
16#include "PPCMachineFunctionInfo.h"
17#include "llvm/Function.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/MachineModuleInfo.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +000023#include "llvm/CodeGen/RegisterScavenging.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000024#include "llvm/Target/TargetOptions.h"
25
26using namespace llvm;
27
28// FIXME This disables some code that aligns the stack to a boundary bigger than
29// the default (16 bytes on Darwin) when there is a stack local of greater
30// alignment. This does not currently work, because the delta between old and
31// new stack pointers is added to offsets that reference incoming parameters
32// after the prolog is generated, and the code that does that doesn't handle a
33// variable delta. You don't want to do that anyway; a better approach is to
34// reserve another register that retains to the incoming stack pointer, and
35// reference parameters relative to that.
36#define ALIGN_STACK 0
37
38
39/// VRRegNo - Map from a numbered VR register to its enum value.
40///
41static const unsigned short VRRegNo[] = {
42 PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
43 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15,
44 PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23,
45 PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31
46};
47
48/// RemoveVRSaveCode - We have found that this function does not need any code
49/// to manipulate the VRSAVE register, even though it uses vector registers.
50/// This can happen when the only registers used are known to be live in or out
51/// of the function. Remove all of the VRSAVE related code from the function.
52static void RemoveVRSaveCode(MachineInstr *MI) {
53 MachineBasicBlock *Entry = MI->getParent();
54 MachineFunction *MF = Entry->getParent();
55
56 // We know that the MTVRSAVE instruction immediately follows MI. Remove it.
57 MachineBasicBlock::iterator MBBI = MI;
58 ++MBBI;
59 assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE);
60 MBBI->eraseFromParent();
61
62 bool RemovedAllMTVRSAVEs = true;
63 // See if we can find and remove the MTVRSAVE instruction from all of the
64 // epilog blocks.
65 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) {
66 // If last instruction is a return instruction, add an epilogue
67 if (!I->empty() && I->back().getDesc().isReturn()) {
68 bool FoundIt = false;
69 for (MBBI = I->end(); MBBI != I->begin(); ) {
70 --MBBI;
71 if (MBBI->getOpcode() == PPC::MTVRSAVE) {
72 MBBI->eraseFromParent(); // remove it.
73 FoundIt = true;
74 break;
75 }
76 }
77 RemovedAllMTVRSAVEs &= FoundIt;
78 }
79 }
80
81 // If we found and removed all MTVRSAVE instructions, remove the read of
82 // VRSAVE as well.
83 if (RemovedAllMTVRSAVEs) {
84 MBBI = MI;
85 assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?");
86 --MBBI;
87 assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?");
88 MBBI->eraseFromParent();
89 }
90
91 // Finally, nuke the UPDATE_VRSAVE.
92 MI->eraseFromParent();
93}
94
95// HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the
96// instruction selector. Based on the vector registers that have been used,
97// transform this into the appropriate ORI instruction.
98static void HandleVRSaveUpdate(MachineInstr *MI, const TargetInstrInfo &TII) {
99 MachineFunction *MF = MI->getParent()->getParent();
100 DebugLoc dl = MI->getDebugLoc();
101
102 unsigned UsedRegMask = 0;
103 for (unsigned i = 0; i != 32; ++i)
104 if (MF->getRegInfo().isPhysRegUsed(VRRegNo[i]))
105 UsedRegMask |= 1 << (31-i);
106
107 // Live in and live out values already must be in the mask, so don't bother
108 // marking them.
109 for (MachineRegisterInfo::livein_iterator
110 I = MF->getRegInfo().livein_begin(),
111 E = MF->getRegInfo().livein_end(); I != E; ++I) {
112 unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(I->first);
113 if (VRRegNo[RegNo] == I->first) // If this really is a vector reg.
114 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
115 }
116 for (MachineRegisterInfo::liveout_iterator
117 I = MF->getRegInfo().liveout_begin(),
118 E = MF->getRegInfo().liveout_end(); I != E; ++I) {
119 unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(*I);
120 if (VRRegNo[RegNo] == *I) // If this really is a vector reg.
121 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
122 }
123
124 // If no registers are used, turn this into a copy.
125 if (UsedRegMask == 0) {
126 // Remove all VRSAVE code.
127 RemoveVRSaveCode(MI);
128 return;
129 }
130
131 unsigned SrcReg = MI->getOperand(1).getReg();
132 unsigned DstReg = MI->getOperand(0).getReg();
133
134 if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
135 if (DstReg != SrcReg)
136 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
137 .addReg(SrcReg)
138 .addImm(UsedRegMask);
139 else
140 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
141 .addReg(SrcReg, RegState::Kill)
142 .addImm(UsedRegMask);
143 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
144 if (DstReg != SrcReg)
145 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
146 .addReg(SrcReg)
147 .addImm(UsedRegMask >> 16);
148 else
149 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
150 .addReg(SrcReg, RegState::Kill)
151 .addImm(UsedRegMask >> 16);
152 } else {
153 if (DstReg != SrcReg)
154 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
155 .addReg(SrcReg)
156 .addImm(UsedRegMask >> 16);
157 else
158 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
159 .addReg(SrcReg, RegState::Kill)
160 .addImm(UsedRegMask >> 16);
161
162 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
163 .addReg(DstReg, RegState::Kill)
164 .addImm(UsedRegMask & 0xFFFF);
165 }
166
167 // Remove the old UPDATE_VRSAVE instruction.
168 MI->eraseFromParent();
169}
170
171/// determineFrameLayout - Determine the size of the frame and maximum call
172/// frame size.
173void PPCFrameInfo::determineFrameLayout(MachineFunction &MF) const {
174 MachineFrameInfo *MFI = MF.getFrameInfo();
175
176 // Get the number of bytes to allocate from the FrameInfo
177 unsigned FrameSize = MFI->getStackSize();
178
179 // Get the alignments provided by the target, and the maximum alignment
180 // (if any) of the fixed frame objects.
181 unsigned MaxAlign = MFI->getMaxAlignment();
182 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
183 unsigned AlignMask = TargetAlign - 1; //
184
185 // If we are a leaf function, and use up to 224 bytes of stack space,
186 // don't have a frame pointer, calls, or dynamic alloca then we do not need
187 // to adjust the stack pointer (we fit in the Red Zone).
188 bool DisableRedZone = MF.getFunction()->hasFnAttr(Attribute::NoRedZone);
189 // FIXME SVR4 The 32-bit SVR4 ABI has no red zone.
190 if (!DisableRedZone &&
191 FrameSize <= 224 && // Fits in red zone.
192 !MFI->hasVarSizedObjects() && // No dynamic alloca.
193 !MFI->adjustsStack() && // No calls.
194 (!ALIGN_STACK || MaxAlign <= TargetAlign)) { // No special alignment.
195 // No need for frame
196 MFI->setStackSize(0);
197 return;
198 }
199
200 // Get the maximum call frame size of all the calls.
201 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
202
203 // Maximum call frame needs to be at least big enough for linkage and 8 args.
204 unsigned minCallFrameSize = getMinCallFrameSize(Subtarget.isPPC64(),
205 Subtarget.isDarwinABI());
206 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
207
208 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
209 // that allocations will be aligned.
210 if (MFI->hasVarSizedObjects())
211 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
212
213 // Update maximum call frame size.
214 MFI->setMaxCallFrameSize(maxCallFrameSize);
215
216 // Include call frame size in total.
217 FrameSize += maxCallFrameSize;
218
219 // Make sure the frame is aligned.
220 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
221
222 // Update frame info.
223 MFI->setStackSize(FrameSize);
224}
225
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000226// hasFP - Return true if the specified function actually has a dedicated frame
227// pointer register.
228bool PPCFrameInfo::hasFP(const MachineFunction &MF) const {
229 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000230 // FIXME: This is pretty much broken by design: hasFP() might be called really
231 // early, before the stack layout was calculated and thus hasFP() might return
232 // true or false here depending on the time of call.
233 return (MFI->getStackSize()) && needsFP(MF);
234}
235
236// needsFP - Return true if the specified function should have a dedicated frame
237// pointer register. This is true if the function has variable sized allocas or
238// if frame pointer elimination is disabled.
239bool PPCFrameInfo::needsFP(const MachineFunction &MF) const {
240 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000241
242 // Naked functions have no stack frame pushed, so we don't have a frame
243 // pointer.
244 if (MF.getFunction()->hasFnAttr(Attribute::Naked))
245 return false;
246
247 return DisableFramePointerElim(MF) || MFI->hasVarSizedObjects() ||
248 (GuaranteedTailCallOpt && MF.getInfo<PPCFunctionInfo>()->hasFastCall());
249}
250
251
Anton Korobeynikov33464912010-11-15 00:06:54 +0000252void PPCFrameInfo::emitPrologue(MachineFunction &MF) const {
253 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
254 MachineBasicBlock::iterator MBBI = MBB.begin();
255 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000256 const PPCInstrInfo &TII =
257 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
258
259 MachineModuleInfo &MMI = MF.getMMI();
260 DebugLoc dl;
261 bool needsFrameMoves = MMI.hasDebugInfo() ||
262 !MF.getFunction()->doesNotThrow() ||
263 UnwindTablesMandatory;
264
265 // Prepare for frame info.
266 MCSymbol *FrameLabel = 0;
267
268 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
269 // process it.
270 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
271 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
272 HandleVRSaveUpdate(MBBI, TII);
273 break;
274 }
275 }
276
277 // Move MBBI back to the beginning of the function.
278 MBBI = MBB.begin();
279
280 // Work out frame sizes.
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000281 // FIXME: determineFrameLayout() may change the frame size. This should be
282 // moved upper, to some hook.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000283 determineFrameLayout(MF);
284 unsigned FrameSize = MFI->getStackSize();
285
286 int NegFrameSize = -FrameSize;
287
288 // Get processor type.
289 bool isPPC64 = Subtarget.isPPC64();
290 // Get operating system
291 bool isDarwinABI = Subtarget.isDarwinABI();
292 // Check if the link register (LR) must be saved.
293 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
294 bool MustSaveLR = FI->mustSaveLR();
295 // Do we have a frame pointer for this function?
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000296 bool HasFP = hasFP(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000297
298 int LROffset = PPCFrameInfo::getReturnSaveOffset(isPPC64, isDarwinABI);
299
300 int FPOffset = 0;
301 if (HasFP) {
302 if (Subtarget.isSVR4ABI()) {
303 MachineFrameInfo *FFI = MF.getFrameInfo();
304 int FPIndex = FI->getFramePointerSaveIndex();
305 assert(FPIndex && "No Frame Pointer Save Slot!");
306 FPOffset = FFI->getObjectOffset(FPIndex);
307 } else {
308 FPOffset = PPCFrameInfo::getFramePointerSaveOffset(isPPC64, isDarwinABI);
309 }
310 }
311
312 if (isPPC64) {
313 if (MustSaveLR)
314 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR8), PPC::X0);
315
316 if (HasFP)
317 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD))
318 .addReg(PPC::X31)
319 .addImm(FPOffset/4)
320 .addReg(PPC::X1);
321
322 if (MustSaveLR)
323 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD))
324 .addReg(PPC::X0)
325 .addImm(LROffset / 4)
326 .addReg(PPC::X1);
327 } else {
328 if (MustSaveLR)
329 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR), PPC::R0);
330
331 if (HasFP)
332 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW))
333 .addReg(PPC::R31)
334 .addImm(FPOffset)
335 .addReg(PPC::R1);
336
337 if (MustSaveLR)
338 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW))
339 .addReg(PPC::R0)
340 .addImm(LROffset)
341 .addReg(PPC::R1);
342 }
343
344 // Skip if a leaf routine.
345 if (!FrameSize) return;
346
347 // Get stack alignments.
348 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
349 unsigned MaxAlign = MFI->getMaxAlignment();
350
351 // Adjust stack pointer: r1 += NegFrameSize.
352 // If there is a preferred stack alignment, align R1 now
353 if (!isPPC64) {
354 // PPC32.
355 if (ALIGN_STACK && MaxAlign > TargetAlign) {
356 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
357 "Invalid alignment!");
358 assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!");
359
360 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), PPC::R0)
361 .addReg(PPC::R1)
362 .addImm(0)
363 .addImm(32 - Log2_32(MaxAlign))
364 .addImm(31);
365 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC) ,PPC::R0)
366 .addReg(PPC::R0, RegState::Kill)
367 .addImm(NegFrameSize);
368 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX))
369 .addReg(PPC::R1)
370 .addReg(PPC::R1)
371 .addReg(PPC::R0);
372 } else if (isInt<16>(NegFrameSize)) {
373 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWU), PPC::R1)
374 .addReg(PPC::R1)
375 .addImm(NegFrameSize)
376 .addReg(PPC::R1);
377 } else {
378 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0)
379 .addImm(NegFrameSize >> 16);
380 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0)
381 .addReg(PPC::R0, RegState::Kill)
382 .addImm(NegFrameSize & 0xFFFF);
383 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX))
384 .addReg(PPC::R1)
385 .addReg(PPC::R1)
386 .addReg(PPC::R0);
387 }
388 } else { // PPC64.
389 if (ALIGN_STACK && MaxAlign > TargetAlign) {
390 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
391 "Invalid alignment!");
392 assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!");
393
394 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), PPC::X0)
395 .addReg(PPC::X1)
396 .addImm(0)
397 .addImm(64 - Log2_32(MaxAlign));
398 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC8), PPC::X0)
399 .addReg(PPC::X0)
400 .addImm(NegFrameSize);
401 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX))
402 .addReg(PPC::X1)
403 .addReg(PPC::X1)
404 .addReg(PPC::X0);
405 } else if (isInt<16>(NegFrameSize)) {
406 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDU), PPC::X1)
407 .addReg(PPC::X1)
408 .addImm(NegFrameSize / 4)
409 .addReg(PPC::X1);
410 } else {
411 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0)
412 .addImm(NegFrameSize >> 16);
413 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0)
414 .addReg(PPC::X0, RegState::Kill)
415 .addImm(NegFrameSize & 0xFFFF);
416 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX))
417 .addReg(PPC::X1)
418 .addReg(PPC::X1)
419 .addReg(PPC::X0);
420 }
421 }
422
423 std::vector<MachineMove> &Moves = MMI.getFrameMoves();
424
425 // Add the "machine moves" for the instructions we generated above, but in
426 // reverse order.
427 if (needsFrameMoves) {
428 // Mark effective beginning of when frame pointer becomes valid.
429 FrameLabel = MMI.getContext().CreateTempSymbol();
430 BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(FrameLabel);
431
432 // Show update of SP.
433 if (NegFrameSize) {
434 MachineLocation SPDst(MachineLocation::VirtualFP);
435 MachineLocation SPSrc(MachineLocation::VirtualFP, NegFrameSize);
436 Moves.push_back(MachineMove(FrameLabel, SPDst, SPSrc));
437 } else {
438 MachineLocation SP(isPPC64 ? PPC::X31 : PPC::R31);
439 Moves.push_back(MachineMove(FrameLabel, SP, SP));
440 }
441
442 if (HasFP) {
443 MachineLocation FPDst(MachineLocation::VirtualFP, FPOffset);
444 MachineLocation FPSrc(isPPC64 ? PPC::X31 : PPC::R31);
445 Moves.push_back(MachineMove(FrameLabel, FPDst, FPSrc));
446 }
447
448 if (MustSaveLR) {
449 MachineLocation LRDst(MachineLocation::VirtualFP, LROffset);
450 MachineLocation LRSrc(isPPC64 ? PPC::LR8 : PPC::LR);
451 Moves.push_back(MachineMove(FrameLabel, LRDst, LRSrc));
452 }
453 }
454
455 MCSymbol *ReadyLabel = 0;
456
457 // If there is a frame pointer, copy R1 into R31
458 if (HasFP) {
459 if (!isPPC64) {
460 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR), PPC::R31)
461 .addReg(PPC::R1)
462 .addReg(PPC::R1);
463 } else {
464 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR8), PPC::X31)
465 .addReg(PPC::X1)
466 .addReg(PPC::X1);
467 }
468
469 if (needsFrameMoves) {
470 ReadyLabel = MMI.getContext().CreateTempSymbol();
471
472 // Mark effective beginning of when frame pointer is ready.
473 BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(ReadyLabel);
474
475 MachineLocation FPDst(HasFP ? (isPPC64 ? PPC::X31 : PPC::R31) :
476 (isPPC64 ? PPC::X1 : PPC::R1));
477 MachineLocation FPSrc(MachineLocation::VirtualFP);
478 Moves.push_back(MachineMove(ReadyLabel, FPDst, FPSrc));
479 }
480 }
481
482 if (needsFrameMoves) {
483 MCSymbol *Label = HasFP ? ReadyLabel : FrameLabel;
484
485 // Add callee saved registers to move list.
486 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
487 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
488 int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx());
489 unsigned Reg = CSI[I].getReg();
490 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
491 MachineLocation CSDst(MachineLocation::VirtualFP, Offset);
492 MachineLocation CSSrc(Reg);
493 Moves.push_back(MachineMove(Label, CSDst, CSSrc));
494 }
495 }
496}
497
498void PPCFrameInfo::emitEpilogue(MachineFunction &MF,
499 MachineBasicBlock &MBB) const {
500 MachineBasicBlock::iterator MBBI = prior(MBB.end());
Anton Korobeynikov33464912010-11-15 00:06:54 +0000501 const PPCInstrInfo &TII =
502 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
503
504 unsigned RetOpcode = MBBI->getOpcode();
505 DebugLoc dl;
506
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000507 assert((RetOpcode == PPC::BLR ||
508 RetOpcode == PPC::TCRETURNri ||
509 RetOpcode == PPC::TCRETURNdi ||
510 RetOpcode == PPC::TCRETURNai ||
511 RetOpcode == PPC::TCRETURNri8 ||
512 RetOpcode == PPC::TCRETURNdi8 ||
513 RetOpcode == PPC::TCRETURNai8) &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000514 "Can only insert epilog into returning blocks");
515
516 // Get alignment info so we know how to restore r1
517 const MachineFrameInfo *MFI = MF.getFrameInfo();
518 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
519 unsigned MaxAlign = MFI->getMaxAlignment();
520
521 // Get the number of bytes allocated from the FrameInfo.
522 int FrameSize = MFI->getStackSize();
523
524 // Get processor type.
525 bool isPPC64 = Subtarget.isPPC64();
526 // Get operating system
527 bool isDarwinABI = Subtarget.isDarwinABI();
528 // Check if the link register (LR) has been saved.
529 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
530 bool MustSaveLR = FI->mustSaveLR();
531 // Do we have a frame pointer for this function?
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000532 bool HasFP = hasFP(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000533
534 int LROffset = PPCFrameInfo::getReturnSaveOffset(isPPC64, isDarwinABI);
535
536 int FPOffset = 0;
537 if (HasFP) {
538 if (Subtarget.isSVR4ABI()) {
539 MachineFrameInfo *FFI = MF.getFrameInfo();
540 int FPIndex = FI->getFramePointerSaveIndex();
541 assert(FPIndex && "No Frame Pointer Save Slot!");
542 FPOffset = FFI->getObjectOffset(FPIndex);
543 } else {
544 FPOffset = PPCFrameInfo::getFramePointerSaveOffset(isPPC64, isDarwinABI);
545 }
546 }
547
548 bool UsesTCRet = RetOpcode == PPC::TCRETURNri ||
549 RetOpcode == PPC::TCRETURNdi ||
550 RetOpcode == PPC::TCRETURNai ||
551 RetOpcode == PPC::TCRETURNri8 ||
552 RetOpcode == PPC::TCRETURNdi8 ||
553 RetOpcode == PPC::TCRETURNai8;
554
555 if (UsesTCRet) {
556 int MaxTCRetDelta = FI->getTailCallSPDelta();
557 MachineOperand &StackAdjust = MBBI->getOperand(1);
558 assert(StackAdjust.isImm() && "Expecting immediate value.");
559 // Adjust stack pointer.
560 int StackAdj = StackAdjust.getImm();
561 int Delta = StackAdj - MaxTCRetDelta;
562 assert((Delta >= 0) && "Delta must be positive");
563 if (MaxTCRetDelta>0)
564 FrameSize += (StackAdj +Delta);
565 else
566 FrameSize += StackAdj;
567 }
568
569 if (FrameSize) {
570 // The loaded (or persistent) stack pointer value is offset by the 'stwu'
571 // on entry to the function. Add this offset back now.
572 if (!isPPC64) {
573 // If this function contained a fastcc call and GuaranteedTailCallOpt is
574 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
575 // call which invalidates the stack pointer value in SP(0). So we use the
576 // value of R31 in this case.
577 if (FI->hasFastCall() && isInt<16>(FrameSize)) {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000578 assert(hasFP(MF) && "Expecting a valid the frame pointer.");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000579 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1)
580 .addReg(PPC::R31).addImm(FrameSize);
581 } else if(FI->hasFastCall()) {
582 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0)
583 .addImm(FrameSize >> 16);
584 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0)
585 .addReg(PPC::R0, RegState::Kill)
586 .addImm(FrameSize & 0xFFFF);
587 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD4))
588 .addReg(PPC::R1)
589 .addReg(PPC::R31)
590 .addReg(PPC::R0);
591 } else if (isInt<16>(FrameSize) &&
592 (!ALIGN_STACK || TargetAlign >= MaxAlign) &&
593 !MFI->hasVarSizedObjects()) {
594 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1)
595 .addReg(PPC::R1).addImm(FrameSize);
596 } else {
597 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ),PPC::R1)
598 .addImm(0).addReg(PPC::R1);
599 }
600 } else {
601 if (FI->hasFastCall() && isInt<16>(FrameSize)) {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000602 assert(hasFP(MF) && "Expecting a valid the frame pointer.");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000603 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1)
604 .addReg(PPC::X31).addImm(FrameSize);
605 } else if(FI->hasFastCall()) {
606 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0)
607 .addImm(FrameSize >> 16);
608 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0)
609 .addReg(PPC::X0, RegState::Kill)
610 .addImm(FrameSize & 0xFFFF);
611 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD8))
612 .addReg(PPC::X1)
613 .addReg(PPC::X31)
614 .addReg(PPC::X0);
615 } else if (isInt<16>(FrameSize) && TargetAlign >= MaxAlign &&
616 !MFI->hasVarSizedObjects()) {
617 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1)
618 .addReg(PPC::X1).addImm(FrameSize);
619 } else {
620 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X1)
621 .addImm(0).addReg(PPC::X1);
622 }
623 }
624 }
625
626 if (isPPC64) {
627 if (MustSaveLR)
628 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X0)
629 .addImm(LROffset/4).addReg(PPC::X1);
630
631 if (HasFP)
632 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X31)
633 .addImm(FPOffset/4).addReg(PPC::X1);
634
635 if (MustSaveLR)
636 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR8)).addReg(PPC::X0);
637 } else {
638 if (MustSaveLR)
639 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R0)
640 .addImm(LROffset).addReg(PPC::R1);
641
642 if (HasFP)
643 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R31)
644 .addImm(FPOffset).addReg(PPC::R1);
645
646 if (MustSaveLR)
647 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR)).addReg(PPC::R0);
648 }
649
650 // Callee pop calling convention. Pop parameter/linkage area. Used for tail
651 // call optimization
652 if (GuaranteedTailCallOpt && RetOpcode == PPC::BLR &&
653 MF.getFunction()->getCallingConv() == CallingConv::Fast) {
654 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
655 unsigned CallerAllocatedAmt = FI->getMinReservedArea();
656 unsigned StackReg = isPPC64 ? PPC::X1 : PPC::R1;
657 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
658 unsigned TmpReg = isPPC64 ? PPC::X0 : PPC::R0;
659 unsigned ADDIInstr = isPPC64 ? PPC::ADDI8 : PPC::ADDI;
660 unsigned ADDInstr = isPPC64 ? PPC::ADD8 : PPC::ADD4;
661 unsigned LISInstr = isPPC64 ? PPC::LIS8 : PPC::LIS;
662 unsigned ORIInstr = isPPC64 ? PPC::ORI8 : PPC::ORI;
663
664 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
665 BuildMI(MBB, MBBI, dl, TII.get(ADDIInstr), StackReg)
666 .addReg(StackReg).addImm(CallerAllocatedAmt);
667 } else {
668 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
669 .addImm(CallerAllocatedAmt >> 16);
670 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
671 .addReg(TmpReg, RegState::Kill)
672 .addImm(CallerAllocatedAmt & 0xFFFF);
673 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr))
674 .addReg(StackReg)
675 .addReg(FPReg)
676 .addReg(TmpReg);
677 }
678 } else if (RetOpcode == PPC::TCRETURNdi) {
679 MBBI = prior(MBB.end());
680 MachineOperand &JumpTarget = MBBI->getOperand(0);
681 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
682 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
683 } else if (RetOpcode == PPC::TCRETURNri) {
684 MBBI = prior(MBB.end());
685 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
686 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
687 } else if (RetOpcode == PPC::TCRETURNai) {
688 MBBI = prior(MBB.end());
689 MachineOperand &JumpTarget = MBBI->getOperand(0);
690 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
691 } else if (RetOpcode == PPC::TCRETURNdi8) {
692 MBBI = prior(MBB.end());
693 MachineOperand &JumpTarget = MBBI->getOperand(0);
694 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
695 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
696 } else if (RetOpcode == PPC::TCRETURNri8) {
697 MBBI = prior(MBB.end());
698 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
699 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
700 } else if (RetOpcode == PPC::TCRETURNai8) {
701 MBBI = prior(MBB.end());
702 MachineOperand &JumpTarget = MBBI->getOperand(0);
703 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
704 }
705}
Anton Korobeynikovd9e33852010-11-18 23:25:52 +0000706
707void PPCFrameInfo::getInitialFrameState(std::vector<MachineMove> &Moves) const {
708 // Initial state of the frame pointer is R1.
709 MachineLocation Dst(MachineLocation::VirtualFP);
710 MachineLocation Src(PPC::R1, 0);
711 Moves.push_back(MachineMove(0, Dst, Src));
712}
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000713
714static bool spillsCR(const MachineFunction &MF) {
715 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
716 return FuncInfo->isCRSpilled();
717}
718
719/// MustSaveLR - Return true if this function requires that we save the LR
720/// register onto the stack in the prolog and restore it in the epilog of the
721/// function.
722static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
723 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
724
725 // We need a save/restore of LR if there is any def of LR (which is
726 // defined by calls, including the PIC setup sequence), or if there is
727 // some use of the LR stack slot (e.g. for builtin_return_address).
728 // (LR comes in 32 and 64 bit versions.)
729 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
730 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
731}
732
733void
734PPCFrameInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
735 RegScavenger *RS) const {
736 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
737
738 // Save and clear the LR state.
739 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
740 unsigned LR = RegInfo->getRARegister();
741 FI->setMustSaveLR(MustSaveLR(MF, LR));
742 MF.getRegInfo().setPhysRegUnused(LR);
743
744 // Save R31 if necessary
745 int FPSI = FI->getFramePointerSaveIndex();
746 bool isPPC64 = Subtarget.isPPC64();
747 bool isDarwinABI = Subtarget.isDarwinABI();
748 MachineFrameInfo *MFI = MF.getFrameInfo();
749
750 // If the frame pointer save index hasn't been defined yet.
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000751 if (!FPSI && needsFP(MF)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000752 // Find out what the fix offset of the frame pointer save area.
753 int FPOffset = getFramePointerSaveOffset(isPPC64, isDarwinABI);
754 // Allocate the frame index for frame pointer save area.
755 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
756 // Save the result.
757 FI->setFramePointerSaveIndex(FPSI);
758 }
759
760 // Reserve stack space to move the linkage area to in case of a tail call.
761 int TCSPDelta = 0;
762 if (GuaranteedTailCallOpt && (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
763 MF.getFrameInfo()->CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true);
764 }
765
766 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
767 // a large stack, which will require scavenging a register to materialize a
768 // large offset.
769 // FIXME: this doesn't actually check stack size, so is a bit pessimistic
770 // FIXME: doesn't detect whether or not we need to spill vXX, which requires
771 // r0 for now.
772
773 if (RegInfo->requiresRegisterScavenging(MF)) // FIXME (64-bit): Enable.
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000774 if (needsFP(MF) || spillsCR(MF)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000775 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
776 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
777 const TargetRegisterClass *RC = isPPC64 ? G8RC : GPRC;
778 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
779 RC->getAlignment(),
780 false));
781 }
782}
783
784void PPCFrameInfo::processFunctionBeforeFrameFinalized(MachineFunction &MF)
785 const {
786 // Early exit if not using the SVR4 ABI.
787 if (!Subtarget.isSVR4ABI())
788 return;
789
790 // Get callee saved register information.
791 MachineFrameInfo *FFI = MF.getFrameInfo();
792 const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
793
794 // Early exit if no callee saved registers are modified!
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000795 if (CSI.empty() && !needsFP(MF)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000796 return;
797 }
798
799 unsigned MinGPR = PPC::R31;
800 unsigned MinG8R = PPC::X31;
801 unsigned MinFPR = PPC::F31;
802 unsigned MinVR = PPC::V31;
803
804 bool HasGPSaveArea = false;
805 bool HasG8SaveArea = false;
806 bool HasFPSaveArea = false;
807 bool HasCRSaveArea = false;
808 bool HasVRSAVESaveArea = false;
809 bool HasVRSaveArea = false;
810
811 SmallVector<CalleeSavedInfo, 18> GPRegs;
812 SmallVector<CalleeSavedInfo, 18> G8Regs;
813 SmallVector<CalleeSavedInfo, 18> FPRegs;
814 SmallVector<CalleeSavedInfo, 18> VRegs;
815
816 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
817 unsigned Reg = CSI[i].getReg();
818 if (PPC::GPRCRegisterClass->contains(Reg)) {
819 HasGPSaveArea = true;
820
821 GPRegs.push_back(CSI[i]);
822
823 if (Reg < MinGPR) {
824 MinGPR = Reg;
825 }
826 } else if (PPC::G8RCRegisterClass->contains(Reg)) {
827 HasG8SaveArea = true;
828
829 G8Regs.push_back(CSI[i]);
830
831 if (Reg < MinG8R) {
832 MinG8R = Reg;
833 }
834 } else if (PPC::F8RCRegisterClass->contains(Reg)) {
835 HasFPSaveArea = true;
836
837 FPRegs.push_back(CSI[i]);
838
839 if (Reg < MinFPR) {
840 MinFPR = Reg;
841 }
842// FIXME SVR4: Disable CR save area for now.
843 } else if (PPC::CRBITRCRegisterClass->contains(Reg)
844 || PPC::CRRCRegisterClass->contains(Reg)) {
845// HasCRSaveArea = true;
846 } else if (PPC::VRSAVERCRegisterClass->contains(Reg)) {
847 HasVRSAVESaveArea = true;
848 } else if (PPC::VRRCRegisterClass->contains(Reg)) {
849 HasVRSaveArea = true;
850
851 VRegs.push_back(CSI[i]);
852
853 if (Reg < MinVR) {
854 MinVR = Reg;
855 }
856 } else {
857 llvm_unreachable("Unknown RegisterClass!");
858 }
859 }
860
861 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
862
863 int64_t LowerBound = 0;
864
865 // Take into account stack space reserved for tail calls.
866 int TCSPDelta = 0;
867 if (GuaranteedTailCallOpt && (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
868 LowerBound = TCSPDelta;
869 }
870
871 // The Floating-point register save area is right below the back chain word
872 // of the previous stack frame.
873 if (HasFPSaveArea) {
874 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
875 int FI = FPRegs[i].getFrameIdx();
876
877 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
878 }
879
880 LowerBound -= (31 - PPCRegisterInfo::getRegisterNumbering(MinFPR) + 1) * 8;
881 }
882
883 // Check whether the frame pointer register is allocated. If so, make sure it
884 // is spilled to the correct offset.
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000885 if (needsFP(MF)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000886 HasGPSaveArea = true;
887
888 int FI = PFI->getFramePointerSaveIndex();
889 assert(FI && "No Frame Pointer Save Slot!");
890
891 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
892 }
893
894 // General register save area starts right below the Floating-point
895 // register save area.
896 if (HasGPSaveArea || HasG8SaveArea) {
897 // Move general register save area spill slots down, taking into account
898 // the size of the Floating-point register save area.
899 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
900 int FI = GPRegs[i].getFrameIdx();
901
902 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
903 }
904
905 // Move general register save area spill slots down, taking into account
906 // the size of the Floating-point register save area.
907 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
908 int FI = G8Regs[i].getFrameIdx();
909
910 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
911 }
912
913 unsigned MinReg =
914 std::min<unsigned>(PPCRegisterInfo::getRegisterNumbering(MinGPR),
915 PPCRegisterInfo::getRegisterNumbering(MinG8R));
916
917 if (Subtarget.isPPC64()) {
918 LowerBound -= (31 - MinReg + 1) * 8;
919 } else {
920 LowerBound -= (31 - MinReg + 1) * 4;
921 }
922 }
923
924 // The CR save area is below the general register save area.
925 if (HasCRSaveArea) {
926 // FIXME SVR4: Is it actually possible to have multiple elements in CSI
927 // which have the CR/CRBIT register class?
928 // Adjust the frame index of the CR spill slot.
929 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
930 unsigned Reg = CSI[i].getReg();
931
932 if (PPC::CRBITRCRegisterClass->contains(Reg) ||
933 PPC::CRRCRegisterClass->contains(Reg)) {
934 int FI = CSI[i].getFrameIdx();
935
936 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
937 }
938 }
939
940 LowerBound -= 4; // The CR save area is always 4 bytes long.
941 }
942
943 if (HasVRSAVESaveArea) {
944 // FIXME SVR4: Is it actually possible to have multiple elements in CSI
945 // which have the VRSAVE register class?
946 // Adjust the frame index of the VRSAVE spill slot.
947 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
948 unsigned Reg = CSI[i].getReg();
949
950 if (PPC::VRSAVERCRegisterClass->contains(Reg)) {
951 int FI = CSI[i].getFrameIdx();
952
953 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
954 }
955 }
956
957 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
958 }
959
960 if (HasVRSaveArea) {
961 // Insert alignment padding, we need 16-byte alignment.
962 LowerBound = (LowerBound - 15) & ~(15);
963
964 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
965 int FI = VRegs[i].getFrameIdx();
966
967 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
968 }
969 }
970}