1 //===- Thumb1RegisterInfo.cpp - Thumb-1 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 Thumb-1 implementation of the TargetRegisterInfo class.
12 //===----------------------------------------------------------------------===//
15 #include "ARMAddressingModes.h"
16 #include "ARMBaseInstrInfo.h"
17 #include "ARMMachineFunctionInfo.h"
18 #include "ARMSubtarget.h"
19 #include "Thumb1InstrInfo.h"
20 #include "Thumb1RegisterInfo.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Function.h"
24 #include "llvm/LLVMContext.h"
25 #include "llvm/CodeGen/MachineConstantPool.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineInstrBuilder.h"
29 #include "llvm/CodeGen/MachineLocation.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/Target/TargetFrameInfo.h"
32 #include "llvm/Target/TargetMachine.h"
33 #include "llvm/ADT/BitVector.h"
34 #include "llvm/ADT/SmallVector.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/raw_ostream.h"
41 ThumbRegScavenging("enable-thumb-reg-scavenging",
43 cl::desc("Enable register scavenging on Thumb"));
45 Thumb1RegisterInfo::Thumb1RegisterInfo(const ARMBaseInstrInfo
&tii
,
46 const ARMSubtarget
&sti
)
47 : ARMBaseRegisterInfo(tii
, sti
) {
50 /// emitLoadConstPool - Emits a load from constpool to materialize the
51 /// specified immediate.
52 void Thumb1RegisterInfo::emitLoadConstPool(MachineBasicBlock
&MBB
,
53 MachineBasicBlock::iterator
&MBBI
,
55 unsigned DestReg
, unsigned SubIdx
,
57 ARMCC::CondCodes Pred
,
58 unsigned PredReg
) const {
59 MachineFunction
&MF
= *MBB
.getParent();
60 MachineConstantPool
*ConstantPool
= MF
.getConstantPool();
61 Constant
*C
= ConstantInt::get(
62 Type::getInt32Ty(MBB
.getParent()->getFunction()->getContext()), Val
);
63 unsigned Idx
= ConstantPool
->getConstantPoolIndex(C
, 4);
65 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tLDRcp
))
66 .addReg(DestReg
, getDefRegState(true), SubIdx
)
67 .addConstantPoolIndex(Idx
).addImm(Pred
).addReg(PredReg
);
70 const TargetRegisterClass
*
71 Thumb1RegisterInfo::getPhysicalRegisterRegClass(unsigned Reg
, EVT VT
) const {
72 if (isARMLowRegister(Reg
))
73 return ARM::tGPRRegisterClass
;
77 case ARM::R8
: case ARM::R9
: case ARM::R10
: case ARM::R11
:
78 case ARM::R12
: case ARM::SP
: case ARM::LR
: case ARM::PC
:
79 return ARM::GPRRegisterClass
;
82 return TargetRegisterInfo::getPhysicalRegisterRegClass(Reg
, VT
);
86 Thumb1RegisterInfo::requiresRegisterScavenging(const MachineFunction
&MF
) const {
87 return ThumbRegScavenging
;
90 bool Thumb1RegisterInfo::hasReservedCallFrame(MachineFunction
&MF
) const {
91 const MachineFrameInfo
*FFI
= MF
.getFrameInfo();
92 unsigned CFSize
= FFI
->getMaxCallFrameSize();
93 // It's not always a good idea to include the call frame as part of the
94 // stack frame. ARM (especially Thumb) has small immediate offset to
95 // address the stack frame. So a large call frame can cause poor codegen
96 // and may even makes it impossible to scavenge a register.
97 if (CFSize
>= ((1 << 8) - 1) * 4 / 2) // Half of imm8 * 4
100 return !MF
.getFrameInfo()->hasVarSizedObjects();
104 /// emitThumbRegPlusImmInReg - Emits a series of instructions to materialize
105 /// a destreg = basereg + immediate in Thumb code. Materialize the immediate
106 /// in a register using mov / mvn sequences or load the immediate from a
109 void emitThumbRegPlusImmInReg(MachineBasicBlock
&MBB
,
110 MachineBasicBlock::iterator
&MBBI
,
111 unsigned DestReg
, unsigned BaseReg
,
112 int NumBytes
, bool CanChangeCC
,
113 const TargetInstrInfo
&TII
,
114 const Thumb1RegisterInfo
& MRI
,
116 bool isHigh
= !isARMLowRegister(DestReg
) ||
117 (BaseReg
!= 0 && !isARMLowRegister(BaseReg
));
119 // Subtract doesn't have high register version. Load the negative value
120 // if either base or dest register is a high register. Also, if do not
121 // issue sub as part of the sequence if condition register is to be
123 if (NumBytes
< 0 && !isHigh
&& CanChangeCC
) {
125 NumBytes
= -NumBytes
;
127 unsigned LdReg
= DestReg
;
128 if (DestReg
== ARM::SP
) {
129 assert(BaseReg
== ARM::SP
&& "Unexpected!");
131 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVtgpr2gpr
), ARM::R12
)
132 .addReg(ARM::R3
, RegState::Kill
);
135 if (NumBytes
<= 255 && NumBytes
>= 0)
136 AddDefaultT1CC(BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVi8
), LdReg
))
138 else if (NumBytes
< 0 && NumBytes
>= -255) {
139 AddDefaultT1CC(BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVi8
), LdReg
))
141 AddDefaultT1CC(BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tRSB
), LdReg
))
142 .addReg(LdReg
, RegState::Kill
);
144 MRI
.emitLoadConstPool(MBB
, MBBI
, dl
, LdReg
, 0, NumBytes
);
147 int Opc
= (isSub
) ? ARM::tSUBrr
: (isHigh
? ARM::tADDhirr
: ARM::tADDrr
);
148 MachineInstrBuilder MIB
=
149 BuildMI(MBB
, MBBI
, dl
, TII
.get(Opc
), DestReg
);
150 if (Opc
!= ARM::tADDhirr
)
151 MIB
= AddDefaultT1CC(MIB
);
152 if (DestReg
== ARM::SP
|| isSub
)
153 MIB
.addReg(BaseReg
).addReg(LdReg
, RegState::Kill
);
155 MIB
.addReg(LdReg
).addReg(BaseReg
, RegState::Kill
);
158 if (DestReg
== ARM::SP
)
159 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVgpr2tgpr
), ARM::R3
)
160 .addReg(ARM::R12
, RegState::Kill
);
163 /// calcNumMI - Returns the number of instructions required to materialize
164 /// the specific add / sub r, c instruction.
165 static unsigned calcNumMI(int Opc
, int ExtraOpc
, unsigned Bytes
,
166 unsigned NumBits
, unsigned Scale
) {
168 unsigned Chunk
= ((1 << NumBits
) - 1) * Scale
;
170 if (Opc
== ARM::tADDrSPi
) {
171 unsigned ThisVal
= (Bytes
> Chunk
) ? Chunk
: Bytes
;
175 Scale
= 1; // Followed by a number of tADDi8.
176 Chunk
= ((1 << NumBits
) - 1) * Scale
;
179 NumMIs
+= Bytes
/ Chunk
;
180 if ((Bytes
% Chunk
) != 0)
187 /// emitThumbRegPlusImmediate - Emits a series of instructions to materialize
188 /// a destreg = basereg + immediate in Thumb code.
190 void emitThumbRegPlusImmediate(MachineBasicBlock
&MBB
,
191 MachineBasicBlock::iterator
&MBBI
,
192 unsigned DestReg
, unsigned BaseReg
,
193 int NumBytes
, const TargetInstrInfo
&TII
,
194 const Thumb1RegisterInfo
& MRI
,
196 bool isSub
= NumBytes
< 0;
197 unsigned Bytes
= (unsigned)NumBytes
;
198 if (isSub
) Bytes
= -NumBytes
;
199 bool isMul4
= (Bytes
& 3) == 0;
200 bool isTwoAddr
= false;
201 bool DstNotEqBase
= false;
202 unsigned NumBits
= 1;
207 bool NeedPred
= false;
209 if (DestReg
== BaseReg
&& BaseReg
== ARM::SP
) {
210 assert(isMul4
&& "Thumb sp inc / dec size must be multiple of 4!");
213 Opc
= isSub
? ARM::tSUBspi
: ARM::tADDspi
;
215 } else if (!isSub
&& BaseReg
== ARM::SP
) {
218 // r1 = add sp, 100 * 4
222 ExtraOpc
= ARM::tADDi3
;
231 if (DestReg
!= BaseReg
)
234 if (DestReg
== ARM::SP
) {
235 Opc
= isSub
? ARM::tSUBspi
: ARM::tADDspi
;
236 assert(isMul4
&& "Thumb sp inc / dec size must be multiple of 4!");
240 Opc
= isSub
? ARM::tSUBi8
: ARM::tADDi8
;
242 NeedPred
= NeedCC
= true;
247 unsigned NumMIs
= calcNumMI(Opc
, ExtraOpc
, Bytes
, NumBits
, Scale
);
248 unsigned Threshold
= (DestReg
== ARM::SP
) ? 3 : 2;
249 if (NumMIs
> Threshold
) {
250 // This will expand into too many instructions. Load the immediate from a
252 emitThumbRegPlusImmInReg(MBB
, MBBI
, DestReg
, BaseReg
, NumBytes
, true, TII
,
258 if (isARMLowRegister(DestReg
) && isARMLowRegister(BaseReg
)) {
259 // If both are low registers, emit DestReg = add BaseReg, max(Imm, 7)
260 unsigned Chunk
= (1 << 3) - 1;
261 unsigned ThisVal
= (Bytes
> Chunk
) ? Chunk
: Bytes
;
263 const TargetInstrDesc
&TID
= TII
.get(isSub
? ARM::tSUBi3
: ARM::tADDi3
);
264 const MachineInstrBuilder MIB
=
265 AddDefaultT1CC(BuildMI(MBB
, MBBI
, dl
, TID
, DestReg
));
266 AddDefaultPred(MIB
.addReg(BaseReg
, RegState::Kill
).addImm(ThisVal
));
268 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVr
), DestReg
)
269 .addReg(BaseReg
, RegState::Kill
);
274 unsigned Chunk
= ((1 << NumBits
) - 1) * Scale
;
276 unsigned ThisVal
= (Bytes
> Chunk
) ? Chunk
: Bytes
;
279 // Build the new tADD / tSUB.
281 MachineInstrBuilder MIB
= BuildMI(MBB
, MBBI
, dl
, TII
.get(Opc
), DestReg
);
283 MIB
= AddDefaultT1CC(MIB
);
284 MIB
.addReg(DestReg
).addImm(ThisVal
);
286 MIB
= AddDefaultPred(MIB
);
289 bool isKill
= BaseReg
!= ARM::SP
;
290 MachineInstrBuilder MIB
= BuildMI(MBB
, MBBI
, dl
, TII
.get(Opc
), DestReg
);
292 MIB
= AddDefaultT1CC(MIB
);
293 MIB
.addReg(BaseReg
, getKillRegState(isKill
)).addImm(ThisVal
);
295 MIB
= AddDefaultPred(MIB
);
298 if (Opc
== ARM::tADDrSPi
) {
304 Chunk
= ((1 << NumBits
) - 1) * Scale
;
305 Opc
= isSub
? ARM::tSUBi8
: ARM::tADDi8
;
306 NeedPred
= NeedCC
= isTwoAddr
= true;
312 const TargetInstrDesc
&TID
= TII
.get(ExtraOpc
);
313 AddDefaultPred(AddDefaultT1CC(BuildMI(MBB
, MBBI
, dl
, TID
, DestReg
))
314 .addReg(DestReg
, RegState::Kill
)
315 .addImm(((unsigned)NumBytes
) & 3));
319 static void emitSPUpdate(MachineBasicBlock
&MBB
,
320 MachineBasicBlock::iterator
&MBBI
,
321 const TargetInstrInfo
&TII
, DebugLoc dl
,
322 const Thumb1RegisterInfo
&MRI
,
324 emitThumbRegPlusImmediate(MBB
, MBBI
, ARM::SP
, ARM::SP
, NumBytes
, TII
,
328 void Thumb1RegisterInfo::
329 eliminateCallFramePseudoInstr(MachineFunction
&MF
, MachineBasicBlock
&MBB
,
330 MachineBasicBlock::iterator I
) const {
331 if (!hasReservedCallFrame(MF
)) {
332 // If we have alloca, convert as follows:
333 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
334 // ADJCALLSTACKUP -> add, sp, sp, amount
335 MachineInstr
*Old
= I
;
336 DebugLoc dl
= Old
->getDebugLoc();
337 unsigned Amount
= Old
->getOperand(0).getImm();
339 // We need to keep the stack aligned properly. To do this, we round the
340 // amount of space needed for the outgoing arguments up to the next
341 // alignment boundary.
342 unsigned Align
= MF
.getTarget().getFrameInfo()->getStackAlignment();
343 Amount
= (Amount
+Align
-1)/Align
*Align
;
345 // Replace the pseudo instruction with a new instruction...
346 unsigned Opc
= Old
->getOpcode();
347 if (Opc
== ARM::ADJCALLSTACKDOWN
|| Opc
== ARM::tADJCALLSTACKDOWN
) {
348 emitSPUpdate(MBB
, I
, TII
, dl
, *this, -Amount
);
350 assert(Opc
== ARM::ADJCALLSTACKUP
|| Opc
== ARM::tADJCALLSTACKUP
);
351 emitSPUpdate(MBB
, I
, TII
, dl
, *this, Amount
);
358 /// emitThumbConstant - Emit a series of instructions to materialize a
360 static void emitThumbConstant(MachineBasicBlock
&MBB
,
361 MachineBasicBlock::iterator
&MBBI
,
362 unsigned DestReg
, int Imm
,
363 const TargetInstrInfo
&TII
,
364 const Thumb1RegisterInfo
& MRI
,
366 bool isSub
= Imm
< 0;
367 if (isSub
) Imm
= -Imm
;
369 int Chunk
= (1 << 8) - 1;
370 int ThisVal
= (Imm
> Chunk
) ? Chunk
: Imm
;
372 AddDefaultPred(AddDefaultT1CC(BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVi8
),
376 emitThumbRegPlusImmediate(MBB
, MBBI
, DestReg
, DestReg
, Imm
, TII
, MRI
, dl
);
378 const TargetInstrDesc
&TID
= TII
.get(ARM::tRSB
);
379 AddDefaultPred(AddDefaultT1CC(BuildMI(MBB
, MBBI
, dl
, TID
, DestReg
))
380 .addReg(DestReg
, RegState::Kill
));
384 static void removeOperands(MachineInstr
&MI
, unsigned i
) {
386 for (unsigned e
= MI
.getNumOperands(); i
!= e
; ++i
)
387 MI
.RemoveOperand(Op
);
390 int Thumb1RegisterInfo::
391 rewriteFrameIndex(MachineInstr
&MI
, unsigned FrameRegIdx
,
392 unsigned FrameReg
, int Offset
,
393 unsigned MOVOpc
, unsigned ADDriOpc
, unsigned SUBriOpc
) const
395 // if/when eliminateFrameIndex() conforms with ARMBaseRegisterInfo
396 // version then can pull out Thumb1 specific parts here
400 void Thumb1RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II
,
401 int SPAdj
, RegScavenger
*RS
) const{
403 MachineInstr
&MI
= *II
;
404 MachineBasicBlock
&MBB
= *MI
.getParent();
405 MachineFunction
&MF
= *MBB
.getParent();
406 ARMFunctionInfo
*AFI
= MF
.getInfo
<ARMFunctionInfo
>();
407 DebugLoc dl
= MI
.getDebugLoc();
409 while (!MI
.getOperand(i
).isFI()) {
411 assert(i
< MI
.getNumOperands() && "Instr doesn't have FrameIndex operand!");
414 unsigned FrameReg
= ARM::SP
;
415 int FrameIndex
= MI
.getOperand(i
).getIndex();
416 int Offset
= MF
.getFrameInfo()->getObjectOffset(FrameIndex
) +
417 MF
.getFrameInfo()->getStackSize() + SPAdj
;
419 if (AFI
->isGPRCalleeSavedArea1Frame(FrameIndex
))
420 Offset
-= AFI
->getGPRCalleeSavedArea1Offset();
421 else if (AFI
->isGPRCalleeSavedArea2Frame(FrameIndex
))
422 Offset
-= AFI
->getGPRCalleeSavedArea2Offset();
423 else if (hasFP(MF
)) {
424 assert(SPAdj
== 0 && "Unexpected");
425 // There is alloca()'s in this function, must reference off the frame
427 FrameReg
= getFrameRegister(MF
);
428 Offset
-= AFI
->getFramePtrSpillOffset();
431 unsigned Opcode
= MI
.getOpcode();
432 const TargetInstrDesc
&Desc
= MI
.getDesc();
433 unsigned AddrMode
= (Desc
.TSFlags
& ARMII::AddrModeMask
);
435 if (Opcode
== ARM::tADDrSPi
) {
436 Offset
+= MI
.getOperand(i
+1).getImm();
438 // Can't use tADDrSPi if it's based off the frame pointer.
439 unsigned NumBits
= 0;
441 if (FrameReg
!= ARM::SP
) {
442 Opcode
= ARM::tADDi3
;
443 MI
.setDesc(TII
.get(Opcode
));
448 assert((Offset
& 3) == 0 &&
449 "Thumb add/sub sp, #imm immediate must be multiple of 4!");
453 // Turn it into a move.
454 MI
.setDesc(TII
.get(ARM::tMOVgpr2tgpr
));
455 MI
.getOperand(i
).ChangeToRegister(FrameReg
, false);
456 MI
.RemoveOperand(i
+1);
460 // Common case: small offset, fits into instruction.
461 unsigned Mask
= (1 << NumBits
) - 1;
462 if (((Offset
/ Scale
) & ~Mask
) == 0) {
463 // Replace the FrameIndex with sp / fp
464 if (Opcode
== ARM::tADDi3
) {
465 removeOperands(MI
, i
);
466 MachineInstrBuilder
MIB(&MI
);
467 AddDefaultPred(AddDefaultT1CC(MIB
).addReg(FrameReg
)
468 .addImm(Offset
/ Scale
));
470 MI
.getOperand(i
).ChangeToRegister(FrameReg
, false);
471 MI
.getOperand(i
+1).ChangeToImmediate(Offset
/ Scale
);
476 unsigned DestReg
= MI
.getOperand(0).getReg();
477 unsigned Bytes
= (Offset
> 0) ? Offset
: -Offset
;
478 unsigned NumMIs
= calcNumMI(Opcode
, 0, Bytes
, NumBits
, Scale
);
479 // MI would expand into a large number of instructions. Don't try to
480 // simplify the immediate.
482 emitThumbRegPlusImmediate(MBB
, II
, DestReg
, FrameReg
, Offset
, TII
,
489 // Translate r0 = add sp, imm to
490 // r0 = add sp, 255*4
491 // r0 = add r0, (imm - 255*4)
492 if (Opcode
== ARM::tADDi3
) {
493 removeOperands(MI
, i
);
494 MachineInstrBuilder
MIB(&MI
);
495 AddDefaultPred(AddDefaultT1CC(MIB
).addReg(FrameReg
).addImm(Mask
));
497 MI
.getOperand(i
).ChangeToRegister(FrameReg
, false);
498 MI
.getOperand(i
+1).ChangeToImmediate(Mask
);
500 Offset
= (Offset
- Mask
* Scale
);
501 MachineBasicBlock::iterator NII
= next(II
);
502 emitThumbRegPlusImmediate(MBB
, NII
, DestReg
, DestReg
, Offset
, TII
,
505 // Translate r0 = add sp, -imm to
506 // r0 = -imm (this is then translated into a series of instructons)
508 emitThumbConstant(MBB
, II
, DestReg
, Offset
, TII
, *this, dl
);
510 MI
.setDesc(TII
.get(ARM::tADDhirr
));
511 MI
.getOperand(i
).ChangeToRegister(DestReg
, false, false, true);
512 MI
.getOperand(i
+1).ChangeToRegister(FrameReg
, false);
513 if (Opcode
== ARM::tADDi3
) {
514 MachineInstrBuilder
MIB(&MI
);
522 unsigned NumBits
= 0;
525 case ARMII::AddrModeT1_s
: {
527 InstrOffs
= MI
.getOperand(ImmIdx
).getImm();
528 NumBits
= (FrameReg
== ARM::SP
) ? 8 : 5;
533 llvm_unreachable("Unsupported addressing mode!");
537 Offset
+= InstrOffs
* Scale
;
538 assert((Offset
& (Scale
-1)) == 0 && "Can't encode this offset!");
540 // Common case: small offset, fits into instruction.
541 MachineOperand
&ImmOp
= MI
.getOperand(ImmIdx
);
542 int ImmedOffset
= Offset
/ Scale
;
543 unsigned Mask
= (1 << NumBits
) - 1;
544 if ((unsigned)Offset
<= Mask
* Scale
) {
545 // Replace the FrameIndex with sp
546 MI
.getOperand(i
).ChangeToRegister(FrameReg
, false);
547 ImmOp
.ChangeToImmediate(ImmedOffset
);
551 bool isThumSpillRestore
= Opcode
== ARM::tRestore
|| Opcode
== ARM::tSpill
;
552 if (AddrMode
== ARMII::AddrModeT1_s
) {
553 // Thumb tLDRspi, tSTRspi. These will change to instructions that use
554 // a different base register.
556 Mask
= (1 << NumBits
) - 1;
558 // If this is a thumb spill / restore, we will be using a constpool load to
559 // materialize the offset.
560 if (AddrMode
== ARMII::AddrModeT1_s
&& isThumSpillRestore
)
561 ImmOp
.ChangeToImmediate(0);
563 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
564 ImmedOffset
= ImmedOffset
& Mask
;
565 ImmOp
.ChangeToImmediate(ImmedOffset
);
566 Offset
&= ~(Mask
*Scale
);
570 // If we get here, the immediate doesn't fit into the instruction. We folded
571 // as much as possible above, handle the rest, providing a register that is
573 assert(Offset
&& "This code isn't needed if offset already handled!");
575 // Remove predicate first.
576 int PIdx
= MI
.findFirstPredOperandIdx();
578 removeOperands(MI
, PIdx
);
580 if (Desc
.mayLoad()) {
581 // Use the destination register to materialize sp + offset.
582 unsigned TmpReg
= MI
.getOperand(0).getReg();
584 if (Opcode
== ARM::tRestore
) {
585 if (FrameReg
== ARM::SP
)
586 emitThumbRegPlusImmInReg(MBB
, II
, TmpReg
, FrameReg
,
587 Offset
, false, TII
, *this, dl
);
589 emitLoadConstPool(MBB
, II
, dl
, TmpReg
, 0, Offset
);
593 emitThumbRegPlusImmediate(MBB
, II
, TmpReg
, FrameReg
, Offset
, TII
,
597 MI
.setDesc(TII
.get(ARM::tLDR
));
598 MI
.getOperand(i
).ChangeToRegister(TmpReg
, false, false, true);
600 // Use [reg, reg] addrmode.
601 MI
.addOperand(MachineOperand::CreateReg(FrameReg
, false));
602 else // tLDR has an extra register operand.
603 MI
.addOperand(MachineOperand::CreateReg(0, false));
604 } else if (Desc
.mayStore()) {
605 // FIXME! This is horrific!!! We need register scavenging.
606 // Our temporary workaround has marked r3 unavailable. Of course, r3 is
607 // also a ABI register so it's possible that is is the register that is
608 // being storing here. If that's the case, we do the following:
610 // Use r2 to materialize sp + offset
613 unsigned ValReg
= MI
.getOperand(0).getReg();
614 unsigned TmpReg
= ARM::R3
;
616 if (ValReg
== ARM::R3
) {
617 BuildMI(MBB
, II
, dl
, TII
.get(ARM::tMOVtgpr2gpr
), ARM::R12
)
618 .addReg(ARM::R2
, RegState::Kill
);
621 if (TmpReg
== ARM::R3
&& AFI
->isR3LiveIn())
622 BuildMI(MBB
, II
, dl
, TII
.get(ARM::tMOVtgpr2gpr
), ARM::R12
)
623 .addReg(ARM::R3
, RegState::Kill
);
624 if (Opcode
== ARM::tSpill
) {
625 if (FrameReg
== ARM::SP
)
626 emitThumbRegPlusImmInReg(MBB
, II
, TmpReg
, FrameReg
,
627 Offset
, false, TII
, *this, dl
);
629 emitLoadConstPool(MBB
, II
, dl
, TmpReg
, 0, Offset
);
633 emitThumbRegPlusImmediate(MBB
, II
, TmpReg
, FrameReg
, Offset
, TII
,
635 MI
.setDesc(TII
.get(ARM::tSTR
));
636 MI
.getOperand(i
).ChangeToRegister(TmpReg
, false, false, true);
637 if (UseRR
) // Use [reg, reg] addrmode.
638 MI
.addOperand(MachineOperand::CreateReg(FrameReg
, false));
639 else // tSTR has an extra register operand.
640 MI
.addOperand(MachineOperand::CreateReg(0, false));
642 MachineBasicBlock::iterator NII
= next(II
);
643 if (ValReg
== ARM::R3
)
644 BuildMI(MBB
, NII
, dl
, TII
.get(ARM::tMOVgpr2tgpr
), ARM::R2
)
645 .addReg(ARM::R12
, RegState::Kill
);
646 if (TmpReg
== ARM::R3
&& AFI
->isR3LiveIn())
647 BuildMI(MBB
, NII
, dl
, TII
.get(ARM::tMOVgpr2tgpr
), ARM::R3
)
648 .addReg(ARM::R12
, RegState::Kill
);
650 assert(false && "Unexpected opcode!");
652 // Add predicate back if it's needed.
653 if (MI
.getDesc().isPredicable()) {
654 MachineInstrBuilder
MIB(&MI
);
659 void Thumb1RegisterInfo::emitPrologue(MachineFunction
&MF
) const {
660 MachineBasicBlock
&MBB
= MF
.front();
661 MachineBasicBlock::iterator MBBI
= MBB
.begin();
662 MachineFrameInfo
*MFI
= MF
.getFrameInfo();
663 ARMFunctionInfo
*AFI
= MF
.getInfo
<ARMFunctionInfo
>();
664 unsigned VARegSaveSize
= AFI
->getVarArgsRegSaveSize();
665 unsigned NumBytes
= MFI
->getStackSize();
666 const std::vector
<CalleeSavedInfo
> &CSI
= MFI
->getCalleeSavedInfo();
667 DebugLoc dl
= (MBBI
!= MBB
.end() ?
668 MBBI
->getDebugLoc() : DebugLoc::getUnknownLoc());
670 // Check if R3 is live in. It might have to be used as a scratch register.
671 for (MachineRegisterInfo::livein_iterator I
=MF
.getRegInfo().livein_begin(),
672 E
= MF
.getRegInfo().livein_end(); I
!= E
; ++I
) {
673 if (I
->first
== ARM::R3
) {
674 AFI
->setR3IsLiveIn(true);
679 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
680 NumBytes
= (NumBytes
+ 3) & ~3;
681 MFI
->setStackSize(NumBytes
);
683 // Determine the sizes of each callee-save spill areas and record which frame
684 // belongs to which callee-save spill areas.
685 unsigned GPRCS1Size
= 0, GPRCS2Size
= 0, DPRCSSize
= 0;
686 int FramePtrSpillFI
= 0;
689 emitSPUpdate(MBB
, MBBI
, TII
, dl
, *this, -VARegSaveSize
);
691 if (!AFI
->hasStackFrame()) {
693 emitSPUpdate(MBB
, MBBI
, TII
, dl
, *this, -NumBytes
);
697 for (unsigned i
= 0, e
= CSI
.size(); i
!= e
; ++i
) {
698 unsigned Reg
= CSI
[i
].getReg();
699 int FI
= CSI
[i
].getFrameIdx();
707 FramePtrSpillFI
= FI
;
708 AFI
->addGPRCalleeSavedArea1Frame(FI
);
716 FramePtrSpillFI
= FI
;
717 if (STI
.isTargetDarwin()) {
718 AFI
->addGPRCalleeSavedArea2Frame(FI
);
721 AFI
->addGPRCalleeSavedArea1Frame(FI
);
726 AFI
->addDPRCalleeSavedAreaFrame(FI
);
731 if (MBBI
!= MBB
.end() && MBBI
->getOpcode() == ARM::tPUSH
) {
733 if (MBBI
!= MBB
.end())
734 dl
= MBBI
->getDebugLoc();
737 // Darwin ABI requires FP to point to the stack slot that contains the
739 if (STI
.isTargetDarwin() || hasFP(MF
)) {
740 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tADDrSPi
), FramePtr
)
741 .addFrameIndex(FramePtrSpillFI
).addImm(0);
744 // Determine starting offsets of spill areas.
745 unsigned DPRCSOffset
= NumBytes
- (GPRCS1Size
+ GPRCS2Size
+ DPRCSSize
);
746 unsigned GPRCS2Offset
= DPRCSOffset
+ DPRCSSize
;
747 unsigned GPRCS1Offset
= GPRCS2Offset
+ GPRCS2Size
;
748 AFI
->setFramePtrSpillOffset(MFI
->getObjectOffset(FramePtrSpillFI
) + NumBytes
);
749 AFI
->setGPRCalleeSavedArea1Offset(GPRCS1Offset
);
750 AFI
->setGPRCalleeSavedArea2Offset(GPRCS2Offset
);
751 AFI
->setDPRCalleeSavedAreaOffset(DPRCSOffset
);
753 NumBytes
= DPRCSOffset
;
755 // Insert it after all the callee-save spills.
756 emitSPUpdate(MBB
, MBBI
, TII
, dl
, *this, -NumBytes
);
759 if (STI
.isTargetELF() && hasFP(MF
)) {
760 MFI
->setOffsetAdjustment(MFI
->getOffsetAdjustment() -
761 AFI
->getFramePtrSpillOffset());
764 AFI
->setGPRCalleeSavedArea1Size(GPRCS1Size
);
765 AFI
->setGPRCalleeSavedArea2Size(GPRCS2Size
);
766 AFI
->setDPRCalleeSavedAreaSize(DPRCSSize
);
769 static bool isCalleeSavedRegister(unsigned Reg
, const unsigned *CSRegs
) {
770 for (unsigned i
= 0; CSRegs
[i
]; ++i
)
771 if (Reg
== CSRegs
[i
])
776 static bool isCSRestore(MachineInstr
*MI
, const unsigned *CSRegs
) {
777 return (MI
->getOpcode() == ARM::tRestore
&&
778 MI
->getOperand(1).isFI() &&
779 isCalleeSavedRegister(MI
->getOperand(0).getReg(), CSRegs
));
782 void Thumb1RegisterInfo::emitEpilogue(MachineFunction
&MF
,
783 MachineBasicBlock
&MBB
) const {
784 MachineBasicBlock::iterator MBBI
= prior(MBB
.end());
785 assert((MBBI
->getOpcode() == ARM::tBX_RET
||
786 MBBI
->getOpcode() == ARM::tPOP_RET
) &&
787 "Can only insert epilog into returning blocks");
788 DebugLoc dl
= MBBI
->getDebugLoc();
789 MachineFrameInfo
*MFI
= MF
.getFrameInfo();
790 ARMFunctionInfo
*AFI
= MF
.getInfo
<ARMFunctionInfo
>();
791 unsigned VARegSaveSize
= AFI
->getVarArgsRegSaveSize();
792 int NumBytes
= (int)MFI
->getStackSize();
794 if (!AFI
->hasStackFrame()) {
796 emitSPUpdate(MBB
, MBBI
, TII
, dl
, *this, NumBytes
);
798 // Unwind MBBI to point to first LDR / FLDD.
799 const unsigned *CSRegs
= getCalleeSavedRegs();
800 if (MBBI
!= MBB
.begin()) {
803 while (MBBI
!= MBB
.begin() && isCSRestore(MBBI
, CSRegs
));
804 if (!isCSRestore(MBBI
, CSRegs
))
808 // Move SP to start of FP callee save spill area.
809 NumBytes
-= (AFI
->getGPRCalleeSavedArea1Size() +
810 AFI
->getGPRCalleeSavedArea2Size() +
811 AFI
->getDPRCalleeSavedAreaSize());
814 NumBytes
= AFI
->getFramePtrSpillOffset() - NumBytes
;
815 // Reset SP based on frame pointer only if the stack frame extends beyond
816 // frame pointer stack slot or target is ELF and the function has FP.
818 emitThumbRegPlusImmediate(MBB
, MBBI
, ARM::SP
, FramePtr
, -NumBytes
,
821 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVtgpr2gpr
), ARM::SP
)
824 if (MBBI
->getOpcode() == ARM::tBX_RET
&&
825 &MBB
.front() != MBBI
&&
826 prior(MBBI
)->getOpcode() == ARM::tPOP
) {
827 MachineBasicBlock::iterator PMBBI
= prior(MBBI
);
828 emitSPUpdate(MBB
, PMBBI
, TII
, dl
, *this, NumBytes
);
830 emitSPUpdate(MBB
, MBBI
, TII
, dl
, *this, NumBytes
);
835 // Epilogue for vararg functions: pop LR to R3 and branch off it.
836 // FIXME: Verify this is still ok when R3 is no longer being reserved.
837 AddDefaultPred(BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tPOP
))).addReg(ARM::R3
);
839 emitSPUpdate(MBB
, MBBI
, TII
, dl
, *this, VARegSaveSize
);
841 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tBX_RET_vararg
)).addReg(ARM::R3
);