1//===-- PPCFrameLowering.cpp - PPC Frame Information ----------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the PPC implementation of TargetFrameLowering class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "PPCFrameLowering.h"
14#include "MCTargetDesc/PPCPredicates.h"
15#include "PPCInstrBuilder.h"
16#include "PPCInstrInfo.h"
17#include "PPCMachineFunctionInfo.h"
18#include "PPCSubtarget.h"
19#include "PPCTargetMachine.h"
20#include "llvm/ADT/Statistic.h"
21#include "llvm/CodeGen/LivePhysRegs.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineModuleInfo.h"
26#include "llvm/CodeGen/MachineRegisterInfo.h"
27#include "llvm/CodeGen/RegisterScavenging.h"
28#include "llvm/IR/Function.h"
29#include "llvm/Target/TargetOptions.h"
30
31using namespace llvm;
32
33#define DEBUG_TYPE "framelowering"
34STATISTIC(NumPESpillVSR, "Number of spills to vector in prologue");
35STATISTIC(NumPEReloadVSR, "Number of reloads from vector in epilogue");
36STATISTIC(NumPrologProbed, "Number of prologues probed");
37
38static cl::opt<bool>
39EnablePEVectorSpills("ppc-enable-pe-vector-spills",
40 cl::desc("Enable spills in prologue to vector registers."),
41 cl::init(Val: false), cl::Hidden);
42
43static unsigned computeReturnSaveOffset(const PPCSubtarget &STI) {
44 if (STI.isAIXABI())
45 return STI.isPPC64() ? 16 : 8;
46 // SVR4 ABI:
47 return STI.isPPC64() ? 16 : 4;
48}
49
50static unsigned computeTOCSaveOffset(const PPCSubtarget &STI) {
51 if (STI.isAIXABI())
52 return STI.isPPC64() ? 40 : 20;
53 return STI.isELFv2ABI() ? 24 : 40;
54}
55
56static unsigned computeFramePointerSaveOffset(const PPCSubtarget &STI) {
57 // First slot in the general register save area.
58 return STI.isPPC64() ? -8U : -4U;
59}
60
61static unsigned computeLinkageSize(const PPCSubtarget &STI) {
62 if (STI.isAIXABI() || STI.isPPC64())
63 return (STI.isELFv2ABI() ? 4 : 6) * (STI.isPPC64() ? 8 : 4);
64
65 // 32-bit SVR4 ABI:
66 return 8;
67}
68
69static unsigned computeBasePointerSaveOffset(const PPCSubtarget &STI) {
70 // Third slot in the general purpose register save area.
71 if (STI.is32BitELFABI() && STI.getTargetMachine().isPositionIndependent())
72 return -12U;
73
74 // Second slot in the general purpose register save area.
75 return STI.isPPC64() ? -16U : -8U;
76}
77
78static unsigned computeCRSaveOffset(const PPCSubtarget &STI) {
79 return (STI.isAIXABI() && !STI.isPPC64()) ? 4 : 8;
80}
81
82PPCFrameLowering::PPCFrameLowering(const PPCSubtarget &STI)
83 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown,
84 STI.getPlatformStackAlignment(), 0),
85 Subtarget(STI), ReturnSaveOffset(computeReturnSaveOffset(STI: Subtarget)),
86 TOCSaveOffset(computeTOCSaveOffset(STI: Subtarget)),
87 FramePointerSaveOffset(computeFramePointerSaveOffset(STI: Subtarget)),
88 LinkageSize(computeLinkageSize(STI: Subtarget)),
89 BasePointerSaveOffset(computeBasePointerSaveOffset(STI: Subtarget)),
90 CRSaveOffset(computeCRSaveOffset(STI: Subtarget)) {}
91
92// With the SVR4 ABI, callee-saved registers have fixed offsets on the stack.
93const PPCFrameLowering::SpillSlot *PPCFrameLowering::getCalleeSavedSpillSlots(
94 unsigned &NumEntries) const {
95
96// Floating-point register save area offsets.
97#define CALLEE_SAVED_FPRS \
98 {PPC::F31, -8}, \
99 {PPC::F30, -16}, \
100 {PPC::F29, -24}, \
101 {PPC::F28, -32}, \
102 {PPC::F27, -40}, \
103 {PPC::F26, -48}, \
104 {PPC::F25, -56}, \
105 {PPC::F24, -64}, \
106 {PPC::F23, -72}, \
107 {PPC::F22, -80}, \
108 {PPC::F21, -88}, \
109 {PPC::F20, -96}, \
110 {PPC::F19, -104}, \
111 {PPC::F18, -112}, \
112 {PPC::F17, -120}, \
113 {PPC::F16, -128}, \
114 {PPC::F15, -136}, \
115 {PPC::F14, -144}
116
117// 32-bit general purpose register save area offsets shared by ELF and
118// AIX. AIX has an extra CSR with r13.
119#define CALLEE_SAVED_GPRS32 \
120 {PPC::R31, -4}, \
121 {PPC::R30, -8}, \
122 {PPC::R29, -12}, \
123 {PPC::R28, -16}, \
124 {PPC::R27, -20}, \
125 {PPC::R26, -24}, \
126 {PPC::R25, -28}, \
127 {PPC::R24, -32}, \
128 {PPC::R23, -36}, \
129 {PPC::R22, -40}, \
130 {PPC::R21, -44}, \
131 {PPC::R20, -48}, \
132 {PPC::R19, -52}, \
133 {PPC::R18, -56}, \
134 {PPC::R17, -60}, \
135 {PPC::R16, -64}, \
136 {PPC::R15, -68}, \
137 {PPC::R14, -72}
138
139// 64-bit general purpose register save area offsets.
140#define CALLEE_SAVED_GPRS64 \
141 {PPC::X31, -8}, \
142 {PPC::X30, -16}, \
143 {PPC::X29, -24}, \
144 {PPC::X28, -32}, \
145 {PPC::X27, -40}, \
146 {PPC::X26, -48}, \
147 {PPC::X25, -56}, \
148 {PPC::X24, -64}, \
149 {PPC::X23, -72}, \
150 {PPC::X22, -80}, \
151 {PPC::X21, -88}, \
152 {PPC::X20, -96}, \
153 {PPC::X19, -104}, \
154 {PPC::X18, -112}, \
155 {PPC::X17, -120}, \
156 {PPC::X16, -128}, \
157 {PPC::X15, -136}, \
158 {PPC::X14, -144}
159
160// Vector register save area offsets.
161#define CALLEE_SAVED_VRS \
162 {PPC::V31, -16}, \
163 {PPC::V30, -32}, \
164 {PPC::V29, -48}, \
165 {PPC::V28, -64}, \
166 {PPC::V27, -80}, \
167 {PPC::V26, -96}, \
168 {PPC::V25, -112}, \
169 {PPC::V24, -128}, \
170 {PPC::V23, -144}, \
171 {PPC::V22, -160}, \
172 {PPC::V21, -176}, \
173 {PPC::V20, -192}
174
175 // Note that the offsets here overlap, but this is fixed up in
176 // processFunctionBeforeFrameFinalized.
177
178 static const SpillSlot ELFOffsets32[] = {
179 CALLEE_SAVED_FPRS,
180 CALLEE_SAVED_GPRS32,
181
182 // CR save area offset. We map each of the nonvolatile CR fields
183 // to the slot for CR2, which is the first of the nonvolatile CR
184 // fields to be assigned, so that we only allocate one save slot.
185 // See PPCRegisterInfo::hasReservedSpillSlot() for more information.
186 {.Reg: PPC::CR2, .Offset: -4},
187
188 // VRSAVE save area offset.
189 {.Reg: PPC::VRSAVE, .Offset: -4},
190
191 CALLEE_SAVED_VRS,
192
193 // SPE register save area (overlaps Vector save area).
194 {.Reg: PPC::S31, .Offset: -8},
195 {.Reg: PPC::S30, .Offset: -16},
196 {.Reg: PPC::S29, .Offset: -24},
197 {.Reg: PPC::S28, .Offset: -32},
198 {.Reg: PPC::S27, .Offset: -40},
199 {.Reg: PPC::S26, .Offset: -48},
200 {.Reg: PPC::S25, .Offset: -56},
201 {.Reg: PPC::S24, .Offset: -64},
202 {.Reg: PPC::S23, .Offset: -72},
203 {.Reg: PPC::S22, .Offset: -80},
204 {.Reg: PPC::S21, .Offset: -88},
205 {.Reg: PPC::S20, .Offset: -96},
206 {.Reg: PPC::S19, .Offset: -104},
207 {.Reg: PPC::S18, .Offset: -112},
208 {.Reg: PPC::S17, .Offset: -120},
209 {.Reg: PPC::S16, .Offset: -128},
210 {.Reg: PPC::S15, .Offset: -136},
211 {.Reg: PPC::S14, .Offset: -144}};
212
213 static const SpillSlot ELFOffsets64[] = {
214 CALLEE_SAVED_FPRS,
215 CALLEE_SAVED_GPRS64,
216
217 // VRSAVE save area offset.
218 {.Reg: PPC::VRSAVE, .Offset: -4},
219 CALLEE_SAVED_VRS
220 };
221
222 static const SpillSlot AIXOffsets32[] = {CALLEE_SAVED_FPRS,
223 CALLEE_SAVED_GPRS32,
224 // Add AIX's extra CSR.
225 {.Reg: PPC::R13, .Offset: -76},
226 CALLEE_SAVED_VRS};
227
228 static const SpillSlot AIXOffsets64[] = {
229 CALLEE_SAVED_FPRS, CALLEE_SAVED_GPRS64, CALLEE_SAVED_VRS};
230
231 if (Subtarget.is64BitELFABI()) {
232 NumEntries = std::size(ELFOffsets64);
233 return ELFOffsets64;
234 }
235
236 if (Subtarget.is32BitELFABI()) {
237 NumEntries = std::size(ELFOffsets32);
238 return ELFOffsets32;
239 }
240
241 assert(Subtarget.isAIXABI() && "Unexpected ABI.");
242
243 if (Subtarget.isPPC64()) {
244 NumEntries = std::size(AIXOffsets64);
245 return AIXOffsets64;
246 }
247
248 NumEntries = std::size(AIXOffsets32);
249 return AIXOffsets32;
250}
251
252static bool spillsCR(const MachineFunction &MF) {
253 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
254 return FuncInfo->isCRSpilled();
255}
256
257static bool hasSpills(const MachineFunction &MF) {
258 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
259 return FuncInfo->hasSpills();
260}
261
262static bool hasNonRISpills(const MachineFunction &MF) {
263 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
264 return FuncInfo->hasNonRISpills();
265}
266
267/// MustSaveLR - Return true if this function requires that we save the LR
268/// register onto the stack in the prolog and restore it in the epilog of the
269/// function.
270static bool MustSaveLR(const MachineFunction &MF, MCRegister LR) {
271 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
272
273 // We need a save/restore of LR if there is any def of LR (which is
274 // defined by calls, including the PIC setup sequence), or if there is
275 // some use of the LR stack slot (e.g. for builtin_return_address).
276 // (LR comes in 32 and 64 bit versions.)
277 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(RegNo: LR);
278 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
279}
280
281/// determineFrameLayoutAndUpdate - Determine the size of the frame and maximum
282/// call frame size. Update the MachineFunction object with the stack size.
283uint64_t
284PPCFrameLowering::determineFrameLayoutAndUpdate(MachineFunction &MF,
285 bool UseEstimate) const {
286 unsigned NewMaxCallFrameSize = 0;
287 uint64_t FrameSize = determineFrameLayout(MF, UseEstimate,
288 NewMaxCallFrameSize: &NewMaxCallFrameSize);
289 MF.getFrameInfo().setStackSize(FrameSize);
290 MF.getFrameInfo().setMaxCallFrameSize(NewMaxCallFrameSize);
291 return FrameSize;
292}
293
294/// determineFrameLayout - Determine the size of the frame and maximum call
295/// frame size.
296uint64_t
297PPCFrameLowering::determineFrameLayout(const MachineFunction &MF,
298 bool UseEstimate,
299 unsigned *NewMaxCallFrameSize) const {
300 const MachineFrameInfo &MFI = MF.getFrameInfo();
301 const PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
302
303 // Get the number of bytes to allocate from the FrameInfo
304 uint64_t FrameSize =
305 UseEstimate ? MFI.estimateStackSize(MF) : MFI.getStackSize();
306
307 // Get stack alignments. The frame must be aligned to the greatest of these:
308 Align TargetAlign = getStackAlign(); // alignment required per the ABI
309 Align MaxAlign = MFI.getMaxAlign(); // algmt required by data in frame
310 Align Alignment = std::max(a: TargetAlign, b: MaxAlign);
311
312 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
313
314 MCRegister LR = RegInfo->getRARegister();
315 bool DisableRedZone = MF.getFunction().hasFnAttribute(Kind: Attribute::NoRedZone);
316 bool CanUseRedZone = !MFI.hasVarSizedObjects() && // No dynamic alloca.
317 !MFI.adjustsStack() && // No calls.
318 !MustSaveLR(MF, LR) && // No need to save LR.
319 !FI->mustSaveTOC() && // No need to save TOC.
320 !RegInfo->hasBasePointer(MF) && // No special alignment.
321 !MFI.isFrameAddressTaken();
322
323 // Note: for PPC32 SVR4ABI, we can still generate stackless
324 // code if all local vars are reg-allocated.
325 bool FitsInRedZone = FrameSize <= Subtarget.getRedZoneSize();
326
327 // Check whether we can skip adjusting the stack pointer (by using red zone)
328 if (!DisableRedZone && CanUseRedZone && FitsInRedZone) {
329 // No need for frame
330 return 0;
331 }
332
333 // Get the maximum call frame size of all the calls.
334 unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
335
336 // Maximum call frame needs to be at least big enough for linkage area.
337 unsigned minCallFrameSize = getLinkageSize();
338 maxCallFrameSize = std::max(a: maxCallFrameSize, b: minCallFrameSize);
339
340 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
341 // that allocations will be aligned.
342 if (MFI.hasVarSizedObjects())
343 maxCallFrameSize = alignTo(Size: maxCallFrameSize, A: Alignment);
344
345 // Update the new max call frame size if the caller passes in a valid pointer.
346 if (NewMaxCallFrameSize)
347 *NewMaxCallFrameSize = maxCallFrameSize;
348
349 // Include call frame size in total.
350 FrameSize += maxCallFrameSize;
351
352 // Make sure the frame is aligned.
353 FrameSize = alignTo(Size: FrameSize, A: Alignment);
354
355 return FrameSize;
356}
357
358// hasFPImpl - Return true if the specified function actually has a dedicated
359// frame pointer register.
360bool PPCFrameLowering::hasFPImpl(const MachineFunction &MF) const {
361 const MachineFrameInfo &MFI = MF.getFrameInfo();
362 // FIXME: This is pretty much broken by design: hasFP() might be called really
363 // early, before the stack layout was calculated and thus hasFP() might return
364 // true or false here depending on the time of call.
365 return (MFI.getStackSize()) && needsFP(MF);
366}
367
368// needsFP - Return true if the specified function should have a dedicated frame
369// pointer register. This is true if the function has variable sized allocas or
370// if frame pointer elimination is disabled.
371bool PPCFrameLowering::needsFP(const MachineFunction &MF) const {
372 const MachineFrameInfo &MFI = MF.getFrameInfo();
373
374 // Naked functions have no stack frame pushed, so we don't have a frame
375 // pointer.
376 if (MF.getFunction().hasFnAttribute(Kind: Attribute::Naked))
377 return false;
378
379 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
380 MFI.hasVarSizedObjects() || MFI.hasStackMap() || MFI.hasPatchPoint() ||
381 MF.exposesReturnsTwice() ||
382 (MF.getTarget().Options.GuaranteedTailCallOpt &&
383 MF.getInfo<PPCFunctionInfo>()->hasFastCall());
384}
385
386void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const {
387 // When there is dynamic alloca in this function, we can not use the frame
388 // pointer X31/R31 for the frameaddress lowering. In this case, only X1/R1
389 // always points to the backchain.
390 bool is31 = needsFP(MF) && !MF.getFrameInfo().hasVarSizedObjects();
391 unsigned FPReg = is31 ? PPC::R31 : PPC::R1;
392 unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1;
393
394 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
395 bool HasBP = RegInfo->hasBasePointer(MF);
396 unsigned BPReg = HasBP ? (unsigned) RegInfo->getBaseRegister(MF) : FPReg;
397 unsigned BP8Reg = HasBP ? (unsigned) PPC::X30 : FP8Reg;
398
399 for (MachineBasicBlock &MBB : MF)
400 for (MachineBasicBlock::iterator MBBI = MBB.end(); MBBI != MBB.begin();) {
401 --MBBI;
402 for (MachineOperand &MO : MBBI->operands()) {
403 if (!MO.isReg())
404 continue;
405
406 switch (MO.getReg()) {
407 case PPC::FP:
408 MO.setReg(FPReg);
409 break;
410 case PPC::FP8:
411 MO.setReg(FP8Reg);
412 break;
413 case PPC::BP:
414 MO.setReg(BPReg);
415 break;
416 case PPC::BP8:
417 MO.setReg(BP8Reg);
418 break;
419
420 }
421 }
422 }
423}
424
425/* This function will do the following:
426 - If MBB is an entry or exit block, set SR1 and SR2 to R0 and R12
427 respectively (defaults recommended by the ABI) and return true
428 - If MBB is not an entry block, initialize the register scavenger and look
429 for available registers.
430 - If the defaults (R0/R12) are available, return true
431 - If TwoUniqueRegsRequired is set to true, it looks for two unique
432 registers. Otherwise, look for a single available register.
433 - If the required registers are found, set SR1 and SR2 and return true.
434 - If the required registers are not found, set SR2 or both SR1 and SR2 to
435 PPC::NoRegister and return false.
436
437 Note that if both SR1 and SR2 are valid parameters and TwoUniqueRegsRequired
438 is not set, this function will attempt to find two different registers, but
439 still return true if only one register is available (and set SR1 == SR2).
440*/
441bool
442PPCFrameLowering::findScratchRegister(MachineBasicBlock *MBB,
443 bool UseAtEnd,
444 bool TwoUniqueRegsRequired,
445 Register *SR1,
446 Register *SR2) const {
447 RegScavenger RS;
448 Register R0 = Subtarget.isPPC64() ? PPC::X0 : PPC::R0;
449 Register R12 = Subtarget.isPPC64() ? PPC::X12 : PPC::R12;
450
451 // Set the defaults for the two scratch registers.
452 if (SR1)
453 *SR1 = R0;
454
455 if (SR2) {
456 assert (SR1 && "Asking for the second scratch register but not the first?");
457 *SR2 = R12;
458 }
459
460 // If MBB is an entry or exit block, use R0 and R12 as the scratch registers.
461 if ((UseAtEnd && MBB->isReturnBlock()) ||
462 (!UseAtEnd && (&MBB->getParent()->front() == MBB)))
463 return true;
464
465 if (UseAtEnd) {
466 // The scratch register will be used before the first terminator (or at the
467 // end of the block if there are no terminators).
468 MachineBasicBlock::iterator MBBI = MBB->getFirstTerminator();
469 if (MBBI == MBB->begin()) {
470 RS.enterBasicBlock(MBB&: *MBB);
471 } else {
472 RS.enterBasicBlockEnd(MBB&: *MBB);
473 RS.backward(I: MBBI);
474 }
475 } else {
476 // The scratch register will be used at the start of the block.
477 RS.enterBasicBlock(MBB&: *MBB);
478 }
479
480 // If the two registers are available, we're all good.
481 // Note that we only return here if both R0 and R12 are available because
482 // although the function may not require two unique registers, it may benefit
483 // from having two so we should try to provide them.
484 if (!RS.isRegUsed(Reg: R0) && !RS.isRegUsed(Reg: R12))
485 return true;
486
487 // Get the list of callee-saved registers for the target.
488 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
489 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(MF: MBB->getParent());
490
491 // Get all the available registers in the block.
492 BitVector BV = RS.getRegsAvailable(RC: Subtarget.isPPC64() ? &PPC::G8RCRegClass :
493 &PPC::GPRCRegClass);
494
495 // We shouldn't use callee-saved registers as scratch registers as they may be
496 // available when looking for a candidate block for shrink wrapping but not
497 // available when the actual prologue/epilogue is being emitted because they
498 // were added as live-in to the prologue block by PrologueEpilogueInserter.
499 for (int i = 0; CSRegs[i]; ++i)
500 BV.reset(Idx: CSRegs[i]);
501
502 // Set the first scratch register to the first available one.
503 if (SR1) {
504 int FirstScratchReg = BV.find_first();
505 *SR1 = FirstScratchReg == -1 ? (unsigned)PPC::NoRegister : FirstScratchReg;
506 }
507
508 // If there is another one available, set the second scratch register to that.
509 // Otherwise, set it to either PPC::NoRegister if this function requires two
510 // or to whatever SR1 is set to if this function doesn't require two.
511 if (SR2) {
512 int SecondScratchReg = BV.find_next(Prev: *SR1);
513 if (SecondScratchReg != -1)
514 *SR2 = SecondScratchReg;
515 else
516 *SR2 = TwoUniqueRegsRequired ? Register() : *SR1;
517 }
518
519 // Now that we've done our best to provide both registers, double check
520 // whether we were unable to provide enough.
521 if (BV.count() < (TwoUniqueRegsRequired ? 2U : 1U))
522 return false;
523
524 return true;
525}
526
527// We need a scratch register for spilling LR and for spilling CR. By default,
528// we use two scratch registers to hide latency. However, if only one scratch
529// register is available, we can adjust for that by not overlapping the spill
530// code. However, if we need to realign the stack (i.e. have a base pointer)
531// and the stack frame is large, we need two scratch registers.
532// Also, stack probe requires two scratch registers, one for old sp, one for
533// large frame and large probe size.
534bool
535PPCFrameLowering::twoUniqueScratchRegsRequired(MachineBasicBlock *MBB) const {
536 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
537 MachineFunction &MF = *(MBB->getParent());
538 bool HasBP = RegInfo->hasBasePointer(MF);
539 unsigned FrameSize = determineFrameLayout(MF);
540 int NegFrameSize = -FrameSize;
541 bool IsLargeFrame = !isInt<16>(x: NegFrameSize);
542 MachineFrameInfo &MFI = MF.getFrameInfo();
543 Align MaxAlign = MFI.getMaxAlign();
544 bool HasRedZone = Subtarget.isPPC64() || !Subtarget.isSVR4ABI();
545 const PPCTargetLowering &TLI = *Subtarget.getTargetLowering();
546
547 return ((IsLargeFrame || !HasRedZone) && HasBP && MaxAlign > 1) ||
548 TLI.hasInlineStackProbe(MF);
549}
550
551bool PPCFrameLowering::canUseAsPrologue(const MachineBasicBlock &MBB) const {
552 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
553
554 return findScratchRegister(MBB: TmpMBB, UseAtEnd: false,
555 TwoUniqueRegsRequired: twoUniqueScratchRegsRequired(MBB: TmpMBB));
556}
557
558bool PPCFrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const {
559 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
560
561 return findScratchRegister(MBB: TmpMBB, UseAtEnd: true);
562}
563
564bool PPCFrameLowering::stackUpdateCanBeMoved(MachineFunction &MF) const {
565 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
566 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
567
568 // Abort if there is no register info or function info.
569 if (!RegInfo || !FI)
570 return false;
571
572 // Only move the stack update on ELFv2 ABI and PPC64.
573 if (!Subtarget.isELFv2ABI() || !Subtarget.isPPC64())
574 return false;
575
576 // Check the frame size first and return false if it does not fit the
577 // requirements.
578 // We need a non-zero frame size as well as a frame that will fit in the red
579 // zone. This is because by moving the stack pointer update we are now storing
580 // to the red zone until the stack pointer is updated. If we get an interrupt
581 // inside the prologue but before the stack update we now have a number of
582 // stores to the red zone and those stores must all fit.
583 MachineFrameInfo &MFI = MF.getFrameInfo();
584 unsigned FrameSize = MFI.getStackSize();
585 if (!FrameSize || FrameSize > Subtarget.getRedZoneSize())
586 return false;
587
588 // Frame pointers and base pointers complicate matters so don't do anything
589 // if we have them. For example having a frame pointer will sometimes require
590 // a copy of r1 into r31 and that makes keeping track of updates to r1 more
591 // difficult. Similar situation exists with setjmp.
592 if (hasFP(MF) || RegInfo->hasBasePointer(MF) || MF.exposesReturnsTwice())
593 return false;
594
595 // Calls to fast_cc functions use different rules for passing parameters on
596 // the stack from the ABI and using PIC base in the function imposes
597 // similar restrictions to using the base pointer. It is not generally safe
598 // to move the stack pointer update in these situations.
599 if (FI->hasFastCall() || FI->usesPICBase())
600 return false;
601
602 // Finally we can move the stack update if we do not require register
603 // scavenging. Register scavenging can introduce more spills and so
604 // may make the frame size larger than we have computed.
605 return !RegInfo->requiresFrameIndexScavenging(MF);
606}
607
608void PPCFrameLowering::emitPrologue(MachineFunction &MF,
609 MachineBasicBlock &MBB) const {
610 MachineBasicBlock::iterator MBBI = MBB.begin();
611 MachineFrameInfo &MFI = MF.getFrameInfo();
612 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
613 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
614 const PPCTargetLowering &TLI = *Subtarget.getTargetLowering();
615
616 const MCRegisterInfo *MRI = MF.getContext().getRegisterInfo();
617 DebugLoc dl;
618 // AIX assembler does not support cfi directives.
619 const bool needsCFI = MF.needsFrameMoves() && !Subtarget.isAIXABI();
620
621 const bool HasFastMFLR = Subtarget.hasFastMFLR();
622
623 // Get processor type.
624 bool isPPC64 = Subtarget.isPPC64();
625 // Get the ABI.
626 bool isSVR4ABI = Subtarget.isSVR4ABI();
627 bool isELFv2ABI = Subtarget.isELFv2ABI();
628 assert((isSVR4ABI || Subtarget.isAIXABI()) && "Unsupported PPC ABI.");
629
630 // Work out frame sizes.
631 uint64_t FrameSize = determineFrameLayoutAndUpdate(MF);
632 int64_t NegFrameSize = -FrameSize;
633 if (!isPPC64 && (!isInt<32>(x: FrameSize) || !isInt<32>(x: NegFrameSize)))
634 llvm_unreachable("Unhandled stack size!");
635
636 if (MFI.isFrameAddressTaken())
637 replaceFPWithRealFP(MF);
638
639 // Check if the link register (LR) must be saved.
640 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
641 bool MustSaveLR = FI->mustSaveLR();
642 bool MustSaveTOC = FI->mustSaveTOC();
643 const SmallVectorImpl<Register> &MustSaveCRs = FI->getMustSaveCRs();
644 bool MustSaveCR = !MustSaveCRs.empty();
645 // Do we have a frame pointer and/or base pointer for this function?
646 bool HasFP = hasFP(MF);
647 bool HasBP = RegInfo->hasBasePointer(MF);
648 bool HasRedZone = isPPC64 || !isSVR4ABI;
649 const bool HasROPProtect = Subtarget.hasROPProtect();
650 bool HasPrivileged = Subtarget.hasPrivileged();
651
652 Register SPReg = isPPC64 ? PPC::X1 : PPC::R1;
653 Register BPReg = RegInfo->getBaseRegister(MF);
654 Register FPReg = isPPC64 ? PPC::X31 : PPC::R31;
655 Register LRReg = isPPC64 ? PPC::LR8 : PPC::LR;
656 Register TOCReg = isPPC64 ? PPC::X2 : PPC::R2;
657 Register ScratchReg;
658 Register TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
659 // ...(R12/X12 is volatile in both Darwin & SVR4, & can't be a function arg.)
660 const MCInstrDesc& MFLRInst = TII.get(Opcode: isPPC64 ? PPC::MFLR8
661 : PPC::MFLR );
662 const MCInstrDesc& StoreInst = TII.get(Opcode: isPPC64 ? PPC::STD
663 : PPC::STW );
664 const MCInstrDesc& StoreUpdtInst = TII.get(Opcode: isPPC64 ? PPC::STDU
665 : PPC::STWU );
666 const MCInstrDesc& StoreUpdtIdxInst = TII.get(Opcode: isPPC64 ? PPC::STDUX
667 : PPC::STWUX);
668 const MCInstrDesc& OrInst = TII.get(Opcode: isPPC64 ? PPC::OR8
669 : PPC::OR );
670 const MCInstrDesc& SubtractCarryingInst = TII.get(Opcode: isPPC64 ? PPC::SUBFC8
671 : PPC::SUBFC);
672 const MCInstrDesc& SubtractImmCarryingInst = TII.get(Opcode: isPPC64 ? PPC::SUBFIC8
673 : PPC::SUBFIC);
674 const MCInstrDesc &MoveFromCondRegInst = TII.get(Opcode: isPPC64 ? PPC::MFCR8
675 : PPC::MFCR);
676 const MCInstrDesc &StoreWordInst = TII.get(Opcode: isPPC64 ? PPC::STW8 : PPC::STW);
677 const MCInstrDesc &HashST =
678 TII.get(Opcode: isPPC64 ? (HasPrivileged ? PPC::HASHSTP8 : PPC::HASHST8)
679 : (HasPrivileged ? PPC::HASHSTP : PPC::HASHST));
680
681 // Regarding this assert: Even though LR is saved in the caller's frame (i.e.,
682 // LROffset is positive), that slot is callee-owned. Because PPC32 SVR4 has no
683 // Red Zone, an asynchronous event (a form of "callee") could claim a frame &
684 // overwrite it, so PPC32 SVR4 must claim at least a minimal frame to save LR.
685 assert((isPPC64 || !isSVR4ABI || !(!FrameSize && (MustSaveLR || HasFP))) &&
686 "FrameSize must be >0 to save/restore the FP or LR for 32-bit SVR4.");
687
688 // Using the same bool variable as below to suppress compiler warnings.
689 bool SingleScratchReg = findScratchRegister(
690 MBB: &MBB, UseAtEnd: false, TwoUniqueRegsRequired: twoUniqueScratchRegsRequired(MBB: &MBB), SR1: &ScratchReg, SR2: &TempReg);
691 assert(SingleScratchReg &&
692 "Required number of registers not available in this block");
693
694 SingleScratchReg = ScratchReg == TempReg;
695
696 int64_t LROffset = getReturnSaveOffset();
697
698 int64_t FPOffset = 0;
699 if (HasFP) {
700 MachineFrameInfo &MFI = MF.getFrameInfo();
701 int FPIndex = FI->getFramePointerSaveIndex();
702 assert(FPIndex && "No Frame Pointer Save Slot!");
703 FPOffset = MFI.getObjectOffset(ObjectIdx: FPIndex);
704 }
705
706 int64_t BPOffset = 0;
707 if (HasBP) {
708 MachineFrameInfo &MFI = MF.getFrameInfo();
709 int BPIndex = FI->getBasePointerSaveIndex();
710 assert(BPIndex && "No Base Pointer Save Slot!");
711 BPOffset = MFI.getObjectOffset(ObjectIdx: BPIndex);
712 }
713
714 int64_t PBPOffset = 0;
715 if (FI->usesPICBase()) {
716 MachineFrameInfo &MFI = MF.getFrameInfo();
717 int PBPIndex = FI->getPICBasePointerSaveIndex();
718 assert(PBPIndex && "No PIC Base Pointer Save Slot!");
719 PBPOffset = MFI.getObjectOffset(ObjectIdx: PBPIndex);
720 }
721
722 // Get stack alignments.
723 Align MaxAlign = MFI.getMaxAlign();
724 if (HasBP && MaxAlign > 1)
725 assert(Log2(MaxAlign) < 16 && "Invalid alignment!");
726
727 // Frames of 32KB & larger require special handling because they cannot be
728 // indexed into with a simple STDU/STWU/STD/STW immediate offset operand.
729 bool isLargeFrame = !isInt<16>(x: NegFrameSize);
730
731 // Check if we can move the stack update instruction (stdu) down the prologue
732 // past the callee saves. Hopefully this will avoid the situation where the
733 // saves are waiting for the update on the store with update to complete.
734 MachineBasicBlock::iterator StackUpdateLoc = MBBI;
735 bool MovingStackUpdateDown = false;
736
737 // Check if we can move the stack update.
738 if (stackUpdateCanBeMoved(MF)) {
739 const std::vector<CalleeSavedInfo> &Info = MFI.getCalleeSavedInfo();
740 for (CalleeSavedInfo CSI : Info) {
741 // If the callee saved register is spilled to a register instead of the
742 // stack then the spill no longer uses the stack pointer.
743 // This can lead to two consequences:
744 // 1) We no longer need to update the stack because the function does not
745 // spill any callee saved registers to stack.
746 // 2) We have a situation where we still have to update the stack pointer
747 // even though some registers are spilled to other registers. In
748 // this case the current code moves the stack update to an incorrect
749 // position.
750 // In either case we should abort moving the stack update operation.
751 if (CSI.isSpilledToReg()) {
752 StackUpdateLoc = MBBI;
753 MovingStackUpdateDown = false;
754 break;
755 }
756
757 int FrIdx = CSI.getFrameIdx();
758 // If the frame index is not negative the callee saved info belongs to a
759 // stack object that is not a fixed stack object. We ignore non-fixed
760 // stack objects because we won't move the stack update pointer past them.
761 if (FrIdx >= 0)
762 continue;
763
764 if (MFI.isFixedObjectIndex(ObjectIdx: FrIdx) && MFI.getObjectOffset(ObjectIdx: FrIdx) < 0) {
765 StackUpdateLoc++;
766 MovingStackUpdateDown = true;
767 } else {
768 // We need all of the Frame Indices to meet these conditions.
769 // If they do not, abort the whole operation.
770 StackUpdateLoc = MBBI;
771 MovingStackUpdateDown = false;
772 break;
773 }
774 }
775
776 // If the operation was not aborted then update the object offset.
777 if (MovingStackUpdateDown) {
778 for (CalleeSavedInfo CSI : Info) {
779 int FrIdx = CSI.getFrameIdx();
780 if (FrIdx < 0)
781 MFI.setObjectOffset(ObjectIdx: FrIdx, SPOffset: MFI.getObjectOffset(ObjectIdx: FrIdx) + NegFrameSize);
782 }
783 }
784 }
785
786 // Where in the prologue we move the CR fields depends on how many scratch
787 // registers we have, and if we need to save the link register or not. This
788 // lambda is to avoid duplicating the logic in 2 places.
789 auto BuildMoveFromCR = [&]() {
790 if (isELFv2ABI && MustSaveCRs.size() == 1) {
791 // In the ELFv2 ABI, we are not required to save all CR fields.
792 // If only one CR field is clobbered, it is more efficient to use
793 // mfocrf to selectively save just that field, because mfocrf has short
794 // latency compares to mfcr.
795 assert(isPPC64 && "V2 ABI is 64-bit only.");
796 MachineInstrBuilder MIB =
797 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::MFOCRF8), DestReg: TempReg);
798 MIB.addReg(RegNo: MustSaveCRs[0], Flags: RegState::Kill);
799 } else {
800 MachineInstrBuilder MIB =
801 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: MoveFromCondRegInst, DestReg: TempReg);
802 for (unsigned CRfield : MustSaveCRs)
803 MIB.addReg(RegNo: CRfield, Flags: RegState::ImplicitKill);
804 }
805 };
806
807 // If we need to spill the CR and the LR but we don't have two separate
808 // registers available, we must spill them one at a time
809 if (MustSaveCR && SingleScratchReg && MustSaveLR) {
810 BuildMoveFromCR();
811 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: StoreWordInst)
812 .addReg(RegNo: TempReg, Flags: getKillRegState(B: true))
813 .addImm(Val: CRSaveOffset)
814 .addReg(RegNo: SPReg);
815 }
816
817 if (MustSaveLR)
818 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: MFLRInst, DestReg: ScratchReg);
819
820 if (MustSaveCR && !(SingleScratchReg && MustSaveLR))
821 BuildMoveFromCR();
822
823 if (HasRedZone) {
824 if (HasFP)
825 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: StoreInst)
826 .addReg(RegNo: FPReg)
827 .addImm(Val: FPOffset)
828 .addReg(RegNo: SPReg);
829 if (FI->usesPICBase())
830 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: StoreInst)
831 .addReg(RegNo: PPC::R30)
832 .addImm(Val: PBPOffset)
833 .addReg(RegNo: SPReg);
834 if (HasBP)
835 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: StoreInst)
836 .addReg(RegNo: BPReg)
837 .addImm(Val: BPOffset)
838 .addReg(RegNo: SPReg);
839 }
840
841 // Generate the instruction to store the LR. In the case where ROP protection
842 // is required the register holding the LR should not be killed as it will be
843 // used by the hash store instruction.
844 auto SaveLR = [&](int64_t Offset) {
845 assert(MustSaveLR && "LR is not required to be saved!");
846 BuildMI(BB&: MBB, I: StackUpdateLoc, MIMD: dl, MCID: StoreInst)
847 .addReg(RegNo: ScratchReg, Flags: getKillRegState(B: !HasROPProtect))
848 .addImm(Val: Offset)
849 .addReg(RegNo: SPReg);
850
851 // Add the ROP protection Hash Store instruction.
852 // NOTE: This is technically a violation of the ABI. The hash can be saved
853 // up to 512 bytes into the Protected Zone. This can be outside of the
854 // initial 288 byte volatile program storage region in the Protected Zone.
855 // However, this restriction will be removed in an upcoming revision of the
856 // ABI.
857 if (HasROPProtect) {
858 const int SaveIndex = FI->getROPProtectionHashSaveIndex();
859 const int64_t ImmOffset = MFI.getObjectOffset(ObjectIdx: SaveIndex);
860 assert((ImmOffset <= -8 && ImmOffset >= -512) &&
861 "ROP hash save offset out of range.");
862 assert(((ImmOffset & 0x7) == 0) &&
863 "ROP hash save offset must be 8 byte aligned.");
864 BuildMI(BB&: MBB, I: StackUpdateLoc, MIMD: dl, MCID: HashST)
865 .addReg(RegNo: ScratchReg, Flags: getKillRegState(B: true))
866 .addImm(Val: ImmOffset)
867 .addReg(RegNo: SPReg);
868 }
869 };
870
871 if (MustSaveLR && HasFastMFLR)
872 SaveLR(LROffset);
873
874 if (MustSaveCR &&
875 !(SingleScratchReg && MustSaveLR)) {
876 assert(HasRedZone && "A red zone is always available on PPC64");
877 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: StoreWordInst)
878 .addReg(RegNo: TempReg, Flags: getKillRegState(B: true))
879 .addImm(Val: CRSaveOffset)
880 .addReg(RegNo: SPReg);
881 }
882
883 // Skip the rest if this is a leaf function & all spills fit in the Red Zone.
884 if (!FrameSize) {
885 if (MustSaveLR && !HasFastMFLR)
886 SaveLR(LROffset);
887 return;
888 }
889
890 // Adjust stack pointer: r1 += NegFrameSize.
891 // If there is a preferred stack alignment, align R1 now
892
893 if (HasBP && HasRedZone) {
894 // Save a copy of r1 as the base pointer.
895 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: OrInst, DestReg: BPReg)
896 .addReg(RegNo: SPReg)
897 .addReg(RegNo: SPReg);
898 }
899
900 // Have we generated a STUX instruction to claim stack frame? If so,
901 // the negated frame size will be placed in ScratchReg.
902 bool HasSTUX =
903 (TLI.hasInlineStackProbe(MF) && FrameSize > TLI.getStackProbeSize(MF)) ||
904 (HasBP && MaxAlign > 1) || isLargeFrame;
905
906 // If we use STUX to update the stack pointer, we need the two scratch
907 // registers TempReg and ScratchReg, we have to save LR here which is stored
908 // in ScratchReg.
909 // If the offset can not be encoded into the store instruction, we also have
910 // to save LR here.
911 // If we are using ROP Protection we need to save the LR here as we cannot
912 // move the hashst instruction past the point where we get the stack frame.
913 if (MustSaveLR && !HasFastMFLR &&
914 (HasSTUX || !isInt<16>(x: FrameSize + LROffset) || HasROPProtect))
915 SaveLR(LROffset);
916
917 // If FrameSize <= TLI.getStackProbeSize(MF), as POWER ABI requires backchain
918 // pointer is always stored at SP, we will get a free probe due to an essential
919 // STU(X) instruction.
920 if (TLI.hasInlineStackProbe(MF) && FrameSize > TLI.getStackProbeSize(MF)) {
921 // To be consistent with other targets, a pseudo instruction is emitted and
922 // will be later expanded in `inlineStackProbe`.
923 BuildMI(BB&: MBB, I: MBBI, MIMD: dl,
924 MCID: TII.get(Opcode: isPPC64 ? PPC::PROBED_STACKALLOC_64
925 : PPC::PROBED_STACKALLOC_32))
926 .addDef(RegNo: TempReg)
927 .addDef(RegNo: ScratchReg) // ScratchReg stores the old sp.
928 .addImm(Val: NegFrameSize);
929 // FIXME: HasSTUX is only read if HasRedZone is not set, in such case, we
930 // update the ScratchReg to meet the assumption that ScratchReg contains
931 // the NegFrameSize. This solution is rather tricky.
932 if (!HasRedZone) {
933 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::SUBF), DestReg: ScratchReg)
934 .addReg(RegNo: ScratchReg)
935 .addReg(RegNo: SPReg);
936 }
937 } else {
938 // This condition must be kept in sync with canUseAsPrologue.
939 if (HasBP && MaxAlign > 1) {
940 if (isPPC64)
941 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::RLDICL), DestReg: ScratchReg)
942 .addReg(RegNo: SPReg)
943 .addImm(Val: 0)
944 .addImm(Val: 64 - Log2(A: MaxAlign));
945 else // PPC32...
946 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::RLWINM), DestReg: ScratchReg)
947 .addReg(RegNo: SPReg)
948 .addImm(Val: 0)
949 .addImm(Val: 32 - Log2(A: MaxAlign))
950 .addImm(Val: 31);
951 if (!isLargeFrame) {
952 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: SubtractImmCarryingInst, DestReg: ScratchReg)
953 .addReg(RegNo: ScratchReg, Flags: RegState::Kill)
954 .addImm(Val: NegFrameSize);
955 } else {
956 assert(!SingleScratchReg && "Only a single scratch reg available");
957 TII.materializeImmPostRA(MBB, MBBI, DL: dl, Reg: TempReg, Imm: NegFrameSize);
958 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: SubtractCarryingInst, DestReg: ScratchReg)
959 .addReg(RegNo: ScratchReg, Flags: RegState::Kill)
960 .addReg(RegNo: TempReg, Flags: RegState::Kill);
961 }
962
963 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: StoreUpdtIdxInst, DestReg: SPReg)
964 .addReg(RegNo: SPReg, Flags: RegState::Kill)
965 .addReg(RegNo: SPReg)
966 .addReg(RegNo: ScratchReg);
967 } else if (!isLargeFrame) {
968 BuildMI(BB&: MBB, I: StackUpdateLoc, MIMD: dl, MCID: StoreUpdtInst, DestReg: SPReg)
969 .addReg(RegNo: SPReg)
970 .addImm(Val: NegFrameSize)
971 .addReg(RegNo: SPReg);
972 } else {
973 TII.materializeImmPostRA(MBB, MBBI, DL: dl, Reg: ScratchReg, Imm: NegFrameSize);
974 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: StoreUpdtIdxInst, DestReg: SPReg)
975 .addReg(RegNo: SPReg, Flags: RegState::Kill)
976 .addReg(RegNo: SPReg)
977 .addReg(RegNo: ScratchReg);
978 }
979 }
980
981 // Save the TOC register after the stack pointer update if a prologue TOC
982 // save is required for the function.
983 if (MustSaveTOC) {
984 assert(isELFv2ABI && "TOC saves in the prologue only supported on ELFv2");
985 BuildMI(BB&: MBB, I: StackUpdateLoc, MIMD: dl, MCID: TII.get(Opcode: PPC::STD))
986 .addReg(RegNo: TOCReg, Flags: getKillRegState(B: true))
987 .addImm(Val: TOCSaveOffset)
988 .addReg(RegNo: SPReg);
989 }
990
991 if (!HasRedZone) {
992 assert(!isPPC64 && "A red zone is always available on PPC64");
993 if (HasSTUX) {
994 // The negated frame size is in ScratchReg, and the SPReg has been
995 // decremented by the frame size: SPReg = old SPReg + ScratchReg.
996 // Since FPOffset, PBPOffset, etc. are relative to the beginning of
997 // the stack frame (i.e. the old SP), ideally, we would put the old
998 // SP into a register and use it as the base for the stores. The
999 // problem is that the only available register may be ScratchReg,
1000 // which could be R0, and R0 cannot be used as a base address.
1001
1002 // First, set ScratchReg to the old SP. This may need to be modified
1003 // later.
1004 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::SUBF), DestReg: ScratchReg)
1005 .addReg(RegNo: ScratchReg, Flags: RegState::Kill)
1006 .addReg(RegNo: SPReg);
1007
1008 if (ScratchReg == PPC::R0) {
1009 // R0 cannot be used as a base register, but it can be used as an
1010 // index in a store-indexed.
1011 int LastOffset = 0;
1012 if (HasFP) {
1013 // R0 += (FPOffset-LastOffset).
1014 // Need addic, since addi treats R0 as 0.
1015 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::ADDIC), DestReg: ScratchReg)
1016 .addReg(RegNo: ScratchReg)
1017 .addImm(Val: FPOffset-LastOffset);
1018 LastOffset = FPOffset;
1019 // Store FP into *R0.
1020 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::STWX))
1021 .addReg(RegNo: FPReg, Flags: RegState::Kill) // Save FP.
1022 .addReg(RegNo: PPC::ZERO)
1023 .addReg(RegNo: ScratchReg); // This will be the index (R0 is ok here).
1024 }
1025 if (FI->usesPICBase()) {
1026 // R0 += (PBPOffset-LastOffset).
1027 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::ADDIC), DestReg: ScratchReg)
1028 .addReg(RegNo: ScratchReg)
1029 .addImm(Val: PBPOffset-LastOffset);
1030 LastOffset = PBPOffset;
1031 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::STWX))
1032 .addReg(RegNo: PPC::R30, Flags: RegState::Kill) // Save PIC base pointer.
1033 .addReg(RegNo: PPC::ZERO)
1034 .addReg(RegNo: ScratchReg); // This will be the index (R0 is ok here).
1035 }
1036 if (HasBP) {
1037 // R0 += (BPOffset-LastOffset).
1038 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::ADDIC), DestReg: ScratchReg)
1039 .addReg(RegNo: ScratchReg)
1040 .addImm(Val: BPOffset-LastOffset);
1041 LastOffset = BPOffset;
1042 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::STWX))
1043 .addReg(RegNo: BPReg, Flags: RegState::Kill) // Save BP.
1044 .addReg(RegNo: PPC::ZERO)
1045 .addReg(RegNo: ScratchReg); // This will be the index (R0 is ok here).
1046 // BP = R0-LastOffset
1047 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::ADDIC), DestReg: BPReg)
1048 .addReg(RegNo: ScratchReg, Flags: RegState::Kill)
1049 .addImm(Val: -LastOffset);
1050 }
1051 } else {
1052 // ScratchReg is not R0, so use it as the base register. It is
1053 // already set to the old SP, so we can use the offsets directly.
1054
1055 // Now that the stack frame has been allocated, save all the necessary
1056 // registers using ScratchReg as the base address.
1057 if (HasFP)
1058 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: StoreInst)
1059 .addReg(RegNo: FPReg)
1060 .addImm(Val: FPOffset)
1061 .addReg(RegNo: ScratchReg);
1062 if (FI->usesPICBase())
1063 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: StoreInst)
1064 .addReg(RegNo: PPC::R30)
1065 .addImm(Val: PBPOffset)
1066 .addReg(RegNo: ScratchReg);
1067 if (HasBP) {
1068 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: StoreInst)
1069 .addReg(RegNo: BPReg)
1070 .addImm(Val: BPOffset)
1071 .addReg(RegNo: ScratchReg);
1072 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: OrInst, DestReg: BPReg)
1073 .addReg(RegNo: ScratchReg, Flags: RegState::Kill)
1074 .addReg(RegNo: ScratchReg);
1075 }
1076 }
1077 } else {
1078 // The frame size is a known 16-bit constant (fitting in the immediate
1079 // field of STWU). To be here we have to be compiling for PPC32.
1080 // Since the SPReg has been decreased by FrameSize, add it back to each
1081 // offset.
1082 if (HasFP)
1083 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: StoreInst)
1084 .addReg(RegNo: FPReg)
1085 .addImm(Val: FrameSize + FPOffset)
1086 .addReg(RegNo: SPReg);
1087 if (FI->usesPICBase())
1088 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: StoreInst)
1089 .addReg(RegNo: PPC::R30)
1090 .addImm(Val: FrameSize + PBPOffset)
1091 .addReg(RegNo: SPReg);
1092 if (HasBP) {
1093 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: StoreInst)
1094 .addReg(RegNo: BPReg)
1095 .addImm(Val: FrameSize + BPOffset)
1096 .addReg(RegNo: SPReg);
1097 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::ADDI), DestReg: BPReg)
1098 .addReg(RegNo: SPReg)
1099 .addImm(Val: FrameSize);
1100 }
1101 }
1102 }
1103
1104 // Save the LR now.
1105 if (!HasSTUX && MustSaveLR && !HasFastMFLR &&
1106 isInt<16>(x: FrameSize + LROffset) && !HasROPProtect)
1107 SaveLR(LROffset + FrameSize);
1108
1109 // Add Call Frame Information for the instructions we generated above.
1110 if (needsCFI) {
1111 unsigned CFIIndex;
1112
1113 if (HasBP) {
1114 // Define CFA in terms of BP. Do this in preference to using FP/SP,
1115 // because if the stack needed aligning then CFA won't be at a fixed
1116 // offset from FP/SP.
1117 unsigned Reg = MRI->getDwarfRegNum(Reg: BPReg, isEH: true);
1118 CFIIndex = MF.addFrameInst(
1119 Inst: MCCFIInstruction::createDefCfaRegister(L: nullptr, Register: Reg));
1120 } else {
1121 // Adjust the definition of CFA to account for the change in SP.
1122 assert(NegFrameSize);
1123 CFIIndex = MF.addFrameInst(
1124 Inst: MCCFIInstruction::cfiDefCfaOffset(L: nullptr, Offset: -NegFrameSize));
1125 }
1126 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: TargetOpcode::CFI_INSTRUCTION))
1127 .addCFIIndex(CFIIndex);
1128
1129 if (HasFP) {
1130 // Describe where FP was saved, at a fixed offset from CFA.
1131 unsigned Reg = MRI->getDwarfRegNum(Reg: FPReg, isEH: true);
1132 CFIIndex = MF.addFrameInst(
1133 Inst: MCCFIInstruction::createOffset(L: nullptr, Register: Reg, Offset: FPOffset));
1134 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: TargetOpcode::CFI_INSTRUCTION))
1135 .addCFIIndex(CFIIndex);
1136 }
1137
1138 if (FI->usesPICBase()) {
1139 // Describe where FP was saved, at a fixed offset from CFA.
1140 unsigned Reg = MRI->getDwarfRegNum(Reg: PPC::R30, isEH: true);
1141 CFIIndex = MF.addFrameInst(
1142 Inst: MCCFIInstruction::createOffset(L: nullptr, Register: Reg, Offset: PBPOffset));
1143 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: TargetOpcode::CFI_INSTRUCTION))
1144 .addCFIIndex(CFIIndex);
1145 }
1146
1147 if (HasBP) {
1148 // Describe where BP was saved, at a fixed offset from CFA.
1149 unsigned Reg = MRI->getDwarfRegNum(Reg: BPReg, isEH: true);
1150 CFIIndex = MF.addFrameInst(
1151 Inst: MCCFIInstruction::createOffset(L: nullptr, Register: Reg, Offset: BPOffset));
1152 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: TargetOpcode::CFI_INSTRUCTION))
1153 .addCFIIndex(CFIIndex);
1154 }
1155
1156 if (MustSaveLR) {
1157 // Describe where LR was saved, at a fixed offset from CFA.
1158 unsigned Reg = MRI->getDwarfRegNum(Reg: LRReg, isEH: true);
1159 CFIIndex = MF.addFrameInst(
1160 Inst: MCCFIInstruction::createOffset(L: nullptr, Register: Reg, Offset: LROffset));
1161 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: TargetOpcode::CFI_INSTRUCTION))
1162 .addCFIIndex(CFIIndex);
1163 }
1164 }
1165
1166 // If there is a frame pointer, copy R1 into R31
1167 if (HasFP) {
1168 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: OrInst, DestReg: FPReg)
1169 .addReg(RegNo: SPReg)
1170 .addReg(RegNo: SPReg);
1171
1172 if (!HasBP && needsCFI) {
1173 // Change the definition of CFA from SP+offset to FP+offset, because SP
1174 // will change at every alloca.
1175 unsigned Reg = MRI->getDwarfRegNum(Reg: FPReg, isEH: true);
1176 unsigned CFIIndex = MF.addFrameInst(
1177 Inst: MCCFIInstruction::createDefCfaRegister(L: nullptr, Register: Reg));
1178
1179 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: TargetOpcode::CFI_INSTRUCTION))
1180 .addCFIIndex(CFIIndex);
1181 }
1182 }
1183
1184 if (needsCFI) {
1185 // Describe where callee saved registers were saved, at fixed offsets from
1186 // CFA.
1187 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
1188 for (const CalleeSavedInfo &I : CSI) {
1189 MCRegister Reg = I.getReg();
1190 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
1191
1192 // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just
1193 // subregisters of CR2. We just need to emit a move of CR2.
1194 if (PPC::CRBITRCRegClass.contains(Reg))
1195 continue;
1196
1197 if ((Reg == PPC::X2 || Reg == PPC::R2) && MustSaveTOC)
1198 continue;
1199
1200 // For 64-bit SVR4 when we have spilled CRs, the spill location
1201 // is SP+8, not a frame-relative slot.
1202 if (isSVR4ABI && isPPC64 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
1203 // In the ELFv1 ABI, only CR2 is noted in CFI and stands in for
1204 // the whole CR word. In the ELFv2 ABI, every CR that was
1205 // actually saved gets its own CFI record.
1206 Register CRReg = isELFv2ABI? Reg : PPC::CR2;
1207 unsigned CFIIndex = MF.addFrameInst(Inst: MCCFIInstruction::createOffset(
1208 L: nullptr, Register: MRI->getDwarfRegNum(Reg: CRReg, isEH: true), Offset: CRSaveOffset));
1209 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: TargetOpcode::CFI_INSTRUCTION))
1210 .addCFIIndex(CFIIndex);
1211 continue;
1212 }
1213
1214 if (I.isSpilledToReg()) {
1215 unsigned SpilledReg = I.getDstReg();
1216 unsigned CFIRegister = MF.addFrameInst(Inst: MCCFIInstruction::createRegister(
1217 L: nullptr, Register1: MRI->getDwarfRegNum(Reg, isEH: true),
1218 Register2: MRI->getDwarfRegNum(Reg: SpilledReg, isEH: true)));
1219 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: TargetOpcode::CFI_INSTRUCTION))
1220 .addCFIIndex(CFIIndex: CFIRegister);
1221 } else {
1222 int64_t Offset = MFI.getObjectOffset(ObjectIdx: I.getFrameIdx());
1223 // We have changed the object offset above but we do not want to change
1224 // the actual offsets in the CFI instruction so we have to undo the
1225 // offset change here.
1226 if (MovingStackUpdateDown)
1227 Offset -= NegFrameSize;
1228
1229 unsigned CFIIndex = MF.addFrameInst(Inst: MCCFIInstruction::createOffset(
1230 L: nullptr, Register: MRI->getDwarfRegNum(Reg, isEH: true), Offset));
1231 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: TargetOpcode::CFI_INSTRUCTION))
1232 .addCFIIndex(CFIIndex);
1233 }
1234 }
1235 }
1236}
1237
1238void PPCFrameLowering::inlineStackProbe(MachineFunction &MF,
1239 MachineBasicBlock &PrologMBB) const {
1240 bool isPPC64 = Subtarget.isPPC64();
1241 const PPCTargetLowering &TLI = *Subtarget.getTargetLowering();
1242 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
1243 MachineFrameInfo &MFI = MF.getFrameInfo();
1244 const MCRegisterInfo *MRI = MF.getContext().getRegisterInfo();
1245 // AIX assembler does not support cfi directives.
1246 const bool needsCFI = MF.needsFrameMoves() && !Subtarget.isAIXABI();
1247 auto StackAllocMIPos = llvm::find_if(Range&: PrologMBB, P: [](MachineInstr &MI) {
1248 int Opc = MI.getOpcode();
1249 return Opc == PPC::PROBED_STACKALLOC_64 || Opc == PPC::PROBED_STACKALLOC_32;
1250 });
1251 if (StackAllocMIPos == PrologMBB.end())
1252 return;
1253 const BasicBlock *ProbedBB = PrologMBB.getBasicBlock();
1254 MachineBasicBlock *CurrentMBB = &PrologMBB;
1255 DebugLoc DL = PrologMBB.findDebugLoc(MBBI: StackAllocMIPos);
1256 MachineInstr &MI = *StackAllocMIPos;
1257 int64_t NegFrameSize = MI.getOperand(i: 2).getImm();
1258 unsigned ProbeSize = TLI.getStackProbeSize(MF);
1259 int64_t NegProbeSize = -(int64_t)ProbeSize;
1260 assert(isInt<32>(NegProbeSize) && "Unhandled probe size");
1261 int64_t NumBlocks = NegFrameSize / NegProbeSize;
1262 int64_t NegResidualSize = NegFrameSize % NegProbeSize;
1263 Register SPReg = isPPC64 ? PPC::X1 : PPC::R1;
1264 Register ScratchReg = MI.getOperand(i: 0).getReg();
1265 Register FPReg = MI.getOperand(i: 1).getReg();
1266 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
1267 bool HasBP = RegInfo->hasBasePointer(MF);
1268 Register BPReg = RegInfo->getBaseRegister(MF);
1269 Align MaxAlign = MFI.getMaxAlign();
1270 bool HasRedZone = Subtarget.isPPC64() || !Subtarget.isSVR4ABI();
1271 const MCInstrDesc &CopyInst = TII.get(Opcode: isPPC64 ? PPC::OR8 : PPC::OR);
1272 // Subroutines to generate .cfi_* directives.
1273 auto buildDefCFAReg = [&](MachineBasicBlock &MBB,
1274 MachineBasicBlock::iterator MBBI, Register Reg) {
1275 unsigned RegNum = MRI->getDwarfRegNum(Reg, isEH: true);
1276 unsigned CFIIndex = MF.addFrameInst(
1277 Inst: MCCFIInstruction::createDefCfaRegister(L: nullptr, Register: RegNum));
1278 BuildMI(BB&: MBB, I: MBBI, MIMD: DL, MCID: TII.get(Opcode: TargetOpcode::CFI_INSTRUCTION))
1279 .addCFIIndex(CFIIndex);
1280 };
1281 auto buildDefCFA = [&](MachineBasicBlock &MBB,
1282 MachineBasicBlock::iterator MBBI, Register Reg,
1283 int Offset) {
1284 unsigned RegNum = MRI->getDwarfRegNum(Reg, isEH: true);
1285 unsigned CFIIndex = MBB.getParent()->addFrameInst(
1286 Inst: MCCFIInstruction::cfiDefCfa(L: nullptr, Register: RegNum, Offset));
1287 BuildMI(BB&: MBB, I: MBBI, MIMD: DL, MCID: TII.get(Opcode: TargetOpcode::CFI_INSTRUCTION))
1288 .addCFIIndex(CFIIndex);
1289 };
1290 // Subroutine to determine if we can use the Imm as part of d-form.
1291 auto CanUseDForm = [](int64_t Imm) { return isInt<16>(x: Imm) && Imm % 4 == 0; };
1292 // Subroutine to materialize the Imm into TempReg.
1293 auto MaterializeImm = [&](MachineBasicBlock &MBB,
1294 MachineBasicBlock::iterator MBBI, int64_t Imm,
1295 Register &TempReg) {
1296 assert(isInt<32>(Imm) && "Unhandled imm");
1297 if (isInt<16>(x: Imm))
1298 BuildMI(BB&: MBB, I: MBBI, MIMD: DL, MCID: TII.get(Opcode: isPPC64 ? PPC::LI8 : PPC::LI), DestReg: TempReg)
1299 .addImm(Val: Imm);
1300 else {
1301 BuildMI(BB&: MBB, I: MBBI, MIMD: DL, MCID: TII.get(Opcode: isPPC64 ? PPC::LIS8 : PPC::LIS), DestReg: TempReg)
1302 .addImm(Val: Imm >> 16);
1303 BuildMI(BB&: MBB, I: MBBI, MIMD: DL, MCID: TII.get(Opcode: isPPC64 ? PPC::ORI8 : PPC::ORI), DestReg: TempReg)
1304 .addReg(RegNo: TempReg)
1305 .addImm(Val: Imm & 0xFFFF);
1306 }
1307 };
1308 // Subroutine to store frame pointer and decrease stack pointer by probe size.
1309 auto allocateAndProbe = [&](MachineBasicBlock &MBB,
1310 MachineBasicBlock::iterator MBBI, int64_t NegSize,
1311 Register NegSizeReg, bool UseDForm,
1312 Register StoreReg) {
1313 if (UseDForm)
1314 BuildMI(BB&: MBB, I: MBBI, MIMD: DL, MCID: TII.get(Opcode: isPPC64 ? PPC::STDU : PPC::STWU), DestReg: SPReg)
1315 .addReg(RegNo: StoreReg)
1316 .addImm(Val: NegSize)
1317 .addReg(RegNo: SPReg);
1318 else
1319 BuildMI(BB&: MBB, I: MBBI, MIMD: DL, MCID: TII.get(Opcode: isPPC64 ? PPC::STDUX : PPC::STWUX), DestReg: SPReg)
1320 .addReg(RegNo: StoreReg)
1321 .addReg(RegNo: SPReg)
1322 .addReg(RegNo: NegSizeReg);
1323 };
1324 // Used to probe stack when realignment is required.
1325 // Note that, according to ABI's requirement, *sp must always equals the
1326 // value of back-chain pointer, only st(w|d)u(x) can be used to update sp.
1327 // Following is pseudo code:
1328 // final_sp = (sp & align) + negframesize;
1329 // neg_gap = final_sp - sp;
1330 // while (neg_gap < negprobesize) {
1331 // stdu fp, negprobesize(sp);
1332 // neg_gap -= negprobesize;
1333 // }
1334 // stdux fp, sp, neg_gap
1335 //
1336 // When HasBP & HasRedzone, back-chain pointer is already saved in BPReg
1337 // before probe code, we don't need to save it, so we get one additional reg
1338 // that can be used to materialize the probeside if needed to use xform.
1339 // Otherwise, we can NOT materialize probeside, so we can only use Dform for
1340 // now.
1341 //
1342 // The allocations are:
1343 // if (HasBP && HasRedzone) {
1344 // r0: materialize the probesize if needed so that we can use xform.
1345 // r12: `neg_gap`
1346 // } else {
1347 // r0: back-chain pointer
1348 // r12: `neg_gap`.
1349 // }
1350 auto probeRealignedStack = [&](MachineBasicBlock &MBB,
1351 MachineBasicBlock::iterator MBBI,
1352 Register ScratchReg, Register TempReg) {
1353 assert(HasBP && "The function is supposed to have base pointer when its "
1354 "stack is realigned.");
1355 assert(isPowerOf2_64(ProbeSize) && "Probe size should be power of 2");
1356
1357 // FIXME: We can eliminate this limitation if we get more infomation about
1358 // which part of redzone are already used. Used redzone can be treated
1359 // probed. But there might be `holes' in redzone probed, this could
1360 // complicate the implementation.
1361 assert(ProbeSize >= Subtarget.getRedZoneSize() &&
1362 "Probe size should be larger or equal to the size of red-zone so "
1363 "that red-zone is not clobbered by probing.");
1364
1365 Register &FinalStackPtr = TempReg;
1366 // FIXME: We only support NegProbeSize materializable by DForm currently.
1367 // When HasBP && HasRedzone, we can use xform if we have an additional idle
1368 // register.
1369 NegProbeSize = std::max(a: NegProbeSize, b: -((int64_t)1 << 15));
1370 assert(isInt<16>(NegProbeSize) &&
1371 "NegProbeSize should be materializable by DForm");
1372 Register CRReg = PPC::CR0;
1373 // Layout of output assembly kinda like:
1374 // bb.0:
1375 // ...
1376 // sub $scratchreg, $finalsp, r1
1377 // cmpdi $scratchreg, <negprobesize>
1378 // bge bb.2
1379 // bb.1:
1380 // stdu <backchain>, <negprobesize>(r1)
1381 // sub $scratchreg, $scratchreg, negprobesize
1382 // cmpdi $scratchreg, <negprobesize>
1383 // blt bb.1
1384 // bb.2:
1385 // stdux <backchain>, r1, $scratchreg
1386 MachineFunction::iterator MBBInsertPoint = std::next(x: MBB.getIterator());
1387 MachineBasicBlock *ProbeLoopBodyMBB = MF.CreateMachineBasicBlock(BB: ProbedBB);
1388 MF.insert(MBBI: MBBInsertPoint, MBB: ProbeLoopBodyMBB);
1389 MachineBasicBlock *ProbeExitMBB = MF.CreateMachineBasicBlock(BB: ProbedBB);
1390 MF.insert(MBBI: MBBInsertPoint, MBB: ProbeExitMBB);
1391 // bb.2
1392 {
1393 Register BackChainPointer = HasRedZone ? BPReg : TempReg;
1394 allocateAndProbe(*ProbeExitMBB, ProbeExitMBB->end(), 0, ScratchReg, false,
1395 BackChainPointer);
1396 if (HasRedZone)
1397 // PROBED_STACKALLOC_64 assumes Operand(1) stores the old sp, copy BPReg
1398 // to TempReg to satisfy it.
1399 BuildMI(BB&: *ProbeExitMBB, I: ProbeExitMBB->end(), MIMD: DL, MCID: CopyInst, DestReg: TempReg)
1400 .addReg(RegNo: BPReg)
1401 .addReg(RegNo: BPReg);
1402 ProbeExitMBB->splice(Where: ProbeExitMBB->end(), Other: &MBB, From: MBBI, To: MBB.end());
1403 ProbeExitMBB->transferSuccessorsAndUpdatePHIs(FromMBB: &MBB);
1404 }
1405 // bb.0
1406 {
1407 BuildMI(BB: &MBB, MIMD: DL, MCID: TII.get(Opcode: isPPC64 ? PPC::SUBF8 : PPC::SUBF), DestReg: ScratchReg)
1408 .addReg(RegNo: SPReg)
1409 .addReg(RegNo: FinalStackPtr);
1410 if (!HasRedZone)
1411 BuildMI(BB: &MBB, MIMD: DL, MCID: CopyInst, DestReg: TempReg).addReg(RegNo: SPReg).addReg(RegNo: SPReg);
1412 BuildMI(BB: &MBB, MIMD: DL, MCID: TII.get(Opcode: isPPC64 ? PPC::CMPDI : PPC::CMPWI), DestReg: CRReg)
1413 .addReg(RegNo: ScratchReg)
1414 .addImm(Val: NegProbeSize);
1415 BuildMI(BB: &MBB, MIMD: DL, MCID: TII.get(Opcode: PPC::BCC))
1416 .addImm(Val: PPC::PRED_GE)
1417 .addReg(RegNo: CRReg)
1418 .addMBB(MBB: ProbeExitMBB);
1419 MBB.addSuccessor(Succ: ProbeLoopBodyMBB);
1420 MBB.addSuccessor(Succ: ProbeExitMBB);
1421 }
1422 // bb.1
1423 {
1424 Register BackChainPointer = HasRedZone ? BPReg : TempReg;
1425 allocateAndProbe(*ProbeLoopBodyMBB, ProbeLoopBodyMBB->end(), NegProbeSize,
1426 0, true /*UseDForm*/, BackChainPointer);
1427 BuildMI(BB: ProbeLoopBodyMBB, MIMD: DL, MCID: TII.get(Opcode: isPPC64 ? PPC::ADDI8 : PPC::ADDI),
1428 DestReg: ScratchReg)
1429 .addReg(RegNo: ScratchReg)
1430 .addImm(Val: -NegProbeSize);
1431 BuildMI(BB: ProbeLoopBodyMBB, MIMD: DL, MCID: TII.get(Opcode: isPPC64 ? PPC::CMPDI : PPC::CMPWI),
1432 DestReg: CRReg)
1433 .addReg(RegNo: ScratchReg)
1434 .addImm(Val: NegProbeSize);
1435 BuildMI(BB: ProbeLoopBodyMBB, MIMD: DL, MCID: TII.get(Opcode: PPC::BCC))
1436 .addImm(Val: PPC::PRED_LT)
1437 .addReg(RegNo: CRReg)
1438 .addMBB(MBB: ProbeLoopBodyMBB);
1439 ProbeLoopBodyMBB->addSuccessor(Succ: ProbeExitMBB);
1440 ProbeLoopBodyMBB->addSuccessor(Succ: ProbeLoopBodyMBB);
1441 }
1442 // Update liveins.
1443 fullyRecomputeLiveIns(MBBs: {ProbeExitMBB, ProbeLoopBodyMBB});
1444 return ProbeExitMBB;
1445 };
1446 // For case HasBP && MaxAlign > 1, we have to realign the SP by performing
1447 // SP = SP - SP % MaxAlign, thus make the probe more like dynamic probe since
1448 // the offset subtracted from SP is determined by SP's runtime value.
1449 if (HasBP && MaxAlign > 1) {
1450 // Calculate final stack pointer.
1451 if (isPPC64)
1452 BuildMI(BB&: *CurrentMBB, I&: {MI}, MIMD: DL, MCID: TII.get(Opcode: PPC::RLDICL), DestReg: ScratchReg)
1453 .addReg(RegNo: SPReg)
1454 .addImm(Val: 0)
1455 .addImm(Val: 64 - Log2(A: MaxAlign));
1456 else
1457 BuildMI(BB&: *CurrentMBB, I&: {MI}, MIMD: DL, MCID: TII.get(Opcode: PPC::RLWINM), DestReg: ScratchReg)
1458 .addReg(RegNo: SPReg)
1459 .addImm(Val: 0)
1460 .addImm(Val: 32 - Log2(A: MaxAlign))
1461 .addImm(Val: 31);
1462 BuildMI(BB&: *CurrentMBB, I&: {MI}, MIMD: DL, MCID: TII.get(Opcode: isPPC64 ? PPC::SUBF8 : PPC::SUBF),
1463 DestReg: FPReg)
1464 .addReg(RegNo: ScratchReg)
1465 .addReg(RegNo: SPReg);
1466 MaterializeImm(*CurrentMBB, {MI}, NegFrameSize, ScratchReg);
1467 BuildMI(BB&: *CurrentMBB, I&: {MI}, MIMD: DL, MCID: TII.get(Opcode: isPPC64 ? PPC::ADD8 : PPC::ADD4),
1468 DestReg: FPReg)
1469 .addReg(RegNo: ScratchReg)
1470 .addReg(RegNo: FPReg);
1471 CurrentMBB = probeRealignedStack(*CurrentMBB, {MI}, ScratchReg, FPReg);
1472 if (needsCFI)
1473 buildDefCFAReg(*CurrentMBB, {MI}, FPReg);
1474 } else {
1475 // Initialize current frame pointer.
1476 BuildMI(BB&: *CurrentMBB, I&: {MI}, MIMD: DL, MCID: CopyInst, DestReg: FPReg).addReg(RegNo: SPReg).addReg(RegNo: SPReg);
1477 // Use FPReg to calculate CFA.
1478 if (needsCFI)
1479 buildDefCFA(*CurrentMBB, {MI}, FPReg, 0);
1480 // Probe residual part.
1481 if (NegResidualSize) {
1482 bool ResidualUseDForm = CanUseDForm(NegResidualSize);
1483 if (!ResidualUseDForm)
1484 MaterializeImm(*CurrentMBB, {MI}, NegResidualSize, ScratchReg);
1485 allocateAndProbe(*CurrentMBB, {MI}, NegResidualSize, ScratchReg,
1486 ResidualUseDForm, FPReg);
1487 }
1488 bool UseDForm = CanUseDForm(NegProbeSize);
1489 // If number of blocks is small, just probe them directly.
1490 if (NumBlocks < 3) {
1491 if (!UseDForm)
1492 MaterializeImm(*CurrentMBB, {MI}, NegProbeSize, ScratchReg);
1493 for (int i = 0; i < NumBlocks; ++i)
1494 allocateAndProbe(*CurrentMBB, {MI}, NegProbeSize, ScratchReg, UseDForm,
1495 FPReg);
1496 if (needsCFI) {
1497 // Restore using SPReg to calculate CFA.
1498 buildDefCFAReg(*CurrentMBB, {MI}, SPReg);
1499 }
1500 } else {
1501 // Since CTR is a volatile register and current shrinkwrap implementation
1502 // won't choose an MBB in a loop as the PrologMBB, it's safe to synthesize a
1503 // CTR loop to probe.
1504 // Calculate trip count and stores it in CTRReg.
1505 MaterializeImm(*CurrentMBB, {MI}, NumBlocks, ScratchReg);
1506 BuildMI(BB&: *CurrentMBB, I&: {MI}, MIMD: DL, MCID: TII.get(Opcode: isPPC64 ? PPC::MTCTR8 : PPC::MTCTR))
1507 .addReg(RegNo: ScratchReg, Flags: RegState::Kill);
1508 if (!UseDForm)
1509 MaterializeImm(*CurrentMBB, {MI}, NegProbeSize, ScratchReg);
1510 // Create MBBs of the loop.
1511 MachineFunction::iterator MBBInsertPoint =
1512 std::next(x: CurrentMBB->getIterator());
1513 MachineBasicBlock *LoopMBB = MF.CreateMachineBasicBlock(BB: ProbedBB);
1514 MF.insert(MBBI: MBBInsertPoint, MBB: LoopMBB);
1515 MachineBasicBlock *ExitMBB = MF.CreateMachineBasicBlock(BB: ProbedBB);
1516 MF.insert(MBBI: MBBInsertPoint, MBB: ExitMBB);
1517 // Synthesize the loop body.
1518 allocateAndProbe(*LoopMBB, LoopMBB->end(), NegProbeSize, ScratchReg,
1519 UseDForm, FPReg);
1520 BuildMI(BB: LoopMBB, MIMD: DL, MCID: TII.get(Opcode: isPPC64 ? PPC::BDNZ8 : PPC::BDNZ))
1521 .addMBB(MBB: LoopMBB);
1522 LoopMBB->addSuccessor(Succ: ExitMBB);
1523 LoopMBB->addSuccessor(Succ: LoopMBB);
1524 // Synthesize the exit MBB.
1525 ExitMBB->splice(Where: ExitMBB->end(), Other: CurrentMBB,
1526 From: std::next(x: MachineBasicBlock::iterator(MI)),
1527 To: CurrentMBB->end());
1528 ExitMBB->transferSuccessorsAndUpdatePHIs(FromMBB: CurrentMBB);
1529 CurrentMBB->addSuccessor(Succ: LoopMBB);
1530 if (needsCFI) {
1531 // Restore using SPReg to calculate CFA.
1532 buildDefCFAReg(*ExitMBB, ExitMBB->begin(), SPReg);
1533 }
1534 // Update liveins.
1535 fullyRecomputeLiveIns(MBBs: {ExitMBB, LoopMBB});
1536 }
1537 }
1538 ++NumPrologProbed;
1539 MI.eraseFromParent();
1540}
1541
1542void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
1543 MachineBasicBlock &MBB) const {
1544 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
1545 DebugLoc dl;
1546
1547 if (MBBI != MBB.end())
1548 dl = MBBI->getDebugLoc();
1549
1550 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
1551 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
1552
1553 // Get alignment info so we know how to restore the SP.
1554 const MachineFrameInfo &MFI = MF.getFrameInfo();
1555
1556 // Get the number of bytes allocated from the FrameInfo.
1557 int64_t FrameSize = MFI.getStackSize();
1558
1559 // Get processor type.
1560 bool isPPC64 = Subtarget.isPPC64();
1561
1562 // Check if the link register (LR) has been saved.
1563 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1564 bool MustSaveLR = FI->mustSaveLR();
1565 const SmallVectorImpl<Register> &MustSaveCRs = FI->getMustSaveCRs();
1566 bool MustSaveCR = !MustSaveCRs.empty();
1567 // Do we have a frame pointer and/or base pointer for this function?
1568 bool HasFP = hasFP(MF);
1569 bool HasBP = RegInfo->hasBasePointer(MF);
1570 bool HasRedZone = Subtarget.isPPC64() || !Subtarget.isSVR4ABI();
1571 bool HasROPProtect = Subtarget.hasROPProtect();
1572 bool HasPrivileged = Subtarget.hasPrivileged();
1573
1574 Register SPReg = isPPC64 ? PPC::X1 : PPC::R1;
1575 Register BPReg = RegInfo->getBaseRegister(MF);
1576 Register FPReg = isPPC64 ? PPC::X31 : PPC::R31;
1577 Register ScratchReg;
1578 Register TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
1579 const MCInstrDesc& MTLRInst = TII.get( Opcode: isPPC64 ? PPC::MTLR8
1580 : PPC::MTLR );
1581 const MCInstrDesc& LoadInst = TII.get( Opcode: isPPC64 ? PPC::LD
1582 : PPC::LWZ );
1583 const MCInstrDesc& LoadImmShiftedInst = TII.get( Opcode: isPPC64 ? PPC::LIS8
1584 : PPC::LIS );
1585 const MCInstrDesc& OrInst = TII.get(Opcode: isPPC64 ? PPC::OR8
1586 : PPC::OR );
1587 const MCInstrDesc& OrImmInst = TII.get( Opcode: isPPC64 ? PPC::ORI8
1588 : PPC::ORI );
1589 const MCInstrDesc& AddImmInst = TII.get( Opcode: isPPC64 ? PPC::ADDI8
1590 : PPC::ADDI );
1591 const MCInstrDesc& AddInst = TII.get( Opcode: isPPC64 ? PPC::ADD8
1592 : PPC::ADD4 );
1593 const MCInstrDesc& LoadWordInst = TII.get( Opcode: isPPC64 ? PPC::LWZ8
1594 : PPC::LWZ);
1595 const MCInstrDesc& MoveToCRInst = TII.get( Opcode: isPPC64 ? PPC::MTOCRF8
1596 : PPC::MTOCRF);
1597 const MCInstrDesc &HashChk =
1598 TII.get(Opcode: isPPC64 ? (HasPrivileged ? PPC::HASHCHKP8 : PPC::HASHCHK8)
1599 : (HasPrivileged ? PPC::HASHCHKP : PPC::HASHCHK));
1600 int64_t LROffset = getReturnSaveOffset();
1601
1602 int64_t FPOffset = 0;
1603
1604 // Using the same bool variable as below to suppress compiler warnings.
1605 bool SingleScratchReg = findScratchRegister(MBB: &MBB, UseAtEnd: true, TwoUniqueRegsRequired: false, SR1: &ScratchReg,
1606 SR2: &TempReg);
1607 assert(SingleScratchReg &&
1608 "Could not find an available scratch register");
1609
1610 SingleScratchReg = ScratchReg == TempReg;
1611
1612 if (HasFP) {
1613 int FPIndex = FI->getFramePointerSaveIndex();
1614 assert(FPIndex && "No Frame Pointer Save Slot!");
1615 FPOffset = MFI.getObjectOffset(ObjectIdx: FPIndex);
1616 }
1617
1618 int64_t BPOffset = 0;
1619 if (HasBP) {
1620 int BPIndex = FI->getBasePointerSaveIndex();
1621 assert(BPIndex && "No Base Pointer Save Slot!");
1622 BPOffset = MFI.getObjectOffset(ObjectIdx: BPIndex);
1623 }
1624
1625 int64_t PBPOffset = 0;
1626 if (FI->usesPICBase()) {
1627 int PBPIndex = FI->getPICBasePointerSaveIndex();
1628 assert(PBPIndex && "No PIC Base Pointer Save Slot!");
1629 PBPOffset = MFI.getObjectOffset(ObjectIdx: PBPIndex);
1630 }
1631
1632 bool IsReturnBlock = (MBBI != MBB.end() && MBBI->isReturn());
1633
1634 if (IsReturnBlock) {
1635 unsigned RetOpcode = MBBI->getOpcode();
1636 bool UsesTCRet = RetOpcode == PPC::TCRETURNri ||
1637 RetOpcode == PPC::TCRETURNdi ||
1638 RetOpcode == PPC::TCRETURNai ||
1639 RetOpcode == PPC::TCRETURNri8 ||
1640 RetOpcode == PPC::TCRETURNdi8 ||
1641 RetOpcode == PPC::TCRETURNai8;
1642
1643 if (UsesTCRet) {
1644 int MaxTCRetDelta = FI->getTailCallSPDelta();
1645 MachineOperand &StackAdjust = MBBI->getOperand(i: 1);
1646 assert(StackAdjust.isImm() && "Expecting immediate value.");
1647 // Adjust stack pointer.
1648 int StackAdj = StackAdjust.getImm();
1649 int Delta = StackAdj - MaxTCRetDelta;
1650 assert((Delta >= 0) && "Delta must be positive");
1651 if (MaxTCRetDelta>0)
1652 FrameSize += (StackAdj +Delta);
1653 else
1654 FrameSize += StackAdj;
1655 }
1656 }
1657
1658 // Frames of 32KB & larger require special handling because they cannot be
1659 // indexed into with a simple LD/LWZ immediate offset operand.
1660 bool isLargeFrame = !isInt<16>(x: FrameSize);
1661
1662 // On targets without red zone, the SP needs to be restored last, so that
1663 // all live contents of the stack frame are upwards of the SP. This means
1664 // that we cannot restore SP just now, since there may be more registers
1665 // to restore from the stack frame (e.g. R31). If the frame size is not
1666 // a simple immediate value, we will need a spare register to hold the
1667 // restored SP. If the frame size is known and small, we can simply adjust
1668 // the offsets of the registers to be restored, and still use SP to restore
1669 // them. In such case, the final update of SP will be to add the frame
1670 // size to it.
1671 // To simplify the code, set RBReg to the base register used to restore
1672 // values from the stack, and set SPAdd to the value that needs to be added
1673 // to the SP at the end. The default values are as if red zone was present.
1674 unsigned RBReg = SPReg;
1675 uint64_t SPAdd = 0;
1676
1677 // Check if we can move the stack update instruction up the epilogue
1678 // past the callee saves. This will allow the move to LR instruction
1679 // to be executed before the restores of the callee saves which means
1680 // that the callee saves can hide the latency from the MTLR instrcution.
1681 MachineBasicBlock::iterator StackUpdateLoc = MBBI;
1682 if (stackUpdateCanBeMoved(MF)) {
1683 const std::vector<CalleeSavedInfo> & Info = MFI.getCalleeSavedInfo();
1684 for (CalleeSavedInfo CSI : Info) {
1685 // If the callee saved register is spilled to another register abort the
1686 // stack update movement.
1687 if (CSI.isSpilledToReg()) {
1688 StackUpdateLoc = MBBI;
1689 break;
1690 }
1691 int FrIdx = CSI.getFrameIdx();
1692 // If the frame index is not negative the callee saved info belongs to a
1693 // stack object that is not a fixed stack object. We ignore non-fixed
1694 // stack objects because we won't move the update of the stack pointer
1695 // past them.
1696 if (FrIdx >= 0)
1697 continue;
1698
1699 if (MFI.isFixedObjectIndex(ObjectIdx: FrIdx) && MFI.getObjectOffset(ObjectIdx: FrIdx) < 0)
1700 StackUpdateLoc--;
1701 else {
1702 // Abort the operation as we can't update all CSR restores.
1703 StackUpdateLoc = MBBI;
1704 break;
1705 }
1706 }
1707 }
1708
1709 if (FrameSize) {
1710 // In the prologue, the loaded (or persistent) stack pointer value is
1711 // offset by the STDU/STDUX/STWU/STWUX instruction. For targets with red
1712 // zone add this offset back now.
1713
1714 // If the function has a base pointer, the stack pointer has been copied
1715 // to it so we can restore it by copying in the other direction.
1716 if (HasRedZone && HasBP) {
1717 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: OrInst, DestReg: RBReg).
1718 addReg(RegNo: BPReg).
1719 addReg(RegNo: BPReg);
1720 }
1721 // If this function contained a fastcc call and GuaranteedTailCallOpt is
1722 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
1723 // call which invalidates the stack pointer value in SP(0). So we use the
1724 // value of R31 in this case. Similar situation exists with setjmp.
1725 else if (FI->hasFastCall() || MF.exposesReturnsTwice()) {
1726 assert(HasFP && "Expecting a valid frame pointer.");
1727 if (!HasRedZone)
1728 RBReg = FPReg;
1729 if (!isLargeFrame) {
1730 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: AddImmInst, DestReg: RBReg)
1731 .addReg(RegNo: FPReg).addImm(Val: FrameSize);
1732 } else {
1733 TII.materializeImmPostRA(MBB, MBBI, DL: dl, Reg: ScratchReg, Imm: FrameSize);
1734 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: AddInst)
1735 .addReg(RegNo: RBReg)
1736 .addReg(RegNo: FPReg)
1737 .addReg(RegNo: ScratchReg);
1738 }
1739 } else if (!isLargeFrame && !HasBP && !MFI.hasVarSizedObjects()) {
1740 if (HasRedZone) {
1741 BuildMI(BB&: MBB, I: StackUpdateLoc, MIMD: dl, MCID: AddImmInst, DestReg: SPReg)
1742 .addReg(RegNo: SPReg)
1743 .addImm(Val: FrameSize);
1744 } else {
1745 // Make sure that adding FrameSize will not overflow the max offset
1746 // size.
1747 assert(FPOffset <= 0 && BPOffset <= 0 && PBPOffset <= 0 &&
1748 "Local offsets should be negative");
1749 SPAdd = FrameSize;
1750 FPOffset += FrameSize;
1751 BPOffset += FrameSize;
1752 PBPOffset += FrameSize;
1753 }
1754 } else {
1755 // We don't want to use ScratchReg as a base register, because it
1756 // could happen to be R0. Use FP instead, but make sure to preserve it.
1757 if (!HasRedZone) {
1758 // If FP is not saved, copy it to ScratchReg.
1759 if (!HasFP)
1760 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: OrInst, DestReg: ScratchReg)
1761 .addReg(RegNo: FPReg)
1762 .addReg(RegNo: FPReg);
1763 RBReg = FPReg;
1764 }
1765 BuildMI(BB&: MBB, I: StackUpdateLoc, MIMD: dl, MCID: LoadInst, DestReg: RBReg)
1766 .addImm(Val: 0)
1767 .addReg(RegNo: SPReg);
1768 }
1769 }
1770 assert(RBReg != ScratchReg && "Should have avoided ScratchReg");
1771
1772 // Lambda to build MTCRF/MTOCRF instruction for restoring CR fields
1773 auto BuildMoveToCR = [&](MachineBasicBlock::iterator InsertPt,
1774 Register SrcReg) {
1775 if (MustSaveCRs.size() == 1)
1776 // Use MTOCRF for single CR field
1777 BuildMI(BB&: MBB, I: InsertPt, MIMD: dl, MCID: MoveToCRInst, DestReg: MustSaveCRs[0])
1778 .addReg(RegNo: SrcReg, Flags: getKillRegState(B: true));
1779 else {
1780 // Build CR mask for MTCRF.
1781 unsigned CRMask = 0;
1782 for (unsigned CRField : MustSaveCRs) {
1783 CRMask |= 0x80 >> (CRField - PPC::CR0);
1784 }
1785 BuildMI(BB&: MBB, I: InsertPt, MIMD: dl, MCID: TII.get(Opcode: isPPC64 ? PPC::MTCRF8 : PPC::MTCRF))
1786 .addImm(Val: CRMask)
1787 .addReg(RegNo: SrcReg, Flags: getKillRegState(B: true));
1788 }
1789 };
1790
1791 // If there is no red zone, ScratchReg may be needed for holding a useful
1792 // value (although not the base register). Make sure it is not overwritten
1793 // too early.
1794
1795 // If we need to restore both the LR and the CR and we only have one
1796 // available scratch register, we must do them one at a time.
1797 if (MustSaveCR && SingleScratchReg && MustSaveLR) {
1798 // Here TempReg == ScratchReg, and in the absence of red zone ScratchReg
1799 // is live here.
1800 assert(HasRedZone && "Expecting red zone");
1801 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: LoadWordInst, DestReg: TempReg)
1802 .addImm(Val: CRSaveOffset)
1803 .addReg(RegNo: SPReg);
1804 BuildMoveToCR(MBBI, TempReg);
1805 }
1806
1807 // Delay restoring of the LR if ScratchReg is needed. This is ok, since
1808 // LR is stored in the caller's stack frame. ScratchReg will be needed
1809 // if RBReg is anything other than SP. We shouldn't use ScratchReg as
1810 // a base register anyway, because it may happen to be R0.
1811 bool LoadedLR = false;
1812 if (MustSaveLR && RBReg == SPReg && isInt<16>(x: LROffset+SPAdd)) {
1813 BuildMI(BB&: MBB, I: StackUpdateLoc, MIMD: dl, MCID: LoadInst, DestReg: ScratchReg)
1814 .addImm(Val: LROffset+SPAdd)
1815 .addReg(RegNo: RBReg);
1816 LoadedLR = true;
1817 }
1818
1819 if (MustSaveCR && !(SingleScratchReg && MustSaveLR)) {
1820 assert(RBReg == SPReg && "Should be using SP as a base register");
1821 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: LoadWordInst, DestReg: TempReg)
1822 .addImm(Val: CRSaveOffset)
1823 .addReg(RegNo: RBReg);
1824 }
1825
1826 if (HasFP) {
1827 // If there is red zone, restore FP directly, since SP has already been
1828 // restored. Otherwise, restore the value of FP into ScratchReg.
1829 if (HasRedZone || RBReg == SPReg)
1830 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: LoadInst, DestReg: FPReg)
1831 .addImm(Val: FPOffset)
1832 .addReg(RegNo: SPReg);
1833 else
1834 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: LoadInst, DestReg: ScratchReg)
1835 .addImm(Val: FPOffset)
1836 .addReg(RegNo: RBReg);
1837 }
1838
1839 if (FI->usesPICBase())
1840 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: LoadInst, DestReg: PPC::R30)
1841 .addImm(Val: PBPOffset)
1842 .addReg(RegNo: RBReg);
1843
1844 if (HasBP)
1845 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: LoadInst, DestReg: BPReg)
1846 .addImm(Val: BPOffset)
1847 .addReg(RegNo: RBReg);
1848
1849 // There is nothing more to be loaded from the stack, so now we can
1850 // restore SP: SP = RBReg + SPAdd.
1851 if (RBReg != SPReg || SPAdd != 0) {
1852 assert(!HasRedZone && "This should not happen with red zone");
1853 // If SPAdd is 0, generate a copy.
1854 if (SPAdd == 0)
1855 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: OrInst, DestReg: SPReg)
1856 .addReg(RegNo: RBReg)
1857 .addReg(RegNo: RBReg);
1858 else
1859 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: AddImmInst, DestReg: SPReg)
1860 .addReg(RegNo: RBReg)
1861 .addImm(Val: SPAdd);
1862
1863 assert(RBReg != ScratchReg && "Should be using FP or SP as base register");
1864 if (RBReg == FPReg)
1865 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: OrInst, DestReg: FPReg)
1866 .addReg(RegNo: ScratchReg)
1867 .addReg(RegNo: ScratchReg);
1868
1869 // Now load the LR from the caller's stack frame.
1870 if (MustSaveLR && !LoadedLR)
1871 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: LoadInst, DestReg: ScratchReg)
1872 .addImm(Val: LROffset)
1873 .addReg(RegNo: SPReg);
1874 }
1875
1876 if (MustSaveCR &&
1877 !(SingleScratchReg && MustSaveLR))
1878 BuildMoveToCR(MBBI, TempReg);
1879
1880 if (MustSaveLR) {
1881 // If ROP protection is required, an extra instruction is added to compute a
1882 // hash and then compare it to the hash stored in the prologue.
1883 if (HasROPProtect) {
1884 const int SaveIndex = FI->getROPProtectionHashSaveIndex();
1885 const int64_t ImmOffset = MFI.getObjectOffset(ObjectIdx: SaveIndex);
1886 assert((ImmOffset <= -8 && ImmOffset >= -512) &&
1887 "ROP hash check location offset out of range.");
1888 assert(((ImmOffset & 0x7) == 0) &&
1889 "ROP hash check location offset must be 8 byte aligned.");
1890 BuildMI(BB&: MBB, I: StackUpdateLoc, MIMD: dl, MCID: HashChk)
1891 .addReg(RegNo: ScratchReg)
1892 .addImm(Val: ImmOffset)
1893 .addReg(RegNo: SPReg);
1894 }
1895 BuildMI(BB&: MBB, I: StackUpdateLoc, MIMD: dl, MCID: MTLRInst).addReg(RegNo: ScratchReg);
1896 }
1897
1898 // Callee pop calling convention. Pop parameter/linkage area. Used for tail
1899 // call optimization
1900 if (IsReturnBlock) {
1901 unsigned RetOpcode = MBBI->getOpcode();
1902 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1903 (RetOpcode == PPC::BLR || RetOpcode == PPC::BLR8) &&
1904 MF.getFunction().getCallingConv() == CallingConv::Fast) {
1905 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1906 unsigned CallerAllocatedAmt = FI->getMinReservedArea();
1907
1908 if (CallerAllocatedAmt && isInt<16>(x: CallerAllocatedAmt)) {
1909 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: AddImmInst, DestReg: SPReg)
1910 .addReg(RegNo: SPReg).addImm(Val: CallerAllocatedAmt);
1911 } else {
1912 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: LoadImmShiftedInst, DestReg: ScratchReg)
1913 .addImm(Val: CallerAllocatedAmt >> 16);
1914 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: OrImmInst, DestReg: ScratchReg)
1915 .addReg(RegNo: ScratchReg, Flags: RegState::Kill)
1916 .addImm(Val: CallerAllocatedAmt & 0xFFFF);
1917 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: AddInst)
1918 .addReg(RegNo: SPReg)
1919 .addReg(RegNo: FPReg)
1920 .addReg(RegNo: ScratchReg);
1921 }
1922 } else {
1923 createTailCallBranchInstr(MBB);
1924 }
1925 }
1926}
1927
1928void PPCFrameLowering::createTailCallBranchInstr(MachineBasicBlock &MBB) const {
1929 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
1930
1931 // If we got this far a first terminator should exist.
1932 assert(MBBI != MBB.end() && "Failed to find the first terminator.");
1933
1934 DebugLoc dl = MBBI->getDebugLoc();
1935 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
1936
1937 // Create branch instruction for pseudo tail call return instruction.
1938 // The TCRETURNdi variants are direct calls. Valid targets for those are
1939 // MO_GlobalAddress operands as well as MO_ExternalSymbol with PC-Rel
1940 // since we can tail call external functions with PC-Rel (i.e. we don't need
1941 // to worry about different TOC pointers). Some of the external functions will
1942 // be MO_GlobalAddress while others like memcpy for example, are going to
1943 // be MO_ExternalSymbol.
1944 unsigned RetOpcode = MBBI->getOpcode();
1945 if (RetOpcode == PPC::TCRETURNdi) {
1946 MBBI = MBB.getLastNonDebugInstr();
1947 MachineOperand &JumpTarget = MBBI->getOperand(i: 0);
1948 if (JumpTarget.isGlobal())
1949 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::TAILB)).
1950 addGlobalAddress(GV: JumpTarget.getGlobal(), Offset: JumpTarget.getOffset());
1951 else if (JumpTarget.isSymbol())
1952 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::TAILB)).
1953 addExternalSymbol(FnName: JumpTarget.getSymbolName());
1954 else
1955 llvm_unreachable("Expecting Global or External Symbol");
1956 } else if (RetOpcode == PPC::TCRETURNri) {
1957 MBBI = MBB.getLastNonDebugInstr();
1958 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1959 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::TAILBCTR));
1960 } else if (RetOpcode == PPC::TCRETURNai) {
1961 MBBI = MBB.getLastNonDebugInstr();
1962 MachineOperand &JumpTarget = MBBI->getOperand(i: 0);
1963 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::TAILBA)).addImm(Val: JumpTarget.getImm());
1964 } else if (RetOpcode == PPC::TCRETURNdi8) {
1965 MBBI = MBB.getLastNonDebugInstr();
1966 MachineOperand &JumpTarget = MBBI->getOperand(i: 0);
1967 if (JumpTarget.isGlobal())
1968 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::TAILB8)).
1969 addGlobalAddress(GV: JumpTarget.getGlobal(), Offset: JumpTarget.getOffset());
1970 else if (JumpTarget.isSymbol())
1971 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::TAILB8)).
1972 addExternalSymbol(FnName: JumpTarget.getSymbolName());
1973 else
1974 llvm_unreachable("Expecting Global or External Symbol");
1975 } else if (RetOpcode == PPC::TCRETURNri8) {
1976 MBBI = MBB.getLastNonDebugInstr();
1977 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1978 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::TAILBCTR8));
1979 } else if (RetOpcode == PPC::TCRETURNai8) {
1980 MBBI = MBB.getLastNonDebugInstr();
1981 MachineOperand &JumpTarget = MBBI->getOperand(i: 0);
1982 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: PPC::TAILBA8)).addImm(Val: JumpTarget.getImm());
1983 }
1984}
1985
1986void PPCFrameLowering::determineCalleeSaves(MachineFunction &MF,
1987 BitVector &SavedRegs,
1988 RegScavenger *RS) const {
1989 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
1990 if (Subtarget.isAIXABI())
1991 updateCalleeSaves(MF, SavedRegs);
1992
1993 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
1994
1995 // Do not explicitly save the callee saved VSRp registers.
1996 // The individual VSR subregisters will be saved instead.
1997 SavedRegs.reset(Idx: PPC::VSRp26);
1998 SavedRegs.reset(Idx: PPC::VSRp27);
1999 SavedRegs.reset(Idx: PPC::VSRp28);
2000 SavedRegs.reset(Idx: PPC::VSRp29);
2001 SavedRegs.reset(Idx: PPC::VSRp30);
2002 SavedRegs.reset(Idx: PPC::VSRp31);
2003
2004 // Save and clear the LR state.
2005 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2006 MCRegister LR = RegInfo->getRARegister();
2007 FI->setMustSaveLR(MustSaveLR(MF, LR));
2008 SavedRegs.reset(Idx: LR);
2009
2010 // Save R31 if necessary
2011 int FPSI = FI->getFramePointerSaveIndex();
2012 const bool isPPC64 = Subtarget.isPPC64();
2013 MachineFrameInfo &MFI = MF.getFrameInfo();
2014
2015 // If the frame pointer save index hasn't been defined yet.
2016 if (!FPSI && needsFP(MF)) {
2017 // Find out what the fix offset of the frame pointer save area.
2018 int FPOffset = getFramePointerSaveOffset();
2019 // Allocate the frame index for frame pointer save area.
2020 FPSI = MFI.CreateFixedObject(Size: isPPC64? 8 : 4, SPOffset: FPOffset, IsImmutable: true);
2021 // Save the result.
2022 FI->setFramePointerSaveIndex(FPSI);
2023 }
2024
2025 int BPSI = FI->getBasePointerSaveIndex();
2026 if (!BPSI && RegInfo->hasBasePointer(MF)) {
2027 int BPOffset = getBasePointerSaveOffset();
2028 // Allocate the frame index for the base pointer save area.
2029 BPSI = MFI.CreateFixedObject(Size: isPPC64? 8 : 4, SPOffset: BPOffset, IsImmutable: true);
2030 // Save the result.
2031 FI->setBasePointerSaveIndex(BPSI);
2032 }
2033
2034 // Reserve stack space for the PIC Base register (R30).
2035 // Only used in SVR4 32-bit.
2036 if (FI->usesPICBase()) {
2037 int PBPSI = MFI.CreateFixedObject(Size: 4, SPOffset: -8, IsImmutable: true);
2038 FI->setPICBasePointerSaveIndex(PBPSI);
2039 }
2040
2041 // Make sure we don't explicitly spill r31, because, for example, we have
2042 // some inline asm which explicitly clobbers it, when we otherwise have a
2043 // frame pointer and are using r31's spill slot for the prologue/epilogue
2044 // code. Same goes for the base pointer and the PIC base register.
2045 if (needsFP(MF))
2046 SavedRegs.reset(Idx: isPPC64 ? PPC::X31 : PPC::R31);
2047 if (RegInfo->hasBasePointer(MF)) {
2048 SavedRegs.reset(Idx: RegInfo->getBaseRegister(MF));
2049 // On AIX, when BaseRegister(R30) is used, need to spill r31 too to match
2050 // AIX trackback table requirement.
2051 if (!needsFP(MF) && !SavedRegs.test(Idx: isPPC64 ? PPC::X31 : PPC::R31) &&
2052 Subtarget.isAIXABI()) {
2053 assert(
2054 (RegInfo->getBaseRegister(MF) == (isPPC64 ? PPC::X30 : PPC::R30)) &&
2055 "Invalid base register on AIX!");
2056 SavedRegs.set(isPPC64 ? PPC::X31 : PPC::R31);
2057 }
2058 }
2059 if (FI->usesPICBase())
2060 SavedRegs.reset(Idx: PPC::R30);
2061
2062 // Reserve stack space to move the linkage area to in case of a tail call.
2063 int TCSPDelta = 0;
2064 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
2065 (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
2066 MFI.CreateFixedObject(Size: -1 * TCSPDelta, SPOffset: TCSPDelta, IsImmutable: true);
2067 }
2068
2069 // Allocate the nonvolatile CR spill slot iff the function uses CR 2, 3, or 4.
2070 // For 64-bit SVR4, and all flavors of AIX we create a FixedStack
2071 // object at the offset of the CR-save slot in the linkage area. The actual
2072 // save and restore of the condition register will be created as part of the
2073 // prologue and epilogue insertion, but the FixedStack object is needed to
2074 // keep the CalleSavedInfo valid.
2075 if ((SavedRegs.test(Idx: PPC::CR2) || SavedRegs.test(Idx: PPC::CR3) ||
2076 SavedRegs.test(Idx: PPC::CR4))) {
2077 const uint64_t SpillSize = 4; // Condition register is always 4 bytes.
2078 const int64_t SpillOffset =
2079 Subtarget.isPPC64() ? 8 : Subtarget.isAIXABI() ? 4 : -4;
2080 int FrameIdx =
2081 MFI.CreateFixedObject(Size: SpillSize, SPOffset: SpillOffset,
2082 /* IsImmutable */ true, /* IsAliased */ isAliased: false);
2083 FI->setCRSpillFrameIndex(FrameIdx);
2084 }
2085}
2086
2087void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
2088 RegScavenger *RS) const {
2089 // Get callee saved register information.
2090 MachineFrameInfo &MFI = MF.getFrameInfo();
2091 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
2092
2093 // If the function is shrink-wrapped, and if the function has a tail call, the
2094 // tail call might not be in the new RestoreBlock, so real branch instruction
2095 // won't be generated by emitEpilogue(), because shrink-wrap has chosen new
2096 // RestoreBlock. So we handle this case here.
2097 if (!MFI.getSavePoints().empty() && MFI.hasTailCall()) {
2098 assert(MFI.getRestorePoints().size() < 2 &&
2099 "MFI can't contain multiple restore points!");
2100 for (MachineBasicBlock &MBB : MF) {
2101 if (MBB.isReturnBlock() && (!MFI.getRestorePoints().contains(Val: &MBB)))
2102 createTailCallBranchInstr(MBB);
2103 }
2104 }
2105
2106 // Early exit if no callee saved registers are modified!
2107 if (CSI.empty() && !needsFP(MF)) {
2108 addScavengingSpillSlot(MF, RS);
2109 return;
2110 }
2111
2112 unsigned MinGPR = PPC::R31;
2113 unsigned MinG8R = PPC::X31;
2114 unsigned MinFPR = PPC::F31;
2115 unsigned MinVR = Subtarget.hasSPE() ? PPC::S31 : PPC::V31;
2116
2117 bool HasGPSaveArea = false;
2118 bool HasG8SaveArea = false;
2119 bool HasFPSaveArea = false;
2120 bool HasVRSaveArea = false;
2121
2122 SmallVector<CalleeSavedInfo, 18> GPRegs;
2123 SmallVector<CalleeSavedInfo, 18> G8Regs;
2124 SmallVector<CalleeSavedInfo, 18> FPRegs;
2125 SmallVector<CalleeSavedInfo, 18> VRegs;
2126
2127 for (const CalleeSavedInfo &I : CSI) {
2128 MCRegister Reg = I.getReg();
2129 assert((!MF.getInfo<PPCFunctionInfo>()->mustSaveTOC() ||
2130 (Reg != PPC::X2 && Reg != PPC::R2)) &&
2131 "Not expecting to try to spill R2 in a function that must save TOC");
2132 if (PPC::GPRCRegClass.contains(Reg)) {
2133 HasGPSaveArea = true;
2134
2135 GPRegs.push_back(Elt: I);
2136
2137 if (Reg < MinGPR) {
2138 MinGPR = Reg;
2139 }
2140 } else if (PPC::G8RCRegClass.contains(Reg)) {
2141 HasG8SaveArea = true;
2142
2143 G8Regs.push_back(Elt: I);
2144
2145 if (Reg < MinG8R) {
2146 MinG8R = Reg;
2147 }
2148 } else if (PPC::F8RCRegClass.contains(Reg)) {
2149 HasFPSaveArea = true;
2150
2151 FPRegs.push_back(Elt: I);
2152
2153 if (Reg < MinFPR) {
2154 MinFPR = Reg;
2155 }
2156 } else if (PPC::CRBITRCRegClass.contains(Reg) ||
2157 PPC::CRRCRegClass.contains(Reg)) {
2158 ; // do nothing, as we already know whether CRs are spilled
2159 } else if (PPC::VRRCRegClass.contains(Reg) ||
2160 PPC::SPERCRegClass.contains(Reg)) {
2161 // Altivec and SPE are mutually exclusive, but have the same stack
2162 // alignment requirements, so overload the save area for both cases.
2163 HasVRSaveArea = true;
2164
2165 VRegs.push_back(Elt: I);
2166
2167 if (Reg < MinVR) {
2168 MinVR = Reg;
2169 }
2170 } else {
2171 llvm_unreachable("Unknown RegisterClass!");
2172 }
2173 }
2174
2175 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
2176 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
2177
2178 int64_t LowerBound = 0;
2179
2180 // Take into account stack space reserved for tail calls.
2181 int TCSPDelta = 0;
2182 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
2183 (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
2184 LowerBound = TCSPDelta;
2185 }
2186
2187 // The Floating-point register save area is right below the back chain word
2188 // of the previous stack frame.
2189 if (HasFPSaveArea) {
2190 for (const CalleeSavedInfo &FPReg : FPRegs) {
2191 int FI = FPReg.getFrameIdx();
2192
2193 MFI.setObjectOffset(ObjectIdx: FI, SPOffset: LowerBound + MFI.getObjectOffset(ObjectIdx: FI));
2194 }
2195
2196 LowerBound -= (31 - TRI->getEncodingValue(Reg: MinFPR) + 1) * 8;
2197 }
2198
2199 // Check whether the frame pointer register is allocated. If so, make sure it
2200 // is spilled to the correct offset.
2201 if (needsFP(MF)) {
2202 int FI = PFI->getFramePointerSaveIndex();
2203 assert(FI && "No Frame Pointer Save Slot!");
2204 MFI.setObjectOffset(ObjectIdx: FI, SPOffset: LowerBound + MFI.getObjectOffset(ObjectIdx: FI));
2205 // FP is R31/X31, so no need to update MinGPR/MinG8R.
2206 HasGPSaveArea = true;
2207 }
2208
2209 if (PFI->usesPICBase()) {
2210 int FI = PFI->getPICBasePointerSaveIndex();
2211 assert(FI && "No PIC Base Pointer Save Slot!");
2212 MFI.setObjectOffset(ObjectIdx: FI, SPOffset: LowerBound + MFI.getObjectOffset(ObjectIdx: FI));
2213
2214 MinGPR = std::min<unsigned>(a: MinGPR, b: PPC::R30);
2215 HasGPSaveArea = true;
2216 }
2217
2218 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
2219 if (RegInfo->hasBasePointer(MF)) {
2220 int FI = PFI->getBasePointerSaveIndex();
2221 assert(FI && "No Base Pointer Save Slot!");
2222 MFI.setObjectOffset(ObjectIdx: FI, SPOffset: LowerBound + MFI.getObjectOffset(ObjectIdx: FI));
2223
2224 Register BP = RegInfo->getBaseRegister(MF);
2225 if (PPC::G8RCRegClass.contains(Reg: BP)) {
2226 MinG8R = std::min<unsigned>(a: MinG8R, b: BP);
2227 HasG8SaveArea = true;
2228 } else if (PPC::GPRCRegClass.contains(Reg: BP)) {
2229 MinGPR = std::min<unsigned>(a: MinGPR, b: BP);
2230 HasGPSaveArea = true;
2231 }
2232 }
2233
2234 // General register save area starts right below the Floating-point
2235 // register save area.
2236 if (HasGPSaveArea || HasG8SaveArea) {
2237 // Move general register save area spill slots down, taking into account
2238 // the size of the Floating-point register save area.
2239 for (const CalleeSavedInfo &GPReg : GPRegs) {
2240 if (!GPReg.isSpilledToReg()) {
2241 int FI = GPReg.getFrameIdx();
2242 MFI.setObjectOffset(ObjectIdx: FI, SPOffset: LowerBound + MFI.getObjectOffset(ObjectIdx: FI));
2243 }
2244 }
2245
2246 // Move general register save area spill slots down, taking into account
2247 // the size of the Floating-point register save area.
2248 for (const CalleeSavedInfo &G8Reg : G8Regs) {
2249 if (!G8Reg.isSpilledToReg()) {
2250 int FI = G8Reg.getFrameIdx();
2251 MFI.setObjectOffset(ObjectIdx: FI, SPOffset: LowerBound + MFI.getObjectOffset(ObjectIdx: FI));
2252 }
2253 }
2254
2255 unsigned MinReg =
2256 std::min<unsigned>(a: TRI->getEncodingValue(Reg: MinGPR),
2257 b: TRI->getEncodingValue(Reg: MinG8R));
2258
2259 const unsigned GPRegSize = Subtarget.isPPC64() ? 8 : 4;
2260 LowerBound -= (31 - MinReg + 1) * GPRegSize;
2261 }
2262
2263 // For 32-bit only, the CR save area is below the general register
2264 // save area. For 64-bit SVR4, the CR save area is addressed relative
2265 // to the stack pointer and hence does not need an adjustment here.
2266 // Only CR2 (the first nonvolatile spilled) has an associated frame
2267 // index so that we have a single uniform save area.
2268 if (spillsCR(MF) && Subtarget.is32BitELFABI()) {
2269 // Adjust the frame index of the CR spill slot.
2270 for (const auto &CSInfo : CSI) {
2271 if (CSInfo.getReg() == PPC::CR2) {
2272 int FI = CSInfo.getFrameIdx();
2273 MFI.setObjectOffset(ObjectIdx: FI, SPOffset: LowerBound + MFI.getObjectOffset(ObjectIdx: FI));
2274 break;
2275 }
2276 }
2277
2278 LowerBound -= 4; // The CR save area is always 4 bytes long.
2279 }
2280
2281 // Both Altivec and SPE have the same alignment and padding requirements
2282 // within the stack frame.
2283 if (HasVRSaveArea) {
2284 // Insert alignment padding, we need 16-byte alignment. Note: for positive
2285 // number the alignment formula is : y = (x + (n-1)) & (~(n-1)). But since
2286 // we are using negative number here (the stack grows downward). We should
2287 // use formula : y = x & (~(n-1)). Where x is the size before aligning, n
2288 // is the alignment size ( n = 16 here) and y is the size after aligning.
2289 assert(LowerBound <= 0 && "Expect LowerBound have a non-positive value!");
2290 LowerBound &= ~(15);
2291
2292 for (const CalleeSavedInfo &VReg : VRegs) {
2293 int FI = VReg.getFrameIdx();
2294
2295 MFI.setObjectOffset(ObjectIdx: FI, SPOffset: LowerBound + MFI.getObjectOffset(ObjectIdx: FI));
2296 }
2297 }
2298
2299 addScavengingSpillSlot(MF, RS);
2300}
2301
2302void
2303PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF,
2304 RegScavenger *RS) const {
2305 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
2306 // a large stack, which will require scavenging a register to materialize a
2307 // large offset.
2308
2309 // We need to have a scavenger spill slot for spills if the frame size is
2310 // large. In case there is no free register for large-offset addressing,
2311 // this slot is used for the necessary emergency spill. Also, we need the
2312 // slot for dynamic stack allocations.
2313
2314 // The scavenger might be invoked if the frame offset does not fit into
2315 // the 16-bit immediate in case of not SPE and 8-bit in case of SPE.
2316 // We don't know the complete frame size here because we've not yet computed
2317 // callee-saved register spills or the needed alignment padding.
2318 unsigned StackSize = determineFrameLayout(MF, UseEstimate: true);
2319 MachineFrameInfo &MFI = MF.getFrameInfo();
2320 bool NeedSpills = Subtarget.hasSPE() ? !isInt<8>(x: StackSize) : !isInt<16>(x: StackSize);
2321
2322 if (MFI.hasVarSizedObjects() || spillsCR(MF) || hasNonRISpills(MF) ||
2323 (hasSpills(MF) && NeedSpills)) {
2324 const TargetRegisterClass &GPRC = PPC::GPRCRegClass;
2325 const TargetRegisterClass &G8RC = PPC::G8RCRegClass;
2326 const TargetRegisterClass &RC = Subtarget.isPPC64() ? G8RC : GPRC;
2327 const TargetRegisterInfo &TRI = *Subtarget.getRegisterInfo();
2328 unsigned Size = TRI.getSpillSize(RC);
2329 Align Alignment = TRI.getSpillAlign(RC);
2330 RS->addScavengingFrameIndex(FI: MFI.CreateSpillStackObject(Size, Alignment));
2331
2332 // Might we have over-aligned allocas?
2333 bool HasAlVars =
2334 MFI.hasVarSizedObjects() && MFI.getMaxAlign() > getStackAlign();
2335
2336 // These kinds of spills might need two registers.
2337 if (spillsCR(MF) || HasAlVars)
2338 RS->addScavengingFrameIndex(FI: MFI.CreateSpillStackObject(Size, Alignment));
2339 }
2340}
2341
2342// This function checks if a callee saved gpr can be spilled to a volatile
2343// vector register. This occurs for leaf functions when the option
2344// ppc-enable-pe-vector-spills is enabled. If there are any remaining registers
2345// which were not spilled to vectors, return false so the target independent
2346// code can handle them by assigning a FrameIdx to a stack slot.
2347bool PPCFrameLowering::assignCalleeSavedSpillSlots(
2348 MachineFunction &MF, const TargetRegisterInfo *TRI,
2349 std::vector<CalleeSavedInfo> &CSI) const {
2350
2351 if (CSI.empty())
2352 return true; // Early exit if no callee saved registers are modified!
2353
2354 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
2355 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(MF: &MF);
2356 const MachineRegisterInfo &MRI = MF.getRegInfo();
2357
2358 if (Subtarget.hasSPE()) {
2359 // In case of SPE we only have SuperRegs and CRs
2360 // in our CalleSaveInfo vector.
2361
2362 for (auto &CalleeSaveReg : CSI) {
2363 MCRegister Reg = CalleeSaveReg.getReg();
2364 MCRegister Lower = RegInfo->getSubReg(Reg, Idx: PPC::sub_32);
2365 MCRegister Higher = RegInfo->getSubReg(Reg, Idx: PPC::sub_32_hi_phony);
2366
2367 if ( // Check only for SuperRegs.
2368 Lower &&
2369 // Replace Reg if only lower-32 bits modified
2370 !MRI.isPhysRegModified(PhysReg: Higher))
2371 CalleeSaveReg = CalleeSavedInfo(Lower);
2372 }
2373 }
2374
2375 // Early exit if cannot spill gprs to volatile vector registers.
2376 MachineFrameInfo &MFI = MF.getFrameInfo();
2377 if (!EnablePEVectorSpills || MFI.hasCalls() || !Subtarget.hasP9Vector())
2378 return false;
2379
2380 // Build a BitVector of VSRs that can be used for spilling GPRs.
2381 BitVector BVAllocatable = TRI->getAllocatableSet(MF);
2382 BitVector BVCalleeSaved(TRI->getNumRegs());
2383 for (unsigned i = 0; CSRegs[i]; ++i)
2384 BVCalleeSaved.set(CSRegs[i]);
2385
2386 for (unsigned Reg : BVAllocatable.set_bits()) {
2387 // Set to 0 if the register is not a volatile VSX register, or if it is
2388 // used in the function.
2389 if (BVCalleeSaved[Reg] || !PPC::VSRCRegClass.contains(Reg) ||
2390 MRI.isPhysRegUsed(PhysReg: Reg))
2391 BVAllocatable.reset(Idx: Reg);
2392 }
2393
2394 bool AllSpilledToReg = true;
2395 unsigned LastVSRUsedForSpill = 0;
2396 for (auto &CS : CSI) {
2397 if (BVAllocatable.none())
2398 return false;
2399
2400 MCRegister Reg = CS.getReg();
2401
2402 if (!PPC::G8RCRegClass.contains(Reg)) {
2403 AllSpilledToReg = false;
2404 continue;
2405 }
2406
2407 // For P9, we can reuse LastVSRUsedForSpill to spill two GPRs
2408 // into one VSR using the mtvsrdd instruction.
2409 if (LastVSRUsedForSpill != 0) {
2410 CS.setDstReg(LastVSRUsedForSpill);
2411 BVAllocatable.reset(Idx: LastVSRUsedForSpill);
2412 LastVSRUsedForSpill = 0;
2413 continue;
2414 }
2415
2416 unsigned VolatileVFReg = BVAllocatable.find_first();
2417 if (VolatileVFReg < BVAllocatable.size()) {
2418 CS.setDstReg(VolatileVFReg);
2419 LastVSRUsedForSpill = VolatileVFReg;
2420 } else {
2421 AllSpilledToReg = false;
2422 }
2423 }
2424 return AllSpilledToReg;
2425}
2426
2427bool PPCFrameLowering::spillCalleeSavedRegisters(
2428 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
2429 ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
2430
2431 MachineFunction *MF = MBB.getParent();
2432 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
2433 PPCFunctionInfo *FI = MF->getInfo<PPCFunctionInfo>();
2434 bool MustSaveTOC = FI->mustSaveTOC();
2435 DebugLoc DL;
2436 bool CRSpilled = false;
2437 MachineInstrBuilder CRMIB;
2438 BitVector Spilled(TRI->getNumRegs());
2439
2440 VSRContainingGPRs.clear();
2441
2442 // Map each VSR to GPRs to be spilled with into it. Single VSR can contain one
2443 // or two GPRs, so we need table to record information for later save/restore.
2444 for (const CalleeSavedInfo &Info : CSI) {
2445 if (Info.isSpilledToReg()) {
2446 auto &SpilledVSR = VSRContainingGPRs[Info.getDstReg()];
2447 assert(SpilledVSR.second == 0 &&
2448 "Can't spill more than two GPRs into VSR!");
2449 if (SpilledVSR.first == 0)
2450 SpilledVSR.first = Info.getReg();
2451 else
2452 SpilledVSR.second = Info.getReg();
2453 }
2454 }
2455
2456 for (const CalleeSavedInfo &I : CSI) {
2457 MCRegister Reg = I.getReg();
2458
2459 // CR2 through CR4 are the nonvolatile CR fields.
2460 bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4;
2461
2462 // Add the callee-saved register as live-in; it's killed at the spill.
2463 // Do not do this for callee-saved registers that are live-in to the
2464 // function because they will already be marked live-in and this will be
2465 // adding it for a second time. It is an error to add the same register
2466 // to the set more than once.
2467 const MachineRegisterInfo &MRI = MF->getRegInfo();
2468 bool IsLiveIn = MRI.isLiveIn(Reg);
2469 if (!IsLiveIn)
2470 MBB.addLiveIn(PhysReg: Reg);
2471
2472 if (CRSpilled && IsCRField) {
2473 CRMIB.addReg(RegNo: Reg, Flags: RegState::ImplicitKill);
2474 continue;
2475 }
2476
2477 // The actual spill will happen in the prologue.
2478 if ((Reg == PPC::X2 || Reg == PPC::R2) && MustSaveTOC)
2479 continue;
2480
2481 // Insert the spill to the stack frame.
2482 if (IsCRField) {
2483 PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
2484 if (!Subtarget.is32BitELFABI()) {
2485 // The actual spill will happen at the start of the prologue.
2486 FuncInfo->addMustSaveCR(Reg);
2487 } else {
2488 CRSpilled = true;
2489 FuncInfo->setSpillsCR();
2490
2491 // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have
2492 // the same frame index in PPCRegisterInfo::hasReservedSpillSlot.
2493 CRMIB = BuildMI(MF&: *MF, MIMD: DL, MCID: TII.get(Opcode: PPC::MFCR), DestReg: PPC::R12)
2494 .addReg(RegNo: Reg, Flags: RegState::ImplicitKill);
2495
2496 MBB.insert(I: MI, MI: CRMIB);
2497 MBB.insert(I: MI, MI: addFrameReference(MIB: BuildMI(MF&: *MF, MIMD: DL, MCID: TII.get(Opcode: PPC::STW))
2498 .addReg(RegNo: PPC::R12,
2499 Flags: getKillRegState(B: true)),
2500 FI: I.getFrameIdx()));
2501 }
2502 } else {
2503 if (I.isSpilledToReg()) {
2504 unsigned Dst = I.getDstReg();
2505
2506 if (Spilled[Dst])
2507 continue;
2508
2509 const auto &VSR = VSRContainingGPRs[Dst];
2510 if (VSR.second != 0) {
2511 assert(Subtarget.hasP9Vector() &&
2512 "mtvsrdd is unavailable on pre-P9 targets.");
2513
2514 NumPESpillVSR += 2;
2515 BuildMI(BB&: MBB, I: MI, MIMD: DL, MCID: TII.get(Opcode: PPC::MTVSRDD), DestReg: Dst)
2516 .addReg(RegNo: VSR.first, Flags: getKillRegState(B: true))
2517 .addReg(RegNo: VSR.second, Flags: getKillRegState(B: true));
2518 } else if (VSR.second == 0) {
2519 assert(Subtarget.hasP8Vector() &&
2520 "Can't move GPR to VSR on pre-P8 targets.");
2521
2522 ++NumPESpillVSR;
2523 BuildMI(BB&: MBB, I: MI, MIMD: DL, MCID: TII.get(Opcode: PPC::MTVSRD),
2524 DestReg: TRI->getSubReg(Reg: Dst, Idx: PPC::sub_64))
2525 .addReg(RegNo: VSR.first, Flags: getKillRegState(B: true));
2526 } else {
2527 llvm_unreachable("More than two GPRs spilled to a VSR!");
2528 }
2529 Spilled.set(Dst);
2530 } else {
2531 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
2532 // Use !IsLiveIn for the kill flag.
2533 // We do not want to kill registers that are live in this function
2534 // before their use because they will become undefined registers.
2535 // Functions without NoUnwind need to preserve the order of elements in
2536 // saved vector registers.
2537 if (Subtarget.needsSwapsForVSXMemOps() &&
2538 !MF->getFunction().hasFnAttribute(Kind: Attribute::NoUnwind))
2539 TII.storeRegToStackSlotNoUpd(MBB, MBBI: MI, SrcReg: Reg, isKill: !IsLiveIn, FrameIndex: I.getFrameIdx(),
2540 RC);
2541 else
2542 TII.storeRegToStackSlot(MBB, MBBI: MI, SrcReg: Reg, isKill: !IsLiveIn, FrameIndex: I.getFrameIdx(), RC,
2543 VReg: Register());
2544 }
2545 }
2546 }
2547 return true;
2548}
2549
2550static void restoreCRs(bool is31, bool CR2Spilled, bool CR3Spilled,
2551 bool CR4Spilled, MachineBasicBlock &MBB,
2552 MachineBasicBlock::iterator MI,
2553 ArrayRef<CalleeSavedInfo> CSI, unsigned CSIIndex) {
2554
2555 MachineFunction *MF = MBB.getParent();
2556 const PPCInstrInfo &TII = *MF->getSubtarget<PPCSubtarget>().getInstrInfo();
2557 DebugLoc DL;
2558 unsigned MoveReg = PPC::R12;
2559
2560 // 32-bit: FP-relative
2561 MBB.insert(I: MI,
2562 MI: addFrameReference(MIB: BuildMI(MF&: *MF, MIMD: DL, MCID: TII.get(Opcode: PPC::LWZ), DestReg: MoveReg),
2563 FI: CSI[CSIIndex].getFrameIdx()));
2564 // Count how many CR fields need restoring
2565 unsigned NumCRs =
2566 (CR2Spilled ? 1 : 0) + (CR3Spilled ? 1 : 0) + (CR4Spilled ? 1 : 0);
2567
2568 assert(NumCRs >= 1 &&
2569 "Requires at least one non-volatile CR field to be restored.");
2570
2571 if (NumCRs == 1) {
2572 // Use MTOCRF for single CR field
2573 unsigned CRReg = CR2Spilled ? PPC::CR2 : (CR3Spilled ? PPC::CR3 : PPC::CR4);
2574 MBB.insert(I: MI, MI: BuildMI(MF&: *MF, MIMD: DL, MCID: TII.get(Opcode: PPC::MTOCRF), DestReg: CRReg)
2575 .addReg(RegNo: MoveReg, Flags: getKillRegState(B: true)));
2576 } else {
2577 // Use MTCRF for multiple CR fields.
2578 unsigned CRMask = 0;
2579 if (CR2Spilled)
2580 CRMask |= 0x20;
2581 if (CR3Spilled)
2582 CRMask |= 0x10;
2583 if (CR4Spilled)
2584 CRMask |= 0x08;
2585 MBB.insert(I: MI, MI: BuildMI(MF&: *MF, MIMD: DL, MCID: TII.get(Opcode: PPC::MTCRF))
2586 .addImm(Val: CRMask)
2587 .addReg(RegNo: MoveReg, Flags: getKillRegState(B: true)));
2588 }
2589}
2590
2591MachineBasicBlock::iterator PPCFrameLowering::
2592eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
2593 MachineBasicBlock::iterator I) const {
2594 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
2595 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
2596 I->getOpcode() == PPC::ADJCALLSTACKUP) {
2597 // Add (actually subtract) back the amount the callee popped on return.
2598 if (int CalleeAmt = I->getOperand(i: 1).getImm()) {
2599 bool is64Bit = Subtarget.isPPC64();
2600 CalleeAmt *= -1;
2601 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
2602 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
2603 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
2604 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
2605 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
2606 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
2607 const DebugLoc &dl = I->getDebugLoc();
2608
2609 if (isInt<16>(x: CalleeAmt)) {
2610 BuildMI(BB&: MBB, I, MIMD: dl, MCID: TII.get(Opcode: ADDIInstr), DestReg: StackReg)
2611 .addReg(RegNo: StackReg, Flags: RegState::Kill)
2612 .addImm(Val: CalleeAmt);
2613 } else {
2614 MachineBasicBlock::iterator MBBI = I;
2615 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: LISInstr), DestReg: TmpReg)
2616 .addImm(Val: CalleeAmt >> 16);
2617 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: ORIInstr), DestReg: TmpReg)
2618 .addReg(RegNo: TmpReg, Flags: RegState::Kill)
2619 .addImm(Val: CalleeAmt & 0xFFFF);
2620 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: ADDInstr), DestReg: StackReg)
2621 .addReg(RegNo: StackReg, Flags: RegState::Kill)
2622 .addReg(RegNo: TmpReg);
2623 }
2624 }
2625 }
2626 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
2627 return MBB.erase(I);
2628}
2629
2630static bool isCalleeSavedCR(unsigned Reg) {
2631 return PPC::CR2 == Reg || Reg == PPC::CR3 || Reg == PPC::CR4;
2632}
2633
2634bool PPCFrameLowering::restoreCalleeSavedRegisters(
2635 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
2636 MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
2637 MachineFunction *MF = MBB.getParent();
2638 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
2639 PPCFunctionInfo *FI = MF->getInfo<PPCFunctionInfo>();
2640 bool MustSaveTOC = FI->mustSaveTOC();
2641 bool CR2Spilled = false;
2642 bool CR3Spilled = false;
2643 bool CR4Spilled = false;
2644 unsigned CSIIndex = 0;
2645 BitVector Restored(TRI->getNumRegs());
2646
2647 // Initialize insertion-point logic; we will be restoring in reverse
2648 // order of spill.
2649 MachineBasicBlock::iterator I = MI, BeforeI = I;
2650 bool AtStart = I == MBB.begin();
2651
2652 if (!AtStart)
2653 --BeforeI;
2654
2655 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
2656 MCRegister Reg = CSI[i].getReg();
2657
2658 if ((Reg == PPC::X2 || Reg == PPC::R2) && MustSaveTOC)
2659 continue;
2660
2661 // Restore of callee saved condition register field is handled during
2662 // epilogue insertion.
2663 if (isCalleeSavedCR(Reg) && !Subtarget.is32BitELFABI())
2664 continue;
2665
2666 if (Reg == PPC::CR2) {
2667 CR2Spilled = true;
2668 // The spill slot is associated only with CR2, which is the
2669 // first nonvolatile spilled. Save it here.
2670 CSIIndex = i;
2671 continue;
2672 } else if (Reg == PPC::CR3) {
2673 CR3Spilled = true;
2674 continue;
2675 } else if (Reg == PPC::CR4) {
2676 CR4Spilled = true;
2677 continue;
2678 } else {
2679 // On 32-bit ELF when we first encounter a non-CR register after seeing at
2680 // least one CR register, restore all spilled CRs together.
2681 if (CR2Spilled || CR3Spilled || CR4Spilled) {
2682 bool is31 = needsFP(MF: *MF);
2683 restoreCRs(is31, CR2Spilled, CR3Spilled, CR4Spilled, MBB, MI: I, CSI,
2684 CSIIndex);
2685 CR2Spilled = CR3Spilled = CR4Spilled = false;
2686 }
2687
2688 if (CSI[i].isSpilledToReg()) {
2689 DebugLoc DL;
2690 unsigned Dst = CSI[i].getDstReg();
2691
2692 if (Restored[Dst])
2693 continue;
2694
2695 const auto &VSR = VSRContainingGPRs[Dst];
2696 if (VSR.second != 0) {
2697 assert(Subtarget.hasP9Vector());
2698 NumPEReloadVSR += 2;
2699 BuildMI(BB&: MBB, I, MIMD: DL, MCID: TII.get(Opcode: PPC::MFVSRLD), DestReg: VSR.second).addReg(RegNo: Dst);
2700 BuildMI(BB&: MBB, I, MIMD: DL, MCID: TII.get(Opcode: PPC::MFVSRD), DestReg: VSR.first)
2701 .addReg(RegNo: TRI->getSubReg(Reg: Dst, Idx: PPC::sub_64), Flags: getKillRegState(B: true));
2702 } else if (VSR.second == 0) {
2703 assert(Subtarget.hasP8Vector());
2704 ++NumPEReloadVSR;
2705 BuildMI(BB&: MBB, I, MIMD: DL, MCID: TII.get(Opcode: PPC::MFVSRD), DestReg: VSR.first)
2706 .addReg(RegNo: TRI->getSubReg(Reg: Dst, Idx: PPC::sub_64), Flags: getKillRegState(B: true));
2707 } else {
2708 llvm_unreachable("More than two GPRs spilled to a VSR!");
2709 }
2710
2711 Restored.set(Dst);
2712
2713 } else {
2714 // Default behavior for non-CR saves.
2715 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
2716
2717 // Functions without NoUnwind need to preserve the order of elements in
2718 // saved vector registers.
2719 if (Subtarget.needsSwapsForVSXMemOps() &&
2720 !MF->getFunction().hasFnAttribute(Kind: Attribute::NoUnwind))
2721 TII.loadRegFromStackSlotNoUpd(MBB, MBBI: I, DestReg: Reg, FrameIndex: CSI[i].getFrameIdx(), RC);
2722 else
2723 TII.loadRegFromStackSlot(MBB, MBBI: I, DestReg: Reg, FrameIndex: CSI[i].getFrameIdx(), RC,
2724 VReg: Register());
2725
2726 assert(I != MBB.begin() &&
2727 "loadRegFromStackSlot didn't insert any code!");
2728 }
2729 }
2730
2731 // Insert in reverse order.
2732 if (AtStart)
2733 I = MBB.begin();
2734 else {
2735 I = BeforeI;
2736 ++I;
2737 }
2738 }
2739
2740 // If we haven't yet spilled the CRs, do so now.
2741 if (CR2Spilled || CR3Spilled || CR4Spilled) {
2742 assert(Subtarget.is32BitELFABI() &&
2743 "Only set CR[2|3|4]Spilled on 32-bit SVR4.");
2744 bool is31 = needsFP(MF: *MF);
2745 restoreCRs(is31, CR2Spilled, CR3Spilled, CR4Spilled, MBB, MI: I, CSI, CSIIndex);
2746 }
2747
2748 return true;
2749}
2750
2751uint64_t PPCFrameLowering::getTOCSaveOffset() const {
2752 return TOCSaveOffset;
2753}
2754
2755uint64_t PPCFrameLowering::getFramePointerSaveOffset() const {
2756 return FramePointerSaveOffset;
2757}
2758
2759uint64_t PPCFrameLowering::getBasePointerSaveOffset() const {
2760 return BasePointerSaveOffset;
2761}
2762
2763bool PPCFrameLowering::enableShrinkWrapping(const MachineFunction &MF) const {
2764 if (MF.getInfo<PPCFunctionInfo>()->shrinkWrapDisabled())
2765 return false;
2766 return !MF.getSubtarget<PPCSubtarget>().is32BitELFABI();
2767}
2768
2769void PPCFrameLowering::updateCalleeSaves(const MachineFunction &MF,
2770 BitVector &SavedRegs) const {
2771 // The AIX ABI uses traceback tables for EH which require that if callee-saved
2772 // register N is used, all registers N-31 must be saved/restored.
2773 // NOTE: The check for AIX is not actually what is relevant. Traceback tables
2774 // on Linux have the same requirements. It is just that AIX is the only ABI
2775 // for which we actually use traceback tables. If another ABI needs to be
2776 // supported that also uses them, we can add a check such as
2777 // Subtarget.usesTraceBackTables().
2778 assert(Subtarget.isAIXABI() &&
2779 "Function updateCalleeSaves should only be called for AIX.");
2780
2781 // If there are no callee saves then there is nothing to do.
2782 if (SavedRegs.none())
2783 return;
2784
2785 const MCPhysReg *CSRegs =
2786 Subtarget.getRegisterInfo()->getCalleeSavedRegs(MF: &MF);
2787 MCPhysReg LowestGPR = PPC::R31;
2788 MCPhysReg LowestG8R = PPC::X31;
2789 MCPhysReg LowestFPR = PPC::F31;
2790 MCPhysReg LowestVR = PPC::V31;
2791
2792 // Traverse the CSRs twice so as not to rely on ascending ordering of
2793 // registers in the array. The first pass finds the lowest numbered
2794 // register and the second pass marks all higher numbered registers
2795 // for spilling.
2796 for (int i = 0; CSRegs[i]; i++) {
2797 // Get the lowest numbered register for each class that actually needs
2798 // to be saved.
2799 MCPhysReg Cand = CSRegs[i];
2800 if (!SavedRegs.test(Idx: Cand))
2801 continue;
2802 // When R2/X2 is a CSR and not used for passing arguments, it is allocated
2803 // earlier than other volatile registers. R2/X2 is not contiguous with
2804 // R13/X13 to R31/X31.
2805 if (Cand == PPC::X2 || Cand == PPC::R2) {
2806 SavedRegs.set(Cand);
2807 continue;
2808 }
2809
2810 if (PPC::GPRCRegClass.contains(Reg: Cand) && Cand < LowestGPR)
2811 LowestGPR = Cand;
2812 else if (PPC::G8RCRegClass.contains(Reg: Cand) && Cand < LowestG8R)
2813 LowestG8R = Cand;
2814 else if ((PPC::F4RCRegClass.contains(Reg: Cand) ||
2815 PPC::F8RCRegClass.contains(Reg: Cand)) &&
2816 Cand < LowestFPR)
2817 LowestFPR = Cand;
2818 else if (PPC::VRRCRegClass.contains(Reg: Cand) && Cand < LowestVR)
2819 LowestVR = Cand;
2820 }
2821
2822 for (int i = 0; CSRegs[i]; i++) {
2823 MCPhysReg Cand = CSRegs[i];
2824 if ((PPC::GPRCRegClass.contains(Reg: Cand) && Cand > LowestGPR) ||
2825 (PPC::G8RCRegClass.contains(Reg: Cand) && Cand > LowestG8R) ||
2826 ((PPC::F4RCRegClass.contains(Reg: Cand) ||
2827 PPC::F8RCRegClass.contains(Reg: Cand)) &&
2828 Cand > LowestFPR) ||
2829 (PPC::VRRCRegClass.contains(Reg: Cand) && Cand > LowestVR))
2830 SavedRegs.set(Cand);
2831 }
2832}
2833
2834uint64_t PPCFrameLowering::getStackThreshold() const {
2835 // On PPC64, we use `stux r1, r1, <scratch_reg>` to extend the stack;
2836 // use `add r1, r1, <scratch_reg>` to release the stack frame.
2837 // Scratch register contains a signed 64-bit number, which is negative
2838 // when extending the stack and is positive when releasing the stack frame.
2839 // To make `stux` and `add` paired, the absolute value of the number contained
2840 // in the scratch register should be the same. Thus the maximum stack size
2841 // is (2^63)-1, i.e., INT64_MAX.
2842 if (Subtarget.isPPC64())
2843 return INT64_MAX;
2844
2845 return TargetFrameLowering::getStackThreshold();
2846}
2847