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