1 //===-- PPCRegisterInfo.cpp - PowerPC Register Information ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the PowerPC implementation of the TargetRegisterInfo
13 //===----------------------------------------------------------------------===//
15 #include "PPCRegisterInfo.h"
17 #include "PPCFrameLowering.h"
18 #include "PPCInstrBuilder.h"
19 #include "PPCMachineFunctionInfo.h"
20 #include "PPCSubtarget.h"
21 #include "PPCTargetMachine.h"
22 #include "llvm/ADT/BitVector.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "llvm/CodeGen/MachineRegisterInfo.h"
30 #include "llvm/CodeGen/RegisterScavenging.h"
31 #include "llvm/CodeGen/TargetFrameLowering.h"
32 #include "llvm/CodeGen/TargetInstrInfo.h"
33 #include "llvm/IR/CallingConv.h"
34 #include "llvm/IR/Constants.h"
35 #include "llvm/IR/Function.h"
36 #include "llvm/IR/Type.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/MathExtras.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include "llvm/Target/TargetMachine.h"
43 #include "llvm/Target/TargetOptions.h"
48 #define DEBUG_TYPE "reginfo"
50 #define GET_REGINFO_TARGET_DESC
51 #include "PPCGenRegisterInfo.inc"
53 STATISTIC(InflateGPRC
, "Number of gprc inputs for getLargestLegalClass");
54 STATISTIC(InflateGP8RC
, "Number of g8rc inputs for getLargestLegalClass");
57 EnableBasePointer("ppc-use-base-pointer", cl::Hidden
, cl::init(true),
58 cl::desc("Enable use of a base pointer for complex stack frames"));
61 AlwaysBasePointer("ppc-always-use-base-pointer", cl::Hidden
, cl::init(false),
62 cl::desc("Force the use of a base pointer in every function"));
65 EnableGPRToVecSpills("ppc-enable-gpr-to-vsr-spills", cl::Hidden
, cl::init(false),
66 cl::desc("Enable spills from gpr to vsr rather than stack"));
69 StackPtrConst("ppc-stack-ptr-caller-preserved",
70 cl::desc("Consider R1 caller preserved so stack saves of "
71 "caller preserved registers can be LICM candidates"),
72 cl::init(true), cl::Hidden
);
74 PPCRegisterInfo::PPCRegisterInfo(const PPCTargetMachine
&TM
)
75 : PPCGenRegisterInfo(TM
.isPPC64() ? PPC::LR8
: PPC::LR
,
77 TM
.isPPC64() ? 0 : 1),
79 ImmToIdxMap
[PPC::LD
] = PPC::LDX
; ImmToIdxMap
[PPC::STD
] = PPC::STDX
;
80 ImmToIdxMap
[PPC::LBZ
] = PPC::LBZX
; ImmToIdxMap
[PPC::STB
] = PPC::STBX
;
81 ImmToIdxMap
[PPC::LHZ
] = PPC::LHZX
; ImmToIdxMap
[PPC::LHA
] = PPC::LHAX
;
82 ImmToIdxMap
[PPC::LWZ
] = PPC::LWZX
; ImmToIdxMap
[PPC::LWA
] = PPC::LWAX
;
83 ImmToIdxMap
[PPC::LFS
] = PPC::LFSX
; ImmToIdxMap
[PPC::LFD
] = PPC::LFDX
;
84 ImmToIdxMap
[PPC::STH
] = PPC::STHX
; ImmToIdxMap
[PPC::STW
] = PPC::STWX
;
85 ImmToIdxMap
[PPC::STFS
] = PPC::STFSX
; ImmToIdxMap
[PPC::STFD
] = PPC::STFDX
;
86 ImmToIdxMap
[PPC::ADDI
] = PPC::ADD4
;
87 ImmToIdxMap
[PPC::LWA_32
] = PPC::LWAX_32
;
90 ImmToIdxMap
[PPC::LHA8
] = PPC::LHAX8
; ImmToIdxMap
[PPC::LBZ8
] = PPC::LBZX8
;
91 ImmToIdxMap
[PPC::LHZ8
] = PPC::LHZX8
; ImmToIdxMap
[PPC::LWZ8
] = PPC::LWZX8
;
92 ImmToIdxMap
[PPC::STB8
] = PPC::STBX8
; ImmToIdxMap
[PPC::STH8
] = PPC::STHX8
;
93 ImmToIdxMap
[PPC::STW8
] = PPC::STWX8
; ImmToIdxMap
[PPC::STDU
] = PPC::STDUX
;
94 ImmToIdxMap
[PPC::ADDI8
] = PPC::ADD8
;
97 ImmToIdxMap
[PPC::DFLOADf32
] = PPC::LXSSPX
;
98 ImmToIdxMap
[PPC::DFLOADf64
] = PPC::LXSDX
;
99 ImmToIdxMap
[PPC::SPILLTOVSR_LD
] = PPC::SPILLTOVSR_LDX
;
100 ImmToIdxMap
[PPC::SPILLTOVSR_ST
] = PPC::SPILLTOVSR_STX
;
101 ImmToIdxMap
[PPC::DFSTOREf32
] = PPC::STXSSPX
;
102 ImmToIdxMap
[PPC::DFSTOREf64
] = PPC::STXSDX
;
103 ImmToIdxMap
[PPC::LXV
] = PPC::LXVX
;
104 ImmToIdxMap
[PPC::LXSD
] = PPC::LXSDX
;
105 ImmToIdxMap
[PPC::LXSSP
] = PPC::LXSSPX
;
106 ImmToIdxMap
[PPC::STXV
] = PPC::STXVX
;
107 ImmToIdxMap
[PPC::STXSD
] = PPC::STXSDX
;
108 ImmToIdxMap
[PPC::STXSSP
] = PPC::STXSSPX
;
111 /// getPointerRegClass - Return the register class to use to hold pointers.
112 /// This is used for addressing modes.
113 const TargetRegisterClass
*
114 PPCRegisterInfo::getPointerRegClass(const MachineFunction
&MF
, unsigned Kind
)
116 // Note that PPCInstrInfo::FoldImmediate also directly uses this Kind value
117 // when it checks for ZERO folding.
120 return &PPC::G8RC_NOX0RegClass
;
121 return &PPC::GPRC_NOR0RegClass
;
125 return &PPC::G8RCRegClass
;
126 return &PPC::GPRCRegClass
;
130 PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction
*MF
) const {
131 const PPCSubtarget
&Subtarget
= MF
->getSubtarget
<PPCSubtarget
>();
132 if (MF
->getFunction().getCallingConv() == CallingConv::AnyReg
) {
133 if (Subtarget
.hasVSX())
134 return CSR_64_AllRegs_VSX_SaveList
;
135 if (Subtarget
.hasAltivec())
136 return CSR_64_AllRegs_Altivec_SaveList
;
137 return CSR_64_AllRegs_SaveList
;
140 if (Subtarget
.isDarwinABI())
142 ? (Subtarget
.hasAltivec() ? CSR_Darwin64_Altivec_SaveList
143 : CSR_Darwin64_SaveList
)
144 : (Subtarget
.hasAltivec() ? CSR_Darwin32_Altivec_SaveList
145 : CSR_Darwin32_SaveList
);
147 if (TM
.isPPC64() && MF
->getInfo
<PPCFunctionInfo
>()->isSplitCSR())
148 return CSR_SRV464_TLS_PE_SaveList
;
150 // On PPC64, we might need to save r2 (but only if it is not reserved).
151 bool SaveR2
= MF
->getRegInfo().isAllocatable(PPC::X2
);
153 if (MF
->getFunction().getCallingConv() == CallingConv::Cold
) {
155 ? (Subtarget
.hasAltivec()
156 ? (SaveR2
? CSR_SVR64_ColdCC_R2_Altivec_SaveList
157 : CSR_SVR64_ColdCC_Altivec_SaveList
)
158 : (SaveR2
? CSR_SVR64_ColdCC_R2_SaveList
159 : CSR_SVR64_ColdCC_SaveList
))
160 : (Subtarget
.hasAltivec() ? CSR_SVR32_ColdCC_Altivec_SaveList
161 : CSR_SVR32_ColdCC_SaveList
);
165 ? (Subtarget
.hasAltivec()
166 ? (SaveR2
? CSR_SVR464_R2_Altivec_SaveList
167 : CSR_SVR464_Altivec_SaveList
)
168 : (SaveR2
? CSR_SVR464_R2_SaveList
: CSR_SVR464_SaveList
))
169 : (Subtarget
.hasAltivec() ? CSR_SVR432_Altivec_SaveList
170 : CSR_SVR432_SaveList
);
174 PPCRegisterInfo::getCalleeSavedRegsViaCopy(const MachineFunction
*MF
) const {
175 assert(MF
&& "Invalid MachineFunction pointer.");
176 const PPCSubtarget
&Subtarget
= MF
->getSubtarget
<PPCSubtarget
>();
177 if (Subtarget
.isDarwinABI())
181 if (MF
->getFunction().getCallingConv() != CallingConv::CXX_FAST_TLS
)
183 if (!MF
->getInfo
<PPCFunctionInfo
>()->isSplitCSR())
186 // On PPC64, we might need to save r2 (but only if it is not reserved).
187 bool SaveR2
= !getReservedRegs(*MF
).test(PPC::X2
);
188 if (Subtarget
.hasAltivec())
190 ? CSR_SVR464_R2_Altivec_ViaCopy_SaveList
191 : CSR_SVR464_Altivec_ViaCopy_SaveList
;
194 ? CSR_SVR464_R2_ViaCopy_SaveList
195 : CSR_SVR464_ViaCopy_SaveList
;
199 PPCRegisterInfo::getCallPreservedMask(const MachineFunction
&MF
,
200 CallingConv::ID CC
) const {
201 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
202 if (CC
== CallingConv::AnyReg
) {
203 if (Subtarget
.hasVSX())
204 return CSR_64_AllRegs_VSX_RegMask
;
205 if (Subtarget
.hasAltivec())
206 return CSR_64_AllRegs_Altivec_RegMask
;
207 return CSR_64_AllRegs_RegMask
;
210 if (Subtarget
.isDarwinABI())
211 return TM
.isPPC64() ? (Subtarget
.hasAltivec() ? CSR_Darwin64_Altivec_RegMask
212 : CSR_Darwin64_RegMask
)
213 : (Subtarget
.hasAltivec() ? CSR_Darwin32_Altivec_RegMask
214 : CSR_Darwin32_RegMask
);
216 if (CC
== CallingConv::Cold
) {
217 return TM
.isPPC64() ? (Subtarget
.hasAltivec() ? CSR_SVR64_ColdCC_Altivec_RegMask
218 : CSR_SVR64_ColdCC_RegMask
)
219 : (Subtarget
.hasAltivec() ? CSR_SVR32_ColdCC_Altivec_RegMask
220 : CSR_SVR32_ColdCC_RegMask
);
223 return TM
.isPPC64() ? (Subtarget
.hasAltivec() ? CSR_SVR464_Altivec_RegMask
224 : CSR_SVR464_RegMask
)
225 : (Subtarget
.hasAltivec() ? CSR_SVR432_Altivec_RegMask
226 : CSR_SVR432_RegMask
);
230 PPCRegisterInfo::getNoPreservedMask() const {
231 return CSR_NoRegs_RegMask
;
234 void PPCRegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask
) const {
235 for (unsigned PseudoReg
: {PPC::ZERO
, PPC::ZERO8
, PPC::RM
})
236 Mask
[PseudoReg
/ 32] &= ~(1u << (PseudoReg
% 32));
239 BitVector
PPCRegisterInfo::getReservedRegs(const MachineFunction
&MF
) const {
240 BitVector
Reserved(getNumRegs());
241 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
242 const PPCFrameLowering
*TFI
= getFrameLowering(MF
);
244 // The ZERO register is not really a register, but the representation of r0
245 // when used in instructions that treat r0 as the constant 0.
246 markSuperRegs(Reserved
, PPC::ZERO
);
248 // The FP register is also not really a register, but is the representation
249 // of the frame pointer register used by ISD::FRAMEADDR.
250 markSuperRegs(Reserved
, PPC::FP
);
252 // The BP register is also not really a register, but is the representation
253 // of the base pointer register used by setjmp.
254 markSuperRegs(Reserved
, PPC::BP
);
256 // The counter registers must be reserved so that counter-based loops can
257 // be correctly formed (and the mtctr instructions are not DCE'd).
258 markSuperRegs(Reserved
, PPC::CTR
);
259 markSuperRegs(Reserved
, PPC::CTR8
);
261 markSuperRegs(Reserved
, PPC::R1
);
262 markSuperRegs(Reserved
, PPC::LR
);
263 markSuperRegs(Reserved
, PPC::LR8
);
264 markSuperRegs(Reserved
, PPC::RM
);
266 if (!Subtarget
.isDarwinABI() || !Subtarget
.hasAltivec())
267 markSuperRegs(Reserved
, PPC::VRSAVE
);
269 // The SVR4 ABI reserves r2 and r13
270 if (Subtarget
.isSVR4ABI()) {
271 // We only reserve r2 if we need to use the TOC pointer. If we have no
272 // explicit uses of the TOC pointer (meaning we're a leaf function with
273 // no constant-pool loads, etc.) and we have no potential uses inside an
274 // inline asm block, then we can treat r2 has an ordinary callee-saved
276 const PPCFunctionInfo
*FuncInfo
= MF
.getInfo
<PPCFunctionInfo
>();
277 if (!TM
.isPPC64() || FuncInfo
->usesTOCBasePtr() || MF
.hasInlineAsm())
278 markSuperRegs(Reserved
, PPC::R2
); // System-reserved register
279 markSuperRegs(Reserved
, PPC::R13
); // Small Data Area pointer register
282 // On PPC64, r13 is the thread pointer. Never allocate this register.
284 markSuperRegs(Reserved
, PPC::R13
);
286 if (TFI
->needsFP(MF
))
287 markSuperRegs(Reserved
, PPC::R31
);
289 bool IsPositionIndependent
= TM
.isPositionIndependent();
290 if (hasBasePointer(MF
)) {
291 if (Subtarget
.isSVR4ABI() && !TM
.isPPC64() && IsPositionIndependent
)
292 markSuperRegs(Reserved
, PPC::R29
);
294 markSuperRegs(Reserved
, PPC::R30
);
297 if (Subtarget
.isSVR4ABI() && !TM
.isPPC64() && IsPositionIndependent
)
298 markSuperRegs(Reserved
, PPC::R30
);
300 // Reserve Altivec registers when Altivec is unavailable.
301 if (!Subtarget
.hasAltivec())
302 for (TargetRegisterClass::iterator I
= PPC::VRRCRegClass
.begin(),
303 IE
= PPC::VRRCRegClass
.end(); I
!= IE
; ++I
)
304 markSuperRegs(Reserved
, *I
);
306 assert(checkAllSuperRegsMarked(Reserved
));
310 bool PPCRegisterInfo::isCallerPreservedPhysReg(unsigned PhysReg
,
311 const MachineFunction
&MF
) const {
312 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg
));
313 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
314 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
318 if (!Subtarget
.isSVR4ABI())
320 if (PhysReg
== PPC::X2
)
321 // X2 is guaranteed to be preserved within a function if it is reserved.
322 // The reason it's reserved is that it's the TOC pointer (and the function
323 // uses the TOC). In functions where it isn't reserved (i.e. leaf functions
324 // with no TOC access), we can't claim that it is preserved.
325 return (getReservedRegs(MF
).test(PPC::X2
));
326 if (StackPtrConst
&& (PhysReg
== PPC::X1
) && !MFI
.hasVarSizedObjects()
327 && !MFI
.hasOpaqueSPAdjustment())
328 // The value of the stack pointer does not change within a function after
329 // the prologue and before the epilogue if there are no dynamic allocations
330 // and no inline asm which clobbers X1.
335 unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass
*RC
,
336 MachineFunction
&MF
) const {
337 const PPCFrameLowering
*TFI
= getFrameLowering(MF
);
338 const unsigned DefaultSafety
= 1;
340 switch (RC
->getID()) {
343 case PPC::G8RC_NOX0RegClassID
:
344 case PPC::GPRC_NOR0RegClassID
:
345 case PPC::G8RCRegClassID
:
346 case PPC::GPRCRegClassID
: {
347 unsigned FP
= TFI
->hasFP(MF
) ? 1 : 0;
348 return 32 - FP
- DefaultSafety
;
350 case PPC::F8RCRegClassID
:
351 case PPC::F4RCRegClassID
:
352 case PPC::QFRCRegClassID
:
353 case PPC::QSRCRegClassID
:
354 case PPC::QBRCRegClassID
:
355 case PPC::VRRCRegClassID
:
356 case PPC::VFRCRegClassID
:
357 case PPC::VSLRCRegClassID
:
358 return 32 - DefaultSafety
;
359 case PPC::VSRCRegClassID
:
360 case PPC::VSFRCRegClassID
:
361 case PPC::VSSRCRegClassID
:
362 return 64 - DefaultSafety
;
363 case PPC::CRRCRegClassID
:
364 return 8 - DefaultSafety
;
368 const TargetRegisterClass
*
369 PPCRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass
*RC
,
370 const MachineFunction
&MF
) const {
371 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
372 if (Subtarget
.hasVSX()) {
373 // With VSX, we can inflate various sub-register classes to the full VSX
376 // For Power9 we allow the user to enable GPR to vector spills.
377 // FIXME: Currently limited to spilling GP8RC. A follow on patch will add
378 // support to spill GPRC.
379 if (TM
.isELFv2ABI()) {
380 if (Subtarget
.hasP9Vector() && EnableGPRToVecSpills
&&
381 RC
== &PPC::G8RCRegClass
) {
383 return &PPC::SPILLTOVSRRCRegClass
;
385 if (RC
== &PPC::GPRCRegClass
&& EnableGPRToVecSpills
)
388 if (RC
== &PPC::F8RCRegClass
)
389 return &PPC::VSFRCRegClass
;
390 else if (RC
== &PPC::VRRCRegClass
)
391 return &PPC::VSRCRegClass
;
392 else if (RC
== &PPC::F4RCRegClass
&& Subtarget
.hasP8Vector())
393 return &PPC::VSSRCRegClass
;
396 return TargetRegisterInfo::getLargestLegalSuperClass(RC
, MF
);
399 //===----------------------------------------------------------------------===//
400 // Stack Frame Processing methods
401 //===----------------------------------------------------------------------===//
403 /// lowerDynamicAlloc - Generate the code for allocating an object in the
404 /// current frame. The sequence of code will be in the general form
406 /// addi R0, SP, \#frameSize ; get the address of the previous frame
407 /// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size
408 /// addi Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
410 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II
) const {
411 // Get the instruction.
412 MachineInstr
&MI
= *II
;
413 // Get the instruction's basic block.
414 MachineBasicBlock
&MBB
= *MI
.getParent();
415 // Get the basic block's function.
416 MachineFunction
&MF
= *MBB
.getParent();
417 // Get the frame info.
418 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
419 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
420 // Get the instruction info.
421 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
422 // Determine whether 64-bit pointers are used.
423 bool LP64
= TM
.isPPC64();
424 DebugLoc dl
= MI
.getDebugLoc();
426 // Get the maximum call stack size.
427 unsigned maxCallFrameSize
= MFI
.getMaxCallFrameSize();
428 // Get the total frame size.
429 unsigned FrameSize
= MFI
.getStackSize();
431 // Get stack alignments.
432 const PPCFrameLowering
*TFI
= getFrameLowering(MF
);
433 unsigned TargetAlign
= TFI
->getStackAlignment();
434 unsigned MaxAlign
= MFI
.getMaxAlignment();
435 assert((maxCallFrameSize
& (MaxAlign
-1)) == 0 &&
436 "Maximum call-frame size not sufficiently aligned");
438 // Determine the previous frame's address. If FrameSize can't be
439 // represented as 16 bits or we need special alignment, then we load the
440 // previous frame's address from 0(SP). Why not do an addis of the hi?
441 // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
442 // Constructing the constant and adding would take 3 instructions.
443 // Fortunately, a frame greater than 32K is rare.
444 const TargetRegisterClass
*G8RC
= &PPC::G8RCRegClass
;
445 const TargetRegisterClass
*GPRC
= &PPC::GPRCRegClass
;
446 unsigned Reg
= MF
.getRegInfo().createVirtualRegister(LP64
? G8RC
: GPRC
);
448 if (MaxAlign
< TargetAlign
&& isInt
<16>(FrameSize
)) {
450 BuildMI(MBB
, II
, dl
, TII
.get(PPC::ADDI8
), Reg
)
454 BuildMI(MBB
, II
, dl
, TII
.get(PPC::ADDI
), Reg
)
458 BuildMI(MBB
, II
, dl
, TII
.get(PPC::LD
), Reg
)
462 BuildMI(MBB
, II
, dl
, TII
.get(PPC::LWZ
), Reg
)
467 bool KillNegSizeReg
= MI
.getOperand(1).isKill();
468 unsigned NegSizeReg
= MI
.getOperand(1).getReg();
470 // Grow the stack and update the stack pointer link, then determine the
471 // address of new allocated space.
473 if (MaxAlign
> TargetAlign
) {
474 unsigned UnalNegSizeReg
= NegSizeReg
;
475 NegSizeReg
= MF
.getRegInfo().createVirtualRegister(G8RC
);
477 // Unfortunately, there is no andi, only andi., and we can't insert that
478 // here because we might clobber cr0 while it is live.
479 BuildMI(MBB
, II
, dl
, TII
.get(PPC::LI8
), NegSizeReg
)
480 .addImm(~(MaxAlign
-1));
482 unsigned NegSizeReg1
= NegSizeReg
;
483 NegSizeReg
= MF
.getRegInfo().createVirtualRegister(G8RC
);
484 BuildMI(MBB
, II
, dl
, TII
.get(PPC::AND8
), NegSizeReg
)
485 .addReg(UnalNegSizeReg
, getKillRegState(KillNegSizeReg
))
486 .addReg(NegSizeReg1
, RegState::Kill
);
487 KillNegSizeReg
= true;
490 BuildMI(MBB
, II
, dl
, TII
.get(PPC::STDUX
), PPC::X1
)
491 .addReg(Reg
, RegState::Kill
)
493 .addReg(NegSizeReg
, getKillRegState(KillNegSizeReg
));
494 BuildMI(MBB
, II
, dl
, TII
.get(PPC::ADDI8
), MI
.getOperand(0).getReg())
496 .addImm(maxCallFrameSize
);
498 if (MaxAlign
> TargetAlign
) {
499 unsigned UnalNegSizeReg
= NegSizeReg
;
500 NegSizeReg
= MF
.getRegInfo().createVirtualRegister(GPRC
);
502 // Unfortunately, there is no andi, only andi., and we can't insert that
503 // here because we might clobber cr0 while it is live.
504 BuildMI(MBB
, II
, dl
, TII
.get(PPC::LI
), NegSizeReg
)
505 .addImm(~(MaxAlign
-1));
507 unsigned NegSizeReg1
= NegSizeReg
;
508 NegSizeReg
= MF
.getRegInfo().createVirtualRegister(GPRC
);
509 BuildMI(MBB
, II
, dl
, TII
.get(PPC::AND
), NegSizeReg
)
510 .addReg(UnalNegSizeReg
, getKillRegState(KillNegSizeReg
))
511 .addReg(NegSizeReg1
, RegState::Kill
);
512 KillNegSizeReg
= true;
515 BuildMI(MBB
, II
, dl
, TII
.get(PPC::STWUX
), PPC::R1
)
516 .addReg(Reg
, RegState::Kill
)
518 .addReg(NegSizeReg
, getKillRegState(KillNegSizeReg
));
519 BuildMI(MBB
, II
, dl
, TII
.get(PPC::ADDI
), MI
.getOperand(0).getReg())
521 .addImm(maxCallFrameSize
);
524 // Discard the DYNALLOC instruction.
528 void PPCRegisterInfo::lowerDynamicAreaOffset(
529 MachineBasicBlock::iterator II
) const {
530 // Get the instruction.
531 MachineInstr
&MI
= *II
;
532 // Get the instruction's basic block.
533 MachineBasicBlock
&MBB
= *MI
.getParent();
534 // Get the basic block's function.
535 MachineFunction
&MF
= *MBB
.getParent();
536 // Get the frame info.
537 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
538 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
539 // Get the instruction info.
540 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
542 unsigned maxCallFrameSize
= MFI
.getMaxCallFrameSize();
543 bool is64Bit
= TM
.isPPC64();
544 DebugLoc dl
= MI
.getDebugLoc();
545 BuildMI(MBB
, II
, dl
, TII
.get(is64Bit
? PPC::LI8
: PPC::LI
),
546 MI
.getOperand(0).getReg())
547 .addImm(maxCallFrameSize
);
551 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
552 /// reserving a whole register (R0), we scrounge for one here. This generates
555 /// mfcr rA ; Move the conditional register into GPR rA.
556 /// rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
557 /// stw rA, FI ; Store rA to the frame.
559 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II
,
560 unsigned FrameIndex
) const {
561 // Get the instruction.
562 MachineInstr
&MI
= *II
; // ; SPILL_CR <SrcReg>, <offset>
563 // Get the instruction's basic block.
564 MachineBasicBlock
&MBB
= *MI
.getParent();
565 MachineFunction
&MF
= *MBB
.getParent();
566 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
567 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
568 DebugLoc dl
= MI
.getDebugLoc();
570 bool LP64
= TM
.isPPC64();
571 const TargetRegisterClass
*G8RC
= &PPC::G8RCRegClass
;
572 const TargetRegisterClass
*GPRC
= &PPC::GPRCRegClass
;
574 unsigned Reg
= MF
.getRegInfo().createVirtualRegister(LP64
? G8RC
: GPRC
);
575 unsigned SrcReg
= MI
.getOperand(0).getReg();
577 // We need to store the CR in the low 4-bits of the saved value. First, issue
578 // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg.
579 BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::MFOCRF8
: PPC::MFOCRF
), Reg
)
580 .addReg(SrcReg
, getKillRegState(MI
.getOperand(0).isKill()));
582 // If the saved register wasn't CR0, shift the bits left so that they are in
584 if (SrcReg
!= PPC::CR0
) {
586 Reg
= MF
.getRegInfo().createVirtualRegister(LP64
? G8RC
: GPRC
);
588 // rlwinm rA, rA, ShiftBits, 0, 31.
589 BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::RLWINM8
: PPC::RLWINM
), Reg
)
590 .addReg(Reg1
, RegState::Kill
)
591 .addImm(getEncodingValue(SrcReg
) * 4)
596 addFrameReference(BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::STW8
: PPC::STW
))
597 .addReg(Reg
, RegState::Kill
),
600 // Discard the pseudo instruction.
604 void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II
,
605 unsigned FrameIndex
) const {
606 // Get the instruction.
607 MachineInstr
&MI
= *II
; // ; <DestReg> = RESTORE_CR <offset>
608 // Get the instruction's basic block.
609 MachineBasicBlock
&MBB
= *MI
.getParent();
610 MachineFunction
&MF
= *MBB
.getParent();
611 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
612 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
613 DebugLoc dl
= MI
.getDebugLoc();
615 bool LP64
= TM
.isPPC64();
616 const TargetRegisterClass
*G8RC
= &PPC::G8RCRegClass
;
617 const TargetRegisterClass
*GPRC
= &PPC::GPRCRegClass
;
619 unsigned Reg
= MF
.getRegInfo().createVirtualRegister(LP64
? G8RC
: GPRC
);
620 unsigned DestReg
= MI
.getOperand(0).getReg();
621 assert(MI
.definesRegister(DestReg
) &&
622 "RESTORE_CR does not define its destination");
624 addFrameReference(BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::LWZ8
: PPC::LWZ
),
627 // If the reloaded register isn't CR0, shift the bits right so that they are
628 // in the right CR's slot.
629 if (DestReg
!= PPC::CR0
) {
631 Reg
= MF
.getRegInfo().createVirtualRegister(LP64
? G8RC
: GPRC
);
633 unsigned ShiftBits
= getEncodingValue(DestReg
)*4;
634 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
635 BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::RLWINM8
: PPC::RLWINM
), Reg
)
636 .addReg(Reg1
, RegState::Kill
).addImm(32-ShiftBits
).addImm(0)
640 BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::MTOCRF8
: PPC::MTOCRF
), DestReg
)
641 .addReg(Reg
, RegState::Kill
);
643 // Discard the pseudo instruction.
647 void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II
,
648 unsigned FrameIndex
) const {
649 // Get the instruction.
650 MachineInstr
&MI
= *II
; // ; SPILL_CRBIT <SrcReg>, <offset>
651 // Get the instruction's basic block.
652 MachineBasicBlock
&MBB
= *MI
.getParent();
653 MachineFunction
&MF
= *MBB
.getParent();
654 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
655 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
656 DebugLoc dl
= MI
.getDebugLoc();
658 bool LP64
= TM
.isPPC64();
659 const TargetRegisterClass
*G8RC
= &PPC::G8RCRegClass
;
660 const TargetRegisterClass
*GPRC
= &PPC::GPRCRegClass
;
662 unsigned Reg
= MF
.getRegInfo().createVirtualRegister(LP64
? G8RC
: GPRC
);
663 unsigned SrcReg
= MI
.getOperand(0).getReg();
665 BuildMI(MBB
, II
, dl
, TII
.get(TargetOpcode::KILL
),
666 getCRFromCRBit(SrcReg
))
667 .addReg(SrcReg
, getKillRegState(MI
.getOperand(0).isKill()));
669 BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::MFOCRF8
: PPC::MFOCRF
), Reg
)
670 .addReg(getCRFromCRBit(SrcReg
));
672 // If the saved register wasn't CR0LT, shift the bits left so that the bit to
673 // store is the first one. Mask all but that bit.
675 Reg
= MF
.getRegInfo().createVirtualRegister(LP64
? G8RC
: GPRC
);
677 // rlwinm rA, rA, ShiftBits, 0, 0.
678 BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::RLWINM8
: PPC::RLWINM
), Reg
)
679 .addReg(Reg1
, RegState::Kill
)
680 .addImm(getEncodingValue(SrcReg
))
681 .addImm(0).addImm(0);
683 addFrameReference(BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::STW8
: PPC::STW
))
684 .addReg(Reg
, RegState::Kill
),
687 // Discard the pseudo instruction.
691 void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II
,
692 unsigned FrameIndex
) const {
693 // Get the instruction.
694 MachineInstr
&MI
= *II
; // ; <DestReg> = RESTORE_CRBIT <offset>
695 // Get the instruction's basic block.
696 MachineBasicBlock
&MBB
= *MI
.getParent();
697 MachineFunction
&MF
= *MBB
.getParent();
698 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
699 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
700 DebugLoc dl
= MI
.getDebugLoc();
702 bool LP64
= TM
.isPPC64();
703 const TargetRegisterClass
*G8RC
= &PPC::G8RCRegClass
;
704 const TargetRegisterClass
*GPRC
= &PPC::GPRCRegClass
;
706 unsigned Reg
= MF
.getRegInfo().createVirtualRegister(LP64
? G8RC
: GPRC
);
707 unsigned DestReg
= MI
.getOperand(0).getReg();
708 assert(MI
.definesRegister(DestReg
) &&
709 "RESTORE_CRBIT does not define its destination");
711 addFrameReference(BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::LWZ8
: PPC::LWZ
),
714 BuildMI(MBB
, II
, dl
, TII
.get(TargetOpcode::IMPLICIT_DEF
), DestReg
);
716 unsigned RegO
= MF
.getRegInfo().createVirtualRegister(LP64
? G8RC
: GPRC
);
717 BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::MFOCRF8
: PPC::MFOCRF
), RegO
)
718 .addReg(getCRFromCRBit(DestReg
));
720 unsigned ShiftBits
= getEncodingValue(DestReg
);
721 // rlwimi r11, r10, 32-ShiftBits, ..., ...
722 BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::RLWIMI8
: PPC::RLWIMI
), RegO
)
723 .addReg(RegO
, RegState::Kill
)
724 .addReg(Reg
, RegState::Kill
)
725 .addImm(ShiftBits
? 32 - ShiftBits
: 0)
729 BuildMI(MBB
, II
, dl
, TII
.get(LP64
? PPC::MTOCRF8
: PPC::MTOCRF
),
730 getCRFromCRBit(DestReg
))
731 .addReg(RegO
, RegState::Kill
)
732 // Make sure we have a use dependency all the way through this
733 // sequence of instructions. We can't have the other bits in the CR
734 // modified in between the mfocrf and the mtocrf.
735 .addReg(getCRFromCRBit(DestReg
), RegState::Implicit
);
737 // Discard the pseudo instruction.
741 void PPCRegisterInfo::lowerVRSAVESpilling(MachineBasicBlock::iterator II
,
742 unsigned FrameIndex
) const {
743 // Get the instruction.
744 MachineInstr
&MI
= *II
; // ; SPILL_VRSAVE <SrcReg>, <offset>
745 // Get the instruction's basic block.
746 MachineBasicBlock
&MBB
= *MI
.getParent();
747 MachineFunction
&MF
= *MBB
.getParent();
748 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
749 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
750 DebugLoc dl
= MI
.getDebugLoc();
752 const TargetRegisterClass
*GPRC
= &PPC::GPRCRegClass
;
753 unsigned Reg
= MF
.getRegInfo().createVirtualRegister(GPRC
);
754 unsigned SrcReg
= MI
.getOperand(0).getReg();
756 BuildMI(MBB
, II
, dl
, TII
.get(PPC::MFVRSAVEv
), Reg
)
757 .addReg(SrcReg
, getKillRegState(MI
.getOperand(0).isKill()));
760 BuildMI(MBB
, II
, dl
, TII
.get(PPC::STW
)).addReg(Reg
, RegState::Kill
),
763 // Discard the pseudo instruction.
767 void PPCRegisterInfo::lowerVRSAVERestore(MachineBasicBlock::iterator II
,
768 unsigned FrameIndex
) const {
769 // Get the instruction.
770 MachineInstr
&MI
= *II
; // ; <DestReg> = RESTORE_VRSAVE <offset>
771 // Get the instruction's basic block.
772 MachineBasicBlock
&MBB
= *MI
.getParent();
773 MachineFunction
&MF
= *MBB
.getParent();
774 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
775 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
776 DebugLoc dl
= MI
.getDebugLoc();
778 const TargetRegisterClass
*GPRC
= &PPC::GPRCRegClass
;
779 unsigned Reg
= MF
.getRegInfo().createVirtualRegister(GPRC
);
780 unsigned DestReg
= MI
.getOperand(0).getReg();
781 assert(MI
.definesRegister(DestReg
) &&
782 "RESTORE_VRSAVE does not define its destination");
784 addFrameReference(BuildMI(MBB
, II
, dl
, TII
.get(PPC::LWZ
),
787 BuildMI(MBB
, II
, dl
, TII
.get(PPC::MTVRSAVEv
), DestReg
)
788 .addReg(Reg
, RegState::Kill
);
790 // Discard the pseudo instruction.
794 bool PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction
&MF
,
795 unsigned Reg
, int &FrameIdx
) const {
796 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
797 // For the nonvolatile condition registers (CR2, CR3, CR4) in an SVR4
798 // ABI, return true to prevent allocating an additional frame slot.
799 // For 64-bit, the CR save area is at SP+8; the value of FrameIdx = 0
800 // is arbitrary and will be subsequently ignored. For 32-bit, we have
801 // previously created the stack slot if needed, so return its FrameIdx.
802 if (Subtarget
.isSVR4ABI() && PPC::CR2
<= Reg
&& Reg
<= PPC::CR4
) {
806 const PPCFunctionInfo
*FI
= MF
.getInfo
<PPCFunctionInfo
>();
807 FrameIdx
= FI
->getCRSpillFrameIndex();
814 // If the offset must be a multiple of some value, return what that value is.
815 static unsigned offsetMinAlign(const MachineInstr
&MI
) {
816 unsigned OpC
= MI
.getOpcode();
829 case PPC::DFSTOREf32
:
830 case PPC::DFSTOREf64
:
842 // Return the OffsetOperandNo given the FIOperandNum (and the instruction).
843 static unsigned getOffsetONFromFION(const MachineInstr
&MI
,
844 unsigned FIOperandNum
) {
845 // Take into account whether it's an add or mem instruction
846 unsigned OffsetOperandNo
= (FIOperandNum
== 2) ? 1 : 2;
847 if (MI
.isInlineAsm())
848 OffsetOperandNo
= FIOperandNum
- 1;
849 else if (MI
.getOpcode() == TargetOpcode::STACKMAP
||
850 MI
.getOpcode() == TargetOpcode::PATCHPOINT
)
851 OffsetOperandNo
= FIOperandNum
+ 1;
853 return OffsetOperandNo
;
857 PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II
,
858 int SPAdj
, unsigned FIOperandNum
,
859 RegScavenger
*RS
) const {
860 assert(SPAdj
== 0 && "Unexpected");
862 // Get the instruction.
863 MachineInstr
&MI
= *II
;
864 // Get the instruction's basic block.
865 MachineBasicBlock
&MBB
= *MI
.getParent();
866 // Get the basic block's function.
867 MachineFunction
&MF
= *MBB
.getParent();
868 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
869 // Get the instruction info.
870 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
871 // Get the frame info.
872 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
873 DebugLoc dl
= MI
.getDebugLoc();
875 unsigned OffsetOperandNo
= getOffsetONFromFION(MI
, FIOperandNum
);
877 // Get the frame index.
878 int FrameIndex
= MI
.getOperand(FIOperandNum
).getIndex();
880 // Get the frame pointer save index. Users of this index are primarily
881 // DYNALLOC instructions.
882 PPCFunctionInfo
*FI
= MF
.getInfo
<PPCFunctionInfo
>();
883 int FPSI
= FI
->getFramePointerSaveIndex();
884 // Get the instruction opcode.
885 unsigned OpC
= MI
.getOpcode();
887 if ((OpC
== PPC::DYNAREAOFFSET
|| OpC
== PPC::DYNAREAOFFSET8
)) {
888 lowerDynamicAreaOffset(II
);
892 // Special case for dynamic alloca.
893 if (FPSI
&& FrameIndex
== FPSI
&&
894 (OpC
== PPC::DYNALLOC
|| OpC
== PPC::DYNALLOC8
)) {
895 lowerDynamicAlloc(II
);
899 // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc.
900 if (OpC
== PPC::SPILL_CR
) {
901 lowerCRSpilling(II
, FrameIndex
);
903 } else if (OpC
== PPC::RESTORE_CR
) {
904 lowerCRRestore(II
, FrameIndex
);
906 } else if (OpC
== PPC::SPILL_CRBIT
) {
907 lowerCRBitSpilling(II
, FrameIndex
);
909 } else if (OpC
== PPC::RESTORE_CRBIT
) {
910 lowerCRBitRestore(II
, FrameIndex
);
912 } else if (OpC
== PPC::SPILL_VRSAVE
) {
913 lowerVRSAVESpilling(II
, FrameIndex
);
915 } else if (OpC
== PPC::RESTORE_VRSAVE
) {
916 lowerVRSAVERestore(II
, FrameIndex
);
920 // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
921 MI
.getOperand(FIOperandNum
).ChangeToRegister(
922 FrameIndex
< 0 ? getBaseRegister(MF
) : getFrameRegister(MF
), false);
924 // If the instruction is not present in ImmToIdxMap, then it has no immediate
925 // form (and must be r+r).
926 bool noImmForm
= !MI
.isInlineAsm() && OpC
!= TargetOpcode::STACKMAP
&&
927 OpC
!= TargetOpcode::PATCHPOINT
&& !ImmToIdxMap
.count(OpC
);
929 // Now add the frame object offset to the offset from r1.
930 int Offset
= MFI
.getObjectOffset(FrameIndex
);
931 Offset
+= MI
.getOperand(OffsetOperandNo
).getImm();
933 // If we're not using a Frame Pointer that has been set to the value of the
934 // SP before having the stack size subtracted from it, then add the stack size
935 // to Offset to get the correct offset.
936 // Naked functions have stack size 0, although getStackSize may not reflect
937 // that because we didn't call all the pieces that compute it for naked
939 if (!MF
.getFunction().hasFnAttribute(Attribute::Naked
)) {
940 if (!(hasBasePointer(MF
) && FrameIndex
< 0))
941 Offset
+= MFI
.getStackSize();
944 // If we can, encode the offset directly into the instruction. If this is a
945 // normal PPC "ri" instruction, any 16-bit value can be safely encoded. If
946 // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
947 // clear can be encoded. This is extremely uncommon, because normally you
948 // only "std" to a stack slot that is at least 4-byte aligned, but it can
949 // happen in invalid code.
950 assert(OpC
!= PPC::DBG_VALUE
&&
951 "This should be handled in a target-independent way");
952 if (!noImmForm
&& ((isInt
<16>(Offset
) &&
953 ((Offset
% offsetMinAlign(MI
)) == 0)) ||
954 OpC
== TargetOpcode::STACKMAP
||
955 OpC
== TargetOpcode::PATCHPOINT
)) {
956 MI
.getOperand(OffsetOperandNo
).ChangeToImmediate(Offset
);
960 // The offset doesn't fit into a single register, scavenge one to build the
963 bool is64Bit
= TM
.isPPC64();
964 const TargetRegisterClass
*G8RC
= &PPC::G8RCRegClass
;
965 const TargetRegisterClass
*GPRC
= &PPC::GPRCRegClass
;
966 const TargetRegisterClass
*RC
= is64Bit
? G8RC
: GPRC
;
967 unsigned SRegHi
= MF
.getRegInfo().createVirtualRegister(RC
),
968 SReg
= MF
.getRegInfo().createVirtualRegister(RC
);
970 // Insert a set of rA with the full offset value before the ld, st, or add
971 if (isInt
<16>(Offset
))
972 BuildMI(MBB
, II
, dl
, TII
.get(is64Bit
? PPC::LI8
: PPC::LI
), SReg
)
975 BuildMI(MBB
, II
, dl
, TII
.get(is64Bit
? PPC::LIS8
: PPC::LIS
), SRegHi
)
976 .addImm(Offset
>> 16);
977 BuildMI(MBB
, II
, dl
, TII
.get(is64Bit
? PPC::ORI8
: PPC::ORI
), SReg
)
978 .addReg(SRegHi
, RegState::Kill
)
982 // Convert into indexed form of the instruction:
984 // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
985 // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
986 unsigned OperandBase
;
990 else if (OpC
!= TargetOpcode::INLINEASM
) {
991 assert(ImmToIdxMap
.count(OpC
) &&
992 "No indexed form of load or store available!");
993 unsigned NewOpcode
= ImmToIdxMap
.find(OpC
)->second
;
994 MI
.setDesc(TII
.get(NewOpcode
));
997 OperandBase
= OffsetOperandNo
;
1000 unsigned StackReg
= MI
.getOperand(FIOperandNum
).getReg();
1001 MI
.getOperand(OperandBase
).ChangeToRegister(StackReg
, false);
1002 MI
.getOperand(OperandBase
+ 1).ChangeToRegister(SReg
, false, false, true);
1005 unsigned PPCRegisterInfo::getFrameRegister(const MachineFunction
&MF
) const {
1006 const PPCFrameLowering
*TFI
= getFrameLowering(MF
);
1009 return TFI
->hasFP(MF
) ? PPC::R31
: PPC::R1
;
1011 return TFI
->hasFP(MF
) ? PPC::X31
: PPC::X1
;
1014 unsigned PPCRegisterInfo::getBaseRegister(const MachineFunction
&MF
) const {
1015 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
1016 if (!hasBasePointer(MF
))
1017 return getFrameRegister(MF
);
1022 if (Subtarget
.isSVR4ABI() && TM
.isPositionIndependent())
1028 bool PPCRegisterInfo::hasBasePointer(const MachineFunction
&MF
) const {
1029 if (!EnableBasePointer
)
1031 if (AlwaysBasePointer
)
1034 // If we need to realign the stack, then the stack pointer can no longer
1035 // serve as an offset into the caller's stack space. As a result, we need a
1037 return needsStackRealignment(MF
);
1040 /// Returns true if the instruction's frame index
1041 /// reference would be better served by a base register other than FP
1042 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
1043 /// references it should create new base registers for.
1044 bool PPCRegisterInfo::
1045 needsFrameBaseReg(MachineInstr
*MI
, int64_t Offset
) const {
1046 assert(Offset
< 0 && "Local offset must be negative");
1048 // It's the load/store FI references that cause issues, as it can be difficult
1049 // to materialize the offset if it won't fit in the literal field. Estimate
1050 // based on the size of the local frame and some conservative assumptions
1051 // about the rest of the stack frame (note, this is pre-regalloc, so
1052 // we don't know everything for certain yet) whether this offset is likely
1053 // to be out of range of the immediate. Return true if so.
1055 // We only generate virtual base registers for loads and stores that have
1056 // an r+i form. Return false for everything else.
1057 unsigned OpC
= MI
->getOpcode();
1058 if (!ImmToIdxMap
.count(OpC
))
1061 // Don't generate a new virtual base register just to add zero to it.
1062 if ((OpC
== PPC::ADDI
|| OpC
== PPC::ADDI8
) &&
1063 MI
->getOperand(2).getImm() == 0)
1066 MachineBasicBlock
&MBB
= *MI
->getParent();
1067 MachineFunction
&MF
= *MBB
.getParent();
1068 const PPCFrameLowering
*TFI
= getFrameLowering(MF
);
1069 unsigned StackEst
= TFI
->determineFrameLayout(MF
, false, true);
1071 // If we likely don't need a stack frame, then we probably don't need a
1072 // virtual base register either.
1076 // Estimate an offset from the stack pointer.
1077 // The incoming offset is relating to the SP at the start of the function,
1078 // but when we access the local it'll be relative to the SP after local
1079 // allocation, so adjust our SP-relative offset by that allocation size.
1082 // The frame pointer will point to the end of the stack, so estimate the
1083 // offset as the difference between the object offset and the FP location.
1084 return !isFrameOffsetLegal(MI
, getBaseRegister(MF
), Offset
);
1087 /// Insert defining instruction(s) for BaseReg to
1088 /// be a pointer to FrameIdx at the beginning of the basic block.
1089 void PPCRegisterInfo::
1090 materializeFrameBaseRegister(MachineBasicBlock
*MBB
,
1091 unsigned BaseReg
, int FrameIdx
,
1092 int64_t Offset
) const {
1093 unsigned ADDriOpc
= TM
.isPPC64() ? PPC::ADDI8
: PPC::ADDI
;
1095 MachineBasicBlock::iterator Ins
= MBB
->begin();
1096 DebugLoc DL
; // Defaults to "unknown"
1097 if (Ins
!= MBB
->end())
1098 DL
= Ins
->getDebugLoc();
1100 const MachineFunction
&MF
= *MBB
->getParent();
1101 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
1102 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
1103 const MCInstrDesc
&MCID
= TII
.get(ADDriOpc
);
1104 MachineRegisterInfo
&MRI
= MBB
->getParent()->getRegInfo();
1105 MRI
.constrainRegClass(BaseReg
, TII
.getRegClass(MCID
, 0, this, MF
));
1107 BuildMI(*MBB
, Ins
, DL
, MCID
, BaseReg
)
1108 .addFrameIndex(FrameIdx
).addImm(Offset
);
1111 void PPCRegisterInfo::resolveFrameIndex(MachineInstr
&MI
, unsigned BaseReg
,
1112 int64_t Offset
) const {
1113 unsigned FIOperandNum
= 0;
1114 while (!MI
.getOperand(FIOperandNum
).isFI()) {
1116 assert(FIOperandNum
< MI
.getNumOperands() &&
1117 "Instr doesn't have FrameIndex operand!");
1120 MI
.getOperand(FIOperandNum
).ChangeToRegister(BaseReg
, false);
1121 unsigned OffsetOperandNo
= getOffsetONFromFION(MI
, FIOperandNum
);
1122 Offset
+= MI
.getOperand(OffsetOperandNo
).getImm();
1123 MI
.getOperand(OffsetOperandNo
).ChangeToImmediate(Offset
);
1125 MachineBasicBlock
&MBB
= *MI
.getParent();
1126 MachineFunction
&MF
= *MBB
.getParent();
1127 const PPCSubtarget
&Subtarget
= MF
.getSubtarget
<PPCSubtarget
>();
1128 const TargetInstrInfo
&TII
= *Subtarget
.getInstrInfo();
1129 const MCInstrDesc
&MCID
= MI
.getDesc();
1130 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
1131 MRI
.constrainRegClass(BaseReg
,
1132 TII
.getRegClass(MCID
, FIOperandNum
, this, MF
));
1135 bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr
*MI
,
1137 int64_t Offset
) const {
1138 unsigned FIOperandNum
= 0;
1139 while (!MI
->getOperand(FIOperandNum
).isFI()) {
1141 assert(FIOperandNum
< MI
->getNumOperands() &&
1142 "Instr doesn't have FrameIndex operand!");
1145 unsigned OffsetOperandNo
= getOffsetONFromFION(*MI
, FIOperandNum
);
1146 Offset
+= MI
->getOperand(OffsetOperandNo
).getImm();
1148 return MI
->getOpcode() == PPC::DBG_VALUE
|| // DBG_VALUE is always Reg+Imm
1149 MI
->getOpcode() == TargetOpcode::STACKMAP
||
1150 MI
->getOpcode() == TargetOpcode::PATCHPOINT
||
1151 (isInt
<16>(Offset
) && (Offset
% offsetMinAlign(*MI
)) == 0);