blob: 6f0fdddd7d55ce70039f0600aac97ce7b453d2cd [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
Hrvoje Varga00d96ee2016-08-01 06:46:20 +0000296bool MipsSEDAGToDAGISel::selectAddrFrameIndexOffset(
297 SDValue Addr, SDValue &Base, SDValue &Offset, unsigned OffsetBits,
298 unsigned ShiftAmount = 0) const {
Daniel Sandersfa961d72014-03-03 14:31:21 +0000299 if (CurDAG->isBaseWithConstantOffset(Addr)) {
300 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
Hrvoje Varga00d96ee2016-08-01 06:46:20 +0000301 if (isIntN(OffsetBits + ShiftAmount, CN->getSExtValue())) {
Daniel Sandersfa961d72014-03-03 14:31:21 +0000302 EVT ValTy = Addr.getValueType();
303
304 // If the first operand is a FI, get the TargetFI Node
Hrvoje Varga00d96ee2016-08-01 06:46:20 +0000305 if (FrameIndexSDNode *FIN =
306 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0)))
Daniel Sandersfa961d72014-03-03 14:31:21 +0000307 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
Hrvoje Varga00d96ee2016-08-01 06:46:20 +0000308 else {
Daniel Sandersfa961d72014-03-03 14:31:21 +0000309 Base = Addr.getOperand(0);
Hrvoje Varga00d96ee2016-08-01 06:46:20 +0000310 // If base is a FI, additional offset calculation is done in
311 // eliminateFrameIndex, otherwise we need to check the alignment
Simon Pilgrim2ddeee12016-08-01 09:40:38 +0000312 if (OffsetToAlignment(CN->getZExtValue(), 1ull << ShiftAmount) != 0)
Hrvoje Varga00d96ee2016-08-01 06:46:20 +0000313 return false;
314 }
Daniel Sandersfa961d72014-03-03 14:31:21 +0000315
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000316 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), SDLoc(Addr),
317 ValTy);
Daniel Sandersfa961d72014-03-03 14:31:21 +0000318 return true;
319 }
320 }
321 return false;
322}
323
324/// ComplexPattern used on MipsInstrInfo
325/// Used on Mips Load/Store instructions
326bool MipsSEDAGToDAGISel::selectAddrRegImm(SDValue Addr, SDValue &Base,
327 SDValue &Offset) const {
328 // if Address is FI, get the TargetFrameIndex.
329 if (selectAddrFrameIndex(Addr, Base, Offset))
330 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000331
332 // on PIC code Load GA
333 if (Addr.getOpcode() == MipsISD::Wrapper) {
334 Base = Addr.getOperand(0);
335 Offset = Addr.getOperand(1);
336 return true;
337 }
338
Rafael Espindolab30e66b2016-06-28 14:33:28 +0000339 if (!TM.isPositionIndependent()) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000340 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
341 Addr.getOpcode() == ISD::TargetGlobalAddress))
342 return false;
343 }
344
345 // Addresses of the form FI+const or FI|const
Daniel Sandersfa961d72014-03-03 14:31:21 +0000346 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 16))
347 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000348
349 // Operand is a result from an ADD.
350 if (Addr.getOpcode() == ISD::ADD) {
351 // When loading from constant pools, load the lower address part in
352 // the instruction itself. Example, instead of:
353 // lui $2, %hi($CPI1_0)
354 // addiu $2, $2, %lo($CPI1_0)
355 // lwc1 $f0, 0($2)
356 // Generate:
357 // lui $2, %hi($CPI1_0)
358 // lwc1 $f0, %lo($CPI1_0)($2)
359 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo ||
360 Addr.getOperand(1).getOpcode() == MipsISD::GPRel) {
361 SDValue Opnd0 = Addr.getOperand(1).getOperand(0);
362 if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) ||
363 isa<JumpTableSDNode>(Opnd0)) {
364 Base = Addr.getOperand(0);
365 Offset = Opnd0;
366 return true;
367 }
368 }
369 }
370
371 return false;
372}
373
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000374/// ComplexPattern used on MipsInstrInfo
375/// Used on Mips Load/Store instructions
Akira Hatanaka30a84782013-03-14 18:27:31 +0000376bool MipsSEDAGToDAGISel::selectAddrDefault(SDValue Addr, SDValue &Base,
377 SDValue &Offset) const {
378 Base = Addr;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000379 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), Addr.getValueType());
Akira Hatanaka30a84782013-03-14 18:27:31 +0000380 return true;
381}
382
383bool MipsSEDAGToDAGISel::selectIntAddr(SDValue Addr, SDValue &Base,
384 SDValue &Offset) const {
385 return selectAddrRegImm(Addr, Base, Offset) ||
386 selectAddrDefault(Addr, Base, Offset);
387}
388
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000389bool MipsSEDAGToDAGISel::selectAddrRegImm9(SDValue Addr, SDValue &Base,
390 SDValue &Offset) const {
391 if (selectAddrFrameIndex(Addr, Base, Offset))
392 return true;
393
394 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 9))
395 return true;
396
397 return false;
398}
399
Zlatko Buljancba9f802016-07-11 07:41:56 +0000400/// Used on microMIPS LWC2, LDC2, SWC2 and SDC2 instructions (11-bit offset)
401bool MipsSEDAGToDAGISel::selectAddrRegImm11(SDValue Addr, SDValue &Base,
402 SDValue &Offset) const {
403 if (selectAddrFrameIndex(Addr, Base, Offset))
404 return true;
405
406 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 11))
407 return true;
408
409 return false;
410}
411
Jack Carter97700972013-08-13 20:19:16 +0000412/// Used on microMIPS Load/Store unaligned instructions (12-bit offset)
413bool MipsSEDAGToDAGISel::selectAddrRegImm12(SDValue Addr, SDValue &Base,
414 SDValue &Offset) const {
Daniel Sandersfa961d72014-03-03 14:31:21 +0000415 if (selectAddrFrameIndex(Addr, Base, Offset))
416 return true;
Jack Carter97700972013-08-13 20:19:16 +0000417
Daniel Sandersfa961d72014-03-03 14:31:21 +0000418 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 12))
419 return true;
Jack Carter97700972013-08-13 20:19:16 +0000420
421 return false;
422}
423
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000424bool MipsSEDAGToDAGISel::selectAddrRegImm16(SDValue Addr, SDValue &Base,
425 SDValue &Offset) const {
426 if (selectAddrFrameIndex(Addr, Base, Offset))
427 return true;
428
429 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 16))
430 return true;
431
432 return false;
433}
434
Zlatko Buljancba9f802016-07-11 07:41:56 +0000435bool MipsSEDAGToDAGISel::selectIntAddr11MM(SDValue Addr, SDValue &Base,
436 SDValue &Offset) const {
437 return selectAddrRegImm11(Addr, Base, Offset) ||
438 selectAddrDefault(Addr, Base, Offset);
439}
440
441bool MipsSEDAGToDAGISel::selectIntAddr12MM(SDValue Addr, SDValue &Base,
Jack Carter97700972013-08-13 20:19:16 +0000442 SDValue &Offset) const {
443 return selectAddrRegImm12(Addr, Base, Offset) ||
444 selectAddrDefault(Addr, Base, Offset);
445}
446
Zlatko Buljancba9f802016-07-11 07:41:56 +0000447bool MipsSEDAGToDAGISel::selectIntAddr16MM(SDValue Addr, SDValue &Base,
448 SDValue &Offset) const {
449 return selectAddrRegImm16(Addr, Base, Offset) ||
450 selectAddrDefault(Addr, Base, Offset);
451}
452
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000453bool MipsSEDAGToDAGISel::selectIntAddrLSL2MM(SDValue Addr, SDValue &Base,
454 SDValue &Offset) const {
455 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 7)) {
Vasileios Kalintiris99eeb8a2015-02-13 19:14:22 +0000456 if (isa<FrameIndexSDNode>(Base))
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000457 return false;
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000458
Vasileios Kalintiris99eeb8a2015-02-13 19:14:22 +0000459 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Offset)) {
460 unsigned CnstOff = CN->getZExtValue();
461 return (CnstOff == (CnstOff & 0x3c));
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000462 }
Vasileios Kalintiris99eeb8a2015-02-13 19:14:22 +0000463
464 return false;
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000465 }
466
467 // For all other cases where "lw" would be selected, don't select "lw16"
468 // because it would result in additional instructions to prepare operands.
469 if (selectAddrRegImm(Addr, Base, Offset))
470 return false;
471
472 return selectAddrDefault(Addr, Base, Offset);
473}
474
Hrvoje Varga00d96ee2016-08-01 06:46:20 +0000475bool MipsSEDAGToDAGISel::selectIntAddrSImm10(SDValue Addr, SDValue &Base,
476 SDValue &Offset) const {
477
478 if (selectAddrFrameIndex(Addr, Base, Offset))
Daniel Sandersfa961d72014-03-03 14:31:21 +0000479 return true;
480
Hrvoje Varga00d96ee2016-08-01 06:46:20 +0000481 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10))
Daniel Sandersfa961d72014-03-03 14:31:21 +0000482 return true;
483
Hrvoje Varga00d96ee2016-08-01 06:46:20 +0000484 return selectAddrDefault(Addr, Base, Offset);
485}
486
487bool MipsSEDAGToDAGISel::selectIntAddrSImm10Lsl1(SDValue Addr, SDValue &Base,
488 SDValue &Offset) const {
489 if (selectAddrFrameIndex(Addr, Base, Offset))
490 return true;
491
492 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10, 1))
493 return true;
494
495 return selectAddrDefault(Addr, Base, Offset);
496}
497
498bool MipsSEDAGToDAGISel::selectIntAddrSImm10Lsl2(SDValue Addr, SDValue &Base,
499 SDValue &Offset) const {
500 if (selectAddrFrameIndex(Addr, Base, Offset))
501 return true;
502
503 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10, 2))
504 return true;
505
506 return selectAddrDefault(Addr, Base, Offset);
507}
508
509bool MipsSEDAGToDAGISel::selectIntAddrSImm10Lsl3(SDValue Addr, SDValue &Base,
510 SDValue &Offset) const {
511 if (selectAddrFrameIndex(Addr, Base, Offset))
512 return true;
513
514 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10, 3))
515 return true;
516
517 return selectAddrDefault(Addr, Base, Offset);
Daniel Sandersfa961d72014-03-03 14:31:21 +0000518}
519
Daniel Sandersf49dd822013-09-24 13:33:07 +0000520// Select constant vector splats.
521//
522// Returns true and sets Imm if:
523// * MSA is enabled
524// * N is a ISD::BUILD_VECTOR representing a constant splat
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000525bool MipsSEDAGToDAGISel::selectVSplat(SDNode *N, APInt &Imm,
526 unsigned MinSizeInBits) const {
Eric Christopher22405e42014-07-10 17:26:51 +0000527 if (!Subtarget->hasMSA())
Daniel Sandersf49dd822013-09-24 13:33:07 +0000528 return false;
529
530 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N);
531
Craig Topper062a2ba2014-04-25 05:30:21 +0000532 if (!Node)
Daniel Sandersf49dd822013-09-24 13:33:07 +0000533 return false;
534
535 APInt SplatValue, SplatUndef;
536 unsigned SplatBitSize;
537 bool HasAnyUndefs;
538
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000539 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
540 MinSizeInBits, !Subtarget->isLittle()))
Daniel Sandersf49dd822013-09-24 13:33:07 +0000541 return false;
542
Daniel Sandersf49dd822013-09-24 13:33:07 +0000543 Imm = SplatValue;
544
545 return true;
546}
547
548// Select constant vector splats.
549//
550// In addition to the requirements of selectVSplat(), this function returns
551// true and sets Imm if:
552// * The splat value is the same width as the elements of the vector
553// * The splat value fits in an integer with the specified signed-ness and
554// width.
555//
556// This function looks through ISD::BITCAST nodes.
557// TODO: This might not be appropriate for big-endian MSA since BITCAST is
558// sometimes a shuffle in big-endian mode.
559//
560// It's worth noting that this function is not used as part of the selection
561// of ldi.[bhwd] since it does not permit using the wrong-typed ldi.[bhwd]
562// instruction to achieve the desired bit pattern. ldi.[bhwd] is selected in
563// MipsSEDAGToDAGISel::selectNode.
564bool MipsSEDAGToDAGISel::
565selectVSplatCommon(SDValue N, SDValue &Imm, bool Signed,
566 unsigned ImmBitSize) const {
567 APInt ImmValue;
568 EVT EltTy = N->getValueType(0).getVectorElementType();
569
570 if (N->getOpcode() == ISD::BITCAST)
571 N = N->getOperand(0);
572
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000573 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
Daniel Sandersf49dd822013-09-24 13:33:07 +0000574 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000575
Daniel Sandersf49dd822013-09-24 13:33:07 +0000576 if (( Signed && ImmValue.isSignedIntN(ImmBitSize)) ||
577 (!Signed && ImmValue.isIntN(ImmBitSize))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000578 Imm = CurDAG->getTargetConstant(ImmValue, SDLoc(N), EltTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +0000579 return true;
580 }
581 }
582
583 return false;
584}
585
586// Select constant vector splats.
587bool MipsSEDAGToDAGISel::
Daniel Sanders7e51fe12013-09-27 11:48:57 +0000588selectVSplatUimm1(SDValue N, SDValue &Imm) const {
589 return selectVSplatCommon(N, Imm, false, 1);
590}
591
592bool MipsSEDAGToDAGISel::
593selectVSplatUimm2(SDValue N, SDValue &Imm) const {
594 return selectVSplatCommon(N, Imm, false, 2);
595}
596
597bool MipsSEDAGToDAGISel::
Daniel Sandersf49dd822013-09-24 13:33:07 +0000598selectVSplatUimm3(SDValue N, SDValue &Imm) const {
599 return selectVSplatCommon(N, Imm, false, 3);
600}
601
602// Select constant vector splats.
603bool MipsSEDAGToDAGISel::
604selectVSplatUimm4(SDValue N, SDValue &Imm) const {
605 return selectVSplatCommon(N, Imm, false, 4);
606}
607
608// Select constant vector splats.
609bool MipsSEDAGToDAGISel::
610selectVSplatUimm5(SDValue N, SDValue &Imm) const {
611 return selectVSplatCommon(N, Imm, false, 5);
612}
613
614// Select constant vector splats.
615bool MipsSEDAGToDAGISel::
616selectVSplatUimm6(SDValue N, SDValue &Imm) const {
617 return selectVSplatCommon(N, Imm, false, 6);
618}
619
620// Select constant vector splats.
621bool MipsSEDAGToDAGISel::
622selectVSplatUimm8(SDValue N, SDValue &Imm) const {
623 return selectVSplatCommon(N, Imm, false, 8);
624}
625
626// Select constant vector splats.
627bool MipsSEDAGToDAGISel::
628selectVSplatSimm5(SDValue N, SDValue &Imm) const {
629 return selectVSplatCommon(N, Imm, true, 5);
630}
631
632// Select constant vector splats whose value is a power of 2.
633//
634// In addition to the requirements of selectVSplat(), this function returns
635// true and sets Imm if:
636// * The splat value is the same width as the elements of the vector
637// * The splat value is a power of two.
638//
639// This function looks through ISD::BITCAST nodes.
640// TODO: This might not be appropriate for big-endian MSA since BITCAST is
641// sometimes a shuffle in big-endian mode.
642bool MipsSEDAGToDAGISel::selectVSplatUimmPow2(SDValue N, SDValue &Imm) const {
643 APInt ImmValue;
644 EVT EltTy = N->getValueType(0).getVectorElementType();
645
646 if (N->getOpcode() == ISD::BITCAST)
647 N = N->getOperand(0);
648
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000649 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
Daniel Sandersf49dd822013-09-24 13:33:07 +0000650 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
651 int32_t Log2 = ImmValue.exactLogBase2();
652
653 if (Log2 != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000654 Imm = CurDAG->getTargetConstant(Log2, SDLoc(N), EltTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +0000655 return true;
656 }
657 }
658
659 return false;
660}
661
Daniel Sandersd74b1302013-10-30 14:45:14 +0000662// Select constant vector splats whose value only has a consecutive sequence
663// of left-most bits set (e.g. 0b11...1100...00).
664//
665// In addition to the requirements of selectVSplat(), this function returns
666// true and sets Imm if:
667// * The splat value is the same width as the elements of the vector
668// * The splat value is a consecutive sequence of left-most bits.
669//
670// This function looks through ISD::BITCAST nodes.
671// TODO: This might not be appropriate for big-endian MSA since BITCAST is
672// sometimes a shuffle in big-endian mode.
673bool MipsSEDAGToDAGISel::selectVSplatMaskL(SDValue N, SDValue &Imm) const {
674 APInt ImmValue;
675 EVT EltTy = N->getValueType(0).getVectorElementType();
676
677 if (N->getOpcode() == ISD::BITCAST)
678 N = N->getOperand(0);
679
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000680 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
Daniel Sandersd74b1302013-10-30 14:45:14 +0000681 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
682 // Extract the run of set bits starting with bit zero from the bitwise
683 // inverse of ImmValue, and test that the inverse of this is the same
684 // as the original value.
685 if (ImmValue == ~(~ImmValue & ~(~ImmValue + 1))) {
686
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000687 Imm = CurDAG->getTargetConstant(ImmValue.countPopulation(), SDLoc(N),
688 EltTy);
Daniel Sandersd74b1302013-10-30 14:45:14 +0000689 return true;
690 }
691 }
692
693 return false;
694}
695
696// Select constant vector splats whose value only has a consecutive sequence
697// of right-most bits set (e.g. 0b00...0011...11).
698//
699// In addition to the requirements of selectVSplat(), this function returns
700// true and sets Imm if:
701// * The splat value is the same width as the elements of the vector
702// * The splat value is a consecutive sequence of right-most bits.
703//
704// This function looks through ISD::BITCAST nodes.
705// TODO: This might not be appropriate for big-endian MSA since BITCAST is
706// sometimes a shuffle in big-endian mode.
707bool MipsSEDAGToDAGISel::selectVSplatMaskR(SDValue N, SDValue &Imm) const {
708 APInt ImmValue;
709 EVT EltTy = N->getValueType(0).getVectorElementType();
710
711 if (N->getOpcode() == ISD::BITCAST)
712 N = N->getOperand(0);
713
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000714 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
Daniel Sandersd74b1302013-10-30 14:45:14 +0000715 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
716 // Extract the run of set bits starting with bit zero, and test that the
717 // result is the same as the original value
718 if (ImmValue == (ImmValue & ~(ImmValue + 1))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000719 Imm = CurDAG->getTargetConstant(ImmValue.countPopulation(), SDLoc(N),
720 EltTy);
Daniel Sandersd74b1302013-10-30 14:45:14 +0000721 return true;
722 }
723 }
724
725 return false;
726}
727
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000728bool MipsSEDAGToDAGISel::selectVSplatUimmInvPow2(SDValue N,
729 SDValue &Imm) const {
730 APInt ImmValue;
731 EVT EltTy = N->getValueType(0).getVectorElementType();
732
733 if (N->getOpcode() == ISD::BITCAST)
734 N = N->getOperand(0);
735
Daniel Sandersc8cd58f2015-05-19 12:24:52 +0000736 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000737 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
738 int32_t Log2 = (~ImmValue).exactLogBase2();
739
740 if (Log2 != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000741 Imm = CurDAG->getTargetConstant(Log2, SDLoc(N), EltTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000742 return true;
743 }
744 }
745
746 return false;
747}
748
Justin Bognereeae7512016-05-13 23:55:59 +0000749bool MipsSEDAGToDAGISel::trySelect(SDNode *Node) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000750 unsigned Opcode = Node->getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000751 SDLoc DL(Node);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000752
753 ///
754 // Instruction Selection not handled by the auto-generated
755 // tablegen selection should be handled here.
756 ///
Akira Hatanaka30a84782013-03-14 18:27:31 +0000757 switch(Opcode) {
758 default: break;
759
Akira Hatanakab8835b82013-03-14 18:39:25 +0000760 case ISD::SUBE: {
761 SDValue InFlag = Node->getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000762 unsigned Opc = Subtarget->isGP64bit() ? Mips::DSUBu : Mips::SUBu;
Justin Bognereeae7512016-05-13 23:55:59 +0000763 selectAddESubE(Opc, InFlag, InFlag.getOperand(0), DL, Node);
764 return true;
Akira Hatanakab8835b82013-03-14 18:39:25 +0000765 }
766
Akira Hatanaka30a84782013-03-14 18:27:31 +0000767 case ISD::ADDE: {
Eric Christopher22405e42014-07-10 17:26:51 +0000768 if (Subtarget->hasDSP()) // Select DSP instructions, ADDSC and ADDWC.
Akira Hatanaka2f088222013-04-13 00:55:41 +0000769 break;
Akira Hatanakab8835b82013-03-14 18:39:25 +0000770 SDValue InFlag = Node->getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000771 unsigned Opc = Subtarget->isGP64bit() ? Mips::DADDu : Mips::ADDu;
Justin Bognereeae7512016-05-13 23:55:59 +0000772 selectAddESubE(Opc, InFlag, InFlag.getValue(0), DL, Node);
773 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000774 }
775
Akira Hatanaka30a84782013-03-14 18:27:31 +0000776 case ISD::ConstantFP: {
777 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
778 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
Eric Christopher22405e42014-07-10 17:26:51 +0000779 if (Subtarget->isGP64bit()) {
Akira Hatanaka040d2252013-03-14 18:33:23 +0000780 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000781 Mips::ZERO_64, MVT::i64);
Justin Bognereeae7512016-05-13 23:55:59 +0000782 ReplaceNode(Node,
783 CurDAG->getMachineNode(Mips::DMTC1, DL, MVT::f64, Zero));
Eric Christopher22405e42014-07-10 17:26:51 +0000784 } else if (Subtarget->isFP64bit()) {
Daniel Sanders08d3cd12013-11-18 13:12:43 +0000785 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
786 Mips::ZERO, MVT::i32);
Justin Bognereeae7512016-05-13 23:55:59 +0000787 ReplaceNode(Node, CurDAG->getMachineNode(Mips::BuildPairF64_64, DL,
788 MVT::f64, Zero, Zero));
Akira Hatanaka30a84782013-03-14 18:27:31 +0000789 } else {
Akira Hatanaka040d2252013-03-14 18:33:23 +0000790 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000791 Mips::ZERO, MVT::i32);
Justin Bognereeae7512016-05-13 23:55:59 +0000792 ReplaceNode(Node, CurDAG->getMachineNode(Mips::BuildPairF64, DL,
793 MVT::f64, Zero, Zero));
Akira Hatanaka30a84782013-03-14 18:27:31 +0000794 }
Justin Bognereeae7512016-05-13 23:55:59 +0000795 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000796 }
797 break;
798 }
799
800 case ISD::Constant: {
801 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node);
Simon Dardis61897522016-07-25 09:57:28 +0000802 int64_t Imm = CN->getSExtValue();
Akira Hatanaka30a84782013-03-14 18:27:31 +0000803 unsigned Size = CN->getValueSizeInBits(0);
804
Simon Dardis61897522016-07-25 09:57:28 +0000805 if (isInt<32>(Imm))
Akira Hatanaka30a84782013-03-14 18:27:31 +0000806 break;
807
808 MipsAnalyzeImmediate AnalyzeImm;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000809
810 const MipsAnalyzeImmediate::InstSeq &Seq =
811 AnalyzeImm.Analyze(Imm, Size, false);
812
813 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000814 SDLoc DL(CN);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000815 SDNode *RegOpnd;
816 SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000817 DL, MVT::i64);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000818
819 // The first instruction can be a LUi which is different from other
820 // instructions (ADDiu, ORI and SLL) in that it does not have a register
821 // operand.
822 if (Inst->Opc == Mips::LUi64)
823 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
824 else
825 RegOpnd =
826 CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
827 CurDAG->getRegister(Mips::ZERO_64, MVT::i64),
828 ImmOpnd);
829
830 // The remaining instructions in the sequence are handled here.
831 for (++Inst; Inst != Seq.end(); ++Inst) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000832 ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd), DL,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000833 MVT::i64);
834 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
835 SDValue(RegOpnd, 0), ImmOpnd);
836 }
837
Justin Bognereeae7512016-05-13 23:55:59 +0000838 ReplaceNode(Node, RegOpnd);
839 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000840 }
841
Daniel Sandersf9aa1d12013-08-28 10:26:24 +0000842 case ISD::INTRINSIC_W_CHAIN: {
843 switch (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
844 default:
845 break;
846
847 case Intrinsic::mips_cfcmsa: {
848 SDValue ChainIn = Node->getOperand(0);
849 SDValue RegIdx = Node->getOperand(2);
850 SDValue Reg = CurDAG->getCopyFromReg(ChainIn, DL,
851 getMSACtrlReg(RegIdx), MVT::i32);
Justin Bognereeae7512016-05-13 23:55:59 +0000852 ReplaceNode(Node, Reg.getNode());
853 return true;
Daniel Sandersf9aa1d12013-08-28 10:26:24 +0000854 }
855 }
856 break;
857 }
858
Daniel Sandersba9c8502013-08-28 10:44:47 +0000859 case ISD::INTRINSIC_WO_CHAIN: {
860 switch (cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue()) {
861 default:
862 break;
863
864 case Intrinsic::mips_move_v:
865 // Like an assignment but will always produce a move.v even if
866 // unnecessary.
Justin Bognereeae7512016-05-13 23:55:59 +0000867 ReplaceNode(Node, CurDAG->getMachineNode(Mips::MOVE_V, DL,
868 Node->getValueType(0),
869 Node->getOperand(1)));
870 return true;
Daniel Sandersba9c8502013-08-28 10:44:47 +0000871 }
872 break;
873 }
874
Daniel Sandersf9aa1d12013-08-28 10:26:24 +0000875 case ISD::INTRINSIC_VOID: {
876 switch (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
877 default:
878 break;
879
880 case Intrinsic::mips_ctcmsa: {
881 SDValue ChainIn = Node->getOperand(0);
882 SDValue RegIdx = Node->getOperand(2);
883 SDValue Value = Node->getOperand(3);
884 SDValue ChainOut = CurDAG->getCopyToReg(ChainIn, DL,
885 getMSACtrlReg(RegIdx), Value);
Justin Bognereeae7512016-05-13 23:55:59 +0000886 ReplaceNode(Node, ChainOut.getNode());
887 return true;
Daniel Sandersf9aa1d12013-08-28 10:26:24 +0000888 }
889 }
890 break;
891 }
892
Akira Hatanaka30a84782013-03-14 18:27:31 +0000893 case MipsISD::ThreadPointer: {
Mehdi Amini44ede332015-07-09 02:09:04 +0000894 EVT PtrVT = getTargetLowering()->getPointerTy(CurDAG->getDataLayout());
Akira Hatanaka85ccf232013-08-08 21:37:32 +0000895 unsigned RdhwrOpc, DestReg;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000896
897 if (PtrVT == MVT::i32) {
898 RdhwrOpc = Mips::RDHWR;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000899 DestReg = Mips::V1;
900 } else {
901 RdhwrOpc = Mips::RDHWR64;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000902 DestReg = Mips::V1_64;
903 }
904
905 SDNode *Rdhwr =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000906 CurDAG->getMachineNode(RdhwrOpc, DL,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000907 Node->getValueType(0),
Akira Hatanaka85ccf232013-08-08 21:37:32 +0000908 CurDAG->getRegister(Mips::HWR29, MVT::i32));
Akira Hatanaka040d2252013-03-14 18:33:23 +0000909 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), DL, DestReg,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000910 SDValue(Rdhwr, 0));
Akira Hatanaka040d2252013-03-14 18:33:23 +0000911 SDValue ResNode = CurDAG->getCopyFromReg(Chain, DL, DestReg, PtrVT);
Justin Bognereeae7512016-05-13 23:55:59 +0000912 ReplaceNode(Node, ResNode.getNode());
913 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000914 }
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000915
Daniel Sandersf49dd822013-09-24 13:33:07 +0000916 case ISD::BUILD_VECTOR: {
917 // Select appropriate ldi.[bhwd] instructions for constant splats of
918 // 128-bit when MSA is enabled. Fixup any register class mismatches that
919 // occur as a result.
920 //
921 // This allows the compiler to use a wider range of immediates than would
922 // otherwise be allowed. If, for example, v4i32 could only use ldi.h then
923 // it would not be possible to load { 0x01010101, 0x01010101, 0x01010101,
924 // 0x01010101 } without using a constant pool. This would be sub-optimal
925 // when // 'ldi.b wd, 1' is capable of producing that bit-pattern in the
926 // same set/ of registers. Similarly, ldi.h isn't capable of producing {
927 // 0x00000000, 0x00000001, 0x00000000, 0x00000001 } but 'ldi.d wd, 1' can.
928
929 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Node);
930 APInt SplatValue, SplatUndef;
931 unsigned SplatBitSize;
932 bool HasAnyUndefs;
933 unsigned LdiOp;
934 EVT ResVecTy = BVN->getValueType(0);
935 EVT ViaVecTy;
936
Eric Christopher22405e42014-07-10 17:26:51 +0000937 if (!Subtarget->hasMSA() || !BVN->getValueType(0).is128BitVector())
Justin Bognereeae7512016-05-13 23:55:59 +0000938 return false;
Daniel Sandersf49dd822013-09-24 13:33:07 +0000939
940 if (!BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
941 HasAnyUndefs, 8,
Eric Christopher22405e42014-07-10 17:26:51 +0000942 !Subtarget->isLittle()))
Justin Bognereeae7512016-05-13 23:55:59 +0000943 return false;
Daniel Sandersf49dd822013-09-24 13:33:07 +0000944
945 switch (SplatBitSize) {
946 default:
Justin Bognereeae7512016-05-13 23:55:59 +0000947 return false;
Daniel Sandersf49dd822013-09-24 13:33:07 +0000948 case 8:
949 LdiOp = Mips::LDI_B;
950 ViaVecTy = MVT::v16i8;
951 break;
952 case 16:
953 LdiOp = Mips::LDI_H;
954 ViaVecTy = MVT::v8i16;
955 break;
956 case 32:
957 LdiOp = Mips::LDI_W;
958 ViaVecTy = MVT::v4i32;
959 break;
960 case 64:
961 LdiOp = Mips::LDI_D;
962 ViaVecTy = MVT::v2i64;
963 break;
964 }
965
966 if (!SplatValue.isSignedIntN(10))
Justin Bognereeae7512016-05-13 23:55:59 +0000967 return false;
Daniel Sandersf49dd822013-09-24 13:33:07 +0000968
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000969 SDValue Imm = CurDAG->getTargetConstant(SplatValue, DL,
Daniel Sandersf49dd822013-09-24 13:33:07 +0000970 ViaVecTy.getVectorElementType());
971
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000972 SDNode *Res = CurDAG->getMachineNode(LdiOp, DL, ViaVecTy, Imm);
Daniel Sandersf49dd822013-09-24 13:33:07 +0000973
974 if (ResVecTy != ViaVecTy) {
975 // If LdiOp is writing to a different register class to ResVecTy, then
976 // fix it up here. This COPY_TO_REGCLASS should never cause a move.v
977 // since the source and destination register sets contain the same
978 // registers.
979 const TargetLowering *TLI = getTargetLowering();
980 MVT ResVecTySimple = ResVecTy.getSimpleVT();
981 const TargetRegisterClass *RC = TLI->getRegClassFor(ResVecTySimple);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000982 Res = CurDAG->getMachineNode(Mips::COPY_TO_REGCLASS, DL,
Daniel Sandersf49dd822013-09-24 13:33:07 +0000983 ResVecTy, SDValue(Res, 0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000984 CurDAG->getTargetConstant(RC->getID(), DL,
Daniel Sandersf49dd822013-09-24 13:33:07 +0000985 MVT::i32));
986 }
987
Justin Bognereeae7512016-05-13 23:55:59 +0000988 ReplaceNode(Node, Res);
989 return true;
Daniel Sandersf49dd822013-09-24 13:33:07 +0000990 }
991
Akira Hatanaka30a84782013-03-14 18:27:31 +0000992 }
993
Justin Bognereeae7512016-05-13 23:55:59 +0000994 return false;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000995}
996
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000997bool MipsSEDAGToDAGISel::
998SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
999 std::vector<SDValue> &OutOps) {
1000 SDValue Base, Offset;
1001
1002 switch(ConstraintID) {
1003 default:
1004 llvm_unreachable("Unexpected asm memory constraint");
1005 // All memory constraints can at least accept raw pointers.
1006 case InlineAsm::Constraint_i:
Daniel Sandersa73d8fe2015-03-24 11:26:34 +00001007 OutOps.push_back(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001008 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Daniel Sandersa73d8fe2015-03-24 11:26:34 +00001009 return false;
Daniel Sandersc676f2a2015-03-24 15:19:14 +00001010 case InlineAsm::Constraint_m:
1011 if (selectAddrRegImm16(Op, Base, Offset)) {
1012 OutOps.push_back(Base);
1013 OutOps.push_back(Offset);
1014 return false;
1015 }
1016 OutOps.push_back(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001017 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Daniel Sandersc676f2a2015-03-24 15:19:14 +00001018 return false;
Daniel Sanders82df6162015-03-30 13:27:25 +00001019 case InlineAsm::Constraint_R:
1020 // The 'R' constraint is supposed to be much more complicated than this.
1021 // However, it's becoming less useful due to architectural changes and
1022 // ought to be replaced by other constraints such as 'ZC'.
1023 // For now, support 9-bit signed offsets which is supportable by all
1024 // subtargets for all instructions.
1025 if (selectAddrRegImm9(Op, Base, Offset)) {
1026 OutOps.push_back(Base);
1027 OutOps.push_back(Offset);
1028 return false;
1029 }
1030 OutOps.push_back(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001031 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Daniel Sanders82df6162015-03-30 13:27:25 +00001032 return false;
Daniel Sandersa73d8fe2015-03-24 11:26:34 +00001033 case InlineAsm::Constraint_ZC:
1034 // ZC matches whatever the pref, ll, and sc instructions can handle for the
1035 // given subtarget.
1036 if (Subtarget->inMicroMipsMode()) {
1037 // On microMIPS, they can handle 12-bit offsets.
1038 if (selectAddrRegImm12(Op, Base, Offset)) {
1039 OutOps.push_back(Base);
1040 OutOps.push_back(Offset);
1041 return false;
1042 }
1043 } else if (Subtarget->hasMips32r6()) {
1044 // On MIPS32r6/MIPS64r6, they can only handle 9-bit offsets.
1045 if (selectAddrRegImm9(Op, Base, Offset)) {
1046 OutOps.push_back(Base);
1047 OutOps.push_back(Offset);
1048 return false;
1049 }
1050 } else if (selectAddrRegImm16(Op, Base, Offset)) {
1051 // Prior to MIPS32r6/MIPS64r6, they can handle 16-bit offsets.
1052 OutOps.push_back(Base);
1053 OutOps.push_back(Offset);
1054 return false;
1055 }
1056 // In all cases, 0-bit offsets are acceptable.
1057 OutOps.push_back(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001058 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Daniel Sandersa73d8fe2015-03-24 11:26:34 +00001059 return false;
1060 }
1061 return true;
1062}
1063
Daniel Sanders46fe6552016-07-14 13:25:22 +00001064FunctionPass *llvm::createMipsSEISelDag(MipsTargetMachine &TM,
1065 CodeGenOpt::Level OptLevel) {
1066 return new MipsSEDAGToDAGISel(TM, OptLevel);
Akira Hatanaka30a84782013-03-14 18:27:31 +00001067}