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