1 //===-- PPCRegisterInfo.cpp - PowerPC Register Information ----------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains the PowerPC implementation of the TargetRegisterInfo
12 //===----------------------------------------------------------------------===//
14 #include "PPCRegisterInfo.h"
15 #include "PPCFrameLowering.h"
16 #include "PPCInstrBuilder.h"
17 #include "PPCMachineFunctionInfo.h"
18 #include "PPCSubtarget.h"
19 #include "PPCTargetMachine.h"
20 #include "llvm/ADT/BitVector.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/MachineModuleInfo.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/RegisterScavenging.h"
29 #include "llvm/CodeGen/TargetFrameLowering.h"
30 #include "llvm/CodeGen/TargetInstrInfo.h"
31 #include "llvm/IR/CallingConv.h"
32 #include "llvm/IR/Constants.h"
33 #include "llvm/IR/Function.h"
34 #include "llvm/IR/Type.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/MathExtras.h"
39 #include "llvm/Support/raw_ostream.h"
40 #include "llvm/Target/TargetMachine.h"
41 #include "llvm/Target/TargetOptions.h"
46 #define DEBUG_TYPE "reginfo"
48 #define GET_REGINFO_TARGET_DESC
49 #include "PPCGenRegisterInfo.inc"
51 STATISTIC(InflateGPRC
, "Number of gprc inputs for getLargestLegalClass");
52 STATISTIC(InflateGP8RC
, "Number of g8rc inputs for getLargestLegalClass");
55 EnableBasePointer("ppc-use-base-pointer", cl::Hidden
, cl::init(true),
56 cl::desc("Enable use of a base pointer for complex stack frames"));
59 AlwaysBasePointer("ppc-always-use-base-pointer", cl::Hidden
, cl::init(false),
60 cl::desc("Force the use of a base pointer in every function"));
63 EnableGPRToVecSpills("ppc-enable-gpr-to-vsr-spills", cl::Hidden
, cl::init(false),
64 cl::desc("Enable spills from gpr to vsr rather than stack"));
67 StackPtrConst("ppc-stack-ptr-caller-preserved",
68 cl::desc("Consider R1 caller preserved so stack saves of "
69 "caller preserved registers can be LICM candidates"),
70 cl::init(true), cl::Hidden
);
72 static cl::opt
<unsigned>
73 MaxCRBitSpillDist("ppc-max-crbit-spill-dist",
74 cl::desc("Maximum search distance for definition of CR bit "
76 cl::Hidden
, cl::init(100));
78 static unsigned offsetMinAlignForOpcode(unsigned OpC
);
80 PPCRegisterInfo::PPCRegisterInfo(const PPCTargetMachine
&TM
)
81 : PPCGenRegisterInfo(TM
.isPPC64() ? PPC::LR8
: PPC::LR
,
83 TM
.isPPC64() ? 0 : 1),
85 ImmToIdxMap
[PPC::LD
] = PPC::LDX
; ImmToIdxMap
[PPC::STD
] = PPC::STDX
;
86 ImmToIdxMap
[PPC::LBZ
] = PPC::LBZX
; ImmToIdxMap
[PPC::STB
] = PPC::STBX
;
87 ImmToIdxMap
[PPC::LHZ
] = PPC::LHZX
; ImmToIdxMap
[PPC::LHA
] = PPC::LHAX
;
88 ImmToIdxMap
[PPC::LWZ
] = PPC::LWZX
; ImmToIdxMap
[PPC::LWA
] = PPC::LWAX
;
89 ImmToIdxMap
[PPC::LFS
] = PPC::LFSX
; ImmToIdxMap
[PPC::LFD
] = PPC::LFDX
;
90 ImmToIdxMap
[PPC::STH
] = PPC::STHX
; ImmToIdxMap
[PPC::STW
] = PPC::STWX
;
91 ImmToIdxMap
[PPC::STFS
] = PPC::STFSX
; ImmToIdxMap
[PPC::STFD
] = PPC::STFDX
;
92 ImmToIdxMap
[PPC::ADDI
] = PPC::ADD4
;
93 ImmToIdxMap
[PPC::LWA_32
] = PPC::LWAX_32
;
96 ImmToIdxMap
[PPC::LHA8
] = PPC::LHAX8
; ImmToIdxMap
[PPC::LBZ8
] = PPC::LBZX8
;
97 ImmToIdxMap
[PPC::LHZ8
] = PPC::LHZX8
; ImmToIdxMap
[PPC::LWZ8
] = PPC::LWZX8
;
98 ImmToIdxMap
[PPC::STB8
] = PPC::STBX8
; ImmToIdxMap
[PPC::STH8
] = PPC::STHX8
;
99 ImmToIdxMap
[PPC::STW8
] = PPC::STWX8
; ImmToIdxMap
[PPC::STDU
] = PPC::STDUX
;
100 ImmToIdxMap
[PPC::ADDI8
] = PPC::ADD8
;
103 ImmToIdxMap
[PPC::DFLOADf32
] = PPC::LXSSPX
;
104 ImmToIdxMap
[PPC::DFLOADf64
] = PPC::LXSDX
;
105 ImmToIdxMap
[PPC::SPILLTOVSR_LD
] = PPC::SPILLTOVSR_LDX
;
106 ImmToIdxMap
[PPC::SPILLTOVSR_ST
] = PPC::SPILLTOVSR_STX
;
107 ImmToIdxMap
[PPC::DFSTOREf32
] = PPC::STXSSPX
;
108 ImmToIdxMap
[PPC::DFSTOREf64
] = PPC::STXSDX
;
109 ImmToIdxMap
[PPC::LXV
] = PPC::LXVX
;
110 ImmToIdxMap
[PPC::LXSD
] = PPC::LXSDX
;
111 ImmToIdxMap
[PPC::LXSSP
] = PPC::LXSSPX
;
112 ImmToIdxMap
[PPC::STXV
] = PPC::STXVX
;
113 ImmToIdxMap
[PPC::STXSD
] = PPC::STXSDX
;
114 ImmToIdxMap
[PPC::STXSSP
] = PPC::STXSSPX
;
117 ImmToIdxMap
[PPC::EVLDD
] = PPC::EVLDDX
;
118 ImmToIdxMap
[PPC::EVSTDD
] = PPC::EVSTDDX
;
119 ImmToIdxMap
[PPC::SPESTW
] = PPC::SPESTWX
;
120 ImmToIdxMap
[PPC::SPELWZ
] = PPC::SPELWZX
;
123 /// getPointerRegClass - Return the register class to use to hold pointers.
124 /// This is used for addressing modes.
125 const TargetRegisterClass
*
126 PPCRegisterInfo::getPointerRegClass(const MachineFunction
&MF
, unsigned Kind
)
128 // Note that PPCInstrInfo::FoldImmediate also directly uses this Kind value
129 // when it checks for ZERO folding.
132 return &PPC::G8RC_NOX0RegClass
;
133 return &PPC::GPRC_NOR0RegClass
;
137 return &PPC::G8RCRegClass
;
138 return &PPC::GPRCRegClass
;
142 PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction
*MF
) const {
143 const PPCSubtarget
&Subtarget
= MF
->getSubtarget
<PPCSubtarget
>();
144 if (MF
->getFunction().getCallingConv() == CallingConv::AnyReg
) {
145 if (Subtarget
.hasVSX())
146 return CSR_64_AllRegs_VSX_SaveList
;
147 if (Subtarget
.hasAltivec())
148 return CSR_64_AllRegs_Altivec_SaveList
;
149 return CSR_64_AllRegs_SaveList
;
152 if (Subtarget
.isDarwinABI())
154 ? (Subtarget
.hasAltivec() ? CSR_Darwin64_Altivec_SaveList
155 : CSR_Darwin64_SaveList
)
156 : (Subtarget
.hasAltivec() ? CSR_Darwin32_Altivec_SaveList
157 : CSR_Darwin32_SaveList
);
159 if (TM
.isPPC64() && MF
->getInfo
<PPCFunctionInfo
>()->isSplitCSR())
160 return CSR_SRV464_TLS_PE_SaveList
;
162 // On PPC64, we might need to save r2 (but only if it is not reserved).
163 bool SaveR2
= MF
->getRegInfo().isAllocatable(PPC::X2
);
165 // Cold calling convention CSRs.
166 if (MF
->getFunction().getCallingConv() == CallingConv::Cold
) {
168 if (Subtarget
.hasAltivec())
169 return SaveR2
? CSR_SVR64_ColdCC_R2_Altivec_SaveList
170 : CSR_SVR64_ColdCC_Altivec_SaveList
;
171 return SaveR2
? CSR_SVR64_ColdCC_R2_SaveList
172 : CSR_SVR64_ColdCC_SaveList
;
175 if (Subtarget
.hasAltivec())
176 return CSR_SVR32_ColdCC_Altivec_SaveList
;
177 else if (Subtarget
.hasSPE())
178 return CSR_SVR32_ColdCC_SPE_SaveList
;
179 return CSR_SVR32_ColdCC_SaveList
;
181 // Standard calling convention CSRs.
183 if (Subtarget
.hasAltivec())
184 return SaveR2
? CSR_SVR464_R2_Altivec_SaveList
185 : CSR_SVR464_Altivec_SaveList
;
186 return SaveR2
? CSR_SVR464_R2_SaveList
187 : CSR_SVR464_SaveList
;
190 if (Subtarget
.hasAltivec())
191 return CSR_SVR432_Altivec_SaveList
;
192 else if (Subtarget
.hasSPE())
193 return CSR_SVR432_SPE_SaveList
;
194 return CSR_SVR432_SaveList
;
198 PPCRegisterInfo::getCalleeSavedRegsViaCopy(const MachineFunction
*MF
) const {
199 assert(MF
&& "Invalid MachineFunction pointer.");
200 const PPCSubtarget
&Subtarget
= MF
->getSubtarget
<PPCSubtarget
>();
201 if (Subtarget
.isDarwinABI())
205 if (MF
->getFunction().getCallingConv() != CallingConv::CXX_FAST_TLS
)
207 if (!MF
->getInfo
<PPCFunctionInfo
>()->isSplitCSR())
210 // On PPC64, we might need to save r2 (but only if it is not reserved).
211 bool SaveR2
= !getReservedRegs(*MF
).test(PPC::X2
);
212 if (Subtarget
.hasAltivec())
214 ? CSR_SVR464_R2_Altivec_ViaCopy_SaveList
215 : CSR_SVR464_Altivec_ViaCopy_SaveList
;
218 ? CSR_SVR464_R2_ViaCopy_SaveList
219 : CSR_SVR464_ViaCopy_SaveList
;
223 PPCRegisterInfo::getCallPreservedMask(const MachineFunction
&MF
,
224 CallingConv::ID CC
) const {
225 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
226 if (CC
== CallingConv::AnyReg
) {
227 if (Subtarget
.hasVSX())
228 return CSR_64_AllRegs_VSX_RegMask
;
229 if (Subtarget
.hasAltivec())
230 return CSR_64_AllRegs_Altivec_RegMask
;
231 return CSR_64_AllRegs_RegMask
;
234 if (Subtarget
.isDarwinABI())
235 return TM
.isPPC64() ? (Subtarget
.hasAltivec() ? CSR_Darwin64_Altivec_RegMask
236 : CSR_Darwin64_RegMask
)
237 : (Subtarget
.hasAltivec() ? CSR_Darwin32_Altivec_RegMask
238 : CSR_Darwin32_RegMask
);
239 if (Subtarget
.isAIXABI()) {
240 assert(!Subtarget
.hasAltivec() && "Altivec is not implemented on AIX yet.");
241 return TM
.isPPC64() ? CSR_AIX64_RegMask
: CSR_AIX32_RegMask
;
244 if (CC
== CallingConv::Cold
) {
245 return TM
.isPPC64() ? (Subtarget
.hasAltivec() ? CSR_SVR64_ColdCC_Altivec_RegMask
246 : CSR_SVR64_ColdCC_RegMask
)
247 : (Subtarget
.hasAltivec() ? CSR_SVR32_ColdCC_Altivec_RegMask
248 : (Subtarget
.hasSPE()
249 ? CSR_SVR32_ColdCC_SPE_RegMask
250 : CSR_SVR32_ColdCC_RegMask
));
253 return TM
.isPPC64() ? (Subtarget
.hasAltivec() ? CSR_SVR464_Altivec_RegMask
254 : CSR_SVR464_RegMask
)
255 : (Subtarget
.hasAltivec() ? CSR_SVR432_Altivec_RegMask
256 : (Subtarget
.hasSPE()
257 ? CSR_SVR432_SPE_RegMask
258 : CSR_SVR432_RegMask
));
262 PPCRegisterInfo::getNoPreservedMask() const {
263 return CSR_NoRegs_RegMask
;
266 void PPCRegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask
) const {
267 for (unsigned PseudoReg
: {PPC::ZERO
, PPC::ZERO8
, PPC::RM
})
268 Mask
[PseudoReg
/ 32] &= ~(1u << (PseudoReg
% 32));
271 BitVector
PPCRegisterInfo::getReservedRegs(const MachineFunction
&MF
) const {
272 BitVector
Reserved(getNumRegs());
273 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
274 const PPCFrameLowering
*TFI
= getFrameLowering(MF
);
276 // The ZERO register is not really a register, but the representation of r0
277 // when used in instructions that treat r0 as the constant 0.
278 markSuperRegs(Reserved
, PPC::ZERO
);
280 // The FP register is also not really a register, but is the representation
281 // of the frame pointer register used by ISD::FRAMEADDR.
282 markSuperRegs(Reserved
, PPC::FP
);
284 // The BP register is also not really a register, but is the representation
285 // of the base pointer register used by setjmp.
286 markSuperRegs(Reserved
, PPC::BP
);
288 // The counter registers must be reserved so that counter-based loops can
289 // be correctly formed (and the mtctr instructions are not DCE'd).
290 markSuperRegs(Reserved
, PPC::CTR
);
291 markSuperRegs(Reserved
, PPC::CTR8
);
293 markSuperRegs(Reserved
, PPC::R1
);
294 markSuperRegs(Reserved
, PPC::LR
);
295 markSuperRegs(Reserved
, PPC::LR8
);
296 markSuperRegs(Reserved
, PPC::RM
);
298 if (!Subtarget
.isDarwinABI() || !Subtarget
.hasAltivec())
299 markSuperRegs(Reserved
, PPC::VRSAVE
);
301 // The SVR4 ABI reserves r2 and r13
302 if (Subtarget
.isSVR4ABI()) {
303 // We only reserve r2 if we need to use the TOC pointer. If we have no
304 // explicit uses of the TOC pointer (meaning we're a leaf function with
305 // no constant-pool loads, etc.) and we have no potential uses inside an
306 // inline asm block, then we can treat r2 has an ordinary callee-saved
308 const PPCFunctionInfo
*FuncInfo
= MF
.getInfo
<PPCFunctionInfo
>();
309 if (!TM
.isPPC64() || FuncInfo
->usesTOCBasePtr() || MF
.hasInlineAsm())
310 markSuperRegs(Reserved
, PPC::R2
); // System-reserved register
311 markSuperRegs(Reserved
, PPC::R13
); // Small Data Area pointer register
314 // Always reserve r2 on AIX for now.
315 // TODO: Make r2 allocatable on AIX/XCOFF for some leaf functions.
316 if (Subtarget
.isAIXABI())
317 markSuperRegs(Reserved
, PPC::R2
); // System-reserved register
319 // On PPC64, r13 is the thread pointer. Never allocate this register.
321 markSuperRegs(Reserved
, PPC::R13
);
323 if (TFI
->needsFP(MF
))
324 markSuperRegs(Reserved
, PPC::R31
);
326 bool IsPositionIndependent
= TM
.isPositionIndependent();
327 if (hasBasePointer(MF
)) {
328 if (Subtarget
.is32BitELFABI() && IsPositionIndependent
)
329 markSuperRegs(Reserved
, PPC::R29
);
331 markSuperRegs(Reserved
, PPC::R30
);
334 if (Subtarget
.is32BitELFABI() && IsPositionIndependent
)
335 markSuperRegs(Reserved
, PPC::R30
);
337 // Reserve Altivec registers when Altivec is unavailable.
338 if (!Subtarget
.hasAltivec())
339 for (TargetRegisterClass::iterator I
= PPC::VRRCRegClass
.begin(),
340 IE
= PPC::VRRCRegClass
.end(); I
!= IE
; ++I
)
341 markSuperRegs(Reserved
, *I
);
343 assert(checkAllSuperRegsMarked(Reserved
));
347 bool PPCRegisterInfo::requiresFrameIndexScavenging(const MachineFunction
&MF
) const {
348 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
349 const PPCInstrInfo
*InstrInfo
= Subtarget
.getInstrInfo();
350 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
351 const std::vector
<CalleeSavedInfo
> &Info
= MFI
.getCalleeSavedInfo();
353 // If the callee saved info is invalid we have to default to true for safety.
354 if (!MFI
.isCalleeSavedInfoValid())
357 // We will require the use of X-Forms because the frame is larger than what
358 // can be represented in signed 16 bits that fit in the immediate of a D-Form.
359 // If we need an X-Form then we need a register to store the address offset.
360 unsigned FrameSize
= MFI
.getStackSize();
361 // Signed 16 bits means that the FrameSize cannot be more than 15 bits.
362 if (FrameSize
& ~0x7FFF)
365 // The callee saved info is valid so it can be traversed.
366 // Checking for registers that need saving that do not have load or store
367 // forms where the address offset is an immediate.
368 for (unsigned i
= 0; i
< Info
.size(); i
++) {
369 int FrIdx
= Info
[i
].getFrameIdx();
370 unsigned Reg
= Info
[i
].getReg();
372 unsigned Opcode
= InstrInfo
->getStoreOpcodeForSpill(Reg
);
373 if (!MFI
.isFixedObjectIndex(FrIdx
)) {
374 // This is not a fixed object. If it requires alignment then we may still
375 // need to use the XForm.
376 if (offsetMinAlignForOpcode(Opcode
) > 1)
381 // 1) A fixed frame index object which we know are aligned so
382 // as long as we have a valid DForm/DSForm/DQForm (non XForm) we don't
383 // need to consider the alignement here.
384 // 2) A not fixed object but in that case we now know that the min required
385 // alignment is no more than 1 based on the previous check.
386 if (InstrInfo
->isXFormMemOp(Opcode
))
392 bool PPCRegisterInfo::isCallerPreservedPhysReg(unsigned PhysReg
,
393 const MachineFunction
&MF
) const {
394 assert(Register::isPhysicalRegister(PhysReg
));
395 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
396 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
400 if (!Subtarget
.isSVR4ABI())
402 if (PhysReg
== PPC::X2
)
403 // X2 is guaranteed to be preserved within a function if it is reserved.
404 // The reason it's reserved is that it's the TOC pointer (and the function
405 // uses the TOC). In functions where it isn't reserved (i.e. leaf functions
406 // with no TOC access), we can't claim that it is preserved.
407 return (getReservedRegs(MF
).test(PPC::X2
));
408 if (StackPtrConst
&& (PhysReg
== PPC::X1
) && !MFI
.hasVarSizedObjects()
409 && !MFI
.hasOpaqueSPAdjustment())
410 // The value of the stack pointer does not change within a function after
411 // the prologue and before the epilogue if there are no dynamic allocations
412 // and no inline asm which clobbers X1.
417 unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass
*RC
,
418 MachineFunction
&MF
) const {
419 const PPCFrameLowering
*TFI
= getFrameLowering(MF
);
420 const unsigned DefaultSafety
= 1;
422 switch (RC
->getID()) {
425 case PPC::G8RC_NOX0RegClassID
:
426 case PPC::GPRC_NOR0RegClassID
:
427 case PPC::SPERCRegClassID
:
428 case PPC::SPE4RCRegClassID
:
429 case PPC::G8RCRegClassID
:
430 case PPC::GPRCRegClassID
: {
431 unsigned FP
= TFI
->hasFP(MF
) ? 1 : 0;
432 return 32 - FP
- DefaultSafety
;
434 case PPC::F8RCRegClassID
:
435 case PPC::F4RCRegClassID
:
436 case PPC::QFRCRegClassID
:
437 case PPC::QSRCRegClassID
:
438 case PPC::QBRCRegClassID
:
439 case PPC::VRRCRegClassID
:
440 case PPC::VFRCRegClassID
:
441 case PPC::VSLRCRegClassID
:
442 return 32 - DefaultSafety
;
443 case PPC::VSRCRegClassID
:
444 case PPC::VSFRCRegClassID
:
445 case PPC::VSSRCRegClassID
:
446 return 64 - DefaultSafety
;
447 case PPC::CRRCRegClassID
:
448 return 8 - DefaultSafety
;
452 const TargetRegisterClass
*
453 PPCRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass
*RC
,
454 const MachineFunction
&MF
) const {
455 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
456 if (Subtarget
.hasVSX()) {
457 // With VSX, we can inflate various sub-register classes to the full VSX
460 // For Power9 we allow the user to enable GPR to vector spills.
461 // FIXME: Currently limited to spilling GP8RC. A follow on patch will add
462 // support to spill GPRC.
463 if (TM
.isELFv2ABI()) {
464 if (Subtarget
.hasP9Vector() && EnableGPRToVecSpills
&&
465 RC
== &PPC::G8RCRegClass
) {
467 return &PPC::SPILLTOVSRRCRegClass
;
469 if (RC
== &PPC::GPRCRegClass
&& EnableGPRToVecSpills
)
472 if (RC
== &PPC::F8RCRegClass
)
473 return &PPC::VSFRCRegClass
;
474 else if (RC
== &PPC::VRRCRegClass
)
475 return &PPC::VSRCRegClass
;
476 else if (RC
== &PPC::F4RCRegClass
&& Subtarget
.hasP8Vector())
477 return &PPC::VSSRCRegClass
;
480 return TargetRegisterInfo::getLargestLegalSuperClass(RC
, MF
);
483 //===----------------------------------------------------------------------===//
484 // Stack Frame Processing methods
485 //===----------------------------------------------------------------------===//
487 /// lowerDynamicAlloc - Generate the code for allocating an object in the
488 /// current frame. The sequence of code will be in the general form
490 /// addi R0, SP, \#frameSize ; get the address of the previous frame
491 /// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size
492 /// addi Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
494 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II
) const {
495 // Get the instruction.
496 MachineInstr
&MI
= *II
;
497 // Get the instruction's basic block.
498 MachineBasicBlock
&MBB
= *MI
.getParent();
499 // Get the basic block's function.
500 MachineFunction
&MF
= *MBB
.getParent();
501 // Get the frame info.
502 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
503 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
504 // Get the instruction info.
505 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
506 // Determine whether 64-bit pointers are used.
507 bool LP64
= TM
.isPPC64();
508 DebugLoc dl
= MI
.getDebugLoc();
510 // Get the maximum call stack size.
511 unsigned maxCallFrameSize
= MFI
.getMaxCallFrameSize();
512 // Get the total frame size.
513 unsigned FrameSize
= MFI
.getStackSize();
515 // Get stack alignments.
516 const PPCFrameLowering
*TFI
= getFrameLowering(MF
);
517 unsigned TargetAlign
= TFI
->getStackAlignment();
518 unsigned MaxAlign
= MFI
.getMaxAlignment();
519 assert((maxCallFrameSize
& (MaxAlign
-1)) == 0 &&
520 "Maximum call-frame size not sufficiently aligned");
522 // Determine the previous frame's address. If FrameSize can't be
523 // represented as 16 bits or we need special alignment, then we load the
524 // previous frame's address from 0(SP). Why not do an addis of the hi?
525 // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
526 // Constructing the constant and adding would take 3 instructions.
527 // Fortunately, a frame greater than 32K is rare.
528 const TargetRegisterClass
*G8RC
= &PPC::G8RCRegClass
;
529 const TargetRegisterClass
*GPRC
= &PPC::GPRCRegClass
;
530 Register Reg
= MF
.getRegInfo().createVirtualRegister(LP64
? G8RC
: GPRC
);
532 if (MaxAlign
< TargetAlign
&& isInt
<16>(FrameSize
)) {
534 BuildMI(MBB
, II
, dl
, TII
.get(PPC::ADDI8
), Reg
)
538 BuildMI(MBB
, II
, dl
, TII
.get(PPC::ADDI
), Reg
)
542 BuildMI(MBB
, II
, dl
, TII
.get(PPC::LD
), Reg
)
546 BuildMI(MBB
, II
, dl
, TII
.get(PPC::LWZ
), Reg
)
551 bool KillNegSizeReg
= MI
.getOperand(1).isKill();
552 Register NegSizeReg
= MI
.getOperand(1).getReg();
554 // Grow the stack and update the stack pointer link, then determine the
555 // address of new allocated space.
557 if (MaxAlign
> TargetAlign
) {
558 unsigned UnalNegSizeReg
= NegSizeReg
;
559 NegSizeReg
= MF
.getRegInfo().createVirtualRegister(G8RC
);
561 // Unfortunately, there is no andi, only andi., and we can't insert that
562 // here because we might clobber cr0 while it is live.
563 BuildMI(MBB
, II
, dl
, TII
.get(PPC::LI8
), NegSizeReg
)
564 .addImm(~(MaxAlign
-1));
566 unsigned NegSizeReg1
= NegSizeReg
;
567 NegSizeReg
= MF
.getRegInfo().createVirtualRegister(G8RC
);
568 BuildMI(MBB
, II
, dl
, TII
.get(PPC::AND8
), NegSizeReg
)
569 .addReg(UnalNegSizeReg
, getKillRegState(KillNegSizeReg
))
570 .addReg(NegSizeReg1
, RegState::Kill
);
571 KillNegSizeReg
= true;
574 BuildMI(MBB
, II
, dl
, TII
.get(PPC::STDUX
), PPC::X1
)
575 .addReg(Reg
, RegState::Kill
)
577 .addReg(NegSizeReg
, getKillRegState(KillNegSizeReg
));
578 BuildMI(MBB
, II
, dl
, TII
.get(PPC::ADDI8
), MI
.getOperand(0).getReg())
580 .addImm(maxCallFrameSize
);
582 if (MaxAlign
> TargetAlign
) {
583 unsigned UnalNegSizeReg
= NegSizeReg
;
584 NegSizeReg
= MF
.getRegInfo().createVirtualRegister(GPRC
);
586 // Unfortunately, there is no andi, only andi., and we can't insert that
587 // here because we might clobber cr0 while it is live.
588 BuildMI(MBB
, II
, dl
, TII
.get(PPC::LI
), NegSizeReg
)
589 .addImm(~(MaxAlign
-1));
591 unsigned NegSizeReg1
= NegSizeReg
;
592 NegSizeReg
= MF
.getRegInfo().createVirtualRegister(GPRC
);
593 BuildMI(MBB
, II
, dl
, TII
.get(PPC::AND
), NegSizeReg
)
594 .addReg(UnalNegSizeReg
, getKillRegState(KillNegSizeReg
))
595 .addReg(NegSizeReg1
, RegState::Kill
);
596 KillNegSizeReg
= true;
599 BuildMI(MBB
, II
, dl
, TII
.get(PPC::STWUX
), PPC::R1
)
600 .addReg(Reg
, RegState::Kill
)
602 .addReg(NegSizeReg
, getKillRegState(KillNegSizeReg
));
603 BuildMI(MBB
, II
, dl
, TII
.get(PPC::ADDI
), MI
.getOperand(0).getReg())
605 .addImm(maxCallFrameSize
);
608 // Discard the DYNALLOC instruction.
612 void PPCRegisterInfo::lowerDynamicAreaOffset(
613 MachineBasicBlock::iterator II
) const {
614 // Get the instruction.
615 MachineInstr
&MI
= *II
;
616 // Get the instruction's basic block.
617 MachineBasicBlock
&MBB
= *MI
.getParent();
618 // Get the basic block's function.
619 MachineFunction
&MF
= *MBB
.getParent();
620 // Get the frame info.
621 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
622 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
623 // Get the instruction info.
624 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
626 unsigned maxCallFrameSize
= MFI
.getMaxCallFrameSize();
627 bool is64Bit
= TM
.isPPC64();
628 DebugLoc dl
= MI
.getDebugLoc();
629 BuildMI(MBB
, II
, dl
, TII
.get(is64Bit
? PPC::LI8
: PPC::LI
),
630 MI
.getOperand(0).getReg())
631 .addImm(maxCallFrameSize
);
635 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
636 /// reserving a whole register (R0), we scrounge for one here. This generates
639 /// mfcr rA ; Move the conditional register into GPR rA.
640 /// rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
641 /// stw rA, FI ; Store rA to the frame.
643 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II
,
644 unsigned FrameIndex
) const {
645 // Get the instruction.
646 MachineInstr
&MI
= *II
; // ; SPILL_CR <SrcReg>, <offset>
647 // Get the instruction's basic block.
648 MachineBasicBlock
&MBB
= *MI
.getParent();
649 MachineFunction
&MF
= *MBB
.getParent();
650 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
651 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
652 DebugLoc dl
= MI
.getDebugLoc();
654 bool LP64
= TM
.isPPC64();
655 const TargetRegisterClass
*G8RC
= &PPC::G8RCRegClass
;
656 const TargetRegisterClass
*GPRC
= &PPC::GPRCRegClass
;
658 Register Reg
= MF
.getRegInfo().createVirtualRegister(LP64
? G8RC
: GPRC
);
659 Register SrcReg
= MI
.getOperand(0).getReg();
661 // We need to store the CR in the low 4-bits of the saved value. First, issue
662 // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg.
663 BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::MFOCRF8
: PPC::MFOCRF
), Reg
)
664 .addReg(SrcReg
, getKillRegState(MI
.getOperand(0).isKill()));
666 // If the saved register wasn't CR0, shift the bits left so that they are in
668 if (SrcReg
!= PPC::CR0
) {
670 Reg
= MF
.getRegInfo().createVirtualRegister(LP64
? G8RC
: GPRC
);
672 // rlwinm rA, rA, ShiftBits, 0, 31.
673 BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::RLWINM8
: PPC::RLWINM
), Reg
)
674 .addReg(Reg1
, RegState::Kill
)
675 .addImm(getEncodingValue(SrcReg
) * 4)
680 addFrameReference(BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::STW8
: PPC::STW
))
681 .addReg(Reg
, RegState::Kill
),
684 // Discard the pseudo instruction.
688 void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II
,
689 unsigned FrameIndex
) const {
690 // Get the instruction.
691 MachineInstr
&MI
= *II
; // ; <DestReg> = RESTORE_CR <offset>
692 // Get the instruction's basic block.
693 MachineBasicBlock
&MBB
= *MI
.getParent();
694 MachineFunction
&MF
= *MBB
.getParent();
695 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
696 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
697 DebugLoc dl
= MI
.getDebugLoc();
699 bool LP64
= TM
.isPPC64();
700 const TargetRegisterClass
*G8RC
= &PPC::G8RCRegClass
;
701 const TargetRegisterClass
*GPRC
= &PPC::GPRCRegClass
;
703 Register Reg
= MF
.getRegInfo().createVirtualRegister(LP64
? G8RC
: GPRC
);
704 Register DestReg
= MI
.getOperand(0).getReg();
705 assert(MI
.definesRegister(DestReg
) &&
706 "RESTORE_CR does not define its destination");
708 addFrameReference(BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::LWZ8
: PPC::LWZ
),
711 // If the reloaded register isn't CR0, shift the bits right so that they are
712 // in the right CR's slot.
713 if (DestReg
!= PPC::CR0
) {
715 Reg
= MF
.getRegInfo().createVirtualRegister(LP64
? G8RC
: GPRC
);
717 unsigned ShiftBits
= getEncodingValue(DestReg
)*4;
718 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
719 BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::RLWINM8
: PPC::RLWINM
), Reg
)
720 .addReg(Reg1
, RegState::Kill
).addImm(32-ShiftBits
).addImm(0)
724 BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::MTOCRF8
: PPC::MTOCRF
), DestReg
)
725 .addReg(Reg
, RegState::Kill
);
727 // Discard the pseudo instruction.
731 void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II
,
732 unsigned FrameIndex
) const {
733 // Get the instruction.
734 MachineInstr
&MI
= *II
; // ; SPILL_CRBIT <SrcReg>, <offset>
735 // Get the instruction's basic block.
736 MachineBasicBlock
&MBB
= *MI
.getParent();
737 MachineFunction
&MF
= *MBB
.getParent();
738 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
739 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
740 const TargetRegisterInfo
* TRI
= Subtarget
.getRegisterInfo();
741 DebugLoc dl
= MI
.getDebugLoc();
743 bool LP64
= TM
.isPPC64();
744 const TargetRegisterClass
*G8RC
= &PPC::G8RCRegClass
;
745 const TargetRegisterClass
*GPRC
= &PPC::GPRCRegClass
;
747 Register Reg
= MF
.getRegInfo().createVirtualRegister(LP64
? G8RC
: GPRC
);
748 Register SrcReg
= MI
.getOperand(0).getReg();
750 // Search up the BB to find the definition of the CR bit.
751 MachineBasicBlock::reverse_iterator Ins
;
752 unsigned CRBitSpillDistance
= 0;
753 for (Ins
= MI
; Ins
!= MBB
.rend(); Ins
++) {
755 if (Ins
->modifiesRegister(SrcReg
, TRI
))
757 // Unable to find CR bit definition within maximum search distance.
758 if (CRBitSpillDistance
== MaxCRBitSpillDist
) {
762 // Skip debug instructions when counting CR bit spill distance.
763 if (!Ins
->isDebugInstr())
764 CRBitSpillDistance
++;
767 // Unable to find the definition of the CR bit in the MBB.
768 if (Ins
== MBB
.rend())
771 // There is no need to extract the CR bit if its value is already known.
772 switch (Ins
->getOpcode()) {
774 BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::LI8
: PPC::LI
), Reg
)
778 BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::LIS8
: PPC::LIS
), Reg
)
782 // We need to move the CR field that contains the CR bit we are spilling.
783 // The super register may not be explicitly defined (i.e. it can be defined
784 // by a CR-logical that only defines the subreg) so we state that the CR
785 // field is undef. Also, in order to preserve the kill flag on the CR bit,
786 // we add it as an implicit use.
787 BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::MFOCRF8
: PPC::MFOCRF
), Reg
)
788 .addReg(getCRFromCRBit(SrcReg
), RegState::Undef
)
790 RegState::Implicit
| getKillRegState(MI
.getOperand(0).isKill()));
792 // If the saved register wasn't CR0LT, shift the bits left so that the bit
793 // to store is the first one. Mask all but that bit.
795 Reg
= MF
.getRegInfo().createVirtualRegister(LP64
? G8RC
: GPRC
);
797 // rlwinm rA, rA, ShiftBits, 0, 0.
798 BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::RLWINM8
: PPC::RLWINM
), Reg
)
799 .addReg(Reg1
, RegState::Kill
)
800 .addImm(getEncodingValue(SrcReg
))
801 .addImm(0).addImm(0);
803 addFrameReference(BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::STW8
: PPC::STW
))
804 .addReg(Reg
, RegState::Kill
),
807 // Discard the pseudo instruction.
811 void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II
,
812 unsigned FrameIndex
) const {
813 // Get the instruction.
814 MachineInstr
&MI
= *II
; // ; <DestReg> = RESTORE_CRBIT <offset>
815 // Get the instruction's basic block.
816 MachineBasicBlock
&MBB
= *MI
.getParent();
817 MachineFunction
&MF
= *MBB
.getParent();
818 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
819 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
820 DebugLoc dl
= MI
.getDebugLoc();
822 bool LP64
= TM
.isPPC64();
823 const TargetRegisterClass
*G8RC
= &PPC::G8RCRegClass
;
824 const TargetRegisterClass
*GPRC
= &PPC::GPRCRegClass
;
826 Register Reg
= MF
.getRegInfo().createVirtualRegister(LP64
? G8RC
: GPRC
);
827 Register DestReg
= MI
.getOperand(0).getReg();
828 assert(MI
.definesRegister(DestReg
) &&
829 "RESTORE_CRBIT does not define its destination");
831 addFrameReference(BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::LWZ8
: PPC::LWZ
),
834 BuildMI(MBB
, II
, dl
, TII
.get(TargetOpcode::IMPLICIT_DEF
), DestReg
);
836 Register RegO
= MF
.getRegInfo().createVirtualRegister(LP64
? G8RC
: GPRC
);
837 BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::MFOCRF8
: PPC::MFOCRF
), RegO
)
838 .addReg(getCRFromCRBit(DestReg
));
840 unsigned ShiftBits
= getEncodingValue(DestReg
);
841 // rlwimi r11, r10, 32-ShiftBits, ..., ...
842 BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::RLWIMI8
: PPC::RLWIMI
), RegO
)
843 .addReg(RegO
, RegState::Kill
)
844 .addReg(Reg
, RegState::Kill
)
845 .addImm(ShiftBits
? 32 - ShiftBits
: 0)
849 BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::MTOCRF8
: PPC::MTOCRF
),
850 getCRFromCRBit(DestReg
))
851 .addReg(RegO
, RegState::Kill
)
852 // Make sure we have a use dependency all the way through this
853 // sequence of instructions. We can't have the other bits in the CR
854 // modified in between the mfocrf and the mtocrf.
855 .addReg(getCRFromCRBit(DestReg
), RegState::Implicit
);
857 // Discard the pseudo instruction.
861 void PPCRegisterInfo::lowerVRSAVESpilling(MachineBasicBlock::iterator II
,
862 unsigned FrameIndex
) const {
863 // Get the instruction.
864 MachineInstr
&MI
= *II
; // ; SPILL_VRSAVE <SrcReg>, <offset>
865 // Get the instruction's basic block.
866 MachineBasicBlock
&MBB
= *MI
.getParent();
867 MachineFunction
&MF
= *MBB
.getParent();
868 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
869 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
870 DebugLoc dl
= MI
.getDebugLoc();
872 const TargetRegisterClass
*GPRC
= &PPC::GPRCRegClass
;
873 Register Reg
= MF
.getRegInfo().createVirtualRegister(GPRC
);
874 Register SrcReg
= MI
.getOperand(0).getReg();
876 BuildMI(MBB
, II
, dl
, TII
.get(PPC::MFVRSAVEv
), Reg
)
877 .addReg(SrcReg
, getKillRegState(MI
.getOperand(0).isKill()));
880 BuildMI(MBB
, II
, dl
, TII
.get(PPC::STW
)).addReg(Reg
, RegState::Kill
),
883 // Discard the pseudo instruction.
887 void PPCRegisterInfo::lowerVRSAVERestore(MachineBasicBlock::iterator II
,
888 unsigned FrameIndex
) const {
889 // Get the instruction.
890 MachineInstr
&MI
= *II
; // ; <DestReg> = RESTORE_VRSAVE <offset>
891 // Get the instruction's basic block.
892 MachineBasicBlock
&MBB
= *MI
.getParent();
893 MachineFunction
&MF
= *MBB
.getParent();
894 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
895 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
896 DebugLoc dl
= MI
.getDebugLoc();
898 const TargetRegisterClass
*GPRC
= &PPC::GPRCRegClass
;
899 Register Reg
= MF
.getRegInfo().createVirtualRegister(GPRC
);
900 Register DestReg
= MI
.getOperand(0).getReg();
901 assert(MI
.definesRegister(DestReg
) &&
902 "RESTORE_VRSAVE does not define its destination");
904 addFrameReference(BuildMI(MBB
, II
, dl
, TII
.get(PPC::LWZ
),
907 BuildMI(MBB
, II
, dl
, TII
.get(PPC::MTVRSAVEv
), DestReg
)
908 .addReg(Reg
, RegState::Kill
);
910 // Discard the pseudo instruction.
914 bool PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction
&MF
,
915 unsigned Reg
, int &FrameIdx
) const {
916 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
917 // For the nonvolatile condition registers (CR2, CR3, CR4) in an SVR4
918 // ABI, return true to prevent allocating an additional frame slot.
919 // For 64-bit, the CR save area is at SP+8; the value of FrameIdx = 0
920 // is arbitrary and will be subsequently ignored. For 32-bit, we have
921 // previously created the stack slot if needed, so return its FrameIdx.
922 if (Subtarget
.isSVR4ABI() && PPC::CR2
<= Reg
&& Reg
<= PPC::CR4
) {
926 const PPCFunctionInfo
*FI
= MF
.getInfo
<PPCFunctionInfo
>();
927 FrameIdx
= FI
->getCRSpillFrameIndex();
934 // If the offset must be a multiple of some value, return what that value is.
935 static unsigned offsetMinAlignForOpcode(unsigned OpC
) {
947 case PPC::DFSTOREf32
:
948 case PPC::DFSTOREf64
:
963 // If the offset must be a multiple of some value, return what that value is.
964 static unsigned offsetMinAlign(const MachineInstr
&MI
) {
965 unsigned OpC
= MI
.getOpcode();
966 return offsetMinAlignForOpcode(OpC
);
969 // Return the OffsetOperandNo given the FIOperandNum (and the instruction).
970 static unsigned getOffsetONFromFION(const MachineInstr
&MI
,
971 unsigned FIOperandNum
) {
972 // Take into account whether it's an add or mem instruction
973 unsigned OffsetOperandNo
= (FIOperandNum
== 2) ? 1 : 2;
974 if (MI
.isInlineAsm())
975 OffsetOperandNo
= FIOperandNum
- 1;
976 else if (MI
.getOpcode() == TargetOpcode::STACKMAP
||
977 MI
.getOpcode() == TargetOpcode::PATCHPOINT
)
978 OffsetOperandNo
= FIOperandNum
+ 1;
980 return OffsetOperandNo
;
984 PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II
,
985 int SPAdj
, unsigned FIOperandNum
,
986 RegScavenger
*RS
) const {
987 assert(SPAdj
== 0 && "Unexpected");
989 // Get the instruction.
990 MachineInstr
&MI
= *II
;
991 // Get the instruction's basic block.
992 MachineBasicBlock
&MBB
= *MI
.getParent();
993 // Get the basic block's function.
994 MachineFunction
&MF
= *MBB
.getParent();
995 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
996 // Get the instruction info.
997 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
998 // Get the frame info.
999 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
1000 DebugLoc dl
= MI
.getDebugLoc();
1002 unsigned OffsetOperandNo
= getOffsetONFromFION(MI
, FIOperandNum
);
1004 // Get the frame index.
1005 int FrameIndex
= MI
.getOperand(FIOperandNum
).getIndex();
1007 // Get the frame pointer save index. Users of this index are primarily
1008 // DYNALLOC instructions.
1009 PPCFunctionInfo
*FI
= MF
.getInfo
<PPCFunctionInfo
>();
1010 int FPSI
= FI
->getFramePointerSaveIndex();
1011 // Get the instruction opcode.
1012 unsigned OpC
= MI
.getOpcode();
1014 if ((OpC
== PPC::DYNAREAOFFSET
|| OpC
== PPC::DYNAREAOFFSET8
)) {
1015 lowerDynamicAreaOffset(II
);
1019 // Special case for dynamic alloca.
1020 if (FPSI
&& FrameIndex
== FPSI
&&
1021 (OpC
== PPC::DYNALLOC
|| OpC
== PPC::DYNALLOC8
)) {
1022 lowerDynamicAlloc(II
);
1026 // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc.
1027 if (OpC
== PPC::SPILL_CR
) {
1028 lowerCRSpilling(II
, FrameIndex
);
1030 } else if (OpC
== PPC::RESTORE_CR
) {
1031 lowerCRRestore(II
, FrameIndex
);
1033 } else if (OpC
== PPC::SPILL_CRBIT
) {
1034 lowerCRBitSpilling(II
, FrameIndex
);
1036 } else if (OpC
== PPC::RESTORE_CRBIT
) {
1037 lowerCRBitRestore(II
, FrameIndex
);
1039 } else if (OpC
== PPC::SPILL_VRSAVE
) {
1040 lowerVRSAVESpilling(II
, FrameIndex
);
1042 } else if (OpC
== PPC::RESTORE_VRSAVE
) {
1043 lowerVRSAVERestore(II
, FrameIndex
);
1047 // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
1048 MI
.getOperand(FIOperandNum
).ChangeToRegister(
1049 FrameIndex
< 0 ? getBaseRegister(MF
) : getFrameRegister(MF
), false);
1051 // If the instruction is not present in ImmToIdxMap, then it has no immediate
1052 // form (and must be r+r).
1053 bool noImmForm
= !MI
.isInlineAsm() && OpC
!= TargetOpcode::STACKMAP
&&
1054 OpC
!= TargetOpcode::PATCHPOINT
&& !ImmToIdxMap
.count(OpC
);
1056 // Now add the frame object offset to the offset from r1.
1057 int Offset
= MFI
.getObjectOffset(FrameIndex
);
1058 Offset
+= MI
.getOperand(OffsetOperandNo
).getImm();
1060 // If we're not using a Frame Pointer that has been set to the value of the
1061 // SP before having the stack size subtracted from it, then add the stack size
1062 // to Offset to get the correct offset.
1063 // Naked functions have stack size 0, although getStackSize may not reflect
1064 // that because we didn't call all the pieces that compute it for naked
1066 if (!MF
.getFunction().hasFnAttribute(Attribute::Naked
)) {
1067 if (!(hasBasePointer(MF
) && FrameIndex
< 0))
1068 Offset
+= MFI
.getStackSize();
1071 // If we can, encode the offset directly into the instruction. If this is a
1072 // normal PPC "ri" instruction, any 16-bit value can be safely encoded. If
1073 // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
1074 // clear can be encoded. This is extremely uncommon, because normally you
1075 // only "std" to a stack slot that is at least 4-byte aligned, but it can
1076 // happen in invalid code.
1077 assert(OpC
!= PPC::DBG_VALUE
&&
1078 "This should be handled in a target-independent way");
1079 bool OffsetFitsMnemonic
= (OpC
== PPC::EVSTDD
|| OpC
== PPC::EVLDD
) ?
1082 if (!noImmForm
&& ((OffsetFitsMnemonic
&&
1083 ((Offset
% offsetMinAlign(MI
)) == 0)) ||
1084 OpC
== TargetOpcode::STACKMAP
||
1085 OpC
== TargetOpcode::PATCHPOINT
)) {
1086 MI
.getOperand(OffsetOperandNo
).ChangeToImmediate(Offset
);
1090 // The offset doesn't fit into a single register, scavenge one to build the
1093 bool is64Bit
= TM
.isPPC64();
1094 const TargetRegisterClass
*G8RC
= &PPC::G8RCRegClass
;
1095 const TargetRegisterClass
*GPRC
= &PPC::GPRCRegClass
;
1096 const TargetRegisterClass
*RC
= is64Bit
? G8RC
: GPRC
;
1097 unsigned SRegHi
= MF
.getRegInfo().createVirtualRegister(RC
),
1098 SReg
= MF
.getRegInfo().createVirtualRegister(RC
);
1100 // Insert a set of rA with the full offset value before the ld, st, or add
1101 if (isInt
<16>(Offset
))
1102 BuildMI(MBB
, II
, dl
, TII
.get(is64Bit
? PPC::LI8
: PPC::LI
), SReg
)
1105 BuildMI(MBB
, II
, dl
, TII
.get(is64Bit
? PPC::LIS8
: PPC::LIS
), SRegHi
)
1106 .addImm(Offset
>> 16);
1107 BuildMI(MBB
, II
, dl
, TII
.get(is64Bit
? PPC::ORI8
: PPC::ORI
), SReg
)
1108 .addReg(SRegHi
, RegState::Kill
)
1112 // Convert into indexed form of the instruction:
1114 // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
1115 // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
1116 unsigned OperandBase
;
1120 else if (OpC
!= TargetOpcode::INLINEASM
&&
1121 OpC
!= TargetOpcode::INLINEASM_BR
) {
1122 assert(ImmToIdxMap
.count(OpC
) &&
1123 "No indexed form of load or store available!");
1124 unsigned NewOpcode
= ImmToIdxMap
.find(OpC
)->second
;
1125 MI
.setDesc(TII
.get(NewOpcode
));
1128 OperandBase
= OffsetOperandNo
;
1131 Register StackReg
= MI
.getOperand(FIOperandNum
).getReg();
1132 MI
.getOperand(OperandBase
).ChangeToRegister(StackReg
, false);
1133 MI
.getOperand(OperandBase
+ 1).ChangeToRegister(SReg
, false, false, true);
1136 Register
PPCRegisterInfo::getFrameRegister(const MachineFunction
&MF
) const {
1137 const PPCFrameLowering
*TFI
= getFrameLowering(MF
);
1140 return TFI
->hasFP(MF
) ? PPC::R31
: PPC::R1
;
1142 return TFI
->hasFP(MF
) ? PPC::X31
: PPC::X1
;
1145 Register
PPCRegisterInfo::getBaseRegister(const MachineFunction
&MF
) const {
1146 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
1147 if (!hasBasePointer(MF
))
1148 return getFrameRegister(MF
);
1153 if (Subtarget
.isSVR4ABI() && TM
.isPositionIndependent())
1159 bool PPCRegisterInfo::hasBasePointer(const MachineFunction
&MF
) const {
1160 if (!EnableBasePointer
)
1162 if (AlwaysBasePointer
)
1165 // If we need to realign the stack, then the stack pointer can no longer
1166 // serve as an offset into the caller's stack space. As a result, we need a
1168 return needsStackRealignment(MF
);
1171 /// Returns true if the instruction's frame index
1172 /// reference would be better served by a base register other than FP
1173 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
1174 /// references it should create new base registers for.
1175 bool PPCRegisterInfo::
1176 needsFrameBaseReg(MachineInstr
*MI
, int64_t Offset
) const {
1177 assert(Offset
< 0 && "Local offset must be negative");
1179 // It's the load/store FI references that cause issues, as it can be difficult
1180 // to materialize the offset if it won't fit in the literal field. Estimate
1181 // based on the size of the local frame and some conservative assumptions
1182 // about the rest of the stack frame (note, this is pre-regalloc, so
1183 // we don't know everything for certain yet) whether this offset is likely
1184 // to be out of range of the immediate. Return true if so.
1186 // We only generate virtual base registers for loads and stores that have
1187 // an r+i form. Return false for everything else.
1188 unsigned OpC
= MI
->getOpcode();
1189 if (!ImmToIdxMap
.count(OpC
))
1192 // Don't generate a new virtual base register just to add zero to it.
1193 if ((OpC
== PPC::ADDI
|| OpC
== PPC::ADDI8
) &&
1194 MI
->getOperand(2).getImm() == 0)
1197 MachineBasicBlock
&MBB
= *MI
->getParent();
1198 MachineFunction
&MF
= *MBB
.getParent();
1199 const PPCFrameLowering
*TFI
= getFrameLowering(MF
);
1200 unsigned StackEst
= TFI
->determineFrameLayout(MF
, true);
1202 // If we likely don't need a stack frame, then we probably don't need a
1203 // virtual base register either.
1207 // Estimate an offset from the stack pointer.
1208 // The incoming offset is relating to the SP at the start of the function,
1209 // but when we access the local it'll be relative to the SP after local
1210 // allocation, so adjust our SP-relative offset by that allocation size.
1213 // The frame pointer will point to the end of the stack, so estimate the
1214 // offset as the difference between the object offset and the FP location.
1215 return !isFrameOffsetLegal(MI
, getBaseRegister(MF
), Offset
);
1218 /// Insert defining instruction(s) for BaseReg to
1219 /// be a pointer to FrameIdx at the beginning of the basic block.
1220 void PPCRegisterInfo::
1221 materializeFrameBaseRegister(MachineBasicBlock
*MBB
,
1222 unsigned BaseReg
, int FrameIdx
,
1223 int64_t Offset
) const {
1224 unsigned ADDriOpc
= TM
.isPPC64() ? PPC::ADDI8
: PPC::ADDI
;
1226 MachineBasicBlock::iterator Ins
= MBB
->begin();
1227 DebugLoc DL
; // Defaults to "unknown"
1228 if (Ins
!= MBB
->end())
1229 DL
= Ins
->getDebugLoc();
1231 const MachineFunction
&MF
= *MBB
->getParent();
1232 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
1233 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
1234 const MCInstrDesc
&MCID
= TII
.get(ADDriOpc
);
1235 MachineRegisterInfo
&MRI
= MBB
->getParent()->getRegInfo();
1236 MRI
.constrainRegClass(BaseReg
, TII
.getRegClass(MCID
, 0, this, MF
));
1238 BuildMI(*MBB
, Ins
, DL
, MCID
, BaseReg
)
1239 .addFrameIndex(FrameIdx
).addImm(Offset
);
1242 void PPCRegisterInfo::resolveFrameIndex(MachineInstr
&MI
, unsigned BaseReg
,
1243 int64_t Offset
) const {
1244 unsigned FIOperandNum
= 0;
1245 while (!MI
.getOperand(FIOperandNum
).isFI()) {
1247 assert(FIOperandNum
< MI
.getNumOperands() &&
1248 "Instr doesn't have FrameIndex operand!");
1251 MI
.getOperand(FIOperandNum
).ChangeToRegister(BaseReg
, false);
1252 unsigned OffsetOperandNo
= getOffsetONFromFION(MI
, FIOperandNum
);
1253 Offset
+= MI
.getOperand(OffsetOperandNo
).getImm();
1254 MI
.getOperand(OffsetOperandNo
).ChangeToImmediate(Offset
);
1256 MachineBasicBlock
&MBB
= *MI
.getParent();
1257 MachineFunction
&MF
= *MBB
.getParent();
1258 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
1259 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
1260 const MCInstrDesc
&MCID
= MI
.getDesc();
1261 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
1262 MRI
.constrainRegClass(BaseReg
,
1263 TII
.getRegClass(MCID
, FIOperandNum
, this, MF
));
1266 bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr
*MI
,
1268 int64_t Offset
) const {
1269 unsigned FIOperandNum
= 0;
1270 while (!MI
->getOperand(FIOperandNum
).isFI()) {
1272 assert(FIOperandNum
< MI
->getNumOperands() &&
1273 "Instr doesn't have FrameIndex operand!");
1276 unsigned OffsetOperandNo
= getOffsetONFromFION(*MI
, FIOperandNum
);
1277 Offset
+= MI
->getOperand(OffsetOperandNo
).getImm();
1279 return MI
->getOpcode() == PPC::DBG_VALUE
|| // DBG_VALUE is always Reg+Imm
1280 MI
->getOpcode() == TargetOpcode::STACKMAP
||
1281 MI
->getOpcode() == TargetOpcode::PATCHPOINT
||
1282 (isInt
<16>(Offset
) && (Offset
% offsetMinAlign(*MI
)) == 0);