LLVM 24.0.0git
MipsSEISelDAGToDAG.cpp
Go to the documentation of this file.
1//===-- MipsSEISelDAGToDAG.cpp - A Dag to Dag Inst Selector for MipsSE ----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Subclass of MipsDAGToDAGISel specialized for mips32/64.
10//
11//===----------------------------------------------------------------------===//
12
13#include "MipsSEISelDAGToDAG.h"
14#include "Mips.h"
16#include "MipsMachineFunction.h"
17#include "MipsRegisterInfo.h"
24#include "llvm/IR/Dominators.h"
25#include "llvm/IR/GlobalValue.h"
27#include "llvm/IR/Intrinsics.h"
28#include "llvm/IR/IntrinsicsMips.h"
29#include "llvm/IR/Type.h"
33using namespace llvm;
34
35#define DEBUG_TYPE "mips-isel"
36
37bool MipsSEDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
39 if (Subtarget->inMips16Mode())
40 return false;
42}
43
47
48void MipsSEDAGToDAGISel::addDSPCtrlRegOperands(bool IsDef, MachineInstr &MI,
49 MachineFunction &MF) {
51 unsigned Mask = MI.getOperand(1).getImm();
52 RegState Flag =
54
55 if (Mask & 1)
56 MIB.addReg(Mips::DSPPos, Flag);
57
58 if (Mask & 2)
59 MIB.addReg(Mips::DSPSCount, Flag);
60
61 if (Mask & 4)
62 MIB.addReg(Mips::DSPCarry, Flag);
63
64 if (Mask & 8)
65 MIB.addReg(Mips::DSPOutFlag, Flag);
66
67 if (Mask & 16)
68 MIB.addReg(Mips::DSPCCond, Flag);
69
70 if (Mask & 32)
71 MIB.addReg(Mips::DSPEFI, Flag);
72}
73
74MCRegister MipsSEDAGToDAGISel::getMSACtrlReg(const SDValue RegIdx) const {
75 uint64_t RegNum = RegIdx->getAsZExtVal();
76 return Mips::MSACtrlRegClass.getRegister(RegNum);
77}
78
79bool MipsSEDAGToDAGISel::replaceUsesWithZeroReg(MachineRegisterInfo *MRI,
80 const MachineInstr& MI) {
81 unsigned DstReg = 0, ZeroReg = 0;
82
83 // Check if MI is "addiu $dst, $zero, 0" or "daddiu $dst, $zero, 0".
84 if ((MI.getOpcode() == Mips::ADDiu) &&
85 (MI.getOperand(1).getReg() == Mips::R0) && (MI.getOperand(2).isImm()) &&
86 (MI.getOperand(2).getImm() == 0)) {
87 DstReg = MI.getOperand(0).getReg();
88 ZeroReg = Mips::R0;
89 } else if ((MI.getOpcode() == Mips::DADDiu) &&
90 (MI.getOperand(1).getReg() == Mips::R0_64) &&
91 (MI.getOperand(2).isImm()) && (MI.getOperand(2).getImm() == 0)) {
92 DstReg = MI.getOperand(0).getReg();
93 ZeroReg = Mips::R0_64;
94 }
95
96 if (!DstReg)
97 return false;
98
99 // Replace uses with ZeroReg.
101 E = MRI->use_end(); U != E;) {
102 MachineOperand &MO = *U;
103 unsigned OpNo = U.getOperandNo();
104 MachineInstr *MI = MO.getParent();
105 ++U;
106
107 // Do not replace if it is a phi's operand or is tied to def operand.
108 if (MI->isPHI() || MI->isRegTiedToDefOperand(OpNo) || MI->isPseudo())
109 continue;
110
111 // Also, we have to check that the register class of the operand
112 // contains the zero register.
113 if (!MRI->getRegClass(MO.getReg())->contains(ZeroReg))
114 continue;
115
116 MO.setReg(ZeroReg);
117 }
118
119 return true;
120}
121
122void MipsSEDAGToDAGISel::emitMCountABI(MachineInstr &MI, MachineBasicBlock &MBB,
123 MachineFunction &MF) {
124 MachineInstrBuilder MIB(MF, &MI);
125 if (!Subtarget->isABI_O32()) { // N32, N64
126 // Save current return address.
127 BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(Mips::OR64))
128 .addDef(Mips::R1_64)
130 .addUse(Mips::R0_64);
131 // Stops instruction above from being removed later on.
132 MIB.addUse(Mips::R1_64, RegState::Implicit);
133 } else { // O32
134 // Save current return address.
135 BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(Mips::OR))
136 .addDef(Mips::R1)
138 .addUse(Mips::R0);
139 // _mcount pops 2 words from stack.
140 BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(Mips::ADDiu))
143 .addImm(-8);
144 // Stops first instruction above from being removed later on.
145 MIB.addUse(Mips::R1, RegState::Implicit);
146 }
147}
148
149void MipsSEDAGToDAGISel::processFunctionAfterISel(MachineFunction &MF) {
150 MF.getInfo<MipsFunctionInfo>()->initGlobalBaseReg(MF);
151
152 MachineRegisterInfo *MRI = &MF.getRegInfo();
153
154 for (auto &MBB: MF) {
155 for (auto &MI: MBB) {
156 switch (MI.getOpcode()) {
157 case Mips::RDDSP:
158 addDSPCtrlRegOperands(false, MI, MF);
159 break;
160 case Mips::WRDSP:
161 addDSPCtrlRegOperands(true, MI, MF);
162 break;
163 case Mips::BuildPairF64_64:
164 case Mips::ExtractElementF64_64:
165 if (!Subtarget->useOddSPReg() || !Subtarget->hasMTHC1()) {
166 MI.addOperand(MachineOperand::CreateReg(Mips::SP, false, true));
167 break;
168 }
169 [[fallthrough]];
170 case Mips::BuildPairF64:
171 case Mips::ExtractElementF64:
172 if (Subtarget->isABI_FPXX() && !Subtarget->hasMTHC1())
173 MI.addOperand(MachineOperand::CreateReg(Mips::SP, false, true));
174 break;
175 case Mips::JAL:
176 case Mips::JAL_MM:
177 if (MI.getOperand(0).isGlobal() &&
178 MI.getOperand(0).getGlobal()->hasExternalLinkage() &&
179 MI.getOperand(0).getGlobal()->getName() == "_mcount")
180 emitMCountABI(MI, MBB, MF);
181 break;
182 case Mips::JALRPseudo:
183 case Mips::JALR64Pseudo:
184 case Mips::JALR16_MM:
185 if (MI.getOperand(2).isMCSymbol() &&
186 MI.getOperand(2).getMCSymbol()->getName() == "_mcount")
187 emitMCountABI(MI, MBB, MF);
188 break;
189 case Mips::JALR:
190 if (MI.getOperand(3).isMCSymbol() &&
191 MI.getOperand(3).getMCSymbol()->getName() == "_mcount")
192 emitMCountABI(MI, MBB, MF);
193 break;
194 default:
195 replaceUsesWithZeroReg(MRI, MI);
196 }
197 }
198 }
199}
200
201void MipsSEDAGToDAGISel::selectAddE(SDNode *Node, const SDLoc &DL) const {
202 SDValue InGlue = Node->getOperand(2);
203 unsigned Opc = InGlue.getOpcode();
204 SDValue LHS = Node->getOperand(0), RHS = Node->getOperand(1);
205 EVT VT = LHS.getValueType();
206
207 // In the base case, we can rely on the carry bit from the addsc
208 // instruction.
209 if (Opc == ISD::ADDC) {
210 SDValue Ops[3] = {LHS, RHS, InGlue};
211 CurDAG->SelectNodeTo(Node, Mips::ADDWC, VT, MVT::Glue, Ops);
212 return;
213 }
214
215 assert(Opc == ISD::ADDE && "ISD::ADDE not in a chain of ADDE nodes!");
216
217 // The more complex case is when there is a chain of ISD::ADDE nodes like:
218 // (adde (adde (adde (addc a b) c) d) e).
219 //
220 // The addwc instruction does not write to the carry bit, instead it writes
221 // to bit 20 of the dsp control register. To match this series of nodes, each
222 // intermediate adde node must be expanded to write the carry bit before the
223 // addition.
224
225 // Start by reading the overflow field for addsc and moving the value to the
226 // carry field. The usage of 1 here with MipsISD::RDDSP / Mips::WRDSP
227 // corresponds to reading/writing the entire control register to/from a GPR.
228
229 SDValue CstOne = CurDAG->getTargetConstant(1, DL, MVT::i32);
230
231 SDValue OuFlag = CurDAG->getTargetConstant(20, DL, MVT::i32);
232
233 SDNode *DSPCtrlField = CurDAG->getMachineNode(Mips::RDDSP, DL, MVT::i32,
234 MVT::Glue, CstOne, InGlue);
235
236 SDNode *Carry = CurDAG->getMachineNode(
237 Mips::EXT, DL, MVT::i32, SDValue(DSPCtrlField, 0), OuFlag, CstOne);
238
239 SDValue Ops[4] = {SDValue(DSPCtrlField, 0),
240 CurDAG->getTargetConstant(6, DL, MVT::i32), CstOne,
241 SDValue(Carry, 0)};
242 SDNode *DSPCFWithCarry = CurDAG->getMachineNode(Mips::INS, DL, MVT::i32, Ops);
243
244 // My reading of the MIPS DSP 3.01 specification isn't as clear as I
245 // would like about whether bit 20 always gets overwritten by addwc.
246 // Hence take an extremely conservative view and presume it's sticky. We
247 // therefore need to clear it.
248
249 SDValue Zero = CurDAG->getRegister(Mips::R0, MVT::i32);
250
251 SDValue InsOps[4] = {Zero, OuFlag, CstOne, SDValue(DSPCFWithCarry, 0)};
252 SDNode *DSPCtrlFinal =
253 CurDAG->getMachineNode(Mips::INS, DL, MVT::i32, InsOps);
254
255 SDNode *WrDSP = CurDAG->getMachineNode(Mips::WRDSP, DL, MVT::Glue,
256 SDValue(DSPCtrlFinal, 0), CstOne);
257
258 SDValue Operands[3] = {LHS, RHS, SDValue(WrDSP, 0)};
259 CurDAG->SelectNodeTo(Node, Mips::ADDWC, VT, MVT::Glue, Operands);
260}
261
262/// Match frameindex
263bool MipsSEDAGToDAGISel::selectAddrFrameIndex(SDValue Addr, SDValue &Base,
264 SDValue &Offset) const {
265 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
266 EVT ValTy = Addr.getValueType();
267
268 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
269 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), ValTy);
270 return true;
271 }
272 return false;
273}
274
275/// Match frameindex+offset and frameindex|offset
276bool MipsSEDAGToDAGISel::selectAddrFrameIndexOffset(
277 SDValue Addr, SDValue &Base, SDValue &Offset, unsigned OffsetBits,
278 unsigned ShiftAmount = 0) const {
279 if (CurDAG->isBaseWithConstantOffset(Addr)) {
280 auto *CN = cast<ConstantSDNode>(Addr.getOperand(1));
281 if (isIntN(OffsetBits + ShiftAmount, CN->getSExtValue())) {
282 EVT ValTy = Addr.getValueType();
283
284 // If the first operand is a FI, get the TargetFI Node
285 if (FrameIndexSDNode *FIN =
287 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
288 else {
289 Base = Addr.getOperand(0);
290 // If base is a FI, additional offset calculation is done in
291 // eliminateFrameIndex, otherwise we need to check the alignment
292 const Align Alignment = Align::fromLog2(ShiftAmount);
293 if (!isAligned(Alignment, CN->getZExtValue()))
294 return false;
295 }
296
297 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), SDLoc(Addr),
298 ValTy);
299 // On MIPS I a double is loaded or stored with two lwc1 or swc1, so fold
300 // the %lo of the address into both. Instead of:
301 // lui $2, %hi(sym)
302 // addiu $2, $2, %lo(sym)
303 // lwc1 $f0, 0($2)
304 // lwc1 $f1, 4($2)
305 // Generate:
306 // lui $2, %hi(sym)
307 // lwc1 $f0, %lo(sym)($2)
308 // lwc1 $f1, %lo(sym+4)($2)
309 // Only fold an offset within the alignment of the symbol, so %hi(sym)
310 // is also %hi(sym+offset).
311 if (Base.getOpcode() == ISD::ADD && !Subtarget->hasMips2() &&
312 (Base.getOperand(1).getOpcode() == MipsISD::Lo ||
313 Base.getOperand(1).getOpcode() == MipsISD::GPRel)) {
314 SDValue Sym = Base.getOperand(1).getOperand(0);
315 int64_t Off = CN->getSExtValue();
316 SDValue SymOffset;
317 if (auto *GA = dyn_cast<GlobalAddressSDNode>(Sym)) {
319 GA->getGlobal()->getPointerAlignment(CurDAG->getDataLayout()),
320 GA->getOffset());
321 if (Off >= 0 && (uint64_t)Off < A.value())
322 SymOffset = CurDAG->getTargetGlobalAddress(
323 GA->getGlobal(), SDLoc(Addr), ValTy, GA->getOffset() + Off,
324 GA->getTargetFlags());
325 } else if (auto *CP = dyn_cast<ConstantPoolSDNode>(Sym)) {
326 Align A = commonAlignment(CP->getAlign(), CP->getOffset());
327 if (Off >= 0 && (uint64_t)Off < A.value())
328 SymOffset = CP->isMachineConstantPoolEntry()
329 ? CurDAG->getTargetConstantPool(
330 CP->getMachineCPVal(), ValTy, CP->getAlign(),
331 CP->getOffset() + Off, CP->getTargetFlags())
332 : CurDAG->getTargetConstantPool(
333 CP->getConstVal(), ValTy, CP->getAlign(),
334 CP->getOffset() + Off, CP->getTargetFlags());
335 }
336 if (SymOffset) {
337 Base = Base.getOperand(0);
338 Offset = SymOffset;
339 }
340 }
341 return true;
342 }
343 }
344 return false;
345}
346
347/// ComplexPattern used on MipsInstrInfo
348/// Used on Mips Load/Store instructions
349bool MipsSEDAGToDAGISel::selectAddrRegImm(SDValue Addr, SDValue &Base,
350 SDValue &Offset) const {
351 // if Address is FI, get the TargetFrameIndex.
352 if (selectAddrFrameIndex(Addr, Base, Offset))
353 return true;
354
355 // on PIC code Load GA
356 if (Addr.getOpcode() == MipsISD::Wrapper) {
357 Base = Addr.getOperand(0);
358 Offset = Addr.getOperand(1);
359 return true;
360 }
361
362 if (!TM.isPositionIndependent()) {
363 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
365 return false;
366 }
367
368 // Addresses of the form FI+const or FI|const
369 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 16))
370 return true;
371
372 // Operand is a result from an ADD.
373 if (Addr.getOpcode() == ISD::ADD) {
374 // When loading from constant pools, load the lower address part in
375 // the instruction itself. Example, instead of:
376 // lui $2, %hi($CPI1_0)
377 // addiu $2, $2, %lo($CPI1_0)
378 // lwc1 $f0, 0($2)
379 // Generate:
380 // lui $2, %hi($CPI1_0)
381 // lwc1 $f0, %lo($CPI1_0)($2)
382 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo ||
383 Addr.getOperand(1).getOpcode() == MipsISD::GPRel) {
384 SDValue Opnd0 = Addr.getOperand(1).getOperand(0);
386 isa<JumpTableSDNode>(Opnd0)) {
387 Base = Addr.getOperand(0);
388 Offset = Opnd0;
389 return true;
390 }
391 }
392 }
393
394 return false;
395}
396
397/// ComplexPattern used on MipsInstrInfo
398/// Used on Mips Load/Store instructions
399bool MipsSEDAGToDAGISel::selectAddrDefault(SDValue Addr, SDValue &Base,
400 SDValue &Offset) const {
401 Base = Addr;
402 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), Addr.getValueType());
403 return true;
404}
405
406bool MipsSEDAGToDAGISel::selectIntAddr(SDValue Addr, SDValue &Base,
407 SDValue &Offset) const {
408 return selectAddrRegImm(Addr, Base, Offset) ||
409 selectAddrDefault(Addr, Base, Offset);
410}
411
412bool MipsSEDAGToDAGISel::selectAddrRegImm9(SDValue Addr, SDValue &Base,
413 SDValue &Offset) const {
414 if (selectAddrFrameIndex(Addr, Base, Offset))
415 return true;
416
417 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 9))
418 return true;
419
420 return false;
421}
422
423/// Used on microMIPS LWC2, LDC2, SWC2 and SDC2 instructions (11-bit offset)
424bool MipsSEDAGToDAGISel::selectAddrRegImm11(SDValue Addr, SDValue &Base,
425 SDValue &Offset) const {
426 if (selectAddrFrameIndex(Addr, Base, Offset))
427 return true;
428
429 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 11))
430 return true;
431
432 return false;
433}
434
435/// Used on microMIPS Load/Store unaligned instructions (12-bit offset)
436bool MipsSEDAGToDAGISel::selectAddrRegImm12(SDValue Addr, SDValue &Base,
437 SDValue &Offset) const {
438 if (selectAddrFrameIndex(Addr, Base, Offset))
439 return true;
440
441 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 12))
442 return true;
443
444 return false;
445}
446
447bool MipsSEDAGToDAGISel::selectAddrRegImm16(SDValue Addr, SDValue &Base,
448 SDValue &Offset) const {
449 if (selectAddrFrameIndex(Addr, Base, Offset))
450 return true;
451
452 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 16))
453 return true;
454
455 return false;
456}
457
458bool MipsSEDAGToDAGISel::selectIntAddr11MM(SDValue Addr, SDValue &Base,
459 SDValue &Offset) const {
460 return selectAddrRegImm11(Addr, Base, Offset) ||
461 selectAddrDefault(Addr, Base, Offset);
462}
463
464bool MipsSEDAGToDAGISel::selectIntAddr12MM(SDValue Addr, SDValue &Base,
465 SDValue &Offset) const {
466 return selectAddrRegImm12(Addr, Base, Offset) ||
467 selectAddrDefault(Addr, Base, Offset);
468}
469
470bool MipsSEDAGToDAGISel::selectIntAddr16MM(SDValue Addr, SDValue &Base,
471 SDValue &Offset) const {
472 return selectAddrRegImm16(Addr, Base, Offset) ||
473 selectAddrDefault(Addr, Base, Offset);
474}
475
476bool MipsSEDAGToDAGISel::selectIntAddrLSL2MM(SDValue Addr, SDValue &Base,
477 SDValue &Offset) const {
478 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 7)) {
480 return false;
481
482 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Offset)) {
483 unsigned CnstOff = CN->getZExtValue();
484 return (CnstOff == (CnstOff & 0x3c));
485 }
486
487 return false;
488 }
489
490 // For all other cases where "lw" would be selected, don't select "lw16"
491 // because it would result in additional instructions to prepare operands.
492 if (selectAddrRegImm(Addr, Base, Offset))
493 return false;
494
495 return selectAddrDefault(Addr, Base, Offset);
496}
497
498bool MipsSEDAGToDAGISel::selectIntAddrSImm10(SDValue Addr, SDValue &Base,
499 SDValue &Offset) const {
500
501 if (selectAddrFrameIndex(Addr, Base, Offset))
502 return true;
503
504 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10))
505 return true;
506
507 return selectAddrDefault(Addr, Base, Offset);
508}
509
510bool MipsSEDAGToDAGISel::selectIntAddrSImm10Lsl1(SDValue Addr, SDValue &Base,
511 SDValue &Offset) const {
512 if (selectAddrFrameIndex(Addr, Base, Offset))
513 return true;
514
515 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10, 1))
516 return true;
517
518 return selectAddrDefault(Addr, Base, Offset);
519}
520
521bool MipsSEDAGToDAGISel::selectIntAddrSImm10Lsl2(SDValue Addr, SDValue &Base,
522 SDValue &Offset) const {
523 if (selectAddrFrameIndex(Addr, Base, Offset))
524 return true;
525
526 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10, 2))
527 return true;
528
529 return selectAddrDefault(Addr, Base, Offset);
530}
531
532bool MipsSEDAGToDAGISel::selectIntAddrSImm10Lsl3(SDValue Addr, SDValue &Base,
533 SDValue &Offset) const {
534 if (selectAddrFrameIndex(Addr, Base, Offset))
535 return true;
536
537 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10, 3))
538 return true;
539
540 return selectAddrDefault(Addr, Base, Offset);
541}
542
543// Select constant vector splats.
544//
545// Returns true and sets Imm if:
546// * MSA is enabled
547// * N is a ISD::BUILD_VECTOR representing a constant splat
548bool MipsSEDAGToDAGISel::selectVSplat(SDNode *N, APInt &Imm,
549 unsigned MinSizeInBits) const {
550 if (!Subtarget->hasMSA())
551 return false;
552
553 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N);
554
555 if (!Node)
556 return false;
557
558 APInt SplatValue, SplatUndef;
559 unsigned SplatBitSize;
560 bool HasAnyUndefs;
561
562 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
563 MinSizeInBits, !Subtarget->isLittle()))
564 return false;
565
566 Imm = SplatValue;
567
568 return true;
569}
570
571// Select constant vector splats.
572//
573// In addition to the requirements of selectVSplat(), this function returns
574// true and sets Imm if:
575// * The splat value is the same width as the elements of the vector
576// * The splat value fits in an integer with the specified signed-ness and
577// width.
578//
579// This function looks through ISD::BITCAST nodes.
580// TODO: This might not be appropriate for big-endian MSA since BITCAST is
581// sometimes a shuffle in big-endian mode.
582//
583// It's worth noting that this function is not used as part of the selection
584// of ldi.[bhwd] since it does not permit using the wrong-typed ldi.[bhwd]
585// instruction to achieve the desired bit pattern. ldi.[bhwd] is selected in
586// MipsSEDAGToDAGISel::selectNode.
587bool MipsSEDAGToDAGISel::
588selectVSplatCommon(SDValue N, SDValue &Imm, bool Signed,
589 unsigned ImmBitSize) const {
590 APInt ImmValue;
591 EVT EltTy = N->getValueType(0).getVectorElementType();
592
593 if (N->getOpcode() == ISD::BITCAST)
594 N = N->getOperand(0);
595
596 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
597 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
598
599 if (( Signed && ImmValue.isSignedIntN(ImmBitSize)) ||
600 (!Signed && ImmValue.isIntN(ImmBitSize))) {
601 Imm = CurDAG->getTargetConstant(ImmValue, SDLoc(N), EltTy);
602 return true;
603 }
604 }
605
606 return false;
607}
608
609// Select constant vector splats whose value is a power of 2.
610//
611// In addition to the requirements of selectVSplat(), this function returns
612// true and sets Imm if:
613// * The splat value is the same width as the elements of the vector
614// * The splat value is a power of two.
615//
616// This function looks through ISD::BITCAST nodes.
617// TODO: This might not be appropriate for big-endian MSA since BITCAST is
618// sometimes a shuffle in big-endian mode.
619bool MipsSEDAGToDAGISel::selectVSplatUimmPow2(SDValue N, SDValue &Imm) const {
620 APInt ImmValue;
621 EVT EltTy = N->getValueType(0).getVectorElementType();
622
623 if (N->getOpcode() == ISD::BITCAST)
624 N = N->getOperand(0);
625
626 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
627 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
628 int32_t Log2 = ImmValue.exactLogBase2();
629
630 if (Log2 != -1) {
631 Imm = CurDAG->getTargetConstant(Log2, SDLoc(N), EltTy);
632 return true;
633 }
634 }
635
636 return false;
637}
638
639// Select constant vector splats whose value only has a consecutive sequence
640// of left-most bits set (e.g. 0b11...1100...00).
641//
642// In addition to the requirements of selectVSplat(), this function returns
643// true and sets Imm if:
644// * The splat value is the same width as the elements of the vector
645// * The splat value is a consecutive sequence of left-most bits.
646//
647// This function looks through ISD::BITCAST nodes.
648// TODO: This might not be appropriate for big-endian MSA since BITCAST is
649// sometimes a shuffle in big-endian mode.
650bool MipsSEDAGToDAGISel::selectVSplatMaskL(SDValue N, SDValue &Imm) const {
651 APInt ImmValue;
652 EVT EltTy = N->getValueType(0).getVectorElementType();
653
654 if (N->getOpcode() == ISD::BITCAST)
655 N = N->getOperand(0);
656
657 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
658 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
659 // Check if we have a leading one, then check if the whole value is a
660 // shifted mask.
661 if (ImmValue.isNegative() && ImmValue.isShiftedMask()) {
662 Imm = CurDAG->getTargetConstant(ImmValue.popcount() - 1, SDLoc(N), EltTy);
663 return true;
664 }
665 }
666
667 return false;
668}
669
670// Select constant vector splats whose value only has a consecutive sequence
671// of right-most bits set (e.g. 0b00...0011...11).
672//
673// In addition to the requirements of selectVSplat(), this function returns
674// true and sets Imm if:
675// * The splat value is the same width as the elements of the vector
676// * The splat value is a consecutive sequence of right-most bits.
677//
678// This function looks through ISD::BITCAST nodes.
679// TODO: This might not be appropriate for big-endian MSA since BITCAST is
680// sometimes a shuffle in big-endian mode.
681bool MipsSEDAGToDAGISel::selectVSplatMaskR(SDValue N, SDValue &Imm) const {
682 APInt ImmValue;
683 EVT EltTy = N->getValueType(0).getVectorElementType();
684
685 if (N->getOpcode() == ISD::BITCAST)
686 N = N->getOperand(0);
687
688 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
689 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
690 if (ImmValue.isMask()) {
691 Imm = CurDAG->getTargetConstant(ImmValue.popcount() - 1, SDLoc(N), EltTy);
692 return true;
693 }
694 }
695
696 return false;
697}
698
699bool MipsSEDAGToDAGISel::selectVSplatUimmInvPow2(SDValue N,
700 SDValue &Imm) const {
701 APInt ImmValue;
702 EVT EltTy = N->getValueType(0).getVectorElementType();
703
704 if (N->getOpcode() == ISD::BITCAST)
705 N = N->getOperand(0);
706
707 if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
708 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
709 int32_t Log2 = (~ImmValue).exactLogBase2();
710
711 if (Log2 != -1) {
712 Imm = CurDAG->getTargetConstant(Log2, SDLoc(N), EltTy);
713 return true;
714 }
715 }
716
717 return false;
718}
719
720// Select const vector splat of 1.
721bool MipsSEDAGToDAGISel::selectVSplatImmEq1(SDValue N) const {
722 APInt ImmValue;
723 EVT EltTy = N->getValueType(0).getVectorElementType();
724
725 if (N->getOpcode() == ISD::BITCAST)
726 N = N->getOperand(0);
727
728 return selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
729 ImmValue.getBitWidth() == EltTy.getSizeInBits() && ImmValue == 1;
730}
731
732bool MipsSEDAGToDAGISel::trySelect(SDNode *Node) {
733 unsigned Opcode = Node->getOpcode();
734 SDLoc DL(Node);
735
736 ///
737 // Instruction Selection not handled by the auto-generated
738 // tablegen selection should be handled here.
739 ///
740 switch(Opcode) {
741 default: break;
742
745 MVT VT = Subtarget->isGP64bit() ? MVT::i64 : MVT::i32;
746 SDValue cond = Node->getOperand(0);
747 SDValue Hi1 = Node->getOperand(1);
748 SDValue Lo1 = Node->getOperand(2);
749 SDValue Hi2 = Node->getOperand(3);
750 SDValue Lo2 = Node->getOperand(4);
751
752 SDValue ops[] = {cond, Hi1, Lo1, Hi2, Lo2};
753 EVT NodeTys[] = {VT, VT};
754 ReplaceNode(Node, CurDAG->getMachineNode(Subtarget->isGP64bit()
755 ? Mips::PseudoD_SELECT_I64
756 : Mips::PseudoD_SELECT_I,
757 DL, NodeTys, ops));
758 return true;
759 }
760
761 case ISD::ADDE: {
762 selectAddE(Node, DL);
763 return true;
764 }
765
766 case ISD::ConstantFP: {
767 auto *CN = cast<ConstantFPSDNode>(Node);
768 if (Node->getValueType(0) == MVT::f64 && CN->isPosZero()) {
769 if (Subtarget->isGP64bit()) {
770 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
771 Mips::R0_64, MVT::i64);
772 ReplaceNode(Node,
773 CurDAG->getMachineNode(Mips::DMTC1, DL, MVT::f64, Zero));
774 } else if (Subtarget->isFP64bit()) {
775 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
776 Mips::R0, MVT::i32);
777 ReplaceNode(Node, CurDAG->getMachineNode(Mips::BuildPairF64_64, DL,
778 MVT::f64, Zero, Zero));
779 } else {
780 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
781 Mips::R0, MVT::i32);
782 ReplaceNode(Node, CurDAG->getMachineNode(Mips::BuildPairF64, DL,
783 MVT::f64, Zero, Zero));
784 }
785 return true;
786 }
787 break;
788 }
789
790 case ISD::Constant: {
791 auto *CN = cast<ConstantSDNode>(Node);
792 int64_t Imm = CN->getSExtValue();
793 unsigned Size = CN->getValueSizeInBits(0);
794
795 if (isInt<32>(Imm))
796 break;
797
798 MipsAnalyzeImmediate AnalyzeImm;
799
801 AnalyzeImm.Analyze(Imm, Size, false);
802
804 SDLoc DL(CN);
805 SDNode *RegOpnd;
806 SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
807 DL, MVT::i64);
808
809 // The first instruction can be a LUi which is different from other
810 // instructions (ADDiu, ORI and SLL) in that it does not have a register
811 // operand.
812 if (Inst->Opc == Mips::LUi64)
813 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
814 else
815 RegOpnd = CurDAG->getMachineNode(
816 Inst->Opc, DL, MVT::i64, CurDAG->getRegister(Mips::R0_64, MVT::i64),
817 ImmOpnd);
818
819 // The remaining instructions in the sequence are handled here.
820 for (++Inst; Inst != Seq.end(); ++Inst) {
821 ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd), DL,
822 MVT::i64);
823 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
824 SDValue(RegOpnd, 0), ImmOpnd);
825 }
826
827 ReplaceNode(Node, RegOpnd);
828 return true;
829 }
830
832 const unsigned IntrinsicOpcode = Node->getConstantOperandVal(1);
833 switch (IntrinsicOpcode) {
834 default:
835 break;
836
837 case Intrinsic::mips_cfcmsa: {
838 SDValue ChainIn = Node->getOperand(0);
839 SDValue RegIdx = Node->getOperand(2);
840 SDValue Reg = CurDAG->getCopyFromReg(ChainIn, DL,
841 getMSACtrlReg(RegIdx), MVT::i32);
842 ReplaceNode(Node, Reg.getNode());
843 return true;
844 }
845 case Intrinsic::mips_ldr_d:
846 case Intrinsic::mips_ldr_w: {
847 unsigned Op = (IntrinsicOpcode == Intrinsic::mips_ldr_d) ? Mips::LDR_D
848 : Mips::LDR_W;
849
850 SDLoc DL(Node);
851 assert(Node->getNumOperands() == 4 && "Unexpected number of operands.");
852 const SDValue &Chain = Node->getOperand(0);
853 const SDValue &Intrinsic = Node->getOperand(1);
854 const SDValue &Pointer = Node->getOperand(2);
855 const SDValue &Constant = Node->getOperand(3);
856
857 assert(Chain.getValueType() == MVT::Other);
858 (void)Intrinsic;
859 assert(Intrinsic.getOpcode() == ISD::TargetConstant &&
860 Constant.getOpcode() == ISD::Constant &&
861 "Invalid instruction operand.");
862
863 // Convert Constant to TargetConstant.
864 const ConstantInt *Val =
865 cast<ConstantSDNode>(Constant)->getConstantIntValue();
866 SDValue Imm =
867 CurDAG->getTargetConstant(*Val, DL, Constant.getValueType());
868
870
871 assert(Node->getNumValues() == 2);
872 assert(Node->getValueType(0).is128BitVector());
873 assert(Node->getValueType(1) == MVT::Other);
874 SmallVector<EVT, 2> ResTys{Node->getValueType(0), Node->getValueType(1)};
875
876 ReplaceNode(Node, CurDAG->getMachineNode(Op, DL, ResTys, Ops));
877
878 return true;
879 }
880 }
881 break;
882 }
883
885 switch (Node->getConstantOperandVal(0)) {
886 default:
887 break;
888
889 case Intrinsic::mips_move_v:
890 // Like an assignment but will always produce a move.v even if
891 // unnecessary.
892 ReplaceNode(Node, CurDAG->getMachineNode(Mips::MOVE_V, DL,
893 Node->getValueType(0),
894 Node->getOperand(1)));
895 return true;
896 }
897 break;
898 }
899
900 case ISD::INTRINSIC_VOID: {
901 const unsigned IntrinsicOpcode = Node->getConstantOperandVal(1);
902 switch (IntrinsicOpcode) {
903 default:
904 break;
905
906 case Intrinsic::mips_ctcmsa: {
907 SDValue ChainIn = Node->getOperand(0);
908 SDValue RegIdx = Node->getOperand(2);
909 SDValue Value = Node->getOperand(3);
910 SDValue ChainOut = CurDAG->getCopyToReg(ChainIn, DL,
911 getMSACtrlReg(RegIdx), Value);
912 ReplaceNode(Node, ChainOut.getNode());
913 return true;
914 }
915 case Intrinsic::mips_str_d:
916 case Intrinsic::mips_str_w: {
917 unsigned Op = (IntrinsicOpcode == Intrinsic::mips_str_d) ? Mips::STR_D
918 : Mips::STR_W;
919
920 SDLoc DL(Node);
921 assert(Node->getNumOperands() == 5 && "Unexpected number of operands.");
922 const SDValue &Chain = Node->getOperand(0);
923 const SDValue &Intrinsic = Node->getOperand(1);
924 const SDValue &Vec = Node->getOperand(2);
925 const SDValue &Pointer = Node->getOperand(3);
926 const SDValue &Constant = Node->getOperand(4);
927
928 assert(Chain.getValueType() == MVT::Other);
929 (void)Intrinsic;
930 assert(Intrinsic.getOpcode() == ISD::TargetConstant &&
931 Constant.getOpcode() == ISD::Constant &&
932 "Invalid instruction operand.");
933
934 // Convert Constant to TargetConstant.
935 const ConstantInt *Val =
936 cast<ConstantSDNode>(Constant)->getConstantIntValue();
937 SDValue Imm =
938 CurDAG->getTargetConstant(*Val, DL, Constant.getValueType());
939
941
942 assert(Node->getNumValues() == 1);
943 assert(Node->getValueType(0) == MVT::Other);
944 SmallVector<EVT, 1> ResTys{Node->getValueType(0)};
945
946 ReplaceNode(Node, CurDAG->getMachineNode(Op, DL, ResTys, Ops));
947 return true;
948 }
949 }
950 break;
951 }
952
953 case MipsISD::FAbs: {
954 MVT ResTy = Node->getSimpleValueType(0);
955 assert((ResTy == MVT::f64 || ResTy == MVT::f32) &&
956 "Unsupported float type!");
957 unsigned Opc = 0;
958 if (ResTy == MVT::f64)
959 Opc = (Subtarget->isFP64bit() ? Mips::FABS_D64 : Mips::FABS_D32);
960 else
961 Opc = Mips::FABS_S;
962
963 if (Subtarget->inMicroMipsMode()) {
964 switch (Opc) {
965 case Mips::FABS_D64:
966 Opc = Mips::FABS_D64_MM;
967 break;
968 case Mips::FABS_D32:
969 Opc = Mips::FABS_D32_MM;
970 break;
971 case Mips::FABS_S:
972 Opc = Mips::FABS_S_MM;
973 break;
974 default:
975 llvm_unreachable("Unknown opcode for MIPS floating point abs!");
976 }
977 }
978
979 ReplaceNode(Node,
980 CurDAG->getMachineNode(Opc, DL, ResTy, Node->getOperand(0)));
981
982 return true;
983 }
984
985 // Manually match MipsISD::Ins nodes to get the correct instruction. It has
986 // to be done in this fashion so that we respect the differences between
987 // dins and dinsm, as the difference is that the size operand has the range
988 // 0 < size <= 32 for dins while dinsm has the range 2 <= size <= 64 which
989 // means SelectionDAGISel would have to test all the operands at once to
990 // match the instruction.
991 case MipsISD::Ins: {
992
993 // Validating the node operands.
994 if (Node->getValueType(0) != MVT::i32 && Node->getValueType(0) != MVT::i64)
995 return false;
996
997 if (Node->getNumOperands() != 4)
998 return false;
999
1000 if (Node->getOperand(1)->getOpcode() != ISD::Constant ||
1001 Node->getOperand(2)->getOpcode() != ISD::Constant)
1002 return false;
1003
1004 MVT ResTy = Node->getSimpleValueType(0);
1005 uint64_t Pos = Node->getConstantOperandVal(1);
1006 uint64_t Size = Node->getConstantOperandVal(2);
1007
1008 // Size has to be >0 for 'ins', 'dins' and 'dinsu'.
1009 if (!Size)
1010 return false;
1011
1012 if (Pos + Size > 64)
1013 return false;
1014
1015 if (ResTy != MVT::i32 && ResTy != MVT::i64)
1016 return false;
1017
1018 unsigned Opcode = 0;
1019 if (ResTy == MVT::i32) {
1020 if (Pos + Size <= 32)
1021 Opcode = Mips::INS;
1022 } else {
1023 if (Pos + Size <= 32)
1024 Opcode = Mips::DINS;
1025 else if (Pos < 32 && 1 < Size)
1026 Opcode = Mips::DINSM;
1027 else
1028 Opcode = Mips::DINSU;
1029 }
1030
1031 if (Opcode) {
1032 SDValue Ops[4] = {
1033 Node->getOperand(0), CurDAG->getTargetConstant(Pos, DL, MVT::i32),
1034 CurDAG->getTargetConstant(Size, DL, MVT::i32), Node->getOperand(3)};
1035
1036 ReplaceNode(Node, CurDAG->getMachineNode(Opcode, DL, ResTy, Ops));
1037 return true;
1038 }
1039
1040 return false;
1041 }
1042
1043 case MipsISD::ThreadPointer: {
1044 EVT PtrVT = getTargetLowering()->getPointerTy(CurDAG->getDataLayout());
1045 unsigned RdhwrOpc, DestReg;
1046
1047 if (PtrVT == MVT::i32) {
1048 RdhwrOpc = Mips::RDHWR;
1049 DestReg = Mips::R3;
1050 } else {
1051 RdhwrOpc = Mips::RDHWR64;
1052 DestReg = Mips::R3_64;
1053 }
1054
1055 SDNode *Rdhwr =
1056 CurDAG->getMachineNode(RdhwrOpc, DL, Node->getValueType(0), MVT::Glue,
1057 CurDAG->getRegister(Mips::HWR29, MVT::i32),
1058 CurDAG->getTargetConstant(0, DL, MVT::i32));
1059 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), DL, DestReg,
1060 SDValue(Rdhwr, 0), SDValue(Rdhwr, 1));
1061 SDValue ResNode = CurDAG->getCopyFromReg(Chain, DL, DestReg, PtrVT,
1062 Chain.getValue(1));
1063 ReplaceNode(Node, ResNode.getNode());
1064 return true;
1065 }
1066
1067 case ISD::BUILD_VECTOR: {
1068 // Select appropriate ldi.[bhwd] instructions for constant splats of
1069 // 128-bit when MSA is enabled. Fixup any register class mismatches that
1070 // occur as a result.
1071 //
1072 // This allows the compiler to use a wider range of immediates than would
1073 // otherwise be allowed. If, for example, v4i32 could only use ldi.h then
1074 // it would not be possible to load { 0x01010101, 0x01010101, 0x01010101,
1075 // 0x01010101 } without using a constant pool. This would be sub-optimal
1076 // when // 'ldi.b wd, 1' is capable of producing that bit-pattern in the
1077 // same set/ of registers. Similarly, ldi.h isn't capable of producing {
1078 // 0x00000000, 0x00000001, 0x00000000, 0x00000001 } but 'ldi.d wd, 1' can.
1079
1080 const MipsABIInfo &ABI = Subtarget->getABI();
1081
1082 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Node);
1083 APInt SplatValue, SplatUndef;
1084 unsigned SplatBitSize;
1085 bool HasAnyUndefs;
1086 unsigned LdiOp;
1087 EVT ResVecTy = BVN->getValueType(0);
1088 EVT ViaVecTy;
1089
1090 if (!Subtarget->hasMSA() || !BVN->getValueType(0).is128BitVector())
1091 return false;
1092
1093 // The result is retyped with COPY_TO_REGCLASS below, which does not
1094 // change the register contents. MSA lanes are laid out the same way
1095 // on big and little endian (byte 0 is the low byte of word 0), so
1096 // get the splat value in little endian order on both.
1097 if (!BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
1098 HasAnyUndefs, 8, /*IsBigEndian=*/false))
1099 return false;
1100
1101 switch (SplatBitSize) {
1102 default:
1103 return false;
1104 case 8:
1105 LdiOp = Mips::LDI_B;
1106 ViaVecTy = MVT::v16i8;
1107 break;
1108 case 16:
1109 LdiOp = Mips::LDI_H;
1110 ViaVecTy = MVT::v8i16;
1111 break;
1112 case 32:
1113 LdiOp = Mips::LDI_W;
1114 ViaVecTy = MVT::v4i32;
1115 break;
1116 case 64:
1117 LdiOp = Mips::LDI_D;
1118 ViaVecTy = MVT::v2i64;
1119 break;
1120 }
1121
1122 SDNode *Res = nullptr;
1123
1124 // If we have a signed 10 bit integer, we can splat it directly.
1125 //
1126 // If we have something bigger we can synthesize the value into a GPR and
1127 // splat from there.
1128 if (SplatValue.isSignedIntN(10)) {
1129 SDValue Imm = CurDAG->getTargetConstant(SplatValue, DL,
1130 ViaVecTy.getVectorElementType());
1131
1132 Res = CurDAG->getMachineNode(LdiOp, DL, ViaVecTy, Imm);
1133 } else if (SplatValue.isSignedIntN(16) &&
1134 ((ABI.IsO32() && SplatBitSize < 64) ||
1135 (ABI.IsN32() || ABI.IsN64()))) {
1136 // Only handle signed 16 bit values when the element size is GPR width.
1137 // MIPS64 can handle all the cases but MIPS32 would need to handle
1138 // negative cases specifically here. Instead, handle those cases as
1139 // 64bit values.
1140
1141 bool Is32BitSplat = ABI.IsO32() || SplatBitSize < 64;
1142 const unsigned ADDiuOp = Is32BitSplat ? Mips::ADDiu : Mips::DADDiu;
1143 const MVT SplatMVT = Is32BitSplat ? MVT::i32 : MVT::i64;
1144 SDValue ZeroVal =
1145 CurDAG->getRegister(Is32BitSplat ? Mips::R0 : Mips::R0_64, SplatMVT);
1146
1147 const unsigned FILLOp =
1148 SplatBitSize == 16
1149 ? Mips::FILL_H
1150 : (SplatBitSize == 32 ? Mips::FILL_W
1151 : (SplatBitSize == 64 ? Mips::FILL_D : 0));
1152
1153 assert(FILLOp != 0 && "Unknown FILL Op for splat synthesis!");
1154 assert((!ABI.IsO32() || (FILLOp != Mips::FILL_D)) &&
1155 "Attempting to use fill.d on MIPS32!");
1156
1157 const unsigned Lo = SplatValue.getLoBits(16).getZExtValue();
1158 SDValue LoVal = CurDAG->getTargetConstant(Lo, DL, SplatMVT);
1159
1160 Res = CurDAG->getMachineNode(ADDiuOp, DL, SplatMVT, ZeroVal, LoVal);
1161 Res = CurDAG->getMachineNode(FILLOp, DL, ViaVecTy, SDValue(Res, 0));
1162
1163 } else if (SplatValue.isSignedIntN(32) && SplatBitSize == 32) {
1164 // Only handle the cases where the splat size agrees with the size
1165 // of the SplatValue here.
1166 const unsigned Lo = SplatValue.getLoBits(16).getZExtValue();
1167 const unsigned Hi = SplatValue.lshr(16).getLoBits(16).getZExtValue();
1168 SDValue ZeroVal = CurDAG->getRegister(Mips::R0, MVT::i32);
1169
1170 SDValue LoVal = CurDAG->getTargetConstant(Lo, DL, MVT::i32);
1171 SDValue HiVal = CurDAG->getTargetConstant(Hi, DL, MVT::i32);
1172
1173 if (Hi)
1174 Res = CurDAG->getMachineNode(Mips::LUi, DL, MVT::i32, HiVal);
1175
1176 if (Lo)
1177 Res = CurDAG->getMachineNode(Mips::ORi, DL, MVT::i32,
1178 Hi ? SDValue(Res, 0) : ZeroVal, LoVal);
1179
1180 assert((Hi || Lo) && "Zero case reached 32 bit case splat synthesis!");
1181 Res =
1182 CurDAG->getMachineNode(Mips::FILL_W, DL, MVT::v4i32, SDValue(Res, 0));
1183
1184 } else if (SplatValue.isSignedIntN(32) && SplatBitSize == 64 &&
1185 (ABI.IsN32() || ABI.IsN64())) {
1186 // N32 and N64 can perform some tricks that O32 can't for signed 32 bit
1187 // integers due to having 64bit registers. lui will cause the necessary
1188 // zero/sign extension.
1189 const unsigned Lo = SplatValue.getLoBits(16).getZExtValue();
1190 const unsigned Hi = SplatValue.lshr(16).getLoBits(16).getZExtValue();
1191 SDValue ZeroVal = CurDAG->getRegister(Mips::R0, MVT::i32);
1192
1193 SDValue LoVal = CurDAG->getTargetConstant(Lo, DL, MVT::i32);
1194 SDValue HiVal = CurDAG->getTargetConstant(Hi, DL, MVT::i32);
1195
1196 if (Hi)
1197 Res = CurDAG->getMachineNode(Mips::LUi, DL, MVT::i32, HiVal);
1198
1199 if (Lo)
1200 Res = CurDAG->getMachineNode(Mips::ORi, DL, MVT::i32,
1201 Hi ? SDValue(Res, 0) : ZeroVal, LoVal);
1202
1203 Res = CurDAG->getMachineNode(
1204 Mips::SUBREG_TO_REG, DL, MVT::i64, SDValue(Res, 0),
1205 CurDAG->getTargetConstant(Mips::sub_32, DL, MVT::i64));
1206
1207 Res =
1208 CurDAG->getMachineNode(Mips::FILL_D, DL, MVT::v2i64, SDValue(Res, 0));
1209
1210 } else if (SplatValue.isSignedIntN(64)) {
1211 // If we have a 64 bit Splat value, we perform a similar sequence to the
1212 // above:
1213 //
1214 // MIPS32: MIPS64:
1215 // lui $res, %highest(val) lui $res, %highest(val)
1216 // ori $res, $res, %higher(val) ori $res, $res, %higher(val)
1217 // lui $res2, %hi(val) lui $res2, %hi(val)
1218 // ori $res2, %res2, %lo(val) ori $res2, %res2, %lo(val)
1219 // $res3 = fill $res2 dinsu $res, $res2, 0, 32
1220 // $res4 = insert.w $res3[1], $res fill.d $res
1221 // splat.d $res4, 0
1222 //
1223 // The ability to use dinsu is guaranteed as MSA requires MIPSR5.
1224 // This saves having to materialize the value by shifts and ors.
1225 //
1226 // FIXME: Implement the preferred sequence for MIPS64R6:
1227 //
1228 // MIPS64R6:
1229 // ori $res, $zero, %lo(val)
1230 // daui $res, $res, %hi(val)
1231 // dahi $res, $res, %higher(val)
1232 // dati $res, $res, %highest(cal)
1233 // fill.d $res
1234 //
1235
1236 const unsigned Lo = SplatValue.getLoBits(16).getZExtValue();
1237 const unsigned Hi = SplatValue.lshr(16).getLoBits(16).getZExtValue();
1238 const unsigned Higher = SplatValue.lshr(32).getLoBits(16).getZExtValue();
1239 const unsigned Highest = SplatValue.lshr(48).getLoBits(16).getZExtValue();
1240
1241 SDValue LoVal = CurDAG->getTargetConstant(Lo, DL, MVT::i32);
1242 SDValue HiVal = CurDAG->getTargetConstant(Hi, DL, MVT::i32);
1243 SDValue HigherVal = CurDAG->getTargetConstant(Higher, DL, MVT::i32);
1244 SDValue HighestVal = CurDAG->getTargetConstant(Highest, DL, MVT::i32);
1245 SDValue ZeroVal = CurDAG->getRegister(Mips::R0, MVT::i32);
1246
1247 // Independent of whether we're targeting MIPS64 or not, the basic
1248 // operations are the same. Also, directly use the $zero register if
1249 // the 16 bit chunk is zero.
1250 //
1251 // For optimization purposes we always synthesize the splat value as
1252 // an i32 value, then if we're targetting MIPS64, use INSERT_SUBREG
1253 // just before combining the values with dinsu to produce an i64. This
1254 // enables SelectionDAG to aggressively share components of splat values
1255 // where possible.
1256 //
1257 // FIXME: This is the general constant synthesis problem. This code
1258 // should be factored out into a class shared between all the
1259 // classes that need it. Specifically, for a splat size of 64
1260 // bits that's a negative number we can do better than LUi/ORi
1261 // for the upper 32bits.
1262
1263 if (Hi)
1264 Res = CurDAG->getMachineNode(Mips::LUi, DL, MVT::i32, HiVal);
1265
1266 if (Lo)
1267 Res = CurDAG->getMachineNode(Mips::ORi, DL, MVT::i32,
1268 Hi ? SDValue(Res, 0) : ZeroVal, LoVal);
1269
1270 SDNode *HiRes;
1271 if (Highest)
1272 HiRes = CurDAG->getMachineNode(Mips::LUi, DL, MVT::i32, HighestVal);
1273
1274 if (Higher)
1275 HiRes = CurDAG->getMachineNode(Mips::ORi, DL, MVT::i32,
1276 Highest ? SDValue(HiRes, 0) : ZeroVal,
1277 HigherVal);
1278
1279
1280 if (ABI.IsO32()) {
1281 Res = CurDAG->getMachineNode(Mips::FILL_W, DL, MVT::v4i32,
1282 (Hi || Lo) ? SDValue(Res, 0) : ZeroVal);
1283
1284 Res = CurDAG->getMachineNode(
1285 Mips::INSERT_W, DL, MVT::v4i32, SDValue(Res, 0),
1286 (Highest || Higher) ? SDValue(HiRes, 0) : ZeroVal,
1287 CurDAG->getTargetConstant(1, DL, MVT::i32));
1288
1289 const TargetLowering *TLI = getTargetLowering();
1290 const TargetRegisterClass *RC =
1291 TLI->getRegClassFor(ViaVecTy.getSimpleVT());
1292
1293 Res = CurDAG->getMachineNode(
1294 Mips::COPY_TO_REGCLASS, DL, ViaVecTy, SDValue(Res, 0),
1295 CurDAG->getTargetConstant(RC->getID(), DL, MVT::i32));
1296
1297 Res = CurDAG->getMachineNode(
1298 Mips::SPLATI_D, DL, MVT::v2i64, SDValue(Res, 0),
1299 CurDAG->getTargetConstant(0, DL, MVT::i32));
1300 } else if (ABI.IsN64() || ABI.IsN32()) {
1301
1302 SDValue Zero64Val = CurDAG->getRegister(Mips::R0_64, MVT::i64);
1303 const bool HiResNonZero = Highest || Higher;
1304 const bool ResNonZero = Hi || Lo;
1305
1306 // The high halves of HiRes and Res are not used by dinsu or dsll32.
1307 SDValue Undef64Val = SDValue(
1308 CurDAG->getMachineNode(Mips::IMPLICIT_DEF, DL, MVT::i64), 0);
1309
1310 if (HiResNonZero)
1311 HiRes = CurDAG
1312 ->getTargetInsertSubreg(Mips::sub_32, DL, MVT::i64,
1313 Undef64Val, SDValue(HiRes, 0))
1314 .getNode();
1315
1316 if (ResNonZero)
1317 Res = CurDAG
1318 ->getTargetInsertSubreg(Mips::sub_32, DL, MVT::i64,
1319 Undef64Val, SDValue(Res, 0))
1320 .getNode();
1321
1322 // We have 3 cases:
1323 // The HiRes is nonzero but Res is $zero => dsll32 HiRes, 0
1324 // The Res is nonzero but HiRes is $zero => dinsu Res, $zero, 32, 32
1325 // Both are non zero => dinsu Res, HiRes, 32, 32
1326 //
1327 // The obvious "missing" case is when both are zero, but that case is
1328 // handled by the ldi case.
1329 if (ResNonZero) {
1330 IntegerType *Int32Ty =
1331 IntegerType::get(MF->getFunction().getContext(), 32);
1332 const ConstantInt *Const32 = ConstantInt::get(Int32Ty, 32);
1333 SDValue Ops[4] = {HiResNonZero ? SDValue(HiRes, 0) : Zero64Val,
1334 CurDAG->getConstant(*Const32, DL, MVT::i32),
1335 CurDAG->getConstant(*Const32, DL, MVT::i32),
1336 SDValue(Res, 0)};
1337
1338 Res = CurDAG->getMachineNode(Mips::DINSU, DL, MVT::i64, Ops);
1339 } else if (HiResNonZero) {
1340 Res = CurDAG->getMachineNode(
1341 Mips::DSLL32, DL, MVT::i64, SDValue(HiRes, 0),
1342 CurDAG->getTargetConstant(0, DL, MVT::i32));
1343 } else
1345 "Zero splat value handled by non-zero 64bit splat synthesis!");
1346
1347 Res = CurDAG->getMachineNode(Mips::FILL_D, DL, MVT::v2i64,
1348 SDValue(Res, 0));
1349 } else
1350 llvm_unreachable("Unknown ABI in MipsISelDAGToDAG!");
1351
1352 } else
1353 return false;
1354
1355 if (ResVecTy != ViaVecTy) {
1356 // If LdiOp is writing to a different register class to ResVecTy, then
1357 // fix it up here. This COPY_TO_REGCLASS should never cause a move.v
1358 // since the source and destination register sets contain the same
1359 // registers.
1360 const TargetLowering *TLI = getTargetLowering();
1361 MVT ResVecTySimple = ResVecTy.getSimpleVT();
1362 const TargetRegisterClass *RC = TLI->getRegClassFor(ResVecTySimple);
1363 Res = CurDAG->getMachineNode(Mips::COPY_TO_REGCLASS, DL,
1364 ResVecTy, SDValue(Res, 0),
1365 CurDAG->getTargetConstant(RC->getID(), DL,
1366 MVT::i32));
1367 }
1368
1369 ReplaceNode(Node, Res);
1370 return true;
1371 }
1372
1373 }
1374
1375 return false;
1376}
1377
1378bool MipsSEDAGToDAGISel::SelectInlineAsmMemoryOperand(
1379 const SDValue &Op, InlineAsm::ConstraintCode ConstraintID,
1380 std::vector<SDValue> &OutOps) {
1382
1383 switch(ConstraintID) {
1384 default:
1385 llvm_unreachable("Unexpected asm memory constraint");
1386 // All memory constraints can at least accept raw pointers.
1389 if (selectAddrRegImm16(Op, Base, Offset) && isa<ConstantSDNode>(Offset)) {
1390 OutOps.push_back(Base);
1391 OutOps.push_back(Offset);
1392 return false;
1393 }
1394 OutOps.push_back(Op);
1395 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
1396 return false;
1398 // The 'R' constraint is supposed to be much more complicated than this.
1399 // However, it's becoming less useful due to architectural changes and
1400 // ought to be replaced by other constraints such as 'ZC'.
1401 // For now, support 9-bit signed offsets which is supportable by all
1402 // subtargets for all instructions.
1403 if (selectAddrRegImm9(Op, Base, Offset) && isa<ConstantSDNode>(Offset)) {
1404 OutOps.push_back(Base);
1405 OutOps.push_back(Offset);
1406 return false;
1407 }
1408 OutOps.push_back(Op);
1409 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
1410 return false;
1412 // ZC matches whatever the pref, ll, and sc instructions can handle for the
1413 // given subtarget.
1414 if (Subtarget->inMicroMipsMode()) {
1415 // On microMIPS, they can handle 12-bit offsets.
1416 if (selectAddrRegImm12(Op, Base, Offset) && isa<ConstantSDNode>(Offset)) {
1417 OutOps.push_back(Base);
1418 OutOps.push_back(Offset);
1419 return false;
1420 }
1421 } else if (Subtarget->hasMips32r6()) {
1422 // On MIPS32r6/MIPS64r6, they can only handle 9-bit offsets.
1423 if (selectAddrRegImm9(Op, Base, Offset) && isa<ConstantSDNode>(Offset)) {
1424 OutOps.push_back(Base);
1425 OutOps.push_back(Offset);
1426 return false;
1427 }
1428 } else if (selectAddrRegImm16(Op, Base, Offset) &&
1430 // Prior to MIPS32r6/MIPS64r6, they can handle 16-bit offsets.
1431 OutOps.push_back(Base);
1432 OutOps.push_back(Offset);
1433 return false;
1434 }
1435 // In all cases, 0-bit offsets are acceptable.
1436 OutOps.push_back(Op);
1437 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
1438 return false;
1439 }
1440 return true;
1441}
1442
1446
1451
return SDValue()
const AArch64Subtarget & Subtarget
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
uint64_t Imm
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static uint64_t getConstant(const Value *IndexValue)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
IRTranslator LLVM IR MI
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
unsigned Opc
Register Reg
MachineRegisterInfo & MRI
SI Fold Operands
static bool initGlobalBaseReg(MachineFunction &MF)
Value * RHS
Value * LHS
xray Insert XRay ops
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt getLoBits(unsigned numBits) const
Compute an APInt containing numBits lowbits from this APInt.
Definition APInt.cpp:641
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1560
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1690
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1508
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:325
int32_t exactLogBase2() const
Definition APInt.h:1803
bool isSignedIntN(unsigned N) const
Check if this APInt has an N-bits signed integer value.
Definition APInt.h:431
bool isShiftedMask() const
Return true if this APInt value contains a non-empty sequence of ones with the remainder zero.
Definition APInt.h:506
bool isMask(unsigned numBits) const
Definition APInt.h:484
bool isIntN(unsigned N) const
Check if this APInt has an N-bits unsigned integer value.
Definition APInt.h:428
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:853
Represent the analysis usage information of a pass.
LLVM_ABI bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef, unsigned &SplatBitSize, bool &HasAnyUndefs, unsigned MinSplatBits=0, bool isBigEndian=false) const
Check if this is a constant splat, and if so, find the smallest element size that splats the vector.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:333
unsigned getID() const
getID() - Return the register class ID number.
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
Wrapper class representing physical registers and target specific virtual registers.
Definition MCRegister.h:42
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
const MachineInstrBuilder & addUse(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
Register getReg() const
getReg - Returns the register number.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
defusechain_iterator< true, false, false, true, false > use_iterator
use_iterator/use_begin/use_end - Walk all uses of the specified register.
use_iterator use_begin(Register RegNo) const
static use_iterator use_end()
const InstSeq & Analyze(uint64_t Imm, unsigned Size, bool LastInstrIsADDiu)
Analyze - Get an instruction sequence to load immediate Imm.
SmallVector< Inst, 7 > InstSeq
MipsDAGToDAGISelLegacy(std::unique_ptr< SelectionDAGISel > S)
bool runOnMachineFunction(MachineFunction &MF) override
const MipsSubtarget * Subtarget
Keep a pointer to the MipsSubtarget around so that we can make the right decision when generating cod...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MipsSEDAGToDAGISelLegacy(MipsTargetMachine &TM, CodeGenOptLevel OL)
MipsSEISelDAGPass(MipsTargetMachine &TM, CodeGenOptLevel OptLevel)
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
const SDValue & getOperand(unsigned i) const
unsigned getOpcode() const
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
SelectionDAGISelPass(std::unique_ptr< SelectionDAGISel > Selector)
const TargetLowering * TLI
const TargetInstrInfo * TII
void ReplaceNode(SDNode *F, SDNode *T)
Replace all uses of F with T, then remove F from the DAG.
const TargetLowering * getTargetLowering() const
LLVM_NO_SANITIZE_CFI_UNRELATED_CAST iterator begin()
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ ADDC
Carry-setting nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:296
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:266
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:222
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
@ TargetExternalSymbol
Definition ISDOpcodes.h:192
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Definition ISDOpcodes.h:187
@ TargetConstant
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification,...
Definition ISDOpcodes.h:181
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:207
@ ADDE
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:306
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:215
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:561
constexpr MCPhysReg RA_64
constexpr MCPhysReg SP
constexpr MCPhysReg RA
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
FunctionPass * createMipsSEISelDAGLegacyPass(MipsTargetMachine &TM, CodeGenOptLevel OptLevel)
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
RegState
Flags to represent properties of register accesses.
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Undef
Value of the register doesn't matter.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
bool isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
Definition Alignment.h:139
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
@ Off
Use window algorithm after SMS algorithm fails.
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:280
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
constexpr bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition MathExtras.h:249
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:206
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:202
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:879
#define N
static constexpr Align fromLog2(unsigned Log2Value)
Returns an alignment of 1 << Log2Value bytes.
Definition Alignment.h:73
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:411
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:354
bool is128BitVector() const
Return true if this is a 128-bit vector type.
Definition ValueTypes.h:245
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:366