1 //===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interfaces that Mips uses to lower LLVM code into a
13 //===----------------------------------------------------------------------===//
14 #include "MipsISelLowering.h"
15 #include "InstPrinter/MipsInstPrinter.h"
16 #include "MCTargetDesc/MipsBaseInfo.h"
17 #include "MipsCCState.h"
18 #include "MipsMachineFunction.h"
19 #include "MipsSubtarget.h"
20 #include "MipsTargetMachine.h"
21 #include "MipsTargetObjectFile.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/ADT/StringSwitch.h"
24 #include "llvm/CodeGen/CallingConvLower.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/MachineJumpTableInfo.h"
29 #include "llvm/CodeGen/MachineRegisterInfo.h"
30 #include "llvm/CodeGen/SelectionDAGISel.h"
31 #include "llvm/CodeGen/ValueTypes.h"
32 #include "llvm/IR/CallingConv.h"
33 #include "llvm/IR/DerivedTypes.h"
34 #include "llvm/IR/GlobalVariable.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/raw_ostream.h"
43 #define DEBUG_TYPE "mips-lower"
45 STATISTIC(NumTailCalls, "Number of tail calls");
48 LargeGOT("mxgot", cl::Hidden,
49 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
52 NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
53 cl::desc("MIPS: Don't trap on integer division by zero."),
57 EnableMipsFastISel("mips-fast-isel", cl::Hidden,
58 cl::desc("Allow mips-fast-isel to be used"),
61 static const MCPhysReg Mips64DPRegs[8] = {
62 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
63 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
66 // If I is a shifted mask, set the size (Size) and the first bit of the
67 // mask (Pos), and return true.
68 // For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
69 static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
70 if (!isShiftedMask_64(I))
73 Size = CountPopulation_64(I);
74 Pos = countTrailingZeros(I);
78 SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
79 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
80 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
83 SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
85 unsigned Flag) const {
86 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
89 SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty,
91 unsigned Flag) const {
92 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
95 SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty,
97 unsigned Flag) const {
98 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
101 SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
103 unsigned Flag) const {
104 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
107 SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
109 unsigned Flag) const {
110 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
111 N->getOffset(), Flag);
114 const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
116 case MipsISD::JmpLink: return "MipsISD::JmpLink";
117 case MipsISD::TailCall: return "MipsISD::TailCall";
118 case MipsISD::Hi: return "MipsISD::Hi";
119 case MipsISD::Lo: return "MipsISD::Lo";
120 case MipsISD::GPRel: return "MipsISD::GPRel";
121 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
122 case MipsISD::Ret: return "MipsISD::Ret";
123 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
124 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
125 case MipsISD::FPCmp: return "MipsISD::FPCmp";
126 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
127 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
128 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
129 case MipsISD::MFHI: return "MipsISD::MFHI";
130 case MipsISD::MFLO: return "MipsISD::MFLO";
131 case MipsISD::MTLOHI: return "MipsISD::MTLOHI";
132 case MipsISD::Mult: return "MipsISD::Mult";
133 case MipsISD::Multu: return "MipsISD::Multu";
134 case MipsISD::MAdd: return "MipsISD::MAdd";
135 case MipsISD::MAddu: return "MipsISD::MAddu";
136 case MipsISD::MSub: return "MipsISD::MSub";
137 case MipsISD::MSubu: return "MipsISD::MSubu";
138 case MipsISD::DivRem: return "MipsISD::DivRem";
139 case MipsISD::DivRemU: return "MipsISD::DivRemU";
140 case MipsISD::DivRem16: return "MipsISD::DivRem16";
141 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
142 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
143 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
144 case MipsISD::Wrapper: return "MipsISD::Wrapper";
145 case MipsISD::Sync: return "MipsISD::Sync";
146 case MipsISD::Ext: return "MipsISD::Ext";
147 case MipsISD::Ins: return "MipsISD::Ins";
148 case MipsISD::LWL: return "MipsISD::LWL";
149 case MipsISD::LWR: return "MipsISD::LWR";
150 case MipsISD::SWL: return "MipsISD::SWL";
151 case MipsISD::SWR: return "MipsISD::SWR";
152 case MipsISD::LDL: return "MipsISD::LDL";
153 case MipsISD::LDR: return "MipsISD::LDR";
154 case MipsISD::SDL: return "MipsISD::SDL";
155 case MipsISD::SDR: return "MipsISD::SDR";
156 case MipsISD::EXTP: return "MipsISD::EXTP";
157 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
158 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
159 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
160 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
161 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
162 case MipsISD::SHILO: return "MipsISD::SHILO";
163 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
164 case MipsISD::MULT: return "MipsISD::MULT";
165 case MipsISD::MULTU: return "MipsISD::MULTU";
166 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
167 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
168 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
169 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
170 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
171 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
172 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
173 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
174 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
175 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
176 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
177 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
178 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
179 case MipsISD::VCEQ: return "MipsISD::VCEQ";
180 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
181 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
182 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
183 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
184 case MipsISD::VSMAX: return "MipsISD::VSMAX";
185 case MipsISD::VSMIN: return "MipsISD::VSMIN";
186 case MipsISD::VUMAX: return "MipsISD::VUMAX";
187 case MipsISD::VUMIN: return "MipsISD::VUMIN";
188 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
189 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
190 case MipsISD::VNOR: return "MipsISD::VNOR";
191 case MipsISD::VSHF: return "MipsISD::VSHF";
192 case MipsISD::SHF: return "MipsISD::SHF";
193 case MipsISD::ILVEV: return "MipsISD::ILVEV";
194 case MipsISD::ILVOD: return "MipsISD::ILVOD";
195 case MipsISD::ILVL: return "MipsISD::ILVL";
196 case MipsISD::ILVR: return "MipsISD::ILVR";
197 case MipsISD::PCKEV: return "MipsISD::PCKEV";
198 case MipsISD::PCKOD: return "MipsISD::PCKOD";
199 case MipsISD::INSVE: return "MipsISD::INSVE";
200 default: return nullptr;
204 MipsTargetLowering::MipsTargetLowering(const MipsTargetMachine &TM,
205 const MipsSubtarget &STI)
206 : TargetLowering(TM), Subtarget(STI) {
207 // Mips does not have i1 type, so use i32 for
208 // setcc operations results (slt, sgt, ...).
209 setBooleanContents(ZeroOrOneBooleanContent);
210 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
211 // The cmp.cond.fmt instruction in MIPS32r6/MIPS64r6 uses 0 and -1 like MSA
212 // does. Integer booleans still use 0 and 1.
213 if (Subtarget.hasMips32r6())
214 setBooleanContents(ZeroOrOneBooleanContent,
215 ZeroOrNegativeOneBooleanContent);
217 // Load extented operations for i1 types must be promoted
218 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
219 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
220 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
222 // MIPS doesn't have extending float->double load/store
223 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
224 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
226 // Used by legalize types to correctly generate the setcc result.
227 // Without this, every float setcc comes with a AND/OR with the result,
228 // we don't want this, since the fpcmp result goes to a flag register,
229 // which is used implicitly by brcond and select operations.
230 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
232 // Mips Custom Operations
233 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
234 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
235 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
236 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
237 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
238 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
239 setOperationAction(ISD::SELECT, MVT::f32, Custom);
240 setOperationAction(ISD::SELECT, MVT::f64, Custom);
241 setOperationAction(ISD::SELECT, MVT::i32, Custom);
242 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
243 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
244 setOperationAction(ISD::SETCC, MVT::f32, Custom);
245 setOperationAction(ISD::SETCC, MVT::f64, Custom);
246 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
247 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
248 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
249 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
251 if (Subtarget.isGP64bit()) {
252 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
253 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
254 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
255 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
256 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
257 setOperationAction(ISD::SELECT, MVT::i64, Custom);
258 setOperationAction(ISD::LOAD, MVT::i64, Custom);
259 setOperationAction(ISD::STORE, MVT::i64, Custom);
260 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
263 if (!Subtarget.isGP64bit()) {
264 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
265 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
266 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
269 setOperationAction(ISD::ADD, MVT::i32, Custom);
270 if (Subtarget.isGP64bit())
271 setOperationAction(ISD::ADD, MVT::i64, Custom);
273 setOperationAction(ISD::SDIV, MVT::i32, Expand);
274 setOperationAction(ISD::SREM, MVT::i32, Expand);
275 setOperationAction(ISD::UDIV, MVT::i32, Expand);
276 setOperationAction(ISD::UREM, MVT::i32, Expand);
277 setOperationAction(ISD::SDIV, MVT::i64, Expand);
278 setOperationAction(ISD::SREM, MVT::i64, Expand);
279 setOperationAction(ISD::UDIV, MVT::i64, Expand);
280 setOperationAction(ISD::UREM, MVT::i64, Expand);
282 // Operations not directly supported by Mips.
283 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
284 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
285 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
286 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
287 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
288 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
289 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
290 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
291 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
292 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
293 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
294 if (Subtarget.hasCnMips()) {
295 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
296 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
298 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
299 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
301 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
302 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
303 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
304 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
305 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
306 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
307 setOperationAction(ISD::ROTL, MVT::i32, Expand);
308 setOperationAction(ISD::ROTL, MVT::i64, Expand);
309 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
310 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
312 if (!Subtarget.hasMips32r2())
313 setOperationAction(ISD::ROTR, MVT::i32, Expand);
315 if (!Subtarget.hasMips64r2())
316 setOperationAction(ISD::ROTR, MVT::i64, Expand);
318 setOperationAction(ISD::FSIN, MVT::f32, Expand);
319 setOperationAction(ISD::FSIN, MVT::f64, Expand);
320 setOperationAction(ISD::FCOS, MVT::f32, Expand);
321 setOperationAction(ISD::FCOS, MVT::f64, Expand);
322 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
323 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
324 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
325 setOperationAction(ISD::FPOW, MVT::f32, Expand);
326 setOperationAction(ISD::FPOW, MVT::f64, Expand);
327 setOperationAction(ISD::FLOG, MVT::f32, Expand);
328 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
329 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
330 setOperationAction(ISD::FEXP, MVT::f32, Expand);
331 setOperationAction(ISD::FMA, MVT::f32, Expand);
332 setOperationAction(ISD::FMA, MVT::f64, Expand);
333 setOperationAction(ISD::FREM, MVT::f32, Expand);
334 setOperationAction(ISD::FREM, MVT::f64, Expand);
336 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
338 setOperationAction(ISD::VASTART, MVT::Other, Custom);
339 setOperationAction(ISD::VAARG, MVT::Other, Custom);
340 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
341 setOperationAction(ISD::VAEND, MVT::Other, Expand);
343 // Use the default for now
344 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
345 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
347 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
348 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
349 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
350 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
352 setInsertFencesForAtomic(true);
354 if (!Subtarget.hasMips32r2()) {
355 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
356 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
359 // MIPS16 lacks MIPS32's clz and clo instructions.
360 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode())
361 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
362 if (!Subtarget.hasMips64())
363 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
365 if (!Subtarget.hasMips32r2())
366 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
367 if (!Subtarget.hasMips64r2())
368 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
370 if (Subtarget.isGP64bit()) {
371 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
372 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
373 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
374 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
377 setOperationAction(ISD::TRAP, MVT::Other, Legal);
379 setTargetDAGCombine(ISD::SDIVREM);
380 setTargetDAGCombine(ISD::UDIVREM);
381 setTargetDAGCombine(ISD::SELECT);
382 setTargetDAGCombine(ISD::AND);
383 setTargetDAGCombine(ISD::OR);
384 setTargetDAGCombine(ISD::ADD);
386 setMinFunctionAlignment(Subtarget.isGP64bit() ? 3 : 2);
388 // The arguments on the stack are defined in terms of 4-byte slots on O32
389 // and 8-byte slots on N32/N64.
390 setMinStackArgumentAlignment(
391 (Subtarget.isABI_N32() || Subtarget.isABI_N64()) ? 8 : 4);
393 setStackPointerRegisterToSaveRestore(Subtarget.isABI_N64() ? Mips::SP_64
396 setExceptionPointerRegister(Subtarget.isABI_N64() ? Mips::A0_64 : Mips::A0);
397 setExceptionSelectorRegister(Subtarget.isABI_N64() ? Mips::A1_64 : Mips::A1);
399 MaxStoresPerMemcpy = 16;
401 isMicroMips = Subtarget.inMicroMipsMode();
404 const MipsTargetLowering *MipsTargetLowering::create(const MipsTargetMachine &TM,
405 const MipsSubtarget &STI) {
406 if (STI.inMips16Mode())
407 return llvm::createMips16TargetLowering(TM, STI);
409 return llvm::createMipsSETargetLowering(TM, STI);
412 // Create a fast isel object.
414 MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
415 const TargetLibraryInfo *libInfo) const {
416 if (!EnableMipsFastISel)
417 return TargetLowering::createFastISel(funcInfo, libInfo);
418 return Mips::createFastISel(funcInfo, libInfo);
421 EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
424 return VT.changeVectorElementTypeToInteger();
427 static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
428 TargetLowering::DAGCombinerInfo &DCI,
429 const MipsSubtarget &Subtarget) {
430 if (DCI.isBeforeLegalizeOps())
433 EVT Ty = N->getValueType(0);
434 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
435 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
436 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
440 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
441 N->getOperand(0), N->getOperand(1));
442 SDValue InChain = DAG.getEntryNode();
443 SDValue InGlue = DivRem;
446 if (N->hasAnyUseOfValue(0)) {
447 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
449 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
450 InChain = CopyFromLo.getValue(1);
451 InGlue = CopyFromLo.getValue(2);
455 if (N->hasAnyUseOfValue(1)) {
456 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
458 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
464 static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
466 default: llvm_unreachable("Unknown fp condition code!");
468 case ISD::SETOEQ: return Mips::FCOND_OEQ;
469 case ISD::SETUNE: return Mips::FCOND_UNE;
471 case ISD::SETOLT: return Mips::FCOND_OLT;
473 case ISD::SETOGT: return Mips::FCOND_OGT;
475 case ISD::SETOLE: return Mips::FCOND_OLE;
477 case ISD::SETOGE: return Mips::FCOND_OGE;
478 case ISD::SETULT: return Mips::FCOND_ULT;
479 case ISD::SETULE: return Mips::FCOND_ULE;
480 case ISD::SETUGT: return Mips::FCOND_UGT;
481 case ISD::SETUGE: return Mips::FCOND_UGE;
482 case ISD::SETUO: return Mips::FCOND_UN;
483 case ISD::SETO: return Mips::FCOND_OR;
485 case ISD::SETONE: return Mips::FCOND_ONE;
486 case ISD::SETUEQ: return Mips::FCOND_UEQ;
491 /// This function returns true if the floating point conditional branches and
492 /// conditional moves which use condition code CC should be inverted.
493 static bool invertFPCondCodeUser(Mips::CondCode CC) {
494 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
497 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
498 "Illegal Condition Code");
503 // Creates and returns an FPCmp node from a setcc node.
504 // Returns Op if setcc is not a floating point comparison.
505 static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
506 // must be a SETCC node
507 if (Op.getOpcode() != ISD::SETCC)
510 SDValue LHS = Op.getOperand(0);
512 if (!LHS.getValueType().isFloatingPoint())
515 SDValue RHS = Op.getOperand(1);
518 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
519 // node if necessary.
520 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
522 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
523 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
526 // Creates and returns a CMovFPT/F node.
527 static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
528 SDValue False, SDLoc DL) {
529 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
530 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
531 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
533 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
534 True.getValueType(), True, FCC0, False, Cond);
537 static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
538 TargetLowering::DAGCombinerInfo &DCI,
539 const MipsSubtarget &Subtarget) {
540 if (DCI.isBeforeLegalizeOps())
543 SDValue SetCC = N->getOperand(0);
545 if ((SetCC.getOpcode() != ISD::SETCC) ||
546 !SetCC.getOperand(0).getValueType().isInteger())
549 SDValue False = N->getOperand(2);
550 EVT FalseTy = False.getValueType();
552 if (!FalseTy.isInteger())
555 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
557 // If the RHS (False) is 0, we swap the order of the operands
558 // of ISD::SELECT (obviously also inverting the condition) so that we can
559 // take advantage of conditional moves using the $0 register.
561 // return (a != 0) ? x : 0;
569 if (!FalseC->getZExtValue()) {
570 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
571 SDValue True = N->getOperand(1);
573 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
574 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
576 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
579 // If both operands are integer constants there's a possibility that we
580 // can do some interesting optimizations.
581 SDValue True = N->getOperand(1);
582 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
584 if (!TrueC || !True.getValueType().isInteger())
587 // We'll also ignore MVT::i64 operands as this optimizations proves
588 // to be ineffective because of the required sign extensions as the result
589 // of a SETCC operator is always MVT::i32 for non-vector types.
590 if (True.getValueType() == MVT::i64)
593 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
595 // 1) (a < x) ? y : y-1
597 // addiu $reg2, $reg1, y-1
599 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
601 // 2) (a < x) ? y-1 : y
603 // xor $reg1, $reg1, 1
604 // addiu $reg2, $reg1, y-1
606 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
607 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
608 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
609 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
612 // Couldn't optimize.
616 static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
617 TargetLowering::DAGCombinerInfo &DCI,
618 const MipsSubtarget &Subtarget) {
619 // Pattern match EXT.
620 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
621 // => ext $dst, $src, size, pos
622 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
625 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
626 unsigned ShiftRightOpc = ShiftRight.getOpcode();
628 // Op's first operand must be a shift right.
629 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
632 // The second operand of the shift must be an immediate.
634 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
637 uint64_t Pos = CN->getZExtValue();
638 uint64_t SMPos, SMSize;
640 // Op's second operand must be a shifted mask.
641 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
642 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
645 // Return if the shifted mask does not start at bit 0 or the sum of its size
646 // and Pos exceeds the word's size.
647 EVT ValTy = N->getValueType(0);
648 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
651 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
652 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
653 DAG.getConstant(SMSize, MVT::i32));
656 static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
657 TargetLowering::DAGCombinerInfo &DCI,
658 const MipsSubtarget &Subtarget) {
659 // Pattern match INS.
660 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
661 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
662 // => ins $dst, $src, size, pos, $src1
663 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
666 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
667 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
670 // See if Op's first operand matches (and $src1 , mask0).
671 if (And0.getOpcode() != ISD::AND)
674 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
675 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
678 // See if Op's second operand matches (and (shl $src, pos), mask1).
679 if (And1.getOpcode() != ISD::AND)
682 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
683 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
686 // The shift masks must have the same position and size.
687 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
690 SDValue Shl = And1.getOperand(0);
691 if (Shl.getOpcode() != ISD::SHL)
694 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
697 unsigned Shamt = CN->getZExtValue();
699 // Return if the shift amount and the first bit position of mask are not the
701 EVT ValTy = N->getValueType(0);
702 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
705 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
706 DAG.getConstant(SMPos0, MVT::i32),
707 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
710 static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
711 TargetLowering::DAGCombinerInfo &DCI,
712 const MipsSubtarget &Subtarget) {
713 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
715 if (DCI.isBeforeLegalizeOps())
718 SDValue Add = N->getOperand(1);
720 if (Add.getOpcode() != ISD::ADD)
723 SDValue Lo = Add.getOperand(1);
725 if ((Lo.getOpcode() != MipsISD::Lo) ||
726 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
729 EVT ValTy = N->getValueType(0);
732 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
734 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
737 SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
739 SelectionDAG &DAG = DCI.DAG;
740 unsigned Opc = N->getOpcode();
746 return performDivRemCombine(N, DAG, DCI, Subtarget);
748 return performSELECTCombine(N, DAG, DCI, Subtarget);
750 return performANDCombine(N, DAG, DCI, Subtarget);
752 return performORCombine(N, DAG, DCI, Subtarget);
754 return performADDCombine(N, DAG, DCI, Subtarget);
761 MipsTargetLowering::LowerOperationWrapper(SDNode *N,
762 SmallVectorImpl<SDValue> &Results,
763 SelectionDAG &DAG) const {
764 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
766 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
767 Results.push_back(Res.getValue(I));
771 MipsTargetLowering::ReplaceNodeResults(SDNode *N,
772 SmallVectorImpl<SDValue> &Results,
773 SelectionDAG &DAG) const {
774 return LowerOperationWrapper(N, Results, DAG);
777 SDValue MipsTargetLowering::
778 LowerOperation(SDValue Op, SelectionDAG &DAG) const
780 switch (Op.getOpcode())
782 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
783 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
784 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
785 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
786 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
787 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
788 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
789 case ISD::SELECT: return lowerSELECT(Op, DAG);
790 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
791 case ISD::SETCC: return lowerSETCC(Op, DAG);
792 case ISD::VASTART: return lowerVASTART(Op, DAG);
793 case ISD::VAARG: return lowerVAARG(Op, DAG);
794 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
795 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
796 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
797 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
798 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
799 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
800 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
801 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
802 case ISD::LOAD: return lowerLOAD(Op, DAG);
803 case ISD::STORE: return lowerSTORE(Op, DAG);
804 case ISD::ADD: return lowerADD(Op, DAG);
805 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
810 //===----------------------------------------------------------------------===//
811 // Lower helper functions
812 //===----------------------------------------------------------------------===//
814 // addLiveIn - This helper function adds the specified physical register to the
815 // MachineFunction as a live in value. It also creates a corresponding
816 // virtual register for it.
818 addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
820 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
821 MF.getRegInfo().addLiveIn(PReg, VReg);
825 static MachineBasicBlock *insertDivByZeroTrap(MachineInstr *MI,
826 MachineBasicBlock &MBB,
827 const TargetInstrInfo &TII,
832 // Insert instruction "teq $divisor_reg, $zero, 7".
833 MachineBasicBlock::iterator I(MI);
834 MachineInstrBuilder MIB;
835 MachineOperand &Divisor = MI->getOperand(2);
836 MIB = BuildMI(MBB, std::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
837 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
838 .addReg(Mips::ZERO).addImm(7);
840 // Use the 32-bit sub-register if this is a 64-bit division.
842 MIB->getOperand(0).setSubReg(Mips::sub_32);
844 // Clear Divisor's kill flag.
845 Divisor.setIsKill(false);
847 // We would normally delete the original instruction here but in this case
848 // we only needed to inject an additional instruction rather than replace it.
854 MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
855 MachineBasicBlock *BB) const {
856 switch (MI->getOpcode()) {
858 llvm_unreachable("Unexpected instr type to insert");
859 case Mips::ATOMIC_LOAD_ADD_I8:
860 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
861 case Mips::ATOMIC_LOAD_ADD_I16:
862 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
863 case Mips::ATOMIC_LOAD_ADD_I32:
864 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
865 case Mips::ATOMIC_LOAD_ADD_I64:
866 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
868 case Mips::ATOMIC_LOAD_AND_I8:
869 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
870 case Mips::ATOMIC_LOAD_AND_I16:
871 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
872 case Mips::ATOMIC_LOAD_AND_I32:
873 return emitAtomicBinary(MI, BB, 4, Mips::AND);
874 case Mips::ATOMIC_LOAD_AND_I64:
875 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
877 case Mips::ATOMIC_LOAD_OR_I8:
878 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
879 case Mips::ATOMIC_LOAD_OR_I16:
880 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
881 case Mips::ATOMIC_LOAD_OR_I32:
882 return emitAtomicBinary(MI, BB, 4, Mips::OR);
883 case Mips::ATOMIC_LOAD_OR_I64:
884 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
886 case Mips::ATOMIC_LOAD_XOR_I8:
887 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
888 case Mips::ATOMIC_LOAD_XOR_I16:
889 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
890 case Mips::ATOMIC_LOAD_XOR_I32:
891 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
892 case Mips::ATOMIC_LOAD_XOR_I64:
893 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
895 case Mips::ATOMIC_LOAD_NAND_I8:
896 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
897 case Mips::ATOMIC_LOAD_NAND_I16:
898 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
899 case Mips::ATOMIC_LOAD_NAND_I32:
900 return emitAtomicBinary(MI, BB, 4, 0, true);
901 case Mips::ATOMIC_LOAD_NAND_I64:
902 return emitAtomicBinary(MI, BB, 8, 0, true);
904 case Mips::ATOMIC_LOAD_SUB_I8:
905 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
906 case Mips::ATOMIC_LOAD_SUB_I16:
907 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
908 case Mips::ATOMIC_LOAD_SUB_I32:
909 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
910 case Mips::ATOMIC_LOAD_SUB_I64:
911 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
913 case Mips::ATOMIC_SWAP_I8:
914 return emitAtomicBinaryPartword(MI, BB, 1, 0);
915 case Mips::ATOMIC_SWAP_I16:
916 return emitAtomicBinaryPartword(MI, BB, 2, 0);
917 case Mips::ATOMIC_SWAP_I32:
918 return emitAtomicBinary(MI, BB, 4, 0);
919 case Mips::ATOMIC_SWAP_I64:
920 return emitAtomicBinary(MI, BB, 8, 0);
922 case Mips::ATOMIC_CMP_SWAP_I8:
923 return emitAtomicCmpSwapPartword(MI, BB, 1);
924 case Mips::ATOMIC_CMP_SWAP_I16:
925 return emitAtomicCmpSwapPartword(MI, BB, 2);
926 case Mips::ATOMIC_CMP_SWAP_I32:
927 return emitAtomicCmpSwap(MI, BB, 4);
928 case Mips::ATOMIC_CMP_SWAP_I64:
929 return emitAtomicCmpSwap(MI, BB, 8);
930 case Mips::PseudoSDIV:
931 case Mips::PseudoUDIV:
936 return insertDivByZeroTrap(
937 MI, *BB, *getTargetMachine().getSubtargetImpl()->getInstrInfo(), false);
938 case Mips::PseudoDSDIV:
939 case Mips::PseudoDUDIV:
944 return insertDivByZeroTrap(
945 MI, *BB, *getTargetMachine().getSubtargetImpl()->getInstrInfo(), true);
947 return emitSEL_D(MI, BB);
949 case Mips::PseudoSELECT_I:
950 case Mips::PseudoSELECT_I64:
951 case Mips::PseudoSELECT_S:
952 case Mips::PseudoSELECT_D32:
953 case Mips::PseudoSELECT_D64:
954 return emitPseudoSELECT(MI, BB, false, Mips::BNE);
955 case Mips::PseudoSELECTFP_F_I:
956 case Mips::PseudoSELECTFP_F_I64:
957 case Mips::PseudoSELECTFP_F_S:
958 case Mips::PseudoSELECTFP_F_D32:
959 case Mips::PseudoSELECTFP_F_D64:
960 return emitPseudoSELECT(MI, BB, true, Mips::BC1F);
961 case Mips::PseudoSELECTFP_T_I:
962 case Mips::PseudoSELECTFP_T_I64:
963 case Mips::PseudoSELECTFP_T_S:
964 case Mips::PseudoSELECTFP_T_D32:
965 case Mips::PseudoSELECTFP_T_D64:
966 return emitPseudoSELECT(MI, BB, true, Mips::BC1T);
970 // This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
971 // Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
973 MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
974 unsigned Size, unsigned BinOpcode,
976 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
978 MachineFunction *MF = BB->getParent();
979 MachineRegisterInfo &RegInfo = MF->getRegInfo();
980 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
981 const TargetInstrInfo *TII =
982 getTargetMachine().getSubtargetImpl()->getInstrInfo();
983 DebugLoc DL = MI->getDebugLoc();
984 unsigned LL, SC, AND, NOR, ZERO, BEQ;
991 LL = Subtarget.hasMips32r6() ? Mips::LL_R6 : Mips::LL;
992 SC = Subtarget.hasMips32r6() ? Mips::SC_R6 : Mips::SC;
999 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1000 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
1003 ZERO = Mips::ZERO_64;
1007 unsigned OldVal = MI->getOperand(0).getReg();
1008 unsigned Ptr = MI->getOperand(1).getReg();
1009 unsigned Incr = MI->getOperand(2).getReg();
1011 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1012 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1013 unsigned Success = RegInfo.createVirtualRegister(RC);
1015 // insert new blocks after the current block
1016 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1017 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1018 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1019 MachineFunction::iterator It = BB;
1021 MF->insert(It, loopMBB);
1022 MF->insert(It, exitMBB);
1024 // Transfer the remainder of BB and its successor edges to exitMBB.
1025 exitMBB->splice(exitMBB->begin(), BB,
1026 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1027 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1031 // fallthrough --> loopMBB
1032 BB->addSuccessor(loopMBB);
1033 loopMBB->addSuccessor(loopMBB);
1034 loopMBB->addSuccessor(exitMBB);
1037 // ll oldval, 0(ptr)
1038 // <binop> storeval, oldval, incr
1039 // sc success, storeval, 0(ptr)
1040 // beq success, $0, loopMBB
1042 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
1044 // and andres, oldval, incr
1045 // nor storeval, $0, andres
1046 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1047 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
1048 } else if (BinOpcode) {
1049 // <binop> storeval, oldval, incr
1050 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
1054 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1055 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
1057 MI->eraseFromParent(); // The instruction is gone now.
1062 MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
1063 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
1064 unsigned SrcReg) const {
1065 const TargetInstrInfo *TII =
1066 getTargetMachine().getSubtargetImpl()->getInstrInfo();
1067 DebugLoc DL = MI->getDebugLoc();
1069 if (Subtarget.hasMips32r2() && Size == 1) {
1070 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1074 if (Subtarget.hasMips32r2() && Size == 2) {
1075 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1079 MachineFunction *MF = BB->getParent();
1080 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1081 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1082 unsigned ScrReg = RegInfo.createVirtualRegister(RC);
1085 int64_t ShiftImm = 32 - (Size * 8);
1087 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1088 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1093 MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
1094 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
1096 assert((Size == 1 || Size == 2) &&
1097 "Unsupported size for EmitAtomicBinaryPartial.");
1099 MachineFunction *MF = BB->getParent();
1100 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1101 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1102 const TargetInstrInfo *TII =
1103 getTargetMachine().getSubtargetImpl()->getInstrInfo();
1104 DebugLoc DL = MI->getDebugLoc();
1106 unsigned Dest = MI->getOperand(0).getReg();
1107 unsigned Ptr = MI->getOperand(1).getReg();
1108 unsigned Incr = MI->getOperand(2).getReg();
1110 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1111 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
1112 unsigned Mask = RegInfo.createVirtualRegister(RC);
1113 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
1114 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1115 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1116 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
1117 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1118 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1119 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1120 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1121 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
1122 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1123 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1124 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1125 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1126 unsigned Success = RegInfo.createVirtualRegister(RC);
1128 // insert new blocks after the current block
1129 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1130 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1131 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1132 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1133 MachineFunction::iterator It = BB;
1135 MF->insert(It, loopMBB);
1136 MF->insert(It, sinkMBB);
1137 MF->insert(It, exitMBB);
1139 // Transfer the remainder of BB and its successor edges to exitMBB.
1140 exitMBB->splice(exitMBB->begin(), BB,
1141 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1142 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1144 BB->addSuccessor(loopMBB);
1145 loopMBB->addSuccessor(loopMBB);
1146 loopMBB->addSuccessor(sinkMBB);
1147 sinkMBB->addSuccessor(exitMBB);
1150 // addiu masklsb2,$0,-4 # 0xfffffffc
1151 // and alignedaddr,ptr,masklsb2
1152 // andi ptrlsb2,ptr,3
1153 // sll shiftamt,ptrlsb2,3
1154 // ori maskupper,$0,255 # 0xff
1155 // sll mask,maskupper,shiftamt
1156 // nor mask2,$0,mask
1157 // sll incr2,incr,shiftamt
1159 int64_t MaskImm = (Size == 1) ? 255 : 65535;
1160 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
1161 .addReg(Mips::ZERO).addImm(-4);
1162 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
1163 .addReg(Ptr).addReg(MaskLSB2);
1164 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1165 if (Subtarget.isLittle()) {
1166 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1168 unsigned Off = RegInfo.createVirtualRegister(RC);
1169 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1170 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1171 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1173 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
1174 .addReg(Mips::ZERO).addImm(MaskImm);
1175 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
1176 .addReg(MaskUpper).addReg(ShiftAmt);
1177 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1178 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
1180 // atomic.load.binop
1182 // ll oldval,0(alignedaddr)
1183 // binop binopres,oldval,incr2
1184 // and newval,binopres,mask
1185 // and maskedoldval0,oldval,mask2
1186 // or storeval,maskedoldval0,newval
1187 // sc success,storeval,0(alignedaddr)
1188 // beq success,$0,loopMBB
1192 // ll oldval,0(alignedaddr)
1193 // and newval,incr2,mask
1194 // and maskedoldval0,oldval,mask2
1195 // or storeval,maskedoldval0,newval
1196 // sc success,storeval,0(alignedaddr)
1197 // beq success,$0,loopMBB
1200 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
1202 // and andres, oldval, incr2
1203 // nor binopres, $0, andres
1204 // and newval, binopres, mask
1205 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1206 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
1207 .addReg(Mips::ZERO).addReg(AndRes);
1208 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
1209 } else if (BinOpcode) {
1210 // <binop> binopres, oldval, incr2
1211 // and newval, binopres, mask
1212 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1213 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
1214 } else { // atomic.swap
1215 // and newval, incr2, mask
1216 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
1219 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
1220 .addReg(OldVal).addReg(Mask2);
1221 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
1222 .addReg(MaskedOldVal0).addReg(NewVal);
1223 BuildMI(BB, DL, TII->get(Mips::SC), Success)
1224 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
1225 BuildMI(BB, DL, TII->get(Mips::BEQ))
1226 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
1229 // and maskedoldval1,oldval,mask
1230 // srl srlres,maskedoldval1,shiftamt
1231 // sign_extend dest,srlres
1234 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
1235 .addReg(OldVal).addReg(Mask);
1236 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
1237 .addReg(MaskedOldVal1).addReg(ShiftAmt);
1238 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
1240 MI->eraseFromParent(); // The instruction is gone now.
1245 MachineBasicBlock * MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
1246 MachineBasicBlock *BB,
1247 unsigned Size) const {
1248 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
1250 MachineFunction *MF = BB->getParent();
1251 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1252 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
1253 const TargetInstrInfo *TII =
1254 getTargetMachine().getSubtargetImpl()->getInstrInfo();
1255 DebugLoc DL = MI->getDebugLoc();
1256 unsigned LL, SC, ZERO, BNE, BEQ;
1259 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1260 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
1267 ZERO = Mips::ZERO_64;
1272 unsigned Dest = MI->getOperand(0).getReg();
1273 unsigned Ptr = MI->getOperand(1).getReg();
1274 unsigned OldVal = MI->getOperand(2).getReg();
1275 unsigned NewVal = MI->getOperand(3).getReg();
1277 unsigned Success = RegInfo.createVirtualRegister(RC);
1279 // insert new blocks after the current block
1280 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1281 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1282 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1283 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1284 MachineFunction::iterator It = BB;
1286 MF->insert(It, loop1MBB);
1287 MF->insert(It, loop2MBB);
1288 MF->insert(It, exitMBB);
1290 // Transfer the remainder of BB and its successor edges to exitMBB.
1291 exitMBB->splice(exitMBB->begin(), BB,
1292 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1293 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1297 // fallthrough --> loop1MBB
1298 BB->addSuccessor(loop1MBB);
1299 loop1MBB->addSuccessor(exitMBB);
1300 loop1MBB->addSuccessor(loop2MBB);
1301 loop2MBB->addSuccessor(loop1MBB);
1302 loop2MBB->addSuccessor(exitMBB);
1306 // bne dest, oldval, exitMBB
1308 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1309 BuildMI(BB, DL, TII->get(BNE))
1310 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
1313 // sc success, newval, 0(ptr)
1314 // beq success, $0, loop1MBB
1316 BuildMI(BB, DL, TII->get(SC), Success)
1317 .addReg(NewVal).addReg(Ptr).addImm(0);
1318 BuildMI(BB, DL, TII->get(BEQ))
1319 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
1321 MI->eraseFromParent(); // The instruction is gone now.
1327 MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
1328 MachineBasicBlock *BB,
1329 unsigned Size) const {
1330 assert((Size == 1 || Size == 2) &&
1331 "Unsupported size for EmitAtomicCmpSwapPartial.");
1333 MachineFunction *MF = BB->getParent();
1334 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1335 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1336 const TargetInstrInfo *TII =
1337 getTargetMachine().getSubtargetImpl()->getInstrInfo();
1338 DebugLoc DL = MI->getDebugLoc();
1340 unsigned Dest = MI->getOperand(0).getReg();
1341 unsigned Ptr = MI->getOperand(1).getReg();
1342 unsigned CmpVal = MI->getOperand(2).getReg();
1343 unsigned NewVal = MI->getOperand(3).getReg();
1345 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1346 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
1347 unsigned Mask = RegInfo.createVirtualRegister(RC);
1348 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
1349 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1350 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1351 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1352 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1353 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1354 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1355 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1356 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1357 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1358 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1359 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1360 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1361 unsigned Success = RegInfo.createVirtualRegister(RC);
1363 // insert new blocks after the current block
1364 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1365 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1366 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1367 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1368 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1369 MachineFunction::iterator It = BB;
1371 MF->insert(It, loop1MBB);
1372 MF->insert(It, loop2MBB);
1373 MF->insert(It, sinkMBB);
1374 MF->insert(It, exitMBB);
1376 // Transfer the remainder of BB and its successor edges to exitMBB.
1377 exitMBB->splice(exitMBB->begin(), BB,
1378 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1379 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1381 BB->addSuccessor(loop1MBB);
1382 loop1MBB->addSuccessor(sinkMBB);
1383 loop1MBB->addSuccessor(loop2MBB);
1384 loop2MBB->addSuccessor(loop1MBB);
1385 loop2MBB->addSuccessor(sinkMBB);
1386 sinkMBB->addSuccessor(exitMBB);
1388 // FIXME: computation of newval2 can be moved to loop2MBB.
1390 // addiu masklsb2,$0,-4 # 0xfffffffc
1391 // and alignedaddr,ptr,masklsb2
1392 // andi ptrlsb2,ptr,3
1393 // sll shiftamt,ptrlsb2,3
1394 // ori maskupper,$0,255 # 0xff
1395 // sll mask,maskupper,shiftamt
1396 // nor mask2,$0,mask
1397 // andi maskedcmpval,cmpval,255
1398 // sll shiftedcmpval,maskedcmpval,shiftamt
1399 // andi maskednewval,newval,255
1400 // sll shiftednewval,maskednewval,shiftamt
1401 int64_t MaskImm = (Size == 1) ? 255 : 65535;
1402 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
1403 .addReg(Mips::ZERO).addImm(-4);
1404 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
1405 .addReg(Ptr).addReg(MaskLSB2);
1406 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1407 if (Subtarget.isLittle()) {
1408 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1410 unsigned Off = RegInfo.createVirtualRegister(RC);
1411 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1412 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1413 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1415 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
1416 .addReg(Mips::ZERO).addImm(MaskImm);
1417 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
1418 .addReg(MaskUpper).addReg(ShiftAmt);
1419 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1420 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
1421 .addReg(CmpVal).addImm(MaskImm);
1422 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
1423 .addReg(MaskedCmpVal).addReg(ShiftAmt);
1424 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
1425 .addReg(NewVal).addImm(MaskImm);
1426 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
1427 .addReg(MaskedNewVal).addReg(ShiftAmt);
1430 // ll oldval,0(alginedaddr)
1431 // and maskedoldval0,oldval,mask
1432 // bne maskedoldval0,shiftedcmpval,sinkMBB
1434 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
1435 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
1436 .addReg(OldVal).addReg(Mask);
1437 BuildMI(BB, DL, TII->get(Mips::BNE))
1438 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
1441 // and maskedoldval1,oldval,mask2
1442 // or storeval,maskedoldval1,shiftednewval
1443 // sc success,storeval,0(alignedaddr)
1444 // beq success,$0,loop1MBB
1446 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
1447 .addReg(OldVal).addReg(Mask2);
1448 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
1449 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1450 BuildMI(BB, DL, TII->get(Mips::SC), Success)
1451 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
1452 BuildMI(BB, DL, TII->get(Mips::BEQ))
1453 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
1456 // srl srlres,maskedoldval0,shiftamt
1457 // sign_extend dest,srlres
1460 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
1461 .addReg(MaskedOldVal0).addReg(ShiftAmt);
1462 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
1464 MI->eraseFromParent(); // The instruction is gone now.
1469 MachineBasicBlock *MipsTargetLowering::emitSEL_D(MachineInstr *MI,
1470 MachineBasicBlock *BB) const {
1471 MachineFunction *MF = BB->getParent();
1472 const TargetRegisterInfo *TRI =
1473 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
1474 const TargetInstrInfo *TII =
1475 getTargetMachine().getSubtargetImpl()->getInstrInfo();
1476 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1477 DebugLoc DL = MI->getDebugLoc();
1478 MachineBasicBlock::iterator II(MI);
1480 unsigned Fc = MI->getOperand(1).getReg();
1481 const auto &FGR64RegClass = TRI->getRegClass(Mips::FGR64RegClassID);
1483 unsigned Fc2 = RegInfo.createVirtualRegister(FGR64RegClass);
1485 BuildMI(*BB, II, DL, TII->get(Mips::SUBREG_TO_REG), Fc2)
1488 .addImm(Mips::sub_lo);
1490 // We don't erase the original instruction, we just replace the condition
1491 // register with the 64-bit super-register.
1492 MI->getOperand(1).setReg(Fc2);
1497 //===----------------------------------------------------------------------===//
1498 // Misc Lower Operation implementation
1499 //===----------------------------------------------------------------------===//
1500 SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
1501 SDValue Chain = Op.getOperand(0);
1502 SDValue Table = Op.getOperand(1);
1503 SDValue Index = Op.getOperand(2);
1505 EVT PTy = getPointerTy();
1506 unsigned EntrySize =
1507 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1509 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1510 DAG.getConstant(EntrySize, PTy));
1511 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1513 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1514 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1515 MachinePointerInfo::getJumpTable(), MemVT, false, false,
1517 Chain = Addr.getValue(1);
1519 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) ||
1520 Subtarget.isABI_N64()) {
1521 // For PIC, the sequence is:
1522 // BRIND(load(Jumptable + index) + RelocBase)
1523 // RelocBase can be JumpTable, GOT or some sort of global base.
1524 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1525 getPICJumpTableRelocBase(Table, DAG));
1528 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1531 SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1532 // The first operand is the chain, the second is the condition, the third is
1533 // the block to branch to if the condition is true.
1534 SDValue Chain = Op.getOperand(0);
1535 SDValue Dest = Op.getOperand(2);
1538 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
1539 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
1541 // Return if flag is not set by a floating point comparison.
1542 if (CondRes.getOpcode() != MipsISD::FPCmp)
1545 SDValue CCNode = CondRes.getOperand(2);
1547 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
1548 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1549 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
1550 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
1551 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
1552 FCC0, Dest, CondRes);
1555 SDValue MipsTargetLowering::
1556 lowerSELECT(SDValue Op, SelectionDAG &DAG) const
1558 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
1559 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
1561 // Return if flag is not set by a floating point comparison.
1562 if (Cond.getOpcode() != MipsISD::FPCmp)
1565 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1569 SDValue MipsTargetLowering::
1570 lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
1573 EVT Ty = Op.getOperand(0).getValueType();
1574 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1575 getSetCCResultType(*DAG.getContext(), Ty),
1576 Op.getOperand(0), Op.getOperand(1),
1579 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1583 SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1584 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
1585 SDValue Cond = createFPCmp(DAG, Op);
1587 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1588 "Floating point operand expected.");
1590 SDValue True = DAG.getConstant(1, MVT::i32);
1591 SDValue False = DAG.getConstant(0, MVT::i32);
1593 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
1596 SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
1597 SelectionDAG &DAG) const {
1598 EVT Ty = Op.getValueType();
1599 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1600 const GlobalValue *GV = N->getGlobal();
1602 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
1603 !Subtarget.isABI_N64()) {
1604 const MipsTargetObjectFile &TLOF =
1605 (const MipsTargetObjectFile&)getObjFileLowering();
1607 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine()))
1608 // %gp_rel relocation
1609 return getAddrGPRel(N, Ty, DAG);
1611 // %hi/%lo relocation
1612 return getAddrNonPIC(N, Ty, DAG);
1615 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
1616 return getAddrLocal(N, Ty, DAG,
1617 Subtarget.isABI_N32() || Subtarget.isABI_N64());
1620 return getAddrGlobalLargeGOT(N, Ty, DAG, MipsII::MO_GOT_HI16,
1621 MipsII::MO_GOT_LO16, DAG.getEntryNode(),
1622 MachinePointerInfo::getGOT());
1624 return getAddrGlobal(N, Ty, DAG,
1625 (Subtarget.isABI_N32() || Subtarget.isABI_N64())
1626 ? MipsII::MO_GOT_DISP
1628 DAG.getEntryNode(), MachinePointerInfo::getGOT());
1631 SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
1632 SelectionDAG &DAG) const {
1633 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1634 EVT Ty = Op.getValueType();
1636 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
1637 !Subtarget.isABI_N64())
1638 return getAddrNonPIC(N, Ty, DAG);
1640 return getAddrLocal(N, Ty, DAG,
1641 Subtarget.isABI_N32() || Subtarget.isABI_N64());
1644 SDValue MipsTargetLowering::
1645 lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
1647 // If the relocation model is PIC, use the General Dynamic TLS Model or
1648 // Local Dynamic TLS model, otherwise use the Initial Exec or
1649 // Local Exec TLS Model.
1651 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1653 const GlobalValue *GV = GA->getGlobal();
1654 EVT PtrVT = getPointerTy();
1656 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1658 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
1659 // General Dynamic and Local Dynamic TLS Model.
1660 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1663 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1664 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1665 getGlobalReg(DAG, PtrVT), TGA);
1666 unsigned PtrSize = PtrVT.getSizeInBits();
1667 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1669 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
1673 Entry.Node = Argument;
1675 Args.push_back(Entry);
1677 TargetLowering::CallLoweringInfo CLI(DAG);
1678 CLI.setDebugLoc(DL).setChain(DAG.getEntryNode())
1679 .setCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args), 0);
1680 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
1682 SDValue Ret = CallResult.first;
1684 if (model != TLSModel::LocalDynamic)
1687 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1688 MipsII::MO_DTPREL_HI);
1689 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1690 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1691 MipsII::MO_DTPREL_LO);
1692 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1693 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1694 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
1698 if (model == TLSModel::InitialExec) {
1699 // Initial Exec TLS Model
1700 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1701 MipsII::MO_GOTTPREL);
1702 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
1704 Offset = DAG.getLoad(PtrVT, DL,
1705 DAG.getEntryNode(), TGA, MachinePointerInfo(),
1706 false, false, false, 0);
1708 // Local Exec TLS Model
1709 assert(model == TLSModel::LocalExec);
1710 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1711 MipsII::MO_TPREL_HI);
1712 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1713 MipsII::MO_TPREL_LO);
1714 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1715 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1716 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1719 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1720 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
1723 SDValue MipsTargetLowering::
1724 lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
1726 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1727 EVT Ty = Op.getValueType();
1729 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
1730 !Subtarget.isABI_N64())
1731 return getAddrNonPIC(N, Ty, DAG);
1733 return getAddrLocal(N, Ty, DAG,
1734 Subtarget.isABI_N32() || Subtarget.isABI_N64());
1737 SDValue MipsTargetLowering::
1738 lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
1740 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1741 EVT Ty = Op.getValueType();
1743 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
1744 !Subtarget.isABI_N64()) {
1745 const MipsTargetObjectFile &TLOF =
1746 (const MipsTargetObjectFile&)getObjFileLowering();
1748 if (TLOF.IsConstantInSmallSection(N->getConstVal(), getTargetMachine()))
1749 // %gp_rel relocation
1750 return getAddrGPRel(N, Ty, DAG);
1752 return getAddrNonPIC(N, Ty, DAG);
1755 return getAddrLocal(N, Ty, DAG,
1756 Subtarget.isABI_N32() || Subtarget.isABI_N64());
1759 SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1760 MachineFunction &MF = DAG.getMachineFunction();
1761 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1764 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1767 // vastart just stores the address of the VarArgsFrameIndex slot into the
1768 // memory location argument.
1769 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1770 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
1771 MachinePointerInfo(SV), false, false, 0);
1774 SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
1775 SDNode *Node = Op.getNode();
1776 EVT VT = Node->getValueType(0);
1777 SDValue Chain = Node->getOperand(0);
1778 SDValue VAListPtr = Node->getOperand(1);
1779 unsigned Align = Node->getConstantOperandVal(3);
1780 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1782 unsigned ArgSlotSizeInBytes =
1783 (Subtarget.isABI_N32() || Subtarget.isABI_N64()) ? 8 : 4;
1785 SDValue VAListLoad = DAG.getLoad(getPointerTy(), DL, Chain, VAListPtr,
1786 MachinePointerInfo(SV), false, false, false,
1788 SDValue VAList = VAListLoad;
1790 // Re-align the pointer if necessary.
1791 // It should only ever be necessary for 64-bit types on O32 since the minimum
1792 // argument alignment is the same as the maximum type alignment for N32/N64.
1794 // FIXME: We currently align too often. The code generator doesn't notice
1795 // when the pointer is still aligned from the last va_arg (or pair of
1796 // va_args for the i64 on O32 case).
1797 if (Align > getMinStackArgumentAlignment()) {
1798 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
1800 VAList = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1801 DAG.getConstant(Align - 1,
1802 VAList.getValueType()));
1804 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
1805 DAG.getConstant(-(int64_t)Align,
1806 VAList.getValueType()));
1809 // Increment the pointer, VAList, to the next vaarg.
1810 unsigned ArgSizeInBytes = getDataLayout()->getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext()));
1811 SDValue Tmp3 = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1812 DAG.getConstant(RoundUpToAlignment(ArgSizeInBytes, ArgSlotSizeInBytes),
1813 VAList.getValueType()));
1814 // Store the incremented VAList to the legalized pointer
1815 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
1816 MachinePointerInfo(SV), false, false, 0);
1818 // In big-endian mode we must adjust the pointer when the load size is smaller
1819 // than the argument slot size. We must also reduce the known alignment to
1820 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
1821 // the correct half of the slot, and reduce the alignment from 8 (slot
1822 // alignment) down to 4 (type alignment).
1823 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
1824 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
1825 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
1826 DAG.getIntPtrConstant(Adjustment));
1828 // Load the actual argument out of the pointer VAList
1829 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo(), false, false,
1833 static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1834 bool HasExtractInsert) {
1835 EVT TyX = Op.getOperand(0).getValueType();
1836 EVT TyY = Op.getOperand(1).getValueType();
1837 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1838 SDValue Const31 = DAG.getConstant(31, MVT::i32);
1842 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1844 SDValue X = (TyX == MVT::f32) ?
1845 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1846 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1848 SDValue Y = (TyY == MVT::f32) ?
1849 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1850 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1853 if (HasExtractInsert) {
1854 // ext E, Y, 31, 1 ; extract bit31 of Y
1855 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1856 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1857 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1860 // srl SrlX, SllX, 1
1862 // sll SllY, SrlX, 31
1863 // or Or, SrlX, SllY
1864 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1865 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1866 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1867 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1868 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1871 if (TyX == MVT::f32)
1872 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1874 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1875 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1876 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1879 static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1880 bool HasExtractInsert) {
1881 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1882 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1883 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1884 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1887 // Bitcast to integer nodes.
1888 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1889 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
1891 if (HasExtractInsert) {
1892 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1893 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1894 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1895 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
1897 if (WidthX > WidthY)
1898 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1899 else if (WidthY > WidthX)
1900 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
1902 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1903 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1904 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1907 // (d)sll SllX, X, 1
1908 // (d)srl SrlX, SllX, 1
1909 // (d)srl SrlY, Y, width(Y)-1
1910 // (d)sll SllY, SrlX, width(Y)-1
1911 // or Or, SrlX, SllY
1912 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1913 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1914 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1915 DAG.getConstant(WidthY - 1, MVT::i32));
1917 if (WidthX > WidthY)
1918 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1919 else if (WidthY > WidthX)
1920 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1922 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1923 DAG.getConstant(WidthX - 1, MVT::i32));
1924 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1925 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
1929 MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
1930 if (Subtarget.isGP64bit())
1931 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
1933 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
1936 SDValue MipsTargetLowering::
1937 lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
1939 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1940 "Frame address can only be determined for current frame.");
1942 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1943 MFI->setFrameAddressIsTaken(true);
1944 EVT VT = Op.getValueType();
1947 DAG.getCopyFromReg(DAG.getEntryNode(), DL,
1948 Subtarget.isABI_N64() ? Mips::FP_64 : Mips::FP, VT);
1952 SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
1953 SelectionDAG &DAG) const {
1954 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
1958 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1959 "Return address can be determined only for current frame.");
1961 MachineFunction &MF = DAG.getMachineFunction();
1962 MachineFrameInfo *MFI = MF.getFrameInfo();
1963 MVT VT = Op.getSimpleValueType();
1964 unsigned RA = Subtarget.isABI_N64() ? Mips::RA_64 : Mips::RA;
1965 MFI->setReturnAddressIsTaken(true);
1967 // Return RA, which contains the return address. Mark it an implicit live-in.
1968 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
1969 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
1972 // An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1973 // generated from __builtin_eh_return (offset, handler)
1974 // The effect of this is to adjust the stack pointer by "offset"
1975 // and then branch to "handler".
1976 SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
1978 MachineFunction &MF = DAG.getMachineFunction();
1979 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1981 MipsFI->setCallsEhReturn();
1982 SDValue Chain = Op.getOperand(0);
1983 SDValue Offset = Op.getOperand(1);
1984 SDValue Handler = Op.getOperand(2);
1986 EVT Ty = Subtarget.isABI_N64() ? MVT::i64 : MVT::i32;
1988 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1989 // EH_RETURN nodes, so that instructions are emitted back-to-back.
1990 unsigned OffsetReg = Subtarget.isABI_N64() ? Mips::V1_64 : Mips::V1;
1991 unsigned AddrReg = Subtarget.isABI_N64() ? Mips::V0_64 : Mips::V0;
1992 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1993 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1994 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1995 DAG.getRegister(OffsetReg, Ty),
1996 DAG.getRegister(AddrReg, getPointerTy()),
2000 SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
2001 SelectionDAG &DAG) const {
2002 // FIXME: Need pseudo-fence for 'singlethread' fences
2003 // FIXME: Set SType for weaker fences where supported/appropriate.
2006 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
2007 DAG.getConstant(SType, MVT::i32));
2010 SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
2011 SelectionDAG &DAG) const {
2013 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2014 SDValue Shamt = Op.getOperand(2);
2017 // lo = (shl lo, shamt)
2018 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2021 // hi = (shl lo, shamt[4:0])
2022 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2023 DAG.getConstant(-1, MVT::i32));
2024 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
2025 DAG.getConstant(1, MVT::i32));
2026 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
2028 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
2029 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2030 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
2031 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2032 DAG.getConstant(0x20, MVT::i32));
2033 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2034 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
2035 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
2037 SDValue Ops[2] = {Lo, Hi};
2038 return DAG.getMergeValues(Ops, DL);
2041 SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
2044 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2045 SDValue Shamt = Op.getOperand(2);
2048 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2050 // hi = (sra hi, shamt)
2052 // hi = (srl hi, shamt)
2055 // lo = (sra hi, shamt[4:0])
2056 // hi = (sra hi, 31)
2058 // lo = (srl hi, shamt[4:0])
2060 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2061 DAG.getConstant(-1, MVT::i32));
2062 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
2063 DAG.getConstant(1, MVT::i32));
2064 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
2065 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
2066 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2067 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
2069 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2070 DAG.getConstant(0x20, MVT::i32));
2071 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
2072 DAG.getConstant(31, MVT::i32));
2073 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
2074 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2075 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
2078 SDValue Ops[2] = {Lo, Hi};
2079 return DAG.getMergeValues(Ops, DL);
2082 static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
2083 SDValue Chain, SDValue Src, unsigned Offset) {
2084 SDValue Ptr = LD->getBasePtr();
2085 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
2086 EVT BasePtrVT = Ptr.getValueType();
2088 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2091 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
2092 DAG.getConstant(Offset, BasePtrVT));
2094 SDValue Ops[] = { Chain, Ptr, Src };
2095 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
2096 LD->getMemOperand());
2099 // Expand an unaligned 32 or 64-bit integer load node.
2100 SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
2101 LoadSDNode *LD = cast<LoadSDNode>(Op);
2102 EVT MemVT = LD->getMemoryVT();
2104 if (Subtarget.systemSupportsUnalignedAccess())
2107 // Return if load is aligned or if MemVT is neither i32 nor i64.
2108 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2109 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2112 bool IsLittle = Subtarget.isLittle();
2113 EVT VT = Op.getValueType();
2114 ISD::LoadExtType ExtType = LD->getExtensionType();
2115 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2117 assert((VT == MVT::i32) || (VT == MVT::i64));
2120 // (set dst, (i64 (load baseptr)))
2122 // (set tmp, (ldl (add baseptr, 7), undef))
2123 // (set dst, (ldr baseptr, tmp))
2124 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
2125 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
2127 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
2131 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
2133 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
2137 // (set dst, (i32 (load baseptr))) or
2138 // (set dst, (i64 (sextload baseptr))) or
2139 // (set dst, (i64 (extload baseptr)))
2141 // (set tmp, (lwl (add baseptr, 3), undef))
2142 // (set dst, (lwr baseptr, tmp))
2143 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2144 (ExtType == ISD::EXTLOAD))
2147 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2150 // (set dst, (i64 (zextload baseptr)))
2152 // (set tmp0, (lwl (add baseptr, 3), undef))
2153 // (set tmp1, (lwr baseptr, tmp0))
2154 // (set tmp2, (shl tmp1, 32))
2155 // (set dst, (srl tmp2, 32))
2157 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2158 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
2159 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2160 SDValue Ops[] = { SRL, LWR.getValue(1) };
2161 return DAG.getMergeValues(Ops, DL);
2164 static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
2165 SDValue Chain, unsigned Offset) {
2166 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2167 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
2169 SDVTList VTList = DAG.getVTList(MVT::Other);
2172 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
2173 DAG.getConstant(Offset, BasePtrVT));
2175 SDValue Ops[] = { Chain, Value, Ptr };
2176 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
2177 SD->getMemOperand());
2180 // Expand an unaligned 32 or 64-bit integer store node.
2181 static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2183 SDValue Value = SD->getValue(), Chain = SD->getChain();
2184 EVT VT = Value.getValueType();
2187 // (store val, baseptr) or
2188 // (truncstore val, baseptr)
2190 // (swl val, (add baseptr, 3))
2191 // (swr val, baseptr)
2192 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
2193 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
2195 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
2198 assert(VT == MVT::i64);
2201 // (store val, baseptr)
2203 // (sdl val, (add baseptr, 7))
2204 // (sdr val, baseptr)
2205 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2206 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
2209 // Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2210 static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2211 SDValue Val = SD->getValue();
2213 if (Val.getOpcode() != ISD::FP_TO_SINT)
2216 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
2217 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
2220 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
2221 SD->getPointerInfo(), SD->isVolatile(),
2222 SD->isNonTemporal(), SD->getAlignment());
2225 SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2226 StoreSDNode *SD = cast<StoreSDNode>(Op);
2227 EVT MemVT = SD->getMemoryVT();
2229 // Lower unaligned integer stores.
2230 if (!Subtarget.systemSupportsUnalignedAccess() &&
2231 (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2232 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2233 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
2235 return lowerFP_TO_SINT_STORE(SD, DAG);
2238 SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
2239 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2240 || cast<ConstantSDNode>
2241 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2242 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2246 // (add (frameaddr 0), (frame_to_args_offset))
2247 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2248 // (add FrameObject, 0)
2249 // where FrameObject is a fixed StackObject with offset 0 which points to
2250 // the old stack pointer.
2251 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2252 EVT ValTy = Op->getValueType(0);
2253 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2254 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
2255 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
2256 DAG.getConstant(0, ValTy));
2259 SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2260 SelectionDAG &DAG) const {
2261 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
2262 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
2264 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
2267 //===----------------------------------------------------------------------===//
2268 // Calling Convention Implementation
2269 //===----------------------------------------------------------------------===//
2271 //===----------------------------------------------------------------------===//
2272 // TODO: Implement a generic logic using tblgen that can support this.
2273 // Mips O32 ABI rules:
2275 // i32 - Passed in A0, A1, A2, A3 and stack
2276 // f32 - Only passed in f32 registers if no int reg has been used yet to hold
2277 // an argument. Otherwise, passed in A1, A2, A3 and stack.
2278 // f64 - Only passed in two aliased f32 registers if no int reg has been used
2279 // yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
2280 // not used, it must be shadowed. If only A3 is available, shadow it and
2283 // For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
2284 //===----------------------------------------------------------------------===//
2286 static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2287 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2288 CCState &State, const MCPhysReg *F64Regs) {
2289 const MipsSubtarget &Subtarget =
2290 State.getMachineFunction().getTarget()
2291 .getSubtarget<const MipsSubtarget>();
2293 static const unsigned IntRegsSize = 4, FloatRegsSize = 2;
2295 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2296 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
2298 // Do not process byval args here.
2299 if (ArgFlags.isByVal())
2302 // Promote i8 and i16
2303 if (ArgFlags.isInReg() && !Subtarget.isLittle()) {
2304 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) {
2306 if (ArgFlags.isSExt())
2307 LocInfo = CCValAssign::SExtUpper;
2308 else if (ArgFlags.isZExt())
2309 LocInfo = CCValAssign::ZExtUpper;
2311 LocInfo = CCValAssign::AExtUpper;
2315 // Promote i8 and i16
2316 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2318 if (ArgFlags.isSExt())
2319 LocInfo = CCValAssign::SExt;
2320 else if (ArgFlags.isZExt())
2321 LocInfo = CCValAssign::ZExt;
2323 LocInfo = CCValAssign::AExt;
2328 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2329 // is true: function is vararg, argument is 3rd or higher, there is previous
2330 // argument which is not f32 or f64.
2331 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2332 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
2333 unsigned OrigAlign = ArgFlags.getOrigAlign();
2334 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
2336 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
2337 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2338 // If this is the first part of an i64 arg,
2339 // the allocated register must be either A0 or A2.
2340 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2341 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2343 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2344 // Allocate int register and shadow next int register. If first
2345 // available register is Mips::A1 or Mips::A3, shadow it too.
2346 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2347 if (Reg == Mips::A1 || Reg == Mips::A3)
2348 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2349 State.AllocateReg(IntRegs, IntRegsSize);
2351 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2352 // we are guaranteed to find an available float register
2353 if (ValVT == MVT::f32) {
2354 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2355 // Shadow int register
2356 State.AllocateReg(IntRegs, IntRegsSize);
2358 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2359 // Shadow int registers
2360 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2361 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2362 State.AllocateReg(IntRegs, IntRegsSize);
2363 State.AllocateReg(IntRegs, IntRegsSize);
2366 llvm_unreachable("Cannot handle this ValVT.");
2369 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2371 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
2373 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
2378 static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2379 MVT LocVT, CCValAssign::LocInfo LocInfo,
2380 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2381 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
2383 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2386 static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2387 MVT LocVT, CCValAssign::LocInfo LocInfo,
2388 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2389 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
2391 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2394 static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2395 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2396 CCState &State) LLVM_ATTRIBUTE_UNUSED;
2398 #include "MipsGenCallingConv.inc"
2400 //===----------------------------------------------------------------------===//
2401 // Call Calling Convention Implementation
2402 //===----------------------------------------------------------------------===//
2404 // Return next O32 integer argument register.
2405 static unsigned getNextIntArgReg(unsigned Reg) {
2406 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2407 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2411 MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
2412 SDValue Chain, SDValue Arg, SDLoc DL,
2413 bool IsTailCall, SelectionDAG &DAG) const {
2415 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2416 DAG.getIntPtrConstant(Offset));
2417 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2421 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2422 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2423 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2424 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2425 /*isVolatile=*/ true, false, 0);
2428 void MipsTargetLowering::
2429 getOpndList(SmallVectorImpl<SDValue> &Ops,
2430 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2431 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2432 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
2433 SDValue Chain) const {
2434 // Insert node "GP copy globalreg" before call to function.
2436 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2437 // in PIC mode) allow symbols to be resolved via lazy binding.
2438 // The lazy binding stub requires GP to point to the GOT.
2439 // Note that we don't need GP to point to the GOT for indirect calls
2440 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
2441 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
2442 // used for the function (that is, Mips linker doesn't generate lazy binding
2443 // stub for a function whose address is taken in the program).
2444 if (IsPICCall && !InternalLinkage && IsCallReloc) {
2445 unsigned GPReg = Subtarget.isABI_N64() ? Mips::GP_64 : Mips::GP;
2446 EVT Ty = Subtarget.isABI_N64() ? MVT::i64 : MVT::i32;
2447 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2450 // Build a sequence of copy-to-reg nodes chained together with token
2451 // chain and flag operands which copy the outgoing args into registers.
2452 // The InFlag in necessary since all emitted instructions must be
2456 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2457 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2458 RegsToPass[i].second, InFlag);
2459 InFlag = Chain.getValue(1);
2462 // Add argument registers to the end of the list so that they are
2463 // known live into the call.
2464 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2465 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2466 RegsToPass[i].second.getValueType()));
2468 // Add a register mask operand representing the call-preserved registers.
2469 const TargetRegisterInfo *TRI =
2470 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
2471 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2472 assert(Mask && "Missing call preserved mask for calling convention");
2473 if (Subtarget.inMips16HardFloat()) {
2474 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2475 llvm::StringRef Sym = G->getGlobal()->getName();
2476 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
2477 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
2478 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2482 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2484 if (InFlag.getNode())
2485 Ops.push_back(InFlag);
2488 /// LowerCall - functions arguments are copied from virtual regs to
2489 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
2491 MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
2492 SmallVectorImpl<SDValue> &InVals) const {
2493 SelectionDAG &DAG = CLI.DAG;
2495 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2496 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2497 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
2498 SDValue Chain = CLI.Chain;
2499 SDValue Callee = CLI.Callee;
2500 bool &IsTailCall = CLI.IsTailCall;
2501 CallingConv::ID CallConv = CLI.CallConv;
2502 bool IsVarArg = CLI.IsVarArg;
2504 MachineFunction &MF = DAG.getMachineFunction();
2505 MachineFrameInfo *MFI = MF.getFrameInfo();
2506 const TargetFrameLowering *TFL = MF.getSubtarget().getFrameLowering();
2507 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
2508 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
2510 // Analyze operands of the call, assigning locations to each operand.
2511 SmallVector<CCValAssign, 16> ArgLocs;
2513 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(),
2514 MipsCCState::getSpecialCallingConvForCallee(Callee.getNode(), Subtarget));
2516 // Allocate the reserved argument area. It seems strange to do this from the
2517 // caller side but removing it breaks the frame size calculation.
2518 const MipsABIInfo &ABI = Subtarget.getABI();
2519 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
2521 CCInfo.AnalyzeCallOperands(Outs, CC_Mips, CLI.getArgs(), Callee.getNode());
2523 // Get a count of how many bytes are to be pushed on the stack.
2524 unsigned NextStackOffset = CCInfo.getNextStackOffset();
2526 // Check if it's really possible to do a tail call.
2528 IsTailCall = isEligibleForTailCallOptimization(
2529 CCInfo, NextStackOffset, *MF.getInfo<MipsFunctionInfo>());
2531 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2532 report_fatal_error("failed to perform tail call elimination on a call "
2533 "site marked musttail");
2538 // Chain is the output chain of the last Load/Store or CopyToReg node.
2539 // ByValChain is the output chain of the last Memcpy node created for copying
2540 // byval arguments to the stack.
2541 unsigned StackAlignment = TFL->getStackAlignment();
2542 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
2543 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
2546 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
2548 SDValue StackPtr = DAG.getCopyFromReg(
2549 Chain, DL, Subtarget.isABI_N64() ? Mips::SP_64 : Mips::SP,
2552 // With EABI is it possible to have 16 args on registers.
2553 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
2554 SmallVector<SDValue, 8> MemOpChains;
2556 CCInfo.rewindByValRegsInfo();
2558 // Walk the register/memloc assignments, inserting copies/loads.
2559 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2560 SDValue Arg = OutVals[i];
2561 CCValAssign &VA = ArgLocs[i];
2562 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
2563 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2564 bool UseUpperBits = false;
2567 if (Flags.isByVal()) {
2568 unsigned FirstByValReg, LastByValReg;
2569 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2570 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2572 assert(Flags.getByValSize() &&
2573 "ByVal args of size 0 should have been ignored by front-end.");
2574 assert(ByValIdx < CCInfo.getInRegsParamsCount());
2575 assert(!IsTailCall &&
2576 "Do not tail-call optimize if there is a byval argument.");
2577 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
2578 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(),
2580 CCInfo.nextInRegsParam();
2584 // Promote the value if needed.
2585 switch (VA.getLocInfo()) {
2587 llvm_unreachable("Unknown loc info!");
2588 case CCValAssign::Full:
2589 if (VA.isRegLoc()) {
2590 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
2591 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2592 (ValVT == MVT::i64 && LocVT == MVT::f64))
2593 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
2594 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
2595 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2596 Arg, DAG.getConstant(0, MVT::i32));
2597 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2598 Arg, DAG.getConstant(1, MVT::i32));
2599 if (!Subtarget.isLittle())
2601 unsigned LocRegLo = VA.getLocReg();
2602 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2603 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2604 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
2609 case CCValAssign::BCvt:
2610 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
2612 case CCValAssign::SExtUpper:
2613 UseUpperBits = true;
2615 case CCValAssign::SExt:
2616 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
2618 case CCValAssign::ZExtUpper:
2619 UseUpperBits = true;
2621 case CCValAssign::ZExt:
2622 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
2624 case CCValAssign::AExtUpper:
2625 UseUpperBits = true;
2627 case CCValAssign::AExt:
2628 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
2633 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
2634 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2636 ISD::SHL, DL, VA.getLocVT(), Arg,
2637 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2640 // Arguments that can be passed on register must be kept at
2641 // RegsToPass vector
2642 if (VA.isRegLoc()) {
2643 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2647 // Register can't get to this point...
2648 assert(VA.isMemLoc());
2650 // emit ISD::STORE whichs stores the
2651 // parameter value to a stack Location
2652 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
2653 Chain, Arg, DL, IsTailCall, DAG));
2656 // Transform all store nodes into one single node because all store
2657 // nodes are independent of each other.
2658 if (!MemOpChains.empty())
2659 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
2661 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
2662 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2663 // node so that legalize doesn't hack it.
2665 (Subtarget.isABI_N64() || IsPIC); // true if calls are translated to
2667 bool GlobalOrExternal = false, InternalLinkage = false, IsCallReloc = false;
2669 EVT Ty = Callee.getValueType();
2671 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2673 const GlobalValue *Val = G->getGlobal();
2674 InternalLinkage = Val->hasInternalLinkage();
2676 if (InternalLinkage)
2677 Callee = getAddrLocal(G, Ty, DAG,
2678 Subtarget.isABI_N32() || Subtarget.isABI_N64());
2679 else if (LargeGOT) {
2680 Callee = getAddrGlobalLargeGOT(G, Ty, DAG, MipsII::MO_CALL_HI16,
2681 MipsII::MO_CALL_LO16, Chain,
2682 FuncInfo->callPtrInfo(Val));
2685 Callee = getAddrGlobal(G, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2686 FuncInfo->callPtrInfo(Val));
2690 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
2691 MipsII::MO_NO_FLAG);
2692 GlobalOrExternal = true;
2694 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
2695 const char *Sym = S->getSymbol();
2697 if (!Subtarget.isABI_N64() && !IsPIC) // !N64 && static
2698 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(),
2699 MipsII::MO_NO_FLAG);
2700 else if (LargeGOT) {
2701 Callee = getAddrGlobalLargeGOT(S, Ty, DAG, MipsII::MO_CALL_HI16,
2702 MipsII::MO_CALL_LO16, Chain,
2703 FuncInfo->callPtrInfo(Sym));
2705 } else { // N64 || PIC
2706 Callee = getAddrGlobal(S, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2707 FuncInfo->callPtrInfo(Sym));
2711 GlobalOrExternal = true;
2714 SmallVector<SDValue, 8> Ops(1, Chain);
2715 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2717 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2718 IsCallReloc, CLI, Callee, Chain);
2721 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
2723 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
2724 SDValue InFlag = Chain.getValue(1);
2726 // Create the CALLSEQ_END node.
2727 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
2728 DAG.getIntPtrConstant(0, true), InFlag, DL);
2729 InFlag = Chain.getValue(1);
2731 // Handle result values, copying them out of physregs into vregs that we
2733 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
2737 /// LowerCallResult - Lower the result values of a call into the
2738 /// appropriate copies out of appropriate physical registers.
2739 SDValue MipsTargetLowering::LowerCallResult(
2740 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
2741 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
2742 SmallVectorImpl<SDValue> &InVals,
2743 TargetLowering::CallLoweringInfo &CLI) const {
2744 // Assign locations to each value returned by this call.
2745 SmallVector<CCValAssign, 16> RVLocs;
2746 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2748 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI);
2750 // Copy all of the result registers out of their specified physreg.
2751 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2752 CCValAssign &VA = RVLocs[i];
2753 assert(VA.isRegLoc() && "Can only return in registers!");
2755 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
2756 RVLocs[i].getLocVT(), InFlag);
2757 Chain = Val.getValue(1);
2758 InFlag = Val.getValue(2);
2760 if (VA.isUpperBitsInLoc()) {
2761 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
2762 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2764 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2766 Shift, DL, VA.getLocVT(), Val,
2767 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2770 switch (VA.getLocInfo()) {
2772 llvm_unreachable("Unknown loc info!");
2773 case CCValAssign::Full:
2775 case CCValAssign::BCvt:
2776 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
2778 case CCValAssign::AExt:
2779 case CCValAssign::AExtUpper:
2780 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2782 case CCValAssign::ZExt:
2783 case CCValAssign::ZExtUpper:
2784 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
2785 DAG.getValueType(VA.getValVT()));
2786 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2788 case CCValAssign::SExt:
2789 case CCValAssign::SExtUpper:
2790 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
2791 DAG.getValueType(VA.getValVT()));
2792 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2796 InVals.push_back(Val);
2802 static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA,
2803 EVT ArgVT, SDLoc DL, SelectionDAG &DAG) {
2804 MVT LocVT = VA.getLocVT();
2805 EVT ValVT = VA.getValVT();
2807 // Shift into the upper bits if necessary.
2808 switch (VA.getLocInfo()) {
2811 case CCValAssign::AExtUpper:
2812 case CCValAssign::SExtUpper:
2813 case CCValAssign::ZExtUpper: {
2814 unsigned ValSizeInBits = ArgVT.getSizeInBits();
2815 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2817 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2819 Opcode, DL, VA.getLocVT(), Val,
2820 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2825 // If this is an value smaller than the argument slot size (32-bit for O32,
2826 // 64-bit for N32/N64), it has been promoted in some way to the argument slot
2827 // size. Extract the value and insert any appropriate assertions regarding
2828 // sign/zero extension.
2829 switch (VA.getLocInfo()) {
2831 llvm_unreachable("Unknown loc info!");
2832 case CCValAssign::Full:
2834 case CCValAssign::AExtUpper:
2835 case CCValAssign::AExt:
2836 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2838 case CCValAssign::SExtUpper:
2839 case CCValAssign::SExt:
2840 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT));
2841 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2843 case CCValAssign::ZExtUpper:
2844 case CCValAssign::ZExt:
2845 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT));
2846 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2848 case CCValAssign::BCvt:
2849 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
2856 //===----------------------------------------------------------------------===//
2857 // Formal Arguments Calling Convention Implementation
2858 //===----------------------------------------------------------------------===//
2859 /// LowerFormalArguments - transform physical registers into virtual registers
2860 /// and generate load operations for arguments places on the stack.
2862 MipsTargetLowering::LowerFormalArguments(SDValue Chain,
2863 CallingConv::ID CallConv,
2865 const SmallVectorImpl<ISD::InputArg> &Ins,
2866 SDLoc DL, SelectionDAG &DAG,
2867 SmallVectorImpl<SDValue> &InVals)
2869 MachineFunction &MF = DAG.getMachineFunction();
2870 MachineFrameInfo *MFI = MF.getFrameInfo();
2871 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2873 MipsFI->setVarArgsFrameIndex(0);
2875 // Used with vargs to acumulate store chains.
2876 std::vector<SDValue> OutChains;
2878 // Assign locations to all of the incoming arguments.
2879 SmallVector<CCValAssign, 16> ArgLocs;
2880 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
2882 const MipsABIInfo &ABI = Subtarget.getABI();
2883 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
2884 Function::const_arg_iterator FuncArg =
2885 DAG.getMachineFunction().getFunction()->arg_begin();
2887 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg);
2888 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2889 CCInfo.getInRegsParamsCount() > 0);
2891 unsigned CurArgIdx = 0;
2892 CCInfo.rewindByValRegsInfo();
2894 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2895 CCValAssign &VA = ArgLocs[i];
2896 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2897 CurArgIdx = Ins[i].OrigArgIndex;
2898 EVT ValVT = VA.getValVT();
2899 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2900 bool IsRegLoc = VA.isRegLoc();
2902 if (Flags.isByVal()) {
2903 unsigned FirstByValReg, LastByValReg;
2904 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2905 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2907 assert(Flags.getByValSize() &&
2908 "ByVal args of size 0 should have been ignored by front-end.");
2909 assert(ByValIdx < CCInfo.getInRegsParamsCount());
2910 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
2911 FirstByValReg, LastByValReg, VA, CCInfo);
2912 CCInfo.nextInRegsParam();
2916 // Arguments stored on registers
2918 MVT RegVT = VA.getLocVT();
2919 unsigned ArgReg = VA.getLocReg();
2920 const TargetRegisterClass *RC = getRegClassFor(RegVT);
2922 // Transform the arguments stored on
2923 // physical registers into virtual ones
2924 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2925 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
2927 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
2929 // Handle floating point arguments passed in integer registers and
2930 // long double arguments passed in floating point registers.
2931 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
2932 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2933 (RegVT == MVT::f64 && ValVT == MVT::i64))
2934 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
2935 else if (Subtarget.isABI_O32() && RegVT == MVT::i32 &&
2936 ValVT == MVT::f64) {
2937 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
2938 getNextIntArgReg(ArgReg), RC);
2939 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
2940 if (!Subtarget.isLittle())
2941 std::swap(ArgValue, ArgValue2);
2942 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
2943 ArgValue, ArgValue2);
2946 InVals.push_back(ArgValue);
2947 } else { // VA.isRegLoc()
2948 MVT LocVT = VA.getLocVT();
2950 if (Subtarget.isABI_O32()) {
2951 // We ought to be able to use LocVT directly but O32 sets it to i32
2952 // when allocating floating point values to integer registers.
2953 // This shouldn't influence how we load the value into registers unless
2954 // we are targetting softfloat.
2955 if (VA.getValVT().isFloatingPoint() && !Subtarget.abiUsesSoftFloat())
2956 LocVT = VA.getValVT();
2960 assert(VA.isMemLoc());
2962 // The stack pointer offset is relative to the caller stack frame.
2963 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
2964 VA.getLocMemOffset(), true);
2966 // Create load nodes to retrieve arguments from the stack
2967 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2968 SDValue ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
2969 MachinePointerInfo::getFixedStack(FI),
2970 false, false, false, 0);
2971 OutChains.push_back(ArgValue.getValue(1));
2973 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
2975 InVals.push_back(ArgValue);
2979 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2980 // The mips ABIs for returning structs by value requires that we copy
2981 // the sret argument into $v0 for the return. Save the argument into
2982 // a virtual register so that we can access it from the return points.
2983 if (Ins[i].Flags.isSRet()) {
2984 unsigned Reg = MipsFI->getSRetReturnReg();
2986 Reg = MF.getRegInfo().createVirtualRegister(
2987 getRegClassFor(Subtarget.isABI_N64() ? MVT::i64 : MVT::i32));
2988 MipsFI->setSRetReturnReg(Reg);
2990 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
2991 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
2997 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo);
2999 // All stores are grouped in one node to allow the matching between
3000 // the size of Ins and InVals. This only happens when on varg functions
3001 if (!OutChains.empty()) {
3002 OutChains.push_back(Chain);
3003 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
3009 //===----------------------------------------------------------------------===//
3010 // Return Value Calling Convention Implementation
3011 //===----------------------------------------------------------------------===//
3014 MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
3015 MachineFunction &MF, bool IsVarArg,
3016 const SmallVectorImpl<ISD::OutputArg> &Outs,
3017 LLVMContext &Context) const {
3018 SmallVector<CCValAssign, 16> RVLocs;
3019 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
3020 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3024 MipsTargetLowering::LowerReturn(SDValue Chain,
3025 CallingConv::ID CallConv, bool IsVarArg,
3026 const SmallVectorImpl<ISD::OutputArg> &Outs,
3027 const SmallVectorImpl<SDValue> &OutVals,
3028 SDLoc DL, SelectionDAG &DAG) const {
3029 // CCValAssign - represent the assignment of
3030 // the return value to a location
3031 SmallVector<CCValAssign, 16> RVLocs;
3032 MachineFunction &MF = DAG.getMachineFunction();
3034 // CCState - Info about the registers and stack slot.
3035 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
3037 // Analyze return values.
3038 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
3041 SmallVector<SDValue, 4> RetOps(1, Chain);
3043 // Copy the result values into the output registers.
3044 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3045 SDValue Val = OutVals[i];
3046 CCValAssign &VA = RVLocs[i];
3047 assert(VA.isRegLoc() && "Can only return in registers!");
3048 bool UseUpperBits = false;
3050 switch (VA.getLocInfo()) {
3052 llvm_unreachable("Unknown loc info!");
3053 case CCValAssign::Full:
3055 case CCValAssign::BCvt:
3056 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3058 case CCValAssign::AExtUpper:
3059 UseUpperBits = true;
3061 case CCValAssign::AExt:
3062 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3064 case CCValAssign::ZExtUpper:
3065 UseUpperBits = true;
3067 case CCValAssign::ZExt:
3068 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3070 case CCValAssign::SExtUpper:
3071 UseUpperBits = true;
3073 case CCValAssign::SExt:
3074 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3079 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3080 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3082 ISD::SHL, DL, VA.getLocVT(), Val,
3083 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
3086 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
3088 // Guarantee that all emitted copies are stuck together with flags.
3089 Flag = Chain.getValue(1);
3090 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
3093 // The mips ABIs for returning structs by value requires that we copy
3094 // the sret argument into $v0 for the return. We saved the argument into
3095 // a virtual register in the entry block, so now we copy the value out
3097 if (MF.getFunction()->hasStructRetAttr()) {
3098 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3099 unsigned Reg = MipsFI->getSRetReturnReg();
3102 llvm_unreachable("sret virtual register not created in the entry block");
3103 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
3104 unsigned V0 = Subtarget.isABI_N64() ? Mips::V0_64 : Mips::V0;
3106 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
3107 Flag = Chain.getValue(1);
3108 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
3111 RetOps[0] = Chain; // Update chain.
3113 // Add the flag if we have it.
3115 RetOps.push_back(Flag);
3117 // Return on Mips is always a "jr $ra"
3118 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
3121 //===----------------------------------------------------------------------===//
3122 // Mips Inline Assembly Support
3123 //===----------------------------------------------------------------------===//
3125 /// getConstraintType - Given a constraint letter, return the type of
3126 /// constraint it is for this target.
3127 MipsTargetLowering::ConstraintType MipsTargetLowering::
3128 getConstraintType(const std::string &Constraint) const
3130 // Mips specific constraints
3131 // GCC config/mips/constraints.md
3133 // 'd' : An address register. Equivalent to r
3134 // unless generating MIPS16 code.
3135 // 'y' : Equivalent to r; retained for
3136 // backwards compatibility.
3137 // 'c' : A register suitable for use in an indirect
3138 // jump. This will always be $25 for -mabicalls.
3139 // 'l' : The lo register. 1 word storage.
3140 // 'x' : The hilo register pair. Double word storage.
3141 if (Constraint.size() == 1) {
3142 switch (Constraint[0]) {
3150 return C_RegisterClass;
3155 return TargetLowering::getConstraintType(Constraint);
3158 /// Examine constraint type and operand type and determine a weight value.
3159 /// This object must already have been set up with the operand type
3160 /// and the current alternative constraint selected.
3161 TargetLowering::ConstraintWeight
3162 MipsTargetLowering::getSingleConstraintMatchWeight(
3163 AsmOperandInfo &info, const char *constraint) const {
3164 ConstraintWeight weight = CW_Invalid;
3165 Value *CallOperandVal = info.CallOperandVal;
3166 // If we don't have a value, we can't do a match,
3167 // but allow it at the lowest weight.
3168 if (!CallOperandVal)
3170 Type *type = CallOperandVal->getType();
3171 // Look at the constraint type.
3172 switch (*constraint) {
3174 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3178 if (type->isIntegerTy())
3179 weight = CW_Register;
3181 case 'f': // FPU or MSA register
3182 if (Subtarget.hasMSA() && type->isVectorTy() &&
3183 cast<VectorType>(type)->getBitWidth() == 128)
3184 weight = CW_Register;
3185 else if (type->isFloatTy())
3186 weight = CW_Register;
3188 case 'c': // $25 for indirect jumps
3189 case 'l': // lo register
3190 case 'x': // hilo register pair
3191 if (type->isIntegerTy())
3192 weight = CW_SpecificReg;
3194 case 'I': // signed 16 bit immediate
3195 case 'J': // integer zero
3196 case 'K': // unsigned 16 bit immediate
3197 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3198 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3199 case 'O': // signed 15 bit immediate (+- 16383)
3200 case 'P': // immediate in the range of 65535 to 1 (inclusive)
3201 if (isa<ConstantInt>(CallOperandVal))
3202 weight = CW_Constant;
3211 /// This is a helper function to parse a physical register string and split it
3212 /// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
3213 /// that is returned indicates whether parsing was successful. The second flag
3214 /// is true if the numeric part exists.
3215 static std::pair<bool, bool>
3216 parsePhysicalReg(StringRef C, std::string &Prefix,
3217 unsigned long long &Reg) {
3218 if (C.front() != '{' || C.back() != '}')
3219 return std::make_pair(false, false);
3221 // Search for the first numeric character.
3222 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
3223 I = std::find_if(B, E, std::ptr_fun(isdigit));
3225 Prefix.assign(B, I - B);
3227 // The second flag is set to false if no numeric characters were found.
3229 return std::make_pair(true, false);
3231 // Parse the numeric characters.
3232 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
3236 std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
3237 parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
3238 const TargetRegisterInfo *TRI =
3239 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
3240 const TargetRegisterClass *RC;
3242 unsigned long long Reg;
3244 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
3247 return std::make_pair(0U, nullptr);
3249 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
3250 // No numeric characters follow "hi" or "lo".
3252 return std::make_pair(0U, nullptr);
3254 RC = TRI->getRegClass(Prefix == "hi" ?
3255 Mips::HI32RegClassID : Mips::LO32RegClassID);
3256 return std::make_pair(*(RC->begin()), RC);
3257 } else if (Prefix.compare(0, 4, "$msa") == 0) {
3258 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3260 // No numeric characters follow the name.
3262 return std::make_pair(0U, nullptr);
3264 Reg = StringSwitch<unsigned long long>(Prefix)
3265 .Case("$msair", Mips::MSAIR)
3266 .Case("$msacsr", Mips::MSACSR)
3267 .Case("$msaaccess", Mips::MSAAccess)
3268 .Case("$msasave", Mips::MSASave)
3269 .Case("$msamodify", Mips::MSAModify)
3270 .Case("$msarequest", Mips::MSARequest)
3271 .Case("$msamap", Mips::MSAMap)
3272 .Case("$msaunmap", Mips::MSAUnmap)
3276 return std::make_pair(0U, nullptr);
3278 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3279 return std::make_pair(Reg, RC);
3283 return std::make_pair(0U, nullptr);
3285 if (Prefix == "$f") { // Parse $f0-$f31.
3286 // If the size of FP registers is 64-bit or Reg is an even number, select
3287 // the 64-bit register class. Otherwise, select the 32-bit register class.
3288 if (VT == MVT::Other)
3289 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
3291 RC = getRegClassFor(VT);
3293 if (RC == &Mips::AFGR64RegClass) {
3294 assert(Reg % 2 == 0);
3297 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
3298 RC = TRI->getRegClass(Mips::FCCRegClassID);
3299 else if (Prefix == "$w") { // Parse $w0-$w31.
3300 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
3301 } else { // Parse $0-$31.
3302 assert(Prefix == "$");
3303 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3306 assert(Reg < RC->getNumRegs());
3307 return std::make_pair(*(RC->begin() + Reg), RC);
3310 /// Given a register class constraint, like 'r', if this corresponds directly
3311 /// to an LLVM register class, return a register of 0 and the register class
3313 std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
3314 getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
3316 if (Constraint.size() == 1) {
3317 switch (Constraint[0]) {
3318 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3319 case 'y': // Same as 'r'. Exists for compatibility.
3321 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
3322 if (Subtarget.inMips16Mode())
3323 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
3324 return std::make_pair(0U, &Mips::GPR32RegClass);
3326 if (VT == MVT::i64 && !Subtarget.isGP64bit())
3327 return std::make_pair(0U, &Mips::GPR32RegClass);
3328 if (VT == MVT::i64 && Subtarget.isGP64bit())
3329 return std::make_pair(0U, &Mips::GPR64RegClass);
3330 // This will generate an error message
3331 return std::make_pair(0U, nullptr);
3332 case 'f': // FPU or MSA register
3333 if (VT == MVT::v16i8)
3334 return std::make_pair(0U, &Mips::MSA128BRegClass);
3335 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3336 return std::make_pair(0U, &Mips::MSA128HRegClass);
3337 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3338 return std::make_pair(0U, &Mips::MSA128WRegClass);
3339 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3340 return std::make_pair(0U, &Mips::MSA128DRegClass);
3341 else if (VT == MVT::f32)
3342 return std::make_pair(0U, &Mips::FGR32RegClass);
3343 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
3344 if (Subtarget.isFP64bit())
3345 return std::make_pair(0U, &Mips::FGR64RegClass);
3346 return std::make_pair(0U, &Mips::AFGR64RegClass);
3349 case 'c': // register suitable for indirect jump
3351 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
3352 assert(VT == MVT::i64 && "Unexpected type.");
3353 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
3354 case 'l': // register suitable for indirect jump
3356 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3357 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
3358 case 'x': // register suitable for indirect jump
3359 // Fixme: Not triggering the use of both hi and low
3360 // This will generate an error message
3361 return std::make_pair(0U, nullptr);
3365 std::pair<unsigned, const TargetRegisterClass *> R;
3366 R = parseRegForInlineAsmConstraint(Constraint, VT);
3371 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3374 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3375 /// vector. If it is invalid, don't add anything to Ops.
3376 void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3377 std::string &Constraint,
3378 std::vector<SDValue>&Ops,
3379 SelectionDAG &DAG) const {
3382 // Only support length 1 constraints for now.
3383 if (Constraint.length() > 1) return;
3385 char ConstraintLetter = Constraint[0];
3386 switch (ConstraintLetter) {
3387 default: break; // This will fall through to the generic implementation
3388 case 'I': // Signed 16 bit constant
3389 // If this fails, the parent routine will give an error
3390 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3391 EVT Type = Op.getValueType();
3392 int64_t Val = C->getSExtValue();
3393 if (isInt<16>(Val)) {
3394 Result = DAG.getTargetConstant(Val, Type);
3399 case 'J': // integer zero
3400 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3401 EVT Type = Op.getValueType();
3402 int64_t Val = C->getZExtValue();
3404 Result = DAG.getTargetConstant(0, Type);
3409 case 'K': // unsigned 16 bit immediate
3410 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3411 EVT Type = Op.getValueType();
3412 uint64_t Val = (uint64_t)C->getZExtValue();
3413 if (isUInt<16>(Val)) {
3414 Result = DAG.getTargetConstant(Val, Type);
3419 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3420 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3421 EVT Type = Op.getValueType();
3422 int64_t Val = C->getSExtValue();
3423 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3424 Result = DAG.getTargetConstant(Val, Type);
3429 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3430 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3431 EVT Type = Op.getValueType();
3432 int64_t Val = C->getSExtValue();
3433 if ((Val >= -65535) && (Val <= -1)) {
3434 Result = DAG.getTargetConstant(Val, Type);
3439 case 'O': // signed 15 bit immediate
3440 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3441 EVT Type = Op.getValueType();
3442 int64_t Val = C->getSExtValue();
3443 if ((isInt<15>(Val))) {
3444 Result = DAG.getTargetConstant(Val, Type);
3449 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3450 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3451 EVT Type = Op.getValueType();
3452 int64_t Val = C->getSExtValue();
3453 if ((Val <= 65535) && (Val >= 1)) {
3454 Result = DAG.getTargetConstant(Val, Type);
3461 if (Result.getNode()) {
3462 Ops.push_back(Result);
3466 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3469 bool MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM,
3471 // No global is ever allowed as a base.
3476 case 0: // "r+i" or just "i", depending on HasBaseReg.
3479 if (!AM.HasBaseReg) // allow "r+i".
3481 return false; // disallow "r+r" or "r+r+i".
3490 MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3491 // The Mips target isn't yet aware of offsets.
3495 EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
3497 bool IsMemset, bool ZeroMemset,
3499 MachineFunction &MF) const {
3500 if (Subtarget.hasMips64())
3506 bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3507 if (VT != MVT::f32 && VT != MVT::f64)
3509 if (Imm.isNegZero())
3511 return Imm.isZero();
3514 unsigned MipsTargetLowering::getJumpTableEncoding() const {
3515 if (Subtarget.isABI_N64())
3516 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
3518 return TargetLowering::getJumpTableEncoding();
3521 void MipsTargetLowering::copyByValRegs(
3522 SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains, SelectionDAG &DAG,
3523 const ISD::ArgFlagsTy &Flags, SmallVectorImpl<SDValue> &InVals,
3524 const Argument *FuncArg, unsigned FirstReg, unsigned LastReg,
3525 const CCValAssign &VA, MipsCCState &State) const {
3526 MachineFunction &MF = DAG.getMachineFunction();
3527 MachineFrameInfo *MFI = MF.getFrameInfo();
3528 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
3529 unsigned NumRegs = LastReg - FirstReg;
3530 unsigned RegAreaSize = NumRegs * GPRSizeInBytes;
3531 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3533 const MipsABIInfo &ABI = Subtarget.getABI();
3534 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs();
3538 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3539 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes);
3541 FrameObjOffset = VA.getLocMemOffset();
3543 // Create frame object.
3544 EVT PtrTy = getPointerTy();
3545 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3546 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3547 InVals.push_back(FIN);
3552 // Copy arg registers.
3553 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
3554 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3556 for (unsigned I = 0; I < NumRegs; ++I) {
3557 unsigned ArgReg = ByValArgRegs[FirstReg + I];
3558 unsigned VReg = addLiveIn(MF, ArgReg, RC);
3559 unsigned Offset = I * GPRSizeInBytes;
3560 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3561 DAG.getConstant(Offset, PtrTy));
3562 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3563 StorePtr, MachinePointerInfo(FuncArg, Offset),
3565 OutChains.push_back(Store);
3569 // Copy byVal arg to registers and stack.
3570 void MipsTargetLowering::passByValArg(
3571 SDValue Chain, SDLoc DL,
3572 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
3573 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
3574 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg,
3575 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle,
3576 const CCValAssign &VA) const {
3577 unsigned ByValSizeInBytes = Flags.getByValSize();
3578 unsigned OffsetInBytes = 0; // From beginning of struct
3579 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3580 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
3581 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
3582 unsigned NumRegs = LastReg - FirstReg;
3585 const ArrayRef<MCPhysReg> ArgRegs = Subtarget.getABI().GetByValArgRegs();
3586 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes);
3589 // Copy words to registers.
3590 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) {
3591 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3592 DAG.getConstant(OffsetInBytes, PtrTy));
3593 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3594 MachinePointerInfo(), false, false, false,
3596 MemOpChains.push_back(LoadVal.getValue(1));
3597 unsigned ArgReg = ArgRegs[FirstReg + I];
3598 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3601 // Return if the struct has been fully copied.
3602 if (ByValSizeInBytes == OffsetInBytes)
3605 // Copy the remainder of the byval argument with sub-word loads and shifts.
3606 if (LeftoverBytes) {
3609 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
3610 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
3611 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
3613 if (RemainingSizeInBytes < LoadSizeInBytes)
3617 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3618 DAG.getConstant(OffsetInBytes, PtrTy));
3619 SDValue LoadVal = DAG.getExtLoad(
3620 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
3621 MVT::getIntegerVT(LoadSizeInBytes * 8), false, false, false,
3623 MemOpChains.push_back(LoadVal.getValue(1));
3625 // Shift the loaded value.
3629 Shamt = TotalBytesLoaded * 8;
3631 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
3633 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3634 DAG.getConstant(Shamt, MVT::i32));
3637 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3641 OffsetInBytes += LoadSizeInBytes;
3642 TotalBytesLoaded += LoadSizeInBytes;
3643 Alignment = std::min(Alignment, LoadSizeInBytes);
3646 unsigned ArgReg = ArgRegs[FirstReg + I];
3647 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3652 // Copy remainder of byval arg to it with memcpy.
3653 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
3654 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3655 DAG.getConstant(OffsetInBytes, PtrTy));
3656 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3657 DAG.getIntPtrConstant(VA.getLocMemOffset()));
3658 Chain = DAG.getMemcpy(Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, PtrTy),
3659 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
3660 MachinePointerInfo(), MachinePointerInfo());
3661 MemOpChains.push_back(Chain);
3664 void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3665 SDValue Chain, SDLoc DL,
3667 CCState &State) const {
3668 const ArrayRef<MCPhysReg> ArgRegs = Subtarget.getABI().GetVarArgRegs();
3669 unsigned Idx = State.getFirstUnallocated(ArgRegs.data(), ArgRegs.size());
3670 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3671 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
3672 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3673 MachineFunction &MF = DAG.getMachineFunction();
3674 MachineFrameInfo *MFI = MF.getFrameInfo();
3675 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3677 // Offset of the first variable argument from stack pointer.
3680 if (ArgRegs.size() == Idx)
3682 RoundUpToAlignment(State.getNextStackOffset(), RegSizeInBytes);
3684 const MipsABIInfo &ABI = Subtarget.getABI();
3686 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3687 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
3690 // Record the frame index of the first variable argument
3691 // which is a value necessary to VASTART.
3692 int FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
3693 MipsFI->setVarArgsFrameIndex(FI);
3695 // Copy the integer registers that have not been used for argument passing
3696 // to the argument register save area. For O32, the save area is allocated
3697 // in the caller's stack frame, while for N32/64, it is allocated in the
3698 // callee's stack frame.
3699 for (unsigned I = Idx; I < ArgRegs.size();
3700 ++I, VaArgOffset += RegSizeInBytes) {
3701 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
3702 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3703 FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
3704 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3705 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3706 MachinePointerInfo(), false, false, 0);
3707 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
3709 OutChains.push_back(Store);
3713 void MipsTargetLowering::HandleByVal(CCState *State, unsigned &Size,
3714 unsigned Align) const {
3715 MachineFunction &MF = State->getMachineFunction();
3716 const TargetFrameLowering *TFL = MF.getSubtarget().getFrameLowering();
3718 assert(Size && "Byval argument's size shouldn't be 0.");
3720 Align = std::min(Align, TFL->getStackAlignment());
3722 unsigned FirstReg = 0;
3723 unsigned NumRegs = 0;
3725 if (State->getCallingConv() != CallingConv::Fast) {
3726 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3727 const ArrayRef<MCPhysReg> IntArgRegs = Subtarget.getABI().GetByValArgRegs();
3728 // FIXME: The O32 case actually describes no shadow registers.
3729 const MCPhysReg *ShadowRegs =
3730 Subtarget.isABI_O32() ? IntArgRegs.data() : Mips64DPRegs;
3732 // We used to check the size as well but we can't do that anymore since
3733 // CCState::HandleByVal() rounds up the size after calling this function.
3734 assert(!(Align % RegSizeInBytes) &&
3735 "Byval argument's alignment should be a multiple of"
3738 FirstReg = State->getFirstUnallocated(IntArgRegs.data(), IntArgRegs.size());
3740 // If Align > RegSizeInBytes, the first arg register must be even.
3741 // FIXME: This condition happens to do the right thing but it's not the
3742 // right way to test it. We want to check that the stack frame offset
3743 // of the register is aligned.
3744 if ((Align > RegSizeInBytes) && (FirstReg % 2)) {
3745 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]);
3749 // Mark the registers allocated.
3750 Size = RoundUpToAlignment(Size, RegSizeInBytes);
3751 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size());
3752 Size -= RegSizeInBytes, ++I, ++NumRegs)
3753 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3756 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs);
3760 MipsTargetLowering::emitPseudoSELECT(MachineInstr *MI, MachineBasicBlock *BB,
3761 bool isFPCmp, unsigned Opc) const {
3762 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
3763 "Subtarget already supports SELECT nodes with the use of"
3764 "conditional-move instructions.");
3766 const TargetInstrInfo *TII =
3767 getTargetMachine().getSubtargetImpl()->getInstrInfo();
3768 DebugLoc DL = MI->getDebugLoc();
3770 // To "insert" a SELECT instruction, we actually have to insert the
3771 // diamond control-flow pattern. The incoming instruction knows the
3772 // destination vreg to set, the condition code register to branch on, the
3773 // true/false values to select between, and a branch opcode to use.
3774 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3775 MachineFunction::iterator It = BB;
3782 // bNE r1, r0, copy1MBB
3783 // fallthrough --> copy0MBB
3784 MachineBasicBlock *thisMBB = BB;
3785 MachineFunction *F = BB->getParent();
3786 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3787 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
3788 F->insert(It, copy0MBB);
3789 F->insert(It, sinkMBB);
3791 // Transfer the remainder of BB and its successor edges to sinkMBB.
3792 sinkMBB->splice(sinkMBB->begin(), BB,
3793 std::next(MachineBasicBlock::iterator(MI)), BB->end());
3794 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3796 // Next, add the true and fallthrough blocks as its successors.
3797 BB->addSuccessor(copy0MBB);
3798 BB->addSuccessor(sinkMBB);
3801 // bc1[tf] cc, sinkMBB
3802 BuildMI(BB, DL, TII->get(Opc))
3803 .addReg(MI->getOperand(1).getReg())
3806 // bne rs, $0, sinkMBB
3807 BuildMI(BB, DL, TII->get(Opc))
3808 .addReg(MI->getOperand(1).getReg())
3814 // %FalseValue = ...
3815 // # fallthrough to sinkMBB
3818 // Update machine-CFG edges
3819 BB->addSuccessor(sinkMBB);
3822 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
3826 BuildMI(*BB, BB->begin(), DL,
3827 TII->get(Mips::PHI), MI->getOperand(0).getReg())
3828 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
3829 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB);
3831 MI->eraseFromParent(); // The pseudo instruction is gone now.