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