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
13 //===----------------------------------------------------------------------===//
16 #include "ARMAddressingModes.h"
17 #include "ARMBaseInstrInfo.h"
18 #include "ARMMachineFunctionInfo.h"
19 #include "ARMSubtarget.h"
20 #include "Thumb1InstrInfo.h"
21 #include "Thumb1RegisterInfo.h"
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Function.h"
25 #include "llvm/LLVMContext.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineLocation.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/Target/TargetFrameInfo.h"
33 #include "llvm/Target/TargetMachine.h"
34 #include "llvm/ADT/BitVector.h"
35 #include "llvm/ADT/SmallVector.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/raw_ostream.h"
41 extern cl::opt
<bool> ReuseFrameIndexVals
;
46 Thumb1RegisterInfo::Thumb1RegisterInfo(const ARMBaseInstrInfo
&tii
,
47 const ARMSubtarget
&sti
)
48 : ARMBaseRegisterInfo(tii
, sti
) {
51 /// emitLoadConstPool - Emits a load from constpool to materialize the
52 /// specified immediate.
53 void Thumb1RegisterInfo::emitLoadConstPool(MachineBasicBlock
&MBB
,
54 MachineBasicBlock::iterator
&MBBI
,
56 unsigned DestReg
, unsigned SubIdx
,
58 ARMCC::CondCodes Pred
,
59 unsigned PredReg
) const {
60 MachineFunction
&MF
= *MBB
.getParent();
61 MachineConstantPool
*ConstantPool
= MF
.getConstantPool();
62 const Constant
*C
= ConstantInt::get(
63 Type::getInt32Ty(MBB
.getParent()->getFunction()->getContext()), Val
);
64 unsigned Idx
= ConstantPool
->getConstantPoolIndex(C
, 4);
66 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tLDRcp
))
67 .addReg(DestReg
, getDefRegState(true), SubIdx
)
68 .addConstantPoolIndex(Idx
).addImm(Pred
).addReg(PredReg
);
71 bool Thumb1RegisterInfo::hasReservedCallFrame(const MachineFunction
&MF
) const {
72 const MachineFrameInfo
*FFI
= MF
.getFrameInfo();
73 unsigned CFSize
= FFI
->getMaxCallFrameSize();
74 // It's not always a good idea to include the call frame as part of the
75 // stack frame. ARM (especially Thumb) has small immediate offset to
76 // address the stack frame. So a large call frame can cause poor codegen
77 // and may even makes it impossible to scavenge a register.
78 if (CFSize
>= ((1 << 8) - 1) * 4 / 2) // Half of imm8 * 4
81 return !MF
.getFrameInfo()->hasVarSizedObjects();
85 /// emitThumbRegPlusImmInReg - Emits a series of instructions to materialize
86 /// a destreg = basereg + immediate in Thumb code. Materialize the immediate
87 /// in a register using mov / mvn sequences or load the immediate from a
90 void emitThumbRegPlusImmInReg(MachineBasicBlock
&MBB
,
91 MachineBasicBlock::iterator
&MBBI
,
92 unsigned DestReg
, unsigned BaseReg
,
93 int NumBytes
, bool CanChangeCC
,
94 const TargetInstrInfo
&TII
,
95 const ARMBaseRegisterInfo
& MRI
,
97 MachineFunction
&MF
= *MBB
.getParent();
98 bool isHigh
= !isARMLowRegister(DestReg
) ||
99 (BaseReg
!= 0 && !isARMLowRegister(BaseReg
));
101 // Subtract doesn't have high register version. Load the negative value
102 // if either base or dest register is a high register. Also, if do not
103 // issue sub as part of the sequence if condition register is to be
105 if (NumBytes
< 0 && !isHigh
&& CanChangeCC
) {
107 NumBytes
= -NumBytes
;
109 unsigned LdReg
= DestReg
;
110 if (DestReg
== ARM::SP
) {
111 assert(BaseReg
== ARM::SP
&& "Unexpected!");
112 LdReg
= MF
.getRegInfo().createVirtualRegister(ARM::tGPRRegisterClass
);
115 if (NumBytes
<= 255 && NumBytes
>= 0)
116 AddDefaultT1CC(BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVi8
), LdReg
))
118 else if (NumBytes
< 0 && NumBytes
>= -255) {
119 AddDefaultT1CC(BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVi8
), LdReg
))
121 AddDefaultT1CC(BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tRSB
), LdReg
))
122 .addReg(LdReg
, RegState::Kill
);
124 MRI
.emitLoadConstPool(MBB
, MBBI
, dl
, LdReg
, 0, NumBytes
);
127 int Opc
= (isSub
) ? ARM::tSUBrr
: (isHigh
? ARM::tADDhirr
: ARM::tADDrr
);
128 MachineInstrBuilder MIB
=
129 BuildMI(MBB
, MBBI
, dl
, TII
.get(Opc
), DestReg
);
130 if (Opc
!= ARM::tADDhirr
)
131 MIB
= AddDefaultT1CC(MIB
);
132 if (DestReg
== ARM::SP
|| isSub
)
133 MIB
.addReg(BaseReg
).addReg(LdReg
, RegState::Kill
);
135 MIB
.addReg(LdReg
).addReg(BaseReg
, RegState::Kill
);
139 /// calcNumMI - Returns the number of instructions required to materialize
140 /// the specific add / sub r, c instruction.
141 static unsigned calcNumMI(int Opc
, int ExtraOpc
, unsigned Bytes
,
142 unsigned NumBits
, unsigned Scale
) {
144 unsigned Chunk
= ((1 << NumBits
) - 1) * Scale
;
146 if (Opc
== ARM::tADDrSPi
) {
147 unsigned ThisVal
= (Bytes
> Chunk
) ? Chunk
: Bytes
;
151 Scale
= 1; // Followed by a number of tADDi8.
152 Chunk
= ((1 << NumBits
) - 1) * Scale
;
155 NumMIs
+= Bytes
/ Chunk
;
156 if ((Bytes
% Chunk
) != 0)
163 /// emitThumbRegPlusImmediate - Emits a series of instructions to materialize
164 /// a destreg = basereg + immediate in Thumb code.
165 void llvm::emitThumbRegPlusImmediate(MachineBasicBlock
&MBB
,
166 MachineBasicBlock::iterator
&MBBI
,
167 unsigned DestReg
, unsigned BaseReg
,
168 int NumBytes
, const TargetInstrInfo
&TII
,
169 const ARMBaseRegisterInfo
& MRI
,
171 bool isSub
= NumBytes
< 0;
172 unsigned Bytes
= (unsigned)NumBytes
;
173 if (isSub
) Bytes
= -NumBytes
;
174 bool isMul4
= (Bytes
& 3) == 0;
175 bool isTwoAddr
= false;
176 bool DstNotEqBase
= false;
177 unsigned NumBits
= 1;
182 bool NeedPred
= false;
184 if (DestReg
== BaseReg
&& BaseReg
== ARM::SP
) {
185 assert(isMul4
&& "Thumb sp inc / dec size must be multiple of 4!");
188 Opc
= isSub
? ARM::tSUBspi
: ARM::tADDspi
;
190 } else if (!isSub
&& BaseReg
== ARM::SP
) {
193 // r1 = add sp, 100 * 4
197 ExtraOpc
= ARM::tADDi3
;
206 if (DestReg
!= BaseReg
)
209 if (DestReg
== ARM::SP
) {
210 Opc
= isSub
? ARM::tSUBspi
: ARM::tADDspi
;
211 assert(isMul4
&& "Thumb sp inc / dec size must be multiple of 4!");
215 Opc
= isSub
? ARM::tSUBi8
: ARM::tADDi8
;
217 NeedPred
= NeedCC
= true;
222 unsigned NumMIs
= calcNumMI(Opc
, ExtraOpc
, Bytes
, NumBits
, Scale
);
223 unsigned Threshold
= (DestReg
== ARM::SP
) ? 3 : 2;
224 if (NumMIs
> Threshold
) {
225 // This will expand into too many instructions. Load the immediate from a
227 emitThumbRegPlusImmInReg(MBB
, MBBI
, DestReg
, BaseReg
, NumBytes
, true, TII
,
233 if (isARMLowRegister(DestReg
) && isARMLowRegister(BaseReg
)) {
234 // If both are low registers, emit DestReg = add BaseReg, max(Imm, 7)
235 unsigned Chunk
= (1 << 3) - 1;
236 unsigned ThisVal
= (Bytes
> Chunk
) ? Chunk
: Bytes
;
238 const TargetInstrDesc
&TID
= TII
.get(isSub
? ARM::tSUBi3
: ARM::tADDi3
);
239 const MachineInstrBuilder MIB
=
240 AddDefaultT1CC(BuildMI(MBB
, MBBI
, dl
, TID
, DestReg
));
241 AddDefaultPred(MIB
.addReg(BaseReg
, RegState::Kill
).addImm(ThisVal
));
243 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVr
), DestReg
)
244 .addReg(BaseReg
, RegState::Kill
);
249 unsigned Chunk
= ((1 << NumBits
) - 1) * Scale
;
251 unsigned ThisVal
= (Bytes
> Chunk
) ? Chunk
: Bytes
;
254 // Build the new tADD / tSUB.
256 MachineInstrBuilder MIB
= BuildMI(MBB
, MBBI
, dl
, TII
.get(Opc
), DestReg
);
258 MIB
= AddDefaultT1CC(MIB
);
259 MIB
.addReg(DestReg
).addImm(ThisVal
);
261 MIB
= AddDefaultPred(MIB
);
264 bool isKill
= BaseReg
!= ARM::SP
;
265 MachineInstrBuilder MIB
= BuildMI(MBB
, MBBI
, dl
, TII
.get(Opc
), DestReg
);
267 MIB
= AddDefaultT1CC(MIB
);
268 MIB
.addReg(BaseReg
, getKillRegState(isKill
)).addImm(ThisVal
);
270 MIB
= AddDefaultPred(MIB
);
273 if (Opc
== ARM::tADDrSPi
) {
279 Chunk
= ((1 << NumBits
) - 1) * Scale
;
280 Opc
= isSub
? ARM::tSUBi8
: ARM::tADDi8
;
281 NeedPred
= NeedCC
= isTwoAddr
= true;
287 const TargetInstrDesc
&TID
= TII
.get(ExtraOpc
);
288 AddDefaultPred(AddDefaultT1CC(BuildMI(MBB
, MBBI
, dl
, TID
, DestReg
))
289 .addReg(DestReg
, RegState::Kill
)
290 .addImm(((unsigned)NumBytes
) & 3));
294 static void emitSPUpdate(MachineBasicBlock
&MBB
,
295 MachineBasicBlock::iterator
&MBBI
,
296 const TargetInstrInfo
&TII
, DebugLoc dl
,
297 const Thumb1RegisterInfo
&MRI
,
299 emitThumbRegPlusImmediate(MBB
, MBBI
, ARM::SP
, ARM::SP
, NumBytes
, TII
,
303 void Thumb1RegisterInfo::
304 eliminateCallFramePseudoInstr(MachineFunction
&MF
, MachineBasicBlock
&MBB
,
305 MachineBasicBlock::iterator I
) const {
306 if (!hasReservedCallFrame(MF
)) {
307 // If we have alloca, convert as follows:
308 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
309 // ADJCALLSTACKUP -> add, sp, sp, amount
310 MachineInstr
*Old
= I
;
311 DebugLoc dl
= Old
->getDebugLoc();
312 unsigned Amount
= Old
->getOperand(0).getImm();
314 // We need to keep the stack aligned properly. To do this, we round the
315 // amount of space needed for the outgoing arguments up to the next
316 // alignment boundary.
317 unsigned Align
= MF
.getTarget().getFrameInfo()->getStackAlignment();
318 Amount
= (Amount
+Align
-1)/Align
*Align
;
320 // Replace the pseudo instruction with a new instruction...
321 unsigned Opc
= Old
->getOpcode();
322 if (Opc
== ARM::ADJCALLSTACKDOWN
|| Opc
== ARM::tADJCALLSTACKDOWN
) {
323 emitSPUpdate(MBB
, I
, TII
, dl
, *this, -Amount
);
325 assert(Opc
== ARM::ADJCALLSTACKUP
|| Opc
== ARM::tADJCALLSTACKUP
);
326 emitSPUpdate(MBB
, I
, TII
, dl
, *this, Amount
);
333 /// emitThumbConstant - Emit a series of instructions to materialize a
335 static void emitThumbConstant(MachineBasicBlock
&MBB
,
336 MachineBasicBlock::iterator
&MBBI
,
337 unsigned DestReg
, int Imm
,
338 const TargetInstrInfo
&TII
,
339 const Thumb1RegisterInfo
& MRI
,
341 bool isSub
= Imm
< 0;
342 if (isSub
) Imm
= -Imm
;
344 int Chunk
= (1 << 8) - 1;
345 int ThisVal
= (Imm
> Chunk
) ? Chunk
: Imm
;
347 AddDefaultPred(AddDefaultT1CC(BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVi8
),
351 emitThumbRegPlusImmediate(MBB
, MBBI
, DestReg
, DestReg
, Imm
, TII
, MRI
, dl
);
353 const TargetInstrDesc
&TID
= TII
.get(ARM::tRSB
);
354 AddDefaultPred(AddDefaultT1CC(BuildMI(MBB
, MBBI
, dl
, TID
, DestReg
))
355 .addReg(DestReg
, RegState::Kill
));
359 static void removeOperands(MachineInstr
&MI
, unsigned i
) {
361 for (unsigned e
= MI
.getNumOperands(); i
!= e
; ++i
)
362 MI
.RemoveOperand(Op
);
365 bool Thumb1RegisterInfo::
366 rewriteFrameIndex(MachineBasicBlock::iterator II
, unsigned FrameRegIdx
,
367 unsigned FrameReg
, int &Offset
,
368 const ARMBaseInstrInfo
&TII
) const {
369 MachineInstr
&MI
= *II
;
370 MachineBasicBlock
&MBB
= *MI
.getParent();
371 DebugLoc dl
= MI
.getDebugLoc();
372 unsigned Opcode
= MI
.getOpcode();
373 const TargetInstrDesc
&Desc
= MI
.getDesc();
374 unsigned AddrMode
= (Desc
.TSFlags
& ARMII::AddrModeMask
);
376 if (Opcode
== ARM::tADDrSPi
) {
377 Offset
+= MI
.getOperand(FrameRegIdx
+1).getImm();
379 // Can't use tADDrSPi if it's based off the frame pointer.
380 unsigned NumBits
= 0;
382 if (FrameReg
!= ARM::SP
) {
383 Opcode
= ARM::tADDi3
;
384 MI
.setDesc(TII
.get(Opcode
));
389 assert((Offset
& 3) == 0 &&
390 "Thumb add/sub sp, #imm immediate must be multiple of 4!");
394 if (Offset
== 0 && getInstrPredicate(&MI
, PredReg
) == ARMCC::AL
) {
395 // Turn it into a move.
396 MI
.setDesc(TII
.get(ARM::tMOVgpr2tgpr
));
397 MI
.getOperand(FrameRegIdx
).ChangeToRegister(FrameReg
, false);
398 // Remove offset and remaining explicit predicate operands.
399 do MI
.RemoveOperand(FrameRegIdx
+1);
400 while (MI
.getNumOperands() > FrameRegIdx
+1 &&
401 (!MI
.getOperand(FrameRegIdx
+1).isReg() ||
402 !MI
.getOperand(FrameRegIdx
+1).isImm()));
406 // Common case: small offset, fits into instruction.
407 unsigned Mask
= (1 << NumBits
) - 1;
408 if (((Offset
/ Scale
) & ~Mask
) == 0) {
409 // Replace the FrameIndex with sp / fp
410 if (Opcode
== ARM::tADDi3
) {
411 removeOperands(MI
, FrameRegIdx
);
412 MachineInstrBuilder
MIB(&MI
);
413 AddDefaultPred(AddDefaultT1CC(MIB
).addReg(FrameReg
)
414 .addImm(Offset
/ Scale
));
416 MI
.getOperand(FrameRegIdx
).ChangeToRegister(FrameReg
, false);
417 MI
.getOperand(FrameRegIdx
+1).ChangeToImmediate(Offset
/ Scale
);
422 unsigned DestReg
= MI
.getOperand(0).getReg();
423 unsigned Bytes
= (Offset
> 0) ? Offset
: -Offset
;
424 unsigned NumMIs
= calcNumMI(Opcode
, 0, Bytes
, NumBits
, Scale
);
425 // MI would expand into a large number of instructions. Don't try to
426 // simplify the immediate.
428 emitThumbRegPlusImmediate(MBB
, II
, DestReg
, FrameReg
, Offset
, TII
,
435 // Translate r0 = add sp, imm to
436 // r0 = add sp, 255*4
437 // r0 = add r0, (imm - 255*4)
438 if (Opcode
== ARM::tADDi3
) {
439 removeOperands(MI
, FrameRegIdx
);
440 MachineInstrBuilder
MIB(&MI
);
441 AddDefaultPred(AddDefaultT1CC(MIB
).addReg(FrameReg
).addImm(Mask
));
443 MI
.getOperand(FrameRegIdx
).ChangeToRegister(FrameReg
, false);
444 MI
.getOperand(FrameRegIdx
+1).ChangeToImmediate(Mask
);
446 Offset
= (Offset
- Mask
* Scale
);
447 MachineBasicBlock::iterator NII
= llvm::next(II
);
448 emitThumbRegPlusImmediate(MBB
, NII
, DestReg
, DestReg
, Offset
, TII
,
451 // Translate r0 = add sp, -imm to
452 // r0 = -imm (this is then translated into a series of instructons)
454 emitThumbConstant(MBB
, II
, DestReg
, Offset
, TII
, *this, dl
);
456 MI
.setDesc(TII
.get(ARM::tADDhirr
));
457 MI
.getOperand(FrameRegIdx
).ChangeToRegister(DestReg
, false, false, true);
458 MI
.getOperand(FrameRegIdx
+1).ChangeToRegister(FrameReg
, false);
459 if (Opcode
== ARM::tADDi3
) {
460 MachineInstrBuilder
MIB(&MI
);
468 unsigned NumBits
= 0;
471 case ARMII::AddrModeT1_s
: {
472 ImmIdx
= FrameRegIdx
+1;
473 InstrOffs
= MI
.getOperand(ImmIdx
).getImm();
474 NumBits
= (FrameReg
== ARM::SP
) ? 8 : 5;
479 llvm_unreachable("Unsupported addressing mode!");
483 Offset
+= InstrOffs
* Scale
;
484 assert((Offset
& (Scale
-1)) == 0 && "Can't encode this offset!");
486 // Common case: small offset, fits into instruction.
487 MachineOperand
&ImmOp
= MI
.getOperand(ImmIdx
);
488 int ImmedOffset
= Offset
/ Scale
;
489 unsigned Mask
= (1 << NumBits
) - 1;
490 if ((unsigned)Offset
<= Mask
* Scale
) {
491 // Replace the FrameIndex with sp
492 MI
.getOperand(FrameRegIdx
).ChangeToRegister(FrameReg
, false);
493 ImmOp
.ChangeToImmediate(ImmedOffset
);
497 bool isThumSpillRestore
= Opcode
== ARM::tRestore
|| Opcode
== ARM::tSpill
;
498 if (AddrMode
== ARMII::AddrModeT1_s
) {
499 // Thumb tLDRspi, tSTRspi. These will change to instructions that use
500 // a different base register.
502 Mask
= (1 << NumBits
) - 1;
504 // If this is a thumb spill / restore, we will be using a constpool load to
505 // materialize the offset.
506 if (AddrMode
== ARMII::AddrModeT1_s
&& isThumSpillRestore
)
507 ImmOp
.ChangeToImmediate(0);
509 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
510 ImmedOffset
= ImmedOffset
& Mask
;
511 ImmOp
.ChangeToImmediate(ImmedOffset
);
512 Offset
&= ~(Mask
*Scale
);
519 Thumb1RegisterInfo::resolveFrameIndex(MachineBasicBlock::iterator I
,
520 unsigned BaseReg
, int64_t Offset
) const {
521 MachineInstr
&MI
= *I
;
522 int Off
= Offset
; // ARM doesn't need the general 64-bit offsets
525 while (!MI
.getOperand(i
).isFI()) {
527 assert(i
< MI
.getNumOperands() && "Instr doesn't have FrameIndex operand!");
530 Done
= rewriteFrameIndex(MI
, i
, BaseReg
, Off
, TII
);
531 assert (Done
&& "Unable to resolve frame index!");
534 /// saveScavengerRegister - Spill the register so it can be used by the
535 /// register scavenger. Return true.
537 Thumb1RegisterInfo::saveScavengerRegister(MachineBasicBlock
&MBB
,
538 MachineBasicBlock::iterator I
,
539 MachineBasicBlock::iterator
&UseMI
,
540 const TargetRegisterClass
*RC
,
541 unsigned Reg
) const {
542 // Thumb1 can't use the emergency spill slot on the stack because
543 // ldr/str immediate offsets must be positive, and if we're referencing
544 // off the frame pointer (if, for example, there are alloca() calls in
545 // the function, the offset will be negative. Use R12 instead since that's
546 // a call clobbered register that we know won't be used in Thumb1 mode.
548 BuildMI(MBB
, I
, DL
, TII
.get(ARM::tMOVtgpr2gpr
)).
549 addReg(ARM::R12
, RegState::Define
).addReg(Reg
, RegState::Kill
);
551 // The UseMI is where we would like to restore the register. If there's
552 // interference with R12 before then, however, we'll need to restore it
553 // before that instead and adjust the UseMI.
555 for (MachineBasicBlock::iterator II
= I
; !done
&& II
!= UseMI
; ++II
) {
556 if (II
->isDebugValue())
558 // If this instruction affects R12, adjust our restore point.
559 for (unsigned i
= 0, e
= II
->getNumOperands(); i
!= e
; ++i
) {
560 const MachineOperand
&MO
= II
->getOperand(i
);
561 if (!MO
.isReg() || MO
.isUndef() || !MO
.getReg() ||
562 TargetRegisterInfo::isVirtualRegister(MO
.getReg()))
564 if (MO
.getReg() == ARM::R12
) {
571 // Restore the register from R12
572 BuildMI(MBB
, UseMI
, DL
, TII
.get(ARM::tMOVgpr2tgpr
)).
573 addReg(Reg
, RegState::Define
).addReg(ARM::R12
, RegState::Kill
);
579 Thumb1RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II
,
580 int SPAdj
, RegScavenger
*RS
) const {
583 MachineInstr
&MI
= *II
;
584 MachineBasicBlock
&MBB
= *MI
.getParent();
585 MachineFunction
&MF
= *MBB
.getParent();
586 ARMFunctionInfo
*AFI
= MF
.getInfo
<ARMFunctionInfo
>();
587 DebugLoc dl
= MI
.getDebugLoc();
589 while (!MI
.getOperand(i
).isFI()) {
591 assert(i
< MI
.getNumOperands() && "Instr doesn't have FrameIndex operand!");
594 unsigned FrameReg
= ARM::SP
;
595 int FrameIndex
= MI
.getOperand(i
).getIndex();
596 int Offset
= MF
.getFrameInfo()->getObjectOffset(FrameIndex
) +
597 MF
.getFrameInfo()->getStackSize() + SPAdj
;
599 if (AFI
->isGPRCalleeSavedArea1Frame(FrameIndex
))
600 Offset
-= AFI
->getGPRCalleeSavedArea1Offset();
601 else if (AFI
->isGPRCalleeSavedArea2Frame(FrameIndex
))
602 Offset
-= AFI
->getGPRCalleeSavedArea2Offset();
603 else if (MF
.getFrameInfo()->hasVarSizedObjects()) {
604 assert(SPAdj
== 0 && hasFP(MF
) && "Unexpected");
605 // There are alloca()'s in this function, must reference off the frame
606 // pointer or base pointer instead.
607 if (!hasBasePointer(MF
)) {
608 FrameReg
= getFrameRegister(MF
);
609 Offset
-= AFI
->getFramePtrSpillOffset();
614 // Special handling of dbg_value instructions.
615 if (MI
.isDebugValue()) {
616 MI
.getOperand(i
). ChangeToRegister(FrameReg
, false /*isDef*/);
617 MI
.getOperand(i
+1).ChangeToImmediate(Offset
);
621 // Modify MI as necessary to handle as much of 'Offset' as possible
622 assert(AFI
->isThumbFunction() &&
623 "This eliminateFrameIndex only supports Thumb1!");
624 if (rewriteFrameIndex(MI
, i
, FrameReg
, Offset
, TII
))
627 // If we get here, the immediate doesn't fit into the instruction. We folded
628 // as much as possible above, handle the rest, providing a register that is
630 assert(Offset
&& "This code isn't needed if offset already handled!");
632 unsigned Opcode
= MI
.getOpcode();
633 const TargetInstrDesc
&Desc
= MI
.getDesc();
635 // Remove predicate first.
636 int PIdx
= MI
.findFirstPredOperandIdx();
638 removeOperands(MI
, PIdx
);
640 if (Desc
.mayLoad()) {
641 // Use the destination register to materialize sp + offset.
642 unsigned TmpReg
= MI
.getOperand(0).getReg();
644 if (Opcode
== ARM::tRestore
) {
645 if (FrameReg
== ARM::SP
)
646 emitThumbRegPlusImmInReg(MBB
, II
, TmpReg
, FrameReg
,
647 Offset
, false, TII
, *this, dl
);
649 emitLoadConstPool(MBB
, II
, dl
, TmpReg
, 0, Offset
);
653 emitThumbRegPlusImmediate(MBB
, II
, TmpReg
, FrameReg
, Offset
, TII
,
657 MI
.setDesc(TII
.get(ARM::tLDR
));
658 MI
.getOperand(i
).ChangeToRegister(TmpReg
, false, false, true);
660 // Use [reg, reg] addrmode.
661 MI
.addOperand(MachineOperand::CreateReg(FrameReg
, false));
662 else // tLDR has an extra register operand.
663 MI
.addOperand(MachineOperand::CreateReg(0, false));
664 } else if (Desc
.mayStore()) {
665 VReg
= MF
.getRegInfo().createVirtualRegister(ARM::tGPRRegisterClass
);
668 if (Opcode
== ARM::tSpill
) {
669 if (FrameReg
== ARM::SP
)
670 emitThumbRegPlusImmInReg(MBB
, II
, VReg
, FrameReg
,
671 Offset
, false, TII
, *this, dl
);
673 emitLoadConstPool(MBB
, II
, dl
, VReg
, 0, Offset
);
677 emitThumbRegPlusImmediate(MBB
, II
, VReg
, FrameReg
, Offset
, TII
,
679 MI
.setDesc(TII
.get(ARM::tSTR
));
680 MI
.getOperand(i
).ChangeToRegister(VReg
, false, false, true);
681 if (UseRR
) // Use [reg, reg] addrmode.
682 MI
.addOperand(MachineOperand::CreateReg(FrameReg
, false));
683 else // tSTR has an extra register operand.
684 MI
.addOperand(MachineOperand::CreateReg(0, false));
686 assert(false && "Unexpected opcode!");
688 // Add predicate back if it's needed.
689 if (MI
.getDesc().isPredicable()) {
690 MachineInstrBuilder
MIB(&MI
);
695 void Thumb1RegisterInfo::emitPrologue(MachineFunction
&MF
) const {
696 MachineBasicBlock
&MBB
= MF
.front();
697 MachineBasicBlock::iterator MBBI
= MBB
.begin();
698 MachineFrameInfo
*MFI
= MF
.getFrameInfo();
699 ARMFunctionInfo
*AFI
= MF
.getInfo
<ARMFunctionInfo
>();
700 unsigned VARegSaveSize
= AFI
->getVarArgsRegSaveSize();
701 unsigned NumBytes
= MFI
->getStackSize();
702 const std::vector
<CalleeSavedInfo
> &CSI
= MFI
->getCalleeSavedInfo();
703 DebugLoc dl
= MBBI
!= MBB
.end() ? MBBI
->getDebugLoc() : DebugLoc();
705 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
706 NumBytes
= (NumBytes
+ 3) & ~3;
707 MFI
->setStackSize(NumBytes
);
709 // Determine the sizes of each callee-save spill areas and record which frame
710 // belongs to which callee-save spill areas.
711 unsigned GPRCS1Size
= 0, GPRCS2Size
= 0, DPRCSSize
= 0;
712 int FramePtrSpillFI
= 0;
715 emitSPUpdate(MBB
, MBBI
, TII
, dl
, *this, -VARegSaveSize
);
717 if (!AFI
->hasStackFrame()) {
719 emitSPUpdate(MBB
, MBBI
, TII
, dl
, *this, -NumBytes
);
723 for (unsigned i
= 0, e
= CSI
.size(); i
!= e
; ++i
) {
724 unsigned Reg
= CSI
[i
].getReg();
725 int FI
= CSI
[i
].getFrameIdx();
733 FramePtrSpillFI
= FI
;
734 AFI
->addGPRCalleeSavedArea1Frame(FI
);
742 FramePtrSpillFI
= FI
;
743 if (STI
.isTargetDarwin()) {
744 AFI
->addGPRCalleeSavedArea2Frame(FI
);
747 AFI
->addGPRCalleeSavedArea1Frame(FI
);
752 AFI
->addDPRCalleeSavedAreaFrame(FI
);
757 if (MBBI
!= MBB
.end() && MBBI
->getOpcode() == ARM::tPUSH
) {
759 if (MBBI
!= MBB
.end())
760 dl
= MBBI
->getDebugLoc();
763 // Adjust FP so it point to the stack slot that contains the previous FP.
765 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tADDrSPi
), FramePtr
)
766 .addFrameIndex(FramePtrSpillFI
).addImm(0);
767 AFI
->setShouldRestoreSPFromFP(true);
770 // Determine starting offsets of spill areas.
771 unsigned DPRCSOffset
= NumBytes
- (GPRCS1Size
+ GPRCS2Size
+ DPRCSSize
);
772 unsigned GPRCS2Offset
= DPRCSOffset
+ DPRCSSize
;
773 unsigned GPRCS1Offset
= GPRCS2Offset
+ GPRCS2Size
;
774 AFI
->setFramePtrSpillOffset(MFI
->getObjectOffset(FramePtrSpillFI
) + NumBytes
);
775 AFI
->setGPRCalleeSavedArea1Offset(GPRCS1Offset
);
776 AFI
->setGPRCalleeSavedArea2Offset(GPRCS2Offset
);
777 AFI
->setDPRCalleeSavedAreaOffset(DPRCSOffset
);
779 NumBytes
= DPRCSOffset
;
781 // Insert it after all the callee-save spills.
782 emitSPUpdate(MBB
, MBBI
, TII
, dl
, *this, -NumBytes
);
785 if (STI
.isTargetELF() && hasFP(MF
))
786 MFI
->setOffsetAdjustment(MFI
->getOffsetAdjustment() -
787 AFI
->getFramePtrSpillOffset());
789 AFI
->setGPRCalleeSavedArea1Size(GPRCS1Size
);
790 AFI
->setGPRCalleeSavedArea2Size(GPRCS2Size
);
791 AFI
->setDPRCalleeSavedAreaSize(DPRCSSize
);
793 // If we need a base pointer, set it up here. It's whatever the value
794 // of the stack pointer is at this point. Any variable size objects
795 // will be allocated after this, so we can still use the base pointer
796 // to reference locals.
797 if (hasBasePointer(MF
))
798 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVgpr2gpr
), BasePtr
).addReg(ARM::SP
);
801 static bool isCalleeSavedRegister(unsigned Reg
, const unsigned *CSRegs
) {
802 for (unsigned i
= 0; CSRegs
[i
]; ++i
)
803 if (Reg
== CSRegs
[i
])
808 static bool isCSRestore(MachineInstr
*MI
, const unsigned *CSRegs
) {
809 if (MI
->getOpcode() == ARM::tRestore
&&
810 MI
->getOperand(1).isFI() &&
811 isCalleeSavedRegister(MI
->getOperand(0).getReg(), CSRegs
))
813 else if (MI
->getOpcode() == ARM::tPOP
) {
814 // The first two operands are predicates. The last two are
815 // imp-def and imp-use of SP. Check everything in between.
816 for (int i
= 2, e
= MI
->getNumOperands() - 2; i
!= e
; ++i
)
817 if (!isCalleeSavedRegister(MI
->getOperand(i
).getReg(), CSRegs
))
824 void Thumb1RegisterInfo::emitEpilogue(MachineFunction
&MF
,
825 MachineBasicBlock
&MBB
) const {
826 MachineBasicBlock::iterator MBBI
= prior(MBB
.end());
827 assert((MBBI
->getOpcode() == ARM::tBX_RET
||
828 MBBI
->getOpcode() == ARM::tPOP_RET
) &&
829 "Can only insert epilog into returning blocks");
830 DebugLoc dl
= MBBI
->getDebugLoc();
831 MachineFrameInfo
*MFI
= MF
.getFrameInfo();
832 ARMFunctionInfo
*AFI
= MF
.getInfo
<ARMFunctionInfo
>();
833 unsigned VARegSaveSize
= AFI
->getVarArgsRegSaveSize();
834 int NumBytes
= (int)MFI
->getStackSize();
835 const unsigned *CSRegs
= getCalleeSavedRegs();
837 if (!AFI
->hasStackFrame()) {
839 emitSPUpdate(MBB
, MBBI
, TII
, dl
, *this, NumBytes
);
841 // Unwind MBBI to point to first LDR / VLDRD.
842 if (MBBI
!= MBB
.begin()) {
845 while (MBBI
!= MBB
.begin() && isCSRestore(MBBI
, CSRegs
));
846 if (!isCSRestore(MBBI
, CSRegs
))
850 // Move SP to start of FP callee save spill area.
851 NumBytes
-= (AFI
->getGPRCalleeSavedArea1Size() +
852 AFI
->getGPRCalleeSavedArea2Size() +
853 AFI
->getDPRCalleeSavedAreaSize());
855 if (AFI
->shouldRestoreSPFromFP()) {
856 NumBytes
= AFI
->getFramePtrSpillOffset() - NumBytes
;
857 // Reset SP based on frame pointer only if the stack frame extends beyond
858 // frame pointer stack slot or target is ELF and the function has FP.
860 emitThumbRegPlusImmediate(MBB
, MBBI
, ARM::SP
, FramePtr
, -NumBytes
,
863 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVtgpr2gpr
), ARM::SP
)
866 if (MBBI
->getOpcode() == ARM::tBX_RET
&&
867 &MBB
.front() != MBBI
&&
868 prior(MBBI
)->getOpcode() == ARM::tPOP
) {
869 MachineBasicBlock::iterator PMBBI
= prior(MBBI
);
870 emitSPUpdate(MBB
, PMBBI
, TII
, dl
, *this, NumBytes
);
872 emitSPUpdate(MBB
, MBBI
, TII
, dl
, *this, NumBytes
);
877 // Unlike T2 and ARM mode, the T1 pop instruction cannot restore
878 // to LR, and we can't pop the value directly to the PC since
879 // we need to update the SP after popping the value. Therefore, we
880 // pop the old LR into R3 as a temporary.
882 // Move back past the callee-saved register restoration
883 while (MBBI
!= MBB
.end() && isCSRestore(MBBI
, CSRegs
))
885 // Epilogue for vararg functions: pop LR to R3 and branch off it.
886 AddDefaultPred(BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tPOP
)))
887 .addReg(ARM::R3
, RegState::Define
);
889 emitSPUpdate(MBB
, MBBI
, TII
, dl
, *this, VARegSaveSize
);
891 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tBX_RET_vararg
))
892 .addReg(ARM::R3
, RegState::Kill
);
893 // erase the old tBX_RET instruction