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