LLVM 24.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"
49#include "llvm/IR/CallingConv.h"
50#include "llvm/IR/Constants.h"
51#include "llvm/IR/DataLayout.h"
52#include "llvm/IR/DebugLoc.h"
54#include "llvm/IR/Function.h"
55#include "llvm/IR/GlobalValue.h"
56#include "llvm/IR/Module.h"
57#include "llvm/IR/Type.h"
58#include "llvm/IR/Value.h"
59#include "llvm/MC/MCContext.h"
67#include <algorithm>
68#include <cassert>
69#include <cctype>
70#include <cstdint>
71#include <deque>
72#include <iterator>
73#include <string>
74#include <utility>
75#include <vector>
76
77using namespace llvm;
78
79#define DEBUG_TYPE "mips-lower"
80
81STATISTIC(NumTailCalls, "Number of tail calls");
82
83static const MCPhysReg Mips64DPRegs[8] = {
84 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
85 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
86};
87
89 Break, // MIPS I
90 Teq, // MIPS II+
91 TeqMM, // microMIPS
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;
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 }
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(STI.getABI()) {
175 // Mips does not have i1 type, so use i32 for
176 // setcc operations results (slt, sgt, ...).
179
180 // Load extented operations for i1 types must be promoted
181 for (MVT VT : MVT::integer_valuetypes()) {
185 }
186
187 // MIPS doesn't have extending float->double load/store. Set LoadExtAction
188 // for f32, f16
189 for (MVT VT : MVT::fp_valuetypes()) {
190 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
191 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
192 }
193
194 // Set LoadExtAction for f16 vectors to Expand
196 MVT F16VT = MVT::getVectorVT(MVT::f16, VT.getVectorNumElements());
197 if (F16VT.isValid())
199 }
200
201 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
202 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
203
204 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
205
206 // Used by legalize types to correctly generate the setcc result.
207 // Without this, every float setcc comes with a AND/OR with the result,
208 // we don't want this, since the fpcmp result goes to a flag register,
209 // which is used implicitly by brcond and select operations.
210 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
211
212 // Mips Custom Operations
218 if (!Subtarget.inMips16Mode())
234
239
240 if (Subtarget.hasMips32r2() ||
241 getTargetMachine().getTargetTriple().isOSLinux())
243
244 // Lower fmin/fmax/fclass operations for MIPS R6.
245 if (Subtarget.hasMips32r6()) {
254 if (!Subtarget.inMicroMipsMode()) {
259 }
264 } else {
267 }
268
269 if (Subtarget.hasMTHC1())
271
272 if (Subtarget.isGP64bit()) {
277 if (!Subtarget.inMips16Mode())
280 if (Subtarget.hasMips64r6()) {
283 } else {
286 }
292 }
293
294 if (!Subtarget.isGP64bit()) {
298 }
299
301 if (Subtarget.isGP64bit())
303
312
313 // Operations not directly supported by Mips.
327
328 if (Subtarget.hasCnMips()) {
331 } else {
334 }
341
342 if (!Subtarget.hasMips32r2())
344
345 if (!Subtarget.hasMips64r2())
347
364
365 // Lower f16 conversion operations into library calls
370 // The constrained f16 conversions default to Legal. Use libcalls for them,
371 // extending to f64 through f32.
376
378
383
384 // Use the default for now
387
388 if (!Subtarget.isGP64bit()) {
391 }
392
393 if (!Subtarget.hasMips32r2()) {
396 }
397
398 // MIPS16 lacks MIPS32's clz and clo instructions.
399 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode())
401 if (!Subtarget.hasMips64())
403
404 if (!Subtarget.hasMips32r2())
406 if (!Subtarget.hasMips64r2())
408
409 if (Subtarget.isGP64bit() && Subtarget.hasMips64r6()) {
410 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i32, Legal);
411 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i32, Legal);
412 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i32, Legal);
413 setTruncStoreAction(MVT::i64, MVT::i32, Legal);
414 } else if (Subtarget.isGP64bit()) {
415 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i32, Custom);
416 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i32, Custom);
417 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i32, Custom);
418 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
419 }
420
421 setOperationAction(ISD::TRAP, MVT::Other, Legal);
422
426
427 // Sink shifts into their users' blocks to expose extract patterns.
428 setHasExtractBitsInsn(Subtarget.hasExtractInsert());
429
430 // MIPS I and the R5900 have no LL/SC instructions for atomic operations.
431 bool HasLLSC = Subtarget.hasMips2() && !Subtarget.isR5900();
432 if (HasLLSC && Subtarget.isGP64bit())
434 else if (HasLLSC)
436 else
438
439 setMinFunctionAlignment(Subtarget.isGP64bit() ? Align(8) : Align(4));
440
441 // The arguments on the stack are defined in terms of 4-byte slots on O32
442 // and 8-byte slots on N32/N64.
443 setMinStackArgumentAlignment((ABI.IsN32() || ABI.IsN64()) ? Align(8)
444 : Align(4));
445
447
449
450 isMicroMips = Subtarget.inMicroMipsMode();
451
452 // Constrained FP operations are lowered explicitly instead of being mutated
453 // to the non-strict ones.
454 IsStrictFPEnabled = true;
455}
456
457const MipsTargetLowering *
459 const MipsSubtarget &STI) {
460 if (STI.inMips16Mode())
461 return createMips16TargetLowering(TM, STI);
462
463 return createMipsSETargetLowering(TM, STI);
464}
465
466// Create a fast isel object.
468 FunctionLoweringInfo &funcInfo, const TargetLibraryInfo *libInfo,
469 const LibcallLoweringInfo *libcallLowering) const {
470 const MipsTargetMachine &TM =
471 static_cast<const MipsTargetMachine &>(funcInfo.MF->getTarget());
472
473 // We support only the standard encoding [MIPS32,MIPS32R5] ISAs.
474 bool UseFastISel = TM.Options.EnableFastISel && Subtarget.hasMips32() &&
475 !Subtarget.hasMips32r6() && !Subtarget.inMips16Mode() &&
476 !Subtarget.inMicroMipsMode();
477
478 // Disable if either of the following is true:
479 // We do not generate PIC, the ABI is not O32, XGOT is being used.
480 if (!TM.isPositionIndependent() || !Subtarget.getABI().IsO32() ||
481 Subtarget.useXGOT())
482 UseFastISel = false;
483
484 return UseFastISel ? Mips::createFastISel(funcInfo, libInfo, libcallLowering)
485 : nullptr;
486}
487
489 EVT VT) const {
490 if (!VT.isVector())
491 return MVT::i32;
493}
494
497 const MipsSubtarget &Subtarget) {
498 if (DCI.isBeforeLegalizeOps())
499 return SDValue();
500
501 EVT Ty = N->getValueType(0);
502 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
503 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
504 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
505 MipsISD::DivRemU16;
506 SDLoc DL(N);
507
508 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
509 N->getOperand(0), N->getOperand(1));
510 SDValue InChain = DAG.getEntryNode();
511 SDValue InGlue = DivRem;
512
513 // insert MFLO
514 if (N->hasAnyUseOfValue(0)) {
515 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
516 InGlue);
517 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
518 InChain = CopyFromLo.getValue(1);
519 InGlue = CopyFromLo.getValue(2);
520 }
521
522 // insert MFHI
523 if (N->hasAnyUseOfValue(1)) {
524 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
525 HI, Ty, InGlue);
526 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
527 }
528
529 return SDValue();
530}
531
533 switch (CC) {
534 default: llvm_unreachable("Unknown fp condition code!");
535 case ISD::SETEQ:
536 case ISD::SETOEQ: return Mips::FCOND_OEQ;
537 case ISD::SETUNE: return Mips::FCOND_UNE;
538 case ISD::SETLT:
539 case ISD::SETOLT: return Mips::FCOND_OLT;
540 case ISD::SETGT:
541 case ISD::SETOGT: return Mips::FCOND_OGT;
542 case ISD::SETLE:
543 case ISD::SETOLE: return Mips::FCOND_OLE;
544 case ISD::SETGE:
545 case ISD::SETOGE: return Mips::FCOND_OGE;
546 case ISD::SETULT: return Mips::FCOND_ULT;
547 case ISD::SETULE: return Mips::FCOND_ULE;
548 case ISD::SETUGT: return Mips::FCOND_UGT;
549 case ISD::SETUGE: return Mips::FCOND_UGE;
550 case ISD::SETUO: return Mips::FCOND_UN;
551 case ISD::SETO: return Mips::FCOND_OR;
552 case ISD::SETNE:
553 case ISD::SETONE: return Mips::FCOND_ONE;
554 case ISD::SETUEQ: return Mips::FCOND_UEQ;
555 }
556}
557
558/// This function returns true if the floating point conditional branches and
559/// conditional moves which use condition code CC should be inverted.
561 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
562 return false;
563
565 "Illegal Condition Code");
566
567 return true;
568}
569
570// Creates and returns an FPCmp node from a setcc node.
571// Returns Op if setcc is not a floating point comparison.
573 // must be a SETCC node
574 if (Op.getOpcode() != ISD::SETCC && Op.getOpcode() != ISD::STRICT_FSETCC &&
575 Op.getOpcode() != ISD::STRICT_FSETCCS)
576 return Op;
577
578 SDValue LHS = Op.getOperand(0);
579
580 if (!LHS.getValueType().isFloatingPoint())
581 return Op;
582
583 SDValue RHS = Op.getOperand(1);
584 SDLoc DL(Op);
585
586 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
587 // node if necessary.
588 ISD::CondCode CC = Op.getOperandCondCode(2);
589
590 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
591 DAG.getConstant(condCodeToFCC(CC), DL, MVT::i32));
592}
593
594// Creates and returns a CMovFPT/F node.
596 SDValue False, const SDLoc &DL) {
597 // The condition code is the last operand of both FPCmp and STRICT_FPCmp.
599 cast<ConstantSDNode>(Cond.getOperand(Cond.getNumOperands() - 1));
600 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
601 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
602
603 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
604 True.getValueType(), True, FCC0, False, Cond);
605}
606
609 const MipsSubtarget &Subtarget) {
610 if (DCI.isBeforeLegalizeOps())
611 return SDValue();
612
613 SDValue SetCC = N->getOperand(0);
614
615 // An R6 FP compare is lowered to (and Mask, 1), where Mask is 0 or -1.
616 // (select (and X, 1), -1, 0) -> (sext X) and
617 // (select (and X, 1), 0, -1) -> (sext (not X))
618 // when every bit of X is a copy of its sign bit.
619 EVT VT = N->getValueType(0);
620 if (SetCC.getOpcode() == ISD::AND && isOneConstant(SetCC.getOperand(1)) &&
621 VT.isScalarInteger()) {
622 SDValue X = SetCC.getOperand(0);
623 SDValue True = N->getOperand(1), False = N->getOperand(2);
624 EVT XVT = X.getValueType();
625 if (DAG.ComputeNumSignBits(X) == XVT.getScalarSizeInBits()) {
626 SDLoc DL(N);
628 return DAG.getSExtOrTrunc(X, DL, VT);
630 return DAG.getSExtOrTrunc(DAG.getNOT(DL, X, XVT), DL, VT);
631 }
632 }
633
634 if ((SetCC.getOpcode() != ISD::SETCC) ||
635 !SetCC.getOperand(0).getValueType().isInteger())
636 return SDValue();
637
638 SDValue False = N->getOperand(2);
639 EVT FalseTy = False.getValueType();
640
641 if (!FalseTy.isInteger())
642 return SDValue();
643
644 // r6 selects can test canonical words directly in i64.
645 SDValue LHS = SetCC.getOperand(0);
647 if (Subtarget.hasMips64r6() && FalseTy == MVT::i64 &&
648 LHS.getOpcode() == ISD::TRUNCATE && LHS.getValueType() == MVT::i32 &&
649 LHS.getOperand(0).getValueType() == MVT::i64 &&
650 (CC == ISD::SETEQ || CC == ISD::SETNE) &&
651 isNullConstant(SetCC.getOperand(1)) &&
652 DAG.ComputeNumSignBits(LHS.getOperand(0)) > 32) {
653 SDLoc DL(N);
654 SDValue WideCC = DAG.getSetCC(DL, SetCC.getValueType(), LHS.getOperand(0),
655 DAG.getConstant(0, DL, MVT::i64), CC);
656 return DAG.getNode(ISD::SELECT, DL, FalseTy, WideCC, N->getOperand(1),
657 False);
658 }
659
661
662 // If the RHS (False) is 0, we swap the order of the operands
663 // of ISD::SELECT (obviously also inverting the condition) so that we can
664 // take advantage of conditional moves using the $0 register.
665 // Example:
666 // return (a != 0) ? x : 0;
667 // load $reg, x
668 // movz $reg, $0, a
669 if (!FalseC)
670 return SDValue();
671
672 const SDLoc DL(N);
673
674 if (!FalseC->getZExtValue()) {
676 SDValue True = N->getOperand(1);
677
678 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
679 SetCC.getOperand(1),
681
682 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
683 }
684
685 // If both operands are integer constants there's a possibility that we
686 // can do some interesting optimizations.
687 SDValue True = N->getOperand(1);
689
690 if (!TrueC || !True.getValueType().isInteger())
691 return SDValue();
692
693 // We'll also ignore MVT::i64 operands as this optimizations proves
694 // to be ineffective because of the required sign extensions as the result
695 // of a SETCC operator is always MVT::i32 for non-vector types.
696 if (True.getValueType() == MVT::i64)
697 return SDValue();
698
699 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
700
701 // 1) (a < x) ? y : y-1
702 // slti $reg1, a, x
703 // addiu $reg2, $reg1, y-1
704 if (Diff == 1)
705 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
706
707 // 2) (a < x) ? y-1 : y
708 // slti $reg1, a, x
709 // xor $reg1, $reg1, 1
710 // addiu $reg2, $reg1, y-1
711 if (Diff == -1) {
713 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
714 SetCC.getOperand(1),
716 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
717 }
718
719 // Could not optimize.
720 return SDValue();
721}
722
725 const MipsSubtarget &Subtarget) {
726 if (DCI.isBeforeLegalizeOps())
727 return SDValue();
728
729 SDValue ValueIfTrue = N->getOperand(0), ValueIfFalse = N->getOperand(2);
730
731 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(ValueIfFalse);
732 if (!FalseC || FalseC->getZExtValue())
733 return SDValue();
734
735 // Since RHS (False) is 0, we swap the order of the True/False operands
736 // (obviously also inverting the condition) so that we can
737 // take advantage of conditional moves using the $0 register.
738 // Example:
739 // return (a != 0) ? x : 0;
740 // load $reg, x
741 // movz $reg, $0, a
742 unsigned Opc = (N->getOpcode() == MipsISD::CMovFP_T) ? MipsISD::CMovFP_F :
743 MipsISD::CMovFP_T;
744
745 SDValue FCC = N->getOperand(1), Glue = N->getOperand(3);
746 return DAG.getNode(Opc, SDLoc(N), ValueIfFalse.getValueType(),
747 ValueIfFalse, FCC, ValueIfTrue, Glue);
748}
749
752 const MipsSubtarget &Subtarget) {
753 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
754 return SDValue();
755
756 SDValue FirstOperand = N->getOperand(0);
757 unsigned FirstOperandOpc = FirstOperand.getOpcode();
758 SDValue Mask = N->getOperand(1);
759 EVT ValTy = N->getValueType(0);
760 SDLoc DL(N);
761
762 uint64_t Pos = 0;
763 unsigned SMPos, SMSize;
764 ConstantSDNode *CN;
765 SDValue NewOperand;
766 unsigned Opc;
767
768 // Op's second operand must be a shifted mask.
769 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
770 !isShiftedMask_64(CN->getZExtValue(), SMPos, SMSize))
771 return SDValue();
772
773 if (FirstOperandOpc == ISD::SRA || FirstOperandOpc == ISD::SRL) {
774 // Pattern match EXT.
775 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
776 // => ext $dst, $src, pos, size
777
778 // The second operand of the shift must be an immediate.
779 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))))
780 return SDValue();
781
782 Pos = CN->getZExtValue();
783
784 // Return if the shifted mask does not start at bit 0 or the sum of its size
785 // and Pos exceeds the word's size.
786 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
787 return SDValue();
788
789 Opc = MipsISD::Ext;
790 NewOperand = FirstOperand.getOperand(0);
791 } else if (FirstOperandOpc == ISD::SHL && Subtarget.hasCnMips()) {
792 // Pattern match CINS.
793 // $dst = and (shl $src , pos), mask
794 // => cins $dst, $src, pos, size
795 // mask is a shifted mask with consecutive 1's, pos = shift amount,
796 // size = population count.
797
798 // The second operand of the shift must be an immediate.
799 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))))
800 return SDValue();
801
802 Pos = CN->getZExtValue();
803
804 if (SMPos != Pos || Pos >= ValTy.getSizeInBits() || SMSize >= 32 ||
805 Pos + SMSize > ValTy.getSizeInBits())
806 return SDValue();
807
808 NewOperand = FirstOperand.getOperand(0);
809 // SMSize is 'location' (position) in this case, not size.
810 SMSize--;
811 Opc = MipsISD::CIns;
812 } else {
813 // Pattern match EXT.
814 // $dst = and $src, (2**size - 1) , if size > 16
815 // => ext $dst, $src, pos, size , pos = 0
816
817 // If the mask is <= 0xffff, andi can be used instead.
818 if (CN->getZExtValue() <= 0xffff)
819 return SDValue();
820
821 // Return if the mask doesn't start at position 0.
822 if (SMPos)
823 return SDValue();
824
825 Opc = MipsISD::Ext;
826 NewOperand = FirstOperand;
827 }
828 return DAG.getNode(Opc, DL, ValTy, NewOperand,
829 DAG.getConstant(Pos, DL, MVT::i32),
830 DAG.getConstant(SMSize, DL, MVT::i32));
831}
832
835 const MipsSubtarget &Subtarget) {
836 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
837 return SDValue();
838
839 SDValue FirstOperand = N->getOperand(0), SecondOperand = N->getOperand(1);
840 unsigned SMPos0, SMSize0, SMPos1, SMSize1;
841 ConstantSDNode *CN, *CN1;
842
843 if ((FirstOperand.getOpcode() == ISD::AND &&
844 SecondOperand.getOpcode() == ISD::SHL) ||
845 (FirstOperand.getOpcode() == ISD::SHL &&
846 SecondOperand.getOpcode() == ISD::AND)) {
847 // Pattern match INS.
848 // $dst = or (and $src1, (2**size0 - 1)), (shl $src2, size0)
849 // ==> ins $src1, $src2, pos, size, pos = size0, size = 32 - pos;
850 // Or:
851 // $dst = or (shl $src2, size0), (and $src1, (2**size0 - 1))
852 // ==> ins $src1, $src2, pos, size, pos = size0, size = 32 - pos;
853 SDValue AndOperand0 = FirstOperand.getOpcode() == ISD::AND
854 ? FirstOperand.getOperand(0)
855 : SecondOperand.getOperand(0);
856 SDValue ShlOperand0 = FirstOperand.getOpcode() == ISD::AND
857 ? SecondOperand.getOperand(0)
858 : FirstOperand.getOperand(0);
859 SDValue AndMask = FirstOperand.getOpcode() == ISD::AND
860 ? FirstOperand.getOperand(1)
861 : SecondOperand.getOperand(1);
862 if (!(CN = dyn_cast<ConstantSDNode>(AndMask)) ||
863 !isShiftedMask_64(CN->getZExtValue(), SMPos0, SMSize0))
864 return SDValue();
865
866 SDValue ShlShift = FirstOperand.getOpcode() == ISD::AND
867 ? SecondOperand.getOperand(1)
868 : FirstOperand.getOperand(1);
869 if (!(CN = dyn_cast<ConstantSDNode>(ShlShift)))
870 return SDValue();
871 uint64_t ShlShiftValue = CN->getZExtValue();
872
873 if (SMPos0 != 0 || SMSize0 != ShlShiftValue)
874 return SDValue();
875
876 SDLoc DL(N);
877 EVT ValTy = N->getValueType(0);
878 SMPos1 = ShlShiftValue;
879 assert(SMPos1 < ValTy.getSizeInBits());
880 SMSize1 = (ValTy == MVT::i64 ? 64 : 32) - SMPos1;
881 return DAG.getNode(MipsISD::Ins, DL, ValTy, ShlOperand0,
882 DAG.getConstant(SMPos1, DL, MVT::i32),
883 DAG.getConstant(SMSize1, DL, MVT::i32), AndOperand0);
884 }
885
886 // See if Op's first operand matches (and $src1 , mask0).
887 if (FirstOperand.getOpcode() != ISD::AND)
888 return SDValue();
889
890 // Pattern match INS.
891 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
892 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
893 // => ins $dst, $src, size, pos, $src1
894 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))) ||
895 !isShiftedMask_64(~CN->getSExtValue(), SMPos0, SMSize0))
896 return SDValue();
897
898 // See if Op's second operand matches (and (shl $src, pos), mask1).
899 if (SecondOperand.getOpcode() == ISD::AND &&
900 SecondOperand.getOperand(0).getOpcode() == ISD::SHL) {
901
902 if (!(CN = dyn_cast<ConstantSDNode>(SecondOperand.getOperand(1))) ||
903 !isShiftedMask_64(CN->getZExtValue(), SMPos1, SMSize1))
904 return SDValue();
905
906 // The shift masks must have the same position and size.
907 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
908 return SDValue();
909
910 SDValue Shl = SecondOperand.getOperand(0);
911
912 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
913 return SDValue();
914
915 unsigned Shamt = CN->getZExtValue();
916
917 // Return if the shift amount and the first bit position of mask are not the
918 // same.
919 EVT ValTy = N->getValueType(0);
920 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
921 return SDValue();
922
923 SDLoc DL(N);
924 return DAG.getNode(MipsISD::Ins, DL, ValTy, Shl.getOperand(0),
925 DAG.getConstant(SMPos0, DL, MVT::i32),
926 DAG.getConstant(SMSize0, DL, MVT::i32),
927 FirstOperand.getOperand(0));
928 } else {
929 // Pattern match DINS.
930 // $dst = or (and $src, mask0), mask1
931 // where mask0 = maskTrailingOnes<uint64_t>(SMSize0) << SMPos0
932 // => dins $dst, $src, pos, size
933 uint64_t Mask = maskTrailingOnes<uint64_t>(SMSize0) << SMPos0;
934 if (~CN->getSExtValue() == (int64_t)Mask &&
935 ((SMSize0 + SMPos0 <= 64 && Subtarget.hasMips64r2()) ||
936 (SMSize0 + SMPos0 <= 32))) {
937 // Check if AND instruction has constant as argument
938 bool isConstCase = SecondOperand.getOpcode() != ISD::AND;
939 if (SecondOperand.getOpcode() == ISD::AND) {
940 if (!(CN1 = dyn_cast<ConstantSDNode>(SecondOperand->getOperand(1))))
941 return SDValue();
942 } else {
943 if (!(CN1 = dyn_cast<ConstantSDNode>(N->getOperand(1))))
944 return SDValue();
945 }
946 // Don't generate INS if constant OR operand doesn't fit into bits
947 // cleared by constant AND operand.
948 if (CN->getSExtValue() & CN1->getSExtValue())
949 return SDValue();
950
951 SDLoc DL(N);
952 EVT ValTy = N->getOperand(0)->getValueType(0);
953 SDValue Const1;
954 SDValue SrlX;
955 if (!isConstCase) {
956 Const1 = DAG.getConstant(SMPos0, DL, MVT::i32);
957 SrlX = DAG.getNode(ISD::SRL, DL, SecondOperand->getValueType(0),
958 SecondOperand, Const1);
959 }
960 return DAG.getNode(
961 MipsISD::Ins, DL, N->getValueType(0),
962 isConstCase
963 ? DAG.getSignedConstant(CN1->getSExtValue() >> SMPos0, DL, ValTy)
964 : SrlX,
965 DAG.getConstant(SMPos0, DL, MVT::i32),
966 DAG.getConstant(ValTy.getSizeInBits() / 8 < 8 ? SMSize0 & 31
967 : SMSize0,
968 DL, MVT::i32),
969 FirstOperand->getOperand(0));
970 }
971 return SDValue();
972 }
973}
974
976 const MipsSubtarget &Subtarget) {
977 // ROOTNode must have a multiplication as an operand for the match to be
978 // successful.
979 if (ROOTNode->getOperand(0).getOpcode() != ISD::MUL &&
980 ROOTNode->getOperand(1).getOpcode() != ISD::MUL)
981 return SDValue();
982
983 // In the case where we have a multiplication as the left operand of
984 // of a subtraction, we can't combine into a MipsISD::MSub node as the
985 // the instruction definition of msub(u) places the multiplication on
986 // on the right.
987 if (ROOTNode->getOpcode() == ISD::SUB &&
988 ROOTNode->getOperand(0).getOpcode() == ISD::MUL)
989 return SDValue();
990
991 // We don't handle vector types here.
992 if (ROOTNode->getValueType(0).isVector())
993 return SDValue();
994
995 // For MIPS64, madd / msub instructions are inefficent to use with 64 bit
996 // arithmetic. E.g.
997 // (add (mul a b) c) =>
998 // let res = (madd (mthi (drotr c 32))x(mtlo c) a b) in
999 // MIPS64: (or (dsll (mfhi res) 32) (dsrl (dsll (mflo res) 32) 32)
1000 // or
1001 // MIPS64R2: (dins (mflo res) (mfhi res) 32 32)
1002 //
1003 // The overhead of setting up the Hi/Lo registers and reassembling the
1004 // result makes this a dubious optimzation for MIPS64. The core of the
1005 // problem is that Hi/Lo contain the upper and lower 32 bits of the
1006 // operand and result.
1007 //
1008 // It requires a chain of 4 add/mul for MIPS64R2 to get better code
1009 // density than doing it naively, 5 for MIPS64. Additionally, using
1010 // madd/msub on MIPS64 requires the operands actually be 32 bit sign
1011 // extended operands, not true 64 bit values.
1012 //
1013 // FIXME: For the moment, disable this completely for MIPS64.
1014 if (Subtarget.hasMips64())
1015 return SDValue();
1016
1017 SDValue Mult = ROOTNode->getOperand(0).getOpcode() == ISD::MUL
1018 ? ROOTNode->getOperand(0)
1019 : ROOTNode->getOperand(1);
1020
1021 SDValue AddOperand = ROOTNode->getOperand(0).getOpcode() == ISD::MUL
1022 ? ROOTNode->getOperand(1)
1023 : ROOTNode->getOperand(0);
1024
1025 // Transform this to a MADD only if the user of this node is the add.
1026 // If there are other users of the mul, this function returns here.
1027 if (!Mult.hasOneUse())
1028 return SDValue();
1029
1030 // maddu and madd are unusual instructions in that on MIPS64 bits 63..31
1031 // must be in canonical form, i.e. sign extended. For MIPS32, the operands
1032 // of the multiply must have 32 or more sign bits, otherwise we cannot
1033 // perform this optimization. We have to check this here as we're performing
1034 // this optimization pre-legalization.
1035 SDValue MultLHS = Mult->getOperand(0);
1036 SDValue MultRHS = Mult->getOperand(1);
1037
1038 bool IsSigned = MultLHS->getOpcode() == ISD::SIGN_EXTEND &&
1039 MultRHS->getOpcode() == ISD::SIGN_EXTEND;
1040 bool IsUnsigned = MultLHS->getOpcode() == ISD::ZERO_EXTEND &&
1041 MultRHS->getOpcode() == ISD::ZERO_EXTEND;
1042
1043 if (!IsSigned && !IsUnsigned)
1044 return SDValue();
1045
1046 // Initialize accumulator.
1047 SDLoc DL(ROOTNode);
1048 SDValue BottomHalf, TopHalf;
1049 std::tie(BottomHalf, TopHalf) =
1050 CurDAG.SplitScalar(AddOperand, DL, MVT::i32, MVT::i32);
1051 SDValue ACCIn =
1052 CurDAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, BottomHalf, TopHalf);
1053
1054 // Create MipsMAdd(u) / MipsMSub(u) node.
1055 bool IsAdd = ROOTNode->getOpcode() == ISD::ADD;
1056 unsigned Opcode = IsAdd ? (IsUnsigned ? MipsISD::MAddu : MipsISD::MAdd)
1057 : (IsUnsigned ? MipsISD::MSubu : MipsISD::MSub);
1058 SDValue MAddOps[3] = {
1059 CurDAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mult->getOperand(0)),
1060 CurDAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mult->getOperand(1)), ACCIn};
1061 SDValue MAdd = CurDAG.getNode(Opcode, DL, MVT::Untyped, MAddOps);
1062
1063 SDValue ResLo = CurDAG.getNode(MipsISD::MFLO, DL, MVT::i32, MAdd);
1064 SDValue ResHi = CurDAG.getNode(MipsISD::MFHI, DL, MVT::i32, MAdd);
1065 SDValue Combined =
1066 CurDAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, ResLo, ResHi);
1067 return Combined;
1068}
1069
1072 const MipsSubtarget &Subtarget) {
1073 // (sub v0 (mul v1, v2)) => (msub v1, v2, v0)
1074 if (DCI.isBeforeLegalizeOps()) {
1075 if (Subtarget.hasMips32() && !Subtarget.hasMips32r6() &&
1076 !Subtarget.inMips16Mode() && N->getValueType(0) == MVT::i64)
1077 return performMADD_MSUBCombine(N, DAG, Subtarget);
1078
1079 return SDValue();
1080 }
1081
1082 return SDValue();
1083}
1084
1087 const MipsSubtarget &Subtarget) {
1088 // (add v0 (mul v1, v2)) => (madd v1, v2, v0)
1089 if (DCI.isBeforeLegalizeOps()) {
1090 if (Subtarget.hasMips32() && !Subtarget.hasMips32r6() &&
1091 !Subtarget.inMips16Mode() && N->getValueType(0) == MVT::i64)
1092 return performMADD_MSUBCombine(N, DAG, Subtarget);
1093
1094 return SDValue();
1095 }
1096
1097 // When loading from a jump table, push the Lo node to the position that
1098 // allows folding it into a load immediate.
1099 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
1100 // (add (add abs_lo(tjt), v1), v0) => (add (add v0, v1), abs_lo(tjt))
1101 SDValue InnerAdd = N->getOperand(1);
1102 SDValue Index = N->getOperand(0);
1103 if (InnerAdd.getOpcode() != ISD::ADD)
1104 std::swap(InnerAdd, Index);
1105 if (InnerAdd.getOpcode() != ISD::ADD)
1106 return SDValue();
1107
1108 SDValue Lo = InnerAdd.getOperand(0);
1109 SDValue Other = InnerAdd.getOperand(1);
1110 if (Lo.getOpcode() != MipsISD::Lo)
1111 std::swap(Lo, Other);
1112
1113 if ((Lo.getOpcode() != MipsISD::Lo) ||
1114 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
1115 return SDValue();
1116
1117 EVT ValTy = N->getValueType(0);
1118 SDLoc DL(N);
1119
1120 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, Index, Other);
1121 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
1122}
1123
1126 const MipsSubtarget &Subtarget) {
1127 // Pattern match CINS.
1128 // $dst = shl (and $src , imm), pos
1129 // => cins $dst, $src, pos, size
1130
1131 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasCnMips())
1132 return SDValue();
1133
1134 SDValue FirstOperand = N->getOperand(0);
1135 unsigned FirstOperandOpc = FirstOperand.getOpcode();
1136 SDValue SecondOperand = N->getOperand(1);
1137 EVT ValTy = N->getValueType(0);
1138 SDLoc DL(N);
1139
1140 uint64_t Pos = 0;
1141 unsigned SMPos, SMSize;
1142 ConstantSDNode *CN;
1143 SDValue NewOperand;
1144
1145 // The second operand of the shift must be an immediate.
1146 if (!(CN = dyn_cast<ConstantSDNode>(SecondOperand)))
1147 return SDValue();
1148
1149 Pos = CN->getZExtValue();
1150
1151 if (Pos >= ValTy.getSizeInBits())
1152 return SDValue();
1153
1154 if (FirstOperandOpc != ISD::AND)
1155 return SDValue();
1156
1157 // AND's second operand must be a shifted mask.
1158 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))) ||
1159 !isShiftedMask_64(CN->getZExtValue(), SMPos, SMSize))
1160 return SDValue();
1161
1162 // Return if the shifted mask does not start at bit 0 or the sum of its size
1163 // and Pos exceeds the word's size.
1164 if (SMPos != 0 || SMSize > 32 || Pos + SMSize > ValTy.getSizeInBits())
1165 return SDValue();
1166
1167 NewOperand = FirstOperand.getOperand(0);
1168 // SMSize is 'location' (position) in this case, not size.
1169 SMSize--;
1170
1171 return DAG.getNode(MipsISD::CIns, DL, ValTy, NewOperand,
1172 DAG.getConstant(Pos, DL, MVT::i32),
1173 DAG.getConstant(SMSize, DL, MVT::i32));
1174}
1175
1178 const MipsSubtarget &Subtarget) {
1179 if (DCI.Level != AfterLegalizeDAG || !Subtarget.isGP64bit()) {
1180 return SDValue();
1181 }
1182
1183 SDValue N0 = N->getOperand(0);
1184 EVT VT = N->getValueType(0);
1185
1186 // Widen AND/OR on canonical words; sign-extended constants preserve this
1187 // form. Require one use to avoid duplicating shared operations and constants.
1188 if (VT == MVT::i64 && N0.getValueType() == MVT::i32 && N0.hasOneUse() &&
1189 (N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR) &&
1190 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1192 SDValue Src = N0.getOperand(0).getOperand(0);
1193 if (Src.getValueType() == MVT::i64 && DAG.ComputeNumSignBits(Src) > 32) {
1194 int64_t Imm = cast<ConstantSDNode>(N0.getOperand(1))->getSExtValue();
1195 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, Src,
1196 DAG.getConstant(Imm, SDLoc(N), VT));
1197 }
1198 }
1199
1200 // Pattern match XOR.
1201 // $dst = sign_extend (xor (trunc $src, i32), imm)
1202 // => $dst = xor (signext_inreg $src, i32), imm
1203 if (N0.getOpcode() == ISD::XOR &&
1204 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1205 N0.getOperand(1).getOpcode() == ISD::Constant) {
1206 SDValue TruncateSource = N0.getOperand(0).getOperand(0);
1207 auto *ConstantOperand = dyn_cast<ConstantSDNode>(N0->getOperand(1));
1208
1209 SDValue FirstOperand =
1210 DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N0), VT, TruncateSource,
1211 DAG.getValueType(N0.getOperand(0).getValueType()));
1212
1213 int64_t ConstImm = ConstantOperand->getSExtValue();
1214 return DAG.getNode(ISD::XOR, SDLoc(N0), VT, FirstOperand,
1215 DAG.getConstant(ConstImm, SDLoc(N0), VT));
1216 }
1217
1218 return SDValue();
1219}
1220
1222 const {
1223 SelectionDAG &DAG = DCI.DAG;
1224 unsigned Opc = N->getOpcode();
1225
1226 switch (Opc) {
1227 default: break;
1228 case ISD::SDIVREM:
1229 case ISD::UDIVREM:
1230 return performDivRemCombine(N, DAG, DCI, Subtarget);
1231 case ISD::SELECT:
1232 return performSELECTCombine(N, DAG, DCI, Subtarget);
1233 case MipsISD::CMovFP_F:
1234 case MipsISD::CMovFP_T:
1235 return performCMovFPCombine(N, DAG, DCI, Subtarget);
1236 case ISD::AND:
1237 return performANDCombine(N, DAG, DCI, Subtarget);
1238 case ISD::OR:
1239 return performORCombine(N, DAG, DCI, Subtarget);
1240 case ISD::ADD:
1241 return performADDCombine(N, DAG, DCI, Subtarget);
1242 case ISD::SHL:
1243 return performSHLCombine(N, DAG, DCI, Subtarget);
1244 case ISD::SUB:
1245 return performSUBCombine(N, DAG, DCI, Subtarget);
1246 case ISD::SIGN_EXTEND:
1247 return performSignExtendCombine(N, DAG, DCI, Subtarget);
1248 }
1249
1250 return SDValue();
1251}
1252
1254 return Subtarget.hasMips32();
1255}
1256
1258 return Subtarget.hasMips32();
1259}
1260
1262 // We can use ANDI+SLTIU as a bit test. Y contains the bit position.
1263 // For MIPSR2 or later, we may be able to use the `ext` instruction or its
1264 // double-word variants.
1265 if (auto *C = dyn_cast<ConstantSDNode>(Y))
1266 return C->getAPIntValue().ule(15);
1267
1268 return false;
1269}
1270
1272 const SDNode *N) const {
1273 assert(((N->getOpcode() == ISD::SHL &&
1274 N->getOperand(0).getOpcode() == ISD::SRL) ||
1275 (N->getOpcode() == ISD::SRL &&
1276 N->getOperand(0).getOpcode() == ISD::SHL)) &&
1277 "Expected shift-shift mask");
1278
1279 if (N->getOperand(0).getValueType().isVector())
1280 return false;
1281 return true;
1282}
1283
1284void
1290
1293{
1294 switch (Op.getOpcode())
1295 {
1296 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
1297 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
1298 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
1299 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
1300 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
1301 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
1302 case ISD::SELECT: return lowerSELECT(Op, DAG);
1303 case ISD::SETCC: return lowerSETCC(Op, DAG);
1304 case ISD::STRICT_FSETCC:
1306 return lowerFSETCC(Op, DAG);
1307 case ISD::VASTART: return lowerVASTART(Op, DAG);
1308 case ISD::VAARG: return lowerVAARG(Op, DAG);
1309 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
1310 case ISD::FABS: return lowerFABS(Op, DAG);
1311 case ISD::FNEG:
1312 return lowerFNEG(Op, DAG);
1313 case ISD::FCANONICALIZE:
1314 return lowerFCANONICALIZE(Op, DAG);
1315 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
1316 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
1317 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
1318 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
1319 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
1320 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
1321 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
1322 case ISD::LOAD: return lowerLOAD(Op, DAG);
1323 case ISD::STORE: return lowerSTORE(Op, DAG);
1324 case ISD::EH_DWARF_CFA: return lowerEH_DWARF_CFA(Op, DAG);
1326 return lowerSTRICT_FP_TO_SINT(Op, DAG);
1327 case ISD::GET_ROUNDING:
1328 return lowerGET_ROUNDING(Op, DAG);
1329 case ISD::SET_ROUNDING:
1330 return lowerSET_ROUNDING(Op, DAG);
1331 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
1333 return lowerREADCYCLECOUNTER(Op, DAG);
1334 case ISD::ConstantFP:
1335 return lowerConstantFP(Op, DAG);
1336 }
1337 return SDValue();
1338}
1339
1340//===----------------------------------------------------------------------===//
1341// Lower helper functions
1342//===----------------------------------------------------------------------===//
1343
1344// addLiveIn - This helper function adds the specified physical register to the
1345// MachineFunction as a live in value. It also creates a corresponding
1346// virtual register for it.
1347static unsigned
1348addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
1349{
1351 MF.getRegInfo().addLiveIn(PReg, VReg);
1352 return VReg;
1353}
1354
1355static MachineBasicBlock *
1356insertDivByZeroTrap(const MipsMCOptions &MCCLOpts, MachineInstr &MI,
1358 bool Is64Bit, const DivByZeroTrapKind TrapKind) {
1359 if (MCCLOpts.mno_check_zero_division)
1360 return &MBB;
1361
1362 MachineOperand &Divisor = MI.getOperand(2);
1363
1364 if (TrapKind == DivByZeroTrapKind::Break) {
1365 // Build instructions:
1366 // MBB:
1367 // bnez $divisor, $zero, SinkMBB
1368 // MI $dst, $dividend, $divisor (delay slot)
1369 //
1370 // BreakMBB:
1371 // break 7
1372 //
1373 // SinkMBB:
1374 // fallthrough
1375 const DebugLoc &DL = MI.getDebugLoc();
1376 const BasicBlock *BB = MBB.getBasicBlock();
1377
1378 // Place all instructions after MI into SinkMBB.
1379 MachineBasicBlock *SinkMBB = MBB.splitAt(MI, true);
1380
1381 // BreakMBB setup.
1382 MachineFunction *MF = MBB.getParent();
1383 MachineBasicBlock *BreakMBB = MF->CreateMachineBasicBlock(BB);
1384 MF->insert(++MBB.getIterator(), BreakMBB);
1385
1386 // Place the branch at the end of the block. Since MI is defined as having
1387 // no side effects in TableGen, the filler will place it in the branch delay
1388 // slot.
1389 BuildMI(&MBB, DL, TII.get(Mips::BNE))
1390 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
1391 .addReg(Mips::R0)
1392 .addMBB(SinkMBB);
1393
1394 // BreakMBB: break 7
1395 BuildMI(BreakMBB, DL, TII.get(Mips::BREAK)).addImm(7).addImm(0);
1396
1397 MBB.addSuccessor(BreakMBB);
1398 BreakMBB->addSuccessor(SinkMBB);
1399
1400 Divisor.setIsKill(false);
1401
1402 return SinkMBB;
1403 }
1404
1405 // Insert instruction "teq $divisor_reg, $zero, 7".
1408 MIB = BuildMI(MBB, std::next(I), MI.getDebugLoc(),
1409 TII.get(TrapKind == DivByZeroTrapKind::TeqMM ? Mips::TEQ_MM
1410 : Mips::TEQ))
1411 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
1412 .addReg(Mips::R0)
1413 .addImm(7);
1414
1415 // Use the 32-bit sub-register if this is a 64-bit division.
1416 if (Is64Bit)
1417 MIB->getOperand(0).setSubReg(Mips::sub_32);
1418
1419 // Clear Divisor's kill flag.
1420 Divisor.setIsKill(false);
1421
1422 // We would normally delete the original instruction here but in this case
1423 // we only needed to inject an additional instruction rather than replace it.
1424
1425 return &MBB;
1426}
1427
1430 MachineBasicBlock *BB) const {
1431 switch (MI.getOpcode()) {
1432 default:
1433 llvm_unreachable("Unexpected instr type to insert");
1434 case Mips::ATOMIC_LOAD_ADD_I8:
1435 return emitAtomicBinaryPartword(MI, BB, 1);
1436 case Mips::ATOMIC_LOAD_ADD_I16:
1437 return emitAtomicBinaryPartword(MI, BB, 2);
1438 case Mips::ATOMIC_LOAD_ADD_I32:
1439 return emitAtomicBinary(MI, BB);
1440 case Mips::ATOMIC_LOAD_ADD_I64:
1441 return emitAtomicBinary(MI, BB);
1442
1443 case Mips::ATOMIC_LOAD_AND_I8:
1444 return emitAtomicBinaryPartword(MI, BB, 1);
1445 case Mips::ATOMIC_LOAD_AND_I16:
1446 return emitAtomicBinaryPartword(MI, BB, 2);
1447 case Mips::ATOMIC_LOAD_AND_I32:
1448 return emitAtomicBinary(MI, BB);
1449 case Mips::ATOMIC_LOAD_AND_I64:
1450 return emitAtomicBinary(MI, BB);
1451
1452 case Mips::ATOMIC_LOAD_OR_I8:
1453 return emitAtomicBinaryPartword(MI, BB, 1);
1454 case Mips::ATOMIC_LOAD_OR_I16:
1455 return emitAtomicBinaryPartword(MI, BB, 2);
1456 case Mips::ATOMIC_LOAD_OR_I32:
1457 return emitAtomicBinary(MI, BB);
1458 case Mips::ATOMIC_LOAD_OR_I64:
1459 return emitAtomicBinary(MI, BB);
1460
1461 case Mips::ATOMIC_LOAD_XOR_I8:
1462 return emitAtomicBinaryPartword(MI, BB, 1);
1463 case Mips::ATOMIC_LOAD_XOR_I16:
1464 return emitAtomicBinaryPartword(MI, BB, 2);
1465 case Mips::ATOMIC_LOAD_XOR_I32:
1466 return emitAtomicBinary(MI, BB);
1467 case Mips::ATOMIC_LOAD_XOR_I64:
1468 return emitAtomicBinary(MI, BB);
1469
1470 case Mips::ATOMIC_LOAD_NAND_I8:
1471 return emitAtomicBinaryPartword(MI, BB, 1);
1472 case Mips::ATOMIC_LOAD_NAND_I16:
1473 return emitAtomicBinaryPartword(MI, BB, 2);
1474 case Mips::ATOMIC_LOAD_NAND_I32:
1475 return emitAtomicBinary(MI, BB);
1476 case Mips::ATOMIC_LOAD_NAND_I64:
1477 return emitAtomicBinary(MI, BB);
1478
1479 case Mips::ATOMIC_LOAD_SUB_I8:
1480 return emitAtomicBinaryPartword(MI, BB, 1);
1481 case Mips::ATOMIC_LOAD_SUB_I16:
1482 return emitAtomicBinaryPartword(MI, BB, 2);
1483 case Mips::ATOMIC_LOAD_SUB_I32:
1484 return emitAtomicBinary(MI, BB);
1485 case Mips::ATOMIC_LOAD_SUB_I64:
1486 return emitAtomicBinary(MI, BB);
1487
1488 case Mips::ATOMIC_SWAP_I8:
1489 return emitAtomicBinaryPartword(MI, BB, 1);
1490 case Mips::ATOMIC_SWAP_I16:
1491 return emitAtomicBinaryPartword(MI, BB, 2);
1492 case Mips::ATOMIC_SWAP_I32:
1493 return emitAtomicBinary(MI, BB);
1494 case Mips::ATOMIC_SWAP_I64:
1495 return emitAtomicBinary(MI, BB);
1496
1497 case Mips::ATOMIC_CMP_SWAP_I8:
1498 return emitAtomicCmpSwapPartword(MI, BB, 1);
1499 case Mips::ATOMIC_CMP_SWAP_I16:
1500 return emitAtomicCmpSwapPartword(MI, BB, 2);
1501 case Mips::ATOMIC_CMP_SWAP_I32:
1502 return emitAtomicCmpSwap(MI, BB);
1503 case Mips::ATOMIC_CMP_SWAP_I64:
1504 return emitAtomicCmpSwap(MI, BB);
1505
1506 case Mips::ATOMIC_LOAD_MIN_I8:
1507 return emitAtomicBinaryPartword(MI, BB, 1);
1508 case Mips::ATOMIC_LOAD_MIN_I16:
1509 return emitAtomicBinaryPartword(MI, BB, 2);
1510 case Mips::ATOMIC_LOAD_MIN_I32:
1511 return emitAtomicBinary(MI, BB);
1512 case Mips::ATOMIC_LOAD_MIN_I64:
1513 return emitAtomicBinary(MI, BB);
1514
1515 case Mips::ATOMIC_LOAD_MAX_I8:
1516 return emitAtomicBinaryPartword(MI, BB, 1);
1517 case Mips::ATOMIC_LOAD_MAX_I16:
1518 return emitAtomicBinaryPartword(MI, BB, 2);
1519 case Mips::ATOMIC_LOAD_MAX_I32:
1520 return emitAtomicBinary(MI, BB);
1521 case Mips::ATOMIC_LOAD_MAX_I64:
1522 return emitAtomicBinary(MI, BB);
1523
1524 case Mips::ATOMIC_LOAD_UMIN_I8:
1525 return emitAtomicBinaryPartword(MI, BB, 1);
1526 case Mips::ATOMIC_LOAD_UMIN_I16:
1527 return emitAtomicBinaryPartword(MI, BB, 2);
1528 case Mips::ATOMIC_LOAD_UMIN_I32:
1529 return emitAtomicBinary(MI, BB);
1530 case Mips::ATOMIC_LOAD_UMIN_I64:
1531 return emitAtomicBinary(MI, BB);
1532
1533 case Mips::ATOMIC_LOAD_UMAX_I8:
1534 return emitAtomicBinaryPartword(MI, BB, 1);
1535 case Mips::ATOMIC_LOAD_UMAX_I16:
1536 return emitAtomicBinaryPartword(MI, BB, 2);
1537 case Mips::ATOMIC_LOAD_UMAX_I32:
1538 return emitAtomicBinary(MI, BB);
1539 case Mips::ATOMIC_LOAD_UMAX_I64:
1540 return emitAtomicBinary(MI, BB);
1541
1542 case Mips::PseudoSDIV:
1543 case Mips::PseudoUDIV:
1544 case Mips::DIV:
1545 case Mips::DIVU:
1546 case Mips::MOD:
1547 case Mips::MODU: {
1548 const DivByZeroTrapKind TrapKind = !Subtarget.hasMips2()
1551 return insertDivByZeroTrap(Subtarget.getMCCLOpts(), MI, *BB,
1552 *Subtarget.getInstrInfo(), false, TrapKind);
1553 }
1554 case Mips::SDIV_MM_Pseudo:
1555 case Mips::UDIV_MM_Pseudo:
1556 case Mips::SDIV_MM:
1557 case Mips::UDIV_MM:
1558 case Mips::DIV_MMR6:
1559 case Mips::DIVU_MMR6:
1560 case Mips::MOD_MMR6:
1561 case Mips::MODU_MMR6:
1562 return insertDivByZeroTrap(Subtarget.getMCCLOpts(), MI, *BB,
1563 *Subtarget.getInstrInfo(), false,
1565 case Mips::PseudoDSDIV:
1566 case Mips::PseudoDUDIV:
1567 case Mips::DDIV:
1568 case Mips::DDIVU:
1569 case Mips::DMOD:
1570 case Mips::DMODU:
1571 return insertDivByZeroTrap(Subtarget.getMCCLOpts(), MI, *BB,
1572 *Subtarget.getInstrInfo(), true,
1574
1575 case Mips::PseudoSELECT_I:
1576 case Mips::PseudoSELECT_I64:
1577 case Mips::PseudoSELECT_S:
1578 case Mips::PseudoSELECT_D32:
1579 case Mips::PseudoSELECT_D64:
1580 return emitPseudoSELECT(MI, BB, false, Mips::BNE);
1581 case Mips::PseudoSELECTFP_F_I:
1582 case Mips::PseudoSELECTFP_F_I64:
1583 case Mips::PseudoSELECTFP_F_S:
1584 case Mips::PseudoSELECTFP_F_D32:
1585 case Mips::PseudoSELECTFP_F_D64:
1586 return emitPseudoSELECT(MI, BB, true, Mips::BC1F);
1587 case Mips::PseudoSELECTFP_T_I:
1588 case Mips::PseudoSELECTFP_T_I64:
1589 case Mips::PseudoSELECTFP_T_S:
1590 case Mips::PseudoSELECTFP_T_D32:
1591 case Mips::PseudoSELECTFP_T_D64:
1592 return emitPseudoSELECT(MI, BB, true, Mips::BC1T);
1593 case Mips::PseudoD_SELECT_I:
1594 case Mips::PseudoD_SELECT_I64:
1595 return emitPseudoD_SELECT(MI, BB);
1596 case Mips::LDR_W:
1597 return emitLDR_W(MI, BB);
1598 case Mips::LDR_D:
1599 return emitLDR_D(MI, BB);
1600 case Mips::STR_W:
1601 return emitSTR_W(MI, BB);
1602 case Mips::STR_D:
1603 return emitSTR_D(MI, BB);
1604 }
1605}
1606
1607// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1608// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
1610MipsTargetLowering::emitAtomicBinary(MachineInstr &MI,
1611 MachineBasicBlock *BB) const {
1612
1613 MachineFunction *MF = BB->getParent();
1614 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1615 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1616 DebugLoc DL = MI.getDebugLoc();
1617
1618 unsigned AtomicOp;
1619 bool NeedsAdditionalReg = false;
1620 switch (MI.getOpcode()) {
1621 case Mips::ATOMIC_LOAD_ADD_I32:
1622 AtomicOp = Mips::ATOMIC_LOAD_ADD_I32_POSTRA;
1623 break;
1624 case Mips::ATOMIC_LOAD_SUB_I32:
1625 AtomicOp = Mips::ATOMIC_LOAD_SUB_I32_POSTRA;
1626 break;
1627 case Mips::ATOMIC_LOAD_AND_I32:
1628 AtomicOp = Mips::ATOMIC_LOAD_AND_I32_POSTRA;
1629 break;
1630 case Mips::ATOMIC_LOAD_OR_I32:
1631 AtomicOp = Mips::ATOMIC_LOAD_OR_I32_POSTRA;
1632 break;
1633 case Mips::ATOMIC_LOAD_XOR_I32:
1634 AtomicOp = Mips::ATOMIC_LOAD_XOR_I32_POSTRA;
1635 break;
1636 case Mips::ATOMIC_LOAD_NAND_I32:
1637 AtomicOp = Mips::ATOMIC_LOAD_NAND_I32_POSTRA;
1638 break;
1639 case Mips::ATOMIC_SWAP_I32:
1640 AtomicOp = Mips::ATOMIC_SWAP_I32_POSTRA;
1641 break;
1642 case Mips::ATOMIC_LOAD_ADD_I64:
1643 AtomicOp = Mips::ATOMIC_LOAD_ADD_I64_POSTRA;
1644 break;
1645 case Mips::ATOMIC_LOAD_SUB_I64:
1646 AtomicOp = Mips::ATOMIC_LOAD_SUB_I64_POSTRA;
1647 break;
1648 case Mips::ATOMIC_LOAD_AND_I64:
1649 AtomicOp = Mips::ATOMIC_LOAD_AND_I64_POSTRA;
1650 break;
1651 case Mips::ATOMIC_LOAD_OR_I64:
1652 AtomicOp = Mips::ATOMIC_LOAD_OR_I64_POSTRA;
1653 break;
1654 case Mips::ATOMIC_LOAD_XOR_I64:
1655 AtomicOp = Mips::ATOMIC_LOAD_XOR_I64_POSTRA;
1656 break;
1657 case Mips::ATOMIC_LOAD_NAND_I64:
1658 AtomicOp = Mips::ATOMIC_LOAD_NAND_I64_POSTRA;
1659 break;
1660 case Mips::ATOMIC_SWAP_I64:
1661 AtomicOp = Mips::ATOMIC_SWAP_I64_POSTRA;
1662 break;
1663 case Mips::ATOMIC_LOAD_MIN_I32:
1664 AtomicOp = Mips::ATOMIC_LOAD_MIN_I32_POSTRA;
1665 NeedsAdditionalReg = true;
1666 break;
1667 case Mips::ATOMIC_LOAD_MAX_I32:
1668 AtomicOp = Mips::ATOMIC_LOAD_MAX_I32_POSTRA;
1669 NeedsAdditionalReg = true;
1670 break;
1671 case Mips::ATOMIC_LOAD_UMIN_I32:
1672 AtomicOp = Mips::ATOMIC_LOAD_UMIN_I32_POSTRA;
1673 NeedsAdditionalReg = true;
1674 break;
1675 case Mips::ATOMIC_LOAD_UMAX_I32:
1676 AtomicOp = Mips::ATOMIC_LOAD_UMAX_I32_POSTRA;
1677 NeedsAdditionalReg = true;
1678 break;
1679 case Mips::ATOMIC_LOAD_MIN_I64:
1680 AtomicOp = Mips::ATOMIC_LOAD_MIN_I64_POSTRA;
1681 NeedsAdditionalReg = true;
1682 break;
1683 case Mips::ATOMIC_LOAD_MAX_I64:
1684 AtomicOp = Mips::ATOMIC_LOAD_MAX_I64_POSTRA;
1685 NeedsAdditionalReg = true;
1686 break;
1687 case Mips::ATOMIC_LOAD_UMIN_I64:
1688 AtomicOp = Mips::ATOMIC_LOAD_UMIN_I64_POSTRA;
1689 NeedsAdditionalReg = true;
1690 break;
1691 case Mips::ATOMIC_LOAD_UMAX_I64:
1692 AtomicOp = Mips::ATOMIC_LOAD_UMAX_I64_POSTRA;
1693 NeedsAdditionalReg = true;
1694 break;
1695 default:
1696 llvm_unreachable("Unknown pseudo atomic for replacement!");
1697 }
1698
1699 Register OldVal = MI.getOperand(0).getReg();
1700 Register Ptr = MI.getOperand(1).getReg();
1701 Register Incr = MI.getOperand(2).getReg();
1702 Register Scratch = RegInfo.createVirtualRegister(RegInfo.getRegClass(OldVal));
1703
1705
1706 // The scratch registers here with the EarlyClobber | Define | Implicit
1707 // flags is used to persuade the register allocator and the machine
1708 // verifier to accept the usage of this register. This has to be a real
1709 // register which has an UNDEF value but is dead after the instruction which
1710 // is unique among the registers chosen for the instruction.
1711
1712 // The EarlyClobber flag has the semantic properties that the operand it is
1713 // attached to is clobbered before the rest of the inputs are read. Hence it
1714 // must be unique among the operands to the instruction.
1715 // The Define flag is needed to coerce the machine verifier that an Undef
1716 // value isn't a problem.
1717 // The Dead flag is needed as the value in scratch isn't used by any other
1718 // instruction. Kill isn't used as Dead is more precise.
1719 // The implicit flag is here due to the interaction between the other flags
1720 // and the machine verifier.
1721
1722 // For correctness purpose, a new pseudo is introduced here. We need this
1723 // new pseudo, so that FastRegisterAllocator does not see an ll/sc sequence
1724 // that is spread over >1 basic blocks. A register allocator which
1725 // introduces (or any codegen infact) a store, can violate the expectations
1726 // of the hardware.
1727 //
1728 // An atomic read-modify-write sequence starts with a linked load
1729 // instruction and ends with a store conditional instruction. The atomic
1730 // read-modify-write sequence fails if any of the following conditions
1731 // occur between the execution of ll and sc:
1732 // * A coherent store is completed by another process or coherent I/O
1733 // module into the block of synchronizable physical memory containing
1734 // the word. The size and alignment of the block is
1735 // implementation-dependent.
1736 // * A coherent store is executed between an LL and SC sequence on the
1737 // same processor to the block of synchornizable physical memory
1738 // containing the word.
1739 //
1740
1741 Register PtrCopy = RegInfo.createVirtualRegister(RegInfo.getRegClass(Ptr));
1742 Register IncrCopy = RegInfo.createVirtualRegister(RegInfo.getRegClass(Incr));
1743
1744 BuildMI(*BB, II, DL, TII->get(Mips::COPY), IncrCopy).addReg(Incr);
1745 BuildMI(*BB, II, DL, TII->get(Mips::COPY), PtrCopy).addReg(Ptr);
1746
1748 BuildMI(*BB, II, DL, TII->get(AtomicOp))
1750 .addReg(PtrCopy)
1751 .addReg(IncrCopy)
1754 if (NeedsAdditionalReg) {
1755 Register Scratch2 =
1756 RegInfo.createVirtualRegister(RegInfo.getRegClass(OldVal));
1759 }
1760
1761 MI.eraseFromParent();
1762
1763 return BB;
1764}
1765
1766MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
1767 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
1768 unsigned SrcReg) const {
1769 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1770 const DebugLoc &DL = MI.getDebugLoc();
1771
1772 if (Subtarget.hasMips32r2() && Size == 1) {
1773 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1774 return BB;
1775 }
1776
1777 if (Subtarget.hasMips32r2() && Size == 2) {
1778 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1779 return BB;
1780 }
1781
1782 MachineFunction *MF = BB->getParent();
1783 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1784 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1785 Register ScrReg = RegInfo.createVirtualRegister(RC);
1786
1787 assert(Size < 32);
1788 int64_t ShiftImm = 32 - (Size * 8);
1789
1790 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1791 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1792
1793 return BB;
1794}
1795
1796MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
1797 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size) const {
1798 assert((Size == 1 || Size == 2) &&
1799 "Unsupported size for EmitAtomicBinaryPartial.");
1800
1801 MachineFunction *MF = BB->getParent();
1802 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1803 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1804 const bool ArePtrs64bit = ABI.ArePtrs64bit();
1805 const TargetRegisterClass *RCp =
1806 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32);
1807 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1808 DebugLoc DL = MI.getDebugLoc();
1809
1810 Register Dest = MI.getOperand(0).getReg();
1811 Register Ptr = MI.getOperand(1).getReg();
1812 Register Incr = MI.getOperand(2).getReg();
1813
1814 Register AlignedAddr = RegInfo.createVirtualRegister(RCp);
1815 Register ShiftAmt = RegInfo.createVirtualRegister(RC);
1816 Register Mask = RegInfo.createVirtualRegister(RC);
1817 Register Mask2 = RegInfo.createVirtualRegister(RC);
1818 Register Incr2 = RegInfo.createVirtualRegister(RC);
1819 Register MaskLSB2 = RegInfo.createVirtualRegister(RCp);
1820 Register PtrLSB2 = RegInfo.createVirtualRegister(RC);
1821 Register MaskUpper = RegInfo.createVirtualRegister(RC);
1822 Register Scratch = RegInfo.createVirtualRegister(RC);
1823 Register Scratch2 = RegInfo.createVirtualRegister(RC);
1824 Register Scratch3 = RegInfo.createVirtualRegister(RC);
1825
1826 unsigned AtomicOp = 0;
1827 bool NeedsAdditionalReg = false;
1828 switch (MI.getOpcode()) {
1829 case Mips::ATOMIC_LOAD_NAND_I8:
1830 AtomicOp = Mips::ATOMIC_LOAD_NAND_I8_POSTRA;
1831 break;
1832 case Mips::ATOMIC_LOAD_NAND_I16:
1833 AtomicOp = Mips::ATOMIC_LOAD_NAND_I16_POSTRA;
1834 break;
1835 case Mips::ATOMIC_SWAP_I8:
1836 AtomicOp = Mips::ATOMIC_SWAP_I8_POSTRA;
1837 break;
1838 case Mips::ATOMIC_SWAP_I16:
1839 AtomicOp = Mips::ATOMIC_SWAP_I16_POSTRA;
1840 break;
1841 case Mips::ATOMIC_LOAD_ADD_I8:
1842 AtomicOp = Mips::ATOMIC_LOAD_ADD_I8_POSTRA;
1843 break;
1844 case Mips::ATOMIC_LOAD_ADD_I16:
1845 AtomicOp = Mips::ATOMIC_LOAD_ADD_I16_POSTRA;
1846 break;
1847 case Mips::ATOMIC_LOAD_SUB_I8:
1848 AtomicOp = Mips::ATOMIC_LOAD_SUB_I8_POSTRA;
1849 break;
1850 case Mips::ATOMIC_LOAD_SUB_I16:
1851 AtomicOp = Mips::ATOMIC_LOAD_SUB_I16_POSTRA;
1852 break;
1853 case Mips::ATOMIC_LOAD_AND_I8:
1854 AtomicOp = Mips::ATOMIC_LOAD_AND_I8_POSTRA;
1855 break;
1856 case Mips::ATOMIC_LOAD_AND_I16:
1857 AtomicOp = Mips::ATOMIC_LOAD_AND_I16_POSTRA;
1858 break;
1859 case Mips::ATOMIC_LOAD_OR_I8:
1860 AtomicOp = Mips::ATOMIC_LOAD_OR_I8_POSTRA;
1861 break;
1862 case Mips::ATOMIC_LOAD_OR_I16:
1863 AtomicOp = Mips::ATOMIC_LOAD_OR_I16_POSTRA;
1864 break;
1865 case Mips::ATOMIC_LOAD_XOR_I8:
1866 AtomicOp = Mips::ATOMIC_LOAD_XOR_I8_POSTRA;
1867 break;
1868 case Mips::ATOMIC_LOAD_XOR_I16:
1869 AtomicOp = Mips::ATOMIC_LOAD_XOR_I16_POSTRA;
1870 break;
1871 case Mips::ATOMIC_LOAD_MIN_I8:
1872 AtomicOp = Mips::ATOMIC_LOAD_MIN_I8_POSTRA;
1873 NeedsAdditionalReg = true;
1874 break;
1875 case Mips::ATOMIC_LOAD_MIN_I16:
1876 AtomicOp = Mips::ATOMIC_LOAD_MIN_I16_POSTRA;
1877 NeedsAdditionalReg = true;
1878 break;
1879 case Mips::ATOMIC_LOAD_MAX_I8:
1880 AtomicOp = Mips::ATOMIC_LOAD_MAX_I8_POSTRA;
1881 NeedsAdditionalReg = true;
1882 break;
1883 case Mips::ATOMIC_LOAD_MAX_I16:
1884 AtomicOp = Mips::ATOMIC_LOAD_MAX_I16_POSTRA;
1885 NeedsAdditionalReg = true;
1886 break;
1887 case Mips::ATOMIC_LOAD_UMIN_I8:
1888 AtomicOp = Mips::ATOMIC_LOAD_UMIN_I8_POSTRA;
1889 NeedsAdditionalReg = true;
1890 break;
1891 case Mips::ATOMIC_LOAD_UMIN_I16:
1892 AtomicOp = Mips::ATOMIC_LOAD_UMIN_I16_POSTRA;
1893 NeedsAdditionalReg = true;
1894 break;
1895 case Mips::ATOMIC_LOAD_UMAX_I8:
1896 AtomicOp = Mips::ATOMIC_LOAD_UMAX_I8_POSTRA;
1897 NeedsAdditionalReg = true;
1898 break;
1899 case Mips::ATOMIC_LOAD_UMAX_I16:
1900 AtomicOp = Mips::ATOMIC_LOAD_UMAX_I16_POSTRA;
1901 NeedsAdditionalReg = true;
1902 break;
1903 default:
1904 llvm_unreachable("Unknown subword atomic pseudo for expansion!");
1905 }
1906
1907 // insert new blocks after the current block
1908 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1909 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1911 MF->insert(It, exitMBB);
1912
1913 // Transfer the remainder of BB and its successor edges to exitMBB.
1914 exitMBB->splice(exitMBB->begin(), BB,
1915 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1917
1919
1920 // thisMBB:
1921 // addiu masklsb2,$0,-4 # 0xfffffffc
1922 // and alignedaddr,ptr,masklsb2
1923 // andi ptrlsb2,ptr,3
1924 // sll shiftamt,ptrlsb2,3
1925 // ori maskupper,$0,255 # 0xff
1926 // sll mask,maskupper,shiftamt
1927 // nor mask2,$0,mask
1928 // sll incr2,incr,shiftamt
1929
1930 int64_t MaskImm = (Size == 1) ? 255 : 65535;
1931 BuildMI(BB, DL, TII->get(ABI.GetPtrAddiuOp()), MaskLSB2)
1932 .addReg(ABI.GetNullPtr()).addImm(-4);
1933 BuildMI(BB, DL, TII->get(ABI.GetPtrAndOp()), AlignedAddr)
1934 .addReg(Ptr).addReg(MaskLSB2);
1935 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2)
1936 .addReg(Ptr, {}, ArePtrs64bit ? Mips::sub_32 : 0)
1937 .addImm(3);
1938 if (Subtarget.isLittle()) {
1939 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1940 } else {
1941 Register Off = RegInfo.createVirtualRegister(RC);
1942 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1943 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1944 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1945 }
1946 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
1947 .addReg(Mips::R0)
1948 .addImm(MaskImm);
1949 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
1950 .addReg(MaskUpper).addReg(ShiftAmt);
1951 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::R0).addReg(Mask);
1952 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
1953
1954
1955 // The purposes of the flags on the scratch registers is explained in
1956 // emitAtomicBinary. In summary, we need a scratch register which is going to
1957 // be undef, that is unique among registers chosen for the instruction.
1958
1959 MachineInstrBuilder MIB =
1960 BuildMI(BB, DL, TII->get(AtomicOp))
1962 .addReg(AlignedAddr)
1963 .addReg(Incr2)
1964 .addReg(Mask)
1965 .addReg(Mask2)
1966 .addReg(ShiftAmt)
1973 if (NeedsAdditionalReg) {
1974 Register Scratch4 = RegInfo.createVirtualRegister(RC);
1977 }
1978
1979 MI.eraseFromParent(); // The instruction is gone now.
1980
1981 return exitMBB;
1982}
1983
1984// Lower atomic compare and swap to a pseudo instruction, taking care to
1985// define a scratch register for the pseudo instruction's expansion. The
1986// instruction is expanded after the register allocator as to prevent
1987// the insertion of stores between the linked load and the store conditional.
1988
1990MipsTargetLowering::emitAtomicCmpSwap(MachineInstr &MI,
1991 MachineBasicBlock *BB) const {
1992
1993 assert((MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32 ||
1994 MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I64) &&
1995 "Unsupported atomic pseudo for EmitAtomicCmpSwap.");
1996
1997 const unsigned Size = MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32 ? 4 : 8;
1998
1999 MachineFunction *MF = BB->getParent();
2000 MachineRegisterInfo &MRI = MF->getRegInfo();
2002 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
2003 DebugLoc DL = MI.getDebugLoc();
2004
2005 unsigned AtomicOp = MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32
2006 ? Mips::ATOMIC_CMP_SWAP_I32_POSTRA
2007 : Mips::ATOMIC_CMP_SWAP_I64_POSTRA;
2008 Register Dest = MI.getOperand(0).getReg();
2009 Register Ptr = MI.getOperand(1).getReg();
2010 Register OldVal = MI.getOperand(2).getReg();
2011 Register NewVal = MI.getOperand(3).getReg();
2012
2013 Register Scratch = MRI.createVirtualRegister(RC);
2015
2016 // We need to create copies of the various registers and kill them at the
2017 // atomic pseudo. If the copies are not made, when the atomic is expanded
2018 // after fast register allocation, the spills will end up outside of the
2019 // blocks that their values are defined in, causing livein errors.
2020
2022 Register OldValCopy = MRI.createVirtualRegister(MRI.getRegClass(OldVal));
2023 Register NewValCopy = MRI.createVirtualRegister(MRI.getRegClass(NewVal));
2024
2025 BuildMI(*BB, II, DL, TII->get(Mips::COPY), PtrCopy).addReg(Ptr);
2026 BuildMI(*BB, II, DL, TII->get(Mips::COPY), OldValCopy).addReg(OldVal);
2027 BuildMI(*BB, II, DL, TII->get(Mips::COPY), NewValCopy).addReg(NewVal);
2028
2029 // The purposes of the flags on the scratch registers is explained in
2030 // emitAtomicBinary. In summary, we need a scratch register which is going to
2031 // be undef, that is unique among registers chosen for the instruction.
2032
2033 BuildMI(*BB, II, DL, TII->get(AtomicOp))
2035 .addReg(PtrCopy)
2036 .addReg(OldValCopy)
2037 .addReg(NewValCopy)
2040
2041 MI.eraseFromParent(); // The instruction is gone now.
2042
2043 return BB;
2044}
2045
2046MachineBasicBlock *MipsTargetLowering::emitAtomicCmpSwapPartword(
2047 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size) const {
2048 assert((Size == 1 || Size == 2) &&
2049 "Unsupported size for EmitAtomicCmpSwapPartial.");
2050
2051 MachineFunction *MF = BB->getParent();
2052 MachineRegisterInfo &RegInfo = MF->getRegInfo();
2053 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
2054 const bool ArePtrs64bit = ABI.ArePtrs64bit();
2055 const TargetRegisterClass *RCp =
2056 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32);
2057 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
2058 DebugLoc DL = MI.getDebugLoc();
2059
2060 Register Dest = MI.getOperand(0).getReg();
2061 Register Ptr = MI.getOperand(1).getReg();
2062 Register CmpVal = MI.getOperand(2).getReg();
2063 Register NewVal = MI.getOperand(3).getReg();
2064
2065 Register AlignedAddr = RegInfo.createVirtualRegister(RCp);
2066 Register ShiftAmt = RegInfo.createVirtualRegister(RC);
2067 Register Mask = RegInfo.createVirtualRegister(RC);
2068 Register Mask2 = RegInfo.createVirtualRegister(RC);
2069 Register ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
2070 Register ShiftedNewVal = RegInfo.createVirtualRegister(RC);
2071 Register MaskLSB2 = RegInfo.createVirtualRegister(RCp);
2072 Register PtrLSB2 = RegInfo.createVirtualRegister(RC);
2073 Register MaskUpper = RegInfo.createVirtualRegister(RC);
2074 Register MaskedCmpVal = RegInfo.createVirtualRegister(RC);
2075 Register MaskedNewVal = RegInfo.createVirtualRegister(RC);
2076 unsigned AtomicOp = MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I8
2077 ? Mips::ATOMIC_CMP_SWAP_I8_POSTRA
2078 : Mips::ATOMIC_CMP_SWAP_I16_POSTRA;
2079
2080 // The scratch registers here with the EarlyClobber | Define | Dead | Implicit
2081 // flags are used to coerce the register allocator and the machine verifier to
2082 // accept the usage of these registers.
2083 // The EarlyClobber flag has the semantic properties that the operand it is
2084 // attached to is clobbered before the rest of the inputs are read. Hence it
2085 // must be unique among the operands to the instruction.
2086 // The Define flag is needed to coerce the machine verifier that an Undef
2087 // value isn't a problem.
2088 // The Dead flag is needed as the value in scratch isn't used by any other
2089 // instruction. Kill isn't used as Dead is more precise.
2090 Register Scratch = RegInfo.createVirtualRegister(RC);
2091 Register Scratch2 = RegInfo.createVirtualRegister(RC);
2092
2093 // insert new blocks after the current block
2094 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2095 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
2097 MF->insert(It, exitMBB);
2098
2099 // Transfer the remainder of BB and its successor edges to exitMBB.
2100 exitMBB->splice(exitMBB->begin(), BB,
2101 std::next(MachineBasicBlock::iterator(MI)), BB->end());
2103
2105
2106 // thisMBB:
2107 // addiu masklsb2,$0,-4 # 0xfffffffc
2108 // and alignedaddr,ptr,masklsb2
2109 // andi ptrlsb2,ptr,3
2110 // xori ptrlsb2,ptrlsb2,3 # Only for BE
2111 // sll shiftamt,ptrlsb2,3
2112 // ori maskupper,$0,255 # 0xff
2113 // sll mask,maskupper,shiftamt
2114 // nor mask2,$0,mask
2115 // andi maskedcmpval,cmpval,255
2116 // sll shiftedcmpval,maskedcmpval,shiftamt
2117 // andi maskednewval,newval,255
2118 // sll shiftednewval,maskednewval,shiftamt
2119 int64_t MaskImm = (Size == 1) ? 255 : 65535;
2120 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::DADDiu : Mips::ADDiu), MaskLSB2)
2121 .addReg(ABI.GetNullPtr()).addImm(-4);
2122 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::AND64 : Mips::AND), AlignedAddr)
2123 .addReg(Ptr).addReg(MaskLSB2);
2124 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2)
2125 .addReg(Ptr, {}, ArePtrs64bit ? Mips::sub_32 : 0)
2126 .addImm(3);
2127 if (Subtarget.isLittle()) {
2128 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
2129 } else {
2130 Register Off = RegInfo.createVirtualRegister(RC);
2131 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
2132 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
2133 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
2134 }
2135 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
2136 .addReg(Mips::R0)
2137 .addImm(MaskImm);
2138 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
2139 .addReg(MaskUpper).addReg(ShiftAmt);
2140 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::R0).addReg(Mask);
2141 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
2142 .addReg(CmpVal).addImm(MaskImm);
2143 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
2144 .addReg(MaskedCmpVal).addReg(ShiftAmt);
2145 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
2146 .addReg(NewVal).addImm(MaskImm);
2147 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
2148 .addReg(MaskedNewVal).addReg(ShiftAmt);
2149
2150 // The purposes of the flags on the scratch registers are explained in
2151 // emitAtomicBinary. In summary, we need a scratch register which is going to
2152 // be undef, that is unique among the register chosen for the instruction.
2153
2154 BuildMI(BB, DL, TII->get(AtomicOp))
2156 .addReg(AlignedAddr)
2157 .addReg(Mask)
2158 .addReg(ShiftedCmpVal)
2159 .addReg(Mask2)
2160 .addReg(ShiftedNewVal)
2161 .addReg(ShiftAmt)
2166
2167 MI.eraseFromParent(); // The instruction is gone now.
2168
2169 return exitMBB;
2170}
2171
2172SDValue MipsTargetLowering::lowerConstantFP(SDValue Op,
2173 SelectionDAG &DAG) const {
2174 EVT VT = Op.getValueType();
2175 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
2176 const APFloat &FPVal = CFP->getValueAPF();
2177
2178 if (FPVal.isZero())
2179 return SDValue();
2180
2181 SDLoc DL(CFP);
2182 APInt INTVal = FPVal.bitcastToAPInt();
2183 switch (VT.getSimpleVT().SimpleTy) {
2184 default:
2185 llvm_unreachable("Unknown floating point type!");
2186 break;
2187 case MVT::f64: {
2188 if (!Subtarget.hasMTHC1() || !Subtarget.hasMips32r2())
2189 return SDValue();
2190 uint64_t Bits = INTVal.getZExtValue();
2191 uint32_t Lo = Bits & 0xFFFFFFFF;
2192 if (Lo != 0 || Bits == 0)
2193 return SDValue();
2194
2195 // TODO: DAG.getConstant(0) should be optimized to avoid generate an extra
2196 // instr `addiu $x, $zero, 0`.
2197 SDValue Low =
2198 DAG.getCopyFromReg(DAG.getEntryNode(), DL, Mips::R0, MVT::i32);
2199 SDValue Hi = DAG.getConstant(INTVal.extractBits(32, 32), DL, MVT::i32);
2200 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Low, Hi);
2201 }
2202 }
2203
2204 return SDValue();
2205}
2206
2207SDValue MipsTargetLowering::lowerREADCYCLECOUNTER(SDValue Op,
2208 SelectionDAG &DAG) const {
2210 SDLoc DL(Op);
2212 unsigned RdhwrOpc, DestReg;
2213 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2214
2215 if (PtrVT == MVT::i64) {
2216 RdhwrOpc = Mips::RDHWR64;
2217 DestReg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
2218 SDNode *Rdhwr = DAG.getMachineNode(RdhwrOpc, DL, MVT::i64, MVT::Glue,
2219 DAG.getRegister(Mips::HWR2, MVT::i32),
2220 DAG.getTargetConstant(0, DL, MVT::i32));
2221 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), DL, DestReg,
2222 SDValue(Rdhwr, 0), SDValue(Rdhwr, 1));
2223 SDValue ResNode =
2224 DAG.getCopyFromReg(Chain, DL, DestReg, MVT::i64, Chain.getValue(1));
2225 Results.push_back(ResNode);
2226 Results.push_back(ResNode.getValue(1));
2227 } else {
2228 RdhwrOpc = Mips::RDHWR;
2229 DestReg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
2230 SDNode *Rdhwr = DAG.getMachineNode(RdhwrOpc, DL, MVT::i32, MVT::Glue,
2231 DAG.getRegister(Mips::HWR2, MVT::i32),
2232 DAG.getTargetConstant(0, DL, MVT::i32));
2233 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), DL, DestReg,
2234 SDValue(Rdhwr, 0), SDValue(Rdhwr, 1));
2235 SDValue ResNode =
2236 DAG.getCopyFromReg(Chain, DL, DestReg, MVT::i32, Chain.getValue(1));
2237 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, ResNode,
2238 DAG.getConstant(0, DL, MVT::i32)));
2239 Results.push_back(ResNode.getValue(1));
2240 }
2241
2242 return DAG.getMergeValues(Results, DL);
2243}
2244
2245SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
2246 // The first operand is the chain, the second is the condition, the third is
2247 // the block to branch to if the condition is true.
2248 SDValue Chain = Op.getOperand(0);
2249 SDValue Dest = Op.getOperand(2);
2250 SDLoc DL(Op);
2251
2252 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
2253 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
2254
2255 // Return if flag is not set by a floating point comparison.
2256 if (CondRes.getOpcode() != MipsISD::FPCmp)
2257 return Op;
2258
2259 SDValue CCNode = CondRes.getOperand(2);
2262 SDValue BrCode = DAG.getConstant(Opc, DL, MVT::i32);
2263 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
2264 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
2265 FCC0, Dest, CondRes);
2266}
2267
2268SDValue MipsTargetLowering::
2269lowerSELECT(SDValue Op, SelectionDAG &DAG) const
2270{
2271 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
2272 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
2273
2274 // Return if flag is not set by a floating point comparison.
2275 if (Cond.getOpcode() != MipsISD::FPCmp)
2276 return Op;
2277
2278 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
2279 SDLoc(Op));
2280}
2281
2282SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
2283 // cmp.cond.fmt writes 0 or -1, while scalar booleans are 0 or 1. Take the
2284 // low bit of the mask as the SETCC result. Selects and branches test only
2285 // the low bit or whether the value is zero, so they can use the mask itself.
2286 if (Subtarget.hasMips32r6()) {
2287 SDLoc DL(Op);
2288 SDValue Mask = DAG.getNode(MipsISD::R6FCmp, DL, MVT::i32, Op.getOperand(0),
2289 Op.getOperand(1), Op.getOperand(2));
2290 SDValue Res = DAG.getNode(ISD::AND, DL, MVT::i32, Mask,
2291 DAG.getConstant(1, DL, MVT::i32));
2292 return DAG.getZExtOrTrunc(Res, DL, Op.getValueType());
2293 }
2294
2295 SDValue Cond = createFPCmp(DAG, Op);
2296
2297 assert(Cond.getOpcode() == MipsISD::FPCmp &&
2298 "Floating point operand expected.");
2299
2300 SDLoc DL(Op);
2301 SDValue True = DAG.getConstant(1, DL, MVT::i32);
2302 SDValue False = DAG.getConstant(0, DL, MVT::i32);
2303
2304 return createCMovFP(DAG, Cond, True, False, DL);
2305}
2306
2307SDValue MipsTargetLowering::lowerFSETCC(SDValue Op, SelectionDAG &DAG) const {
2308 SDLoc DL(Op);
2309 SDValue Chain = Op.getOperand(0);
2310 SDValue LHS = Op.getOperand(1);
2311 SDValue RHS = Op.getOperand(2);
2312 ISD::CondCode CC = Op.getOperandCondCode(3);
2313 bool IsSignaling = Op.getOpcode() == ISD::STRICT_FSETCCS;
2314
2315 if (Subtarget.hasMips32r6()) {
2316 // The patterns only cover the ordered and unordered condition codes.
2317 // Treat the ones that do not care about NaN as ordered.
2318 switch (CC) {
2319 case ISD::SETGT:
2320 case ISD::SETGE:
2321 std::swap(LHS, RHS);
2323 break;
2324 case ISD::SETEQ:
2325 CC = ISD::SETOEQ;
2326 break;
2327 case ISD::SETLT:
2328 CC = ISD::SETOLT;
2329 break;
2330 case ISD::SETLE:
2331 CC = ISD::SETOLE;
2332 break;
2333 default:
2334 break;
2335 }
2336
2337 // CMP.cond.fmt writes an all-ones or all-zeros mask to an FPR. As for
2338 // SETCC, take the low bit of its low word as the result.
2339 EVT VT = LHS.getValueType();
2340 SDValue Cmp = DAG.getNode(
2341 IsSignaling ? MipsISD::STRICT_FPCmpsR6 : MipsISD::STRICT_FPCmpR6, DL,
2342 DAG.getVTList(VT, MVT::Other), Chain, LHS, RHS, DAG.getCondCode(CC));
2343 SDValue Mask = Cmp;
2344 if (VT == MVT::f64)
2345 Mask = DAG.getTargetExtractSubreg(Mips::sub_lo, DL, MVT::f32, Cmp);
2346 SDValue Res = DAG.getNode(ISD::AND, DL, MVT::i32,
2347 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Mask),
2348 DAG.getConstant(1, DL, MVT::i32));
2349 Res = DAG.getZExtOrTrunc(Res, DL, Op.getValueType());
2350 return DAG.getMergeValues({Res, Cmp.getValue(1)}, DL);
2351 }
2352
2354
2355 // A signaling compare must raise invalid for a quiet NaN operand as well.
2356 // c.cond.fmt does that for the conditions with cond<3> set, which are the
2357 // quiet ones plus 8. The R5900 FPU does not implement them.
2358 if (IsSignaling && !Subtarget.isR5900())
2359 FCC = static_cast<Mips::CondCode>(FCC + (Mips::FCOND_SF - Mips::FCOND_F));
2360
2361 // Keep the compare on the chain so that it is not reordered with respect
2362 // to other operations that access the FP environment, or removed when its
2363 // result is unused.
2364 SDValue Cmp = DAG.getNode(MipsISD::STRICT_FPCmp, DL,
2365 DAG.getVTList(MVT::Other, MVT::Glue), Chain, LHS,
2366 RHS, DAG.getConstant(FCC, DL, MVT::i32));
2367 SDValue True = DAG.getConstant(1, DL, MVT::i32);
2368 SDValue False = DAG.getConstant(0, DL, MVT::i32);
2369 SDValue CMovFP = createCMovFP(DAG, Cmp.getValue(1), True, False, DL);
2370
2371 return DAG.getMergeValues({CMovFP, Cmp.getValue(0)}, DL);
2372}
2373
2374SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
2375 SelectionDAG &DAG) const {
2376 EVT Ty = Op.getValueType();
2377 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
2378 const GlobalValue *GV = N->getGlobal();
2379
2380 if (GV->hasDLLImportStorageClass()) {
2381 assert(Subtarget.isTargetWindows() &&
2382 "Windows is the only supported COFF target");
2383 return getDllimportVariable(
2384 N, SDLoc(N), Ty, DAG, DAG.getEntryNode(),
2386 }
2387
2388 if (!isPositionIndependent()) {
2389 const MipsTargetObjectFile *TLOF =
2390 static_cast<const MipsTargetObjectFile *>(
2392 const GlobalObject *GO = GV->getAliaseeObject();
2393 if (Subtarget.useSmallSection() && GO && TLOF->IsGlobalInSmallSection(GO))
2394 // %gp_rel relocation
2395 return getAddrGPRel(N, SDLoc(N), Ty, DAG, ABI.IsN64());
2396
2397 // %hi/%lo relocation
2398 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2399 // %highest/%higher/%hi/%lo relocation
2400 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
2401 }
2402
2403 // Every other architecture would use shouldAssumeDSOLocal in here, but
2404 // mips is special.
2405 // * In PIC code mips requires got loads even for local statics!
2406 // * To save on got entries, for local statics the got entry contains the
2407 // page and an additional add instruction takes care of the low bits.
2408 // * It is legal to access a hidden symbol with a non hidden undefined,
2409 // so one cannot guarantee that all access to a hidden symbol will know
2410 // it is hidden.
2411 // * Mips linkers don't support creating a page and a full got entry for
2412 // the same symbol.
2413 // * Given all that, we have to use a full got entry for hidden symbols :-(
2414 if (GV->hasLocalLinkage())
2415 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
2416
2417 if (Subtarget.useXGOT())
2418 return getAddrGlobalLargeGOT(
2419 N, SDLoc(N), Ty, DAG, MipsII::MO_GOT_HI16, MipsII::MO_GOT_LO16,
2420 DAG.getEntryNode(),
2422
2423 return getAddrGlobal(
2424 N, SDLoc(N), Ty, DAG,
2425 (ABI.IsN32() || ABI.IsN64()) ? MipsII::MO_GOT_DISP : MipsII::MO_GOT,
2427}
2428
2429SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
2430 SelectionDAG &DAG) const {
2431 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
2432 EVT Ty = Op.getValueType();
2433
2434 if (!isPositionIndependent())
2435 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2436 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
2437
2438 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
2439}
2440
2441SDValue MipsTargetLowering::
2442lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
2443{
2444 // If the relocation model is PIC, use the General Dynamic TLS Model or
2445 // Local Dynamic TLS model, otherwise use the Initial Exec or
2446 // Local Exec TLS Model.
2447
2448 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2449 if (DAG.getTarget().useEmulatedTLS())
2450 return LowerToTLSEmulatedModel(GA, DAG);
2451
2452 SDLoc DL(GA);
2453 const GlobalValue *GV = GA->getGlobal();
2454 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2455
2457
2458 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
2459 // General Dynamic and Local Dynamic TLS Model.
2460 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
2461 : MipsII::MO_TLSGD;
2462
2463 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
2464 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
2465 getGlobalReg(DAG, PtrVT), TGA);
2466 unsigned PtrSize = PtrVT.getSizeInBits();
2467 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
2468
2469 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
2470
2472 Args.emplace_back(Argument, PtrTy);
2473
2474 TargetLowering::CallLoweringInfo CLI(DAG);
2475 CLI.setDebugLoc(DL)
2476 .setChain(DAG.getEntryNode())
2477 .setLibCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args));
2478 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
2479
2480 SDValue Ret = CallResult.first;
2481
2482 if (model != TLSModel::LocalDynamic)
2483 return Ret;
2484
2485 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2487 SDValue Hi = DAG.getNode(MipsISD::TlsHi, DL, PtrVT, TGAHi);
2488 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2490 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
2491 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
2492 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
2493 }
2494
2496 if (model == TLSModel::InitialExec) {
2497 // Initial Exec TLS Model
2498 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2500 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
2501 TGA);
2502 Offset =
2503 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), TGA, MachinePointerInfo());
2504 } else {
2505 // Local Exec TLS Model
2506 assert(model == TLSModel::LocalExec);
2507 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2509 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2511 SDValue Hi = DAG.getNode(MipsISD::TlsHi, DL, PtrVT, TGAHi);
2512 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
2513 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
2514 }
2515
2516 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
2517 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
2518}
2519
2520SDValue MipsTargetLowering::
2521lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
2522{
2523 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
2524 EVT Ty = Op.getValueType();
2525
2526 if (!isPositionIndependent())
2527 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2528 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
2529
2530 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
2531}
2532
2533SDValue MipsTargetLowering::
2534lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
2535{
2536 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
2537 EVT Ty = Op.getValueType();
2538
2539 if (!isPositionIndependent()) {
2540 const MipsTargetObjectFile *TLOF =
2541 static_cast<const MipsTargetObjectFile *>(
2543
2544 if (TLOF->IsConstantInSmallSection(DAG.getDataLayout(), N->getConstVal(),
2546 // %gp_rel relocation
2547 return getAddrGPRel(N, SDLoc(N), Ty, DAG, ABI.IsN64());
2548
2549 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2550 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
2551 }
2552
2553 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
2554}
2555
2556SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
2558 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
2559
2560 SDLoc DL(Op);
2561 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
2563
2564 // vastart just stores the address of the VarArgsFrameIndex slot into the
2565 // memory location argument.
2566 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
2567 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
2568 MachinePointerInfo(SV));
2569}
2570
2571SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
2572 SDNode *Node = Op.getNode();
2573 EVT VT = Node->getValueType(0);
2574 SDValue Chain = Node->getOperand(0);
2575 SDValue VAListPtr = Node->getOperand(1);
2576 const Align Align =
2577 llvm::MaybeAlign(Node->getConstantOperandVal(3)).valueOrOne();
2578 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2579 SDLoc DL(Node);
2580 unsigned ArgSlotSizeInBytes = (ABI.IsN32() || ABI.IsN64()) ? 8 : 4;
2581
2582 SDValue VAListLoad = DAG.getLoad(getPointerTy(DAG.getDataLayout()), DL, Chain,
2583 VAListPtr, MachinePointerInfo(SV));
2584 SDValue VAList = VAListLoad;
2585
2586 // Re-align the pointer if necessary.
2587 // It should only ever be necessary for 64-bit types on O32 since the minimum
2588 // argument alignment is the same as the maximum type alignment for N32/N64.
2589 //
2590 // FIXME: We currently align too often. The code generator doesn't notice
2591 // when the pointer is still aligned from the last va_arg (or pair of
2592 // va_args for the i64 on O32 case).
2593 if (Align > getMinStackArgumentAlignment()) {
2594 VAList = DAG.getNode(
2595 ISD::ADD, DL, VAList.getValueType(), VAList,
2596 DAG.getConstant(Align.value() - 1, DL, VAList.getValueType()));
2597
2598 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
2599 DAG.getSignedConstant(-(int64_t)Align.value(), DL,
2600 VAList.getValueType()));
2601 }
2602
2603 // Increment the pointer, VAList, to the next vaarg.
2604 auto &TD = DAG.getDataLayout();
2605 unsigned ArgSizeInBytes =
2607 SDValue Tmp3 =
2608 DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
2609 DAG.getConstant(alignTo(ArgSizeInBytes, ArgSlotSizeInBytes),
2610 DL, VAList.getValueType()));
2611 // Store the incremented VAList to the legalized pointer
2612 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
2613 MachinePointerInfo(SV));
2614
2615 // In big-endian mode we must adjust the pointer when the load size is smaller
2616 // than the argument slot size. We must also reduce the known alignment to
2617 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
2618 // the correct half of the slot, and reduce the alignment from 8 (slot
2619 // alignment) down to 4 (type alignment).
2620 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
2621 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
2622 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
2623 DAG.getIntPtrConstant(Adjustment, DL));
2624 }
2625 // Load the actual argument out of the pointer VAList
2626 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo());
2627}
2628
2630 bool HasExtractInsert) {
2631 EVT TyX = Op.getOperand(0).getValueType();
2632 EVT TyY = Op.getOperand(1).getValueType();
2633 SDLoc DL(Op);
2634 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
2635 SDValue Const31 = DAG.getConstant(31, DL, MVT::i32);
2636 SDValue Res;
2637
2638 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2639 // to i32.
2640 SDValue X = (TyX == MVT::f32) ?
2641 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
2642 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2643 Const1);
2644 SDValue Y = (TyY == MVT::f32) ?
2645 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
2646 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
2647 Const1);
2648
2649 if (HasExtractInsert) {
2650 // ext E, Y, 31, 1 ; extract bit31 of Y
2651 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
2652 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
2653 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
2654 } else {
2655 // sll SllX, X, 1
2656 // srl SrlX, SllX, 1
2657 // srl SrlY, Y, 31
2658 // sll SllY, SrlX, 31
2659 // or Or, SrlX, SllY
2660 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2661 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2662 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
2663 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
2664 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
2665 }
2666
2667 if (TyX == MVT::f32)
2668 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
2669
2670 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2671 Op.getOperand(0),
2672 DAG.getConstant(0, DL, MVT::i32));
2673 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
2674}
2675
2677 bool HasExtractInsert) {
2678 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
2679 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
2680 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
2681 SDLoc DL(Op);
2682 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
2683
2684 // Bitcast to integer nodes.
2685 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
2686 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
2687
2688 if (HasExtractInsert) {
2689 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
2690 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
2691 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
2692 DAG.getConstant(WidthY - 1, DL, MVT::i32), Const1);
2693
2694 if (WidthX > WidthY)
2695 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
2696 else if (WidthY > WidthX)
2697 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
2698
2699 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
2700 DAG.getConstant(WidthX - 1, DL, MVT::i32), Const1,
2701 X);
2702 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
2703 }
2704
2705 // (d)sll SllX, X, 1
2706 // (d)srl SrlX, SllX, 1
2707 // (d)srl SrlY, Y, width(Y)-1
2708 // (d)sll SllY, SrlX, width(Y)-1
2709 // or Or, SrlX, SllY
2710 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
2711 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
2712 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
2713 DAG.getConstant(WidthY - 1, DL, MVT::i32));
2714
2715 if (WidthX > WidthY)
2716 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
2717 else if (WidthY > WidthX)
2718 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
2719
2720 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
2721 DAG.getConstant(WidthX - 1, DL, MVT::i32));
2722 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
2723 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
2724}
2725
2726SDValue
2727MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
2728 if (Subtarget.isGP64bit())
2729 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
2730
2731 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
2732}
2733
2734SDValue MipsTargetLowering::lowerFABS32(SDValue Op, SelectionDAG &DAG,
2735 bool HasExtractInsert) const {
2736 SDLoc DL(Op);
2737 SDValue Res, Const1 = DAG.getConstant(1, DL, MVT::i32);
2738
2739 if (Op->getFlags().hasNoNaNs() || Subtarget.inAbs2008Mode())
2740 return DAG.getNode(MipsISD::FAbs, DL, Op.getValueType(), Op.getOperand(0));
2741
2742 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2743 // to i32.
2744 SDValue X = (Op.getValueType() == MVT::f32)
2745 ? DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0))
2746 : DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2747 Op.getOperand(0), Const1);
2748
2749 // Clear MSB.
2750 if (HasExtractInsert)
2751 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
2752 DAG.getRegister(Mips::R0, MVT::i32),
2753 DAG.getConstant(31, DL, MVT::i32), Const1, X);
2754 else {
2755 // TODO: Provide DAG patterns which transform (and x, cst)
2756 // back to a (shl (srl x (clz cst)) (clz cst)) sequence.
2757 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2758 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2759 }
2760
2761 if (Op.getValueType() == MVT::f32)
2762 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
2763
2764 // FIXME: For mips32r2, the sequence of (BuildPairF64 (ins (ExtractElementF64
2765 // Op 1), $zero, 31 1) (ExtractElementF64 Op 0)) and the Op has one use, we
2766 // should be able to drop the usage of mfc1/mtc1 and rewrite the register in
2767 // place.
2768 SDValue LowX =
2769 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2770 DAG.getConstant(0, DL, MVT::i32));
2771 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
2772}
2773
2774SDValue MipsTargetLowering::lowerFABS64(SDValue Op, SelectionDAG &DAG,
2775 bool HasExtractInsert) const {
2776 SDLoc DL(Op);
2777 SDValue Res, Const1 = DAG.getConstant(1, DL, MVT::i32);
2778
2779 if (Op->getFlags().hasNoNaNs() || Subtarget.inAbs2008Mode())
2780 return DAG.getNode(MipsISD::FAbs, DL, Op.getValueType(), Op.getOperand(0));
2781
2782 // Bitcast to integer node.
2783 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
2784
2785 // Clear MSB.
2786 if (HasExtractInsert)
2787 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
2788 DAG.getRegister(Mips::R0_64, MVT::i64),
2789 DAG.getConstant(63, DL, MVT::i32), Const1, X);
2790 else {
2791 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
2792 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
2793 }
2794
2795 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
2796}
2797
2798SDValue MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
2799 if ((ABI.IsN32() || ABI.IsN64()) && (Op.getValueType() == MVT::f64))
2800 return lowerFABS64(Op, DAG, Subtarget.hasExtractInsert());
2801
2802 return lowerFABS32(Op, DAG, Subtarget.hasExtractInsert());
2803}
2804
2805SDValue MipsTargetLowering::lowerFNEG(SDValue Op, SelectionDAG &DAG) const {
2806 // Unless in abs2008 mode, neg.fmt is arithmetic: it raises invalid for a
2807 // signaling NaN and does not flip the sign of a NaN. Only use it when NaNs
2808 // can be ignored, otherwise flip the sign bit in a GPR.
2809 if (Op->getFlags().hasNoNaNs() || Subtarget.inAbs2008Mode())
2810 return Op;
2811
2812 SDLoc DL(Op);
2813 EVT VT = Op.getValueType();
2814 SDValue Src = Op.getOperand(0);
2815
2816 if (VT == MVT::f32) {
2817 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Src);
2818 X = DAG.getNode(ISD::XOR, DL, MVT::i32, X,
2819 DAG.getConstant(APInt::getSignMask(32), DL, MVT::i32));
2820 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, X);
2821 }
2822
2823 if (ABI.IsN32() || ABI.IsN64()) {
2824 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Src);
2825 X = DAG.getNode(ISD::XOR, DL, MVT::i64, X,
2826 DAG.getConstant(APInt::getSignMask(64), DL, MVT::i64));
2827 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, X);
2828 }
2829
2830 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Src,
2831 DAG.getConstant(0, DL, MVT::i32));
2832 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Src,
2833 DAG.getConstant(1, DL, MVT::i32));
2834 Hi = DAG.getNode(ISD::XOR, DL, MVT::i32, Hi,
2835 DAG.getConstant(APInt::getSignMask(32), DL, MVT::i32));
2836 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
2837}
2838
2839SDValue MipsTargetLowering::lowerFCANONICALIZE(SDValue Op,
2840 SelectionDAG &DAG) const {
2841 SDLoc DL(Op);
2842 EVT VT = Op.getValueType();
2843 SDValue Operand = Op.getOperand(0);
2844 SDNodeFlags Flags = Op->getFlags();
2845
2846 if (Flags.hasNoNaNs() || DAG.isKnownNeverNaN(Operand))
2847 return Operand;
2848
2849 SDValue Quiet = DAG.getNode(ISD::FADD, DL, VT, Operand, Operand);
2850 return DAG.getSelectCC(DL, Operand, Operand, Quiet, Operand, ISD::SETUO);
2851}
2852
2853SDValue MipsTargetLowering::
2854lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
2855 // check the depth
2856 if (Op.getConstantOperandVal(0) != 0) {
2857 DAG.getContext()->emitError(
2858 "return address can be determined only for current frame");
2859 return SDValue();
2860 }
2861
2862 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2863 MFI.setFrameAddressIsTaken(true);
2864 EVT VT = Op.getValueType();
2865 SDLoc DL(Op);
2866 SDValue FrameAddr = DAG.getCopyFromReg(
2867 DAG.getEntryNode(), DL, ABI.IsN64() ? Mips::FP_64 : Mips::FP, VT);
2868 return FrameAddr;
2869}
2870
2871SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
2872 SelectionDAG &DAG) const {
2873 // check the depth
2874 if (Op.getConstantOperandVal(0) != 0) {
2875 DAG.getContext()->emitError(
2876 "return address can be determined only for current frame");
2877 return SDValue();
2878 }
2879
2881 MachineFrameInfo &MFI = MF.getFrameInfo();
2882 MVT VT = Op.getSimpleValueType();
2883 unsigned RA = ABI.IsN64() ? Mips::RA_64 : Mips::RA;
2884 MFI.setReturnAddressIsTaken(true);
2885
2886 // Return RA, which contains the return address. Mark it an implicit live-in.
2888 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
2889}
2890
2891// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
2892// generated from __builtin_eh_return (offset, handler)
2893// The effect of this is to adjust the stack pointer by "offset"
2894// and then branch to "handler".
2895SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
2896 const {
2898 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2899
2900 MipsFI->setCallsEhReturn();
2901 SDValue Chain = Op.getOperand(0);
2902 SDValue Offset = Op.getOperand(1);
2903 SDValue Handler = Op.getOperand(2);
2904 SDLoc DL(Op);
2905 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
2906
2907 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2908 // EH_RETURN nodes, so that instructions are emitted back-to-back.
2909 unsigned OffsetReg = ABI.getReturnRegPtr(1);
2910 unsigned AddrReg = ABI.getReturnRegPtr(0);
2911 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2912 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2913 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2914 DAG.getRegister(OffsetReg, Ty),
2915 DAG.getRegister(AddrReg, getPointerTy(MF.getDataLayout())),
2916 Chain.getValue(1));
2917}
2918
2919SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
2920 SelectionDAG &DAG) const {
2921 // FIXME: Need pseudo-fence for 'singlethread' fences
2922 // FIXME: Set SType for weaker fences where supported/appropriate.
2923 unsigned SType = 0;
2924 SDLoc DL(Op);
2925 SyncScope::ID FenceSSID =
2926 static_cast<SyncScope::ID>(Op.getConstantOperandVal(2));
2927
2928 if (Subtarget.hasMips2() && FenceSSID == SyncScope::System)
2929 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
2930 DAG.getTargetConstant(SType, DL, MVT::i32));
2931
2932 // singlethread fences only synchronize with signal handlers on the same
2933 // thread and thus only need to preserve instruction order, not actually
2934 // enforce memory ordering.
2935 if ((Subtarget.hasMips1() && !Subtarget.hasMips2()) ||
2936 FenceSSID == SyncScope::SingleThread) {
2937 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
2938 return DAG.getNode(ISD::MEMBARRIER, DL, MVT::Other, Op.getOperand(0));
2939 }
2940
2941 return Op;
2942}
2943
2944SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
2945 SelectionDAG &DAG) const {
2946 SDLoc DL(Op);
2947 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
2948
2949 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2950 SDValue Shamt = Op.getOperand(2);
2951 // if shamt < (VT.bits):
2952 // lo = (shl lo, shamt)
2953 // hi = (or (shl hi, shamt) (srl (srl lo, 1), (xor shamt, (VT.bits-1))))
2954 // else:
2955 // lo = 0
2956 // hi = (shl lo, shamt[4:0])
2957 SDValue Not =
2958 DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2959 DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32));
2960 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo,
2961 DAG.getConstant(1, DL, VT));
2962 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, Not);
2963 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
2964 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2965 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
2966 // The masked shift amount is 0 or VT.bits, not a 0/1 boolean, so compare it
2967 // against zero to form a proper select condition.
2968 SDValue Masked =
2969 DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2970 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
2971 SDValue Cond = DAG.getSetCC(DL, MVT::i32, Masked,
2972 DAG.getConstant(0, DL, MVT::i32), ISD::SETNE);
2973 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond,
2974 DAG.getConstant(0, DL, VT), ShiftLeftLo);
2975 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftLeftLo, Or);
2976
2977 SDValue Ops[2] = {Lo, Hi};
2978 return DAG.getMergeValues(Ops, DL);
2979}
2980
2981SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
2982 bool IsSRA) const {
2983 SDLoc DL(Op);
2984 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2985 SDValue Shamt = Op.getOperand(2);
2986 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
2987
2988 // if shamt < (VT.bits):
2989 // lo = (or (shl (shl hi, 1), (xor shamt, (VT.bits-1))) (srl lo, shamt))
2990 // if isSRA:
2991 // hi = (sra hi, shamt)
2992 // else:
2993 // hi = (srl hi, shamt)
2994 // else:
2995 // if isSRA:
2996 // lo = (sra hi, shamt[4:0])
2997 // hi = (sra hi, 31)
2998 // else:
2999 // lo = (srl hi, shamt[4:0])
3000 // hi = 0
3001 SDValue Not =
3002 DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
3003 DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32));
3004 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, VT, Hi,
3005 DAG.getConstant(1, DL, VT));
3006 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, ShiftLeft1Hi, Not);
3007 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
3008 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
3009 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL,
3010 DL, VT, Hi, Shamt);
3011 SDValue Masked =
3012 DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
3013 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
3014 SDValue Ext = DAG.getNode(ISD::SRA, DL, VT, Hi,
3015 DAG.getConstant(VT.getSizeInBits() - 1, DL, VT));
3016
3017 if (!(Subtarget.hasMips4() || Subtarget.hasMips32())) {
3018 // DOUBLE_SELECT_I* branches on the condition being non-zero, so the
3019 // masked shift amount can be used directly.
3020 SDVTList VTList = DAG.getVTList(VT, VT);
3021 return DAG.getNode(Subtarget.isGP64bit() ? MipsISD::DOUBLE_SELECT_I64
3023 DL, VTList, Masked, ShiftRightHi,
3024 IsSRA ? Ext : DAG.getConstant(0, DL, VT), Or,
3025 ShiftRightHi);
3026 }
3027
3028 // The masked shift amount is 0 or VT.bits, not a 0/1 boolean, so compare it
3029 // against zero to form a proper select condition.
3030 SDValue Cond = DAG.getSetCC(DL, MVT::i32, Masked,
3031 DAG.getConstant(0, DL, MVT::i32), ISD::SETNE);
3032 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftRightHi, Or);
3033 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond,
3034 IsSRA ? Ext : DAG.getConstant(0, DL, VT), ShiftRightHi);
3035
3036 SDValue Ops[2] = {Lo, Hi};
3037 return DAG.getMergeValues(Ops, DL);
3038}
3039
3041 SDValue Chain, SDValue Src, unsigned Offset) {
3042 SDValue Ptr = LD->getBasePtr();
3043 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
3044 EVT BasePtrVT = Ptr.getValueType();
3045 SDLoc DL(LD);
3046 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
3047
3048 if (Offset)
3049 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
3050 DAG.getConstant(Offset, DL, BasePtrVT));
3051
3052 SDValue Ops[] = { Chain, Ptr, Src };
3053 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
3054 LD->getMemOperand());
3055}
3056
3057// Expand an unaligned 32 or 64-bit integer load node.
3060 EVT MemVT = LD->getMemoryVT();
3061
3062 if (Subtarget.systemSupportsUnalignedAccess())
3063 return Op;
3064
3065 // Return if load is aligned or if MemVT is neither i32 nor i64.
3066 if ((LD->getAlign().value() >= (MemVT.getSizeInBits() / 8)) ||
3067 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
3068 return SDValue();
3069
3070 bool IsLittle = Subtarget.isLittle();
3071 EVT VT = Op.getValueType();
3072 ISD::LoadExtType ExtType = LD->getExtensionType();
3073 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
3074
3075 assert((VT == MVT::i32) || (VT == MVT::i64));
3076
3077 // Expand
3078 // (set dst, (i64 (load baseptr)))
3079 // to
3080 // (set tmp, (ldl (add baseptr, 7), undef))
3081 // (set dst, (ldr baseptr, tmp))
3082 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
3083 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
3084 IsLittle ? 7 : 0);
3085 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
3086 IsLittle ? 0 : 7);
3087 }
3088
3089 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
3090 IsLittle ? 3 : 0);
3091 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
3092 IsLittle ? 0 : 3);
3093
3094 // Expand
3095 // (set dst, (i32 (load baseptr))) or
3096 // (set dst, (i64 (sextload baseptr))) or
3097 // (set dst, (i64 (extload baseptr)))
3098 // to
3099 // (set tmp, (lwl (add baseptr, 3), undef))
3100 // (set dst, (lwr baseptr, tmp))
3101 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
3102 (ExtType == ISD::EXTLOAD))
3103 return LWR;
3104
3105 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
3106
3107 // Expand
3108 // (set dst, (i64 (zextload baseptr)))
3109 // to
3110 // (set tmp0, (lwl (add baseptr, 3), undef))
3111 // (set tmp1, (lwr baseptr, tmp0))
3112 // (set tmp2, (shl tmp1, 32))
3113 // (set dst, (srl tmp2, 32))
3114 SDLoc DL(LD);
3115 SDValue Const32 = DAG.getConstant(32, DL, MVT::i32);
3116 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
3117 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
3118 SDValue Ops[] = { SRL, LWR.getValue(1) };
3119 return DAG.getMergeValues(Ops, DL);
3120}
3121
3123 SDValue Chain, unsigned Offset) {
3124 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
3125 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
3126 SDLoc DL(SD);
3127 SDVTList VTList = DAG.getVTList(MVT::Other);
3128
3129 if (Offset)
3130 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
3131 DAG.getConstant(Offset, DL, BasePtrVT));
3132
3133 SDValue Ops[] = { Chain, Value, Ptr };
3134 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
3135 SD->getMemOperand());
3136}
3137
3138// Expand an unaligned 32 or 64-bit integer store node.
3140 bool IsLittle) {
3141 SDValue Value = SD->getValue(), Chain = SD->getChain();
3142 EVT VT = Value.getValueType();
3143
3144 // Expand
3145 // (store val, baseptr) or
3146 // (truncstore val, baseptr)
3147 // to
3148 // (swl val, (add baseptr, 3))
3149 // (swr val, baseptr)
3150 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
3151 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
3152 IsLittle ? 3 : 0);
3153 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
3154 }
3155
3156 assert(VT == MVT::i64);
3157
3158 // Expand
3159 // (store val, baseptr)
3160 // to
3161 // (sdl val, (add baseptr, 7))
3162 // (sdr val, baseptr)
3163 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
3164 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
3165}
3166
3167// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
3169 bool SingleFloat) {
3170 SDValue Val = SD->getValue();
3171
3172 if (Val.getOpcode() != ISD::FP_TO_SINT ||
3173 (Val.getValueSizeInBits() > 32 && SingleFloat))
3174 return SDValue();
3175
3177 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
3178 Val.getOperand(0));
3179 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
3180 SD->getPointerInfo(), SD->getAlign(),
3181 SD->getMemOperand()->getFlags());
3182}
3183
3186 EVT MemVT = SD->getMemoryVT();
3187
3188 // Lower unaligned integer stores.
3189 if (!Subtarget.systemSupportsUnalignedAccess() &&
3190 (SD->getAlign().value() < (MemVT.getSizeInBits() / 8)) &&
3191 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
3192 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
3193
3194 return lowerFP_TO_SINT_STORE(SD, DAG, Subtarget.isSingleFloat());
3195}
3196
3197SDValue MipsTargetLowering::lowerEH_DWARF_CFA(SDValue Op,
3198 SelectionDAG &DAG) const {
3199
3200 // Return a fixed StackObject with offset 0 which points to the old stack
3201 // pointer.
3203 EVT ValTy = Op->getValueType(0);
3204 int FI = MFI.CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
3205 return DAG.getFrameIndex(FI, ValTy);
3206}
3207
3208SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
3209 SelectionDAG &DAG) const {
3210 if (Op.getValueSizeInBits() > 32 && Subtarget.isSingleFloat())
3211 return SDValue();
3212
3213 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
3214 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
3215 Op.getOperand(0));
3216 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
3217}
3218
3219SDValue MipsTargetLowering::lowerSTRICT_FP_TO_SINT(SDValue Op,
3220 SelectionDAG &DAG) const {
3221 if (Op.getValueSizeInBits() > 32 && Subtarget.isSingleFloat())
3222 return SDValue();
3223
3224 // Keep the conversion on the chain so that it is not removed or reordered
3225 // with respect to other operations that access the FP environment.
3226 SDLoc DL(Op);
3227 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
3228 SDValue Trunc = DAG.getNode(MipsISD::STRICT_TruncIntFP, DL,
3229 DAG.getVTList(FPTy, MVT::Other), Op.getOperand(0),
3230 Op.getOperand(1));
3231 SDValue Res = DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Trunc);
3232 return DAG.getMergeValues({Res, Trunc.getValue(1)}, DL);
3233}
3234
3235// FCSR.RM uses 0, 1, 2 and 3 for to nearest, toward zero, toward +inf and
3236// toward -inf, while llvm.get.rounding and llvm.set.rounding use 1, 0, 2 and
3237// 3. Both directions swap 0 and 1: x ^ ((x >> 1) ^ 1) for x in [0, 3].
3239 SelectionDAG &DAG) {
3240 SDValue One = DAG.getConstant(1, DL, MVT::i32);
3241 SDValue Hi = DAG.getNode(ISD::SRL, DL, MVT::i32, X, One);
3242 return DAG.getNode(ISD::XOR, DL, MVT::i32, X,
3243 DAG.getNode(ISD::XOR, DL, MVT::i32, Hi, One));
3244}
3245
3246SDValue MipsTargetLowering::lowerGET_ROUNDING(SDValue Op,
3247 SelectionDAG &DAG) const {
3248 SDLoc DL(Op);
3249 SDValue FCSR =
3250 DAG.getCopyFromReg(Op.getOperand(0), DL, Mips::FCR31, MVT::i32);
3251 SDValue RM = DAG.getNode(ISD::AND, DL, MVT::i32, FCSR,
3252 DAG.getConstant(3, DL, MVT::i32));
3253 SDValue Res = DAG.getZExtOrTrunc(convertRoundingMode(RM, DL, DAG), DL,
3254 Op.getValueType());
3255 return DAG.getMergeValues({Res, FCSR.getValue(1)}, DL);
3256}
3257
3258SDValue MipsTargetLowering::lowerSET_ROUNDING(SDValue Op,
3259 SelectionDAG &DAG) const {
3260 SDLoc DL(Op);
3261 SDValue Three = DAG.getConstant(3, DL, MVT::i32);
3262 SDValue Mode = DAG.getZExtOrTrunc(Op.getOperand(1), DL, MVT::i32);
3264 DAG.getNode(ISD::AND, DL, MVT::i32, Mode, Three), DL, DAG);
3265 SDValue FCSR =
3266 DAG.getCopyFromReg(Op.getOperand(0), DL, Mips::FCR31, MVT::i32);
3267 SDValue Chain = FCSR.getValue(1);
3268 FCSR = DAG.getNode(ISD::AND, DL, MVT::i32, FCSR,
3269 DAG.getConstant(~3U, DL, MVT::i32));
3270 FCSR = DAG.getNode(ISD::OR, DL, MVT::i32, FCSR, RM);
3271 return DAG.getCopyToReg(Chain, DL, Mips::FCR31, FCSR);
3272}
3273
3275 static const MCPhysReg RCRegs[] = {Mips::FCR31};
3276 return RCRegs;
3277}
3278
3279//===----------------------------------------------------------------------===//
3280// Calling Convention Implementation
3281//===----------------------------------------------------------------------===//
3282
3283//===----------------------------------------------------------------------===//
3284// TODO: Implement a generic logic using tblgen that can support this.
3285// Mips O32 ABI rules:
3286// ---
3287// i32 - Passed in A0, A1, A2, A3 and stack
3288// f32 - Only passed in f32 registers if no int reg has been used yet to hold
3289// an argument. Otherwise, passed in A1, A2, A3 and stack.
3290// f64 - Only passed in two aliased f32 registers if no int reg has been used
3291// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
3292// not used, it must be shadowed. If only A3 is available, shadow it and
3293// go to stack.
3294// vXiX - Received as scalarized i32s, passed in A0 - A3 and the stack.
3295// vXf32 - Passed in either a pair of registers {A0, A1}, {A2, A3} or {A0 - A3}
3296// with the remainder spilled to the stack.
3297// vXf64 - Passed in either {A0, A1, A2, A3} or {A2, A3} and in both cases
3298// spilling the remainder to the stack.
3299//
3300// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
3301//===----------------------------------------------------------------------===//
3302
3303static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
3304 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
3305 Type *OrigTy, CCState &State,
3306 ArrayRef<MCPhysReg> F64Regs) {
3307 const MipsSubtarget &Subtarget = static_cast<const MipsSubtarget &>(
3308 State.getMachineFunction().getSubtarget());
3309
3310 const MipsABIInfo &ABI = Subtarget.getABI();
3311 ArrayRef<MCPhysReg> IntRegs = ABI.getArgRegs(false);
3312
3313 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
3314
3315 const MCPhysReg FloatVectorIntRegs[] = {IntRegs[0], IntRegs[2]};
3316
3317 // Do not process byval args here.
3318 if (ArgFlags.isByVal())
3319 return true;
3320
3321 // Promote i8 and i16
3322 if (ArgFlags.isInReg() && !Subtarget.isLittle()) {
3323 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) {
3324 LocVT = MVT::i32;
3325 if (ArgFlags.isSExt())
3326 LocInfo = CCValAssign::SExtUpper;
3327 else if (ArgFlags.isZExt())
3328 LocInfo = CCValAssign::ZExtUpper;
3329 else
3330 LocInfo = CCValAssign::AExtUpper;
3331 }
3332 }
3333
3334 // Promote i8 and i16
3335 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
3336 LocVT = MVT::i32;
3337 if (ArgFlags.isSExt())
3338 LocInfo = CCValAssign::SExt;
3339 else if (ArgFlags.isZExt())
3340 LocInfo = CCValAssign::ZExt;
3341 else
3342 LocInfo = CCValAssign::AExt;
3343 }
3344
3345 unsigned Reg;
3346
3347 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
3348 // is true: function is vararg, argument is 3rd or higher, there is previous
3349 // argument which is not f32 or f64.
3350 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1 ||
3351 State.getFirstUnallocated(F32Regs) != ValNo;
3352 Align OrigAlign = ArgFlags.getNonZeroOrigAlign();
3353 bool isI64 = (ValVT == MVT::i32 && OrigAlign == Align(8));
3354 bool isVectorFloat = OrigTy->isVectorTy() && OrigTy->isFPOrFPVectorTy();
3355
3356 // The MIPS vector ABI for floats passes them in a pair of registers
3357 if (ValVT == MVT::i32 && isVectorFloat) {
3358 // This is the start of an vector that was scalarized into an unknown number
3359 // of components. It doesn't matter how many there are. Allocate one of the
3360 // notional 8 byte aligned registers which map onto the argument stack, and
3361 // shadow the register lost to alignment requirements.
3362 if (ArgFlags.isSplit()) {
3363 Reg = State.AllocateReg(FloatVectorIntRegs);
3364 if (Reg == IntRegs[2])
3365 State.AllocateReg(IntRegs[1]);
3366 else if (Reg == 0)
3367 State.AllocateReg(IntRegs[3]);
3368 } else {
3369 // If we're an intermediate component of the split, we can just attempt to
3370 // allocate a register directly.
3371 Reg = State.AllocateReg(IntRegs);
3372 }
3373 } else if (ValVT == MVT::i32 ||
3374 (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
3375 Reg = State.AllocateReg(IntRegs);
3376 // If this is the first part of an i64 arg,
3377 // the allocated register must be either A0 or A2.
3378 if (isI64 && (Reg == IntRegs[1] || Reg == IntRegs[3]))
3379 Reg = State.AllocateReg(IntRegs);
3380 // An f32 is only passed as an i32 when it is in an integer register. On
3381 // the stack it keeps its own type, as f64 does.
3382 if (Reg)
3383 LocVT = MVT::i32;
3384 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
3385 // Allocate int register and shadow next int register. If first
3386 // available register is $a1 or $a3, shadow it too.
3387 Reg = State.AllocateReg(IntRegs);
3388 if (Reg == IntRegs[1] || Reg == IntRegs[3])
3389 Reg = State.AllocateReg(IntRegs);
3390
3391 if (Reg) {
3392 LocVT = MVT::i32;
3393
3394 State.addLoc(
3395 CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
3396 MCRegister HiReg = State.AllocateReg(IntRegs);
3397 assert(HiReg);
3398 State.addLoc(
3399 CCValAssign::getCustomReg(ValNo, ValVT, HiReg, LocVT, LocInfo));
3400 return false;
3401 }
3402 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
3403 // we are guaranteed to find an available float register
3404 if (ValVT == MVT::f32) {
3405 Reg = State.AllocateReg(F32Regs);
3406 // Shadow int register
3407 State.AllocateReg(IntRegs);
3408 } else {
3409 Reg = State.AllocateReg(F64Regs);
3410 // Shadow int registers
3411 MCRegister Reg2 = State.AllocateReg(IntRegs);
3412 if (Reg2 == IntRegs[1] || Reg2 == IntRegs[3])
3413 State.AllocateReg(IntRegs);
3414 State.AllocateReg(IntRegs);
3415 }
3416 } else
3417 llvm_unreachable("Cannot handle this ValVT.");
3418
3419 if (!Reg) {
3420 unsigned Offset = State.AllocateStack(ValVT.getStoreSize(), OrigAlign);
3421 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
3422 } else
3423 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
3424
3425 return false;
3426}
3427
3428static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, MVT LocVT,
3429 CCValAssign::LocInfo LocInfo,
3430 ISD::ArgFlagsTy ArgFlags, Type *OrigTy,
3431 CCState &State) {
3432 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
3433
3434 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, OrigTy, State,
3435 F64Regs);
3436}
3437
3438static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, MVT LocVT,
3439 CCValAssign::LocInfo LocInfo,
3440 ISD::ArgFlagsTy ArgFlags, Type *OrigTy,
3441 CCState &State) {
3442 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
3443
3444 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, OrigTy, State,
3445 F64Regs);
3446}
3447
3448[[maybe_unused]] static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
3449 CCValAssign::LocInfo LocInfo,
3450 ISD::ArgFlagsTy ArgFlags, Type *OrigTy,
3451 CCState &State);
3452
3453#define GET_CALLING_CONV_IMPL
3454#include "MipsGenCallingConv.inc"
3455
3457 return CC_Mips_FixedArg;
3458 }
3459
3461 return RetCC_Mips;
3462 }
3463//===----------------------------------------------------------------------===//
3464// Call Calling Convention Implementation
3465//===----------------------------------------------------------------------===//
3466
3467SDValue MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
3468 SDValue Chain, SDValue Arg,
3469 const SDLoc &DL, bool IsTailCall,
3470 SelectionDAG &DAG) const {
3471 if (!IsTailCall) {
3472 SDValue PtrOff =
3473 DAG.getNode(ISD::ADD, DL, getPointerTy(DAG.getDataLayout()), StackPtr,
3475 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo());
3476 }
3477
3479 int FI = MFI.CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
3480 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
3481 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(), MaybeAlign(),
3483}
3484
3487 std::deque<std::pair<unsigned, SDValue>> &RegsToPass, bool IsPICCall,
3488 bool GlobalOrExternal, bool LocalLinkage, bool IsCallReloc,
3489 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
3490 // Insert node "GP copy globalreg" before call to function.
3491 //
3492 // R_MIPS_CALL* operators (emitted when non-local functions are called
3493 // in PIC mode) allow symbols to be resolved via lazy binding.
3494 // The lazy binding stub requires GP to point to the GOT.
3495 // Note that we don't need GP to point to the GOT for indirect calls
3496 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
3497 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
3498 // used for the function (that is, Mips linker doesn't generate lazy binding
3499 // stub for a function whose address is taken in the program).
3500 if (IsPICCall && !LocalLinkage && IsCallReloc) {
3501 unsigned GPReg = ABI.IsN64() ? Mips::GP_64 : Mips::GP;
3502 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
3503 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
3504 }
3505
3506 // Build a sequence of copy-to-reg nodes chained together with token
3507 // chain and flag operands which copy the outgoing args into registers.
3508 // The InGlue in necessary since all emitted instructions must be
3509 // stuck together.
3510 SDValue InGlue;
3511
3512 for (auto &R : RegsToPass) {
3513 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, R.first, R.second, InGlue);
3514 InGlue = Chain.getValue(1);
3515 }
3516
3517 // Add argument registers to the end of the list so that they are
3518 // known live into the call.
3519 for (auto &R : RegsToPass)
3520 Ops.push_back(CLI.DAG.getRegister(R.first, R.second.getValueType()));
3521
3522 // Add a register mask operand representing the call-preserved registers.
3523 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
3524 const uint32_t *Mask =
3525 TRI->getCallPreservedMask(CLI.DAG.getMachineFunction(), CLI.CallConv);
3526 assert(Mask && "Missing call preserved mask for calling convention");
3527 if (Subtarget.inMips16HardFloat()) {
3529 StringRef Sym = G->getGlobal()->getName();
3530 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
3531 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
3533 }
3534 }
3535 }
3536 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
3537
3538 if (InGlue.getNode())
3539 Ops.push_back(InGlue);
3540}
3541
3543 SDNode *Node) const {
3544 switch (MI.getOpcode()) {
3545 default:
3546 return;
3547 case Mips::JALR:
3548 case Mips::JALRPseudo:
3549 case Mips::JALR64:
3550 case Mips::JALR64Pseudo:
3551 case Mips::JALR16_MM:
3552 case Mips::JALRC16_MMR6:
3553 case Mips::TAILCALLREG:
3554 case Mips::TAILCALLREG64:
3555 case Mips::TAILCALLR6REG:
3556 case Mips::TAILCALL64R6REG:
3557 case Mips::TAILCALLREG_MM:
3558 case Mips::TAILCALLREG_MMR6: {
3559 if (!Subtarget.getMCCLOpts().jalr_reloc || Subtarget.inMips16Mode() ||
3560 !isPositionIndependent() || Node->getNumOperands() < 1 ||
3561 Node->getOperand(0).getNumOperands() < 2) {
3562 return;
3563 }
3564 // We are after the callee address, set by LowerCall().
3565 // If added to MI, asm printer will emit .reloc R_MIPS_JALR for the
3566 // symbol.
3567 const SDValue TargetAddr = Node->getOperand(0).getOperand(1);
3568 MCSymbol *S = nullptr;
3569 if (const GlobalAddressSDNode *G =
3571 const GlobalValue *GV = G->getGlobal();
3572
3573 // We must not emit the R_MIPS_JALR relocation against data symbols
3574 // since this will cause run-time crashes if the linker replaces the
3575 // call instruction with a relative branch to the data symbol.
3576 if (!isa<Function>(GV)) {
3577 LLVM_DEBUG(dbgs() << "Not adding R_MIPS_JALR against data symbol "
3578 << GV->getName() << "\n");
3579 return;
3580 }
3581 S = getTargetMachine().getSymbol(GV);
3582 }
3583 else if (const ExternalSymbolSDNode *ES =
3585 S = MI.getParent()->getParent()->getContext().getOrCreateSymbol(
3586 ES->getSymbol());
3587 }
3588
3589 if (!S)
3590 return;
3591
3592 LLVM_DEBUG(dbgs() << "Adding R_MIPS_JALR against " << S->getName()
3593 << "\n");
3595 }
3596 }
3597}
3598
3600 // Compute the max call frame size early so we can reserve FP if needed.
3603}
3604
3605/// LowerCall - functions arguments are copied from virtual regs to
3606/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
3607SDValue
3608MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
3609 SmallVectorImpl<SDValue> &InVals) const {
3610 SelectionDAG &DAG = CLI.DAG;
3611 SDLoc DL = CLI.DL;
3613 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
3615 SDValue Chain = CLI.Chain;
3616 SDValue Callee = CLI.Callee;
3617 bool &IsTailCall = CLI.IsTailCall;
3618 CallingConv::ID CallConv = CLI.CallConv;
3619 bool IsVarArg = CLI.IsVarArg;
3620 const CallBase *CB = CLI.CB;
3621
3623 MachineFrameInfo &MFI = MF.getFrameInfo();
3624 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
3625 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
3626 bool IsPIC = isPositionIndependent();
3627
3628 // Analyze operands of the call, assigning locations to each operand.
3630 MipsCCState CCInfo(
3631 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(),
3633
3634 const ExternalSymbolSDNode *ES =
3636
3637 // There is one case where CALLSEQ_START..CALLSEQ_END can be nested, which
3638 // is during the lowering of a call with a byval argument which produces
3639 // a call to memcpy. For the O32 case, this causes the caller to allocate
3640 // stack space for the reserved argument area for the callee, then recursively
3641 // again for the memcpy call. In the NEWABI case, this doesn't occur as those
3642 // ABIs mandate that the callee allocates the reserved argument area. We do
3643 // still produce nested CALLSEQ_START..CALLSEQ_END with zero space though.
3644 //
3645 // If the callee has a byval argument and memcpy is used, we are mandated
3646 // to already have produced a reserved argument area for the callee for O32.
3647 // Therefore, the reserved argument area can be reused for both calls.
3648 //
3649 // Other cases of calling memcpy cannot have a chain with a CALLSEQ_START
3650 // present, as we have yet to hook that node onto the chain.
3651 //
3652 // Hence, the CALLSEQ_START and CALLSEQ_END nodes can be eliminated in this
3653 // case. GCC does a similar trick, in that wherever possible, it calculates
3654 // the maximum out going argument area (including the reserved area), and
3655 // preallocates the stack space on entrance to the caller.
3656 //
3657 // FIXME: We should do the same for efficiency and space.
3658
3659 // Note: The check on the calling convention below must match
3660 // MipsABIInfo::GetCalleeAllocdArgSizeInBytes().
3661 bool MemcpyInByVal = ES && StringRef(ES->getSymbol()) == "memcpy" &&
3662 CallConv != CallingConv::Fast &&
3663 Chain.getOpcode() == ISD::CALLSEQ_START;
3664
3665 // Allocate the reserved argument area. It seems strange to do this from the
3666 // caller side but removing it breaks the frame size calculation.
3667 unsigned ReservedArgArea =
3668 MemcpyInByVal ? 0 : ABI.GetCalleeAllocdArgSizeInBytes(CallConv);
3669 CCInfo.AllocateStack(ReservedArgArea, Align(1));
3670
3671 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
3672
3673 // Get a count of how many bytes are to be pushed on the stack.
3674 unsigned StackSize = CCInfo.getStackSize();
3675
3676 // Call site info for function parameters tracking and call base type info.
3678 // Set type id for call site info.
3679 setTypeIdForCallsiteInfo(CB, MF, CSInfo);
3680
3681 // Check if it's really possible to do a tail call.
3682 // For non-musttail calls, restrict to functions that won't require $gp
3683 // restoration. In PIC mode, calling external functions via tail call can
3684 // cause issues with $gp register handling (see D24763).
3685 bool IsMustTail = CLI.CB && CLI.CB->isMustTailCall();
3686 bool CalleeIsLocal = true;
3688 const GlobalValue *GV = G->getGlobal();
3689 bool HasLocalLinkage = GV->hasLocalLinkage() || GV->hasPrivateLinkage();
3690 bool HasHiddenVisibility =
3692 if (GV->isDeclarationForLinker())
3693 CalleeIsLocal = HasLocalLinkage || HasHiddenVisibility;
3694 else
3695 CalleeIsLocal = GV->isDSOLocal();
3696 }
3697
3698 if (IsTailCall) {
3699 if (!Subtarget.getCLOpts().tail_calls) {
3700 IsTailCall = false;
3701 if (IsMustTail)
3702 report_fatal_error("failed to perform tail call elimination on a call "
3703 "site marked musttail");
3704 } else {
3705 bool Eligible = isEligibleForTailCallOptimization(
3706 CCInfo, StackSize, *MF.getInfo<MipsFunctionInfo>());
3707 if (!Eligible || !CalleeIsLocal) {
3708 IsTailCall = false;
3709 if (IsMustTail)
3711 "failed to perform tail call elimination on a call "
3712 "site marked musttail");
3713 }
3714 }
3715 }
3716
3717 if (IsTailCall)
3718 ++NumTailCalls;
3719
3720 // Chain is the output chain of the last Load/Store or CopyToReg node.
3721 // ByValChain is the output chain of the last Memcpy node created for copying
3722 // byval arguments to the stack.
3723 unsigned StackAlignment = TFL->getStackAlignment();
3724 StackSize = alignTo(StackSize, StackAlignment);
3725
3726 if (!(IsTailCall || MemcpyInByVal))
3727 Chain = DAG.getCALLSEQ_START(Chain, StackSize, 0, DL);
3728
3730 DAG.getCopyFromReg(Chain, DL, ABI.IsN64() ? Mips::SP_64 : Mips::SP,
3732 std::deque<std::pair<unsigned, SDValue>> RegsToPass;
3733 SmallVector<SDValue, 8> MemOpChains;
3734
3735 CCInfo.rewindByValRegsInfo();
3736
3737 // Walk the register/memloc assignments, inserting copies/loads.
3738 for (unsigned i = 0, e = ArgLocs.size(), OutIdx = 0; i != e; ++i, ++OutIdx) {
3739 SDValue Arg = OutVals[OutIdx];
3740 CCValAssign &VA = ArgLocs[i];
3741 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
3742 ISD::ArgFlagsTy Flags = Outs[OutIdx].Flags;
3743 bool UseUpperBits = false;
3744
3745 // ByVal Arg.
3746 if (Flags.isByVal()) {
3747 unsigned FirstByValReg, LastByValReg;
3748 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
3749 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
3750
3751 assert(Flags.getByValSize() &&
3752 "ByVal args of size 0 should have been ignored by front-end.");
3753 assert(ByValIdx < CCInfo.getInRegsParamsCount());
3754 assert(!IsTailCall &&
3755 "Do not tail-call optimize if there is a byval argument.");
3756 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
3757 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(),
3758 VA);
3759 CCInfo.nextInRegsParam();
3760 continue;
3761 }
3762
3763 // Promote the value if needed.
3764 switch (VA.getLocInfo()) {
3765 default:
3766 llvm_unreachable("Unknown loc info!");
3767 case CCValAssign::Full:
3768 if (VA.isRegLoc()) {
3769 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
3770 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
3771 (ValVT == MVT::i64 && LocVT == MVT::f64))
3772 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
3773 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
3774 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
3775 Arg, DAG.getConstant(0, DL, MVT::i32));
3776 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
3777 Arg, DAG.getConstant(1, DL, MVT::i32));
3778 if (!Subtarget.isLittle())
3779 std::swap(Lo, Hi);
3780
3781 assert(VA.needsCustom());
3782
3783 Register LocRegLo = VA.getLocReg();
3784 Register LocRegHigh = ArgLocs[++i].getLocReg();
3785 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
3786 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
3787 continue;
3788 }
3789 }
3790 break;
3791 case CCValAssign::BCvt:
3792 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
3793 break;
3795 UseUpperBits = true;
3796 [[fallthrough]];
3797 case CCValAssign::SExt:
3798 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
3799 break;
3801 UseUpperBits = true;
3802 [[fallthrough]];
3803 case CCValAssign::ZExt:
3804 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
3805 break;
3807 UseUpperBits = true;
3808 [[fallthrough]];
3809 case CCValAssign::AExt:
3810 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
3811 break;
3812 }
3813
3814 if (UseUpperBits) {
3815 unsigned ValSizeInBits = Outs[OutIdx].ArgVT.getSizeInBits();
3816 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3817 Arg = DAG.getNode(
3818 ISD::SHL, DL, VA.getLocVT(), Arg,
3819 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
3820 }
3821
3822 // Arguments that can be passed on register must be kept at
3823 // RegsToPass vector
3824 if (VA.isRegLoc()) {
3825 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3826
3827 // If the parameter is passed through reg $D, which splits into
3828 // two physical registers, avoid creating call site info.
3829 if (Mips::AFGR64RegClass.contains(VA.getLocReg()))
3830 continue;
3831
3832 // Collect CSInfo about which register passes which parameter.
3833 const TargetOptions &Options = DAG.getTarget().Options;
3834 if (Options.EmitCallSiteInfo)
3835 CSInfo.ArgRegPairs.emplace_back(VA.getLocReg(), i);
3836
3837 continue;
3838 }
3839
3840 // Register can't get to this point...
3841 assert(VA.isMemLoc());
3842
3843 // emit ISD::STORE whichs stores the
3844 // parameter value to a stack Location
3845 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
3846 Chain, Arg, DL, IsTailCall, DAG));
3847 }
3848
3849 // Transform all store nodes into one single node because all store
3850 // nodes are independent of each other.
3851 if (!MemOpChains.empty())
3852 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
3853
3854 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
3855 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
3856 // node so that legalize doesn't hack it.
3857
3858 EVT Ty = Callee.getValueType();
3859 bool GlobalOrExternal = false, IsCallReloc = false;
3860
3861 // The long-calls feature is ignored in case of PIC.
3862 // While we do not support -mshared / -mno-shared properly,
3863 // ignore long-calls in case of -mabicalls too.
3864 if (!Subtarget.isABICalls() && !IsPIC) {
3865 // If the function should be called using "long call",
3866 // get its address into a register to prevent using
3867 // of the `jal` instruction for the direct call.
3868 if (auto *N = dyn_cast<ExternalSymbolSDNode>(Callee)) {
3869 if (Subtarget.useLongCalls())
3870 Callee = Subtarget.hasSym32()
3871 ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
3872 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
3873 } else if (auto *N = dyn_cast<GlobalAddressSDNode>(Callee)) {
3874 bool UseLongCalls = Subtarget.useLongCalls();
3875 // If the function has long-call/far/near attribute
3876 // it overrides command line switch pased to the backend.
3877 if (auto *F = dyn_cast<Function>(N->getGlobal())) {
3878 if (F->hasFnAttribute("long-call"))
3879 UseLongCalls = true;
3880 else if (F->hasFnAttribute("short-call"))
3881 UseLongCalls = false;
3882 }
3883 if (UseLongCalls)
3884 Callee = Subtarget.hasSym32()
3885 ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
3886 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
3887 }
3888 }
3889
3890 bool LocalLinkage = false;
3891 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3892 if (Subtarget.isTargetCOFF() &&
3893 G->getGlobal()->hasDLLImportStorageClass()) {
3894 assert(Subtarget.isTargetWindows() &&
3895 "Windows is the only supported COFF target");
3896 auto PtrInfo = MachinePointerInfo();
3897 Callee = DAG.getLoad(Ty, DL, Chain,
3898 getDllimportSymbol(G, SDLoc(G), Ty, DAG), PtrInfo);
3899 } else if (IsPIC) {
3900 const GlobalValue *Val = G->getGlobal();
3901 LocalLinkage = Val->hasLocalLinkage();
3902
3903 if (LocalLinkage)
3904 Callee = getAddrLocal(G, DL, Ty, DAG, ABI.IsN32() || ABI.IsN64());
3905 else if (Subtarget.useXGOT()) {
3907 MipsII::MO_CALL_LO16, Chain,
3908 FuncInfo->callPtrInfo(MF, Val));
3909 IsCallReloc = true;
3910 } else {
3911 Callee = getAddrGlobal(G, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
3912 FuncInfo->callPtrInfo(MF, Val));
3913 IsCallReloc = true;
3914 }
3915 } else
3916 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL,
3917 getPointerTy(DAG.getDataLayout()), 0,
3919 GlobalOrExternal = true;
3920 }
3921 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
3922 const char *Sym = S->getSymbol();
3923
3924 if (!IsPIC) // static
3927 else if (Subtarget.useXGOT()) {
3929 MipsII::MO_CALL_LO16, Chain,
3930 FuncInfo->callPtrInfo(MF, Sym));
3931 IsCallReloc = true;
3932 } else { // PIC
3933 Callee = getAddrGlobal(S, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
3934 FuncInfo->callPtrInfo(MF, Sym));
3935 IsCallReloc = true;
3936 }
3937
3938 GlobalOrExternal = true;
3939 }
3940
3941 SmallVector<SDValue, 8> Ops(1, Chain);
3942 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
3943
3944 getOpndList(Ops, RegsToPass, IsPIC, GlobalOrExternal, LocalLinkage,
3945 IsCallReloc, CLI, Callee, Chain);
3946
3947 if (IsTailCall) {
3949 SDValue Ret = DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
3950 DAG.addCallSiteInfo(Ret.getNode(), std::move(CSInfo));
3951 return Ret;
3952 }
3953
3954 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
3955 SDValue InGlue = Chain.getValue(1);
3956
3957 DAG.addCallSiteInfo(Chain.getNode(), std::move(CSInfo));
3958
3959 // Create the CALLSEQ_END node in the case of where it is not a call to
3960 // memcpy.
3961 if (!(MemcpyInByVal)) {
3962 Chain = DAG.getCALLSEQ_END(Chain, StackSize, 0, InGlue, DL);
3963 InGlue = Chain.getValue(1);
3964 }
3965
3966 // Handle result values, copying them out of physregs into vregs that we
3967 // return.
3968 return LowerCallResult(Chain, InGlue, CallConv, IsVarArg, Ins, DL, DAG,
3969 InVals, CLI);
3970}
3971
3972/// LowerCallResult - Lower the result values of a call into the
3973/// appropriate copies out of appropriate physical registers.
3974SDValue MipsTargetLowering::LowerCallResult(
3975 SDValue Chain, SDValue InGlue, CallingConv::ID CallConv, bool IsVarArg,
3976 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
3979 // Assign locations to each value returned by this call.
3981 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
3982 *DAG.getContext());
3983
3984 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
3985
3986 // Copy all of the result registers out of their specified physreg.
3987 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3988 CCValAssign &VA = RVLocs[i];
3989 assert(VA.isRegLoc() && "Can only return in registers!");
3990
3991 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
3992 RVLocs[i].getLocVT(), InGlue);
3993 Chain = Val.getValue(1);
3994 InGlue = Val.getValue(2);
3995
3996 if (VA.isUpperBitsInLoc()) {
3997 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
3998 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3999 unsigned Shift =
4001 Val = DAG.getNode(
4002 Shift, DL, VA.getLocVT(), Val,
4003 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
4004 }
4005
4006 switch (VA.getLocInfo()) {
4007 default:
4008 llvm_unreachable("Unknown loc info!");
4009 case CCValAssign::Full:
4010 break;
4011 case CCValAssign::BCvt:
4012 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
4013 break;
4014 case CCValAssign::AExt:
4016 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
4017 break;
4018 case CCValAssign::ZExt:
4020 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
4021 DAG.getValueType(VA.getValVT()));
4022 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
4023 break;
4024 case CCValAssign::SExt:
4026 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
4027 DAG.getValueType(VA.getValVT()));
4028 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
4029 break;
4030 }
4031
4032 InVals.push_back(Val);
4033 }
4034
4035 return Chain;
4036}
4037
4039 EVT ArgVT, const SDLoc &DL,
4040 SelectionDAG &DAG) {
4041 MVT LocVT = VA.getLocVT();
4042 EVT ValVT = VA.getValVT();
4043
4044 // Shift into the upper bits if necessary.
4045 switch (VA.getLocInfo()) {
4046 default:
4047 break;
4051 unsigned ValSizeInBits = ArgVT.getSizeInBits();
4052 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
4053 unsigned Opcode =
4055 Val = DAG.getNode(
4056 Opcode, DL, VA.getLocVT(), Val,
4057 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
4058 break;
4059 }
4060 }
4061
4062 // If this is an value smaller than the argument slot size (32-bit for O32,
4063 // 64-bit for N32/N64), it has been promoted in some way to the argument slot
4064 // size. Extract the value and insert any appropriate assertions regarding
4065 // sign/zero extension.
4066 switch (VA.getLocInfo()) {
4067 default:
4068 llvm_unreachable("Unknown loc info!");
4069 case CCValAssign::Full:
4070 break;
4072 case CCValAssign::AExt:
4073 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
4074 break;
4076 case CCValAssign::SExt:
4077 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT));
4078 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
4079 break;
4081 case CCValAssign::ZExt:
4082 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT));
4083 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
4084 break;
4085 case CCValAssign::BCvt:
4086 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
4087 break;
4088 }
4089
4090 return Val;
4091}
4092
4093//===----------------------------------------------------------------------===//
4094// Formal Arguments Calling Convention Implementation
4095//===----------------------------------------------------------------------===//
4096/// LowerFormalArguments - transform physical registers into virtual registers
4097/// and generate load operations for arguments places on the stack.
4098SDValue MipsTargetLowering::LowerFormalArguments(
4099 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
4100 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
4101 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
4103 MachineFrameInfo &MFI = MF.getFrameInfo();
4104 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
4105
4106 MipsFI->setVarArgsFrameIndex(0);
4107
4108 // Used with vargs to acumulate store chains.
4109 std::vector<SDValue> OutChains;
4110
4111 // Assign locations to all of the incoming arguments.
4113 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
4114 *DAG.getContext());
4115 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), Align(1));
4117 Function::const_arg_iterator FuncArg = Func.arg_begin();
4118
4119 if (Func.hasFnAttribute("interrupt") && !Func.arg_empty())
4121 "Functions with the interrupt attribute cannot have arguments!");
4122
4123 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg);
4124 MipsFI->setFormalArgInfo(CCInfo.getStackSize(),
4125 CCInfo.getInRegsParamsCount() > 0);
4126
4127 unsigned CurArgIdx = 0;
4128 CCInfo.rewindByValRegsInfo();
4129
4130 for (unsigned i = 0, e = ArgLocs.size(), InsIdx = 0; i != e; ++i, ++InsIdx) {
4131 CCValAssign &VA = ArgLocs[i];
4132 if (Ins[InsIdx].isOrigArg()) {
4133 std::advance(FuncArg, Ins[InsIdx].getOrigArgIndex() - CurArgIdx);
4134 CurArgIdx = Ins[InsIdx].getOrigArgIndex();
4135 }
4136 EVT ValVT = VA.getValVT();
4137 ISD::ArgFlagsTy Flags = Ins[InsIdx].Flags;
4138 bool IsRegLoc = VA.isRegLoc();
4139
4140 if (Flags.isByVal()) {
4141 assert(Ins[InsIdx].isOrigArg() && "Byval arguments cannot be implicit");
4142 unsigned FirstByValReg, LastByValReg;
4143 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
4144 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
4145
4146 assert(Flags.getByValSize() &&
4147 "ByVal args of size 0 should have been ignored by front-end.");
4148 assert(ByValIdx < CCInfo.getInRegsParamsCount());
4149 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
4150 FirstByValReg, LastByValReg, VA, CCInfo);
4151 CCInfo.nextInRegsParam();
4152 continue;
4153 }
4154
4155 // Arguments stored on registers
4156 if (IsRegLoc) {
4157 MVT RegVT = VA.getLocVT();
4158 Register ArgReg = VA.getLocReg();
4159 const TargetRegisterClass *RC = getRegClassFor(RegVT);
4160
4161 // Transform the arguments stored on
4162 // physical registers into virtual ones
4163 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
4164 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
4165
4166 ArgValue =
4167 UnpackFromArgumentSlot(ArgValue, VA, Ins[InsIdx].ArgVT, DL, DAG);
4168
4169 // Handle floating point arguments passed in integer registers and
4170 // long double arguments passed in floating point registers.
4171 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
4172 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
4173 (RegVT == MVT::f64 && ValVT == MVT::i64))
4174 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
4175 else if (ABI.IsO32() && RegVT == MVT::i32 &&
4176 ValVT == MVT::f64) {
4177 assert(VA.needsCustom() && "Expected custom argument for f64 split");
4178 CCValAssign &NextVA = ArgLocs[++i];
4179 unsigned Reg2 =
4180 addLiveIn(DAG.getMachineFunction(), NextVA.getLocReg(), RC);
4181 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
4182 if (!Subtarget.isLittle())
4183 std::swap(ArgValue, ArgValue2);
4184 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
4185 ArgValue, ArgValue2);
4186 }
4187
4188 InVals.push_back(ArgValue);
4189 } else { // VA.isRegLoc()
4190 MVT LocVT = VA.getLocVT();
4191
4192 assert(!VA.needsCustom() && "unexpected custom memory argument");
4193
4194 // Only arguments pased on the stack should make it here.
4195 assert(VA.isMemLoc());
4196
4197 // The stack pointer offset is relative to the caller stack frame.
4198 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8,
4199 VA.getLocMemOffset(), true);
4200
4201 // Create load nodes to retrieve arguments from the stack
4202 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
4203 SDValue ArgValue = DAG.getLoad(
4204 LocVT, DL, Chain, FIN,
4206 OutChains.push_back(ArgValue.getValue(1));
4207
4208 ArgValue =
4209 UnpackFromArgumentSlot(ArgValue, VA, Ins[InsIdx].ArgVT, DL, DAG);
4210
4211 InVals.push_back(ArgValue);
4212 }
4213 }
4214
4215 for (unsigned i = 0, e = ArgLocs.size(), InsIdx = 0; i != e; ++i, ++InsIdx) {
4216
4217 if (ArgLocs[i].needsCustom()) {
4218 ++i;
4219 continue;
4220 }
4221
4222 // The mips ABIs for returning structs by value requires that we copy
4223 // the sret argument into $v0 for the return. Save the argument into
4224 // a virtual register so that we can access it from the return points.
4225 if (Ins[InsIdx].Flags.isSRet()) {
4226 unsigned Reg = MipsFI->getSRetReturnReg();
4227 if (!Reg) {
4229 getRegClassFor(ABI.IsN64() ? MVT::i64 : MVT::i32));
4230 MipsFI->setSRetReturnReg(Reg);
4231 }
4232 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
4233 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
4234 break;
4235 }
4236 }
4237
4238 if (IsVarArg)
4239 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo);
4240
4241 // All stores are grouped in one node to allow the matching between
4242 // the size of Ins and InVals. This only happens when on varg functions
4243 if (!OutChains.empty()) {
4244 OutChains.push_back(Chain);
4245 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
4246 }
4247
4248 return Chain;
4249}
4250
4251//===----------------------------------------------------------------------===//
4252// Return Value Calling Convention Implementation
4253//===----------------------------------------------------------------------===//
4254
4255bool
4256MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
4257 MachineFunction &MF, bool IsVarArg,
4259 LLVMContext &Context, const Type *RetTy) const {
4261 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
4262 return CCInfo.CheckReturn(Outs, RetCC_Mips);
4263}
4264
4265bool MipsTargetLowering::shouldSignExtendTypeInLibCall(Type *Ty,
4266 bool IsSigned) const {
4267 if ((ABI.IsN32() || ABI.IsN64()) && Ty->isIntegerTy(32))
4268 return true;
4269
4270 return IsSigned;
4271}
4272
4273SDValue
4274MipsTargetLowering::LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
4275 const SDLoc &DL,
4276 SelectionDAG &DAG) const {
4278 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
4279
4280 MipsFI->setISR();
4281
4282 return DAG.getNode(MipsISD::ERet, DL, MVT::Other, RetOps);
4283}
4284
4285SDValue
4286MipsTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
4287 bool IsVarArg,
4289 const SmallVectorImpl<SDValue> &OutVals,
4290 const SDLoc &DL, SelectionDAG &DAG) const {
4291 // CCValAssign - represent the assignment of
4292 // the return value to a location
4295
4296 // CCState - Info about the registers and stack slot.
4297 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
4298
4299 // Analyze return values.
4300 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
4301
4302 SDValue Glue;
4303 SmallVector<SDValue, 4> RetOps(1, Chain);
4304
4305 // Copy the result values into the output registers.
4306 for (unsigned i = 0; i != RVLocs.size(); ++i) {
4307 SDValue Val = OutVals[i];
4308 CCValAssign &VA = RVLocs[i];
4309 assert(VA.isRegLoc() && "Can only return in registers!");
4310 bool UseUpperBits = false;
4311
4312 switch (VA.getLocInfo()) {
4313 default:
4314 llvm_unreachable("Unknown loc info!");
4315 case CCValAssign::Full:
4316 break;
4317 case CCValAssign::BCvt:
4318 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
4319 break;
4321 UseUpperBits = true;
4322 [[fallthrough]];
4323 case CCValAssign::AExt:
4324 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
4325 break;
4327 UseUpperBits = true;
4328 [[fallthrough]];
4329 case CCValAssign::ZExt:
4330 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
4331 break;
4333 UseUpperBits = true;
4334 [[fallthrough]];
4335 case CCValAssign::SExt:
4336 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
4337 break;
4338 }
4339
4340 if (UseUpperBits) {
4341 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
4342 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
4343 Val = DAG.getNode(
4344 ISD::SHL, DL, VA.getLocVT(), Val,
4345 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
4346 }
4347
4348 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Glue);
4349
4350 // Guarantee that all emitted copies are stuck together with flags.
4351 Glue = Chain.getValue(1);
4352 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
4353 }
4354
4355 // The mips ABIs for returning structs by value requires that we copy
4356 // the sret argument into $v0 for the return. We saved the argument into
4357 // a virtual register in the entry block, so now we copy the value out
4358 // and into $v0.
4359 if (MF.getFunction().hasStructRetAttr()) {
4360 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
4361 unsigned Reg = MipsFI->getSRetReturnReg();
4362
4363 if (!Reg)
4364 llvm_unreachable("sret virtual register not created in the entry block");
4365 SDValue Val =
4366 DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(DAG.getDataLayout()));
4367 unsigned V0 = ABI.getReturnRegPtr(0);
4368
4369 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Glue);
4370 Glue = Chain.getValue(1);
4371 RetOps.push_back(DAG.getRegister(V0, getPointerTy(DAG.getDataLayout())));
4372 }
4373
4374 RetOps[0] = Chain; // Update chain.
4375
4376 // Add the glue if we have it.
4377 if (Glue.getNode())
4378 RetOps.push_back(Glue);
4379
4380 // ISRs must use "eret".
4381 if (DAG.getMachineFunction().getFunction().hasFnAttribute("interrupt"))
4382 return LowerInterruptReturn(RetOps, DL, DAG);
4383
4384 // Standard return on Mips is a "jr $ra"
4385 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
4386}
4387
4388//===----------------------------------------------------------------------===//
4389// Mips Inline Assembly Support
4390//===----------------------------------------------------------------------===//
4391
4392/// getConstraintType - Given a constraint letter, return the type of
4393/// constraint it is for this target.
4395MipsTargetLowering::getConstraintType(StringRef Constraint) const {
4396 // Mips specific constraints
4397 // GCC config/mips/constraints.md
4398 //
4399 // 'd' : An address register. Equivalent to r
4400 // unless generating MIPS16 code.
4401 // 'y' : Equivalent to r; retained for
4402 // backwards compatibility.
4403 // 'c' : A register suitable for use in an indirect
4404 // jump. This will always be $25 for -mabicalls.
4405 // 'l' : The lo register. 1 word storage.
4406 // 'x' : The hilo register pair. Double word storage.
4407 if (Constraint.size() == 1) {
4408 switch (Constraint[0]) {
4409 default : break;
4410 case 'd':
4411 case 'y':
4412 case 'f':
4413 case 'c':
4414 case 'l':
4415 case 'x':
4416 return C_RegisterClass;
4417 case 'R':
4418 return C_Memory;
4419 }
4420 }
4421
4422 if (Constraint == "ZC")
4423 return C_Memory;
4424
4425 return TargetLowering::getConstraintType(Constraint);
4426}
4427
4428/// Examine constraint type and operand type and determine a weight value.
4429/// This object must already have been set up with the operand type
4430/// and the current alternative constraint selected.
4432MipsTargetLowering::getSingleConstraintMatchWeight(
4433 AsmOperandInfo &info, const char *constraint) const {
4435 Value *CallOperandVal = info.CallOperandVal;
4436 // If we don't have a value, we can't do a match,
4437 // but allow it at the lowest weight.
4438 if (!CallOperandVal)
4439 return CW_Default;
4440 Type *type = CallOperandVal->getType();
4441 // Look at the constraint type.
4442 switch (*constraint) {
4443 default:
4445 break;
4446 case 'd':
4447 case 'y':
4448 if (type->isIntegerTy())
4450 break;
4451 case 'f': // FPU or MSA register
4452 if (Subtarget.hasMSA() && type->isVectorTy() &&
4453 type->getPrimitiveSizeInBits().getFixedValue() == 128)
4455 else if (type->isFloatTy())
4457 break;
4458 case 'c': // $25 for indirect jumps
4459 case 'l': // lo register
4460 case 'x': // hilo register pair
4461 if (type->isIntegerTy())
4463 break;
4464 case 'I': // signed 16 bit immediate
4465 case 'J': // integer zero
4466 case 'K': // unsigned 16 bit immediate
4467 case 'L': // signed 32 bit immediate where lower 16 bits are 0
4468 case 'N': // immediate in the range of -65535 to -1 (inclusive)
4469 case 'O': // signed 15 bit immediate (+- 16383)
4470 case 'P': // immediate in the range of 65535 to 1 (inclusive)
4471 if (isa<ConstantInt>(CallOperandVal))
4473 break;
4474 case 'R':
4475 weight = CW_Memory;
4476 break;
4477 }
4478 return weight;
4479}
4480
4481/// This is a helper function to parse a physical register string and split it
4482/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
4483/// that is returned indicates whether parsing was successful. The second flag
4484/// is true if the numeric part exists.
4485static std::pair<bool, bool> parsePhysicalReg(StringRef C, StringRef &Prefix,
4486 unsigned long long &Reg) {
4487 if (C.front() != '{' || C.back() != '}')
4488 return std::make_pair(false, false);
4489
4490 // Search for the first numeric character.
4491 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
4492 I = std::find_if(B, E, isdigit);
4493
4494 Prefix = StringRef(B, I - B);
4495
4496 // The second flag is set to false if no numeric characters were found.
4497 if (I == E)
4498 return std::make_pair(true, false);
4499
4500 // Parse the numeric characters.
4501 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
4502 true);
4503}
4504
4506 ISD::NodeType) const {
4507 bool Cond = !Subtarget.isABI_O32() && VT.getSizeInBits() == 32;
4508 EVT MinVT = getRegisterType(Context, Cond ? MVT::i64 : MVT::i32);
4509 return VT.bitsLT(MinVT) ? MinVT : VT;
4510}
4511
4512std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
4513parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
4514 const TargetRegisterInfo *TRI =
4515 Subtarget.getRegisterInfo();
4516 const TargetRegisterClass *RC;
4517 StringRef Prefix;
4518 unsigned long long Reg;
4519
4520 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
4521
4522 if (!R.first)
4523 return std::make_pair(0U, nullptr);
4524
4525 for (unsigned RegClassID : {Mips::HI32RegClassID, Mips::LO32RegClassID}) {
4527 Prefix, *TRI, RegClassID, Mips::RegAliasName)) {
4528 // No numeric characters follow a hi/lo register name.
4529 if (R.second)
4530 return std::make_pair(0U, nullptr);
4531 return std::make_pair(NamedReg.id(), TRI->getRegClass(RegClassID));
4532 }
4533 }
4534
4535 if (Prefix.starts_with("$msa")) {
4536 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
4537
4538 // No numeric characters follow the name.
4539 if (R.second)
4540 return std::make_pair(0U, nullptr);
4541
4542 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
4544 Prefix.drop_front(), *TRI, Mips::MSACtrlRegClassID, Mips::RegAliasName);
4545 if (!Reg)
4546 return std::make_pair(0U, nullptr);
4547
4548 return std::make_pair(Reg, RC);
4549 }
4550
4551 if (!R.second)
4552 return std::make_pair(0U, nullptr);
4553
4554 if (Prefix == "$f") { // Parse $f0-$f31.
4555 // If the targets is single float only, always select 32-bit registers,
4556 // otherwise if the size of FP registers is 64-bit or Reg is an even number,
4557 // select the 64-bit register class. Otherwise, select the 32-bit register
4558 // class.
4559 if (VT == MVT::Other) {
4560 if (Subtarget.isSingleFloat())
4561 VT = MVT::f32;
4562 else
4563 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
4564 }
4565
4566 RC = getRegClassFor(VT);
4567
4568 if (RC == &Mips::AFGR64RegClass) {
4569 assert(Reg % 2 == 0);
4570 Reg >>= 1;
4571 }
4572 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
4573 RC = TRI->getRegClass(Mips::FCCRegClassID);
4574 else if (Prefix == "$w") { // Parse $w0-$w31.
4575 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
4576 } else { // Parse $0-$31.
4577 assert(Prefix == "$");
4578 // $0-$31 always name GPRs, whatever the type of the operand. Use the
4579 // 64-bit GPRs for values wider than 32 bits on 64-bit targets and the
4580 // 32-bit GPRs otherwise. Narrower integers are extended into the
4581 // register, and values that do not fit are split across consecutive
4582 // registers.
4583 if (VT != MVT::Other && VT.getFixedSizeInBits() > 32 &&
4584 Subtarget.isGP64bit())
4585 RC = &Mips::GPR64RegClass;
4586 else
4587 RC = &Mips::GPR32RegClass;
4588 }
4589
4590 assert(Reg < RC->getNumRegs());
4591 return std::make_pair(*(RC->begin() + Reg), RC);
4592}
4593
4594/// Given a register class constraint, like 'r', if this corresponds directly
4595/// to an LLVM register class, return a register of 0 and the register class
4596/// pointer.
4597std::pair<unsigned, const TargetRegisterClass *>
4598MipsTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
4599 StringRef Constraint,
4600 MVT VT) const {
4601 if (Constraint.size() == 1) {
4602 switch (Constraint[0]) {
4603 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
4604 case 'y': // Same as 'r'. Exists for compatibility.
4605 case 'r':
4606 if ((VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8 ||
4607 VT == MVT::i1) ||
4608 (VT == MVT::f32 && Subtarget.useSoftFloat())) {
4609 if (Subtarget.inMips16Mode())
4610 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
4611 return std::make_pair(0U, &Mips::GPR32RegClass);
4612 }
4613 if ((VT == MVT::i64 || (VT == MVT::f64 && Subtarget.useSoftFloat()) ||
4614 (VT == MVT::f64 && Subtarget.isSingleFloat())) &&
4615 !Subtarget.isGP64bit())
4616 return std::make_pair(0U, &Mips::GPR32RegClass);
4617 if ((VT == MVT::i64 || (VT == MVT::f64 && Subtarget.useSoftFloat()) ||
4618 (VT == MVT::f64 && Subtarget.isSingleFloat())) &&
4619 Subtarget.isGP64bit())
4620 return std::make_pair(0U, &Mips::GPR64RegClass);
4621 // This will generate an error message
4622 return std::make_pair(0U, nullptr);
4623 case 'f': // FPU or MSA register
4624 if (VT == MVT::v16i8)
4625 return std::make_pair(0U, &Mips::MSA128BRegClass);
4626 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
4627 return std::make_pair(0U, &Mips::MSA128HRegClass);
4628 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
4629 return std::make_pair(0U, &Mips::MSA128WRegClass);
4630 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
4631 return std::make_pair(0U, &Mips::MSA128DRegClass);
4632 else if (VT == MVT::f32)
4633 return std::make_pair(0U, &Mips::FGR32RegClass);
4634 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
4635 if (Subtarget.isFP64bit())
4636 return std::make_pair(0U, &Mips::FGR64RegClass);
4637 return std::make_pair(0U, &Mips::AFGR64RegClass);
4638 }
4639 break;
4640 case 'c': // register suitable for indirect jump
4641 if (VT == MVT::i32)
4642 return std::make_pair(ABI.getTempReg(9, false).id(),
4643 &Mips::GPR32RegClass);
4644 if (VT == MVT::i64)
4645 return std::make_pair(ABI.getTempReg(9, true).id(),
4646 &Mips::GPR64RegClass);
4647 // This will generate an error message
4648 return std::make_pair(0U, nullptr);
4649 case 'l': // use the `lo` register to store values
4650 // that are no bigger than a word
4651 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8)
4652 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
4653 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
4654 case 'x': // use the concatenated `hi` and `lo` registers
4655 // to store doubleword values
4656 // Fixme: Not triggering the use of both hi and low
4657 // This will generate an error message
4658 return std::make_pair(0U, nullptr);
4659 }
4660 }
4661
4662 if (!Constraint.empty()) {
4663 std::pair<unsigned, const TargetRegisterClass *> R;
4664 R = parseRegForInlineAsmConstraint(Constraint, VT);
4665
4666 if (R.second)
4667 return R;
4668 }
4669
4670 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
4671}
4672
4673/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4674/// vector. If it is invalid, don't add anything to Ops.
4675void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4676 StringRef Constraint,
4677 std::vector<SDValue> &Ops,
4678 SelectionDAG &DAG) const {
4679 SDLoc DL(Op);
4681
4682 // Only support length 1 constraints for now.
4683 if (Constraint.size() > 1)
4684 return;
4685
4686 char ConstraintLetter = Constraint[0];
4687 switch (ConstraintLetter) {
4688 default: break; // This will fall through to the generic implementation
4689 case 'I': // Signed 16 bit constant
4690 // If this fails, the parent routine will give an error
4691 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4692 EVT Type = Op.getValueType();
4693 int64_t Val = C->getSExtValue();
4694 if (isInt<16>(Val)) {
4696 break;
4697 }
4698 }
4699 return;
4700 case 'J': // integer zero
4701 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4702 EVT Type = Op.getValueType();
4703 int64_t Val = C->getZExtValue();
4704 if (Val == 0) {
4705 Result = DAG.getTargetConstant(0, DL, Type);
4706 break;
4707 }
4708 }
4709 return;
4710 case 'K': // unsigned 16 bit immediate
4711 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4712 EVT Type = Op.getValueType();
4713 uint64_t Val = C->getZExtValue();
4714 if (isUInt<16>(Val)) {
4715 Result = DAG.getTargetConstant(Val, DL, Type);
4716 break;
4717 }
4718 }
4719 return;
4720 case 'L': // signed 32 bit immediate where lower 16 bits are 0
4721 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4722 EVT Type = Op.getValueType();
4723 int64_t Val = C->getSExtValue();
4724 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
4726 break;
4727 }
4728 }
4729 return;
4730 case 'N': // immediate in the range of -65535 to -1 (inclusive)
4731 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4732 EVT Type = Op.getValueType();
4733 int64_t Val = C->getSExtValue();
4734 if ((Val >= -65535) && (Val <= -1)) {
4736 break;
4737 }
4738 }
4739 return;
4740 case 'O': // signed 15 bit immediate
4741 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4742 EVT Type = Op.getValueType();
4743 int64_t Val = C->getSExtValue();
4744 if ((isInt<15>(Val))) {
4746 break;
4747 }
4748 }
4749 return;
4750 case 'P': // immediate in the range of 1 to 65535 (inclusive)
4751 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4752 EVT Type = Op.getValueType();
4753 int64_t Val = C->getSExtValue();
4754 if ((Val <= 65535) && (Val >= 1)) {
4755 Result = DAG.getTargetConstant(Val, DL, Type);
4756 break;
4757 }
4758 }
4759 return;
4760 }
4761
4762 if (Result.getNode()) {
4763 Ops.push_back(Result);
4764 return;
4765 }
4766
4768}
4769
4770bool MipsTargetLowering::isLegalAddressingMode(const DataLayout &DL,
4771 const AddrMode &AM, Type *Ty,
4772 unsigned AS,
4773 Instruction *I) const {
4774 // No global is ever allowed as a base.
4775 if (AM.BaseGV)
4776 return false;
4777
4778 switch (AM.Scale) {
4779 case 0: // "r+i" or just "i", depending on HasBaseReg.
4780 break;
4781 case 1:
4782 if (!AM.HasBaseReg) // allow "r+i".
4783 break;
4784 return false; // disallow "r+r" or "r+r+i".
4785 default:
4786 return false;
4787 }
4788
4789 return true;
4790}
4791
4792bool
4793MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4794 // The Mips target isn't yet aware of offsets.
4795 return false;
4796}
4797
4798EVT MipsTargetLowering::getOptimalMemOpType(
4799 LLVMContext &Context, const MemOp &Op,
4800 const AttributeList &FuncAttributes) const {
4801 if (Subtarget.isGP64bit())
4802 return MVT::i64;
4803
4804 return MVT::i32;
4805}
4806
4808MipsTargetLowering::getFPImmAction(const APFloat &Imm, EVT VT,
4809 bool ForCodeSize) const {
4810 if (VT != MVT::f32 && VT != MVT::f64)
4812 if (Imm.isNegZero())
4815}
4816
4817bool MipsTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
4818 return isInt<16>(Imm);
4819}
4820
4821bool MipsTargetLowering::isLegalAddImmediate(int64_t Imm) const {
4822 return isInt<16>(Imm);
4823}
4824
4826 SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG,
4827 unsigned Depth) const {
4828 switch (Op.getOpcode()) {
4829 case MipsISD::R6FCmp:
4830 case MipsISD::STRICT_FPCmpR6:
4831 case MipsISD::STRICT_FPCmpsR6:
4832 // cmp.cond.fmt writes 0 or -1.
4833 return Op.getScalarValueSizeInBits();
4834 }
4835
4836 // The low word of an f64 compare mask is taken with EXTRACT_SUBREG, so that
4837 // FP users can read it from the FPR. Each word of a value that is 0 or -1 is
4838 // also 0 or -1.
4839 if (Op.isMachineOpcode() &&
4840 Op.getMachineOpcode() == TargetOpcode::EXTRACT_SUBREG &&
4841 Op.getConstantOperandVal(1) == Mips::sub_lo &&
4842 DAG.ComputeNumSignBits(Op.getOperand(0), Depth + 1) == 64)
4843 return 32;
4844 return 1;
4845}
4846
4848 if (!isPositionIndependent())
4850 if (ABI.IsN64())
4853}
4854
4855SDValue MipsTargetLowering::getPICJumpTableRelocBase(SDValue Table,
4856 SelectionDAG &DAG) const {
4857 if (!isPositionIndependent())
4858 return Table;
4860}
4861
4863 return Subtarget.useSoftFloat();
4864}
4865
4866void MipsTargetLowering::copyByValRegs(
4867 SDValue Chain, const SDLoc &DL, std::vector<SDValue> &OutChains,
4868 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
4869 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
4870 unsigned FirstReg, unsigned LastReg, const CCValAssign &VA,
4871 MipsCCState &State) const {
4873 MachineFrameInfo &MFI = MF.getFrameInfo();
4874 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
4875 unsigned NumRegs = LastReg - FirstReg;
4876 unsigned RegAreaSize = NumRegs * GPRSizeInBytes;
4877 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
4878 int FrameObjOffset;
4879 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs();
4880
4881 if (RegAreaSize)
4882 FrameObjOffset =
4883 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
4884 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes);
4885 else
4886 FrameObjOffset = VA.getLocMemOffset();
4887
4888 // Create frame object.
4889 EVT PtrTy = getPointerTy(DAG.getDataLayout());
4890 // Make the fixed object stored to mutable so that the load instructions
4891 // referencing it have their memory dependencies added.
4892 // Set the frame object as isAliased which clears the underlying objects
4893 // vector in ScheduleDAGInstrs::buildSchedGraph() resulting in addition of all
4894 // stores as dependencies for loads referencing this fixed object.
4895 int FI = MFI.CreateFixedObject(FrameObjSize, FrameObjOffset, false, true);
4896 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4897 InVals.push_back(FIN);
4898
4899 if (!NumRegs)
4900 return;
4901
4902 // Copy arg registers.
4903 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
4904 const TargetRegisterClass *RC = getRegClassFor(RegTy);
4905
4906 for (unsigned I = 0; I < NumRegs; ++I) {
4907 unsigned ArgReg = ByValArgRegs[FirstReg + I];
4908 unsigned VReg = addLiveIn(MF, ArgReg, RC);
4909 unsigned Offset = I * GPRSizeInBytes;
4910 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
4911 DAG.getConstant(Offset, DL, PtrTy));
4912 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
4913 StorePtr, MachinePointerInfo(FuncArg, Offset));
4914 OutChains.push_back(Store);
4915 }
4916}
4917
4918// Copy byVal arg to registers and stack.
4919void MipsTargetLowering::passByValArg(
4920 SDValue Chain, const SDLoc &DL,
4921 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
4922 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
4923 MachineFrameInfo &MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg,
4924 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle,
4925 const CCValAssign &VA) const {
4926 unsigned ByValSizeInBytes = Flags.getByValSize();
4927 unsigned OffsetInBytes = 0; // From beginning of struct
4928 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
4930 std::min(Flags.getNonZeroByValAlign(), Align(RegSizeInBytes));
4931 EVT PtrTy = getPointerTy(DAG.getDataLayout()),
4932 RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
4933 unsigned NumRegs = LastReg - FirstReg;
4934
4935 if (NumRegs) {
4936 ArrayRef<MCPhysReg> ArgRegs = ABI.GetByValArgRegs();
4937 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes);
4938 unsigned I = 0;
4939
4940 // Copy words to registers.
4941 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) {
4942 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
4943 DAG.getConstant(OffsetInBytes, DL, PtrTy));
4944 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
4945 MachinePointerInfo(), Alignment);
4946 MemOpChains.push_back(LoadVal.getValue(1));
4947 unsigned ArgReg = ArgRegs[FirstReg + I];
4948 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
4949 }
4950
4951 // Return if the struct has been fully copied.
4952 if (ByValSizeInBytes == OffsetInBytes)
4953 return;
4954
4955 // Copy the remainder of the byval argument with sub-word loads and shifts.
4956 if (LeftoverBytes) {
4957 SDValue Val;
4958
4959 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
4960 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
4961 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
4962
4963 if (RemainingSizeInBytes < LoadSizeInBytes)
4964 continue;
4965
4966 // Load subword.
4967 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
4968 DAG.getConstant(OffsetInBytes, DL,
4969 PtrTy));
4970 SDValue LoadVal = DAG.getExtLoad(
4971 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
4972 MVT::getIntegerVT(LoadSizeInBytes * 8), Alignment);
4973 MemOpChains.push_back(LoadVal.getValue(1));
4974
4975 // Shift the loaded value.
4976 unsigned Shamt;
4977
4978 if (isLittle)
4979 Shamt = TotalBytesLoaded * 8;
4980 else
4981 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
4982
4983 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
4984 DAG.getConstant(Shamt, DL, MVT::i32));
4985
4986 if (Val.getNode())
4987 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
4988 else
4989 Val = Shift;
4990
4991 OffsetInBytes += LoadSizeInBytes;
4992 TotalBytesLoaded += LoadSizeInBytes;
4993 Alignment = std::min(Alignment, Align(LoadSizeInBytes));
4994 }
4995
4996 unsigned ArgReg = ArgRegs[FirstReg + I];
4997 RegsToPass.push_back(std::make_pair(ArgReg, Val));
4998 return;
4999 }
5000 }
5001
5002 // Copy remainder of byval arg to it with memcpy.
5003 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
5004 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
5005 DAG.getConstant(OffsetInBytes, DL, PtrTy));
5006 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
5008 Chain = DAG.getMemcpy(
5009 Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, DL, PtrTy), Alignment,
5010 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
5011 /*CI=*/nullptr, std::nullopt, MachinePointerInfo(), MachinePointerInfo());
5012 MemOpChains.push_back(Chain);
5013}
5014
5015void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
5016 SDValue Chain, const SDLoc &DL,
5017 SelectionDAG &DAG,
5018 CCState &State) const {
5019 ArrayRef<MCPhysReg> ArgRegs = ABI.getVarArgRegs(Subtarget.isGP64bit());
5020 unsigned Idx = State.getFirstUnallocated(ArgRegs);
5021 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
5022 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
5023 const TargetRegisterClass *RC = getRegClassFor(RegTy);
5025 MachineFrameInfo &MFI = MF.getFrameInfo();
5026 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
5027
5028 // Offset of the first variable argument from stack pointer.
5029 int VaArgOffset;
5030
5031 if (ArgRegs.size() == Idx)
5032 VaArgOffset = alignTo(State.getStackSize(), RegSizeInBytes);
5033 else {
5034 VaArgOffset =
5035 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
5036 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
5037 }
5038
5039 // Record the frame index of the first variable argument
5040 // which is a value necessary to VASTART.
5041 int FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
5042 MipsFI->setVarArgsFrameIndex(FI);
5043
5044 // Copy the integer registers that have not been used for argument passing
5045 // to the argument register save area. For O32, the save area is allocated
5046 // in the caller's stack frame, while for N32/64, it is allocated in the
5047 // callee's stack frame.
5048 for (unsigned I = Idx; I < ArgRegs.size();
5049 ++I, VaArgOffset += RegSizeInBytes) {
5050 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
5051 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
5052 FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
5053 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
5054 SDValue Store =
5055 DAG.getStore(Chain, DL, ArgValue, PtrOff, MachinePointerInfo());
5056 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
5057 (Value *)nullptr);
5058 OutChains.push_back(Store);
5059 }
5060}
5061
5063 Align Alignment) const {
5064 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
5065
5066 assert(Size && "Byval argument's size shouldn't be 0.");
5067
5068 Alignment = std::min(Alignment, TFL->getStackAlign());
5069
5070 unsigned FirstReg = 0;
5071 unsigned NumRegs = 0;
5072
5073 if (State->getCallingConv() != CallingConv::Fast) {
5074 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
5075 ArrayRef<MCPhysReg> IntArgRegs = ABI.GetByValArgRegs();
5076 // FIXME: The O32 case actually describes no shadow registers.
5077 const MCPhysReg *ShadowRegs =
5078 ABI.IsO32() ? IntArgRegs.data() : Mips64DPRegs;
5079
5080 // We used to check the size as well but we can't do that anymore since
5081 // CCState::HandleByVal() rounds up the size after calling this function.
5082 assert(
5083 Alignment >= Align(RegSizeInBytes) &&
5084 "Byval argument's alignment should be a multiple of RegSizeInBytes.");
5085
5086 FirstReg = State->getFirstUnallocated(IntArgRegs);
5087
5088 // If Alignment > RegSizeInBytes, the first arg register must be even.
5089 // FIXME: This condition happens to do the right thing but it's not the
5090 // right way to test it. We want to check that the stack frame offset
5091 // of the register is aligned.
5092 if ((Alignment > RegSizeInBytes) && (FirstReg % 2)) {
5093 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]);
5094 ++FirstReg;
5095 }
5096
5097 // Mark the registers allocated.
5098 Size = alignTo(Size, RegSizeInBytes);
5099 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size());
5100 Size -= RegSizeInBytes, ++I, ++NumRegs)
5101 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]);
5102 }
5103
5104 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs);
5105}
5106
5107MachineBasicBlock *MipsTargetLowering::emitPseudoSELECT(MachineInstr &MI,
5109 bool isFPCmp,
5110 unsigned Opc) const {
5111 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
5112 "Subtarget already supports SELECT nodes with the use of"
5113 "conditional-move instructions.");
5114
5115 const TargetInstrInfo *TII =
5116 Subtarget.getInstrInfo();
5117 DebugLoc DL = MI.getDebugLoc();
5118
5119 // To "insert" a SELECT instruction, we actually have to insert the
5120 // diamond control-flow pattern. The incoming instruction knows the
5121 // destination vreg to set, the condition code register to branch on, the
5122 // true/false values to select between, and a branch opcode to use.
5123 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5125
5126 // thisMBB:
5127 // ...
5128 // TrueVal = ...
5129 // setcc r1, r2, r3
5130 // bNE r1, r0, copy1MBB
5131 // fallthrough --> copy0MBB
5132 MachineBasicBlock *thisMBB = BB;
5133 MachineFunction *F = BB->getParent();
5134 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5135 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
5136 F->insert(It, copy0MBB);
5137 F->insert(It, sinkMBB);
5138
5139 // Transfer the remainder of BB and its successor edges to sinkMBB.
5140 sinkMBB->splice(sinkMBB->begin(), BB,
5141 std::next(MachineBasicBlock::iterator(MI)), BB->end());
5143
5144 // Next, add the true and fallthrough blocks as its successors.
5145 BB->addSuccessor(copy0MBB);
5146 BB->addSuccessor(sinkMBB);
5147
5148 if (isFPCmp) {
5149 // bc1[tf] cc, sinkMBB
5150 BuildMI(BB, DL, TII->get(Opc))
5151 .addReg(MI.getOperand(1).getReg())
5152 .addMBB(sinkMBB);
5153 } else {
5154 // bne rs, $0, sinkMBB
5155 BuildMI(BB, DL, TII->get(Opc))
5156 .addReg(MI.getOperand(1).getReg())
5157 .addReg(Mips::R0)
5158 .addMBB(sinkMBB);
5159 }
5160
5161 // copy0MBB:
5162 // %FalseValue = ...
5163 // # fallthrough to sinkMBB
5164 BB = copy0MBB;
5165
5166 // Update machine-CFG edges
5167 BB->addSuccessor(sinkMBB);
5168
5169 // sinkMBB:
5170 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
5171 // ...
5172 BB = sinkMBB;
5173
5174 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
5175 .addReg(MI.getOperand(2).getReg())
5176 .addMBB(thisMBB)
5177 .addReg(MI.getOperand(3).getReg())
5178 .addMBB(copy0MBB);
5179
5180 MI.eraseFromParent(); // The pseudo instruction is gone now.
5181
5182 return BB;
5183}
5184
5186MipsTargetLowering::emitPseudoD_SELECT(MachineInstr &MI,
5187 MachineBasicBlock *BB) const {
5188 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
5189 "Subtarget already supports SELECT nodes with the use of"
5190 "conditional-move instructions.");
5191
5192 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
5193 DebugLoc DL = MI.getDebugLoc();
5194
5195 // D_SELECT substitutes two SELECT nodes that goes one after another and
5196 // have the same condition operand. On machines which don't have
5197 // conditional-move instruction, it reduces unnecessary branch instructions
5198 // which are result of using two diamond patterns that are result of two
5199 // SELECT pseudo instructions.
5200 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5202
5203 // thisMBB:
5204 // ...
5205 // TrueVal = ...
5206 // setcc r1, r2, r3
5207 // bNE r1, r0, copy1MBB
5208 // fallthrough --> copy0MBB
5209 MachineBasicBlock *thisMBB = BB;
5210 MachineFunction *F = BB->getParent();
5211 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5212 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
5213 F->insert(It, copy0MBB);
5214 F->insert(It, sinkMBB);
5215
5216 // Transfer the remainder of BB and its successor edges to sinkMBB.
5217 sinkMBB->splice(sinkMBB->begin(), BB,
5218 std::next(MachineBasicBlock::iterator(MI)), BB->end());
5220
5221 // Next, add the true and fallthrough blocks as its successors.
5222 BB->addSuccessor(copy0MBB);
5223 BB->addSuccessor(sinkMBB);
5224
5225 // bne rs, $0, sinkMBB
5226 BuildMI(BB, DL, TII->get(Mips::BNE))
5227 .addReg(MI.getOperand(2).getReg())
5228 .addReg(Mips::R0)
5229 .addMBB(sinkMBB);
5230
5231 // copy0MBB:
5232 // %FalseValue = ...
5233 // # fallthrough to sinkMBB
5234 BB = copy0MBB;
5235
5236 // Update machine-CFG edges
5237 BB->addSuccessor(sinkMBB);
5238
5239 // sinkMBB:
5240 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
5241 // ...
5242 BB = sinkMBB;
5243
5244 // Use two PHI nodes to select two reults
5245 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
5246 .addReg(MI.getOperand(3).getReg())
5247 .addMBB(thisMBB)
5248 .addReg(MI.getOperand(5).getReg())
5249 .addMBB(copy0MBB);
5250 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(1).getReg())
5251 .addReg(MI.getOperand(4).getReg())
5252 .addMBB(thisMBB)
5253 .addReg(MI.getOperand(6).getReg())
5254 .addMBB(copy0MBB);
5255
5256 MI.eraseFromParent(); // The pseudo instruction is gone now.
5257
5258 return BB;
5259}
5260
5263 const MachineFunction &MF) const {
5264 StringRef Name(RegName);
5265 Name.consume_front("$");
5266
5267 unsigned RegIdx;
5268 if (Name.getAsInteger(10, RegIdx)) {
5269 std::string LowerName = Name.lower();
5271 int Index =
5272 MIPS_MC::getCPURegisterIndex(LowerName, MRI, ABI.getRegAltNameIndex());
5273 if (Index < 0)
5275 Twine("Invalid register name \"" + StringRef(RegName) + "\"."));
5276 RegIdx = Index;
5277 }
5278
5279 if (RegIdx < 32) {
5281 unsigned RegClassID = Mips::GPR32RegClassID;
5282 if (VT.isValid()) {
5283 if (VT.getSizeInBits() == 64) {
5284 if (!Subtarget.isGP64bit())
5285 report_fatal_error("64-bit registers not supported on 32-bit target");
5286 RegClassID = Mips::GPR64RegClassID;
5287 } else if (VT.getSizeInBits() == 32) {
5288 RegClassID = Mips::GPR32RegClassID;
5289 } else {
5290 report_fatal_error(Twine("Invalid register \"" + StringRef(RegName) +
5291 "\" for " + Twine(VT.getSizeInBits()) +
5292 "-bit type."));
5293 }
5294 } else if (Subtarget.isGP64bit()) {
5295 RegClassID = Mips::GPR64RegClassID;
5296 }
5297 const MCRegisterClass &RC = MRI->getRegClass(RegClassID);
5298 Register Reg = RC.getRegister(RegIdx);
5299 BitVector ReservedRegs = Subtarget.getRegisterInfo()->getReservedRegs(MF);
5300 if (!ReservedRegs.test(Reg))
5301 reportFatalUsageError(Twine("Trying to obtain non-reserved register \"" +
5302 StringRef(RegName) + "\"."));
5303 return Reg;
5304 }
5305
5307 Twine("Invalid register name \"" + StringRef(RegName) + "\"."));
5308}
5309
5310MachineBasicBlock *MipsTargetLowering::emitLDR_W(MachineInstr &MI,
5311 MachineBasicBlock *BB) const {
5312 MachineFunction *MF = BB->getParent();
5314 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
5315 const bool IsLittle = Subtarget.isLittle();
5316 DebugLoc DL = MI.getDebugLoc();
5317
5318 Register Dest = MI.getOperand(0).getReg();
5319 Register Address = MI.getOperand(1).getReg();
5320 unsigned Imm = MI.getOperand(2).getImm();
5321
5323
5324 if (Subtarget.hasMips32r6() || Subtarget.hasMips64r6()) {
5325 // Mips release 6 can load from adress that is not naturally-aligned.
5326 Register Temp = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5327 BuildMI(*BB, I, DL, TII->get(Mips::LW))
5328 .addDef(Temp)
5329 .addUse(Address)
5330 .addImm(Imm);
5331 BuildMI(*BB, I, DL, TII->get(Mips::FILL_W)).addDef(Dest).addUse(Temp);
5332 } else {
5333 // Mips release 5 needs to use instructions that can load from an unaligned
5334 // memory address.
5335 Register LoadHalf = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5336 Register LoadFull = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5337 Register Undef = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5338 BuildMI(*BB, I, DL, TII->get(Mips::IMPLICIT_DEF)).addDef(Undef);
5339 BuildMI(*BB, I, DL, TII->get(Mips::LWR))
5340 .addDef(LoadHalf)
5341 .addUse(Address)
5342 .addImm(Imm + (IsLittle ? 0 : 3))
5343 .addUse(Undef);
5344 BuildMI(*BB, I, DL, TII->get(Mips::LWL))
5345 .addDef(LoadFull)
5346 .addUse(Address)
5347 .addImm(Imm + (IsLittle ? 3 : 0))
5348 .addUse(LoadHalf);
5349 BuildMI(*BB, I, DL, TII->get(Mips::FILL_W)).addDef(Dest).addUse(LoadFull);
5350 }
5351
5352 MI.eraseFromParent();
5353 return BB;
5354}
5355
5356MachineBasicBlock *MipsTargetLowering::emitLDR_D(MachineInstr &MI,
5357 MachineBasicBlock *BB) const {
5358 MachineFunction *MF = BB->getParent();
5359 MachineRegisterInfo &MRI = MF->getRegInfo();
5360 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
5361 const bool IsLittle = Subtarget.isLittle();
5362 DebugLoc DL = MI.getDebugLoc();
5363
5364 Register Dest = MI.getOperand(0).getReg();
5365 Register Address = MI.getOperand(1).getReg();
5366 unsigned Imm = MI.getOperand(2).getImm();
5367
5369
5370 if (Subtarget.hasMips32r6() || Subtarget.hasMips64r6()) {
5371 // Mips release 6 can load from adress that is not naturally-aligned.
5372 if (Subtarget.isGP64bit()) {
5373 Register Temp = MRI.createVirtualRegister(&Mips::GPR64RegClass);
5374 BuildMI(*BB, I, DL, TII->get(Mips::LD))
5375 .addDef(Temp)
5376 .addUse(Address)
5377 .addImm(Imm);
5378 BuildMI(*BB, I, DL, TII->get(Mips::FILL_D)).addDef(Dest).addUse(Temp);
5379 } else {
5380 Register Wtemp = MRI.createVirtualRegister(&Mips::MSA128WRegClass);
5381 Register Lo = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5382 Register Hi = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5383 BuildMI(*BB, I, DL, TII->get(Mips::LW))
5384 .addDef(Lo)
5385 .addUse(Address)
5386 .addImm(Imm + (IsLittle ? 0 : 4));
5387 BuildMI(*BB, I, DL, TII->get(Mips::LW))
5388 .addDef(Hi)
5389 .addUse(Address)
5390 .addImm(Imm + (IsLittle ? 4 : 0));
5391 BuildMI(*BB, I, DL, TII->get(Mips::FILL_W)).addDef(Wtemp).addUse(Lo);
5392 BuildMI(*BB, I, DL, TII->get(Mips::INSERT_W), Dest)
5393 .addUse(Wtemp)
5394 .addUse(Hi)
5395 .addImm(1);
5396 }
5397 } else {
5398 // Mips release 5 needs to use instructions that can load from an unaligned
5399 // memory address.
5400 Register LoHalf = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5401 Register LoFull = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5402 Register LoUndef = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5403 Register HiHalf = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5404 Register HiFull = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5405 Register HiUndef = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5406 Register Wtemp = MRI.createVirtualRegister(&Mips::MSA128WRegClass);
5407 BuildMI(*BB, I, DL, TII->get(Mips::IMPLICIT_DEF)).addDef(LoUndef);
5408 BuildMI(*BB, I, DL, TII->get(Mips::LWR))
5409 .addDef(LoHalf)
5410 .addUse(Address)
5411 .addImm(Imm + (IsLittle ? 0 : 7))
5412 .addUse(LoUndef);
5413 BuildMI(*BB, I, DL, TII->get(Mips::LWL))
5414 .addDef(LoFull)
5415 .addUse(Address)
5416 .addImm(Imm + (IsLittle ? 3 : 4))
5417 .addUse(LoHalf);
5418 BuildMI(*BB, I, DL, TII->get(Mips::IMPLICIT_DEF)).addDef(HiUndef);
5419 BuildMI(*BB, I, DL, TII->get(Mips::LWR))
5420 .addDef(HiHalf)
5421 .addUse(Address)
5422 .addImm(Imm + (IsLittle ? 4 : 3))
5423 .addUse(HiUndef);
5424 BuildMI(*BB, I, DL, TII->get(Mips::LWL))
5425 .addDef(HiFull)
5426 .addUse(Address)
5427 .addImm(Imm + (IsLittle ? 7 : 0))
5428 .addUse(HiHalf);
5429 BuildMI(*BB, I, DL, TII->get(Mips::FILL_W)).addDef(Wtemp).addUse(LoFull);
5430 BuildMI(*BB, I, DL, TII->get(Mips::INSERT_W), Dest)
5431 .addUse(Wtemp)
5432 .addUse(HiFull)
5433 .addImm(1);
5434 }
5435
5436 MI.eraseFromParent();
5437 return BB;
5438}
5439
5440MachineBasicBlock *MipsTargetLowering::emitSTR_W(MachineInstr &MI,
5441 MachineBasicBlock *BB) const {
5442 MachineFunction *MF = BB->getParent();
5443 MachineRegisterInfo &MRI = MF->getRegInfo();
5444 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
5445 const bool IsLittle = Subtarget.isLittle();
5446 DebugLoc DL = MI.getDebugLoc();
5447
5448 Register StoreVal = MI.getOperand(0).getReg();
5449 Register Address = MI.getOperand(1).getReg();
5450 unsigned Imm = MI.getOperand(2).getImm();
5451
5453
5454 if (Subtarget.hasMips32r6() || Subtarget.hasMips64r6()) {
5455 // Mips release 6 can store to adress that is not naturally-aligned.
5456 Register BitcastW = MRI.createVirtualRegister(&Mips::MSA128WRegClass);
5457 Register Tmp = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5458 BuildMI(*BB, I, DL, TII->get(Mips::COPY)).addDef(BitcastW).addUse(StoreVal);
5459 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W))
5460 .addDef(Tmp)
5461 .addUse(BitcastW)
5462 .addImm(0);
5463 BuildMI(*BB, I, DL, TII->get(Mips::SW))
5464 .addUse(Tmp)
5465 .addUse(Address)
5466 .addImm(Imm);
5467 } else {
5468 // Mips release 5 needs to use instructions that can store to an unaligned
5469 // memory address.
5470 Register Tmp = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5471 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W))
5472 .addDef(Tmp)
5473 .addUse(StoreVal)
5474 .addImm(0);
5475 BuildMI(*BB, I, DL, TII->get(Mips::SWR))
5476 .addUse(Tmp)
5477 .addUse(Address)
5478 .addImm(Imm + (IsLittle ? 0 : 3));
5479 BuildMI(*BB, I, DL, TII->get(Mips::SWL))
5480 .addUse(Tmp)
5481 .addUse(Address)
5482 .addImm(Imm + (IsLittle ? 3 : 0));
5483 }
5484
5485 MI.eraseFromParent();
5486
5487 return BB;
5488}
5489
5490MachineBasicBlock *MipsTargetLowering::emitSTR_D(MachineInstr &MI,
5491 MachineBasicBlock *BB) const {
5492 MachineFunction *MF = BB->getParent();
5493 MachineRegisterInfo &MRI = MF->getRegInfo();
5494 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
5495 const bool IsLittle = Subtarget.isLittle();
5496 DebugLoc DL = MI.getDebugLoc();
5497
5498 Register StoreVal = MI.getOperand(0).getReg();
5499 Register Address = MI.getOperand(1).getReg();
5500 unsigned Imm = MI.getOperand(2).getImm();
5501
5503
5504 if (Subtarget.hasMips32r6() || Subtarget.hasMips64r6()) {
5505 // Mips release 6 can store to adress that is not naturally-aligned.
5506 if (Subtarget.isGP64bit()) {
5507 Register BitcastD = MRI.createVirtualRegister(&Mips::MSA128DRegClass);
5508 Register Lo = MRI.createVirtualRegister(&Mips::GPR64RegClass);
5509 BuildMI(*BB, I, DL, TII->get(Mips::COPY))
5510 .addDef(BitcastD)
5511 .addUse(StoreVal);
5512 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_D))
5513 .addDef(Lo)
5514 .addUse(BitcastD)
5515 .addImm(0);
5516 BuildMI(*BB, I, DL, TII->get(Mips::SD))
5517 .addUse(Lo)
5518 .addUse(Address)
5519 .addImm(Imm);
5520 } else {
5521 Register BitcastW = MRI.createVirtualRegister(&Mips::MSA128WRegClass);
5522 Register Lo = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5523 Register Hi = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5524 BuildMI(*BB, I, DL, TII->get(Mips::COPY))
5525 .addDef(BitcastW)
5526 .addUse(StoreVal);
5527 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W))
5528 .addDef(Lo)
5529 .addUse(BitcastW)
5530 .addImm(0);
5531 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W))
5532 .addDef(Hi)
5533 .addUse(BitcastW)
5534 .addImm(1);
5535 BuildMI(*BB, I, DL, TII->get(Mips::SW))
5536 .addUse(Lo)
5537 .addUse(Address)
5538 .addImm(Imm + (IsLittle ? 0 : 4));
5539 BuildMI(*BB, I, DL, TII->get(Mips::SW))
5540 .addUse(Hi)
5541 .addUse(Address)
5542 .addImm(Imm + (IsLittle ? 4 : 0));
5543 }
5544 } else {
5545 // Mips release 5 needs to use instructions that can store to an unaligned
5546 // memory address.
5547 Register Bitcast = MRI.createVirtualRegister(&Mips::MSA128WRegClass);
5548 Register Lo = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5549 Register Hi = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5550 BuildMI(*BB, I, DL, TII->get(Mips::COPY)).addDef(Bitcast).addUse(StoreVal);
5551 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W))
5552 .addDef(Lo)
5553 .addUse(Bitcast)
5554 .addImm(0);
5555 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W))
5556 .addDef(Hi)
5557 .addUse(Bitcast)
5558 .addImm(1);
5559 BuildMI(*BB, I, DL, TII->get(Mips::SWR))
5560 .addUse(Lo)
5561 .addUse(Address)
5562 .addImm(Imm + (IsLittle ? 0 : 3));
5563 BuildMI(*BB, I, DL, TII->get(Mips::SWL))
5564 .addUse(Lo)
5565 .addUse(Address)
5566 .addImm(Imm + (IsLittle ? 3 : 0));
5567 BuildMI(*BB, I, DL, TII->get(Mips::SWR))
5568 .addUse(Hi)
5569 .addUse(Address)
5570 .addImm(Imm + (IsLittle ? 4 : 7));
5571 BuildMI(*BB, I, DL, TII->get(Mips::SWL))
5572 .addUse(Hi)
5573 .addUse(Address)
5574 .addImm(Imm + (IsLittle ? 7 : 4));
5575 }
5576
5577 MI.eraseFromParent();
5578 return BB;
5579}
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)
return SDValue()
static SDValue performANDCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI)
const AArch64Subtarget & Subtarget
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
uint64_t Imm
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
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
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
hexagon merge activate weight
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 SDValue performADDCombine(SDNode *N, SelectionDAG &DAG, TargetLowering::DAGCombinerInfo &DCI, const LoongArchSubtarget &Subtarget)
static SDValue performSUBCombine(SDNode *N, SelectionDAG &DAG, TargetLowering::DAGCombinerInfo &DCI, const LoongArchSubtarget &Subtarget)
static MachineBasicBlock * insertDivByZeroTrap(MachineInstr &MI, MachineBasicBlock *MBB)
static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG, TargetLowering::DAGCombinerInfo &DCI, const LoongArchSubtarget &Subtarget)
SmallVector< MachineOperand, 4 > Cond
unsigned Opc
#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
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 convertRoundingMode(SDValue X, const SDLoc &DL, SelectionDAG &DAG)
static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op)
static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasExtractInsert)
DivByZeroTrapKind
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
RISCVCC::CondCode CC
MachineRegisterInfo & MRI
MachineInstr *& Lo
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
SI optimize exec mask operations pre RA
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:479
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:170
#define LLVM_DEBUG(...)
Definition Debug.h:119
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
Value * RHS
Value * LHS
bool isZero() const
Definition APFloat.h:1612
APInt bitcastToAPInt() const
Definition APFloat.h:1508
Class for arbitrary precision integers.
Definition APInt.h:78
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:225
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1560
LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition APInt.cpp:478
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
const T * data() const
Definition ArrayRef.h:138
LLVM Basic Block Representation.
Definition BasicBlock.h:61
bool test(unsigned Idx) const
Returns true if bit Idx is set.
Definition BitVector.h:483
static constexpr 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.
const APFloat & getValueAPF() const
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:63
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:126
const char * getSymbol() const
This is a fast-path instruction selection class that generates poor code and doesn't support illegal ...
Definition FastISel.h:67
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:673
const Argument * const_arg_iterator
Definition Function.h:74
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:731
const GlobalValue * getGlobal() const
bool isDSOLocal() const
bool hasLocalLinkage() const
bool hasPrivateLinkage() const
bool hasHiddenVisibility() const
bool hasDLLImportStorageClass() const
bool isDeclarationForLinker() const
LLVM_ABI const GlobalObject * getAliaseeObject() const
Definition Globals.cpp:521
bool hasProtectedVisibility() const
constexpr bool isValid() const
constexpr TypeSize getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
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...
Tracks which library functions to use for a particular subtarget or function.
This class is used to represent ISD::LOAD nodes.
const MCRegisterInfo * getRegisterInfo() const
Definition MCContext.h:411
MCRegisterClass - Base class of TargetRegisterClass.
MCRegister getRegister(unsigned i) const
getRegister - Return the specified register in the class.
iterator begin() const
begin/end - Return all of the registers in this class.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Wrapper class representing physical registers and target specific virtual registers.
Definition MCRegister.h:42
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
Machine Value Type.
SimpleValueType SimpleTy
static auto integer_valuetypes()
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
uint64_t getFixedSizeInBits() const
Return the size of the specified fixed width value type 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.
LLVM_ABI void computeMaxCallFrameSize(MachineFunction &MF, std::vector< MachineBasicBlock::iterator > *FrameSDOps=nullptr)
Computes the maximum size of a callframe.
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 & addUse(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, 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,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
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 the unique 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 inMips16Mode() const
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
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...
virtual void getOpndList(SmallVectorImpl< SDValue > &Ops, std::deque< std::pair< unsigned, SDValue > > &RegsToPass, bool IsPICCall, bool GlobalOrExternal, bool LocalLinkage, 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...
SDValue getAddrNonPIC(NodeTy *N, const SDLoc &DL, EVT Ty, SelectionDAG &DAG) const
SDValue lowerSTORE(SDValue Op, SelectionDAG &DAG) const
FastISel * createFastISel(FunctionLoweringInfo &funcInfo, const TargetLibraryInfo *libInfo, const LibcallLoweringInfo *libcallLowering) const override
createFastISel - This method returns a target specific FastISel object, or null if the target does no...
void AdjustInstrPostInstrSelection(MachineInstr &MI, SDNode *Node) const override
This method should be implemented by targets that mark instructions with the 'hasPostISelHook' flag.
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
unsigned ComputeNumSignBitsForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth) const override
This method can be implemented by targets that want to expose additional information about sign bits ...
SDValue getGlobalReg(SelectionDAG &DAG, EVT Ty) const
const MipsSubtarget & Subtarget
void HandleByVal(CCState *, unsigned &, Align) const override
Target-specific cleanup for formal ByVal parameters.
void finalizeLowering(MachineFunction &MF) const override
Execute target specific actions to finalize target lowering.
SDValue lowerLOAD(SDValue Op, SelectionDAG &DAG) const
bool IsConstantInSmallSection(const DataLayout &DL, const Constant *CN, const Function *F) 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, in exactly one operand.
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
ISD::CondCode getOperandCondCode(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...
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),...
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
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.
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false, SDNodeFlags Flags={})
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
LLVM_ABI SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
LLVM_ABI SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align DstAlign, Align SrcAlign, 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 getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const MMOMetadata &Metadata=MMOMetadata())
Helper function to build ISD::STORE nodes.
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 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 MMOMetadata &Metadata=MMOMetadata())
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 ...
LLVM_ABI SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
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 getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either sign-extending or trunca...
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const MMOMetadata &Metadata=MMOMetadata())
Loads are not normal binary operators: their result type is not determined by their operands,...
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)
LLVM_ABI unsigned ComputeNumSignBits(SDValue Op, unsigned Depth=0) const
Return the number of times the sign bit of the register is replicated into the other bits.
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)
LLVM_ABI SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
LLVM_ABI SDValue getCondCode(ISD::CondCode Cond)
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.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
const char * const_iterator
Definition StringRef.h:61
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
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 void finalizeLowering(MachineFunction &MF) const
Execute target specific actions to finalize target lowering.
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
void setHasExtractBitsInsn(bool hasExtractInsn=true)
Tells the code generator that the target has BitExtract instructions.
MVT getRegisterType(LLVMContext &Context, EVT VT) const
Return the type of registers that this ValueType will eventually require.
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 a boolean value from i1 to a wider type.
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...
FPImmAction
How the legalizer should materialize a floating-point immediate.
@ ConstantPool
Load the immediate from the constant pool.
@ Legal
The target can instruction select the immediate natively.
@ Bitcast
Build the immediate as a bitcast of an integer constant with the same bits.
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.
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...
void setTypeIdForCallsiteInfo(const CallBase *CB, MachineFunction &MF, MachineFunction::CallSiteInfo &CSInfo) const
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
MCSymbol * getSymbol(const GlobalValue *GV) const
unsigned EnableFastISel
EnableFastISel - This flag enables fast-path instruction selection which trades away generated code q...
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:46
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:283
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition Type.h:155
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:187
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:252
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:298
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:222
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:314
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:201
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:43
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:851
@ 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:516
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:811
@ 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:266
@ 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:885
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:523
@ GlobalAddress
Definition ISDOpcodes.h:90
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:420
@ MEMBARRIER
MEMBARRIER - Compiler barrier only; generate a no-op.
@ 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:282
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:256
@ GlobalTLSAddress
Definition ISDOpcodes.h:91
@ EH_RETURN
OUTCHAIN = EH_RETURN(INCHAIN, OFFSET, HANDLER) - This node represents 'eh_return' gcc dwarf builtin,...
Definition ISDOpcodes.h:158
@ SET_ROUNDING
Set rounding mode.
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:876
@ TargetJumpTable
Definition ISDOpcodes.h:190
@ FSINCOS
FSINCOS - Compute both fsin and fcos as a single operation.
@ FNEG
Perform various unary floating-point operations inspired by libm.
@ 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:546
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition ISDOpcodes.h:553
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:828
@ 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:83
@ GET_ROUNDING
Returns current rounding mode: -1 Undefined 0 Round to 0 1 Round to nearest, ties to even 2 Round to ...
@ STRICT_FP_TO_FP16
@ STRICT_FP16_TO_FP
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:793
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Same as FMINNUM/FMAXNUM, except that a signaling NaN operand deterministi...
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:882
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:843
@ FMINNUM
FMINNUM/FMAXNUM - Same semantics as the llvm.minnum/maxnum intrinsics.
@ 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:920
@ EH_DWARF_CFA
EH_DWARF_CFA - This node represents the pointer to the DWARF Canonical Frame Address (CFA),...
Definition ISDOpcodes.h:152
@ FRAMEADDR
FRAMEADDR, RETURNADDR - These nodes represent llvm.frameaddress and llvm.returnaddress on the DAG.
Definition ISDOpcodes.h:112
@ STRICT_FP_TO_SINT
STRICT_FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:482
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:958
@ READCYCLECOUNTER
READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:763
@ TRAP
TRAP - Trapping instruction.
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:55
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:888
@ 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:865
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:64
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:539
@ 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).
@ 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.
MCRegister matchRegisterName(StringRef Name, const MCRegisterInfo &MRI, unsigned RegClassID, unsigned AltIdx)
Match a symbolic name in RegClassID, or return an invalid register.
int getCPURegisterIndex(StringRef Name, const MCRegisterInfo &MRI, unsigned AltIdx, bool *IsDeprecated=nullptr)
Return a GPR name's hardware index, or -1 if unknown.
constexpr MCPhysReg FP_64
FastISel * createFastISel(FunctionLoweringInfo &funcInfo, const TargetLibraryInfo *libInfo, const LibcallLoweringInfo *libcallLowering)
constexpr MCPhysReg GP_64
constexpr MCPhysReg FP
constexpr MCPhysReg GP
constexpr MCPhysReg RA_64
constexpr MCPhysReg SP
constexpr MCPhysReg SP_64
constexpr MCPhysReg RA
Not(const Pred &P) -> Not< Pred >
@ SingleThread
Synchronized with respect to signal handlers executing in the same thread.
Definition LLVMContext.h:63
@ System
Synchronized with respect to all concurrently executing threads.
Definition LLVMContext.h:66
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
This is an optimization pass for GlobalISel generic memory operations.
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
Definition Threading.h:280
@ Offset
Definition DWP.cpp:577
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
LLVM_ABI bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Dead
Unused definition.
@ Undef
Value of the register doesn't matter.
@ EarlyClobber
Register definition happens before uses.
@ Define
Register definition.
constexpr RegState getKillRegState(bool B)
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.
@ Store
The extracted value is stored (ExtractElement only).
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
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:274
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
@ Off
Use window algorithm after SMS algorithm fails.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:149
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
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:389
@ 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.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
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
LLVM_ABI bool isOneConstant(SDValue V)
Returns true if V is a constant integer one.
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.
constexpr T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
Definition MathExtras.h:78
LLVM_ABI bool isAllOnesConstant(SDValue V)
Returns true if V is an integer constant with all bits set.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:881
#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:82
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:90
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:338
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:411
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:423
bool isPow2VectorType() const
Returns true if the given vector is a power of 2.
Definition ValueTypes.h:516
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:354
static EVT getFloatingPointVT(unsigned BitWidth)
Returns the EVT that represents a floating-point type with the given number of bits.
Definition ValueTypes.h:55
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:191
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:286
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:366
bool isScalarInteger() const
Return true if this is an integer, but not a vector.
Definition ValueTypes.h:180
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:374
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:175
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:111
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