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