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