blob: d9528da5a96de4ca06cc2882283a00ec23be1817 [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
Rafael Espindolab30e66b2016-06-28 14:33:28 +0000166 if (!MF.getTarget().isPositionIndependent()) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000167 // 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
Rafael Espindolab30e66b2016-06-28 14:33:28 +0000334 if (!TM.isPositionIndependent()) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000335 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
Akira Hatanaka30a84782013-03-14 18:27:31 +0000371bool MipsSEDAGToDAGISel::selectAddrDefault(SDValue Addr, SDValue &Base,
372 SDValue &Offset) const {
373 Base = Addr;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000374 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), Addr.getValueType());
Akira Hatanaka30a84782013-03-14 18:27:31 +0000375 return true;
376}
377
378bool MipsSEDAGToDAGISel::selectIntAddr(SDValue Addr, SDValue &Base,
379 SDValue &Offset) const {
380 return selectAddrRegImm(Addr, Base, Offset) ||
381 selectAddrDefault(Addr, Base, Offset);
382}
383
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000384bool MipsSEDAGToDAGISel::selectAddrRegImm9(SDValue Addr, SDValue &Base,
385 SDValue &Offset) const {
386 if (selectAddrFrameIndex(Addr, Base, Offset))
387 return true;
388
389 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 9))
390 return true;
391
392 return false;
393}
394
Daniel Sandersfa961d72014-03-03 14:31:21 +0000395bool MipsSEDAGToDAGISel::selectAddrRegImm10(SDValue Addr, SDValue &Base,
396 SDValue &Offset) const {
397 if (selectAddrFrameIndex(Addr, Base, Offset))
398 return true;
399
400 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10))
401 return true;
402
403 return false;
404}
405
Zlatko Buljancba9f802016-07-11 07:41:56 +0000406/// Used on microMIPS LWC2, LDC2, SWC2 and SDC2 instructions (11-bit offset)
407bool MipsSEDAGToDAGISel::selectAddrRegImm11(SDValue Addr, SDValue &Base,
408 SDValue &Offset) const {
409 if (selectAddrFrameIndex(Addr, Base, Offset))
410 return true;
411
412 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 11))
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
Zlatko Buljancba9f802016-07-11 07:41:56 +0000441bool MipsSEDAGToDAGISel::selectIntAddr11MM(SDValue Addr, SDValue &Base,
442 SDValue &Offset) const {
443 return selectAddrRegImm11(Addr, Base, Offset) ||
444 selectAddrDefault(Addr, Base, Offset);
445}
446
447bool MipsSEDAGToDAGISel::selectIntAddr12MM(SDValue Addr, SDValue &Base,
Jack Carter97700972013-08-13 20:19:16 +0000448 SDValue &Offset) const {
449 return selectAddrRegImm12(Addr, Base, Offset) ||
450 selectAddrDefault(Addr, Base, Offset);
451}
452
Zlatko Buljancba9f802016-07-11 07:41:56 +0000453bool MipsSEDAGToDAGISel::selectIntAddr16MM(SDValue Addr, SDValue &Base,
454 SDValue &Offset) const {
455 return selectAddrRegImm16(Addr, Base, Offset) ||
456 selectAddrDefault(Addr, Base, Offset);
457}
458
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000459bool MipsSEDAGToDAGISel::selectIntAddrLSL2MM(SDValue Addr, SDValue &Base,
460 SDValue &Offset) const {
461 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 7)) {
Vasileios Kalintiris99eeb8a2015-02-13 19:14:22 +0000462 if (isa<FrameIndexSDNode>(Base))
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000463 return false;
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000464
Vasileios Kalintiris99eeb8a2015-02-13 19:14:22 +0000465 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Offset)) {
466 unsigned CnstOff = CN->getZExtValue();
467 return (CnstOff == (CnstOff & 0x3c));
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000468 }
Vasileios Kalintiris99eeb8a2015-02-13 19:14:22 +0000469
470 return false;
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000471 }
472
473 // For all other cases where "lw" would be selected, don't select "lw16"
474 // because it would result in additional instructions to prepare operands.
475 if (selectAddrRegImm(Addr, Base, Offset))
476 return false;
477
478 return selectAddrDefault(Addr, Base, Offset);
479}
480
Daniel Sandersfa961d72014-03-03 14:31:21 +0000481bool MipsSEDAGToDAGISel::selectIntAddrMSA(SDValue Addr, SDValue &Base,
482 SDValue &Offset) const {
483 if (selectAddrRegImm10(Addr, Base, Offset))
484 return true;
485
486 if (selectAddrDefault(Addr, Base, Offset))
487 return true;
488
489 return false;
490}
491
Daniel Sandersf49dd822013-09-24 13:33:07 +0000492// Select constant vector splats.
493//
494// Returns true and sets Imm if:
495// * MSA is enabled
496// * N is a ISD::BUILD_VECTOR representing a constant splat
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000497bool MipsSEDAGToDAGISel::selectVSplat(SDNode *N, APInt &Imm,
498 unsigned MinSizeInBits) const {
Eric Christopher22405e42014-07-10 17:26:51 +0000499 if (!Subtarget->hasMSA())
Daniel Sandersf49dd822013-09-24 13:33:07 +0000500 return false;
501
502 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N);
503
Craig Topper062a2ba2014-04-25 05:30:21 +0000504 if (!Node)
Daniel Sandersf49dd822013-09-24 13:33:07 +0000505 return false;
506
507 APInt SplatValue, SplatUndef;
508 unsigned SplatBitSize;
509 bool HasAnyUndefs;
510
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000511 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
512 MinSizeInBits, !Subtarget->isLittle()))
Daniel Sandersf49dd822013-09-24 13:33:07 +0000513 return false;
514
Daniel Sandersf49dd822013-09-24 13:33:07 +0000515 Imm = SplatValue;
516
517 return true;
518}
519
520// Select constant vector splats.
521//
522// In addition to the requirements of selectVSplat(), this function returns
523// true and sets Imm if:
524// * The splat value is the same width as the elements of the vector
525// * The splat value fits in an integer with the specified signed-ness and
526// width.
527//
528// This function looks through ISD::BITCAST nodes.
529// TODO: This might not be appropriate for big-endian MSA since BITCAST is
530// sometimes a shuffle in big-endian mode.
531//
532// It's worth noting that this function is not used as part of the selection
533// of ldi.[bhwd] since it does not permit using the wrong-typed ldi.[bhwd]
534// instruction to achieve the desired bit pattern. ldi.[bhwd] is selected in
535// MipsSEDAGToDAGISel::selectNode.
536bool MipsSEDAGToDAGISel::
537selectVSplatCommon(SDValue N, SDValue &Imm, bool Signed,
538 unsigned ImmBitSize) const {
539 APInt ImmValue;
540 EVT EltTy = N->getValueType(0).getVectorElementType();
541
542 if (N->getOpcode() == ISD::BITCAST)
543 N = N->getOperand(0);
544
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000545 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
Daniel Sandersf49dd822013-09-24 13:33:07 +0000546 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000547
Daniel Sandersf49dd822013-09-24 13:33:07 +0000548 if (( Signed && ImmValue.isSignedIntN(ImmBitSize)) ||
549 (!Signed && ImmValue.isIntN(ImmBitSize))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000550 Imm = CurDAG->getTargetConstant(ImmValue, SDLoc(N), EltTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +0000551 return true;
552 }
553 }
554
555 return false;
556}
557
558// Select constant vector splats.
559bool MipsSEDAGToDAGISel::
Daniel Sanders7e51fe12013-09-27 11:48:57 +0000560selectVSplatUimm1(SDValue N, SDValue &Imm) const {
561 return selectVSplatCommon(N, Imm, false, 1);
562}
563
564bool MipsSEDAGToDAGISel::
565selectVSplatUimm2(SDValue N, SDValue &Imm) const {
566 return selectVSplatCommon(N, Imm, false, 2);
567}
568
569bool MipsSEDAGToDAGISel::
Daniel Sandersf49dd822013-09-24 13:33:07 +0000570selectVSplatUimm3(SDValue N, SDValue &Imm) const {
571 return selectVSplatCommon(N, Imm, false, 3);
572}
573
574// Select constant vector splats.
575bool MipsSEDAGToDAGISel::
576selectVSplatUimm4(SDValue N, SDValue &Imm) const {
577 return selectVSplatCommon(N, Imm, false, 4);
578}
579
580// Select constant vector splats.
581bool MipsSEDAGToDAGISel::
582selectVSplatUimm5(SDValue N, SDValue &Imm) const {
583 return selectVSplatCommon(N, Imm, false, 5);
584}
585
586// Select constant vector splats.
587bool MipsSEDAGToDAGISel::
588selectVSplatUimm6(SDValue N, SDValue &Imm) const {
589 return selectVSplatCommon(N, Imm, false, 6);
590}
591
592// Select constant vector splats.
593bool MipsSEDAGToDAGISel::
594selectVSplatUimm8(SDValue N, SDValue &Imm) const {
595 return selectVSplatCommon(N, Imm, false, 8);
596}
597
598// Select constant vector splats.
599bool MipsSEDAGToDAGISel::
600selectVSplatSimm5(SDValue N, SDValue &Imm) const {
601 return selectVSplatCommon(N, Imm, true, 5);
602}
603
604// Select constant vector splats whose value is a power of 2.
605//
606// In addition to the requirements of selectVSplat(), this function returns
607// true and sets Imm if:
608// * The splat value is the same width as the elements of the vector
609// * The splat value is a power of two.
610//
611// This function looks through ISD::BITCAST nodes.
612// TODO: This might not be appropriate for big-endian MSA since BITCAST is
613// sometimes a shuffle in big-endian mode.
614bool MipsSEDAGToDAGISel::selectVSplatUimmPow2(SDValue N, SDValue &Imm) const {
615 APInt ImmValue;
616 EVT EltTy = N->getValueType(0).getVectorElementType();
617
618 if (N->getOpcode() == ISD::BITCAST)
619 N = N->getOperand(0);
620
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000621 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
Daniel Sandersf49dd822013-09-24 13:33:07 +0000622 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
623 int32_t Log2 = ImmValue.exactLogBase2();
624
625 if (Log2 != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000626 Imm = CurDAG->getTargetConstant(Log2, SDLoc(N), EltTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +0000627 return true;
628 }
629 }
630
631 return false;
632}
633
Daniel Sandersd74b1302013-10-30 14:45:14 +0000634// Select constant vector splats whose value only has a consecutive sequence
635// of left-most bits set (e.g. 0b11...1100...00).
636//
637// In addition to the requirements of selectVSplat(), this function returns
638// true and sets Imm if:
639// * The splat value is the same width as the elements of the vector
640// * The splat value is a consecutive sequence of left-most bits.
641//
642// This function looks through ISD::BITCAST nodes.
643// TODO: This might not be appropriate for big-endian MSA since BITCAST is
644// sometimes a shuffle in big-endian mode.
645bool MipsSEDAGToDAGISel::selectVSplatMaskL(SDValue N, SDValue &Imm) const {
646 APInt ImmValue;
647 EVT EltTy = N->getValueType(0).getVectorElementType();
648
649 if (N->getOpcode() == ISD::BITCAST)
650 N = N->getOperand(0);
651
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000652 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
Daniel Sandersd74b1302013-10-30 14:45:14 +0000653 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
654 // Extract the run of set bits starting with bit zero from the bitwise
655 // inverse of ImmValue, and test that the inverse of this is the same
656 // as the original value.
657 if (ImmValue == ~(~ImmValue & ~(~ImmValue + 1))) {
658
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000659 Imm = CurDAG->getTargetConstant(ImmValue.countPopulation(), SDLoc(N),
660 EltTy);
Daniel Sandersd74b1302013-10-30 14:45:14 +0000661 return true;
662 }
663 }
664
665 return false;
666}
667
668// Select constant vector splats whose value only has a consecutive sequence
669// of right-most bits set (e.g. 0b00...0011...11).
670//
671// In addition to the requirements of selectVSplat(), this function returns
672// true and sets Imm if:
673// * The splat value is the same width as the elements of the vector
674// * The splat value is a consecutive sequence of right-most bits.
675//
676// This function looks through ISD::BITCAST nodes.
677// TODO: This might not be appropriate for big-endian MSA since BITCAST is
678// sometimes a shuffle in big-endian mode.
679bool MipsSEDAGToDAGISel::selectVSplatMaskR(SDValue N, SDValue &Imm) const {
680 APInt ImmValue;
681 EVT EltTy = N->getValueType(0).getVectorElementType();
682
683 if (N->getOpcode() == ISD::BITCAST)
684 N = N->getOperand(0);
685
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000686 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
Daniel Sandersd74b1302013-10-30 14:45:14 +0000687 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
688 // Extract the run of set bits starting with bit zero, and test that the
689 // result is the same as the original value
690 if (ImmValue == (ImmValue & ~(ImmValue + 1))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000691 Imm = CurDAG->getTargetConstant(ImmValue.countPopulation(), SDLoc(N),
692 EltTy);
Daniel Sandersd74b1302013-10-30 14:45:14 +0000693 return true;
694 }
695 }
696
697 return false;
698}
699
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000700bool MipsSEDAGToDAGISel::selectVSplatUimmInvPow2(SDValue N,
701 SDValue &Imm) const {
702 APInt ImmValue;
703 EVT EltTy = N->getValueType(0).getVectorElementType();
704
705 if (N->getOpcode() == ISD::BITCAST)
706 N = N->getOperand(0);
707
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000708 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000709 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
710 int32_t Log2 = (~ImmValue).exactLogBase2();
711
712 if (Log2 != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000713 Imm = CurDAG->getTargetConstant(Log2, SDLoc(N), EltTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000714 return true;
715 }
716 }
717
718 return false;
719}
720
Justin Bognereeae7512016-05-13 23:55:59 +0000721bool MipsSEDAGToDAGISel::trySelect(SDNode *Node) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000722 unsigned Opcode = Node->getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000723 SDLoc DL(Node);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000724
725 ///
726 // Instruction Selection not handled by the auto-generated
727 // tablegen selection should be handled here.
728 ///
Akira Hatanaka30a84782013-03-14 18:27:31 +0000729 switch(Opcode) {
730 default: break;
731
Akira Hatanakab8835b82013-03-14 18:39:25 +0000732 case ISD::SUBE: {
733 SDValue InFlag = Node->getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000734 unsigned Opc = Subtarget->isGP64bit() ? Mips::DSUBu : Mips::SUBu;
Justin Bognereeae7512016-05-13 23:55:59 +0000735 selectAddESubE(Opc, InFlag, InFlag.getOperand(0), DL, Node);
736 return true;
Akira Hatanakab8835b82013-03-14 18:39:25 +0000737 }
738
Akira Hatanaka30a84782013-03-14 18:27:31 +0000739 case ISD::ADDE: {
Eric Christopher22405e42014-07-10 17:26:51 +0000740 if (Subtarget->hasDSP()) // Select DSP instructions, ADDSC and ADDWC.
Akira Hatanaka2f088222013-04-13 00:55:41 +0000741 break;
Akira Hatanakab8835b82013-03-14 18:39:25 +0000742 SDValue InFlag = Node->getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000743 unsigned Opc = Subtarget->isGP64bit() ? Mips::DADDu : Mips::ADDu;
Justin Bognereeae7512016-05-13 23:55:59 +0000744 selectAddESubE(Opc, InFlag, InFlag.getValue(0), DL, Node);
745 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000746 }
747
Akira Hatanaka30a84782013-03-14 18:27:31 +0000748 case ISD::ConstantFP: {
749 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
750 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
Eric Christopher22405e42014-07-10 17:26:51 +0000751 if (Subtarget->isGP64bit()) {
Akira Hatanaka040d2252013-03-14 18:33:23 +0000752 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000753 Mips::ZERO_64, MVT::i64);
Justin Bognereeae7512016-05-13 23:55:59 +0000754 ReplaceNode(Node,
755 CurDAG->getMachineNode(Mips::DMTC1, DL, MVT::f64, Zero));
Eric Christopher22405e42014-07-10 17:26:51 +0000756 } else if (Subtarget->isFP64bit()) {
Daniel Sanders08d3cd12013-11-18 13:12:43 +0000757 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
758 Mips::ZERO, MVT::i32);
Justin Bognereeae7512016-05-13 23:55:59 +0000759 ReplaceNode(Node, CurDAG->getMachineNode(Mips::BuildPairF64_64, DL,
760 MVT::f64, Zero, Zero));
Akira Hatanaka30a84782013-03-14 18:27:31 +0000761 } else {
Akira Hatanaka040d2252013-03-14 18:33:23 +0000762 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000763 Mips::ZERO, MVT::i32);
Justin Bognereeae7512016-05-13 23:55:59 +0000764 ReplaceNode(Node, CurDAG->getMachineNode(Mips::BuildPairF64, DL,
765 MVT::f64, Zero, Zero));
Akira Hatanaka30a84782013-03-14 18:27:31 +0000766 }
Justin Bognereeae7512016-05-13 23:55:59 +0000767 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000768 }
769 break;
770 }
771
772 case ISD::Constant: {
773 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node);
774 unsigned Size = CN->getValueSizeInBits(0);
775
776 if (Size == 32)
777 break;
778
779 MipsAnalyzeImmediate AnalyzeImm;
780 int64_t Imm = CN->getSExtValue();
781
782 const MipsAnalyzeImmediate::InstSeq &Seq =
783 AnalyzeImm.Analyze(Imm, Size, false);
784
785 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000786 SDLoc DL(CN);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000787 SDNode *RegOpnd;
788 SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000789 DL, MVT::i64);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000790
791 // The first instruction can be a LUi which is different from other
792 // instructions (ADDiu, ORI and SLL) in that it does not have a register
793 // operand.
794 if (Inst->Opc == Mips::LUi64)
795 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
796 else
797 RegOpnd =
798 CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
799 CurDAG->getRegister(Mips::ZERO_64, MVT::i64),
800 ImmOpnd);
801
802 // The remaining instructions in the sequence are handled here.
803 for (++Inst; Inst != Seq.end(); ++Inst) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000804 ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd), DL,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000805 MVT::i64);
806 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
807 SDValue(RegOpnd, 0), ImmOpnd);
808 }
809
Justin Bognereeae7512016-05-13 23:55:59 +0000810 ReplaceNode(Node, RegOpnd);
811 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000812 }
813
Daniel Sandersf9aa1d12013-08-28 10:26:24 +0000814 case ISD::INTRINSIC_W_CHAIN: {
815 switch (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
816 default:
817 break;
818
819 case Intrinsic::mips_cfcmsa: {
820 SDValue ChainIn = Node->getOperand(0);
821 SDValue RegIdx = Node->getOperand(2);
822 SDValue Reg = CurDAG->getCopyFromReg(ChainIn, DL,
823 getMSACtrlReg(RegIdx), MVT::i32);
Justin Bognereeae7512016-05-13 23:55:59 +0000824 ReplaceNode(Node, Reg.getNode());
825 return true;
Daniel Sandersf9aa1d12013-08-28 10:26:24 +0000826 }
827 }
828 break;
829 }
830
Daniel Sandersba9c8502013-08-28 10:44:47 +0000831 case ISD::INTRINSIC_WO_CHAIN: {
832 switch (cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue()) {
833 default:
834 break;
835
836 case Intrinsic::mips_move_v:
837 // Like an assignment but will always produce a move.v even if
838 // unnecessary.
Justin Bognereeae7512016-05-13 23:55:59 +0000839 ReplaceNode(Node, CurDAG->getMachineNode(Mips::MOVE_V, DL,
840 Node->getValueType(0),
841 Node->getOperand(1)));
842 return true;
Daniel Sandersba9c8502013-08-28 10:44:47 +0000843 }
844 break;
845 }
846
Daniel Sandersf9aa1d12013-08-28 10:26:24 +0000847 case ISD::INTRINSIC_VOID: {
848 switch (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
849 default:
850 break;
851
852 case Intrinsic::mips_ctcmsa: {
853 SDValue ChainIn = Node->getOperand(0);
854 SDValue RegIdx = Node->getOperand(2);
855 SDValue Value = Node->getOperand(3);
856 SDValue ChainOut = CurDAG->getCopyToReg(ChainIn, DL,
857 getMSACtrlReg(RegIdx), Value);
Justin Bognereeae7512016-05-13 23:55:59 +0000858 ReplaceNode(Node, ChainOut.getNode());
859 return true;
Daniel Sandersf9aa1d12013-08-28 10:26:24 +0000860 }
861 }
862 break;
863 }
864
Akira Hatanaka30a84782013-03-14 18:27:31 +0000865 case MipsISD::ThreadPointer: {
Mehdi Amini44ede332015-07-09 02:09:04 +0000866 EVT PtrVT = getTargetLowering()->getPointerTy(CurDAG->getDataLayout());
Akira Hatanaka85ccf232013-08-08 21:37:32 +0000867 unsigned RdhwrOpc, DestReg;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000868
869 if (PtrVT == MVT::i32) {
870 RdhwrOpc = Mips::RDHWR;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000871 DestReg = Mips::V1;
872 } else {
873 RdhwrOpc = Mips::RDHWR64;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000874 DestReg = Mips::V1_64;
875 }
876
877 SDNode *Rdhwr =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000878 CurDAG->getMachineNode(RdhwrOpc, DL,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000879 Node->getValueType(0),
Akira Hatanaka85ccf232013-08-08 21:37:32 +0000880 CurDAG->getRegister(Mips::HWR29, MVT::i32));
Akira Hatanaka040d2252013-03-14 18:33:23 +0000881 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), DL, DestReg,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000882 SDValue(Rdhwr, 0));
Akira Hatanaka040d2252013-03-14 18:33:23 +0000883 SDValue ResNode = CurDAG->getCopyFromReg(Chain, DL, DestReg, PtrVT);
Justin Bognereeae7512016-05-13 23:55:59 +0000884 ReplaceNode(Node, ResNode.getNode());
885 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000886 }
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000887
Daniel Sandersf49dd822013-09-24 13:33:07 +0000888 case ISD::BUILD_VECTOR: {
889 // Select appropriate ldi.[bhwd] instructions for constant splats of
890 // 128-bit when MSA is enabled. Fixup any register class mismatches that
891 // occur as a result.
892 //
893 // This allows the compiler to use a wider range of immediates than would
894 // otherwise be allowed. If, for example, v4i32 could only use ldi.h then
895 // it would not be possible to load { 0x01010101, 0x01010101, 0x01010101,
896 // 0x01010101 } without using a constant pool. This would be sub-optimal
897 // when // 'ldi.b wd, 1' is capable of producing that bit-pattern in the
898 // same set/ of registers. Similarly, ldi.h isn't capable of producing {
899 // 0x00000000, 0x00000001, 0x00000000, 0x00000001 } but 'ldi.d wd, 1' can.
900
901 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Node);
902 APInt SplatValue, SplatUndef;
903 unsigned SplatBitSize;
904 bool HasAnyUndefs;
905 unsigned LdiOp;
906 EVT ResVecTy = BVN->getValueType(0);
907 EVT ViaVecTy;
908
Eric Christopher22405e42014-07-10 17:26:51 +0000909 if (!Subtarget->hasMSA() || !BVN->getValueType(0).is128BitVector())
Justin Bognereeae7512016-05-13 23:55:59 +0000910 return false;
Daniel Sandersf49dd822013-09-24 13:33:07 +0000911
912 if (!BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
913 HasAnyUndefs, 8,
Eric Christopher22405e42014-07-10 17:26:51 +0000914 !Subtarget->isLittle()))
Justin Bognereeae7512016-05-13 23:55:59 +0000915 return false;
Daniel Sandersf49dd822013-09-24 13:33:07 +0000916
917 switch (SplatBitSize) {
918 default:
Justin Bognereeae7512016-05-13 23:55:59 +0000919 return false;
Daniel Sandersf49dd822013-09-24 13:33:07 +0000920 case 8:
921 LdiOp = Mips::LDI_B;
922 ViaVecTy = MVT::v16i8;
923 break;
924 case 16:
925 LdiOp = Mips::LDI_H;
926 ViaVecTy = MVT::v8i16;
927 break;
928 case 32:
929 LdiOp = Mips::LDI_W;
930 ViaVecTy = MVT::v4i32;
931 break;
932 case 64:
933 LdiOp = Mips::LDI_D;
934 ViaVecTy = MVT::v2i64;
935 break;
936 }
937
938 if (!SplatValue.isSignedIntN(10))
Justin Bognereeae7512016-05-13 23:55:59 +0000939 return false;
Daniel Sandersf49dd822013-09-24 13:33:07 +0000940
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000941 SDValue Imm = CurDAG->getTargetConstant(SplatValue, DL,
Daniel Sandersf49dd822013-09-24 13:33:07 +0000942 ViaVecTy.getVectorElementType());
943
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000944 SDNode *Res = CurDAG->getMachineNode(LdiOp, DL, ViaVecTy, Imm);
Daniel Sandersf49dd822013-09-24 13:33:07 +0000945
946 if (ResVecTy != ViaVecTy) {
947 // If LdiOp is writing to a different register class to ResVecTy, then
948 // fix it up here. This COPY_TO_REGCLASS should never cause a move.v
949 // since the source and destination register sets contain the same
950 // registers.
951 const TargetLowering *TLI = getTargetLowering();
952 MVT ResVecTySimple = ResVecTy.getSimpleVT();
953 const TargetRegisterClass *RC = TLI->getRegClassFor(ResVecTySimple);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000954 Res = CurDAG->getMachineNode(Mips::COPY_TO_REGCLASS, DL,
Daniel Sandersf49dd822013-09-24 13:33:07 +0000955 ResVecTy, SDValue(Res, 0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000956 CurDAG->getTargetConstant(RC->getID(), DL,
Daniel Sandersf49dd822013-09-24 13:33:07 +0000957 MVT::i32));
958 }
959
Justin Bognereeae7512016-05-13 23:55:59 +0000960 ReplaceNode(Node, Res);
961 return true;
Daniel Sandersf49dd822013-09-24 13:33:07 +0000962 }
963
Akira Hatanaka30a84782013-03-14 18:27:31 +0000964 }
965
Justin Bognereeae7512016-05-13 23:55:59 +0000966 return false;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000967}
968
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000969bool MipsSEDAGToDAGISel::
970SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
971 std::vector<SDValue> &OutOps) {
972 SDValue Base, Offset;
973
974 switch(ConstraintID) {
975 default:
976 llvm_unreachable("Unexpected asm memory constraint");
977 // All memory constraints can at least accept raw pointers.
978 case InlineAsm::Constraint_i:
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000979 OutOps.push_back(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000980 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000981 return false;
Daniel Sandersc676f2a2015-03-24 15:19:14 +0000982 case InlineAsm::Constraint_m:
983 if (selectAddrRegImm16(Op, Base, Offset)) {
984 OutOps.push_back(Base);
985 OutOps.push_back(Offset);
986 return false;
987 }
988 OutOps.push_back(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000989 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Daniel Sandersc676f2a2015-03-24 15:19:14 +0000990 return false;
Daniel Sanders82df6162015-03-30 13:27:25 +0000991 case InlineAsm::Constraint_R:
992 // The 'R' constraint is supposed to be much more complicated than this.
993 // However, it's becoming less useful due to architectural changes and
994 // ought to be replaced by other constraints such as 'ZC'.
995 // For now, support 9-bit signed offsets which is supportable by all
996 // subtargets for all instructions.
997 if (selectAddrRegImm9(Op, Base, Offset)) {
998 OutOps.push_back(Base);
999 OutOps.push_back(Offset);
1000 return false;
1001 }
1002 OutOps.push_back(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001003 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Daniel Sanders82df6162015-03-30 13:27:25 +00001004 return false;
Daniel Sandersa73d8fe2015-03-24 11:26:34 +00001005 case InlineAsm::Constraint_ZC:
1006 // ZC matches whatever the pref, ll, and sc instructions can handle for the
1007 // given subtarget.
1008 if (Subtarget->inMicroMipsMode()) {
1009 // On microMIPS, they can handle 12-bit offsets.
1010 if (selectAddrRegImm12(Op, Base, Offset)) {
1011 OutOps.push_back(Base);
1012 OutOps.push_back(Offset);
1013 return false;
1014 }
1015 } else if (Subtarget->hasMips32r6()) {
1016 // On MIPS32r6/MIPS64r6, they can only handle 9-bit offsets.
1017 if (selectAddrRegImm9(Op, Base, Offset)) {
1018 OutOps.push_back(Base);
1019 OutOps.push_back(Offset);
1020 return false;
1021 }
1022 } else if (selectAddrRegImm16(Op, Base, Offset)) {
1023 // Prior to MIPS32r6/MIPS64r6, they can handle 16-bit offsets.
1024 OutOps.push_back(Base);
1025 OutOps.push_back(Offset);
1026 return false;
1027 }
1028 // In all cases, 0-bit offsets are acceptable.
1029 OutOps.push_back(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001030 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Daniel Sandersa73d8fe2015-03-24 11:26:34 +00001031 return false;
1032 }
1033 return true;
1034}
1035
Daniel Sanders46fe6552016-07-14 13:25:22 +00001036FunctionPass *llvm::createMipsSEISelDag(MipsTargetMachine &TM,
1037 CodeGenOpt::Level OptLevel) {
1038 return new MipsSEDAGToDAGISel(TM, OptLevel);
Akira Hatanaka30a84782013-03-14 18:27:31 +00001039}