LLVM 22.0.0git
MipsISelLowering.cpp
Go to the documentation of this file.
1//===- MipsISelLowering.cpp - Mips DAG Lowering Implementation ------------===//
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// This file defines the interfaces that Mips uses to lower LLVM code into a
10// selection DAG.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MipsISelLowering.h"
18#include "MipsCCState.h"
19#include "MipsInstrInfo.h"
20#include "MipsMachineFunction.h"
21#include "MipsRegisterInfo.h"
22#include "MipsSubtarget.h"
23#include "MipsTargetMachine.h"
25#include "llvm/ADT/APFloat.h"
26#include "llvm/ADT/ArrayRef.h"
28#include "llvm/ADT/Statistic.h"
29#include "llvm/ADT/StringRef.h"
50#include "llvm/IR/CallingConv.h"
51#include "llvm/IR/Constants.h"
52#include "llvm/IR/DataLayout.h"
53#include "llvm/IR/DebugLoc.h"
55#include "llvm/IR/Function.h"
56#include "llvm/IR/GlobalValue.h"
57#include "llvm/IR/Module.h"
58#include "llvm/IR/Type.h"
59#include "llvm/IR/Value.h"
60#include "llvm/MC/MCContext.h"
69#include <algorithm>
70#include <cassert>
71#include <cctype>
72#include <cstdint>
73#include <deque>
74#include <iterator>
75#include <regex>
76#include <string>
77#include <utility>
78#include <vector>
79
80using namespace llvm;
81
82#define DEBUG_TYPE "mips-lower"
83
84STATISTIC(NumTailCalls, "Number of tail calls");
85
88
89static const MCPhysReg Mips64DPRegs[8] = {
90 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
91 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
92};
93
94// The MIPS MSA ABI passes vector arguments in the integer register set.
95// The number of integer registers used is dependant on the ABI used.
98 EVT VT) const {
99 if (!VT.isVector())
100 return getRegisterType(Context, VT);
101
103 return Subtarget.isABI_O32() || VT.getSizeInBits() == 32 ? MVT::i32
104 : MVT::i64;
105 return getRegisterType(Context, VT.getVectorElementType());
106}
107
110 EVT VT) const {
111 if (VT.isVector()) {
113 return divideCeil(VT.getSizeInBits(), Subtarget.isABI_O32() ? 32 : 64);
114 return VT.getVectorNumElements() *
116 }
117 return MipsTargetLowering::getNumRegisters(Context, VT);
118}
119
121 LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT,
122 unsigned &NumIntermediates, MVT &RegisterVT) const {
123 if (VT.isPow2VectorType() && VT.getVectorElementType().isRound()) {
124 IntermediateVT = getRegisterTypeForCallingConv(Context, CC, VT);
125 RegisterVT = IntermediateVT.getSimpleVT();
126 NumIntermediates = getNumRegistersForCallingConv(Context, CC, VT);
127 return NumIntermediates;
128 }
129 IntermediateVT = VT.getVectorElementType();
130 NumIntermediates = VT.getVectorNumElements();
131 RegisterVT = getRegisterType(Context, IntermediateVT);
132 return NumIntermediates * getNumRegisters(Context, IntermediateVT);
133}
134
140
141SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
142 SelectionDAG &DAG,
143 unsigned Flag) const {
144 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
145}
146
147SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty,
148 SelectionDAG &DAG,
149 unsigned Flag) const {
150 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
151}
152
153SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty,
154 SelectionDAG &DAG,
155 unsigned Flag) const {
156 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
157}
158
159SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
160 SelectionDAG &DAG,
161 unsigned Flag) const {
162 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
163}
164
165SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
166 SelectionDAG &DAG,
167 unsigned Flag) const {
168 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlign(),
169 N->getOffset(), Flag);
170}
171
173 const MipsSubtarget &STI)
174 : TargetLowering(TM, STI), Subtarget(STI), ABI(TM.getABI()) {
175 // Mips does not have i1 type, so use i32 for
176 // setcc operations results (slt, sgt, ...).
179 // The cmp.cond.fmt instruction in MIPS32r6/MIPS64r6 uses 0 and -1 like MSA
180 // does. Integer booleans still use 0 and 1.
181 if (Subtarget.hasMips32r6())
184
185 // Load extented operations for i1 types must be promoted
186 for (MVT VT : MVT::integer_valuetypes()) {
190 }
191
192 // MIPS doesn't have extending float->double load/store. Set LoadExtAction
193 // for f32, f16
194 for (MVT VT : MVT::fp_valuetypes()) {
195 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
196 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
197 }
198
199 // Set LoadExtAction for f16 vectors to Expand
201 MVT F16VT = MVT::getVectorVT(MVT::f16, VT.getVectorNumElements());
202 if (F16VT.isValid())
204 }
205
206 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
207 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
208
209 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
210
211 // Used by legalize types to correctly generate the setcc result.
212 // Without this, every float setcc comes with a AND/OR with the result,
213 // we don't want this, since the fpcmp result goes to a flag register,
214 // which is used implicitly by brcond and select operations.
215 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
216
217 // Mips Custom Operations
237
242
243 if (Subtarget.hasMips32r2() ||
244 getTargetMachine().getTargetTriple().isOSLinux())
246
247 // Lower fmin/fmax/fclass operations for MIPS R6.
248 if (Subtarget.hasMips32r6()) {
261 } else {
264 }
265
266 if (Subtarget.isGP64bit()) {
273 if (Subtarget.hasMips64r6()) {
276 } else {
279 }
286 }
287
288 if (!Subtarget.isGP64bit()) {
292 }
293
295 if (Subtarget.isGP64bit())
297
306
307 // Operations not directly supported by Mips.
321
322 if (Subtarget.hasCnMips()) {
325 } else {
328 }
335
336 if (!Subtarget.hasMips32r2())
338
339 if (!Subtarget.hasMips64r2())
341
358
359 // Lower f16 conversion operations into library calls
364
366
371
372 // Use the default for now
375
376 if (!Subtarget.isGP64bit()) {
379 }
380
381 if (!Subtarget.hasMips32r2()) {
384 }
385
386 // MIPS16 lacks MIPS32's clz and clo instructions.
387 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode())
389 if (!Subtarget.hasMips64())
391
392 if (!Subtarget.hasMips32r2())
394 if (!Subtarget.hasMips64r2())
396
397 if (Subtarget.isGP64bit() && Subtarget.hasMips64r6()) {
398 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i32, Legal);
399 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i32, Legal);
400 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i32, Legal);
401 setTruncStoreAction(MVT::i64, MVT::i32, Legal);
402 } else if (Subtarget.isGP64bit()) {
403 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i32, Custom);
404 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i32, Custom);
405 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i32, Custom);
406 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
407 }
408
409 setOperationAction(ISD::TRAP, MVT::Other, Legal);
410
414
415 if (Subtarget.isGP64bit())
417 else
419
420 setMinFunctionAlignment(Subtarget.isGP64bit() ? Align(8) : Align(4));
421
422 // The arguments on the stack are defined in terms of 4-byte slots on O32
423 // and 8-byte slots on N32/N64.
424 setMinStackArgumentAlignment((ABI.IsN32() || ABI.IsN64()) ? Align(8)
425 : Align(4));
426
427 setStackPointerRegisterToSaveRestore(ABI.IsN64() ? Mips::SP_64 : Mips::SP);
428
430
431 isMicroMips = Subtarget.inMicroMipsMode();
432}
433
434const MipsTargetLowering *
436 const MipsSubtarget &STI) {
437 if (STI.inMips16Mode())
438 return createMips16TargetLowering(TM, STI);
439
440 return createMipsSETargetLowering(TM, STI);
441}
442
443// Create a fast isel object.
444FastISel *
446 const TargetLibraryInfo *libInfo) const {
447 const MipsTargetMachine &TM =
448 static_cast<const MipsTargetMachine &>(funcInfo.MF->getTarget());
449
450 // We support only the standard encoding [MIPS32,MIPS32R5] ISAs.
451 bool UseFastISel = TM.Options.EnableFastISel && Subtarget.hasMips32() &&
452 !Subtarget.hasMips32r6() && !Subtarget.inMips16Mode() &&
453 !Subtarget.inMicroMipsMode();
454
455 // Disable if either of the following is true:
456 // We do not generate PIC, the ABI is not O32, XGOT is being used.
457 if (!TM.isPositionIndependent() || !TM.getABI().IsO32() ||
458 Subtarget.useXGOT())
459 UseFastISel = false;
460
461 return UseFastISel ? Mips::createFastISel(funcInfo, libInfo) : nullptr;
462}
463
465 EVT VT) const {
466 if (!VT.isVector())
467 return MVT::i32;
469}
470
473 const MipsSubtarget &Subtarget) {
474 if (DCI.isBeforeLegalizeOps())
475 return SDValue();
476
477 EVT Ty = N->getValueType(0);
478 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
479 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
480 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
481 MipsISD::DivRemU16;
482 SDLoc DL(N);
483
484 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
485 N->getOperand(0), N->getOperand(1));
486 SDValue InChain = DAG.getEntryNode();
487 SDValue InGlue = DivRem;
488
489 // insert MFLO
490 if (N->hasAnyUseOfValue(0)) {
491 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
492 InGlue);
493 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
494 InChain = CopyFromLo.getValue(1);
495 InGlue = CopyFromLo.getValue(2);
496 }
497
498 // insert MFHI
499 if (N->hasAnyUseOfValue(1)) {
500 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
501 HI, Ty, InGlue);
502 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
503 }
504
505 return SDValue();
506}
507
509 switch (CC) {
510 default: llvm_unreachable("Unknown fp condition code!");
511 case ISD::SETEQ:
512 case ISD::SETOEQ: return Mips::FCOND_OEQ;
513 case ISD::SETUNE: return Mips::FCOND_UNE;
514 case ISD::SETLT:
515 case ISD::SETOLT: return Mips::FCOND_OLT;
516 case ISD::SETGT:
517 case ISD::SETOGT: return Mips::FCOND_OGT;
518 case ISD::SETLE:
519 case ISD::SETOLE: return Mips::FCOND_OLE;
520 case ISD::SETGE:
521 case ISD::SETOGE: return Mips::FCOND_OGE;
522 case ISD::SETULT: return Mips::FCOND_ULT;
523 case ISD::SETULE: return Mips::FCOND_ULE;
524 case ISD::SETUGT: return Mips::FCOND_UGT;
525 case ISD::SETUGE: return Mips::FCOND_UGE;
526 case ISD::SETUO: return Mips::FCOND_UN;
527 case ISD::SETO: return Mips::FCOND_OR;
528 case ISD::SETNE:
529 case ISD::SETONE: return Mips::FCOND_ONE;
530 case ISD::SETUEQ: return Mips::FCOND_UEQ;
531 }
532}
533
534/// This function returns true if the floating point conditional branches and
535/// conditional moves which use condition code CC should be inverted.
537 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
538 return false;
539
540 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
541 "Illegal Condition Code");
542
543 return true;
544}
545
546// Creates and returns an FPCmp node from a setcc node.
547// Returns Op if setcc is not a floating point comparison.
549 // must be a SETCC node
550 if (Op.getOpcode() != ISD::SETCC && Op.getOpcode() != ISD::STRICT_FSETCC &&
551 Op.getOpcode() != ISD::STRICT_FSETCCS)
552 return Op;
553
554 SDValue LHS = Op.getOperand(0);
555
556 if (!LHS.getValueType().isFloatingPoint())
557 return Op;
558
559 SDValue RHS = Op.getOperand(1);
560 SDLoc DL(Op);
561
562 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
563 // node if necessary.
564 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
565
566 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
567 DAG.getConstant(condCodeToFCC(CC), DL, MVT::i32));
568}
569
570// Creates and returns a CMovFPT/F node.
572 SDValue False, const SDLoc &DL) {
573 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
575 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
576
577 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
578 True.getValueType(), True, FCC0, False, Cond);
579}
580
583 const MipsSubtarget &Subtarget) {
584 if (DCI.isBeforeLegalizeOps())
585 return SDValue();
586
587 SDValue SetCC = N->getOperand(0);
588
589 if ((SetCC.getOpcode() != ISD::SETCC) ||
590 !SetCC.getOperand(0).getValueType().isInteger())
591 return SDValue();
592
593 SDValue False = N->getOperand(2);
594 EVT FalseTy = False.getValueType();
595
596 if (!FalseTy.isInteger())
597 return SDValue();
598
600
601 // If the RHS (False) is 0, we swap the order of the operands
602 // of ISD::SELECT (obviously also inverting the condition) so that we can
603 // take advantage of conditional moves using the $0 register.
604 // Example:
605 // return (a != 0) ? x : 0;
606 // load $reg, x
607 // movz $reg, $0, a
608 if (!FalseC)
609 return SDValue();
610
611 const SDLoc DL(N);
612
613 if (!FalseC->getZExtValue()) {
614 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
615 SDValue True = N->getOperand(1);
616
617 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
618 SetCC.getOperand(1),
620
621 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
622 }
623
624 // If both operands are integer constants there's a possibility that we
625 // can do some interesting optimizations.
626 SDValue True = N->getOperand(1);
628
629 if (!TrueC || !True.getValueType().isInteger())
630 return SDValue();
631
632 // We'll also ignore MVT::i64 operands as this optimizations proves
633 // to be ineffective because of the required sign extensions as the result
634 // of a SETCC operator is always MVT::i32 for non-vector types.
635 if (True.getValueType() == MVT::i64)
636 return SDValue();
637
638 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
639
640 // 1) (a < x) ? y : y-1
641 // slti $reg1, a, x
642 // addiu $reg2, $reg1, y-1
643 if (Diff == 1)
644 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
645
646 // 2) (a < x) ? y-1 : y
647 // slti $reg1, a, x
648 // xor $reg1, $reg1, 1
649 // addiu $reg2, $reg1, y-1
650 if (Diff == -1) {
651 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
652 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
653 SetCC.getOperand(1),
655 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
656 }
657
658 // Could not optimize.
659 return SDValue();
660}
661
664 const MipsSubtarget &Subtarget) {
665 if (DCI.isBeforeLegalizeOps())
666 return SDValue();
667
668 SDValue ValueIfTrue = N->getOperand(0), ValueIfFalse = N->getOperand(2);
669
670 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(ValueIfFalse);
671 if (!FalseC || FalseC->getZExtValue())
672 return SDValue();
673
674 // Since RHS (False) is 0, we swap the order of the True/False operands
675 // (obviously also inverting the condition) so that we can
676 // take advantage of conditional moves using the $0 register.
677 // Example:
678 // return (a != 0) ? x : 0;
679 // load $reg, x
680 // movz $reg, $0, a
681 unsigned Opc = (N->getOpcode() == MipsISD::CMovFP_T) ? MipsISD::CMovFP_F :
682 MipsISD::CMovFP_T;
683
684 SDValue FCC = N->getOperand(1), Glue = N->getOperand(3);
685 return DAG.getNode(Opc, SDLoc(N), ValueIfFalse.getValueType(),
686 ValueIfFalse, FCC, ValueIfTrue, Glue);
687}
688
691 const MipsSubtarget &Subtarget) {
692 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
693 return SDValue();
694
695 SDValue FirstOperand = N->getOperand(0);
696 unsigned FirstOperandOpc = FirstOperand.getOpcode();
697 SDValue Mask = N->getOperand(1);
698 EVT ValTy = N->getValueType(0);
699 SDLoc DL(N);
700
701 uint64_t Pos = 0;
702 unsigned SMPos, SMSize;
703 ConstantSDNode *CN;
704 SDValue NewOperand;
705 unsigned Opc;
706
707 // Op's second operand must be a shifted mask.
708 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
709 !isShiftedMask_64(CN->getZExtValue(), SMPos, SMSize))
710 return SDValue();
711
712 if (FirstOperandOpc == ISD::SRA || FirstOperandOpc == ISD::SRL) {
713 // Pattern match EXT.
714 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
715 // => ext $dst, $src, pos, size
716
717 // The second operand of the shift must be an immediate.
718 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))))
719 return SDValue();
720
721 Pos = CN->getZExtValue();
722
723 // Return if the shifted mask does not start at bit 0 or the sum of its size
724 // and Pos exceeds the word's size.
725 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
726 return SDValue();
727
728 Opc = MipsISD::Ext;
729 NewOperand = FirstOperand.getOperand(0);
730 } else if (FirstOperandOpc == ISD::SHL && Subtarget.hasCnMips()) {
731 // Pattern match CINS.
732 // $dst = and (shl $src , pos), mask
733 // => cins $dst, $src, pos, size
734 // mask is a shifted mask with consecutive 1's, pos = shift amount,
735 // size = population count.
736
737 // The second operand of the shift must be an immediate.
738 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))))
739 return SDValue();
740
741 Pos = CN->getZExtValue();
742
743 if (SMPos != Pos || Pos >= ValTy.getSizeInBits() || SMSize >= 32 ||
744 Pos + SMSize > ValTy.getSizeInBits())
745 return SDValue();
746
747 NewOperand = FirstOperand.getOperand(0);
748 // SMSize is 'location' (position) in this case, not size.
749 SMSize--;
750 Opc = MipsISD::CIns;
751 } else {
752 // Pattern match EXT.
753 // $dst = and $src, (2**size - 1) , if size > 16
754 // => ext $dst, $src, pos, size , pos = 0
755
756 // If the mask is <= 0xffff, andi can be used instead.
757 if (CN->getZExtValue() <= 0xffff)
758 return SDValue();
759
760 // Return if the mask doesn't start at position 0.
761 if (SMPos)
762 return SDValue();
763
764 Opc = MipsISD::Ext;
765 NewOperand = FirstOperand;
766 }
767 return DAG.getNode(Opc, DL, ValTy, NewOperand,
768 DAG.getConstant(Pos, DL, MVT::i32),
769 DAG.getConstant(SMSize, DL, MVT::i32));
770}
771
774 const MipsSubtarget &Subtarget) {
775 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
776 return SDValue();
777
778 SDValue FirstOperand = N->getOperand(0), SecondOperand = N->getOperand(1);
779 unsigned SMPos0, SMSize0, SMPos1, SMSize1;
780 ConstantSDNode *CN, *CN1;
781
782 if ((FirstOperand.getOpcode() == ISD::AND &&
783 SecondOperand.getOpcode() == ISD::SHL) ||
784 (FirstOperand.getOpcode() == ISD::SHL &&
785 SecondOperand.getOpcode() == ISD::AND)) {
786 // Pattern match INS.
787 // $dst = or (and $src1, (2**size0 - 1)), (shl $src2, size0)
788 // ==> ins $src1, $src2, pos, size, pos = size0, size = 32 - pos;
789 // Or:
790 // $dst = or (shl $src2, size0), (and $src1, (2**size0 - 1))
791 // ==> ins $src1, $src2, pos, size, pos = size0, size = 32 - pos;
792 SDValue AndOperand0 = FirstOperand.getOpcode() == ISD::AND
793 ? FirstOperand.getOperand(0)
794 : SecondOperand.getOperand(0);
795 SDValue ShlOperand0 = FirstOperand.getOpcode() == ISD::AND
796 ? SecondOperand.getOperand(0)
797 : FirstOperand.getOperand(0);
798 SDValue AndMask = FirstOperand.getOpcode() == ISD::AND
799 ? FirstOperand.getOperand(1)
800 : SecondOperand.getOperand(1);
801 if (!(CN = dyn_cast<ConstantSDNode>(AndMask)) ||
802 !isShiftedMask_64(CN->getZExtValue(), SMPos0, SMSize0))
803 return SDValue();
804
805 SDValue ShlShift = FirstOperand.getOpcode() == ISD::AND
806 ? SecondOperand.getOperand(1)
807 : FirstOperand.getOperand(1);
808 if (!(CN = dyn_cast<ConstantSDNode>(ShlShift)))
809 return SDValue();
810 uint64_t ShlShiftValue = CN->getZExtValue();
811
812 if (SMPos0 != 0 || SMSize0 != ShlShiftValue)
813 return SDValue();
814
815 SDLoc DL(N);
816 EVT ValTy = N->getValueType(0);
817 SMPos1 = ShlShiftValue;
818 assert(SMPos1 < ValTy.getSizeInBits());
819 SMSize1 = (ValTy == MVT::i64 ? 64 : 32) - SMPos1;
820 return DAG.getNode(MipsISD::Ins, DL, ValTy, ShlOperand0,
821 DAG.getConstant(SMPos1, DL, MVT::i32),
822 DAG.getConstant(SMSize1, DL, MVT::i32), AndOperand0);
823 }
824
825 // See if Op's first operand matches (and $src1 , mask0).
826 if (FirstOperand.getOpcode() != ISD::AND)
827 return SDValue();
828
829 // Pattern match INS.
830 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
831 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
832 // => ins $dst, $src, size, pos, $src1
833 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))) ||
834 !isShiftedMask_64(~CN->getSExtValue(), SMPos0, SMSize0))
835 return SDValue();
836
837 // See if Op's second operand matches (and (shl $src, pos), mask1).
838 if (SecondOperand.getOpcode() == ISD::AND &&
839 SecondOperand.getOperand(0).getOpcode() == ISD::SHL) {
840
841 if (!(CN = dyn_cast<ConstantSDNode>(SecondOperand.getOperand(1))) ||
842 !isShiftedMask_64(CN->getZExtValue(), SMPos1, SMSize1))
843 return SDValue();
844
845 // The shift masks must have the same position and size.
846 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
847 return SDValue();
848
849 SDValue Shl = SecondOperand.getOperand(0);
850
851 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
852 return SDValue();
853
854 unsigned Shamt = CN->getZExtValue();
855
856 // Return if the shift amount and the first bit position of mask are not the
857 // same.
858 EVT ValTy = N->getValueType(0);
859 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
860 return SDValue();
861
862 SDLoc DL(N);
863 return DAG.getNode(MipsISD::Ins, DL, ValTy, Shl.getOperand(0),
864 DAG.getConstant(SMPos0, DL, MVT::i32),
865 DAG.getConstant(SMSize0, DL, MVT::i32),
866 FirstOperand.getOperand(0));
867 } else {
868 // Pattern match DINS.
869 // $dst = or (and $src, mask0), mask1
870 // where mask0 = ((1 << SMSize0) -1) << SMPos0
871 // => dins $dst, $src, pos, size
872 if (~CN->getSExtValue() == ((((int64_t)1 << SMSize0) - 1) << SMPos0) &&
873 ((SMSize0 + SMPos0 <= 64 && Subtarget.hasMips64r2()) ||
874 (SMSize0 + SMPos0 <= 32))) {
875 // Check if AND instruction has constant as argument
876 bool isConstCase = SecondOperand.getOpcode() != ISD::AND;
877 if (SecondOperand.getOpcode() == ISD::AND) {
878 if (!(CN1 = dyn_cast<ConstantSDNode>(SecondOperand->getOperand(1))))
879 return SDValue();
880 } else {
881 if (!(CN1 = dyn_cast<ConstantSDNode>(N->getOperand(1))))
882 return SDValue();
883 }
884 // Don't generate INS if constant OR operand doesn't fit into bits
885 // cleared by constant AND operand.
886 if (CN->getSExtValue() & CN1->getSExtValue())
887 return SDValue();
888
889 SDLoc DL(N);
890 EVT ValTy = N->getOperand(0)->getValueType(0);
891 SDValue Const1;
892 SDValue SrlX;
893 if (!isConstCase) {
894 Const1 = DAG.getConstant(SMPos0, DL, MVT::i32);
895 SrlX = DAG.getNode(ISD::SRL, DL, SecondOperand->getValueType(0),
896 SecondOperand, Const1);
897 }
898 return DAG.getNode(
899 MipsISD::Ins, DL, N->getValueType(0),
900 isConstCase
901 ? DAG.getSignedConstant(CN1->getSExtValue() >> SMPos0, DL, ValTy)
902 : SrlX,
903 DAG.getConstant(SMPos0, DL, MVT::i32),
904 DAG.getConstant(ValTy.getSizeInBits() / 8 < 8 ? SMSize0 & 31
905 : SMSize0,
906 DL, MVT::i32),
907 FirstOperand->getOperand(0));
908 }
909 return SDValue();
910 }
911}
912
914 const MipsSubtarget &Subtarget) {
915 // ROOTNode must have a multiplication as an operand for the match to be
916 // successful.
917 if (ROOTNode->getOperand(0).getOpcode() != ISD::MUL &&
918 ROOTNode->getOperand(1).getOpcode() != ISD::MUL)
919 return SDValue();
920
921 // In the case where we have a multiplication as the left operand of
922 // of a subtraction, we can't combine into a MipsISD::MSub node as the
923 // the instruction definition of msub(u) places the multiplication on
924 // on the right.
925 if (ROOTNode->getOpcode() == ISD::SUB &&
926 ROOTNode->getOperand(0).getOpcode() == ISD::MUL)
927 return SDValue();
928
929 // We don't handle vector types here.
930 if (ROOTNode->getValueType(0).isVector())
931 return SDValue();
932
933 // For MIPS64, madd / msub instructions are inefficent to use with 64 bit
934 // arithmetic. E.g.
935 // (add (mul a b) c) =>
936 // let res = (madd (mthi (drotr c 32))x(mtlo c) a b) in
937 // MIPS64: (or (dsll (mfhi res) 32) (dsrl (dsll (mflo res) 32) 32)
938 // or
939 // MIPS64R2: (dins (mflo res) (mfhi res) 32 32)
940 //
941 // The overhead of setting up the Hi/Lo registers and reassembling the
942 // result makes this a dubious optimzation for MIPS64. The core of the
943 // problem is that Hi/Lo contain the upper and lower 32 bits of the
944 // operand and result.
945 //
946 // It requires a chain of 4 add/mul for MIPS64R2 to get better code
947 // density than doing it naively, 5 for MIPS64. Additionally, using
948 // madd/msub on MIPS64 requires the operands actually be 32 bit sign
949 // extended operands, not true 64 bit values.
950 //
951 // FIXME: For the moment, disable this completely for MIPS64.
952 if (Subtarget.hasMips64())
953 return SDValue();
954
955 SDValue Mult = ROOTNode->getOperand(0).getOpcode() == ISD::MUL
956 ? ROOTNode->getOperand(0)
957 : ROOTNode->getOperand(1);
958
959 SDValue AddOperand = ROOTNode->getOperand(0).getOpcode() == ISD::MUL
960 ? ROOTNode->getOperand(1)
961 : ROOTNode->getOperand(0);
962
963 // Transform this to a MADD only if the user of this node is the add.
964 // If there are other users of the mul, this function returns here.
965 if (!Mult.hasOneUse())
966 return SDValue();
967
968 // maddu and madd are unusual instructions in that on MIPS64 bits 63..31
969 // must be in canonical form, i.e. sign extended. For MIPS32, the operands
970 // of the multiply must have 32 or more sign bits, otherwise we cannot
971 // perform this optimization. We have to check this here as we're performing
972 // this optimization pre-legalization.
973 SDValue MultLHS = Mult->getOperand(0);
974 SDValue MultRHS = Mult->getOperand(1);
975
976 bool IsSigned = MultLHS->getOpcode() == ISD::SIGN_EXTEND &&
977 MultRHS->getOpcode() == ISD::SIGN_EXTEND;
978 bool IsUnsigned = MultLHS->getOpcode() == ISD::ZERO_EXTEND &&
979 MultRHS->getOpcode() == ISD::ZERO_EXTEND;
980
981 if (!IsSigned && !IsUnsigned)
982 return SDValue();
983
984 // Initialize accumulator.
985 SDLoc DL(ROOTNode);
986 SDValue BottomHalf, TopHalf;
987 std::tie(BottomHalf, TopHalf) =
988 CurDAG.SplitScalar(AddOperand, DL, MVT::i32, MVT::i32);
989 SDValue ACCIn =
990 CurDAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, BottomHalf, TopHalf);
991
992 // Create MipsMAdd(u) / MipsMSub(u) node.
993 bool IsAdd = ROOTNode->getOpcode() == ISD::ADD;
994 unsigned Opcode = IsAdd ? (IsUnsigned ? MipsISD::MAddu : MipsISD::MAdd)
995 : (IsUnsigned ? MipsISD::MSubu : MipsISD::MSub);
996 SDValue MAddOps[3] = {
997 CurDAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mult->getOperand(0)),
998 CurDAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mult->getOperand(1)), ACCIn};
999 SDValue MAdd = CurDAG.getNode(Opcode, DL, MVT::Untyped, MAddOps);
1000
1001 SDValue ResLo = CurDAG.getNode(MipsISD::MFLO, DL, MVT::i32, MAdd);
1002 SDValue ResHi = CurDAG.getNode(MipsISD::MFHI, DL, MVT::i32, MAdd);
1003 SDValue Combined =
1004 CurDAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, ResLo, ResHi);
1005 return Combined;
1006}
1007
1010 const MipsSubtarget &Subtarget) {
1011 // (sub v0 (mul v1, v2)) => (msub v1, v2, v0)
1012 if (DCI.isBeforeLegalizeOps()) {
1013 if (Subtarget.hasMips32() && !Subtarget.hasMips32r6() &&
1014 !Subtarget.inMips16Mode() && N->getValueType(0) == MVT::i64)
1015 return performMADD_MSUBCombine(N, DAG, Subtarget);
1016
1017 return SDValue();
1018 }
1019
1020 return SDValue();
1021}
1022
1025 const MipsSubtarget &Subtarget) {
1026 // (add v0 (mul v1, v2)) => (madd v1, v2, v0)
1027 if (DCI.isBeforeLegalizeOps()) {
1028 if (Subtarget.hasMips32() && !Subtarget.hasMips32r6() &&
1029 !Subtarget.inMips16Mode() && N->getValueType(0) == MVT::i64)
1030 return performMADD_MSUBCombine(N, DAG, Subtarget);
1031
1032 return SDValue();
1033 }
1034
1035 // When loading from a jump table, push the Lo node to the position that
1036 // allows folding it into a load immediate.
1037 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
1038 // (add (add abs_lo(tjt), v1), v0) => (add (add v0, v1), abs_lo(tjt))
1039 SDValue InnerAdd = N->getOperand(1);
1040 SDValue Index = N->getOperand(0);
1041 if (InnerAdd.getOpcode() != ISD::ADD)
1042 std::swap(InnerAdd, Index);
1043 if (InnerAdd.getOpcode() != ISD::ADD)
1044 return SDValue();
1045
1046 SDValue Lo = InnerAdd.getOperand(0);
1047 SDValue Other = InnerAdd.getOperand(1);
1048 if (Lo.getOpcode() != MipsISD::Lo)
1049 std::swap(Lo, Other);
1050
1051 if ((Lo.getOpcode() != MipsISD::Lo) ||
1052 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
1053 return SDValue();
1054
1055 EVT ValTy = N->getValueType(0);
1056 SDLoc DL(N);
1057
1058 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, Index, Other);
1059 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
1060}
1061
1064 const MipsSubtarget &Subtarget) {
1065 // Pattern match CINS.
1066 // $dst = shl (and $src , imm), pos
1067 // => cins $dst, $src, pos, size
1068
1069 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasCnMips())
1070 return SDValue();
1071
1072 SDValue FirstOperand = N->getOperand(0);
1073 unsigned FirstOperandOpc = FirstOperand.getOpcode();
1074 SDValue SecondOperand = N->getOperand(1);
1075 EVT ValTy = N->getValueType(0);
1076 SDLoc DL(N);
1077
1078 uint64_t Pos = 0;
1079 unsigned SMPos, SMSize;
1080 ConstantSDNode *CN;
1081 SDValue NewOperand;
1082
1083 // The second operand of the shift must be an immediate.
1084 if (!(CN = dyn_cast<ConstantSDNode>(SecondOperand)))
1085 return SDValue();
1086
1087 Pos = CN->getZExtValue();
1088
1089 if (Pos >= ValTy.getSizeInBits())
1090 return SDValue();
1091
1092 if (FirstOperandOpc != ISD::AND)
1093 return SDValue();
1094
1095 // AND's second operand must be a shifted mask.
1096 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))) ||
1097 !isShiftedMask_64(CN->getZExtValue(), SMPos, SMSize))
1098 return SDValue();
1099
1100 // Return if the shifted mask does not start at bit 0 or the sum of its size
1101 // and Pos exceeds the word's size.
1102 if (SMPos != 0 || SMSize > 32 || Pos + SMSize > ValTy.getSizeInBits())
1103 return SDValue();
1104
1105 NewOperand = FirstOperand.getOperand(0);
1106 // SMSize is 'location' (position) in this case, not size.
1107 SMSize--;
1108
1109 return DAG.getNode(MipsISD::CIns, DL, ValTy, NewOperand,
1110 DAG.getConstant(Pos, DL, MVT::i32),
1111 DAG.getConstant(SMSize, DL, MVT::i32));
1112}
1113
1116 const MipsSubtarget &Subtarget) {
1117 if (DCI.Level != AfterLegalizeDAG || !Subtarget.isGP64bit()) {
1118 return SDValue();
1119 }
1120
1121 SDValue N0 = N->getOperand(0);
1122 EVT VT = N->getValueType(0);
1123
1124 // Pattern match XOR.
1125 // $dst = sign_extend (xor (trunc $src, i32), imm)
1126 // => $dst = xor (signext_inreg $src, i32), imm
1127 if (N0.getOpcode() == ISD::XOR &&
1128 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1129 N0.getOperand(1).getOpcode() == ISD::Constant) {
1130 SDValue TruncateSource = N0.getOperand(0).getOperand(0);
1131 auto *ConstantOperand = dyn_cast<ConstantSDNode>(N0->getOperand(1));
1132
1133 SDValue FirstOperand =
1134 DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N0), VT, TruncateSource,
1135 DAG.getValueType(N0.getOperand(0).getValueType()));
1136
1137 int64_t ConstImm = ConstantOperand->getSExtValue();
1138 return DAG.getNode(ISD::XOR, SDLoc(N0), VT, FirstOperand,
1139 DAG.getConstant(ConstImm, SDLoc(N0), VT));
1140 }
1141
1142 return SDValue();
1143}
1144
1146 const {
1147 SelectionDAG &DAG = DCI.DAG;
1148 unsigned Opc = N->getOpcode();
1149
1150 switch (Opc) {
1151 default: break;
1152 case ISD::SDIVREM:
1153 case ISD::UDIVREM:
1154 return performDivRemCombine(N, DAG, DCI, Subtarget);
1155 case ISD::SELECT:
1156 return performSELECTCombine(N, DAG, DCI, Subtarget);
1157 case MipsISD::CMovFP_F:
1158 case MipsISD::CMovFP_T:
1159 return performCMovFPCombine(N, DAG, DCI, Subtarget);
1160 case ISD::AND:
1161 return performANDCombine(N, DAG, DCI, Subtarget);
1162 case ISD::OR:
1163 return performORCombine(N, DAG, DCI, Subtarget);
1164 case ISD::ADD:
1165 return performADDCombine(N, DAG, DCI, Subtarget);
1166 case ISD::SHL:
1167 return performSHLCombine(N, DAG, DCI, Subtarget);
1168 case ISD::SUB:
1169 return performSUBCombine(N, DAG, DCI, Subtarget);
1170 case ISD::SIGN_EXTEND:
1171 return performSignExtendCombine(N, DAG, DCI, Subtarget);
1172 }
1173
1174 return SDValue();
1175}
1176
1178 return Subtarget.hasMips32();
1179}
1180
1182 return Subtarget.hasMips32();
1183}
1184
1186 // We can use ANDI+SLTIU as a bit test. Y contains the bit position.
1187 // For MIPSR2 or later, we may be able to use the `ext` instruction or its'
1188 // double-word variants.
1189 if (auto *C = dyn_cast<ConstantSDNode>(Y))
1190 return C->getAPIntValue().ule(15);
1191
1192 return false;
1193}
1194
1196 const SDNode *N) const {
1197 assert(((N->getOpcode() == ISD::SHL &&
1198 N->getOperand(0).getOpcode() == ISD::SRL) ||
1199 (N->getOpcode() == ISD::SRL &&
1200 N->getOperand(0).getOpcode() == ISD::SHL)) &&
1201 "Expected shift-shift mask");
1202
1203 if (N->getOperand(0).getValueType().isVector())
1204 return false;
1205 return true;
1206}
1207
1208void
1214
1217{
1218 switch (Op.getOpcode())
1219 {
1220 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
1221 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
1222 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
1223 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
1224 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
1225 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
1226 case ISD::SELECT: return lowerSELECT(Op, DAG);
1227 case ISD::SETCC: return lowerSETCC(Op, DAG);
1228 case ISD::STRICT_FSETCC:
1230 return lowerFSETCC(Op, DAG);
1231 case ISD::VASTART: return lowerVASTART(Op, DAG);
1232 case ISD::VAARG: return lowerVAARG(Op, DAG);
1233 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
1234 case ISD::FABS: return lowerFABS(Op, DAG);
1235 case ISD::FCANONICALIZE:
1236 return lowerFCANONICALIZE(Op, DAG);
1237 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
1238 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
1239 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
1240 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
1241 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
1242 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
1243 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
1244 case ISD::LOAD: return lowerLOAD(Op, DAG);
1245 case ISD::STORE: return lowerSTORE(Op, DAG);
1246 case ISD::EH_DWARF_CFA: return lowerEH_DWARF_CFA(Op, DAG);
1249 return lowerSTRICT_FP_TO_INT(Op, DAG);
1250 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
1252 return lowerREADCYCLECOUNTER(Op, DAG);
1253 }
1254 return SDValue();
1255}
1256
1257//===----------------------------------------------------------------------===//
1258// Lower helper functions
1259//===----------------------------------------------------------------------===//
1260
1261// addLiveIn - This helper function adds the specified physical register to the
1262// MachineFunction as a live in value. It also creates a corresponding
1263// virtual register for it.
1264static unsigned
1265addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
1266{
1268 MF.getRegInfo().addLiveIn(PReg, VReg);
1269 return VReg;
1270}
1271
1274 const TargetInstrInfo &TII,
1275 bool Is64Bit, bool IsMicroMips) {
1276 if (NoZeroDivCheck)
1277 return &MBB;
1278
1279 // Insert instruction "teq $divisor_reg, $zero, 7".
1282 MachineOperand &Divisor = MI.getOperand(2);
1283 MIB = BuildMI(MBB, std::next(I), MI.getDebugLoc(),
1284 TII.get(IsMicroMips ? Mips::TEQ_MM : Mips::TEQ))
1285 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
1286 .addReg(Mips::ZERO)
1287 .addImm(7);
1288
1289 // Use the 32-bit sub-register if this is a 64-bit division.
1290 if (Is64Bit)
1291 MIB->getOperand(0).setSubReg(Mips::sub_32);
1292
1293 // Clear Divisor's kill flag.
1294 Divisor.setIsKill(false);
1295
1296 // We would normally delete the original instruction here but in this case
1297 // we only needed to inject an additional instruction rather than replace it.
1298
1299 return &MBB;
1300}
1301
1304 MachineBasicBlock *BB) const {
1305 switch (MI.getOpcode()) {
1306 default:
1307 llvm_unreachable("Unexpected instr type to insert");
1308 case Mips::ATOMIC_LOAD_ADD_I8:
1309 return emitAtomicBinaryPartword(MI, BB, 1);
1310 case Mips::ATOMIC_LOAD_ADD_I16:
1311 return emitAtomicBinaryPartword(MI, BB, 2);
1312 case Mips::ATOMIC_LOAD_ADD_I32:
1313 return emitAtomicBinary(MI, BB);
1314 case Mips::ATOMIC_LOAD_ADD_I64:
1315 return emitAtomicBinary(MI, BB);
1316
1317 case Mips::ATOMIC_LOAD_AND_I8:
1318 return emitAtomicBinaryPartword(MI, BB, 1);
1319 case Mips::ATOMIC_LOAD_AND_I16:
1320 return emitAtomicBinaryPartword(MI, BB, 2);
1321 case Mips::ATOMIC_LOAD_AND_I32:
1322 return emitAtomicBinary(MI, BB);
1323 case Mips::ATOMIC_LOAD_AND_I64:
1324 return emitAtomicBinary(MI, BB);
1325
1326 case Mips::ATOMIC_LOAD_OR_I8:
1327 return emitAtomicBinaryPartword(MI, BB, 1);
1328 case Mips::ATOMIC_LOAD_OR_I16:
1329 return emitAtomicBinaryPartword(MI, BB, 2);
1330 case Mips::ATOMIC_LOAD_OR_I32:
1331 return emitAtomicBinary(MI, BB);
1332 case Mips::ATOMIC_LOAD_OR_I64:
1333 return emitAtomicBinary(MI, BB);
1334
1335 case Mips::ATOMIC_LOAD_XOR_I8:
1336 return emitAtomicBinaryPartword(MI, BB, 1);
1337 case Mips::ATOMIC_LOAD_XOR_I16:
1338 return emitAtomicBinaryPartword(MI, BB, 2);
1339 case Mips::ATOMIC_LOAD_XOR_I32:
1340 return emitAtomicBinary(MI, BB);
1341 case Mips::ATOMIC_LOAD_XOR_I64:
1342 return emitAtomicBinary(MI, BB);
1343
1344 case Mips::ATOMIC_LOAD_NAND_I8:
1345 return emitAtomicBinaryPartword(MI, BB, 1);
1346 case Mips::ATOMIC_LOAD_NAND_I16:
1347 return emitAtomicBinaryPartword(MI, BB, 2);
1348 case Mips::ATOMIC_LOAD_NAND_I32:
1349 return emitAtomicBinary(MI, BB);
1350 case Mips::ATOMIC_LOAD_NAND_I64:
1351 return emitAtomicBinary(MI, BB);
1352
1353 case Mips::ATOMIC_LOAD_SUB_I8:
1354 return emitAtomicBinaryPartword(MI, BB, 1);
1355 case Mips::ATOMIC_LOAD_SUB_I16:
1356 return emitAtomicBinaryPartword(MI, BB, 2);
1357 case Mips::ATOMIC_LOAD_SUB_I32:
1358 return emitAtomicBinary(MI, BB);
1359 case Mips::ATOMIC_LOAD_SUB_I64:
1360 return emitAtomicBinary(MI, BB);
1361
1362 case Mips::ATOMIC_SWAP_I8:
1363 return emitAtomicBinaryPartword(MI, BB, 1);
1364 case Mips::ATOMIC_SWAP_I16:
1365 return emitAtomicBinaryPartword(MI, BB, 2);
1366 case Mips::ATOMIC_SWAP_I32:
1367 return emitAtomicBinary(MI, BB);
1368 case Mips::ATOMIC_SWAP_I64:
1369 return emitAtomicBinary(MI, BB);
1370
1371 case Mips::ATOMIC_CMP_SWAP_I8:
1372 return emitAtomicCmpSwapPartword(MI, BB, 1);
1373 case Mips::ATOMIC_CMP_SWAP_I16:
1374 return emitAtomicCmpSwapPartword(MI, BB, 2);
1375 case Mips::ATOMIC_CMP_SWAP_I32:
1376 return emitAtomicCmpSwap(MI, BB);
1377 case Mips::ATOMIC_CMP_SWAP_I64:
1378 return emitAtomicCmpSwap(MI, BB);
1379
1380 case Mips::ATOMIC_LOAD_MIN_I8:
1381 return emitAtomicBinaryPartword(MI, BB, 1);
1382 case Mips::ATOMIC_LOAD_MIN_I16:
1383 return emitAtomicBinaryPartword(MI, BB, 2);
1384 case Mips::ATOMIC_LOAD_MIN_I32:
1385 return emitAtomicBinary(MI, BB);
1386 case Mips::ATOMIC_LOAD_MIN_I64:
1387 return emitAtomicBinary(MI, BB);
1388
1389 case Mips::ATOMIC_LOAD_MAX_I8:
1390 return emitAtomicBinaryPartword(MI, BB, 1);
1391 case Mips::ATOMIC_LOAD_MAX_I16:
1392 return emitAtomicBinaryPartword(MI, BB, 2);
1393 case Mips::ATOMIC_LOAD_MAX_I32:
1394 return emitAtomicBinary(MI, BB);
1395 case Mips::ATOMIC_LOAD_MAX_I64:
1396 return emitAtomicBinary(MI, BB);
1397
1398 case Mips::ATOMIC_LOAD_UMIN_I8:
1399 return emitAtomicBinaryPartword(MI, BB, 1);
1400 case Mips::ATOMIC_LOAD_UMIN_I16:
1401 return emitAtomicBinaryPartword(MI, BB, 2);
1402 case Mips::ATOMIC_LOAD_UMIN_I32:
1403 return emitAtomicBinary(MI, BB);
1404 case Mips::ATOMIC_LOAD_UMIN_I64:
1405 return emitAtomicBinary(MI, BB);
1406
1407 case Mips::ATOMIC_LOAD_UMAX_I8:
1408 return emitAtomicBinaryPartword(MI, BB, 1);
1409 case Mips::ATOMIC_LOAD_UMAX_I16:
1410 return emitAtomicBinaryPartword(MI, BB, 2);
1411 case Mips::ATOMIC_LOAD_UMAX_I32:
1412 return emitAtomicBinary(MI, BB);
1413 case Mips::ATOMIC_LOAD_UMAX_I64:
1414 return emitAtomicBinary(MI, BB);
1415
1416 case Mips::PseudoSDIV:
1417 case Mips::PseudoUDIV:
1418 case Mips::DIV:
1419 case Mips::DIVU:
1420 case Mips::MOD:
1421 case Mips::MODU:
1422 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false,
1423 false);
1424 case Mips::SDIV_MM_Pseudo:
1425 case Mips::UDIV_MM_Pseudo:
1426 case Mips::SDIV_MM:
1427 case Mips::UDIV_MM:
1428 case Mips::DIV_MMR6:
1429 case Mips::DIVU_MMR6:
1430 case Mips::MOD_MMR6:
1431 case Mips::MODU_MMR6:
1432 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false, true);
1433 case Mips::PseudoDSDIV:
1434 case Mips::PseudoDUDIV:
1435 case Mips::DDIV:
1436 case Mips::DDIVU:
1437 case Mips::DMOD:
1438 case Mips::DMODU:
1439 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), true, false);
1440
1441 case Mips::PseudoSELECT_I:
1442 case Mips::PseudoSELECT_I64:
1443 case Mips::PseudoSELECT_S:
1444 case Mips::PseudoSELECT_D32:
1445 case Mips::PseudoSELECT_D64:
1446 return emitPseudoSELECT(MI, BB, false, Mips::BNE);
1447 case Mips::PseudoSELECTFP_F_I:
1448 case Mips::PseudoSELECTFP_F_I64:
1449 case Mips::PseudoSELECTFP_F_S:
1450 case Mips::PseudoSELECTFP_F_D32:
1451 case Mips::PseudoSELECTFP_F_D64:
1452 return emitPseudoSELECT(MI, BB, true, Mips::BC1F);
1453 case Mips::PseudoSELECTFP_T_I:
1454 case Mips::PseudoSELECTFP_T_I64:
1455 case Mips::PseudoSELECTFP_T_S:
1456 case Mips::PseudoSELECTFP_T_D32:
1457 case Mips::PseudoSELECTFP_T_D64:
1458 return emitPseudoSELECT(MI, BB, true, Mips::BC1T);
1459 case Mips::PseudoD_SELECT_I:
1460 case Mips::PseudoD_SELECT_I64:
1461 return emitPseudoD_SELECT(MI, BB);
1462 case Mips::LDR_W:
1463 return emitLDR_W(MI, BB);
1464 case Mips::LDR_D:
1465 return emitLDR_D(MI, BB);
1466 case Mips::STR_W:
1467 return emitSTR_W(MI, BB);
1468 case Mips::STR_D:
1469 return emitSTR_D(MI, BB);
1470 }
1471}
1472
1473// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1474// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
1476MipsTargetLowering::emitAtomicBinary(MachineInstr &MI,
1477 MachineBasicBlock *BB) const {
1478
1479 MachineFunction *MF = BB->getParent();
1480 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1482 DebugLoc DL = MI.getDebugLoc();
1483
1484 unsigned AtomicOp;
1485 bool NeedsAdditionalReg = false;
1486 switch (MI.getOpcode()) {
1487 case Mips::ATOMIC_LOAD_ADD_I32:
1488 AtomicOp = Mips::ATOMIC_LOAD_ADD_I32_POSTRA;
1489 break;
1490 case Mips::ATOMIC_LOAD_SUB_I32:
1491 AtomicOp = Mips::ATOMIC_LOAD_SUB_I32_POSTRA;
1492 break;
1493 case Mips::ATOMIC_LOAD_AND_I32:
1494 AtomicOp = Mips::ATOMIC_LOAD_AND_I32_POSTRA;
1495 break;
1496 case Mips::ATOMIC_LOAD_OR_I32:
1497 AtomicOp = Mips::ATOMIC_LOAD_OR_I32_POSTRA;
1498 break;
1499 case Mips::ATOMIC_LOAD_XOR_I32:
1500 AtomicOp = Mips::ATOMIC_LOAD_XOR_I32_POSTRA;
1501 break;
1502 case Mips::ATOMIC_LOAD_NAND_I32:
1503 AtomicOp = Mips::ATOMIC_LOAD_NAND_I32_POSTRA;
1504 break;
1505 case Mips::ATOMIC_SWAP_I32:
1506 AtomicOp = Mips::ATOMIC_SWAP_I32_POSTRA;
1507 break;
1508 case Mips::ATOMIC_LOAD_ADD_I64:
1509 AtomicOp = Mips::ATOMIC_LOAD_ADD_I64_POSTRA;
1510 break;
1511 case Mips::ATOMIC_LOAD_SUB_I64:
1512 AtomicOp = Mips::ATOMIC_LOAD_SUB_I64_POSTRA;
1513 break;
1514 case Mips::ATOMIC_LOAD_AND_I64:
1515 AtomicOp = Mips::ATOMIC_LOAD_AND_I64_POSTRA;
1516 break;
1517 case Mips::ATOMIC_LOAD_OR_I64:
1518 AtomicOp = Mips::ATOMIC_LOAD_OR_I64_POSTRA;
1519 break;
1520 case Mips::ATOMIC_LOAD_XOR_I64:
1521 AtomicOp = Mips::ATOMIC_LOAD_XOR_I64_POSTRA;
1522 break;
1523 case Mips::ATOMIC_LOAD_NAND_I64:
1524 AtomicOp = Mips::ATOMIC_LOAD_NAND_I64_POSTRA;
1525 break;
1526 case Mips::ATOMIC_SWAP_I64:
1527 AtomicOp = Mips::ATOMIC_SWAP_I64_POSTRA;
1528 break;
1529 case Mips::ATOMIC_LOAD_MIN_I32:
1530 AtomicOp = Mips::ATOMIC_LOAD_MIN_I32_POSTRA;
1531 NeedsAdditionalReg = true;
1532 break;
1533 case Mips::ATOMIC_LOAD_MAX_I32:
1534 AtomicOp = Mips::ATOMIC_LOAD_MAX_I32_POSTRA;
1535 NeedsAdditionalReg = true;
1536 break;
1537 case Mips::ATOMIC_LOAD_UMIN_I32:
1538 AtomicOp = Mips::ATOMIC_LOAD_UMIN_I32_POSTRA;
1539 NeedsAdditionalReg = true;
1540 break;
1541 case Mips::ATOMIC_LOAD_UMAX_I32:
1542 AtomicOp = Mips::ATOMIC_LOAD_UMAX_I32_POSTRA;
1543 NeedsAdditionalReg = true;
1544 break;
1545 case Mips::ATOMIC_LOAD_MIN_I64:
1546 AtomicOp = Mips::ATOMIC_LOAD_MIN_I64_POSTRA;
1547 NeedsAdditionalReg = true;
1548 break;
1549 case Mips::ATOMIC_LOAD_MAX_I64:
1550 AtomicOp = Mips::ATOMIC_LOAD_MAX_I64_POSTRA;
1551 NeedsAdditionalReg = true;
1552 break;
1553 case Mips::ATOMIC_LOAD_UMIN_I64:
1554 AtomicOp = Mips::ATOMIC_LOAD_UMIN_I64_POSTRA;
1555 NeedsAdditionalReg = true;
1556 break;
1557 case Mips::ATOMIC_LOAD_UMAX_I64:
1558 AtomicOp = Mips::ATOMIC_LOAD_UMAX_I64_POSTRA;
1559 NeedsAdditionalReg = true;
1560 break;
1561 default:
1562 llvm_unreachable("Unknown pseudo atomic for replacement!");
1563 }
1564
1565 Register OldVal = MI.getOperand(0).getReg();
1566 Register Ptr = MI.getOperand(1).getReg();
1567 Register Incr = MI.getOperand(2).getReg();
1568 Register Scratch = RegInfo.createVirtualRegister(RegInfo.getRegClass(OldVal));
1569
1571
1572 // The scratch registers here with the EarlyClobber | Define | Implicit
1573 // flags is used to persuade the register allocator and the machine
1574 // verifier to accept the usage of this register. This has to be a real
1575 // register which has an UNDEF value but is dead after the instruction which
1576 // is unique among the registers chosen for the instruction.
1577
1578 // The EarlyClobber flag has the semantic properties that the operand it is
1579 // attached to is clobbered before the rest of the inputs are read. Hence it
1580 // must be unique among the operands to the instruction.
1581 // The Define flag is needed to coerce the machine verifier that an Undef
1582 // value isn't a problem.
1583 // The Dead flag is needed as the value in scratch isn't used by any other
1584 // instruction. Kill isn't used as Dead is more precise.
1585 // The implicit flag is here due to the interaction between the other flags
1586 // and the machine verifier.
1587
1588 // For correctness purpose, a new pseudo is introduced here. We need this
1589 // new pseudo, so that FastRegisterAllocator does not see an ll/sc sequence
1590 // that is spread over >1 basic blocks. A register allocator which
1591 // introduces (or any codegen infact) a store, can violate the expectations
1592 // of the hardware.
1593 //
1594 // An atomic read-modify-write sequence starts with a linked load
1595 // instruction and ends with a store conditional instruction. The atomic
1596 // read-modify-write sequence fails if any of the following conditions
1597 // occur between the execution of ll and sc:
1598 // * A coherent store is completed by another process or coherent I/O
1599 // module into the block of synchronizable physical memory containing
1600 // the word. The size and alignment of the block is
1601 // implementation-dependent.
1602 // * A coherent store is executed between an LL and SC sequence on the
1603 // same processor to the block of synchornizable physical memory
1604 // containing the word.
1605 //
1606
1607 Register PtrCopy = RegInfo.createVirtualRegister(RegInfo.getRegClass(Ptr));
1608 Register IncrCopy = RegInfo.createVirtualRegister(RegInfo.getRegClass(Incr));
1609
1610 BuildMI(*BB, II, DL, TII->get(Mips::COPY), IncrCopy).addReg(Incr);
1611 BuildMI(*BB, II, DL, TII->get(Mips::COPY), PtrCopy).addReg(Ptr);
1612
1614 BuildMI(*BB, II, DL, TII->get(AtomicOp))
1616 .addReg(PtrCopy)
1617 .addReg(IncrCopy)
1620 if (NeedsAdditionalReg) {
1621 Register Scratch2 =
1622 RegInfo.createVirtualRegister(RegInfo.getRegClass(OldVal));
1625 }
1626
1627 MI.eraseFromParent();
1628
1629 return BB;
1630}
1631
1632MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
1633 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
1634 unsigned SrcReg) const {
1635 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1636 const DebugLoc &DL = MI.getDebugLoc();
1637
1638 if (Subtarget.hasMips32r2() && Size == 1) {
1639 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1640 return BB;
1641 }
1642
1643 if (Subtarget.hasMips32r2() && Size == 2) {
1644 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1645 return BB;
1646 }
1647
1648 MachineFunction *MF = BB->getParent();
1649 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1650 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1651 Register ScrReg = RegInfo.createVirtualRegister(RC);
1652
1653 assert(Size < 32);
1654 int64_t ShiftImm = 32 - (Size * 8);
1655
1656 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1657 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1658
1659 return BB;
1660}
1661
1662MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
1663 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size) const {
1664 assert((Size == 1 || Size == 2) &&
1665 "Unsupported size for EmitAtomicBinaryPartial.");
1666
1667 MachineFunction *MF = BB->getParent();
1668 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1669 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1670 const bool ArePtrs64bit = ABI.ArePtrs64bit();
1671 const TargetRegisterClass *RCp =
1672 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32);
1673 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1674 DebugLoc DL = MI.getDebugLoc();
1675
1676 Register Dest = MI.getOperand(0).getReg();
1677 Register Ptr = MI.getOperand(1).getReg();
1678 Register Incr = MI.getOperand(2).getReg();
1679
1680 Register AlignedAddr = RegInfo.createVirtualRegister(RCp);
1681 Register ShiftAmt = RegInfo.createVirtualRegister(RC);
1682 Register Mask = RegInfo.createVirtualRegister(RC);
1683 Register Mask2 = RegInfo.createVirtualRegister(RC);
1684 Register Incr2 = RegInfo.createVirtualRegister(RC);
1685 Register MaskLSB2 = RegInfo.createVirtualRegister(RCp);
1686 Register PtrLSB2 = RegInfo.createVirtualRegister(RC);
1687 Register MaskUpper = RegInfo.createVirtualRegister(RC);
1688 Register Scratch = RegInfo.createVirtualRegister(RC);
1689 Register Scratch2 = RegInfo.createVirtualRegister(RC);
1690 Register Scratch3 = RegInfo.createVirtualRegister(RC);
1691
1692 unsigned AtomicOp = 0;
1693 bool NeedsAdditionalReg = false;
1694 switch (MI.getOpcode()) {
1695 case Mips::ATOMIC_LOAD_NAND_I8:
1696 AtomicOp = Mips::ATOMIC_LOAD_NAND_I8_POSTRA;
1697 break;
1698 case Mips::ATOMIC_LOAD_NAND_I16:
1699 AtomicOp = Mips::ATOMIC_LOAD_NAND_I16_POSTRA;
1700 break;
1701 case Mips::ATOMIC_SWAP_I8:
1702 AtomicOp = Mips::ATOMIC_SWAP_I8_POSTRA;
1703 break;
1704 case Mips::ATOMIC_SWAP_I16:
1705 AtomicOp = Mips::ATOMIC_SWAP_I16_POSTRA;
1706 break;
1707 case Mips::ATOMIC_LOAD_ADD_I8:
1708 AtomicOp = Mips::ATOMIC_LOAD_ADD_I8_POSTRA;
1709 break;
1710 case Mips::ATOMIC_LOAD_ADD_I16:
1711 AtomicOp = Mips::ATOMIC_LOAD_ADD_I16_POSTRA;
1712 break;
1713 case Mips::ATOMIC_LOAD_SUB_I8:
1714 AtomicOp = Mips::ATOMIC_LOAD_SUB_I8_POSTRA;
1715 break;
1716 case Mips::ATOMIC_LOAD_SUB_I16:
1717 AtomicOp = Mips::ATOMIC_LOAD_SUB_I16_POSTRA;
1718 break;
1719 case Mips::ATOMIC_LOAD_AND_I8:
1720 AtomicOp = Mips::ATOMIC_LOAD_AND_I8_POSTRA;
1721 break;
1722 case Mips::ATOMIC_LOAD_AND_I16:
1723 AtomicOp = Mips::ATOMIC_LOAD_AND_I16_POSTRA;
1724 break;
1725 case Mips::ATOMIC_LOAD_OR_I8:
1726 AtomicOp = Mips::ATOMIC_LOAD_OR_I8_POSTRA;
1727 break;
1728 case Mips::ATOMIC_LOAD_OR_I16:
1729 AtomicOp = Mips::ATOMIC_LOAD_OR_I16_POSTRA;
1730 break;
1731 case Mips::ATOMIC_LOAD_XOR_I8:
1732 AtomicOp = Mips::ATOMIC_LOAD_XOR_I8_POSTRA;
1733 break;
1734 case Mips::ATOMIC_LOAD_XOR_I16:
1735 AtomicOp = Mips::ATOMIC_LOAD_XOR_I16_POSTRA;
1736 break;
1737 case Mips::ATOMIC_LOAD_MIN_I8:
1738 AtomicOp = Mips::ATOMIC_LOAD_MIN_I8_POSTRA;
1739 NeedsAdditionalReg = true;
1740 break;
1741 case Mips::ATOMIC_LOAD_MIN_I16:
1742 AtomicOp = Mips::ATOMIC_LOAD_MIN_I16_POSTRA;
1743 NeedsAdditionalReg = true;
1744 break;
1745 case Mips::ATOMIC_LOAD_MAX_I8:
1746 AtomicOp = Mips::ATOMIC_LOAD_MAX_I8_POSTRA;
1747 NeedsAdditionalReg = true;
1748 break;
1749 case Mips::ATOMIC_LOAD_MAX_I16:
1750 AtomicOp = Mips::ATOMIC_LOAD_MAX_I16_POSTRA;
1751 NeedsAdditionalReg = true;
1752 break;
1753 case Mips::ATOMIC_LOAD_UMIN_I8:
1754 AtomicOp = Mips::ATOMIC_LOAD_UMIN_I8_POSTRA;
1755 NeedsAdditionalReg = true;
1756 break;
1757 case Mips::ATOMIC_LOAD_UMIN_I16:
1758 AtomicOp = Mips::ATOMIC_LOAD_UMIN_I16_POSTRA;
1759 NeedsAdditionalReg = true;
1760 break;
1761 case Mips::ATOMIC_LOAD_UMAX_I8:
1762 AtomicOp = Mips::ATOMIC_LOAD_UMAX_I8_POSTRA;
1763 NeedsAdditionalReg = true;
1764 break;
1765 case Mips::ATOMIC_LOAD_UMAX_I16:
1766 AtomicOp = Mips::ATOMIC_LOAD_UMAX_I16_POSTRA;
1767 NeedsAdditionalReg = true;
1768 break;
1769 default:
1770 llvm_unreachable("Unknown subword atomic pseudo for expansion!");
1771 }
1772
1773 // insert new blocks after the current block
1774 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1775 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1777 MF->insert(It, exitMBB);
1778
1779 // Transfer the remainder of BB and its successor edges to exitMBB.
1780 exitMBB->splice(exitMBB->begin(), BB,
1781 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1783
1785
1786 // thisMBB:
1787 // addiu masklsb2,$0,-4 # 0xfffffffc
1788 // and alignedaddr,ptr,masklsb2
1789 // andi ptrlsb2,ptr,3
1790 // sll shiftamt,ptrlsb2,3
1791 // ori maskupper,$0,255 # 0xff
1792 // sll mask,maskupper,shiftamt
1793 // nor mask2,$0,mask
1794 // sll incr2,incr,shiftamt
1795
1796 int64_t MaskImm = (Size == 1) ? 255 : 65535;
1797 BuildMI(BB, DL, TII->get(ABI.GetPtrAddiuOp()), MaskLSB2)
1798 .addReg(ABI.GetNullPtr()).addImm(-4);
1799 BuildMI(BB, DL, TII->get(ABI.GetPtrAndOp()), AlignedAddr)
1800 .addReg(Ptr).addReg(MaskLSB2);
1801 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2)
1802 .addReg(Ptr, 0, ArePtrs64bit ? Mips::sub_32 : 0).addImm(3);
1803 if (Subtarget.isLittle()) {
1804 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1805 } else {
1806 Register Off = RegInfo.createVirtualRegister(RC);
1807 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1808 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1809 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1810 }
1811 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
1812 .addReg(Mips::ZERO).addImm(MaskImm);
1813 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
1814 .addReg(MaskUpper).addReg(ShiftAmt);
1815 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1816 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
1817
1818
1819 // The purposes of the flags on the scratch registers is explained in
1820 // emitAtomicBinary. In summary, we need a scratch register which is going to
1821 // be undef, that is unique among registers chosen for the instruction.
1822
1823 MachineInstrBuilder MIB =
1824 BuildMI(BB, DL, TII->get(AtomicOp))
1826 .addReg(AlignedAddr)
1827 .addReg(Incr2)
1828 .addReg(Mask)
1829 .addReg(Mask2)
1830 .addReg(ShiftAmt)
1837 if (NeedsAdditionalReg) {
1838 Register Scratch4 = RegInfo.createVirtualRegister(RC);
1841 }
1842
1843 MI.eraseFromParent(); // The instruction is gone now.
1844
1845 return exitMBB;
1846}
1847
1848// Lower atomic compare and swap to a pseudo instruction, taking care to
1849// define a scratch register for the pseudo instruction's expansion. The
1850// instruction is expanded after the register allocator as to prevent
1851// the insertion of stores between the linked load and the store conditional.
1852
1854MipsTargetLowering::emitAtomicCmpSwap(MachineInstr &MI,
1855 MachineBasicBlock *BB) const {
1856
1857 assert((MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32 ||
1858 MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I64) &&
1859 "Unsupported atomic pseudo for EmitAtomicCmpSwap.");
1860
1861 const unsigned Size = MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32 ? 4 : 8;
1862
1863 MachineFunction *MF = BB->getParent();
1864 MachineRegisterInfo &MRI = MF->getRegInfo();
1865 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
1866 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1867 DebugLoc DL = MI.getDebugLoc();
1868
1869 unsigned AtomicOp = MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32
1870 ? Mips::ATOMIC_CMP_SWAP_I32_POSTRA
1871 : Mips::ATOMIC_CMP_SWAP_I64_POSTRA;
1872 Register Dest = MI.getOperand(0).getReg();
1873 Register Ptr = MI.getOperand(1).getReg();
1874 Register OldVal = MI.getOperand(2).getReg();
1875 Register NewVal = MI.getOperand(3).getReg();
1876
1877 Register Scratch = MRI.createVirtualRegister(RC);
1879
1880 // We need to create copies of the various registers and kill them at the
1881 // atomic pseudo. If the copies are not made, when the atomic is expanded
1882 // after fast register allocation, the spills will end up outside of the
1883 // blocks that their values are defined in, causing livein errors.
1884
1885 Register PtrCopy = MRI.createVirtualRegister(MRI.getRegClass(Ptr));
1886 Register OldValCopy = MRI.createVirtualRegister(MRI.getRegClass(OldVal));
1887 Register NewValCopy = MRI.createVirtualRegister(MRI.getRegClass(NewVal));
1888
1889 BuildMI(*BB, II, DL, TII->get(Mips::COPY), PtrCopy).addReg(Ptr);
1890 BuildMI(*BB, II, DL, TII->get(Mips::COPY), OldValCopy).addReg(OldVal);
1891 BuildMI(*BB, II, DL, TII->get(Mips::COPY), NewValCopy).addReg(NewVal);
1892
1893 // The purposes of the flags on the scratch registers is explained in
1894 // emitAtomicBinary. In summary, we need a scratch register which is going to
1895 // be undef, that is unique among registers chosen for the instruction.
1896
1897 BuildMI(*BB, II, DL, TII->get(AtomicOp))
1899 .addReg(PtrCopy, RegState::Kill)
1900 .addReg(OldValCopy, RegState::Kill)
1901 .addReg(NewValCopy, RegState::Kill)
1904
1905 MI.eraseFromParent(); // The instruction is gone now.
1906
1907 return BB;
1908}
1909
1910MachineBasicBlock *MipsTargetLowering::emitAtomicCmpSwapPartword(
1911 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size) const {
1912 assert((Size == 1 || Size == 2) &&
1913 "Unsupported size for EmitAtomicCmpSwapPartial.");
1914
1915 MachineFunction *MF = BB->getParent();
1916 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1917 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1918 const bool ArePtrs64bit = ABI.ArePtrs64bit();
1919 const TargetRegisterClass *RCp =
1920 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32);
1921 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1922 DebugLoc DL = MI.getDebugLoc();
1923
1924 Register Dest = MI.getOperand(0).getReg();
1925 Register Ptr = MI.getOperand(1).getReg();
1926 Register CmpVal = MI.getOperand(2).getReg();
1927 Register NewVal = MI.getOperand(3).getReg();
1928
1929 Register AlignedAddr = RegInfo.createVirtualRegister(RCp);
1930 Register ShiftAmt = RegInfo.createVirtualRegister(RC);
1931 Register Mask = RegInfo.createVirtualRegister(RC);
1932 Register Mask2 = RegInfo.createVirtualRegister(RC);
1933 Register ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1934 Register ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1935 Register MaskLSB2 = RegInfo.createVirtualRegister(RCp);
1936 Register PtrLSB2 = RegInfo.createVirtualRegister(RC);
1937 Register MaskUpper = RegInfo.createVirtualRegister(RC);
1938 Register MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1939 Register MaskedNewVal = RegInfo.createVirtualRegister(RC);
1940 unsigned AtomicOp = MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I8
1941 ? Mips::ATOMIC_CMP_SWAP_I8_POSTRA
1942 : Mips::ATOMIC_CMP_SWAP_I16_POSTRA;
1943
1944 // The scratch registers here with the EarlyClobber | Define | Dead | Implicit
1945 // flags are used to coerce the register allocator and the machine verifier to
1946 // accept the usage of these registers.
1947 // The EarlyClobber flag has the semantic properties that the operand it is
1948 // attached to is clobbered before the rest of the inputs are read. Hence it
1949 // must be unique among the operands to the instruction.
1950 // The Define flag is needed to coerce the machine verifier that an Undef
1951 // value isn't a problem.
1952 // The Dead flag is needed as the value in scratch isn't used by any other
1953 // instruction. Kill isn't used as Dead is more precise.
1954 Register Scratch = RegInfo.createVirtualRegister(RC);
1955 Register Scratch2 = RegInfo.createVirtualRegister(RC);
1956
1957 // insert new blocks after the current block
1958 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1959 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1961 MF->insert(It, exitMBB);
1962
1963 // Transfer the remainder of BB and its successor edges to exitMBB.
1964 exitMBB->splice(exitMBB->begin(), BB,
1965 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1967
1969
1970 // thisMBB:
1971 // addiu masklsb2,$0,-4 # 0xfffffffc
1972 // and alignedaddr,ptr,masklsb2
1973 // andi ptrlsb2,ptr,3
1974 // xori ptrlsb2,ptrlsb2,3 # Only for BE
1975 // sll shiftamt,ptrlsb2,3
1976 // ori maskupper,$0,255 # 0xff
1977 // sll mask,maskupper,shiftamt
1978 // nor mask2,$0,mask
1979 // andi maskedcmpval,cmpval,255
1980 // sll shiftedcmpval,maskedcmpval,shiftamt
1981 // andi maskednewval,newval,255
1982 // sll shiftednewval,maskednewval,shiftamt
1983 int64_t MaskImm = (Size == 1) ? 255 : 65535;
1984 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::DADDiu : Mips::ADDiu), MaskLSB2)
1985 .addReg(ABI.GetNullPtr()).addImm(-4);
1986 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::AND64 : Mips::AND), AlignedAddr)
1987 .addReg(Ptr).addReg(MaskLSB2);
1988 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2)
1989 .addReg(Ptr, 0, ArePtrs64bit ? Mips::sub_32 : 0).addImm(3);
1990 if (Subtarget.isLittle()) {
1991 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1992 } else {
1993 Register Off = RegInfo.createVirtualRegister(RC);
1994 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1995 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1996 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1997 }
1998 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
1999 .addReg(Mips::ZERO).addImm(MaskImm);
2000 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
2001 .addReg(MaskUpper).addReg(ShiftAmt);
2002 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
2003 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
2004 .addReg(CmpVal).addImm(MaskImm);
2005 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
2006 .addReg(MaskedCmpVal).addReg(ShiftAmt);
2007 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
2008 .addReg(NewVal).addImm(MaskImm);
2009 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
2010 .addReg(MaskedNewVal).addReg(ShiftAmt);
2011
2012 // The purposes of the flags on the scratch registers are explained in
2013 // emitAtomicBinary. In summary, we need a scratch register which is going to
2014 // be undef, that is unique among the register chosen for the instruction.
2015
2016 BuildMI(BB, DL, TII->get(AtomicOp))
2018 .addReg(AlignedAddr)
2019 .addReg(Mask)
2020 .addReg(ShiftedCmpVal)
2021 .addReg(Mask2)
2022 .addReg(ShiftedNewVal)
2023 .addReg(ShiftAmt)
2028
2029 MI.eraseFromParent(); // The instruction is gone now.
2030
2031 return exitMBB;
2032}
2033
2034SDValue MipsTargetLowering::lowerREADCYCLECOUNTER(SDValue Op,
2035 SelectionDAG &DAG) const {
2037 SDLoc DL(Op);
2038 MachineFunction &MF = DAG.getMachineFunction();
2039 unsigned RdhwrOpc, DestReg;
2040 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2041
2042 if (PtrVT == MVT::i64) {
2043 RdhwrOpc = Mips::RDHWR64;
2044 DestReg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
2045 SDNode *Rdhwr = DAG.getMachineNode(RdhwrOpc, DL, MVT::i64, MVT::Glue,
2046 DAG.getRegister(Mips::HWR2, MVT::i32),
2047 DAG.getTargetConstant(0, DL, MVT::i32));
2048 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), DL, DestReg,
2049 SDValue(Rdhwr, 0), SDValue(Rdhwr, 1));
2050 SDValue ResNode =
2051 DAG.getCopyFromReg(Chain, DL, DestReg, MVT::i64, Chain.getValue(1));
2052 Results.push_back(ResNode);
2053 Results.push_back(ResNode.getValue(1));
2054 } else {
2055 RdhwrOpc = Mips::RDHWR;
2056 DestReg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
2057 SDNode *Rdhwr = DAG.getMachineNode(RdhwrOpc, DL, MVT::i32, MVT::Glue,
2058 DAG.getRegister(Mips::HWR2, MVT::i32),
2059 DAG.getTargetConstant(0, DL, MVT::i32));
2060 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), DL, DestReg,
2061 SDValue(Rdhwr, 0), SDValue(Rdhwr, 1));
2062 SDValue ResNode =
2063 DAG.getCopyFromReg(Chain, DL, DestReg, MVT::i32, Chain.getValue(1));
2064 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, ResNode,
2065 DAG.getConstant(0, DL, MVT::i32)));
2066 Results.push_back(ResNode.getValue(1));
2067 }
2068
2069 return DAG.getMergeValues(Results, DL);
2070}
2071
2072SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
2073 // The first operand is the chain, the second is the condition, the third is
2074 // the block to branch to if the condition is true.
2075 SDValue Chain = Op.getOperand(0);
2076 SDValue Dest = Op.getOperand(2);
2077 SDLoc DL(Op);
2078
2079 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
2080 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
2081
2082 // Return if flag is not set by a floating point comparison.
2083 if (CondRes.getOpcode() != MipsISD::FPCmp)
2084 return Op;
2085
2086 SDValue CCNode = CondRes.getOperand(2);
2089 SDValue BrCode = DAG.getConstant(Opc, DL, MVT::i32);
2090 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
2091 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
2092 FCC0, Dest, CondRes);
2093}
2094
2095SDValue MipsTargetLowering::
2096lowerSELECT(SDValue Op, SelectionDAG &DAG) const
2097{
2098 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
2099 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
2100
2101 // Return if flag is not set by a floating point comparison.
2102 if (Cond.getOpcode() != MipsISD::FPCmp)
2103 return Op;
2104
2105 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
2106 SDLoc(Op));
2107}
2108
2109SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
2110 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
2111 SDValue Cond = createFPCmp(DAG, Op);
2112
2113 assert(Cond.getOpcode() == MipsISD::FPCmp &&
2114 "Floating point operand expected.");
2115
2116 SDLoc DL(Op);
2117 SDValue True = DAG.getConstant(1, DL, MVT::i32);
2118 SDValue False = DAG.getConstant(0, DL, MVT::i32);
2119
2120 return createCMovFP(DAG, Cond, True, False, DL);
2121}
2122
2123SDValue MipsTargetLowering::lowerFSETCC(SDValue Op, SelectionDAG &DAG) const {
2124 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
2125
2126 SDLoc DL(Op);
2127 SDValue Chain = Op.getOperand(0);
2128 SDValue LHS = Op.getOperand(1);
2129 SDValue RHS = Op.getOperand(2);
2130 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(3))->get();
2131
2132 SDValue Cond = DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
2133 DAG.getConstant(condCodeToFCC(CC), DL, MVT::i32));
2134 SDValue True = DAG.getConstant(1, DL, MVT::i32);
2135 SDValue False = DAG.getConstant(0, DL, MVT::i32);
2136 SDValue CMovFP = createCMovFP(DAG, Cond, True, False, DL);
2137
2138 return DAG.getMergeValues({CMovFP, Chain}, DL);
2139}
2140
2141SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
2142 SelectionDAG &DAG) const {
2143 EVT Ty = Op.getValueType();
2144 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
2145 const GlobalValue *GV = N->getGlobal();
2146
2147 if (GV->hasDLLImportStorageClass()) {
2148 assert(Subtarget.isTargetWindows() &&
2149 "Windows is the only supported COFF target");
2150 return getDllimportVariable(
2151 N, SDLoc(N), Ty, DAG, DAG.getEntryNode(),
2153 }
2154
2155 if (!isPositionIndependent()) {
2156 const MipsTargetObjectFile *TLOF =
2157 static_cast<const MipsTargetObjectFile *>(
2159 const GlobalObject *GO = GV->getAliaseeObject();
2160 if (GO && TLOF->IsGlobalInSmallSection(GO, getTargetMachine()))
2161 // %gp_rel relocation
2162 return getAddrGPRel(N, SDLoc(N), Ty, DAG, ABI.IsN64());
2163
2164 // %hi/%lo relocation
2165 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2166 // %highest/%higher/%hi/%lo relocation
2167 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
2168 }
2169
2170 // Every other architecture would use shouldAssumeDSOLocal in here, but
2171 // mips is special.
2172 // * In PIC code mips requires got loads even for local statics!
2173 // * To save on got entries, for local statics the got entry contains the
2174 // page and an additional add instruction takes care of the low bits.
2175 // * It is legal to access a hidden symbol with a non hidden undefined,
2176 // so one cannot guarantee that all access to a hidden symbol will know
2177 // it is hidden.
2178 // * Mips linkers don't support creating a page and a full got entry for
2179 // the same symbol.
2180 // * Given all that, we have to use a full got entry for hidden symbols :-(
2181 if (GV->hasLocalLinkage())
2182 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
2183
2184 if (Subtarget.useXGOT())
2185 return getAddrGlobalLargeGOT(
2186 N, SDLoc(N), Ty, DAG, MipsII::MO_GOT_HI16, MipsII::MO_GOT_LO16,
2187 DAG.getEntryNode(),
2189
2190 return getAddrGlobal(
2191 N, SDLoc(N), Ty, DAG,
2192 (ABI.IsN32() || ABI.IsN64()) ? MipsII::MO_GOT_DISP : MipsII::MO_GOT,
2194}
2195
2196SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
2197 SelectionDAG &DAG) const {
2198 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
2199 EVT Ty = Op.getValueType();
2200
2201 if (!isPositionIndependent())
2202 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2203 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
2204
2205 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
2206}
2207
2208SDValue MipsTargetLowering::
2209lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
2210{
2211 // If the relocation model is PIC, use the General Dynamic TLS Model or
2212 // Local Dynamic TLS model, otherwise use the Initial Exec or
2213 // Local Exec TLS Model.
2214
2215 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2216 if (DAG.getTarget().useEmulatedTLS())
2217 return LowerToTLSEmulatedModel(GA, DAG);
2218
2219 SDLoc DL(GA);
2220 const GlobalValue *GV = GA->getGlobal();
2221 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2222
2224
2225 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
2226 // General Dynamic and Local Dynamic TLS Model.
2227 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
2228 : MipsII::MO_TLSGD;
2229
2230 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
2231 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
2232 getGlobalReg(DAG, PtrVT), TGA);
2233 unsigned PtrSize = PtrVT.getSizeInBits();
2234 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
2235
2236 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
2237
2239 Args.emplace_back(Argument, PtrTy);
2240
2241 TargetLowering::CallLoweringInfo CLI(DAG);
2242 CLI.setDebugLoc(DL)
2243 .setChain(DAG.getEntryNode())
2244 .setLibCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args));
2245 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
2246
2247 SDValue Ret = CallResult.first;
2248
2249 if (model != TLSModel::LocalDynamic)
2250 return Ret;
2251
2252 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2254 SDValue Hi = DAG.getNode(MipsISD::TlsHi, DL, PtrVT, TGAHi);
2255 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2257 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
2258 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
2259 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
2260 }
2261
2263 if (model == TLSModel::InitialExec) {
2264 // Initial Exec TLS Model
2265 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2267 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
2268 TGA);
2269 Offset =
2270 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), TGA, MachinePointerInfo());
2271 } else {
2272 // Local Exec TLS Model
2273 assert(model == TLSModel::LocalExec);
2274 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2276 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2278 SDValue Hi = DAG.getNode(MipsISD::TlsHi, DL, PtrVT, TGAHi);
2279 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
2280 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
2281 }
2282
2283 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
2284 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
2285}
2286
2287SDValue MipsTargetLowering::
2288lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
2289{
2290 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
2291 EVT Ty = Op.getValueType();
2292
2293 if (!isPositionIndependent())
2294 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2295 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
2296
2297 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
2298}
2299
2300SDValue MipsTargetLowering::
2301lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
2302{
2303 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
2304 EVT Ty = Op.getValueType();
2305
2306 if (!isPositionIndependent()) {
2307 const MipsTargetObjectFile *TLOF =
2308 static_cast<const MipsTargetObjectFile *>(
2310
2311 if (TLOF->IsConstantInSmallSection(DAG.getDataLayout(), N->getConstVal(),
2313 // %gp_rel relocation
2314 return getAddrGPRel(N, SDLoc(N), Ty, DAG, ABI.IsN64());
2315
2316 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2317 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
2318 }
2319
2320 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
2321}
2322
2323SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
2324 MachineFunction &MF = DAG.getMachineFunction();
2325 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
2326
2327 SDLoc DL(Op);
2328 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
2330
2331 // vastart just stores the address of the VarArgsFrameIndex slot into the
2332 // memory location argument.
2333 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
2334 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
2335 MachinePointerInfo(SV));
2336}
2337
2338SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
2339 SDNode *Node = Op.getNode();
2340 EVT VT = Node->getValueType(0);
2341 SDValue Chain = Node->getOperand(0);
2342 SDValue VAListPtr = Node->getOperand(1);
2343 const Align Align =
2344 llvm::MaybeAlign(Node->getConstantOperandVal(3)).valueOrOne();
2345 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2346 SDLoc DL(Node);
2347 unsigned ArgSlotSizeInBytes = (ABI.IsN32() || ABI.IsN64()) ? 8 : 4;
2348
2349 SDValue VAListLoad = DAG.getLoad(getPointerTy(DAG.getDataLayout()), DL, Chain,
2350 VAListPtr, MachinePointerInfo(SV));
2351 SDValue VAList = VAListLoad;
2352
2353 // Re-align the pointer if necessary.
2354 // It should only ever be necessary for 64-bit types on O32 since the minimum
2355 // argument alignment is the same as the maximum type alignment for N32/N64.
2356 //
2357 // FIXME: We currently align too often. The code generator doesn't notice
2358 // when the pointer is still aligned from the last va_arg (or pair of
2359 // va_args for the i64 on O32 case).
2360 if (Align > getMinStackArgumentAlignment()) {
2361 VAList = DAG.getNode(
2362 ISD::ADD, DL, VAList.getValueType(), VAList,
2363 DAG.getConstant(Align.value() - 1, DL, VAList.getValueType()));
2364
2365 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
2366 DAG.getSignedConstant(-(int64_t)Align.value(), DL,
2367 VAList.getValueType()));
2368 }
2369
2370 // Increment the pointer, VAList, to the next vaarg.
2371 auto &TD = DAG.getDataLayout();
2372 unsigned ArgSizeInBytes =
2374 SDValue Tmp3 =
2375 DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
2376 DAG.getConstant(alignTo(ArgSizeInBytes, ArgSlotSizeInBytes),
2377 DL, VAList.getValueType()));
2378 // Store the incremented VAList to the legalized pointer
2379 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
2380 MachinePointerInfo(SV));
2381
2382 // In big-endian mode we must adjust the pointer when the load size is smaller
2383 // than the argument slot size. We must also reduce the known alignment to
2384 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
2385 // the correct half of the slot, and reduce the alignment from 8 (slot
2386 // alignment) down to 4 (type alignment).
2387 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
2388 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
2389 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
2390 DAG.getIntPtrConstant(Adjustment, DL));
2391 }
2392 // Load the actual argument out of the pointer VAList
2393 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo());
2394}
2395
2397 bool HasExtractInsert) {
2398 EVT TyX = Op.getOperand(0).getValueType();
2399 EVT TyY = Op.getOperand(1).getValueType();
2400 SDLoc DL(Op);
2401 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
2402 SDValue Const31 = DAG.getConstant(31, DL, MVT::i32);
2403 SDValue Res;
2404
2405 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2406 // to i32.
2407 SDValue X = (TyX == MVT::f32) ?
2408 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
2409 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2410 Const1);
2411 SDValue Y = (TyY == MVT::f32) ?
2412 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
2413 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
2414 Const1);
2415
2416 if (HasExtractInsert) {
2417 // ext E, Y, 31, 1 ; extract bit31 of Y
2418 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
2419 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
2420 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
2421 } else {
2422 // sll SllX, X, 1
2423 // srl SrlX, SllX, 1
2424 // srl SrlY, Y, 31
2425 // sll SllY, SrlX, 31
2426 // or Or, SrlX, SllY
2427 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2428 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2429 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
2430 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
2431 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
2432 }
2433
2434 if (TyX == MVT::f32)
2435 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
2436
2437 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2438 Op.getOperand(0),
2439 DAG.getConstant(0, DL, MVT::i32));
2440 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
2441}
2442
2444 bool HasExtractInsert) {
2445 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
2446 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
2447 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
2448 SDLoc DL(Op);
2449 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
2450
2451 // Bitcast to integer nodes.
2452 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
2453 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
2454
2455 if (HasExtractInsert) {
2456 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
2457 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
2458 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
2459 DAG.getConstant(WidthY - 1, DL, MVT::i32), Const1);
2460
2461 if (WidthX > WidthY)
2462 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
2463 else if (WidthY > WidthX)
2464 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
2465
2466 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
2467 DAG.getConstant(WidthX - 1, DL, MVT::i32), Const1,
2468 X);
2469 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
2470 }
2471
2472 // (d)sll SllX, X, 1
2473 // (d)srl SrlX, SllX, 1
2474 // (d)srl SrlY, Y, width(Y)-1
2475 // (d)sll SllY, SrlX, width(Y)-1
2476 // or Or, SrlX, SllY
2477 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
2478 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
2479 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
2480 DAG.getConstant(WidthY - 1, DL, MVT::i32));
2481
2482 if (WidthX > WidthY)
2483 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
2484 else if (WidthY > WidthX)
2485 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
2486
2487 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
2488 DAG.getConstant(WidthX - 1, DL, MVT::i32));
2489 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
2490 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
2491}
2492
2493SDValue
2494MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
2495 if (Subtarget.isGP64bit())
2496 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
2497
2498 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
2499}
2500
2501SDValue MipsTargetLowering::lowerFABS32(SDValue Op, SelectionDAG &DAG,
2502 bool HasExtractInsert) const {
2503 SDLoc DL(Op);
2504 SDValue Res, Const1 = DAG.getConstant(1, DL, MVT::i32);
2505
2506 if (DAG.getTarget().Options.NoNaNsFPMath || Subtarget.inAbs2008Mode())
2507 return DAG.getNode(MipsISD::FAbs, DL, Op.getValueType(), Op.getOperand(0));
2508
2509 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2510 // to i32.
2511 SDValue X = (Op.getValueType() == MVT::f32)
2512 ? DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0))
2513 : DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2514 Op.getOperand(0), Const1);
2515
2516 // Clear MSB.
2517 if (HasExtractInsert)
2518 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
2519 DAG.getRegister(Mips::ZERO, MVT::i32),
2520 DAG.getConstant(31, DL, MVT::i32), Const1, X);
2521 else {
2522 // TODO: Provide DAG patterns which transform (and x, cst)
2523 // back to a (shl (srl x (clz cst)) (clz cst)) sequence.
2524 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2525 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2526 }
2527
2528 if (Op.getValueType() == MVT::f32)
2529 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
2530
2531 // FIXME: For mips32r2, the sequence of (BuildPairF64 (ins (ExtractElementF64
2532 // Op 1), $zero, 31 1) (ExtractElementF64 Op 0)) and the Op has one use, we
2533 // should be able to drop the usage of mfc1/mtc1 and rewrite the register in
2534 // place.
2535 SDValue LowX =
2536 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2537 DAG.getConstant(0, DL, MVT::i32));
2538 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
2539}
2540
2541SDValue MipsTargetLowering::lowerFABS64(SDValue Op, SelectionDAG &DAG,
2542 bool HasExtractInsert) const {
2543 SDLoc DL(Op);
2544 SDValue Res, Const1 = DAG.getConstant(1, DL, MVT::i32);
2545
2546 if (DAG.getTarget().Options.NoNaNsFPMath || Subtarget.inAbs2008Mode())
2547 return DAG.getNode(MipsISD::FAbs, DL, Op.getValueType(), Op.getOperand(0));
2548
2549 // Bitcast to integer node.
2550 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
2551
2552 // Clear MSB.
2553 if (HasExtractInsert)
2554 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
2555 DAG.getRegister(Mips::ZERO_64, MVT::i64),
2556 DAG.getConstant(63, DL, MVT::i32), Const1, X);
2557 else {
2558 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
2559 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
2560 }
2561
2562 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
2563}
2564
2565SDValue MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
2566 if ((ABI.IsN32() || ABI.IsN64()) && (Op.getValueType() == MVT::f64))
2567 return lowerFABS64(Op, DAG, Subtarget.hasExtractInsert());
2568
2569 return lowerFABS32(Op, DAG, Subtarget.hasExtractInsert());
2570}
2571
2572SDValue MipsTargetLowering::lowerFCANONICALIZE(SDValue Op,
2573 SelectionDAG &DAG) const {
2574 SDLoc DL(Op);
2575 EVT VT = Op.getValueType();
2576 SDValue Operand = Op.getOperand(0);
2577 SDNodeFlags Flags = Op->getFlags();
2578
2579 if (Flags.hasNoNaNs() || DAG.isKnownNeverNaN(Operand))
2580 return Operand;
2581
2582 SDValue Quiet = DAG.getNode(ISD::FADD, DL, VT, Operand, Operand);
2583 return DAG.getSelectCC(DL, Operand, Operand, Quiet, Operand, ISD::SETUO);
2584}
2585
2586SDValue MipsTargetLowering::
2587lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
2588 // check the depth
2589 if (Op.getConstantOperandVal(0) != 0) {
2590 DAG.getContext()->emitError(
2591 "return address can be determined only for current frame");
2592 return SDValue();
2593 }
2594
2595 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2596 MFI.setFrameAddressIsTaken(true);
2597 EVT VT = Op.getValueType();
2598 SDLoc DL(Op);
2599 SDValue FrameAddr = DAG.getCopyFromReg(
2600 DAG.getEntryNode(), DL, ABI.IsN64() ? Mips::FP_64 : Mips::FP, VT);
2601 return FrameAddr;
2602}
2603
2604SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
2605 SelectionDAG &DAG) const {
2606 // check the depth
2607 if (Op.getConstantOperandVal(0) != 0) {
2608 DAG.getContext()->emitError(
2609 "return address can be determined only for current frame");
2610 return SDValue();
2611 }
2612
2613 MachineFunction &MF = DAG.getMachineFunction();
2614 MachineFrameInfo &MFI = MF.getFrameInfo();
2615 MVT VT = Op.getSimpleValueType();
2616 unsigned RA = ABI.IsN64() ? Mips::RA_64 : Mips::RA;
2617 MFI.setReturnAddressIsTaken(true);
2618
2619 // Return RA, which contains the return address. Mark it an implicit live-in.
2621 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
2622}
2623
2624// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
2625// generated from __builtin_eh_return (offset, handler)
2626// The effect of this is to adjust the stack pointer by "offset"
2627// and then branch to "handler".
2628SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
2629 const {
2630 MachineFunction &MF = DAG.getMachineFunction();
2631 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2632
2633 MipsFI->setCallsEhReturn();
2634 SDValue Chain = Op.getOperand(0);
2635 SDValue Offset = Op.getOperand(1);
2636 SDValue Handler = Op.getOperand(2);
2637 SDLoc DL(Op);
2638 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
2639
2640 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2641 // EH_RETURN nodes, so that instructions are emitted back-to-back.
2642 unsigned OffsetReg = ABI.IsN64() ? Mips::V1_64 : Mips::V1;
2643 unsigned AddrReg = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
2644 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2645 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2646 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2647 DAG.getRegister(OffsetReg, Ty),
2648 DAG.getRegister(AddrReg, getPointerTy(MF.getDataLayout())),
2649 Chain.getValue(1));
2650}
2651
2652SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
2653 SelectionDAG &DAG) const {
2654 // FIXME: Need pseudo-fence for 'singlethread' fences
2655 // FIXME: Set SType for weaker fences where supported/appropriate.
2656 unsigned SType = 0;
2657 SDLoc DL(Op);
2658 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
2659 DAG.getConstant(SType, DL, MVT::i32));
2660}
2661
2662SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
2663 SelectionDAG &DAG) const {
2664 SDLoc DL(Op);
2665 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
2666
2667 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2668 SDValue Shamt = Op.getOperand(2);
2669 // if shamt < (VT.bits):
2670 // lo = (shl lo, shamt)
2671 // hi = (or (shl hi, shamt) (srl (srl lo, 1), (xor shamt, (VT.bits-1))))
2672 // else:
2673 // lo = 0
2674 // hi = (shl lo, shamt[4:0])
2675 SDValue Not =
2676 DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2677 DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32));
2678 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo,
2679 DAG.getConstant(1, DL, VT));
2680 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, Not);
2681 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
2682 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2683 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
2684 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2685 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
2686 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond,
2687 DAG.getConstant(0, DL, VT), ShiftLeftLo);
2688 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftLeftLo, Or);
2689
2690 SDValue Ops[2] = {Lo, Hi};
2691 return DAG.getMergeValues(Ops, DL);
2692}
2693
2694SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
2695 bool IsSRA) const {
2696 SDLoc DL(Op);
2697 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2698 SDValue Shamt = Op.getOperand(2);
2699 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
2700
2701 // if shamt < (VT.bits):
2702 // lo = (or (shl (shl hi, 1), (xor shamt, (VT.bits-1))) (srl lo, shamt))
2703 // if isSRA:
2704 // hi = (sra hi, shamt)
2705 // else:
2706 // hi = (srl hi, shamt)
2707 // else:
2708 // if isSRA:
2709 // lo = (sra hi, shamt[4:0])
2710 // hi = (sra hi, 31)
2711 // else:
2712 // lo = (srl hi, shamt[4:0])
2713 // hi = 0
2714 SDValue Not =
2715 DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2716 DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32));
2717 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, VT, Hi,
2718 DAG.getConstant(1, DL, VT));
2719 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, ShiftLeft1Hi, Not);
2720 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
2721 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2722 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL,
2723 DL, VT, Hi, Shamt);
2724 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2725 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
2726 SDValue Ext = DAG.getNode(ISD::SRA, DL, VT, Hi,
2727 DAG.getConstant(VT.getSizeInBits() - 1, DL, VT));
2728
2729 if (!(Subtarget.hasMips4() || Subtarget.hasMips32())) {
2730 SDVTList VTList = DAG.getVTList(VT, VT);
2731 return DAG.getNode(Subtarget.isGP64bit() ? MipsISD::DOUBLE_SELECT_I64
2733 DL, VTList, Cond, ShiftRightHi,
2734 IsSRA ? Ext : DAG.getConstant(0, DL, VT), Or,
2735 ShiftRightHi);
2736 }
2737
2738 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftRightHi, Or);
2739 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond,
2740 IsSRA ? Ext : DAG.getConstant(0, DL, VT), ShiftRightHi);
2741
2742 SDValue Ops[2] = {Lo, Hi};
2743 return DAG.getMergeValues(Ops, DL);
2744}
2745
2747 SDValue Chain, SDValue Src, unsigned Offset) {
2748 SDValue Ptr = LD->getBasePtr();
2749 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
2750 EVT BasePtrVT = Ptr.getValueType();
2751 SDLoc DL(LD);
2752 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2753
2754 if (Offset)
2755 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
2756 DAG.getConstant(Offset, DL, BasePtrVT));
2757
2758 SDValue Ops[] = { Chain, Ptr, Src };
2759 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
2760 LD->getMemOperand());
2761}
2762
2763// Expand an unaligned 32 or 64-bit integer load node.
2766 EVT MemVT = LD->getMemoryVT();
2767
2768 if (Subtarget.systemSupportsUnalignedAccess())
2769 return Op;
2770
2771 // Return if load is aligned or if MemVT is neither i32 nor i64.
2772 if ((LD->getAlign().value() >= (MemVT.getSizeInBits() / 8)) ||
2773 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2774 return SDValue();
2775
2776 bool IsLittle = Subtarget.isLittle();
2777 EVT VT = Op.getValueType();
2778 ISD::LoadExtType ExtType = LD->getExtensionType();
2779 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2780
2781 assert((VT == MVT::i32) || (VT == MVT::i64));
2782
2783 // Expand
2784 // (set dst, (i64 (load baseptr)))
2785 // to
2786 // (set tmp, (ldl (add baseptr, 7), undef))
2787 // (set dst, (ldr baseptr, tmp))
2788 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
2789 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
2790 IsLittle ? 7 : 0);
2791 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
2792 IsLittle ? 0 : 7);
2793 }
2794
2795 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
2796 IsLittle ? 3 : 0);
2797 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
2798 IsLittle ? 0 : 3);
2799
2800 // Expand
2801 // (set dst, (i32 (load baseptr))) or
2802 // (set dst, (i64 (sextload baseptr))) or
2803 // (set dst, (i64 (extload baseptr)))
2804 // to
2805 // (set tmp, (lwl (add baseptr, 3), undef))
2806 // (set dst, (lwr baseptr, tmp))
2807 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2808 (ExtType == ISD::EXTLOAD))
2809 return LWR;
2810
2811 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2812
2813 // Expand
2814 // (set dst, (i64 (zextload baseptr)))
2815 // to
2816 // (set tmp0, (lwl (add baseptr, 3), undef))
2817 // (set tmp1, (lwr baseptr, tmp0))
2818 // (set tmp2, (shl tmp1, 32))
2819 // (set dst, (srl tmp2, 32))
2820 SDLoc DL(LD);
2821 SDValue Const32 = DAG.getConstant(32, DL, MVT::i32);
2822 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
2823 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2824 SDValue Ops[] = { SRL, LWR.getValue(1) };
2825 return DAG.getMergeValues(Ops, DL);
2826}
2827
2829 SDValue Chain, unsigned Offset) {
2830 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2831 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
2832 SDLoc DL(SD);
2833 SDVTList VTList = DAG.getVTList(MVT::Other);
2834
2835 if (Offset)
2836 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
2837 DAG.getConstant(Offset, DL, BasePtrVT));
2838
2839 SDValue Ops[] = { Chain, Value, Ptr };
2840 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
2841 SD->getMemOperand());
2842}
2843
2844// Expand an unaligned 32 or 64-bit integer store node.
2846 bool IsLittle) {
2847 SDValue Value = SD->getValue(), Chain = SD->getChain();
2848 EVT VT = Value.getValueType();
2849
2850 // Expand
2851 // (store val, baseptr) or
2852 // (truncstore val, baseptr)
2853 // to
2854 // (swl val, (add baseptr, 3))
2855 // (swr val, baseptr)
2856 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
2857 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
2858 IsLittle ? 3 : 0);
2859 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
2860 }
2861
2862 assert(VT == MVT::i64);
2863
2864 // Expand
2865 // (store val, baseptr)
2866 // to
2867 // (sdl val, (add baseptr, 7))
2868 // (sdr val, baseptr)
2869 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2870 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
2871}
2872
2873// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2875 bool SingleFloat) {
2876 SDValue Val = SD->getValue();
2877
2878 if (Val.getOpcode() != ISD::FP_TO_SINT ||
2879 (Val.getValueSizeInBits() > 32 && SingleFloat))
2880 return SDValue();
2881
2883 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
2884 Val.getOperand(0));
2885 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
2886 SD->getPointerInfo(), SD->getAlign(),
2887 SD->getMemOperand()->getFlags());
2888}
2889
2892 EVT MemVT = SD->getMemoryVT();
2893
2894 // Lower unaligned integer stores.
2895 if (!Subtarget.systemSupportsUnalignedAccess() &&
2896 (SD->getAlign().value() < (MemVT.getSizeInBits() / 8)) &&
2897 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2898 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
2899
2900 return lowerFP_TO_SINT_STORE(SD, DAG, Subtarget.isSingleFloat());
2901}
2902
2903SDValue MipsTargetLowering::lowerEH_DWARF_CFA(SDValue Op,
2904 SelectionDAG &DAG) const {
2905
2906 // Return a fixed StackObject with offset 0 which points to the old stack
2907 // pointer.
2909 EVT ValTy = Op->getValueType(0);
2910 int FI = MFI.CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2911 return DAG.getFrameIndex(FI, ValTy);
2912}
2913
2914SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2915 SelectionDAG &DAG) const {
2916 if (Op.getValueSizeInBits() > 32 && Subtarget.isSingleFloat())
2917 return SDValue();
2918
2919 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
2920 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
2921 Op.getOperand(0));
2922 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
2923}
2924
2925SDValue MipsTargetLowering::lowerSTRICT_FP_TO_INT(SDValue Op,
2926 SelectionDAG &DAG) const {
2927 assert(Op->isStrictFPOpcode());
2928 SDValue SrcVal = Op.getOperand(1);
2929 SDLoc Loc(Op);
2930
2931 SDValue Result =
2934 Loc, Op.getValueType(), SrcVal);
2935
2936 return DAG.getMergeValues({Result, Op.getOperand(0)}, Loc);
2937}
2938
2940 static const MCPhysReg RCRegs[] = {Mips::FCR31};
2941 return RCRegs;
2942}
2943
2944//===----------------------------------------------------------------------===//
2945// Calling Convention Implementation
2946//===----------------------------------------------------------------------===//
2947
2948//===----------------------------------------------------------------------===//
2949// TODO: Implement a generic logic using tblgen that can support this.
2950// Mips O32 ABI rules:
2951// ---
2952// i32 - Passed in A0, A1, A2, A3 and stack
2953// f32 - Only passed in f32 registers if no int reg has been used yet to hold
2954// an argument. Otherwise, passed in A1, A2, A3 and stack.
2955// f64 - Only passed in two aliased f32 registers if no int reg has been used
2956// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
2957// not used, it must be shadowed. If only A3 is available, shadow it and
2958// go to stack.
2959// vXiX - Received as scalarized i32s, passed in A0 - A3 and the stack.
2960// vXf32 - Passed in either a pair of registers {A0, A1}, {A2, A3} or {A0 - A3}
2961// with the remainder spilled to the stack.
2962// vXf64 - Passed in either {A0, A1, A2, A3} or {A2, A3} and in both cases
2963// spilling the remainder to the stack.
2964//
2965// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
2966//===----------------------------------------------------------------------===//
2967
2968static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2969 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2970 Type *OrigTy, CCState &State,
2971 ArrayRef<MCPhysReg> F64Regs) {
2972 const MipsSubtarget &Subtarget = static_cast<const MipsSubtarget &>(
2973 State.getMachineFunction().getSubtarget());
2974
2975 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2976
2977 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
2978
2979 static const MCPhysReg FloatVectorIntRegs[] = { Mips::A0, Mips::A2 };
2980
2981 // Do not process byval args here.
2982 if (ArgFlags.isByVal())
2983 return true;
2984
2985 // Promote i8 and i16
2986 if (ArgFlags.isInReg() && !Subtarget.isLittle()) {
2987 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) {
2988 LocVT = MVT::i32;
2989 if (ArgFlags.isSExt())
2990 LocInfo = CCValAssign::SExtUpper;
2991 else if (ArgFlags.isZExt())
2992 LocInfo = CCValAssign::ZExtUpper;
2993 else
2994 LocInfo = CCValAssign::AExtUpper;
2995 }
2996 }
2997
2998 // Promote i8 and i16
2999 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
3000 LocVT = MVT::i32;
3001 if (ArgFlags.isSExt())
3002 LocInfo = CCValAssign::SExt;
3003 else if (ArgFlags.isZExt())
3004 LocInfo = CCValAssign::ZExt;
3005 else
3006 LocInfo = CCValAssign::AExt;
3007 }
3008
3009 unsigned Reg;
3010
3011 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
3012 // is true: function is vararg, argument is 3rd or higher, there is previous
3013 // argument which is not f32 or f64.
3014 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1 ||
3015 State.getFirstUnallocated(F32Regs) != ValNo;
3016 Align OrigAlign = ArgFlags.getNonZeroOrigAlign();
3017 bool isI64 = (ValVT == MVT::i32 && OrigAlign == Align(8));
3018 bool isVectorFloat = OrigTy->isVectorTy() && OrigTy->isFPOrFPVectorTy();
3019
3020 // The MIPS vector ABI for floats passes them in a pair of registers
3021 if (ValVT == MVT::i32 && isVectorFloat) {
3022 // This is the start of an vector that was scalarized into an unknown number
3023 // of components. It doesn't matter how many there are. Allocate one of the
3024 // notional 8 byte aligned registers which map onto the argument stack, and
3025 // shadow the register lost to alignment requirements.
3026 if (ArgFlags.isSplit()) {
3027 Reg = State.AllocateReg(FloatVectorIntRegs);
3028 if (Reg == Mips::A2)
3029 State.AllocateReg(Mips::A1);
3030 else if (Reg == 0)
3031 State.AllocateReg(Mips::A3);
3032 } else {
3033 // If we're an intermediate component of the split, we can just attempt to
3034 // allocate a register directly.
3035 Reg = State.AllocateReg(IntRegs);
3036 }
3037 } else if (ValVT == MVT::i32 ||
3038 (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
3039 Reg = State.AllocateReg(IntRegs);
3040 // If this is the first part of an i64 arg,
3041 // the allocated register must be either A0 or A2.
3042 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
3043 Reg = State.AllocateReg(IntRegs);
3044 LocVT = MVT::i32;
3045 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
3046 // Allocate int register and shadow next int register. If first
3047 // available register is Mips::A1 or Mips::A3, shadow it too.
3048 Reg = State.AllocateReg(IntRegs);
3049 if (Reg == Mips::A1 || Reg == Mips::A3)
3050 Reg = State.AllocateReg(IntRegs);
3051
3052 if (Reg) {
3053 LocVT = MVT::i32;
3054
3055 State.addLoc(
3056 CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
3057 MCRegister HiReg = State.AllocateReg(IntRegs);
3058 assert(HiReg);
3059 State.addLoc(
3060 CCValAssign::getCustomReg(ValNo, ValVT, HiReg, LocVT, LocInfo));
3061 return false;
3062 }
3063 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
3064 // we are guaranteed to find an available float register
3065 if (ValVT == MVT::f32) {
3066 Reg = State.AllocateReg(F32Regs);
3067 // Shadow int register
3068 State.AllocateReg(IntRegs);
3069 } else {
3070 Reg = State.AllocateReg(F64Regs);
3071 // Shadow int registers
3072 MCRegister Reg2 = State.AllocateReg(IntRegs);
3073 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
3074 State.AllocateReg(IntRegs);
3075 State.AllocateReg(IntRegs);
3076 }
3077 } else
3078 llvm_unreachable("Cannot handle this ValVT.");
3079
3080 if (!Reg) {
3081 unsigned Offset = State.AllocateStack(ValVT.getStoreSize(), OrigAlign);
3082 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
3083 } else
3084 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
3085
3086 return false;
3087}
3088
3089static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, MVT LocVT,
3090 CCValAssign::LocInfo LocInfo,
3091 ISD::ArgFlagsTy ArgFlags, Type *OrigTy,
3092 CCState &State) {
3093 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
3094
3095 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, OrigTy, State,
3096 F64Regs);
3097}
3098
3099static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, MVT LocVT,
3100 CCValAssign::LocInfo LocInfo,
3101 ISD::ArgFlagsTy ArgFlags, Type *OrigTy,
3102 CCState &State) {
3103 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
3104
3105 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, OrigTy, State,
3106 F64Regs);
3107}
3108
3109[[maybe_unused]] static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
3110 CCValAssign::LocInfo LocInfo,
3111 ISD::ArgFlagsTy ArgFlags, Type *OrigTy,
3112 CCState &State);
3113
3114#include "MipsGenCallingConv.inc"
3115
3117 return CC_Mips_FixedArg;
3118 }
3119
3121 return RetCC_Mips;
3122 }
3123//===----------------------------------------------------------------------===//
3124// Call Calling Convention Implementation
3125//===----------------------------------------------------------------------===//
3126
3127SDValue MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
3128 SDValue Chain, SDValue Arg,
3129 const SDLoc &DL, bool IsTailCall,
3130 SelectionDAG &DAG) const {
3131 if (!IsTailCall) {
3132 SDValue PtrOff =
3133 DAG.getNode(ISD::ADD, DL, getPointerTy(DAG.getDataLayout()), StackPtr,
3135 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo());
3136 }
3137
3139 int FI = MFI.CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
3140 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
3141 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(), MaybeAlign(),
3143}
3144
3147 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
3148 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
3149 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
3150 SDValue Chain) const {
3151 // Insert node "GP copy globalreg" before call to function.
3152 //
3153 // R_MIPS_CALL* operators (emitted when non-internal functions are called
3154 // in PIC mode) allow symbols to be resolved via lazy binding.
3155 // The lazy binding stub requires GP to point to the GOT.
3156 // Note that we don't need GP to point to the GOT for indirect calls
3157 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
3158 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
3159 // used for the function (that is, Mips linker doesn't generate lazy binding
3160 // stub for a function whose address is taken in the program).
3161 if (IsPICCall && !InternalLinkage && IsCallReloc) {
3162 unsigned GPReg = ABI.IsN64() ? Mips::GP_64 : Mips::GP;
3163 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
3164 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
3165 }
3166
3167 // Build a sequence of copy-to-reg nodes chained together with token
3168 // chain and flag operands which copy the outgoing args into registers.
3169 // The InGlue in necessary since all emitted instructions must be
3170 // stuck together.
3171 SDValue InGlue;
3172
3173 for (auto &R : RegsToPass) {
3174 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, R.first, R.second, InGlue);
3175 InGlue = Chain.getValue(1);
3176 }
3177
3178 // Add argument registers to the end of the list so that they are
3179 // known live into the call.
3180 for (auto &R : RegsToPass)
3181 Ops.push_back(CLI.DAG.getRegister(R.first, R.second.getValueType()));
3182
3183 // Add a register mask operand representing the call-preserved registers.
3184 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
3185 const uint32_t *Mask =
3186 TRI->getCallPreservedMask(CLI.DAG.getMachineFunction(), CLI.CallConv);
3187 assert(Mask && "Missing call preserved mask for calling convention");
3188 if (Subtarget.inMips16HardFloat()) {
3190 StringRef Sym = G->getGlobal()->getName();
3191 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
3192 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
3194 }
3195 }
3196 }
3197 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
3198
3199 if (InGlue.getNode())
3200 Ops.push_back(InGlue);
3201}
3202
3204 SDNode *Node) const {
3205 switch (MI.getOpcode()) {
3206 default:
3207 return;
3208 case Mips::JALR:
3209 case Mips::JALRPseudo:
3210 case Mips::JALR64:
3211 case Mips::JALR64Pseudo:
3212 case Mips::JALR16_MM:
3213 case Mips::JALRC16_MMR6:
3214 case Mips::TAILCALLREG:
3215 case Mips::TAILCALLREG64:
3216 case Mips::TAILCALLR6REG:
3217 case Mips::TAILCALL64R6REG:
3218 case Mips::TAILCALLREG_MM:
3219 case Mips::TAILCALLREG_MMR6: {
3220 if (!EmitJalrReloc ||
3221 Subtarget.inMips16Mode() ||
3223 Node->getNumOperands() < 1 ||
3224 Node->getOperand(0).getNumOperands() < 2) {
3225 return;
3226 }
3227 // We are after the callee address, set by LowerCall().
3228 // If added to MI, asm printer will emit .reloc R_MIPS_JALR for the
3229 // symbol.
3230 const SDValue TargetAddr = Node->getOperand(0).getOperand(1);
3231 StringRef Sym;
3232 if (const GlobalAddressSDNode *G =
3234 // We must not emit the R_MIPS_JALR relocation against data symbols
3235 // since this will cause run-time crashes if the linker replaces the
3236 // call instruction with a relative branch to the data symbol.
3237 if (!isa<Function>(G->getGlobal())) {
3238 LLVM_DEBUG(dbgs() << "Not adding R_MIPS_JALR against data symbol "
3239 << G->getGlobal()->getName() << "\n");
3240 return;
3241 }
3242 Sym = G->getGlobal()->getName();
3243 }
3244 else if (const ExternalSymbolSDNode *ES =
3246 Sym = ES->getSymbol();
3247 }
3248
3249 if (Sym.empty())
3250 return;
3251
3252 MachineFunction *MF = MI.getParent()->getParent();
3253 MCSymbol *S = MF->getContext().getOrCreateSymbol(Sym);
3254 LLVM_DEBUG(dbgs() << "Adding R_MIPS_JALR against " << Sym << "\n");
3256 }
3257 }
3258}
3259
3260/// LowerCall - functions arguments are copied from virtual regs to
3261/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
3262SDValue
3263MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
3264 SmallVectorImpl<SDValue> &InVals) const {
3265 SelectionDAG &DAG = CLI.DAG;
3266 SDLoc DL = CLI.DL;
3268 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
3270 SDValue Chain = CLI.Chain;
3271 SDValue Callee = CLI.Callee;
3272 bool &IsTailCall = CLI.IsTailCall;
3273 CallingConv::ID CallConv = CLI.CallConv;
3274 bool IsVarArg = CLI.IsVarArg;
3275 const CallBase *CB = CLI.CB;
3276
3278 MachineFrameInfo &MFI = MF.getFrameInfo();
3280 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
3281 bool IsPIC = isPositionIndependent();
3282
3283 // Analyze operands of the call, assigning locations to each operand.
3285 MipsCCState CCInfo(
3286 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(),
3288
3289 const ExternalSymbolSDNode *ES =
3291
3292 // There is one case where CALLSEQ_START..CALLSEQ_END can be nested, which
3293 // is during the lowering of a call with a byval argument which produces
3294 // a call to memcpy. For the O32 case, this causes the caller to allocate
3295 // stack space for the reserved argument area for the callee, then recursively
3296 // again for the memcpy call. In the NEWABI case, this doesn't occur as those
3297 // ABIs mandate that the callee allocates the reserved argument area. We do
3298 // still produce nested CALLSEQ_START..CALLSEQ_END with zero space though.
3299 //
3300 // If the callee has a byval argument and memcpy is used, we are mandated
3301 // to already have produced a reserved argument area for the callee for O32.
3302 // Therefore, the reserved argument area can be reused for both calls.
3303 //
3304 // Other cases of calling memcpy cannot have a chain with a CALLSEQ_START
3305 // present, as we have yet to hook that node onto the chain.
3306 //
3307 // Hence, the CALLSEQ_START and CALLSEQ_END nodes can be eliminated in this
3308 // case. GCC does a similar trick, in that wherever possible, it calculates
3309 // the maximum out going argument area (including the reserved area), and
3310 // preallocates the stack space on entrance to the caller.
3311 //
3312 // FIXME: We should do the same for efficiency and space.
3313
3314 // Note: The check on the calling convention below must match
3315 // MipsABIInfo::GetCalleeAllocdArgSizeInBytes().
3316 bool MemcpyInByVal = ES && StringRef(ES->getSymbol()) == "memcpy" &&
3317 CallConv != CallingConv::Fast &&
3318 Chain.getOpcode() == ISD::CALLSEQ_START;
3319
3320 // Allocate the reserved argument area. It seems strange to do this from the
3321 // caller side but removing it breaks the frame size calculation.
3322 unsigned ReservedArgArea =
3323 MemcpyInByVal ? 0 : ABI.GetCalleeAllocdArgSizeInBytes(CallConv);
3324 CCInfo.AllocateStack(ReservedArgArea, Align(1));
3325
3326 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
3327
3328 // Get a count of how many bytes are to be pushed on the stack.
3329 unsigned StackSize = CCInfo.getStackSize();
3330
3331 // Call site info for function parameters tracking and call base type info.
3333 // Set type id for call site info.
3334 if (MF.getTarget().Options.EmitCallGraphSection && CB && CB->isIndirectCall())
3335 CSInfo = MachineFunction::CallSiteInfo(*CB);
3336
3337 // Check if it's really possible to do a tail call. Restrict it to functions
3338 // that are part of this compilation unit.
3339 bool InternalLinkage = false;
3340 if (IsTailCall) {
3341 IsTailCall = isEligibleForTailCallOptimization(
3342 CCInfo, StackSize, *MF.getInfo<MipsFunctionInfo>());
3344 InternalLinkage = G->getGlobal()->hasInternalLinkage();
3345 IsTailCall &= (InternalLinkage || G->getGlobal()->hasLocalLinkage() ||
3346 G->getGlobal()->hasPrivateLinkage() ||
3347 G->getGlobal()->hasHiddenVisibility() ||
3348 G->getGlobal()->hasProtectedVisibility());
3349 }
3350 }
3351 if (!IsTailCall && CLI.CB && CLI.CB->isMustTailCall())
3352 report_fatal_error("failed to perform tail call elimination on a call "
3353 "site marked musttail");
3354
3355 if (IsTailCall)
3356 ++NumTailCalls;
3357
3358 // Chain is the output chain of the last Load/Store or CopyToReg node.
3359 // ByValChain is the output chain of the last Memcpy node created for copying
3360 // byval arguments to the stack.
3361 unsigned StackAlignment = TFL->getStackAlignment();
3362 StackSize = alignTo(StackSize, StackAlignment);
3363
3364 if (!(IsTailCall || MemcpyInByVal))
3365 Chain = DAG.getCALLSEQ_START(Chain, StackSize, 0, DL);
3366
3368 DAG.getCopyFromReg(Chain, DL, ABI.IsN64() ? Mips::SP_64 : Mips::SP,
3370 std::deque<std::pair<unsigned, SDValue>> RegsToPass;
3371 SmallVector<SDValue, 8> MemOpChains;
3372
3373 CCInfo.rewindByValRegsInfo();
3374
3375 // Walk the register/memloc assignments, inserting copies/loads.
3376 for (unsigned i = 0, e = ArgLocs.size(), OutIdx = 0; i != e; ++i, ++OutIdx) {
3377 SDValue Arg = OutVals[OutIdx];
3378 CCValAssign &VA = ArgLocs[i];
3379 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
3380 ISD::ArgFlagsTy Flags = Outs[OutIdx].Flags;
3381 bool UseUpperBits = false;
3382
3383 // ByVal Arg.
3384 if (Flags.isByVal()) {
3385 unsigned FirstByValReg, LastByValReg;
3386 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
3387 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
3388
3389 assert(Flags.getByValSize() &&
3390 "ByVal args of size 0 should have been ignored by front-end.");
3391 assert(ByValIdx < CCInfo.getInRegsParamsCount());
3392 assert(!IsTailCall &&
3393 "Do not tail-call optimize if there is a byval argument.");
3394 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
3395 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(),
3396 VA);
3397 CCInfo.nextInRegsParam();
3398 continue;
3399 }
3400
3401 // Promote the value if needed.
3402 switch (VA.getLocInfo()) {
3403 default:
3404 llvm_unreachable("Unknown loc info!");
3405 case CCValAssign::Full:
3406 if (VA.isRegLoc()) {
3407 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
3408 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
3409 (ValVT == MVT::i64 && LocVT == MVT::f64))
3410 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
3411 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
3412 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
3413 Arg, DAG.getConstant(0, DL, MVT::i32));
3414 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
3415 Arg, DAG.getConstant(1, DL, MVT::i32));
3416 if (!Subtarget.isLittle())
3417 std::swap(Lo, Hi);
3418
3419 assert(VA.needsCustom());
3420
3421 Register LocRegLo = VA.getLocReg();
3422 Register LocRegHigh = ArgLocs[++i].getLocReg();
3423 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
3424 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
3425 continue;
3426 }
3427 }
3428 break;
3429 case CCValAssign::BCvt:
3430 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
3431 break;
3433 UseUpperBits = true;
3434 [[fallthrough]];
3435 case CCValAssign::SExt:
3436 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
3437 break;
3439 UseUpperBits = true;
3440 [[fallthrough]];
3441 case CCValAssign::ZExt:
3442 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
3443 break;
3445 UseUpperBits = true;
3446 [[fallthrough]];
3447 case CCValAssign::AExt:
3448 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
3449 break;
3450 }
3451
3452 if (UseUpperBits) {
3453 unsigned ValSizeInBits = Outs[OutIdx].ArgVT.getSizeInBits();
3454 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3455 Arg = DAG.getNode(
3456 ISD::SHL, DL, VA.getLocVT(), Arg,
3457 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
3458 }
3459
3460 // Arguments that can be passed on register must be kept at
3461 // RegsToPass vector
3462 if (VA.isRegLoc()) {
3463 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3464
3465 // If the parameter is passed through reg $D, which splits into
3466 // two physical registers, avoid creating call site info.
3467 if (Mips::AFGR64RegClass.contains(VA.getLocReg()))
3468 continue;
3469
3470 // Collect CSInfo about which register passes which parameter.
3471 const TargetOptions &Options = DAG.getTarget().Options;
3472 if (Options.EmitCallSiteInfo)
3473 CSInfo.ArgRegPairs.emplace_back(VA.getLocReg(), i);
3474
3475 continue;
3476 }
3477
3478 // Register can't get to this point...
3479 assert(VA.isMemLoc());
3480
3481 // emit ISD::STORE whichs stores the
3482 // parameter value to a stack Location
3483 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
3484 Chain, Arg, DL, IsTailCall, DAG));
3485 }
3486
3487 // Transform all store nodes into one single node because all store
3488 // nodes are independent of each other.
3489 if (!MemOpChains.empty())
3490 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
3491
3492 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
3493 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
3494 // node so that legalize doesn't hack it.
3495
3496 EVT Ty = Callee.getValueType();
3497 bool GlobalOrExternal = false, IsCallReloc = false;
3498
3499 // The long-calls feature is ignored in case of PIC.
3500 // While we do not support -mshared / -mno-shared properly,
3501 // ignore long-calls in case of -mabicalls too.
3502 if (!Subtarget.isABICalls() && !IsPIC) {
3503 // If the function should be called using "long call",
3504 // get its address into a register to prevent using
3505 // of the `jal` instruction for the direct call.
3506 if (auto *N = dyn_cast<ExternalSymbolSDNode>(Callee)) {
3507 if (Subtarget.useLongCalls())
3508 Callee = Subtarget.hasSym32()
3509 ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
3510 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
3511 } else if (auto *N = dyn_cast<GlobalAddressSDNode>(Callee)) {
3512 bool UseLongCalls = Subtarget.useLongCalls();
3513 // If the function has long-call/far/near attribute
3514 // it overrides command line switch pased to the backend.
3515 if (auto *F = dyn_cast<Function>(N->getGlobal())) {
3516 if (F->hasFnAttribute("long-call"))
3517 UseLongCalls = true;
3518 else if (F->hasFnAttribute("short-call"))
3519 UseLongCalls = false;
3520 }
3521 if (UseLongCalls)
3522 Callee = Subtarget.hasSym32()
3523 ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
3524 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
3525 }
3526 }
3527
3528 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3529 if (Subtarget.isTargetCOFF() &&
3530 G->getGlobal()->hasDLLImportStorageClass()) {
3531 assert(Subtarget.isTargetWindows() &&
3532 "Windows is the only supported COFF target");
3533 auto PtrInfo = MachinePointerInfo();
3534 Callee = DAG.getLoad(Ty, DL, Chain,
3535 getDllimportSymbol(G, SDLoc(G), Ty, DAG), PtrInfo);
3536 } else if (IsPIC) {
3537 const GlobalValue *Val = G->getGlobal();
3538 InternalLinkage = Val->hasInternalLinkage();
3539
3540 if (InternalLinkage)
3541 Callee = getAddrLocal(G, DL, Ty, DAG, ABI.IsN32() || ABI.IsN64());
3542 else if (Subtarget.useXGOT()) {
3544 MipsII::MO_CALL_LO16, Chain,
3545 FuncInfo->callPtrInfo(MF, Val));
3546 IsCallReloc = true;
3547 } else {
3548 Callee = getAddrGlobal(G, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
3549 FuncInfo->callPtrInfo(MF, Val));
3550 IsCallReloc = true;
3551 }
3552 } else
3553 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL,
3554 getPointerTy(DAG.getDataLayout()), 0,
3556 GlobalOrExternal = true;
3557 }
3558 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
3559 const char *Sym = S->getSymbol();
3560
3561 if (!IsPIC) // static
3564 else if (Subtarget.useXGOT()) {
3566 MipsII::MO_CALL_LO16, Chain,
3567 FuncInfo->callPtrInfo(MF, Sym));
3568 IsCallReloc = true;
3569 } else { // PIC
3570 Callee = getAddrGlobal(S, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
3571 FuncInfo->callPtrInfo(MF, Sym));
3572 IsCallReloc = true;
3573 }
3574
3575 GlobalOrExternal = true;
3576 }
3577
3578 SmallVector<SDValue, 8> Ops(1, Chain);
3579 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
3580
3581 getOpndList(Ops, RegsToPass, IsPIC, GlobalOrExternal, InternalLinkage,
3582 IsCallReloc, CLI, Callee, Chain);
3583
3584 if (IsTailCall) {
3586 SDValue Ret = DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
3587 DAG.addCallSiteInfo(Ret.getNode(), std::move(CSInfo));
3588 return Ret;
3589 }
3590
3591 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
3592 SDValue InGlue = Chain.getValue(1);
3593
3594 DAG.addCallSiteInfo(Chain.getNode(), std::move(CSInfo));
3595
3596 // Create the CALLSEQ_END node in the case of where it is not a call to
3597 // memcpy.
3598 if (!(MemcpyInByVal)) {
3599 Chain = DAG.getCALLSEQ_END(Chain, StackSize, 0, InGlue, DL);
3600 InGlue = Chain.getValue(1);
3601 }
3602
3603 // Handle result values, copying them out of physregs into vregs that we
3604 // return.
3605 return LowerCallResult(Chain, InGlue, CallConv, IsVarArg, Ins, DL, DAG,
3606 InVals, CLI);
3607}
3608
3609/// LowerCallResult - Lower the result values of a call into the
3610/// appropriate copies out of appropriate physical registers.
3611SDValue MipsTargetLowering::LowerCallResult(
3612 SDValue Chain, SDValue InGlue, CallingConv::ID CallConv, bool IsVarArg,
3613 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
3616 // Assign locations to each value returned by this call.
3618 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
3619 *DAG.getContext());
3620
3621 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
3622
3623 // Copy all of the result registers out of their specified physreg.
3624 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3625 CCValAssign &VA = RVLocs[i];
3626 assert(VA.isRegLoc() && "Can only return in registers!");
3627
3628 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
3629 RVLocs[i].getLocVT(), InGlue);
3630 Chain = Val.getValue(1);
3631 InGlue = Val.getValue(2);
3632
3633 if (VA.isUpperBitsInLoc()) {
3634 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
3635 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3636 unsigned Shift =
3638 Val = DAG.getNode(
3639 Shift, DL, VA.getLocVT(), Val,
3640 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
3641 }
3642
3643 switch (VA.getLocInfo()) {
3644 default:
3645 llvm_unreachable("Unknown loc info!");
3646 case CCValAssign::Full:
3647 break;
3648 case CCValAssign::BCvt:
3649 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
3650 break;
3651 case CCValAssign::AExt:
3653 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3654 break;
3655 case CCValAssign::ZExt:
3657 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
3658 DAG.getValueType(VA.getValVT()));
3659 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3660 break;
3661 case CCValAssign::SExt:
3663 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
3664 DAG.getValueType(VA.getValVT()));
3665 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3666 break;
3667 }
3668
3669 InVals.push_back(Val);
3670 }
3671
3672 return Chain;
3673}
3674
3676 EVT ArgVT, const SDLoc &DL,
3677 SelectionDAG &DAG) {
3678 MVT LocVT = VA.getLocVT();
3679 EVT ValVT = VA.getValVT();
3680
3681 // Shift into the upper bits if necessary.
3682 switch (VA.getLocInfo()) {
3683 default:
3684 break;
3688 unsigned ValSizeInBits = ArgVT.getSizeInBits();
3689 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3690 unsigned Opcode =
3692 Val = DAG.getNode(
3693 Opcode, DL, VA.getLocVT(), Val,
3694 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
3695 break;
3696 }
3697 }
3698
3699 // If this is an value smaller than the argument slot size (32-bit for O32,
3700 // 64-bit for N32/N64), it has been promoted in some way to the argument slot
3701 // size. Extract the value and insert any appropriate assertions regarding
3702 // sign/zero extension.
3703 switch (VA.getLocInfo()) {
3704 default:
3705 llvm_unreachable("Unknown loc info!");
3706 case CCValAssign::Full:
3707 break;
3709 case CCValAssign::AExt:
3710 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
3711 break;
3713 case CCValAssign::SExt:
3714 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT));
3715 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
3716 break;
3718 case CCValAssign::ZExt:
3719 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT));
3720 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
3721 break;
3722 case CCValAssign::BCvt:
3723 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
3724 break;
3725 }
3726
3727 return Val;
3728}
3729
3730//===----------------------------------------------------------------------===//
3731// Formal Arguments Calling Convention Implementation
3732//===----------------------------------------------------------------------===//
3733/// LowerFormalArguments - transform physical registers into virtual registers
3734/// and generate load operations for arguments places on the stack.
3735SDValue MipsTargetLowering::LowerFormalArguments(
3736 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
3737 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
3738 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
3739 MachineFunction &MF = DAG.getMachineFunction();
3740 MachineFrameInfo &MFI = MF.getFrameInfo();
3741 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3742
3743 MipsFI->setVarArgsFrameIndex(0);
3744
3745 // Used with vargs to acumulate store chains.
3746 std::vector<SDValue> OutChains;
3747
3748 // Assign locations to all of the incoming arguments.
3750 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
3751 *DAG.getContext());
3752 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), Align(1));
3754 Function::const_arg_iterator FuncArg = Func.arg_begin();
3755
3756 if (Func.hasFnAttribute("interrupt") && !Func.arg_empty())
3758 "Functions with the interrupt attribute cannot have arguments!");
3759
3760 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg);
3761 MipsFI->setFormalArgInfo(CCInfo.getStackSize(),
3762 CCInfo.getInRegsParamsCount() > 0);
3763
3764 unsigned CurArgIdx = 0;
3765 CCInfo.rewindByValRegsInfo();
3766
3767 for (unsigned i = 0, e = ArgLocs.size(), InsIdx = 0; i != e; ++i, ++InsIdx) {
3768 CCValAssign &VA = ArgLocs[i];
3769 if (Ins[InsIdx].isOrigArg()) {
3770 std::advance(FuncArg, Ins[InsIdx].getOrigArgIndex() - CurArgIdx);
3771 CurArgIdx = Ins[InsIdx].getOrigArgIndex();
3772 }
3773 EVT ValVT = VA.getValVT();
3774 ISD::ArgFlagsTy Flags = Ins[InsIdx].Flags;
3775 bool IsRegLoc = VA.isRegLoc();
3776
3777 if (Flags.isByVal()) {
3778 assert(Ins[InsIdx].isOrigArg() && "Byval arguments cannot be implicit");
3779 unsigned FirstByValReg, LastByValReg;
3780 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
3781 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
3782
3783 assert(Flags.getByValSize() &&
3784 "ByVal args of size 0 should have been ignored by front-end.");
3785 assert(ByValIdx < CCInfo.getInRegsParamsCount());
3786 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
3787 FirstByValReg, LastByValReg, VA, CCInfo);
3788 CCInfo.nextInRegsParam();
3789 continue;
3790 }
3791
3792 // Arguments stored on registers
3793 if (IsRegLoc) {
3794 MVT RegVT = VA.getLocVT();
3795 Register ArgReg = VA.getLocReg();
3796 const TargetRegisterClass *RC = getRegClassFor(RegVT);
3797
3798 // Transform the arguments stored on
3799 // physical registers into virtual ones
3800 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
3801 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
3802
3803 ArgValue =
3804 UnpackFromArgumentSlot(ArgValue, VA, Ins[InsIdx].ArgVT, DL, DAG);
3805
3806 // Handle floating point arguments passed in integer registers and
3807 // long double arguments passed in floating point registers.
3808 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
3809 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
3810 (RegVT == MVT::f64 && ValVT == MVT::i64))
3811 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
3812 else if (ABI.IsO32() && RegVT == MVT::i32 &&
3813 ValVT == MVT::f64) {
3814 assert(VA.needsCustom() && "Expected custom argument for f64 split");
3815 CCValAssign &NextVA = ArgLocs[++i];
3816 unsigned Reg2 =
3817 addLiveIn(DAG.getMachineFunction(), NextVA.getLocReg(), RC);
3818 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
3819 if (!Subtarget.isLittle())
3820 std::swap(ArgValue, ArgValue2);
3821 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
3822 ArgValue, ArgValue2);
3823 }
3824
3825 InVals.push_back(ArgValue);
3826 } else { // VA.isRegLoc()
3827 MVT LocVT = VA.getLocVT();
3828
3829 assert(!VA.needsCustom() && "unexpected custom memory argument");
3830
3831 // Only arguments pased on the stack should make it here.
3832 assert(VA.isMemLoc());
3833
3834 // The stack pointer offset is relative to the caller stack frame.
3835 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8,
3836 VA.getLocMemOffset(), true);
3837
3838 // Create load nodes to retrieve arguments from the stack
3839 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
3840 SDValue ArgValue = DAG.getLoad(
3841 LocVT, DL, Chain, FIN,
3843 OutChains.push_back(ArgValue.getValue(1));
3844
3845 ArgValue =
3846 UnpackFromArgumentSlot(ArgValue, VA, Ins[InsIdx].ArgVT, DL, DAG);
3847
3848 InVals.push_back(ArgValue);
3849 }
3850 }
3851
3852 for (unsigned i = 0, e = ArgLocs.size(), InsIdx = 0; i != e; ++i, ++InsIdx) {
3853
3854 if (ArgLocs[i].needsCustom()) {
3855 ++i;
3856 continue;
3857 }
3858
3859 // The mips ABIs for returning structs by value requires that we copy
3860 // the sret argument into $v0 for the return. Save the argument into
3861 // a virtual register so that we can access it from the return points.
3862 if (Ins[InsIdx].Flags.isSRet()) {
3863 unsigned Reg = MipsFI->getSRetReturnReg();
3864 if (!Reg) {
3866 getRegClassFor(ABI.IsN64() ? MVT::i64 : MVT::i32));
3867 MipsFI->setSRetReturnReg(Reg);
3868 }
3869 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
3870 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
3871 break;
3872 }
3873 }
3874
3875 if (IsVarArg)
3876 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo);
3877
3878 // All stores are grouped in one node to allow the matching between
3879 // the size of Ins and InVals. This only happens when on varg functions
3880 if (!OutChains.empty()) {
3881 OutChains.push_back(Chain);
3882 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
3883 }
3884
3885 return Chain;
3886}
3887
3888//===----------------------------------------------------------------------===//
3889// Return Value Calling Convention Implementation
3890//===----------------------------------------------------------------------===//
3891
3892bool
3893MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
3894 MachineFunction &MF, bool IsVarArg,
3896 LLVMContext &Context, const Type *RetTy) const {
3898 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
3899 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3900}
3901
3902bool MipsTargetLowering::shouldSignExtendTypeInLibCall(Type *Ty,
3903 bool IsSigned) const {
3904 if ((ABI.IsN32() || ABI.IsN64()) && Ty->isIntegerTy(32))
3905 return true;
3906
3907 return IsSigned;
3908}
3909
3910SDValue
3911MipsTargetLowering::LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
3912 const SDLoc &DL,
3913 SelectionDAG &DAG) const {
3914 MachineFunction &MF = DAG.getMachineFunction();
3915 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3916
3917 MipsFI->setISR();
3918
3919 return DAG.getNode(MipsISD::ERet, DL, MVT::Other, RetOps);
3920}
3921
3922SDValue
3923MipsTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
3924 bool IsVarArg,
3926 const SmallVectorImpl<SDValue> &OutVals,
3927 const SDLoc &DL, SelectionDAG &DAG) const {
3928 // CCValAssign - represent the assignment of
3929 // the return value to a location
3931 MachineFunction &MF = DAG.getMachineFunction();
3932
3933 // CCState - Info about the registers and stack slot.
3934 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
3935
3936 // Analyze return values.
3937 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
3938
3939 SDValue Glue;
3940 SmallVector<SDValue, 4> RetOps(1, Chain);
3941
3942 // Copy the result values into the output registers.
3943 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3944 SDValue Val = OutVals[i];
3945 CCValAssign &VA = RVLocs[i];
3946 assert(VA.isRegLoc() && "Can only return in registers!");
3947 bool UseUpperBits = false;
3948
3949 switch (VA.getLocInfo()) {
3950 default:
3951 llvm_unreachable("Unknown loc info!");
3952 case CCValAssign::Full:
3953 break;
3954 case CCValAssign::BCvt:
3955 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3956 break;
3958 UseUpperBits = true;
3959 [[fallthrough]];
3960 case CCValAssign::AExt:
3961 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3962 break;
3964 UseUpperBits = true;
3965 [[fallthrough]];
3966 case CCValAssign::ZExt:
3967 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3968 break;
3970 UseUpperBits = true;
3971 [[fallthrough]];
3972 case CCValAssign::SExt:
3973 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3974 break;
3975 }
3976
3977 if (UseUpperBits) {
3978 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3979 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3980 Val = DAG.getNode(
3981 ISD::SHL, DL, VA.getLocVT(), Val,
3982 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
3983 }
3984
3985 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Glue);
3986
3987 // Guarantee that all emitted copies are stuck together with flags.
3988 Glue = Chain.getValue(1);
3989 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
3990 }
3991
3992 // The mips ABIs for returning structs by value requires that we copy
3993 // the sret argument into $v0 for the return. We saved the argument into
3994 // a virtual register in the entry block, so now we copy the value out
3995 // and into $v0.
3996 if (MF.getFunction().hasStructRetAttr()) {
3997 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3998 unsigned Reg = MipsFI->getSRetReturnReg();
3999
4000 if (!Reg)
4001 llvm_unreachable("sret virtual register not created in the entry block");
4002 SDValue Val =
4003 DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(DAG.getDataLayout()));
4004 unsigned V0 = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
4005
4006 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Glue);
4007 Glue = Chain.getValue(1);
4008 RetOps.push_back(DAG.getRegister(V0, getPointerTy(DAG.getDataLayout())));
4009 }
4010
4011 RetOps[0] = Chain; // Update chain.
4012
4013 // Add the glue if we have it.
4014 if (Glue.getNode())
4015 RetOps.push_back(Glue);
4016
4017 // ISRs must use "eret".
4018 if (DAG.getMachineFunction().getFunction().hasFnAttribute("interrupt"))
4019 return LowerInterruptReturn(RetOps, DL, DAG);
4020
4021 // Standard return on Mips is a "jr $ra"
4022 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
4023}
4024
4025//===----------------------------------------------------------------------===//
4026// Mips Inline Assembly Support
4027//===----------------------------------------------------------------------===//
4028
4029/// getConstraintType - Given a constraint letter, return the type of
4030/// constraint it is for this target.
4032MipsTargetLowering::getConstraintType(StringRef Constraint) const {
4033 // Mips specific constraints
4034 // GCC config/mips/constraints.md
4035 //
4036 // 'd' : An address register. Equivalent to r
4037 // unless generating MIPS16 code.
4038 // 'y' : Equivalent to r; retained for
4039 // backwards compatibility.
4040 // 'c' : A register suitable for use in an indirect
4041 // jump. This will always be $25 for -mabicalls.
4042 // 'l' : The lo register. 1 word storage.
4043 // 'x' : The hilo register pair. Double word storage.
4044 if (Constraint.size() == 1) {
4045 switch (Constraint[0]) {
4046 default : break;
4047 case 'd':
4048 case 'y':
4049 case 'f':
4050 case 'c':
4051 case 'l':
4052 case 'x':
4053 return C_RegisterClass;
4054 case 'R':
4055 return C_Memory;
4056 }
4057 }
4058
4059 if (Constraint == "ZC")
4060 return C_Memory;
4061
4062 return TargetLowering::getConstraintType(Constraint);
4063}
4064
4065/// Examine constraint type and operand type and determine a weight value.
4066/// This object must already have been set up with the operand type
4067/// and the current alternative constraint selected.
4069MipsTargetLowering::getSingleConstraintMatchWeight(
4070 AsmOperandInfo &info, const char *constraint) const {
4072 Value *CallOperandVal = info.CallOperandVal;
4073 // If we don't have a value, we can't do a match,
4074 // but allow it at the lowest weight.
4075 if (!CallOperandVal)
4076 return CW_Default;
4077 Type *type = CallOperandVal->getType();
4078 // Look at the constraint type.
4079 switch (*constraint) {
4080 default:
4082 break;
4083 case 'd':
4084 case 'y':
4085 if (type->isIntegerTy())
4086 weight = CW_Register;
4087 break;
4088 case 'f': // FPU or MSA register
4089 if (Subtarget.hasMSA() && type->isVectorTy() &&
4090 type->getPrimitiveSizeInBits().getFixedValue() == 128)
4091 weight = CW_Register;
4092 else if (type->isFloatTy())
4093 weight = CW_Register;
4094 break;
4095 case 'c': // $25 for indirect jumps
4096 case 'l': // lo register
4097 case 'x': // hilo register pair
4098 if (type->isIntegerTy())
4099 weight = CW_SpecificReg;
4100 break;
4101 case 'I': // signed 16 bit immediate
4102 case 'J': // integer zero
4103 case 'K': // unsigned 16 bit immediate
4104 case 'L': // signed 32 bit immediate where lower 16 bits are 0
4105 case 'N': // immediate in the range of -65535 to -1 (inclusive)
4106 case 'O': // signed 15 bit immediate (+- 16383)
4107 case 'P': // immediate in the range of 65535 to 1 (inclusive)
4108 if (isa<ConstantInt>(CallOperandVal))
4109 weight = CW_Constant;
4110 break;
4111 case 'R':
4112 weight = CW_Memory;
4113 break;
4114 }
4115 return weight;
4116}
4117
4118/// This is a helper function to parse a physical register string and split it
4119/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
4120/// that is returned indicates whether parsing was successful. The second flag
4121/// is true if the numeric part exists.
4122static std::pair<bool, bool> parsePhysicalReg(StringRef C, StringRef &Prefix,
4123 unsigned long long &Reg) {
4124 if (C.front() != '{' || C.back() != '}')
4125 return std::make_pair(false, false);
4126
4127 // Search for the first numeric character.
4128 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
4129 I = std::find_if(B, E, isdigit);
4130
4131 Prefix = StringRef(B, I - B);
4132
4133 // The second flag is set to false if no numeric characters were found.
4134 if (I == E)
4135 return std::make_pair(true, false);
4136
4137 // Parse the numeric characters.
4138 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
4139 true);
4140}
4141
4143 ISD::NodeType) const {
4144 bool Cond = !Subtarget.isABI_O32() && VT.getSizeInBits() == 32;
4145 EVT MinVT = getRegisterType(Cond ? MVT::i64 : MVT::i32);
4146 return VT.bitsLT(MinVT) ? MinVT : VT;
4147}
4148
4149std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
4150parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
4151 const TargetRegisterInfo *TRI =
4153 const TargetRegisterClass *RC;
4154 StringRef Prefix;
4155 unsigned long long Reg;
4156
4157 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
4158
4159 if (!R.first)
4160 return std::make_pair(0U, nullptr);
4161
4162 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
4163 // No numeric characters follow "hi" or "lo".
4164 if (R.second)
4165 return std::make_pair(0U, nullptr);
4166
4167 RC = TRI->getRegClass(Prefix == "hi" ?
4168 Mips::HI32RegClassID : Mips::LO32RegClassID);
4169 return std::make_pair(*(RC->begin()), RC);
4170 } else if (Prefix.starts_with("$msa")) {
4171 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
4172
4173 // No numeric characters follow the name.
4174 if (R.second)
4175 return std::make_pair(0U, nullptr);
4176
4178 .Case("$msair", Mips::MSAIR)
4179 .Case("$msacsr", Mips::MSACSR)
4180 .Case("$msaaccess", Mips::MSAAccess)
4181 .Case("$msasave", Mips::MSASave)
4182 .Case("$msamodify", Mips::MSAModify)
4183 .Case("$msarequest", Mips::MSARequest)
4184 .Case("$msamap", Mips::MSAMap)
4185 .Case("$msaunmap", Mips::MSAUnmap)
4186 .Default(0);
4187
4188 if (!Reg)
4189 return std::make_pair(0U, nullptr);
4190
4191 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
4192 return std::make_pair(Reg, RC);
4193 }
4194
4195 if (!R.second)
4196 return std::make_pair(0U, nullptr);
4197
4198 if (Prefix == "$f") { // Parse $f0-$f31.
4199 // If the targets is single float only, always select 32-bit registers,
4200 // otherwise if the size of FP registers is 64-bit or Reg is an even number,
4201 // select the 64-bit register class. Otherwise, select the 32-bit register
4202 // class.
4203 if (VT == MVT::Other) {
4204 if (Subtarget.isSingleFloat())
4205 VT = MVT::f32;
4206 else
4207 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
4208 }
4209
4210 RC = getRegClassFor(VT);
4211
4212 if (RC == &Mips::AFGR64RegClass) {
4213 assert(Reg % 2 == 0);
4214 Reg >>= 1;
4215 }
4216 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
4217 RC = TRI->getRegClass(Mips::FCCRegClassID);
4218 else if (Prefix == "$w") { // Parse $w0-$w31.
4219 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
4220 } else { // Parse $0-$31.
4221 assert(Prefix == "$");
4222 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
4223 }
4224
4225 assert(Reg < RC->getNumRegs());
4226 return std::make_pair(*(RC->begin() + Reg), RC);
4227}
4228
4229/// Given a register class constraint, like 'r', if this corresponds directly
4230/// to an LLVM register class, return a register of 0 and the register class
4231/// pointer.
4232std::pair<unsigned, const TargetRegisterClass *>
4233MipsTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
4234 StringRef Constraint,
4235 MVT VT) const {
4236 if (Constraint.size() == 1) {
4237 switch (Constraint[0]) {
4238 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
4239 case 'y': // Same as 'r'. Exists for compatibility.
4240 case 'r':
4241 if ((VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8 ||
4242 VT == MVT::i1) ||
4243 (VT == MVT::f32 && Subtarget.useSoftFloat())) {
4244 if (Subtarget.inMips16Mode())
4245 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
4246 return std::make_pair(0U, &Mips::GPR32RegClass);
4247 }
4248 if ((VT == MVT::i64 || (VT == MVT::f64 && Subtarget.useSoftFloat()) ||
4249 (VT == MVT::f64 && Subtarget.isSingleFloat())) &&
4250 !Subtarget.isGP64bit())
4251 return std::make_pair(0U, &Mips::GPR32RegClass);
4252 if ((VT == MVT::i64 || (VT == MVT::f64 && Subtarget.useSoftFloat()) ||
4253 (VT == MVT::f64 && Subtarget.isSingleFloat())) &&
4254 Subtarget.isGP64bit())
4255 return std::make_pair(0U, &Mips::GPR64RegClass);
4256 // This will generate an error message
4257 return std::make_pair(0U, nullptr);
4258 case 'f': // FPU or MSA register
4259 if (VT == MVT::v16i8)
4260 return std::make_pair(0U, &Mips::MSA128BRegClass);
4261 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
4262 return std::make_pair(0U, &Mips::MSA128HRegClass);
4263 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
4264 return std::make_pair(0U, &Mips::MSA128WRegClass);
4265 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
4266 return std::make_pair(0U, &Mips::MSA128DRegClass);
4267 else if (VT == MVT::f32)
4268 return std::make_pair(0U, &Mips::FGR32RegClass);
4269 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
4270 if (Subtarget.isFP64bit())
4271 return std::make_pair(0U, &Mips::FGR64RegClass);
4272 return std::make_pair(0U, &Mips::AFGR64RegClass);
4273 }
4274 break;
4275 case 'c': // register suitable for indirect jump
4276 if (VT == MVT::i32)
4277 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
4278 if (VT == MVT::i64)
4279 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
4280 // This will generate an error message
4281 return std::make_pair(0U, nullptr);
4282 case 'l': // use the `lo` register to store values
4283 // that are no bigger than a word
4284 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8)
4285 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
4286 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
4287 case 'x': // use the concatenated `hi` and `lo` registers
4288 // to store doubleword values
4289 // Fixme: Not triggering the use of both hi and low
4290 // This will generate an error message
4291 return std::make_pair(0U, nullptr);
4292 }
4293 }
4294
4295 if (!Constraint.empty()) {
4296 std::pair<unsigned, const TargetRegisterClass *> R;
4297 R = parseRegForInlineAsmConstraint(Constraint, VT);
4298
4299 if (R.second)
4300 return R;
4301 }
4302
4303 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
4304}
4305
4306/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4307/// vector. If it is invalid, don't add anything to Ops.
4308void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4309 StringRef Constraint,
4310 std::vector<SDValue> &Ops,
4311 SelectionDAG &DAG) const {
4312 SDLoc DL(Op);
4314
4315 // Only support length 1 constraints for now.
4316 if (Constraint.size() > 1)
4317 return;
4318
4319 char ConstraintLetter = Constraint[0];
4320 switch (ConstraintLetter) {
4321 default: break; // This will fall through to the generic implementation
4322 case 'I': // Signed 16 bit constant
4323 // If this fails, the parent routine will give an error
4324 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4325 EVT Type = Op.getValueType();
4326 int64_t Val = C->getSExtValue();
4327 if (isInt<16>(Val)) {
4329 break;
4330 }
4331 }
4332 return;
4333 case 'J': // integer zero
4334 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4335 EVT Type = Op.getValueType();
4336 int64_t Val = C->getZExtValue();
4337 if (Val == 0) {
4338 Result = DAG.getTargetConstant(0, DL, Type);
4339 break;
4340 }
4341 }
4342 return;
4343 case 'K': // unsigned 16 bit immediate
4344 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4345 EVT Type = Op.getValueType();
4346 uint64_t Val = C->getZExtValue();
4347 if (isUInt<16>(Val)) {
4348 Result = DAG.getTargetConstant(Val, DL, Type);
4349 break;
4350 }
4351 }
4352 return;
4353 case 'L': // signed 32 bit immediate where lower 16 bits are 0
4354 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4355 EVT Type = Op.getValueType();
4356 int64_t Val = C->getSExtValue();
4357 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
4359 break;
4360 }
4361 }
4362 return;
4363 case 'N': // immediate in the range of -65535 to -1 (inclusive)
4364 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4365 EVT Type = Op.getValueType();
4366 int64_t Val = C->getSExtValue();
4367 if ((Val >= -65535) && (Val <= -1)) {
4369 break;
4370 }
4371 }
4372 return;
4373 case 'O': // signed 15 bit immediate
4374 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4375 EVT Type = Op.getValueType();
4376 int64_t Val = C->getSExtValue();
4377 if ((isInt<15>(Val))) {
4379 break;
4380 }
4381 }
4382 return;
4383 case 'P': // immediate in the range of 1 to 65535 (inclusive)
4384 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4385 EVT Type = Op.getValueType();
4386 int64_t Val = C->getSExtValue();
4387 if ((Val <= 65535) && (Val >= 1)) {
4388 Result = DAG.getTargetConstant(Val, DL, Type);
4389 break;
4390 }
4391 }
4392 return;
4393 }
4394
4395 if (Result.getNode()) {
4396 Ops.push_back(Result);
4397 return;
4398 }
4399
4401}
4402
4403bool MipsTargetLowering::isLegalAddressingMode(const DataLayout &DL,
4404 const AddrMode &AM, Type *Ty,
4405 unsigned AS,
4406 Instruction *I) const {
4407 // No global is ever allowed as a base.
4408 if (AM.BaseGV)
4409 return false;
4410
4411 switch (AM.Scale) {
4412 case 0: // "r+i" or just "i", depending on HasBaseReg.
4413 break;
4414 case 1:
4415 if (!AM.HasBaseReg) // allow "r+i".
4416 break;
4417 return false; // disallow "r+r" or "r+r+i".
4418 default:
4419 return false;
4420 }
4421
4422 return true;
4423}
4424
4425bool
4426MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4427 // The Mips target isn't yet aware of offsets.
4428 return false;
4429}
4430
4431EVT MipsTargetLowering::getOptimalMemOpType(
4432 LLVMContext &Context, const MemOp &Op,
4433 const AttributeList &FuncAttributes) const {
4434 if (Subtarget.hasMips64())
4435 return MVT::i64;
4436
4437 return MVT::i32;
4438}
4439
4440bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT,
4441 bool ForCodeSize) const {
4442 if (VT != MVT::f32 && VT != MVT::f64)
4443 return false;
4444 if (Imm.isNegZero())
4445 return false;
4446 return Imm.isZero();
4447}
4448
4449bool MipsTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
4450 return isInt<16>(Imm);
4451}
4452
4453bool MipsTargetLowering::isLegalAddImmediate(int64_t Imm) const {
4454 return isInt<16>(Imm);
4455}
4456
4458 if (!isPositionIndependent())
4460 if (ABI.IsN64())
4463}
4464
4465SDValue MipsTargetLowering::getPICJumpTableRelocBase(SDValue Table,
4466 SelectionDAG &DAG) const {
4467 if (!isPositionIndependent())
4468 return Table;
4470}
4471
4473 return Subtarget.useSoftFloat();
4474}
4475
4476void MipsTargetLowering::copyByValRegs(
4477 SDValue Chain, const SDLoc &DL, std::vector<SDValue> &OutChains,
4478 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
4479 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
4480 unsigned FirstReg, unsigned LastReg, const CCValAssign &VA,
4481 MipsCCState &State) const {
4482 MachineFunction &MF = DAG.getMachineFunction();
4483 MachineFrameInfo &MFI = MF.getFrameInfo();
4484 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
4485 unsigned NumRegs = LastReg - FirstReg;
4486 unsigned RegAreaSize = NumRegs * GPRSizeInBytes;
4487 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
4488 int FrameObjOffset;
4489 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs();
4490
4491 if (RegAreaSize)
4492 FrameObjOffset =
4493 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
4494 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes);
4495 else
4496 FrameObjOffset = VA.getLocMemOffset();
4497
4498 // Create frame object.
4499 EVT PtrTy = getPointerTy(DAG.getDataLayout());
4500 // Make the fixed object stored to mutable so that the load instructions
4501 // referencing it have their memory dependencies added.
4502 // Set the frame object as isAliased which clears the underlying objects
4503 // vector in ScheduleDAGInstrs::buildSchedGraph() resulting in addition of all
4504 // stores as dependencies for loads referencing this fixed object.
4505 int FI = MFI.CreateFixedObject(FrameObjSize, FrameObjOffset, false, true);
4506 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4507 InVals.push_back(FIN);
4508
4509 if (!NumRegs)
4510 return;
4511
4512 // Copy arg registers.
4513 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
4514 const TargetRegisterClass *RC = getRegClassFor(RegTy);
4515
4516 for (unsigned I = 0; I < NumRegs; ++I) {
4517 unsigned ArgReg = ByValArgRegs[FirstReg + I];
4518 unsigned VReg = addLiveIn(MF, ArgReg, RC);
4519 unsigned Offset = I * GPRSizeInBytes;
4520 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
4521 DAG.getConstant(Offset, DL, PtrTy));
4522 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
4523 StorePtr, MachinePointerInfo(FuncArg, Offset));
4524 OutChains.push_back(Store);
4525 }
4526}
4527
4528// Copy byVal arg to registers and stack.
4529void MipsTargetLowering::passByValArg(
4530 SDValue Chain, const SDLoc &DL,
4531 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
4532 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
4533 MachineFrameInfo &MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg,
4534 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle,
4535 const CCValAssign &VA) const {
4536 unsigned ByValSizeInBytes = Flags.getByValSize();
4537 unsigned OffsetInBytes = 0; // From beginning of struct
4538 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
4539 Align Alignment =
4540 std::min(Flags.getNonZeroByValAlign(), Align(RegSizeInBytes));
4541 EVT PtrTy = getPointerTy(DAG.getDataLayout()),
4542 RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
4543 unsigned NumRegs = LastReg - FirstReg;
4544
4545 if (NumRegs) {
4546 ArrayRef<MCPhysReg> ArgRegs = ABI.GetByValArgRegs();
4547 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes);
4548 unsigned I = 0;
4549
4550 // Copy words to registers.
4551 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) {
4552 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
4553 DAG.getConstant(OffsetInBytes, DL, PtrTy));
4554 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
4555 MachinePointerInfo(), Alignment);
4556 MemOpChains.push_back(LoadVal.getValue(1));
4557 unsigned ArgReg = ArgRegs[FirstReg + I];
4558 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
4559 }
4560
4561 // Return if the struct has been fully copied.
4562 if (ByValSizeInBytes == OffsetInBytes)
4563 return;
4564
4565 // Copy the remainder of the byval argument with sub-word loads and shifts.
4566 if (LeftoverBytes) {
4567 SDValue Val;
4568
4569 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
4570 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
4571 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
4572
4573 if (RemainingSizeInBytes < LoadSizeInBytes)
4574 continue;
4575
4576 // Load subword.
4577 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
4578 DAG.getConstant(OffsetInBytes, DL,
4579 PtrTy));
4580 SDValue LoadVal = DAG.getExtLoad(
4581 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
4582 MVT::getIntegerVT(LoadSizeInBytes * 8), Alignment);
4583 MemOpChains.push_back(LoadVal.getValue(1));
4584
4585 // Shift the loaded value.
4586 unsigned Shamt;
4587
4588 if (isLittle)
4589 Shamt = TotalBytesLoaded * 8;
4590 else
4591 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
4592
4593 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
4594 DAG.getConstant(Shamt, DL, MVT::i32));
4595
4596 if (Val.getNode())
4597 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
4598 else
4599 Val = Shift;
4600
4601 OffsetInBytes += LoadSizeInBytes;
4602 TotalBytesLoaded += LoadSizeInBytes;
4603 Alignment = std::min(Alignment, Align(LoadSizeInBytes));
4604 }
4605
4606 unsigned ArgReg = ArgRegs[FirstReg + I];
4607 RegsToPass.push_back(std::make_pair(ArgReg, Val));
4608 return;
4609 }
4610 }
4611
4612 // Copy remainder of byval arg to it with memcpy.
4613 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
4614 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
4615 DAG.getConstant(OffsetInBytes, DL, PtrTy));
4616 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
4618 Chain = DAG.getMemcpy(
4619 Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, DL, PtrTy),
4620 Align(Alignment), /*isVolatile=*/false, /*AlwaysInline=*/false,
4621 /*CI=*/nullptr, std::nullopt, MachinePointerInfo(), MachinePointerInfo());
4622 MemOpChains.push_back(Chain);
4623}
4624
4625void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
4626 SDValue Chain, const SDLoc &DL,
4627 SelectionDAG &DAG,
4628 CCState &State) const {
4629 ArrayRef<MCPhysReg> ArgRegs = ABI.getVarArgRegs(Subtarget.isGP64bit());
4630 unsigned Idx = State.getFirstUnallocated(ArgRegs);
4631 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
4632 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
4633 const TargetRegisterClass *RC = getRegClassFor(RegTy);
4634 MachineFunction &MF = DAG.getMachineFunction();
4635 MachineFrameInfo &MFI = MF.getFrameInfo();
4636 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
4637
4638 // Offset of the first variable argument from stack pointer.
4639 int VaArgOffset;
4640
4641 if (ArgRegs.size() == Idx)
4642 VaArgOffset = alignTo(State.getStackSize(), RegSizeInBytes);
4643 else {
4644 VaArgOffset =
4645 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
4646 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
4647 }
4648
4649 // Record the frame index of the first variable argument
4650 // which is a value necessary to VASTART.
4651 int FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
4652 MipsFI->setVarArgsFrameIndex(FI);
4653
4654 // Copy the integer registers that have not been used for argument passing
4655 // to the argument register save area. For O32, the save area is allocated
4656 // in the caller's stack frame, while for N32/64, it is allocated in the
4657 // callee's stack frame.
4658 for (unsigned I = Idx; I < ArgRegs.size();
4659 ++I, VaArgOffset += RegSizeInBytes) {
4660 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
4661 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
4662 FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
4663 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
4664 SDValue Store =
4665 DAG.getStore(Chain, DL, ArgValue, PtrOff, MachinePointerInfo());
4666 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
4667 (Value *)nullptr);
4668 OutChains.push_back(Store);
4669 }
4670}
4671
4673 Align Alignment) const {
4674 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
4675
4676 assert(Size && "Byval argument's size shouldn't be 0.");
4677
4678 Alignment = std::min(Alignment, TFL->getStackAlign());
4679
4680 unsigned FirstReg = 0;
4681 unsigned NumRegs = 0;
4682
4683 if (State->getCallingConv() != CallingConv::Fast) {
4684 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
4685 ArrayRef<MCPhysReg> IntArgRegs = ABI.GetByValArgRegs();
4686 // FIXME: The O32 case actually describes no shadow registers.
4687 const MCPhysReg *ShadowRegs =
4688 ABI.IsO32() ? IntArgRegs.data() : Mips64DPRegs;
4689
4690 // We used to check the size as well but we can't do that anymore since
4691 // CCState::HandleByVal() rounds up the size after calling this function.
4692 assert(
4693 Alignment >= Align(RegSizeInBytes) &&
4694 "Byval argument's alignment should be a multiple of RegSizeInBytes.");
4695
4696 FirstReg = State->getFirstUnallocated(IntArgRegs);
4697
4698 // If Alignment > RegSizeInBytes, the first arg register must be even.
4699 // FIXME: This condition happens to do the right thing but it's not the
4700 // right way to test it. We want to check that the stack frame offset
4701 // of the register is aligned.
4702 if ((Alignment > RegSizeInBytes) && (FirstReg % 2)) {
4703 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]);
4704 ++FirstReg;
4705 }
4706
4707 // Mark the registers allocated.
4708 Size = alignTo(Size, RegSizeInBytes);
4709 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size());
4710 Size -= RegSizeInBytes, ++I, ++NumRegs)
4711 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]);
4712 }
4713
4714 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs);
4715}
4716
4717MachineBasicBlock *MipsTargetLowering::emitPseudoSELECT(MachineInstr &MI,
4719 bool isFPCmp,
4720 unsigned Opc) const {
4722 "Subtarget already supports SELECT nodes with the use of"
4723 "conditional-move instructions.");
4724
4725 const TargetInstrInfo *TII =
4727 DebugLoc DL = MI.getDebugLoc();
4728
4729 // To "insert" a SELECT instruction, we actually have to insert the
4730 // diamond control-flow pattern. The incoming instruction knows the
4731 // destination vreg to set, the condition code register to branch on, the
4732 // true/false values to select between, and a branch opcode to use.
4733 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4735
4736 // thisMBB:
4737 // ...
4738 // TrueVal = ...
4739 // setcc r1, r2, r3
4740 // bNE r1, r0, copy1MBB
4741 // fallthrough --> copy0MBB
4742 MachineBasicBlock *thisMBB = BB;
4743 MachineFunction *F = BB->getParent();
4744 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4745 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
4746 F->insert(It, copy0MBB);
4747 F->insert(It, sinkMBB);
4748
4749 // Transfer the remainder of BB and its successor edges to sinkMBB.
4750 sinkMBB->splice(sinkMBB->begin(), BB,
4751 std::next(MachineBasicBlock::iterator(MI)), BB->end());
4753
4754 // Next, add the true and fallthrough blocks as its successors.
4755 BB->addSuccessor(copy0MBB);
4756 BB->addSuccessor(sinkMBB);
4757
4758 if (isFPCmp) {
4759 // bc1[tf] cc, sinkMBB
4760 BuildMI(BB, DL, TII->get(Opc))
4761 .addReg(MI.getOperand(1).getReg())
4762 .addMBB(sinkMBB);
4763 } else {
4764 // bne rs, $0, sinkMBB
4765 BuildMI(BB, DL, TII->get(Opc))
4766 .addReg(MI.getOperand(1).getReg())
4767 .addReg(Mips::ZERO)
4768 .addMBB(sinkMBB);
4769 }
4770
4771 // copy0MBB:
4772 // %FalseValue = ...
4773 // # fallthrough to sinkMBB
4774 BB = copy0MBB;
4775
4776 // Update machine-CFG edges
4777 BB->addSuccessor(sinkMBB);
4778
4779 // sinkMBB:
4780 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
4781 // ...
4782 BB = sinkMBB;
4783
4784 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
4785 .addReg(MI.getOperand(2).getReg())
4786 .addMBB(thisMBB)
4787 .addReg(MI.getOperand(3).getReg())
4788 .addMBB(copy0MBB);
4789
4790 MI.eraseFromParent(); // The pseudo instruction is gone now.
4791
4792 return BB;
4793}
4794
4796MipsTargetLowering::emitPseudoD_SELECT(MachineInstr &MI,
4797 MachineBasicBlock *BB) const {
4798 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
4799 "Subtarget already supports SELECT nodes with the use of"
4800 "conditional-move instructions.");
4801
4802 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
4803 DebugLoc DL = MI.getDebugLoc();
4804
4805 // D_SELECT substitutes two SELECT nodes that goes one after another and
4806 // have the same condition operand. On machines which don't have
4807 // conditional-move instruction, it reduces unnecessary branch instructions
4808 // which are result of using two diamond patterns that are result of two
4809 // SELECT pseudo instructions.
4810 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4812
4813 // thisMBB:
4814 // ...
4815 // TrueVal = ...
4816 // setcc r1, r2, r3
4817 // bNE r1, r0, copy1MBB
4818 // fallthrough --> copy0MBB
4819 MachineBasicBlock *thisMBB = BB;
4820 MachineFunction *F = BB->getParent();
4821 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4822 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
4823 F->insert(It, copy0MBB);
4824 F->insert(It, sinkMBB);
4825
4826 // Transfer the remainder of BB and its successor edges to sinkMBB.
4827 sinkMBB->splice(sinkMBB->begin(), BB,
4828 std::next(MachineBasicBlock::iterator(MI)), BB->end());
4830
4831 // Next, add the true and fallthrough blocks as its successors.
4832 BB->addSuccessor(copy0MBB);
4833 BB->addSuccessor(sinkMBB);
4834
4835 // bne rs, $0, sinkMBB
4836 BuildMI(BB, DL, TII->get(Mips::BNE))
4837 .addReg(MI.getOperand(2).getReg())
4838 .addReg(Mips::ZERO)
4839 .addMBB(sinkMBB);
4840
4841 // copy0MBB:
4842 // %FalseValue = ...
4843 // # fallthrough to sinkMBB
4844 BB = copy0MBB;
4845
4846 // Update machine-CFG edges
4847 BB->addSuccessor(sinkMBB);
4848
4849 // sinkMBB:
4850 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
4851 // ...
4852 BB = sinkMBB;
4853
4854 // Use two PHI nodes to select two reults
4855 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
4856 .addReg(MI.getOperand(3).getReg())
4857 .addMBB(thisMBB)
4858 .addReg(MI.getOperand(5).getReg())
4859 .addMBB(copy0MBB);
4860 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(1).getReg())
4861 .addReg(MI.getOperand(4).getReg())
4862 .addMBB(thisMBB)
4863 .addReg(MI.getOperand(6).getReg())
4864 .addMBB(copy0MBB);
4865
4866 MI.eraseFromParent(); // The pseudo instruction is gone now.
4867
4868 return BB;
4869}
4870
4871// Copies the function MipsAsmParser::matchCPURegisterName.
4872int MipsTargetLowering::getCPURegisterIndex(StringRef Name) const {
4873 int CC;
4874
4875 CC = StringSwitch<unsigned>(Name)
4876 .Case("zero", 0)
4877 .Case("at", 1)
4878 .Case("AT", 1)
4879 .Case("a0", 4)
4880 .Case("a1", 5)
4881 .Case("a2", 6)
4882 .Case("a3", 7)
4883 .Case("v0", 2)
4884 .Case("v1", 3)
4885 .Case("s0", 16)
4886 .Case("s1", 17)
4887 .Case("s2", 18)
4888 .Case("s3", 19)
4889 .Case("s4", 20)
4890 .Case("s5", 21)
4891 .Case("s6", 22)
4892 .Case("s7", 23)
4893 .Case("k0", 26)
4894 .Case("k1", 27)
4895 .Case("gp", 28)
4896 .Case("sp", 29)
4897 .Case("fp", 30)
4898 .Case("s8", 30)
4899 .Case("ra", 31)
4900 .Case("t0", 8)
4901 .Case("t1", 9)
4902 .Case("t2", 10)
4903 .Case("t3", 11)
4904 .Case("t4", 12)
4905 .Case("t5", 13)
4906 .Case("t6", 14)
4907 .Case("t7", 15)
4908 .Case("t8", 24)
4909 .Case("t9", 25)
4910 .Default(-1);
4911
4912 if (!(ABI.IsN32() || ABI.IsN64()))
4913 return CC;
4914
4915 // Although SGI documentation just cuts out t0-t3 for n32/n64,
4916 // GNU pushes the values of t0-t3 to override the o32/o64 values for t4-t7
4917 // We are supporting both cases, so for t0-t3 we'll just push them to t4-t7.
4918 if (8 <= CC && CC <= 11)
4919 CC += 4;
4920
4921 if (CC == -1)
4922 CC = StringSwitch<unsigned>(Name)
4923 .Case("a4", 8)
4924 .Case("a5", 9)
4925 .Case("a6", 10)
4926 .Case("a7", 11)
4927 .Case("kt0", 26)
4928 .Case("kt1", 27)
4929 .Default(-1);
4930
4931 return CC;
4932}
4933
4934// FIXME? Maybe this could be a TableGen attribute on some registers and
4935// this table could be generated automatically from RegInfo.
4938 const MachineFunction &MF) const {
4939 // 1. Delete symbol '$'.
4940 std::string newRegName = RegName;
4941 if (StringRef(RegName).starts_with("$"))
4942 newRegName = StringRef(RegName).substr(1);
4943
4944 // 2. Get register index value.
4945 std::smatch matchResult;
4946 int regIdx;
4947 static const std::regex matchStr("^[0-9]*$");
4948 if (std::regex_match(newRegName, matchResult, matchStr))
4949 regIdx = std::stoi(newRegName);
4950 else {
4951 newRegName = StringRef(newRegName).lower();
4952 regIdx = getCPURegisterIndex(StringRef(newRegName));
4953 }
4954
4955 // 3. Get register.
4956 if (regIdx >= 0 && regIdx < 32) {
4958 const MCRegisterClass &RC = Subtarget.isGP64bit()
4959 ? MRI->getRegClass(Mips::GPR64RegClassID)
4960 : MRI->getRegClass(Mips::GPR32RegClassID);
4961 return RC.getRegister(regIdx);
4962 }
4963
4965 Twine("Invalid register name \"" + StringRef(RegName) + "\"."));
4966}
4967
4968MachineBasicBlock *MipsTargetLowering::emitLDR_W(MachineInstr &MI,
4969 MachineBasicBlock *BB) const {
4970 MachineFunction *MF = BB->getParent();
4973 const bool IsLittle = Subtarget.isLittle();
4974 DebugLoc DL = MI.getDebugLoc();
4975
4976 Register Dest = MI.getOperand(0).getReg();
4977 Register Address = MI.getOperand(1).getReg();
4978 unsigned Imm = MI.getOperand(2).getImm();
4979
4981
4983 // Mips release 6 can load from adress that is not naturally-aligned.
4984 Register Temp = MRI.createVirtualRegister(&Mips::GPR32RegClass);
4985 BuildMI(*BB, I, DL, TII->get(Mips::LW))
4986 .addDef(Temp)
4987 .addUse(Address)
4988 .addImm(Imm);
4989 BuildMI(*BB, I, DL, TII->get(Mips::FILL_W)).addDef(Dest).addUse(Temp);
4990 } else {
4991 // Mips release 5 needs to use instructions that can load from an unaligned
4992 // memory address.
4993 Register LoadHalf = MRI.createVirtualRegister(&Mips::GPR32RegClass);
4994 Register LoadFull = MRI.createVirtualRegister(&Mips::GPR32RegClass);
4995 Register Undef = MRI.createVirtualRegister(&Mips::GPR32RegClass);
4996 BuildMI(*BB, I, DL, TII->get(Mips::IMPLICIT_DEF)).addDef(Undef);
4997 BuildMI(*BB, I, DL, TII->get(Mips::LWR))
4998 .addDef(LoadHalf)
4999 .addUse(Address)
5000 .addImm(Imm + (IsLittle ? 0 : 3))
5001 .addUse(Undef);
5002 BuildMI(*BB, I, DL, TII->get(Mips::LWL))
5003 .addDef(LoadFull)
5004 .addUse(Address)
5005 .addImm(Imm + (IsLittle ? 3 : 0))
5006 .addUse(LoadHalf);
5007 BuildMI(*BB, I, DL, TII->get(Mips::FILL_W)).addDef(Dest).addUse(LoadFull);
5008 }
5009
5010 MI.eraseFromParent();
5011 return BB;
5012}
5013
5014MachineBasicBlock *MipsTargetLowering::emitLDR_D(MachineInstr &MI,
5015 MachineBasicBlock *BB) const {
5016 MachineFunction *MF = BB->getParent();
5017 MachineRegisterInfo &MRI = MF->getRegInfo();
5018 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
5019 const bool IsLittle = Subtarget.isLittle();
5020 DebugLoc DL = MI.getDebugLoc();
5021
5022 Register Dest = MI.getOperand(0).getReg();
5023 Register Address = MI.getOperand(1).getReg();
5024 unsigned Imm = MI.getOperand(2).getImm();
5025
5027
5028 if (Subtarget.hasMips32r6() || Subtarget.hasMips64r6()) {
5029 // Mips release 6 can load from adress that is not naturally-aligned.
5030 if (Subtarget.isGP64bit()) {
5031 Register Temp = MRI.createVirtualRegister(&Mips::GPR64RegClass);
5032 BuildMI(*BB, I, DL, TII->get(Mips::LD))
5033 .addDef(Temp)
5034 .addUse(Address)
5035 .addImm(Imm);
5036 BuildMI(*BB, I, DL, TII->get(Mips::FILL_D)).addDef(Dest).addUse(Temp);
5037 } else {
5038 Register Wtemp = MRI.createVirtualRegister(&Mips::MSA128WRegClass);
5039 Register Lo = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5040 Register Hi = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5041 BuildMI(*BB, I, DL, TII->get(Mips::LW))
5042 .addDef(Lo)
5043 .addUse(Address)
5044 .addImm(Imm + (IsLittle ? 0 : 4));
5045 BuildMI(*BB, I, DL, TII->get(Mips::LW))
5046 .addDef(Hi)
5047 .addUse(Address)
5048 .addImm(Imm + (IsLittle ? 4 : 0));
5049 BuildMI(*BB, I, DL, TII->get(Mips::FILL_W)).addDef(Wtemp).addUse(Lo);
5050 BuildMI(*BB, I, DL, TII->get(Mips::INSERT_W), Dest)
5051 .addUse(Wtemp)
5052 .addUse(Hi)
5053 .addImm(1);
5054 }
5055 } else {
5056 // Mips release 5 needs to use instructions that can load from an unaligned
5057 // memory address.
5058 Register LoHalf = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5059 Register LoFull = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5060 Register LoUndef = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5061 Register HiHalf = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5062 Register HiFull = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5063 Register HiUndef = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5064 Register Wtemp = MRI.createVirtualRegister(&Mips::MSA128WRegClass);
5065 BuildMI(*BB, I, DL, TII->get(Mips::IMPLICIT_DEF)).addDef(LoUndef);
5066 BuildMI(*BB, I, DL, TII->get(Mips::LWR))
5067 .addDef(LoHalf)
5068 .addUse(Address)
5069 .addImm(Imm + (IsLittle ? 0 : 7))
5070 .addUse(LoUndef);
5071 BuildMI(*BB, I, DL, TII->get(Mips::LWL))
5072 .addDef(LoFull)
5073 .addUse(Address)
5074 .addImm(Imm + (IsLittle ? 3 : 4))
5075 .addUse(LoHalf);
5076 BuildMI(*BB, I, DL, TII->get(Mips::IMPLICIT_DEF)).addDef(HiUndef);
5077 BuildMI(*BB, I, DL, TII->get(Mips::LWR))
5078 .addDef(HiHalf)
5079 .addUse(Address)
5080 .addImm(Imm + (IsLittle ? 4 : 3))
5081 .addUse(HiUndef);
5082 BuildMI(*BB, I, DL, TII->get(Mips::LWL))
5083 .addDef(HiFull)
5084 .addUse(Address)
5085 .addImm(Imm + (IsLittle ? 7 : 0))
5086 .addUse(HiHalf);
5087 BuildMI(*BB, I, DL, TII->get(Mips::FILL_W)).addDef(Wtemp).addUse(LoFull);
5088 BuildMI(*BB, I, DL, TII->get(Mips::INSERT_W), Dest)
5089 .addUse(Wtemp)
5090 .addUse(HiFull)
5091 .addImm(1);
5092 }
5093
5094 MI.eraseFromParent();
5095 return BB;
5096}
5097
5098MachineBasicBlock *MipsTargetLowering::emitSTR_W(MachineInstr &MI,
5099 MachineBasicBlock *BB) const {
5100 MachineFunction *MF = BB->getParent();
5101 MachineRegisterInfo &MRI = MF->getRegInfo();
5102 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
5103 const bool IsLittle = Subtarget.isLittle();
5104 DebugLoc DL = MI.getDebugLoc();
5105
5106 Register StoreVal = MI.getOperand(0).getReg();
5107 Register Address = MI.getOperand(1).getReg();
5108 unsigned Imm = MI.getOperand(2).getImm();
5109
5111
5112 if (Subtarget.hasMips32r6() || Subtarget.hasMips64r6()) {
5113 // Mips release 6 can store to adress that is not naturally-aligned.
5114 Register BitcastW = MRI.createVirtualRegister(&Mips::MSA128WRegClass);
5115 Register Tmp = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5116 BuildMI(*BB, I, DL, TII->get(Mips::COPY)).addDef(BitcastW).addUse(StoreVal);
5117 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W))
5118 .addDef(Tmp)
5119 .addUse(BitcastW)
5120 .addImm(0);
5121 BuildMI(*BB, I, DL, TII->get(Mips::SW))
5122 .addUse(Tmp)
5123 .addUse(Address)
5124 .addImm(Imm);
5125 } else {
5126 // Mips release 5 needs to use instructions that can store to an unaligned
5127 // memory address.
5128 Register Tmp = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5129 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W))
5130 .addDef(Tmp)
5131 .addUse(StoreVal)
5132 .addImm(0);
5133 BuildMI(*BB, I, DL, TII->get(Mips::SWR))
5134 .addUse(Tmp)
5135 .addUse(Address)
5136 .addImm(Imm + (IsLittle ? 0 : 3));
5137 BuildMI(*BB, I, DL, TII->get(Mips::SWL))
5138 .addUse(Tmp)
5139 .addUse(Address)
5140 .addImm(Imm + (IsLittle ? 3 : 0));
5141 }
5142
5143 MI.eraseFromParent();
5144
5145 return BB;
5146}
5147
5148MachineBasicBlock *MipsTargetLowering::emitSTR_D(MachineInstr &MI,
5149 MachineBasicBlock *BB) const {
5150 MachineFunction *MF = BB->getParent();
5151 MachineRegisterInfo &MRI = MF->getRegInfo();
5152 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
5153 const bool IsLittle = Subtarget.isLittle();
5154 DebugLoc DL = MI.getDebugLoc();
5155
5156 Register StoreVal = MI.getOperand(0).getReg();
5157 Register Address = MI.getOperand(1).getReg();
5158 unsigned Imm = MI.getOperand(2).getImm();
5159
5161
5162 if (Subtarget.hasMips32r6() || Subtarget.hasMips64r6()) {
5163 // Mips release 6 can store to adress that is not naturally-aligned.
5164 if (Subtarget.isGP64bit()) {
5165 Register BitcastD = MRI.createVirtualRegister(&Mips::MSA128DRegClass);
5166 Register Lo = MRI.createVirtualRegister(&Mips::GPR64RegClass);
5167 BuildMI(*BB, I, DL, TII->get(Mips::COPY))
5168 .addDef(BitcastD)
5169 .addUse(StoreVal);
5170 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_D))
5171 .addDef(Lo)
5172 .addUse(BitcastD)
5173 .addImm(0);
5174 BuildMI(*BB, I, DL, TII->get(Mips::SD))
5175 .addUse(Lo)
5176 .addUse(Address)
5177 .addImm(Imm);
5178 } else {
5179 Register BitcastW = MRI.createVirtualRegister(&Mips::MSA128WRegClass);
5180 Register Lo = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5181 Register Hi = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5182 BuildMI(*BB, I, DL, TII->get(Mips::COPY))
5183 .addDef(BitcastW)
5184 .addUse(StoreVal);
5185 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W))
5186 .addDef(Lo)
5187 .addUse(BitcastW)
5188 .addImm(0);
5189 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W))
5190 .addDef(Hi)
5191 .addUse(BitcastW)
5192 .addImm(1);
5193 BuildMI(*BB, I, DL, TII->get(Mips::SW))
5194 .addUse(Lo)
5195 .addUse(Address)
5196 .addImm(Imm + (IsLittle ? 0 : 4));
5197 BuildMI(*BB, I, DL, TII->get(Mips::SW))
5198 .addUse(Hi)
5199 .addUse(Address)
5200 .addImm(Imm + (IsLittle ? 4 : 0));
5201 }
5202 } else {
5203 // Mips release 5 needs to use instructions that can store to an unaligned
5204 // memory address.
5205 Register Bitcast = MRI.createVirtualRegister(&Mips::MSA128WRegClass);
5206 Register Lo = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5207 Register Hi = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5208 BuildMI(*BB, I, DL, TII->get(Mips::COPY)).addDef(Bitcast).addUse(StoreVal);
5209 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W))
5210 .addDef(Lo)
5211 .addUse(Bitcast)
5212 .addImm(0);
5213 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W))
5214 .addDef(Hi)
5215 .addUse(Bitcast)
5216 .addImm(1);
5217 BuildMI(*BB, I, DL, TII->get(Mips::SWR))
5218 .addUse(Lo)
5219 .addUse(Address)
5220 .addImm(Imm + (IsLittle ? 0 : 3));
5221 BuildMI(*BB, I, DL, TII->get(Mips::SWL))
5222 .addUse(Lo)
5223 .addUse(Address)
5224 .addImm(Imm + (IsLittle ? 3 : 0));
5225 BuildMI(*BB, I, DL, TII->get(Mips::SWR))
5226 .addUse(Hi)
5227 .addUse(Address)
5228 .addImm(Imm + (IsLittle ? 4 : 7));
5229 BuildMI(*BB, I, DL, TII->get(Mips::SWL))
5230 .addUse(Hi)
5231 .addUse(Address)
5232 .addImm(Imm + (IsLittle ? 7 : 4));
5233 }
5234
5235 MI.eraseFromParent();
5236 return BB;
5237}
unsigned const MachineRegisterInfo * MRI
static SDValue performSHLCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, SelectionDAG &DAG)
If the operand is a bitwise AND with a constant RHS, and the shift has a constant RHS and is the only...
static SDValue performORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, const AArch64Subtarget *Subtarget, const AArch64TargetLowering &TLI)
return SDValue()
static SDValue performANDCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file declares a class to represent arbitrary precision floating point values and provide a varie...
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define RegName(no)
static LVOptions Options
Definition LVOptions.cpp:25
lazy value info
static MachineBasicBlock * insertDivByZeroTrap(MachineInstr &MI, MachineBasicBlock *MBB)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
cl::opt< bool > EmitJalrReloc
cl::opt< bool > NoZeroDivCheck
static bool CC_Mips(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
static SDValue performMADD_MSUBCombine(SDNode *ROOTNode, SelectionDAG &CurDAG, const MipsSubtarget &Subtarget)
static bool invertFPCondCodeUser(Mips::CondCode CC)
This function returns true if the floating point conditional branches and conditional moves which use...
static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State, ArrayRef< MCPhysReg > F64Regs)
static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG, bool SingleFloat)
static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG, TargetLowering::DAGCombinerInfo &DCI, const MipsSubtarget &Subtarget)
static const MCPhysReg Mips64DPRegs[8]
static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG, bool IsLittle)
static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD, SDValue Chain, unsigned Offset)
static unsigned addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
static std::pair< bool, bool > parsePhysicalReg(StringRef C, StringRef &Prefix, unsigned long long &Reg)
This is a helper function to parse a physical register string and split it into non-numeric and numer...
static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD, SDValue Chain, SDValue Src, unsigned Offset)
static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasExtractInsert)
static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG, TargetLowering::DAGCombinerInfo &DCI, const MipsSubtarget &Subtarget)
static SDValue performSUBCombine(SDNode *N, SelectionDAG &DAG, TargetLowering::DAGCombinerInfo &DCI, const MipsSubtarget &Subtarget)
static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op)
static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasExtractInsert)
static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG, TargetLowering::DAGCombinerInfo &DCI, const MipsSubtarget &Subtarget)
static SDValue performSignExtendCombine(SDNode *N, SelectionDAG &DAG, TargetLowering::DAGCombinerInfo &DCI, const MipsSubtarget &Subtarget)
static SDValue performCMovFPCombine(SDNode *N, SelectionDAG &DAG, TargetLowering::DAGCombinerInfo &DCI, const MipsSubtarget &Subtarget)
static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA, EVT ArgVT, const SDLoc &DL, SelectionDAG &DAG)
static Mips::CondCode condCodeToFCC(ISD::CondCode CC)
static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True, SDValue False, const SDLoc &DL)
uint64_t IntrinsicInst * II
const SmallVectorImpl< MachineOperand > & Cond
SI optimize exec mask operations pre RA
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:487
This file defines the SmallVector class.
static const MCPhysReg IntRegs[32]
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
DEMANGLE_NAMESPACE_BEGIN bool starts_with(std::string_view self, char C) noexcept
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
static const MCPhysReg F32Regs[64]
Value * RHS
Value * LHS
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
const T * data() const
Definition ArrayRef.h:139
LLVM Basic Block Representation.
Definition BasicBlock.h:62
static BranchProbability getOne()
CCState - This class holds information needed while lowering arguments and return values.
unsigned getFirstUnallocated(ArrayRef< MCPhysReg > Regs) const
getFirstUnallocated - Return the index of the first unallocated register in the set,...
CallingConv::ID getCallingConv() const
uint64_t getStackSize() const
Returns the size of the currently allocated portion of the stack.
CCValAssign - Represent assignment of one arg/retval to a location.
Register getLocReg() const
LocInfo getLocInfo() const
static CCValAssign getReg(unsigned ValNo, MVT ValVT, MCRegister Reg, MVT LocVT, LocInfo HTP, bool IsCustom=false)
static CCValAssign getCustomReg(unsigned ValNo, MVT ValVT, MCRegister Reg, MVT LocVT, LocInfo HTP)
bool isUpperBitsInLoc() const
static CCValAssign getMem(unsigned ValNo, MVT ValVT, int64_t Offset, MVT LocVT, LocInfo HTP, bool IsCustom=false)
bool needsCustom() const
int64_t getLocMemOffset() const
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
LLVM_ABI bool isMustTailCall() const
Tests if this call site must be tail call optimized.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
uint64_t getZExtValue() const
int64_t getSExtValue() const
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
LLVM_ABI TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
A debug info location.
Definition DebugLoc.h:123
const char * getSymbol() const
This is a fast-path instruction selection class that generates poor code and doesn't support illegal ...
Definition FastISel.h:66
FunctionLoweringInfo - This contains information that is global to a function that is used when lower...
bool hasStructRetAttr() const
Determine if the function returns a structure through first or second pointer argument.
Definition Function.h:687
const Argument * const_arg_iterator
Definition Function.h:73
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:730
const GlobalValue * getGlobal() const
bool hasLocalLinkage() const
bool hasDLLImportStorageClass() const
LLVM_ABI const GlobalObject * getAliaseeObject() const
Definition Globals.cpp:432
bool hasInternalLinkage() const
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
LLVM_ABI void emitError(const Instruction *I, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
This class is used to represent ISD::LOAD nodes.
const MCRegisterInfo * getRegisterInfo() const
Definition MCContext.h:414
LLVM_ABI MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
MCRegisterClass - Base class of TargetRegisterClass.
MCRegister getRegister(unsigned i) const
getRegister - Return the specified register in the class.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Machine Value Type.
static auto integer_valuetypes()
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
static MVT getVectorVT(MVT VT, unsigned NumElements)
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
bool isValid() const
Return true if this is a valid simple valuetype.
static MVT getIntegerVT(unsigned BitWidth)
static auto fp_valuetypes()
static auto fp_fixedlen_vector_valuetypes()
LLVM_ABI void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
LLVM_ABI int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable, bool isAliased=false)
Create a new object at a fixed location on the stack.
void setFrameAddressIsTaken(bool T)
void setHasTailCall(bool V=true)
void setReturnAddressIsTaken(bool s)
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MCContext & getContext() const
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
Register addLiveIn(MCRegister PReg, const TargetRegisterClass *RC)
addLiveIn - Add the specified physical register as a live-in value and create a corresponding virtual...
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addUse(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addDef(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
const MachineOperand & getOperand(unsigned i) const
@ EK_GPRel32BlockAddress
EK_GPRel32BlockAddress - Each entry is an address of block, encoded with a relocation as gp-relative,...
@ EK_BlockAddress
EK_BlockAddress - Each entry is a plain address of block, e.g.: .word LBB123.
@ EK_GPRel64BlockAddress
EK_GPRel64BlockAddress - Each entry is an address of block, encoded with a relocation as gp-relative,...
@ MOVolatile
The memory access is volatile.
Flags getFlags() const
Return the raw flags of the source value,.
MachineOperand class - Representation of each machine instruction operand.
void setSubReg(unsigned subReg)
static MachineOperand CreateMCSymbol(MCSymbol *Sym, unsigned TargetFlags=0)
void setIsKill(bool Val=true)
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
void addLiveIn(MCRegister Reg, Register vreg=Register())
addLiveIn - Add the specified register as a live-in.
Align getAlign() const
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
const MachinePointerInfo & getPointerInfo() const
const SDValue & getChain() const
EVT getMemoryVT() const
Return the type of the in-memory value.
static SpecialCallingConvType getSpecialCallingConvForCallee(const SDNode *Callee, const MipsSubtarget &Subtarget)
Determine the SpecialCallingConvType for the given callee.
MipsFunctionInfo - This class is derived from MachineFunction private Mips target-specific informatio...
void setVarArgsFrameIndex(int Index)
unsigned getSRetReturnReg() const
MachinePointerInfo callPtrInfo(MachineFunction &MF, const char *ES)
Create a MachinePointerInfo that has an ExternalSymbolPseudoSourceValue object representing a GOT ent...
Register getGlobalBaseReg(MachineFunction &MF)
void setSRetReturnReg(unsigned Reg)
void setFormalArgInfo(unsigned Size, bool HasByval)
static const uint32_t * getMips16RetHelperMask()
bool hasMips32r6() const
bool hasMips4() const
bool hasMips64r2() const
bool isLittle() const
const MipsInstrInfo * getInstrInfo() const override
bool hasMips64r6() const
bool inMips16Mode() const
bool hasMips64() const
bool hasMips32() const
const MipsRegisterInfo * getRegisterInfo() const override
bool hasCnMips() const
bool isGP64bit() const
bool hasExtractInsert() const
Features related to the presence of specific instructions.
bool isSingleFloat() const
const TargetFrameLowering * getFrameLowering() const override
MVT getRegisterTypeForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const override
Return the register type for a given MVT, ensuring vectors are treated as a series of gpr sized integ...
bool hasBitTest(SDValue X, SDValue Y) const override
Return true if the target has a bit-test instruction: (X & (1 << Y)) ==/!= 0 This knowledge can be us...
static const MipsTargetLowering * create(const MipsTargetMachine &TM, const MipsSubtarget &STI)
SDValue getAddrGPRel(NodeTy *N, const SDLoc &DL, EVT Ty, SelectionDAG &DAG, bool IsN64) const
unsigned getVectorTypeBreakdownForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const override
Break down vectors to the correct number of gpr sized integers.
Register getRegisterByName(const char *RegName, LLT VT, const MachineFunction &MF) const override
Return the register ID of the name passed in.
SDValue getAddrNonPICSym64(NodeTy *N, const SDLoc &DL, EVT Ty, SelectionDAG &DAG) const
EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const override
getSetCCResultType - get the ISD::SETCC result ValueType
SDValue getAddrGlobal(NodeTy *N, const SDLoc &DL, EVT Ty, SelectionDAG &DAG, unsigned Flag, SDValue Chain, const MachinePointerInfo &PtrInfo) const
FastISel * createFastISel(FunctionLoweringInfo &funcInfo, const TargetLibraryInfo *libInfo) const override
createFastISel - This method returns a target specific FastISel object, or null if the target does no...
MipsTargetLowering(const MipsTargetMachine &TM, const MipsSubtarget &STI)
const MipsABIInfo & ABI
SDValue getAddrGlobalLargeGOT(NodeTy *N, const SDLoc &DL, EVT Ty, SelectionDAG &DAG, unsigned HiFlag, unsigned LoFlag, SDValue Chain, const MachinePointerInfo &PtrInfo) const
SDValue getDllimportVariable(NodeTy *N, const SDLoc &DL, EVT Ty, SelectionDAG &DAG, SDValue Chain, const MachinePointerInfo &PtrInfo) const
bool shouldFoldConstantShiftPairToMask(const SDNode *N) const override
Return true if it is profitable to fold a pair of shifts into a mask.
SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override
This method will be invoked for all target nodes and for any target-independent nodes that the target...
CCAssignFn * CCAssignFnForReturn() const
void ReplaceNodeResults(SDNode *N, SmallVectorImpl< SDValue > &Results, SelectionDAG &DAG) const override
ReplaceNodeResults - Replace the results of node with an illegal result type with new values built ou...
MachineBasicBlock * EmitInstrWithCustomInserter(MachineInstr &MI, MachineBasicBlock *MBB) const override
This method should be implemented by targets that mark instructions with the 'usesCustomInserter' fla...
SDValue getDllimportSymbol(NodeTy *N, const SDLoc &DL, EVT Ty, SelectionDAG &DAG) const
CCAssignFn * CCAssignFnForCall() const
unsigned getNumRegistersForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const override
Return the number of registers for a given MVT, ensuring vectors are treated as a series of gpr sized...
SDValue getAddrNonPIC(NodeTy *N, const SDLoc &DL, EVT Ty, SelectionDAG &DAG) const
SDValue lowerSTORE(SDValue Op, SelectionDAG &DAG) const
void AdjustInstrPostInstrSelection(MachineInstr &MI, SDNode *Node) const override
This method should be implemented by targets that mark instructions with the 'hasPostISelHook' flag.
virtual void getOpndList(SmallVectorImpl< SDValue > &Ops, std::deque< std::pair< unsigned, SDValue > > &RegsToPass, bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage, bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const
This function fills Ops, which is the list of operands that will later be used when a function call n...
EVT getTypeForExtReturn(LLVMContext &Context, EVT VT, ISD::NodeType) const override
Return the type that should be used to zero or sign extend a zeroext/signext integer return value.
bool isCheapToSpeculateCtlz(Type *Ty) const override
Return true if it is cheap to speculate a call to intrinsic ctlz.
SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override
LowerOperation - Provide custom lowering hooks for some operations.
bool isCheapToSpeculateCttz(Type *Ty) const override
Return true if it is cheap to speculate a call to intrinsic cttz.
SDValue getAddrLocal(NodeTy *N, const SDLoc &DL, EVT Ty, SelectionDAG &DAG, bool IsN32OrN64) const
SDValue getGlobalReg(SelectionDAG &DAG, EVT Ty) const
const MipsSubtarget & Subtarget
void HandleByVal(CCState *, unsigned &, Align) const override
Target-specific cleanup for formal ByVal parameters.
SDValue lowerLOAD(SDValue Op, SelectionDAG &DAG) const
bool IsConstantInSmallSection(const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const
Return true if this constant should be placed into small data section.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
const SDValue & getOperand(unsigned Num) const
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
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
unsigned getOpcode() const
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
LLVM_ABI SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
SDValue getTargetGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, unsigned TargetFlags=0)
SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, Register Reg, SDValue N)
LLVM_ABI SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
LLVM_ABI MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false)
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
SDValue getGLOBAL_OFFSET_TABLE(EVT VT)
Return a GLOBAL_OFFSET_TABLE node. This does not have a useful SDLoc.
LLVM_ABI SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef< SDValue > Ops, EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags Flags=MachineMemOperand::MOLoad|MachineMemOperand::MOStore, LocationSize Size=LocationSize::precise(0), const AAMDNodes &AAInfo=AAMDNodes())
Creates a MemIntrinsicNode that may produce a result and takes a list of operands.
LLVM_ABI SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
SDValue getTargetJumpTable(int JTI, EVT VT, unsigned TargetFlags=0)
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
SDValue getCALLSEQ_END(SDValue Chain, SDValue Op1, SDValue Op2, SDValue InGlue, const SDLoc &DL)
Return a new CALLSEQ_END node, which always must have a glue result (to ensure it's not CSE'd).
SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, Register Reg, EVT VT)
const DataLayout & getDataLayout() const
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
SDValue getSignedTargetConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
LLVM_ABI SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
LLVM_ABI SDValue getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
SDValue getCALLSEQ_START(SDValue Chain, uint64_t InSize, uint64_t OutSize, const SDLoc &DL)
Return a new CALLSEQ_START node, that starts new call frame, in which InSize bytes are set up inside ...
SDValue getSelectCC(const SDLoc &DL, SDValue LHS, SDValue RHS, SDValue True, SDValue False, ISD::CondCode Cond, SDNodeFlags Flags=SDNodeFlags())
Helper function to make it easier to build SelectCC's if you just have an ISD::CondCode instead of an...
LLVM_ABI SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
LLVM_ABI SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI SDValue getValueType(EVT)
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI bool isKnownNeverNaN(SDValue Op, const APInt &DemandedElts, bool SNaN=false, unsigned Depth=0) const
Test whether the given SDValue (or all elements of it, if it is a vector) is known to never be NaN in...
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
SDValue getTargetBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset=0, unsigned TargetFlags=0)
LLVM_ABI void ReplaceAllUsesOfValueWith(SDValue From, SDValue To)
Replace any uses of From with To, leaving uses of other values produced by From.getNode() alone.
MachineFunction & getMachineFunction() const
LLVM_ABI SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LLVM_ABI SDValue getRegisterMask(const uint32_t *RegMask)
void addCallSiteInfo(const SDNode *Node, CallSiteInfo &&CallInfo)
Set CallSiteInfo to be associated with Node.
LLVMContext * getContext() const
LLVM_ABI SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
SDValue getTargetConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offset=0, unsigned TargetFlags=0)
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
LLVM_ABI std::pair< SDValue, SDValue > SplitScalar(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the scalar node with EXTRACT_ELEMENT using the provided VTs and return the low/high part.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class is used to represent ISD::STORE nodes.
const SDValue & getBasePtr() const
const SDValue & getValue() const
bool isTruncatingStore() const
Return true if the op does a truncation before store.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:573
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
const char * const_iterator
Definition StringRef.h:60
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:146
LLVM_ABI std::string lower() const
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
Information about stack frame layout on the target.
unsigned getStackAlignment() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
TargetInstrInfo - Interface to description of machine instruction set.
Provides information about what library functions are available for the current target.
void setBooleanVectorContents(BooleanContent Ty)
Specify how the target extends the result of a vector boolean value from a vector of i1 to a wider ty...
void setOperationAction(unsigned Op, MVT VT, LegalizeAction Action)
Indicate that the specified operation does not work with the specified type and indicate what to do a...
virtual const TargetRegisterClass * getRegClassFor(MVT VT, bool isDivergent=false) const
Return the register class that should be used for the specified value type.
void setMinStackArgumentAlignment(Align Alignment)
Set the minimum stack alignment of an argument.
const TargetMachine & getTargetMachine() const
virtual unsigned getNumRegisters(LLVMContext &Context, EVT VT, std::optional< MVT > RegisterVT=std::nullopt) const
Return the number of registers that this ValueType will eventually require.
void setMaxAtomicSizeInBitsSupported(unsigned SizeInBits)
Set the maximum atomic operation size supported by the backend.
void setMinFunctionAlignment(Align Alignment)
Set the target's minimum function alignment.
void setBooleanContents(BooleanContent Ty)
Specify how the target extends the result of integer and floating point boolean values from i1 to a w...
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...
void setTruncStoreAction(MVT ValVT, MVT MemVT, LegalizeAction Action)
Indicate that the specified truncating store does not work with the specified type and indicate what ...
void setStackPointerRegisterToSaveRestore(Register R)
If set to a physical register, this specifies the register that llvm.savestack/llvm....
void AddPromotedToType(unsigned Opc, MVT OrigVT, MVT DestVT)
If Opc/OrigVT is specified as being promoted, the promotion code defaults to trying a larger integer/...
void setTargetDAGCombine(ArrayRef< ISD::NodeType > NTs)
Targets should invoke this method for each target independent node that they want to provide a custom...
virtual bool useSoftFloat() const
Align getMinStackArgumentAlignment() const
Return the minimum stack alignment of an argument.
void setLoadExtAction(unsigned ExtType, MVT ValVT, MVT MemVT, LegalizeAction Action)
Indicate that the specified load with extension does not work with the specified type and indicate wh...
std::vector< ArgListEntry > ArgListTy
unsigned MaxStoresPerMemcpy
Specify maximum number of store instructions per memcpy call.
MVT getRegisterType(MVT VT) const
Return the type of registers that this ValueType will eventually require.
virtual ConstraintType getConstraintType(StringRef Constraint) const
Given a constraint, return the type of constraint it is for this target.
virtual SDValue LowerToTLSEmulatedModel(const GlobalAddressSDNode *GA, SelectionDAG &DAG) const
Lower TLS global address SDNode for target independent emulated TLS model.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
bool isPositionIndependent() const
virtual ConstraintWeight getSingleConstraintMatchWeight(AsmOperandInfo &info, const char *constraint) const
Examine constraint string and operand type and determine a weight value.
virtual std::pair< unsigned, const TargetRegisterClass * > getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const
Given a physical register constraint (e.g.
TargetLowering(const TargetLowering &)=delete
virtual ArrayRef< MCPhysReg > getRoundingControlRegisters() const
Returns a 0 terminated array of rounding control registers that can be attached into strict FP call.
virtual void LowerAsmOperandForConstraint(SDValue Op, StringRef Constraint, std::vector< SDValue > &Ops, SelectionDAG &DAG) const
Lower the specified operand into the Ops vector.
virtual unsigned getJumpTableEncoding() const
Return the entry encoding for a jump table in the current function.
virtual void LowerOperationWrapper(SDNode *N, SmallVectorImpl< SDValue > &Results, SelectionDAG &DAG) const
This callback is invoked by the type legalizer to legalize nodes with an illegal operand type but leg...
TLSModel::Model getTLSModel(const GlobalValue *GV) const
Returns the TLS model which should be used for the given global variable.
bool useEmulatedTLS() const
Returns true if this target uses emulated TLS.
virtual TargetLoweringObjectFile * getObjFileLowering() const
TargetOptions Options
unsigned NoNaNsFPMath
NoNaNsFPMath - This flag is enabled when the -enable-no-nans-fp-math flag is specified on the command...
unsigned EnableFastISel
EnableFastISel - This flag enables fast-path instruction selection which trades away generated code q...
unsigned EmitCallGraphSection
Emit section containing call graph metadata.
iterator begin() const
begin/end - Return all of the registers in this class.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition Type.h:153
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:197
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:240
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:300
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:225
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
self_iterator getIterator()
Definition ilist_node.h:123
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ Fast
Attempts to make calls as fast as possible (e.g.
Definition CallingConv.h:41
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:809
@ STACKRESTORE
STACKRESTORE has two operands, an input chain and a pointer to restore to it returns an output chain.
@ STACKSAVE
STACKSAVE - STACKSAVE has one operand, an input chain.
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:506
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:773
@ VAEND
VAEND, VASTART - VAEND and VASTART have three operands: an input chain, pointer, and a SRCVALUE.
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:259
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:843
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:513
@ GlobalAddress
Definition ISDOpcodes.h:88
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:412
@ ATOMIC_FENCE
OUTCHAIN = ATOMIC_FENCE(INCHAIN, ordering, scope) This corresponds to the fence instruction.
@ SDIVREM
SDIVREM/UDIVREM - Divide two integers and produce both a quotient and remainder result.
Definition ISDOpcodes.h:275
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
Definition ISDOpcodes.h:993
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition ISDOpcodes.h:983
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:249
@ GlobalTLSAddress
Definition ISDOpcodes.h:89
@ EH_RETURN
OUTCHAIN = EH_RETURN(INCHAIN, OFFSET, HANDLER) - This node represents 'eh_return' gcc dwarf builtin,...
Definition ISDOpcodes.h:151
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:834
@ TargetJumpTable
Definition ISDOpcodes.h:183
@ FSINCOS
FSINCOS - Compute both fsin and fcos as a single operation.
@ BR_CC
BR_CC - Conditional branch.
@ BR_JT
BR_JT - Jumptable branch.
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:536
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition ISDOpcodes.h:543
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:786
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
@ VACOPY
VACOPY - VACOPY has 5 operands: an input chain, a destination pointer, a source pointer,...
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:764
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:840
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:801
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum maximum on two values, following IEEE-754 definition...
@ DYNAMIC_STACKALLOC
DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned to a specified boundary.
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:878
@ EH_DWARF_CFA
EH_DWARF_CFA - This node represents the pointer to the DWARF Canonical Frame Address (CFA),...
Definition ISDOpcodes.h:145
@ FRAMEADDR
FRAMEADDR, RETURNADDR - These nodes represent llvm.frameaddress and llvm.returnaddress on the DAG.
Definition ISDOpcodes.h:110
@ STRICT_FP_TO_UINT
Definition ISDOpcodes.h:473
@ STRICT_FP_TO_SINT
STRICT_FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:472
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:916
@ READCYCLECOUNTER
READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:738
@ TRAP
TRAP - Trapping instruction.
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:846
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
@ BRCOND
BRCOND - Conditional branch.
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition ISDOpcodes.h:823
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:529
@ CALLSEQ_START
CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of a call sequence,...
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, EVT Type)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
@ Bitcast
Perform the operation on a different, but equivalently sized type.
@ MO_TLSGD
On a symbol operand, this indicates that the immediate is the offset to the slot in GOT which stores ...
Flag
These should be considered private to the implementation of the MCInstrDesc class.
FastISel * createFastISel(FunctionLoweringInfo &funcInfo, const TargetLibraryInfo *libInfo)
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Dead
Unused definition.
@ Define
Register definition.
@ Kill
The last use of a register.
@ EarlyClobber
Register definition happens before uses.
Not(const Pred &P) -> Not< Pred >
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:532
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:165
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
bool CCAssignFn(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
CCAssignFn - This function assigns a location for Val, updating State to reflect the change.
constexpr bool isShiftedMask_64(uint64_t Value)
Return true if the argument contains a non-empty sequence of ones with the remainder zero (64 bit ver...
Definition MathExtras.h:273
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:189
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
constexpr T divideCeil(U Numerator, V Denominator)
Returns the integer ceil(Numerator / Denominator).
Definition MathExtras.h:394
@ Other
Any other memory.
Definition ModRef.h:68
@ AfterLegalizeDAG
Definition DAGCombine.h:19
const MipsTargetLowering * createMips16TargetLowering(const MipsTargetMachine &TM, const MipsSubtarget &STI)
Create MipsTargetLowering objects.
@ Or
Bitwise or logical OR of integers.
@ Add
Sum of integers.
unsigned getKillRegState(bool B)
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
const MipsTargetLowering * createMipsSETargetLowering(const MipsTargetMachine &TM, const MipsSubtarget &STI)
LLVM_ABI bool getAsUnsignedInteger(StringRef Str, unsigned Radix, unsigned long long &Result)
Helper functions for StringRef::getAsInteger.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
Extended Value Type.
Definition ValueTypes.h:35
EVT changeVectorElementTypeToInteger() const
Return a vector with the same number of elements as this vector, but with the element type converted ...
Definition ValueTypes.h:94
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:300
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:373
bool isPow2VectorType() const
Returns true if the given vector is a power of 2.
Definition ValueTypes.h:470
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:316
static EVT getFloatingPointVT(unsigned BitWidth)
Returns the EVT that represents a floating-point type with the given number of bits.
Definition ValueTypes.h:59
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:168
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
bool isRound() const
Return true if the size is a power-of-two number of bytes.
Definition ValueTypes.h:248
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:328
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:336
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:152
Align getNonZeroOrigAlign() const
SmallVector< ArgRegPair, 1 > ArgRegPairs
Vector of call argument and its forwarding register.
This class contains a discriminated union of information about pointers in memory operands,...
static LLVM_ABI MachinePointerInfo getGOT(MachineFunction &MF)
Return a MachinePointerInfo record that refers to a GOT entry.
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
This structure contains all information that is necessary for lowering calls.
SmallVector< ISD::InputArg, 32 > Ins
SmallVector< ISD::OutputArg, 32 > Outs