blob: 3823e3f5fa4f468f46e27cc7983cdf5b65026e64 [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"
23#include "llvm/Target/TargetOptions.h"
24
25using namespace llvm;
26
27// FIXME This disables some code that aligns the stack to a boundary bigger than
28// the default (16 bytes on Darwin) when there is a stack local of greater
29// alignment. This does not currently work, because the delta between old and
30// new stack pointers is added to offsets that reference incoming parameters
31// after the prolog is generated, and the code that does that doesn't handle a
32// variable delta. You don't want to do that anyway; a better approach is to
33// reserve another register that retains to the incoming stack pointer, and
34// reference parameters relative to that.
35#define ALIGN_STACK 0
36
37
38/// VRRegNo - Map from a numbered VR register to its enum value.
39///
40static const unsigned short VRRegNo[] = {
41 PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
42 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15,
43 PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23,
44 PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31
45};
46
47/// RemoveVRSaveCode - We have found that this function does not need any code
48/// to manipulate the VRSAVE register, even though it uses vector registers.
49/// This can happen when the only registers used are known to be live in or out
50/// of the function. Remove all of the VRSAVE related code from the function.
51static void RemoveVRSaveCode(MachineInstr *MI) {
52 MachineBasicBlock *Entry = MI->getParent();
53 MachineFunction *MF = Entry->getParent();
54
55 // We know that the MTVRSAVE instruction immediately follows MI. Remove it.
56 MachineBasicBlock::iterator MBBI = MI;
57 ++MBBI;
58 assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE);
59 MBBI->eraseFromParent();
60
61 bool RemovedAllMTVRSAVEs = true;
62 // See if we can find and remove the MTVRSAVE instruction from all of the
63 // epilog blocks.
64 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) {
65 // If last instruction is a return instruction, add an epilogue
66 if (!I->empty() && I->back().getDesc().isReturn()) {
67 bool FoundIt = false;
68 for (MBBI = I->end(); MBBI != I->begin(); ) {
69 --MBBI;
70 if (MBBI->getOpcode() == PPC::MTVRSAVE) {
71 MBBI->eraseFromParent(); // remove it.
72 FoundIt = true;
73 break;
74 }
75 }
76 RemovedAllMTVRSAVEs &= FoundIt;
77 }
78 }
79
80 // If we found and removed all MTVRSAVE instructions, remove the read of
81 // VRSAVE as well.
82 if (RemovedAllMTVRSAVEs) {
83 MBBI = MI;
84 assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?");
85 --MBBI;
86 assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?");
87 MBBI->eraseFromParent();
88 }
89
90 // Finally, nuke the UPDATE_VRSAVE.
91 MI->eraseFromParent();
92}
93
94// HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the
95// instruction selector. Based on the vector registers that have been used,
96// transform this into the appropriate ORI instruction.
97static void HandleVRSaveUpdate(MachineInstr *MI, const TargetInstrInfo &TII) {
98 MachineFunction *MF = MI->getParent()->getParent();
99 DebugLoc dl = MI->getDebugLoc();
100
101 unsigned UsedRegMask = 0;
102 for (unsigned i = 0; i != 32; ++i)
103 if (MF->getRegInfo().isPhysRegUsed(VRRegNo[i]))
104 UsedRegMask |= 1 << (31-i);
105
106 // Live in and live out values already must be in the mask, so don't bother
107 // marking them.
108 for (MachineRegisterInfo::livein_iterator
109 I = MF->getRegInfo().livein_begin(),
110 E = MF->getRegInfo().livein_end(); I != E; ++I) {
111 unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(I->first);
112 if (VRRegNo[RegNo] == I->first) // If this really is a vector reg.
113 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
114 }
115 for (MachineRegisterInfo::liveout_iterator
116 I = MF->getRegInfo().liveout_begin(),
117 E = MF->getRegInfo().liveout_end(); I != E; ++I) {
118 unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(*I);
119 if (VRRegNo[RegNo] == *I) // If this really is a vector reg.
120 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
121 }
122
123 // If no registers are used, turn this into a copy.
124 if (UsedRegMask == 0) {
125 // Remove all VRSAVE code.
126 RemoveVRSaveCode(MI);
127 return;
128 }
129
130 unsigned SrcReg = MI->getOperand(1).getReg();
131 unsigned DstReg = MI->getOperand(0).getReg();
132
133 if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
134 if (DstReg != SrcReg)
135 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
136 .addReg(SrcReg)
137 .addImm(UsedRegMask);
138 else
139 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
140 .addReg(SrcReg, RegState::Kill)
141 .addImm(UsedRegMask);
142 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
143 if (DstReg != SrcReg)
144 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
145 .addReg(SrcReg)
146 .addImm(UsedRegMask >> 16);
147 else
148 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
149 .addReg(SrcReg, RegState::Kill)
150 .addImm(UsedRegMask >> 16);
151 } else {
152 if (DstReg != SrcReg)
153 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
154 .addReg(SrcReg)
155 .addImm(UsedRegMask >> 16);
156 else
157 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
158 .addReg(SrcReg, RegState::Kill)
159 .addImm(UsedRegMask >> 16);
160
161 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
162 .addReg(DstReg, RegState::Kill)
163 .addImm(UsedRegMask & 0xFFFF);
164 }
165
166 // Remove the old UPDATE_VRSAVE instruction.
167 MI->eraseFromParent();
168}
169
170/// determineFrameLayout - Determine the size of the frame and maximum call
171/// frame size.
172void PPCFrameInfo::determineFrameLayout(MachineFunction &MF) const {
173 MachineFrameInfo *MFI = MF.getFrameInfo();
174
175 // Get the number of bytes to allocate from the FrameInfo
176 unsigned FrameSize = MFI->getStackSize();
177
178 // Get the alignments provided by the target, and the maximum alignment
179 // (if any) of the fixed frame objects.
180 unsigned MaxAlign = MFI->getMaxAlignment();
181 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
182 unsigned AlignMask = TargetAlign - 1; //
183
184 // If we are a leaf function, and use up to 224 bytes of stack space,
185 // don't have a frame pointer, calls, or dynamic alloca then we do not need
186 // to adjust the stack pointer (we fit in the Red Zone).
187 bool DisableRedZone = MF.getFunction()->hasFnAttr(Attribute::NoRedZone);
188 // FIXME SVR4 The 32-bit SVR4 ABI has no red zone.
189 if (!DisableRedZone &&
190 FrameSize <= 224 && // Fits in red zone.
191 !MFI->hasVarSizedObjects() && // No dynamic alloca.
192 !MFI->adjustsStack() && // No calls.
193 (!ALIGN_STACK || MaxAlign <= TargetAlign)) { // No special alignment.
194 // No need for frame
195 MFI->setStackSize(0);
196 return;
197 }
198
199 // Get the maximum call frame size of all the calls.
200 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
201
202 // Maximum call frame needs to be at least big enough for linkage and 8 args.
203 unsigned minCallFrameSize = getMinCallFrameSize(Subtarget.isPPC64(),
204 Subtarget.isDarwinABI());
205 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
206
207 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
208 // that allocations will be aligned.
209 if (MFI->hasVarSizedObjects())
210 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
211
212 // Update maximum call frame size.
213 MFI->setMaxCallFrameSize(maxCallFrameSize);
214
215 // Include call frame size in total.
216 FrameSize += maxCallFrameSize;
217
218 // Make sure the frame is aligned.
219 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
220
221 // Update frame info.
222 MFI->setStackSize(FrameSize);
223}
224
225void PPCFrameInfo::emitPrologue(MachineFunction &MF) const {
226 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
227 MachineBasicBlock::iterator MBBI = MBB.begin();
228 MachineFrameInfo *MFI = MF.getFrameInfo();
229 const PPCRegisterInfo *RegInfo =
230 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
231 const PPCInstrInfo &TII =
232 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
233
234 MachineModuleInfo &MMI = MF.getMMI();
235 DebugLoc dl;
236 bool needsFrameMoves = MMI.hasDebugInfo() ||
237 !MF.getFunction()->doesNotThrow() ||
238 UnwindTablesMandatory;
239
240 // Prepare for frame info.
241 MCSymbol *FrameLabel = 0;
242
243 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
244 // process it.
245 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
246 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
247 HandleVRSaveUpdate(MBBI, TII);
248 break;
249 }
250 }
251
252 // Move MBBI back to the beginning of the function.
253 MBBI = MBB.begin();
254
255 // Work out frame sizes.
256 determineFrameLayout(MF);
257 unsigned FrameSize = MFI->getStackSize();
258
259 int NegFrameSize = -FrameSize;
260
261 // Get processor type.
262 bool isPPC64 = Subtarget.isPPC64();
263 // Get operating system
264 bool isDarwinABI = Subtarget.isDarwinABI();
265 // Check if the link register (LR) must be saved.
266 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
267 bool MustSaveLR = FI->mustSaveLR();
268 // Do we have a frame pointer for this function?
269 bool HasFP = RegInfo->hasFP(MF) && FrameSize;
270
271 int LROffset = PPCFrameInfo::getReturnSaveOffset(isPPC64, isDarwinABI);
272
273 int FPOffset = 0;
274 if (HasFP) {
275 if (Subtarget.isSVR4ABI()) {
276 MachineFrameInfo *FFI = MF.getFrameInfo();
277 int FPIndex = FI->getFramePointerSaveIndex();
278 assert(FPIndex && "No Frame Pointer Save Slot!");
279 FPOffset = FFI->getObjectOffset(FPIndex);
280 } else {
281 FPOffset = PPCFrameInfo::getFramePointerSaveOffset(isPPC64, isDarwinABI);
282 }
283 }
284
285 if (isPPC64) {
286 if (MustSaveLR)
287 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR8), PPC::X0);
288
289 if (HasFP)
290 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD))
291 .addReg(PPC::X31)
292 .addImm(FPOffset/4)
293 .addReg(PPC::X1);
294
295 if (MustSaveLR)
296 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD))
297 .addReg(PPC::X0)
298 .addImm(LROffset / 4)
299 .addReg(PPC::X1);
300 } else {
301 if (MustSaveLR)
302 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR), PPC::R0);
303
304 if (HasFP)
305 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW))
306 .addReg(PPC::R31)
307 .addImm(FPOffset)
308 .addReg(PPC::R1);
309
310 if (MustSaveLR)
311 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW))
312 .addReg(PPC::R0)
313 .addImm(LROffset)
314 .addReg(PPC::R1);
315 }
316
317 // Skip if a leaf routine.
318 if (!FrameSize) return;
319
320 // Get stack alignments.
321 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
322 unsigned MaxAlign = MFI->getMaxAlignment();
323
324 // Adjust stack pointer: r1 += NegFrameSize.
325 // If there is a preferred stack alignment, align R1 now
326 if (!isPPC64) {
327 // PPC32.
328 if (ALIGN_STACK && MaxAlign > TargetAlign) {
329 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
330 "Invalid alignment!");
331 assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!");
332
333 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), PPC::R0)
334 .addReg(PPC::R1)
335 .addImm(0)
336 .addImm(32 - Log2_32(MaxAlign))
337 .addImm(31);
338 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC) ,PPC::R0)
339 .addReg(PPC::R0, RegState::Kill)
340 .addImm(NegFrameSize);
341 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX))
342 .addReg(PPC::R1)
343 .addReg(PPC::R1)
344 .addReg(PPC::R0);
345 } else if (isInt<16>(NegFrameSize)) {
346 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWU), PPC::R1)
347 .addReg(PPC::R1)
348 .addImm(NegFrameSize)
349 .addReg(PPC::R1);
350 } else {
351 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0)
352 .addImm(NegFrameSize >> 16);
353 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0)
354 .addReg(PPC::R0, RegState::Kill)
355 .addImm(NegFrameSize & 0xFFFF);
356 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX))
357 .addReg(PPC::R1)
358 .addReg(PPC::R1)
359 .addReg(PPC::R0);
360 }
361 } else { // PPC64.
362 if (ALIGN_STACK && MaxAlign > TargetAlign) {
363 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
364 "Invalid alignment!");
365 assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!");
366
367 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), PPC::X0)
368 .addReg(PPC::X1)
369 .addImm(0)
370 .addImm(64 - Log2_32(MaxAlign));
371 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC8), PPC::X0)
372 .addReg(PPC::X0)
373 .addImm(NegFrameSize);
374 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX))
375 .addReg(PPC::X1)
376 .addReg(PPC::X1)
377 .addReg(PPC::X0);
378 } else if (isInt<16>(NegFrameSize)) {
379 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDU), PPC::X1)
380 .addReg(PPC::X1)
381 .addImm(NegFrameSize / 4)
382 .addReg(PPC::X1);
383 } else {
384 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0)
385 .addImm(NegFrameSize >> 16);
386 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0)
387 .addReg(PPC::X0, RegState::Kill)
388 .addImm(NegFrameSize & 0xFFFF);
389 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX))
390 .addReg(PPC::X1)
391 .addReg(PPC::X1)
392 .addReg(PPC::X0);
393 }
394 }
395
396 std::vector<MachineMove> &Moves = MMI.getFrameMoves();
397
398 // Add the "machine moves" for the instructions we generated above, but in
399 // reverse order.
400 if (needsFrameMoves) {
401 // Mark effective beginning of when frame pointer becomes valid.
402 FrameLabel = MMI.getContext().CreateTempSymbol();
403 BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(FrameLabel);
404
405 // Show update of SP.
406 if (NegFrameSize) {
407 MachineLocation SPDst(MachineLocation::VirtualFP);
408 MachineLocation SPSrc(MachineLocation::VirtualFP, NegFrameSize);
409 Moves.push_back(MachineMove(FrameLabel, SPDst, SPSrc));
410 } else {
411 MachineLocation SP(isPPC64 ? PPC::X31 : PPC::R31);
412 Moves.push_back(MachineMove(FrameLabel, SP, SP));
413 }
414
415 if (HasFP) {
416 MachineLocation FPDst(MachineLocation::VirtualFP, FPOffset);
417 MachineLocation FPSrc(isPPC64 ? PPC::X31 : PPC::R31);
418 Moves.push_back(MachineMove(FrameLabel, FPDst, FPSrc));
419 }
420
421 if (MustSaveLR) {
422 MachineLocation LRDst(MachineLocation::VirtualFP, LROffset);
423 MachineLocation LRSrc(isPPC64 ? PPC::LR8 : PPC::LR);
424 Moves.push_back(MachineMove(FrameLabel, LRDst, LRSrc));
425 }
426 }
427
428 MCSymbol *ReadyLabel = 0;
429
430 // If there is a frame pointer, copy R1 into R31
431 if (HasFP) {
432 if (!isPPC64) {
433 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR), PPC::R31)
434 .addReg(PPC::R1)
435 .addReg(PPC::R1);
436 } else {
437 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR8), PPC::X31)
438 .addReg(PPC::X1)
439 .addReg(PPC::X1);
440 }
441
442 if (needsFrameMoves) {
443 ReadyLabel = MMI.getContext().CreateTempSymbol();
444
445 // Mark effective beginning of when frame pointer is ready.
446 BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(ReadyLabel);
447
448 MachineLocation FPDst(HasFP ? (isPPC64 ? PPC::X31 : PPC::R31) :
449 (isPPC64 ? PPC::X1 : PPC::R1));
450 MachineLocation FPSrc(MachineLocation::VirtualFP);
451 Moves.push_back(MachineMove(ReadyLabel, FPDst, FPSrc));
452 }
453 }
454
455 if (needsFrameMoves) {
456 MCSymbol *Label = HasFP ? ReadyLabel : FrameLabel;
457
458 // Add callee saved registers to move list.
459 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
460 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
461 int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx());
462 unsigned Reg = CSI[I].getReg();
463 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
464 MachineLocation CSDst(MachineLocation::VirtualFP, Offset);
465 MachineLocation CSSrc(Reg);
466 Moves.push_back(MachineMove(Label, CSDst, CSSrc));
467 }
468 }
469}
470
471void PPCFrameInfo::emitEpilogue(MachineFunction &MF,
472 MachineBasicBlock &MBB) const {
473 MachineBasicBlock::iterator MBBI = prior(MBB.end());
474 const PPCRegisterInfo *RegInfo =
475 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
476 const PPCInstrInfo &TII =
477 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
478
479 unsigned RetOpcode = MBBI->getOpcode();
480 DebugLoc dl;
481
482 assert( (RetOpcode == PPC::BLR ||
483 RetOpcode == PPC::TCRETURNri ||
484 RetOpcode == PPC::TCRETURNdi ||
485 RetOpcode == PPC::TCRETURNai ||
486 RetOpcode == PPC::TCRETURNri8 ||
487 RetOpcode == PPC::TCRETURNdi8 ||
488 RetOpcode == PPC::TCRETURNai8) &&
489 "Can only insert epilog into returning blocks");
490
491 // Get alignment info so we know how to restore r1
492 const MachineFrameInfo *MFI = MF.getFrameInfo();
493 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
494 unsigned MaxAlign = MFI->getMaxAlignment();
495
496 // Get the number of bytes allocated from the FrameInfo.
497 int FrameSize = MFI->getStackSize();
498
499 // Get processor type.
500 bool isPPC64 = Subtarget.isPPC64();
501 // Get operating system
502 bool isDarwinABI = Subtarget.isDarwinABI();
503 // Check if the link register (LR) has been saved.
504 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
505 bool MustSaveLR = FI->mustSaveLR();
506 // Do we have a frame pointer for this function?
507 bool HasFP = RegInfo->hasFP(MF) && FrameSize;
508
509 int LROffset = PPCFrameInfo::getReturnSaveOffset(isPPC64, isDarwinABI);
510
511 int FPOffset = 0;
512 if (HasFP) {
513 if (Subtarget.isSVR4ABI()) {
514 MachineFrameInfo *FFI = MF.getFrameInfo();
515 int FPIndex = FI->getFramePointerSaveIndex();
516 assert(FPIndex && "No Frame Pointer Save Slot!");
517 FPOffset = FFI->getObjectOffset(FPIndex);
518 } else {
519 FPOffset = PPCFrameInfo::getFramePointerSaveOffset(isPPC64, isDarwinABI);
520 }
521 }
522
523 bool UsesTCRet = RetOpcode == PPC::TCRETURNri ||
524 RetOpcode == PPC::TCRETURNdi ||
525 RetOpcode == PPC::TCRETURNai ||
526 RetOpcode == PPC::TCRETURNri8 ||
527 RetOpcode == PPC::TCRETURNdi8 ||
528 RetOpcode == PPC::TCRETURNai8;
529
530 if (UsesTCRet) {
531 int MaxTCRetDelta = FI->getTailCallSPDelta();
532 MachineOperand &StackAdjust = MBBI->getOperand(1);
533 assert(StackAdjust.isImm() && "Expecting immediate value.");
534 // Adjust stack pointer.
535 int StackAdj = StackAdjust.getImm();
536 int Delta = StackAdj - MaxTCRetDelta;
537 assert((Delta >= 0) && "Delta must be positive");
538 if (MaxTCRetDelta>0)
539 FrameSize += (StackAdj +Delta);
540 else
541 FrameSize += StackAdj;
542 }
543
544 if (FrameSize) {
545 // The loaded (or persistent) stack pointer value is offset by the 'stwu'
546 // on entry to the function. Add this offset back now.
547 if (!isPPC64) {
548 // If this function contained a fastcc call and GuaranteedTailCallOpt is
549 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
550 // call which invalidates the stack pointer value in SP(0). So we use the
551 // value of R31 in this case.
552 if (FI->hasFastCall() && isInt<16>(FrameSize)) {
553 assert(RegInfo->hasFP(MF) && "Expecting a valid the frame pointer.");
554 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1)
555 .addReg(PPC::R31).addImm(FrameSize);
556 } else if(FI->hasFastCall()) {
557 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0)
558 .addImm(FrameSize >> 16);
559 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0)
560 .addReg(PPC::R0, RegState::Kill)
561 .addImm(FrameSize & 0xFFFF);
562 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD4))
563 .addReg(PPC::R1)
564 .addReg(PPC::R31)
565 .addReg(PPC::R0);
566 } else if (isInt<16>(FrameSize) &&
567 (!ALIGN_STACK || TargetAlign >= MaxAlign) &&
568 !MFI->hasVarSizedObjects()) {
569 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1)
570 .addReg(PPC::R1).addImm(FrameSize);
571 } else {
572 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ),PPC::R1)
573 .addImm(0).addReg(PPC::R1);
574 }
575 } else {
576 if (FI->hasFastCall() && isInt<16>(FrameSize)) {
577 assert(RegInfo->hasFP(MF) && "Expecting a valid the frame pointer.");
578 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1)
579 .addReg(PPC::X31).addImm(FrameSize);
580 } else if(FI->hasFastCall()) {
581 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0)
582 .addImm(FrameSize >> 16);
583 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0)
584 .addReg(PPC::X0, RegState::Kill)
585 .addImm(FrameSize & 0xFFFF);
586 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD8))
587 .addReg(PPC::X1)
588 .addReg(PPC::X31)
589 .addReg(PPC::X0);
590 } else if (isInt<16>(FrameSize) && TargetAlign >= MaxAlign &&
591 !MFI->hasVarSizedObjects()) {
592 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1)
593 .addReg(PPC::X1).addImm(FrameSize);
594 } else {
595 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X1)
596 .addImm(0).addReg(PPC::X1);
597 }
598 }
599 }
600
601 if (isPPC64) {
602 if (MustSaveLR)
603 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X0)
604 .addImm(LROffset/4).addReg(PPC::X1);
605
606 if (HasFP)
607 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X31)
608 .addImm(FPOffset/4).addReg(PPC::X1);
609
610 if (MustSaveLR)
611 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR8)).addReg(PPC::X0);
612 } else {
613 if (MustSaveLR)
614 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R0)
615 .addImm(LROffset).addReg(PPC::R1);
616
617 if (HasFP)
618 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R31)
619 .addImm(FPOffset).addReg(PPC::R1);
620
621 if (MustSaveLR)
622 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR)).addReg(PPC::R0);
623 }
624
625 // Callee pop calling convention. Pop parameter/linkage area. Used for tail
626 // call optimization
627 if (GuaranteedTailCallOpt && RetOpcode == PPC::BLR &&
628 MF.getFunction()->getCallingConv() == CallingConv::Fast) {
629 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
630 unsigned CallerAllocatedAmt = FI->getMinReservedArea();
631 unsigned StackReg = isPPC64 ? PPC::X1 : PPC::R1;
632 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
633 unsigned TmpReg = isPPC64 ? PPC::X0 : PPC::R0;
634 unsigned ADDIInstr = isPPC64 ? PPC::ADDI8 : PPC::ADDI;
635 unsigned ADDInstr = isPPC64 ? PPC::ADD8 : PPC::ADD4;
636 unsigned LISInstr = isPPC64 ? PPC::LIS8 : PPC::LIS;
637 unsigned ORIInstr = isPPC64 ? PPC::ORI8 : PPC::ORI;
638
639 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
640 BuildMI(MBB, MBBI, dl, TII.get(ADDIInstr), StackReg)
641 .addReg(StackReg).addImm(CallerAllocatedAmt);
642 } else {
643 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
644 .addImm(CallerAllocatedAmt >> 16);
645 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
646 .addReg(TmpReg, RegState::Kill)
647 .addImm(CallerAllocatedAmt & 0xFFFF);
648 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr))
649 .addReg(StackReg)
650 .addReg(FPReg)
651 .addReg(TmpReg);
652 }
653 } else if (RetOpcode == PPC::TCRETURNdi) {
654 MBBI = prior(MBB.end());
655 MachineOperand &JumpTarget = MBBI->getOperand(0);
656 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
657 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
658 } else if (RetOpcode == PPC::TCRETURNri) {
659 MBBI = prior(MBB.end());
660 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
661 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
662 } else if (RetOpcode == PPC::TCRETURNai) {
663 MBBI = prior(MBB.end());
664 MachineOperand &JumpTarget = MBBI->getOperand(0);
665 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
666 } else if (RetOpcode == PPC::TCRETURNdi8) {
667 MBBI = prior(MBB.end());
668 MachineOperand &JumpTarget = MBBI->getOperand(0);
669 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
670 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
671 } else if (RetOpcode == PPC::TCRETURNri8) {
672 MBBI = prior(MBB.end());
673 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
674 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
675 } else if (RetOpcode == PPC::TCRETURNai8) {
676 MBBI = prior(MBB.end());
677 MachineOperand &JumpTarget = MBBI->getOperand(0);
678 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
679 }
680}