blob: 7de86413de1eb0701b0fc4b3a70837dc1de9a3d4 [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();
50 unsigned Flag = IsDef ? RegState::ImplicitDefine : RegState::Implicit;
51
52 if (Mask & 1)
53 MIB.addReg(Mips::DSPPos, Flag);
54
55 if (Mask & 2)
56 MIB.addReg(Mips::DSPSCount, Flag);
57
58 if (Mask & 4)
59 MIB.addReg(Mips::DSPCarry, Flag);
60
61 if (Mask & 8)
62 MIB.addReg(Mips::DSPOutFlag, Flag);
63
64 if (Mask & 16)
65 MIB.addReg(Mips::DSPCCond, Flag);
66
67 if (Mask & 32)
68 MIB.addReg(Mips::DSPEFI, Flag);
69}
70
Daniel Sandersf9aa1d12013-08-28 10:26:24 +000071unsigned MipsSEDAGToDAGISel::getMSACtrlReg(const SDValue RegIdx) const {
72 switch (cast<ConstantSDNode>(RegIdx)->getZExtValue()) {
73 default:
74 llvm_unreachable("Could not map int to register");
75 case 0: return Mips::MSAIR;
76 case 1: return Mips::MSACSR;
77 case 2: return Mips::MSAAccess;
78 case 3: return Mips::MSASave;
79 case 4: return Mips::MSAModify;
80 case 5: return Mips::MSARequest;
81 case 6: return Mips::MSAMap;
82 case 7: return Mips::MSAUnmap;
83 }
84}
85
Akira Hatanaka040d2252013-03-14 18:33:23 +000086bool MipsSEDAGToDAGISel::replaceUsesWithZeroReg(MachineRegisterInfo *MRI,
Akira Hatanaka30a84782013-03-14 18:27:31 +000087 const MachineInstr& MI) {
88 unsigned DstReg = 0, ZeroReg = 0;
89
90 // Check if MI is "addiu $dst, $zero, 0" or "daddiu $dst, $zero, 0".
91 if ((MI.getOpcode() == Mips::ADDiu) &&
92 (MI.getOperand(1).getReg() == Mips::ZERO) &&
93 (MI.getOperand(2).getImm() == 0)) {
94 DstReg = MI.getOperand(0).getReg();
95 ZeroReg = Mips::ZERO;
96 } else if ((MI.getOpcode() == Mips::DADDiu) &&
97 (MI.getOperand(1).getReg() == Mips::ZERO_64) &&
98 (MI.getOperand(2).getImm() == 0)) {
99 DstReg = MI.getOperand(0).getReg();
100 ZeroReg = Mips::ZERO_64;
101 }
102
103 if (!DstReg)
104 return false;
105
106 // Replace uses with ZeroReg.
107 for (MachineRegisterInfo::use_iterator U = MRI->use_begin(DstReg),
108 E = MRI->use_end(); U != E;) {
Owen Anderson16c6bf42014-03-13 23:12:04 +0000109 MachineOperand &MO = *U;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000110 unsigned OpNo = U.getOperandNo();
111 MachineInstr *MI = MO.getParent();
112 ++U;
113
114 // Do not replace if it is a phi's operand or is tied to def operand.
115 if (MI->isPHI() || MI->isRegTiedToDefOperand(OpNo) || MI->isPseudo())
116 continue;
117
Vasileios Kalintiris2f412682015-10-29 10:17:16 +0000118 // Also, we have to check that the register class of the operand
119 // contains the zero register.
120 if (!MRI->getRegClass(MO.getReg())->contains(ZeroReg))
121 continue;
122
Akira Hatanaka30a84782013-03-14 18:27:31 +0000123 MO.setReg(ZeroReg);
124 }
125
126 return true;
127}
128
Akira Hatanaka040d2252013-03-14 18:33:23 +0000129void MipsSEDAGToDAGISel::initGlobalBaseReg(MachineFunction &MF) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000130 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
131
132 if (!MipsFI->globalBaseRegSet())
133 return;
134
135 MachineBasicBlock &MBB = MF.front();
136 MachineBasicBlock::iterator I = MBB.begin();
137 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +0000138 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Petar Jovanovic28e2b712015-08-28 17:53:26 +0000139 DebugLoc DL;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000140 unsigned V0, V1, GlobalBaseReg = MipsFI->getGlobalBaseReg();
141 const TargetRegisterClass *RC;
Eric Christopherd86af632015-01-29 23:27:45 +0000142 const MipsABIInfo &ABI = static_cast<const MipsTargetMachine &>(TM).getABI();
143 RC = (ABI.IsN64()) ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000144
145 V0 = RegInfo.createVirtualRegister(RC);
146 V1 = RegInfo.createVirtualRegister(RC);
147
Eric Christopherd86af632015-01-29 23:27:45 +0000148 if (ABI.IsN64()) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000149 MF.getRegInfo().addLiveIn(Mips::T9_64);
150 MBB.addLiveIn(Mips::T9_64);
151
152 // lui $v0, %hi(%neg(%gp_rel(fname)))
153 // daddu $v1, $v0, $t9
154 // daddiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
155 const GlobalValue *FName = MF.getFunction();
156 BuildMI(MBB, I, DL, TII.get(Mips::LUi64), V0)
157 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
158 BuildMI(MBB, I, DL, TII.get(Mips::DADDu), V1).addReg(V0)
159 .addReg(Mips::T9_64);
160 BuildMI(MBB, I, DL, TII.get(Mips::DADDiu), GlobalBaseReg).addReg(V1)
161 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
162 return;
163 }
164
165 if (MF.getTarget().getRelocationModel() == Reloc::Static) {
166 // Set global register to __gnu_local_gp.
167 //
168 // lui $v0, %hi(__gnu_local_gp)
169 // addiu $globalbasereg, $v0, %lo(__gnu_local_gp)
170 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
171 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_HI);
172 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V0)
173 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_LO);
174 return;
175 }
176
177 MF.getRegInfo().addLiveIn(Mips::T9);
178 MBB.addLiveIn(Mips::T9);
179
Eric Christopherd86af632015-01-29 23:27:45 +0000180 if (ABI.IsN32()) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000181 // lui $v0, %hi(%neg(%gp_rel(fname)))
182 // addu $v1, $v0, $t9
183 // addiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
184 const GlobalValue *FName = MF.getFunction();
185 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
186 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
187 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), V1).addReg(V0).addReg(Mips::T9);
188 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V1)
189 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
190 return;
191 }
192
Eric Christopherd86af632015-01-29 23:27:45 +0000193 assert(ABI.IsO32());
Akira Hatanaka30a84782013-03-14 18:27:31 +0000194
195 // For O32 ABI, the following instruction sequence is emitted to initialize
196 // the global base register:
197 //
198 // 0. lui $2, %hi(_gp_disp)
199 // 1. addiu $2, $2, %lo(_gp_disp)
200 // 2. addu $globalbasereg, $2, $t9
201 //
202 // We emit only the last instruction here.
203 //
204 // GNU linker requires that the first two instructions appear at the beginning
205 // of a function and no instructions be inserted before or between them.
206 // The two instructions are emitted during lowering to MC layer in order to
207 // avoid any reordering.
208 //
209 // Register $2 (Mips::V0) is added to the list of live-in registers to ensure
210 // the value instruction 1 (addiu) defines is valid when instruction 2 (addu)
211 // reads it.
212 MF.getRegInfo().addLiveIn(Mips::V0);
213 MBB.addLiveIn(Mips::V0);
214 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), GlobalBaseReg)
215 .addReg(Mips::V0).addReg(Mips::T9);
216}
217
Akira Hatanaka040d2252013-03-14 18:33:23 +0000218void MipsSEDAGToDAGISel::processFunctionAfterISel(MachineFunction &MF) {
219 initGlobalBaseReg(MF);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000220
221 MachineRegisterInfo *MRI = &MF.getRegInfo();
222
Vasileios Kalintiris36311392016-04-15 20:18:48 +0000223 for (auto &MBB: MF) {
224 for (auto &MI: MBB) {
225 switch (MI.getOpcode()) {
226 case Mips::RDDSP:
227 addDSPCtrlRegOperands(false, MI, MF);
228 break;
229 case Mips::WRDSP:
230 addDSPCtrlRegOperands(true, MI, MF);
231 break;
232 default:
233 replaceUsesWithZeroReg(MRI, MI);
234 }
Akira Hatanakae86bd4f2013-05-03 18:37:49 +0000235 }
Vasileios Kalintiris36311392016-04-15 20:18:48 +0000236 }
Akira Hatanaka30a84782013-03-14 18:27:31 +0000237}
238
Justin Bognereeae7512016-05-13 23:55:59 +0000239void MipsSEDAGToDAGISel::selectAddESubE(unsigned MOp, SDValue InFlag,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000240 SDValue CmpLHS, SDLoc DL,
Akira Hatanakab8835b82013-03-14 18:39:25 +0000241 SDNode *Node) const {
242 unsigned Opc = InFlag.getOpcode(); (void)Opc;
243
244 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
245 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
246 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
247
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000248 unsigned SLTuOp = Mips::SLTu, ADDuOp = Mips::ADDu;
249 if (Subtarget->isGP64bit()) {
250 SLTuOp = Mips::SLTu64;
251 ADDuOp = Mips::DADDu;
252 }
253
Akira Hatanakab8835b82013-03-14 18:39:25 +0000254 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
255 SDValue LHS = Node->getOperand(0), RHS = Node->getOperand(1);
256 EVT VT = LHS.getValueType();
257
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000258 SDNode *Carry = CurDAG->getMachineNode(SLTuOp, DL, VT, Ops);
259
260 if (Subtarget->isGP64bit()) {
261 // On 64-bit targets, sltu produces an i64 but our backend currently says
262 // that SLTu64 produces an i32. We need to fix this in the long run but for
263 // now, just make the DAG type-correct by asserting the upper bits are zero.
264 Carry = CurDAG->getMachineNode(Mips::SUBREG_TO_REG, DL, VT,
265 CurDAG->getTargetConstant(0, DL, VT),
266 SDValue(Carry, 0),
267 CurDAG->getTargetConstant(Mips::sub_32, DL,
268 VT));
269 }
270
Vasileios Kalintiris18581f12015-02-27 09:01:39 +0000271 // Generate a second addition only if we know that RHS is not a
272 // constant-zero node.
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000273 SDNode *AddCarry = Carry;
Vasileios Kalintiris18581f12015-02-27 09:01:39 +0000274 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
275 if (!C || C->getZExtValue())
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000276 AddCarry = CurDAG->getMachineNode(ADDuOp, DL, VT, SDValue(Carry, 0), RHS);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000277
Justin Bognereeae7512016-05-13 23:55:59 +0000278 CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue, LHS, SDValue(AddCarry, 0));
Akira Hatanakab8835b82013-03-14 18:39:25 +0000279}
280
Daniel Sandersfa961d72014-03-03 14:31:21 +0000281/// Match frameindex
282bool MipsSEDAGToDAGISel::selectAddrFrameIndex(SDValue Addr, SDValue &Base,
283 SDValue &Offset) const {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000284 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Daniel Sandersfa961d72014-03-03 14:31:21 +0000285 EVT ValTy = Addr.getValueType();
286
Akira Hatanaka30a84782013-03-14 18:27:31 +0000287 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000288 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), ValTy);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000289 return true;
290 }
Daniel Sandersfa961d72014-03-03 14:31:21 +0000291 return false;
292}
293
294/// Match frameindex+offset and frameindex|offset
295bool MipsSEDAGToDAGISel::selectAddrFrameIndexOffset(SDValue Addr, SDValue &Base,
296 SDValue &Offset,
297 unsigned OffsetBits) const {
298 if (CurDAG->isBaseWithConstantOffset(Addr)) {
299 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
300 if (isIntN(OffsetBits, CN->getSExtValue())) {
301 EVT ValTy = Addr.getValueType();
302
303 // If the first operand is a FI, get the TargetFI Node
304 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
305 (Addr.getOperand(0)))
306 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
307 else
308 Base = Addr.getOperand(0);
309
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000310 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), SDLoc(Addr),
311 ValTy);
Daniel Sandersfa961d72014-03-03 14:31:21 +0000312 return true;
313 }
314 }
315 return false;
316}
317
318/// ComplexPattern used on MipsInstrInfo
319/// Used on Mips Load/Store instructions
320bool MipsSEDAGToDAGISel::selectAddrRegImm(SDValue Addr, SDValue &Base,
321 SDValue &Offset) const {
322 // if Address is FI, get the TargetFrameIndex.
323 if (selectAddrFrameIndex(Addr, Base, Offset))
324 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000325
326 // on PIC code Load GA
327 if (Addr.getOpcode() == MipsISD::Wrapper) {
328 Base = Addr.getOperand(0);
329 Offset = Addr.getOperand(1);
330 return true;
331 }
332
333 if (TM.getRelocationModel() != Reloc::PIC_) {
334 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
335 Addr.getOpcode() == ISD::TargetGlobalAddress))
336 return false;
337 }
338
339 // Addresses of the form FI+const or FI|const
Daniel Sandersfa961d72014-03-03 14:31:21 +0000340 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 16))
341 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000342
343 // Operand is a result from an ADD.
344 if (Addr.getOpcode() == ISD::ADD) {
345 // When loading from constant pools, load the lower address part in
346 // the instruction itself. Example, instead of:
347 // lui $2, %hi($CPI1_0)
348 // addiu $2, $2, %lo($CPI1_0)
349 // lwc1 $f0, 0($2)
350 // Generate:
351 // lui $2, %hi($CPI1_0)
352 // lwc1 $f0, %lo($CPI1_0)($2)
353 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo ||
354 Addr.getOperand(1).getOpcode() == MipsISD::GPRel) {
355 SDValue Opnd0 = Addr.getOperand(1).getOperand(0);
356 if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) ||
357 isa<JumpTableSDNode>(Opnd0)) {
358 Base = Addr.getOperand(0);
359 Offset = Opnd0;
360 return true;
361 }
362 }
363 }
364
365 return false;
366}
367
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000368/// ComplexPattern used on MipsInstrInfo
369/// Used on Mips Load/Store instructions
370bool MipsSEDAGToDAGISel::selectAddrRegReg(SDValue Addr, SDValue &Base,
371 SDValue &Offset) const {
372 // Operand is a result from an ADD.
373 if (Addr.getOpcode() == ISD::ADD) {
374 Base = Addr.getOperand(0);
375 Offset = Addr.getOperand(1);
376 return true;
377 }
378
379 return false;
380}
381
Akira Hatanaka30a84782013-03-14 18:27:31 +0000382bool MipsSEDAGToDAGISel::selectAddrDefault(SDValue Addr, SDValue &Base,
383 SDValue &Offset) const {
384 Base = Addr;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000385 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), Addr.getValueType());
Akira Hatanaka30a84782013-03-14 18:27:31 +0000386 return true;
387}
388
389bool MipsSEDAGToDAGISel::selectIntAddr(SDValue Addr, SDValue &Base,
390 SDValue &Offset) const {
391 return selectAddrRegImm(Addr, Base, Offset) ||
392 selectAddrDefault(Addr, Base, Offset);
393}
394
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000395bool MipsSEDAGToDAGISel::selectAddrRegImm9(SDValue Addr, SDValue &Base,
396 SDValue &Offset) const {
397 if (selectAddrFrameIndex(Addr, Base, Offset))
398 return true;
399
400 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 9))
401 return true;
402
403 return false;
404}
405
Daniel Sandersfa961d72014-03-03 14:31:21 +0000406bool MipsSEDAGToDAGISel::selectAddrRegImm10(SDValue Addr, SDValue &Base,
407 SDValue &Offset) const {
408 if (selectAddrFrameIndex(Addr, Base, Offset))
409 return true;
410
411 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10))
412 return true;
413
414 return false;
415}
416
Jack Carter97700972013-08-13 20:19:16 +0000417/// Used on microMIPS Load/Store unaligned instructions (12-bit offset)
418bool MipsSEDAGToDAGISel::selectAddrRegImm12(SDValue Addr, SDValue &Base,
419 SDValue &Offset) const {
Daniel Sandersfa961d72014-03-03 14:31:21 +0000420 if (selectAddrFrameIndex(Addr, Base, Offset))
421 return true;
Jack Carter97700972013-08-13 20:19:16 +0000422
Daniel Sandersfa961d72014-03-03 14:31:21 +0000423 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 12))
424 return true;
Jack Carter97700972013-08-13 20:19:16 +0000425
426 return false;
427}
428
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000429bool MipsSEDAGToDAGISel::selectAddrRegImm16(SDValue Addr, SDValue &Base,
430 SDValue &Offset) const {
431 if (selectAddrFrameIndex(Addr, Base, Offset))
432 return true;
433
434 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 16))
435 return true;
436
437 return false;
438}
439
Jack Carter97700972013-08-13 20:19:16 +0000440bool MipsSEDAGToDAGISel::selectIntAddrMM(SDValue Addr, SDValue &Base,
441 SDValue &Offset) const {
442 return selectAddrRegImm12(Addr, Base, Offset) ||
443 selectAddrDefault(Addr, Base, Offset);
444}
445
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000446bool MipsSEDAGToDAGISel::selectIntAddrLSL2MM(SDValue Addr, SDValue &Base,
447 SDValue &Offset) const {
448 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 7)) {
Vasileios Kalintiris99eeb8a2015-02-13 19:14:22 +0000449 if (isa<FrameIndexSDNode>(Base))
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000450 return false;
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000451
Vasileios Kalintiris99eeb8a2015-02-13 19:14:22 +0000452 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Offset)) {
453 unsigned CnstOff = CN->getZExtValue();
454 return (CnstOff == (CnstOff & 0x3c));
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000455 }
Vasileios Kalintiris99eeb8a2015-02-13 19:14:22 +0000456
457 return false;
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000458 }
459
460 // For all other cases where "lw" would be selected, don't select "lw16"
461 // because it would result in additional instructions to prepare operands.
462 if (selectAddrRegImm(Addr, Base, Offset))
463 return false;
464
465 return selectAddrDefault(Addr, Base, Offset);
466}
467
Daniel Sandersfa961d72014-03-03 14:31:21 +0000468bool MipsSEDAGToDAGISel::selectIntAddrMSA(SDValue Addr, SDValue &Base,
469 SDValue &Offset) const {
470 if (selectAddrRegImm10(Addr, Base, Offset))
471 return true;
472
473 if (selectAddrDefault(Addr, Base, Offset))
474 return true;
475
476 return false;
477}
478
Daniel Sandersf49dd822013-09-24 13:33:07 +0000479// Select constant vector splats.
480//
481// Returns true and sets Imm if:
482// * MSA is enabled
483// * N is a ISD::BUILD_VECTOR representing a constant splat
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000484bool MipsSEDAGToDAGISel::selectVSplat(SDNode *N, APInt &Imm,
485 unsigned MinSizeInBits) const {
Eric Christopher22405e42014-07-10 17:26:51 +0000486 if (!Subtarget->hasMSA())
Daniel Sandersf49dd822013-09-24 13:33:07 +0000487 return false;
488
489 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N);
490
Craig Topper062a2ba2014-04-25 05:30:21 +0000491 if (!Node)
Daniel Sandersf49dd822013-09-24 13:33:07 +0000492 return false;
493
494 APInt SplatValue, SplatUndef;
495 unsigned SplatBitSize;
496 bool HasAnyUndefs;
497
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000498 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
499 MinSizeInBits, !Subtarget->isLittle()))
Daniel Sandersf49dd822013-09-24 13:33:07 +0000500 return false;
501
Daniel Sandersf49dd822013-09-24 13:33:07 +0000502 Imm = SplatValue;
503
504 return true;
505}
506
507// Select constant vector splats.
508//
509// In addition to the requirements of selectVSplat(), this function returns
510// true and sets Imm if:
511// * The splat value is the same width as the elements of the vector
512// * The splat value fits in an integer with the specified signed-ness and
513// width.
514//
515// This function looks through ISD::BITCAST nodes.
516// TODO: This might not be appropriate for big-endian MSA since BITCAST is
517// sometimes a shuffle in big-endian mode.
518//
519// It's worth noting that this function is not used as part of the selection
520// of ldi.[bhwd] since it does not permit using the wrong-typed ldi.[bhwd]
521// instruction to achieve the desired bit pattern. ldi.[bhwd] is selected in
522// MipsSEDAGToDAGISel::selectNode.
523bool MipsSEDAGToDAGISel::
524selectVSplatCommon(SDValue N, SDValue &Imm, bool Signed,
525 unsigned ImmBitSize) const {
526 APInt ImmValue;
527 EVT EltTy = N->getValueType(0).getVectorElementType();
528
529 if (N->getOpcode() == ISD::BITCAST)
530 N = N->getOperand(0);
531
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000532 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
Daniel Sandersf49dd822013-09-24 13:33:07 +0000533 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000534
Daniel Sandersf49dd822013-09-24 13:33:07 +0000535 if (( Signed && ImmValue.isSignedIntN(ImmBitSize)) ||
536 (!Signed && ImmValue.isIntN(ImmBitSize))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000537 Imm = CurDAG->getTargetConstant(ImmValue, SDLoc(N), EltTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +0000538 return true;
539 }
540 }
541
542 return false;
543}
544
545// Select constant vector splats.
546bool MipsSEDAGToDAGISel::
Daniel Sanders7e51fe12013-09-27 11:48:57 +0000547selectVSplatUimm1(SDValue N, SDValue &Imm) const {
548 return selectVSplatCommon(N, Imm, false, 1);
549}
550
551bool MipsSEDAGToDAGISel::
552selectVSplatUimm2(SDValue N, SDValue &Imm) const {
553 return selectVSplatCommon(N, Imm, false, 2);
554}
555
556bool MipsSEDAGToDAGISel::
Daniel Sandersf49dd822013-09-24 13:33:07 +0000557selectVSplatUimm3(SDValue N, SDValue &Imm) const {
558 return selectVSplatCommon(N, Imm, false, 3);
559}
560
561// Select constant vector splats.
562bool MipsSEDAGToDAGISel::
563selectVSplatUimm4(SDValue N, SDValue &Imm) const {
564 return selectVSplatCommon(N, Imm, false, 4);
565}
566
567// Select constant vector splats.
568bool MipsSEDAGToDAGISel::
569selectVSplatUimm5(SDValue N, SDValue &Imm) const {
570 return selectVSplatCommon(N, Imm, false, 5);
571}
572
573// Select constant vector splats.
574bool MipsSEDAGToDAGISel::
575selectVSplatUimm6(SDValue N, SDValue &Imm) const {
576 return selectVSplatCommon(N, Imm, false, 6);
577}
578
579// Select constant vector splats.
580bool MipsSEDAGToDAGISel::
581selectVSplatUimm8(SDValue N, SDValue &Imm) const {
582 return selectVSplatCommon(N, Imm, false, 8);
583}
584
585// Select constant vector splats.
586bool MipsSEDAGToDAGISel::
587selectVSplatSimm5(SDValue N, SDValue &Imm) const {
588 return selectVSplatCommon(N, Imm, true, 5);
589}
590
591// Select constant vector splats whose value is a power of 2.
592//
593// In addition to the requirements of selectVSplat(), this function returns
594// true and sets Imm if:
595// * The splat value is the same width as the elements of the vector
596// * The splat value is a power of two.
597//
598// This function looks through ISD::BITCAST nodes.
599// TODO: This might not be appropriate for big-endian MSA since BITCAST is
600// sometimes a shuffle in big-endian mode.
601bool MipsSEDAGToDAGISel::selectVSplatUimmPow2(SDValue N, SDValue &Imm) const {
602 APInt ImmValue;
603 EVT EltTy = N->getValueType(0).getVectorElementType();
604
605 if (N->getOpcode() == ISD::BITCAST)
606 N = N->getOperand(0);
607
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000608 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
Daniel Sandersf49dd822013-09-24 13:33:07 +0000609 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
610 int32_t Log2 = ImmValue.exactLogBase2();
611
612 if (Log2 != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000613 Imm = CurDAG->getTargetConstant(Log2, SDLoc(N), EltTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +0000614 return true;
615 }
616 }
617
618 return false;
619}
620
Daniel Sandersd74b1302013-10-30 14:45:14 +0000621// Select constant vector splats whose value only has a consecutive sequence
622// of left-most bits set (e.g. 0b11...1100...00).
623//
624// In addition to the requirements of selectVSplat(), this function returns
625// true and sets Imm if:
626// * The splat value is the same width as the elements of the vector
627// * The splat value is a consecutive sequence of left-most bits.
628//
629// This function looks through ISD::BITCAST nodes.
630// TODO: This might not be appropriate for big-endian MSA since BITCAST is
631// sometimes a shuffle in big-endian mode.
632bool MipsSEDAGToDAGISel::selectVSplatMaskL(SDValue N, SDValue &Imm) const {
633 APInt ImmValue;
634 EVT EltTy = N->getValueType(0).getVectorElementType();
635
636 if (N->getOpcode() == ISD::BITCAST)
637 N = N->getOperand(0);
638
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000639 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
Daniel Sandersd74b1302013-10-30 14:45:14 +0000640 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
641 // Extract the run of set bits starting with bit zero from the bitwise
642 // inverse of ImmValue, and test that the inverse of this is the same
643 // as the original value.
644 if (ImmValue == ~(~ImmValue & ~(~ImmValue + 1))) {
645
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000646 Imm = CurDAG->getTargetConstant(ImmValue.countPopulation(), SDLoc(N),
647 EltTy);
Daniel Sandersd74b1302013-10-30 14:45:14 +0000648 return true;
649 }
650 }
651
652 return false;
653}
654
655// Select constant vector splats whose value only has a consecutive sequence
656// of right-most bits set (e.g. 0b00...0011...11).
657//
658// In addition to the requirements of selectVSplat(), this function returns
659// true and sets Imm if:
660// * The splat value is the same width as the elements of the vector
661// * The splat value is a consecutive sequence of right-most bits.
662//
663// This function looks through ISD::BITCAST nodes.
664// TODO: This might not be appropriate for big-endian MSA since BITCAST is
665// sometimes a shuffle in big-endian mode.
666bool MipsSEDAGToDAGISel::selectVSplatMaskR(SDValue N, SDValue &Imm) const {
667 APInt ImmValue;
668 EVT EltTy = N->getValueType(0).getVectorElementType();
669
670 if (N->getOpcode() == ISD::BITCAST)
671 N = N->getOperand(0);
672
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000673 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
Daniel Sandersd74b1302013-10-30 14:45:14 +0000674 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
675 // Extract the run of set bits starting with bit zero, and test that the
676 // result is the same as the original value
677 if (ImmValue == (ImmValue & ~(ImmValue + 1))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000678 Imm = CurDAG->getTargetConstant(ImmValue.countPopulation(), SDLoc(N),
679 EltTy);
Daniel Sandersd74b1302013-10-30 14:45:14 +0000680 return true;
681 }
682 }
683
684 return false;
685}
686
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000687bool MipsSEDAGToDAGISel::selectVSplatUimmInvPow2(SDValue N,
688 SDValue &Imm) const {
689 APInt ImmValue;
690 EVT EltTy = N->getValueType(0).getVectorElementType();
691
692 if (N->getOpcode() == ISD::BITCAST)
693 N = N->getOperand(0);
694
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000695 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000696 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
697 int32_t Log2 = (~ImmValue).exactLogBase2();
698
699 if (Log2 != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000700 Imm = CurDAG->getTargetConstant(Log2, SDLoc(N), EltTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000701 return true;
702 }
703 }
704
705 return false;
706}
707
Justin Bognereeae7512016-05-13 23:55:59 +0000708bool MipsSEDAGToDAGISel::trySelect(SDNode *Node) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000709 unsigned Opcode = Node->getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000710 SDLoc DL(Node);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000711
712 ///
713 // Instruction Selection not handled by the auto-generated
714 // tablegen selection should be handled here.
715 ///
Akira Hatanaka30a84782013-03-14 18:27:31 +0000716 switch(Opcode) {
717 default: break;
718
Akira Hatanakab8835b82013-03-14 18:39:25 +0000719 case ISD::SUBE: {
720 SDValue InFlag = Node->getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000721 unsigned Opc = Subtarget->isGP64bit() ? Mips::DSUBu : Mips::SUBu;
Justin Bognereeae7512016-05-13 23:55:59 +0000722 selectAddESubE(Opc, InFlag, InFlag.getOperand(0), DL, Node);
723 return true;
Akira Hatanakab8835b82013-03-14 18:39:25 +0000724 }
725
Akira Hatanaka30a84782013-03-14 18:27:31 +0000726 case ISD::ADDE: {
Eric Christopher22405e42014-07-10 17:26:51 +0000727 if (Subtarget->hasDSP()) // Select DSP instructions, ADDSC and ADDWC.
Akira Hatanaka2f088222013-04-13 00:55:41 +0000728 break;
Akira Hatanakab8835b82013-03-14 18:39:25 +0000729 SDValue InFlag = Node->getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000730 unsigned Opc = Subtarget->isGP64bit() ? Mips::DADDu : Mips::ADDu;
Justin Bognereeae7512016-05-13 23:55:59 +0000731 selectAddESubE(Opc, InFlag, InFlag.getValue(0), DL, Node);
732 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000733 }
734
Akira Hatanaka30a84782013-03-14 18:27:31 +0000735 case ISD::ConstantFP: {
736 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
737 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
Eric Christopher22405e42014-07-10 17:26:51 +0000738 if (Subtarget->isGP64bit()) {
Akira Hatanaka040d2252013-03-14 18:33:23 +0000739 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000740 Mips::ZERO_64, MVT::i64);
Justin Bognereeae7512016-05-13 23:55:59 +0000741 ReplaceNode(Node,
742 CurDAG->getMachineNode(Mips::DMTC1, DL, MVT::f64, Zero));
Eric Christopher22405e42014-07-10 17:26:51 +0000743 } else if (Subtarget->isFP64bit()) {
Daniel Sanders08d3cd12013-11-18 13:12:43 +0000744 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
745 Mips::ZERO, MVT::i32);
Justin Bognereeae7512016-05-13 23:55:59 +0000746 ReplaceNode(Node, CurDAG->getMachineNode(Mips::BuildPairF64_64, DL,
747 MVT::f64, Zero, Zero));
Akira Hatanaka30a84782013-03-14 18:27:31 +0000748 } else {
Akira Hatanaka040d2252013-03-14 18:33:23 +0000749 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000750 Mips::ZERO, MVT::i32);
Justin Bognereeae7512016-05-13 23:55:59 +0000751 ReplaceNode(Node, CurDAG->getMachineNode(Mips::BuildPairF64, DL,
752 MVT::f64, Zero, Zero));
Akira Hatanaka30a84782013-03-14 18:27:31 +0000753 }
Justin Bognereeae7512016-05-13 23:55:59 +0000754 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000755 }
756 break;
757 }
758
759 case ISD::Constant: {
760 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node);
761 unsigned Size = CN->getValueSizeInBits(0);
762
763 if (Size == 32)
764 break;
765
766 MipsAnalyzeImmediate AnalyzeImm;
767 int64_t Imm = CN->getSExtValue();
768
769 const MipsAnalyzeImmediate::InstSeq &Seq =
770 AnalyzeImm.Analyze(Imm, Size, false);
771
772 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000773 SDLoc DL(CN);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000774 SDNode *RegOpnd;
775 SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000776 DL, MVT::i64);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000777
778 // The first instruction can be a LUi which is different from other
779 // instructions (ADDiu, ORI and SLL) in that it does not have a register
780 // operand.
781 if (Inst->Opc == Mips::LUi64)
782 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
783 else
784 RegOpnd =
785 CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
786 CurDAG->getRegister(Mips::ZERO_64, MVT::i64),
787 ImmOpnd);
788
789 // The remaining instructions in the sequence are handled here.
790 for (++Inst; Inst != Seq.end(); ++Inst) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000791 ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd), DL,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000792 MVT::i64);
793 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
794 SDValue(RegOpnd, 0), ImmOpnd);
795 }
796
Justin Bognereeae7512016-05-13 23:55:59 +0000797 ReplaceNode(Node, RegOpnd);
798 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000799 }
800
Daniel Sandersf9aa1d12013-08-28 10:26:24 +0000801 case ISD::INTRINSIC_W_CHAIN: {
802 switch (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
803 default:
804 break;
805
806 case Intrinsic::mips_cfcmsa: {
807 SDValue ChainIn = Node->getOperand(0);
808 SDValue RegIdx = Node->getOperand(2);
809 SDValue Reg = CurDAG->getCopyFromReg(ChainIn, DL,
810 getMSACtrlReg(RegIdx), MVT::i32);
Justin Bognereeae7512016-05-13 23:55:59 +0000811 ReplaceNode(Node, Reg.getNode());
812 return true;
Daniel Sandersf9aa1d12013-08-28 10:26:24 +0000813 }
814 }
815 break;
816 }
817
Daniel Sandersba9c8502013-08-28 10:44:47 +0000818 case ISD::INTRINSIC_WO_CHAIN: {
819 switch (cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue()) {
820 default:
821 break;
822
823 case Intrinsic::mips_move_v:
824 // Like an assignment but will always produce a move.v even if
825 // unnecessary.
Justin Bognereeae7512016-05-13 23:55:59 +0000826 ReplaceNode(Node, CurDAG->getMachineNode(Mips::MOVE_V, DL,
827 Node->getValueType(0),
828 Node->getOperand(1)));
829 return true;
Daniel Sandersba9c8502013-08-28 10:44:47 +0000830 }
831 break;
832 }
833
Daniel Sandersf9aa1d12013-08-28 10:26:24 +0000834 case ISD::INTRINSIC_VOID: {
835 switch (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
836 default:
837 break;
838
839 case Intrinsic::mips_ctcmsa: {
840 SDValue ChainIn = Node->getOperand(0);
841 SDValue RegIdx = Node->getOperand(2);
842 SDValue Value = Node->getOperand(3);
843 SDValue ChainOut = CurDAG->getCopyToReg(ChainIn, DL,
844 getMSACtrlReg(RegIdx), Value);
Justin Bognereeae7512016-05-13 23:55:59 +0000845 ReplaceNode(Node, ChainOut.getNode());
846 return true;
Daniel Sandersf9aa1d12013-08-28 10:26:24 +0000847 }
848 }
849 break;
850 }
851
Akira Hatanaka30a84782013-03-14 18:27:31 +0000852 case MipsISD::ThreadPointer: {
Mehdi Amini44ede332015-07-09 02:09:04 +0000853 EVT PtrVT = getTargetLowering()->getPointerTy(CurDAG->getDataLayout());
Akira Hatanaka85ccf232013-08-08 21:37:32 +0000854 unsigned RdhwrOpc, DestReg;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000855
856 if (PtrVT == MVT::i32) {
857 RdhwrOpc = Mips::RDHWR;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000858 DestReg = Mips::V1;
859 } else {
860 RdhwrOpc = Mips::RDHWR64;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000861 DestReg = Mips::V1_64;
862 }
863
864 SDNode *Rdhwr =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000865 CurDAG->getMachineNode(RdhwrOpc, DL,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000866 Node->getValueType(0),
Akira Hatanaka85ccf232013-08-08 21:37:32 +0000867 CurDAG->getRegister(Mips::HWR29, MVT::i32));
Akira Hatanaka040d2252013-03-14 18:33:23 +0000868 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), DL, DestReg,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000869 SDValue(Rdhwr, 0));
Akira Hatanaka040d2252013-03-14 18:33:23 +0000870 SDValue ResNode = CurDAG->getCopyFromReg(Chain, DL, DestReg, PtrVT);
Justin Bognereeae7512016-05-13 23:55:59 +0000871 ReplaceNode(Node, ResNode.getNode());
872 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000873 }
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000874
Daniel Sandersf49dd822013-09-24 13:33:07 +0000875 case ISD::BUILD_VECTOR: {
876 // Select appropriate ldi.[bhwd] instructions for constant splats of
877 // 128-bit when MSA is enabled. Fixup any register class mismatches that
878 // occur as a result.
879 //
880 // This allows the compiler to use a wider range of immediates than would
881 // otherwise be allowed. If, for example, v4i32 could only use ldi.h then
882 // it would not be possible to load { 0x01010101, 0x01010101, 0x01010101,
883 // 0x01010101 } without using a constant pool. This would be sub-optimal
884 // when // 'ldi.b wd, 1' is capable of producing that bit-pattern in the
885 // same set/ of registers. Similarly, ldi.h isn't capable of producing {
886 // 0x00000000, 0x00000001, 0x00000000, 0x00000001 } but 'ldi.d wd, 1' can.
887
888 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Node);
889 APInt SplatValue, SplatUndef;
890 unsigned SplatBitSize;
891 bool HasAnyUndefs;
892 unsigned LdiOp;
893 EVT ResVecTy = BVN->getValueType(0);
894 EVT ViaVecTy;
895
Eric Christopher22405e42014-07-10 17:26:51 +0000896 if (!Subtarget->hasMSA() || !BVN->getValueType(0).is128BitVector())
Justin Bognereeae7512016-05-13 23:55:59 +0000897 return false;
Daniel Sandersf49dd822013-09-24 13:33:07 +0000898
899 if (!BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
900 HasAnyUndefs, 8,
Eric Christopher22405e42014-07-10 17:26:51 +0000901 !Subtarget->isLittle()))
Justin Bognereeae7512016-05-13 23:55:59 +0000902 return false;
Daniel Sandersf49dd822013-09-24 13:33:07 +0000903
904 switch (SplatBitSize) {
905 default:
Justin Bognereeae7512016-05-13 23:55:59 +0000906 return false;
Daniel Sandersf49dd822013-09-24 13:33:07 +0000907 case 8:
908 LdiOp = Mips::LDI_B;
909 ViaVecTy = MVT::v16i8;
910 break;
911 case 16:
912 LdiOp = Mips::LDI_H;
913 ViaVecTy = MVT::v8i16;
914 break;
915 case 32:
916 LdiOp = Mips::LDI_W;
917 ViaVecTy = MVT::v4i32;
918 break;
919 case 64:
920 LdiOp = Mips::LDI_D;
921 ViaVecTy = MVT::v2i64;
922 break;
923 }
924
925 if (!SplatValue.isSignedIntN(10))
Justin Bognereeae7512016-05-13 23:55:59 +0000926 return false;
Daniel Sandersf49dd822013-09-24 13:33:07 +0000927
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000928 SDValue Imm = CurDAG->getTargetConstant(SplatValue, DL,
Daniel Sandersf49dd822013-09-24 13:33:07 +0000929 ViaVecTy.getVectorElementType());
930
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000931 SDNode *Res = CurDAG->getMachineNode(LdiOp, DL, ViaVecTy, Imm);
Daniel Sandersf49dd822013-09-24 13:33:07 +0000932
933 if (ResVecTy != ViaVecTy) {
934 // If LdiOp is writing to a different register class to ResVecTy, then
935 // fix it up here. This COPY_TO_REGCLASS should never cause a move.v
936 // since the source and destination register sets contain the same
937 // registers.
938 const TargetLowering *TLI = getTargetLowering();
939 MVT ResVecTySimple = ResVecTy.getSimpleVT();
940 const TargetRegisterClass *RC = TLI->getRegClassFor(ResVecTySimple);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000941 Res = CurDAG->getMachineNode(Mips::COPY_TO_REGCLASS, DL,
Daniel Sandersf49dd822013-09-24 13:33:07 +0000942 ResVecTy, SDValue(Res, 0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000943 CurDAG->getTargetConstant(RC->getID(), DL,
Daniel Sandersf49dd822013-09-24 13:33:07 +0000944 MVT::i32));
945 }
946
Justin Bognereeae7512016-05-13 23:55:59 +0000947 ReplaceNode(Node, Res);
948 return true;
Daniel Sandersf49dd822013-09-24 13:33:07 +0000949 }
950
Akira Hatanaka30a84782013-03-14 18:27:31 +0000951 }
952
Justin Bognereeae7512016-05-13 23:55:59 +0000953 return false;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000954}
955
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000956bool MipsSEDAGToDAGISel::
957SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
958 std::vector<SDValue> &OutOps) {
959 SDValue Base, Offset;
960
961 switch(ConstraintID) {
962 default:
963 llvm_unreachable("Unexpected asm memory constraint");
964 // All memory constraints can at least accept raw pointers.
965 case InlineAsm::Constraint_i:
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000966 OutOps.push_back(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000967 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000968 return false;
Daniel Sandersc676f2a2015-03-24 15:19:14 +0000969 case InlineAsm::Constraint_m:
970 if (selectAddrRegImm16(Op, Base, Offset)) {
971 OutOps.push_back(Base);
972 OutOps.push_back(Offset);
973 return false;
974 }
975 OutOps.push_back(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000976 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Daniel Sandersc676f2a2015-03-24 15:19:14 +0000977 return false;
Daniel Sanders82df6162015-03-30 13:27:25 +0000978 case InlineAsm::Constraint_R:
979 // The 'R' constraint is supposed to be much more complicated than this.
980 // However, it's becoming less useful due to architectural changes and
981 // ought to be replaced by other constraints such as 'ZC'.
982 // For now, support 9-bit signed offsets which is supportable by all
983 // subtargets for all instructions.
984 if (selectAddrRegImm9(Op, Base, Offset)) {
985 OutOps.push_back(Base);
986 OutOps.push_back(Offset);
987 return false;
988 }
989 OutOps.push_back(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000990 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Daniel Sanders82df6162015-03-30 13:27:25 +0000991 return false;
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000992 case InlineAsm::Constraint_ZC:
993 // ZC matches whatever the pref, ll, and sc instructions can handle for the
994 // given subtarget.
995 if (Subtarget->inMicroMipsMode()) {
996 // On microMIPS, they can handle 12-bit offsets.
997 if (selectAddrRegImm12(Op, Base, Offset)) {
998 OutOps.push_back(Base);
999 OutOps.push_back(Offset);
1000 return false;
1001 }
1002 } else if (Subtarget->hasMips32r6()) {
1003 // On MIPS32r6/MIPS64r6, they can only handle 9-bit offsets.
1004 if (selectAddrRegImm9(Op, Base, Offset)) {
1005 OutOps.push_back(Base);
1006 OutOps.push_back(Offset);
1007 return false;
1008 }
1009 } else if (selectAddrRegImm16(Op, Base, Offset)) {
1010 // Prior to MIPS32r6/MIPS64r6, they can handle 16-bit offsets.
1011 OutOps.push_back(Base);
1012 OutOps.push_back(Offset);
1013 return false;
1014 }
1015 // In all cases, 0-bit offsets are acceptable.
1016 OutOps.push_back(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001017 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Daniel Sandersa73d8fe2015-03-24 11:26:34 +00001018 return false;
1019 }
1020 return true;
1021}
1022
Akira Hatanaka30a84782013-03-14 18:27:31 +00001023FunctionPass *llvm::createMipsSEISelDag(MipsTargetMachine &TM) {
1024 return new MipsSEDAGToDAGISel(TM);
1025}