LLVM 24.0.0git
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"
18#include "PPCSubtarget.h"
19#include "PPCTargetMachine.h"
20#include "llvm/ADT/BitVector.h"
21#include "llvm/ADT/Statistic.h"
31#include "llvm/IR/CallingConv.h"
32#include "llvm/IR/Function.h"
33#include "llvm/IR/Type.h"
35#include "llvm/Support/Debug.h"
41
42using namespace llvm;
43
44#define DEBUG_TYPE "reginfo"
45
46#define GET_REGINFO_TARGET_DESC
47#include "PPCGenRegisterInfo.inc"
48
49STATISTIC(InflateGPRC, "Number of gprc inputs for getLargestLegalClass");
50STATISTIC(InflateGP8RC, "Number of g8rc inputs for getLargestLegalClass");
51
52static cl::opt<bool>
53EnableBasePointer("ppc-use-base-pointer", cl::Hidden, cl::init(true),
54 cl::desc("Enable use of a base pointer for complex stack frames"));
55
56static cl::opt<bool>
57AlwaysBasePointer("ppc-always-use-base-pointer", cl::Hidden, cl::init(false),
58 cl::desc("Force the use of a base pointer in every function"));
59
60static cl::opt<bool>
61EnableGPRToVecSpills("ppc-enable-gpr-to-vsr-spills", cl::Hidden, cl::init(false),
62 cl::desc("Enable spills from gpr to vsr rather than stack"));
63
64static cl::opt<bool>
65StackPtrConst("ppc-stack-ptr-caller-preserved",
66 cl::desc("Consider R1 caller preserved so stack saves of "
67 "caller preserved registers can be LICM candidates"),
68 cl::init(true), cl::Hidden);
69
71MaxCRBitSpillDist("ppc-max-crbit-spill-dist",
72 cl::desc("Maximum search distance for definition of CR bit "
73 "spill on ppc"),
74 cl::Hidden, cl::init(100));
75
76// Copies/moves of physical accumulators are expensive operations
77// that should be avoided whenever possible. MMA instructions are
78// meant to be used in performance-sensitive computational kernels.
79// This option is provided, at least for the time being, to give the
80// user a tool to detect this expensive operation and either rework
81// their code or report a compiler bug if that turns out to be the
82// cause.
83#ifndef NDEBUG
84static cl::opt<bool>
85ReportAccMoves("ppc-report-acc-moves",
86 cl::desc("Emit information about accumulator register spills "
87 "and copies"),
88 cl::Hidden, cl::init(false));
89#endif
90
92
93static unsigned offsetMinAlignForOpcode(unsigned OpC);
94
96 : PPCGenRegisterInfo(TM.isPPC64() ? PPC::LR8 : PPC::LR,
97 TM.isPPC64() ? 0 : 1,
98 TM.isPPC64() ? 0 : 1),
99 TM(TM) {
100 ImmToIdxMap[PPC::LD] = PPC::LDX; ImmToIdxMap[PPC::STD] = PPC::STDX;
101 ImmToIdxMap[PPC::LBZ] = PPC::LBZX; ImmToIdxMap[PPC::STB] = PPC::STBX;
102 ImmToIdxMap[PPC::LHZ] = PPC::LHZX; ImmToIdxMap[PPC::LHA] = PPC::LHAX;
103 ImmToIdxMap[PPC::LWZ] = PPC::LWZX; ImmToIdxMap[PPC::LWA] = PPC::LWAX;
104 ImmToIdxMap[PPC::LFS] = PPC::LFSX; ImmToIdxMap[PPC::LFD] = PPC::LFDX;
105 ImmToIdxMap[PPC::STH] = PPC::STHX; ImmToIdxMap[PPC::STW] = PPC::STWX;
106 ImmToIdxMap[PPC::STFS] = PPC::STFSX; ImmToIdxMap[PPC::STFD] = PPC::STFDX;
107 ImmToIdxMap[PPC::ADDI] = PPC::ADD4;
108 ImmToIdxMap[PPC::LWA_32] = PPC::LWAX_32;
109
110 // 64-bit
111 ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8;
112 ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8;
113 ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8;
114 ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX;
115 ImmToIdxMap[PPC::ADDI8] = PPC::ADD8;
116 ImmToIdxMap[PPC::LQ] = PPC::LQX_PSEUDO;
117 ImmToIdxMap[PPC::STQ] = PPC::STQX_PSEUDO;
118
119 // VSX
120 ImmToIdxMap[PPC::DFLOADf32] = PPC::LXSSPX;
121 ImmToIdxMap[PPC::DFLOADf64] = PPC::LXSDX;
122 ImmToIdxMap[PPC::SPILLTOVSR_LD] = PPC::SPILLTOVSR_LDX;
123 ImmToIdxMap[PPC::SPILLTOVSR_ST] = PPC::SPILLTOVSR_STX;
124 ImmToIdxMap[PPC::DFSTOREf32] = PPC::STXSSPX;
125 ImmToIdxMap[PPC::DFSTOREf64] = PPC::STXSDX;
126 ImmToIdxMap[PPC::LXV] = PPC::LXVX;
127 ImmToIdxMap[PPC::LXSD] = PPC::LXSDX;
128 ImmToIdxMap[PPC::LXSSP] = PPC::LXSSPX;
129 ImmToIdxMap[PPC::STXV] = PPC::STXVX;
130 ImmToIdxMap[PPC::STXSD] = PPC::STXSDX;
131 ImmToIdxMap[PPC::STXSSP] = PPC::STXSSPX;
132
133 // SPE
134 ImmToIdxMap[PPC::EVLDD] = PPC::EVLDDX;
135 ImmToIdxMap[PPC::EVSTDD] = PPC::EVSTDDX;
136 ImmToIdxMap[PPC::SPESTW] = PPC::SPESTWX;
137 ImmToIdxMap[PPC::SPELWZ] = PPC::SPELWZX;
138
139 // Power10
140 ImmToIdxMap[PPC::PLBZ] = PPC::LBZX; ImmToIdxMap[PPC::PLBZ8] = PPC::LBZX8;
141 ImmToIdxMap[PPC::PLHZ] = PPC::LHZX; ImmToIdxMap[PPC::PLHZ8] = PPC::LHZX8;
142 ImmToIdxMap[PPC::PLHA] = PPC::LHAX; ImmToIdxMap[PPC::PLHA8] = PPC::LHAX8;
143 ImmToIdxMap[PPC::PLWZ] = PPC::LWZX; ImmToIdxMap[PPC::PLWZ8] = PPC::LWZX8;
144 ImmToIdxMap[PPC::PLWA] = PPC::LWAX; ImmToIdxMap[PPC::PLWA8] = PPC::LWAX;
145 ImmToIdxMap[PPC::PLD] = PPC::LDX; ImmToIdxMap[PPC::PSTD] = PPC::STDX;
146
147 ImmToIdxMap[PPC::PSTB] = PPC::STBX; ImmToIdxMap[PPC::PSTB8] = PPC::STBX8;
148 ImmToIdxMap[PPC::PSTH] = PPC::STHX; ImmToIdxMap[PPC::PSTH8] = PPC::STHX8;
149 ImmToIdxMap[PPC::PSTW] = PPC::STWX; ImmToIdxMap[PPC::PSTW8] = PPC::STWX8;
150
151 ImmToIdxMap[PPC::PLFS] = PPC::LFSX; ImmToIdxMap[PPC::PSTFS] = PPC::STFSX;
152 ImmToIdxMap[PPC::PLFD] = PPC::LFDX; ImmToIdxMap[PPC::PSTFD] = PPC::STFDX;
153 ImmToIdxMap[PPC::PLXSSP] = PPC::LXSSPX; ImmToIdxMap[PPC::PSTXSSP] = PPC::STXSSPX;
154 ImmToIdxMap[PPC::PLXSD] = PPC::LXSDX; ImmToIdxMap[PPC::PSTXSD] = PPC::STXSDX;
155 ImmToIdxMap[PPC::PLXV] = PPC::LXVX; ImmToIdxMap[PPC::PSTXV] = PPC::STXVX;
156
157 ImmToIdxMap[PPC::LXVP] = PPC::LXVPX;
158 ImmToIdxMap[PPC::STXVP] = PPC::STXVPX;
159 ImmToIdxMap[PPC::PLXVP] = PPC::LXVPX;
160 ImmToIdxMap[PPC::PSTXVP] = PPC::STXVPX;
161}
162
163/// getPointerRegClass - Return the register class to use to hold pointers.
164/// This is used for addressing modes.
167 // Note that PPCInstrInfo::foldImmediate also directly uses this Kind value
168 // when it checks for ZERO folding.
169 if (Kind == 1) {
170 if (TM.isPPC64())
171 return &PPC::G8RC_NOX0RegClass;
172 return &PPC::GPRC_NOR0RegClass;
173 }
174
175 if (TM.isPPC64())
176 return &PPC::G8RCRegClass;
177 return &PPC::GPRCRegClass;
178}
179
180const MCPhysReg*
182 const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>();
184 if (!TM.isPPC64() && Subtarget.isAIXABI())
185 report_fatal_error("AnyReg unimplemented on 32-bit AIX.");
186 if (Subtarget.hasVSX()) {
187 if (Subtarget.pairedVectorMemops())
188 return CSR_64_AllRegs_VSRP_SaveList;
189 if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI())
190 return CSR_64_AllRegs_AIX_Dflt_VSX_SaveList;
191 return CSR_64_AllRegs_VSX_SaveList;
192 }
193 if (Subtarget.hasAltivec()) {
194 if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI())
195 return CSR_64_AllRegs_AIX_Dflt_Altivec_SaveList;
196 return CSR_64_AllRegs_Altivec_SaveList;
197 }
198 return CSR_64_AllRegs_SaveList;
199 }
200
201 // On PPC64, we might need to save r2 (but only if it is not reserved).
202 // We do not need to treat R2 as callee-saved when using PC-Relative calls
203 // because any direct uses of R2 will cause it to be reserved. If the function
204 // is a leaf or the only uses of R2 are implicit uses for calls, the calls
205 // will use the @notoc relocation which will cause this function to set the
206 // st_other bit to 1, thereby communicating to its caller that it arbitrarily
207 // clobbers the TOC.
208 bool SaveR2 = MF->getRegInfo().isAllocatable(PPC::X2) &&
209 !Subtarget.isUsingPCRelativeCalls();
210
211 // Cold calling convention CSRs.
213 if (Subtarget.isAIXABI())
214 report_fatal_error("Cold calling unimplemented on AIX.");
215 if (TM.isPPC64()) {
216 if (Subtarget.pairedVectorMemops())
217 return SaveR2 ? CSR_SVR64_ColdCC_R2_VSRP_SaveList
218 : CSR_SVR64_ColdCC_VSRP_SaveList;
219 if (Subtarget.hasAltivec())
220 return SaveR2 ? CSR_SVR64_ColdCC_R2_Altivec_SaveList
221 : CSR_SVR64_ColdCC_Altivec_SaveList;
222 return SaveR2 ? CSR_SVR64_ColdCC_R2_SaveList
223 : CSR_SVR64_ColdCC_SaveList;
224 }
225 // 32-bit targets.
226 if (Subtarget.pairedVectorMemops())
227 return CSR_SVR32_ColdCC_VSRP_SaveList;
228 else if (Subtarget.hasAltivec())
229 return CSR_SVR32_ColdCC_Altivec_SaveList;
230 else if (Subtarget.hasSPE())
231 return CSR_SVR32_ColdCC_SPE_SaveList;
232 return CSR_SVR32_ColdCC_SaveList;
233 }
234 // Standard calling convention CSRs.
235 if (TM.isPPC64()) {
236 if (Subtarget.pairedVectorMemops()) {
237 if (Subtarget.isAIXABI()) {
238 if (!TM.getAIXExtendedAltivecABI())
239 return SaveR2 ? CSR_PPC64_R2_SaveList : CSR_PPC64_SaveList;
240 return SaveR2 ? CSR_AIX64_R2_VSRP_SaveList : CSR_AIX64_VSRP_SaveList;
241 }
242 return SaveR2 ? CSR_SVR464_R2_VSRP_SaveList : CSR_SVR464_VSRP_SaveList;
243 }
244 if (Subtarget.hasAltivec() &&
245 (!Subtarget.isAIXABI() || TM.getAIXExtendedAltivecABI())) {
246 return SaveR2 ? CSR_PPC64_R2_Altivec_SaveList
247 : CSR_PPC64_Altivec_SaveList;
248 }
249 return SaveR2 ? CSR_PPC64_R2_SaveList : CSR_PPC64_SaveList;
250 }
251 // 32-bit targets.
252 if (Subtarget.isAIXABI()) {
253 if (Subtarget.pairedVectorMemops())
254 return TM.getAIXExtendedAltivecABI() ? CSR_AIX32_VSRP_SaveList
255 : CSR_AIX32_SaveList;
256 if (Subtarget.hasAltivec())
257 return TM.getAIXExtendedAltivecABI() ? CSR_AIX32_Altivec_SaveList
258 : CSR_AIX32_SaveList;
259 return CSR_AIX32_SaveList;
260 }
261 if (Subtarget.pairedVectorMemops())
262 return CSR_SVR432_VSRP_SaveList;
263 if (Subtarget.hasAltivec())
264 return CSR_SVR432_Altivec_SaveList;
265 else if (Subtarget.hasSPE()) {
266 if (TM.isPositionIndependent() && !TM.isPPC64())
267 return CSR_SVR432_SPE_NO_S30_31_SaveList;
268 return CSR_SVR432_SPE_SaveList;
269 }
270 return CSR_SVR432_SaveList;
271}
272
273const uint32_t *
275 CallingConv::ID CC) const {
276 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
277 if (CC == CallingConv::AnyReg) {
278 if (Subtarget.hasVSX()) {
279 if (Subtarget.pairedVectorMemops())
280 return CSR_64_AllRegs_VSRP_RegMask;
281 if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI())
282 return CSR_64_AllRegs_AIX_Dflt_VSX_RegMask;
283 return CSR_64_AllRegs_VSX_RegMask;
284 }
285 if (Subtarget.hasAltivec()) {
286 if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI())
287 return CSR_64_AllRegs_AIX_Dflt_Altivec_RegMask;
288 return CSR_64_AllRegs_Altivec_RegMask;
289 }
290 return CSR_64_AllRegs_RegMask;
291 }
292
293 if (Subtarget.isAIXABI()) {
294 if (Subtarget.pairedVectorMemops()) {
295 if (!TM.getAIXExtendedAltivecABI())
296 return TM.isPPC64() ? CSR_PPC64_RegMask : CSR_AIX32_RegMask;
297 return TM.isPPC64() ? CSR_AIX64_VSRP_RegMask : CSR_AIX32_VSRP_RegMask;
298 }
299 return TM.isPPC64()
300 ? ((Subtarget.hasAltivec() && TM.getAIXExtendedAltivecABI())
301 ? CSR_PPC64_Altivec_RegMask
302 : CSR_PPC64_RegMask)
303 : ((Subtarget.hasAltivec() && TM.getAIXExtendedAltivecABI())
304 ? CSR_AIX32_Altivec_RegMask
305 : CSR_AIX32_RegMask);
306 }
307
308 if (CC == CallingConv::Cold) {
309 if (TM.isPPC64())
310 return Subtarget.pairedVectorMemops()
311 ? CSR_SVR64_ColdCC_VSRP_RegMask
312 : (Subtarget.hasAltivec() ? CSR_SVR64_ColdCC_Altivec_RegMask
313 : CSR_SVR64_ColdCC_RegMask);
314 else
315 return Subtarget.pairedVectorMemops()
316 ? CSR_SVR32_ColdCC_VSRP_RegMask
317 : (Subtarget.hasAltivec()
318 ? CSR_SVR32_ColdCC_Altivec_RegMask
319 : (Subtarget.hasSPE() ? CSR_SVR32_ColdCC_SPE_RegMask
320 : CSR_SVR32_ColdCC_RegMask));
321 }
322
323 if (TM.isPPC64())
324 return Subtarget.pairedVectorMemops()
325 ? CSR_SVR464_VSRP_RegMask
326 : (Subtarget.hasAltivec() ? CSR_PPC64_Altivec_RegMask
327 : CSR_PPC64_RegMask);
328 else
329 return Subtarget.pairedVectorMemops()
330 ? CSR_SVR432_VSRP_RegMask
331 : (Subtarget.hasAltivec()
332 ? CSR_SVR432_Altivec_RegMask
333 : (Subtarget.hasSPE()
334 ? (TM.isPositionIndependent()
335 ? CSR_SVR432_SPE_NO_S30_31_RegMask
336 : CSR_SVR432_SPE_RegMask)
337 : CSR_SVR432_RegMask));
338}
339
340const uint32_t*
342 return CSR_NoRegs_RegMask;
343}
344
346 for (unsigned PseudoReg : {PPC::ZERO, PPC::ZERO8, PPC::RM})
347 Mask[PseudoReg / 32] &= ~(1u << (PseudoReg % 32));
348}
349
351 BitVector Reserved(getNumRegs());
352 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
353 const PPCFrameLowering *TFI = getFrameLowering(MF);
354
355 // The ZERO register is not really a register, but the representation of r0
356 // when used in instructions that treat r0 as the constant 0.
357 markSuperRegs(Reserved, PPC::ZERO);
358
359 // The FP register is also not really a register, but is the representation
360 // of the frame pointer register used by ISD::FRAMEADDR.
361 markSuperRegs(Reserved, PPC::FP);
362
363 // The BP register is also not really a register, but is the representation
364 // of the base pointer register used by setjmp.
365 markSuperRegs(Reserved, PPC::BP);
366
367 // The counter registers must be reserved so that counter-based loops can
368 // be correctly formed (and the mtctr instructions are not DCE'd).
369 markSuperRegs(Reserved, PPC::CTR);
370 markSuperRegs(Reserved, PPC::CTR8);
371
372 markSuperRegs(Reserved, PPC::R1);
373 markSuperRegs(Reserved, PPC::LR);
374 markSuperRegs(Reserved, PPC::LR8);
375 markSuperRegs(Reserved, PPC::RM);
376
377 markSuperRegs(Reserved, PPC::VRSAVE);
378
379 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
380 bool UsesTOCBasePtr = FuncInfo->usesTOCBasePtr();
381 // The SVR4 ABI reserves r2 and r13
382 if (Subtarget.isSVR4ABI() || Subtarget.isAIXABI()) {
383 // We only reserve r2 if we need to use the TOC pointer. If we have no
384 // explicit uses of the TOC pointer (meaning we're a leaf function with
385 // no constant-pool loads, etc.) and we have no potential uses inside an
386 // inline asm block, then we can treat r2 has an ordinary callee-saved
387 // register.
388 if (!TM.isPPC64() || UsesTOCBasePtr || MF.hasInlineAsm())
389 markSuperRegs(Reserved, PPC::R2); // System-reserved register.
390
391 if (Subtarget.isSVR4ABI())
392 markSuperRegs(Reserved, PPC::R13); // Small Data Area pointer register.
393 }
394
395 // On PPC64, r13 is the thread pointer. Never allocate this register.
396 if (TM.isPPC64())
397 markSuperRegs(Reserved, PPC::R13);
398
399 if (TFI->needsFP(MF))
400 markSuperRegs(Reserved, PPC::R31);
401
402 bool IsPositionIndependent = TM.isPositionIndependent();
403 if (hasBasePointer(MF)) {
404 if (Subtarget.is32BitELFABI() && IsPositionIndependent)
405 markSuperRegs(Reserved, PPC::R29);
406 else
407 markSuperRegs(Reserved, PPC::R30);
408 }
409
410 if (Subtarget.is32BitELFABI() && IsPositionIndependent)
411 markSuperRegs(Reserved, PPC::R30);
412
413 // Reserve Altivec registers when Altivec is unavailable.
414 if (!Subtarget.hasAltivec())
415 for (MCRegister Reg : PPC::VRRCRegClass)
416 markSuperRegs(Reserved, Reg);
417
418 if (Subtarget.isAIXABI() && Subtarget.hasAltivec() &&
419 !TM.getAIXExtendedAltivecABI()) {
420 // In the AIX default Altivec ABI, vector registers VR20-VR31 are reserved
421 // and cannot be used.
422 for (auto Reg : CSR_Altivec_SaveList) {
423 if (Reg == 0)
424 break;
425 markSuperRegs(Reserved, Reg);
426 for (MCRegAliasIterator AS(Reg, this, true); AS.isValid(); ++AS) {
427 Reserved.set(*AS);
428 }
429 }
430 }
431
432 assert(checkAllSuperRegsMarked(Reserved));
433 return Reserved;
434}
435
437 MCRegister PhysReg) const {
438 // CTR and LR registers are always reserved, but they are asm clobberable.
439 if (PhysReg == PPC::CTR || PhysReg == PPC::CTR8 || PhysReg == PPC::LR ||
440 PhysReg == PPC::LR8)
441 return true;
442
443 return !getReservedRegs(MF).test(PhysReg);
444}
445
447 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
448 const PPCInstrInfo *InstrInfo = Subtarget.getInstrInfo();
449 const MachineFrameInfo &MFI = MF.getFrameInfo();
450 const std::vector<CalleeSavedInfo> &Info = MFI.getCalleeSavedInfo();
451
452 LLVM_DEBUG(dbgs() << "requiresFrameIndexScavenging for " << MF.getName()
453 << ".\n");
454 // If the callee saved info is invalid we have to default to true for safety.
455 if (!MFI.isCalleeSavedInfoValid()) {
456 LLVM_DEBUG(dbgs() << "TRUE - Invalid callee saved info.\n");
457 return true;
458 }
459
460 // We will require the use of X-Forms because the frame is larger than what
461 // can be represented in signed 16 bits that fit in the immediate of a D-Form.
462 // If we need an X-Form then we need a register to store the address offset.
463 unsigned FrameSize = MFI.getStackSize();
464 // Signed 16 bits means that the FrameSize cannot be more than 15 bits.
465 if (FrameSize & ~0x7FFF) {
466 LLVM_DEBUG(dbgs() << "TRUE - Frame size is too large for D-Form.\n");
467 return true;
468 }
469
470 // The callee saved info is valid so it can be traversed.
471 // Checking for registers that need saving that do not have load or store
472 // forms where the address offset is an immediate.
473 for (const CalleeSavedInfo &CSI : Info) {
474 // If the spill is to a register no scavenging is required.
475 if (CSI.isSpilledToReg())
476 continue;
477
478 int FrIdx = CSI.getFrameIdx();
479 Register Reg = CSI.getReg();
480
481 const TargetRegisterClass *RC = getMinimalPhysRegClass(Reg);
482 unsigned Opcode = InstrInfo->getStoreOpcodeForSpill(RC);
483 if (!MFI.isFixedObjectIndex(FrIdx)) {
484 // This is not a fixed object. If it requires alignment then we may still
485 // need to use the XForm.
486 if (offsetMinAlignForOpcode(Opcode) > 1) {
487 LLVM_DEBUG(dbgs() << "Memory Operand: " << InstrInfo->getName(Opcode)
488 << " for register " << printReg(Reg, this) << ".\n");
489 LLVM_DEBUG(dbgs() << "TRUE - Not fixed frame object that requires "
490 << "alignment.\n");
491 return true;
492 }
493 }
494
495 // This is eiher:
496 // 1) A fixed frame index object which we know are aligned so
497 // as long as we have a valid DForm/DSForm/DQForm (non XForm) we don't
498 // need to consider the alignment here.
499 // 2) A not fixed object but in that case we now know that the min required
500 // alignment is no more than 1 based on the previous check.
501 if (InstrInfo->isXFormMemOp(Opcode)) {
502 LLVM_DEBUG(dbgs() << "Memory Operand: " << InstrInfo->getName(Opcode)
503 << " for register " << printReg(Reg, this) << ".\n");
504 LLVM_DEBUG(dbgs() << "TRUE - Memory operand is X-Form.\n");
505 return true;
506 }
507
508 // This is a spill/restore of a quadword.
509 if ((Opcode == PPC::RESTORE_QUADWORD) || (Opcode == PPC::SPILL_QUADWORD)) {
510 LLVM_DEBUG(dbgs() << "Memory Operand: " << InstrInfo->getName(Opcode)
511 << " for register " << printReg(Reg, this) << ".\n");
512 LLVM_DEBUG(dbgs() << "TRUE - Memory operand is a quadword.\n");
513 return true;
514 }
515 }
516 LLVM_DEBUG(dbgs() << "FALSE - Scavenging is not required.\n");
517 return false;
518}
519
521 const MachineFunction &MF) const {
522 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
523 // Do not use virtual base registers when ROP protection is turned on.
524 // Virtual base registers break the layout of the local variable space and may
525 // push the ROP Hash location past the 512 byte range of the ROP store
526 // instruction.
527 return !Subtarget.hasROPProtect();
528}
529
531 const MachineFunction &MF) const {
532 assert(PhysReg.isPhysical());
533 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
534 const MachineFrameInfo &MFI = MF.getFrameInfo();
535
536 if (!Subtarget.is64BitELFABI() && !Subtarget.isAIXABI())
537 return false;
538 if (PhysReg == Subtarget.getTOCPointerRegister())
539 // X2/R2 is guaranteed to be preserved within a function if it is reserved.
540 // The reason it's reserved is that it's the TOC pointer (and the function
541 // uses the TOC). In functions where it isn't reserved (i.e. leaf functions
542 // with no TOC access), we can't claim that it is preserved.
543 return (getReservedRegs(MF).test(PhysReg));
544 if (StackPtrConst && PhysReg == Subtarget.getStackPointerRegister() &&
545 !MFI.hasVarSizedObjects() && !MFI.hasOpaqueSPAdjustment())
546 // The value of the stack pointer does not change within a function after
547 // the prologue and before the epilogue if there are no dynamic allocations
548 // and no inline asm which clobbers X1/R1.
549 return true;
550 return false;
551}
552
556 const MachineFunction &MF,
557 const VirtRegMap *VRM,
558 const LiveRegMatrix *Matrix) const {
559 const MachineRegisterInfo *MRI = &MF.getRegInfo();
560
561 // Call the base implementation first to set any hints based on the usual
562 // heuristics and decide what the return value should be. We want to return
563 // the same value returned by the base implementation. If the base
564 // implementation decides to return true and force the allocation then we
565 // will leave it as such. On the other hand if the base implementation
566 // decides to return false the following code will not force the allocation
567 // as we are just looking to provide a hint.
568 bool BaseImplRetVal = TargetRegisterInfo::getRegAllocationHints(
569 VirtReg, Order, Hints, MF, VRM, Matrix);
570
571 // Don't use the allocation hints for ISAFuture.
572 // The WACC registers used in ISAFuture are unlike the ACC registers on
573 // Power 10 and so this logic to register allocation hints does not apply.
574 if (MF.getSubtarget<PPCSubtarget>().isISAFuture())
575 return BaseImplRetVal;
576
577 // We are interested in instructions that copy values to ACC/UACC.
578 // The copy into UACC will be simply a COPY to a subreg so we
579 // want to allocate the corresponding physical subreg for the source.
580 // The copy into ACC will be a BUILD_UACC so we want to allocate
581 // the same number UACC for the source.
582 const TargetRegisterClass *RegClass = MRI->getRegClass(VirtReg);
583 for (MachineInstr &Use : MRI->reg_nodbg_instructions(VirtReg)) {
584 const MachineOperand *ResultOp = nullptr;
585 Register ResultReg;
586 switch (Use.getOpcode()) {
587 case TargetOpcode::COPY: {
588 ResultOp = &Use.getOperand(0);
589 ResultReg = ResultOp->getReg();
590 if (ResultReg.isVirtual() &&
591 MRI->getRegClass(ResultReg)->contains(PPC::UACC0) &&
592 VRM->hasPhys(ResultReg)) {
593 Register UACCPhys = VRM->getPhys(ResultReg);
594 Register HintReg;
595 if (RegClass->contains(PPC::VSRp0)) {
596 HintReg = getSubReg(UACCPhys, ResultOp->getSubReg());
597 // Ensure that the hint is a VSRp register.
598 if (HintReg >= PPC::VSRp0 && HintReg <= PPC::VSRp31)
599 Hints.push_back(HintReg);
600 } else if (RegClass->contains(PPC::ACC0)) {
601 HintReg = PPC::ACC0 + (UACCPhys - PPC::UACC0);
602 if (HintReg >= PPC::ACC0 && HintReg <= PPC::ACC7)
603 Hints.push_back(HintReg);
604 }
605 }
606 break;
607 }
608 case PPC::BUILD_UACC: {
609 ResultOp = &Use.getOperand(0);
610 ResultReg = ResultOp->getReg();
611 if (MRI->getRegClass(ResultReg)->contains(PPC::ACC0) &&
612 VRM->hasPhys(ResultReg)) {
613 Register ACCPhys = VRM->getPhys(ResultReg);
614 assert((ACCPhys >= PPC::ACC0 && ACCPhys <= PPC::ACC7) &&
615 "Expecting an ACC register for BUILD_UACC.");
616 Register HintReg = PPC::UACC0 + (ACCPhys - PPC::ACC0);
617 Hints.push_back(HintReg);
618 }
619 break;
620 }
621 }
622 }
623 return BaseImplRetVal;
624}
625
628 if (RC == &PPC::CARRYRCRegClass)
629 return TM.isPPC64() ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
630 return RC;
631}
632
634 MachineFunction &MF) const {
635 const PPCFrameLowering *TFI = getFrameLowering(MF);
636 const unsigned DefaultSafety = 1;
637
638 switch (RC->getID()) {
639 default:
640 return 0;
641 case PPC::G8RC_NOX0RegClassID:
642 case PPC::GPRC_NOR0RegClassID:
643 case PPC::SPERCRegClassID:
644 case PPC::G8RCRegClassID:
645 case PPC::GPRCRegClassID: {
646 unsigned FP = TFI->hasFP(MF) ? 1 : 0;
647 return 32 - FP - DefaultSafety;
648 }
649 case PPC::F4RCRegClassID:
650 case PPC::F8RCRegClassID:
651 case PPC::VSLRCRegClassID:
652 return 32 - DefaultSafety;
653 case PPC::VFRCRegClassID:
654 case PPC::VRRCRegClassID: {
655 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
656 // Vector registers VR20-VR31 are reserved and cannot be used in the default
657 // Altivec ABI on AIX.
658 if (!TM.getAIXExtendedAltivecABI() && Subtarget.isAIXABI())
659 return 20 - DefaultSafety;
660 }
661 return 32 - DefaultSafety;
662 case PPC::VSFRCRegClassID:
663 case PPC::VSSRCRegClassID:
664 case PPC::VSRCRegClassID: {
665 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
666 if (!TM.getAIXExtendedAltivecABI() && Subtarget.isAIXABI())
667 // Vector registers VR20-VR31 are reserved and cannot be used in the
668 // default Altivec ABI on AIX.
669 return 52 - DefaultSafety;
670 }
671 return 64 - DefaultSafety;
672 case PPC::CRRCRegClassID:
673 return 8 - DefaultSafety;
674 }
675}
676
679 const MachineFunction &MF) const {
680 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
681 const auto *DefaultSuperclass =
683 if (Subtarget.hasVSX()) {
684 // With VSX, we can inflate various sub-register classes to the full VSX
685 // register set.
686
687 // For Power9 we allow the user to enable GPR to vector spills.
688 // FIXME: Currently limited to spilling GP8RC. A follow on patch will add
689 // support to spill GPRC.
690 if (TM.isELFv2ABI() || Subtarget.isAIXABI()) {
691 if (Subtarget.hasP9Vector() && EnableGPRToVecSpills &&
692 RC == &PPC::G8RCRegClass) {
693 InflateGP8RC++;
694 return &PPC::SPILLTOVSRRCRegClass;
695 }
696 if (RC == &PPC::GPRCRegClass && EnableGPRToVecSpills)
697 InflateGPRC++;
698 }
699
700 for (unsigned SuperID : RC->superclasses()) {
701 if (getRegSizeInBits(*getRegClass(SuperID)) != getRegSizeInBits(*RC))
702 continue;
703
704 switch (SuperID) {
705 case PPC::VSSRCRegClassID:
706 return Subtarget.hasP8Vector() ? getRegClass(SuperID)
707 : DefaultSuperclass;
708 case PPC::VSFRCRegClassID:
709 case PPC::VSRCRegClassID:
710 return getRegClass(SuperID);
711 case PPC::VSRpRCRegClassID:
712 return Subtarget.pairedVectorMemops() ? getRegClass(SuperID)
713 : DefaultSuperclass;
714 case PPC::ACCRCRegClassID:
715 case PPC::UACCRCRegClassID:
716 return Subtarget.hasMMA() ? getRegClass(SuperID) : DefaultSuperclass;
717 }
718 }
719 }
720
721 return DefaultSuperclass;
722}
723
724//===----------------------------------------------------------------------===//
725// Stack Frame Processing methods
726//===----------------------------------------------------------------------===//
727
728/// lowerDynamicAlloc - Generate the code for allocating an object in the
729/// current frame. The sequence of code will be in the general form
730///
731/// addi R0, SP, \#frameSize ; get the address of the previous frame
732/// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size
733/// addi Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
734///
736 // Get the instruction.
737 MachineInstr &MI = *II;
738 // Get the instruction's basic block.
739 MachineBasicBlock &MBB = *MI.getParent();
740 // Get the basic block's function.
741 MachineFunction &MF = *MBB.getParent();
742 // Get the frame info.
743 MachineFrameInfo &MFI = MF.getFrameInfo();
744 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
745 // Get the instruction info.
746 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
747 // Determine whether 64-bit pointers are used.
748 bool LP64 = TM.isPPC64();
749 DebugLoc dl = MI.getDebugLoc();
750
751 // Get the maximum call stack size.
752 unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
753 Align MaxAlign = MFI.getMaxAlign();
754 assert(isAligned(MaxAlign, maxCallFrameSize) &&
755 "Maximum call-frame size not sufficiently aligned");
756 (void)MaxAlign;
757
758 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
759 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
760 Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
761 bool KillNegSizeReg = MI.getOperand(1).isKill();
762 Register NegSizeReg = MI.getOperand(1).getReg();
763
764 prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, Reg);
765 // Grow the stack and update the stack pointer link, then determine the
766 // address of new allocated space.
767 if (LP64) {
768 BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1)
770 .addReg(PPC::X1)
771 .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
772 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
773 .addReg(PPC::X1)
774 .addImm(maxCallFrameSize);
775 } else {
776 BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1)
778 .addReg(PPC::R1)
779 .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
780 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
781 .addReg(PPC::R1)
782 .addImm(maxCallFrameSize);
783 }
784
785 // Discard the DYNALLOC instruction.
786 MBB.erase(II);
787}
788
789/// To accomplish dynamic stack allocation, we have to calculate exact size
790/// subtracted from the stack pointer according alignment information and get
791/// previous frame pointer.
793 Register &NegSizeReg,
794 bool &KillNegSizeReg,
795 Register &FramePointer) const {
796 // Get the instruction.
797 MachineInstr &MI = *II;
798 // Get the instruction's basic block.
799 MachineBasicBlock &MBB = *MI.getParent();
800 // Get the basic block's function.
801 MachineFunction &MF = *MBB.getParent();
802 // Get the frame info.
803 MachineFrameInfo &MFI = MF.getFrameInfo();
804 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
805 // Get the instruction info.
806 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
807 // Determine whether 64-bit pointers are used.
808 bool LP64 = TM.isPPC64();
809 DebugLoc dl = MI.getDebugLoc();
810 // Get the total frame size.
811 unsigned FrameSize = MFI.getStackSize();
812
813 // Get stack alignments.
814 const PPCFrameLowering *TFI = getFrameLowering(MF);
815 Align TargetAlign = TFI->getStackAlign();
816 Align MaxAlign = MFI.getMaxAlign();
817
818 // Determine the previous frame's address. If FrameSize can't be
819 // represented as 16 bits or we need special alignment, then we load the
820 // previous frame's address from 0(SP). Why not do an addis of the hi?
821 // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
822 // Constructing the constant and adding would take 3 instructions.
823 // Fortunately, a frame greater than 32K is rare.
824 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
825 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
826
827 if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) {
828 if (LP64)
829 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), FramePointer)
830 .addReg(PPC::X31)
831 .addImm(FrameSize);
832 else
833 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), FramePointer)
834 .addReg(PPC::R31)
835 .addImm(FrameSize);
836 } else if (LP64) {
837 BuildMI(MBB, II, dl, TII.get(PPC::LD), FramePointer)
838 .addImm(0)
839 .addReg(PPC::X1);
840 } else {
841 BuildMI(MBB, II, dl, TII.get(PPC::LWZ), FramePointer)
842 .addImm(0)
843 .addReg(PPC::R1);
844 }
845 // Determine the actual NegSizeReg according to alignment info.
846 if (LP64) {
847 if (MaxAlign > TargetAlign) {
848 unsigned UnalNegSizeReg = NegSizeReg;
849 NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
850
851 // Unfortunately, there is no andi, only andi., and we can't insert that
852 // here because we might clobber cr0 while it is live.
853 BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg)
854 .addImm(~(MaxAlign.value() - 1));
855
856 unsigned NegSizeReg1 = NegSizeReg;
857 NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
858 BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg)
859 .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
860 .addReg(NegSizeReg1, RegState::Kill);
861 KillNegSizeReg = true;
862 }
863 } else {
864 if (MaxAlign > TargetAlign) {
865 unsigned UnalNegSizeReg = NegSizeReg;
866 NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
867
868 // Unfortunately, there is no andi, only andi., and we can't insert that
869 // here because we might clobber cr0 while it is live.
870 BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg)
871 .addImm(~(MaxAlign.value() - 1));
872
873 unsigned NegSizeReg1 = NegSizeReg;
874 NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
875 BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg)
876 .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
877 .addReg(NegSizeReg1, RegState::Kill);
878 KillNegSizeReg = true;
879 }
880 }
881}
882
885 MachineInstr &MI = *II;
886 // Get the instruction's basic block.
887 MachineBasicBlock &MBB = *MI.getParent();
888 // Get the basic block's function.
889 MachineFunction &MF = *MBB.getParent();
890 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
891 // Get the instruction info.
892 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
893 // Determine whether 64-bit pointers are used.
894 bool LP64 = TM.isPPC64();
895 DebugLoc dl = MI.getDebugLoc();
896 Register FramePointer = MI.getOperand(0).getReg();
897 const Register ActualNegSizeReg = MI.getOperand(1).getReg();
898 bool KillNegSizeReg = MI.getOperand(2).isKill();
899 Register NegSizeReg = MI.getOperand(2).getReg();
900 const MCInstrDesc &CopyInst = TII.get(LP64 ? PPC::OR8 : PPC::OR);
901 // RegAllocator might allocate FramePointer and NegSizeReg in the same phyreg.
902 if (FramePointer == NegSizeReg) {
903 assert(KillNegSizeReg && "FramePointer is a def and NegSizeReg is an use, "
904 "NegSizeReg should be killed");
905 // FramePointer is clobbered earlier than the use of NegSizeReg in
906 // prepareDynamicAlloca, save NegSizeReg in ActualNegSizeReg to avoid
907 // misuse.
908 BuildMI(MBB, II, dl, CopyInst, ActualNegSizeReg)
909 .addReg(NegSizeReg)
910 .addReg(NegSizeReg);
911 NegSizeReg = ActualNegSizeReg;
912 KillNegSizeReg = false;
913 }
914 prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, FramePointer);
915 // NegSizeReg might be updated in prepareDynamicAlloca if MaxAlign >
916 // TargetAlign.
917 if (NegSizeReg != ActualNegSizeReg)
918 BuildMI(MBB, II, dl, CopyInst, ActualNegSizeReg)
919 .addReg(NegSizeReg)
920 .addReg(NegSizeReg);
921 MBB.erase(II);
922}
923
926 // Get the instruction.
927 MachineInstr &MI = *II;
928 // Get the instruction's basic block.
929 MachineBasicBlock &MBB = *MI.getParent();
930 // Get the basic block's function.
931 MachineFunction &MF = *MBB.getParent();
932 // Get the frame info.
933 MachineFrameInfo &MFI = MF.getFrameInfo();
934 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
935 // Get the instruction info.
936 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
937
938 unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
939 bool is64Bit = TM.isPPC64();
940 DebugLoc dl = MI.getDebugLoc();
941 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI),
942 MI.getOperand(0).getReg())
943 .addImm(maxCallFrameSize);
944 MBB.erase(II);
945}
946
947/// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
948/// reserving a whole register (R0), we scrounge for one here. This generates
949/// code like this:
950///
951/// mfcr rA ; Move the conditional register into GPR rA.
952/// rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
953/// stw rA, FI ; Store rA to the frame.
954///
956 unsigned FrameIndex) const {
957 // Get the instruction.
958 MachineInstr &MI = *II; // ; SPILL_CR <SrcReg>, <offset>
959 // Get the instruction's basic block.
960 MachineBasicBlock &MBB = *MI.getParent();
961 MachineFunction &MF = *MBB.getParent();
962 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
963 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
964 DebugLoc dl = MI.getDebugLoc();
965
966 bool LP64 = TM.isPPC64();
967 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
968 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
969
970 Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
971 Register SrcReg = MI.getOperand(0).getReg();
972
973 // We need to store the CR in the low 4-bits of the saved value. First, issue
974 // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg.
975 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
976 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
977
978 // If the saved register wasn't CR0, shift the bits left so that they are in
979 // CR0's slot.
980 if (SrcReg != PPC::CR0) {
981 Register Reg1 = Reg;
982 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
983
984 // rlwinm rA, rA, ShiftBits, 0, 31.
985 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
986 .addReg(Reg1, RegState::Kill)
987 .addImm(getEncodingValue(SrcReg) * 4)
988 .addImm(0)
989 .addImm(31);
990 }
991
992 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
993 .addReg(Reg, RegState::Kill),
994 FrameIndex);
995
996 // Discard the pseudo instruction.
997 MBB.erase(II);
998}
999
1001 unsigned FrameIndex) const {
1002 // Get the instruction.
1003 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CR <offset>
1004 // Get the instruction's basic block.
1005 MachineBasicBlock &MBB = *MI.getParent();
1006 MachineFunction &MF = *MBB.getParent();
1007 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1008 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1009 DebugLoc dl = MI.getDebugLoc();
1010
1011 bool LP64 = TM.isPPC64();
1012 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1013 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1014
1015 Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
1016 Register DestReg = MI.getOperand(0).getReg();
1017 assert(MI.definesRegister(DestReg, /*TRI=*/nullptr) &&
1018 "RESTORE_CR does not define its destination");
1019
1020 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
1021 Reg), FrameIndex);
1022
1023 // If the reloaded register isn't CR0, shift the bits right so that they are
1024 // in the right CR's slot.
1025 if (DestReg != PPC::CR0) {
1026 Register Reg1 = Reg;
1027 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
1028
1029 unsigned ShiftBits = getEncodingValue(DestReg)*4;
1030 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
1031 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
1032 .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0)
1033 .addImm(31);
1034 }
1035
1036 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg)
1037 .addReg(Reg, RegState::Kill);
1038
1039 // Discard the pseudo instruction.
1040 MBB.erase(II);
1041}
1042
1044 unsigned FrameIndex) const {
1045 // Get the instruction.
1046 MachineInstr &MI = *II; // ; SPILL_CRBIT <SrcReg>, <offset>
1047 // Get the instruction's basic block.
1048 MachineBasicBlock &MBB = *MI.getParent();
1049 MachineFunction &MF = *MBB.getParent();
1050 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1051 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1052 const TargetRegisterInfo* TRI = Subtarget.getRegisterInfo();
1053 DebugLoc dl = MI.getDebugLoc();
1054
1055 bool LP64 = TM.isPPC64();
1056 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1057 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1058
1059 Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
1060 Register SrcReg = MI.getOperand(0).getReg();
1061
1062 // Search up the BB to find the definition of the CR bit.
1065 ++Ins;
1066 unsigned CRBitSpillDistance = 0;
1067 bool SeenUse = false;
1068 for (; Ins != Rend; ++Ins) {
1069 // Definition found.
1070 if (Ins->modifiesRegister(SrcReg, TRI))
1071 break;
1072 // Use found.
1073 if (Ins->readsRegister(SrcReg, TRI))
1074 SeenUse = true;
1075 // Unable to find CR bit definition within maximum search distance.
1076 if (CRBitSpillDistance == MaxCRBitSpillDist) {
1077 Ins = MI;
1078 break;
1079 }
1080 // Skip debug instructions when counting CR bit spill distance.
1081 if (!Ins->isDebugInstr())
1082 CRBitSpillDistance++;
1083 }
1084
1085 // Unable to find the definition of the CR bit in the MBB.
1086 if (Ins == MBB.rend())
1087 Ins = MI;
1088
1089 bool SpillsKnownBit = false;
1090 // There is no need to extract the CR bit if its value is already known.
1091 switch (Ins->getOpcode()) {
1092 case PPC::CRUNSET:
1093 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LI8 : PPC::LI), Reg)
1094 .addImm(0);
1095 SpillsKnownBit = true;
1096 break;
1097 case PPC::CRSET:
1098 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LIS8 : PPC::LIS), Reg)
1099 .addImm(-32768);
1100 SpillsKnownBit = true;
1101 break;
1102 default:
1103 // When spilling a CR bit, the super register may not be explicitly defined
1104 // (i.e. it can be defined by a CR-logical that only defines the subreg) so
1105 // we state that the CR field is undef. Also, in order to preserve the kill
1106 // flag on the CR bit, we add it as an implicit use.
1107
1108 // On Power10, we can use SETNBC to spill all CR bits. SETNBC will set all
1109 // bits (specifically, it produces a -1 if the CR bit is set). Ultimately,
1110 // the bit that is of importance to us is bit 32 (bit 0 of a 32-bit
1111 // register), and SETNBC will set this.
1112 if (Subtarget.isISA3_1()) {
1113 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETNBC8 : PPC::SETNBC), Reg)
1114 .addReg(SrcReg, RegState::Undef)
1115 .addReg(SrcReg, RegState::Implicit |
1116 getKillRegState(MI.getOperand(0).isKill()));
1117 break;
1118 }
1119
1120 // On Power9, we can use SETB to extract the LT bit. This only works for
1121 // the LT bit since SETB produces -1/1/0 for LT/GT/<neither>. So the value
1122 // of the bit we care about (32-bit sign bit) will be set to the value of
1123 // the LT bit (regardless of the other bits in the CR field).
1124 if (Subtarget.isISA3_0()) {
1125 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR1LT ||
1126 SrcReg == PPC::CR2LT || SrcReg == PPC::CR3LT ||
1127 SrcReg == PPC::CR4LT || SrcReg == PPC::CR5LT ||
1128 SrcReg == PPC::CR6LT || SrcReg == PPC::CR7LT) {
1129 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETB8 : PPC::SETB), Reg)
1131 .addReg(SrcReg, RegState::Implicit |
1132 getKillRegState(MI.getOperand(0).isKill()));
1133 break;
1134 }
1135 }
1136
1137 // We need to move the CR field that contains the CR bit we are spilling.
1138 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
1140 .addReg(SrcReg,
1141 RegState::Implicit | getKillRegState(MI.getOperand(0).isKill()));
1142
1143 // If the saved register wasn't CR0LT, shift the bits left so that the bit
1144 // to store is the first one. Mask all but that bit.
1145 Register Reg1 = Reg;
1146 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
1147
1148 // rlwinm rA, rA, ShiftBits, 0, 0.
1149 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
1150 .addReg(Reg1, RegState::Kill)
1151 .addImm(getEncodingValue(SrcReg))
1152 .addImm(0).addImm(0);
1153 }
1154 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
1155 .addReg(Reg, RegState::Kill),
1156 FrameIndex);
1157
1158 bool KillsCRBit = MI.killsRegister(SrcReg, TRI);
1159 // Discard the pseudo instruction.
1160 MBB.erase(II);
1161 if (SpillsKnownBit && KillsCRBit && !SeenUse) {
1162 Ins->setDesc(TII.get(PPC::UNENCODED_NOP));
1163 Ins->removeOperand(0);
1164 }
1165}
1166
1168 unsigned FrameIndex) const {
1169 // Get the instruction.
1170 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CRBIT <offset>
1171 // Get the instruction's basic block.
1172 MachineBasicBlock &MBB = *MI.getParent();
1173 MachineFunction &MF = *MBB.getParent();
1174 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1175 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1176 DebugLoc dl = MI.getDebugLoc();
1177
1178 bool LP64 = TM.isPPC64();
1179 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1180 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1181
1182 Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
1183 Register DestReg = MI.getOperand(0).getReg();
1184 assert(MI.definesRegister(DestReg, /*TRI=*/nullptr) &&
1185 "RESTORE_CRBIT does not define its destination");
1186
1187 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
1188 Reg), FrameIndex);
1189
1190 BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg);
1191
1192 Register RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
1193 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO)
1194 .addReg(getCRFromCRBit(DestReg));
1195
1196 unsigned ShiftBits = getEncodingValue(DestReg);
1197 // rlwimi r11, r10, 32-ShiftBits, ..., ...
1198 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO)
1199 .addReg(RegO, RegState::Kill)
1200 .addReg(Reg, RegState::Kill)
1201 .addImm(ShiftBits ? 32 - ShiftBits : 0)
1202 .addImm(ShiftBits)
1203 .addImm(ShiftBits);
1204
1205 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF),
1206 getCRFromCRBit(DestReg))
1207 .addReg(RegO, RegState::Kill)
1208 // Make sure we have a use dependency all the way through this
1209 // sequence of instructions. We can't have the other bits in the CR
1210 // modified in between the mfocrf and the mtocrf.
1212
1213 // Discard the pseudo instruction.
1214 MBB.erase(II);
1215}
1216
1218 MCRegister DestReg, MCRegister SrcReg) {
1219#ifdef NDEBUG
1220 return;
1221#else
1222 if (ReportAccMoves) {
1223 std::string Dest = PPC::ACCRCRegClass.contains(DestReg) ? "acc" : "uacc";
1224 std::string Src = PPC::ACCRCRegClass.contains(SrcReg) ? "acc" : "uacc";
1225 dbgs() << "Emitting copy from " << Src << " to " << Dest << ":\n";
1226 MBB.dump();
1227 }
1228#endif
1229}
1230
1232 bool IsRestore) {
1233#ifdef NDEBUG
1234 return;
1235#else
1236 if (ReportAccMoves) {
1237 dbgs() << "Emitting " << (IsPrimed ? "acc" : "uacc") << " register "
1238 << (IsRestore ? "restore" : "spill") << ":\n";
1239 MBB.dump();
1240 }
1241#endif
1242}
1243
1244void PPCRegisterInfo::spillRegPair(MachineBasicBlock &MBB,
1246 const TargetInstrInfo &TII,
1247 unsigned FrameIndex, bool IsLittleEndian,
1248 bool IsKilled, Register Reg,
1249 int Offset) const {
1250
1251 // This function does not support virtual registers.
1252 assert(!Reg.isVirtual() &&
1253 "Spilling register pairs does not support virtual registers.");
1254
1256 BuildMI(MBB, II, DL, TII.get(PPC::STXV))
1257 .addReg(TargetRegisterInfo::getSubReg(Reg, PPC::sub_vsx0),
1258 getKillRegState(IsKilled)),
1259 FrameIndex, Offset);
1260
1262 BuildMI(MBB, II, DL, TII.get(PPC::STXV))
1263 .addReg(TargetRegisterInfo::getSubReg(Reg, PPC::sub_vsx1),
1264 getKillRegState(IsKilled)),
1265 FrameIndex, IsLittleEndian ? Offset - 16 : Offset + 16);
1266}
1267
1268/// Remove any STXVP[X] instructions and split them out into a pair of
1269/// STXV[X] instructions if --disable-auto-paired-vec-st is specified on
1270/// the command line.
1272 unsigned FrameIndex) const {
1274 "Expecting to do this only if paired vector stores are disabled.");
1275 MachineInstr &MI = *II; // STXVP <SrcReg>, <offset>
1276 MachineBasicBlock &MBB = *MI.getParent();
1277 MachineFunction &MF = *MBB.getParent();
1278 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1279 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1280 DebugLoc DL = MI.getDebugLoc();
1281 Register SrcReg = MI.getOperand(0).getReg();
1282 bool IsLittleEndian = Subtarget.isLittleEndian();
1283 bool IsKilled = MI.getOperand(0).isKill();
1284
1285 spillRegPair(MBB, II, DL, TII, FrameIndex, IsLittleEndian, IsKilled, SrcReg,
1286 IsLittleEndian ? 16 : 0);
1287
1288 // Discard the original instruction.
1289 MBB.erase(II);
1290}
1291
1292static void emitWAccSpillRestoreInfo(MachineBasicBlock &MBB, bool IsRestore) {
1293#ifdef NDEBUG
1294 return;
1295#else
1296 if (ReportAccMoves) {
1297 dbgs() << "Emitting wacc register " << (IsRestore ? "restore" : "spill")
1298 << ":\n";
1299 MBB.dump();
1300 }
1301#endif
1302}
1303
1304/// lowerACCSpilling - Generate the code for spilling the accumulator register.
1305/// Similarly to other spills/reloads that use pseudo-ops, we do not actually
1306/// eliminate the FrameIndex here nor compute the stack offset. We simply
1307/// create a real instruction with an FI and rely on eliminateFrameIndex to
1308/// handle the FI elimination.
1310 unsigned FrameIndex) const {
1311 MachineInstr &MI = *II; // SPILL_ACC <SrcReg>, <offset>
1312 MachineBasicBlock &MBB = *MI.getParent();
1313 MachineFunction &MF = *MBB.getParent();
1314 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1315 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1316 DebugLoc DL = MI.getDebugLoc();
1317 Register SrcReg = MI.getOperand(0).getReg();
1318 bool IsKilled = MI.getOperand(0).isKill();
1319
1320 bool IsPrimed = PPC::ACCRCRegClass.contains(SrcReg);
1321 bool IsLittleEndian = Subtarget.isLittleEndian();
1322
1323 emitAccSpillRestoreInfo(MBB, IsPrimed, false);
1324
1325 // De-prime the register being spilled, create two stores for the pair
1326 // subregisters accounting for endianness and then re-prime the register if
1327 // it isn't killed. This uses the Offset parameter to addFrameReference() to
1328 // adjust the offset of the store that is within the 64-byte stack slot.
1329 if (IsPrimed)
1330 BuildMI(MBB, II, DL, TII.get(PPC::XXMFACC), SrcReg).addReg(SrcReg);
1332 spillRegPair(MBB, II, DL, TII, FrameIndex, IsLittleEndian, IsKilled,
1333 TargetRegisterInfo::getSubReg(SrcReg, PPC::sub_pair0),
1334 IsLittleEndian ? 48 : 0);
1335 spillRegPair(MBB, II, DL, TII, FrameIndex, IsLittleEndian, IsKilled,
1336 TargetRegisterInfo::getSubReg(SrcReg, PPC::sub_pair1),
1337 IsLittleEndian ? 16 : 32);
1338 } else {
1340 BuildMI(MBB, II, DL, TII.get(PPC::STXVP))
1341 .addReg(TargetRegisterInfo::getSubReg(SrcReg, PPC::sub_pair0),
1342 getKillRegState(IsKilled)),
1343 FrameIndex, IsLittleEndian ? 32 : 0);
1345 BuildMI(MBB, II, DL, TII.get(PPC::STXVP))
1346 .addReg(TargetRegisterInfo::getSubReg(SrcReg, PPC::sub_pair1),
1347 getKillRegState(IsKilled)),
1348 FrameIndex, IsLittleEndian ? 0 : 32);
1349 }
1350 if (IsPrimed && !IsKilled)
1351 BuildMI(MBB, II, DL, TII.get(PPC::XXMTACC), SrcReg).addReg(SrcReg);
1352
1353 // Discard the pseudo instruction.
1354 MBB.erase(II);
1355}
1356
1357/// lowerACCRestore - Generate the code to restore the accumulator register.
1359 unsigned FrameIndex) const {
1360 MachineInstr &MI = *II; // <DestReg> = RESTORE_ACC <offset>
1361 MachineBasicBlock &MBB = *MI.getParent();
1362 MachineFunction &MF = *MBB.getParent();
1363 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1364 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1365 DebugLoc DL = MI.getDebugLoc();
1366
1367 Register DestReg = MI.getOperand(0).getReg();
1368 assert(MI.definesRegister(DestReg, /*TRI=*/nullptr) &&
1369 "RESTORE_ACC does not define its destination");
1370
1371 bool IsPrimed = PPC::ACCRCRegClass.contains(DestReg);
1372 Register Reg =
1373 PPC::VSRp0 + (DestReg - (IsPrimed ? PPC::ACC0 : PPC::UACC0)) * 2;
1374 bool IsLittleEndian = Subtarget.isLittleEndian();
1375
1376 emitAccSpillRestoreInfo(MBB, IsPrimed, true);
1377
1378 // Create two loads for the pair subregisters accounting for endianness and
1379 // then prime the accumulator register being restored.
1380 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), Reg),
1381 FrameIndex, IsLittleEndian ? 32 : 0);
1382 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), Reg + 1),
1383 FrameIndex, IsLittleEndian ? 0 : 32);
1384 if (IsPrimed)
1385 BuildMI(MBB, II, DL, TII.get(PPC::XXMTACC), DestReg).addReg(DestReg);
1386
1387 // Discard the pseudo instruction.
1388 MBB.erase(II);
1389}
1390
1391/// lowerWACCSpilling - Generate the code for spilling the wide accumulator
1392/// register.
1394 unsigned FrameIndex) const {
1395 MachineInstr &MI = *II; // SPILL_WACC <SrcReg>, <offset>
1396 MachineBasicBlock &MBB = *MI.getParent();
1397 MachineFunction &MF = *MBB.getParent();
1398 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1399 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1400 DebugLoc DL = MI.getDebugLoc();
1401 bool IsLittleEndian = Subtarget.isLittleEndian();
1402
1404
1405 const TargetRegisterClass *RC = &PPC::VSRpRCRegClass;
1406 Register VSRpReg0 = MF.getRegInfo().createVirtualRegister(RC);
1407 Register VSRpReg1 = MF.getRegInfo().createVirtualRegister(RC);
1408 Register SrcReg = MI.getOperand(0).getReg();
1409
1410 BuildMI(MBB, II, DL, TII.get(PPC::DMXXEXTFDMR512), VSRpReg0)
1411 .addDef(VSRpReg1)
1412 .addReg(SrcReg);
1413
1414 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP))
1415 .addReg(VSRpReg0, RegState::Kill),
1416 FrameIndex, IsLittleEndian ? 32 : 0);
1417 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP))
1418 .addReg(VSRpReg1, RegState::Kill),
1419 FrameIndex, IsLittleEndian ? 0 : 32);
1420
1421 // Discard the pseudo instruction.
1422 MBB.erase(II);
1423}
1424
1425/// lowerWACCRestore - Generate the code to restore the wide accumulator
1426/// register.
1428 unsigned FrameIndex) const {
1429 MachineInstr &MI = *II; // <DestReg> = RESTORE_WACC <offset>
1430 MachineBasicBlock &MBB = *MI.getParent();
1431 MachineFunction &MF = *MBB.getParent();
1432 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1433 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1434 DebugLoc DL = MI.getDebugLoc();
1435 bool IsLittleEndian = Subtarget.isLittleEndian();
1436
1438
1439 const TargetRegisterClass *RC = &PPC::VSRpRCRegClass;
1440 Register VSRpReg0 = MF.getRegInfo().createVirtualRegister(RC);
1441 Register VSRpReg1 = MF.getRegInfo().createVirtualRegister(RC);
1442 Register DestReg = MI.getOperand(0).getReg();
1443
1444 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), VSRpReg0),
1445 FrameIndex, IsLittleEndian ? 32 : 0);
1446 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), VSRpReg1),
1447 FrameIndex, IsLittleEndian ? 0 : 32);
1448
1449 // Kill VSRpReg0, VSRpReg1 (killedRegState::Killed)
1450 BuildMI(MBB, II, DL, TII.get(PPC::DMXXINSTDMR512), DestReg)
1451 .addReg(VSRpReg0, RegState::Kill)
1452 .addReg(VSRpReg1, RegState::Kill);
1453
1454 // Discard the pseudo instruction.
1455 MBB.erase(II);
1456}
1457
1458/// lowerQuadwordSpilling - Generate code to spill paired general register.
1460 unsigned FrameIndex) const {
1461 MachineInstr &MI = *II;
1462 MachineBasicBlock &MBB = *MI.getParent();
1463 MachineFunction &MF = *MBB.getParent();
1464 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1465 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1466 DebugLoc DL = MI.getDebugLoc();
1467
1468 Register SrcReg = MI.getOperand(0).getReg();
1469 bool IsKilled = MI.getOperand(0).isKill();
1470
1471 Register Reg = PPC::X0 + (SrcReg - PPC::G8p0) * 2;
1472 bool IsLittleEndian = Subtarget.isLittleEndian();
1473
1474 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STD))
1475 .addReg(Reg, getKillRegState(IsKilled)),
1476 FrameIndex, IsLittleEndian ? 8 : 0);
1477 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STD))
1478 .addReg(Reg + 1, getKillRegState(IsKilled)),
1479 FrameIndex, IsLittleEndian ? 0 : 8);
1480
1481 // Discard the pseudo instruction.
1482 MBB.erase(II);
1483}
1484
1485/// lowerQuadwordRestore - Generate code to restore paired general register.
1487 unsigned FrameIndex) const {
1488 MachineInstr &MI = *II;
1489 MachineBasicBlock &MBB = *MI.getParent();
1490 MachineFunction &MF = *MBB.getParent();
1491 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1492 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1493 DebugLoc DL = MI.getDebugLoc();
1494
1495 Register DestReg = MI.getOperand(0).getReg();
1496 assert(MI.definesRegister(DestReg, /*TRI=*/nullptr) &&
1497 "RESTORE_QUADWORD does not define its destination");
1498
1499 Register Reg = PPC::X0 + (DestReg - PPC::G8p0) * 2;
1500 bool IsLittleEndian = Subtarget.isLittleEndian();
1501
1502 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LD), Reg), FrameIndex,
1503 IsLittleEndian ? 8 : 0);
1504 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LD), Reg + 1), FrameIndex,
1505 IsLittleEndian ? 0 : 8);
1506
1507 // Discard the pseudo instruction.
1508 MBB.erase(II);
1509}
1510
1511/// lowerDMRSpilling - Generate the code for spilling the DMR register.
1513 unsigned FrameIndex) const {
1514 MachineInstr &MI = *II; // SPILL_DMR <SrcReg>, <offset>
1515 MachineBasicBlock &MBB = *MI.getParent();
1516 MachineFunction &MF = *MBB.getParent();
1517 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1518 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1519 DebugLoc DL = MI.getDebugLoc();
1520 bool IsLittleEndian = Subtarget.isLittleEndian();
1521
1522 // DMR is made up of WACC and WACC_HI, so DMXXEXTFDMR512 to spill
1523 // the corresponding 512 bits.
1524 const TargetRegisterClass *RC = &PPC::VSRpRCRegClass;
1525 auto spillDMR = [&](Register SrcReg, int BEIdx, int LEIdx) {
1526 auto spillWACC = [&](unsigned Opc, unsigned RegIdx, int IdxBE, int IdxLE) {
1527 Register VSRpReg0 = MF.getRegInfo().createVirtualRegister(RC);
1528 Register VSRpReg1 = MF.getRegInfo().createVirtualRegister(RC);
1529
1530 BuildMI(MBB, II, DL, TII.get(Opc), VSRpReg0)
1531 .addDef(VSRpReg1)
1532 .addReg(TargetRegisterInfo::getSubReg(SrcReg, RegIdx));
1533
1534 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP))
1535 .addReg(VSRpReg0, RegState::Kill),
1536 FrameIndex, IsLittleEndian ? IdxLE : IdxBE);
1537 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP))
1538 .addReg(VSRpReg1, RegState::Kill),
1539 FrameIndex, IsLittleEndian ? IdxLE - 32 : IdxBE + 32);
1540 };
1541 spillWACC(PPC::DMXXEXTFDMR512, PPC::sub_wacc_lo, BEIdx, LEIdx);
1542 spillWACC(PPC::DMXXEXTFDMR512_HI, PPC::sub_wacc_hi, BEIdx + 64, LEIdx - 64);
1543 };
1544
1545 Register SrcReg = MI.getOperand(0).getReg();
1546 if (MI.getOpcode() == PPC::SPILL_DMRP) {
1547 spillDMR(TargetRegisterInfo::getSubReg(SrcReg, PPC::sub_dmr1), 0, 96);
1548 spillDMR(TargetRegisterInfo::getSubReg(SrcReg, PPC::sub_dmr0), 128, 224);
1549 } else
1550 spillDMR(SrcReg, 0, 96);
1551
1552 // Discard the pseudo instruction.
1553 MBB.erase(II);
1554}
1555
1556/// lowerDMRRestore - Generate the code to restore the DMR register.
1558 unsigned FrameIndex) const {
1559 MachineInstr &MI = *II; // <DestReg> = RESTORE_DMR[P] <offset>
1560 MachineBasicBlock &MBB = *MI.getParent();
1561 MachineFunction &MF = *MBB.getParent();
1562 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1563 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1564 DebugLoc DL = MI.getDebugLoc();
1565 bool IsLittleEndian = Subtarget.isLittleEndian();
1566
1567 const TargetRegisterClass *RC = &PPC::VSRpRCRegClass;
1568 auto restoreDMR = [&](Register DestReg, int BEIdx, int LEIdx) {
1569 auto restoreWACC = [&](unsigned Opc, unsigned RegIdx, int IdxBE,
1570 int IdxLE) {
1571 Register VSRpReg0 = MF.getRegInfo().createVirtualRegister(RC);
1572 Register VSRpReg1 = MF.getRegInfo().createVirtualRegister(RC);
1573
1574 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), VSRpReg0),
1575 FrameIndex, IsLittleEndian ? IdxLE : IdxBE);
1576 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), VSRpReg1),
1577 FrameIndex, IsLittleEndian ? IdxLE - 32 : IdxBE + 32);
1578
1579 // Kill virtual registers (killedRegState::Killed).
1580 BuildMI(MBB, II, DL, TII.get(Opc),
1581 TargetRegisterInfo::getSubReg(DestReg, RegIdx))
1582 .addReg(VSRpReg0, RegState::Kill)
1583 .addReg(VSRpReg1, RegState::Kill);
1584 };
1585 restoreWACC(PPC::DMXXINSTDMR512, PPC::sub_wacc_lo, BEIdx, LEIdx);
1586 restoreWACC(PPC::DMXXINSTDMR512_HI, PPC::sub_wacc_hi, BEIdx + 64,
1587 LEIdx - 64);
1588 };
1589
1590 Register DestReg = MI.getOperand(0).getReg();
1591 if (MI.getOpcode() == PPC::RESTORE_DMRP) {
1592 restoreDMR(TargetRegisterInfo::getSubReg(DestReg, PPC::sub_dmr1), 0, 96);
1593 restoreDMR(TargetRegisterInfo::getSubReg(DestReg, PPC::sub_dmr0), 128, 224);
1594 } else
1595 restoreDMR(DestReg, 0, 96);
1596
1597 // Discard the pseudo instruction.
1598 MBB.erase(II);
1599}
1600
1602 Register Reg, int &FrameIdx) const {
1603 // For the nonvolatile condition registers (CR2, CR3, CR4) return true to
1604 // prevent allocating an additional frame slot.
1605 // For 64-bit ELF and AIX, the CR save area is in the linkage area at SP+8,
1606 // for 32-bit AIX the CR save area is in the linkage area at SP+4.
1607 // We have created a FrameIndex to that spill slot to keep the CalleSaveInfos
1608 // valid.
1609 // For 32-bit ELF, we have previously created the stack slot if needed, so
1610 // return its FrameIdx.
1611 if (PPC::CR2 <= Reg && Reg <= PPC::CR4) {
1612 FrameIdx = MF.getInfo<PPCFunctionInfo>()->getCRSpillFrameIndex();
1613 return true;
1614 }
1615 return false;
1616}
1617
1618// If the offset must be a multiple of some value, return what that value is.
1619static unsigned offsetMinAlignForOpcode(unsigned OpC) {
1620 switch (OpC) {
1621 default:
1622 return 1;
1623 case PPC::LWA:
1624 case PPC::LWA_32:
1625 case PPC::LD:
1626 case PPC::LDU:
1627 case PPC::STD:
1628 case PPC::STDU:
1629 case PPC::DFLOADf32:
1630 case PPC::DFLOADf64:
1631 case PPC::DFSTOREf32:
1632 case PPC::DFSTOREf64:
1633 case PPC::LXSD:
1634 case PPC::LXSSP:
1635 case PPC::STXSD:
1636 case PPC::STXSSP:
1637 case PPC::STQ:
1638 return 4;
1639 case PPC::EVLDD:
1640 case PPC::EVSTDD:
1641 return 8;
1642 case PPC::LXV:
1643 case PPC::STXV:
1644 case PPC::LQ:
1645 case PPC::LXVP:
1646 case PPC::STXVP:
1647 return 16;
1648 }
1649}
1650
1651// If the offset must be a multiple of some value, return what that value is.
1652static unsigned offsetMinAlign(const MachineInstr &MI) {
1653 unsigned OpC = MI.getOpcode();
1654 return offsetMinAlignForOpcode(OpC);
1655}
1656
1657// Return the OffsetOperandNo given the FIOperandNum (and the instruction).
1658static unsigned getOffsetONFromFION(const MachineInstr &MI,
1659 unsigned FIOperandNum) {
1660 // Take into account whether it's an add or mem instruction
1661 unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2;
1662 if (MI.isInlineAsm())
1663 OffsetOperandNo = FIOperandNum - 1;
1664 else if (MI.getOpcode() == TargetOpcode::STACKMAP ||
1665 MI.getOpcode() == TargetOpcode::PATCHPOINT)
1666 OffsetOperandNo = FIOperandNum + 1;
1667
1668 return OffsetOperandNo;
1669}
1670
1671bool
1673 int SPAdj, unsigned FIOperandNum,
1674 RegScavenger *RS) const {
1675 assert(SPAdj == 0 && "Unexpected");
1676
1677 // Get the instruction.
1678 MachineInstr &MI = *II;
1679 // Get the instruction's basic block.
1680 MachineBasicBlock &MBB = *MI.getParent();
1681 // Get the basic block's function.
1682 MachineFunction &MF = *MBB.getParent();
1683 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1684 // Get the instruction info.
1685 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
1686 // Get the frame info.
1687 MachineFrameInfo &MFI = MF.getFrameInfo();
1688 DebugLoc dl = MI.getDebugLoc();
1689
1690 unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
1691
1692 // Get the frame index.
1693 int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
1694
1695 // Get the frame pointer save index. Users of this index are primarily
1696 // DYNALLOC instructions.
1698 int FPSI = FI->getFramePointerSaveIndex();
1699 // Get the instruction opcode.
1700 unsigned OpC = MI.getOpcode();
1701
1702 switch (OpC) {
1703 default:
1704 break;
1705 case PPC::DYNAREAOFFSET:
1706 case PPC::DYNAREAOFFSET8:
1708 // lowerDynamicAreaOffset erases II
1709 return true;
1710 case PPC::DYNALLOC:
1711 case PPC::DYNALLOC8: {
1712 // Special case for dynamic alloca.
1713 if (FPSI && FrameIndex == FPSI) {
1714 lowerDynamicAlloc(II); // lowerDynamicAlloc erases II
1715 return true;
1716 }
1717 break;
1718 }
1719 case PPC::PREPARE_PROBED_ALLOCA_64:
1720 case PPC::PREPARE_PROBED_ALLOCA_32:
1721 case PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_64:
1722 case PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_32: {
1723 if (FPSI && FrameIndex == FPSI) {
1724 lowerPrepareProbedAlloca(II); // lowerPrepareProbedAlloca erases II
1725 return true;
1726 }
1727 break;
1728 }
1729 case PPC::SPILL_CR:
1730 // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc.
1731 lowerCRSpilling(II, FrameIndex);
1732 return true;
1733 case PPC::RESTORE_CR:
1734 lowerCRRestore(II, FrameIndex);
1735 return true;
1736 case PPC::SPILL_CRBIT:
1737 lowerCRBitSpilling(II, FrameIndex);
1738 return true;
1739 case PPC::RESTORE_CRBIT:
1740 lowerCRBitRestore(II, FrameIndex);
1741 return true;
1742 case PPC::SPILL_ACC:
1743 case PPC::SPILL_UACC:
1744 lowerACCSpilling(II, FrameIndex);
1745 return true;
1746 case PPC::RESTORE_ACC:
1747 case PPC::RESTORE_UACC:
1748 lowerACCRestore(II, FrameIndex);
1749 return true;
1750 case PPC::STXVP: {
1752 lowerOctWordSpilling(II, FrameIndex);
1753 return true;
1754 }
1755 break;
1756 }
1757 case PPC::SPILL_WACC:
1758 lowerWACCSpilling(II, FrameIndex);
1759 return true;
1760 case PPC::RESTORE_WACC:
1761 lowerWACCRestore(II, FrameIndex);
1762 return true;
1763 case PPC::SPILL_DMRP:
1764 case PPC::SPILL_DMR:
1765 lowerDMRSpilling(II, FrameIndex);
1766 return true;
1767 case PPC::RESTORE_DMRP:
1768 case PPC::RESTORE_DMR:
1769 lowerDMRRestore(II, FrameIndex);
1770 return true;
1771 case PPC::SPILL_QUADWORD:
1772 lowerQuadwordSpilling(II, FrameIndex);
1773 return true;
1774 case PPC::RESTORE_QUADWORD:
1775 lowerQuadwordRestore(II, FrameIndex);
1776 return true;
1777 }
1778
1779 // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
1780 MI.getOperand(FIOperandNum).ChangeToRegister(
1781 FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false);
1782
1783 // If the instruction is not present in ImmToIdxMap, then it has no immediate
1784 // form (and must be r+r).
1785 bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP &&
1786 OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC);
1787
1788 // Now add the frame object offset to the offset from r1.
1789 int64_t Offset = MFI.getObjectOffset(FrameIndex);
1790 Offset += MI.getOperand(OffsetOperandNo).getImm();
1791
1792 // If we're not using a Frame Pointer that has been set to the value of the
1793 // SP before having the stack size subtracted from it, then add the stack size
1794 // to Offset to get the correct offset.
1795 // Naked functions have stack size 0, although getStackSize may not reflect
1796 // that because we didn't call all the pieces that compute it for naked
1797 // functions.
1798 if (!MF.getFunction().hasFnAttribute(Attribute::Naked)) {
1799 if (!(hasBasePointer(MF) && FrameIndex < 0))
1800 Offset += MFI.getStackSize();
1801 }
1802
1803 // If we encounter an LXVP/STXVP with an offset that doesn't fit, we can
1804 // transform it to the prefixed version so we don't have to use the XForm.
1805 if ((OpC == PPC::LXVP || OpC == PPC::STXVP) &&
1806 (!isInt<16>(Offset) || (Offset % offsetMinAlign(MI)) != 0) &&
1807 Subtarget.hasPrefixInstrs() && Subtarget.hasP10Vector()) {
1808 unsigned NewOpc = OpC == PPC::LXVP ? PPC::PLXVP : PPC::PSTXVP;
1809 MI.setDesc(TII.get(NewOpc));
1810 OpC = NewOpc;
1811 }
1812
1813 // If we can, encode the offset directly into the instruction. If this is a
1814 // normal PPC "ri" instruction, any 16-bit value can be safely encoded. If
1815 // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
1816 // clear can be encoded. This is extremely uncommon, because normally you
1817 // only "std" to a stack slot that is at least 4-byte aligned, but it can
1818 // happen in invalid code.
1819 assert(OpC != PPC::DBG_VALUE &&
1820 "This should be handled in a target-independent way");
1821 // FIXME: This should be factored out to a separate function as prefixed
1822 // instructions add a number of opcodes for which we can use 34-bit imm.
1823 bool OffsetFitsMnemonic = (OpC == PPC::EVSTDD || OpC == PPC::EVLDD) ?
1824 isUInt<8>(Offset) :
1826 if (TII.isPrefixed(MI.getOpcode()))
1827 OffsetFitsMnemonic = isInt<34>(Offset);
1828 if (!noImmForm && ((OffsetFitsMnemonic &&
1829 ((Offset % offsetMinAlign(MI)) == 0)) ||
1830 OpC == TargetOpcode::STACKMAP ||
1831 OpC == TargetOpcode::PATCHPOINT)) {
1832 MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
1833 return false;
1834 }
1835
1836 // The offset doesn't fit into a single register, scavenge one to build the
1837 // offset in.
1838
1839 bool is64Bit = TM.isPPC64();
1840 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1841 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1842 const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC;
1843 unsigned NewOpcode = 0u;
1844 bool ScavengingFailed = RS && RS->getRegsAvailable(RC).none() &&
1845 RS->getRegsAvailable(&PPC::VSFRCRegClass).any();
1846 Register SRegHi, SReg, VSReg;
1847
1848 // The register scavenger is unable to get a GPR but can get a VSR. We
1849 // need to stash a GPR into a VSR so that we can free one up.
1850 if (ScavengingFailed && Subtarget.hasDirectMove()) {
1851 // Pick a volatile register and if we are spilling/restoring that
1852 // particular one, pick the next one.
1853 SRegHi = SReg = is64Bit ? PPC::X4 : PPC::R4;
1854 if (MI.getOperand(0).getReg() == SReg)
1855 SRegHi = SReg = SReg + 1;
1856 VSReg = MF.getRegInfo().createVirtualRegister(&PPC::VSFRCRegClass);
1857 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::MTVSRD : PPC::MTVSRWZ), VSReg)
1858 .addReg(SReg);
1859 } else {
1860 SRegHi = MF.getRegInfo().createVirtualRegister(RC);
1861 SReg = MF.getRegInfo().createVirtualRegister(RC);
1862 }
1863
1864 // Insert a set of rA with the full offset value before the ld, st, or add
1865 if (isInt<16>(Offset))
1866 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), SReg)
1867 .addImm(Offset);
1868 else if (isInt<32>(Offset)) {
1869 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi)
1870 .addImm(Offset >> 16);
1871 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg)
1872 .addReg(SRegHi, RegState::Kill)
1873 .addImm(Offset);
1874 } else {
1875 assert(is64Bit && "Huge stack is only supported on PPC64");
1876 TII.materializeImmPostRA(MBB, II, dl, SReg, Offset);
1877 }
1878
1879 // Convert into indexed form of the instruction:
1880 //
1881 // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
1882 // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
1883 unsigned OperandBase;
1884
1885 if (noImmForm)
1886 OperandBase = 1;
1887 else if (OpC != TargetOpcode::INLINEASM &&
1888 OpC != TargetOpcode::INLINEASM_BR) {
1889 assert(ImmToIdxMap.count(OpC) &&
1890 "No indexed form of load or store available!");
1891 NewOpcode = ImmToIdxMap.find(OpC)->second;
1892 MI.setDesc(TII.get(NewOpcode));
1893 OperandBase = 1;
1894 } else {
1895 OperandBase = OffsetOperandNo;
1896 }
1897
1898 Register StackReg = MI.getOperand(FIOperandNum).getReg();
1899 MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
1900 MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true);
1901
1902 // If we stashed a value from a GPR into a VSR, we need to get it back after
1903 // spilling the register.
1904 if (ScavengingFailed && Subtarget.hasDirectMove())
1905 BuildMI(MBB, ++II, dl, TII.get(is64Bit ? PPC::MFVSRD : PPC::MFVSRWZ), SReg)
1906 .addReg(VSReg);
1907
1908 // Since these are not real X-Form instructions, we must
1909 // add the registers and access 0(NewReg) rather than
1910 // emitting the X-Form pseudo.
1911 if (NewOpcode == PPC::LQX_PSEUDO || NewOpcode == PPC::STQX_PSEUDO) {
1912 assert(is64Bit && "Quadword loads/stores only supported in 64-bit mode");
1913 Register NewReg = MF.getRegInfo().createVirtualRegister(&PPC::G8RCRegClass);
1914 BuildMI(MBB, II, dl, TII.get(PPC::ADD8), NewReg)
1915 .addReg(SReg, RegState::Kill)
1916 .addReg(StackReg);
1917 MI.setDesc(TII.get(NewOpcode == PPC::LQX_PSEUDO ? PPC::LQ : PPC::STQ));
1918 MI.getOperand(OperandBase + 1).ChangeToRegister(NewReg, false);
1919 MI.getOperand(OperandBase).ChangeToImmediate(0);
1920 }
1921 return false;
1922}
1923
1925 const PPCFrameLowering *TFI = getFrameLowering(MF);
1926
1927 if (!TM.isPPC64())
1928 return TFI->hasFP(MF) ? PPC::R31 : PPC::R1;
1929 else
1930 return TFI->hasFP(MF) ? PPC::X31 : PPC::X1;
1931}
1932
1934 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1935 if (!hasBasePointer(MF))
1936 return getFrameRegister(MF);
1937
1938 if (TM.isPPC64())
1939 return PPC::X30;
1940
1941 if (Subtarget.isSVR4ABI() && TM.isPositionIndependent())
1942 return PPC::R29;
1943
1944 return PPC::R30;
1945}
1946
1948 if (!EnableBasePointer)
1949 return false;
1951 return true;
1952
1953 // If we need to realign the stack, then the stack pointer can no longer
1954 // serve as an offset into the caller's stack space. As a result, we need a
1955 // base pointer.
1956 return hasStackRealignment(MF);
1957}
1958
1959/// Returns true if the instruction's frame index
1960/// reference would be better served by a base register other than FP
1961/// or SP. Used by LocalStackFrameAllocation to determine which frame index
1962/// references it should create new base registers for.
1964needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
1965 assert(Offset < 0 && "Local offset must be negative");
1966
1967 // It's the load/store FI references that cause issues, as it can be difficult
1968 // to materialize the offset if it won't fit in the literal field. Estimate
1969 // based on the size of the local frame and some conservative assumptions
1970 // about the rest of the stack frame (note, this is pre-regalloc, so
1971 // we don't know everything for certain yet) whether this offset is likely
1972 // to be out of range of the immediate. Return true if so.
1973
1974 // We only generate virtual base registers for loads and stores that have
1975 // an r+i form. Return false for everything else.
1976 unsigned OpC = MI->getOpcode();
1977 if (!ImmToIdxMap.count(OpC))
1978 return false;
1979
1980 // Don't generate a new virtual base register just to add zero to it.
1981 if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) &&
1982 MI->getOperand(2).getImm() == 0)
1983 return false;
1984
1985 MachineBasicBlock &MBB = *MI->getParent();
1986 MachineFunction &MF = *MBB.getParent();
1987 const PPCFrameLowering *TFI = getFrameLowering(MF);
1988 unsigned StackEst = TFI->determineFrameLayout(MF, true);
1989
1990 // If we likely don't need a stack frame, then we probably don't need a
1991 // virtual base register either.
1992 if (!StackEst)
1993 return false;
1994
1995 // Estimate an offset from the stack pointer.
1996 // The incoming offset is relating to the SP at the start of the function,
1997 // but when we access the local it'll be relative to the SP after local
1998 // allocation, so adjust our SP-relative offset by that allocation size.
1999 Offset += StackEst;
2000
2001 // The frame pointer will point to the end of the stack, so estimate the
2002 // offset as the difference between the object offset and the FP location.
2004}
2005
2006/// Insert defining instruction(s) for BaseReg to
2007/// be a pointer to FrameIdx at the beginning of the basic block.
2009 int FrameIdx,
2010 int64_t Offset) const {
2011 unsigned ADDriOpc = TM.isPPC64() ? PPC::ADDI8 : PPC::ADDI;
2012
2013 MachineBasicBlock::iterator Ins = MBB->begin();
2014 DebugLoc DL; // Defaults to "unknown"
2015 if (Ins != MBB->end())
2016 DL = Ins->getDebugLoc();
2017
2018 const MachineFunction &MF = *MBB->getParent();
2019 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
2020 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
2021 const MCInstrDesc &MCID = TII.get(ADDriOpc);
2022 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
2024 Register BaseReg = MRI.createVirtualRegister(RC);
2025 MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0));
2026
2027 BuildMI(*MBB, Ins, DL, MCID, BaseReg)
2028 .addFrameIndex(FrameIdx).addImm(Offset);
2029
2030 return BaseReg;
2031}
2032
2034 int64_t Offset) const {
2035 unsigned FIOperandNum = 0;
2036 while (!MI.getOperand(FIOperandNum).isFI()) {
2037 ++FIOperandNum;
2038 assert(FIOperandNum < MI.getNumOperands() &&
2039 "Instr doesn't have FrameIndex operand!");
2040 }
2041
2042 MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false);
2043 unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
2044 Offset += MI.getOperand(OffsetOperandNo).getImm();
2045 MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
2046
2047 MachineBasicBlock &MBB = *MI.getParent();
2048 MachineFunction &MF = *MBB.getParent();
2049 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
2050 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
2051 const MCInstrDesc &MCID = MI.getDesc();
2052 MachineRegisterInfo &MRI = MF.getRegInfo();
2053 MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, FIOperandNum));
2054}
2055
2057 Register BaseReg,
2058 int64_t Offset) const {
2059 unsigned FIOperandNum = 0;
2060 while (!MI->getOperand(FIOperandNum).isFI()) {
2061 ++FIOperandNum;
2062 assert(FIOperandNum < MI->getNumOperands() &&
2063 "Instr doesn't have FrameIndex operand!");
2064 }
2065
2066 unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum);
2067 Offset += MI->getOperand(OffsetOperandNo).getImm();
2068
2069 return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm
2070 MI->getOpcode() == TargetOpcode::STACKMAP ||
2071 MI->getOpcode() == TargetOpcode::PATCHPOINT ||
2072 (isInt<16>(Offset) && (Offset % offsetMinAlign(*MI)) == 0);
2073}
static const TargetRegisterClass * getRegClass(const MachineInstr &MI, Register Reg)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file implements the BitVector class.
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Live Register Matrix
static cl::opt< bool > EnableBasePointer("m68k-use-base-pointer", cl::Hidden, cl::init(true), cl::desc("Enable use of a base pointer for complex stack frames"))
Register Reg
Register const TargetRegisterInfo * TRI
modulo schedule test
uint64_t IntrinsicInst * II
cl::opt< bool > DisableAutoPairedVecSt("disable-auto-paired-vec-st", cl::desc("disable automatically generated 32byte paired vector stores"), cl::init(true), cl::Hidden)
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))
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"))
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"))
static cl::opt< bool > ReportAccMoves("ppc-report-acc-moves", cl::desc("Emit information about accumulator register spills " "and copies"), cl::Hidden, cl::init(false))
static void emitWAccSpillRestoreInfo(MachineBasicBlock &MBB, bool IsRestore)
static unsigned getOffsetONFromFION(const MachineInstr &MI, unsigned FIOperandNum)
static unsigned offsetMinAlignForOpcode(unsigned OpC)
static void emitAccSpillRestoreInfo(MachineBasicBlock &MBB, bool IsPrimed, bool IsRestore)
static unsigned offsetMinAlign(const MachineInstr &MI)
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)
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"))
This file declares the machine register scavenger class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
static bool is64Bit(const char *name)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
bool test(unsigned Idx) const
Returns true if bit Idx is set.
Definition BitVector.h:482
The CalleeSavedInfo class tracks the information need to locate where a callee saved register is in t...
A debug info location.
Definition DebugLoc.h:126
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:272
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:727
Describe properties that are true of each instruction in the target description file.
MCRegAliasIterator enumerates all registers aliasing Reg.
ArrayRef< unsigned > superclasses() const
Returns a list of super-classes.
unsigned getID() const
getID() - Return the register class ID number.
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
MCRegister getSubReg(MCRegister Reg, unsigned Idx) const
Returns the physical register number of sub-register "Index" for physical register RegNo.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition MCRegister.h:72
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
MachineInstrBundleIterator< MachineInstr > iterator
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
Align getMaxAlign() const
Return alignment of this function's frame.
uint64_t getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call.
bool isCalleeSavedInfoValid() const
Has the callee saved info been calculated yet?
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
bool hasInlineAsm() const
Returns true if the function contains any inline assembly.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addFrameIndex(int Idx) const
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
bool isAllocatable(MCRegister PhysReg) const
isAllocatable - Returns true when PhysReg belongs to an allocatable register class and it hasn't been...
LLVM_ABI const TargetRegisterClass * constrainRegClass(Register Reg, const TargetRegisterClass *RC, unsigned MinNumRegs=0)
constrainRegClass - Constrain the register class of the specified virtual register to be a common sub...
iterator_range< reg_instr_nodbg_iterator > reg_nodbg_instructions(Register Reg) const
uint64_t determineFrameLayout(const MachineFunction &MF, bool UseEstimate=false, unsigned *NewMaxCallFrameSize=nullptr) const
Determine the frame layout but do not update the machine function.
PPCFunctionInfo - This class is derived from MachineFunction private PowerPC target-specific informat...
void resolveFrameIndex(MachineInstr &MI, Register BaseReg, int64_t Offset) const override
bool getRegAllocationHints(Register VirtReg, ArrayRef< MCPhysReg > Order, SmallVectorImpl< MCPhysReg > &Hints, const MachineFunction &MF, const VirtRegMap *VRM, const LiveRegMatrix *Matrix) const override
Register getFrameRegister(const MachineFunction &MF) const override
bool hasBasePointer(const MachineFunction &MF) const
Register getBaseRegister(const MachineFunction &MF) const
void lowerDMRRestore(MachineBasicBlock::iterator II, unsigned FrameIndex) const
lowerDMRRestore - Generate the code to restore the DMR register.
void prepareDynamicAlloca(MachineBasicBlock::iterator II, Register &NegSizeReg, bool &KillNegSizeReg, Register &FramePointer) const
To accomplish dynamic stack allocation, we have to calculate exact size subtracted from the stack poi...
void lowerCRBitSpilling(MachineBasicBlock::iterator II, unsigned FrameIndex) const
void lowerACCSpilling(MachineBasicBlock::iterator II, unsigned FrameIndex) const
lowerACCSpilling - Generate the code for spilling the accumulator register.
bool requiresFrameIndexScavenging(const MachineFunction &MF) const override
void lowerCRSpilling(MachineBasicBlock::iterator II, unsigned FrameIndex) const
lowerCRSpilling - Generate the code for spilling a CR register.
const TargetRegisterClass * getPointerRegClass(unsigned Kind=0) const override
getPointerRegClass - Return the register class to use to hold pointers.
void lowerDynamicAreaOffset(MachineBasicBlock::iterator II) const
void lowerDynamicAlloc(MachineBasicBlock::iterator II) const
lowerDynamicAlloc - Generate the code for allocating an object in the current frame.
const uint32_t * getCallPreservedMask(const MachineFunction &MF, CallingConv::ID CC) const override
void adjustStackMapLiveOutMask(uint32_t *Mask) const override
bool hasReservedSpillSlot(const MachineFunction &MF, Register Reg, int &FrameIdx) const override
bool isCallerPreservedPhysReg(MCRegister PhysReg, const MachineFunction &MF) const override
bool needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const override
Returns true if the instruction's frame index reference would be better served by a base register oth...
const TargetRegisterClass * getCrossCopyRegClass(const TargetRegisterClass *RC) const override
const uint32_t * getNoPreservedMask() const override
void lowerDMRSpilling(MachineBasicBlock::iterator II, unsigned FrameIndex) const
lowerDMRSpilling - Generate the code for spilling the DMR register.
void lowerCRRestore(MachineBasicBlock::iterator II, unsigned FrameIndex) const
bool eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj, unsigned FIOperandNum, RegScavenger *RS=nullptr) const override
void lowerQuadwordRestore(MachineBasicBlock::iterator II, unsigned FrameIndex) const
lowerQuadwordRestore - Generate code to restore paired general register.
static void emitAccCopyInfo(MachineBasicBlock &MBB, MCRegister DestReg, MCRegister SrcReg)
bool requiresVirtualBaseRegisters(const MachineFunction &MF) const override
void lowerCRBitRestore(MachineBasicBlock::iterator II, unsigned FrameIndex) const
const MCPhysReg * getCalleeSavedRegs(const MachineFunction *MF) const override
Code Generation virtual methods...
Register materializeFrameBaseRegister(MachineBasicBlock *MBB, 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 ...
unsigned getRegPressureLimit(const TargetRegisterClass *RC, MachineFunction &MF) const override
void lowerWACCRestore(MachineBasicBlock::iterator II, unsigned FrameIndex) const
lowerWACCRestore - Generate the code to restore the wide accumulator register.
void lowerPrepareProbedAlloca(MachineBasicBlock::iterator II) const
void lowerQuadwordSpilling(MachineBasicBlock::iterator II, unsigned FrameIndex) const
lowerQuadwordSpilling - Generate code to spill paired general register.
PPCRegisterInfo(const PPCTargetMachine &TM)
bool isFrameOffsetLegal(const MachineInstr *MI, Register BaseReg, int64_t Offset) const override
void lowerWACCSpilling(MachineBasicBlock::iterator II, unsigned FrameIndex) const
lowerWACCSpilling - Generate the code for spilling the wide accumulator register.
void lowerOctWordSpilling(MachineBasicBlock::iterator II, unsigned FrameIndex) const
Remove any STXVP[X] instructions and split them out into a pair of STXV[X] instructions if –disable-a...
bool isAsmClobberable(const MachineFunction &MF, MCRegister PhysReg) const override
BitVector getReservedRegs(const MachineFunction &MF) const override
const TargetRegisterClass * getLargestLegalSuperClass(const TargetRegisterClass *RC, const MachineFunction &MF) const override
void lowerACCRestore(MachineBasicBlock::iterator II, unsigned FrameIndex) const
lowerACCRestore - Generate the code to restore the accumulator register.
bool is32BitELFABI() const
bool isAIXABI() const
bool isUsingPCRelativeCalls() const
const PPCInstrInfo * getInstrInfo() const override
bool isSVR4ABI() const
bool isLittleEndian() const
MCRegister getTOCPointerRegister() const
MCRegister getStackPointerRegister() const
bool is64BitELFABI() const
const PPCRegisterInfo * getRegisterInfo() const override
Common code between 32-bit and 64-bit PowerPC targets.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
bool hasFP(const MachineFunction &MF) const
hasFP - Return true if the specified function should have a dedicated frame pointer register.
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
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...
virtual bool getRegAllocationHints(Register VirtReg, ArrayRef< MCPhysReg > Order, SmallVectorImpl< MCPhysReg > &Hints, const MachineFunction &MF, const VirtRegMap *VRM=nullptr, const LiveRegMatrix *Matrix=nullptr) const
Get a list of 'hint' registers that the register allocator should try first when allocating a physica...
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
MCRegister getPhys(Register virtReg) const
returns the physical register mapped to the specified virtual register
Definition VirtRegMap.h:91
bool hasPhys(Register virtReg) const
returns true if the specified virtual register is mapped to a physical register
Definition VirtRegMap.h:87
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ AnyReg
OBSOLETED - Used for stack based JavaScript calls.
Definition CallingConv.h:60
@ Cold
Attempts to make code in the caller as efficient as possible under the assumption that the call is no...
Definition CallingConv.h:47
Define some predicates that are used for node matching.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Kill
The last use of a register.
@ Undef
Value of the register doesn't matter.
constexpr RegState getKillRegState(bool B)
bool isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
Definition Alignment.h:134
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...
static unsigned getCRFromCRBit(unsigned SrcReg)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77