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, 0, ArePtrs64bit ? Mips::sub_32 : 0).addImm(3);
1804 if (Subtarget.isLittle()) {
1805 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1806 } else {
1807 Register Off = RegInfo.createVirtualRegister(RC);
1808 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1809 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1810 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1811 }
1812 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
1813 .addReg(Mips::ZERO).addImm(MaskImm);
1814 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
1815 .addReg(MaskUpper).addReg(ShiftAmt);
1816 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1817 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
1818
1819
1820 // The purposes of the flags on the scratch registers is explained in
1821 // emitAtomicBinary. In summary, we need a scratch register which is going to
1822 // be undef, that is unique among registers chosen for the instruction.
1823
1824 MachineInstrBuilder MIB =
1825 BuildMI(BB, DL, TII->get(AtomicOp))
1827 .addReg(AlignedAddr)
1828 .addReg(Incr2)
1829 .addReg(Mask)
1830 .addReg(Mask2)
1831 .addReg(ShiftAmt)
1838 if (NeedsAdditionalReg) {
1839 Register Scratch4 = RegInfo.createVirtualRegister(RC);
1842 }
1843
1844 MI.eraseFromParent(); // The instruction is gone now.
1845
1846 return exitMBB;
1847}
1848
1849// Lower atomic compare and swap to a pseudo instruction, taking care to
1850// define a scratch register for the pseudo instruction's expansion. The
1851// instruction is expanded after the register allocator as to prevent
1852// the insertion of stores between the linked load and the store conditional.
1853
1855MipsTargetLowering::emitAtomicCmpSwap(MachineInstr &MI,
1856 MachineBasicBlock *BB) const {
1857
1858 assert((MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32 ||
1859 MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I64) &&
1860 "Unsupported atomic pseudo for EmitAtomicCmpSwap.");
1861
1862 const unsigned Size = MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32 ? 4 : 8;
1863
1864 MachineFunction *MF = BB->getParent();
1865 MachineRegisterInfo &MRI = MF->getRegInfo();
1866 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
1867 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1868 DebugLoc DL = MI.getDebugLoc();
1869
1870 unsigned AtomicOp = MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32
1871 ? Mips::ATOMIC_CMP_SWAP_I32_POSTRA
1872 : Mips::ATOMIC_CMP_SWAP_I64_POSTRA;
1873 Register Dest = MI.getOperand(0).getReg();
1874 Register Ptr = MI.getOperand(1).getReg();
1875 Register OldVal = MI.getOperand(2).getReg();
1876 Register NewVal = MI.getOperand(3).getReg();
1877
1878 Register Scratch = MRI.createVirtualRegister(RC);
1880
1881 // We need to create copies of the various registers and kill them at the
1882 // atomic pseudo. If the copies are not made, when the atomic is expanded
1883 // after fast register allocation, the spills will end up outside of the
1884 // blocks that their values are defined in, causing livein errors.
1885
1886 Register PtrCopy = MRI.createVirtualRegister(MRI.getRegClass(Ptr));
1887 Register OldValCopy = MRI.createVirtualRegister(MRI.getRegClass(OldVal));
1888 Register NewValCopy = MRI.createVirtualRegister(MRI.getRegClass(NewVal));
1889
1890 BuildMI(*BB, II, DL, TII->get(Mips::COPY), PtrCopy).addReg(Ptr);
1891 BuildMI(*BB, II, DL, TII->get(Mips::COPY), OldValCopy).addReg(OldVal);
1892 BuildMI(*BB, II, DL, TII->get(Mips::COPY), NewValCopy).addReg(NewVal);
1893
1894 // The purposes of the flags on the scratch registers is explained in
1895 // emitAtomicBinary. In summary, we need a scratch register which is going to
1896 // be undef, that is unique among registers chosen for the instruction.
1897
1898 BuildMI(*BB, II, DL, TII->get(AtomicOp))
1900 .addReg(PtrCopy, RegState::Kill)
1901 .addReg(OldValCopy, RegState::Kill)
1902 .addReg(NewValCopy, RegState::Kill)
1905
1906 MI.eraseFromParent(); // The instruction is gone now.
1907
1908 return BB;
1909}
1910
1911MachineBasicBlock *MipsTargetLowering::emitAtomicCmpSwapPartword(
1912 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size) const {
1913 assert((Size == 1 || Size == 2) &&
1914 "Unsupported size for EmitAtomicCmpSwapPartial.");
1915
1916 MachineFunction *MF = BB->getParent();
1917 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1918 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1919 const bool ArePtrs64bit = ABI.ArePtrs64bit();
1920 const TargetRegisterClass *RCp =
1921 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32);
1922 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1923 DebugLoc DL = MI.getDebugLoc();
1924
1925 Register Dest = MI.getOperand(0).getReg();
1926 Register Ptr = MI.getOperand(1).getReg();
1927 Register CmpVal = MI.getOperand(2).getReg();
1928 Register NewVal = MI.getOperand(3).getReg();
1929
1930 Register AlignedAddr = RegInfo.createVirtualRegister(RCp);
1931 Register ShiftAmt = RegInfo.createVirtualRegister(RC);
1932 Register Mask = RegInfo.createVirtualRegister(RC);
1933 Register Mask2 = RegInfo.createVirtualRegister(RC);
1934 Register ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1935 Register ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1936 Register MaskLSB2 = RegInfo.createVirtualRegister(RCp);
1937 Register PtrLSB2 = RegInfo.createVirtualRegister(RC);
1938 Register MaskUpper = RegInfo.createVirtualRegister(RC);
1939 Register MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1940 Register MaskedNewVal = RegInfo.createVirtualRegister(RC);
1941 unsigned AtomicOp = MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I8
1942 ? Mips::ATOMIC_CMP_SWAP_I8_POSTRA
1943 : Mips::ATOMIC_CMP_SWAP_I16_POSTRA;
1944
1945 // The scratch registers here with the EarlyClobber | Define | Dead | Implicit
1946 // flags are used to coerce the register allocator and the machine verifier to
1947 // accept the usage of these registers.
1948 // The EarlyClobber flag has the semantic properties that the operand it is
1949 // attached to is clobbered before the rest of the inputs are read. Hence it
1950 // must be unique among the operands to the instruction.
1951 // The Define flag is needed to coerce the machine verifier that an Undef
1952 // value isn't a problem.
1953 // The Dead flag is needed as the value in scratch isn't used by any other
1954 // instruction. Kill isn't used as Dead is more precise.
1955 Register Scratch = RegInfo.createVirtualRegister(RC);
1956 Register Scratch2 = RegInfo.createVirtualRegister(RC);
1957
1958 // insert new blocks after the current block
1959 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1960 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1962 MF->insert(It, exitMBB);
1963
1964 // Transfer the remainder of BB and its successor edges to exitMBB.
1965 exitMBB->splice(exitMBB->begin(), BB,
1966 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1968
1970
1971 // thisMBB:
1972 // addiu masklsb2,$0,-4 # 0xfffffffc
1973 // and alignedaddr,ptr,masklsb2
1974 // andi ptrlsb2,ptr,3
1975 // xori ptrlsb2,ptrlsb2,3 # Only for BE
1976 // sll shiftamt,ptrlsb2,3
1977 // ori maskupper,$0,255 # 0xff
1978 // sll mask,maskupper,shiftamt
1979 // nor mask2,$0,mask
1980 // andi maskedcmpval,cmpval,255
1981 // sll shiftedcmpval,maskedcmpval,shiftamt
1982 // andi maskednewval,newval,255
1983 // sll shiftednewval,maskednewval,shiftamt
1984 int64_t MaskImm = (Size == 1) ? 255 : 65535;
1985 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::DADDiu : Mips::ADDiu), MaskLSB2)
1986 .addReg(ABI.GetNullPtr()).addImm(-4);
1987 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::AND64 : Mips::AND), AlignedAddr)
1988 .addReg(Ptr).addReg(MaskLSB2);
1989 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2)
1990 .addReg(Ptr, 0, ArePtrs64bit ? Mips::sub_32 : 0).addImm(3);
1991 if (Subtarget.isLittle()) {
1992 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1993 } else {
1994 Register Off = RegInfo.createVirtualRegister(RC);
1995 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1996 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1997 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1998 }
1999 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
2000 .addReg(Mips::ZERO).addImm(MaskImm);
2001 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
2002 .addReg(MaskUpper).addReg(ShiftAmt);
2003 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
2004 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
2005 .addReg(CmpVal).addImm(MaskImm);
2006 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
2007 .addReg(MaskedCmpVal).addReg(ShiftAmt);
2008 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
2009 .addReg(NewVal).addImm(MaskImm);
2010 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
2011 .addReg(MaskedNewVal).addReg(ShiftAmt);
2012
2013 // The purposes of the flags on the scratch registers are explained in
2014 // emitAtomicBinary. In summary, we need a scratch register which is going to
2015 // be undef, that is unique among the register chosen for the instruction.
2016
2017 BuildMI(BB, DL, TII->get(AtomicOp))
2019 .addReg(AlignedAddr)
2020 .addReg(Mask)
2021 .addReg(ShiftedCmpVal)
2022 .addReg(Mask2)
2023 .addReg(ShiftedNewVal)
2024 .addReg(ShiftAmt)
2029
2030 MI.eraseFromParent(); // The instruction is gone now.
2031
2032 return exitMBB;
2033}
2034
2035SDValue MipsTargetLowering::lowerREADCYCLECOUNTER(SDValue Op,
2036 SelectionDAG &DAG) const {
2038 SDLoc DL(Op);
2039 MachineFunction &MF = DAG.getMachineFunction();
2040 unsigned RdhwrOpc, DestReg;
2041 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2042
2043 if (PtrVT == MVT::i64) {
2044 RdhwrOpc = Mips::RDHWR64;
2045 DestReg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
2046 SDNode *Rdhwr = DAG.getMachineNode(RdhwrOpc, DL, MVT::i64, MVT::Glue,
2047 DAG.getRegister(Mips::HWR2, MVT::i32),
2048 DAG.getTargetConstant(0, DL, MVT::i32));
2049 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), DL, DestReg,
2050 SDValue(Rdhwr, 0), SDValue(Rdhwr, 1));
2051 SDValue ResNode =
2052 DAG.getCopyFromReg(Chain, DL, DestReg, MVT::i64, Chain.getValue(1));
2053 Results.push_back(ResNode);
2054 Results.push_back(ResNode.getValue(1));
2055 } else {
2056 RdhwrOpc = Mips::RDHWR;
2057 DestReg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
2058 SDNode *Rdhwr = DAG.getMachineNode(RdhwrOpc, DL, MVT::i32, MVT::Glue,
2059 DAG.getRegister(Mips::HWR2, MVT::i32),
2060 DAG.getTargetConstant(0, DL, MVT::i32));
2061 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), DL, DestReg,
2062 SDValue(Rdhwr, 0), SDValue(Rdhwr, 1));
2063 SDValue ResNode =
2064 DAG.getCopyFromReg(Chain, DL, DestReg, MVT::i32, Chain.getValue(1));
2065 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, ResNode,
2066 DAG.getConstant(0, DL, MVT::i32)));
2067 Results.push_back(ResNode.getValue(1));
2068 }
2069
2070 return DAG.getMergeValues(Results, DL);
2071}
2072
2073SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
2074 // The first operand is the chain, the second is the condition, the third is
2075 // the block to branch to if the condition is true.
2076 SDValue Chain = Op.getOperand(0);
2077 SDValue Dest = Op.getOperand(2);
2078 SDLoc DL(Op);
2079
2080 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
2081 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
2082
2083 // Return if flag is not set by a floating point comparison.
2084 if (CondRes.getOpcode() != MipsISD::FPCmp)
2085 return Op;
2086
2087 SDValue CCNode = CondRes.getOperand(2);
2090 SDValue BrCode = DAG.getConstant(Opc, DL, MVT::i32);
2091 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
2092 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
2093 FCC0, Dest, CondRes);
2094}
2095
2096SDValue MipsTargetLowering::
2097lowerSELECT(SDValue Op, SelectionDAG &DAG) const
2098{
2099 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
2100 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
2101
2102 // Return if flag is not set by a floating point comparison.
2103 if (Cond.getOpcode() != MipsISD::FPCmp)
2104 return Op;
2105
2106 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
2107 SDLoc(Op));
2108}
2109
2110SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
2111 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
2112 SDValue Cond = createFPCmp(DAG, Op);
2113
2114 assert(Cond.getOpcode() == MipsISD::FPCmp &&
2115 "Floating point operand expected.");
2116
2117 SDLoc DL(Op);
2118 SDValue True = DAG.getConstant(1, DL, MVT::i32);
2119 SDValue False = DAG.getConstant(0, DL, MVT::i32);
2120
2121 return createCMovFP(DAG, Cond, True, False, DL);
2122}
2123
2124SDValue MipsTargetLowering::lowerFSETCC(SDValue Op, SelectionDAG &DAG) const {
2125 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
2126
2127 SDLoc DL(Op);
2128 SDValue Chain = Op.getOperand(0);
2129 SDValue LHS = Op.getOperand(1);
2130 SDValue RHS = Op.getOperand(2);
2131 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(3))->get();
2132
2133 SDValue Cond = DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
2134 DAG.getConstant(condCodeToFCC(CC), DL, MVT::i32));
2135 SDValue True = DAG.getConstant(1, DL, MVT::i32);
2136 SDValue False = DAG.getConstant(0, DL, MVT::i32);
2137 SDValue CMovFP = createCMovFP(DAG, Cond, True, False, DL);
2138
2139 return DAG.getMergeValues({CMovFP, Chain}, DL);
2140}
2141
2142SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
2143 SelectionDAG &DAG) const {
2144 EVT Ty = Op.getValueType();
2145 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
2146 const GlobalValue *GV = N->getGlobal();
2147
2148 if (GV->hasDLLImportStorageClass()) {
2149 assert(Subtarget.isTargetWindows() &&
2150 "Windows is the only supported COFF target");
2151 return getDllimportVariable(
2152 N, SDLoc(N), Ty, DAG, DAG.getEntryNode(),
2154 }
2155
2156 if (!isPositionIndependent()) {
2157 const MipsTargetObjectFile *TLOF =
2158 static_cast<const MipsTargetObjectFile *>(
2160 const GlobalObject *GO = GV->getAliaseeObject();
2161 if (GO && TLOF->IsGlobalInSmallSection(GO, getTargetMachine()))
2162 // %gp_rel relocation
2163 return getAddrGPRel(N, SDLoc(N), Ty, DAG, ABI.IsN64());
2164
2165 // %hi/%lo relocation
2166 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2167 // %highest/%higher/%hi/%lo relocation
2168 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
2169 }
2170
2171 // Every other architecture would use shouldAssumeDSOLocal in here, but
2172 // mips is special.
2173 // * In PIC code mips requires got loads even for local statics!
2174 // * To save on got entries, for local statics the got entry contains the
2175 // page and an additional add instruction takes care of the low bits.
2176 // * It is legal to access a hidden symbol with a non hidden undefined,
2177 // so one cannot guarantee that all access to a hidden symbol will know
2178 // it is hidden.
2179 // * Mips linkers don't support creating a page and a full got entry for
2180 // the same symbol.
2181 // * Given all that, we have to use a full got entry for hidden symbols :-(
2182 if (GV->hasLocalLinkage())
2183 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
2184
2185 if (Subtarget.useXGOT())
2186 return getAddrGlobalLargeGOT(
2187 N, SDLoc(N), Ty, DAG, MipsII::MO_GOT_HI16, MipsII::MO_GOT_LO16,
2188 DAG.getEntryNode(),
2190
2191 return getAddrGlobal(
2192 N, SDLoc(N), Ty, DAG,
2193 (ABI.IsN32() || ABI.IsN64()) ? MipsII::MO_GOT_DISP : MipsII::MO_GOT,
2195}
2196
2197SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
2198 SelectionDAG &DAG) const {
2199 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
2200 EVT Ty = Op.getValueType();
2201
2202 if (!isPositionIndependent())
2203 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2204 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
2205
2206 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
2207}
2208
2209SDValue MipsTargetLowering::
2210lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
2211{
2212 // If the relocation model is PIC, use the General Dynamic TLS Model or
2213 // Local Dynamic TLS model, otherwise use the Initial Exec or
2214 // Local Exec TLS Model.
2215
2216 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2217 if (DAG.getTarget().useEmulatedTLS())
2218 return LowerToTLSEmulatedModel(GA, DAG);
2219
2220 SDLoc DL(GA);
2221 const GlobalValue *GV = GA->getGlobal();
2222 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2223
2225
2226 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
2227 // General Dynamic and Local Dynamic TLS Model.
2228 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
2229 : MipsII::MO_TLSGD;
2230
2231 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
2232 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
2233 getGlobalReg(DAG, PtrVT), TGA);
2234 unsigned PtrSize = PtrVT.getSizeInBits();
2235 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
2236
2237 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
2238
2240 Args.emplace_back(Argument, PtrTy);
2241
2242 TargetLowering::CallLoweringInfo CLI(DAG);
2243 CLI.setDebugLoc(DL)
2244 .setChain(DAG.getEntryNode())
2245 .setLibCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args));
2246 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
2247
2248 SDValue Ret = CallResult.first;
2249
2250 if (model != TLSModel::LocalDynamic)
2251 return Ret;
2252
2253 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2255 SDValue Hi = DAG.getNode(MipsISD::TlsHi, DL, PtrVT, TGAHi);
2256 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2258 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
2259 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
2260 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
2261 }
2262
2264 if (model == TLSModel::InitialExec) {
2265 // Initial Exec TLS Model
2266 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2268 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
2269 TGA);
2270 Offset =
2271 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), TGA, MachinePointerInfo());
2272 } else {
2273 // Local Exec TLS Model
2274 assert(model == TLSModel::LocalExec);
2275 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2277 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2279 SDValue Hi = DAG.getNode(MipsISD::TlsHi, DL, PtrVT, TGAHi);
2280 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
2281 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
2282 }
2283
2284 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
2285 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
2286}
2287
2288SDValue MipsTargetLowering::
2289lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
2290{
2291 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
2292 EVT Ty = Op.getValueType();
2293
2294 if (!isPositionIndependent())
2295 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2296 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
2297
2298 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
2299}
2300
2301SDValue MipsTargetLowering::
2302lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
2303{
2304 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
2305 EVT Ty = Op.getValueType();
2306
2307 if (!isPositionIndependent()) {
2308 const MipsTargetObjectFile *TLOF =
2309 static_cast<const MipsTargetObjectFile *>(
2311
2312 if (TLOF->IsConstantInSmallSection(DAG.getDataLayout(), N->getConstVal(),
2314 // %gp_rel relocation
2315 return getAddrGPRel(N, SDLoc(N), Ty, DAG, ABI.IsN64());
2316
2317 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2318 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
2319 }
2320
2321 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
2322}
2323
2324SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
2325 MachineFunction &MF = DAG.getMachineFunction();
2326 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
2327
2328 SDLoc DL(Op);
2329 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
2331
2332 // vastart just stores the address of the VarArgsFrameIndex slot into the
2333 // memory location argument.
2334 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
2335 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
2336 MachinePointerInfo(SV));
2337}
2338
2339SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
2340 SDNode *Node = Op.getNode();
2341 EVT VT = Node->getValueType(0);
2342 SDValue Chain = Node->getOperand(0);
2343 SDValue VAListPtr = Node->getOperand(1);
2344 const Align Align =
2345 llvm::MaybeAlign(Node->getConstantOperandVal(3)).valueOrOne();
2346 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2347 SDLoc DL(Node);
2348 unsigned ArgSlotSizeInBytes = (ABI.IsN32() || ABI.IsN64()) ? 8 : 4;
2349
2350 SDValue VAListLoad = DAG.getLoad(getPointerTy(DAG.getDataLayout()), DL, Chain,
2351 VAListPtr, MachinePointerInfo(SV));
2352 SDValue VAList = VAListLoad;
2353
2354 // Re-align the pointer if necessary.
2355 // It should only ever be necessary for 64-bit types on O32 since the minimum
2356 // argument alignment is the same as the maximum type alignment for N32/N64.
2357 //
2358 // FIXME: We currently align too often. The code generator doesn't notice
2359 // when the pointer is still aligned from the last va_arg (or pair of
2360 // va_args for the i64 on O32 case).
2361 if (Align > getMinStackArgumentAlignment()) {
2362 VAList = DAG.getNode(
2363 ISD::ADD, DL, VAList.getValueType(), VAList,
2364 DAG.getConstant(Align.value() - 1, DL, VAList.getValueType()));
2365
2366 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
2367 DAG.getSignedConstant(-(int64_t)Align.value(), DL,
2368 VAList.getValueType()));
2369 }
2370
2371 // Increment the pointer, VAList, to the next vaarg.
2372 auto &TD = DAG.getDataLayout();
2373 unsigned ArgSizeInBytes =
2375 SDValue Tmp3 =
2376 DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
2377 DAG.getConstant(alignTo(ArgSizeInBytes, ArgSlotSizeInBytes),
2378 DL, VAList.getValueType()));
2379 // Store the incremented VAList to the legalized pointer
2380 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
2381 MachinePointerInfo(SV));
2382
2383 // In big-endian mode we must adjust the pointer when the load size is smaller
2384 // than the argument slot size. We must also reduce the known alignment to
2385 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
2386 // the correct half of the slot, and reduce the alignment from 8 (slot
2387 // alignment) down to 4 (type alignment).
2388 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
2389 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
2390 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
2391 DAG.getIntPtrConstant(Adjustment, DL));
2392 }
2393 // Load the actual argument out of the pointer VAList
2394 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo());
2395}
2396
2398 bool HasExtractInsert) {
2399 EVT TyX = Op.getOperand(0).getValueType();
2400 EVT TyY = Op.getOperand(1).getValueType();
2401 SDLoc DL(Op);
2402 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
2403 SDValue Const31 = DAG.getConstant(31, DL, MVT::i32);
2404 SDValue Res;
2405
2406 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2407 // to i32.
2408 SDValue X = (TyX == MVT::f32) ?
2409 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
2410 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2411 Const1);
2412 SDValue Y = (TyY == MVT::f32) ?
2413 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
2414 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
2415 Const1);
2416
2417 if (HasExtractInsert) {
2418 // ext E, Y, 31, 1 ; extract bit31 of Y
2419 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
2420 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
2421 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
2422 } else {
2423 // sll SllX, X, 1
2424 // srl SrlX, SllX, 1
2425 // srl SrlY, Y, 31
2426 // sll SllY, SrlX, 31
2427 // or Or, SrlX, SllY
2428 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2429 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2430 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
2431 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
2432 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
2433 }
2434
2435 if (TyX == MVT::f32)
2436 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
2437
2438 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2439 Op.getOperand(0),
2440 DAG.getConstant(0, DL, MVT::i32));
2441 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
2442}
2443
2445 bool HasExtractInsert) {
2446 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
2447 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
2448 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
2449 SDLoc DL(Op);
2450 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
2451
2452 // Bitcast to integer nodes.
2453 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
2454 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
2455
2456 if (HasExtractInsert) {
2457 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
2458 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
2459 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
2460 DAG.getConstant(WidthY - 1, DL, MVT::i32), Const1);
2461
2462 if (WidthX > WidthY)
2463 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
2464 else if (WidthY > WidthX)
2465 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
2466
2467 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
2468 DAG.getConstant(WidthX - 1, DL, MVT::i32), Const1,
2469 X);
2470 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
2471 }
2472
2473 // (d)sll SllX, X, 1
2474 // (d)srl SrlX, SllX, 1
2475 // (d)srl SrlY, Y, width(Y)-1
2476 // (d)sll SllY, SrlX, width(Y)-1
2477 // or Or, SrlX, SllY
2478 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
2479 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
2480 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
2481 DAG.getConstant(WidthY - 1, DL, MVT::i32));
2482
2483 if (WidthX > WidthY)
2484 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
2485 else if (WidthY > WidthX)
2486 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
2487
2488 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
2489 DAG.getConstant(WidthX - 1, DL, MVT::i32));
2490 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
2491 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
2492}
2493
2494SDValue
2495MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
2496 if (Subtarget.isGP64bit())
2497 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
2498
2499 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
2500}
2501
2502SDValue MipsTargetLowering::lowerFABS32(SDValue Op, SelectionDAG &DAG,
2503 bool HasExtractInsert) const {
2504 SDLoc DL(Op);
2505 SDValue Res, Const1 = DAG.getConstant(1, DL, MVT::i32);
2506
2507 if (DAG.getTarget().Options.NoNaNsFPMath || Subtarget.inAbs2008Mode())
2508 return DAG.getNode(MipsISD::FAbs, DL, Op.getValueType(), Op.getOperand(0));
2509
2510 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2511 // to i32.
2512 SDValue X = (Op.getValueType() == MVT::f32)
2513 ? DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0))
2514 : DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2515 Op.getOperand(0), Const1);
2516
2517 // Clear MSB.
2518 if (HasExtractInsert)
2519 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
2520 DAG.getRegister(Mips::ZERO, MVT::i32),
2521 DAG.getConstant(31, DL, MVT::i32), Const1, X);
2522 else {
2523 // TODO: Provide DAG patterns which transform (and x, cst)
2524 // back to a (shl (srl x (clz cst)) (clz cst)) sequence.
2525 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2526 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2527 }
2528
2529 if (Op.getValueType() == MVT::f32)
2530 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
2531
2532 // FIXME: For mips32r2, the sequence of (BuildPairF64 (ins (ExtractElementF64
2533 // Op 1), $zero, 31 1) (ExtractElementF64 Op 0)) and the Op has one use, we
2534 // should be able to drop the usage of mfc1/mtc1 and rewrite the register in
2535 // place.
2536 SDValue LowX =
2537 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2538 DAG.getConstant(0, DL, MVT::i32));
2539 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
2540}
2541
2542SDValue MipsTargetLowering::lowerFABS64(SDValue Op, SelectionDAG &DAG,
2543 bool HasExtractInsert) const {
2544 SDLoc DL(Op);
2545 SDValue Res, Const1 = DAG.getConstant(1, DL, MVT::i32);
2546
2547 if (DAG.getTarget().Options.NoNaNsFPMath || Subtarget.inAbs2008Mode())
2548 return DAG.getNode(MipsISD::FAbs, DL, Op.getValueType(), Op.getOperand(0));
2549
2550 // Bitcast to integer node.
2551 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
2552
2553 // Clear MSB.
2554 if (HasExtractInsert)
2555 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
2556 DAG.getRegister(Mips::ZERO_64, MVT::i64),
2557 DAG.getConstant(63, DL, MVT::i32), Const1, X);
2558 else {
2559 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
2560 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
2561 }
2562
2563 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
2564}
2565
2566SDValue MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
2567 if ((ABI.IsN32() || ABI.IsN64()) && (Op.getValueType() == MVT::f64))
2568 return lowerFABS64(Op, DAG, Subtarget.hasExtractInsert());
2569
2570 return lowerFABS32(Op, DAG, Subtarget.hasExtractInsert());
2571}
2572
2573SDValue MipsTargetLowering::lowerFCANONICALIZE(SDValue Op,
2574 SelectionDAG &DAG) const {
2575 SDLoc DL(Op);
2576 EVT VT = Op.getValueType();
2577 SDValue Operand = Op.getOperand(0);
2578 SDNodeFlags Flags = Op->getFlags();
2579
2580 if (Flags.hasNoNaNs() || DAG.isKnownNeverNaN(Operand))
2581 return Operand;
2582
2583 SDValue Quiet = DAG.getNode(ISD::FADD, DL, VT, Operand, Operand);
2584 return DAG.getSelectCC(DL, Operand, Operand, Quiet, Operand, ISD::SETUO);
2585}
2586
2587SDValue MipsTargetLowering::
2588lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
2589 // check the depth
2590 if (Op.getConstantOperandVal(0) != 0) {
2591 DAG.getContext()->emitError(
2592 "return address can be determined only for current frame");
2593 return SDValue();
2594 }
2595
2596 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2597 MFI.setFrameAddressIsTaken(true);
2598 EVT VT = Op.getValueType();
2599 SDLoc DL(Op);
2600 SDValue FrameAddr = DAG.getCopyFromReg(
2601 DAG.getEntryNode(), DL, ABI.IsN64() ? Mips::FP_64 : Mips::FP, VT);
2602 return FrameAddr;
2603}
2604
2605SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
2606 SelectionDAG &DAG) const {
2607 // check the depth
2608 if (Op.getConstantOperandVal(0) != 0) {
2609 DAG.getContext()->emitError(
2610 "return address can be determined only for current frame");
2611 return SDValue();
2612 }
2613
2614 MachineFunction &MF = DAG.getMachineFunction();
2615 MachineFrameInfo &MFI = MF.getFrameInfo();
2616 MVT VT = Op.getSimpleValueType();
2617 unsigned RA = ABI.IsN64() ? Mips::RA_64 : Mips::RA;
2618 MFI.setReturnAddressIsTaken(true);
2619
2620 // Return RA, which contains the return address. Mark it an implicit live-in.
2622 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
2623}
2624
2625// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
2626// generated from __builtin_eh_return (offset, handler)
2627// The effect of this is to adjust the stack pointer by "offset"
2628// and then branch to "handler".
2629SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
2630 const {
2631 MachineFunction &MF = DAG.getMachineFunction();
2632 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2633
2634 MipsFI->setCallsEhReturn();
2635 SDValue Chain = Op.getOperand(0);
2636 SDValue Offset = Op.getOperand(1);
2637 SDValue Handler = Op.getOperand(2);
2638 SDLoc DL(Op);
2639 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
2640
2641 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2642 // EH_RETURN nodes, so that instructions are emitted back-to-back.
2643 unsigned OffsetReg = ABI.IsN64() ? Mips::V1_64 : Mips::V1;
2644 unsigned AddrReg = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
2645 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2646 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2647 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2648 DAG.getRegister(OffsetReg, Ty),
2649 DAG.getRegister(AddrReg, getPointerTy(MF.getDataLayout())),
2650 Chain.getValue(1));
2651}
2652
2653SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
2654 SelectionDAG &DAG) const {
2655 // FIXME: Need pseudo-fence for 'singlethread' fences
2656 // FIXME: Set SType for weaker fences where supported/appropriate.
2657 unsigned SType = 0;
2658 SDLoc DL(Op);
2659 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
2660 DAG.getConstant(SType, DL, MVT::i32));
2661}
2662
2663SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
2664 SelectionDAG &DAG) const {
2665 SDLoc DL(Op);
2666 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
2667
2668 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2669 SDValue Shamt = Op.getOperand(2);
2670 // if shamt < (VT.bits):
2671 // lo = (shl lo, shamt)
2672 // hi = (or (shl hi, shamt) (srl (srl lo, 1), (xor shamt, (VT.bits-1))))
2673 // else:
2674 // lo = 0
2675 // hi = (shl lo, shamt[4:0])
2676 SDValue Not =
2677 DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2678 DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32));
2679 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo,
2680 DAG.getConstant(1, DL, VT));
2681 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, Not);
2682 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
2683 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2684 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
2685 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2686 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
2687 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond,
2688 DAG.getConstant(0, DL, VT), ShiftLeftLo);
2689 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftLeftLo, Or);
2690
2691 SDValue Ops[2] = {Lo, Hi};
2692 return DAG.getMergeValues(Ops, DL);
2693}
2694
2695SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
2696 bool IsSRA) const {
2697 SDLoc DL(Op);
2698 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2699 SDValue Shamt = Op.getOperand(2);
2700 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
2701
2702 // if shamt < (VT.bits):
2703 // lo = (or (shl (shl hi, 1), (xor shamt, (VT.bits-1))) (srl lo, shamt))
2704 // if isSRA:
2705 // hi = (sra hi, shamt)
2706 // else:
2707 // hi = (srl hi, shamt)
2708 // else:
2709 // if isSRA:
2710 // lo = (sra hi, shamt[4:0])
2711 // hi = (sra hi, 31)
2712 // else:
2713 // lo = (srl hi, shamt[4:0])
2714 // hi = 0
2715 SDValue Not =
2716 DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2717 DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32));
2718 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, VT, Hi,
2719 DAG.getConstant(1, DL, VT));
2720 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, ShiftLeft1Hi, Not);
2721 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
2722 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2723 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL,
2724 DL, VT, Hi, Shamt);
2725 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2726 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
2727 SDValue Ext = DAG.getNode(ISD::SRA, DL, VT, Hi,
2728 DAG.getConstant(VT.getSizeInBits() - 1, DL, VT));
2729
2730 if (!(Subtarget.hasMips4() || Subtarget.hasMips32())) {
2731 SDVTList VTList = DAG.getVTList(VT, VT);
2732 return DAG.getNode(Subtarget.isGP64bit() ? MipsISD::DOUBLE_SELECT_I64
2734 DL, VTList, Cond, ShiftRightHi,
2735 IsSRA ? Ext : DAG.getConstant(0, DL, VT), Or,
2736 ShiftRightHi);
2737 }
2738
2739 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftRightHi, Or);
2740 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond,
2741 IsSRA ? Ext : DAG.getConstant(0, DL, VT), ShiftRightHi);
2742
2743 SDValue Ops[2] = {Lo, Hi};
2744 return DAG.getMergeValues(Ops, DL);
2745}
2746
2748 SDValue Chain, SDValue Src, unsigned Offset) {
2749 SDValue Ptr = LD->getBasePtr();
2750 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
2751 EVT BasePtrVT = Ptr.getValueType();
2752 SDLoc DL(LD);
2753 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2754
2755 if (Offset)
2756 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
2757 DAG.getConstant(Offset, DL, BasePtrVT));
2758
2759 SDValue Ops[] = { Chain, Ptr, Src };
2760 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
2761 LD->getMemOperand());
2762}
2763
2764// Expand an unaligned 32 or 64-bit integer load node.
2767 EVT MemVT = LD->getMemoryVT();
2768
2769 if (Subtarget.systemSupportsUnalignedAccess())
2770 return Op;
2771
2772 // Return if load is aligned or if MemVT is neither i32 nor i64.
2773 if ((LD->getAlign().value() >= (MemVT.getSizeInBits() / 8)) ||
2774 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2775 return SDValue();
2776
2777 bool IsLittle = Subtarget.isLittle();
2778 EVT VT = Op.getValueType();
2779 ISD::LoadExtType ExtType = LD->getExtensionType();
2780 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2781
2782 assert((VT == MVT::i32) || (VT == MVT::i64));
2783
2784 // Expand
2785 // (set dst, (i64 (load baseptr)))
2786 // to
2787 // (set tmp, (ldl (add baseptr, 7), undef))
2788 // (set dst, (ldr baseptr, tmp))
2789 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
2790 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
2791 IsLittle ? 7 : 0);
2792 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
2793 IsLittle ? 0 : 7);
2794 }
2795
2796 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
2797 IsLittle ? 3 : 0);
2798 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
2799 IsLittle ? 0 : 3);
2800
2801 // Expand
2802 // (set dst, (i32 (load baseptr))) or
2803 // (set dst, (i64 (sextload baseptr))) or
2804 // (set dst, (i64 (extload baseptr)))
2805 // to
2806 // (set tmp, (lwl (add baseptr, 3), undef))
2807 // (set dst, (lwr baseptr, tmp))
2808 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2809 (ExtType == ISD::EXTLOAD))
2810 return LWR;
2811
2812 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2813
2814 // Expand
2815 // (set dst, (i64 (zextload baseptr)))
2816 // to
2817 // (set tmp0, (lwl (add baseptr, 3), undef))
2818 // (set tmp1, (lwr baseptr, tmp0))
2819 // (set tmp2, (shl tmp1, 32))
2820 // (set dst, (srl tmp2, 32))
2821 SDLoc DL(LD);
2822 SDValue Const32 = DAG.getConstant(32, DL, MVT::i32);
2823 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
2824 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2825 SDValue Ops[] = { SRL, LWR.getValue(1) };
2826 return DAG.getMergeValues(Ops, DL);
2827}
2828
2830 SDValue Chain, unsigned Offset) {
2831 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2832 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
2833 SDLoc DL(SD);
2834 SDVTList VTList = DAG.getVTList(MVT::Other);
2835
2836 if (Offset)
2837 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
2838 DAG.getConstant(Offset, DL, BasePtrVT));
2839
2840 SDValue Ops[] = { Chain, Value, Ptr };
2841 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
2842 SD->getMemOperand());
2843}
2844
2845// Expand an unaligned 32 or 64-bit integer store node.
2847 bool IsLittle) {
2848 SDValue Value = SD->getValue(), Chain = SD->getChain();
2849 EVT VT = Value.getValueType();
2850
2851 // Expand
2852 // (store val, baseptr) or
2853 // (truncstore val, baseptr)
2854 // to
2855 // (swl val, (add baseptr, 3))
2856 // (swr val, baseptr)
2857 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
2858 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
2859 IsLittle ? 3 : 0);
2860 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
2861 }
2862
2863 assert(VT == MVT::i64);
2864
2865 // Expand
2866 // (store val, baseptr)
2867 // to
2868 // (sdl val, (add baseptr, 7))
2869 // (sdr val, baseptr)
2870 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2871 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
2872}
2873
2874// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2876 bool SingleFloat) {
2877 SDValue Val = SD->getValue();
2878
2879 if (Val.getOpcode() != ISD::FP_TO_SINT ||
2880 (Val.getValueSizeInBits() > 32 && SingleFloat))
2881 return SDValue();
2882
2884 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
2885 Val.getOperand(0));
2886 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
2887 SD->getPointerInfo(), SD->getAlign(),
2888 SD->getMemOperand()->getFlags());
2889}
2890
2893 EVT MemVT = SD->getMemoryVT();
2894
2895 // Lower unaligned integer stores.
2896 if (!Subtarget.systemSupportsUnalignedAccess() &&
2897 (SD->getAlign().value() < (MemVT.getSizeInBits() / 8)) &&
2898 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2899 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
2900
2901 return lowerFP_TO_SINT_STORE(SD, DAG, Subtarget.isSingleFloat());
2902}
2903
2904SDValue MipsTargetLowering::lowerEH_DWARF_CFA(SDValue Op,
2905 SelectionDAG &DAG) const {
2906
2907 // Return a fixed StackObject with offset 0 which points to the old stack
2908 // pointer.
2910 EVT ValTy = Op->getValueType(0);
2911 int FI = MFI.CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2912 return DAG.getFrameIndex(FI, ValTy);
2913}
2914
2915SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2916 SelectionDAG &DAG) const {
2917 if (Op.getValueSizeInBits() > 32 && Subtarget.isSingleFloat())
2918 return SDValue();
2919
2920 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
2921 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
2922 Op.getOperand(0));
2923 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
2924}
2925
2926SDValue MipsTargetLowering::lowerSTRICT_FP_TO_INT(SDValue Op,
2927 SelectionDAG &DAG) const {
2928 assert(Op->isStrictFPOpcode());
2929 SDValue SrcVal = Op.getOperand(1);
2930 SDLoc Loc(Op);
2931
2932 SDValue Result =
2935 Loc, Op.getValueType(), SrcVal);
2936
2937 return DAG.getMergeValues({Result, Op.getOperand(0)}, Loc);
2938}
2939
2941 static const MCPhysReg RCRegs[] = {Mips::FCR31};
2942 return RCRegs;
2943}
2944
2945//===----------------------------------------------------------------------===//
2946// Calling Convention Implementation
2947//===----------------------------------------------------------------------===//
2948
2949//===----------------------------------------------------------------------===//
2950// TODO: Implement a generic logic using tblgen that can support this.
2951// Mips O32 ABI rules:
2952// ---
2953// i32 - Passed in A0, A1, A2, A3 and stack
2954// f32 - Only passed in f32 registers if no int reg has been used yet to hold
2955// an argument. Otherwise, passed in A1, A2, A3 and stack.
2956// f64 - Only passed in two aliased f32 registers if no int reg has been used
2957// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
2958// not used, it must be shadowed. If only A3 is available, shadow it and
2959// go to stack.
2960// vXiX - Received as scalarized i32s, passed in A0 - A3 and the stack.
2961// vXf32 - Passed in either a pair of registers {A0, A1}, {A2, A3} or {A0 - A3}
2962// with the remainder spilled to the stack.
2963// vXf64 - Passed in either {A0, A1, A2, A3} or {A2, A3} and in both cases
2964// spilling the remainder to the stack.
2965//
2966// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
2967//===----------------------------------------------------------------------===//
2968
2969static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2970 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2971 Type *OrigTy, CCState &State,
2972 ArrayRef<MCPhysReg> F64Regs) {
2973 const MipsSubtarget &Subtarget = static_cast<const MipsSubtarget &>(
2974 State.getMachineFunction().getSubtarget());
2975
2976 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2977
2978 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
2979
2980 static const MCPhysReg FloatVectorIntRegs[] = { Mips::A0, Mips::A2 };
2981
2982 // Do not process byval args here.
2983 if (ArgFlags.isByVal())
2984 return true;
2985
2986 // Promote i8 and i16
2987 if (ArgFlags.isInReg() && !Subtarget.isLittle()) {
2988 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) {
2989 LocVT = MVT::i32;
2990 if (ArgFlags.isSExt())
2991 LocInfo = CCValAssign::SExtUpper;
2992 else if (ArgFlags.isZExt())
2993 LocInfo = CCValAssign::ZExtUpper;
2994 else
2995 LocInfo = CCValAssign::AExtUpper;
2996 }
2997 }
2998
2999 // Promote i8 and i16
3000 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
3001 LocVT = MVT::i32;
3002 if (ArgFlags.isSExt())
3003 LocInfo = CCValAssign::SExt;
3004 else if (ArgFlags.isZExt())
3005 LocInfo = CCValAssign::ZExt;
3006 else
3007 LocInfo = CCValAssign::AExt;
3008 }
3009
3010 unsigned Reg;
3011
3012 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
3013 // is true: function is vararg, argument is 3rd or higher, there is previous
3014 // argument which is not f32 or f64.
3015 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1 ||
3016 State.getFirstUnallocated(F32Regs) != ValNo;
3017 Align OrigAlign = ArgFlags.getNonZeroOrigAlign();
3018 bool isI64 = (ValVT == MVT::i32 && OrigAlign == Align(8));
3019 bool isVectorFloat = OrigTy->isVectorTy() && OrigTy->isFPOrFPVectorTy();
3020
3021 // The MIPS vector ABI for floats passes them in a pair of registers
3022 if (ValVT == MVT::i32 && isVectorFloat) {
3023 // This is the start of an vector that was scalarized into an unknown number
3024 // of components. It doesn't matter how many there are. Allocate one of the
3025 // notional 8 byte aligned registers which map onto the argument stack, and
3026 // shadow the register lost to alignment requirements.
3027 if (ArgFlags.isSplit()) {
3028 Reg = State.AllocateReg(FloatVectorIntRegs);
3029 if (Reg == Mips::A2)
3030 State.AllocateReg(Mips::A1);
3031 else if (Reg == 0)
3032 State.AllocateReg(Mips::A3);
3033 } else {
3034 // If we're an intermediate component of the split, we can just attempt to
3035 // allocate a register directly.
3036 Reg = State.AllocateReg(IntRegs);
3037 }
3038 } else if (ValVT == MVT::i32 ||
3039 (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
3040 Reg = State.AllocateReg(IntRegs);
3041 // If this is the first part of an i64 arg,
3042 // the allocated register must be either A0 or A2.
3043 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
3044 Reg = State.AllocateReg(IntRegs);
3045 LocVT = MVT::i32;
3046 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
3047 // Allocate int register and shadow next int register. If first
3048 // available register is Mips::A1 or Mips::A3, shadow it too.
3049 Reg = State.AllocateReg(IntRegs);
3050 if (Reg == Mips::A1 || Reg == Mips::A3)
3051 Reg = State.AllocateReg(IntRegs);
3052
3053 if (Reg) {
3054 LocVT = MVT::i32;
3055
3056 State.addLoc(
3057 CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
3058 MCRegister HiReg = State.AllocateReg(IntRegs);
3059 assert(HiReg);
3060 State.addLoc(
3061 CCValAssign::getCustomReg(ValNo, ValVT, HiReg, LocVT, LocInfo));
3062 return false;
3063 }
3064 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
3065 // we are guaranteed to find an available float register
3066 if (ValVT == MVT::f32) {
3067 Reg = State.AllocateReg(F32Regs);
3068 // Shadow int register
3069 State.AllocateReg(IntRegs);
3070 } else {
3071 Reg = State.AllocateReg(F64Regs);
3072 // Shadow int registers
3073 MCRegister Reg2 = State.AllocateReg(IntRegs);
3074 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
3075 State.AllocateReg(IntRegs);
3076 State.AllocateReg(IntRegs);
3077 }
3078 } else
3079 llvm_unreachable("Cannot handle this ValVT.");
3080
3081 if (!Reg) {
3082 unsigned Offset = State.AllocateStack(ValVT.getStoreSize(), OrigAlign);
3083 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
3084 } else
3085 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
3086
3087 return false;
3088}
3089
3090static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, MVT LocVT,
3091 CCValAssign::LocInfo LocInfo,
3092 ISD::ArgFlagsTy ArgFlags, Type *OrigTy,
3093 CCState &State) {
3094 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
3095
3096 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, OrigTy, State,
3097 F64Regs);
3098}
3099
3100static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, MVT LocVT,
3101 CCValAssign::LocInfo LocInfo,
3102 ISD::ArgFlagsTy ArgFlags, Type *OrigTy,
3103 CCState &State) {
3104 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
3105
3106 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, OrigTy, State,
3107 F64Regs);
3108}
3109
3110[[maybe_unused]] static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
3111 CCValAssign::LocInfo LocInfo,
3112 ISD::ArgFlagsTy ArgFlags, Type *OrigTy,
3113 CCState &State);
3114
3115#include "MipsGenCallingConv.inc"
3116
3118 return CC_Mips_FixedArg;
3119 }
3120
3122 return RetCC_Mips;
3123 }
3124//===----------------------------------------------------------------------===//
3125// Call Calling Convention Implementation
3126//===----------------------------------------------------------------------===//
3127
3128SDValue MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
3129 SDValue Chain, SDValue Arg,
3130 const SDLoc &DL, bool IsTailCall,
3131 SelectionDAG &DAG) const {
3132 if (!IsTailCall) {
3133 SDValue PtrOff =
3134 DAG.getNode(ISD::ADD, DL, getPointerTy(DAG.getDataLayout()), StackPtr,
3136 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo());
3137 }
3138
3140 int FI = MFI.CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
3141 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
3142 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(), MaybeAlign(),
3144}
3145
3148 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
3149 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
3150 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
3151 SDValue Chain) const {
3152 // Insert node "GP copy globalreg" before call to function.
3153 //
3154 // R_MIPS_CALL* operators (emitted when non-internal functions are called
3155 // in PIC mode) allow symbols to be resolved via lazy binding.
3156 // The lazy binding stub requires GP to point to the GOT.
3157 // Note that we don't need GP to point to the GOT for indirect calls
3158 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
3159 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
3160 // used for the function (that is, Mips linker doesn't generate lazy binding
3161 // stub for a function whose address is taken in the program).
3162 if (IsPICCall && !InternalLinkage && IsCallReloc) {
3163 unsigned GPReg = ABI.IsN64() ? Mips::GP_64 : Mips::GP;
3164 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
3165 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
3166 }
3167
3168 // Build a sequence of copy-to-reg nodes chained together with token
3169 // chain and flag operands which copy the outgoing args into registers.
3170 // The InGlue in necessary since all emitted instructions must be
3171 // stuck together.
3172 SDValue InGlue;
3173
3174 for (auto &R : RegsToPass) {
3175 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, R.first, R.second, InGlue);
3176 InGlue = Chain.getValue(1);
3177 }
3178
3179 // Add argument registers to the end of the list so that they are
3180 // known live into the call.
3181 for (auto &R : RegsToPass)
3182 Ops.push_back(CLI.DAG.getRegister(R.first, R.second.getValueType()));
3183
3184 // Add a register mask operand representing the call-preserved registers.
3185 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
3186 const uint32_t *Mask =
3187 TRI->getCallPreservedMask(CLI.DAG.getMachineFunction(), CLI.CallConv);
3188 assert(Mask && "Missing call preserved mask for calling convention");
3189 if (Subtarget.inMips16HardFloat()) {
3191 StringRef Sym = G->getGlobal()->getName();
3192 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
3193 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
3195 }
3196 }
3197 }
3198 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
3199
3200 if (InGlue.getNode())
3201 Ops.push_back(InGlue);
3202}
3203
3205 SDNode *Node) const {
3206 switch (MI.getOpcode()) {
3207 default:
3208 return;
3209 case Mips::JALR:
3210 case Mips::JALRPseudo:
3211 case Mips::JALR64:
3212 case Mips::JALR64Pseudo:
3213 case Mips::JALR16_MM:
3214 case Mips::JALRC16_MMR6:
3215 case Mips::TAILCALLREG:
3216 case Mips::TAILCALLREG64:
3217 case Mips::TAILCALLR6REG:
3218 case Mips::TAILCALL64R6REG:
3219 case Mips::TAILCALLREG_MM:
3220 case Mips::TAILCALLREG_MMR6: {
3221 if (!EmitJalrReloc ||
3222 Subtarget.inMips16Mode() ||
3224 Node->getNumOperands() < 1 ||
3225 Node->getOperand(0).getNumOperands() < 2) {
3226 return;
3227 }
3228 // We are after the callee address, set by LowerCall().
3229 // If added to MI, asm printer will emit .reloc R_MIPS_JALR for the
3230 // symbol.
3231 const SDValue TargetAddr = Node->getOperand(0).getOperand(1);
3232 StringRef Sym;
3233 if (const GlobalAddressSDNode *G =
3235 // We must not emit the R_MIPS_JALR relocation against data symbols
3236 // since this will cause run-time crashes if the linker replaces the
3237 // call instruction with a relative branch to the data symbol.
3238 if (!isa<Function>(G->getGlobal())) {
3239 LLVM_DEBUG(dbgs() << "Not adding R_MIPS_JALR against data symbol "
3240 << G->getGlobal()->getName() << "\n");
3241 return;
3242 }
3243 Sym = G->getGlobal()->getName();
3244 }
3245 else if (const ExternalSymbolSDNode *ES =
3247 Sym = ES->getSymbol();
3248 }
3249
3250 if (Sym.empty())
3251 return;
3252
3253 MachineFunction *MF = MI.getParent()->getParent();
3254 MCSymbol *S = MF->getContext().getOrCreateSymbol(Sym);
3255 LLVM_DEBUG(dbgs() << "Adding R_MIPS_JALR against " << Sym << "\n");
3257 }
3258 }
3259}
3260
3261/// LowerCall - functions arguments are copied from virtual regs to
3262/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
3263SDValue
3264MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
3265 SmallVectorImpl<SDValue> &InVals) const {
3266 SelectionDAG &DAG = CLI.DAG;
3267 SDLoc DL = CLI.DL;
3269 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
3271 SDValue Chain = CLI.Chain;
3272 SDValue Callee = CLI.Callee;
3273 bool &IsTailCall = CLI.IsTailCall;
3274 CallingConv::ID CallConv = CLI.CallConv;
3275 bool IsVarArg = CLI.IsVarArg;
3276 const CallBase *CB = CLI.CB;
3277
3279 MachineFrameInfo &MFI = MF.getFrameInfo();
3281 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
3282 bool IsPIC = isPositionIndependent();
3283
3284 // Analyze operands of the call, assigning locations to each operand.
3286 MipsCCState CCInfo(
3287 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(),
3289
3290 const ExternalSymbolSDNode *ES =
3292
3293 // There is one case where CALLSEQ_START..CALLSEQ_END can be nested, which
3294 // is during the lowering of a call with a byval argument which produces
3295 // a call to memcpy. For the O32 case, this causes the caller to allocate
3296 // stack space for the reserved argument area for the callee, then recursively
3297 // again for the memcpy call. In the NEWABI case, this doesn't occur as those
3298 // ABIs mandate that the callee allocates the reserved argument area. We do
3299 // still produce nested CALLSEQ_START..CALLSEQ_END with zero space though.
3300 //
3301 // If the callee has a byval argument and memcpy is used, we are mandated
3302 // to already have produced a reserved argument area for the callee for O32.
3303 // Therefore, the reserved argument area can be reused for both calls.
3304 //
3305 // Other cases of calling memcpy cannot have a chain with a CALLSEQ_START
3306 // present, as we have yet to hook that node onto the chain.
3307 //
3308 // Hence, the CALLSEQ_START and CALLSEQ_END nodes can be eliminated in this
3309 // case. GCC does a similar trick, in that wherever possible, it calculates
3310 // the maximum out going argument area (including the reserved area), and
3311 // preallocates the stack space on entrance to the caller.
3312 //
3313 // FIXME: We should do the same for efficiency and space.
3314
3315 // Note: The check on the calling convention below must match
3316 // MipsABIInfo::GetCalleeAllocdArgSizeInBytes().
3317 bool MemcpyInByVal = ES && StringRef(ES->getSymbol()) == "memcpy" &&
3318 CallConv != CallingConv::Fast &&
3319 Chain.getOpcode() == ISD::CALLSEQ_START;
3320
3321 // Allocate the reserved argument area. It seems strange to do this from the
3322 // caller side but removing it breaks the frame size calculation.
3323 unsigned ReservedArgArea =
3324 MemcpyInByVal ? 0 : ABI.GetCalleeAllocdArgSizeInBytes(CallConv);
3325 CCInfo.AllocateStack(ReservedArgArea, Align(1));
3326
3327 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
3328
3329 // Get a count of how many bytes are to be pushed on the stack.
3330 unsigned StackSize = CCInfo.getStackSize();
3331
3332 // Call site info for function parameters tracking and call base type info.
3334 // Set type id for call site info.
3335 if (MF.getTarget().Options.EmitCallGraphSection && CB && CB->isIndirectCall())
3336 CSInfo = MachineFunction::CallSiteInfo(*CB);
3337
3338 // Check if it's really possible to do a tail call. Restrict it to functions
3339 // that are part of this compilation unit.
3340 bool InternalLinkage = false;
3341 if (IsTailCall) {
3342 IsTailCall = isEligibleForTailCallOptimization(
3343 CCInfo, StackSize, *MF.getInfo<MipsFunctionInfo>());
3345 InternalLinkage = G->getGlobal()->hasInternalLinkage();
3346 IsTailCall &= (InternalLinkage || G->getGlobal()->hasLocalLinkage() ||
3347 G->getGlobal()->hasPrivateLinkage() ||
3348 G->getGlobal()->hasHiddenVisibility() ||
3349 G->getGlobal()->hasProtectedVisibility());
3350 }
3351 }
3352 if (!IsTailCall && CLI.CB && CLI.CB->isMustTailCall())
3353 report_fatal_error("failed to perform tail call elimination on a call "
3354 "site marked musttail");
3355
3356 if (IsTailCall)
3357 ++NumTailCalls;
3358
3359 // Chain is the output chain of the last Load/Store or CopyToReg node.
3360 // ByValChain is the output chain of the last Memcpy node created for copying
3361 // byval arguments to the stack.
3362 unsigned StackAlignment = TFL->getStackAlignment();
3363 StackSize = alignTo(StackSize, StackAlignment);
3364
3365 if (!(IsTailCall || MemcpyInByVal))
3366 Chain = DAG.getCALLSEQ_START(Chain, StackSize, 0, DL);
3367
3369 DAG.getCopyFromReg(Chain, DL, ABI.IsN64() ? Mips::SP_64 : Mips::SP,
3371 std::deque<std::pair<unsigned, SDValue>> RegsToPass;
3372 SmallVector<SDValue, 8> MemOpChains;
3373
3374 CCInfo.rewindByValRegsInfo();
3375
3376 // Walk the register/memloc assignments, inserting copies/loads.
3377 for (unsigned i = 0, e = ArgLocs.size(), OutIdx = 0; i != e; ++i, ++OutIdx) {
3378 SDValue Arg = OutVals[OutIdx];
3379 CCValAssign &VA = ArgLocs[i];
3380 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
3381 ISD::ArgFlagsTy Flags = Outs[OutIdx].Flags;
3382 bool UseUpperBits = false;
3383
3384 // ByVal Arg.
3385 if (Flags.isByVal()) {
3386 unsigned FirstByValReg, LastByValReg;
3387 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
3388 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
3389
3390 assert(Flags.getByValSize() &&
3391 "ByVal args of size 0 should have been ignored by front-end.");
3392 assert(ByValIdx < CCInfo.getInRegsParamsCount());
3393 assert(!IsTailCall &&
3394 "Do not tail-call optimize if there is a byval argument.");
3395 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
3396 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(),
3397 VA);
3398 CCInfo.nextInRegsParam();
3399 continue;
3400 }
3401
3402 // Promote the value if needed.
3403 switch (VA.getLocInfo()) {
3404 default:
3405 llvm_unreachable("Unknown loc info!");
3406 case CCValAssign::Full:
3407 if (VA.isRegLoc()) {
3408 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
3409 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
3410 (ValVT == MVT::i64 && LocVT == MVT::f64))
3411 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
3412 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
3413 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
3414 Arg, DAG.getConstant(0, DL, MVT::i32));
3415 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
3416 Arg, DAG.getConstant(1, DL, MVT::i32));
3417 if (!Subtarget.isLittle())
3418 std::swap(Lo, Hi);
3419
3420 assert(VA.needsCustom());
3421
3422 Register LocRegLo = VA.getLocReg();
3423 Register LocRegHigh = ArgLocs[++i].getLocReg();
3424 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
3425 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
3426 continue;
3427 }
3428 }
3429 break;
3430 case CCValAssign::BCvt:
3431 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
3432 break;
3434 UseUpperBits = true;
3435 [[fallthrough]];
3436 case CCValAssign::SExt:
3437 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
3438 break;
3440 UseUpperBits = true;
3441 [[fallthrough]];
3442 case CCValAssign::ZExt:
3443 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
3444 break;
3446 UseUpperBits = true;
3447 [[fallthrough]];
3448 case CCValAssign::AExt:
3449 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
3450 break;
3451 }
3452
3453 if (UseUpperBits) {
3454 unsigned ValSizeInBits = Outs[OutIdx].ArgVT.getSizeInBits();
3455 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3456 Arg = DAG.getNode(
3457 ISD::SHL, DL, VA.getLocVT(), Arg,
3458 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
3459 }
3460
3461 // Arguments that can be passed on register must be kept at
3462 // RegsToPass vector
3463 if (VA.isRegLoc()) {
3464 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3465
3466 // If the parameter is passed through reg $D, which splits into
3467 // two physical registers, avoid creating call site info.
3468 if (Mips::AFGR64RegClass.contains(VA.getLocReg()))
3469 continue;
3470
3471 // Collect CSInfo about which register passes which parameter.
3472 const TargetOptions &Options = DAG.getTarget().Options;
3473 if (Options.EmitCallSiteInfo)
3474 CSInfo.ArgRegPairs.emplace_back(VA.getLocReg(), i);
3475
3476 continue;
3477 }
3478
3479 // Register can't get to this point...
3480 assert(VA.isMemLoc());
3481
3482 // emit ISD::STORE whichs stores the
3483 // parameter value to a stack Location
3484 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
3485 Chain, Arg, DL, IsTailCall, DAG));
3486 }
3487
3488 // Transform all store nodes into one single node because all store
3489 // nodes are independent of each other.
3490 if (!MemOpChains.empty())
3491 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
3492
3493 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
3494 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
3495 // node so that legalize doesn't hack it.
3496
3497 EVT Ty = Callee.getValueType();
3498 bool GlobalOrExternal = false, IsCallReloc = false;
3499
3500 // The long-calls feature is ignored in case of PIC.
3501 // While we do not support -mshared / -mno-shared properly,
3502 // ignore long-calls in case of -mabicalls too.
3503 if (!Subtarget.isABICalls() && !IsPIC) {
3504 // If the function should be called using "long call",
3505 // get its address into a register to prevent using
3506 // of the `jal` instruction for the direct call.
3507 if (auto *N = dyn_cast<ExternalSymbolSDNode>(Callee)) {
3508 if (Subtarget.useLongCalls())
3509 Callee = Subtarget.hasSym32()
3510 ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
3511 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
3512 } else if (auto *N = dyn_cast<GlobalAddressSDNode>(Callee)) {
3513 bool UseLongCalls = Subtarget.useLongCalls();
3514 // If the function has long-call/far/near attribute
3515 // it overrides command line switch pased to the backend.
3516 if (auto *F = dyn_cast<Function>(N->getGlobal())) {
3517 if (F->hasFnAttribute("long-call"))
3518 UseLongCalls = true;
3519 else if (F->hasFnAttribute("short-call"))
3520 UseLongCalls = false;
3521 }
3522 if (UseLongCalls)
3523 Callee = Subtarget.hasSym32()
3524 ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
3525 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
3526 }
3527 }
3528
3529 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3530 if (Subtarget.isTargetCOFF() &&
3531 G->getGlobal()->hasDLLImportStorageClass()) {
3532 assert(Subtarget.isTargetWindows() &&
3533 "Windows is the only supported COFF target");
3534 auto PtrInfo = MachinePointerInfo();
3535 Callee = DAG.getLoad(Ty, DL, Chain,
3536 getDllimportSymbol(G, SDLoc(G), Ty, DAG), PtrInfo);
3537 } else if (IsPIC) {
3538 const GlobalValue *Val = G->getGlobal();
3539 InternalLinkage = Val->hasInternalLinkage();
3540
3541 if (InternalLinkage)
3542 Callee = getAddrLocal(G, DL, Ty, DAG, ABI.IsN32() || ABI.IsN64());
3543 else if (Subtarget.useXGOT()) {
3545 MipsII::MO_CALL_LO16, Chain,
3546 FuncInfo->callPtrInfo(MF, Val));
3547 IsCallReloc = true;
3548 } else {
3549 Callee = getAddrGlobal(G, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
3550 FuncInfo->callPtrInfo(MF, Val));
3551 IsCallReloc = true;
3552 }
3553 } else
3554 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL,
3555 getPointerTy(DAG.getDataLayout()), 0,
3557 GlobalOrExternal = true;
3558 }
3559 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
3560 const char *Sym = S->getSymbol();
3561
3562 if (!IsPIC) // static
3565 else if (Subtarget.useXGOT()) {
3567 MipsII::MO_CALL_LO16, Chain,
3568 FuncInfo->callPtrInfo(MF, Sym));
3569 IsCallReloc = true;
3570 } else { // PIC
3571 Callee = getAddrGlobal(S, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
3572 FuncInfo->callPtrInfo(MF, Sym));
3573 IsCallReloc = true;
3574 }
3575
3576 GlobalOrExternal = true;
3577 }
3578
3579 SmallVector<SDValue, 8> Ops(1, Chain);
3580 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
3581
3582 getOpndList(Ops, RegsToPass, IsPIC, GlobalOrExternal, InternalLinkage,
3583 IsCallReloc, CLI, Callee, Chain);
3584
3585 if (IsTailCall) {
3587 SDValue Ret = DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
3588 DAG.addCallSiteInfo(Ret.getNode(), std::move(CSInfo));
3589 return Ret;
3590 }
3591
3592 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
3593 SDValue InGlue = Chain.getValue(1);
3594
3595 DAG.addCallSiteInfo(Chain.getNode(), std::move(CSInfo));
3596
3597 // Create the CALLSEQ_END node in the case of where it is not a call to
3598 // memcpy.
3599 if (!(MemcpyInByVal)) {
3600 Chain = DAG.getCALLSEQ_END(Chain, StackSize, 0, InGlue, DL);
3601 InGlue = Chain.getValue(1);
3602 }
3603
3604 // Handle result values, copying them out of physregs into vregs that we
3605 // return.
3606 return LowerCallResult(Chain, InGlue, CallConv, IsVarArg, Ins, DL, DAG,
3607 InVals, CLI);
3608}
3609
3610/// LowerCallResult - Lower the result values of a call into the
3611/// appropriate copies out of appropriate physical registers.
3612SDValue MipsTargetLowering::LowerCallResult(
3613 SDValue Chain, SDValue InGlue, CallingConv::ID CallConv, bool IsVarArg,
3614 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
3617 // Assign locations to each value returned by this call.
3619 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
3620 *DAG.getContext());
3621
3622 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
3623
3624 // Copy all of the result registers out of their specified physreg.
3625 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3626 CCValAssign &VA = RVLocs[i];
3627 assert(VA.isRegLoc() && "Can only return in registers!");
3628
3629 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
3630 RVLocs[i].getLocVT(), InGlue);
3631 Chain = Val.getValue(1);
3632 InGlue = Val.getValue(2);
3633
3634 if (VA.isUpperBitsInLoc()) {
3635 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
3636 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3637 unsigned Shift =
3639 Val = DAG.getNode(
3640 Shift, DL, VA.getLocVT(), Val,
3641 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
3642 }
3643
3644 switch (VA.getLocInfo()) {
3645 default:
3646 llvm_unreachable("Unknown loc info!");
3647 case CCValAssign::Full:
3648 break;
3649 case CCValAssign::BCvt:
3650 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
3651 break;
3652 case CCValAssign::AExt:
3654 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3655 break;
3656 case CCValAssign::ZExt:
3658 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
3659 DAG.getValueType(VA.getValVT()));
3660 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3661 break;
3662 case CCValAssign::SExt:
3664 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
3665 DAG.getValueType(VA.getValVT()));
3666 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3667 break;
3668 }
3669
3670 InVals.push_back(Val);
3671 }
3672
3673 return Chain;
3674}
3675
3677 EVT ArgVT, const SDLoc &DL,
3678 SelectionDAG &DAG) {
3679 MVT LocVT = VA.getLocVT();
3680 EVT ValVT = VA.getValVT();
3681
3682 // Shift into the upper bits if necessary.
3683 switch (VA.getLocInfo()) {
3684 default:
3685 break;
3689 unsigned ValSizeInBits = ArgVT.getSizeInBits();
3690 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3691 unsigned Opcode =
3693 Val = DAG.getNode(
3694 Opcode, DL, VA.getLocVT(), Val,
3695 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
3696 break;
3697 }
3698 }
3699
3700 // If this is an value smaller than the argument slot size (32-bit for O32,
3701 // 64-bit for N32/N64), it has been promoted in some way to the argument slot
3702 // size. Extract the value and insert any appropriate assertions regarding
3703 // sign/zero extension.
3704 switch (VA.getLocInfo()) {
3705 default:
3706 llvm_unreachable("Unknown loc info!");
3707 case CCValAssign::Full:
3708 break;
3710 case CCValAssign::AExt:
3711 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
3712 break;
3714 case CCValAssign::SExt:
3715 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT));
3716 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
3717 break;
3719 case CCValAssign::ZExt:
3720 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT));
3721 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
3722 break;
3723 case CCValAssign::BCvt:
3724 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
3725 break;
3726 }
3727
3728 return Val;
3729}
3730
3731//===----------------------------------------------------------------------===//
3732// Formal Arguments Calling Convention Implementation
3733//===----------------------------------------------------------------------===//
3734/// LowerFormalArguments - transform physical registers into virtual registers
3735/// and generate load operations for arguments places on the stack.
3736SDValue MipsTargetLowering::LowerFormalArguments(
3737 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
3738 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
3739 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
3740 MachineFunction &MF = DAG.getMachineFunction();
3741 MachineFrameInfo &MFI = MF.getFrameInfo();
3742 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3743
3744 MipsFI->setVarArgsFrameIndex(0);
3745
3746 // Used with vargs to acumulate store chains.
3747 std::vector<SDValue> OutChains;
3748
3749 // Assign locations to all of the incoming arguments.
3751 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
3752 *DAG.getContext());
3753 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), Align(1));
3755 Function::const_arg_iterator FuncArg = Func.arg_begin();
3756
3757 if (Func.hasFnAttribute("interrupt") && !Func.arg_empty())
3759 "Functions with the interrupt attribute cannot have arguments!");
3760
3761 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg);
3762 MipsFI->setFormalArgInfo(CCInfo.getStackSize(),
3763 CCInfo.getInRegsParamsCount() > 0);
3764
3765 unsigned CurArgIdx = 0;
3766 CCInfo.rewindByValRegsInfo();
3767
3768 for (unsigned i = 0, e = ArgLocs.size(), InsIdx = 0; i != e; ++i, ++InsIdx) {
3769 CCValAssign &VA = ArgLocs[i];
3770 if (Ins[InsIdx].isOrigArg()) {
3771 std::advance(FuncArg, Ins[InsIdx].getOrigArgIndex() - CurArgIdx);
3772 CurArgIdx = Ins[InsIdx].getOrigArgIndex();
3773 }
3774 EVT ValVT = VA.getValVT();
3775 ISD::ArgFlagsTy Flags = Ins[InsIdx].Flags;
3776 bool IsRegLoc = VA.isRegLoc();
3777
3778 if (Flags.isByVal()) {
3779 assert(Ins[InsIdx].isOrigArg() && "Byval arguments cannot be implicit");
3780 unsigned FirstByValReg, LastByValReg;
3781 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
3782 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
3783
3784 assert(Flags.getByValSize() &&
3785 "ByVal args of size 0 should have been ignored by front-end.");
3786 assert(ByValIdx < CCInfo.getInRegsParamsCount());
3787 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
3788 FirstByValReg, LastByValReg, VA, CCInfo);
3789 CCInfo.nextInRegsParam();
3790 continue;
3791 }
3792
3793 // Arguments stored on registers
3794 if (IsRegLoc) {
3795 MVT RegVT = VA.getLocVT();
3796 Register ArgReg = VA.getLocReg();
3797 const TargetRegisterClass *RC = getRegClassFor(RegVT);
3798
3799 // Transform the arguments stored on
3800 // physical registers into virtual ones
3801 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
3802 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
3803
3804 ArgValue =
3805 UnpackFromArgumentSlot(ArgValue, VA, Ins[InsIdx].ArgVT, DL, DAG);
3806
3807 // Handle floating point arguments passed in integer registers and
3808 // long double arguments passed in floating point registers.
3809 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
3810 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
3811 (RegVT == MVT::f64 && ValVT == MVT::i64))
3812 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
3813 else if (ABI.IsO32() && RegVT == MVT::i32 &&
3814 ValVT == MVT::f64) {
3815 assert(VA.needsCustom() && "Expected custom argument for f64 split");
3816 CCValAssign &NextVA = ArgLocs[++i];
3817 unsigned Reg2 =
3818 addLiveIn(DAG.getMachineFunction(), NextVA.getLocReg(), RC);
3819 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
3820 if (!Subtarget.isLittle())
3821 std::swap(ArgValue, ArgValue2);
3822 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
3823 ArgValue, ArgValue2);
3824 }
3825
3826 InVals.push_back(ArgValue);
3827 } else { // VA.isRegLoc()
3828 MVT LocVT = VA.getLocVT();
3829
3830 assert(!VA.needsCustom() && "unexpected custom memory argument");
3831
3832 // Only arguments pased on the stack should make it here.
3833 assert(VA.isMemLoc());
3834
3835 // The stack pointer offset is relative to the caller stack frame.
3836 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8,
3837 VA.getLocMemOffset(), true);
3838
3839 // Create load nodes to retrieve arguments from the stack
3840 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
3841 SDValue ArgValue = DAG.getLoad(
3842 LocVT, DL, Chain, FIN,
3844 OutChains.push_back(ArgValue.getValue(1));
3845
3846 ArgValue =
3847 UnpackFromArgumentSlot(ArgValue, VA, Ins[InsIdx].ArgVT, DL, DAG);
3848
3849 InVals.push_back(ArgValue);
3850 }
3851 }
3852
3853 for (unsigned i = 0, e = ArgLocs.size(), InsIdx = 0; i != e; ++i, ++InsIdx) {
3854
3855 if (ArgLocs[i].needsCustom()) {
3856 ++i;
3857 continue;
3858 }
3859
3860 // The mips ABIs for returning structs by value requires that we copy
3861 // the sret argument into $v0 for the return. Save the argument into
3862 // a virtual register so that we can access it from the return points.
3863 if (Ins[InsIdx].Flags.isSRet()) {
3864 unsigned Reg = MipsFI->getSRetReturnReg();
3865 if (!Reg) {
3867 getRegClassFor(ABI.IsN64() ? MVT::i64 : MVT::i32));
3868 MipsFI->setSRetReturnReg(Reg);
3869 }
3870 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
3871 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
3872 break;
3873 }
3874 }
3875
3876 if (IsVarArg)
3877 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo);
3878
3879 // All stores are grouped in one node to allow the matching between
3880 // the size of Ins and InVals. This only happens when on varg functions
3881 if (!OutChains.empty()) {
3882 OutChains.push_back(Chain);
3883 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
3884 }
3885
3886 return Chain;
3887}
3888
3889//===----------------------------------------------------------------------===//
3890// Return Value Calling Convention Implementation
3891//===----------------------------------------------------------------------===//
3892
3893bool
3894MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
3895 MachineFunction &MF, bool IsVarArg,
3897 LLVMContext &Context, const Type *RetTy) const {
3899 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
3900 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3901}
3902
3903bool MipsTargetLowering::shouldSignExtendTypeInLibCall(Type *Ty,
3904 bool IsSigned) const {
3905 if ((ABI.IsN32() || ABI.IsN64()) && Ty->isIntegerTy(32))
3906 return true;
3907
3908 return IsSigned;
3909}
3910
3911SDValue
3912MipsTargetLowering::LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
3913 const SDLoc &DL,
3914 SelectionDAG &DAG) const {
3915 MachineFunction &MF = DAG.getMachineFunction();
3916 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3917
3918 MipsFI->setISR();
3919
3920 return DAG.getNode(MipsISD::ERet, DL, MVT::Other, RetOps);
3921}
3922
3923SDValue
3924MipsTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
3925 bool IsVarArg,
3927 const SmallVectorImpl<SDValue> &OutVals,
3928 const SDLoc &DL, SelectionDAG &DAG) const {
3929 // CCValAssign - represent the assignment of
3930 // the return value to a location
3932 MachineFunction &MF = DAG.getMachineFunction();
3933
3934 // CCState - Info about the registers and stack slot.
3935 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
3936
3937 // Analyze return values.
3938 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
3939
3940 SDValue Glue;
3941 SmallVector<SDValue, 4> RetOps(1, Chain);
3942
3943 // Copy the result values into the output registers.
3944 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3945 SDValue Val = OutVals[i];
3946 CCValAssign &VA = RVLocs[i];
3947 assert(VA.isRegLoc() && "Can only return in registers!");
3948 bool UseUpperBits = false;
3949
3950 switch (VA.getLocInfo()) {
3951 default:
3952 llvm_unreachable("Unknown loc info!");
3953 case CCValAssign::Full:
3954 break;
3955 case CCValAssign::BCvt:
3956 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3957 break;
3959 UseUpperBits = true;
3960 [[fallthrough]];
3961 case CCValAssign::AExt:
3962 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3963 break;
3965 UseUpperBits = true;
3966 [[fallthrough]];
3967 case CCValAssign::ZExt:
3968 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3969 break;
3971 UseUpperBits = true;
3972 [[fallthrough]];
3973 case CCValAssign::SExt:
3974 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3975 break;
3976 }
3977
3978 if (UseUpperBits) {
3979 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3980 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3981 Val = DAG.getNode(
3982 ISD::SHL, DL, VA.getLocVT(), Val,
3983 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
3984 }
3985
3986 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Glue);
3987
3988 // Guarantee that all emitted copies are stuck together with flags.
3989 Glue = Chain.getValue(1);
3990 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
3991 }
3992
3993 // The mips ABIs for returning structs by value requires that we copy
3994 // the sret argument into $v0 for the return. We saved the argument into
3995 // a virtual register in the entry block, so now we copy the value out
3996 // and into $v0.
3997 if (MF.getFunction().hasStructRetAttr()) {
3998 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3999 unsigned Reg = MipsFI->getSRetReturnReg();
4000
4001 if (!Reg)
4002 llvm_unreachable("sret virtual register not created in the entry block");
4003 SDValue Val =
4004 DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(DAG.getDataLayout()));
4005 unsigned V0 = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
4006
4007 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Glue);
4008 Glue = Chain.getValue(1);
4009 RetOps.push_back(DAG.getRegister(V0, getPointerTy(DAG.getDataLayout())));
4010 }
4011
4012 RetOps[0] = Chain; // Update chain.
4013
4014 // Add the glue if we have it.
4015 if (Glue.getNode())
4016 RetOps.push_back(Glue);
4017
4018 // ISRs must use "eret".
4019 if (DAG.getMachineFunction().getFunction().hasFnAttribute("interrupt"))
4020 return LowerInterruptReturn(RetOps, DL, DAG);
4021
4022 // Standard return on Mips is a "jr $ra"
4023 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
4024}
4025
4026//===----------------------------------------------------------------------===//
4027// Mips Inline Assembly Support
4028//===----------------------------------------------------------------------===//
4029
4030/// getConstraintType - Given a constraint letter, return the type of
4031/// constraint it is for this target.
4033MipsTargetLowering::getConstraintType(StringRef Constraint) const {
4034 // Mips specific constraints
4035 // GCC config/mips/constraints.md
4036 //
4037 // 'd' : An address register. Equivalent to r
4038 // unless generating MIPS16 code.
4039 // 'y' : Equivalent to r; retained for
4040 // backwards compatibility.
4041 // 'c' : A register suitable for use in an indirect
4042 // jump. This will always be $25 for -mabicalls.
4043 // 'l' : The lo register. 1 word storage.
4044 // 'x' : The hilo register pair. Double word storage.
4045 if (Constraint.size() == 1) {
4046 switch (Constraint[0]) {
4047 default : break;
4048 case 'd':
4049 case 'y':
4050 case 'f':
4051 case 'c':
4052 case 'l':
4053 case 'x':
4054 return C_RegisterClass;
4055 case 'R':
4056 return C_Memory;
4057 }
4058 }
4059
4060 if (Constraint == "ZC")
4061 return C_Memory;
4062
4063 return TargetLowering::getConstraintType(Constraint);
4064}
4065
4066/// Examine constraint type and operand type and determine a weight value.
4067/// This object must already have been set up with the operand type
4068/// and the current alternative constraint selected.
4070MipsTargetLowering::getSingleConstraintMatchWeight(
4071 AsmOperandInfo &info, const char *constraint) const {
4073 Value *CallOperandVal = info.CallOperandVal;
4074 // If we don't have a value, we can't do a match,
4075 // but allow it at the lowest weight.
4076 if (!CallOperandVal)
4077 return CW_Default;
4078 Type *type = CallOperandVal->getType();
4079 // Look at the constraint type.
4080 switch (*constraint) {
4081 default:
4083 break;
4084 case 'd':
4085 case 'y':
4086 if (type->isIntegerTy())
4087 weight = CW_Register;
4088 break;
4089 case 'f': // FPU or MSA register
4090 if (Subtarget.hasMSA() && type->isVectorTy() &&
4091 type->getPrimitiveSizeInBits().getFixedValue() == 128)
4092 weight = CW_Register;
4093 else if (type->isFloatTy())
4094 weight = CW_Register;
4095 break;
4096 case 'c': // $25 for indirect jumps
4097 case 'l': // lo register
4098 case 'x': // hilo register pair
4099 if (type->isIntegerTy())
4100 weight = CW_SpecificReg;
4101 break;
4102 case 'I': // signed 16 bit immediate
4103 case 'J': // integer zero
4104 case 'K': // unsigned 16 bit immediate
4105 case 'L': // signed 32 bit immediate where lower 16 bits are 0
4106 case 'N': // immediate in the range of -65535 to -1 (inclusive)
4107 case 'O': // signed 15 bit immediate (+- 16383)
4108 case 'P': // immediate in the range of 65535 to 1 (inclusive)
4109 if (isa<ConstantInt>(CallOperandVal))
4110 weight = CW_Constant;
4111 break;
4112 case 'R':
4113 weight = CW_Memory;
4114 break;
4115 }
4116 return weight;
4117}
4118
4119/// This is a helper function to parse a physical register string and split it
4120/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
4121/// that is returned indicates whether parsing was successful. The second flag
4122/// is true if the numeric part exists.
4123static std::pair<bool, bool> parsePhysicalReg(StringRef C, StringRef &Prefix,
4124 unsigned long long &Reg) {
4125 if (C.front() != '{' || C.back() != '}')
4126 return std::make_pair(false, false);
4127
4128 // Search for the first numeric character.
4129 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
4130 I = std::find_if(B, E, isdigit);
4131
4132 Prefix = StringRef(B, I - B);
4133
4134 // The second flag is set to false if no numeric characters were found.
4135 if (I == E)
4136 return std::make_pair(true, false);
4137
4138 // Parse the numeric characters.
4139 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
4140 true);
4141}
4142
4144 ISD::NodeType) const {
4145 bool Cond = !Subtarget.isABI_O32() && VT.getSizeInBits() == 32;
4146 EVT MinVT = getRegisterType(Cond ? MVT::i64 : MVT::i32);
4147 return VT.bitsLT(MinVT) ? MinVT : VT;
4148}
4149
4150std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
4151parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
4152 const TargetRegisterInfo *TRI =
4154 const TargetRegisterClass *RC;
4155 StringRef Prefix;
4156 unsigned long long Reg;
4157
4158 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
4159
4160 if (!R.first)
4161 return std::make_pair(0U, nullptr);
4162
4163 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
4164 // No numeric characters follow "hi" or "lo".
4165 if (R.second)
4166 return std::make_pair(0U, nullptr);
4167
4168 RC = TRI->getRegClass(Prefix == "hi" ?
4169 Mips::HI32RegClassID : Mips::LO32RegClassID);
4170 return std::make_pair(*(RC->begin()), RC);
4171 } else if (Prefix.starts_with("$msa")) {
4172 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
4173
4174 // No numeric characters follow the name.
4175 if (R.second)
4176 return std::make_pair(0U, nullptr);
4177
4179 .Case("$msair", Mips::MSAIR)
4180 .Case("$msacsr", Mips::MSACSR)
4181 .Case("$msaaccess", Mips::MSAAccess)
4182 .Case("$msasave", Mips::MSASave)
4183 .Case("$msamodify", Mips::MSAModify)
4184 .Case("$msarequest", Mips::MSARequest)
4185 .Case("$msamap", Mips::MSAMap)
4186 .Case("$msaunmap", Mips::MSAUnmap)
4187 .Default(0);
4188
4189 if (!Reg)
4190 return std::make_pair(0U, nullptr);
4191
4192 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
4193 return std::make_pair(Reg, RC);
4194 }
4195
4196 if (!R.second)
4197 return std::make_pair(0U, nullptr);
4198
4199 if (Prefix == "$f") { // Parse $f0-$f31.
4200 // If the targets is single float only, always select 32-bit registers,
4201 // otherwise if the size of FP registers is 64-bit or Reg is an even number,
4202 // select the 64-bit register class. Otherwise, select the 32-bit register
4203 // class.
4204 if (VT == MVT::Other) {
4205 if (Subtarget.isSingleFloat())
4206 VT = MVT::f32;
4207 else
4208 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
4209 }
4210
4211 RC = getRegClassFor(VT);
4212
4213 if (RC == &Mips::AFGR64RegClass) {
4214 assert(Reg % 2 == 0);
4215 Reg >>= 1;
4216 }
4217 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
4218 RC = TRI->getRegClass(Mips::FCCRegClassID);
4219 else if (Prefix == "$w") { // Parse $w0-$w31.
4220 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
4221 } else { // Parse $0-$31.
4222 assert(Prefix == "$");
4223 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
4224 }
4225
4226 assert(Reg < RC->getNumRegs());
4227 return std::make_pair(*(RC->begin() + Reg), RC);
4228}
4229
4230/// Given a register class constraint, like 'r', if this corresponds directly
4231/// to an LLVM register class, return a register of 0 and the register class
4232/// pointer.
4233std::pair<unsigned, const TargetRegisterClass *>
4234MipsTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
4235 StringRef Constraint,
4236 MVT VT) const {
4237 if (Constraint.size() == 1) {
4238 switch (Constraint[0]) {
4239 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
4240 case 'y': // Same as 'r'. Exists for compatibility.
4241 case 'r':
4242 if ((VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8 ||
4243 VT == MVT::i1) ||
4244 (VT == MVT::f32 && Subtarget.useSoftFloat())) {
4245 if (Subtarget.inMips16Mode())
4246 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
4247 return std::make_pair(0U, &Mips::GPR32RegClass);
4248 }
4249 if ((VT == MVT::i64 || (VT == MVT::f64 && Subtarget.useSoftFloat()) ||
4250 (VT == MVT::f64 && Subtarget.isSingleFloat())) &&
4251 !Subtarget.isGP64bit())
4252 return std::make_pair(0U, &Mips::GPR32RegClass);
4253 if ((VT == MVT::i64 || (VT == MVT::f64 && Subtarget.useSoftFloat()) ||
4254 (VT == MVT::f64 && Subtarget.isSingleFloat())) &&
4255 Subtarget.isGP64bit())
4256 return std::make_pair(0U, &Mips::GPR64RegClass);
4257 // This will generate an error message
4258 return std::make_pair(0U, nullptr);
4259 case 'f': // FPU or MSA register
4260 if (VT == MVT::v16i8)
4261 return std::make_pair(0U, &Mips::MSA128BRegClass);
4262 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
4263 return std::make_pair(0U, &Mips::MSA128HRegClass);
4264 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
4265 return std::make_pair(0U, &Mips::MSA128WRegClass);
4266 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
4267 return std::make_pair(0U, &Mips::MSA128DRegClass);
4268 else if (VT == MVT::f32)
4269 return std::make_pair(0U, &Mips::FGR32RegClass);
4270 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
4271 if (Subtarget.isFP64bit())
4272 return std::make_pair(0U, &Mips::FGR64RegClass);
4273 return std::make_pair(0U, &Mips::AFGR64RegClass);
4274 }
4275 break;
4276 case 'c': // register suitable for indirect jump
4277 if (VT == MVT::i32)
4278 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
4279 if (VT == MVT::i64)
4280 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
4281 // This will generate an error message
4282 return std::make_pair(0U, nullptr);
4283 case 'l': // use the `lo` register to store values
4284 // that are no bigger than a word
4285 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8)
4286 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
4287 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
4288 case 'x': // use the concatenated `hi` and `lo` registers
4289 // to store doubleword values
4290 // Fixme: Not triggering the use of both hi and low
4291 // This will generate an error message
4292 return std::make_pair(0U, nullptr);
4293 }
4294 }
4295
4296 if (!Constraint.empty()) {
4297 std::pair<unsigned, const TargetRegisterClass *> R;
4298 R = parseRegForInlineAsmConstraint(Constraint, VT);
4299
4300 if (R.second)
4301 return R;
4302 }
4303
4304 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
4305}
4306
4307/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4308/// vector. If it is invalid, don't add anything to Ops.
4309void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4310 StringRef Constraint,
4311 std::vector<SDValue> &Ops,
4312 SelectionDAG &DAG) const {
4313 SDLoc DL(Op);
4315
4316 // Only support length 1 constraints for now.
4317 if (Constraint.size() > 1)
4318 return;
4319
4320 char ConstraintLetter = Constraint[0];
4321 switch (ConstraintLetter) {
4322 default: break; // This will fall through to the generic implementation
4323 case 'I': // Signed 16 bit constant
4324 // If this fails, the parent routine will give an error
4325 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4326 EVT Type = Op.getValueType();
4327 int64_t Val = C->getSExtValue();
4328 if (isInt<16>(Val)) {
4330 break;
4331 }
4332 }
4333 return;
4334 case 'J': // integer zero
4335 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4336 EVT Type = Op.getValueType();
4337 int64_t Val = C->getZExtValue();
4338 if (Val == 0) {
4339 Result = DAG.getTargetConstant(0, DL, Type);
4340 break;
4341 }
4342 }
4343 return;
4344 case 'K': // unsigned 16 bit immediate
4345 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4346 EVT Type = Op.getValueType();
4347 uint64_t Val = C->getZExtValue();
4348 if (isUInt<16>(Val)) {
4349 Result = DAG.getTargetConstant(Val, DL, Type);
4350 break;
4351 }
4352 }
4353 return;
4354 case 'L': // signed 32 bit immediate where lower 16 bits are 0
4355 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4356 EVT Type = Op.getValueType();
4357 int64_t Val = C->getSExtValue();
4358 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
4360 break;
4361 }
4362 }
4363 return;
4364 case 'N': // immediate in the range of -65535 to -1 (inclusive)
4365 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4366 EVT Type = Op.getValueType();
4367 int64_t Val = C->getSExtValue();
4368 if ((Val >= -65535) && (Val <= -1)) {
4370 break;
4371 }
4372 }
4373 return;
4374 case 'O': // signed 15 bit immediate
4375 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4376 EVT Type = Op.getValueType();
4377 int64_t Val = C->getSExtValue();
4378 if ((isInt<15>(Val))) {
4380 break;
4381 }
4382 }
4383 return;
4384 case 'P': // immediate in the range of 1 to 65535 (inclusive)
4385 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4386 EVT Type = Op.getValueType();
4387 int64_t Val = C->getSExtValue();
4388 if ((Val <= 65535) && (Val >= 1)) {
4389 Result = DAG.getTargetConstant(Val, DL, Type);
4390 break;
4391 }
4392 }
4393 return;
4394 }
4395
4396 if (Result.getNode()) {
4397 Ops.push_back(Result);
4398 return;
4399 }
4400
4402}
4403
4404bool MipsTargetLowering::isLegalAddressingMode(const DataLayout &DL,
4405 const AddrMode &AM, Type *Ty,
4406 unsigned AS,
4407 Instruction *I) const {
4408 // No global is ever allowed as a base.
4409 if (AM.BaseGV)
4410 return false;
4411
4412 switch (AM.Scale) {
4413 case 0: // "r+i" or just "i", depending on HasBaseReg.
4414 break;
4415 case 1:
4416 if (!AM.HasBaseReg) // allow "r+i".
4417 break;
4418 return false; // disallow "r+r" or "r+r+i".
4419 default:
4420 return false;
4421 }
4422
4423 return true;
4424}
4425
4426bool
4427MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4428 // The Mips target isn't yet aware of offsets.
4429 return false;
4430}
4431
4432EVT MipsTargetLowering::getOptimalMemOpType(
4433 LLVMContext &Context, const MemOp &Op,
4434 const AttributeList &FuncAttributes) const {
4435 if (Subtarget.hasMips64())
4436 return MVT::i64;
4437
4438 return MVT::i32;
4439}
4440
4441bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT,
4442 bool ForCodeSize) const {
4443 if (VT != MVT::f32 && VT != MVT::f64)
4444 return false;
4445 if (Imm.isNegZero())
4446 return false;
4447 return Imm.isZero();
4448}
4449
4450bool MipsTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
4451 return isInt<16>(Imm);
4452}
4453
4454bool MipsTargetLowering::isLegalAddImmediate(int64_t Imm) const {
4455 return isInt<16>(Imm);
4456}
4457
4459 if (!isPositionIndependent())
4461 if (ABI.IsN64())
4464}
4465
4466SDValue MipsTargetLowering::getPICJumpTableRelocBase(SDValue Table,
4467 SelectionDAG &DAG) const {
4468 if (!isPositionIndependent())
4469 return Table;
4471}
4472
4474 return Subtarget.useSoftFloat();
4475}
4476
4477void MipsTargetLowering::copyByValRegs(
4478 SDValue Chain, const SDLoc &DL, std::vector<SDValue> &OutChains,
4479 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
4480 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
4481 unsigned FirstReg, unsigned LastReg, const CCValAssign &VA,
4482 MipsCCState &State) const {
4483 MachineFunction &MF = DAG.getMachineFunction();
4484 MachineFrameInfo &MFI = MF.getFrameInfo();
4485 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
4486 unsigned NumRegs = LastReg - FirstReg;
4487 unsigned RegAreaSize = NumRegs * GPRSizeInBytes;
4488 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
4489 int FrameObjOffset;
4490 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs();
4491
4492 if (RegAreaSize)
4493 FrameObjOffset =
4494 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
4495 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes);
4496 else
4497 FrameObjOffset = VA.getLocMemOffset();
4498
4499 // Create frame object.
4500 EVT PtrTy = getPointerTy(DAG.getDataLayout());
4501 // Make the fixed object stored to mutable so that the load instructions
4502 // referencing it have their memory dependencies added.
4503 // Set the frame object as isAliased which clears the underlying objects
4504 // vector in ScheduleDAGInstrs::buildSchedGraph() resulting in addition of all
4505 // stores as dependencies for loads referencing this fixed object.
4506 int FI = MFI.CreateFixedObject(FrameObjSize, FrameObjOffset, false, true);
4507 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4508 InVals.push_back(FIN);
4509
4510 if (!NumRegs)
4511 return;
4512
4513 // Copy arg registers.
4514 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
4515 const TargetRegisterClass *RC = getRegClassFor(RegTy);
4516
4517 for (unsigned I = 0; I < NumRegs; ++I) {
4518 unsigned ArgReg = ByValArgRegs[FirstReg + I];
4519 unsigned VReg = addLiveIn(MF, ArgReg, RC);
4520 unsigned Offset = I * GPRSizeInBytes;
4521 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
4522 DAG.getConstant(Offset, DL, PtrTy));
4523 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
4524 StorePtr, MachinePointerInfo(FuncArg, Offset));
4525 OutChains.push_back(Store);
4526 }
4527}
4528
4529// Copy byVal arg to registers and stack.
4530void MipsTargetLowering::passByValArg(
4531 SDValue Chain, const SDLoc &DL,
4532 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
4533 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
4534 MachineFrameInfo &MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg,
4535 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle,
4536 const CCValAssign &VA) const {
4537 unsigned ByValSizeInBytes = Flags.getByValSize();
4538 unsigned OffsetInBytes = 0; // From beginning of struct
4539 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
4540 Align Alignment =
4541 std::min(Flags.getNonZeroByValAlign(), Align(RegSizeInBytes));
4542 EVT PtrTy = getPointerTy(DAG.getDataLayout()),
4543 RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
4544 unsigned NumRegs = LastReg - FirstReg;
4545
4546 if (NumRegs) {
4547 ArrayRef<MCPhysReg> ArgRegs = ABI.GetByValArgRegs();
4548 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes);
4549 unsigned I = 0;
4550
4551 // Copy words to registers.
4552 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) {
4553 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
4554 DAG.getConstant(OffsetInBytes, DL, PtrTy));
4555 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
4556 MachinePointerInfo(), Alignment);
4557 MemOpChains.push_back(LoadVal.getValue(1));
4558 unsigned ArgReg = ArgRegs[FirstReg + I];
4559 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
4560 }
4561
4562 // Return if the struct has been fully copied.
4563 if (ByValSizeInBytes == OffsetInBytes)
4564 return;
4565
4566 // Copy the remainder of the byval argument with sub-word loads and shifts.
4567 if (LeftoverBytes) {
4568 SDValue Val;
4569
4570 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
4571 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
4572 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
4573
4574 if (RemainingSizeInBytes < LoadSizeInBytes)
4575 continue;
4576
4577 // Load subword.
4578 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
4579 DAG.getConstant(OffsetInBytes, DL,
4580 PtrTy));
4581 SDValue LoadVal = DAG.getExtLoad(
4582 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
4583 MVT::getIntegerVT(LoadSizeInBytes * 8), Alignment);
4584 MemOpChains.push_back(LoadVal.getValue(1));
4585
4586 // Shift the loaded value.
4587 unsigned Shamt;
4588
4589 if (isLittle)
4590 Shamt = TotalBytesLoaded * 8;
4591 else
4592 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
4593
4594 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
4595 DAG.getConstant(Shamt, DL, MVT::i32));
4596
4597 if (Val.getNode())
4598 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
4599 else
4600 Val = Shift;
4601
4602 OffsetInBytes += LoadSizeInBytes;
4603 TotalBytesLoaded += LoadSizeInBytes;
4604 Alignment = std::min(Alignment, Align(LoadSizeInBytes));
4605 }
4606
4607 unsigned ArgReg = ArgRegs[FirstReg + I];
4608 RegsToPass.push_back(std::make_pair(ArgReg, Val));
4609 return;
4610 }
4611 }
4612
4613 // Copy remainder of byval arg to it with memcpy.
4614 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
4615 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
4616 DAG.getConstant(OffsetInBytes, DL, PtrTy));
4617 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
4619 Chain = DAG.getMemcpy(
4620 Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, DL, PtrTy),
4621 Align(Alignment), /*isVolatile=*/false, /*AlwaysInline=*/false,
4622 /*CI=*/nullptr, std::nullopt, MachinePointerInfo(), MachinePointerInfo());
4623 MemOpChains.push_back(Chain);
4624}
4625
4626void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
4627 SDValue Chain, const SDLoc &DL,
4628 SelectionDAG &DAG,
4629 CCState &State) const {
4630 ArrayRef<MCPhysReg> ArgRegs = ABI.getVarArgRegs(Subtarget.isGP64bit());
4631 unsigned Idx = State.getFirstUnallocated(ArgRegs);
4632 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
4633 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
4634 const TargetRegisterClass *RC = getRegClassFor(RegTy);
4635 MachineFunction &MF = DAG.getMachineFunction();
4636 MachineFrameInfo &MFI = MF.getFrameInfo();
4637 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
4638
4639 // Offset of the first variable argument from stack pointer.
4640 int VaArgOffset;
4641
4642 if (ArgRegs.size() == Idx)
4643 VaArgOffset = alignTo(State.getStackSize(), RegSizeInBytes);
4644 else {
4645 VaArgOffset =
4646 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
4647 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
4648 }
4649
4650 // Record the frame index of the first variable argument
4651 // which is a value necessary to VASTART.
4652 int FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
4653 MipsFI->setVarArgsFrameIndex(FI);
4654
4655 // Copy the integer registers that have not been used for argument passing
4656 // to the argument register save area. For O32, the save area is allocated
4657 // in the caller's stack frame, while for N32/64, it is allocated in the
4658 // callee's stack frame.
4659 for (unsigned I = Idx; I < ArgRegs.size();
4660 ++I, VaArgOffset += RegSizeInBytes) {
4661 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
4662 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
4663 FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
4664 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
4665 SDValue Store =
4666 DAG.getStore(Chain, DL, ArgValue, PtrOff, MachinePointerInfo());
4667 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
4668 (Value *)nullptr);
4669 OutChains.push_back(Store);
4670 }
4671}
4672
4674 Align Alignment) const {
4675 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
4676
4677 assert(Size && "Byval argument's size shouldn't be 0.");
4678
4679 Alignment = std::min(Alignment, TFL->getStackAlign());
4680
4681 unsigned FirstReg = 0;
4682 unsigned NumRegs = 0;
4683
4684 if (State->getCallingConv() != CallingConv::Fast) {
4685 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
4686 ArrayRef<MCPhysReg> IntArgRegs = ABI.GetByValArgRegs();
4687 // FIXME: The O32 case actually describes no shadow registers.
4688 const MCPhysReg *ShadowRegs =
4689 ABI.IsO32() ? IntArgRegs.data() : Mips64DPRegs;
4690
4691 // We used to check the size as well but we can't do that anymore since
4692 // CCState::HandleByVal() rounds up the size after calling this function.
4693 assert(
4694 Alignment >= Align(RegSizeInBytes) &&
4695 "Byval argument's alignment should be a multiple of RegSizeInBytes.");
4696
4697 FirstReg = State->getFirstUnallocated(IntArgRegs);
4698
4699 // If Alignment > RegSizeInBytes, the first arg register must be even.
4700 // FIXME: This condition happens to do the right thing but it's not the
4701 // right way to test it. We want to check that the stack frame offset
4702 // of the register is aligned.
4703 if ((Alignment > RegSizeInBytes) && (FirstReg % 2)) {
4704 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]);
4705 ++FirstReg;
4706 }
4707
4708 // Mark the registers allocated.
4709 Size = alignTo(Size, RegSizeInBytes);
4710 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size());
4711 Size -= RegSizeInBytes, ++I, ++NumRegs)
4712 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]);
4713 }
4714
4715 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs);
4716}
4717
4718MachineBasicBlock *MipsTargetLowering::emitPseudoSELECT(MachineInstr &MI,
4720 bool isFPCmp,
4721 unsigned Opc) const {
4723 "Subtarget already supports SELECT nodes with the use of"
4724 "conditional-move instructions.");
4725
4726 const TargetInstrInfo *TII =
4728 DebugLoc DL = MI.getDebugLoc();
4729
4730 // To "insert" a SELECT instruction, we actually have to insert the
4731 // diamond control-flow pattern. The incoming instruction knows the
4732 // destination vreg to set, the condition code register to branch on, the
4733 // true/false values to select between, and a branch opcode to use.
4734 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4736
4737 // thisMBB:
4738 // ...
4739 // TrueVal = ...
4740 // setcc r1, r2, r3
4741 // bNE r1, r0, copy1MBB
4742 // fallthrough --> copy0MBB
4743 MachineBasicBlock *thisMBB = BB;
4744 MachineFunction *F = BB->getParent();
4745 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4746 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
4747 F->insert(It, copy0MBB);
4748 F->insert(It, sinkMBB);
4749
4750 // Transfer the remainder of BB and its successor edges to sinkMBB.
4751 sinkMBB->splice(sinkMBB->begin(), BB,
4752 std::next(MachineBasicBlock::iterator(MI)), BB->end());
4754
4755 // Next, add the true and fallthrough blocks as its successors.
4756 BB->addSuccessor(copy0MBB);
4757 BB->addSuccessor(sinkMBB);
4758
4759 if (isFPCmp) {
4760 // bc1[tf] cc, sinkMBB
4761 BuildMI(BB, DL, TII->get(Opc))
4762 .addReg(MI.getOperand(1).getReg())
4763 .addMBB(sinkMBB);
4764 } else {
4765 // bne rs, $0, sinkMBB
4766 BuildMI(BB, DL, TII->get(Opc))
4767 .addReg(MI.getOperand(1).getReg())
4768 .addReg(Mips::ZERO)
4769 .addMBB(sinkMBB);
4770 }
4771
4772 // copy0MBB:
4773 // %FalseValue = ...
4774 // # fallthrough to sinkMBB
4775 BB = copy0MBB;
4776
4777 // Update machine-CFG edges
4778 BB->addSuccessor(sinkMBB);
4779
4780 // sinkMBB:
4781 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
4782 // ...
4783 BB = sinkMBB;
4784
4785 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
4786 .addReg(MI.getOperand(2).getReg())
4787 .addMBB(thisMBB)
4788 .addReg(MI.getOperand(3).getReg())
4789 .addMBB(copy0MBB);
4790
4791 MI.eraseFromParent(); // The pseudo instruction is gone now.
4792
4793 return BB;
4794}
4795
4797MipsTargetLowering::emitPseudoD_SELECT(MachineInstr &MI,
4798 MachineBasicBlock *BB) const {
4799 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
4800 "Subtarget already supports SELECT nodes with the use of"
4801 "conditional-move instructions.");
4802
4803 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
4804 DebugLoc DL = MI.getDebugLoc();
4805
4806 // D_SELECT substitutes two SELECT nodes that goes one after another and
4807 // have the same condition operand. On machines which don't have
4808 // conditional-move instruction, it reduces unnecessary branch instructions
4809 // which are result of using two diamond patterns that are result of two
4810 // SELECT pseudo instructions.
4811 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4813
4814 // thisMBB:
4815 // ...
4816 // TrueVal = ...
4817 // setcc r1, r2, r3
4818 // bNE r1, r0, copy1MBB
4819 // fallthrough --> copy0MBB
4820 MachineBasicBlock *thisMBB = BB;
4821 MachineFunction *F = BB->getParent();
4822 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4823 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
4824 F->insert(It, copy0MBB);
4825 F->insert(It, sinkMBB);
4826
4827 // Transfer the remainder of BB and its successor edges to sinkMBB.
4828 sinkMBB->splice(sinkMBB->begin(), BB,
4829 std::next(MachineBasicBlock::iterator(MI)), BB->end());
4831
4832 // Next, add the true and fallthrough blocks as its successors.
4833 BB->addSuccessor(copy0MBB);
4834 BB->addSuccessor(sinkMBB);
4835
4836 // bne rs, $0, sinkMBB
4837 BuildMI(BB, DL, TII->get(Mips::BNE))
4838 .addReg(MI.getOperand(2).getReg())
4839 .addReg(Mips::ZERO)
4840 .addMBB(sinkMBB);
4841
4842 // copy0MBB:
4843 // %FalseValue = ...
4844 // # fallthrough to sinkMBB
4845 BB = copy0MBB;
4846
4847 // Update machine-CFG edges
4848 BB->addSuccessor(sinkMBB);
4849
4850 // sinkMBB:
4851 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
4852 // ...
4853 BB = sinkMBB;
4854
4855 // Use two PHI nodes to select two reults
4856 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
4857 .addReg(MI.getOperand(3).getReg())
4858 .addMBB(thisMBB)
4859 .addReg(MI.getOperand(5).getReg())
4860 .addMBB(copy0MBB);
4861 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(1).getReg())
4862 .addReg(MI.getOperand(4).getReg())
4863 .addMBB(thisMBB)
4864 .addReg(MI.getOperand(6).getReg())
4865 .addMBB(copy0MBB);
4866
4867 MI.eraseFromParent(); // The pseudo instruction is gone now.
4868
4869 return BB;
4870}
4871
4872// Copies the function MipsAsmParser::matchCPURegisterName.
4873int MipsTargetLowering::getCPURegisterIndex(StringRef Name) const {
4874 int CC;
4875
4876 CC = StringSwitch<unsigned>(Name)
4877 .Case("zero", 0)
4878 .Case("at", 1)
4879 .Case("AT", 1)
4880 .Case("a0", 4)
4881 .Case("a1", 5)
4882 .Case("a2", 6)
4883 .Case("a3", 7)
4884 .Case("v0", 2)
4885 .Case("v1", 3)
4886 .Case("s0", 16)
4887 .Case("s1", 17)
4888 .Case("s2", 18)
4889 .Case("s3", 19)
4890 .Case("s4", 20)
4891 .Case("s5", 21)
4892 .Case("s6", 22)
4893 .Case("s7", 23)
4894 .Case("k0", 26)
4895 .Case("k1", 27)
4896 .Case("gp", 28)
4897 .Case("sp", 29)
4898 .Case("fp", 30)
4899 .Case("s8", 30)
4900 .Case("ra", 31)
4901 .Case("t0", 8)
4902 .Case("t1", 9)
4903 .Case("t2", 10)
4904 .Case("t3", 11)
4905 .Case("t4", 12)
4906 .Case("t5", 13)
4907 .Case("t6", 14)
4908 .Case("t7", 15)
4909 .Case("t8", 24)
4910 .Case("t9", 25)
4911 .Default(-1);
4912
4913 if (!(ABI.IsN32() || ABI.IsN64()))
4914 return CC;
4915
4916 // Although SGI documentation just cuts out t0-t3 for n32/n64,
4917 // GNU pushes the values of t0-t3 to override the o32/o64 values for t4-t7
4918 // We are supporting both cases, so for t0-t3 we'll just push them to t4-t7.
4919 if (8 <= CC && CC <= 11)
4920 CC += 4;
4921
4922 if (CC == -1)
4923 CC = StringSwitch<unsigned>(Name)
4924 .Case("a4", 8)
4925 .Case("a5", 9)
4926 .Case("a6", 10)
4927 .Case("a7", 11)
4928 .Case("kt0", 26)
4929 .Case("kt1", 27)
4930 .Default(-1);
4931
4932 return CC;
4933}
4934
4935// FIXME? Maybe this could be a TableGen attribute on some registers and
4936// this table could be generated automatically from RegInfo.
4939 const MachineFunction &MF) const {
4940 // 1. Delete symbol '$'.
4941 std::string newRegName = RegName;
4942 if (StringRef(RegName).starts_with("$"))
4943 newRegName = StringRef(RegName).substr(1);
4944
4945 // 2. Get register index value.
4946 std::smatch matchResult;
4947 int regIdx;
4948 static const std::regex matchStr("^[0-9]*$");
4949 if (std::regex_match(newRegName, matchResult, matchStr))
4950 regIdx = std::stoi(newRegName);
4951 else {
4952 newRegName = StringRef(newRegName).lower();
4953 regIdx = getCPURegisterIndex(StringRef(newRegName));
4954 }
4955
4956 // 3. Get register.
4957 if (regIdx >= 0 && regIdx < 32) {
4959 const MCRegisterClass &RC = Subtarget.isGP64bit()
4960 ? MRI->getRegClass(Mips::GPR64RegClassID)
4961 : MRI->getRegClass(Mips::GPR32RegClassID);
4962 return RC.getRegister(regIdx);
4963 }
4964
4966 Twine("Invalid register name \"" + StringRef(RegName) + "\"."));
4967}
4968
4969MachineBasicBlock *MipsTargetLowering::emitLDR_W(MachineInstr &MI,
4970 MachineBasicBlock *BB) const {
4971 MachineFunction *MF = BB->getParent();
4974 const bool IsLittle = Subtarget.isLittle();
4975 DebugLoc DL = MI.getDebugLoc();
4976
4977 Register Dest = MI.getOperand(0).getReg();
4978 Register Address = MI.getOperand(1).getReg();
4979 unsigned Imm = MI.getOperand(2).getImm();
4980
4982
4984 // Mips release 6 can load from adress that is not naturally-aligned.
4985 Register Temp = MRI.createVirtualRegister(&Mips::GPR32RegClass);
4986 BuildMI(*BB, I, DL, TII->get(Mips::LW))
4987 .addDef(Temp)
4988 .addUse(Address)
4989 .addImm(Imm);
4990 BuildMI(*BB, I, DL, TII->get(Mips::FILL_W)).addDef(Dest).addUse(Temp);
4991 } else {
4992 // Mips release 5 needs to use instructions that can load from an unaligned
4993 // memory address.
4994 Register LoadHalf = MRI.createVirtualRegister(&Mips::GPR32RegClass);
4995 Register LoadFull = MRI.createVirtualRegister(&Mips::GPR32RegClass);
4996 Register Undef = MRI.createVirtualRegister(&Mips::GPR32RegClass);
4997 BuildMI(*BB, I, DL, TII->get(Mips::IMPLICIT_DEF)).addDef(Undef);
4998 BuildMI(*BB, I, DL, TII->get(Mips::LWR))
4999 .addDef(LoadHalf)
5000 .addUse(Address)
5001 .addImm(Imm + (IsLittle ? 0 : 3))
5002 .addUse(Undef);
5003 BuildMI(*BB, I, DL, TII->get(Mips::LWL))
5004 .addDef(LoadFull)
5005 .addUse(Address)
5006 .addImm(Imm + (IsLittle ? 3 : 0))
5007 .addUse(LoadHalf);
5008 BuildMI(*BB, I, DL, TII->get(Mips::FILL_W)).addDef(Dest).addUse(LoadFull);
5009 }
5010
5011 MI.eraseFromParent();
5012 return BB;
5013}
5014
5015MachineBasicBlock *MipsTargetLowering::emitLDR_D(MachineInstr &MI,
5016 MachineBasicBlock *BB) const {
5017 MachineFunction *MF = BB->getParent();
5018 MachineRegisterInfo &MRI = MF->getRegInfo();
5019 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
5020 const bool IsLittle = Subtarget.isLittle();
5021 DebugLoc DL = MI.getDebugLoc();
5022
5023 Register Dest = MI.getOperand(0).getReg();
5024 Register Address = MI.getOperand(1).getReg();
5025 unsigned Imm = MI.getOperand(2).getImm();
5026
5028
5029 if (Subtarget.hasMips32r6() || Subtarget.hasMips64r6()) {
5030 // Mips release 6 can load from adress that is not naturally-aligned.
5031 if (Subtarget.isGP64bit()) {
5032 Register Temp = MRI.createVirtualRegister(&Mips::GPR64RegClass);
5033 BuildMI(*BB, I, DL, TII->get(Mips::LD))
5034 .addDef(Temp)
5035 .addUse(Address)
5036 .addImm(Imm);
5037 BuildMI(*BB, I, DL, TII->get(Mips::FILL_D)).addDef(Dest).addUse(Temp);
5038 } else {
5039 Register Wtemp = MRI.createVirtualRegister(&Mips::MSA128WRegClass);
5040 Register Lo = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5041 Register Hi = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5042 BuildMI(*BB, I, DL, TII->get(Mips::LW))
5043 .addDef(Lo)
5044 .addUse(Address)
5045 .addImm(Imm + (IsLittle ? 0 : 4));
5046 BuildMI(*BB, I, DL, TII->get(Mips::LW))
5047 .addDef(Hi)
5048 .addUse(Address)
5049 .addImm(Imm + (IsLittle ? 4 : 0));
5050 BuildMI(*BB, I, DL, TII->get(Mips::FILL_W)).addDef(Wtemp).addUse(Lo);
5051 BuildMI(*BB, I, DL, TII->get(Mips::INSERT_W), Dest)
5052 .addUse(Wtemp)
5053 .addUse(Hi)
5054 .addImm(1);
5055 }
5056 } else {
5057 // Mips release 5 needs to use instructions that can load from an unaligned
5058 // memory address.
5059 Register LoHalf = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5060 Register LoFull = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5061 Register LoUndef = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5062 Register HiHalf = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5063 Register HiFull = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5064 Register HiUndef = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5065 Register Wtemp = MRI.createVirtualRegister(&Mips::MSA128WRegClass);
5066 BuildMI(*BB, I, DL, TII->get(Mips::IMPLICIT_DEF)).addDef(LoUndef);
5067 BuildMI(*BB, I, DL, TII->get(Mips::LWR))
5068 .addDef(LoHalf)
5069 .addUse(Address)
5070 .addImm(Imm + (IsLittle ? 0 : 7))
5071 .addUse(LoUndef);
5072 BuildMI(*BB, I, DL, TII->get(Mips::LWL))
5073 .addDef(LoFull)
5074 .addUse(Address)
5075 .addImm(Imm + (IsLittle ? 3 : 4))
5076 .addUse(LoHalf);
5077 BuildMI(*BB, I, DL, TII->get(Mips::IMPLICIT_DEF)).addDef(HiUndef);
5078 BuildMI(*BB, I, DL, TII->get(Mips::LWR))
5079 .addDef(HiHalf)
5080 .addUse(Address)
5081 .addImm(Imm + (IsLittle ? 4 : 3))
5082 .addUse(HiUndef);
5083 BuildMI(*BB, I, DL, TII->get(Mips::LWL))
5084 .addDef(HiFull)
5085 .addUse(Address)
5086 .addImm(Imm + (IsLittle ? 7 : 0))
5087 .addUse(HiHalf);
5088 BuildMI(*BB, I, DL, TII->get(Mips::FILL_W)).addDef(Wtemp).addUse(LoFull);
5089 BuildMI(*BB, I, DL, TII->get(Mips::INSERT_W), Dest)
5090 .addUse(Wtemp)
5091 .addUse(HiFull)
5092 .addImm(1);
5093 }
5094
5095 MI.eraseFromParent();
5096 return BB;
5097}
5098
5099MachineBasicBlock *MipsTargetLowering::emitSTR_W(MachineInstr &MI,
5100 MachineBasicBlock *BB) const {
5101 MachineFunction *MF = BB->getParent();
5102 MachineRegisterInfo &MRI = MF->getRegInfo();
5103 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
5104 const bool IsLittle = Subtarget.isLittle();
5105 DebugLoc DL = MI.getDebugLoc();
5106
5107 Register StoreVal = MI.getOperand(0).getReg();
5108 Register Address = MI.getOperand(1).getReg();
5109 unsigned Imm = MI.getOperand(2).getImm();
5110
5112
5113 if (Subtarget.hasMips32r6() || Subtarget.hasMips64r6()) {
5114 // Mips release 6 can store to adress that is not naturally-aligned.
5115 Register BitcastW = MRI.createVirtualRegister(&Mips::MSA128WRegClass);
5116 Register Tmp = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5117 BuildMI(*BB, I, DL, TII->get(Mips::COPY)).addDef(BitcastW).addUse(StoreVal);
5118 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W))
5119 .addDef(Tmp)
5120 .addUse(BitcastW)
5121 .addImm(0);
5122 BuildMI(*BB, I, DL, TII->get(Mips::SW))
5123 .addUse(Tmp)
5124 .addUse(Address)
5125 .addImm(Imm);
5126 } else {
5127 // Mips release 5 needs to use instructions that can store to an unaligned
5128 // memory address.
5129 Register Tmp = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5130 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W))
5131 .addDef(Tmp)
5132 .addUse(StoreVal)
5133 .addImm(0);
5134 BuildMI(*BB, I, DL, TII->get(Mips::SWR))
5135 .addUse(Tmp)
5136 .addUse(Address)
5137 .addImm(Imm + (IsLittle ? 0 : 3));
5138 BuildMI(*BB, I, DL, TII->get(Mips::SWL))
5139 .addUse(Tmp)
5140 .addUse(Address)
5141 .addImm(Imm + (IsLittle ? 3 : 0));
5142 }
5143
5144 MI.eraseFromParent();
5145
5146 return BB;
5147}
5148
5149MachineBasicBlock *MipsTargetLowering::emitSTR_D(MachineInstr &MI,
5150 MachineBasicBlock *BB) const {
5151 MachineFunction *MF = BB->getParent();
5152 MachineRegisterInfo &MRI = MF->getRegInfo();
5153 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
5154 const bool IsLittle = Subtarget.isLittle();
5155 DebugLoc DL = MI.getDebugLoc();
5156
5157 Register StoreVal = MI.getOperand(0).getReg();
5158 Register Address = MI.getOperand(1).getReg();
5159 unsigned Imm = MI.getOperand(2).getImm();
5160
5162
5163 if (Subtarget.hasMips32r6() || Subtarget.hasMips64r6()) {
5164 // Mips release 6 can store to adress that is not naturally-aligned.
5165 if (Subtarget.isGP64bit()) {
5166 Register BitcastD = MRI.createVirtualRegister(&Mips::MSA128DRegClass);
5167 Register Lo = MRI.createVirtualRegister(&Mips::GPR64RegClass);
5168 BuildMI(*BB, I, DL, TII->get(Mips::COPY))
5169 .addDef(BitcastD)
5170 .addUse(StoreVal);
5171 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_D))
5172 .addDef(Lo)
5173 .addUse(BitcastD)
5174 .addImm(0);
5175 BuildMI(*BB, I, DL, TII->get(Mips::SD))
5176 .addUse(Lo)
5177 .addUse(Address)
5178 .addImm(Imm);
5179 } else {
5180 Register BitcastW = MRI.createVirtualRegister(&Mips::MSA128WRegClass);
5181 Register Lo = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5182 Register Hi = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5183 BuildMI(*BB, I, DL, TII->get(Mips::COPY))
5184 .addDef(BitcastW)
5185 .addUse(StoreVal);
5186 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W))
5187 .addDef(Lo)
5188 .addUse(BitcastW)
5189 .addImm(0);
5190 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W))
5191 .addDef(Hi)
5192 .addUse(BitcastW)
5193 .addImm(1);
5194 BuildMI(*BB, I, DL, TII->get(Mips::SW))
5195 .addUse(Lo)
5196 .addUse(Address)
5197 .addImm(Imm + (IsLittle ? 0 : 4));
5198 BuildMI(*BB, I, DL, TII->get(Mips::SW))
5199 .addUse(Hi)
5200 .addUse(Address)
5201 .addImm(Imm + (IsLittle ? 4 : 0));
5202 }
5203 } else {
5204 // Mips release 5 needs to use instructions that can store to an unaligned
5205 // memory address.
5206 Register Bitcast = MRI.createVirtualRegister(&Mips::MSA128WRegClass);
5207 Register Lo = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5208 Register Hi = MRI.createVirtualRegister(&Mips::GPR32RegClass);
5209 BuildMI(*BB, I, DL, TII->get(Mips::COPY)).addDef(Bitcast).addUse(StoreVal);
5210 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W))
5211 .addDef(Lo)
5212 .addUse(Bitcast)
5213 .addImm(0);
5214 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W))
5215 .addDef(Hi)
5216 .addUse(Bitcast)
5217 .addImm(1);
5218 BuildMI(*BB, I, DL, TII->get(Mips::SWR))
5219 .addUse(Lo)
5220 .addUse(Address)
5221 .addImm(Imm + (IsLittle ? 0 : 3));
5222 BuildMI(*BB, I, DL, TII->get(Mips::SWL))
5223 .addUse(Lo)
5224 .addUse(Address)
5225 .addImm(Imm + (IsLittle ? 3 : 0));
5226 BuildMI(*BB, I, DL, TII->get(Mips::SWR))
5227 .addUse(Hi)
5228 .addUse(Address)
5229 .addImm(Imm + (IsLittle ? 4 : 7));
5230 BuildMI(*BB, I, DL, TII->get(Mips::SWL))
5231 .addUse(Hi)
5232 .addUse(Address)
5233 .addImm(Imm + (IsLittle ? 7 : 4));
5234 }
5235
5236 MI.eraseFromParent();
5237 return BB;
5238}
unsigned const MachineRegisterInfo * MRI
static SDValue performSHLCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, SelectionDAG &DAG)
If the operand is a bitwise AND with a constant RHS, and the shift has a constant RHS and is the only...
static SDValue performORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, const AArch64Subtarget *Subtarget, const AArch64TargetLowering &TLI)
return SDValue()
static SDValue performANDCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file declares a class to represent arbitrary precision floating point values and provide a varie...
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define RegName(no)
static LVOptions Options
Definition LVOptions.cpp:25
lazy value info
static MachineBasicBlock * insertDivByZeroTrap(MachineInstr &MI, MachineBasicBlock *MBB)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
cl::opt< bool > EmitJalrReloc
cl::opt< bool > NoZeroDivCheck
static bool CC_Mips(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
static SDValue performMADD_MSUBCombine(SDNode *ROOTNode, SelectionDAG &CurDAG, const MipsSubtarget &Subtarget)
static bool invertFPCondCodeUser(Mips::CondCode CC)
This function returns true if the floating point conditional branches and conditional moves which use...
static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State, ArrayRef< MCPhysReg > F64Regs)
static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG, bool SingleFloat)
static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG, TargetLowering::DAGCombinerInfo &DCI, const MipsSubtarget &Subtarget)
static const MCPhysReg Mips64DPRegs[8]
static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG, bool IsLittle)
static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD, SDValue Chain, unsigned Offset)
static unsigned addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
static std::pair< bool, bool > parsePhysicalReg(StringRef C, StringRef &Prefix, unsigned long long &Reg)
This is a helper function to parse a physical register string and split it into non-numeric and numer...
static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD, SDValue Chain, SDValue Src, unsigned Offset)
static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasExtractInsert)
static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG, TargetLowering::DAGCombinerInfo &DCI, const MipsSubtarget &Subtarget)
static SDValue performSUBCombine(SDNode *N, SelectionDAG &DAG, TargetLowering::DAGCombinerInfo &DCI, const MipsSubtarget &Subtarget)
static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op)
static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasExtractInsert)
static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG, TargetLowering::DAGCombinerInfo &DCI, const MipsSubtarget &Subtarget)
static SDValue performSignExtendCombine(SDNode *N, SelectionDAG &DAG, TargetLowering::DAGCombinerInfo &DCI, const MipsSubtarget &Subtarget)
static SDValue performCMovFPCombine(SDNode *N, SelectionDAG &DAG, TargetLowering::DAGCombinerInfo &DCI, const MipsSubtarget &Subtarget)
static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA, EVT ArgVT, const SDLoc &DL, SelectionDAG &DAG)
static Mips::CondCode condCodeToFCC(ISD::CondCode CC)
static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True, SDValue False, const SDLoc &DL)
uint64_t IntrinsicInst * II
const SmallVectorImpl< MachineOperand > & Cond
SI optimize exec mask operations pre RA
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:487
This file defines the SmallVector class.
static const MCPhysReg IntRegs[32]
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
DEMANGLE_NAMESPACE_BEGIN bool starts_with(std::string_view self, char C) noexcept
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
static const MCPhysReg F32Regs[64]
Value * RHS
Value * LHS
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
const T * data() const
Definition ArrayRef.h:139
LLVM Basic Block Representation.
Definition BasicBlock.h:62
static BranchProbability getOne()
CCState - This class holds information needed while lowering arguments and return values.
unsigned getFirstUnallocated(ArrayRef< MCPhysReg > Regs) const
getFirstUnallocated - Return the index of the first unallocated register in the set,...
CallingConv::ID getCallingConv() const
uint64_t getStackSize() const
Returns the size of the currently allocated portion of the stack.
CCValAssign - Represent assignment of one arg/retval to a location.
Register getLocReg() const
LocInfo getLocInfo() const
static CCValAssign getReg(unsigned ValNo, MVT ValVT, MCRegister Reg, MVT LocVT, LocInfo HTP, bool IsCustom=false)
static CCValAssign getCustomReg(unsigned ValNo, MVT ValVT, MCRegister Reg, MVT LocVT, LocInfo HTP)
bool isUpperBitsInLoc() const
static CCValAssign getMem(unsigned ValNo, MVT ValVT, int64_t Offset, MVT LocVT, LocInfo HTP, bool IsCustom=false)
bool needsCustom() const
int64_t getLocMemOffset() const
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
LLVM_ABI bool isMustTailCall() const
Tests if this call site must be tail call optimized.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
uint64_t getZExtValue() const
int64_t getSExtValue() const
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
LLVM_ABI TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
A debug info location.
Definition DebugLoc.h:123
const char * getSymbol() const
This is a fast-path instruction selection class that generates poor code and doesn't support illegal ...
Definition FastISel.h:66
FunctionLoweringInfo - This contains information that is global to a function that is used when lower...
bool hasStructRetAttr() const
Determine if the function returns a structure through first or second pointer argument.
Definition Function.h:687
const Argument * const_arg_iterator
Definition Function.h:73
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:730
const GlobalValue * getGlobal() const
bool hasLocalLinkage() const
bool hasDLLImportStorageClass() const
LLVM_ABI const GlobalObject * getAliaseeObject() const
Definition Globals.cpp:441
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 & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addReg(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addUse(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addDef(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
const MachineOperand & getOperand(unsigned i) const
@ EK_GPRel32BlockAddress
EK_GPRel32BlockAddress - Each entry is an address of block, encoded with a relocation as gp-relative,...
@ EK_BlockAddress
EK_BlockAddress - Each entry is a plain address of block, e.g.: .word LBB123.
@ EK_GPRel64BlockAddress
EK_GPRel64BlockAddress - Each entry is an address of block, encoded with a relocation as gp-relative,...
@ MOVolatile
The memory access is volatile.
Flags getFlags() const
Return the raw flags of the source value,.
MachineOperand class - Representation of each machine instruction operand.
void setSubReg(unsigned subReg)
static MachineOperand CreateMCSymbol(MCSymbol *Sym, unsigned TargetFlags=0)
void setIsKill(bool Val=true)
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
void addLiveIn(MCRegister Reg, Register vreg=Register())
addLiveIn - Add the specified register as a live-in.
Align getAlign() const
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
const MachinePointerInfo & getPointerInfo() const
const SDValue & getChain() const
EVT getMemoryVT() const
Return the type of the in-memory value.
static SpecialCallingConvType getSpecialCallingConvForCallee(const SDNode *Callee, const MipsSubtarget &Subtarget)
Determine the SpecialCallingConvType for the given callee.
MipsFunctionInfo - This class is derived from MachineFunction private Mips target-specific informatio...
void setVarArgsFrameIndex(int Index)
unsigned getSRetReturnReg() const
MachinePointerInfo callPtrInfo(MachineFunction &MF, const char *ES)
Create a MachinePointerInfo that has an ExternalSymbolPseudoSourceValue object representing a GOT ent...
Register getGlobalBaseReg(MachineFunction &MF)
void setSRetReturnReg(unsigned Reg)
void setFormalArgInfo(unsigned Size, bool HasByval)
static const uint32_t * getMips16RetHelperMask()
bool hasMips32r6() const
bool hasMips4() const
bool hasMips64r2() const
bool isLittle() const
const MipsInstrInfo * getInstrInfo() const override
bool hasMips64r6() const
bool inMips16Mode() const
bool hasMips64() const
bool hasMips32() const
const MipsRegisterInfo * getRegisterInfo() const override
bool hasCnMips() const
bool isGP64bit() const
bool hasExtractInsert() const
Features related to the presence of specific instructions.
bool isSingleFloat() const
const TargetFrameLowering * getFrameLowering() const override
MVT getRegisterTypeForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const override
Return the register type for a given MVT, ensuring vectors are treated as a series of gpr sized integ...
bool hasBitTest(SDValue X, SDValue Y) const override
Return true if the target has a bit-test instruction: (X & (1 << Y)) ==/!= 0 This knowledge can be us...
static const MipsTargetLowering * create(const MipsTargetMachine &TM, const MipsSubtarget &STI)
SDValue getAddrGPRel(NodeTy *N, const SDLoc &DL, EVT Ty, SelectionDAG &DAG, bool IsN64) const
unsigned getVectorTypeBreakdownForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const override
Break down vectors to the correct number of gpr sized integers.
Register getRegisterByName(const char *RegName, LLT VT, const MachineFunction &MF) const override
Return the register ID of the name passed in.
SDValue getAddrNonPICSym64(NodeTy *N, const SDLoc &DL, EVT Ty, SelectionDAG &DAG) const
EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const override
getSetCCResultType - get the ISD::SETCC result ValueType
SDValue getAddrGlobal(NodeTy *N, const SDLoc &DL, EVT Ty, SelectionDAG &DAG, unsigned Flag, SDValue Chain, const MachinePointerInfo &PtrInfo) const
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:818
@ 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:778
@ 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:852
@ 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:992
@ 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:843
@ 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:795
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
@ VACOPY
VACOPY - VACOPY has 5 operands: an input chain, a destination pointer, a source pointer,...
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:764
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:849
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:810
@ 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:887
@ 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:925
@ READCYCLECOUNTER
READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:738
@ TRAP
TRAP - Trapping instruction.
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:855
@ 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:832
@ 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)
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Dead
Unused definition.
@ Define
Register definition.
@ Kill
The last use of a register.
@ EarlyClobber
Register definition happens before uses.
Not(const Pred &P) -> Not< Pred >
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
This is an optimization pass for GlobalISel generic memory operations.
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 unsigned getKillRegState(bool B)
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:165
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
bool CCAssignFn(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
CCAssignFn - This function assigns a location for Val, updating State to reflect the change.
constexpr bool isShiftedMask_64(uint64_t Value)
Return true if the argument contains a non-empty sequence of ones with the remainder zero (64 bit ver...
Definition MathExtras.h:273
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp: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