1 //===- ARMRegisterInfo.cpp - ARM Register Information -----------*- C++ -*-===//
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 ARM implementation of the TargetRegisterInfo class.
12 //===----------------------------------------------------------------------===//
15 #include "ARMAddressingModes.h"
16 #include "ARMInstrInfo.h"
17 #include "ARMMachineFunctionInfo.h"
18 #include "ARMRegisterInfo.h"
19 #include "ARMSubtarget.h"
20 #include "llvm/Constants.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/CodeGen/MachineConstantPool.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/MachineLocation.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/RegisterScavenging.h"
29 #include "llvm/Target/TargetFrameInfo.h"
30 #include "llvm/Target/TargetMachine.h"
31 #include "llvm/Target/TargetOptions.h"
32 #include "llvm/ADT/BitVector.h"
33 #include "llvm/ADT/SmallVector.h"
34 #include "llvm/ADT/STLExtras.h"
35 #include "llvm/Support/CommandLine.h"
39 static cl::opt
<bool> ThumbRegScavenging("enable-thumb-reg-scavenging",
41 cl::desc("Enable register scavenging on Thumb"));
43 unsigned ARMRegisterInfo::getRegisterNumbering(unsigned RegEnum
) {
46 case R0
: case S0
: case D0
: return 0;
47 case R1
: case S1
: case D1
: return 1;
48 case R2
: case S2
: case D2
: return 2;
49 case R3
: case S3
: case D3
: return 3;
50 case R4
: case S4
: case D4
: return 4;
51 case R5
: case S5
: case D5
: return 5;
52 case R6
: case S6
: case D6
: return 6;
53 case R7
: case S7
: case D7
: return 7;
54 case R8
: case S8
: case D8
: return 8;
55 case R9
: case S9
: case D9
: return 9;
56 case R10
: case S10
: case D10
: return 10;
57 case R11
: case S11
: case D11
: return 11;
58 case R12
: case S12
: case D12
: return 12;
59 case SP
: case S13
: case D13
: return 13;
60 case LR
: case S14
: case D14
: return 14;
61 case PC
: case S15
: case D15
: return 15;
79 assert(0 && "Unknown ARM register!");
84 unsigned ARMRegisterInfo::getRegisterNumbering(unsigned RegEnum
,
91 assert(0 && "Unknown ARM register!");
93 case R0
: case D0
: return 0;
94 case R1
: case D1
: return 1;
95 case R2
: case D2
: return 2;
96 case R3
: case D3
: return 3;
97 case R4
: case D4
: return 4;
98 case R5
: case D5
: return 5;
99 case R6
: case D6
: return 6;
100 case R7
: case D7
: return 7;
101 case R8
: case D8
: return 8;
102 case R9
: case D9
: return 9;
103 case R10
: case D10
: return 10;
104 case R11
: case D11
: return 11;
105 case R12
: case D12
: return 12;
106 case SP
: case D13
: return 13;
107 case LR
: case D14
: return 14;
108 case PC
: case D15
: return 15;
110 case S0
: case S1
: case S2
: case S3
:
111 case S4
: case S5
: case S6
: case S7
:
112 case S8
: case S9
: case S10
: case S11
:
113 case S12
: case S13
: case S14
: case S15
:
114 case S16
: case S17
: case S18
: case S19
:
115 case S20
: case S21
: case S22
: case S23
:
116 case S24
: case S25
: case S26
: case S27
:
117 case S28
: case S29
: case S30
: case S31
: {
120 default: return 0; // Avoid compile time warning.
158 ARMRegisterInfo::ARMRegisterInfo(const TargetInstrInfo
&tii
,
159 const ARMSubtarget
&sti
)
160 : ARMGenRegisterInfo(ARM::ADJCALLSTACKDOWN
, ARM::ADJCALLSTACKUP
),
162 FramePtr((STI
.useThumbBacktraces() || STI
.isThumb()) ? ARM::R7
: ARM::R11
) {
166 const MachineInstrBuilder
&AddDefaultPred(const MachineInstrBuilder
&MIB
) {
167 return MIB
.addImm((int64_t)ARMCC::AL
).addReg(0);
171 const MachineInstrBuilder
&AddDefaultCC(const MachineInstrBuilder
&MIB
) {
172 return MIB
.addReg(0);
175 /// emitLoadConstPool - Emits a load from constpool to materialize the
176 /// specified immediate.
177 void ARMRegisterInfo::emitLoadConstPool(MachineBasicBlock
&MBB
,
178 MachineBasicBlock::iterator
&MBBI
,
179 unsigned DestReg
, int Val
,
180 unsigned Pred
, unsigned PredReg
,
181 const TargetInstrInfo
*TII
,
184 MachineFunction
&MF
= *MBB
.getParent();
185 MachineConstantPool
*ConstantPool
= MF
.getConstantPool();
186 Constant
*C
= ConstantInt::get(Type::Int32Ty
, Val
);
187 unsigned Idx
= ConstantPool
->getConstantPoolIndex(C
, 4);
189 BuildMI(MBB
, MBBI
, dl
,
190 TII
->get(ARM::tLDRcp
),DestReg
).addConstantPoolIndex(Idx
);
192 BuildMI(MBB
, MBBI
, dl
, TII
->get(ARM::LDRcp
), DestReg
)
193 .addConstantPoolIndex(Idx
)
194 .addReg(0).addImm(0).addImm(Pred
).addReg(PredReg
);
197 const TargetRegisterClass
*ARMRegisterInfo::getPointerRegClass() const {
198 return &ARM::GPRRegClass
;
201 /// isLowRegister - Returns true if the register is low register r0-r7.
203 bool ARMRegisterInfo::isLowRegister(unsigned Reg
) const {
206 case R0
: case R1
: case R2
: case R3
:
207 case R4
: case R5
: case R6
: case R7
:
214 const TargetRegisterClass
*
215 ARMRegisterInfo::getPhysicalRegisterRegClass(unsigned Reg
, MVT VT
) const {
217 if (isLowRegister(Reg
))
218 return ARM::tGPRRegisterClass
;
222 case ARM::R8
: case ARM::R9
: case ARM::R10
: case ARM::R11
:
223 case ARM::R12
: case ARM::SP
: case ARM::LR
: case ARM::PC
:
224 return ARM::GPRRegisterClass
;
227 return TargetRegisterInfo::getPhysicalRegisterRegClass(Reg
, VT
);
231 ARMRegisterInfo::getCalleeSavedRegs(const MachineFunction
*MF
) const {
232 static const unsigned CalleeSavedRegs
[] = {
233 ARM::LR
, ARM::R11
, ARM::R10
, ARM::R9
, ARM::R8
,
234 ARM::R7
, ARM::R6
, ARM::R5
, ARM::R4
,
236 ARM::D15
, ARM::D14
, ARM::D13
, ARM::D12
,
237 ARM::D11
, ARM::D10
, ARM::D9
, ARM::D8
,
241 static const unsigned DarwinCalleeSavedRegs
[] = {
242 ARM::LR
, ARM::R7
, ARM::R6
, ARM::R5
, ARM::R4
,
243 ARM::R11
, ARM::R10
, ARM::R9
, ARM::R8
,
245 ARM::D15
, ARM::D14
, ARM::D13
, ARM::D12
,
246 ARM::D11
, ARM::D10
, ARM::D9
, ARM::D8
,
249 return STI
.isTargetDarwin() ? DarwinCalleeSavedRegs
: CalleeSavedRegs
;
252 const TargetRegisterClass
* const *
253 ARMRegisterInfo::getCalleeSavedRegClasses(const MachineFunction
*MF
) const {
254 static const TargetRegisterClass
* const CalleeSavedRegClasses
[] = {
255 &ARM::GPRRegClass
, &ARM::GPRRegClass
, &ARM::GPRRegClass
,
256 &ARM::GPRRegClass
, &ARM::GPRRegClass
, &ARM::GPRRegClass
,
257 &ARM::GPRRegClass
, &ARM::GPRRegClass
, &ARM::GPRRegClass
,
259 &ARM::DPRRegClass
, &ARM::DPRRegClass
, &ARM::DPRRegClass
, &ARM::DPRRegClass
,
260 &ARM::DPRRegClass
, &ARM::DPRRegClass
, &ARM::DPRRegClass
, &ARM::DPRRegClass
,
263 static const TargetRegisterClass
* const ThumbCalleeSavedRegClasses
[] = {
264 &ARM::GPRRegClass
, &ARM::GPRRegClass
, &ARM::GPRRegClass
,
265 &ARM::GPRRegClass
, &ARM::GPRRegClass
, &ARM::tGPRRegClass
,
266 &ARM::tGPRRegClass
,&ARM::tGPRRegClass
,&ARM::tGPRRegClass
,
268 &ARM::DPRRegClass
, &ARM::DPRRegClass
, &ARM::DPRRegClass
, &ARM::DPRRegClass
,
269 &ARM::DPRRegClass
, &ARM::DPRRegClass
, &ARM::DPRRegClass
, &ARM::DPRRegClass
,
272 return STI
.isThumb() ? ThumbCalleeSavedRegClasses
: CalleeSavedRegClasses
;
275 BitVector
ARMRegisterInfo::getReservedRegs(const MachineFunction
&MF
) const {
276 // FIXME: avoid re-calculating this everytime.
277 BitVector
Reserved(getNumRegs());
278 Reserved
.set(ARM::SP
);
279 Reserved
.set(ARM::PC
);
280 if (STI
.isTargetDarwin() || hasFP(MF
))
281 Reserved
.set(FramePtr
);
282 // Some targets reserve R9.
283 if (STI
.isR9Reserved())
284 Reserved
.set(ARM::R9
);
289 ARMRegisterInfo::isReservedReg(const MachineFunction
&MF
, unsigned Reg
) const {
297 if (FramePtr
== Reg
&& (STI
.isTargetDarwin() || hasFP(MF
)))
301 return STI
.isR9Reserved();
308 ARMRegisterInfo::requiresRegisterScavenging(const MachineFunction
&MF
) const {
309 const ARMFunctionInfo
*AFI
= MF
.getInfo
<ARMFunctionInfo
>();
310 return ThumbRegScavenging
|| !AFI
->isThumbFunction();
313 /// hasFP - Return true if the specified function should have a dedicated frame
314 /// pointer register. This is true if the function has variable sized allocas
315 /// or if frame pointer elimination is disabled.
317 bool ARMRegisterInfo::hasFP(const MachineFunction
&MF
) const {
318 const MachineFrameInfo
*MFI
= MF
.getFrameInfo();
319 return NoFramePointerElim
|| MFI
->hasVarSizedObjects();
322 // hasReservedCallFrame - Under normal circumstances, when a frame pointer is
323 // not required, we reserve argument space for call sites in the function
324 // immediately on entry to the current function. This eliminates the need for
325 // add/sub sp brackets around call sites. Returns true if the call frame is
326 // included as part of the stack frame.
327 bool ARMRegisterInfo::hasReservedCallFrame(MachineFunction
&MF
) const {
328 const MachineFrameInfo
*FFI
= MF
.getFrameInfo();
329 unsigned CFSize
= FFI
->getMaxCallFrameSize();
330 ARMFunctionInfo
*AFI
= MF
.getInfo
<ARMFunctionInfo
>();
331 // It's not always a good idea to include the call frame as part of the
332 // stack frame. ARM (especially Thumb) has small immediate offset to
333 // address the stack frame. So a large call frame can cause poor codegen
334 // and may even makes it impossible to scavenge a register.
335 if (AFI
->isThumbFunction()) {
336 if (CFSize
>= ((1 << 8) - 1) * 4 / 2) // Half of imm8 * 4
339 if (CFSize
>= ((1 << 12) - 1) / 2) // Half of imm12
342 return !MF
.getFrameInfo()->hasVarSizedObjects();
345 /// emitARMRegPlusImmediate - Emits a series of instructions to materialize
346 /// a destreg = basereg + immediate in ARM code.
348 void emitARMRegPlusImmediate(MachineBasicBlock
&MBB
,
349 MachineBasicBlock::iterator
&MBBI
,
350 unsigned DestReg
, unsigned BaseReg
, int NumBytes
,
351 ARMCC::CondCodes Pred
, unsigned PredReg
,
352 const TargetInstrInfo
&TII
,
354 bool isSub
= NumBytes
< 0;
355 if (isSub
) NumBytes
= -NumBytes
;
358 unsigned RotAmt
= ARM_AM::getSOImmValRotate(NumBytes
);
359 unsigned ThisVal
= NumBytes
& ARM_AM::rotr32(0xFF, RotAmt
);
360 assert(ThisVal
&& "Didn't extract field correctly");
362 // We will handle these bits from offset, clear them.
363 NumBytes
&= ~ThisVal
;
365 // Get the properly encoded SOImmVal field.
366 int SOImmVal
= ARM_AM::getSOImmVal(ThisVal
);
367 assert(SOImmVal
!= -1 && "Bit extraction didn't work?");
369 // Build the new ADD / SUB.
370 BuildMI(MBB
, MBBI
, dl
, TII
.get(isSub
? ARM::SUBri
: ARM::ADDri
), DestReg
)
371 .addReg(BaseReg
, false, false, true).addImm(SOImmVal
)
372 .addImm((unsigned)Pred
).addReg(PredReg
).addReg(0);
377 /// calcNumMI - Returns the number of instructions required to materialize
378 /// the specific add / sub r, c instruction.
379 static unsigned calcNumMI(int Opc
, int ExtraOpc
, unsigned Bytes
,
380 unsigned NumBits
, unsigned Scale
) {
382 unsigned Chunk
= ((1 << NumBits
) - 1) * Scale
;
384 if (Opc
== ARM::tADDrSPi
) {
385 unsigned ThisVal
= (Bytes
> Chunk
) ? Chunk
: Bytes
;
389 Scale
= 1; // Followed by a number of tADDi8.
390 Chunk
= ((1 << NumBits
) - 1) * Scale
;
393 NumMIs
+= Bytes
/ Chunk
;
394 if ((Bytes
% Chunk
) != 0)
401 /// emitThumbRegPlusImmInReg - Emits a series of instructions to materialize
402 /// a destreg = basereg + immediate in Thumb code. Materialize the immediate
403 /// in a register using mov / mvn sequences or load the immediate from a
406 void emitThumbRegPlusImmInReg(MachineBasicBlock
&MBB
,
407 MachineBasicBlock::iterator
&MBBI
,
408 unsigned DestReg
, unsigned BaseReg
,
409 int NumBytes
, bool CanChangeCC
,
410 const TargetInstrInfo
&TII
,
411 const ARMRegisterInfo
& MRI
,
413 bool isHigh
= !MRI
.isLowRegister(DestReg
) ||
414 (BaseReg
!= 0 && !MRI
.isLowRegister(BaseReg
));
416 // Subtract doesn't have high register version. Load the negative value
417 // if either base or dest register is a high register. Also, if do not
418 // issue sub as part of the sequence if condition register is to be
420 if (NumBytes
< 0 && !isHigh
&& CanChangeCC
) {
422 NumBytes
= -NumBytes
;
424 unsigned LdReg
= DestReg
;
425 if (DestReg
== ARM::SP
) {
426 assert(BaseReg
== ARM::SP
&& "Unexpected!");
428 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVlor2hir
), ARM::R12
)
429 .addReg(ARM::R3
, false, false, true);
432 if (NumBytes
<= 255 && NumBytes
>= 0)
433 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVi8
), LdReg
).addImm(NumBytes
);
434 else if (NumBytes
< 0 && NumBytes
>= -255) {
435 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVi8
), LdReg
).addImm(NumBytes
);
436 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tNEG
), LdReg
)
437 .addReg(LdReg
, false, false, true);
439 MRI
.emitLoadConstPool(MBB
, MBBI
, LdReg
, NumBytes
, ARMCC::AL
, 0, &TII
,
443 int Opc
= (isSub
) ? ARM::tSUBrr
: (isHigh
? ARM::tADDhirr
: ARM::tADDrr
);
444 const MachineInstrBuilder MIB
= BuildMI(MBB
, MBBI
, dl
,
445 TII
.get(Opc
), DestReg
);
446 if (DestReg
== ARM::SP
|| isSub
)
447 MIB
.addReg(BaseReg
).addReg(LdReg
, false, false, true);
449 MIB
.addReg(LdReg
).addReg(BaseReg
, false, false, true);
450 if (DestReg
== ARM::SP
)
451 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVhir2lor
), ARM::R3
)
452 .addReg(ARM::R12
, false, false, true);
455 /// emitThumbRegPlusImmediate - Emits a series of instructions to materialize
456 /// a destreg = basereg + immediate in Thumb code.
458 void emitThumbRegPlusImmediate(MachineBasicBlock
&MBB
,
459 MachineBasicBlock::iterator
&MBBI
,
460 unsigned DestReg
, unsigned BaseReg
,
461 int NumBytes
, const TargetInstrInfo
&TII
,
462 const ARMRegisterInfo
& MRI
,
464 bool isSub
= NumBytes
< 0;
465 unsigned Bytes
= (unsigned)NumBytes
;
466 if (isSub
) Bytes
= -NumBytes
;
467 bool isMul4
= (Bytes
& 3) == 0;
468 bool isTwoAddr
= false;
469 bool DstNotEqBase
= false;
470 unsigned NumBits
= 1;
475 if (DestReg
== BaseReg
&& BaseReg
== ARM::SP
) {
476 assert(isMul4
&& "Thumb sp inc / dec size must be multiple of 4!");
479 Opc
= isSub
? ARM::tSUBspi
: ARM::tADDspi
;
481 } else if (!isSub
&& BaseReg
== ARM::SP
) {
484 // r1 = add sp, 100 * 4
488 ExtraOpc
= ARM::tADDi3
;
497 if (DestReg
!= BaseReg
)
500 Opc
= isSub
? ARM::tSUBi8
: ARM::tADDi8
;
504 unsigned NumMIs
= calcNumMI(Opc
, ExtraOpc
, Bytes
, NumBits
, Scale
);
505 unsigned Threshold
= (DestReg
== ARM::SP
) ? 3 : 2;
506 if (NumMIs
> Threshold
) {
507 // This will expand into too many instructions. Load the immediate from a
509 emitThumbRegPlusImmInReg(MBB
, MBBI
, DestReg
, BaseReg
, NumBytes
, true, TII
,
515 if (MRI
.isLowRegister(DestReg
) && MRI
.isLowRegister(BaseReg
)) {
516 // If both are low registers, emit DestReg = add BaseReg, max(Imm, 7)
517 unsigned Chunk
= (1 << 3) - 1;
518 unsigned ThisVal
= (Bytes
> Chunk
) ? Chunk
: Bytes
;
520 BuildMI(MBB
, MBBI
, dl
,TII
.get(isSub
? ARM::tSUBi3
: ARM::tADDi3
), DestReg
)
521 .addReg(BaseReg
, false, false, true).addImm(ThisVal
);
523 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVr
), DestReg
)
524 .addReg(BaseReg
, false, false, true);
529 unsigned Chunk
= ((1 << NumBits
) - 1) * Scale
;
531 unsigned ThisVal
= (Bytes
> Chunk
) ? Chunk
: Bytes
;
534 // Build the new tADD / tSUB.
536 BuildMI(MBB
, MBBI
, dl
, TII
.get(Opc
), DestReg
)
537 .addReg(DestReg
).addImm(ThisVal
);
539 bool isKill
= BaseReg
!= ARM::SP
;
540 BuildMI(MBB
, MBBI
, dl
, TII
.get(Opc
), DestReg
)
541 .addReg(BaseReg
, false, false, isKill
).addImm(ThisVal
);
544 if (Opc
== ARM::tADDrSPi
) {
550 Chunk
= ((1 << NumBits
) - 1) * Scale
;
551 Opc
= isSub
? ARM::tSUBi8
: ARM::tADDi8
;
558 BuildMI(MBB
, MBBI
, dl
, TII
.get(ExtraOpc
), DestReg
)
559 .addReg(DestReg
, false, false, true)
560 .addImm(((unsigned)NumBytes
) & 3);
564 void emitSPUpdate(MachineBasicBlock
&MBB
, MachineBasicBlock::iterator
&MBBI
,
565 int NumBytes
, ARMCC::CondCodes Pred
, unsigned PredReg
,
566 bool isThumb
, const TargetInstrInfo
&TII
,
567 const ARMRegisterInfo
& MRI
,
570 emitThumbRegPlusImmediate(MBB
, MBBI
, ARM::SP
, ARM::SP
, NumBytes
, TII
,
573 emitARMRegPlusImmediate(MBB
, MBBI
, ARM::SP
, ARM::SP
, NumBytes
,
574 Pred
, PredReg
, TII
, dl
);
577 void ARMRegisterInfo::
578 eliminateCallFramePseudoInstr(MachineFunction
&MF
, MachineBasicBlock
&MBB
,
579 MachineBasicBlock::iterator I
) const {
580 if (!hasReservedCallFrame(MF
)) {
581 // If we have alloca, convert as follows:
582 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
583 // ADJCALLSTACKUP -> add, sp, sp, amount
584 MachineInstr
*Old
= I
;
585 DebugLoc dl
= Old
->getDebugLoc();
586 unsigned Amount
= Old
->getOperand(0).getImm();
588 ARMFunctionInfo
*AFI
= MF
.getInfo
<ARMFunctionInfo
>();
589 // We need to keep the stack aligned properly. To do this, we round the
590 // amount of space needed for the outgoing arguments up to the next
591 // alignment boundary.
592 unsigned Align
= MF
.getTarget().getFrameInfo()->getStackAlignment();
593 Amount
= (Amount
+Align
-1)/Align
*Align
;
595 // Replace the pseudo instruction with a new instruction...
596 unsigned Opc
= Old
->getOpcode();
597 bool isThumb
= AFI
->isThumbFunction();
598 ARMCC::CondCodes Pred
= isThumb
599 ? ARMCC::AL
: (ARMCC::CondCodes
)Old
->getOperand(1).getImm();
600 if (Opc
== ARM::ADJCALLSTACKDOWN
|| Opc
== ARM::tADJCALLSTACKDOWN
) {
601 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
602 unsigned PredReg
= isThumb
? 0 : Old
->getOperand(2).getReg();
603 emitSPUpdate(MBB
, I
, -Amount
, Pred
, PredReg
, isThumb
, TII
, *this, dl
);
605 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
606 unsigned PredReg
= isThumb
? 0 : Old
->getOperand(3).getReg();
607 assert(Opc
== ARM::ADJCALLSTACKUP
|| Opc
== ARM::tADJCALLSTACKUP
);
608 emitSPUpdate(MBB
, I
, Amount
, Pred
, PredReg
, isThumb
, TII
, *this, dl
);
615 /// emitThumbConstant - Emit a series of instructions to materialize a
617 static void emitThumbConstant(MachineBasicBlock
&MBB
,
618 MachineBasicBlock::iterator
&MBBI
,
619 unsigned DestReg
, int Imm
,
620 const TargetInstrInfo
&TII
,
621 const ARMRegisterInfo
& MRI
,
623 bool isSub
= Imm
< 0;
624 if (isSub
) Imm
= -Imm
;
626 int Chunk
= (1 << 8) - 1;
627 int ThisVal
= (Imm
> Chunk
) ? Chunk
: Imm
;
629 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVi8
), DestReg
).addImm(ThisVal
);
631 emitThumbRegPlusImmediate(MBB
, MBBI
, DestReg
, DestReg
, Imm
, TII
, MRI
, dl
);
633 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tNEG
), DestReg
)
634 .addReg(DestReg
, false, false, true);
637 /// findScratchRegister - Find a 'free' ARM register. If register scavenger
638 /// is not being used, R12 is available. Otherwise, try for a call-clobbered
639 /// register first and then a spilled callee-saved register if that fails.
641 unsigned findScratchRegister(RegScavenger
*RS
, const TargetRegisterClass
*RC
,
642 ARMFunctionInfo
*AFI
) {
643 unsigned Reg
= RS
? RS
->FindUnusedReg(RC
, true) : (unsigned) ARM::R12
;
644 assert (!AFI
->isThumbFunction());
646 // Try a already spilled CS register.
647 Reg
= RS
->FindUnusedReg(RC
, AFI
->getSpilledCSRegisters());
652 void ARMRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II
,
653 int SPAdj
, RegScavenger
*RS
) const{
655 MachineInstr
&MI
= *II
;
656 MachineBasicBlock
&MBB
= *MI
.getParent();
657 MachineFunction
&MF
= *MBB
.getParent();
658 ARMFunctionInfo
*AFI
= MF
.getInfo
<ARMFunctionInfo
>();
659 bool isThumb
= AFI
->isThumbFunction();
660 DebugLoc dl
= MI
.getDebugLoc();
662 while (!MI
.getOperand(i
).isFI()) {
664 assert(i
< MI
.getNumOperands() && "Instr doesn't have FrameIndex operand!");
667 unsigned FrameReg
= ARM::SP
;
668 int FrameIndex
= MI
.getOperand(i
).getIndex();
669 int Offset
= MF
.getFrameInfo()->getObjectOffset(FrameIndex
) +
670 MF
.getFrameInfo()->getStackSize() + SPAdj
;
672 if (AFI
->isGPRCalleeSavedArea1Frame(FrameIndex
))
673 Offset
-= AFI
->getGPRCalleeSavedArea1Offset();
674 else if (AFI
->isGPRCalleeSavedArea2Frame(FrameIndex
))
675 Offset
-= AFI
->getGPRCalleeSavedArea2Offset();
676 else if (AFI
->isDPRCalleeSavedAreaFrame(FrameIndex
))
677 Offset
-= AFI
->getDPRCalleeSavedAreaOffset();
678 else if (hasFP(MF
)) {
679 assert(SPAdj
== 0 && "Unexpected");
680 // There is alloca()'s in this function, must reference off the frame
682 FrameReg
= getFrameRegister(MF
);
683 Offset
-= AFI
->getFramePtrSpillOffset();
686 unsigned Opcode
= MI
.getOpcode();
687 const TargetInstrDesc
&Desc
= MI
.getDesc();
688 unsigned AddrMode
= (Desc
.TSFlags
& ARMII::AddrModeMask
);
691 if (Opcode
== ARM::ADDri
) {
692 Offset
+= MI
.getOperand(i
+1).getImm();
694 // Turn it into a move.
695 MI
.setDesc(TII
.get(ARM::MOVr
));
696 MI
.getOperand(i
).ChangeToRegister(FrameReg
, false);
697 MI
.RemoveOperand(i
+1);
699 } else if (Offset
< 0) {
702 MI
.setDesc(TII
.get(ARM::SUBri
));
705 // Common case: small offset, fits into instruction.
706 int ImmedOffset
= ARM_AM::getSOImmVal(Offset
);
707 if (ImmedOffset
!= -1) {
708 // Replace the FrameIndex with sp / fp
709 MI
.getOperand(i
).ChangeToRegister(FrameReg
, false);
710 MI
.getOperand(i
+1).ChangeToImmediate(ImmedOffset
);
714 // Otherwise, we fallback to common code below to form the imm offset with
715 // a sequence of ADDri instructions. First though, pull as much of the imm
716 // into this ADDri as possible.
717 unsigned RotAmt
= ARM_AM::getSOImmValRotate(Offset
);
718 unsigned ThisImmVal
= Offset
& ARM_AM::rotr32(0xFF, RotAmt
);
720 // We will handle these bits from offset, clear them.
721 Offset
&= ~ThisImmVal
;
723 // Get the properly encoded SOImmVal field.
724 int ThisSOImmVal
= ARM_AM::getSOImmVal(ThisImmVal
);
725 assert(ThisSOImmVal
!= -1 && "Bit extraction didn't work?");
726 MI
.getOperand(i
+1).ChangeToImmediate(ThisSOImmVal
);
727 } else if (Opcode
== ARM::tADDrSPi
) {
728 Offset
+= MI
.getOperand(i
+1).getImm();
730 // Can't use tADDrSPi if it's based off the frame pointer.
731 unsigned NumBits
= 0;
733 if (FrameReg
!= ARM::SP
) {
734 Opcode
= ARM::tADDi3
;
735 MI
.setDesc(TII
.get(ARM::tADDi3
));
740 assert((Offset
& 3) == 0 &&
741 "Thumb add/sub sp, #imm immediate must be multiple of 4!");
745 // Turn it into a move.
746 MI
.setDesc(TII
.get(ARM::tMOVhir2lor
));
747 MI
.getOperand(i
).ChangeToRegister(FrameReg
, false);
748 MI
.RemoveOperand(i
+1);
752 // Common case: small offset, fits into instruction.
753 unsigned Mask
= (1 << NumBits
) - 1;
754 if (((Offset
/ Scale
) & ~Mask
) == 0) {
755 // Replace the FrameIndex with sp / fp
756 MI
.getOperand(i
).ChangeToRegister(FrameReg
, false);
757 MI
.getOperand(i
+1).ChangeToImmediate(Offset
/ Scale
);
761 unsigned DestReg
= MI
.getOperand(0).getReg();
762 unsigned Bytes
= (Offset
> 0) ? Offset
: -Offset
;
763 unsigned NumMIs
= calcNumMI(Opcode
, 0, Bytes
, NumBits
, Scale
);
764 // MI would expand into a large number of instructions. Don't try to
765 // simplify the immediate.
767 emitThumbRegPlusImmediate(MBB
, II
, DestReg
, FrameReg
, Offset
, TII
,
774 // Translate r0 = add sp, imm to
775 // r0 = add sp, 255*4
776 // r0 = add r0, (imm - 255*4)
777 MI
.getOperand(i
).ChangeToRegister(FrameReg
, false);
778 MI
.getOperand(i
+1).ChangeToImmediate(Mask
);
779 Offset
= (Offset
- Mask
* Scale
);
780 MachineBasicBlock::iterator NII
= next(II
);
781 emitThumbRegPlusImmediate(MBB
, NII
, DestReg
, DestReg
, Offset
, TII
,
784 // Translate r0 = add sp, -imm to
785 // r0 = -imm (this is then translated into a series of instructons)
787 emitThumbConstant(MBB
, II
, DestReg
, Offset
, TII
, *this, dl
);
788 MI
.setDesc(TII
.get(ARM::tADDhirr
));
789 MI
.getOperand(i
).ChangeToRegister(DestReg
, false, false, true);
790 MI
.getOperand(i
+1).ChangeToRegister(FrameReg
, false);
796 unsigned NumBits
= 0;
799 case ARMII::AddrMode2
: {
801 InstrOffs
= ARM_AM::getAM2Offset(MI
.getOperand(ImmIdx
).getImm());
802 if (ARM_AM::getAM2Op(MI
.getOperand(ImmIdx
).getImm()) == ARM_AM::sub
)
807 case ARMII::AddrMode3
: {
809 InstrOffs
= ARM_AM::getAM3Offset(MI
.getOperand(ImmIdx
).getImm());
810 if (ARM_AM::getAM3Op(MI
.getOperand(ImmIdx
).getImm()) == ARM_AM::sub
)
815 case ARMII::AddrMode5
: {
817 InstrOffs
= ARM_AM::getAM5Offset(MI
.getOperand(ImmIdx
).getImm());
818 if (ARM_AM::getAM5Op(MI
.getOperand(ImmIdx
).getImm()) == ARM_AM::sub
)
824 case ARMII::AddrModeTs
: {
826 InstrOffs
= MI
.getOperand(ImmIdx
).getImm();
827 NumBits
= (FrameReg
== ARM::SP
) ? 8 : 5;
832 assert(0 && "Unsupported addressing mode!");
837 Offset
+= InstrOffs
* Scale
;
838 assert((Offset
& (Scale
-1)) == 0 && "Can't encode this offset!");
839 if (Offset
< 0 && !isThumb
) {
844 // Common case: small offset, fits into instruction.
845 MachineOperand
&ImmOp
= MI
.getOperand(ImmIdx
);
846 int ImmedOffset
= Offset
/ Scale
;
847 unsigned Mask
= (1 << NumBits
) - 1;
848 if ((unsigned)Offset
<= Mask
* Scale
) {
849 // Replace the FrameIndex with sp
850 MI
.getOperand(i
).ChangeToRegister(FrameReg
, false);
852 ImmedOffset
|= 1 << NumBits
;
853 ImmOp
.ChangeToImmediate(ImmedOffset
);
857 bool isThumSpillRestore
= Opcode
== ARM::tRestore
|| Opcode
== ARM::tSpill
;
858 if (AddrMode
== ARMII::AddrModeTs
) {
859 // Thumb tLDRspi, tSTRspi. These will change to instructions that use
860 // a different base register.
862 Mask
= (1 << NumBits
) - 1;
864 // If this is a thumb spill / restore, we will be using a constpool load to
865 // materialize the offset.
866 if (AddrMode
== ARMII::AddrModeTs
&& isThumSpillRestore
)
867 ImmOp
.ChangeToImmediate(0);
869 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
870 ImmedOffset
= ImmedOffset
& Mask
;
872 ImmedOffset
|= 1 << NumBits
;
873 ImmOp
.ChangeToImmediate(ImmedOffset
);
874 Offset
&= ~(Mask
*Scale
);
878 // If we get here, the immediate doesn't fit into the instruction. We folded
879 // as much as possible above, handle the rest, providing a register that is
881 assert(Offset
&& "This code isn't needed if offset already handled!");
884 if (Desc
.mayLoad()) {
885 // Use the destination register to materialize sp + offset.
886 unsigned TmpReg
= MI
.getOperand(0).getReg();
888 if (Opcode
== ARM::tRestore
) {
889 if (FrameReg
== ARM::SP
)
890 emitThumbRegPlusImmInReg(MBB
, II
, TmpReg
, FrameReg
,
891 Offset
, false, TII
, *this, dl
);
893 emitLoadConstPool(MBB
, II
, TmpReg
, Offset
, ARMCC::AL
, 0, &TII
,
898 emitThumbRegPlusImmediate(MBB
, II
, TmpReg
, FrameReg
, Offset
, TII
,
900 MI
.setDesc(TII
.get(ARM::tLDR
));
901 MI
.getOperand(i
).ChangeToRegister(TmpReg
, false, false, true);
903 // Use [reg, reg] addrmode.
904 MI
.addOperand(MachineOperand::CreateReg(FrameReg
, false));
905 else // tLDR has an extra register operand.
906 MI
.addOperand(MachineOperand::CreateReg(0, false));
907 } else if (Desc
.mayStore()) {
908 // FIXME! This is horrific!!! We need register scavenging.
909 // Our temporary workaround has marked r3 unavailable. Of course, r3 is
910 // also a ABI register so it's possible that is is the register that is
911 // being storing here. If that's the case, we do the following:
913 // Use r2 to materialize sp + offset
916 unsigned ValReg
= MI
.getOperand(0).getReg();
917 unsigned TmpReg
= ARM::R3
;
919 if (ValReg
== ARM::R3
) {
920 BuildMI(MBB
, II
, dl
, TII
.get(ARM::tMOVlor2hir
), ARM::R12
)
921 .addReg(ARM::R2
, false, false, true);
924 if (TmpReg
== ARM::R3
&& AFI
->isR3LiveIn())
925 BuildMI(MBB
, II
, dl
, TII
.get(ARM::tMOVlor2hir
), ARM::R12
)
926 .addReg(ARM::R3
, false, false, true);
927 if (Opcode
== ARM::tSpill
) {
928 if (FrameReg
== ARM::SP
)
929 emitThumbRegPlusImmInReg(MBB
, II
, TmpReg
, FrameReg
,
930 Offset
, false, TII
, *this, dl
);
932 emitLoadConstPool(MBB
, II
, TmpReg
, Offset
, ARMCC::AL
, 0, &TII
,
937 emitThumbRegPlusImmediate(MBB
, II
, TmpReg
, FrameReg
, Offset
, TII
,
939 MI
.setDesc(TII
.get(ARM::tSTR
));
940 MI
.getOperand(i
).ChangeToRegister(TmpReg
, false, false, true);
941 if (UseRR
) // Use [reg, reg] addrmode.
942 MI
.addOperand(MachineOperand::CreateReg(FrameReg
, false));
943 else // tSTR has an extra register operand.
944 MI
.addOperand(MachineOperand::CreateReg(0, false));
946 MachineBasicBlock::iterator NII
= next(II
);
947 if (ValReg
== ARM::R3
)
948 BuildMI(MBB
, NII
, dl
, TII
.get(ARM::tMOVhir2lor
), ARM::R2
)
949 .addReg(ARM::R12
, false, false, true);
950 if (TmpReg
== ARM::R3
&& AFI
->isR3LiveIn())
951 BuildMI(MBB
, NII
, dl
, TII
.get(ARM::tMOVhir2lor
), ARM::R3
)
952 .addReg(ARM::R12
, false, false, true);
954 assert(false && "Unexpected opcode!");
956 // Insert a set of r12 with the full address: r12 = sp + offset
957 // If the offset we have is too large to fit into the instruction, we need
958 // to form it with a series of ADDri's. Do this by taking 8-bit chunks
960 unsigned ScratchReg
= findScratchRegister(RS
, &ARM::GPRRegClass
, AFI
);
962 // No register is "free". Scavenge a register.
963 ScratchReg
= RS
->scavengeRegister(&ARM::GPRRegClass
, II
, SPAdj
);
964 int PIdx
= MI
.findFirstPredOperandIdx();
965 ARMCC::CondCodes Pred
= (PIdx
== -1)
966 ? ARMCC::AL
: (ARMCC::CondCodes
)MI
.getOperand(PIdx
).getImm();
967 unsigned PredReg
= (PIdx
== -1) ? 0 : MI
.getOperand(PIdx
+1).getReg();
968 emitARMRegPlusImmediate(MBB
, II
, ScratchReg
, FrameReg
,
969 isSub
? -Offset
: Offset
, Pred
, PredReg
, TII
, dl
);
970 MI
.getOperand(i
).ChangeToRegister(ScratchReg
, false, false, true);
974 static unsigned estimateStackSize(MachineFunction
&MF
, MachineFrameInfo
*MFI
) {
975 const MachineFrameInfo
*FFI
= MF
.getFrameInfo();
977 for (int i
= FFI
->getObjectIndexBegin(); i
!= 0; ++i
) {
978 int FixedOff
= -FFI
->getObjectOffset(i
);
979 if (FixedOff
> Offset
) Offset
= FixedOff
;
981 for (unsigned i
= 0, e
= FFI
->getObjectIndexEnd(); i
!= e
; ++i
) {
982 if (FFI
->isDeadObjectIndex(i
))
984 Offset
+= FFI
->getObjectSize(i
);
985 unsigned Align
= FFI
->getObjectAlignment(i
);
986 // Adjust to alignment boundary
987 Offset
= (Offset
+Align
-1)/Align
*Align
;
989 return (unsigned)Offset
;
993 ARMRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction
&MF
,
994 RegScavenger
*RS
) const {
995 // This tells PEI to spill the FP as if it is any other callee-save register
996 // to take advantage the eliminateFrameIndex machinery. This also ensures it
997 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
998 // to combine multiple loads / stores.
999 bool CanEliminateFrame
= true;
1000 bool CS1Spilled
= false;
1001 bool LRSpilled
= false;
1002 unsigned NumGPRSpills
= 0;
1003 SmallVector
<unsigned, 4> UnspilledCS1GPRs
;
1004 SmallVector
<unsigned, 4> UnspilledCS2GPRs
;
1005 ARMFunctionInfo
*AFI
= MF
.getInfo
<ARMFunctionInfo
>();
1007 // Don't spill FP if the frame can be eliminated. This is determined
1008 // by scanning the callee-save registers to see if any is used.
1009 const unsigned *CSRegs
= getCalleeSavedRegs();
1010 const TargetRegisterClass
* const *CSRegClasses
= getCalleeSavedRegClasses();
1011 for (unsigned i
= 0; CSRegs
[i
]; ++i
) {
1012 unsigned Reg
= CSRegs
[i
];
1013 bool Spilled
= false;
1014 if (MF
.getRegInfo().isPhysRegUsed(Reg
)) {
1015 AFI
->setCSRegisterIsSpilled(Reg
);
1017 CanEliminateFrame
= false;
1019 // Check alias registers too.
1020 for (const unsigned *Aliases
= getAliasSet(Reg
); *Aliases
; ++Aliases
) {
1021 if (MF
.getRegInfo().isPhysRegUsed(*Aliases
)) {
1023 CanEliminateFrame
= false;
1028 if (CSRegClasses
[i
] == &ARM::GPRRegClass
) {
1032 if (!STI
.isTargetDarwin()) {
1039 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
1054 if (!STI
.isTargetDarwin()) {
1055 UnspilledCS1GPRs
.push_back(Reg
);
1065 UnspilledCS1GPRs
.push_back(Reg
);
1068 UnspilledCS2GPRs
.push_back(Reg
);
1075 bool ForceLRSpill
= false;
1076 if (!LRSpilled
&& AFI
->isThumbFunction()) {
1077 unsigned FnSize
= TII
.GetFunctionSizeInBytes(MF
);
1078 // Force LR to be spilled if the Thumb function size is > 2048. This enables
1079 // use of BL to implement far jump. If it turns out that it's not needed
1080 // then the branch fix up path will undo it.
1081 if (FnSize
>= (1 << 11)) {
1082 CanEliminateFrame
= false;
1083 ForceLRSpill
= true;
1087 bool ExtraCSSpill
= false;
1088 if (!CanEliminateFrame
|| hasFP(MF
)) {
1089 AFI
->setHasStackFrame(true);
1091 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
1092 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
1093 if (!LRSpilled
&& CS1Spilled
) {
1094 MF
.getRegInfo().setPhysRegUsed(ARM::LR
);
1095 AFI
->setCSRegisterIsSpilled(ARM::LR
);
1097 UnspilledCS1GPRs
.erase(std::find(UnspilledCS1GPRs
.begin(),
1098 UnspilledCS1GPRs
.end(), (unsigned)ARM::LR
));
1099 ForceLRSpill
= false;
1100 ExtraCSSpill
= true;
1103 // Darwin ABI requires FP to point to the stack slot that contains the
1105 if (STI
.isTargetDarwin() || hasFP(MF
)) {
1106 MF
.getRegInfo().setPhysRegUsed(FramePtr
);
1110 // If stack and double are 8-byte aligned and we are spilling an odd number
1111 // of GPRs. Spill one extra callee save GPR so we won't have to pad between
1112 // the integer and double callee save areas.
1113 unsigned TargetAlign
= MF
.getTarget().getFrameInfo()->getStackAlignment();
1114 if (TargetAlign
== 8 && (NumGPRSpills
& 1)) {
1115 if (CS1Spilled
&& !UnspilledCS1GPRs
.empty()) {
1116 for (unsigned i
= 0, e
= UnspilledCS1GPRs
.size(); i
!= e
; ++i
) {
1117 unsigned Reg
= UnspilledCS1GPRs
[i
];
1118 // Don't spiil high register if the function is thumb
1119 if (!AFI
->isThumbFunction() || isLowRegister(Reg
) || Reg
== ARM::LR
) {
1120 MF
.getRegInfo().setPhysRegUsed(Reg
);
1121 AFI
->setCSRegisterIsSpilled(Reg
);
1122 if (!isReservedReg(MF
, Reg
))
1123 ExtraCSSpill
= true;
1127 } else if (!UnspilledCS2GPRs
.empty() &&
1128 !AFI
->isThumbFunction()) {
1129 unsigned Reg
= UnspilledCS2GPRs
.front();
1130 MF
.getRegInfo().setPhysRegUsed(Reg
);
1131 AFI
->setCSRegisterIsSpilled(Reg
);
1132 if (!isReservedReg(MF
, Reg
))
1133 ExtraCSSpill
= true;
1137 // Estimate if we might need to scavenge a register at some point in order
1138 // to materialize a stack offset. If so, either spill one additiona
1139 // callee-saved register or reserve a special spill slot to facilitate
1140 // register scavenging.
1141 if (RS
&& !ExtraCSSpill
&& !AFI
->isThumbFunction()) {
1142 MachineFrameInfo
*MFI
= MF
.getFrameInfo();
1143 unsigned Size
= estimateStackSize(MF
, MFI
);
1144 unsigned Limit
= (1 << 12) - 1;
1145 for (MachineFunction::iterator BB
= MF
.begin(),E
= MF
.end();BB
!= E
; ++BB
)
1146 for (MachineBasicBlock::iterator I
= BB
->begin(); I
!= BB
->end(); ++I
) {
1147 for (unsigned i
= 0, e
= I
->getNumOperands(); i
!= e
; ++i
)
1148 if (I
->getOperand(i
).isFI()) {
1149 unsigned Opcode
= I
->getOpcode();
1150 const TargetInstrDesc
&Desc
= TII
.get(Opcode
);
1151 unsigned AddrMode
= (Desc
.TSFlags
& ARMII::AddrModeMask
);
1152 if (AddrMode
== ARMII::AddrMode3
) {
1153 Limit
= (1 << 8) - 1;
1154 goto DoneEstimating
;
1155 } else if (AddrMode
== ARMII::AddrMode5
) {
1156 unsigned ThisLimit
= ((1 << 8) - 1) * 4;
1157 if (ThisLimit
< Limit
)
1163 if (Size
>= Limit
) {
1164 // If any non-reserved CS register isn't spilled, just spill one or two
1165 // extra. That should take care of it!
1166 unsigned NumExtras
= TargetAlign
/ 4;
1167 SmallVector
<unsigned, 2> Extras
;
1168 while (NumExtras
&& !UnspilledCS1GPRs
.empty()) {
1169 unsigned Reg
= UnspilledCS1GPRs
.back();
1170 UnspilledCS1GPRs
.pop_back();
1171 if (!isReservedReg(MF
, Reg
)) {
1172 Extras
.push_back(Reg
);
1176 while (NumExtras
&& !UnspilledCS2GPRs
.empty()) {
1177 unsigned Reg
= UnspilledCS2GPRs
.back();
1178 UnspilledCS2GPRs
.pop_back();
1179 if (!isReservedReg(MF
, Reg
)) {
1180 Extras
.push_back(Reg
);
1184 if (Extras
.size() && NumExtras
== 0) {
1185 for (unsigned i
= 0, e
= Extras
.size(); i
!= e
; ++i
) {
1186 MF
.getRegInfo().setPhysRegUsed(Extras
[i
]);
1187 AFI
->setCSRegisterIsSpilled(Extras
[i
]);
1190 // Reserve a slot closest to SP or frame pointer.
1191 const TargetRegisterClass
*RC
= &ARM::GPRRegClass
;
1192 RS
->setScavengingFrameIndex(MFI
->CreateStackObject(RC
->getSize(),
1193 RC
->getAlignment()));
1200 MF
.getRegInfo().setPhysRegUsed(ARM::LR
);
1201 AFI
->setCSRegisterIsSpilled(ARM::LR
);
1202 AFI
->setLRIsSpilledForFarJump(true);
1206 /// Move iterator pass the next bunch of callee save load / store ops for
1207 /// the particular spill area (1: integer area 1, 2: integer area 2,
1208 /// 3: fp area, 0: don't care).
1209 static void movePastCSLoadStoreOps(MachineBasicBlock
&MBB
,
1210 MachineBasicBlock::iterator
&MBBI
,
1211 int Opc
, unsigned Area
,
1212 const ARMSubtarget
&STI
) {
1213 while (MBBI
!= MBB
.end() &&
1214 MBBI
->getOpcode() == Opc
&& MBBI
->getOperand(1).isFI()) {
1217 unsigned Category
= 0;
1218 switch (MBBI
->getOperand(0).getReg()) {
1219 case ARM::R4
: case ARM::R5
: case ARM::R6
: case ARM::R7
:
1223 case ARM::R8
: case ARM::R9
: case ARM::R10
: case ARM::R11
:
1224 Category
= STI
.isTargetDarwin() ? 2 : 1;
1226 case ARM::D8
: case ARM::D9
: case ARM::D10
: case ARM::D11
:
1227 case ARM::D12
: case ARM::D13
: case ARM::D14
: case ARM::D15
:
1234 if (Done
|| Category
!= Area
)
1242 void ARMRegisterInfo::emitPrologue(MachineFunction
&MF
) const {
1243 MachineBasicBlock
&MBB
= MF
.front();
1244 MachineBasicBlock::iterator MBBI
= MBB
.begin();
1245 MachineFrameInfo
*MFI
= MF
.getFrameInfo();
1246 ARMFunctionInfo
*AFI
= MF
.getInfo
<ARMFunctionInfo
>();
1247 bool isThumb
= AFI
->isThumbFunction();
1248 unsigned VARegSaveSize
= AFI
->getVarArgsRegSaveSize();
1249 unsigned NumBytes
= MFI
->getStackSize();
1250 const std::vector
<CalleeSavedInfo
> &CSI
= MFI
->getCalleeSavedInfo();
1251 DebugLoc dl
= (MBBI
!= MBB
.end() ?
1252 MBBI
->getDebugLoc() : DebugLoc::getUnknownLoc());
1255 // Check if R3 is live in. It might have to be used as a scratch register.
1256 for (MachineRegisterInfo::livein_iterator I
=MF
.getRegInfo().livein_begin(),
1257 E
= MF
.getRegInfo().livein_end(); I
!= E
; ++I
) {
1258 if (I
->first
== ARM::R3
) {
1259 AFI
->setR3IsLiveIn(true);
1264 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
1265 NumBytes
= (NumBytes
+ 3) & ~3;
1266 MFI
->setStackSize(NumBytes
);
1269 // Determine the sizes of each callee-save spill areas and record which frame
1270 // belongs to which callee-save spill areas.
1271 unsigned GPRCS1Size
= 0, GPRCS2Size
= 0, DPRCSSize
= 0;
1272 int FramePtrSpillFI
= 0;
1275 emitSPUpdate(MBB
, MBBI
, -VARegSaveSize
, ARMCC::AL
, 0, isThumb
, TII
,
1278 if (!AFI
->hasStackFrame()) {
1280 emitSPUpdate(MBB
, MBBI
, -NumBytes
, ARMCC::AL
, 0, isThumb
, TII
, *this, dl
);
1284 for (unsigned i
= 0, e
= CSI
.size(); i
!= e
; ++i
) {
1285 unsigned Reg
= CSI
[i
].getReg();
1286 int FI
= CSI
[i
].getFrameIdx();
1293 if (Reg
== FramePtr
)
1294 FramePtrSpillFI
= FI
;
1295 AFI
->addGPRCalleeSavedArea1Frame(FI
);
1302 if (Reg
== FramePtr
)
1303 FramePtrSpillFI
= FI
;
1304 if (STI
.isTargetDarwin()) {
1305 AFI
->addGPRCalleeSavedArea2Frame(FI
);
1308 AFI
->addGPRCalleeSavedArea1Frame(FI
);
1313 AFI
->addDPRCalleeSavedAreaFrame(FI
);
1319 // Build the new SUBri to adjust SP for integer callee-save spill area 1.
1320 emitSPUpdate(MBB
, MBBI
, -GPRCS1Size
, ARMCC::AL
, 0, isThumb
, TII
, *this, dl
);
1321 movePastCSLoadStoreOps(MBB
, MBBI
, ARM::STR
, 1, STI
);
1322 } else if (MBBI
!= MBB
.end() && MBBI
->getOpcode() == ARM::tPUSH
) {
1324 if (MBBI
!= MBB
.end())
1325 dl
= MBBI
->getDebugLoc();
1328 // Darwin ABI requires FP to point to the stack slot that contains the
1330 if (STI
.isTargetDarwin() || hasFP(MF
)) {
1331 MachineInstrBuilder MIB
=
1332 BuildMI(MBB
, MBBI
, dl
, TII
.get(isThumb
? ARM::tADDrSPi
: ARM::ADDri
),
1334 .addFrameIndex(FramePtrSpillFI
).addImm(0);
1335 if (!isThumb
) AddDefaultCC(AddDefaultPred(MIB
));
1339 // Build the new SUBri to adjust SP for integer callee-save spill area 2.
1340 emitSPUpdate(MBB
, MBBI
, -GPRCS2Size
, ARMCC::AL
, 0, false, TII
, *this, dl
);
1342 // Build the new SUBri to adjust SP for FP callee-save spill area.
1343 movePastCSLoadStoreOps(MBB
, MBBI
, ARM::STR
, 2, STI
);
1344 emitSPUpdate(MBB
, MBBI
, -DPRCSSize
, ARMCC::AL
, 0, false, TII
, *this, dl
);
1347 // Determine starting offsets of spill areas.
1348 unsigned DPRCSOffset
= NumBytes
- (GPRCS1Size
+ GPRCS2Size
+ DPRCSSize
);
1349 unsigned GPRCS2Offset
= DPRCSOffset
+ DPRCSSize
;
1350 unsigned GPRCS1Offset
= GPRCS2Offset
+ GPRCS2Size
;
1351 AFI
->setFramePtrSpillOffset(MFI
->getObjectOffset(FramePtrSpillFI
) + NumBytes
);
1352 AFI
->setGPRCalleeSavedArea1Offset(GPRCS1Offset
);
1353 AFI
->setGPRCalleeSavedArea2Offset(GPRCS2Offset
);
1354 AFI
->setDPRCalleeSavedAreaOffset(DPRCSOffset
);
1356 NumBytes
= DPRCSOffset
;
1358 // Insert it after all the callee-save spills.
1360 movePastCSLoadStoreOps(MBB
, MBBI
, ARM::FSTD
, 3, STI
);
1361 emitSPUpdate(MBB
, MBBI
, -NumBytes
, ARMCC::AL
, 0, isThumb
, TII
, *this, dl
);
1364 if(STI
.isTargetELF() && hasFP(MF
)) {
1365 MFI
->setOffsetAdjustment(MFI
->getOffsetAdjustment() -
1366 AFI
->getFramePtrSpillOffset());
1369 AFI
->setGPRCalleeSavedArea1Size(GPRCS1Size
);
1370 AFI
->setGPRCalleeSavedArea2Size(GPRCS2Size
);
1371 AFI
->setDPRCalleeSavedAreaSize(DPRCSSize
);
1374 static bool isCalleeSavedRegister(unsigned Reg
, const unsigned *CSRegs
) {
1375 for (unsigned i
= 0; CSRegs
[i
]; ++i
)
1376 if (Reg
== CSRegs
[i
])
1381 static bool isCSRestore(MachineInstr
*MI
, const unsigned *CSRegs
) {
1382 return ((MI
->getOpcode() == ARM::FLDD
||
1383 MI
->getOpcode() == ARM::LDR
||
1384 MI
->getOpcode() == ARM::tRestore
) &&
1385 MI
->getOperand(1).isFI() &&
1386 isCalleeSavedRegister(MI
->getOperand(0).getReg(), CSRegs
));
1389 void ARMRegisterInfo::emitEpilogue(MachineFunction
&MF
,
1390 MachineBasicBlock
&MBB
) const {
1391 MachineBasicBlock::iterator MBBI
= prior(MBB
.end());
1392 assert((MBBI
->getOpcode() == ARM::BX_RET
||
1393 MBBI
->getOpcode() == ARM::tBX_RET
||
1394 MBBI
->getOpcode() == ARM::tPOP_RET
) &&
1395 "Can only insert epilog into returning blocks");
1396 DebugLoc dl
= MBBI
->getDebugLoc();
1397 MachineFrameInfo
*MFI
= MF
.getFrameInfo();
1398 ARMFunctionInfo
*AFI
= MF
.getInfo
<ARMFunctionInfo
>();
1399 bool isThumb
= AFI
->isThumbFunction();
1400 unsigned VARegSaveSize
= AFI
->getVarArgsRegSaveSize();
1401 int NumBytes
= (int)MFI
->getStackSize();
1403 if (!AFI
->hasStackFrame()) {
1405 emitSPUpdate(MBB
, MBBI
, NumBytes
, ARMCC::AL
, 0, isThumb
, TII
, *this, dl
);
1407 // Unwind MBBI to point to first LDR / FLDD.
1408 const unsigned *CSRegs
= getCalleeSavedRegs();
1409 if (MBBI
!= MBB
.begin()) {
1412 while (MBBI
!= MBB
.begin() && isCSRestore(MBBI
, CSRegs
));
1413 if (!isCSRestore(MBBI
, CSRegs
))
1417 // Move SP to start of FP callee save spill area.
1418 NumBytes
-= (AFI
->getGPRCalleeSavedArea1Size() +
1419 AFI
->getGPRCalleeSavedArea2Size() +
1420 AFI
->getDPRCalleeSavedAreaSize());
1423 NumBytes
= AFI
->getFramePtrSpillOffset() - NumBytes
;
1424 // Reset SP based on frame pointer only if the stack frame extends beyond
1425 // frame pointer stack slot or target is ELF and the function has FP.
1427 emitThumbRegPlusImmediate(MBB
, MBBI
, ARM::SP
, FramePtr
, -NumBytes
,
1430 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVlor2hir
), ARM::SP
)
1433 if (MBBI
->getOpcode() == ARM::tBX_RET
&&
1434 &MBB
.front() != MBBI
&&
1435 prior(MBBI
)->getOpcode() == ARM::tPOP
) {
1436 MachineBasicBlock::iterator PMBBI
= prior(MBBI
);
1437 emitSPUpdate(MBB
, PMBBI
, NumBytes
, ARMCC::AL
, 0, isThumb
, TII
,
1440 emitSPUpdate(MBB
, MBBI
, NumBytes
, ARMCC::AL
, 0, isThumb
, TII
,
1444 // Darwin ABI requires FP to point to the stack slot that contains the
1446 if ((STI
.isTargetDarwin() && NumBytes
) || hasFP(MF
)) {
1447 NumBytes
= AFI
->getFramePtrSpillOffset() - NumBytes
;
1448 // Reset SP based on frame pointer only if the stack frame extends beyond
1449 // frame pointer stack slot or target is ELF and the function has FP.
1450 if (AFI
->getGPRCalleeSavedArea2Size() ||
1451 AFI
->getDPRCalleeSavedAreaSize() ||
1452 AFI
->getDPRCalleeSavedAreaOffset()||
1455 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::SUBri
), ARM::SP
).addReg(FramePtr
)
1457 .addImm((unsigned)ARMCC::AL
).addReg(0).addReg(0);
1459 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::MOVr
), ARM::SP
).addReg(FramePtr
)
1460 .addImm((unsigned)ARMCC::AL
).addReg(0).addReg(0);
1462 } else if (NumBytes
) {
1463 emitSPUpdate(MBB
, MBBI
, NumBytes
, ARMCC::AL
, 0, false, TII
, *this, dl
);
1466 // Move SP to start of integer callee save spill area 2.
1467 movePastCSLoadStoreOps(MBB
, MBBI
, ARM::FLDD
, 3, STI
);
1468 emitSPUpdate(MBB
, MBBI
, AFI
->getDPRCalleeSavedAreaSize(), ARMCC::AL
, 0,
1469 false, TII
, *this, dl
);
1471 // Move SP to start of integer callee save spill area 1.
1472 movePastCSLoadStoreOps(MBB
, MBBI
, ARM::LDR
, 2, STI
);
1473 emitSPUpdate(MBB
, MBBI
, AFI
->getGPRCalleeSavedArea2Size(), ARMCC::AL
, 0,
1474 false, TII
, *this, dl
);
1476 // Move SP to SP upon entry to the function.
1477 movePastCSLoadStoreOps(MBB
, MBBI
, ARM::LDR
, 1, STI
);
1478 emitSPUpdate(MBB
, MBBI
, AFI
->getGPRCalleeSavedArea1Size(), ARMCC::AL
, 0,
1479 false, TII
, *this, dl
);
1483 if (VARegSaveSize
) {
1485 // Epilogue for vararg functions: pop LR to R3 and branch off it.
1486 // FIXME: Verify this is still ok when R3 is no longer being reserved.
1487 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tPOP
)).addReg(ARM::R3
);
1489 emitSPUpdate(MBB
, MBBI
, VARegSaveSize
, ARMCC::AL
, 0, isThumb
, TII
,
1493 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tBX_RET_vararg
)).addReg(ARM::R3
);
1499 unsigned ARMRegisterInfo::getRARegister() const {
1503 unsigned ARMRegisterInfo::getFrameRegister(MachineFunction
&MF
) const {
1504 if (STI
.isTargetDarwin() || hasFP(MF
))
1505 return (STI
.useThumbBacktraces() || STI
.isThumb()) ? ARM::R7
: ARM::R11
;
1510 unsigned ARMRegisterInfo::getEHExceptionRegister() const {
1511 assert(0 && "What is the exception register");
1515 unsigned ARMRegisterInfo::getEHHandlerRegister() const {
1516 assert(0 && "What is the exception handler register");
1520 int ARMRegisterInfo::getDwarfRegNum(unsigned RegNum
, bool isEH
) const {
1521 return ARMGenRegisterInfo::getDwarfRegNumFull(RegNum
, 0);
1524 #include "ARMGenRegisterInfo.inc"