1 //===-- MipsSEISelDAGToDAG.cpp - A Dag to Dag Inst Selector for MipsSE ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Subclass of MipsDAGToDAGISel specialized for mips32/64.
12 //===----------------------------------------------------------------------===//
14 #include "MipsSEISelDAGToDAG.h"
15 #include "MCTargetDesc/MipsBaseInfo.h"
17 #include "MipsAnalyzeImmediate.h"
18 #include "MipsMachineFunction.h"
19 #include "MipsRegisterInfo.h"
20 #include "llvm/CodeGen/MachineConstantPool.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/SelectionDAGNodes.h"
26 #include "llvm/IR/CFG.h"
27 #include "llvm/IR/GlobalValue.h"
28 #include "llvm/IR/Instructions.h"
29 #include "llvm/IR/Intrinsics.h"
30 #include "llvm/IR/Type.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Target/TargetMachine.h"
37 #define DEBUG_TYPE "mips-isel"
39 bool MipsSEDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
40 Subtarget = &static_cast<const MipsSubtarget &>(MF.getSubtarget());
41 if (Subtarget->inMips16Mode())
43 return MipsDAGToDAGISel::runOnMachineFunction(MF);
46 void MipsSEDAGToDAGISel::addDSPCtrlRegOperands(bool IsDef, MachineInstr &MI,
47 MachineFunction &MF) {
48 MachineInstrBuilder MIB(MF, &MI);
49 unsigned Mask = MI.getOperand(1).getImm();
50 unsigned Flag = IsDef ? RegState::ImplicitDefine : RegState::Implicit;
53 MIB.addReg(Mips::DSPPos, Flag);
56 MIB.addReg(Mips::DSPSCount, Flag);
59 MIB.addReg(Mips::DSPCarry, Flag);
62 MIB.addReg(Mips::DSPOutFlag, Flag);
65 MIB.addReg(Mips::DSPCCond, Flag);
68 MIB.addReg(Mips::DSPEFI, Flag);
71 unsigned MipsSEDAGToDAGISel::getMSACtrlReg(const SDValue RegIdx) const {
72 switch (cast<ConstantSDNode>(RegIdx)->getZExtValue()) {
74 llvm_unreachable("Could not map int to register");
75 case 0: return Mips::MSAIR;
76 case 1: return Mips::MSACSR;
77 case 2: return Mips::MSAAccess;
78 case 3: return Mips::MSASave;
79 case 4: return Mips::MSAModify;
80 case 5: return Mips::MSARequest;
81 case 6: return Mips::MSAMap;
82 case 7: return Mips::MSAUnmap;
86 bool MipsSEDAGToDAGISel::replaceUsesWithZeroReg(MachineRegisterInfo *MRI,
87 const MachineInstr& MI) {
88 unsigned DstReg = 0, ZeroReg = 0;
90 // Check if MI is "addiu $dst, $zero, 0" or "daddiu $dst, $zero, 0".
91 if ((MI.getOpcode() == Mips::ADDiu) &&
92 (MI.getOperand(1).getReg() == Mips::ZERO) &&
93 (MI.getOperand(2).getImm() == 0)) {
94 DstReg = MI.getOperand(0).getReg();
96 } else if ((MI.getOpcode() == Mips::DADDiu) &&
97 (MI.getOperand(1).getReg() == Mips::ZERO_64) &&
98 (MI.getOperand(2).getImm() == 0)) {
99 DstReg = MI.getOperand(0).getReg();
100 ZeroReg = Mips::ZERO_64;
106 // Replace uses with ZeroReg.
107 for (MachineRegisterInfo::use_iterator U = MRI->use_begin(DstReg),
108 E = MRI->use_end(); U != E;) {
109 MachineOperand &MO = *U;
110 unsigned OpNo = U.getOperandNo();
111 MachineInstr *MI = MO.getParent();
114 // Do not replace if it is a phi's operand or is tied to def operand.
115 if (MI->isPHI() || MI->isRegTiedToDefOperand(OpNo) || MI->isPseudo())
124 void MipsSEDAGToDAGISel::initGlobalBaseReg(MachineFunction &MF) {
125 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
127 if (!MipsFI->globalBaseRegSet())
130 MachineBasicBlock &MBB = MF.front();
131 MachineBasicBlock::iterator I = MBB.begin();
132 MachineRegisterInfo &RegInfo = MF.getRegInfo();
133 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
134 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
135 unsigned V0, V1, GlobalBaseReg = MipsFI->getGlobalBaseReg();
136 const TargetRegisterClass *RC;
137 const MipsABIInfo &ABI = static_cast<const MipsTargetMachine &>(TM).getABI();
138 RC = (ABI.IsN64()) ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
140 V0 = RegInfo.createVirtualRegister(RC);
141 V1 = RegInfo.createVirtualRegister(RC);
144 MF.getRegInfo().addLiveIn(Mips::T9_64);
145 MBB.addLiveIn(Mips::T9_64);
147 // lui $v0, %hi(%neg(%gp_rel(fname)))
148 // daddu $v1, $v0, $t9
149 // daddiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
150 const GlobalValue *FName = MF.getFunction();
151 BuildMI(MBB, I, DL, TII.get(Mips::LUi64), V0)
152 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
153 BuildMI(MBB, I, DL, TII.get(Mips::DADDu), V1).addReg(V0)
154 .addReg(Mips::T9_64);
155 BuildMI(MBB, I, DL, TII.get(Mips::DADDiu), GlobalBaseReg).addReg(V1)
156 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
160 if (MF.getTarget().getRelocationModel() == Reloc::Static) {
161 // Set global register to __gnu_local_gp.
163 // lui $v0, %hi(__gnu_local_gp)
164 // addiu $globalbasereg, $v0, %lo(__gnu_local_gp)
165 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
166 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_HI);
167 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V0)
168 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_LO);
172 MF.getRegInfo().addLiveIn(Mips::T9);
173 MBB.addLiveIn(Mips::T9);
176 // lui $v0, %hi(%neg(%gp_rel(fname)))
177 // addu $v1, $v0, $t9
178 // addiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
179 const GlobalValue *FName = MF.getFunction();
180 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
181 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
182 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), V1).addReg(V0).addReg(Mips::T9);
183 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V1)
184 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
190 // For O32 ABI, the following instruction sequence is emitted to initialize
191 // the global base register:
193 // 0. lui $2, %hi(_gp_disp)
194 // 1. addiu $2, $2, %lo(_gp_disp)
195 // 2. addu $globalbasereg, $2, $t9
197 // We emit only the last instruction here.
199 // GNU linker requires that the first two instructions appear at the beginning
200 // of a function and no instructions be inserted before or between them.
201 // The two instructions are emitted during lowering to MC layer in order to
202 // avoid any reordering.
204 // Register $2 (Mips::V0) is added to the list of live-in registers to ensure
205 // the value instruction 1 (addiu) defines is valid when instruction 2 (addu)
207 MF.getRegInfo().addLiveIn(Mips::V0);
208 MBB.addLiveIn(Mips::V0);
209 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), GlobalBaseReg)
210 .addReg(Mips::V0).addReg(Mips::T9);
213 void MipsSEDAGToDAGISel::processFunctionAfterISel(MachineFunction &MF) {
214 initGlobalBaseReg(MF);
216 MachineRegisterInfo *MRI = &MF.getRegInfo();
218 for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end(); MFI != MFE;
220 for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I) {
221 if (I->getOpcode() == Mips::RDDSP)
222 addDSPCtrlRegOperands(false, *I, MF);
223 else if (I->getOpcode() == Mips::WRDSP)
224 addDSPCtrlRegOperands(true, *I, MF);
226 replaceUsesWithZeroReg(MRI, *I);
230 SDNode *MipsSEDAGToDAGISel::selectAddESubE(unsigned MOp, SDValue InFlag,
231 SDValue CmpLHS, SDLoc DL,
232 SDNode *Node) const {
233 unsigned Opc = InFlag.getOpcode(); (void)Opc;
235 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
236 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
237 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
239 unsigned SLTuOp = Mips::SLTu, ADDuOp = Mips::ADDu;
240 if (Subtarget->isGP64bit()) {
241 SLTuOp = Mips::SLTu64;
242 ADDuOp = Mips::DADDu;
245 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
246 SDValue LHS = Node->getOperand(0), RHS = Node->getOperand(1);
247 EVT VT = LHS.getValueType();
249 SDNode *Carry = CurDAG->getMachineNode(SLTuOp, DL, VT, Ops);
251 if (Subtarget->isGP64bit()) {
252 // On 64-bit targets, sltu produces an i64 but our backend currently says
253 // that SLTu64 produces an i32. We need to fix this in the long run but for
254 // now, just make the DAG type-correct by asserting the upper bits are zero.
255 Carry = CurDAG->getMachineNode(Mips::SUBREG_TO_REG, DL, VT,
256 CurDAG->getTargetConstant(0, DL, VT),
258 CurDAG->getTargetConstant(Mips::sub_32, DL,
262 // Generate a second addition only if we know that RHS is not a
263 // constant-zero node.
264 SDNode *AddCarry = Carry;
265 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
266 if (!C || C->getZExtValue())
267 AddCarry = CurDAG->getMachineNode(ADDuOp, DL, VT, SDValue(Carry, 0), RHS);
269 return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue, LHS,
270 SDValue(AddCarry, 0));
274 bool MipsSEDAGToDAGISel::selectAddrFrameIndex(SDValue Addr, SDValue &Base,
275 SDValue &Offset) const {
276 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
277 EVT ValTy = Addr.getValueType();
279 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
280 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), ValTy);
286 /// Match frameindex+offset and frameindex|offset
287 bool MipsSEDAGToDAGISel::selectAddrFrameIndexOffset(SDValue Addr, SDValue &Base,
289 unsigned OffsetBits) const {
290 if (CurDAG->isBaseWithConstantOffset(Addr)) {
291 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
292 if (isIntN(OffsetBits, CN->getSExtValue())) {
293 EVT ValTy = Addr.getValueType();
295 // If the first operand is a FI, get the TargetFI Node
296 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
297 (Addr.getOperand(0)))
298 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
300 Base = Addr.getOperand(0);
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.getRelocationModel() != Reloc::PIC_) {
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::selectAddrRegReg(SDValue Addr, SDValue &Base,
363 SDValue &Offset) const {
364 // Operand is a result from an ADD.
365 if (Addr.getOpcode() == ISD::ADD) {
366 Base = Addr.getOperand(0);
367 Offset = Addr.getOperand(1);
374 bool MipsSEDAGToDAGISel::selectAddrDefault(SDValue Addr, SDValue &Base,
375 SDValue &Offset) const {
377 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), Addr.getValueType());
381 bool MipsSEDAGToDAGISel::selectIntAddr(SDValue Addr, SDValue &Base,
382 SDValue &Offset) const {
383 return selectAddrRegImm(Addr, Base, Offset) ||
384 selectAddrDefault(Addr, Base, Offset);
387 bool MipsSEDAGToDAGISel::selectAddrRegImm9(SDValue Addr, SDValue &Base,
388 SDValue &Offset) const {
389 if (selectAddrFrameIndex(Addr, Base, Offset))
392 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 9))
398 bool MipsSEDAGToDAGISel::selectAddrRegImm10(SDValue Addr, SDValue &Base,
399 SDValue &Offset) const {
400 if (selectAddrFrameIndex(Addr, Base, Offset))
403 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10))
409 /// Used on microMIPS Load/Store unaligned instructions (12-bit offset)
410 bool MipsSEDAGToDAGISel::selectAddrRegImm12(SDValue Addr, SDValue &Base,
411 SDValue &Offset) const {
412 if (selectAddrFrameIndex(Addr, Base, Offset))
415 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 12))
421 bool MipsSEDAGToDAGISel::selectAddrRegImm16(SDValue Addr, SDValue &Base,
422 SDValue &Offset) const {
423 if (selectAddrFrameIndex(Addr, Base, Offset))
426 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 16))
432 bool MipsSEDAGToDAGISel::selectIntAddrMM(SDValue Addr, SDValue &Base,
433 SDValue &Offset) const {
434 return selectAddrRegImm12(Addr, Base, Offset) ||
435 selectAddrDefault(Addr, Base, Offset);
438 bool MipsSEDAGToDAGISel::selectIntAddrLSL2MM(SDValue Addr, SDValue &Base,
439 SDValue &Offset) const {
440 if (selectAddrFrameIndexOffset(Addr, Base, Offset, 7)) {
441 if (isa<FrameIndexSDNode>(Base))
444 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Offset)) {
445 unsigned CnstOff = CN->getZExtValue();
446 return (CnstOff == (CnstOff & 0x3c));
452 // For all other cases where "lw" would be selected, don't select "lw16"
453 // because it would result in additional instructions to prepare operands.
454 if (selectAddrRegImm(Addr, Base, Offset))
457 return selectAddrDefault(Addr, Base, Offset);
460 bool MipsSEDAGToDAGISel::selectIntAddrMSA(SDValue Addr, SDValue &Base,
461 SDValue &Offset) const {
462 if (selectAddrRegImm10(Addr, Base, Offset))
465 if (selectAddrDefault(Addr, Base, Offset))
471 // Select constant vector splats.
473 // Returns true and sets Imm if:
475 // * N is a ISD::BUILD_VECTOR representing a constant splat
476 bool MipsSEDAGToDAGISel::selectVSplat(SDNode *N, APInt &Imm) const {
477 if (!Subtarget->hasMSA())
480 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N);
485 APInt SplatValue, SplatUndef;
486 unsigned SplatBitSize;
489 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
491 !Subtarget->isLittle()))
499 // Select constant vector splats.
501 // In addition to the requirements of selectVSplat(), this function returns
502 // true and sets Imm if:
503 // * The splat value is the same width as the elements of the vector
504 // * The splat value fits in an integer with the specified signed-ness and
507 // This function looks through ISD::BITCAST nodes.
508 // TODO: This might not be appropriate for big-endian MSA since BITCAST is
509 // sometimes a shuffle in big-endian mode.
511 // It's worth noting that this function is not used as part of the selection
512 // of ldi.[bhwd] since it does not permit using the wrong-typed ldi.[bhwd]
513 // instruction to achieve the desired bit pattern. ldi.[bhwd] is selected in
514 // MipsSEDAGToDAGISel::selectNode.
515 bool MipsSEDAGToDAGISel::
516 selectVSplatCommon(SDValue N, SDValue &Imm, bool Signed,
517 unsigned ImmBitSize) const {
519 EVT EltTy = N->getValueType(0).getVectorElementType();
521 if (N->getOpcode() == ISD::BITCAST)
522 N = N->getOperand(0);
524 if (selectVSplat (N.getNode(), ImmValue) &&
525 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
526 if (( Signed && ImmValue.isSignedIntN(ImmBitSize)) ||
527 (!Signed && ImmValue.isIntN(ImmBitSize))) {
528 Imm = CurDAG->getTargetConstant(ImmValue, SDLoc(N), EltTy);
536 // Select constant vector splats.
537 bool MipsSEDAGToDAGISel::
538 selectVSplatUimm1(SDValue N, SDValue &Imm) const {
539 return selectVSplatCommon(N, Imm, false, 1);
542 bool MipsSEDAGToDAGISel::
543 selectVSplatUimm2(SDValue N, SDValue &Imm) const {
544 return selectVSplatCommon(N, Imm, false, 2);
547 bool MipsSEDAGToDAGISel::
548 selectVSplatUimm3(SDValue N, SDValue &Imm) const {
549 return selectVSplatCommon(N, Imm, false, 3);
552 // Select constant vector splats.
553 bool MipsSEDAGToDAGISel::
554 selectVSplatUimm4(SDValue N, SDValue &Imm) const {
555 return selectVSplatCommon(N, Imm, false, 4);
558 // Select constant vector splats.
559 bool MipsSEDAGToDAGISel::
560 selectVSplatUimm5(SDValue N, SDValue &Imm) const {
561 return selectVSplatCommon(N, Imm, false, 5);
564 // Select constant vector splats.
565 bool MipsSEDAGToDAGISel::
566 selectVSplatUimm6(SDValue N, SDValue &Imm) const {
567 return selectVSplatCommon(N, Imm, false, 6);
570 // Select constant vector splats.
571 bool MipsSEDAGToDAGISel::
572 selectVSplatUimm8(SDValue N, SDValue &Imm) const {
573 return selectVSplatCommon(N, Imm, false, 8);
576 // Select constant vector splats.
577 bool MipsSEDAGToDAGISel::
578 selectVSplatSimm5(SDValue N, SDValue &Imm) const {
579 return selectVSplatCommon(N, Imm, true, 5);
582 // Select constant vector splats whose value is a power of 2.
584 // In addition to the requirements of selectVSplat(), this function returns
585 // true and sets Imm if:
586 // * The splat value is the same width as the elements of the vector
587 // * The splat value is a power of two.
589 // This function looks through ISD::BITCAST nodes.
590 // TODO: This might not be appropriate for big-endian MSA since BITCAST is
591 // sometimes a shuffle in big-endian mode.
592 bool MipsSEDAGToDAGISel::selectVSplatUimmPow2(SDValue N, SDValue &Imm) const {
594 EVT EltTy = N->getValueType(0).getVectorElementType();
596 if (N->getOpcode() == ISD::BITCAST)
597 N = N->getOperand(0);
599 if (selectVSplat (N.getNode(), ImmValue) &&
600 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
601 int32_t Log2 = ImmValue.exactLogBase2();
604 Imm = CurDAG->getTargetConstant(Log2, SDLoc(N), EltTy);
612 // Select constant vector splats whose value only has a consecutive sequence
613 // of left-most bits set (e.g. 0b11...1100...00).
615 // In addition to the requirements of selectVSplat(), this function returns
616 // true and sets Imm if:
617 // * The splat value is the same width as the elements of the vector
618 // * The splat value is a consecutive sequence of left-most bits.
620 // This function looks through ISD::BITCAST nodes.
621 // TODO: This might not be appropriate for big-endian MSA since BITCAST is
622 // sometimes a shuffle in big-endian mode.
623 bool MipsSEDAGToDAGISel::selectVSplatMaskL(SDValue N, SDValue &Imm) const {
625 EVT EltTy = N->getValueType(0).getVectorElementType();
627 if (N->getOpcode() == ISD::BITCAST)
628 N = N->getOperand(0);
630 if (selectVSplat(N.getNode(), ImmValue) &&
631 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
632 // Extract the run of set bits starting with bit zero from the bitwise
633 // inverse of ImmValue, and test that the inverse of this is the same
634 // as the original value.
635 if (ImmValue == ~(~ImmValue & ~(~ImmValue + 1))) {
637 Imm = CurDAG->getTargetConstant(ImmValue.countPopulation(), SDLoc(N),
646 // Select constant vector splats whose value only has a consecutive sequence
647 // of right-most bits set (e.g. 0b00...0011...11).
649 // In addition to the requirements of selectVSplat(), this function returns
650 // true and sets Imm if:
651 // * The splat value is the same width as the elements of the vector
652 // * The splat value is a consecutive sequence of right-most bits.
654 // This function looks through ISD::BITCAST nodes.
655 // TODO: This might not be appropriate for big-endian MSA since BITCAST is
656 // sometimes a shuffle in big-endian mode.
657 bool MipsSEDAGToDAGISel::selectVSplatMaskR(SDValue N, SDValue &Imm) const {
659 EVT EltTy = N->getValueType(0).getVectorElementType();
661 if (N->getOpcode() == ISD::BITCAST)
662 N = N->getOperand(0);
664 if (selectVSplat(N.getNode(), ImmValue) &&
665 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
666 // Extract the run of set bits starting with bit zero, and test that the
667 // result is the same as the original value
668 if (ImmValue == (ImmValue & ~(ImmValue + 1))) {
669 Imm = CurDAG->getTargetConstant(ImmValue.countPopulation(), SDLoc(N),
678 bool MipsSEDAGToDAGISel::selectVSplatUimmInvPow2(SDValue N,
679 SDValue &Imm) const {
681 EVT EltTy = N->getValueType(0).getVectorElementType();
683 if (N->getOpcode() == ISD::BITCAST)
684 N = N->getOperand(0);
686 if (selectVSplat(N.getNode(), ImmValue) &&
687 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
688 int32_t Log2 = (~ImmValue).exactLogBase2();
691 Imm = CurDAG->getTargetConstant(Log2, SDLoc(N), EltTy);
699 std::pair<bool, SDNode*> MipsSEDAGToDAGISel::selectNode(SDNode *Node) {
700 unsigned Opcode = Node->getOpcode();
704 // Instruction Selection not handled by the auto-generated
705 // tablegen selection should be handled here.
713 SDValue InFlag = Node->getOperand(2);
714 unsigned Opc = Subtarget->isGP64bit() ? Mips::DSUBu : Mips::SUBu;
715 Result = selectAddESubE(Opc, InFlag, InFlag.getOperand(0), DL, Node);
716 return std::make_pair(true, Result);
720 if (Subtarget->hasDSP()) // Select DSP instructions, ADDSC and ADDWC.
722 SDValue InFlag = Node->getOperand(2);
723 unsigned Opc = Subtarget->isGP64bit() ? Mips::DADDu : Mips::ADDu;
724 Result = selectAddESubE(Opc, InFlag, InFlag.getValue(0), DL, Node);
725 return std::make_pair(true, Result);
728 case ISD::ConstantFP: {
729 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
730 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
731 if (Subtarget->isGP64bit()) {
732 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
733 Mips::ZERO_64, MVT::i64);
734 Result = CurDAG->getMachineNode(Mips::DMTC1, DL, MVT::f64, Zero);
735 } else if (Subtarget->isFP64bit()) {
736 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
737 Mips::ZERO, MVT::i32);
738 Result = CurDAG->getMachineNode(Mips::BuildPairF64_64, DL, MVT::f64,
741 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
742 Mips::ZERO, MVT::i32);
743 Result = CurDAG->getMachineNode(Mips::BuildPairF64, DL, MVT::f64, Zero,
747 return std::make_pair(true, Result);
752 case ISD::Constant: {
753 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node);
754 unsigned Size = CN->getValueSizeInBits(0);
759 MipsAnalyzeImmediate AnalyzeImm;
760 int64_t Imm = CN->getSExtValue();
762 const MipsAnalyzeImmediate::InstSeq &Seq =
763 AnalyzeImm.Analyze(Imm, Size, false);
765 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
768 SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
771 // The first instruction can be a LUi which is different from other
772 // instructions (ADDiu, ORI and SLL) in that it does not have a register
774 if (Inst->Opc == Mips::LUi64)
775 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
778 CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
779 CurDAG->getRegister(Mips::ZERO_64, MVT::i64),
782 // The remaining instructions in the sequence are handled here.
783 for (++Inst; Inst != Seq.end(); ++Inst) {
784 ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd), DL,
786 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
787 SDValue(RegOpnd, 0), ImmOpnd);
790 return std::make_pair(true, RegOpnd);
793 case ISD::INTRINSIC_W_CHAIN: {
794 switch (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
798 case Intrinsic::mips_cfcmsa: {
799 SDValue ChainIn = Node->getOperand(0);
800 SDValue RegIdx = Node->getOperand(2);
801 SDValue Reg = CurDAG->getCopyFromReg(ChainIn, DL,
802 getMSACtrlReg(RegIdx), MVT::i32);
803 return std::make_pair(true, Reg.getNode());
809 case ISD::INTRINSIC_WO_CHAIN: {
810 switch (cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue()) {
814 case Intrinsic::mips_move_v:
815 // Like an assignment but will always produce a move.v even if
817 return std::make_pair(true,
818 CurDAG->getMachineNode(Mips::MOVE_V, DL,
819 Node->getValueType(0),
820 Node->getOperand(1)));
825 case ISD::INTRINSIC_VOID: {
826 switch (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
830 case Intrinsic::mips_ctcmsa: {
831 SDValue ChainIn = Node->getOperand(0);
832 SDValue RegIdx = Node->getOperand(2);
833 SDValue Value = Node->getOperand(3);
834 SDValue ChainOut = CurDAG->getCopyToReg(ChainIn, DL,
835 getMSACtrlReg(RegIdx), Value);
836 return std::make_pair(true, ChainOut.getNode());
842 case MipsISD::ThreadPointer: {
843 EVT PtrVT = getTargetLowering()->getPointerTy();
844 unsigned RdhwrOpc, DestReg;
846 if (PtrVT == MVT::i32) {
847 RdhwrOpc = Mips::RDHWR;
850 RdhwrOpc = Mips::RDHWR64;
851 DestReg = Mips::V1_64;
855 CurDAG->getMachineNode(RdhwrOpc, DL,
856 Node->getValueType(0),
857 CurDAG->getRegister(Mips::HWR29, MVT::i32));
858 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), DL, DestReg,
860 SDValue ResNode = CurDAG->getCopyFromReg(Chain, DL, DestReg, PtrVT);
861 ReplaceUses(SDValue(Node, 0), ResNode);
862 return std::make_pair(true, ResNode.getNode());
865 case ISD::BUILD_VECTOR: {
866 // Select appropriate ldi.[bhwd] instructions for constant splats of
867 // 128-bit when MSA is enabled. Fixup any register class mismatches that
868 // occur as a result.
870 // This allows the compiler to use a wider range of immediates than would
871 // otherwise be allowed. If, for example, v4i32 could only use ldi.h then
872 // it would not be possible to load { 0x01010101, 0x01010101, 0x01010101,
873 // 0x01010101 } without using a constant pool. This would be sub-optimal
874 // when // 'ldi.b wd, 1' is capable of producing that bit-pattern in the
875 // same set/ of registers. Similarly, ldi.h isn't capable of producing {
876 // 0x00000000, 0x00000001, 0x00000000, 0x00000001 } but 'ldi.d wd, 1' can.
878 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Node);
879 APInt SplatValue, SplatUndef;
880 unsigned SplatBitSize;
883 EVT ResVecTy = BVN->getValueType(0);
886 if (!Subtarget->hasMSA() || !BVN->getValueType(0).is128BitVector())
887 return std::make_pair(false, nullptr);
889 if (!BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
891 !Subtarget->isLittle()))
892 return std::make_pair(false, nullptr);
894 switch (SplatBitSize) {
896 return std::make_pair(false, nullptr);
899 ViaVecTy = MVT::v16i8;
903 ViaVecTy = MVT::v8i16;
907 ViaVecTy = MVT::v4i32;
911 ViaVecTy = MVT::v2i64;
915 if (!SplatValue.isSignedIntN(10))
916 return std::make_pair(false, nullptr);
918 SDValue Imm = CurDAG->getTargetConstant(SplatValue, DL,
919 ViaVecTy.getVectorElementType());
921 SDNode *Res = CurDAG->getMachineNode(LdiOp, DL, ViaVecTy, Imm);
923 if (ResVecTy != ViaVecTy) {
924 // If LdiOp is writing to a different register class to ResVecTy, then
925 // fix it up here. This COPY_TO_REGCLASS should never cause a move.v
926 // since the source and destination register sets contain the same
928 const TargetLowering *TLI = getTargetLowering();
929 MVT ResVecTySimple = ResVecTy.getSimpleVT();
930 const TargetRegisterClass *RC = TLI->getRegClassFor(ResVecTySimple);
931 Res = CurDAG->getMachineNode(Mips::COPY_TO_REGCLASS, DL,
932 ResVecTy, SDValue(Res, 0),
933 CurDAG->getTargetConstant(RC->getID(), DL,
937 return std::make_pair(true, Res);
942 return std::make_pair(false, nullptr);
945 bool MipsSEDAGToDAGISel::
946 SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
947 std::vector<SDValue> &OutOps) {
948 SDValue Base, Offset;
950 switch(ConstraintID) {
952 llvm_unreachable("Unexpected asm memory constraint");
953 // All memory constraints can at least accept raw pointers.
954 case InlineAsm::Constraint_i:
955 OutOps.push_back(Op);
956 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
958 case InlineAsm::Constraint_m:
959 if (selectAddrRegImm16(Op, Base, Offset)) {
960 OutOps.push_back(Base);
961 OutOps.push_back(Offset);
964 OutOps.push_back(Op);
965 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
967 case InlineAsm::Constraint_R:
968 // The 'R' constraint is supposed to be much more complicated than this.
969 // However, it's becoming less useful due to architectural changes and
970 // ought to be replaced by other constraints such as 'ZC'.
971 // For now, support 9-bit signed offsets which is supportable by all
972 // subtargets for all instructions.
973 if (selectAddrRegImm9(Op, Base, Offset)) {
974 OutOps.push_back(Base);
975 OutOps.push_back(Offset);
978 OutOps.push_back(Op);
979 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
981 case InlineAsm::Constraint_ZC:
982 // ZC matches whatever the pref, ll, and sc instructions can handle for the
984 if (Subtarget->inMicroMipsMode()) {
985 // On microMIPS, they can handle 12-bit offsets.
986 if (selectAddrRegImm12(Op, Base, Offset)) {
987 OutOps.push_back(Base);
988 OutOps.push_back(Offset);
991 } else if (Subtarget->hasMips32r6()) {
992 // On MIPS32r6/MIPS64r6, they can only handle 9-bit offsets.
993 if (selectAddrRegImm9(Op, Base, Offset)) {
994 OutOps.push_back(Base);
995 OutOps.push_back(Offset);
998 } else if (selectAddrRegImm16(Op, Base, Offset)) {
999 // Prior to MIPS32r6/MIPS64r6, they can handle 16-bit offsets.
1000 OutOps.push_back(Base);
1001 OutOps.push_back(Offset);
1004 // In all cases, 0-bit offsets are acceptable.
1005 OutOps.push_back(Op);
1006 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
1012 FunctionPass *llvm::createMipsSEISelDag(MipsTargetMachine &TM) {
1013 return new MipsSEDAGToDAGISel(TM);