1 //===- Thumb2InstrInfo.cpp - Thumb-2 Instruction 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-2 implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "ARMInstrInfo.h"
16 #include "ARMAddressingModes.h"
17 #include "ARMGenInstrInfo.inc"
18 #include "ARMMachineFunctionInfo.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "Thumb2InstrInfo.h"
26 Thumb2InstrInfo::Thumb2InstrInfo(const ARMSubtarget &STI) : RI(*this, STI) {
29 unsigned Thumb2InstrInfo::getUnindexedOpcode(unsigned Opc) const {
35 Thumb2InstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
36 if (MBB.empty()) return false;
38 switch (MBB.back().getOpcode()) {
40 case ARM::t2B: // Uncond branch.
41 case ARM::t2BR_JT: // Jumptable branch.
42 case ARM::t2TBB: // Table branch byte.
43 case ARM::t2TBH: // Table branch halfword.
44 case ARM::tBR_JTr: // Jumptable branch (16-bit version).
46 case ARM::tBX_RET_vararg:
58 Thumb2InstrInfo::copyRegToReg(MachineBasicBlock &MBB,
59 MachineBasicBlock::iterator I,
60 unsigned DestReg, unsigned SrcReg,
61 const TargetRegisterClass *DestRC,
62 const TargetRegisterClass *SrcRC) const {
63 DebugLoc DL = DebugLoc::getUnknownLoc();
64 if (I != MBB.end()) DL = I->getDebugLoc();
66 if (DestRC == ARM::GPRRegisterClass &&
67 SrcRC == ARM::GPRRegisterClass) {
68 // FIXME: Just use tMOVgpr2gpr since it's shorter?
69 if (SrcReg == ARM::SP || DestReg == ARM::SP)
70 BuildMI(MBB, I, DL, get(ARM::tMOVgpr2gpr), DestReg).addReg(SrcReg);
72 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::t2MOVr),
73 DestReg).addReg(SrcReg)));
75 } else if (DestRC == ARM::GPRRegisterClass &&
76 SrcRC == ARM::tGPRRegisterClass) {
77 BuildMI(MBB, I, DL, get(ARM::tMOVtgpr2gpr), DestReg).addReg(SrcReg);
79 } else if (DestRC == ARM::tGPRRegisterClass &&
80 SrcRC == ARM::GPRRegisterClass) {
81 BuildMI(MBB, I, DL, get(ARM::tMOVgpr2tgpr), DestReg).addReg(SrcReg);
85 // Handle SPR, DPR, and QPR copies.
86 return ARMBaseInstrInfo::copyRegToReg(MBB, I, DestReg, SrcReg, DestRC, SrcRC);
89 void Thumb2InstrInfo::
90 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
91 unsigned SrcReg, bool isKill, int FI,
92 const TargetRegisterClass *RC) const {
93 DebugLoc DL = DebugLoc::getUnknownLoc();
94 if (I != MBB.end()) DL = I->getDebugLoc();
96 if (RC == ARM::GPRRegisterClass) {
97 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::t2STRi12))
98 .addReg(SrcReg, getKillRegState(isKill))
99 .addFrameIndex(FI).addImm(0));
103 ARMBaseInstrInfo::storeRegToStackSlot(MBB, I, SrcReg, isKill, FI, RC);
106 void Thumb2InstrInfo::
107 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
108 unsigned DestReg, int FI,
109 const TargetRegisterClass *RC) const {
110 DebugLoc DL = DebugLoc::getUnknownLoc();
111 if (I != MBB.end()) DL = I->getDebugLoc();
113 if (RC == ARM::GPRRegisterClass) {
114 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::t2LDRi12), DestReg)
115 .addFrameIndex(FI).addImm(0));
119 ARMBaseInstrInfo::loadRegFromStackSlot(MBB, I, DestReg, FI, RC);
123 void llvm::emitT2RegPlusImmediate(MachineBasicBlock &MBB,
124 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
125 unsigned DestReg, unsigned BaseReg, int NumBytes,
126 ARMCC::CondCodes Pred, unsigned PredReg,
127 const ARMBaseInstrInfo &TII) {
128 bool isSub = NumBytes < 0;
129 if (isSub) NumBytes = -NumBytes;
131 // If profitable, use a movw or movt to materialize the offset.
132 // FIXME: Use the scavenger to grab a scratch register.
133 if (DestReg != ARM::SP && DestReg != BaseReg &&
135 ARM_AM::getT2SOImmVal(NumBytes) == -1) {
137 if (NumBytes < 65536) {
138 // Use a movw to materialize the 16-bit constant.
139 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi16), DestReg)
141 .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
143 } else if ((NumBytes & 0xffff) == 0) {
144 // Use a movt to materialize the 32-bit constant.
145 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVTi16), DestReg)
147 .addImm(NumBytes >> 16)
148 .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
154 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2SUBrr), DestReg)
155 .addReg(BaseReg, RegState::Kill)
156 .addReg(DestReg, RegState::Kill)
157 .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
159 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2ADDrr), DestReg)
160 .addReg(DestReg, RegState::Kill)
161 .addReg(BaseReg, RegState::Kill)
162 .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
169 unsigned ThisVal = NumBytes;
171 if (DestReg == ARM::SP && BaseReg != ARM::SP) {
172 // mov sp, rn. Note t2MOVr cannot be used.
173 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr),DestReg).addReg(BaseReg);
178 if (BaseReg == ARM::SP) {
180 if (DestReg == ARM::SP && (ThisVal < ((1 << 7)-1) * 4)) {
181 assert((ThisVal & 3) == 0 && "Stack update is not multiple of 4?");
182 Opc = isSub ? ARM::tSUBspi : ARM::tADDspi;
183 // FIXME: Fix Thumb1 immediate encoding.
184 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
185 .addReg(BaseReg).addImm(ThisVal/4);
190 // sub rd, sp, so_imm
191 Opc = isSub ? ARM::t2SUBrSPi : ARM::t2ADDrSPi;
192 if (ARM_AM::getT2SOImmVal(NumBytes) != -1) {
195 // FIXME: Move this to ARMAddressingModes.h?
196 unsigned RotAmt = CountLeadingZeros_32(ThisVal);
197 ThisVal = ThisVal & ARM_AM::rotr32(0xff000000U, RotAmt);
198 NumBytes &= ~ThisVal;
199 assert(ARM_AM::getT2SOImmVal(ThisVal) != -1 &&
200 "Bit extraction didn't work?");
203 assert(DestReg != ARM::SP && BaseReg != ARM::SP);
204 Opc = isSub ? ARM::t2SUBri : ARM::t2ADDri;
205 if (ARM_AM::getT2SOImmVal(NumBytes) != -1) {
207 } else if (ThisVal < 4096) {
208 Opc = isSub ? ARM::t2SUBri12 : ARM::t2ADDri12;
211 // FIXME: Move this to ARMAddressingModes.h?
212 unsigned RotAmt = CountLeadingZeros_32(ThisVal);
213 ThisVal = ThisVal & ARM_AM::rotr32(0xff000000U, RotAmt);
214 NumBytes &= ~ThisVal;
215 assert(ARM_AM::getT2SOImmVal(ThisVal) != -1 &&
216 "Bit extraction didn't work?");
220 // Build the new ADD / SUB.
221 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
222 .addReg(BaseReg, RegState::Kill)
230 negativeOffsetOpcode(unsigned opcode)
233 case ARM::t2LDRi12: return ARM::t2LDRi8;
234 case ARM::t2LDRHi12: return ARM::t2LDRHi8;
235 case ARM::t2LDRBi12: return ARM::t2LDRBi8;
236 case ARM::t2LDRSHi12: return ARM::t2LDRSHi8;
237 case ARM::t2LDRSBi12: return ARM::t2LDRSBi8;
238 case ARM::t2STRi12: return ARM::t2STRi8;
239 case ARM::t2STRBi12: return ARM::t2STRBi8;
240 case ARM::t2STRHi12: return ARM::t2STRHi8;
260 positiveOffsetOpcode(unsigned opcode)
263 case ARM::t2LDRi8: return ARM::t2LDRi12;
264 case ARM::t2LDRHi8: return ARM::t2LDRHi12;
265 case ARM::t2LDRBi8: return ARM::t2LDRBi12;
266 case ARM::t2LDRSHi8: return ARM::t2LDRSHi12;
267 case ARM::t2LDRSBi8: return ARM::t2LDRSBi12;
268 case ARM::t2STRi8: return ARM::t2STRi12;
269 case ARM::t2STRBi8: return ARM::t2STRBi12;
270 case ARM::t2STRHi8: return ARM::t2STRHi12;
275 case ARM::t2LDRSHi12:
276 case ARM::t2LDRSBi12:
290 immediateOffsetOpcode(unsigned opcode)
293 case ARM::t2LDRs: return ARM::t2LDRi12;
294 case ARM::t2LDRHs: return ARM::t2LDRHi12;
295 case ARM::t2LDRBs: return ARM::t2LDRBi12;
296 case ARM::t2LDRSHs: return ARM::t2LDRSHi12;
297 case ARM::t2LDRSBs: return ARM::t2LDRSBi12;
298 case ARM::t2STRs: return ARM::t2STRi12;
299 case ARM::t2STRBs: return ARM::t2STRBi12;
300 case ARM::t2STRHs: return ARM::t2STRHi12;
305 case ARM::t2LDRSHi12:
306 case ARM::t2LDRSBi12:
327 int llvm::rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
328 unsigned FrameReg, int Offset,
329 const ARMBaseInstrInfo &TII) {
330 unsigned Opcode = MI.getOpcode();
331 const TargetInstrDesc &Desc = MI.getDesc();
332 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
335 // Memory operands in inline assembly always use AddrModeT2_i12.
336 if (Opcode == ARM::INLINEASM)
337 AddrMode = ARMII::AddrModeT2_i12; // FIXME. mode for thumb2?
339 if (Opcode == ARM::t2ADDri || Opcode == ARM::t2ADDri12) {
340 Offset += MI.getOperand(FrameRegIdx+1).getImm();
342 bool isSP = FrameReg == ARM::SP;
344 // Turn it into a move.
345 unsigned NewOpc = isSP ? ARM::tMOVgpr2gpr : ARM::t2MOVr;
346 MI.setDesc(TII.get(NewOpc));
347 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
348 MI.RemoveOperand(FrameRegIdx+1);
355 MI.setDesc(TII.get(isSP ? ARM::t2SUBrSPi : ARM::t2SUBri));
357 MI.setDesc(TII.get(isSP ? ARM::t2ADDrSPi : ARM::t2ADDri));
360 // Common case: small offset, fits into instruction.
361 if (ARM_AM::getT2SOImmVal(Offset) != -1) {
362 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
363 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
366 // Another common case: imm12.
368 unsigned NewOpc = isSP
369 ? (isSub ? ARM::t2SUBrSPi12 : ARM::t2ADDrSPi12)
370 : (isSub ? ARM::t2SUBri12 : ARM::t2ADDri12);
371 MI.setDesc(TII.get(NewOpc));
372 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
373 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
377 // Otherwise, extract 8 adjacent bits from the immediate into this
379 unsigned RotAmt = CountLeadingZeros_32(Offset);
380 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xff000000U, RotAmt);
382 // We will handle these bits from offset, clear them.
383 Offset &= ~ThisImmVal;
385 assert(ARM_AM::getT2SOImmVal(ThisImmVal) != -1 &&
386 "Bit extraction didn't work?");
387 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
389 // AddrModeT2_so cannot handle any offset. If there is no offset
390 // register then we change to an immediate version.
391 unsigned NewOpc = Opcode;
392 if (AddrMode == ARMII::AddrModeT2_so) {
393 unsigned OffsetReg = MI.getOperand(FrameRegIdx+1).getReg();
394 if (OffsetReg != 0) {
395 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
399 MI.RemoveOperand(FrameRegIdx+1);
400 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(0);
401 NewOpc = immediateOffsetOpcode(Opcode);
402 AddrMode = ARMII::AddrModeT2_i12;
405 unsigned NumBits = 0;
407 if (AddrMode == ARMII::AddrModeT2_i8 || AddrMode == ARMII::AddrModeT2_i12) {
408 // i8 supports only negative, and i12 supports only positive, so
409 // based on Offset sign convert Opcode to the appropriate
411 Offset += MI.getOperand(FrameRegIdx+1).getImm();
413 NewOpc = negativeOffsetOpcode(Opcode);
418 NewOpc = positiveOffsetOpcode(Opcode);
422 // VFP address modes.
423 assert(AddrMode == ARMII::AddrMode5);
424 int InstrOffs=ARM_AM::getAM5Offset(MI.getOperand(FrameRegIdx+1).getImm());
425 if (ARM_AM::getAM5Op(MI.getOperand(FrameRegIdx+1).getImm()) ==ARM_AM::sub)
429 Offset += InstrOffs * 4;
430 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
437 if (NewOpc != Opcode)
438 MI.setDesc(TII.get(NewOpc));
440 MachineOperand &ImmOp = MI.getOperand(FrameRegIdx+1);
442 // Attempt to fold address computation
443 // Common case: small offset, fits into instruction.
444 int ImmedOffset = Offset / Scale;
445 unsigned Mask = (1 << NumBits) - 1;
446 if ((unsigned)Offset <= Mask * Scale) {
447 // Replace the FrameIndex with fp/sp
448 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
450 if (AddrMode == ARMII::AddrMode5)
451 // FIXME: Not consistent.
452 ImmedOffset |= 1 << NumBits;
454 ImmedOffset = -ImmedOffset;
456 ImmOp.ChangeToImmediate(ImmedOffset);
460 // Otherwise, offset doesn't fit. Pull in what we can to simplify
461 ImmedOffset = ImmedOffset & Mask;
463 if (AddrMode == ARMII::AddrMode5)
464 // FIXME: Not consistent.
465 ImmedOffset |= 1 << NumBits;
467 ImmedOffset = -ImmedOffset;
468 if (ImmedOffset == 0)
469 // Change the opcode back if the encoded offset is zero.
470 MI.setDesc(TII.get(positiveOffsetOpcode(NewOpc)));
473 ImmOp.ChangeToImmediate(ImmedOffset);
474 Offset &= ~(Mask*Scale);
477 return (isSub) ? -Offset : Offset;