1 //===-- Thumb1RegisterInfo.cpp - Thumb-1 Register Information -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the Thumb-1 implementation of the TargetRegisterInfo
13 //===----------------------------------------------------------------------===//
15 #include "Thumb1RegisterInfo.h"
16 #include "ARMBaseInstrInfo.h"
17 #include "ARMMachineFunctionInfo.h"
18 #include "ARMSubtarget.h"
19 #include "MCTargetDesc/ARMAddressingModes.h"
20 #include "llvm/CodeGen/MachineConstantPool.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/RegisterScavenging.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/DerivedTypes.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/LLVMContext.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Target/TargetFrameLowering.h"
33 #include "llvm/Target/TargetMachine.h"
36 extern cl::opt<bool> ReuseFrameIndexVals;
41 Thumb1RegisterInfo::Thumb1RegisterInfo(const ARMSubtarget &sti)
42 : ARMBaseRegisterInfo(sti) {
45 const TargetRegisterClass*
46 Thumb1RegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC)
48 if (ARM::tGPRRegClass.hasSubClassEq(RC))
49 return &ARM::tGPRRegClass;
50 return ARMBaseRegisterInfo::getLargestLegalSuperClass(RC);
53 const TargetRegisterClass *
54 Thumb1RegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
56 return &ARM::tGPRRegClass;
59 /// emitLoadConstPool - Emits a load from constpool to materialize the
60 /// specified immediate.
62 Thumb1RegisterInfo::emitLoadConstPool(MachineBasicBlock &MBB,
63 MachineBasicBlock::iterator &MBBI,
65 unsigned DestReg, unsigned SubIdx,
67 ARMCC::CondCodes Pred, unsigned PredReg,
68 unsigned MIFlags) const {
69 assert((isARMLowRegister(DestReg) ||
70 isVirtualRegister(DestReg)) &&
71 "Thumb1 does not have ldr to high register");
73 MachineFunction &MF = *MBB.getParent();
74 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
75 MachineConstantPool *ConstantPool = MF.getConstantPool();
76 const Constant *C = ConstantInt::get(
77 Type::getInt32Ty(MBB.getParent()->getFunction()->getContext()), Val);
78 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
80 BuildMI(MBB, MBBI, dl, TII.get(ARM::tLDRpci))
81 .addReg(DestReg, getDefRegState(true), SubIdx)
82 .addConstantPoolIndex(Idx).addImm(Pred).addReg(PredReg)
87 /// emitThumbRegPlusImmInReg - Emits a series of instructions to materialize
88 /// a destreg = basereg + immediate in Thumb code. Materialize the immediate
89 /// in a register using mov / mvn sequences or load the immediate from a
92 void emitThumbRegPlusImmInReg(MachineBasicBlock &MBB,
93 MachineBasicBlock::iterator &MBBI,
95 unsigned DestReg, unsigned BaseReg,
96 int NumBytes, bool CanChangeCC,
97 const TargetInstrInfo &TII,
98 const ARMBaseRegisterInfo& MRI,
99 unsigned MIFlags = MachineInstr::NoFlags) {
100 MachineFunction &MF = *MBB.getParent();
101 bool isHigh = !isARMLowRegister(DestReg) ||
102 (BaseReg != 0 && !isARMLowRegister(BaseReg));
104 // Subtract doesn't have high register version. Load the negative value
105 // if either base or dest register is a high register. Also, if do not
106 // issue sub as part of the sequence if condition register is to be
108 if (NumBytes < 0 && !isHigh && CanChangeCC) {
110 NumBytes = -NumBytes;
112 unsigned LdReg = DestReg;
113 if (DestReg == ARM::SP)
114 assert(BaseReg == ARM::SP && "Unexpected!");
115 if (!isARMLowRegister(DestReg) && !MRI.isVirtualRegister(DestReg))
116 LdReg = MF.getRegInfo().createVirtualRegister(&ARM::tGPRRegClass);
118 if (NumBytes <= 255 && NumBytes >= 0 && CanChangeCC) {
119 AddDefaultT1CC(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVi8), LdReg))
120 .addImm(NumBytes).setMIFlags(MIFlags);
121 } else if (NumBytes < 0 && NumBytes >= -255 && CanChangeCC) {
122 AddDefaultT1CC(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVi8), LdReg))
123 .addImm(NumBytes).setMIFlags(MIFlags);
124 AddDefaultT1CC(BuildMI(MBB, MBBI, dl, TII.get(ARM::tRSB), LdReg))
125 .addReg(LdReg, RegState::Kill).setMIFlags(MIFlags);
127 MRI.emitLoadConstPool(MBB, MBBI, dl, LdReg, 0, NumBytes,
128 ARMCC::AL, 0, MIFlags);
131 int Opc = (isSub) ? ARM::tSUBrr : ((isHigh || !CanChangeCC) ? ARM::tADDhirr
133 MachineInstrBuilder MIB =
134 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg);
135 if (Opc != ARM::tADDhirr)
136 MIB = AddDefaultT1CC(MIB);
137 if (DestReg == ARM::SP || isSub)
138 MIB.addReg(BaseReg).addReg(LdReg, RegState::Kill);
140 MIB.addReg(LdReg).addReg(BaseReg, RegState::Kill);
144 /// emitThumbRegPlusImmediate - Emits a series of instructions to materialize
145 /// a destreg = basereg + immediate in Thumb code. Tries a series of ADDs or
146 /// SUBs first, and uses a constant pool value if the instruction sequence would
147 /// be too long. This is allowed to modify the condition flags.
148 void llvm::emitThumbRegPlusImmediate(MachineBasicBlock &MBB,
149 MachineBasicBlock::iterator &MBBI,
151 unsigned DestReg, unsigned BaseReg,
152 int NumBytes, const TargetInstrInfo &TII,
153 const ARMBaseRegisterInfo& MRI,
155 bool isSub = NumBytes < 0;
156 unsigned Bytes = (unsigned)NumBytes;
157 if (isSub) Bytes = -NumBytes;
160 unsigned CopyBits = 0;
161 unsigned CopyScale = 1;
162 bool CopyNeedsCC = false;
164 unsigned ExtraBits = 0;
165 unsigned ExtraScale = 1;
166 bool ExtraNeedsCC = false;
169 // We need to select two types of instruction, maximizing the available
170 // immediate range of each. The instructions we use will depend on whether
171 // DestReg and BaseReg are low, high or the stack pointer.
172 // * CopyOpc - DestReg = BaseReg + imm
173 // This will be emitted once if DestReg != BaseReg, and never if
174 // DestReg == BaseReg.
175 // * ExtraOpc - DestReg = DestReg + imm
176 // This will be emitted as many times as necessary to add the
178 // If the immediate ranges of these instructions are not large enough to cover
179 // NumBytes with a reasonable number of instructions, we fall back to using a
180 // value loaded from a constant pool.
181 if (DestReg == ARM::SP) {
182 if (BaseReg == ARM::SP) {
184 // Already in right reg, no copy needed
186 // low -> sp or high -> sp
187 CopyOpc = ARM::tMOVr;
190 ExtraOpc = isSub ? ARM::tSUBspi : ARM::tADDspi;
193 } else if (isARMLowRegister(DestReg)) {
194 if (BaseReg == ARM::SP) {
196 assert(!isSub && "Thumb1 does not have tSUBrSPi");
197 CopyOpc = ARM::tADDrSPi;
200 } else if (DestReg == BaseReg) {
202 // Already in right reg, no copy needed
203 } else if (isARMLowRegister(BaseReg)) {
204 // low -> different low
205 CopyOpc = isSub ? ARM::tSUBi3 : ARM::tADDi3;
210 CopyOpc = ARM::tMOVr;
213 ExtraOpc = isSub ? ARM::tSUBi8 : ARM::tADDi8;
216 } else /* DestReg is high */ {
217 if (DestReg == BaseReg) {
219 // Already in right reg, no copy needed
221 // {low,high,sp} -> high
222 CopyOpc = ARM::tMOVr;
228 // We could handle an unaligned immediate with an unaligned copy instruction
229 // and an aligned extra instruction, but this case is not currently needed.
230 assert(((Bytes & 3) == 0 || ExtraScale == 1) &&
231 "Unaligned offset, but all instructions require alignment");
233 unsigned CopyRange = ((1 << CopyBits) - 1) * CopyScale;
234 // If we would emit the copy with an immediate of 0, just use tMOVr.
235 if (CopyOpc && Bytes < CopyScale) {
236 CopyOpc = ARM::tMOVr;
242 unsigned ExtraRange = ((1 << ExtraBits) - 1) * ExtraScale; // per instruction
243 unsigned RequiredCopyInstrs = CopyOpc ? 1 : 0;
244 unsigned RangeAfterCopy = (CopyRange > Bytes) ? 0 : (Bytes - CopyRange);
246 // We could handle this case when the copy instruction does not require an
247 // aligned immediate, but we do not currently do this.
248 assert(RangeAfterCopy % ExtraScale == 0 &&
249 "Extra instruction requires immediate to be aligned");
251 unsigned RequiredExtraInstrs;
253 RequiredExtraInstrs = RoundUpToAlignment(RangeAfterCopy, ExtraRange) / ExtraRange;
254 else if (RangeAfterCopy > 0)
255 // We need an extra instruction but none is available
256 RequiredExtraInstrs = 1000000;
258 RequiredExtraInstrs = 0;
259 unsigned RequiredInstrs = RequiredCopyInstrs + RequiredExtraInstrs;
260 unsigned Threshold = (DestReg == ARM::SP) ? 3 : 2;
262 // Use a constant pool, if the sequence of ADDs/SUBs is too expensive.
263 if (RequiredInstrs > Threshold) {
264 emitThumbRegPlusImmInReg(MBB, MBBI, dl,
265 DestReg, BaseReg, NumBytes, true,
270 // Emit zero or one copy instructions
272 unsigned CopyImm = std::min(Bytes, CopyRange) / CopyScale;
273 Bytes -= CopyImm * CopyScale;
275 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(CopyOpc), DestReg);
277 MIB = AddDefaultT1CC(MIB);
278 MIB.addReg(BaseReg, RegState::Kill);
279 if (CopyOpc != ARM::tMOVr) {
282 AddDefaultPred(MIB.setMIFlags(MIFlags));
287 // Emit zero or more in-place add/sub instructions
289 unsigned ExtraImm = std::min(Bytes, ExtraRange) / ExtraScale;
290 Bytes -= ExtraImm * ExtraScale;
292 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(ExtraOpc), DestReg);
294 MIB = AddDefaultT1CC(MIB);
295 MIB.addReg(BaseReg).addImm(ExtraImm);
296 MIB = AddDefaultPred(MIB);
297 MIB.setMIFlags(MIFlags);
301 static void removeOperands(MachineInstr &MI, unsigned i) {
303 for (unsigned e = MI.getNumOperands(); i != e; ++i)
304 MI.RemoveOperand(Op);
307 /// convertToNonSPOpcode - Change the opcode to the non-SP version, because
308 /// we're replacing the frame index with a non-SP register.
309 static unsigned convertToNonSPOpcode(unsigned Opcode) {
321 bool Thumb1RegisterInfo::
322 rewriteFrameIndex(MachineBasicBlock::iterator II, unsigned FrameRegIdx,
323 unsigned FrameReg, int &Offset,
324 const ARMBaseInstrInfo &TII) const {
325 MachineInstr &MI = *II;
326 MachineBasicBlock &MBB = *MI.getParent();
327 DebugLoc dl = MI.getDebugLoc();
328 MachineInstrBuilder MIB(*MBB.getParent(), &MI);
329 unsigned Opcode = MI.getOpcode();
330 const MCInstrDesc &Desc = MI.getDesc();
331 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
333 if (Opcode == ARM::tADDframe) {
334 Offset += MI.getOperand(FrameRegIdx+1).getImm();
335 unsigned DestReg = MI.getOperand(0).getReg();
337 emitThumbRegPlusImmediate(MBB, II, dl, DestReg, FrameReg, Offset, TII,
342 if (AddrMode != ARMII::AddrModeT1_s)
343 llvm_unreachable("Unsupported addressing mode!");
345 unsigned ImmIdx = FrameRegIdx + 1;
346 int InstrOffs = MI.getOperand(ImmIdx).getImm();
347 unsigned NumBits = (FrameReg == ARM::SP) ? 8 : 5;
350 Offset += InstrOffs * Scale;
351 assert((Offset & (Scale - 1)) == 0 && "Can't encode this offset!");
353 // Common case: small offset, fits into instruction.
354 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
355 int ImmedOffset = Offset / Scale;
356 unsigned Mask = (1 << NumBits) - 1;
358 if ((unsigned)Offset <= Mask * Scale) {
359 // Replace the FrameIndex with the frame register (e.g., sp).
360 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
361 ImmOp.ChangeToImmediate(ImmedOffset);
363 // If we're using a register where sp was stored, convert the instruction
364 // to the non-SP version.
365 unsigned NewOpc = convertToNonSPOpcode(Opcode);
366 if (NewOpc != Opcode && FrameReg != ARM::SP)
367 MI.setDesc(TII.get(NewOpc));
373 Mask = (1 << NumBits) - 1;
375 // If this is a thumb spill / restore, we will be using a constpool load to
376 // materialize the offset.
377 if (Opcode == ARM::tLDRspi || Opcode == ARM::tSTRspi) {
378 ImmOp.ChangeToImmediate(0);
380 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
381 ImmedOffset = ImmedOffset & Mask;
382 ImmOp.ChangeToImmediate(ImmedOffset);
383 Offset &= ~(Mask * Scale);
390 void Thumb1RegisterInfo::resolveFrameIndex(MachineInstr &MI, unsigned BaseReg,
391 int64_t Offset) const {
392 const ARMBaseInstrInfo &TII =
393 *static_cast<const ARMBaseInstrInfo *>(MI.getParent()
398 int Off = Offset; // ARM doesn't need the general 64-bit offsets
401 while (!MI.getOperand(i).isFI()) {
403 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
405 bool Done = rewriteFrameIndex(MI, i, BaseReg, Off, TII);
406 assert (Done && "Unable to resolve frame index!");
410 /// saveScavengerRegister - Spill the register so it can be used by the
411 /// register scavenger. Return true.
413 Thumb1RegisterInfo::saveScavengerRegister(MachineBasicBlock &MBB,
414 MachineBasicBlock::iterator I,
415 MachineBasicBlock::iterator &UseMI,
416 const TargetRegisterClass *RC,
417 unsigned Reg) const {
418 // Thumb1 can't use the emergency spill slot on the stack because
419 // ldr/str immediate offsets must be positive, and if we're referencing
420 // off the frame pointer (if, for example, there are alloca() calls in
421 // the function, the offset will be negative. Use R12 instead since that's
422 // a call clobbered register that we know won't be used in Thumb1 mode.
423 const TargetInstrInfo &TII = *MBB.getParent()->getSubtarget().getInstrInfo();
425 AddDefaultPred(BuildMI(MBB, I, DL, TII.get(ARM::tMOVr))
426 .addReg(ARM::R12, RegState::Define)
427 .addReg(Reg, RegState::Kill));
429 // The UseMI is where we would like to restore the register. If there's
430 // interference with R12 before then, however, we'll need to restore it
431 // before that instead and adjust the UseMI.
433 for (MachineBasicBlock::iterator II = I; !done && II != UseMI ; ++II) {
434 if (II->isDebugValue())
436 // If this instruction affects R12, adjust our restore point.
437 for (unsigned i = 0, e = II->getNumOperands(); i != e; ++i) {
438 const MachineOperand &MO = II->getOperand(i);
439 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::R12)) {
444 if (!MO.isReg() || MO.isUndef() || !MO.getReg() ||
445 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
447 if (MO.getReg() == ARM::R12) {
454 // Restore the register from R12
455 AddDefaultPred(BuildMI(MBB, UseMI, DL, TII.get(ARM::tMOVr)).
456 addReg(Reg, RegState::Define).addReg(ARM::R12, RegState::Kill));
462 Thumb1RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
463 int SPAdj, unsigned FIOperandNum,
464 RegScavenger *RS) const {
466 MachineInstr &MI = *II;
467 MachineBasicBlock &MBB = *MI.getParent();
468 MachineFunction &MF = *MBB.getParent();
469 const ARMBaseInstrInfo &TII =
470 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
471 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
472 DebugLoc dl = MI.getDebugLoc();
473 MachineInstrBuilder MIB(*MBB.getParent(), &MI);
475 unsigned FrameReg = ARM::SP;
476 int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
477 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
478 MF.getFrameInfo()->getStackSize() + SPAdj;
480 if (MF.getFrameInfo()->hasVarSizedObjects()) {
481 assert(SPAdj == 0 && MF.getSubtarget().getFrameLowering()->hasFP(MF) &&
483 // There are alloca()'s in this function, must reference off the frame
484 // pointer or base pointer instead.
485 if (!hasBasePointer(MF)) {
486 FrameReg = getFrameRegister(MF);
487 Offset -= AFI->getFramePtrSpillOffset();
492 // PEI::scavengeFrameVirtualRegs() cannot accurately track SPAdj because the
493 // call frame setup/destroy instructions have already been eliminated. That
494 // means the stack pointer cannot be used to access the emergency spill slot
495 // when !hasReservedCallFrame().
497 if (RS && FrameReg == ARM::SP && RS->isScavengingFrameIndex(FrameIndex)){
498 assert(MF.getTarget()
501 ->hasReservedCallFrame(MF) &&
502 "Cannot use SP to access the emergency spill slot in "
503 "functions without a reserved call frame");
504 assert(!MF.getFrameInfo()->hasVarSizedObjects() &&
505 "Cannot use SP to access the emergency spill slot in "
506 "functions with variable sized frame objects");
510 // Special handling of dbg_value instructions.
511 if (MI.isDebugValue()) {
512 MI.getOperand(FIOperandNum). ChangeToRegister(FrameReg, false /*isDef*/);
513 MI.getOperand(FIOperandNum+1).ChangeToImmediate(Offset);
517 // Modify MI as necessary to handle as much of 'Offset' as possible
518 assert(AFI->isThumbFunction() &&
519 "This eliminateFrameIndex only supports Thumb1!");
520 if (rewriteFrameIndex(MI, FIOperandNum, FrameReg, Offset, TII))
523 // If we get here, the immediate doesn't fit into the instruction. We folded
524 // as much as possible above, handle the rest, providing a register that is
526 assert(Offset && "This code isn't needed if offset already handled!");
528 unsigned Opcode = MI.getOpcode();
530 // Remove predicate first.
531 int PIdx = MI.findFirstPredOperandIdx();
533 removeOperands(MI, PIdx);
536 // Use the destination register to materialize sp + offset.
537 unsigned TmpReg = MI.getOperand(0).getReg();
539 if (Opcode == ARM::tLDRspi) {
540 if (FrameReg == ARM::SP)
541 emitThumbRegPlusImmInReg(MBB, II, dl, TmpReg, FrameReg,
542 Offset, false, TII, *this);
544 emitLoadConstPool(MBB, II, dl, TmpReg, 0, Offset);
548 emitThumbRegPlusImmediate(MBB, II, dl, TmpReg, FrameReg, Offset, TII,
552 MI.setDesc(TII.get(UseRR ? ARM::tLDRr : ARM::tLDRi));
553 MI.getOperand(FIOperandNum).ChangeToRegister(TmpReg, false, false, true);
555 // Use [reg, reg] addrmode. Replace the immediate operand w/ the frame
556 // register. The offset is already handled in the vreg value.
557 MI.getOperand(FIOperandNum+1).ChangeToRegister(FrameReg, false, false,
559 } else if (MI.mayStore()) {
560 VReg = MF.getRegInfo().createVirtualRegister(&ARM::tGPRRegClass);
563 if (Opcode == ARM::tSTRspi) {
564 if (FrameReg == ARM::SP)
565 emitThumbRegPlusImmInReg(MBB, II, dl, VReg, FrameReg,
566 Offset, false, TII, *this);
568 emitLoadConstPool(MBB, II, dl, VReg, 0, Offset);
572 emitThumbRegPlusImmediate(MBB, II, dl, VReg, FrameReg, Offset, TII,
574 MI.setDesc(TII.get(UseRR ? ARM::tSTRr : ARM::tSTRi));
575 MI.getOperand(FIOperandNum).ChangeToRegister(VReg, false, false, true);
577 // Use [reg, reg] addrmode. Replace the immediate operand w/ the frame
578 // register. The offset is already handled in the vreg value.
579 MI.getOperand(FIOperandNum+1).ChangeToRegister(FrameReg, false, false,
582 llvm_unreachable("Unexpected opcode!");
585 // Add predicate back if it's needed.
586 if (MI.isPredicable())