1 //===-- MipsSEISelDAGToDAG.cpp - A Dag to Dag Inst Selector for MipsSE ----===//
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
7 //===----------------------------------------------------------------------===//
9 // Subclass of MipsDAGToDAGISel specialized for mips32/64.
11 //===----------------------------------------------------------------------===//
13 #include "MipsSEISelDAGToDAG.h"
14 #include "MCTargetDesc/MipsBaseInfo.h"
16 #include "MipsAnalyzeImmediate.h"
17 #include "MipsMachineFunction.h"
18 #include "MipsRegisterInfo.h"
19 #include "llvm/CodeGen/MachineConstantPool.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/CodeGen/SelectionDAGNodes.h"
25 #include "llvm/IR/CFG.h"
26 #include "llvm/IR/Dominators.h"
27 #include "llvm/IR/GlobalValue.h"
28 #include "llvm/IR/Instructions.h"
29 #include "llvm/IR/Intrinsics.h"
30 #include "llvm/IR/IntrinsicsMips.h"
31 #include "llvm/IR/Type.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Target/TargetMachine.h"
38 #define DEBUG_TYPE "mips-isel"
40 bool MipsSEDAGToDAGISel::runOnMachineFunction(MachineFunction
&MF
) {
41 Subtarget
= &MF
.getSubtarget
<MipsSubtarget
>();
42 if (Subtarget
->inMips16Mode())
44 return MipsDAGToDAGISel::runOnMachineFunction(MF
);
47 void MipsSEDAGToDAGISelLegacy::getAnalysisUsage(AnalysisUsage
&AU
) const {
48 AU
.addRequired
<DominatorTreeWrapperPass
>();
49 SelectionDAGISelLegacy::getAnalysisUsage(AU
);
52 void MipsSEDAGToDAGISel::addDSPCtrlRegOperands(bool IsDef
, MachineInstr
&MI
,
53 MachineFunction
&MF
) {
54 MachineInstrBuilder
MIB(MF
, &MI
);
55 unsigned Mask
= MI
.getOperand(1).getImm();
57 IsDef
? RegState::ImplicitDefine
: RegState::Implicit
| RegState::Undef
;
60 MIB
.addReg(Mips::DSPPos
, Flag
);
63 MIB
.addReg(Mips::DSPSCount
, Flag
);
66 MIB
.addReg(Mips::DSPCarry
, Flag
);
69 MIB
.addReg(Mips::DSPOutFlag
, Flag
);
72 MIB
.addReg(Mips::DSPCCond
, Flag
);
75 MIB
.addReg(Mips::DSPEFI
, Flag
);
78 unsigned MipsSEDAGToDAGISel::getMSACtrlReg(const SDValue RegIdx
) const {
79 uint64_t RegNum
= RegIdx
->getAsZExtVal();
80 return Mips::MSACtrlRegClass
.getRegister(RegNum
);
83 bool MipsSEDAGToDAGISel::replaceUsesWithZeroReg(MachineRegisterInfo
*MRI
,
84 const MachineInstr
& MI
) {
85 unsigned DstReg
= 0, ZeroReg
= 0;
87 // Check if MI is "addiu $dst, $zero, 0" or "daddiu $dst, $zero, 0".
88 if ((MI
.getOpcode() == Mips::ADDiu
) &&
89 (MI
.getOperand(1).getReg() == Mips::ZERO
) &&
90 (MI
.getOperand(2).isImm()) &&
91 (MI
.getOperand(2).getImm() == 0)) {
92 DstReg
= MI
.getOperand(0).getReg();
94 } else if ((MI
.getOpcode() == Mips::DADDiu
) &&
95 (MI
.getOperand(1).getReg() == Mips::ZERO_64
) &&
96 (MI
.getOperand(2).isImm()) &&
97 (MI
.getOperand(2).getImm() == 0)) {
98 DstReg
= MI
.getOperand(0).getReg();
99 ZeroReg
= Mips::ZERO_64
;
105 // Replace uses with ZeroReg.
106 for (MachineRegisterInfo::use_iterator U
= MRI
->use_begin(DstReg
),
107 E
= MRI
->use_end(); U
!= E
;) {
108 MachineOperand
&MO
= *U
;
109 unsigned OpNo
= U
.getOperandNo();
110 MachineInstr
*MI
= MO
.getParent();
113 // Do not replace if it is a phi's operand or is tied to def operand.
114 if (MI
->isPHI() || MI
->isRegTiedToDefOperand(OpNo
) || MI
->isPseudo())
117 // Also, we have to check that the register class of the operand
118 // contains the zero register.
119 if (!MRI
->getRegClass(MO
.getReg())->contains(ZeroReg
))
128 void MipsSEDAGToDAGISel::emitMCountABI(MachineInstr
&MI
, MachineBasicBlock
&MBB
,
129 MachineFunction
&MF
) {
130 MachineInstrBuilder
MIB(MF
, &MI
);
131 if (!Subtarget
->isABI_O32()) { // N32, N64
132 // Save current return address.
133 BuildMI(MBB
, &MI
, MI
.getDebugLoc(), TII
->get(Mips::OR64
))
135 .addUse(Mips::RA_64
, RegState::Undef
)
136 .addUse(Mips::ZERO_64
);
137 // Stops instruction above from being removed later on.
138 MIB
.addUse(Mips::AT_64
, RegState::Implicit
);
140 // Save current return address.
141 BuildMI(MBB
, &MI
, MI
.getDebugLoc(), TII
->get(Mips::OR
))
143 .addUse(Mips::RA
, RegState::Undef
)
145 // _mcount pops 2 words from stack.
146 BuildMI(MBB
, &MI
, MI
.getDebugLoc(), TII
->get(Mips::ADDiu
))
150 // Stops first instruction above from being removed later on.
151 MIB
.addUse(Mips::AT
, RegState::Implicit
);
155 void MipsSEDAGToDAGISel::processFunctionAfterISel(MachineFunction
&MF
) {
156 MF
.getInfo
<MipsFunctionInfo
>()->initGlobalBaseReg(MF
);
158 MachineRegisterInfo
*MRI
= &MF
.getRegInfo();
160 for (auto &MBB
: MF
) {
161 for (auto &MI
: MBB
) {
162 switch (MI
.getOpcode()) {
164 addDSPCtrlRegOperands(false, MI
, MF
);
167 addDSPCtrlRegOperands(true, MI
, MF
);
169 case Mips::BuildPairF64_64
:
170 case Mips::ExtractElementF64_64
:
171 if (!Subtarget
->useOddSPReg()) {
172 MI
.addOperand(MachineOperand::CreateReg(Mips::SP
, false, true));
176 case Mips::BuildPairF64
:
177 case Mips::ExtractElementF64
:
178 if (Subtarget
->isABI_FPXX() && !Subtarget
->hasMTHC1())
179 MI
.addOperand(MachineOperand::CreateReg(Mips::SP
, false, true));
183 if (MI
.getOperand(0).isGlobal() &&
184 MI
.getOperand(0).getGlobal()->getGlobalIdentifier() == "_mcount")
185 emitMCountABI(MI
, MBB
, MF
);
187 case Mips::JALRPseudo
:
188 case Mips::JALR64Pseudo
:
189 case Mips::JALR16_MM
:
190 if (MI
.getOperand(2).isMCSymbol() &&
191 MI
.getOperand(2).getMCSymbol()->getName() == "_mcount")
192 emitMCountABI(MI
, MBB
, MF
);
195 if (MI
.getOperand(3).isMCSymbol() &&
196 MI
.getOperand(3).getMCSymbol()->getName() == "_mcount")
197 emitMCountABI(MI
, MBB
, MF
);
200 replaceUsesWithZeroReg(MRI
, MI
);
206 void MipsSEDAGToDAGISel::selectAddE(SDNode
*Node
, const SDLoc
&DL
) const {
207 SDValue InGlue
= Node
->getOperand(2);
208 unsigned Opc
= InGlue
.getOpcode();
209 SDValue LHS
= Node
->getOperand(0), RHS
= Node
->getOperand(1);
210 EVT VT
= LHS
.getValueType();
212 // In the base case, we can rely on the carry bit from the addsc
214 if (Opc
== ISD::ADDC
) {
215 SDValue Ops
[3] = {LHS
, RHS
, InGlue
};
216 CurDAG
->SelectNodeTo(Node
, Mips::ADDWC
, VT
, MVT::Glue
, Ops
);
220 assert(Opc
== ISD::ADDE
&& "ISD::ADDE not in a chain of ADDE nodes!");
222 // The more complex case is when there is a chain of ISD::ADDE nodes like:
223 // (adde (adde (adde (addc a b) c) d) e).
225 // The addwc instruction does not write to the carry bit, instead it writes
226 // to bit 20 of the dsp control register. To match this series of nodes, each
227 // intermediate adde node must be expanded to write the carry bit before the
230 // Start by reading the overflow field for addsc and moving the value to the
231 // carry field. The usage of 1 here with MipsISD::RDDSP / Mips::WRDSP
232 // corresponds to reading/writing the entire control register to/from a GPR.
234 SDValue CstOne
= CurDAG
->getTargetConstant(1, DL
, MVT::i32
);
236 SDValue OuFlag
= CurDAG
->getTargetConstant(20, DL
, MVT::i32
);
238 SDNode
*DSPCtrlField
= CurDAG
->getMachineNode(Mips::RDDSP
, DL
, MVT::i32
,
239 MVT::Glue
, CstOne
, InGlue
);
241 SDNode
*Carry
= CurDAG
->getMachineNode(
242 Mips::EXT
, DL
, MVT::i32
, SDValue(DSPCtrlField
, 0), OuFlag
, CstOne
);
244 SDValue Ops
[4] = {SDValue(DSPCtrlField
, 0),
245 CurDAG
->getTargetConstant(6, DL
, MVT::i32
), CstOne
,
247 SDNode
*DSPCFWithCarry
= CurDAG
->getMachineNode(Mips::INS
, DL
, MVT::i32
, Ops
);
249 // My reading of the MIPS DSP 3.01 specification isn't as clear as I
250 // would like about whether bit 20 always gets overwritten by addwc.
251 // Hence take an extremely conservative view and presume it's sticky. We
252 // therefore need to clear it.
254 SDValue Zero
= CurDAG
->getRegister(Mips::ZERO
, MVT::i32
);
256 SDValue InsOps
[4] = {Zero
, OuFlag
, CstOne
, SDValue(DSPCFWithCarry
, 0)};
257 SDNode
*DSPCtrlFinal
=
258 CurDAG
->getMachineNode(Mips::INS
, DL
, MVT::i32
, InsOps
);
260 SDNode
*WrDSP
= CurDAG
->getMachineNode(Mips::WRDSP
, DL
, MVT::Glue
,
261 SDValue(DSPCtrlFinal
, 0), CstOne
);
263 SDValue Operands
[3] = {LHS
, RHS
, SDValue(WrDSP
, 0)};
264 CurDAG
->SelectNodeTo(Node
, Mips::ADDWC
, VT
, MVT::Glue
, Operands
);
268 bool MipsSEDAGToDAGISel::selectAddrFrameIndex(SDValue Addr
, SDValue
&Base
,
269 SDValue
&Offset
) const {
270 if (FrameIndexSDNode
*FIN
= dyn_cast
<FrameIndexSDNode
>(Addr
)) {
271 EVT ValTy
= Addr
.getValueType();
273 Base
= CurDAG
->getTargetFrameIndex(FIN
->getIndex(), ValTy
);
274 Offset
= CurDAG
->getTargetConstant(0, SDLoc(Addr
), ValTy
);
280 /// Match frameindex+offset and frameindex|offset
281 bool MipsSEDAGToDAGISel::selectAddrFrameIndexOffset(
282 SDValue Addr
, SDValue
&Base
, SDValue
&Offset
, unsigned OffsetBits
,
283 unsigned ShiftAmount
= 0) const {
284 if (CurDAG
->isBaseWithConstantOffset(Addr
)) {
285 auto *CN
= cast
<ConstantSDNode
>(Addr
.getOperand(1));
286 if (isIntN(OffsetBits
+ ShiftAmount
, CN
->getSExtValue())) {
287 EVT ValTy
= Addr
.getValueType();
289 // If the first operand is a FI, get the TargetFI Node
290 if (FrameIndexSDNode
*FIN
=
291 dyn_cast
<FrameIndexSDNode
>(Addr
.getOperand(0)))
292 Base
= CurDAG
->getTargetFrameIndex(FIN
->getIndex(), ValTy
);
294 Base
= Addr
.getOperand(0);
295 // If base is a FI, additional offset calculation is done in
296 // eliminateFrameIndex, otherwise we need to check the alignment
297 const Align
Alignment(1ULL << ShiftAmount
);
298 if (!isAligned(Alignment
, CN
->getZExtValue()))
302 Offset
= CurDAG
->getTargetConstant(CN
->getZExtValue(), SDLoc(Addr
),
310 /// ComplexPattern used on MipsInstrInfo
311 /// Used on Mips Load/Store instructions
312 bool MipsSEDAGToDAGISel::selectAddrRegImm(SDValue Addr
, SDValue
&Base
,
313 SDValue
&Offset
) const {
314 // if Address is FI, get the TargetFrameIndex.
315 if (selectAddrFrameIndex(Addr
, Base
, Offset
))
318 // on PIC code Load GA
319 if (Addr
.getOpcode() == MipsISD::Wrapper
) {
320 Base
= Addr
.getOperand(0);
321 Offset
= Addr
.getOperand(1);
325 if (!TM
.isPositionIndependent()) {
326 if ((Addr
.getOpcode() == ISD::TargetExternalSymbol
||
327 Addr
.getOpcode() == ISD::TargetGlobalAddress
))
331 // Addresses of the form FI+const or FI|const
332 if (selectAddrFrameIndexOffset(Addr
, Base
, Offset
, 16))
335 // Operand is a result from an ADD.
336 if (Addr
.getOpcode() == ISD::ADD
) {
337 // When loading from constant pools, load the lower address part in
338 // the instruction itself. Example, instead of:
339 // lui $2, %hi($CPI1_0)
340 // addiu $2, $2, %lo($CPI1_0)
343 // lui $2, %hi($CPI1_0)
344 // lwc1 $f0, %lo($CPI1_0)($2)
345 if (Addr
.getOperand(1).getOpcode() == MipsISD::Lo
||
346 Addr
.getOperand(1).getOpcode() == MipsISD::GPRel
) {
347 SDValue Opnd0
= Addr
.getOperand(1).getOperand(0);
348 if (isa
<ConstantPoolSDNode
>(Opnd0
) || isa
<GlobalAddressSDNode
>(Opnd0
) ||
349 isa
<JumpTableSDNode
>(Opnd0
)) {
350 Base
= Addr
.getOperand(0);
360 /// ComplexPattern used on MipsInstrInfo
361 /// Used on Mips Load/Store instructions
362 bool MipsSEDAGToDAGISel::selectAddrDefault(SDValue Addr
, SDValue
&Base
,
363 SDValue
&Offset
) const {
365 Offset
= CurDAG
->getTargetConstant(0, SDLoc(Addr
), Addr
.getValueType());
369 bool MipsSEDAGToDAGISel::selectIntAddr(SDValue Addr
, SDValue
&Base
,
370 SDValue
&Offset
) const {
371 return selectAddrRegImm(Addr
, Base
, Offset
) ||
372 selectAddrDefault(Addr
, Base
, Offset
);
375 bool MipsSEDAGToDAGISel::selectAddrRegImm9(SDValue Addr
, SDValue
&Base
,
376 SDValue
&Offset
) const {
377 if (selectAddrFrameIndex(Addr
, Base
, Offset
))
380 if (selectAddrFrameIndexOffset(Addr
, Base
, Offset
, 9))
386 /// Used on microMIPS LWC2, LDC2, SWC2 and SDC2 instructions (11-bit offset)
387 bool MipsSEDAGToDAGISel::selectAddrRegImm11(SDValue Addr
, SDValue
&Base
,
388 SDValue
&Offset
) const {
389 if (selectAddrFrameIndex(Addr
, Base
, Offset
))
392 if (selectAddrFrameIndexOffset(Addr
, Base
, Offset
, 11))
398 /// Used on microMIPS Load/Store unaligned instructions (12-bit offset)
399 bool MipsSEDAGToDAGISel::selectAddrRegImm12(SDValue Addr
, SDValue
&Base
,
400 SDValue
&Offset
) const {
401 if (selectAddrFrameIndex(Addr
, Base
, Offset
))
404 if (selectAddrFrameIndexOffset(Addr
, Base
, Offset
, 12))
410 bool MipsSEDAGToDAGISel::selectAddrRegImm16(SDValue Addr
, SDValue
&Base
,
411 SDValue
&Offset
) const {
412 if (selectAddrFrameIndex(Addr
, Base
, Offset
))
415 if (selectAddrFrameIndexOffset(Addr
, Base
, Offset
, 16))
421 bool MipsSEDAGToDAGISel::selectIntAddr11MM(SDValue Addr
, SDValue
&Base
,
422 SDValue
&Offset
) const {
423 return selectAddrRegImm11(Addr
, Base
, Offset
) ||
424 selectAddrDefault(Addr
, Base
, Offset
);
427 bool MipsSEDAGToDAGISel::selectIntAddr12MM(SDValue Addr
, SDValue
&Base
,
428 SDValue
&Offset
) const {
429 return selectAddrRegImm12(Addr
, Base
, Offset
) ||
430 selectAddrDefault(Addr
, Base
, Offset
);
433 bool MipsSEDAGToDAGISel::selectIntAddr16MM(SDValue Addr
, SDValue
&Base
,
434 SDValue
&Offset
) const {
435 return selectAddrRegImm16(Addr
, Base
, Offset
) ||
436 selectAddrDefault(Addr
, Base
, Offset
);
439 bool MipsSEDAGToDAGISel::selectIntAddrLSL2MM(SDValue Addr
, SDValue
&Base
,
440 SDValue
&Offset
) const {
441 if (selectAddrFrameIndexOffset(Addr
, Base
, Offset
, 7)) {
442 if (isa
<FrameIndexSDNode
>(Base
))
445 if (ConstantSDNode
*CN
= dyn_cast
<ConstantSDNode
>(Offset
)) {
446 unsigned CnstOff
= CN
->getZExtValue();
447 return (CnstOff
== (CnstOff
& 0x3c));
453 // For all other cases where "lw" would be selected, don't select "lw16"
454 // because it would result in additional instructions to prepare operands.
455 if (selectAddrRegImm(Addr
, Base
, Offset
))
458 return selectAddrDefault(Addr
, Base
, Offset
);
461 bool MipsSEDAGToDAGISel::selectIntAddrSImm10(SDValue Addr
, SDValue
&Base
,
462 SDValue
&Offset
) const {
464 if (selectAddrFrameIndex(Addr
, Base
, Offset
))
467 if (selectAddrFrameIndexOffset(Addr
, Base
, Offset
, 10))
470 return selectAddrDefault(Addr
, Base
, Offset
);
473 bool MipsSEDAGToDAGISel::selectIntAddrSImm10Lsl1(SDValue Addr
, SDValue
&Base
,
474 SDValue
&Offset
) const {
475 if (selectAddrFrameIndex(Addr
, Base
, Offset
))
478 if (selectAddrFrameIndexOffset(Addr
, Base
, Offset
, 10, 1))
481 return selectAddrDefault(Addr
, Base
, Offset
);
484 bool MipsSEDAGToDAGISel::selectIntAddrSImm10Lsl2(SDValue Addr
, SDValue
&Base
,
485 SDValue
&Offset
) const {
486 if (selectAddrFrameIndex(Addr
, Base
, Offset
))
489 if (selectAddrFrameIndexOffset(Addr
, Base
, Offset
, 10, 2))
492 return selectAddrDefault(Addr
, Base
, Offset
);
495 bool MipsSEDAGToDAGISel::selectIntAddrSImm10Lsl3(SDValue Addr
, SDValue
&Base
,
496 SDValue
&Offset
) const {
497 if (selectAddrFrameIndex(Addr
, Base
, Offset
))
500 if (selectAddrFrameIndexOffset(Addr
, Base
, Offset
, 10, 3))
503 return selectAddrDefault(Addr
, Base
, Offset
);
506 // Select constant vector splats.
508 // Returns true and sets Imm if:
510 // * N is a ISD::BUILD_VECTOR representing a constant splat
511 bool MipsSEDAGToDAGISel::selectVSplat(SDNode
*N
, APInt
&Imm
,
512 unsigned MinSizeInBits
) const {
513 if (!Subtarget
->hasMSA())
516 BuildVectorSDNode
*Node
= dyn_cast
<BuildVectorSDNode
>(N
);
521 APInt SplatValue
, SplatUndef
;
522 unsigned SplatBitSize
;
525 if (!Node
->isConstantSplat(SplatValue
, SplatUndef
, SplatBitSize
, HasAnyUndefs
,
526 MinSizeInBits
, !Subtarget
->isLittle()))
534 // Select constant vector splats.
536 // In addition to the requirements of selectVSplat(), this function returns
537 // true and sets Imm if:
538 // * The splat value is the same width as the elements of the vector
539 // * The splat value fits in an integer with the specified signed-ness and
542 // This function looks through ISD::BITCAST nodes.
543 // TODO: This might not be appropriate for big-endian MSA since BITCAST is
544 // sometimes a shuffle in big-endian mode.
546 // It's worth noting that this function is not used as part of the selection
547 // of ldi.[bhwd] since it does not permit using the wrong-typed ldi.[bhwd]
548 // instruction to achieve the desired bit pattern. ldi.[bhwd] is selected in
549 // MipsSEDAGToDAGISel::selectNode.
550 bool MipsSEDAGToDAGISel::
551 selectVSplatCommon(SDValue N
, SDValue
&Imm
, bool Signed
,
552 unsigned ImmBitSize
) const {
554 EVT EltTy
= N
->getValueType(0).getVectorElementType();
556 if (N
->getOpcode() == ISD::BITCAST
)
557 N
= N
->getOperand(0);
559 if (selectVSplat(N
.getNode(), ImmValue
, EltTy
.getSizeInBits()) &&
560 ImmValue
.getBitWidth() == EltTy
.getSizeInBits()) {
562 if (( Signed
&& ImmValue
.isSignedIntN(ImmBitSize
)) ||
563 (!Signed
&& ImmValue
.isIntN(ImmBitSize
))) {
564 Imm
= CurDAG
->getTargetConstant(ImmValue
, SDLoc(N
), EltTy
);
572 // Select constant vector splats.
573 bool MipsSEDAGToDAGISel::
574 selectVSplatUimm1(SDValue N
, SDValue
&Imm
) const {
575 return selectVSplatCommon(N
, Imm
, false, 1);
578 bool MipsSEDAGToDAGISel::
579 selectVSplatUimm2(SDValue N
, SDValue
&Imm
) const {
580 return selectVSplatCommon(N
, Imm
, false, 2);
583 bool MipsSEDAGToDAGISel::
584 selectVSplatUimm3(SDValue N
, SDValue
&Imm
) const {
585 return selectVSplatCommon(N
, Imm
, false, 3);
588 // Select constant vector splats.
589 bool MipsSEDAGToDAGISel::
590 selectVSplatUimm4(SDValue N
, SDValue
&Imm
) const {
591 return selectVSplatCommon(N
, Imm
, false, 4);
594 // Select constant vector splats.
595 bool MipsSEDAGToDAGISel::
596 selectVSplatUimm5(SDValue N
, SDValue
&Imm
) const {
597 return selectVSplatCommon(N
, Imm
, false, 5);
600 // Select constant vector splats.
601 bool MipsSEDAGToDAGISel::
602 selectVSplatUimm6(SDValue N
, SDValue
&Imm
) const {
603 return selectVSplatCommon(N
, Imm
, false, 6);
606 // Select constant vector splats.
607 bool MipsSEDAGToDAGISel::
608 selectVSplatUimm8(SDValue N
, SDValue
&Imm
) const {
609 return selectVSplatCommon(N
, Imm
, false, 8);
612 // Select constant vector splats.
613 bool MipsSEDAGToDAGISel::
614 selectVSplatSimm5(SDValue N
, SDValue
&Imm
) const {
615 return selectVSplatCommon(N
, Imm
, true, 5);
618 // Select constant vector splats whose value is a power of 2.
620 // In addition to the requirements of selectVSplat(), this function returns
621 // true and sets Imm if:
622 // * The splat value is the same width as the elements of the vector
623 // * The splat value is a power of two.
625 // This function looks through ISD::BITCAST nodes.
626 // TODO: This might not be appropriate for big-endian MSA since BITCAST is
627 // sometimes a shuffle in big-endian mode.
628 bool MipsSEDAGToDAGISel::selectVSplatUimmPow2(SDValue N
, SDValue
&Imm
) const {
630 EVT EltTy
= N
->getValueType(0).getVectorElementType();
632 if (N
->getOpcode() == ISD::BITCAST
)
633 N
= N
->getOperand(0);
635 if (selectVSplat(N
.getNode(), ImmValue
, EltTy
.getSizeInBits()) &&
636 ImmValue
.getBitWidth() == EltTy
.getSizeInBits()) {
637 int32_t Log2
= ImmValue
.exactLogBase2();
640 Imm
= CurDAG
->getTargetConstant(Log2
, SDLoc(N
), EltTy
);
648 // Select constant vector splats whose value only has a consecutive sequence
649 // of left-most bits set (e.g. 0b11...1100...00).
651 // In addition to the requirements of selectVSplat(), this function returns
652 // true and sets Imm if:
653 // * The splat value is the same width as the elements of the vector
654 // * The splat value is a consecutive sequence of left-most bits.
656 // This function looks through ISD::BITCAST nodes.
657 // TODO: This might not be appropriate for big-endian MSA since BITCAST is
658 // sometimes a shuffle in big-endian mode.
659 bool MipsSEDAGToDAGISel::selectVSplatMaskL(SDValue N
, SDValue
&Imm
) const {
661 EVT EltTy
= N
->getValueType(0).getVectorElementType();
663 if (N
->getOpcode() == ISD::BITCAST
)
664 N
= N
->getOperand(0);
666 if (selectVSplat(N
.getNode(), ImmValue
, EltTy
.getSizeInBits()) &&
667 ImmValue
.getBitWidth() == EltTy
.getSizeInBits()) {
668 // Extract the run of set bits starting with bit zero from the bitwise
669 // inverse of ImmValue, and test that the inverse of this is the same
670 // as the original value.
671 if (ImmValue
== ~(~ImmValue
& ~(~ImmValue
+ 1))) {
673 Imm
= CurDAG
->getTargetConstant(ImmValue
.popcount() - 1, SDLoc(N
), EltTy
);
681 // Select constant vector splats whose value only has a consecutive sequence
682 // of right-most bits set (e.g. 0b00...0011...11).
684 // In addition to the requirements of selectVSplat(), this function returns
685 // true and sets Imm if:
686 // * The splat value is the same width as the elements of the vector
687 // * The splat value is a consecutive sequence of right-most bits.
689 // This function looks through ISD::BITCAST nodes.
690 // TODO: This might not be appropriate for big-endian MSA since BITCAST is
691 // sometimes a shuffle in big-endian mode.
692 bool MipsSEDAGToDAGISel::selectVSplatMaskR(SDValue N
, SDValue
&Imm
) const {
694 EVT EltTy
= N
->getValueType(0).getVectorElementType();
696 if (N
->getOpcode() == ISD::BITCAST
)
697 N
= N
->getOperand(0);
699 if (selectVSplat(N
.getNode(), ImmValue
, EltTy
.getSizeInBits()) &&
700 ImmValue
.getBitWidth() == EltTy
.getSizeInBits()) {
701 // Extract the run of set bits starting with bit zero, and test that the
702 // result is the same as the original value
703 if (ImmValue
== (ImmValue
& ~(ImmValue
+ 1))) {
704 Imm
= CurDAG
->getTargetConstant(ImmValue
.popcount() - 1, SDLoc(N
), EltTy
);
712 bool MipsSEDAGToDAGISel::selectVSplatUimmInvPow2(SDValue N
,
713 SDValue
&Imm
) const {
715 EVT EltTy
= N
->getValueType(0).getVectorElementType();
717 if (N
->getOpcode() == ISD::BITCAST
)
718 N
= N
->getOperand(0);
720 if (selectVSplat(N
.getNode(), ImmValue
, EltTy
.getSizeInBits()) &&
721 ImmValue
.getBitWidth() == EltTy
.getSizeInBits()) {
722 int32_t Log2
= (~ImmValue
).exactLogBase2();
725 Imm
= CurDAG
->getTargetConstant(Log2
, SDLoc(N
), EltTy
);
733 bool MipsSEDAGToDAGISel::trySelect(SDNode
*Node
) {
734 unsigned Opcode
= Node
->getOpcode();
738 // Instruction Selection not handled by the auto-generated
739 // tablegen selection should be handled here.
744 case MipsISD::DOUBLE_SELECT_I
:
745 case MipsISD::DOUBLE_SELECT_I64
: {
746 MVT VT
= Subtarget
->isGP64bit() ? MVT::i64
: MVT::i32
;
747 SDValue cond
= Node
->getOperand(0);
748 SDValue Hi1
= Node
->getOperand(1);
749 SDValue Lo1
= Node
->getOperand(2);
750 SDValue Hi2
= Node
->getOperand(3);
751 SDValue Lo2
= Node
->getOperand(4);
753 SDValue ops
[] = {cond
, Hi1
, Lo1
, Hi2
, Lo2
};
754 EVT NodeTys
[] = {VT
, VT
};
755 ReplaceNode(Node
, CurDAG
->getMachineNode(Subtarget
->isGP64bit()
756 ? Mips::PseudoD_SELECT_I64
757 : Mips::PseudoD_SELECT_I
,
763 selectAddE(Node
, DL
);
767 case ISD::ConstantFP
: {
768 auto *CN
= cast
<ConstantFPSDNode
>(Node
);
769 if (Node
->getValueType(0) == MVT::f64
&& CN
->isExactlyValue(+0.0)) {
770 if (Subtarget
->isGP64bit()) {
771 SDValue Zero
= CurDAG
->getCopyFromReg(CurDAG
->getEntryNode(), DL
,
772 Mips::ZERO_64
, MVT::i64
);
774 CurDAG
->getMachineNode(Mips::DMTC1
, DL
, MVT::f64
, Zero
));
775 } else if (Subtarget
->isFP64bit()) {
776 SDValue Zero
= CurDAG
->getCopyFromReg(CurDAG
->getEntryNode(), DL
,
777 Mips::ZERO
, MVT::i32
);
778 ReplaceNode(Node
, CurDAG
->getMachineNode(Mips::BuildPairF64_64
, DL
,
779 MVT::f64
, Zero
, Zero
));
781 SDValue Zero
= CurDAG
->getCopyFromReg(CurDAG
->getEntryNode(), DL
,
782 Mips::ZERO
, MVT::i32
);
783 ReplaceNode(Node
, CurDAG
->getMachineNode(Mips::BuildPairF64
, DL
,
784 MVT::f64
, Zero
, Zero
));
791 case ISD::Constant
: {
792 auto *CN
= cast
<ConstantSDNode
>(Node
);
793 int64_t Imm
= CN
->getSExtValue();
794 unsigned Size
= CN
->getValueSizeInBits(0);
799 MipsAnalyzeImmediate AnalyzeImm
;
801 const MipsAnalyzeImmediate::InstSeq
&Seq
=
802 AnalyzeImm
.Analyze(Imm
, Size
, false);
804 MipsAnalyzeImmediate::InstSeq::const_iterator Inst
= Seq
.begin();
807 SDValue ImmOpnd
= CurDAG
->getTargetConstant(SignExtend64
<16>(Inst
->ImmOpnd
),
810 // The first instruction can be a LUi which is different from other
811 // instructions (ADDiu, ORI and SLL) in that it does not have a register
813 if (Inst
->Opc
== Mips::LUi64
)
814 RegOpnd
= CurDAG
->getMachineNode(Inst
->Opc
, DL
, MVT::i64
, ImmOpnd
);
817 CurDAG
->getMachineNode(Inst
->Opc
, DL
, MVT::i64
,
818 CurDAG
->getRegister(Mips::ZERO_64
, MVT::i64
),
821 // The remaining instructions in the sequence are handled here.
822 for (++Inst
; Inst
!= Seq
.end(); ++Inst
) {
823 ImmOpnd
= CurDAG
->getTargetConstant(SignExtend64
<16>(Inst
->ImmOpnd
), DL
,
825 RegOpnd
= CurDAG
->getMachineNode(Inst
->Opc
, DL
, MVT::i64
,
826 SDValue(RegOpnd
, 0), ImmOpnd
);
829 ReplaceNode(Node
, RegOpnd
);
833 case ISD::INTRINSIC_W_CHAIN
: {
834 const unsigned IntrinsicOpcode
= Node
->getConstantOperandVal(1);
835 switch (IntrinsicOpcode
) {
839 case Intrinsic::mips_cfcmsa
: {
840 SDValue ChainIn
= Node
->getOperand(0);
841 SDValue RegIdx
= Node
->getOperand(2);
842 SDValue Reg
= CurDAG
->getCopyFromReg(ChainIn
, DL
,
843 getMSACtrlReg(RegIdx
), MVT::i32
);
844 ReplaceNode(Node
, Reg
.getNode());
847 case Intrinsic::mips_ldr_d
:
848 case Intrinsic::mips_ldr_w
: {
849 unsigned Op
= (IntrinsicOpcode
== Intrinsic::mips_ldr_d
) ? Mips::LDR_D
853 assert(Node
->getNumOperands() == 4 && "Unexpected number of operands.");
854 const SDValue
&Chain
= Node
->getOperand(0);
855 const SDValue
&Intrinsic
= Node
->getOperand(1);
856 const SDValue
&Pointer
= Node
->getOperand(2);
857 const SDValue
&Constant
= Node
->getOperand(3);
859 assert(Chain
.getValueType() == MVT::Other
);
861 assert(Intrinsic
.getOpcode() == ISD::TargetConstant
&&
862 Constant
.getOpcode() == ISD::Constant
&&
863 "Invalid instruction operand.");
865 // Convert Constant to TargetConstant.
866 const ConstantInt
*Val
=
867 cast
<ConstantSDNode
>(Constant
)->getConstantIntValue();
869 CurDAG
->getTargetConstant(*Val
, DL
, Constant
.getValueType());
871 SmallVector
<SDValue
, 3> Ops
{Pointer
, Imm
, Chain
};
873 assert(Node
->getNumValues() == 2);
874 assert(Node
->getValueType(0).is128BitVector());
875 assert(Node
->getValueType(1) == MVT::Other
);
876 SmallVector
<EVT
, 2> ResTys
{Node
->getValueType(0), Node
->getValueType(1)};
878 ReplaceNode(Node
, CurDAG
->getMachineNode(Op
, DL
, ResTys
, Ops
));
886 case ISD::INTRINSIC_WO_CHAIN
: {
887 switch (Node
->getConstantOperandVal(0)) {
891 case Intrinsic::mips_move_v
:
892 // Like an assignment but will always produce a move.v even if
894 ReplaceNode(Node
, CurDAG
->getMachineNode(Mips::MOVE_V
, DL
,
895 Node
->getValueType(0),
896 Node
->getOperand(1)));
902 case ISD::INTRINSIC_VOID
: {
903 const unsigned IntrinsicOpcode
= Node
->getConstantOperandVal(1);
904 switch (IntrinsicOpcode
) {
908 case Intrinsic::mips_ctcmsa
: {
909 SDValue ChainIn
= Node
->getOperand(0);
910 SDValue RegIdx
= Node
->getOperand(2);
911 SDValue Value
= Node
->getOperand(3);
912 SDValue ChainOut
= CurDAG
->getCopyToReg(ChainIn
, DL
,
913 getMSACtrlReg(RegIdx
), Value
);
914 ReplaceNode(Node
, ChainOut
.getNode());
917 case Intrinsic::mips_str_d
:
918 case Intrinsic::mips_str_w
: {
919 unsigned Op
= (IntrinsicOpcode
== Intrinsic::mips_str_d
) ? Mips::STR_D
923 assert(Node
->getNumOperands() == 5 && "Unexpected number of operands.");
924 const SDValue
&Chain
= Node
->getOperand(0);
925 const SDValue
&Intrinsic
= Node
->getOperand(1);
926 const SDValue
&Vec
= Node
->getOperand(2);
927 const SDValue
&Pointer
= Node
->getOperand(3);
928 const SDValue
&Constant
= Node
->getOperand(4);
930 assert(Chain
.getValueType() == MVT::Other
);
932 assert(Intrinsic
.getOpcode() == ISD::TargetConstant
&&
933 Constant
.getOpcode() == ISD::Constant
&&
934 "Invalid instruction operand.");
936 // Convert Constant to TargetConstant.
937 const ConstantInt
*Val
=
938 cast
<ConstantSDNode
>(Constant
)->getConstantIntValue();
940 CurDAG
->getTargetConstant(*Val
, DL
, Constant
.getValueType());
942 SmallVector
<SDValue
, 4> Ops
{Vec
, Pointer
, Imm
, Chain
};
944 assert(Node
->getNumValues() == 1);
945 assert(Node
->getValueType(0) == MVT::Other
);
946 SmallVector
<EVT
, 1> ResTys
{Node
->getValueType(0)};
948 ReplaceNode(Node
, CurDAG
->getMachineNode(Op
, DL
, ResTys
, Ops
));
955 case MipsISD::FAbs
: {
956 MVT ResTy
= Node
->getSimpleValueType(0);
957 assert((ResTy
== MVT::f64
|| ResTy
== MVT::f32
) &&
958 "Unsupported float type!");
960 if (ResTy
== MVT::f64
)
961 Opc
= (Subtarget
->isFP64bit() ? Mips::FABS_D64
: Mips::FABS_D32
);
965 if (Subtarget
->inMicroMipsMode()) {
968 Opc
= Mips::FABS_D64_MM
;
971 Opc
= Mips::FABS_D32_MM
;
974 Opc
= Mips::FABS_S_MM
;
977 llvm_unreachable("Unknown opcode for MIPS floating point abs!");
982 CurDAG
->getMachineNode(Opc
, DL
, ResTy
, Node
->getOperand(0)));
987 // Manually match MipsISD::Ins nodes to get the correct instruction. It has
988 // to be done in this fashion so that we respect the differences between
989 // dins and dinsm, as the difference is that the size operand has the range
990 // 0 < size <= 32 for dins while dinsm has the range 2 <= size <= 64 which
991 // means SelectionDAGISel would have to test all the operands at once to
992 // match the instruction.
995 // Validating the node operands.
996 if (Node
->getValueType(0) != MVT::i32
&& Node
->getValueType(0) != MVT::i64
)
999 if (Node
->getNumOperands() != 4)
1002 if (Node
->getOperand(1)->getOpcode() != ISD::Constant
||
1003 Node
->getOperand(2)->getOpcode() != ISD::Constant
)
1006 MVT ResTy
= Node
->getSimpleValueType(0);
1007 uint64_t Pos
= Node
->getConstantOperandVal(1);
1008 uint64_t Size
= Node
->getConstantOperandVal(2);
1010 // Size has to be >0 for 'ins', 'dins' and 'dinsu'.
1014 if (Pos
+ Size
> 64)
1017 if (ResTy
!= MVT::i32
&& ResTy
!= MVT::i64
)
1020 unsigned Opcode
= 0;
1021 if (ResTy
== MVT::i32
) {
1022 if (Pos
+ Size
<= 32)
1025 if (Pos
+ Size
<= 32)
1026 Opcode
= Mips::DINS
;
1027 else if (Pos
< 32 && 1 < Size
)
1028 Opcode
= Mips::DINSM
;
1030 Opcode
= Mips::DINSU
;
1035 Node
->getOperand(0), CurDAG
->getTargetConstant(Pos
, DL
, MVT::i32
),
1036 CurDAG
->getTargetConstant(Size
, DL
, MVT::i32
), Node
->getOperand(3)};
1038 ReplaceNode(Node
, CurDAG
->getMachineNode(Opcode
, DL
, ResTy
, Ops
));
1045 case MipsISD::ThreadPointer
: {
1046 EVT PtrVT
= getTargetLowering()->getPointerTy(CurDAG
->getDataLayout());
1047 unsigned RdhwrOpc
, DestReg
;
1049 if (PtrVT
== MVT::i32
) {
1050 RdhwrOpc
= Mips::RDHWR
;
1053 RdhwrOpc
= Mips::RDHWR64
;
1054 DestReg
= Mips::V1_64
;
1058 CurDAG
->getMachineNode(RdhwrOpc
, DL
, Node
->getValueType(0), MVT::Glue
,
1059 CurDAG
->getRegister(Mips::HWR29
, MVT::i32
),
1060 CurDAG
->getTargetConstant(0, DL
, MVT::i32
));
1061 SDValue Chain
= CurDAG
->getCopyToReg(CurDAG
->getEntryNode(), DL
, DestReg
,
1062 SDValue(Rdhwr
, 0), SDValue(Rdhwr
, 1));
1063 SDValue ResNode
= CurDAG
->getCopyFromReg(Chain
, DL
, DestReg
, PtrVT
,
1065 ReplaceNode(Node
, ResNode
.getNode());
1069 case ISD::BUILD_VECTOR
: {
1070 // Select appropriate ldi.[bhwd] instructions for constant splats of
1071 // 128-bit when MSA is enabled. Fixup any register class mismatches that
1072 // occur as a result.
1074 // This allows the compiler to use a wider range of immediates than would
1075 // otherwise be allowed. If, for example, v4i32 could only use ldi.h then
1076 // it would not be possible to load { 0x01010101, 0x01010101, 0x01010101,
1077 // 0x01010101 } without using a constant pool. This would be sub-optimal
1078 // when // 'ldi.b wd, 1' is capable of producing that bit-pattern in the
1079 // same set/ of registers. Similarly, ldi.h isn't capable of producing {
1080 // 0x00000000, 0x00000001, 0x00000000, 0x00000001 } but 'ldi.d wd, 1' can.
1082 const MipsABIInfo
&ABI
=
1083 static_cast<const MipsTargetMachine
&>(TM
).getABI();
1085 BuildVectorSDNode
*BVN
= cast
<BuildVectorSDNode
>(Node
);
1086 APInt SplatValue
, SplatUndef
;
1087 unsigned SplatBitSize
;
1090 EVT ResVecTy
= BVN
->getValueType(0);
1093 if (!Subtarget
->hasMSA() || !BVN
->getValueType(0).is128BitVector())
1096 if (!BVN
->isConstantSplat(SplatValue
, SplatUndef
, SplatBitSize
,
1098 !Subtarget
->isLittle()))
1101 switch (SplatBitSize
) {
1105 LdiOp
= Mips::LDI_B
;
1106 ViaVecTy
= MVT::v16i8
;
1109 LdiOp
= Mips::LDI_H
;
1110 ViaVecTy
= MVT::v8i16
;
1113 LdiOp
= Mips::LDI_W
;
1114 ViaVecTy
= MVT::v4i32
;
1117 LdiOp
= Mips::LDI_D
;
1118 ViaVecTy
= MVT::v2i64
;
1122 SDNode
*Res
= nullptr;
1124 // If we have a signed 10 bit integer, we can splat it directly.
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());
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
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
= CurDAG
->getRegister(
1145 Is32BitSplat
? Mips::ZERO
: Mips::ZERO_64
, SplatMVT
);
1147 const unsigned FILLOp
=
1150 : (SplatBitSize
== 32 ? Mips::FILL_W
1151 : (SplatBitSize
== 64 ? Mips::FILL_D
: 0));
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!");
1157 const unsigned Lo
= SplatValue
.getLoBits(16).getZExtValue();
1158 SDValue LoVal
= CurDAG
->getTargetConstant(Lo
, DL
, SplatMVT
);
1160 Res
= CurDAG
->getMachineNode(ADDiuOp
, DL
, SplatMVT
, ZeroVal
, LoVal
);
1161 Res
= CurDAG
->getMachineNode(FILLOp
, DL
, ViaVecTy
, SDValue(Res
, 0));
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::ZERO
, MVT::i32
);
1170 SDValue LoVal
= CurDAG
->getTargetConstant(Lo
, DL
, MVT::i32
);
1171 SDValue HiVal
= CurDAG
->getTargetConstant(Hi
, DL
, MVT::i32
);
1174 Res
= CurDAG
->getMachineNode(Mips::LUi
, DL
, MVT::i32
, HiVal
);
1177 Res
= CurDAG
->getMachineNode(Mips::ORi
, DL
, MVT::i32
,
1178 Hi
? SDValue(Res
, 0) : ZeroVal
, LoVal
);
1180 assert((Hi
|| Lo
) && "Zero case reached 32 bit case splat synthesis!");
1182 CurDAG
->getMachineNode(Mips::FILL_W
, DL
, MVT::v4i32
, SDValue(Res
, 0));
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::ZERO
, MVT::i32
);
1193 SDValue LoVal
= CurDAG
->getTargetConstant(Lo
, DL
, MVT::i32
);
1194 SDValue HiVal
= CurDAG
->getTargetConstant(Hi
, DL
, MVT::i32
);
1197 Res
= CurDAG
->getMachineNode(Mips::LUi
, DL
, MVT::i32
, HiVal
);
1200 Res
= CurDAG
->getMachineNode(Mips::ORi
, DL
, MVT::i32
,
1201 Hi
? SDValue(Res
, 0) : ZeroVal
, LoVal
);
1203 Res
= CurDAG
->getMachineNode(
1204 Mips::SUBREG_TO_REG
, DL
, MVT::i64
,
1205 CurDAG
->getTargetConstant(((Hi
>> 15) & 0x1), DL
, MVT::i64
),
1207 CurDAG
->getTargetConstant(Mips::sub_32
, DL
, MVT::i64
));
1210 CurDAG
->getMachineNode(Mips::FILL_D
, DL
, MVT::v2i64
, SDValue(Res
, 0));
1212 } else if (SplatValue
.isSignedIntN(64)) {
1213 // If we have a 64 bit Splat value, we perform a similar sequence to the
1217 // lui $res, %highest(val) lui $res, %highest(val)
1218 // ori $res, $res, %higher(val) ori $res, $res, %higher(val)
1219 // lui $res2, %hi(val) lui $res2, %hi(val)
1220 // ori $res2, %res2, %lo(val) ori $res2, %res2, %lo(val)
1221 // $res3 = fill $res2 dinsu $res, $res2, 0, 32
1222 // $res4 = insert.w $res3[1], $res fill.d $res
1225 // The ability to use dinsu is guaranteed as MSA requires MIPSR5.
1226 // This saves having to materialize the value by shifts and ors.
1228 // FIXME: Implement the preferred sequence for MIPS64R6:
1231 // ori $res, $zero, %lo(val)
1232 // daui $res, $res, %hi(val)
1233 // dahi $res, $res, %higher(val)
1234 // dati $res, $res, %highest(cal)
1238 const unsigned Lo
= SplatValue
.getLoBits(16).getZExtValue();
1239 const unsigned Hi
= SplatValue
.lshr(16).getLoBits(16).getZExtValue();
1240 const unsigned Higher
= SplatValue
.lshr(32).getLoBits(16).getZExtValue();
1241 const unsigned Highest
= SplatValue
.lshr(48).getLoBits(16).getZExtValue();
1243 SDValue LoVal
= CurDAG
->getTargetConstant(Lo
, DL
, MVT::i32
);
1244 SDValue HiVal
= CurDAG
->getTargetConstant(Hi
, DL
, MVT::i32
);
1245 SDValue HigherVal
= CurDAG
->getTargetConstant(Higher
, DL
, MVT::i32
);
1246 SDValue HighestVal
= CurDAG
->getTargetConstant(Highest
, DL
, MVT::i32
);
1247 SDValue ZeroVal
= CurDAG
->getRegister(Mips::ZERO
, MVT::i32
);
1249 // Independent of whether we're targeting MIPS64 or not, the basic
1250 // operations are the same. Also, directly use the $zero register if
1251 // the 16 bit chunk is zero.
1253 // For optimization purposes we always synthesize the splat value as
1254 // an i32 value, then if we're targetting MIPS64, use SUBREG_TO_REG
1255 // just before combining the values with dinsu to produce an i64. This
1256 // enables SelectionDAG to aggressively share components of splat values
1259 // FIXME: This is the general constant synthesis problem. This code
1260 // should be factored out into a class shared between all the
1261 // classes that need it. Specifically, for a splat size of 64
1262 // bits that's a negative number we can do better than LUi/ORi
1263 // for the upper 32bits.
1266 Res
= CurDAG
->getMachineNode(Mips::LUi
, DL
, MVT::i32
, HiVal
);
1269 Res
= CurDAG
->getMachineNode(Mips::ORi
, DL
, MVT::i32
,
1270 Hi
? SDValue(Res
, 0) : ZeroVal
, LoVal
);
1274 HiRes
= CurDAG
->getMachineNode(Mips::LUi
, DL
, MVT::i32
, HighestVal
);
1277 HiRes
= CurDAG
->getMachineNode(Mips::ORi
, DL
, MVT::i32
,
1278 Highest
? SDValue(HiRes
, 0) : ZeroVal
,
1283 Res
= CurDAG
->getMachineNode(Mips::FILL_W
, DL
, MVT::v4i32
,
1284 (Hi
|| Lo
) ? SDValue(Res
, 0) : ZeroVal
);
1286 Res
= CurDAG
->getMachineNode(
1287 Mips::INSERT_W
, DL
, MVT::v4i32
, SDValue(Res
, 0),
1288 (Highest
|| Higher
) ? SDValue(HiRes
, 0) : ZeroVal
,
1289 CurDAG
->getTargetConstant(1, DL
, MVT::i32
));
1291 const TargetLowering
*TLI
= getTargetLowering();
1292 const TargetRegisterClass
*RC
=
1293 TLI
->getRegClassFor(ViaVecTy
.getSimpleVT());
1295 Res
= CurDAG
->getMachineNode(
1296 Mips::COPY_TO_REGCLASS
, DL
, ViaVecTy
, SDValue(Res
, 0),
1297 CurDAG
->getTargetConstant(RC
->getID(), DL
, MVT::i32
));
1299 Res
= CurDAG
->getMachineNode(
1300 Mips::SPLATI_D
, DL
, MVT::v2i64
, SDValue(Res
, 0),
1301 CurDAG
->getTargetConstant(0, DL
, MVT::i32
));
1302 } else if (ABI
.IsN64() || ABI
.IsN32()) {
1304 SDValue Zero64Val
= CurDAG
->getRegister(Mips::ZERO_64
, MVT::i64
);
1305 const bool HiResNonZero
= Highest
|| Higher
;
1306 const bool ResNonZero
= Hi
|| Lo
;
1309 HiRes
= CurDAG
->getMachineNode(
1310 Mips::SUBREG_TO_REG
, DL
, MVT::i64
,
1311 CurDAG
->getTargetConstant(((Highest
>> 15) & 0x1), DL
, MVT::i64
),
1313 CurDAG
->getTargetConstant(Mips::sub_32
, DL
, MVT::i64
));
1316 Res
= CurDAG
->getMachineNode(
1317 Mips::SUBREG_TO_REG
, DL
, MVT::i64
,
1318 CurDAG
->getTargetConstant(((Hi
>> 15) & 0x1), DL
, MVT::i64
),
1320 CurDAG
->getTargetConstant(Mips::sub_32
, DL
, MVT::i64
));
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
1327 // The obvious "missing" case is when both are zero, but that case is
1328 // handled by the ldi case.
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
),
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
));
1345 "Zero splat value handled by non-zero 64bit splat synthesis!");
1347 Res
= CurDAG
->getMachineNode(Mips::FILL_D
, DL
, MVT::v2i64
,
1350 llvm_unreachable("Unknown ABI in MipsISelDAGToDAG!");
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
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
,
1369 ReplaceNode(Node
, Res
);
1378 bool MipsSEDAGToDAGISel::SelectInlineAsmMemoryOperand(
1379 const SDValue
&Op
, InlineAsm::ConstraintCode ConstraintID
,
1380 std::vector
<SDValue
> &OutOps
) {
1381 SDValue Base
, Offset
;
1383 switch(ConstraintID
) {
1385 llvm_unreachable("Unexpected asm memory constraint");
1386 // All memory constraints can at least accept raw pointers.
1387 case InlineAsm::ConstraintCode::m
:
1388 case InlineAsm::ConstraintCode::o
:
1389 if (selectAddrRegImm16(Op
, Base
, Offset
)) {
1390 OutOps
.push_back(Base
);
1391 OutOps
.push_back(Offset
);
1394 OutOps
.push_back(Op
);
1395 OutOps
.push_back(CurDAG
->getTargetConstant(0, SDLoc(Op
), MVT::i32
));
1397 case InlineAsm::ConstraintCode::R
:
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
)) {
1404 OutOps
.push_back(Base
);
1405 OutOps
.push_back(Offset
);
1408 OutOps
.push_back(Op
);
1409 OutOps
.push_back(CurDAG
->getTargetConstant(0, SDLoc(Op
), MVT::i32
));
1411 case InlineAsm::ConstraintCode::ZC
:
1412 // ZC matches whatever the pref, ll, and sc instructions can handle for the
1414 if (Subtarget
->inMicroMipsMode()) {
1415 // On microMIPS, they can handle 12-bit offsets.
1416 if (selectAddrRegImm12(Op
, Base
, Offset
)) {
1417 OutOps
.push_back(Base
);
1418 OutOps
.push_back(Offset
);
1421 } else if (Subtarget
->hasMips32r6()) {
1422 // On MIPS32r6/MIPS64r6, they can only handle 9-bit offsets.
1423 if (selectAddrRegImm9(Op
, Base
, Offset
)) {
1424 OutOps
.push_back(Base
);
1425 OutOps
.push_back(Offset
);
1428 } else if (selectAddrRegImm16(Op
, Base
, Offset
)) {
1429 // Prior to MIPS32r6/MIPS64r6, they can handle 16-bit offsets.
1430 OutOps
.push_back(Base
);
1431 OutOps
.push_back(Offset
);
1434 // In all cases, 0-bit offsets are acceptable.
1435 OutOps
.push_back(Op
);
1436 OutOps
.push_back(CurDAG
->getTargetConstant(0, SDLoc(Op
), MVT::i32
));
1442 MipsSEDAGToDAGISelLegacy::MipsSEDAGToDAGISelLegacy(MipsTargetMachine
&TM
,
1444 : MipsDAGToDAGISelLegacy(std::make_unique
<MipsSEDAGToDAGISel
>(TM
, OL
)) {}
1446 FunctionPass
*llvm::createMipsSEISelDag(MipsTargetMachine
&TM
,
1447 CodeGenOptLevel OptLevel
) {
1448 return new MipsSEDAGToDAGISelLegacy(TM
, OptLevel
);