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