LLVM  10.0.0svn
PPCRegisterInfo.cpp
Go to the documentation of this file.
1 //===-- PPCRegisterInfo.cpp - PowerPC Register 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 PowerPC implementation of the TargetRegisterInfo
10 // class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "PPCRegisterInfo.h"
15 #include "PPCFrameLowering.h"
16 #include "PPCInstrBuilder.h"
17 #include "PPCMachineFunctionInfo.h"
18 #include "PPCSubtarget.h"
19 #include "PPCTargetMachine.h"
20 #include "llvm/ADT/BitVector.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/Statistic.h"
31 #include "llvm/IR/CallingConv.h"
32 #include "llvm/IR/Constants.h"
33 #include "llvm/IR/Function.h"
34 #include "llvm/IR/Type.h"
36 #include "llvm/Support/Debug.h"
42 #include <cstdlib>
43 
44 using namespace llvm;
45 
46 #define DEBUG_TYPE "reginfo"
47 
48 #define GET_REGINFO_TARGET_DESC
49 #include "PPCGenRegisterInfo.inc"
50 
51 STATISTIC(InflateGPRC, "Number of gprc inputs for getLargestLegalClass");
52 STATISTIC(InflateGP8RC, "Number of g8rc inputs for getLargestLegalClass");
53 
54 static cl::opt<bool>
55 EnableBasePointer("ppc-use-base-pointer", cl::Hidden, cl::init(true),
56  cl::desc("Enable use of a base pointer for complex stack frames"));
57 
58 static cl::opt<bool>
59 AlwaysBasePointer("ppc-always-use-base-pointer", cl::Hidden, cl::init(false),
60  cl::desc("Force the use of a base pointer in every function"));
61 
62 static cl::opt<bool>
63 EnableGPRToVecSpills("ppc-enable-gpr-to-vsr-spills", cl::Hidden, cl::init(false),
64  cl::desc("Enable spills from gpr to vsr rather than stack"));
65 
66 static cl::opt<bool>
67 StackPtrConst("ppc-stack-ptr-caller-preserved",
68  cl::desc("Consider R1 caller preserved so stack saves of "
69  "caller preserved registers can be LICM candidates"),
70  cl::init(true), cl::Hidden);
71 
72 static cl::opt<unsigned>
73 MaxCRBitSpillDist("ppc-max-crbit-spill-dist",
74  cl::desc("Maximum search distance for definition of CR bit "
75  "spill on ppc"),
76  cl::Hidden, cl::init(100));
77 
78 static unsigned offsetMinAlignForOpcode(unsigned OpC);
79 
81  : PPCGenRegisterInfo(TM.isPPC64() ? PPC::LR8 : PPC::LR,
82  TM.isPPC64() ? 0 : 1,
83  TM.isPPC64() ? 0 : 1),
84  TM(TM) {
85  ImmToIdxMap[PPC::LD] = PPC::LDX; ImmToIdxMap[PPC::STD] = PPC::STDX;
86  ImmToIdxMap[PPC::LBZ] = PPC::LBZX; ImmToIdxMap[PPC::STB] = PPC::STBX;
87  ImmToIdxMap[PPC::LHZ] = PPC::LHZX; ImmToIdxMap[PPC::LHA] = PPC::LHAX;
88  ImmToIdxMap[PPC::LWZ] = PPC::LWZX; ImmToIdxMap[PPC::LWA] = PPC::LWAX;
89  ImmToIdxMap[PPC::LFS] = PPC::LFSX; ImmToIdxMap[PPC::LFD] = PPC::LFDX;
90  ImmToIdxMap[PPC::STH] = PPC::STHX; ImmToIdxMap[PPC::STW] = PPC::STWX;
91  ImmToIdxMap[PPC::STFS] = PPC::STFSX; ImmToIdxMap[PPC::STFD] = PPC::STFDX;
92  ImmToIdxMap[PPC::ADDI] = PPC::ADD4;
93  ImmToIdxMap[PPC::LWA_32] = PPC::LWAX_32;
94 
95  // 64-bit
96  ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8;
97  ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8;
98  ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8;
99  ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX;
100  ImmToIdxMap[PPC::ADDI8] = PPC::ADD8;
101 
102  // VSX
103  ImmToIdxMap[PPC::DFLOADf32] = PPC::LXSSPX;
104  ImmToIdxMap[PPC::DFLOADf64] = PPC::LXSDX;
105  ImmToIdxMap[PPC::SPILLTOVSR_LD] = PPC::SPILLTOVSR_LDX;
106  ImmToIdxMap[PPC::SPILLTOVSR_ST] = PPC::SPILLTOVSR_STX;
107  ImmToIdxMap[PPC::DFSTOREf32] = PPC::STXSSPX;
108  ImmToIdxMap[PPC::DFSTOREf64] = PPC::STXSDX;
109  ImmToIdxMap[PPC::LXV] = PPC::LXVX;
110  ImmToIdxMap[PPC::LXSD] = PPC::LXSDX;
111  ImmToIdxMap[PPC::LXSSP] = PPC::LXSSPX;
112  ImmToIdxMap[PPC::STXV] = PPC::STXVX;
113  ImmToIdxMap[PPC::STXSD] = PPC::STXSDX;
114  ImmToIdxMap[PPC::STXSSP] = PPC::STXSSPX;
115 
116  // SPE
117  ImmToIdxMap[PPC::EVLDD] = PPC::EVLDDX;
118  ImmToIdxMap[PPC::EVSTDD] = PPC::EVSTDDX;
119  ImmToIdxMap[PPC::SPESTW] = PPC::SPESTWX;
120  ImmToIdxMap[PPC::SPELWZ] = PPC::SPELWZX;
121 }
122 
123 /// getPointerRegClass - Return the register class to use to hold pointers.
124 /// This is used for addressing modes.
125 const TargetRegisterClass *
127  const {
128  // Note that PPCInstrInfo::FoldImmediate also directly uses this Kind value
129  // when it checks for ZERO folding.
130  if (Kind == 1) {
131  if (TM.isPPC64())
132  return &PPC::G8RC_NOX0RegClass;
133  return &PPC::GPRC_NOR0RegClass;
134  }
135 
136  if (TM.isPPC64())
137  return &PPC::G8RCRegClass;
138  return &PPC::GPRCRegClass;
139 }
140 
141 const MCPhysReg*
143  const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>();
145  if (Subtarget.hasVSX())
146  return CSR_64_AllRegs_VSX_SaveList;
147  if (Subtarget.hasAltivec())
148  return CSR_64_AllRegs_Altivec_SaveList;
149  return CSR_64_AllRegs_SaveList;
150  }
151 
152  if (Subtarget.isDarwinABI())
153  return TM.isPPC64()
154  ? (Subtarget.hasAltivec() ? CSR_Darwin64_Altivec_SaveList
155  : CSR_Darwin64_SaveList)
156  : (Subtarget.hasAltivec() ? CSR_Darwin32_Altivec_SaveList
157  : CSR_Darwin32_SaveList);
158 
159  if (TM.isPPC64() && MF->getInfo<PPCFunctionInfo>()->isSplitCSR())
160  return CSR_SRV464_TLS_PE_SaveList;
161 
162  // On PPC64, we might need to save r2 (but only if it is not reserved).
163  bool SaveR2 = MF->getRegInfo().isAllocatable(PPC::X2);
164 
165  // Cold calling convention CSRs.
167  if (TM.isPPC64()) {
168  if (Subtarget.hasAltivec())
169  return SaveR2 ? CSR_SVR64_ColdCC_R2_Altivec_SaveList
170  : CSR_SVR64_ColdCC_Altivec_SaveList;
171  return SaveR2 ? CSR_SVR64_ColdCC_R2_SaveList
172  : CSR_SVR64_ColdCC_SaveList;
173  }
174  // 32-bit targets.
175  if (Subtarget.hasAltivec())
176  return CSR_SVR32_ColdCC_Altivec_SaveList;
177  else if (Subtarget.hasSPE())
178  return CSR_SVR32_ColdCC_SPE_SaveList;
179  return CSR_SVR32_ColdCC_SaveList;
180  }
181  // Standard calling convention CSRs.
182  if (TM.isPPC64()) {
183  if (Subtarget.hasAltivec())
184  return SaveR2 ? CSR_SVR464_R2_Altivec_SaveList
185  : CSR_SVR464_Altivec_SaveList;
186  return SaveR2 ? CSR_SVR464_R2_SaveList
187  : CSR_SVR464_SaveList;
188  }
189  // 32-bit targets.
190  if (Subtarget.hasAltivec())
191  return CSR_SVR432_Altivec_SaveList;
192  else if (Subtarget.hasSPE())
193  return CSR_SVR432_SPE_SaveList;
194  return CSR_SVR432_SaveList;
195 }
196 
197 const MCPhysReg *
199  assert(MF && "Invalid MachineFunction pointer.");
200  const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>();
201  if (Subtarget.isDarwinABI())
202  return nullptr;
203  if (!TM.isPPC64())
204  return nullptr;
206  return nullptr;
207  if (!MF->getInfo<PPCFunctionInfo>()->isSplitCSR())
208  return nullptr;
209 
210  // On PPC64, we might need to save r2 (but only if it is not reserved).
211  bool SaveR2 = !getReservedRegs(*MF).test(PPC::X2);
212  if (Subtarget.hasAltivec())
213  return SaveR2
214  ? CSR_SVR464_R2_Altivec_ViaCopy_SaveList
215  : CSR_SVR464_Altivec_ViaCopy_SaveList;
216  else
217  return SaveR2
218  ? CSR_SVR464_R2_ViaCopy_SaveList
219  : CSR_SVR464_ViaCopy_SaveList;
220 }
221 
222 const uint32_t *
224  CallingConv::ID CC) const {
225  const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
226  if (CC == CallingConv::AnyReg) {
227  if (Subtarget.hasVSX())
228  return CSR_64_AllRegs_VSX_RegMask;
229  if (Subtarget.hasAltivec())
230  return CSR_64_AllRegs_Altivec_RegMask;
231  return CSR_64_AllRegs_RegMask;
232  }
233 
234  if (Subtarget.isDarwinABI())
235  return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_Darwin64_Altivec_RegMask
236  : CSR_Darwin64_RegMask)
237  : (Subtarget.hasAltivec() ? CSR_Darwin32_Altivec_RegMask
238  : CSR_Darwin32_RegMask);
239  if (Subtarget.isAIXABI()) {
240  assert(!Subtarget.hasAltivec() && "Altivec is not implemented on AIX yet.");
241  return TM.isPPC64() ? CSR_AIX64_RegMask : CSR_AIX32_RegMask;
242  }
243 
244  if (CC == CallingConv::Cold) {
245  return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_SVR64_ColdCC_Altivec_RegMask
246  : CSR_SVR64_ColdCC_RegMask)
247  : (Subtarget.hasAltivec() ? CSR_SVR32_ColdCC_Altivec_RegMask
248  : (Subtarget.hasSPE()
249  ? CSR_SVR32_ColdCC_SPE_RegMask
250  : CSR_SVR32_ColdCC_RegMask));
251  }
252 
253  return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_SVR464_Altivec_RegMask
254  : CSR_SVR464_RegMask)
255  : (Subtarget.hasAltivec() ? CSR_SVR432_Altivec_RegMask
256  : (Subtarget.hasSPE()
257  ? CSR_SVR432_SPE_RegMask
258  : CSR_SVR432_RegMask));
259 }
260 
261 const uint32_t*
263  return CSR_NoRegs_RegMask;
264 }
265 
267  for (unsigned PseudoReg : {PPC::ZERO, PPC::ZERO8, PPC::RM})
268  Mask[PseudoReg / 32] &= ~(1u << (PseudoReg % 32));
269 }
270 
272  BitVector Reserved(getNumRegs());
273  const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
274  const PPCFrameLowering *TFI = getFrameLowering(MF);
275 
276  // The ZERO register is not really a register, but the representation of r0
277  // when used in instructions that treat r0 as the constant 0.
278  markSuperRegs(Reserved, PPC::ZERO);
279 
280  // The FP register is also not really a register, but is the representation
281  // of the frame pointer register used by ISD::FRAMEADDR.
282  markSuperRegs(Reserved, PPC::FP);
283 
284  // The BP register is also not really a register, but is the representation
285  // of the base pointer register used by setjmp.
286  markSuperRegs(Reserved, PPC::BP);
287 
288  // The counter registers must be reserved so that counter-based loops can
289  // be correctly formed (and the mtctr instructions are not DCE'd).
290  markSuperRegs(Reserved, PPC::CTR);
291  markSuperRegs(Reserved, PPC::CTR8);
292 
293  markSuperRegs(Reserved, PPC::R1);
294  markSuperRegs(Reserved, PPC::LR);
295  markSuperRegs(Reserved, PPC::LR8);
296  markSuperRegs(Reserved, PPC::RM);
297 
298  if (!Subtarget.isDarwinABI() || !Subtarget.hasAltivec())
299  markSuperRegs(Reserved, PPC::VRSAVE);
300 
301  // The SVR4 ABI reserves r2 and r13
302  if (Subtarget.isSVR4ABI()) {
303  // We only reserve r2 if we need to use the TOC pointer. If we have no
304  // explicit uses of the TOC pointer (meaning we're a leaf function with
305  // no constant-pool loads, etc.) and we have no potential uses inside an
306  // inline asm block, then we can treat r2 has an ordinary callee-saved
307  // register.
308  const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
309  if (!TM.isPPC64() || FuncInfo->usesTOCBasePtr() || MF.hasInlineAsm())
310  markSuperRegs(Reserved, PPC::R2); // System-reserved register
311  markSuperRegs(Reserved, PPC::R13); // Small Data Area pointer register
312  }
313 
314  // Always reserve r2 on AIX for now.
315  // TODO: Make r2 allocatable on AIX/XCOFF for some leaf functions.
316  if (Subtarget.isAIXABI())
317  markSuperRegs(Reserved, PPC::R2); // System-reserved register
318 
319  // On PPC64, r13 is the thread pointer. Never allocate this register.
320  if (TM.isPPC64())
321  markSuperRegs(Reserved, PPC::R13);
322 
323  if (TFI->needsFP(MF))
324  markSuperRegs(Reserved, PPC::R31);
325 
326  bool IsPositionIndependent = TM.isPositionIndependent();
327  if (hasBasePointer(MF)) {
328  if (Subtarget.isSVR4ABI() && !TM.isPPC64() && IsPositionIndependent)
329  markSuperRegs(Reserved, PPC::R29);
330  else
331  markSuperRegs(Reserved, PPC::R30);
332  }
333 
334  if (Subtarget.isSVR4ABI() && !TM.isPPC64() && IsPositionIndependent)
335  markSuperRegs(Reserved, PPC::R30);
336 
337  // Reserve Altivec registers when Altivec is unavailable.
338  if (!Subtarget.hasAltivec())
339  for (TargetRegisterClass::iterator I = PPC::VRRCRegClass.begin(),
340  IE = PPC::VRRCRegClass.end(); I != IE; ++I)
341  markSuperRegs(Reserved, *I);
342 
343  assert(checkAllSuperRegsMarked(Reserved));
344  return Reserved;
345 }
346 
348  const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
349  const PPCInstrInfo *InstrInfo = Subtarget.getInstrInfo();
350  const MachineFrameInfo &MFI = MF.getFrameInfo();
351  const std::vector<CalleeSavedInfo> &Info = MFI.getCalleeSavedInfo();
352 
353  // If the callee saved info is invalid we have to default to true for safety.
354  if (!MFI.isCalleeSavedInfoValid())
355  return true;
356 
357  // We will require the use of X-Forms because the frame is larger than what
358  // can be represented in signed 16 bits that fit in the immediate of a D-Form.
359  // If we need an X-Form then we need a register to store the address offset.
360  unsigned FrameSize = MFI.getStackSize();
361  // Signed 16 bits means that the FrameSize cannot be more than 15 bits.
362  if (FrameSize & ~0x7FFF)
363  return true;
364 
365  // The callee saved info is valid so it can be traversed.
366  // Checking for registers that need saving that do not have load or store
367  // forms where the address offset is an immediate.
368  for (unsigned i = 0; i < Info.size(); i++) {
369  int FrIdx = Info[i].getFrameIdx();
370  unsigned Reg = Info[i].getReg();
371 
372  unsigned Opcode = InstrInfo->getStoreOpcodeForSpill(Reg);
373  if (!MFI.isFixedObjectIndex(FrIdx)) {
374  // This is not a fixed object. If it requires alignment then we may still
375  // need to use the XForm.
376  if (offsetMinAlignForOpcode(Opcode) > 1)
377  return true;
378  }
379 
380  // This is eiher:
381  // 1) A fixed frame index object which we know are aligned so
382  // as long as we have a valid DForm/DSForm/DQForm (non XForm) we don't
383  // need to consider the alignement here.
384  // 2) A not fixed object but in that case we now know that the min required
385  // alignment is no more than 1 based on the previous check.
386  if (InstrInfo->isXFormMemOp(Opcode))
387  return true;
388  }
389  return false;
390 }
391 
393  const MachineFunction &MF) const {
395  const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
396  const MachineFrameInfo &MFI = MF.getFrameInfo();
397  if (!TM.isPPC64())
398  return false;
399 
400  if (!Subtarget.isSVR4ABI())
401  return false;
402  if (PhysReg == PPC::X2)
403  // X2 is guaranteed to be preserved within a function if it is reserved.
404  // The reason it's reserved is that it's the TOC pointer (and the function
405  // uses the TOC). In functions where it isn't reserved (i.e. leaf functions
406  // with no TOC access), we can't claim that it is preserved.
407  return (getReservedRegs(MF).test(PPC::X2));
408  if (StackPtrConst && (PhysReg == PPC::X1) && !MFI.hasVarSizedObjects()
409  && !MFI.hasOpaqueSPAdjustment())
410  // The value of the stack pointer does not change within a function after
411  // the prologue and before the epilogue if there are no dynamic allocations
412  // and no inline asm which clobbers X1.
413  return true;
414  return false;
415 }
416 
418  MachineFunction &MF) const {
419  const PPCFrameLowering *TFI = getFrameLowering(MF);
420  const unsigned DefaultSafety = 1;
421 
422  switch (RC->getID()) {
423  default:
424  return 0;
425  case PPC::G8RC_NOX0RegClassID:
426  case PPC::GPRC_NOR0RegClassID:
427  case PPC::SPERCRegClassID:
428  case PPC::SPE4RCRegClassID:
429  case PPC::G8RCRegClassID:
430  case PPC::GPRCRegClassID: {
431  unsigned FP = TFI->hasFP(MF) ? 1 : 0;
432  return 32 - FP - DefaultSafety;
433  }
434  case PPC::F8RCRegClassID:
435  case PPC::F4RCRegClassID:
436  case PPC::QFRCRegClassID:
437  case PPC::QSRCRegClassID:
438  case PPC::QBRCRegClassID:
439  case PPC::VRRCRegClassID:
440  case PPC::VFRCRegClassID:
441  case PPC::VSLRCRegClassID:
442  return 32 - DefaultSafety;
443  case PPC::VSRCRegClassID:
444  case PPC::VSFRCRegClassID:
445  case PPC::VSSRCRegClassID:
446  return 64 - DefaultSafety;
447  case PPC::CRRCRegClassID:
448  return 8 - DefaultSafety;
449  }
450 }
451 
452 const TargetRegisterClass *
454  const MachineFunction &MF) const {
455  const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
456  if (Subtarget.hasVSX()) {
457  // With VSX, we can inflate various sub-register classes to the full VSX
458  // register set.
459 
460  // For Power9 we allow the user to enable GPR to vector spills.
461  // FIXME: Currently limited to spilling GP8RC. A follow on patch will add
462  // support to spill GPRC.
463  if (TM.isELFv2ABI()) {
464  if (Subtarget.hasP9Vector() && EnableGPRToVecSpills &&
465  RC == &PPC::G8RCRegClass) {
466  InflateGP8RC++;
467  return &PPC::SPILLTOVSRRCRegClass;
468  }
469  if (RC == &PPC::GPRCRegClass && EnableGPRToVecSpills)
470  InflateGPRC++;
471  }
472  if (RC == &PPC::F8RCRegClass)
473  return &PPC::VSFRCRegClass;
474  else if (RC == &PPC::VRRCRegClass)
475  return &PPC::VSRCRegClass;
476  else if (RC == &PPC::F4RCRegClass && Subtarget.hasP8Vector())
477  return &PPC::VSSRCRegClass;
478  }
479 
481 }
482 
483 //===----------------------------------------------------------------------===//
484 // Stack Frame Processing methods
485 //===----------------------------------------------------------------------===//
486 
487 /// lowerDynamicAlloc - Generate the code for allocating an object in the
488 /// current frame. The sequence of code will be in the general form
489 ///
490 /// addi R0, SP, \#frameSize ; get the address of the previous frame
491 /// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size
492 /// addi Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
493 ///
495  // Get the instruction.
496  MachineInstr &MI = *II;
497  // Get the instruction's basic block.
498  MachineBasicBlock &MBB = *MI.getParent();
499  // Get the basic block's function.
500  MachineFunction &MF = *MBB.getParent();
501  // Get the frame info.
502  MachineFrameInfo &MFI = MF.getFrameInfo();
503  const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
504  // Get the instruction info.
505  const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
506  // Determine whether 64-bit pointers are used.
507  bool LP64 = TM.isPPC64();
508  DebugLoc dl = MI.getDebugLoc();
509 
510  // Get the maximum call stack size.
511  unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
512  // Get the total frame size.
513  unsigned FrameSize = MFI.getStackSize();
514 
515  // Get stack alignments.
516  const PPCFrameLowering *TFI = getFrameLowering(MF);
517  unsigned TargetAlign = TFI->getStackAlignment();
518  unsigned MaxAlign = MFI.getMaxAlignment();
519  assert((maxCallFrameSize & (MaxAlign-1)) == 0 &&
520  "Maximum call-frame size not sufficiently aligned");
521 
522  // Determine the previous frame's address. If FrameSize can't be
523  // represented as 16 bits or we need special alignment, then we load the
524  // previous frame's address from 0(SP). Why not do an addis of the hi?
525  // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
526  // Constructing the constant and adding would take 3 instructions.
527  // Fortunately, a frame greater than 32K is rare.
528  const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
529  const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
530  unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
531 
532  if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) {
533  if (LP64)
534  BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), Reg)
535  .addReg(PPC::X31)
536  .addImm(FrameSize);
537  else
538  BuildMI(MBB, II, dl, TII.get(PPC::ADDI), Reg)
539  .addReg(PPC::R31)
540  .addImm(FrameSize);
541  } else if (LP64) {
542  BuildMI(MBB, II, dl, TII.get(PPC::LD), Reg)
543  .addImm(0)
544  .addReg(PPC::X1);
545  } else {
546  BuildMI(MBB, II, dl, TII.get(PPC::LWZ), Reg)
547  .addImm(0)
548  .addReg(PPC::R1);
549  }
550 
551  bool KillNegSizeReg = MI.getOperand(1).isKill();
552  unsigned NegSizeReg = MI.getOperand(1).getReg();
553 
554  // Grow the stack and update the stack pointer link, then determine the
555  // address of new allocated space.
556  if (LP64) {
557  if (MaxAlign > TargetAlign) {
558  unsigned UnalNegSizeReg = NegSizeReg;
559  NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
560 
561  // Unfortunately, there is no andi, only andi., and we can't insert that
562  // here because we might clobber cr0 while it is live.
563  BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg)
564  .addImm(~(MaxAlign-1));
565 
566  unsigned NegSizeReg1 = NegSizeReg;
567  NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
568  BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg)
569  .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
570  .addReg(NegSizeReg1, RegState::Kill);
571  KillNegSizeReg = true;
572  }
573 
574  BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1)
575  .addReg(Reg, RegState::Kill)
576  .addReg(PPC::X1)
577  .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
578  BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
579  .addReg(PPC::X1)
580  .addImm(maxCallFrameSize);
581  } else {
582  if (MaxAlign > TargetAlign) {
583  unsigned UnalNegSizeReg = NegSizeReg;
584  NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
585 
586  // Unfortunately, there is no andi, only andi., and we can't insert that
587  // here because we might clobber cr0 while it is live.
588  BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg)
589  .addImm(~(MaxAlign-1));
590 
591  unsigned NegSizeReg1 = NegSizeReg;
592  NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
593  BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg)
594  .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
595  .addReg(NegSizeReg1, RegState::Kill);
596  KillNegSizeReg = true;
597  }
598 
599  BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1)
600  .addReg(Reg, RegState::Kill)
601  .addReg(PPC::R1)
602  .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
603  BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
604  .addReg(PPC::R1)
605  .addImm(maxCallFrameSize);
606  }
607 
608  // Discard the DYNALLOC instruction.
609  MBB.erase(II);
610 }
611 
613  MachineBasicBlock::iterator II) const {
614  // Get the instruction.
615  MachineInstr &MI = *II;
616  // Get the instruction's basic block.
617  MachineBasicBlock &MBB = *MI.getParent();
618  // Get the basic block's function.
619  MachineFunction &MF = *MBB.getParent();
620  // Get the frame info.
621  MachineFrameInfo &MFI = MF.getFrameInfo();
622  const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
623  // Get the instruction info.
624  const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
625 
626  unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
627  bool is64Bit = TM.isPPC64();
628  DebugLoc dl = MI.getDebugLoc();
629  BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI),
630  MI.getOperand(0).getReg())
631  .addImm(maxCallFrameSize);
632  MBB.erase(II);
633 }
634 
635 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
636 /// reserving a whole register (R0), we scrounge for one here. This generates
637 /// code like this:
638 ///
639 /// mfcr rA ; Move the conditional register into GPR rA.
640 /// rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
641 /// stw rA, FI ; Store rA to the frame.
642 ///
644  unsigned FrameIndex) const {
645  // Get the instruction.
646  MachineInstr &MI = *II; // ; SPILL_CR <SrcReg>, <offset>
647  // Get the instruction's basic block.
648  MachineBasicBlock &MBB = *MI.getParent();
649  MachineFunction &MF = *MBB.getParent();
650  const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
651  const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
652  DebugLoc dl = MI.getDebugLoc();
653 
654  bool LP64 = TM.isPPC64();
655  const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
656  const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
657 
658  unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
659  unsigned SrcReg = MI.getOperand(0).getReg();
660 
661  // We need to store the CR in the low 4-bits of the saved value. First, issue
662  // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg.
663  BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
664  .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
665 
666  // If the saved register wasn't CR0, shift the bits left so that they are in
667  // CR0's slot.
668  if (SrcReg != PPC::CR0) {
669  unsigned Reg1 = Reg;
670  Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
671 
672  // rlwinm rA, rA, ShiftBits, 0, 31.
673  BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
674  .addReg(Reg1, RegState::Kill)
675  .addImm(getEncodingValue(SrcReg) * 4)
676  .addImm(0)
677  .addImm(31);
678  }
679 
680  addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
681  .addReg(Reg, RegState::Kill),
682  FrameIndex);
683 
684  // Discard the pseudo instruction.
685  MBB.erase(II);
686 }
687 
689  unsigned FrameIndex) const {
690  // Get the instruction.
691  MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CR <offset>
692  // Get the instruction's basic block.
693  MachineBasicBlock &MBB = *MI.getParent();
694  MachineFunction &MF = *MBB.getParent();
695  const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
696  const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
697  DebugLoc dl = MI.getDebugLoc();
698 
699  bool LP64 = TM.isPPC64();
700  const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
701  const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
702 
703  unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
704  unsigned DestReg = MI.getOperand(0).getReg();
705  assert(MI.definesRegister(DestReg) &&
706  "RESTORE_CR does not define its destination");
707 
708  addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
709  Reg), FrameIndex);
710 
711  // If the reloaded register isn't CR0, shift the bits right so that they are
712  // in the right CR's slot.
713  if (DestReg != PPC::CR0) {
714  unsigned Reg1 = Reg;
715  Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
716 
717  unsigned ShiftBits = getEncodingValue(DestReg)*4;
718  // rlwinm r11, r11, 32-ShiftBits, 0, 31.
719  BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
720  .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0)
721  .addImm(31);
722  }
723 
724  BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg)
725  .addReg(Reg, RegState::Kill);
726 
727  // Discard the pseudo instruction.
728  MBB.erase(II);
729 }
730 
732  unsigned FrameIndex) const {
733  // Get the instruction.
734  MachineInstr &MI = *II; // ; SPILL_CRBIT <SrcReg>, <offset>
735  // Get the instruction's basic block.
736  MachineBasicBlock &MBB = *MI.getParent();
737  MachineFunction &MF = *MBB.getParent();
738  const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
739  const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
740  const TargetRegisterInfo* TRI = Subtarget.getRegisterInfo();
741  DebugLoc dl = MI.getDebugLoc();
742 
743  bool LP64 = TM.isPPC64();
744  const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
745  const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
746 
747  unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
748  unsigned SrcReg = MI.getOperand(0).getReg();
749 
750  // Search up the BB to find the definition of the CR bit.
752  unsigned CRBitSpillDistance = 0;
753  for (Ins = MI; Ins != MBB.rend(); Ins++) {
754  // Definition found.
755  if (Ins->modifiesRegister(SrcReg, TRI))
756  break;
757  // Unable to find CR bit definition within maximum search distance.
758  if (CRBitSpillDistance == MaxCRBitSpillDist) {
759  Ins = MI;
760  break;
761  }
762  // Skip debug instructions when counting CR bit spill distance.
763  if (!Ins->isDebugInstr())
764  CRBitSpillDistance++;
765  }
766 
767  // Unable to find the definition of the CR bit in the MBB.
768  if (Ins == MBB.rend())
769  Ins = MI;
770 
771  // There is no need to extract the CR bit if its value is already known.
772  switch (Ins->getOpcode()) {
773  case PPC::CRUNSET:
774  BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LI8 : PPC::LI), Reg)
775  .addImm(0);
776  break;
777  case PPC::CRSET:
778  BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LIS8 : PPC::LIS), Reg)
779  .addImm(-32768);
780  break;
781  default:
782  // We need to move the CR field that contains the CR bit we are spilling.
783  // The super register may not be explicitly defined (i.e. it can be defined
784  // by a CR-logical that only defines the subreg) so we state that the CR
785  // field is undef. Also, in order to preserve the kill flag on the CR bit,
786  // we add it as an implicit use.
787  BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
788  .addReg(getCRFromCRBit(SrcReg), RegState::Undef)
789  .addReg(SrcReg,
791 
792  // If the saved register wasn't CR0LT, shift the bits left so that the bit
793  // to store is the first one. Mask all but that bit.
794  unsigned Reg1 = Reg;
795  Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
796 
797  // rlwinm rA, rA, ShiftBits, 0, 0.
798  BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
799  .addReg(Reg1, RegState::Kill)
800  .addImm(getEncodingValue(SrcReg))
801  .addImm(0).addImm(0);
802  }
803  addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
804  .addReg(Reg, RegState::Kill),
805  FrameIndex);
806 
807  // Discard the pseudo instruction.
808  MBB.erase(II);
809 }
810 
812  unsigned FrameIndex) const {
813  // Get the instruction.
814  MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CRBIT <offset>
815  // Get the instruction's basic block.
816  MachineBasicBlock &MBB = *MI.getParent();
817  MachineFunction &MF = *MBB.getParent();
818  const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
819  const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
820  DebugLoc dl = MI.getDebugLoc();
821 
822  bool LP64 = TM.isPPC64();
823  const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
824  const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
825 
826  unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
827  unsigned DestReg = MI.getOperand(0).getReg();
828  assert(MI.definesRegister(DestReg) &&
829  "RESTORE_CRBIT does not define its destination");
830 
831  addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
832  Reg), FrameIndex);
833 
834  BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg);
835 
836  unsigned RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
837  BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO)
838  .addReg(getCRFromCRBit(DestReg));
839 
840  unsigned ShiftBits = getEncodingValue(DestReg);
841  // rlwimi r11, r10, 32-ShiftBits, ..., ...
842  BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO)
843  .addReg(RegO, RegState::Kill)
844  .addReg(Reg, RegState::Kill)
845  .addImm(ShiftBits ? 32 - ShiftBits : 0)
846  .addImm(ShiftBits)
847  .addImm(ShiftBits);
848 
849  BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF),
850  getCRFromCRBit(DestReg))
851  .addReg(RegO, RegState::Kill)
852  // Make sure we have a use dependency all the way through this
853  // sequence of instructions. We can't have the other bits in the CR
854  // modified in between the mfocrf and the mtocrf.
856 
857  // Discard the pseudo instruction.
858  MBB.erase(II);
859 }
860 
862  unsigned FrameIndex) const {
863  // Get the instruction.
864  MachineInstr &MI = *II; // ; SPILL_VRSAVE <SrcReg>, <offset>
865  // Get the instruction's basic block.
866  MachineBasicBlock &MBB = *MI.getParent();
867  MachineFunction &MF = *MBB.getParent();
868  const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
869  const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
870  DebugLoc dl = MI.getDebugLoc();
871 
872  const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
873  unsigned Reg = MF.getRegInfo().createVirtualRegister(GPRC);
874  unsigned SrcReg = MI.getOperand(0).getReg();
875 
876  BuildMI(MBB, II, dl, TII.get(PPC::MFVRSAVEv), Reg)
877  .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
878 
880  BuildMI(MBB, II, dl, TII.get(PPC::STW)).addReg(Reg, RegState::Kill),
881  FrameIndex);
882 
883  // Discard the pseudo instruction.
884  MBB.erase(II);
885 }
886 
888  unsigned FrameIndex) const {
889  // Get the instruction.
890  MachineInstr &MI = *II; // ; <DestReg> = RESTORE_VRSAVE <offset>
891  // Get the instruction's basic block.
892  MachineBasicBlock &MBB = *MI.getParent();
893  MachineFunction &MF = *MBB.getParent();
894  const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
895  const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
896  DebugLoc dl = MI.getDebugLoc();
897 
898  const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
899  unsigned Reg = MF.getRegInfo().createVirtualRegister(GPRC);
900  unsigned DestReg = MI.getOperand(0).getReg();
901  assert(MI.definesRegister(DestReg) &&
902  "RESTORE_VRSAVE does not define its destination");
903 
904  addFrameReference(BuildMI(MBB, II, dl, TII.get(PPC::LWZ),
905  Reg), FrameIndex);
906 
907  BuildMI(MBB, II, dl, TII.get(PPC::MTVRSAVEv), DestReg)
908  .addReg(Reg, RegState::Kill);
909 
910  // Discard the pseudo instruction.
911  MBB.erase(II);
912 }
913 
915  unsigned Reg, int &FrameIdx) const {
916  const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
917  // For the nonvolatile condition registers (CR2, CR3, CR4) in an SVR4
918  // ABI, return true to prevent allocating an additional frame slot.
919  // For 64-bit, the CR save area is at SP+8; the value of FrameIdx = 0
920  // is arbitrary and will be subsequently ignored. For 32-bit, we have
921  // previously created the stack slot if needed, so return its FrameIdx.
922  if (Subtarget.isSVR4ABI() && PPC::CR2 <= Reg && Reg <= PPC::CR4) {
923  if (TM.isPPC64())
924  FrameIdx = 0;
925  else {
926  const PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
927  FrameIdx = FI->getCRSpillFrameIndex();
928  }
929  return true;
930  }
931  return false;
932 }
933 
934 // If the offset must be a multiple of some value, return what that value is.
935 static unsigned offsetMinAlignForOpcode(unsigned OpC) {
936  switch (OpC) {
937  default:
938  return 1;
939  case PPC::LWA:
940  case PPC::LWA_32:
941  case PPC::LD:
942  case PPC::LDU:
943  case PPC::STD:
944  case PPC::STDU:
945  case PPC::DFLOADf32:
946  case PPC::DFLOADf64:
947  case PPC::DFSTOREf32:
948  case PPC::DFSTOREf64:
949  case PPC::LXSD:
950  case PPC::LXSSP:
951  case PPC::STXSD:
952  case PPC::STXSSP:
953  return 4;
954  case PPC::EVLDD:
955  case PPC::EVSTDD:
956  return 8;
957  case PPC::LXV:
958  case PPC::STXV:
959  return 16;
960  }
961 }
962 
963 // If the offset must be a multiple of some value, return what that value is.
964 static unsigned offsetMinAlign(const MachineInstr &MI) {
965  unsigned OpC = MI.getOpcode();
966  return offsetMinAlignForOpcode(OpC);
967 }
968 
969 // Return the OffsetOperandNo given the FIOperandNum (and the instruction).
970 static unsigned getOffsetONFromFION(const MachineInstr &MI,
971  unsigned FIOperandNum) {
972  // Take into account whether it's an add or mem instruction
973  unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2;
974  if (MI.isInlineAsm())
975  OffsetOperandNo = FIOperandNum - 1;
976  else if (MI.getOpcode() == TargetOpcode::STACKMAP ||
977  MI.getOpcode() == TargetOpcode::PATCHPOINT)
978  OffsetOperandNo = FIOperandNum + 1;
979 
980  return OffsetOperandNo;
981 }
982 
983 void
985  int SPAdj, unsigned FIOperandNum,
986  RegScavenger *RS) const {
987  assert(SPAdj == 0 && "Unexpected");
988 
989  // Get the instruction.
990  MachineInstr &MI = *II;
991  // Get the instruction's basic block.
992  MachineBasicBlock &MBB = *MI.getParent();
993  // Get the basic block's function.
994  MachineFunction &MF = *MBB.getParent();
995  const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
996  // Get the instruction info.
997  const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
998  // Get the frame info.
999  MachineFrameInfo &MFI = MF.getFrameInfo();
1000  DebugLoc dl = MI.getDebugLoc();
1001 
1002  unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
1003 
1004  // Get the frame index.
1005  int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
1006 
1007  // Get the frame pointer save index. Users of this index are primarily
1008  // DYNALLOC instructions.
1010  int FPSI = FI->getFramePointerSaveIndex();
1011  // Get the instruction opcode.
1012  unsigned OpC = MI.getOpcode();
1013 
1014  if ((OpC == PPC::DYNAREAOFFSET || OpC == PPC::DYNAREAOFFSET8)) {
1016  return;
1017  }
1018 
1019  // Special case for dynamic alloca.
1020  if (FPSI && FrameIndex == FPSI &&
1021  (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
1022  lowerDynamicAlloc(II);
1023  return;
1024  }
1025 
1026  // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc.
1027  if (OpC == PPC::SPILL_CR) {
1028  lowerCRSpilling(II, FrameIndex);
1029  return;
1030  } else if (OpC == PPC::RESTORE_CR) {
1031  lowerCRRestore(II, FrameIndex);
1032  return;
1033  } else if (OpC == PPC::SPILL_CRBIT) {
1034  lowerCRBitSpilling(II, FrameIndex);
1035  return;
1036  } else if (OpC == PPC::RESTORE_CRBIT) {
1037  lowerCRBitRestore(II, FrameIndex);
1038  return;
1039  } else if (OpC == PPC::SPILL_VRSAVE) {
1040  lowerVRSAVESpilling(II, FrameIndex);
1041  return;
1042  } else if (OpC == PPC::RESTORE_VRSAVE) {
1043  lowerVRSAVERestore(II, FrameIndex);
1044  return;
1045  }
1046 
1047  // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
1048  MI.getOperand(FIOperandNum).ChangeToRegister(
1049  FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false);
1050 
1051  // If the instruction is not present in ImmToIdxMap, then it has no immediate
1052  // form (and must be r+r).
1053  bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP &&
1054  OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC);
1055 
1056  // Now add the frame object offset to the offset from r1.
1057  int Offset = MFI.getObjectOffset(FrameIndex);
1058  Offset += MI.getOperand(OffsetOperandNo).getImm();
1059 
1060  // If we're not using a Frame Pointer that has been set to the value of the
1061  // SP before having the stack size subtracted from it, then add the stack size
1062  // to Offset to get the correct offset.
1063  // Naked functions have stack size 0, although getStackSize may not reflect
1064  // that because we didn't call all the pieces that compute it for naked
1065  // functions.
1066  if (!MF.getFunction().hasFnAttribute(Attribute::Naked)) {
1067  if (!(hasBasePointer(MF) && FrameIndex < 0))
1068  Offset += MFI.getStackSize();
1069  }
1070 
1071  // If we can, encode the offset directly into the instruction. If this is a
1072  // normal PPC "ri" instruction, any 16-bit value can be safely encoded. If
1073  // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
1074  // clear can be encoded. This is extremely uncommon, because normally you
1075  // only "std" to a stack slot that is at least 4-byte aligned, but it can
1076  // happen in invalid code.
1077  assert(OpC != PPC::DBG_VALUE &&
1078  "This should be handled in a target-independent way");
1079  bool OffsetFitsMnemonic = (OpC == PPC::EVSTDD || OpC == PPC::EVLDD) ?
1080  isUInt<8>(Offset) :
1081  isInt<16>(Offset);
1082  if (!noImmForm && ((OffsetFitsMnemonic &&
1083  ((Offset % offsetMinAlign(MI)) == 0)) ||
1084  OpC == TargetOpcode::STACKMAP ||
1085  OpC == TargetOpcode::PATCHPOINT)) {
1086  MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
1087  return;
1088  }
1089 
1090  // The offset doesn't fit into a single register, scavenge one to build the
1091  // offset in.
1092 
1093  bool is64Bit = TM.isPPC64();
1094  const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1095  const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1096  const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC;
1097  unsigned SRegHi = MF.getRegInfo().createVirtualRegister(RC),
1098  SReg = MF.getRegInfo().createVirtualRegister(RC);
1099 
1100  // Insert a set of rA with the full offset value before the ld, st, or add
1101  if (isInt<16>(Offset))
1102  BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), SReg)
1103  .addImm(Offset);
1104  else {
1105  BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi)
1106  .addImm(Offset >> 16);
1107  BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg)
1108  .addReg(SRegHi, RegState::Kill)
1109  .addImm(Offset);
1110  }
1111 
1112  // Convert into indexed form of the instruction:
1113  //
1114  // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
1115  // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
1116  unsigned OperandBase;
1117 
1118  if (noImmForm)
1119  OperandBase = 1;
1120  else if (OpC != TargetOpcode::INLINEASM &&
1121  OpC != TargetOpcode::INLINEASM_BR) {
1122  assert(ImmToIdxMap.count(OpC) &&
1123  "No indexed form of load or store available!");
1124  unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
1125  MI.setDesc(TII.get(NewOpcode));
1126  OperandBase = 1;
1127  } else {
1128  OperandBase = OffsetOperandNo;
1129  }
1130 
1131  unsigned StackReg = MI.getOperand(FIOperandNum).getReg();
1132  MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
1133  MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true);
1134 }
1135 
1137  const PPCFrameLowering *TFI = getFrameLowering(MF);
1138 
1139  if (!TM.isPPC64())
1140  return TFI->hasFP(MF) ? PPC::R31 : PPC::R1;
1141  else
1142  return TFI->hasFP(MF) ? PPC::X31 : PPC::X1;
1143 }
1144 
1146  const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1147  if (!hasBasePointer(MF))
1148  return getFrameRegister(MF);
1149 
1150  if (TM.isPPC64())
1151  return PPC::X30;
1152 
1153  if (Subtarget.isSVR4ABI() && TM.isPositionIndependent())
1154  return PPC::R29;
1155 
1156  return PPC::R30;
1157 }
1158 
1160  if (!EnableBasePointer)
1161  return false;
1162  if (AlwaysBasePointer)
1163  return true;
1164 
1165  // If we need to realign the stack, then the stack pointer can no longer
1166  // serve as an offset into the caller's stack space. As a result, we need a
1167  // base pointer.
1168  return needsStackRealignment(MF);
1169 }
1170 
1171 /// Returns true if the instruction's frame index
1172 /// reference would be better served by a base register other than FP
1173 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
1174 /// references it should create new base registers for.
1175 bool PPCRegisterInfo::
1177  assert(Offset < 0 && "Local offset must be negative");
1178 
1179  // It's the load/store FI references that cause issues, as it can be difficult
1180  // to materialize the offset if it won't fit in the literal field. Estimate
1181  // based on the size of the local frame and some conservative assumptions
1182  // about the rest of the stack frame (note, this is pre-regalloc, so
1183  // we don't know everything for certain yet) whether this offset is likely
1184  // to be out of range of the immediate. Return true if so.
1185 
1186  // We only generate virtual base registers for loads and stores that have
1187  // an r+i form. Return false for everything else.
1188  unsigned OpC = MI->getOpcode();
1189  if (!ImmToIdxMap.count(OpC))
1190  return false;
1191 
1192  // Don't generate a new virtual base register just to add zero to it.
1193  if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) &&
1194  MI->getOperand(2).getImm() == 0)
1195  return false;
1196 
1197  MachineBasicBlock &MBB = *MI->getParent();
1198  MachineFunction &MF = *MBB.getParent();
1199  const PPCFrameLowering *TFI = getFrameLowering(MF);
1200  unsigned StackEst = TFI->determineFrameLayout(MF, true);
1201 
1202  // If we likely don't need a stack frame, then we probably don't need a
1203  // virtual base register either.
1204  if (!StackEst)
1205  return false;
1206 
1207  // Estimate an offset from the stack pointer.
1208  // The incoming offset is relating to the SP at the start of the function,
1209  // but when we access the local it'll be relative to the SP after local
1210  // allocation, so adjust our SP-relative offset by that allocation size.
1211  Offset += StackEst;
1212 
1213  // The frame pointer will point to the end of the stack, so estimate the
1214  // offset as the difference between the object offset and the FP location.
1215  return !isFrameOffsetLegal(MI, getBaseRegister(MF), Offset);
1216 }
1217 
1218 /// Insert defining instruction(s) for BaseReg to
1219 /// be a pointer to FrameIdx at the beginning of the basic block.
1220 void PPCRegisterInfo::
1222  unsigned BaseReg, int FrameIdx,
1223  int64_t Offset) const {
1224  unsigned ADDriOpc = TM.isPPC64() ? PPC::ADDI8 : PPC::ADDI;
1225 
1227  DebugLoc DL; // Defaults to "unknown"
1228  if (Ins != MBB->end())
1229  DL = Ins->getDebugLoc();
1230 
1231  const MachineFunction &MF = *MBB->getParent();
1232  const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1233  const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1234  const MCInstrDesc &MCID = TII.get(ADDriOpc);
1236  MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
1237 
1238  BuildMI(*MBB, Ins, DL, MCID, BaseReg)
1239  .addFrameIndex(FrameIdx).addImm(Offset);
1240 }
1241 
1243  int64_t Offset) const {
1244  unsigned FIOperandNum = 0;
1245  while (!MI.getOperand(FIOperandNum).isFI()) {
1246  ++FIOperandNum;
1247  assert(FIOperandNum < MI.getNumOperands() &&
1248  "Instr doesn't have FrameIndex operand!");
1249  }
1250 
1251  MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false);
1252  unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
1253  Offset += MI.getOperand(OffsetOperandNo).getImm();
1254  MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
1255 
1256  MachineBasicBlock &MBB = *MI.getParent();
1257  MachineFunction &MF = *MBB.getParent();
1258  const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1259  const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1260  const MCInstrDesc &MCID = MI.getDesc();
1262  MRI.constrainRegClass(BaseReg,
1263  TII.getRegClass(MCID, FIOperandNum, this, MF));
1264 }
1265 
1267  unsigned BaseReg,
1268  int64_t Offset) const {
1269  unsigned FIOperandNum = 0;
1270  while (!MI->getOperand(FIOperandNum).isFI()) {
1271  ++FIOperandNum;
1272  assert(FIOperandNum < MI->getNumOperands() &&
1273  "Instr doesn't have FrameIndex operand!");
1274  }
1275 
1276  unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum);
1277  Offset += MI->getOperand(OffsetOperandNo).getImm();
1278 
1279  return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm
1280  MI->getOpcode() == TargetOpcode::STACKMAP ||
1281  MI->getOpcode() == TargetOpcode::PATCHPOINT ||
1282  (isInt<16>(Offset) && (Offset % offsetMinAlign(*MI)) == 0);
1283 }
bool isAIXABI() const
Definition: PPCSubtarget.h:315
const uint32_t * getCallPreservedMask(const MachineFunction &MF, CallingConv::ID CC) const override
BitVector getReservedRegs(const MachineFunction &MF) const override
bool isCalleeSavedInfoValid() const
Has the callee saved info been calculated yet?
DILocation * get() const
Get the underlying DILocation.
Definition: DebugLoc.cpp:21
bool isFrameOffsetLegal(const MachineInstr *MI, unsigned BaseReg, int64_t Offset) const override
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:224
bool isAllocatable(unsigned PhysReg) const
isAllocatable - Returns true when PhysReg belongs to an allocatable register class and it hasn&#39;t been...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void ChangeToRegister(unsigned Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isDebug=false)
ChangeToRegister - Replace this operand with a new register operand of the specified value...
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
bool needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const override
Returns true if the instruction&#39;s frame index reference would be better served by a base register oth...
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:385
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:164
The following two target-specific nodes are used for calls through function pointers in the 64-bit SV...
const TargetRegisterClass * getLargestLegalSuperClass(const TargetRegisterClass *RC, const MachineFunction &MF) const override
unsigned Reg
bool hasVSX() const
Definition: PPCSubtarget.h:248
bool isInlineAsm() const
unsigned determineFrameLayout(const MachineFunction &MF, bool UseEstimate=false, unsigned *NewMaxCallFrameSize=nullptr) const
Determine the frame layout but do not update the machine function.
bool test(unsigned Idx) const
Definition: BitVector.h:501
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:323
void lowerCRBitRestore(MachineBasicBlock::iterator II, unsigned FrameIndex) const
constexpr bool isInt< 16 >(int64_t x)
Definition: MathExtras.h:305
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
A debug info location.
Definition: DebugLoc.h:33
static unsigned offsetMinAlignForOpcode(unsigned OpC)
void lowerDynamicAreaOffset(MachineBasicBlock::iterator II) const
#define R2(n)
bool hasSPE() const
Definition: PPCSubtarget.h:245
void lowerCRSpilling(MachineBasicBlock::iterator II, unsigned FrameIndex) const
lowerCRSpilling - Generate the code for spilling a CR register.
void eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj, unsigned FIOperandNum, RegScavenger *RS=nullptr) const override
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
PPCFunctionInfo - This class is derived from MachineFunction private PowerPC target-specific informat...
const HexagonInstrInfo * TII
unsigned getNumOperands() const
Retuns the total number of operands.
Definition: MachineInstr.h:414
static unsigned getOffsetONFromFION(const MachineInstr &MI, unsigned FIOperandNum)
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:411
void lowerCRRestore(MachineBasicBlock::iterator II, unsigned FrameIndex) const
bool hasFP(const MachineFunction &MF) const override
hasFP - Return true if the specified function should have a dedicated frame pointer register...
bool isCallerPreservedPhysReg(unsigned PhysReg, const MachineFunction &MF) const override
unsigned getID() const
Return the register class ID number.
INLINEASM - Represents an inline asm block.
Definition: ISDOpcodes.h:695
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
Definition: MachineInstr.h:408
bool hasP9Vector() const
Definition: PPCSubtarget.h:253
R32 = MFOCRF(CRREG, INFLAG) - Represents the MFOCRF instruction.
void lowerVRSAVESpilling(MachineBasicBlock::iterator II, unsigned FrameIndex) const
virtual const TargetRegisterClass * getLargestLegalSuperClass(const TargetRegisterClass *RC, const MachineFunction &) const
Returns the largest super class of RC that is legal to use in the current sub-target and has the same...
bool hasBasePointer(const MachineFunction &MF) const
reverse_iterator rend()
void adjustStackMapLiveOutMask(uint32_t *Mask) const override
const TargetRegisterClass * constrainRegClass(unsigned Reg, const TargetRegisterClass *RC, unsigned MinNumRegs=0)
constrainRegClass - Constrain the register class of the specified virtual register to be a common sub...
Analysis containing CSE Info
Definition: CSEInfo.cpp:20
This instruction is lowered in PPCRegisterInfo::eliminateFrameIndex to compute an offset from native ...
unsigned getKillRegState(bool B)
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
void ChangeToImmediate(int64_t ImmVal)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value...
TargetInstrInfo - Interface to description of machine instruction set.
const MCPhysReg * getCalleeSavedRegs(const MachineFunction *MF) const override
Code Generation virtual methods...
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:176
void materializeFrameBaseRegister(MachineBasicBlock *MBB, unsigned BaseReg, int FrameIdx, int64_t Offset) const override
Insert defining instruction(s) for BaseReg to be a pointer to FrameIdx at the beginning of the basic ...
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
This file declares the machine register scavenger class.
void lowerVRSAVERestore(MachineBasicBlock::iterator II, unsigned FrameIndex) const
constexpr bool isUInt< 8 >(uint64_t x)
Definition: MathExtras.h:342
unsigned const MachineRegisterInfo * MRI
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
static bool is64Bit(const char *name)
bool requiresFrameIndexScavenging(const MachineFunction &MF) const override
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
static unsigned offsetMinAlign(const MachineInstr &MI)
unsigned getMaxAlignment() const
Return the alignment in bytes that this function must be aligned to, which is greater than the defaul...
static cl::opt< unsigned > MaxCRBitSpillDist("ppc-max-crbit-spill-dist", cl::desc("Maximum search distance for definition of CR bit " "spill on ppc"), cl::Hidden, cl::init(100))
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
bool definesRegister(unsigned Reg, const TargetRegisterInfo *TRI=nullptr) const
Return true if the MachineInstr fully defines the specified register.
Common code between 32-bit and 64-bit PowerPC targets.
const MachineInstrBuilder & addFrameIndex(int Idx) const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
void resolveFrameIndex(MachineInstr &MI, unsigned BaseReg, int64_t Offset) const override
PPCRegisterInfo(const PPCTargetMachine &TM)
static unsigned getCRFromCRBit(unsigned SrcReg)
unsigned getStackAlignment() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
const uint32_t * getNoPreservedMask() const override
bool hasP8Vector() const
Definition: PPCSubtarget.h:250
static const MachineInstrBuilder & addFrameReference(const MachineInstrBuilder &MIB, int FI, int Offset=0, bool mem=true)
addFrameReference - This function is used to add a reference to the base of an abstract object on the...
const MCPhysReg * getCalleeSavedRegsViaCopy(const MachineFunction *MF) const
const PPCRegisterInfo * getRegisterInfo() const override
Definition: PPCSubtarget.h:191
static cl::opt< bool > EnableGPRToVecSpills("ppc-enable-gpr-to-vsr-spills", cl::Hidden, cl::init(false), cl::desc("Enable spills from gpr to vsr rather than stack"))
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
static cl::opt< bool > AlwaysBasePointer("ppc-always-use-base-pointer", cl::Hidden, cl::init(false), cl::desc("Force the use of a base pointer in every function"))
unsigned getRegPressureLimit(const TargetRegisterClass *RC, MachineFunction &MF) const override
unsigned getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call...
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:212
const PPCInstrInfo * getInstrInfo() const override
Definition: PPCSubtarget.h:184
bool isDarwinABI() const
Definition: PPCSubtarget.h:314
Register getBaseRegister(const MachineFunction &MF) const
int64_t getImm() const
const Function & getFunction() const
Return the LLVM function that this machine code represents.
void lowerCRBitSpilling(MachineBasicBlock::iterator II, unsigned FrameIndex) const
Register getFrameRegister(const MachineFunction &MF) const override
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:256
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
Definition: MachineInstr.h:64
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:411
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
bool isPositionIndependent() const
const TargetRegisterClass * getPointerRegClass(const MachineFunction &MF, unsigned Kind=0) const override
getPointerRegClass - Return the register class to use to hold pointers.
#define I(x, y, z)
Definition: MD5.cpp:58
bool hasInlineAsm() const
Returns true if the function contains any inline assembly.
static cl::opt< bool > StackPtrConst("ppc-stack-ptr-caller-preserved", cl::desc("Consider R1 caller preserved so stack saves of " "caller preserved registers can be LICM candidates"), cl::init(true), cl::Hidden)
bool isFI() const
isFI - Tests if this is a MO_FrameIndex operand.
bool hasAltivec() const
Definition: PPCSubtarget.h:244
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:171
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
INLINEASM_BR - Terminator version of inline asm. Used by asm-goto.
Definition: ISDOpcodes.h:698
void lowerDynamicAlloc(MachineBasicBlock::iterator II) const
lowerDynamicAlloc - Generate the code for allocating an object in the current frame.
static cl::opt< bool > EnableBasePointer("ppc-use-base-pointer", cl::Hidden, cl::init(true), cl::desc("Enable use of a base pointer for complex stack frames"))
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
IRTranslator LLVM IR MI
bool isSVR4ABI() const
Definition: PPCSubtarget.h:316
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:416
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects...
Wrapper class representing virtual and physical registers.
Definition: Register.h:18
bool hasReservedSpillSlot(const MachineFunction &MF, unsigned Reg, int &FrameIdx) const override