blob: ae45bbdb708752cfe5f92621a4278111a1cba607 [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
118 MO.setReg(ZeroReg);
119 }
120
121 return true;
122}
123
Akira Hatanaka040d2252013-03-14 18:33:23 +0000124void MipsSEDAGToDAGISel::initGlobalBaseReg(MachineFunction &MF) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000125 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
126
127 if (!MipsFI->globalBaseRegSet())
128 return;
129
130 MachineBasicBlock &MBB = MF.front();
131 MachineBasicBlock::iterator I = MBB.begin();
132 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +0000133 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Akira Hatanaka30a84782013-03-14 18:27:31 +0000134 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
135 unsigned V0, V1, GlobalBaseReg = MipsFI->getGlobalBaseReg();
136 const TargetRegisterClass *RC;
Eric Christopherd86af632015-01-29 23:27:45 +0000137 const MipsABIInfo &ABI = static_cast<const MipsTargetMachine &>(TM).getABI();
138 RC = (ABI.IsN64()) ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000139
140 V0 = RegInfo.createVirtualRegister(RC);
141 V1 = RegInfo.createVirtualRegister(RC);
142
Eric Christopherd86af632015-01-29 23:27:45 +0000143 if (ABI.IsN64()) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000144 MF.getRegInfo().addLiveIn(Mips::T9_64);
145 MBB.addLiveIn(Mips::T9_64);
146
147 // lui $v0, %hi(%neg(%gp_rel(fname)))
148 // daddu $v1, $v0, $t9
149 // daddiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
150 const GlobalValue *FName = MF.getFunction();
151 BuildMI(MBB, I, DL, TII.get(Mips::LUi64), V0)
152 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
153 BuildMI(MBB, I, DL, TII.get(Mips::DADDu), V1).addReg(V0)
154 .addReg(Mips::T9_64);
155 BuildMI(MBB, I, DL, TII.get(Mips::DADDiu), GlobalBaseReg).addReg(V1)
156 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
157 return;
158 }
159
160 if (MF.getTarget().getRelocationModel() == Reloc::Static) {
161 // Set global register to __gnu_local_gp.
162 //
163 // lui $v0, %hi(__gnu_local_gp)
164 // addiu $globalbasereg, $v0, %lo(__gnu_local_gp)
165 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
166 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_HI);
167 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V0)
168 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_LO);
169 return;
170 }
171
172 MF.getRegInfo().addLiveIn(Mips::T9);
173 MBB.addLiveIn(Mips::T9);
174
Eric Christopherd86af632015-01-29 23:27:45 +0000175 if (ABI.IsN32()) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000176 // lui $v0, %hi(%neg(%gp_rel(fname)))
177 // addu $v1, $v0, $t9
178 // addiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
179 const GlobalValue *FName = MF.getFunction();
180 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
181 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
182 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), V1).addReg(V0).addReg(Mips::T9);
183 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V1)
184 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
185 return;
186 }
187
Eric Christopherd86af632015-01-29 23:27:45 +0000188 assert(ABI.IsO32());
Akira Hatanaka30a84782013-03-14 18:27:31 +0000189
190 // For O32 ABI, the following instruction sequence is emitted to initialize
191 // the global base register:
192 //
193 // 0. lui $2, %hi(_gp_disp)
194 // 1. addiu $2, $2, %lo(_gp_disp)
195 // 2. addu $globalbasereg, $2, $t9
196 //
197 // We emit only the last instruction here.
198 //
199 // GNU linker requires that the first two instructions appear at the beginning
200 // of a function and no instructions be inserted before or between them.
201 // The two instructions are emitted during lowering to MC layer in order to
202 // avoid any reordering.
203 //
204 // Register $2 (Mips::V0) is added to the list of live-in registers to ensure
205 // the value instruction 1 (addiu) defines is valid when instruction 2 (addu)
206 // reads it.
207 MF.getRegInfo().addLiveIn(Mips::V0);
208 MBB.addLiveIn(Mips::V0);
209 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), GlobalBaseReg)
210 .addReg(Mips::V0).addReg(Mips::T9);
211}
212
Akira Hatanaka040d2252013-03-14 18:33:23 +0000213void MipsSEDAGToDAGISel::processFunctionAfterISel(MachineFunction &MF) {
214 initGlobalBaseReg(MF);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000215
216 MachineRegisterInfo *MRI = &MF.getRegInfo();
217
218 for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end(); MFI != MFE;
219 ++MFI)
Akira Hatanakae86bd4f2013-05-03 18:37:49 +0000220 for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I) {
221 if (I->getOpcode() == Mips::RDDSP)
222 addDSPCtrlRegOperands(false, *I, MF);
223 else if (I->getOpcode() == Mips::WRDSP)
224 addDSPCtrlRegOperands(true, *I, MF);
225 else
226 replaceUsesWithZeroReg(MRI, *I);
227 }
Akira Hatanaka30a84782013-03-14 18:27:31 +0000228}
229
Akira Hatanakab8835b82013-03-14 18:39:25 +0000230SDNode *MipsSEDAGToDAGISel::selectAddESubE(unsigned MOp, SDValue InFlag,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000231 SDValue CmpLHS, SDLoc DL,
Akira Hatanakab8835b82013-03-14 18:39:25 +0000232 SDNode *Node) const {
233 unsigned Opc = InFlag.getOpcode(); (void)Opc;
234
235 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
236 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
237 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
238
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000239 unsigned SLTuOp = Mips::SLTu, ADDuOp = Mips::ADDu;
240 if (Subtarget->isGP64bit()) {
241 SLTuOp = Mips::SLTu64;
242 ADDuOp = Mips::DADDu;
243 }
244
Akira Hatanakab8835b82013-03-14 18:39:25 +0000245 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
246 SDValue LHS = Node->getOperand(0), RHS = Node->getOperand(1);
247 EVT VT = LHS.getValueType();
248
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000249 SDNode *Carry = CurDAG->getMachineNode(SLTuOp, DL, VT, Ops);
250
251 if (Subtarget->isGP64bit()) {
252 // On 64-bit targets, sltu produces an i64 but our backend currently says
253 // that SLTu64 produces an i32. We need to fix this in the long run but for
254 // now, just make the DAG type-correct by asserting the upper bits are zero.
255 Carry = CurDAG->getMachineNode(Mips::SUBREG_TO_REG, DL, VT,
256 CurDAG->getTargetConstant(0, VT),
257 SDValue(Carry, 0),
258 CurDAG->getTargetConstant(Mips::sub_32, VT));
259 }
260
261 SDNode *AddCarry = CurDAG->getMachineNode(ADDuOp, DL, VT,
Vasileios Kalintiris30c54512015-01-26 09:53:30 +0000262 SDValue(Carry, 0), RHS);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000263
Akira Hatanakab8835b82013-03-14 18:39:25 +0000264 return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue, LHS,
265 SDValue(AddCarry, 0));
266}
267
Daniel Sandersfa961d72014-03-03 14:31:21 +0000268/// Match frameindex
269bool MipsSEDAGToDAGISel::selectAddrFrameIndex(SDValue Addr, SDValue &Base,
270 SDValue &Offset) const {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000271 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Daniel Sandersfa961d72014-03-03 14:31:21 +0000272 EVT ValTy = Addr.getValueType();
273
Akira Hatanaka30a84782013-03-14 18:27:31 +0000274 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
275 Offset = CurDAG->getTargetConstant(0, ValTy);
276 return true;
277 }
Daniel Sandersfa961d72014-03-03 14:31:21 +0000278 return false;
279}
280
281/// Match frameindex+offset and frameindex|offset
282bool MipsSEDAGToDAGISel::selectAddrFrameIndexOffset(SDValue Addr, SDValue &Base,
283 SDValue &Offset,
284 unsigned OffsetBits) const {
285 if (CurDAG->isBaseWithConstantOffset(Addr)) {
286 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
287 if (isIntN(OffsetBits, CN->getSExtValue())) {
288 EVT ValTy = Addr.getValueType();
289
290 // If the first operand is a FI, get the TargetFI Node
291 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
292 (Addr.getOperand(0)))
293 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
294 else
295 Base = Addr.getOperand(0);
296
297 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy);
298 return true;
299 }
300 }
301 return false;
302}
303
304/// ComplexPattern used on MipsInstrInfo
305/// Used on Mips Load/Store instructions
306bool MipsSEDAGToDAGISel::selectAddrRegImm(SDValue Addr, SDValue &Base,
307 SDValue &Offset) const {
308 // if Address is FI, get the TargetFrameIndex.
309 if (selectAddrFrameIndex(Addr, Base, Offset))
310 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000311
312 // on PIC code Load GA
313 if (Addr.getOpcode() == MipsISD::Wrapper) {
314 Base = Addr.getOperand(0);
315 Offset = Addr.getOperand(1);
316 return true;
317 }
318
319 if (TM.getRelocationModel() != Reloc::PIC_) {
320 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
321 Addr.getOpcode() == ISD::TargetGlobalAddress))
322 return false;
323 }
324
325 // Addresses of the form FI+const or FI|const
Daniel Sandersfa961d72014-03-03 14:31:21 +0000326 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 16))
327 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000328
329 // Operand is a result from an ADD.
330 if (Addr.getOpcode() == ISD::ADD) {
331 // When loading from constant pools, load the lower address part in
332 // the instruction itself. Example, instead of:
333 // lui $2, %hi($CPI1_0)
334 // addiu $2, $2, %lo($CPI1_0)
335 // lwc1 $f0, 0($2)
336 // Generate:
337 // lui $2, %hi($CPI1_0)
338 // lwc1 $f0, %lo($CPI1_0)($2)
339 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo ||
340 Addr.getOperand(1).getOpcode() == MipsISD::GPRel) {
341 SDValue Opnd0 = Addr.getOperand(1).getOperand(0);
342 if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) ||
343 isa<JumpTableSDNode>(Opnd0)) {
344 Base = Addr.getOperand(0);
345 Offset = Opnd0;
346 return true;
347 }
348 }
349 }
350
351 return false;
352}
353
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000354/// ComplexPattern used on MipsInstrInfo
355/// Used on Mips Load/Store instructions
356bool MipsSEDAGToDAGISel::selectAddrRegReg(SDValue Addr, SDValue &Base,
357 SDValue &Offset) const {
358 // Operand is a result from an ADD.
359 if (Addr.getOpcode() == ISD::ADD) {
360 Base = Addr.getOperand(0);
361 Offset = Addr.getOperand(1);
362 return true;
363 }
364
365 return false;
366}
367
Akira Hatanaka30a84782013-03-14 18:27:31 +0000368bool MipsSEDAGToDAGISel::selectAddrDefault(SDValue Addr, SDValue &Base,
369 SDValue &Offset) const {
370 Base = Addr;
371 Offset = CurDAG->getTargetConstant(0, Addr.getValueType());
372 return true;
373}
374
375bool MipsSEDAGToDAGISel::selectIntAddr(SDValue Addr, SDValue &Base,
376 SDValue &Offset) const {
377 return selectAddrRegImm(Addr, Base, Offset) ||
378 selectAddrDefault(Addr, Base, Offset);
379}
380
Daniel Sandersfa961d72014-03-03 14:31:21 +0000381bool MipsSEDAGToDAGISel::selectAddrRegImm10(SDValue Addr, SDValue &Base,
382 SDValue &Offset) const {
383 if (selectAddrFrameIndex(Addr, Base, Offset))
384 return true;
385
386 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10))
387 return true;
388
389 return false;
390}
391
Jack Carter97700972013-08-13 20:19:16 +0000392/// Used on microMIPS Load/Store unaligned instructions (12-bit offset)
393bool MipsSEDAGToDAGISel::selectAddrRegImm12(SDValue Addr, SDValue &Base,
394 SDValue &Offset) const {
Daniel Sandersfa961d72014-03-03 14:31:21 +0000395 if (selectAddrFrameIndex(Addr, Base, Offset))
396 return true;
Jack Carter97700972013-08-13 20:19:16 +0000397
Daniel Sandersfa961d72014-03-03 14:31:21 +0000398 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 12))
399 return true;
Jack Carter97700972013-08-13 20:19:16 +0000400
401 return false;
402}
403
404bool MipsSEDAGToDAGISel::selectIntAddrMM(SDValue Addr, SDValue &Base,
405 SDValue &Offset) const {
406 return selectAddrRegImm12(Addr, Base, Offset) ||
407 selectAddrDefault(Addr, Base, Offset);
408}
409
Zoran Jovanovic5a1a7802015-02-04 15:43:17 +0000410bool MipsSEDAGToDAGISel::selectIntAddrLSL2MM(SDValue Addr, SDValue &Base,
411 SDValue &Offset) const {
412 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 7)) {
413 if (dyn_cast<FrameIndexSDNode>(Base))
414 return false;
415 else {
416 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Offset);
417 if (CN) {
418 unsigned CnstOff = CN->getZExtValue();
419 if (CnstOff == (CnstOff & 0x3c))
420 return true;
421 }
422
423 return false;
424 }
425 }
426
427 // For all other cases where "lw" would be selected, don't select "lw16"
428 // because it would result in additional instructions to prepare operands.
429 if (selectAddrRegImm(Addr, Base, Offset))
430 return false;
431
432 return selectAddrDefault(Addr, Base, Offset);
433}
434
Daniel Sandersfa961d72014-03-03 14:31:21 +0000435bool MipsSEDAGToDAGISel::selectIntAddrMSA(SDValue Addr, SDValue &Base,
436 SDValue &Offset) const {
437 if (selectAddrRegImm10(Addr, Base, Offset))
438 return true;
439
440 if (selectAddrDefault(Addr, Base, Offset))
441 return true;
442
443 return false;
444}
445
Daniel Sandersf49dd822013-09-24 13:33:07 +0000446// Select constant vector splats.
447//
448// Returns true and sets Imm if:
449// * MSA is enabled
450// * N is a ISD::BUILD_VECTOR representing a constant splat
Daniel Sandersf49dd822013-09-24 13:33:07 +0000451bool MipsSEDAGToDAGISel::selectVSplat(SDNode *N, APInt &Imm) const {
Eric Christopher22405e42014-07-10 17:26:51 +0000452 if (!Subtarget->hasMSA())
Daniel Sandersf49dd822013-09-24 13:33:07 +0000453 return false;
454
455 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N);
456
Craig Topper062a2ba2014-04-25 05:30:21 +0000457 if (!Node)
Daniel Sandersf49dd822013-09-24 13:33:07 +0000458 return false;
459
460 APInt SplatValue, SplatUndef;
461 unsigned SplatBitSize;
462 bool HasAnyUndefs;
463
464 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
465 HasAnyUndefs, 8,
Eric Christopher22405e42014-07-10 17:26:51 +0000466 !Subtarget->isLittle()))
Daniel Sandersf49dd822013-09-24 13:33:07 +0000467 return false;
468
Daniel Sandersf49dd822013-09-24 13:33:07 +0000469 Imm = SplatValue;
470
471 return true;
472}
473
474// Select constant vector splats.
475//
476// In addition to the requirements of selectVSplat(), this function returns
477// true and sets Imm if:
478// * The splat value is the same width as the elements of the vector
479// * The splat value fits in an integer with the specified signed-ness and
480// width.
481//
482// This function looks through ISD::BITCAST nodes.
483// TODO: This might not be appropriate for big-endian MSA since BITCAST is
484// sometimes a shuffle in big-endian mode.
485//
486// It's worth noting that this function is not used as part of the selection
487// of ldi.[bhwd] since it does not permit using the wrong-typed ldi.[bhwd]
488// instruction to achieve the desired bit pattern. ldi.[bhwd] is selected in
489// MipsSEDAGToDAGISel::selectNode.
490bool MipsSEDAGToDAGISel::
491selectVSplatCommon(SDValue N, SDValue &Imm, bool Signed,
492 unsigned ImmBitSize) const {
493 APInt ImmValue;
494 EVT EltTy = N->getValueType(0).getVectorElementType();
495
496 if (N->getOpcode() == ISD::BITCAST)
497 N = N->getOperand(0);
498
499 if (selectVSplat (N.getNode(), ImmValue) &&
500 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
501 if (( Signed && ImmValue.isSignedIntN(ImmBitSize)) ||
502 (!Signed && ImmValue.isIntN(ImmBitSize))) {
503 Imm = CurDAG->getTargetConstant(ImmValue, EltTy);
504 return true;
505 }
506 }
507
508 return false;
509}
510
511// Select constant vector splats.
512bool MipsSEDAGToDAGISel::
Daniel Sanders7e51fe12013-09-27 11:48:57 +0000513selectVSplatUimm1(SDValue N, SDValue &Imm) const {
514 return selectVSplatCommon(N, Imm, false, 1);
515}
516
517bool MipsSEDAGToDAGISel::
518selectVSplatUimm2(SDValue N, SDValue &Imm) const {
519 return selectVSplatCommon(N, Imm, false, 2);
520}
521
522bool MipsSEDAGToDAGISel::
Daniel Sandersf49dd822013-09-24 13:33:07 +0000523selectVSplatUimm3(SDValue N, SDValue &Imm) const {
524 return selectVSplatCommon(N, Imm, false, 3);
525}
526
527// Select constant vector splats.
528bool MipsSEDAGToDAGISel::
529selectVSplatUimm4(SDValue N, SDValue &Imm) const {
530 return selectVSplatCommon(N, Imm, false, 4);
531}
532
533// Select constant vector splats.
534bool MipsSEDAGToDAGISel::
535selectVSplatUimm5(SDValue N, SDValue &Imm) const {
536 return selectVSplatCommon(N, Imm, false, 5);
537}
538
539// Select constant vector splats.
540bool MipsSEDAGToDAGISel::
541selectVSplatUimm6(SDValue N, SDValue &Imm) const {
542 return selectVSplatCommon(N, Imm, false, 6);
543}
544
545// Select constant vector splats.
546bool MipsSEDAGToDAGISel::
547selectVSplatUimm8(SDValue N, SDValue &Imm) const {
548 return selectVSplatCommon(N, Imm, false, 8);
549}
550
551// Select constant vector splats.
552bool MipsSEDAGToDAGISel::
553selectVSplatSimm5(SDValue N, SDValue &Imm) const {
554 return selectVSplatCommon(N, Imm, true, 5);
555}
556
557// Select constant vector splats whose value is a power of 2.
558//
559// In addition to the requirements of selectVSplat(), this function returns
560// true and sets Imm if:
561// * The splat value is the same width as the elements of the vector
562// * The splat value is a power of two.
563//
564// This function looks through ISD::BITCAST nodes.
565// TODO: This might not be appropriate for big-endian MSA since BITCAST is
566// sometimes a shuffle in big-endian mode.
567bool MipsSEDAGToDAGISel::selectVSplatUimmPow2(SDValue N, SDValue &Imm) const {
568 APInt ImmValue;
569 EVT EltTy = N->getValueType(0).getVectorElementType();
570
571 if (N->getOpcode() == ISD::BITCAST)
572 N = N->getOperand(0);
573
574 if (selectVSplat (N.getNode(), ImmValue) &&
575 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
576 int32_t Log2 = ImmValue.exactLogBase2();
577
578 if (Log2 != -1) {
579 Imm = CurDAG->getTargetConstant(Log2, EltTy);
580 return true;
581 }
582 }
583
584 return false;
585}
586
Daniel Sandersd74b1302013-10-30 14:45:14 +0000587// Select constant vector splats whose value only has a consecutive sequence
588// of left-most bits set (e.g. 0b11...1100...00).
589//
590// In addition to the requirements of selectVSplat(), this function returns
591// true and sets Imm if:
592// * The splat value is the same width as the elements of the vector
593// * The splat value is a consecutive sequence of left-most bits.
594//
595// This function looks through ISD::BITCAST nodes.
596// TODO: This might not be appropriate for big-endian MSA since BITCAST is
597// sometimes a shuffle in big-endian mode.
598bool MipsSEDAGToDAGISel::selectVSplatMaskL(SDValue N, SDValue &Imm) const {
599 APInt ImmValue;
600 EVT EltTy = N->getValueType(0).getVectorElementType();
601
602 if (N->getOpcode() == ISD::BITCAST)
603 N = N->getOperand(0);
604
605 if (selectVSplat(N.getNode(), ImmValue) &&
606 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
607 // Extract the run of set bits starting with bit zero from the bitwise
608 // inverse of ImmValue, and test that the inverse of this is the same
609 // as the original value.
610 if (ImmValue == ~(~ImmValue & ~(~ImmValue + 1))) {
611
612 Imm = CurDAG->getTargetConstant(ImmValue.countPopulation(), EltTy);
613 return true;
614 }
615 }
616
617 return false;
618}
619
620// Select constant vector splats whose value only has a consecutive sequence
621// of right-most bits set (e.g. 0b00...0011...11).
622//
623// In addition to the requirements of selectVSplat(), this function returns
624// true and sets Imm if:
625// * The splat value is the same width as the elements of the vector
626// * The splat value is a consecutive sequence of right-most bits.
627//
628// This function looks through ISD::BITCAST nodes.
629// TODO: This might not be appropriate for big-endian MSA since BITCAST is
630// sometimes a shuffle in big-endian mode.
631bool MipsSEDAGToDAGISel::selectVSplatMaskR(SDValue N, SDValue &Imm) const {
632 APInt ImmValue;
633 EVT EltTy = N->getValueType(0).getVectorElementType();
634
635 if (N->getOpcode() == ISD::BITCAST)
636 N = N->getOperand(0);
637
638 if (selectVSplat(N.getNode(), ImmValue) &&
639 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
640 // Extract the run of set bits starting with bit zero, and test that the
641 // result is the same as the original value
642 if (ImmValue == (ImmValue & ~(ImmValue + 1))) {
643 Imm = CurDAG->getTargetConstant(ImmValue.countPopulation(), EltTy);
644 return true;
645 }
646 }
647
648 return false;
649}
650
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000651bool MipsSEDAGToDAGISel::selectVSplatUimmInvPow2(SDValue N,
652 SDValue &Imm) const {
653 APInt ImmValue;
654 EVT EltTy = N->getValueType(0).getVectorElementType();
655
656 if (N->getOpcode() == ISD::BITCAST)
657 N = N->getOperand(0);
658
659 if (selectVSplat(N.getNode(), ImmValue) &&
660 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
661 int32_t Log2 = (~ImmValue).exactLogBase2();
662
663 if (Log2 != -1) {
664 Imm = CurDAG->getTargetConstant(Log2, EltTy);
665 return true;
666 }
667 }
668
669 return false;
670}
671
Akira Hatanaka040d2252013-03-14 18:33:23 +0000672std::pair<bool, SDNode*> MipsSEDAGToDAGISel::selectNode(SDNode *Node) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000673 unsigned Opcode = Node->getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000674 SDLoc DL(Node);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000675
676 ///
677 // Instruction Selection not handled by the auto-generated
678 // tablegen selection should be handled here.
679 ///
Akira Hatanaka30a84782013-03-14 18:27:31 +0000680 SDNode *Result;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000681
682 switch(Opcode) {
683 default: break;
684
Akira Hatanakab8835b82013-03-14 18:39:25 +0000685 case ISD::SUBE: {
686 SDValue InFlag = Node->getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000687 unsigned Opc = Subtarget->isGP64bit() ? Mips::DSUBu : Mips::SUBu;
688 Result = selectAddESubE(Opc, InFlag, InFlag.getOperand(0), DL, Node);
Akira Hatanakab8835b82013-03-14 18:39:25 +0000689 return std::make_pair(true, Result);
690 }
691
Akira Hatanaka30a84782013-03-14 18:27:31 +0000692 case ISD::ADDE: {
Eric Christopher22405e42014-07-10 17:26:51 +0000693 if (Subtarget->hasDSP()) // Select DSP instructions, ADDSC and ADDWC.
Akira Hatanaka2f088222013-04-13 00:55:41 +0000694 break;
Akira Hatanakab8835b82013-03-14 18:39:25 +0000695 SDValue InFlag = Node->getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000696 unsigned Opc = Subtarget->isGP64bit() ? Mips::DADDu : Mips::ADDu;
697 Result = selectAddESubE(Opc, InFlag, InFlag.getValue(0), DL, Node);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000698 return std::make_pair(true, Result);
699 }
700
Akira Hatanaka30a84782013-03-14 18:27:31 +0000701 case ISD::ConstantFP: {
702 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
703 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
Eric Christopher22405e42014-07-10 17:26:51 +0000704 if (Subtarget->isGP64bit()) {
Akira Hatanaka040d2252013-03-14 18:33:23 +0000705 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000706 Mips::ZERO_64, MVT::i64);
Akira Hatanaka040d2252013-03-14 18:33:23 +0000707 Result = CurDAG->getMachineNode(Mips::DMTC1, DL, MVT::f64, Zero);
Eric Christopher22405e42014-07-10 17:26:51 +0000708 } else if (Subtarget->isFP64bit()) {
Daniel Sanders08d3cd12013-11-18 13:12:43 +0000709 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
710 Mips::ZERO, MVT::i32);
711 Result = CurDAG->getMachineNode(Mips::BuildPairF64_64, DL, MVT::f64,
712 Zero, Zero);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000713 } else {
Akira Hatanaka040d2252013-03-14 18:33:23 +0000714 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000715 Mips::ZERO, MVT::i32);
Akira Hatanaka040d2252013-03-14 18:33:23 +0000716 Result = CurDAG->getMachineNode(Mips::BuildPairF64, DL, MVT::f64, Zero,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000717 Zero);
718 }
719
720 return std::make_pair(true, Result);
721 }
722 break;
723 }
724
725 case ISD::Constant: {
726 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node);
727 unsigned Size = CN->getValueSizeInBits(0);
728
729 if (Size == 32)
730 break;
731
732 MipsAnalyzeImmediate AnalyzeImm;
733 int64_t Imm = CN->getSExtValue();
734
735 const MipsAnalyzeImmediate::InstSeq &Seq =
736 AnalyzeImm.Analyze(Imm, Size, false);
737
738 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000739 SDLoc DL(CN);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000740 SDNode *RegOpnd;
741 SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
742 MVT::i64);
743
744 // The first instruction can be a LUi which is different from other
745 // instructions (ADDiu, ORI and SLL) in that it does not have a register
746 // operand.
747 if (Inst->Opc == Mips::LUi64)
748 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
749 else
750 RegOpnd =
751 CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
752 CurDAG->getRegister(Mips::ZERO_64, MVT::i64),
753 ImmOpnd);
754
755 // The remaining instructions in the sequence are handled here.
756 for (++Inst; Inst != Seq.end(); ++Inst) {
757 ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
758 MVT::i64);
759 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
760 SDValue(RegOpnd, 0), ImmOpnd);
761 }
762
763 return std::make_pair(true, RegOpnd);
764 }
765
Daniel Sandersf9aa1d12013-08-28 10:26:24 +0000766 case ISD::INTRINSIC_W_CHAIN: {
767 switch (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
768 default:
769 break;
770
771 case Intrinsic::mips_cfcmsa: {
772 SDValue ChainIn = Node->getOperand(0);
773 SDValue RegIdx = Node->getOperand(2);
774 SDValue Reg = CurDAG->getCopyFromReg(ChainIn, DL,
775 getMSACtrlReg(RegIdx), MVT::i32);
776 return std::make_pair(true, Reg.getNode());
777 }
778 }
779 break;
780 }
781
Daniel Sandersba9c8502013-08-28 10:44:47 +0000782 case ISD::INTRINSIC_WO_CHAIN: {
783 switch (cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue()) {
784 default:
785 break;
786
787 case Intrinsic::mips_move_v:
788 // Like an assignment but will always produce a move.v even if
789 // unnecessary.
790 return std::make_pair(true,
791 CurDAG->getMachineNode(Mips::MOVE_V, DL,
792 Node->getValueType(0),
793 Node->getOperand(1)));
794 }
795 break;
796 }
797
Daniel Sandersf9aa1d12013-08-28 10:26:24 +0000798 case ISD::INTRINSIC_VOID: {
799 switch (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
800 default:
801 break;
802
803 case Intrinsic::mips_ctcmsa: {
804 SDValue ChainIn = Node->getOperand(0);
805 SDValue RegIdx = Node->getOperand(2);
806 SDValue Value = Node->getOperand(3);
807 SDValue ChainOut = CurDAG->getCopyToReg(ChainIn, DL,
808 getMSACtrlReg(RegIdx), Value);
809 return std::make_pair(true, ChainOut.getNode());
810 }
811 }
812 break;
813 }
814
Akira Hatanaka30a84782013-03-14 18:27:31 +0000815 case MipsISD::ThreadPointer: {
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000816 EVT PtrVT = getTargetLowering()->getPointerTy();
Akira Hatanaka85ccf232013-08-08 21:37:32 +0000817 unsigned RdhwrOpc, DestReg;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000818
819 if (PtrVT == MVT::i32) {
820 RdhwrOpc = Mips::RDHWR;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000821 DestReg = Mips::V1;
822 } else {
823 RdhwrOpc = Mips::RDHWR64;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000824 DestReg = Mips::V1_64;
825 }
826
827 SDNode *Rdhwr =
Andrew Trickef9de2a2013-05-25 02:42:55 +0000828 CurDAG->getMachineNode(RdhwrOpc, SDLoc(Node),
Akira Hatanaka30a84782013-03-14 18:27:31 +0000829 Node->getValueType(0),
Akira Hatanaka85ccf232013-08-08 21:37:32 +0000830 CurDAG->getRegister(Mips::HWR29, MVT::i32));
Akira Hatanaka040d2252013-03-14 18:33:23 +0000831 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), DL, DestReg,
Akira Hatanaka30a84782013-03-14 18:27:31 +0000832 SDValue(Rdhwr, 0));
Akira Hatanaka040d2252013-03-14 18:33:23 +0000833 SDValue ResNode = CurDAG->getCopyFromReg(Chain, DL, DestReg, PtrVT);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000834 ReplaceUses(SDValue(Node, 0), ResNode);
835 return std::make_pair(true, ResNode.getNode());
836 }
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000837
Daniel Sandersf49dd822013-09-24 13:33:07 +0000838 case ISD::BUILD_VECTOR: {
839 // Select appropriate ldi.[bhwd] instructions for constant splats of
840 // 128-bit when MSA is enabled. Fixup any register class mismatches that
841 // occur as a result.
842 //
843 // This allows the compiler to use a wider range of immediates than would
844 // otherwise be allowed. If, for example, v4i32 could only use ldi.h then
845 // it would not be possible to load { 0x01010101, 0x01010101, 0x01010101,
846 // 0x01010101 } without using a constant pool. This would be sub-optimal
847 // when // 'ldi.b wd, 1' is capable of producing that bit-pattern in the
848 // same set/ of registers. Similarly, ldi.h isn't capable of producing {
849 // 0x00000000, 0x00000001, 0x00000000, 0x00000001 } but 'ldi.d wd, 1' can.
850
851 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Node);
852 APInt SplatValue, SplatUndef;
853 unsigned SplatBitSize;
854 bool HasAnyUndefs;
855 unsigned LdiOp;
856 EVT ResVecTy = BVN->getValueType(0);
857 EVT ViaVecTy;
858
Eric Christopher22405e42014-07-10 17:26:51 +0000859 if (!Subtarget->hasMSA() || !BVN->getValueType(0).is128BitVector())
Craig Topper062a2ba2014-04-25 05:30:21 +0000860 return std::make_pair(false, nullptr);
Daniel Sandersf49dd822013-09-24 13:33:07 +0000861
862 if (!BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
863 HasAnyUndefs, 8,
Eric Christopher22405e42014-07-10 17:26:51 +0000864 !Subtarget->isLittle()))
Craig Topper062a2ba2014-04-25 05:30:21 +0000865 return std::make_pair(false, nullptr);
Daniel Sandersf49dd822013-09-24 13:33:07 +0000866
867 switch (SplatBitSize) {
868 default:
Craig Topper062a2ba2014-04-25 05:30:21 +0000869 return std::make_pair(false, nullptr);
Daniel Sandersf49dd822013-09-24 13:33:07 +0000870 case 8:
871 LdiOp = Mips::LDI_B;
872 ViaVecTy = MVT::v16i8;
873 break;
874 case 16:
875 LdiOp = Mips::LDI_H;
876 ViaVecTy = MVT::v8i16;
877 break;
878 case 32:
879 LdiOp = Mips::LDI_W;
880 ViaVecTy = MVT::v4i32;
881 break;
882 case 64:
883 LdiOp = Mips::LDI_D;
884 ViaVecTy = MVT::v2i64;
885 break;
886 }
887
888 if (!SplatValue.isSignedIntN(10))
Craig Topper062a2ba2014-04-25 05:30:21 +0000889 return std::make_pair(false, nullptr);
Daniel Sandersf49dd822013-09-24 13:33:07 +0000890
891 SDValue Imm = CurDAG->getTargetConstant(SplatValue,
892 ViaVecTy.getVectorElementType());
893
894 SDNode *Res = CurDAG->getMachineNode(LdiOp, SDLoc(Node), ViaVecTy, Imm);
895
896 if (ResVecTy != ViaVecTy) {
897 // If LdiOp is writing to a different register class to ResVecTy, then
898 // fix it up here. This COPY_TO_REGCLASS should never cause a move.v
899 // since the source and destination register sets contain the same
900 // registers.
901 const TargetLowering *TLI = getTargetLowering();
902 MVT ResVecTySimple = ResVecTy.getSimpleVT();
903 const TargetRegisterClass *RC = TLI->getRegClassFor(ResVecTySimple);
904 Res = CurDAG->getMachineNode(Mips::COPY_TO_REGCLASS, SDLoc(Node),
905 ResVecTy, SDValue(Res, 0),
906 CurDAG->getTargetConstant(RC->getID(),
907 MVT::i32));
908 }
909
910 return std::make_pair(true, Res);
911 }
912
Akira Hatanaka30a84782013-03-14 18:27:31 +0000913 }
914
Craig Topper062a2ba2014-04-25 05:30:21 +0000915 return std::make_pair(false, nullptr);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000916}
917
918FunctionPass *llvm::createMipsSEISelDag(MipsTargetMachine &TM) {
919 return new MipsSEDAGToDAGISel(TM);
920}