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