blob: 35ef31749f40232f536cb88b66b10fec095c9b17 [file] [log] [blame]
Akira Hatanakab7fa3c92012-07-31 21:49:49 +00001//===-- Mips16InstrInfo.cpp - Mips16 Instruction Information --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Mips16 implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000013#include "Mips16InstrInfo.h"
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000014#include "InstPrinter/MipsInstPrinter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "MipsMachineFunction.h"
16#include "MipsTargetMachine.h"
17#include "llvm/ADT/STLExtras.h"
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineRegisterInfo.h"
Reed Kotler66165c82013-02-08 03:57:41 +000020#include "llvm/CodeGen/RegisterScavenging.h"
Reed Kotler5c8ae092013-11-13 04:37:52 +000021#include "llvm/MC/MCAsmInfo.h"
Reed Kotlercb374092013-02-18 00:59:04 +000022#include "llvm/Support/Debug.h"
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000023#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/TargetRegistry.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000025#include "llvm/Support/raw_ostream.h"
NAKAMURA Takumi435f62a2013-11-13 06:27:53 +000026#include <cctype>
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000027
28using namespace llvm;
29
Chandler Carruthe96dd892014-04-21 22:55:11 +000030#define DEBUG_TYPE "mips16-instrinfo"
Reed Kotlerd019dbf2012-12-20 04:07:42 +000031
Eric Christopher675cb4d2014-07-18 23:25:00 +000032Mips16InstrInfo::Mips16InstrInfo(const MipsSubtarget &STI)
Eric Christophera20c3cf2015-03-12 05:43:57 +000033 : MipsInstrInfo(STI, Mips::Bimm16), RI() {}
Akira Hatanakacb37e132012-07-31 23:41:32 +000034
35const MipsRegisterInfo &Mips16InstrInfo::getRegisterInfo() const {
36 return RI;
37}
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000038
39/// isLoadFromStackSlot - If the specified machine instruction is a direct
40/// load from a stack slot, return the virtual or physical register number of
41/// the destination along with the FrameIndex of the loaded stack slot. If
42/// not, return 0. This predicate must return 0 if the instruction has
43/// any side effects other than loading from the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +000044unsigned Mips16InstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Eric Christopher66b70692014-07-18 22:34:14 +000045 int &FrameIndex) const {
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000046 return 0;
47}
48
49/// isStoreToStackSlot - If the specified machine instruction is a direct
50/// store to a stack slot, return the virtual or physical register number of
51/// the source reg along with the FrameIndex of the loaded stack slot. If
52/// not, return 0. This predicate must return 0 if the instruction has
53/// any side effects other than storing to the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +000054unsigned Mips16InstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Eric Christopher66b70692014-07-18 22:34:14 +000055 int &FrameIndex) const {
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000056 return 0;
57}
58
59void Mips16InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +000060 MachineBasicBlock::iterator I,
61 const DebugLoc &DL, unsigned DestReg,
62 unsigned SrcReg, bool KillSrc) const {
Reed Kotlercf11c592012-10-12 02:01:09 +000063 unsigned Opc = 0;
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000064
Reed Kotlercf11c592012-10-12 02:01:09 +000065 if (Mips::CPU16RegsRegClass.contains(DestReg) &&
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000066 Mips::GPR32RegClass.contains(SrcReg))
Reed Kotlercf11c592012-10-12 02:01:09 +000067 Opc = Mips::MoveR3216;
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000068 else if (Mips::GPR32RegClass.contains(DestReg) &&
Reed Kotlercf11c592012-10-12 02:01:09 +000069 Mips::CPU16RegsRegClass.contains(SrcReg))
70 Opc = Mips::Move32R16;
Akira Hatanaka8002a3f2013-08-14 00:47:08 +000071 else if ((SrcReg == Mips::HI0) &&
Reed Kotlercf11c592012-10-12 02:01:09 +000072 (Mips::CPU16RegsRegClass.contains(DestReg)))
73 Opc = Mips::Mfhi16, SrcReg = 0;
74
Akira Hatanaka8002a3f2013-08-14 00:47:08 +000075 else if ((SrcReg == Mips::LO0) &&
Reed Kotlercf11c592012-10-12 02:01:09 +000076 (Mips::CPU16RegsRegClass.contains(DestReg)))
77 Opc = Mips::Mflo16, SrcReg = 0;
78
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000079
80 assert(Opc && "Cannot copy registers");
81
82 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc));
83
84 if (DestReg)
85 MIB.addReg(DestReg, RegState::Define);
86
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000087 if (SrcReg)
88 MIB.addReg(SrcReg, getKillRegState(KillSrc));
89}
90
Eric Christopher66b70692014-07-18 22:34:14 +000091void Mips16InstrInfo::storeRegToStack(MachineBasicBlock &MBB,
92 MachineBasicBlock::iterator I,
93 unsigned SrcReg, bool isKill, int FI,
94 const TargetRegisterClass *RC,
95 const TargetRegisterInfo *TRI,
96 int64_t Offset) const {
Reed Kotler210ebe92012-09-28 02:26:24 +000097 DebugLoc DL;
98 if (I != MBB.end()) DL = I->getDebugLoc();
99 MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOStore);
100 unsigned Opc = 0;
101 if (Mips::CPU16RegsRegClass.hasSubClassEq(RC))
102 Opc = Mips::SwRxSpImmX16;
103 assert(Opc && "Register class not handled!");
Reed Kotler30cedf62013-08-04 01:13:25 +0000104 BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill)).
105 addFrameIndex(FI).addImm(Offset)
106 .addMemOperand(MMO);
Akira Hatanakab7fa3c92012-07-31 21:49:49 +0000107}
108
Eric Christopher66b70692014-07-18 22:34:14 +0000109void Mips16InstrInfo::loadRegFromStack(MachineBasicBlock &MBB,
110 MachineBasicBlock::iterator I,
111 unsigned DestReg, int FI,
112 const TargetRegisterClass *RC,
113 const TargetRegisterInfo *TRI,
114 int64_t Offset) const {
Reed Kotler210ebe92012-09-28 02:26:24 +0000115 DebugLoc DL;
116 if (I != MBB.end()) DL = I->getDebugLoc();
117 MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOLoad);
118 unsigned Opc = 0;
119
120 if (Mips::CPU16RegsRegClass.hasSubClassEq(RC))
121 Opc = Mips::LwRxSpImmX16;
122 assert(Opc && "Register class not handled!");
Akira Hatanaka465facca2013-03-29 02:14:12 +0000123 BuildMI(MBB, I, DL, get(Opc), DestReg).addFrameIndex(FI).addImm(Offset)
Reed Kotler210ebe92012-09-28 02:26:24 +0000124 .addMemOperand(MMO);
Akira Hatanakab7fa3c92012-07-31 21:49:49 +0000125}
126
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000127bool Mips16InstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
128 MachineBasicBlock &MBB = *MI.getParent();
129 switch (MI.getDesc().getOpcode()) {
Akira Hatanakab7fa3c92012-07-31 21:49:49 +0000130 default:
131 return false;
132 case Mips::RetRA16:
Reed Kotlera8117532012-10-30 00:54:49 +0000133 ExpandRetRA16(MBB, MI, Mips::JrcRa16);
Akira Hatanakab7fa3c92012-07-31 21:49:49 +0000134 break;
135 }
136
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000137 MBB.erase(MI.getIterator());
Akira Hatanakab7fa3c92012-07-31 21:49:49 +0000138 return true;
139}
140
141/// GetOppositeBranchOpc - Return the inverse of the specified
142/// opcode, e.g. turning BEQ to BNE.
Akira Hatanaka067d8152013-05-13 17:43:19 +0000143unsigned Mips16InstrInfo::getOppositeBranchOpc(unsigned Opc) const {
Reed Kotler67439242012-10-17 22:29:54 +0000144 switch (Opc) {
Reed Kotler67439242012-10-17 22:29:54 +0000145 case Mips::BeqzRxImmX16: return Mips::BnezRxImmX16;
146 case Mips::BnezRxImmX16: return Mips::BeqzRxImmX16;
Reed Kotler09e59152013-11-15 02:21:52 +0000147 case Mips::BeqzRxImm16: return Mips::BnezRxImm16;
148 case Mips::BnezRxImm16: return Mips::BeqzRxImm16;
Reed Kotler67439242012-10-17 22:29:54 +0000149 case Mips::BteqzT8CmpX16: return Mips::BtnezT8CmpX16;
150 case Mips::BteqzT8SltX16: return Mips::BtnezT8SltX16;
151 case Mips::BteqzT8SltiX16: return Mips::BtnezT8SltiX16;
Reed Kotler09e59152013-11-15 02:21:52 +0000152 case Mips::Btnez16: return Mips::Bteqz16;
Reed Kotler67439242012-10-17 22:29:54 +0000153 case Mips::BtnezX16: return Mips::BteqzX16;
154 case Mips::BtnezT8CmpiX16: return Mips::BteqzT8CmpiX16;
155 case Mips::BtnezT8SltuX16: return Mips::BteqzT8SltuX16;
156 case Mips::BtnezT8SltiuX16: return Mips::BteqzT8SltiuX16;
Reed Kotler09e59152013-11-15 02:21:52 +0000157 case Mips::Bteqz16: return Mips::Btnez16;
Reed Kotler67439242012-10-17 22:29:54 +0000158 case Mips::BteqzX16: return Mips::BtnezX16;
159 case Mips::BteqzT8CmpiX16: return Mips::BtnezT8CmpiX16;
160 case Mips::BteqzT8SltuX16: return Mips::BtnezT8SltuX16;
161 case Mips::BteqzT8SltiuX16: return Mips::BtnezT8SltiuX16;
162 case Mips::BtnezT8CmpX16: return Mips::BteqzT8CmpX16;
163 case Mips::BtnezT8SltX16: return Mips::BteqzT8SltX16;
164 case Mips::BtnezT8SltiX16: return Mips::BteqzT8SltiX16;
165 }
Craig Topperd3c02f12015-01-05 10:15:49 +0000166 llvm_unreachable("Illegal opcode!");
Akira Hatanakab7fa3c92012-07-31 21:49:49 +0000167}
168
Reed Kotler5c29d632013-12-15 20:49:30 +0000169static void addSaveRestoreRegs(MachineInstrBuilder &MIB,
Eric Christopher66b70692014-07-18 22:34:14 +0000170 const std::vector<CalleeSavedInfo> &CSI,
171 unsigned Flags = 0) {
Reed Kotler5c29d632013-12-15 20:49:30 +0000172 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
173 // Add the callee-saved register as live-in. Do not add if the register is
174 // RA and return address is taken, because it has already been added in
Daniel Sanders94ed30a2016-07-26 14:46:11 +0000175 // method MipsTargetLowering::lowerRETURNADDR.
Reed Kotler5c29d632013-12-15 20:49:30 +0000176 // It's killed at the spill, unless the register is RA and return address
177 // is taken.
178 unsigned Reg = CSI[e-i-1].getReg();
179 switch (Reg) {
180 case Mips::RA:
181 case Mips::S0:
182 case Mips::S1:
183 MIB.addReg(Reg, Flags);
184 break;
185 case Mips::S2:
186 break;
187 default:
188 llvm_unreachable("unexpected mips16 callee saved register");
189
190 }
191 }
Reed Kotler5c29d632013-12-15 20:49:30 +0000192}
Reed Kotlerd019dbf2012-12-20 04:07:42 +0000193// Adjust SP by FrameSize bytes. Save RA, S0, S1
Tim Northover775aaeb2015-11-05 21:54:58 +0000194void Mips16InstrInfo::makeFrame(unsigned SP, int64_t FrameSize,
195 MachineBasicBlock &MBB,
196 MachineBasicBlock::iterator I) const {
197 DebugLoc DL;
198 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000199 MachineFrameInfo &MFI = MF.getFrameInfo();
Tim Northover775aaeb2015-11-05 21:54:58 +0000200 const BitVector Reserved = RI.getReservedRegs(MF);
Reed Kotler0ff40012013-12-10 14:29:38 +0000201 bool SaveS2 = Reserved[Mips::S2];
202 MachineInstrBuilder MIB;
Reed Kotler5bde5c32013-12-11 03:32:44 +0000203 unsigned Opc = ((FrameSize <= 128) && !SaveS2)? Mips::Save16:Mips::SaveX16;
Reed Kotler5c29d632013-12-15 20:49:30 +0000204 MIB = BuildMI(MBB, I, DL, get(Opc));
Matthias Braun941a7052016-07-28 18:40:00 +0000205 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Reed Kotler5c29d632013-12-15 20:49:30 +0000206 addSaveRestoreRegs(MIB, CSI);
207 if (SaveS2)
208 MIB.addReg(Mips::S2);
Reed Kotler2e362b32013-12-09 21:19:51 +0000209 if (isUInt<11>(FrameSize))
Reed Kotler5c29d632013-12-15 20:49:30 +0000210 MIB.addImm(FrameSize);
Reed Kotlerd019dbf2012-12-20 04:07:42 +0000211 else {
Reed Kotler2e362b32013-12-09 21:19:51 +0000212 int Base = 2040; // should create template function like isUInt that
213 // returns largest possible n bit unsigned integer
214 int64_t Remainder = FrameSize - Base;
Reed Kotler5c29d632013-12-15 20:49:30 +0000215 MIB.addImm(Base);
Reed Kotler2e362b32013-12-09 21:19:51 +0000216 if (isInt<16>(-Remainder))
217 BuildAddiuSpImm(MBB, I, -Remainder);
218 else
219 adjustStackPtrBig(SP, -Remainder, MBB, I, Mips::V0, Mips::V1);
Reed Kotlerd019dbf2012-12-20 04:07:42 +0000220 }
221}
222
223// Adjust SP by FrameSize bytes. Restore RA, S0, S1
Jack Carter7ab15fa2013-01-19 02:00:40 +0000224void Mips16InstrInfo::restoreFrame(unsigned SP, int64_t FrameSize,
225 MachineBasicBlock &MBB,
226 MachineBasicBlock::iterator I) const {
Reed Kotlerd019dbf2012-12-20 04:07:42 +0000227 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
Reed Kotler5c29d632013-12-15 20:49:30 +0000228 MachineFunction *MF = MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000229 MachineFrameInfo &MFI = MF->getFrameInfo();
Reed Kotler5c29d632013-12-15 20:49:30 +0000230 const BitVector Reserved = RI.getReservedRegs(*MF);
Reed Kotler0ff40012013-12-10 14:29:38 +0000231 bool SaveS2 = Reserved[Mips::S2];
232 MachineInstrBuilder MIB;
Reed Kotler5bde5c32013-12-11 03:32:44 +0000233 unsigned Opc = ((FrameSize <= 128) && !SaveS2)?
234 Mips::Restore16:Mips::RestoreX16;
Reed Kotler5c29d632013-12-15 20:49:30 +0000235
236 if (!isUInt<11>(FrameSize)) {
237 unsigned Base = 2040;
Reed Kotler2e362b32013-12-09 21:19:51 +0000238 int64_t Remainder = FrameSize - Base;
Reed Kotler5c29d632013-12-15 20:49:30 +0000239 FrameSize = Base; // should create template function like isUInt that
240 // returns largest possible n bit unsigned integer
241
Reed Kotler2e362b32013-12-09 21:19:51 +0000242 if (isInt<16>(Remainder))
243 BuildAddiuSpImm(MBB, I, Remainder);
244 else
245 adjustStackPtrBig(SP, Remainder, MBB, I, Mips::A0, Mips::A1);
Reed Kotlerd019dbf2012-12-20 04:07:42 +0000246 }
Reed Kotler5c29d632013-12-15 20:49:30 +0000247 MIB = BuildMI(MBB, I, DL, get(Opc));
Matthias Braun941a7052016-07-28 18:40:00 +0000248 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Reed Kotler5c29d632013-12-15 20:49:30 +0000249 addSaveRestoreRegs(MIB, CSI, RegState::Define);
Reed Kotler0ff40012013-12-10 14:29:38 +0000250 if (SaveS2)
251 MIB.addReg(Mips::S2, RegState::Define);
Reed Kotler5c29d632013-12-15 20:49:30 +0000252 MIB.addImm(FrameSize);
Reed Kotlerd019dbf2012-12-20 04:07:42 +0000253}
254
255// Adjust SP by Amount bytes where bytes can be up to 32bit number.
Jack Carter7ab15fa2013-01-19 02:00:40 +0000256// This can only be called at times that we know that there is at least one free
257// register.
Reed Kotlerd019dbf2012-12-20 04:07:42 +0000258// This is clearly safe at prologue and epilogue.
259//
Jack Carter7ab15fa2013-01-19 02:00:40 +0000260void Mips16InstrInfo::adjustStackPtrBig(unsigned SP, int64_t Amount,
Tim Northover775aaeb2015-11-05 21:54:58 +0000261 MachineBasicBlock &MBB,
262 MachineBasicBlock::iterator I,
263 unsigned Reg1, unsigned Reg2) const {
264 DebugLoc DL;
265 //
266 // li reg1, constant
267 // move reg2, sp
Reed Kotlerd019dbf2012-12-20 04:07:42 +0000268 // add reg1, reg1, reg2
269 // move sp, reg1
270 //
271 //
272 MachineInstrBuilder MIB1 = BuildMI(MBB, I, DL, get(Mips::LwConstant32), Reg1);
Reed Kotlera787aa22013-11-24 06:18:50 +0000273 MIB1.addImm(Amount).addImm(-1);
Reed Kotlerd019dbf2012-12-20 04:07:42 +0000274 MachineInstrBuilder MIB2 = BuildMI(MBB, I, DL, get(Mips::MoveR3216), Reg2);
275 MIB2.addReg(Mips::SP, RegState::Kill);
276 MachineInstrBuilder MIB3 = BuildMI(MBB, I, DL, get(Mips::AdduRxRyRz16), Reg1);
277 MIB3.addReg(Reg1);
278 MIB3.addReg(Reg2, RegState::Kill);
Jack Carter7ab15fa2013-01-19 02:00:40 +0000279 MachineInstrBuilder MIB4 = BuildMI(MBB, I, DL, get(Mips::Move32R16),
280 Mips::SP);
Reed Kotlerd019dbf2012-12-20 04:07:42 +0000281 MIB4.addReg(Reg1, RegState::Kill);
282}
283
Eric Christopher66b70692014-07-18 22:34:14 +0000284void Mips16InstrInfo::adjustStackPtrBigUnrestricted(
285 unsigned SP, int64_t Amount, MachineBasicBlock &MBB,
286 MachineBasicBlock::iterator I) const {
Craig Topperd3c02f12015-01-05 10:15:49 +0000287 llvm_unreachable("adjust stack pointer amount exceeded");
Reed Kotlerd019dbf2012-12-20 04:07:42 +0000288}
289
Reed Kotler27a72292012-10-31 05:21:10 +0000290/// Adjust SP by Amount bytes.
291void Mips16InstrInfo::adjustStackPtr(unsigned SP, int64_t Amount,
292 MachineBasicBlock &MBB,
293 MachineBasicBlock::iterator I) const {
Vasileios Kalintirisb3698a52015-04-02 10:14:54 +0000294 if (Amount == 0)
295 return;
296
Reed Kotlerd019dbf2012-12-20 04:07:42 +0000297 if (isInt<16>(Amount)) // need to change to addiu sp, ....and isInt<16>
Reed Kotler188dad02013-02-16 19:04:29 +0000298 BuildAddiuSpImm(MBB, I, Amount);
Reed Kotler27a72292012-10-31 05:21:10 +0000299 else
Reed Kotlerd019dbf2012-12-20 04:07:42 +0000300 adjustStackPtrBigUnrestricted(SP, Amount, MBB, I);
301}
302
303/// This function generates the sequence of instructions needed to get the
304/// result of adding register REG and immediate IMM.
Eric Christopher66b70692014-07-18 22:34:14 +0000305unsigned Mips16InstrInfo::loadImmediate(unsigned FrameReg, int64_t Imm,
306 MachineBasicBlock &MBB,
307 MachineBasicBlock::iterator II,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000308 const DebugLoc &DL,
309 unsigned &NewImm) const {
Reed Kotler66165c82013-02-08 03:57:41 +0000310 //
311 // given original instruction is:
312 // Instr rx, T[offset] where offset is too big.
313 //
314 // lo = offset & 0xFFFF
315 // hi = ((offset >> 16) + (lo >> 15)) & 0xFFFF;
316 //
317 // let T = temporary register
318 // li T, hi
319 // shl T, 16
320 // add T, Rx, T
321 //
322 RegScavenger rs;
323 int32_t lo = Imm & 0xFFFF;
Reed Kotler66165c82013-02-08 03:57:41 +0000324 NewImm = lo;
Reed Kotler30cedf62013-08-04 01:13:25 +0000325 int Reg =0;
326 int SpReg = 0;
327
Matthias Braun7dc03f02016-04-06 02:47:09 +0000328 rs.enterBasicBlock(MBB);
Reed Kotler66165c82013-02-08 03:57:41 +0000329 rs.forward(II);
330 //
Reed Kotler30cedf62013-08-04 01:13:25 +0000331 // We need to know which registers can be used, in the case where there
332 // are not enough free registers. We exclude all registers that
333 // are used in the instruction that we are helping.
334 // // Consider all allocatable registers in the register class initially
335 BitVector Candidates =
336 RI.getAllocatableSet
337 (*II->getParent()->getParent(), &Mips::CPU16RegsRegClass);
338 // Exclude all the registers being used by the instruction.
339 for (unsigned i = 0, e = II->getNumOperands(); i != e; ++i) {
340 MachineOperand &MO = II->getOperand(i);
341 if (MO.isReg() && MO.getReg() != 0 && !MO.isDef() &&
342 !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
343 Candidates.reset(MO.getReg());
344 }
Eric Christopher66b70692014-07-18 22:34:14 +0000345
Reed Kotler30cedf62013-08-04 01:13:25 +0000346 // If the same register was used and defined in an instruction, then
347 // it will not be in the list of candidates.
348 //
349 // we need to analyze the instruction that we are helping.
350 // we need to know if it defines register x but register x is not
351 // present as an operand of the instruction. this tells
352 // whether the register is live before the instruction. if it's not
353 // then we don't need to save it in case there are no free registers.
Reed Kotler30cedf62013-08-04 01:13:25 +0000354 int DefReg = 0;
355 for (unsigned i = 0, e = II->getNumOperands(); i != e; ++i) {
356 MachineOperand &MO = II->getOperand(i);
357 if (MO.isReg() && MO.isDef()) {
358 DefReg = MO.getReg();
359 break;
360 }
361 }
Reed Kotler30cedf62013-08-04 01:13:25 +0000362
Eric Christopher66b70692014-07-18 22:34:14 +0000363 BitVector Available = rs.getRegsAvailable(&Mips::CPU16RegsRegClass);
Reed Kotler30cedf62013-08-04 01:13:25 +0000364 Available &= Candidates;
365 //
Reed Kotler66165c82013-02-08 03:57:41 +0000366 // we use T0 for the first register, if we need to save something away.
367 // we use T1 for the second register, if we need to save something away.
368 //
369 unsigned FirstRegSaved =0, SecondRegSaved=0;
370 unsigned FirstRegSavedTo = 0, SecondRegSavedTo = 0;
Reed Kotlerd019dbf2012-12-20 04:07:42 +0000371
Reed Kotler30cedf62013-08-04 01:13:25 +0000372 Reg = Available.find_first();
373
374 if (Reg == -1) {
375 Reg = Candidates.find_first();
376 Candidates.reset(Reg);
377 if (DefReg != Reg) {
378 FirstRegSaved = Reg;
379 FirstRegSavedTo = Mips::T0;
380 copyPhysReg(MBB, II, DL, FirstRegSavedTo, FirstRegSaved, true);
381 }
Reed Kotler66165c82013-02-08 03:57:41 +0000382 }
383 else
Reed Kotler30cedf62013-08-04 01:13:25 +0000384 Available.reset(Reg);
Reed Kotlera787aa22013-11-24 06:18:50 +0000385 BuildMI(MBB, II, DL, get(Mips::LwConstant32), Reg).addImm(Imm).addImm(-1);
Reed Kotler30cedf62013-08-04 01:13:25 +0000386 NewImm = 0;
Reed Kotler66165c82013-02-08 03:57:41 +0000387 if (FrameReg == Mips::SP) {
Reed Kotler30cedf62013-08-04 01:13:25 +0000388 SpReg = Available.find_first();
389 if (SpReg == -1) {
390 SpReg = Candidates.find_first();
391 // Candidates.reset(SpReg); // not really needed
392 if (DefReg!= SpReg) {
393 SecondRegSaved = SpReg;
Reed Kotler66165c82013-02-08 03:57:41 +0000394 SecondRegSavedTo = Mips::T1;
395 }
Reed Kotler30cedf62013-08-04 01:13:25 +0000396 if (SecondRegSaved)
397 copyPhysReg(MBB, II, DL, SecondRegSavedTo, SecondRegSaved, true);
Reed Kotler66165c82013-02-08 03:57:41 +0000398 }
Reed Kotler30cedf62013-08-04 01:13:25 +0000399 else
400 Available.reset(SpReg);
Reed Kotler66165c82013-02-08 03:57:41 +0000401 copyPhysReg(MBB, II, DL, SpReg, Mips::SP, false);
Reed Kotler30cedf62013-08-04 01:13:25 +0000402 BuildMI(MBB, II, DL, get(Mips:: AdduRxRyRz16), Reg).addReg(SpReg, RegState::Kill)
Reed Kotler66165c82013-02-08 03:57:41 +0000403 .addReg(Reg);
404 }
405 else
406 BuildMI(MBB, II, DL, get(Mips:: AdduRxRyRz16), Reg).addReg(FrameReg)
407 .addReg(Reg, RegState::Kill);
408 if (FirstRegSaved || SecondRegSaved) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000409 II = std::next(II);
Reed Kotler66165c82013-02-08 03:57:41 +0000410 if (FirstRegSaved)
411 copyPhysReg(MBB, II, DL, FirstRegSaved, FirstRegSavedTo, true);
412 if (SecondRegSaved)
413 copyPhysReg(MBB, II, DL, SecondRegSaved, SecondRegSavedTo, true);
414 }
415 return Reg;
Reed Kotler27a72292012-10-31 05:21:10 +0000416}
417
Akira Hatanaka067d8152013-05-13 17:43:19 +0000418unsigned Mips16InstrInfo::getAnalyzableBrOpc(unsigned Opc) const {
Reed Kotler67439242012-10-17 22:29:54 +0000419 return (Opc == Mips::BeqzRxImmX16 || Opc == Mips::BimmX16 ||
Reed Kotlerf0e69682013-11-12 02:27:12 +0000420 Opc == Mips::Bimm16 ||
Reed Kotler09e59152013-11-15 02:21:52 +0000421 Opc == Mips::Bteqz16 || Opc == Mips::Btnez16 ||
422 Opc == Mips::BeqzRxImm16 || Opc == Mips::BnezRxImm16 ||
Reed Kotler67439242012-10-17 22:29:54 +0000423 Opc == Mips::BnezRxImmX16 || Opc == Mips::BteqzX16 ||
424 Opc == Mips::BteqzT8CmpX16 || Opc == Mips::BteqzT8CmpiX16 ||
425 Opc == Mips::BteqzT8SltX16 || Opc == Mips::BteqzT8SltuX16 ||
426 Opc == Mips::BteqzT8SltiX16 || Opc == Mips::BteqzT8SltiuX16 ||
427 Opc == Mips::BtnezX16 || Opc == Mips::BtnezT8CmpX16 ||
428 Opc == Mips::BtnezT8CmpiX16 || Opc == Mips::BtnezT8SltX16 ||
429 Opc == Mips::BtnezT8SltuX16 || Opc == Mips::BtnezT8SltiX16 ||
430 Opc == Mips::BtnezT8SltiuX16 ) ? Opc : 0;
Akira Hatanakab7fa3c92012-07-31 21:49:49 +0000431}
432
433void Mips16InstrInfo::ExpandRetRA16(MachineBasicBlock &MBB,
434 MachineBasicBlock::iterator I,
435 unsigned Opc) const {
436 BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
437}
Akira Hatanakafab89292012-08-02 18:21:47 +0000438
Reed Kotler8cf51032013-02-16 09:47:57 +0000439const MCInstrDesc &Mips16InstrInfo::AddiuSpImm(int64_t Imm) const {
Reed Kotlerf662cff2013-02-13 20:28:27 +0000440 if (validSpImm8(Imm))
Reed Kotler8cf51032013-02-16 09:47:57 +0000441 return get(Mips::AddiuSpImm16);
Reed Kotlerf662cff2013-02-13 20:28:27 +0000442 else
Reed Kotler8cf51032013-02-16 09:47:57 +0000443 return get(Mips::AddiuSpImmX16);
Reed Kotlerf662cff2013-02-13 20:28:27 +0000444}
Tim Northover775aaeb2015-11-05 21:54:58 +0000445
446void Mips16InstrInfo::BuildAddiuSpImm
447 (MachineBasicBlock &MBB, MachineBasicBlock::iterator I, int64_t Imm) const {
448 DebugLoc DL;
449 BuildMI(MBB, I, DL, AddiuSpImm(Imm)).addImm(Imm);
450}
451
Eric Christopher675cb4d2014-07-18 23:25:00 +0000452const MipsInstrInfo *llvm::createMips16InstrInfo(const MipsSubtarget &STI) {
453 return new Mips16InstrInfo(STI);
Akira Hatanakafab89292012-08-02 18:21:47 +0000454}
Reed Kotler30cedf62013-08-04 01:13:25 +0000455
Reed Kotler30cedf62013-08-04 01:13:25 +0000456bool Mips16InstrInfo::validImmediate(unsigned Opcode, unsigned Reg,
457 int64_t Amount) {
458 switch (Opcode) {
459 case Mips::LbRxRyOffMemX16:
460 case Mips::LbuRxRyOffMemX16:
461 case Mips::LhRxRyOffMemX16:
462 case Mips::LhuRxRyOffMemX16:
463 case Mips::SbRxRyOffMemX16:
464 case Mips::ShRxRyOffMemX16:
465 case Mips::LwRxRyOffMemX16:
466 case Mips::SwRxRyOffMemX16:
467 case Mips::SwRxSpImmX16:
468 case Mips::LwRxSpImmX16:
469 return isInt<16>(Amount);
470 case Mips::AddiuRxRyOffMemX16:
471 if ((Reg == Mips::PC) || (Reg == Mips::SP))
472 return isInt<16>(Amount);
473 return isInt<15>(Amount);
474 }
Reed Kotler30cedf62013-08-04 01:13:25 +0000475 llvm_unreachable("unexpected Opcode in validImmediate");
476}
Reed Kotler5c8ae092013-11-13 04:37:52 +0000477
478/// Measure the specified inline asm to determine an approximation of its
479/// length.
480/// Comments (which run till the next SeparatorString or newline) do not
481/// count as an instruction.
482/// Any other non-whitespace text is considered an instruction, with
483/// multiple instructions separated by SeparatorString or newlines.
484/// Variable-length instructions are not handled here; this function
485/// may be overloaded in the target code to do that.
486/// We implement the special case of the .space directive taking only an
487/// integer argument, which is the size in bytes. This is used for creating
488/// inline code spacing for testing purposes using inline assembly.
489///
490unsigned Mips16InstrInfo::getInlineAsmLength(const char *Str,
491 const MCAsmInfo &MAI) const {
492
Reed Kotler5c8ae092013-11-13 04:37:52 +0000493 // Count the number of instructions in the asm.
494 bool atInsnStart = true;
495 unsigned Length = 0;
496 for (; *Str; ++Str) {
497 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
498 strlen(MAI.getSeparatorString())) == 0)
499 atInsnStart = true;
500 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
501 if (strncmp(Str, ".space", 6)==0) {
502 char *EStr; int Sz;
503 Sz = strtol(Str+6, &EStr, 10);
504 while (isspace(*EStr)) ++EStr;
505 if (*EStr=='\0') {
506 DEBUG(dbgs() << "parsed .space " << Sz << '\n');
507 return Sz;
508 }
509 }
510 Length += MAI.getMaxInstLength();
511 atInsnStart = false;
512 }
Mehdi Amini36d33fc2016-10-01 06:46:33 +0000513 if (atInsnStart && strncmp(Str, MAI.getCommentString().data(),
514 MAI.getCommentString().size()) == 0)
Reed Kotler5c8ae092013-11-13 04:37:52 +0000515 atInsnStart = false;
516 }
517
518 return Length;
519}