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