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