blob: 72c15e22c482d59a18991301ae2679d7ccc562ef [file] [log] [blame]
Akira Hatanaka30a84782013-03-14 18:27:31 +00001//===-- MipsSEISelDAGToDAG.cpp - A Dag to Dag Inst Selector for MipsSE ----===//
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// Subclass of MipsDAGToDAGISel specialized for mips32/64.
11//
12//===----------------------------------------------------------------------===//
13
Akira Hatanaka30a84782013-03-14 18:27:31 +000014#include "MipsSEISelDAGToDAG.h"
Akira Hatanaka30a84782013-03-14 18:27:31 +000015#include "MCTargetDesc/MipsBaseInfo.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "Mips.h"
Akira Hatanaka30a84782013-03-14 18:27:31 +000017#include "MipsAnalyzeImmediate.h"
18#include "MipsMachineFunction.h"
19#include "MipsRegisterInfo.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/SelectionDAGNodes.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000026#include "llvm/IR/CFG.h"
Akira Hatanaka30a84782013-03-14 18:27:31 +000027#include "llvm/IR/GlobalValue.h"
28#include "llvm/IR/Instructions.h"
29#include "llvm/IR/Intrinsics.h"
30#include "llvm/IR/Type.h"
Akira Hatanaka30a84782013-03-14 18:27:31 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/raw_ostream.h"
34#include "llvm/Target/TargetMachine.h"
35using namespace llvm;
36
Chandler Carruth84e68b22014-04-22 02:41:26 +000037#define DEBUG_TYPE "mips-isel"
38
Reed Kotler1595f362013-04-09 19:46:01 +000039bool MipsSEDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
Eric Christopher96e72c62015-01-29 23:27:36 +000040 Subtarget = &static_cast<const MipsSubtarget &>(MF.getSubtarget());
Eric Christopher22405e42014-07-10 17:26:51 +000041 if (Subtarget->inMips16Mode())
Reed Kotler1595f362013-04-09 19:46:01 +000042 return false;
43 return MipsDAGToDAGISel::runOnMachineFunction(MF);
44}
Akira Hatanaka30a84782013-03-14 18:27:31 +000045
Akira Hatanakae86bd4f2013-05-03 18:37:49 +000046void MipsSEDAGToDAGISel::addDSPCtrlRegOperands(bool IsDef, MachineInstr &MI,
47 MachineFunction &MF) {
48 MachineInstrBuilder MIB(MF, &MI);
49 unsigned Mask = MI.getOperand(1).getImm();
Daniel Sanders435a6532016-06-14 09:29:46 +000050 unsigned Flag =
51 IsDef ? RegState::ImplicitDefine : RegState::Implicit | RegState::Undef;
Akira Hatanakae86bd4f2013-05-03 18:37:49 +000052
53 if (Mask & 1)
54 MIB.addReg(Mips::DSPPos, Flag);
55
56 if (Mask & 2)
57 MIB.addReg(Mips::DSPSCount, Flag);
58
59 if (Mask & 4)
60 MIB.addReg(Mips::DSPCarry, Flag);
61
62 if (Mask & 8)
63 MIB.addReg(Mips::DSPOutFlag, Flag);
64
65 if (Mask & 16)
66 MIB.addReg(Mips::DSPCCond, Flag);
67
68 if (Mask & 32)
69 MIB.addReg(Mips::DSPEFI, Flag);
70}
71
Daniel Sandersf9aa1d12013-08-28 10:26:24 +000072unsigned MipsSEDAGToDAGISel::getMSACtrlReg(const SDValue RegIdx) const {
73 switch (cast<ConstantSDNode>(RegIdx)->getZExtValue()) {
74 default:
75 llvm_unreachable("Could not map int to register");
76 case 0: return Mips::MSAIR;
77 case 1: return Mips::MSACSR;
78 case 2: return Mips::MSAAccess;
79 case 3: return Mips::MSASave;
80 case 4: return Mips::MSAModify;
81 case 5: return Mips::MSARequest;
82 case 6: return Mips::MSAMap;
83 case 7: return Mips::MSAUnmap;
84 }
85}
86
Akira Hatanaka040d2252013-03-14 18:33:23 +000087bool MipsSEDAGToDAGISel::replaceUsesWithZeroReg(MachineRegisterInfo *MRI,
Akira Hatanaka30a84782013-03-14 18:27:31 +000088 const MachineInstr& MI) {
89 unsigned DstReg = 0, ZeroReg = 0;
90
91 // Check if MI is "addiu $dst, $zero, 0" or "daddiu $dst, $zero, 0".
92 if ((MI.getOpcode() == Mips::ADDiu) &&
93 (MI.getOperand(1).getReg() == Mips::ZERO) &&
94 (MI.getOperand(2).getImm() == 0)) {
95 DstReg = MI.getOperand(0).getReg();
96 ZeroReg = Mips::ZERO;
97 } else if ((MI.getOpcode() == Mips::DADDiu) &&
98 (MI.getOperand(1).getReg() == Mips::ZERO_64) &&
99 (MI.getOperand(2).getImm() == 0)) {
100 DstReg = MI.getOperand(0).getReg();
101 ZeroReg = Mips::ZERO_64;
102 }
103
104 if (!DstReg)
105 return false;
106
107 // Replace uses with ZeroReg.
108 for (MachineRegisterInfo::use_iterator U = MRI->use_begin(DstReg),
109 E = MRI->use_end(); U != E;) {
Owen Anderson16c6bf42014-03-13 23:12:04 +0000110 MachineOperand &MO = *U;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000111 unsigned OpNo = U.getOperandNo();
112 MachineInstr *MI = MO.getParent();
113 ++U;
114
115 // Do not replace if it is a phi's operand or is tied to def operand.
116 if (MI->isPHI() || MI->isRegTiedToDefOperand(OpNo) || MI->isPseudo())
117 continue;
118
Vasileios Kalintiris2f412682015-10-29 10:17:16 +0000119 // Also, we have to check that the register class of the operand
120 // contains the zero register.
121 if (!MRI->getRegClass(MO.getReg())->contains(ZeroReg))
122 continue;
123
Akira Hatanaka30a84782013-03-14 18:27:31 +0000124 MO.setReg(ZeroReg);
125 }
126
127 return true;
128}
129
Akira Hatanaka040d2252013-03-14 18:33:23 +0000130void MipsSEDAGToDAGISel::initGlobalBaseReg(MachineFunction &MF) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000131 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
132
133 if (!MipsFI->globalBaseRegSet())
134 return;
135
136 MachineBasicBlock &MBB = MF.front();
137 MachineBasicBlock::iterator I = MBB.begin();
138 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +0000139 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Petar Jovanovic28e2b712015-08-28 17:53:26 +0000140 DebugLoc DL;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000141 unsigned V0, V1, GlobalBaseReg = MipsFI->getGlobalBaseReg();
142 const TargetRegisterClass *RC;
Eric Christopherd86af632015-01-29 23:27:45 +0000143 const MipsABIInfo &ABI = static_cast<const MipsTargetMachine &>(TM).getABI();
144 RC = (ABI.IsN64()) ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000145
146 V0 = RegInfo.createVirtualRegister(RC);
147 V1 = RegInfo.createVirtualRegister(RC);
148
Eric Christopherd86af632015-01-29 23:27:45 +0000149 if (ABI.IsN64()) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000150 MF.getRegInfo().addLiveIn(Mips::T9_64);
151 MBB.addLiveIn(Mips::T9_64);
152
153 // lui $v0, %hi(%neg(%gp_rel(fname)))
154 // daddu $v1, $v0, $t9
155 // daddiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
156 const GlobalValue *FName = MF.getFunction();
157 BuildMI(MBB, I, DL, TII.get(Mips::LUi64), V0)
158 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
159 BuildMI(MBB, I, DL, TII.get(Mips::DADDu), V1).addReg(V0)
160 .addReg(Mips::T9_64);
161 BuildMI(MBB, I, DL, TII.get(Mips::DADDiu), GlobalBaseReg).addReg(V1)
162 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
163 return;
164 }
165
166 if (MF.getTarget().getRelocationModel() == Reloc::Static) {
167 // Set global register to __gnu_local_gp.
168 //
169 // lui $v0, %hi(__gnu_local_gp)
170 // addiu $globalbasereg, $v0, %lo(__gnu_local_gp)
171 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
172 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_HI);
173 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V0)
174 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_LO);
175 return;
176 }
177
178 MF.getRegInfo().addLiveIn(Mips::T9);
179 MBB.addLiveIn(Mips::T9);
180
Eric Christopherd86af632015-01-29 23:27:45 +0000181 if (ABI.IsN32()) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000182 // lui $v0, %hi(%neg(%gp_rel(fname)))
183 // addu $v1, $v0, $t9
184 // addiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
185 const GlobalValue *FName = MF.getFunction();
186 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
187 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
188 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), V1).addReg(V0).addReg(Mips::T9);
189 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V1)
190 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
191 return;
192 }
193
Eric Christopherd86af632015-01-29 23:27:45 +0000194 assert(ABI.IsO32());
Akira Hatanaka30a84782013-03-14 18:27:31 +0000195
196 // For O32 ABI, the following instruction sequence is emitted to initialize
197 // the global base register:
198 //
199 // 0. lui $2, %hi(_gp_disp)
200 // 1. addiu $2, $2, %lo(_gp_disp)
201 // 2. addu $globalbasereg, $2, $t9
202 //
203 // We emit only the last instruction here.
204 //
205 // GNU linker requires that the first two instructions appear at the beginning
206 // of a function and no instructions be inserted before or between them.
207 // The two instructions are emitted during lowering to MC layer in order to
208 // avoid any reordering.
209 //
210 // Register $2 (Mips::V0) is added to the list of live-in registers to ensure
211 // the value instruction 1 (addiu) defines is valid when instruction 2 (addu)
212 // reads it.
213 MF.getRegInfo().addLiveIn(Mips::V0);
214 MBB.addLiveIn(Mips::V0);
215 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), GlobalBaseReg)
216 .addReg(Mips::V0).addReg(Mips::T9);
217}
218
Akira Hatanaka040d2252013-03-14 18:33:23 +0000219void MipsSEDAGToDAGISel::processFunctionAfterISel(MachineFunction &MF) {
220 initGlobalBaseReg(MF);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000221
222 MachineRegisterInfo *MRI = &MF.getRegInfo();
223
Vasileios Kalintiris36311392016-04-15 20:18:48 +0000224 for (auto &MBB: MF) {
225 for (auto &MI: MBB) {
226 switch (MI.getOpcode()) {
227 case Mips::RDDSP:
228 addDSPCtrlRegOperands(false, MI, MF);
229 break;
230 case Mips::WRDSP:
231 addDSPCtrlRegOperands(true, MI, MF);
232 break;
233 default:
234 replaceUsesWithZeroReg(MRI, MI);
235 }
Akira Hatanakae86bd4f2013-05-03 18:37:49 +0000236 }
Vasileios Kalintiris36311392016-04-15 20:18:48 +0000237 }
Akira Hatanaka30a84782013-03-14 18:27:31 +0000238}
239
Justin Bognereeae7512016-05-13 23:55:59 +0000240void MipsSEDAGToDAGISel::selectAddESubE(unsigned MOp, SDValue InFlag,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000241 SDValue CmpLHS, const SDLoc &DL,
242 SDNode *Node) const {
Akira Hatanakab8835b82013-03-14 18:39:25 +0000243 unsigned Opc = InFlag.getOpcode(); (void)Opc;
244
245 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
246 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
247 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
248
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000249 unsigned SLTuOp = Mips::SLTu, ADDuOp = Mips::ADDu;
250 if (Subtarget->isGP64bit()) {
251 SLTuOp = Mips::SLTu64;
252 ADDuOp = Mips::DADDu;
253 }
254
Akira Hatanakab8835b82013-03-14 18:39:25 +0000255 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
256 SDValue LHS = Node->getOperand(0), RHS = Node->getOperand(1);
257 EVT VT = LHS.getValueType();
258
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000259 SDNode *Carry = CurDAG->getMachineNode(SLTuOp, DL, VT, Ops);
260
261 if (Subtarget->isGP64bit()) {
262 // On 64-bit targets, sltu produces an i64 but our backend currently says
263 // that SLTu64 produces an i32. We need to fix this in the long run but for
264 // now, just make the DAG type-correct by asserting the upper bits are zero.
265 Carry = CurDAG->getMachineNode(Mips::SUBREG_TO_REG, DL, VT,
266 CurDAG->getTargetConstant(0, DL, VT),
267 SDValue(Carry, 0),
268 CurDAG->getTargetConstant(Mips::sub_32, DL,
269 VT));
270 }
271
Vasileios Kalintiris18581f12015-02-27 09:01:39 +0000272 // Generate a second addition only if we know that RHS is not a
273 // constant-zero node.
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000274 SDNode *AddCarry = Carry;
Vasileios Kalintiris18581f12015-02-27 09:01:39 +0000275 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
276 if (!C || C->getZExtValue())
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000277 AddCarry = CurDAG->getMachineNode(ADDuOp, DL, VT, SDValue(Carry, 0), RHS);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000278
Justin Bognereeae7512016-05-13 23:55:59 +0000279 CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue, LHS, SDValue(AddCarry, 0));
Akira Hatanakab8835b82013-03-14 18:39:25 +0000280}
281
Daniel Sandersfa961d72014-03-03 14:31:21 +0000282/// Match frameindex
283bool MipsSEDAGToDAGISel::selectAddrFrameIndex(SDValue Addr, SDValue &Base,
284 SDValue &Offset) const {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000285 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Daniel Sandersfa961d72014-03-03 14:31:21 +0000286 EVT ValTy = Addr.getValueType();
287
Akira Hatanaka30a84782013-03-14 18:27:31 +0000288 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000289 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), ValTy);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000290 return true;
291 }
Daniel Sandersfa961d72014-03-03 14:31:21 +0000292 return false;
293}
294
295/// Match frameindex+offset and frameindex|offset
296bool MipsSEDAGToDAGISel::selectAddrFrameIndexOffset(SDValue Addr, SDValue &Base,
297 SDValue &Offset,
298 unsigned OffsetBits) const {
299 if (CurDAG->isBaseWithConstantOffset(Addr)) {
300 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
301 if (isIntN(OffsetBits, CN->getSExtValue())) {
302 EVT ValTy = Addr.getValueType();
303
304 // If the first operand is a FI, get the TargetFI Node
305 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
306 (Addr.getOperand(0)))
307 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
308 else
309 Base = Addr.getOperand(0);
310
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000311 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), SDLoc(Addr),
312 ValTy);
Daniel Sandersfa961d72014-03-03 14:31:21 +0000313 return true;
314 }
315 }
316 return false;
317}
318
319/// ComplexPattern used on MipsInstrInfo
320/// Used on Mips Load/Store instructions
321bool MipsSEDAGToDAGISel::selectAddrRegImm(SDValue Addr, SDValue &Base,
322 SDValue &Offset) const {
323 // if Address is FI, get the TargetFrameIndex.
324 if (selectAddrFrameIndex(Addr, Base, Offset))
325 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000326
327 // on PIC code Load GA
328 if (Addr.getOpcode() == MipsISD::Wrapper) {
329 Base = Addr.getOperand(0);
330 Offset = Addr.getOperand(1);
331 return true;
332 }
333
334 if (TM.getRelocationModel() != Reloc::PIC_) {
335 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
336 Addr.getOpcode() == ISD::TargetGlobalAddress))
337 return false;
338 }
339
340 // Addresses of the form FI+const or FI|const
Daniel Sandersfa961d72014-03-03 14:31:21 +0000341 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 16))
342 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000343
344 // Operand is a result from an ADD.
345 if (Addr.getOpcode() == ISD::ADD) {
346 // When loading from constant pools, load the lower address part in
347 // the instruction itself. Example, instead of:
348 // lui $2, %hi($CPI1_0)
349 // addiu $2, $2, %lo($CPI1_0)
350 // lwc1 $f0, 0($2)
351 // Generate:
352 // lui $2, %hi($CPI1_0)
353 // lwc1 $f0, %lo($CPI1_0)($2)
354 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo ||
355 Addr.getOperand(1).getOpcode() == MipsISD::GPRel) {
356 SDValue Opnd0 = Addr.getOperand(1).getOperand(0);
357 if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) ||
358 isa<JumpTableSDNode>(Opnd0)) {
359 Base = Addr.getOperand(0);
360 Offset = Opnd0;
361 return true;
362 }
363 }
364 }
365
366 return false;
367}
368
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000369/// ComplexPattern used on MipsInstrInfo
370/// Used on Mips Load/Store instructions
371bool MipsSEDAGToDAGISel::selectAddrRegReg(SDValue Addr, SDValue &Base,
372 SDValue &Offset) const {
373 // Operand is a result from an ADD.
374 if (Addr.getOpcode() == ISD::ADD) {
375 Base = Addr.getOperand(0);
376 Offset = Addr.getOperand(1);
377 return true;
378 }
379
380 return false;
381}
382
Akira Hatanaka30a84782013-03-14 18:27:31 +0000383bool MipsSEDAGToDAGISel::selectAddrDefault(SDValue Addr, SDValue &Base,
384 SDValue &Offset) const {
385 Base = Addr;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000386 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), Addr.getValueType());
Akira Hatanaka30a84782013-03-14 18:27:31 +0000387 return true;
388}
389
390bool MipsSEDAGToDAGISel::selectIntAddr(SDValue Addr, SDValue &Base,
391 SDValue &Offset) const {
392 return selectAddrRegImm(Addr, Base, Offset) ||
393 selectAddrDefault(Addr, Base, Offset);
394}
395
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000396bool MipsSEDAGToDAGISel::selectAddrRegImm9(SDValue Addr, SDValue &Base,
397 SDValue &Offset) const {
398 if (selectAddrFrameIndex(Addr, Base, Offset))
399 return true;
400
401 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 9))
402 return true;
403
404 return false;
405}
406
Daniel Sandersfa961d72014-03-03 14:31:21 +0000407bool MipsSEDAGToDAGISel::selectAddrRegImm10(SDValue Addr, SDValue &Base,
408 SDValue &Offset) const {
409 if (selectAddrFrameIndex(Addr, Base, Offset))
410 return true;
411
412 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10))
413 return true;
414
415 return false;
416}
417
Jack Carter97700972013-08-13 20:19:16 +0000418/// Used on microMIPS Load/Store unaligned instructions (12-bit offset)
419bool MipsSEDAGToDAGISel::selectAddrRegImm12(SDValue Addr, SDValue &Base,
420 SDValue &Offset) const {
Daniel Sandersfa961d72014-03-03 14:31:21 +0000421 if (selectAddrFrameIndex(Addr, Base, Offset))
422 return true;
Jack Carter97700972013-08-13 20:19:16 +0000423
Daniel Sandersfa961d72014-03-03 14:31:21 +0000424 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 12))
425 return true;
Jack Carter97700972013-08-13 20:19:16 +0000426
427 return false;
428}
429
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000430bool MipsSEDAGToDAGISel::selectAddrRegImm16(SDValue Addr, SDValue &Base,
431 SDValue &Offset) const {
432 if (selectAddrFrameIndex(Addr, Base, Offset))
433 return true;
434
435 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 16))
436 return true;
437
438 return false;
439}
440
Jack Carter97700972013-08-13 20:19:16 +0000441bool MipsSEDAGToDAGISel::selectIntAddrMM(SDValue Addr, SDValue &Base,
442 SDValue &Offset) const {
443 return selectAddrRegImm12(Addr, Base, Offset) ||
444 selectAddrDefault(Addr, Base, Offset);
445}
446
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000447bool MipsSEDAGToDAGISel::selectIntAddrLSL2MM(SDValue Addr, SDValue &Base,
448 SDValue &Offset) const {
449 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 7)) {
Vasileios Kalintiris99eeb8a2015-02-13 19:14:22 +0000450 if (isa<FrameIndexSDNode>(Base))
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000451 return false;
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000452
Vasileios Kalintiris99eeb8a2015-02-13 19:14:22 +0000453 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Offset)) {
454 unsigned CnstOff = CN->getZExtValue();
455 return (CnstOff == (CnstOff & 0x3c));
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000456 }
Vasileios Kalintiris99eeb8a2015-02-13 19:14:22 +0000457
458 return false;
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000459 }
460
461 // For all other cases where "lw" would be selected, don't select "lw16"
462 // because it would result in additional instructions to prepare operands.
463 if (selectAddrRegImm(Addr, Base, Offset))
464 return false;
465
466 return selectAddrDefault(Addr, Base, Offset);
467}
468
Daniel Sandersfa961d72014-03-03 14:31:21 +0000469bool MipsSEDAGToDAGISel::selectIntAddrMSA(SDValue Addr, SDValue &Base,
470 SDValue &Offset) const {
471 if (selectAddrRegImm10(Addr, Base, Offset))
472 return true;
473
474 if (selectAddrDefault(Addr, Base, Offset))
475 return true;
476
477 return false;
478}
479
Daniel Sandersf49dd822013-09-24 13:33:07 +0000480// Select constant vector splats.
481//
482// Returns true and sets Imm if:
483// * MSA is enabled
484// * N is a ISD::BUILD_VECTOR representing a constant splat
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000485bool MipsSEDAGToDAGISel::selectVSplat(SDNode *N, APInt &Imm,
486 unsigned MinSizeInBits) const {
Eric Christopher22405e42014-07-10 17:26:51 +0000487 if (!Subtarget->hasMSA())
Daniel Sandersf49dd822013-09-24 13:33:07 +0000488 return false;
489
490 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N);
491
Craig Topper062a2ba2014-04-25 05:30:21 +0000492 if (!Node)
Daniel Sandersf49dd822013-09-24 13:33:07 +0000493 return false;
494
495 APInt SplatValue, SplatUndef;
496 unsigned SplatBitSize;
497 bool HasAnyUndefs;
498
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000499 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
500 MinSizeInBits, !Subtarget->isLittle()))
Daniel Sandersf49dd822013-09-24 13:33:07 +0000501 return false;
502
Daniel Sandersf49dd822013-09-24 13:33:07 +0000503 Imm = SplatValue;
504
505 return true;
506}
507
508// Select constant vector splats.
509//
510// In addition to the requirements of selectVSplat(), this function returns
511// true and sets Imm if:
512// * The splat value is the same width as the elements of the vector
513// * The splat value fits in an integer with the specified signed-ness and
514// width.
515//
516// This function looks through ISD::BITCAST nodes.
517// TODO: This might not be appropriate for big-endian MSA since BITCAST is
518// sometimes a shuffle in big-endian mode.
519//
520// It's worth noting that this function is not used as part of the selection
521// of ldi.[bhwd] since it does not permit using the wrong-typed ldi.[bhwd]
522// instruction to achieve the desired bit pattern. ldi.[bhwd] is selected in
523// MipsSEDAGToDAGISel::selectNode.
524bool MipsSEDAGToDAGISel::
525selectVSplatCommon(SDValue N, SDValue &Imm, bool Signed,
526 unsigned ImmBitSize) const {
527 APInt ImmValue;
528 EVT EltTy = N->getValueType(0).getVectorElementType();
529
530 if (N->getOpcode() == ISD::BITCAST)
531 N = N->getOperand(0);
532
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000533 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
Daniel Sandersf49dd822013-09-24 13:33:07 +0000534 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000535
Daniel Sandersf49dd822013-09-24 13:33:07 +0000536 if (( Signed && ImmValue.isSignedIntN(ImmBitSize)) ||
537 (!Signed && ImmValue.isIntN(ImmBitSize))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000538 Imm = CurDAG->getTargetConstant(ImmValue, SDLoc(N), EltTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +0000539 return true;
540 }
541 }
542
543 return false;
544}
545
546// Select constant vector splats.
547bool MipsSEDAGToDAGISel::
Daniel Sanders7e51fe12013-09-27 11:48:57 +0000548selectVSplatUimm1(SDValue N, SDValue &Imm) const {
549 return selectVSplatCommon(N, Imm, false, 1);
550}
551
552bool MipsSEDAGToDAGISel::
553selectVSplatUimm2(SDValue N, SDValue &Imm) const {
554 return selectVSplatCommon(N, Imm, false, 2);
555}
556
557bool MipsSEDAGToDAGISel::
Daniel Sandersf49dd822013-09-24 13:33:07 +0000558selectVSplatUimm3(SDValue N, SDValue &Imm) const {
559 return selectVSplatCommon(N, Imm, false, 3);
560}
561
562// Select constant vector splats.
563bool MipsSEDAGToDAGISel::
564selectVSplatUimm4(SDValue N, SDValue &Imm) const {
565 return selectVSplatCommon(N, Imm, false, 4);
566}
567
568// Select constant vector splats.
569bool MipsSEDAGToDAGISel::
570selectVSplatUimm5(SDValue N, SDValue &Imm) const {
571 return selectVSplatCommon(N, Imm, false, 5);
572}
573
574// Select constant vector splats.
575bool MipsSEDAGToDAGISel::
576selectVSplatUimm6(SDValue N, SDValue &Imm) const {
577 return selectVSplatCommon(N, Imm, false, 6);
578}
579
580// Select constant vector splats.
581bool MipsSEDAGToDAGISel::
582selectVSplatUimm8(SDValue N, SDValue &Imm) const {
583 return selectVSplatCommon(N, Imm, false, 8);
584}
585
586// Select constant vector splats.
587bool MipsSEDAGToDAGISel::
588selectVSplatSimm5(SDValue N, SDValue &Imm) const {
589 return selectVSplatCommon(N, Imm, true, 5);
590}
591
592// Select constant vector splats whose value is a power of 2.
593//
594// In addition to the requirements of selectVSplat(), this function returns
595// true and sets Imm if:
596// * The splat value is the same width as the elements of the vector
597// * The splat value is a power of two.
598//
599// This function looks through ISD::BITCAST nodes.
600// TODO: This might not be appropriate for big-endian MSA since BITCAST is
601// sometimes a shuffle in big-endian mode.
602bool MipsSEDAGToDAGISel::selectVSplatUimmPow2(SDValue N, SDValue &Imm) const {
603 APInt ImmValue;
604 EVT EltTy = N->getValueType(0).getVectorElementType();
605
606 if (N->getOpcode() == ISD::BITCAST)
607 N = N->getOperand(0);
608
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000609 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
Daniel Sandersf49dd822013-09-24 13:33:07 +0000610 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
611 int32_t Log2 = ImmValue.exactLogBase2();
612
613 if (Log2 != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000614 Imm = CurDAG->getTargetConstant(Log2, SDLoc(N), EltTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +0000615 return true;
616 }
617 }
618
619 return false;
620}
621
Daniel Sandersd74b1302013-10-30 14:45:14 +0000622// Select constant vector splats whose value only has a consecutive sequence
623// of left-most bits set (e.g. 0b11...1100...00).
624//
625// In addition to the requirements of selectVSplat(), this function returns
626// true and sets Imm if:
627// * The splat value is the same width as the elements of the vector
628// * The splat value is a consecutive sequence of left-most bits.
629//
630// This function looks through ISD::BITCAST nodes.
631// TODO: This might not be appropriate for big-endian MSA since BITCAST is
632// sometimes a shuffle in big-endian mode.
633bool MipsSEDAGToDAGISel::selectVSplatMaskL(SDValue N, SDValue &Imm) const {
634 APInt ImmValue;
635 EVT EltTy = N->getValueType(0).getVectorElementType();
636
637 if (N->getOpcode() == ISD::BITCAST)
638 N = N->getOperand(0);
639
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000640 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
Daniel Sandersd74b1302013-10-30 14:45:14 +0000641 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
642 // Extract the run of set bits starting with bit zero from the bitwise
643 // inverse of ImmValue, and test that the inverse of this is the same
644 // as the original value.
645 if (ImmValue == ~(~ImmValue & ~(~ImmValue + 1))) {
646
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000647 Imm = CurDAG->getTargetConstant(ImmValue.countPopulation(), SDLoc(N),
648 EltTy);
Daniel Sandersd74b1302013-10-30 14:45:14 +0000649 return true;
650 }
651 }
652
653 return false;
654}
655
656// Select constant vector splats whose value only has a consecutive sequence
657// of right-most bits set (e.g. 0b00...0011...11).
658//
659// In addition to the requirements of selectVSplat(), this function returns
660// true and sets Imm if:
661// * The splat value is the same width as the elements of the vector
662// * The splat value is a consecutive sequence of right-most bits.
663//
664// This function looks through ISD::BITCAST nodes.
665// TODO: This might not be appropriate for big-endian MSA since BITCAST is
666// sometimes a shuffle in big-endian mode.
667bool MipsSEDAGToDAGISel::selectVSplatMaskR(SDValue N, SDValue &Imm) const {
668 APInt ImmValue;
669 EVT EltTy = N->getValueType(0).getVectorElementType();
670
671 if (N->getOpcode() == ISD::BITCAST)
672 N = N->getOperand(0);
673
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000674 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
Daniel Sandersd74b1302013-10-30 14:45:14 +0000675 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
676 // Extract the run of set bits starting with bit zero, and test that the
677 // result is the same as the original value
678 if (ImmValue == (ImmValue & ~(ImmValue + 1))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000679 Imm = CurDAG->getTargetConstant(ImmValue.countPopulation(), SDLoc(N),
680 EltTy);
Daniel Sandersd74b1302013-10-30 14:45:14 +0000681 return true;
682 }
683 }
684
685 return false;
686}
687
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000688bool MipsSEDAGToDAGISel::selectVSplatUimmInvPow2(SDValue N,
689 SDValue &Imm) const {
690 APInt ImmValue;
691 EVT EltTy = N->getValueType(0).getVectorElementType();
692
693 if (N->getOpcode() == ISD::BITCAST)
694 N = N->getOperand(0);
695
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000696 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000697 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
698 int32_t Log2 = (~ImmValue).exactLogBase2();
699
700 if (Log2 != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000701 Imm = CurDAG->getTargetConstant(Log2, SDLoc(N), EltTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000702 return true;
703 }
704 }
705
706 return false;
707}
708
Justin Bognereeae7512016-05-13 23:55:59 +0000709bool MipsSEDAGToDAGISel::trySelect(SDNode *Node) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000710 unsigned Opcode = Node->getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000711 SDLoc DL(Node);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000712
713 ///
714 // Instruction Selection not handled by the auto-generated
715 // tablegen selection should be handled here.
716 ///
Akira Hatanaka30a84782013-03-14 18:27:31 +0000717 switch(Opcode) {
718 default: break;
719
Akira Hatanakab8835b82013-03-14 18:39:25 +0000720 case ISD::SUBE: {
721 SDValue InFlag = Node->getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000722 unsigned Opc = Subtarget->isGP64bit() ? Mips::DSUBu : Mips::SUBu;
Justin Bognereeae7512016-05-13 23:55:59 +0000723 selectAddESubE(Opc, InFlag, InFlag.getOperand(0), DL, Node);
724 return true;
Akira Hatanakab8835b82013-03-14 18:39:25 +0000725 }
726
Akira Hatanaka30a84782013-03-14 18:27:31 +0000727 case ISD::ADDE: {
Eric Christopher22405e42014-07-10 17:26:51 +0000728 if (Subtarget->hasDSP()) // Select DSP instructions, ADDSC and ADDWC.
Akira Hatanaka2f088222013-04-13 00:55:41 +0000729 break;
Akira Hatanakab8835b82013-03-14 18:39:25 +0000730 SDValue InFlag = Node->getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000731 unsigned Opc = Subtarget->isGP64bit() ? Mips::DADDu : Mips::ADDu;
Justin Bognereeae7512016-05-13 23:55:59 +0000732 selectAddESubE(Opc, InFlag, InFlag.getValue(0), DL, Node);
733 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000734 }
735
Akira Hatanaka30a84782013-03-14 18:27:31 +0000736 case ISD::ConstantFP: {
737 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
738 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
Eric Christopher22405e42014-07-10 17:26:51 +0000739 if (Subtarget->isGP64bit()) {
Akira Hatanaka040d2252013-03-14 18:33:23 +0000740 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000741 Mips::ZERO_64, MVT::i64);
Justin Bognereeae7512016-05-13 23:55:59 +0000742 ReplaceNode(Node,
743 CurDAG->getMachineNode(Mips::DMTC1, DL, MVT::f64, Zero));
Eric Christopher22405e42014-07-10 17:26:51 +0000744 } else if (Subtarget->isFP64bit()) {
Daniel Sanders08d3cd12013-11-18 13:12:43 +0000745 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
746 Mips::ZERO, MVT::i32);
Justin Bognereeae7512016-05-13 23:55:59 +0000747 ReplaceNode(Node, CurDAG->getMachineNode(Mips::BuildPairF64_64, DL,
748 MVT::f64, Zero, Zero));
Akira Hatanaka30a84782013-03-14 18:27:31 +0000749 } else {
Akira Hatanaka040d2252013-03-14 18:33:23 +0000750 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000751 Mips::ZERO, MVT::i32);
Justin Bognereeae7512016-05-13 23:55:59 +0000752 ReplaceNode(Node, CurDAG->getMachineNode(Mips::BuildPairF64, DL,
753 MVT::f64, Zero, Zero));
Akira Hatanaka30a84782013-03-14 18:27:31 +0000754 }
Justin Bognereeae7512016-05-13 23:55:59 +0000755 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000756 }
757 break;
758 }
759
760 case ISD::Constant: {
761 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node);
762 unsigned Size = CN->getValueSizeInBits(0);
763
764 if (Size == 32)
765 break;
766
767 MipsAnalyzeImmediate AnalyzeImm;
768 int64_t Imm = CN->getSExtValue();
769
770 const MipsAnalyzeImmediate::InstSeq &Seq =
771 AnalyzeImm.Analyze(Imm, Size, false);
772
773 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000774 SDLoc DL(CN);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000775 SDNode *RegOpnd;
776 SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000777 DL, MVT::i64);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000778
779 // The first instruction can be a LUi which is different from other
780 // instructions (ADDiu, ORI and SLL) in that it does not have a register
781 // operand.
782 if (Inst->Opc == Mips::LUi64)
783 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
784 else
785 RegOpnd =
786 CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
787 CurDAG->getRegister(Mips::ZERO_64, MVT::i64),
788 ImmOpnd);
789
790 // The remaining instructions in the sequence are handled here.
791 for (++Inst; Inst != Seq.end(); ++Inst) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000792 ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd), DL,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000793 MVT::i64);
794 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
795 SDValue(RegOpnd, 0), ImmOpnd);
796 }
797
Justin Bognereeae7512016-05-13 23:55:59 +0000798 ReplaceNode(Node, RegOpnd);
799 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000800 }
801
Daniel Sandersf9aa1d12013-08-28 10:26:24 +0000802 case ISD::INTRINSIC_W_CHAIN: {
803 switch (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
804 default:
805 break;
806
807 case Intrinsic::mips_cfcmsa: {
808 SDValue ChainIn = Node->getOperand(0);
809 SDValue RegIdx = Node->getOperand(2);
810 SDValue Reg = CurDAG->getCopyFromReg(ChainIn, DL,
811 getMSACtrlReg(RegIdx), MVT::i32);
Justin Bognereeae7512016-05-13 23:55:59 +0000812 ReplaceNode(Node, Reg.getNode());
813 return true;
Daniel Sandersf9aa1d12013-08-28 10:26:24 +0000814 }
815 }
816 break;
817 }
818
Daniel Sandersba9c8502013-08-28 10:44:47 +0000819 case ISD::INTRINSIC_WO_CHAIN: {
820 switch (cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue()) {
821 default:
822 break;
823
824 case Intrinsic::mips_move_v:
825 // Like an assignment but will always produce a move.v even if
826 // unnecessary.
Justin Bognereeae7512016-05-13 23:55:59 +0000827 ReplaceNode(Node, CurDAG->getMachineNode(Mips::MOVE_V, DL,
828 Node->getValueType(0),
829 Node->getOperand(1)));
830 return true;
Daniel Sandersba9c8502013-08-28 10:44:47 +0000831 }
832 break;
833 }
834
Daniel Sandersf9aa1d12013-08-28 10:26:24 +0000835 case ISD::INTRINSIC_VOID: {
836 switch (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
837 default:
838 break;
839
840 case Intrinsic::mips_ctcmsa: {
841 SDValue ChainIn = Node->getOperand(0);
842 SDValue RegIdx = Node->getOperand(2);
843 SDValue Value = Node->getOperand(3);
844 SDValue ChainOut = CurDAG->getCopyToReg(ChainIn, DL,
845 getMSACtrlReg(RegIdx), Value);
Justin Bognereeae7512016-05-13 23:55:59 +0000846 ReplaceNode(Node, ChainOut.getNode());
847 return true;
Daniel Sandersf9aa1d12013-08-28 10:26:24 +0000848 }
849 }
850 break;
851 }
852
Akira Hatanaka30a84782013-03-14 18:27:31 +0000853 case MipsISD::ThreadPointer: {
Mehdi Amini44ede332015-07-09 02:09:04 +0000854 EVT PtrVT = getTargetLowering()->getPointerTy(CurDAG->getDataLayout());
Akira Hatanaka85ccf232013-08-08 21:37:32 +0000855 unsigned RdhwrOpc, DestReg;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000856
857 if (PtrVT == MVT::i32) {
858 RdhwrOpc = Mips::RDHWR;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000859 DestReg = Mips::V1;
860 } else {
861 RdhwrOpc = Mips::RDHWR64;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000862 DestReg = Mips::V1_64;
863 }
864
865 SDNode *Rdhwr =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000866 CurDAG->getMachineNode(RdhwrOpc, DL,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000867 Node->getValueType(0),
Akira Hatanaka85ccf232013-08-08 21:37:32 +0000868 CurDAG->getRegister(Mips::HWR29, MVT::i32));
Akira Hatanaka040d2252013-03-14 18:33:23 +0000869 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), DL, DestReg,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000870 SDValue(Rdhwr, 0));
Akira Hatanaka040d2252013-03-14 18:33:23 +0000871 SDValue ResNode = CurDAG->getCopyFromReg(Chain, DL, DestReg, PtrVT);
Justin Bognereeae7512016-05-13 23:55:59 +0000872 ReplaceNode(Node, ResNode.getNode());
873 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000874 }
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000875
Daniel Sandersf49dd822013-09-24 13:33:07 +0000876 case ISD::BUILD_VECTOR: {
877 // Select appropriate ldi.[bhwd] instructions for constant splats of
878 // 128-bit when MSA is enabled. Fixup any register class mismatches that
879 // occur as a result.
880 //
881 // This allows the compiler to use a wider range of immediates than would
882 // otherwise be allowed. If, for example, v4i32 could only use ldi.h then
883 // it would not be possible to load { 0x01010101, 0x01010101, 0x01010101,
884 // 0x01010101 } without using a constant pool. This would be sub-optimal
885 // when // 'ldi.b wd, 1' is capable of producing that bit-pattern in the
886 // same set/ of registers. Similarly, ldi.h isn't capable of producing {
887 // 0x00000000, 0x00000001, 0x00000000, 0x00000001 } but 'ldi.d wd, 1' can.
888
889 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Node);
890 APInt SplatValue, SplatUndef;
891 unsigned SplatBitSize;
892 bool HasAnyUndefs;
893 unsigned LdiOp;
894 EVT ResVecTy = BVN->getValueType(0);
895 EVT ViaVecTy;
896
Eric Christopher22405e42014-07-10 17:26:51 +0000897 if (!Subtarget->hasMSA() || !BVN->getValueType(0).is128BitVector())
Justin Bognereeae7512016-05-13 23:55:59 +0000898 return false;
Daniel Sandersf49dd822013-09-24 13:33:07 +0000899
900 if (!BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
901 HasAnyUndefs, 8,
Eric Christopher22405e42014-07-10 17:26:51 +0000902 !Subtarget->isLittle()))
Justin Bognereeae7512016-05-13 23:55:59 +0000903 return false;
Daniel Sandersf49dd822013-09-24 13:33:07 +0000904
905 switch (SplatBitSize) {
906 default:
Justin Bognereeae7512016-05-13 23:55:59 +0000907 return false;
Daniel Sandersf49dd822013-09-24 13:33:07 +0000908 case 8:
909 LdiOp = Mips::LDI_B;
910 ViaVecTy = MVT::v16i8;
911 break;
912 case 16:
913 LdiOp = Mips::LDI_H;
914 ViaVecTy = MVT::v8i16;
915 break;
916 case 32:
917 LdiOp = Mips::LDI_W;
918 ViaVecTy = MVT::v4i32;
919 break;
920 case 64:
921 LdiOp = Mips::LDI_D;
922 ViaVecTy = MVT::v2i64;
923 break;
924 }
925
926 if (!SplatValue.isSignedIntN(10))
Justin Bognereeae7512016-05-13 23:55:59 +0000927 return false;
Daniel Sandersf49dd822013-09-24 13:33:07 +0000928
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000929 SDValue Imm = CurDAG->getTargetConstant(SplatValue, DL,
Daniel Sandersf49dd822013-09-24 13:33:07 +0000930 ViaVecTy.getVectorElementType());
931
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000932 SDNode *Res = CurDAG->getMachineNode(LdiOp, DL, ViaVecTy, Imm);
Daniel Sandersf49dd822013-09-24 13:33:07 +0000933
934 if (ResVecTy != ViaVecTy) {
935 // If LdiOp is writing to a different register class to ResVecTy, then
936 // fix it up here. This COPY_TO_REGCLASS should never cause a move.v
937 // since the source and destination register sets contain the same
938 // registers.
939 const TargetLowering *TLI = getTargetLowering();
940 MVT ResVecTySimple = ResVecTy.getSimpleVT();
941 const TargetRegisterClass *RC = TLI->getRegClassFor(ResVecTySimple);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000942 Res = CurDAG->getMachineNode(Mips::COPY_TO_REGCLASS, DL,
Daniel Sandersf49dd822013-09-24 13:33:07 +0000943 ResVecTy, SDValue(Res, 0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000944 CurDAG->getTargetConstant(RC->getID(), DL,
Daniel Sandersf49dd822013-09-24 13:33:07 +0000945 MVT::i32));
946 }
947
Justin Bognereeae7512016-05-13 23:55:59 +0000948 ReplaceNode(Node, Res);
949 return true;
Daniel Sandersf49dd822013-09-24 13:33:07 +0000950 }
951
Akira Hatanaka30a84782013-03-14 18:27:31 +0000952 }
953
Justin Bognereeae7512016-05-13 23:55:59 +0000954 return false;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000955}
956
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000957bool MipsSEDAGToDAGISel::
958SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
959 std::vector<SDValue> &OutOps) {
960 SDValue Base, Offset;
961
962 switch(ConstraintID) {
963 default:
964 llvm_unreachable("Unexpected asm memory constraint");
965 // All memory constraints can at least accept raw pointers.
966 case InlineAsm::Constraint_i:
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000967 OutOps.push_back(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000968 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000969 return false;
Daniel Sandersc676f2a2015-03-24 15:19:14 +0000970 case InlineAsm::Constraint_m:
971 if (selectAddrRegImm16(Op, Base, Offset)) {
972 OutOps.push_back(Base);
973 OutOps.push_back(Offset);
974 return false;
975 }
976 OutOps.push_back(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000977 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Daniel Sandersc676f2a2015-03-24 15:19:14 +0000978 return false;
Daniel Sanders82df6162015-03-30 13:27:25 +0000979 case InlineAsm::Constraint_R:
980 // The 'R' constraint is supposed to be much more complicated than this.
981 // However, it's becoming less useful due to architectural changes and
982 // ought to be replaced by other constraints such as 'ZC'.
983 // For now, support 9-bit signed offsets which is supportable by all
984 // subtargets for all instructions.
985 if (selectAddrRegImm9(Op, Base, Offset)) {
986 OutOps.push_back(Base);
987 OutOps.push_back(Offset);
988 return false;
989 }
990 OutOps.push_back(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000991 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Daniel Sanders82df6162015-03-30 13:27:25 +0000992 return false;
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000993 case InlineAsm::Constraint_ZC:
994 // ZC matches whatever the pref, ll, and sc instructions can handle for the
995 // given subtarget.
996 if (Subtarget->inMicroMipsMode()) {
997 // On microMIPS, they can handle 12-bit offsets.
998 if (selectAddrRegImm12(Op, Base, Offset)) {
999 OutOps.push_back(Base);
1000 OutOps.push_back(Offset);
1001 return false;
1002 }
1003 } else if (Subtarget->hasMips32r6()) {
1004 // On MIPS32r6/MIPS64r6, they can only handle 9-bit offsets.
1005 if (selectAddrRegImm9(Op, Base, Offset)) {
1006 OutOps.push_back(Base);
1007 OutOps.push_back(Offset);
1008 return false;
1009 }
1010 } else if (selectAddrRegImm16(Op, Base, Offset)) {
1011 // Prior to MIPS32r6/MIPS64r6, they can handle 16-bit offsets.
1012 OutOps.push_back(Base);
1013 OutOps.push_back(Offset);
1014 return false;
1015 }
1016 // In all cases, 0-bit offsets are acceptable.
1017 OutOps.push_back(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001018 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Daniel Sandersa73d8fe2015-03-24 11:26:34 +00001019 return false;
1020 }
1021 return true;
1022}
1023
Akira Hatanaka30a84782013-03-14 18:27:31 +00001024FunctionPass *llvm::createMipsSEISelDag(MipsTargetMachine &TM) {
1025 return new MipsSEDAGToDAGISel(TM);
1026}