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 for (MVT VT : MVT::integer_valuetypes()) {
219 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
220 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
221 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
224 // MIPS doesn't have extending float->double load/store
225 for (MVT VT : MVT::fp_valuetypes())
226 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
227 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
229 // Used by legalize types to correctly generate the setcc result.
230 // Without this, every float setcc comes with a AND/OR with the result,
231 // we don't want this, since the fpcmp result goes to a flag register,
232 // which is used implicitly by brcond and select operations.
233 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
235 // Mips Custom Operations
236 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
237 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
238 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
239 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
240 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
241 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
242 setOperationAction(ISD::SELECT, MVT::f32, Custom);
243 setOperationAction(ISD::SELECT, MVT::f64, Custom);
244 setOperationAction(ISD::SELECT, MVT::i32, Custom);
245 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
246 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
247 setOperationAction(ISD::SETCC, MVT::f32, Custom);
248 setOperationAction(ISD::SETCC, MVT::f64, Custom);
249 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
250 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
251 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
252 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
254 if (Subtarget.isGP64bit()) {
255 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
256 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
257 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
258 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
259 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
260 setOperationAction(ISD::SELECT, MVT::i64, Custom);
261 setOperationAction(ISD::LOAD, MVT::i64, Custom);
262 setOperationAction(ISD::STORE, MVT::i64, Custom);
263 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
266 if (!Subtarget.isGP64bit()) {
267 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
268 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
269 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
272 setOperationAction(ISD::ADD, MVT::i32, Custom);
273 if (Subtarget.isGP64bit())
274 setOperationAction(ISD::ADD, MVT::i64, Custom);
276 setOperationAction(ISD::SDIV, MVT::i32, Expand);
277 setOperationAction(ISD::SREM, MVT::i32, Expand);
278 setOperationAction(ISD::UDIV, MVT::i32, Expand);
279 setOperationAction(ISD::UREM, MVT::i32, Expand);
280 setOperationAction(ISD::SDIV, MVT::i64, Expand);
281 setOperationAction(ISD::SREM, MVT::i64, Expand);
282 setOperationAction(ISD::UDIV, MVT::i64, Expand);
283 setOperationAction(ISD::UREM, MVT::i64, Expand);
285 // Operations not directly supported by Mips.
286 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
287 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
288 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
289 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
290 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
291 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
292 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
293 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
294 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
295 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
296 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
297 if (Subtarget.hasCnMips()) {
298 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
299 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
301 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
302 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
304 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
305 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
306 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
307 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
308 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
309 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
310 setOperationAction(ISD::ROTL, MVT::i32, Expand);
311 setOperationAction(ISD::ROTL, MVT::i64, Expand);
312 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
313 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
315 if (!Subtarget.hasMips32r2())
316 setOperationAction(ISD::ROTR, MVT::i32, Expand);
318 if (!Subtarget.hasMips64r2())
319 setOperationAction(ISD::ROTR, MVT::i64, Expand);
321 setOperationAction(ISD::FSIN, MVT::f32, Expand);
322 setOperationAction(ISD::FSIN, MVT::f64, Expand);
323 setOperationAction(ISD::FCOS, MVT::f32, Expand);
324 setOperationAction(ISD::FCOS, MVT::f64, Expand);
325 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
326 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
327 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
328 setOperationAction(ISD::FPOW, MVT::f32, Expand);
329 setOperationAction(ISD::FPOW, MVT::f64, Expand);
330 setOperationAction(ISD::FLOG, MVT::f32, Expand);
331 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
332 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
333 setOperationAction(ISD::FEXP, MVT::f32, Expand);
334 setOperationAction(ISD::FMA, MVT::f32, Expand);
335 setOperationAction(ISD::FMA, MVT::f64, Expand);
336 setOperationAction(ISD::FREM, MVT::f32, Expand);
337 setOperationAction(ISD::FREM, MVT::f64, Expand);
339 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
341 setOperationAction(ISD::VASTART, MVT::Other, Custom);
342 setOperationAction(ISD::VAARG, MVT::Other, Custom);
343 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
344 setOperationAction(ISD::VAEND, MVT::Other, Expand);
346 // Use the default for now
347 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
348 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
350 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
351 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
352 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
353 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
355 setInsertFencesForAtomic(true);
357 if (!Subtarget.hasMips32r2()) {
358 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
359 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
362 // MIPS16 lacks MIPS32's clz and clo instructions.
363 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode())
364 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
365 if (!Subtarget.hasMips64())
366 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
368 if (!Subtarget.hasMips32r2())
369 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
370 if (!Subtarget.hasMips64r2())
371 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
373 if (Subtarget.isGP64bit()) {
374 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i32, Custom);
375 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i32, Custom);
376 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i32, Custom);
377 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
380 setOperationAction(ISD::TRAP, MVT::Other, Legal);
382 setTargetDAGCombine(ISD::SDIVREM);
383 setTargetDAGCombine(ISD::UDIVREM);
384 setTargetDAGCombine(ISD::SELECT);
385 setTargetDAGCombine(ISD::AND);
386 setTargetDAGCombine(ISD::OR);
387 setTargetDAGCombine(ISD::ADD);
389 setMinFunctionAlignment(Subtarget.isGP64bit() ? 3 : 2);
391 // The arguments on the stack are defined in terms of 4-byte slots on O32
392 // and 8-byte slots on N32/N64.
393 setMinStackArgumentAlignment(
394 (Subtarget.isABI_N32() || Subtarget.isABI_N64()) ? 8 : 4);
396 setStackPointerRegisterToSaveRestore(Subtarget.isABI_N64() ? Mips::SP_64
399 setExceptionPointerRegister(Subtarget.isABI_N64() ? Mips::A0_64 : Mips::A0);
400 setExceptionSelectorRegister(Subtarget.isABI_N64() ? Mips::A1_64 : Mips::A1);
402 MaxStoresPerMemcpy = 16;
404 isMicroMips = Subtarget.inMicroMipsMode();
407 const MipsTargetLowering *MipsTargetLowering::create(const MipsTargetMachine &TM,
408 const MipsSubtarget &STI) {
409 if (STI.inMips16Mode())
410 return llvm::createMips16TargetLowering(TM, STI);
412 return llvm::createMipsSETargetLowering(TM, STI);
415 // Create a fast isel object.
417 MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
418 const TargetLibraryInfo *libInfo) const {
419 if (!EnableMipsFastISel)
420 return TargetLowering::createFastISel(funcInfo, libInfo);
421 return Mips::createFastISel(funcInfo, libInfo);
424 EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
427 return VT.changeVectorElementTypeToInteger();
430 static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
431 TargetLowering::DAGCombinerInfo &DCI,
432 const MipsSubtarget &Subtarget) {
433 if (DCI.isBeforeLegalizeOps())
436 EVT Ty = N->getValueType(0);
437 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
438 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
439 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
443 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
444 N->getOperand(0), N->getOperand(1));
445 SDValue InChain = DAG.getEntryNode();
446 SDValue InGlue = DivRem;
449 if (N->hasAnyUseOfValue(0)) {
450 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
452 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
453 InChain = CopyFromLo.getValue(1);
454 InGlue = CopyFromLo.getValue(2);
458 if (N->hasAnyUseOfValue(1)) {
459 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
461 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
467 static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
469 default: llvm_unreachable("Unknown fp condition code!");
471 case ISD::SETOEQ: return Mips::FCOND_OEQ;
472 case ISD::SETUNE: return Mips::FCOND_UNE;
474 case ISD::SETOLT: return Mips::FCOND_OLT;
476 case ISD::SETOGT: return Mips::FCOND_OGT;
478 case ISD::SETOLE: return Mips::FCOND_OLE;
480 case ISD::SETOGE: return Mips::FCOND_OGE;
481 case ISD::SETULT: return Mips::FCOND_ULT;
482 case ISD::SETULE: return Mips::FCOND_ULE;
483 case ISD::SETUGT: return Mips::FCOND_UGT;
484 case ISD::SETUGE: return Mips::FCOND_UGE;
485 case ISD::SETUO: return Mips::FCOND_UN;
486 case ISD::SETO: return Mips::FCOND_OR;
488 case ISD::SETONE: return Mips::FCOND_ONE;
489 case ISD::SETUEQ: return Mips::FCOND_UEQ;
494 /// This function returns true if the floating point conditional branches and
495 /// conditional moves which use condition code CC should be inverted.
496 static bool invertFPCondCodeUser(Mips::CondCode CC) {
497 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
500 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
501 "Illegal Condition Code");
506 // Creates and returns an FPCmp node from a setcc node.
507 // Returns Op if setcc is not a floating point comparison.
508 static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
509 // must be a SETCC node
510 if (Op.getOpcode() != ISD::SETCC)
513 SDValue LHS = Op.getOperand(0);
515 if (!LHS.getValueType().isFloatingPoint())
518 SDValue RHS = Op.getOperand(1);
521 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
522 // node if necessary.
523 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
525 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
526 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
529 // Creates and returns a CMovFPT/F node.
530 static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
531 SDValue False, SDLoc DL) {
532 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
533 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
534 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
536 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
537 True.getValueType(), True, FCC0, False, Cond);
540 static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
541 TargetLowering::DAGCombinerInfo &DCI,
542 const MipsSubtarget &Subtarget) {
543 if (DCI.isBeforeLegalizeOps())
546 SDValue SetCC = N->getOperand(0);
548 if ((SetCC.getOpcode() != ISD::SETCC) ||
549 !SetCC.getOperand(0).getValueType().isInteger())
552 SDValue False = N->getOperand(2);
553 EVT FalseTy = False.getValueType();
555 if (!FalseTy.isInteger())
558 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
560 // If the RHS (False) is 0, we swap the order of the operands
561 // of ISD::SELECT (obviously also inverting the condition) so that we can
562 // take advantage of conditional moves using the $0 register.
564 // return (a != 0) ? x : 0;
572 if (!FalseC->getZExtValue()) {
573 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
574 SDValue True = N->getOperand(1);
576 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
577 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
579 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
582 // If both operands are integer constants there's a possibility that we
583 // can do some interesting optimizations.
584 SDValue True = N->getOperand(1);
585 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
587 if (!TrueC || !True.getValueType().isInteger())
590 // We'll also ignore MVT::i64 operands as this optimizations proves
591 // to be ineffective because of the required sign extensions as the result
592 // of a SETCC operator is always MVT::i32 for non-vector types.
593 if (True.getValueType() == MVT::i64)
596 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
598 // 1) (a < x) ? y : y-1
600 // addiu $reg2, $reg1, y-1
602 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
604 // 2) (a < x) ? y-1 : y
606 // xor $reg1, $reg1, 1
607 // addiu $reg2, $reg1, y-1
609 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
610 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
611 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
612 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
615 // Couldn't optimize.
619 static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
620 TargetLowering::DAGCombinerInfo &DCI,
621 const MipsSubtarget &Subtarget) {
622 // Pattern match EXT.
623 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
624 // => ext $dst, $src, size, pos
625 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
628 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
629 unsigned ShiftRightOpc = ShiftRight.getOpcode();
631 // Op's first operand must be a shift right.
632 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
635 // The second operand of the shift must be an immediate.
637 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
640 uint64_t Pos = CN->getZExtValue();
641 uint64_t SMPos, SMSize;
643 // Op's second operand must be a shifted mask.
644 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
645 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
648 // Return if the shifted mask does not start at bit 0 or the sum of its size
649 // and Pos exceeds the word's size.
650 EVT ValTy = N->getValueType(0);
651 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
654 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
655 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
656 DAG.getConstant(SMSize, MVT::i32));
659 static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
660 TargetLowering::DAGCombinerInfo &DCI,
661 const MipsSubtarget &Subtarget) {
662 // Pattern match INS.
663 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
664 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
665 // => ins $dst, $src, size, pos, $src1
666 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
669 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
670 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
673 // See if Op's first operand matches (and $src1 , mask0).
674 if (And0.getOpcode() != ISD::AND)
677 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
678 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
681 // See if Op's second operand matches (and (shl $src, pos), mask1).
682 if (And1.getOpcode() != ISD::AND)
685 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
686 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
689 // The shift masks must have the same position and size.
690 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
693 SDValue Shl = And1.getOperand(0);
694 if (Shl.getOpcode() != ISD::SHL)
697 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
700 unsigned Shamt = CN->getZExtValue();
702 // Return if the shift amount and the first bit position of mask are not the
704 EVT ValTy = N->getValueType(0);
705 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
708 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
709 DAG.getConstant(SMPos0, MVT::i32),
710 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
713 static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
714 TargetLowering::DAGCombinerInfo &DCI,
715 const MipsSubtarget &Subtarget) {
716 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
718 if (DCI.isBeforeLegalizeOps())
721 SDValue Add = N->getOperand(1);
723 if (Add.getOpcode() != ISD::ADD)
726 SDValue Lo = Add.getOperand(1);
728 if ((Lo.getOpcode() != MipsISD::Lo) ||
729 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
732 EVT ValTy = N->getValueType(0);
735 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
737 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
740 SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
742 SelectionDAG &DAG = DCI.DAG;
743 unsigned Opc = N->getOpcode();
749 return performDivRemCombine(N, DAG, DCI, Subtarget);
751 return performSELECTCombine(N, DAG, DCI, Subtarget);
753 return performANDCombine(N, DAG, DCI, Subtarget);
755 return performORCombine(N, DAG, DCI, Subtarget);
757 return performADDCombine(N, DAG, DCI, Subtarget);
764 MipsTargetLowering::LowerOperationWrapper(SDNode *N,
765 SmallVectorImpl<SDValue> &Results,
766 SelectionDAG &DAG) const {
767 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
769 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
770 Results.push_back(Res.getValue(I));
774 MipsTargetLowering::ReplaceNodeResults(SDNode *N,
775 SmallVectorImpl<SDValue> &Results,
776 SelectionDAG &DAG) const {
777 return LowerOperationWrapper(N, Results, DAG);
780 SDValue MipsTargetLowering::
781 LowerOperation(SDValue Op, SelectionDAG &DAG) const
783 switch (Op.getOpcode())
785 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
786 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
787 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
788 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
789 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
790 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
791 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
792 case ISD::SELECT: return lowerSELECT(Op, DAG);
793 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
794 case ISD::SETCC: return lowerSETCC(Op, DAG);
795 case ISD::VASTART: return lowerVASTART(Op, DAG);
796 case ISD::VAARG: return lowerVAARG(Op, DAG);
797 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
798 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
799 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
800 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
801 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
802 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
803 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
804 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
805 case ISD::LOAD: return lowerLOAD(Op, DAG);
806 case ISD::STORE: return lowerSTORE(Op, DAG);
807 case ISD::ADD: return lowerADD(Op, DAG);
808 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
813 //===----------------------------------------------------------------------===//
814 // Lower helper functions
815 //===----------------------------------------------------------------------===//
817 // addLiveIn - This helper function adds the specified physical register to the
818 // MachineFunction as a live in value. It also creates a corresponding
819 // virtual register for it.
821 addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
823 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
824 MF.getRegInfo().addLiveIn(PReg, VReg);
828 static MachineBasicBlock *insertDivByZeroTrap(MachineInstr *MI,
829 MachineBasicBlock &MBB,
830 const TargetInstrInfo &TII,
835 // Insert instruction "teq $divisor_reg, $zero, 7".
836 MachineBasicBlock::iterator I(MI);
837 MachineInstrBuilder MIB;
838 MachineOperand &Divisor = MI->getOperand(2);
839 MIB = BuildMI(MBB, std::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
840 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
841 .addReg(Mips::ZERO).addImm(7);
843 // Use the 32-bit sub-register if this is a 64-bit division.
845 MIB->getOperand(0).setSubReg(Mips::sub_32);
847 // Clear Divisor's kill flag.
848 Divisor.setIsKill(false);
850 // We would normally delete the original instruction here but in this case
851 // we only needed to inject an additional instruction rather than replace it.
857 MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
858 MachineBasicBlock *BB) const {
859 switch (MI->getOpcode()) {
861 llvm_unreachable("Unexpected instr type to insert");
862 case Mips::ATOMIC_LOAD_ADD_I8:
863 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
864 case Mips::ATOMIC_LOAD_ADD_I16:
865 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
866 case Mips::ATOMIC_LOAD_ADD_I32:
867 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
868 case Mips::ATOMIC_LOAD_ADD_I64:
869 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
871 case Mips::ATOMIC_LOAD_AND_I8:
872 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
873 case Mips::ATOMIC_LOAD_AND_I16:
874 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
875 case Mips::ATOMIC_LOAD_AND_I32:
876 return emitAtomicBinary(MI, BB, 4, Mips::AND);
877 case Mips::ATOMIC_LOAD_AND_I64:
878 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
880 case Mips::ATOMIC_LOAD_OR_I8:
881 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
882 case Mips::ATOMIC_LOAD_OR_I16:
883 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
884 case Mips::ATOMIC_LOAD_OR_I32:
885 return emitAtomicBinary(MI, BB, 4, Mips::OR);
886 case Mips::ATOMIC_LOAD_OR_I64:
887 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
889 case Mips::ATOMIC_LOAD_XOR_I8:
890 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
891 case Mips::ATOMIC_LOAD_XOR_I16:
892 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
893 case Mips::ATOMIC_LOAD_XOR_I32:
894 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
895 case Mips::ATOMIC_LOAD_XOR_I64:
896 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
898 case Mips::ATOMIC_LOAD_NAND_I8:
899 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
900 case Mips::ATOMIC_LOAD_NAND_I16:
901 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
902 case Mips::ATOMIC_LOAD_NAND_I32:
903 return emitAtomicBinary(MI, BB, 4, 0, true);
904 case Mips::ATOMIC_LOAD_NAND_I64:
905 return emitAtomicBinary(MI, BB, 8, 0, true);
907 case Mips::ATOMIC_LOAD_SUB_I8:
908 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
909 case Mips::ATOMIC_LOAD_SUB_I16:
910 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
911 case Mips::ATOMIC_LOAD_SUB_I32:
912 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
913 case Mips::ATOMIC_LOAD_SUB_I64:
914 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
916 case Mips::ATOMIC_SWAP_I8:
917 return emitAtomicBinaryPartword(MI, BB, 1, 0);
918 case Mips::ATOMIC_SWAP_I16:
919 return emitAtomicBinaryPartword(MI, BB, 2, 0);
920 case Mips::ATOMIC_SWAP_I32:
921 return emitAtomicBinary(MI, BB, 4, 0);
922 case Mips::ATOMIC_SWAP_I64:
923 return emitAtomicBinary(MI, BB, 8, 0);
925 case Mips::ATOMIC_CMP_SWAP_I8:
926 return emitAtomicCmpSwapPartword(MI, BB, 1);
927 case Mips::ATOMIC_CMP_SWAP_I16:
928 return emitAtomicCmpSwapPartword(MI, BB, 2);
929 case Mips::ATOMIC_CMP_SWAP_I32:
930 return emitAtomicCmpSwap(MI, BB, 4);
931 case Mips::ATOMIC_CMP_SWAP_I64:
932 return emitAtomicCmpSwap(MI, BB, 8);
933 case Mips::PseudoSDIV:
934 case Mips::PseudoUDIV:
939 return insertDivByZeroTrap(
940 MI, *BB, *getTargetMachine().getSubtargetImpl()->getInstrInfo(), false);
941 case Mips::PseudoDSDIV:
942 case Mips::PseudoDUDIV:
947 return insertDivByZeroTrap(
948 MI, *BB, *getTargetMachine().getSubtargetImpl()->getInstrInfo(), true);
950 return emitSEL_D(MI, BB);
952 case Mips::PseudoSELECT_I:
953 case Mips::PseudoSELECT_I64:
954 case Mips::PseudoSELECT_S:
955 case Mips::PseudoSELECT_D32:
956 case Mips::PseudoSELECT_D64:
957 return emitPseudoSELECT(MI, BB, false, Mips::BNE);
958 case Mips::PseudoSELECTFP_F_I:
959 case Mips::PseudoSELECTFP_F_I64:
960 case Mips::PseudoSELECTFP_F_S:
961 case Mips::PseudoSELECTFP_F_D32:
962 case Mips::PseudoSELECTFP_F_D64:
963 return emitPseudoSELECT(MI, BB, true, Mips::BC1F);
964 case Mips::PseudoSELECTFP_T_I:
965 case Mips::PseudoSELECTFP_T_I64:
966 case Mips::PseudoSELECTFP_T_S:
967 case Mips::PseudoSELECTFP_T_D32:
968 case Mips::PseudoSELECTFP_T_D64:
969 return emitPseudoSELECT(MI, BB, true, Mips::BC1T);
973 // This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
974 // Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
976 MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
977 unsigned Size, unsigned BinOpcode,
979 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
981 MachineFunction *MF = BB->getParent();
982 MachineRegisterInfo &RegInfo = MF->getRegInfo();
983 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
984 const TargetInstrInfo *TII =
985 getTargetMachine().getSubtargetImpl()->getInstrInfo();
986 DebugLoc DL = MI->getDebugLoc();
987 unsigned LL, SC, AND, NOR, ZERO, BEQ;
994 LL = Subtarget.hasMips32r6() ? Mips::LL_R6 : Mips::LL;
995 SC = Subtarget.hasMips32r6() ? Mips::SC_R6 : Mips::SC;
1002 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1003 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
1006 ZERO = Mips::ZERO_64;
1010 unsigned OldVal = MI->getOperand(0).getReg();
1011 unsigned Ptr = MI->getOperand(1).getReg();
1012 unsigned Incr = MI->getOperand(2).getReg();
1014 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1015 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1016 unsigned Success = RegInfo.createVirtualRegister(RC);
1018 // insert new blocks after the current block
1019 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1020 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1021 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1022 MachineFunction::iterator It = BB;
1024 MF->insert(It, loopMBB);
1025 MF->insert(It, exitMBB);
1027 // Transfer the remainder of BB and its successor edges to exitMBB.
1028 exitMBB->splice(exitMBB->begin(), BB,
1029 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1030 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1034 // fallthrough --> loopMBB
1035 BB->addSuccessor(loopMBB);
1036 loopMBB->addSuccessor(loopMBB);
1037 loopMBB->addSuccessor(exitMBB);
1040 // ll oldval, 0(ptr)
1041 // <binop> storeval, oldval, incr
1042 // sc success, storeval, 0(ptr)
1043 // beq success, $0, loopMBB
1045 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
1047 // and andres, oldval, incr
1048 // nor storeval, $0, andres
1049 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1050 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
1051 } else if (BinOpcode) {
1052 // <binop> storeval, oldval, incr
1053 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
1057 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1058 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
1060 MI->eraseFromParent(); // The instruction is gone now.
1065 MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
1066 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
1067 unsigned SrcReg) const {
1068 const TargetInstrInfo *TII =
1069 getTargetMachine().getSubtargetImpl()->getInstrInfo();
1070 DebugLoc DL = MI->getDebugLoc();
1072 if (Subtarget.hasMips32r2() && Size == 1) {
1073 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1077 if (Subtarget.hasMips32r2() && Size == 2) {
1078 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1082 MachineFunction *MF = BB->getParent();
1083 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1084 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1085 unsigned ScrReg = RegInfo.createVirtualRegister(RC);
1088 int64_t ShiftImm = 32 - (Size * 8);
1090 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1091 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1096 MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
1097 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
1099 assert((Size == 1 || Size == 2) &&
1100 "Unsupported size for EmitAtomicBinaryPartial.");
1102 MachineFunction *MF = BB->getParent();
1103 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1104 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1105 const TargetInstrInfo *TII =
1106 getTargetMachine().getSubtargetImpl()->getInstrInfo();
1107 DebugLoc DL = MI->getDebugLoc();
1109 unsigned Dest = MI->getOperand(0).getReg();
1110 unsigned Ptr = MI->getOperand(1).getReg();
1111 unsigned Incr = MI->getOperand(2).getReg();
1113 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1114 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
1115 unsigned Mask = RegInfo.createVirtualRegister(RC);
1116 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
1117 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1118 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1119 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
1120 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1121 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1122 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1123 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1124 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
1125 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1126 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1127 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1128 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1129 unsigned Success = RegInfo.createVirtualRegister(RC);
1131 // insert new blocks after the current block
1132 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1133 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1134 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1135 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1136 MachineFunction::iterator It = BB;
1138 MF->insert(It, loopMBB);
1139 MF->insert(It, sinkMBB);
1140 MF->insert(It, exitMBB);
1142 // Transfer the remainder of BB and its successor edges to exitMBB.
1143 exitMBB->splice(exitMBB->begin(), BB,
1144 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1145 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1147 BB->addSuccessor(loopMBB);
1148 loopMBB->addSuccessor(loopMBB);
1149 loopMBB->addSuccessor(sinkMBB);
1150 sinkMBB->addSuccessor(exitMBB);
1153 // addiu masklsb2,$0,-4 # 0xfffffffc
1154 // and alignedaddr,ptr,masklsb2
1155 // andi ptrlsb2,ptr,3
1156 // sll shiftamt,ptrlsb2,3
1157 // ori maskupper,$0,255 # 0xff
1158 // sll mask,maskupper,shiftamt
1159 // nor mask2,$0,mask
1160 // sll incr2,incr,shiftamt
1162 int64_t MaskImm = (Size == 1) ? 255 : 65535;
1163 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
1164 .addReg(Mips::ZERO).addImm(-4);
1165 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
1166 .addReg(Ptr).addReg(MaskLSB2);
1167 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1168 if (Subtarget.isLittle()) {
1169 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1171 unsigned Off = RegInfo.createVirtualRegister(RC);
1172 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1173 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1174 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1176 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
1177 .addReg(Mips::ZERO).addImm(MaskImm);
1178 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
1179 .addReg(MaskUpper).addReg(ShiftAmt);
1180 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1181 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
1183 // atomic.load.binop
1185 // ll oldval,0(alignedaddr)
1186 // binop binopres,oldval,incr2
1187 // and newval,binopres,mask
1188 // and maskedoldval0,oldval,mask2
1189 // or storeval,maskedoldval0,newval
1190 // sc success,storeval,0(alignedaddr)
1191 // beq success,$0,loopMBB
1195 // ll oldval,0(alignedaddr)
1196 // and newval,incr2,mask
1197 // and maskedoldval0,oldval,mask2
1198 // or storeval,maskedoldval0,newval
1199 // sc success,storeval,0(alignedaddr)
1200 // beq success,$0,loopMBB
1203 unsigned LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1204 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
1206 // and andres, oldval, incr2
1207 // nor binopres, $0, andres
1208 // and newval, binopres, mask
1209 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1210 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
1211 .addReg(Mips::ZERO).addReg(AndRes);
1212 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
1213 } else if (BinOpcode) {
1214 // <binop> binopres, oldval, incr2
1215 // and newval, binopres, mask
1216 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1217 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
1218 } else { // atomic.swap
1219 // and newval, incr2, mask
1220 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
1223 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
1224 .addReg(OldVal).addReg(Mask2);
1225 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
1226 .addReg(MaskedOldVal0).addReg(NewVal);
1227 unsigned SC = isMicroMips ? Mips::SC_MM : Mips::SC;
1228 BuildMI(BB, DL, TII->get(SC), Success)
1229 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
1230 BuildMI(BB, DL, TII->get(Mips::BEQ))
1231 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
1234 // and maskedoldval1,oldval,mask
1235 // srl srlres,maskedoldval1,shiftamt
1236 // sign_extend dest,srlres
1239 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
1240 .addReg(OldVal).addReg(Mask);
1241 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
1242 .addReg(MaskedOldVal1).addReg(ShiftAmt);
1243 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
1245 MI->eraseFromParent(); // The instruction is gone now.
1250 MachineBasicBlock * MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
1251 MachineBasicBlock *BB,
1252 unsigned Size) const {
1253 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
1255 MachineFunction *MF = BB->getParent();
1256 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1257 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
1258 const TargetInstrInfo *TII =
1259 getTargetMachine().getSubtargetImpl()->getInstrInfo();
1260 DebugLoc DL = MI->getDebugLoc();
1261 unsigned LL, SC, ZERO, BNE, BEQ;
1264 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1265 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
1272 ZERO = Mips::ZERO_64;
1277 unsigned Dest = MI->getOperand(0).getReg();
1278 unsigned Ptr = MI->getOperand(1).getReg();
1279 unsigned OldVal = MI->getOperand(2).getReg();
1280 unsigned NewVal = MI->getOperand(3).getReg();
1282 unsigned Success = RegInfo.createVirtualRegister(RC);
1284 // insert new blocks after the current block
1285 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1286 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1287 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1288 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1289 MachineFunction::iterator It = BB;
1291 MF->insert(It, loop1MBB);
1292 MF->insert(It, loop2MBB);
1293 MF->insert(It, exitMBB);
1295 // Transfer the remainder of BB and its successor edges to exitMBB.
1296 exitMBB->splice(exitMBB->begin(), BB,
1297 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1298 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1302 // fallthrough --> loop1MBB
1303 BB->addSuccessor(loop1MBB);
1304 loop1MBB->addSuccessor(exitMBB);
1305 loop1MBB->addSuccessor(loop2MBB);
1306 loop2MBB->addSuccessor(loop1MBB);
1307 loop2MBB->addSuccessor(exitMBB);
1311 // bne dest, oldval, exitMBB
1313 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1314 BuildMI(BB, DL, TII->get(BNE))
1315 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
1318 // sc success, newval, 0(ptr)
1319 // beq success, $0, loop1MBB
1321 BuildMI(BB, DL, TII->get(SC), Success)
1322 .addReg(NewVal).addReg(Ptr).addImm(0);
1323 BuildMI(BB, DL, TII->get(BEQ))
1324 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
1326 MI->eraseFromParent(); // The instruction is gone now.
1332 MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
1333 MachineBasicBlock *BB,
1334 unsigned Size) const {
1335 assert((Size == 1 || Size == 2) &&
1336 "Unsupported size for EmitAtomicCmpSwapPartial.");
1338 MachineFunction *MF = BB->getParent();
1339 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1340 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1341 const TargetInstrInfo *TII =
1342 getTargetMachine().getSubtargetImpl()->getInstrInfo();
1343 DebugLoc DL = MI->getDebugLoc();
1345 unsigned Dest = MI->getOperand(0).getReg();
1346 unsigned Ptr = MI->getOperand(1).getReg();
1347 unsigned CmpVal = MI->getOperand(2).getReg();
1348 unsigned NewVal = MI->getOperand(3).getReg();
1350 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1351 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
1352 unsigned Mask = RegInfo.createVirtualRegister(RC);
1353 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
1354 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1355 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1356 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1357 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1358 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1359 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1360 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1361 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1362 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1363 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1364 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1365 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1366 unsigned Success = RegInfo.createVirtualRegister(RC);
1368 // insert new blocks after the current block
1369 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1370 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1371 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1372 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1373 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1374 MachineFunction::iterator It = BB;
1376 MF->insert(It, loop1MBB);
1377 MF->insert(It, loop2MBB);
1378 MF->insert(It, sinkMBB);
1379 MF->insert(It, exitMBB);
1381 // Transfer the remainder of BB and its successor edges to exitMBB.
1382 exitMBB->splice(exitMBB->begin(), BB,
1383 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1384 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1386 BB->addSuccessor(loop1MBB);
1387 loop1MBB->addSuccessor(sinkMBB);
1388 loop1MBB->addSuccessor(loop2MBB);
1389 loop2MBB->addSuccessor(loop1MBB);
1390 loop2MBB->addSuccessor(sinkMBB);
1391 sinkMBB->addSuccessor(exitMBB);
1393 // FIXME: computation of newval2 can be moved to loop2MBB.
1395 // addiu masklsb2,$0,-4 # 0xfffffffc
1396 // and alignedaddr,ptr,masklsb2
1397 // andi ptrlsb2,ptr,3
1398 // sll shiftamt,ptrlsb2,3
1399 // ori maskupper,$0,255 # 0xff
1400 // sll mask,maskupper,shiftamt
1401 // nor mask2,$0,mask
1402 // andi maskedcmpval,cmpval,255
1403 // sll shiftedcmpval,maskedcmpval,shiftamt
1404 // andi maskednewval,newval,255
1405 // sll shiftednewval,maskednewval,shiftamt
1406 int64_t MaskImm = (Size == 1) ? 255 : 65535;
1407 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
1408 .addReg(Mips::ZERO).addImm(-4);
1409 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
1410 .addReg(Ptr).addReg(MaskLSB2);
1411 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1412 if (Subtarget.isLittle()) {
1413 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1415 unsigned Off = RegInfo.createVirtualRegister(RC);
1416 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1417 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1418 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1420 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
1421 .addReg(Mips::ZERO).addImm(MaskImm);
1422 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
1423 .addReg(MaskUpper).addReg(ShiftAmt);
1424 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1425 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
1426 .addReg(CmpVal).addImm(MaskImm);
1427 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
1428 .addReg(MaskedCmpVal).addReg(ShiftAmt);
1429 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
1430 .addReg(NewVal).addImm(MaskImm);
1431 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
1432 .addReg(MaskedNewVal).addReg(ShiftAmt);
1435 // ll oldval,0(alginedaddr)
1436 // and maskedoldval0,oldval,mask
1437 // bne maskedoldval0,shiftedcmpval,sinkMBB
1439 unsigned LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1440 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
1441 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
1442 .addReg(OldVal).addReg(Mask);
1443 BuildMI(BB, DL, TII->get(Mips::BNE))
1444 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
1447 // and maskedoldval1,oldval,mask2
1448 // or storeval,maskedoldval1,shiftednewval
1449 // sc success,storeval,0(alignedaddr)
1450 // beq success,$0,loop1MBB
1452 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
1453 .addReg(OldVal).addReg(Mask2);
1454 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
1455 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1456 unsigned SC = isMicroMips ? Mips::SC_MM : Mips::SC;
1457 BuildMI(BB, DL, TII->get(SC), Success)
1458 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
1459 BuildMI(BB, DL, TII->get(Mips::BEQ))
1460 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
1463 // srl srlres,maskedoldval0,shiftamt
1464 // sign_extend dest,srlres
1467 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
1468 .addReg(MaskedOldVal0).addReg(ShiftAmt);
1469 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
1471 MI->eraseFromParent(); // The instruction is gone now.
1476 MachineBasicBlock *MipsTargetLowering::emitSEL_D(MachineInstr *MI,
1477 MachineBasicBlock *BB) const {
1478 MachineFunction *MF = BB->getParent();
1479 const TargetRegisterInfo *TRI =
1480 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
1481 const TargetInstrInfo *TII =
1482 getTargetMachine().getSubtargetImpl()->getInstrInfo();
1483 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1484 DebugLoc DL = MI->getDebugLoc();
1485 MachineBasicBlock::iterator II(MI);
1487 unsigned Fc = MI->getOperand(1).getReg();
1488 const auto &FGR64RegClass = TRI->getRegClass(Mips::FGR64RegClassID);
1490 unsigned Fc2 = RegInfo.createVirtualRegister(FGR64RegClass);
1492 BuildMI(*BB, II, DL, TII->get(Mips::SUBREG_TO_REG), Fc2)
1495 .addImm(Mips::sub_lo);
1497 // We don't erase the original instruction, we just replace the condition
1498 // register with the 64-bit super-register.
1499 MI->getOperand(1).setReg(Fc2);
1504 //===----------------------------------------------------------------------===//
1505 // Misc Lower Operation implementation
1506 //===----------------------------------------------------------------------===//
1507 SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
1508 SDValue Chain = Op.getOperand(0);
1509 SDValue Table = Op.getOperand(1);
1510 SDValue Index = Op.getOperand(2);
1512 EVT PTy = getPointerTy();
1513 unsigned EntrySize =
1514 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1516 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1517 DAG.getConstant(EntrySize, PTy));
1518 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1520 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1521 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1522 MachinePointerInfo::getJumpTable(), MemVT, false, false,
1524 Chain = Addr.getValue(1);
1526 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) ||
1527 Subtarget.isABI_N64()) {
1528 // For PIC, the sequence is:
1529 // BRIND(load(Jumptable + index) + RelocBase)
1530 // RelocBase can be JumpTable, GOT or some sort of global base.
1531 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1532 getPICJumpTableRelocBase(Table, DAG));
1535 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1538 SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1539 // The first operand is the chain, the second is the condition, the third is
1540 // the block to branch to if the condition is true.
1541 SDValue Chain = Op.getOperand(0);
1542 SDValue Dest = Op.getOperand(2);
1545 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
1546 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
1548 // Return if flag is not set by a floating point comparison.
1549 if (CondRes.getOpcode() != MipsISD::FPCmp)
1552 SDValue CCNode = CondRes.getOperand(2);
1554 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
1555 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1556 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
1557 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
1558 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
1559 FCC0, Dest, CondRes);
1562 SDValue MipsTargetLowering::
1563 lowerSELECT(SDValue Op, SelectionDAG &DAG) const
1565 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
1566 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
1568 // Return if flag is not set by a floating point comparison.
1569 if (Cond.getOpcode() != MipsISD::FPCmp)
1572 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1576 SDValue MipsTargetLowering::
1577 lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
1580 EVT Ty = Op.getOperand(0).getValueType();
1581 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1582 getSetCCResultType(*DAG.getContext(), Ty),
1583 Op.getOperand(0), Op.getOperand(1),
1586 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1590 SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1591 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
1592 SDValue Cond = createFPCmp(DAG, Op);
1594 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1595 "Floating point operand expected.");
1597 SDValue True = DAG.getConstant(1, MVT::i32);
1598 SDValue False = DAG.getConstant(0, MVT::i32);
1600 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
1603 SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
1604 SelectionDAG &DAG) const {
1605 EVT Ty = Op.getValueType();
1606 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1607 const GlobalValue *GV = N->getGlobal();
1609 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
1610 !Subtarget.isABI_N64()) {
1611 const MipsTargetObjectFile &TLOF =
1612 (const MipsTargetObjectFile&)getObjFileLowering();
1614 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine()))
1615 // %gp_rel relocation
1616 return getAddrGPRel(N, Ty, DAG);
1618 // %hi/%lo relocation
1619 return getAddrNonPIC(N, Ty, DAG);
1622 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
1623 return getAddrLocal(N, Ty, DAG,
1624 Subtarget.isABI_N32() || Subtarget.isABI_N64());
1627 return getAddrGlobalLargeGOT(N, Ty, DAG, MipsII::MO_GOT_HI16,
1628 MipsII::MO_GOT_LO16, DAG.getEntryNode(),
1629 MachinePointerInfo::getGOT());
1631 return getAddrGlobal(N, Ty, DAG,
1632 (Subtarget.isABI_N32() || Subtarget.isABI_N64())
1633 ? MipsII::MO_GOT_DISP
1635 DAG.getEntryNode(), MachinePointerInfo::getGOT());
1638 SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
1639 SelectionDAG &DAG) const {
1640 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1641 EVT Ty = Op.getValueType();
1643 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
1644 !Subtarget.isABI_N64())
1645 return getAddrNonPIC(N, Ty, DAG);
1647 return getAddrLocal(N, Ty, DAG,
1648 Subtarget.isABI_N32() || Subtarget.isABI_N64());
1651 SDValue MipsTargetLowering::
1652 lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
1654 // If the relocation model is PIC, use the General Dynamic TLS Model or
1655 // Local Dynamic TLS model, otherwise use the Initial Exec or
1656 // Local Exec TLS Model.
1658 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1660 const GlobalValue *GV = GA->getGlobal();
1661 EVT PtrVT = getPointerTy();
1663 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1665 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
1666 // General Dynamic and Local Dynamic TLS Model.
1667 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1670 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1671 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1672 getGlobalReg(DAG, PtrVT), TGA);
1673 unsigned PtrSize = PtrVT.getSizeInBits();
1674 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1676 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
1680 Entry.Node = Argument;
1682 Args.push_back(Entry);
1684 TargetLowering::CallLoweringInfo CLI(DAG);
1685 CLI.setDebugLoc(DL).setChain(DAG.getEntryNode())
1686 .setCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args), 0);
1687 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
1689 SDValue Ret = CallResult.first;
1691 if (model != TLSModel::LocalDynamic)
1694 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1695 MipsII::MO_DTPREL_HI);
1696 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1697 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1698 MipsII::MO_DTPREL_LO);
1699 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1700 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1701 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
1705 if (model == TLSModel::InitialExec) {
1706 // Initial Exec TLS Model
1707 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1708 MipsII::MO_GOTTPREL);
1709 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
1711 Offset = DAG.getLoad(PtrVT, DL,
1712 DAG.getEntryNode(), TGA, MachinePointerInfo(),
1713 false, false, false, 0);
1715 // Local Exec TLS Model
1716 assert(model == TLSModel::LocalExec);
1717 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1718 MipsII::MO_TPREL_HI);
1719 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1720 MipsII::MO_TPREL_LO);
1721 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1722 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1723 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1726 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1727 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
1730 SDValue MipsTargetLowering::
1731 lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
1733 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1734 EVT Ty = Op.getValueType();
1736 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
1737 !Subtarget.isABI_N64())
1738 return getAddrNonPIC(N, Ty, DAG);
1740 return getAddrLocal(N, Ty, DAG,
1741 Subtarget.isABI_N32() || Subtarget.isABI_N64());
1744 SDValue MipsTargetLowering::
1745 lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
1747 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1748 EVT Ty = Op.getValueType();
1750 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
1751 !Subtarget.isABI_N64()) {
1752 const MipsTargetObjectFile &TLOF =
1753 (const MipsTargetObjectFile&)getObjFileLowering();
1755 if (TLOF.IsConstantInSmallSection(N->getConstVal(), getTargetMachine()))
1756 // %gp_rel relocation
1757 return getAddrGPRel(N, Ty, DAG);
1759 return getAddrNonPIC(N, Ty, DAG);
1762 return getAddrLocal(N, Ty, DAG,
1763 Subtarget.isABI_N32() || Subtarget.isABI_N64());
1766 SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1767 MachineFunction &MF = DAG.getMachineFunction();
1768 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1771 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1774 // vastart just stores the address of the VarArgsFrameIndex slot into the
1775 // memory location argument.
1776 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1777 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
1778 MachinePointerInfo(SV), false, false, 0);
1781 SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
1782 SDNode *Node = Op.getNode();
1783 EVT VT = Node->getValueType(0);
1784 SDValue Chain = Node->getOperand(0);
1785 SDValue VAListPtr = Node->getOperand(1);
1786 unsigned Align = Node->getConstantOperandVal(3);
1787 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1789 unsigned ArgSlotSizeInBytes =
1790 (Subtarget.isABI_N32() || Subtarget.isABI_N64()) ? 8 : 4;
1792 SDValue VAListLoad = DAG.getLoad(getPointerTy(), DL, Chain, VAListPtr,
1793 MachinePointerInfo(SV), false, false, false,
1795 SDValue VAList = VAListLoad;
1797 // Re-align the pointer if necessary.
1798 // It should only ever be necessary for 64-bit types on O32 since the minimum
1799 // argument alignment is the same as the maximum type alignment for N32/N64.
1801 // FIXME: We currently align too often. The code generator doesn't notice
1802 // when the pointer is still aligned from the last va_arg (or pair of
1803 // va_args for the i64 on O32 case).
1804 if (Align > getMinStackArgumentAlignment()) {
1805 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
1807 VAList = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1808 DAG.getConstant(Align - 1,
1809 VAList.getValueType()));
1811 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
1812 DAG.getConstant(-(int64_t)Align,
1813 VAList.getValueType()));
1816 // Increment the pointer, VAList, to the next vaarg.
1817 unsigned ArgSizeInBytes = getDataLayout()->getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext()));
1818 SDValue Tmp3 = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1819 DAG.getConstant(RoundUpToAlignment(ArgSizeInBytes, ArgSlotSizeInBytes),
1820 VAList.getValueType()));
1821 // Store the incremented VAList to the legalized pointer
1822 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
1823 MachinePointerInfo(SV), false, false, 0);
1825 // In big-endian mode we must adjust the pointer when the load size is smaller
1826 // than the argument slot size. We must also reduce the known alignment to
1827 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
1828 // the correct half of the slot, and reduce the alignment from 8 (slot
1829 // alignment) down to 4 (type alignment).
1830 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
1831 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
1832 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
1833 DAG.getIntPtrConstant(Adjustment));
1835 // Load the actual argument out of the pointer VAList
1836 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo(), false, false,
1840 static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1841 bool HasExtractInsert) {
1842 EVT TyX = Op.getOperand(0).getValueType();
1843 EVT TyY = Op.getOperand(1).getValueType();
1844 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1845 SDValue Const31 = DAG.getConstant(31, MVT::i32);
1849 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1851 SDValue X = (TyX == MVT::f32) ?
1852 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1853 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1855 SDValue Y = (TyY == MVT::f32) ?
1856 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1857 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1860 if (HasExtractInsert) {
1861 // ext E, Y, 31, 1 ; extract bit31 of Y
1862 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1863 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1864 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1867 // srl SrlX, SllX, 1
1869 // sll SllY, SrlX, 31
1870 // or Or, SrlX, SllY
1871 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1872 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1873 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1874 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1875 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1878 if (TyX == MVT::f32)
1879 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1881 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1882 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1883 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1886 static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1887 bool HasExtractInsert) {
1888 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1889 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1890 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1891 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1894 // Bitcast to integer nodes.
1895 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1896 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
1898 if (HasExtractInsert) {
1899 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1900 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1901 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1902 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
1904 if (WidthX > WidthY)
1905 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1906 else if (WidthY > WidthX)
1907 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
1909 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1910 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1911 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1914 // (d)sll SllX, X, 1
1915 // (d)srl SrlX, SllX, 1
1916 // (d)srl SrlY, Y, width(Y)-1
1917 // (d)sll SllY, SrlX, width(Y)-1
1918 // or Or, SrlX, SllY
1919 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1920 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1921 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1922 DAG.getConstant(WidthY - 1, MVT::i32));
1924 if (WidthX > WidthY)
1925 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1926 else if (WidthY > WidthX)
1927 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1929 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1930 DAG.getConstant(WidthX - 1, MVT::i32));
1931 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1932 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
1936 MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
1937 if (Subtarget.isGP64bit())
1938 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
1940 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
1943 SDValue MipsTargetLowering::
1944 lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
1946 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1947 "Frame address can only be determined for current frame.");
1949 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1950 MFI->setFrameAddressIsTaken(true);
1951 EVT VT = Op.getValueType();
1954 DAG.getCopyFromReg(DAG.getEntryNode(), DL,
1955 Subtarget.isABI_N64() ? Mips::FP_64 : Mips::FP, VT);
1959 SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
1960 SelectionDAG &DAG) const {
1961 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
1965 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1966 "Return address can be determined only for current frame.");
1968 MachineFunction &MF = DAG.getMachineFunction();
1969 MachineFrameInfo *MFI = MF.getFrameInfo();
1970 MVT VT = Op.getSimpleValueType();
1971 unsigned RA = Subtarget.isABI_N64() ? Mips::RA_64 : Mips::RA;
1972 MFI->setReturnAddressIsTaken(true);
1974 // Return RA, which contains the return address. Mark it an implicit live-in.
1975 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
1976 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
1979 // An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1980 // generated from __builtin_eh_return (offset, handler)
1981 // The effect of this is to adjust the stack pointer by "offset"
1982 // and then branch to "handler".
1983 SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
1985 MachineFunction &MF = DAG.getMachineFunction();
1986 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1988 MipsFI->setCallsEhReturn();
1989 SDValue Chain = Op.getOperand(0);
1990 SDValue Offset = Op.getOperand(1);
1991 SDValue Handler = Op.getOperand(2);
1993 EVT Ty = Subtarget.isABI_N64() ? MVT::i64 : MVT::i32;
1995 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1996 // EH_RETURN nodes, so that instructions are emitted back-to-back.
1997 unsigned OffsetReg = Subtarget.isABI_N64() ? Mips::V1_64 : Mips::V1;
1998 unsigned AddrReg = Subtarget.isABI_N64() ? Mips::V0_64 : Mips::V0;
1999 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2000 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2001 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2002 DAG.getRegister(OffsetReg, Ty),
2003 DAG.getRegister(AddrReg, getPointerTy()),
2007 SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
2008 SelectionDAG &DAG) const {
2009 // FIXME: Need pseudo-fence for 'singlethread' fences
2010 // FIXME: Set SType for weaker fences where supported/appropriate.
2013 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
2014 DAG.getConstant(SType, MVT::i32));
2017 SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
2018 SelectionDAG &DAG) const {
2020 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2021 SDValue Shamt = Op.getOperand(2);
2024 // lo = (shl lo, shamt)
2025 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2028 // hi = (shl lo, shamt[4:0])
2029 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2030 DAG.getConstant(-1, MVT::i32));
2031 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
2032 DAG.getConstant(1, MVT::i32));
2033 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
2035 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
2036 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2037 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
2038 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2039 DAG.getConstant(0x20, MVT::i32));
2040 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2041 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
2042 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
2044 SDValue Ops[2] = {Lo, Hi};
2045 return DAG.getMergeValues(Ops, DL);
2048 SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
2051 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2052 SDValue Shamt = Op.getOperand(2);
2055 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2057 // hi = (sra hi, shamt)
2059 // hi = (srl hi, shamt)
2062 // lo = (sra hi, shamt[4:0])
2063 // hi = (sra hi, 31)
2065 // lo = (srl hi, shamt[4:0])
2067 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2068 DAG.getConstant(-1, MVT::i32));
2069 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
2070 DAG.getConstant(1, MVT::i32));
2071 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
2072 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
2073 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2074 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
2076 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2077 DAG.getConstant(0x20, MVT::i32));
2078 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
2079 DAG.getConstant(31, MVT::i32));
2080 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
2081 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2082 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
2085 SDValue Ops[2] = {Lo, Hi};
2086 return DAG.getMergeValues(Ops, DL);
2089 static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
2090 SDValue Chain, SDValue Src, unsigned Offset) {
2091 SDValue Ptr = LD->getBasePtr();
2092 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
2093 EVT BasePtrVT = Ptr.getValueType();
2095 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2098 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
2099 DAG.getConstant(Offset, BasePtrVT));
2101 SDValue Ops[] = { Chain, Ptr, Src };
2102 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
2103 LD->getMemOperand());
2106 // Expand an unaligned 32 or 64-bit integer load node.
2107 SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
2108 LoadSDNode *LD = cast<LoadSDNode>(Op);
2109 EVT MemVT = LD->getMemoryVT();
2111 if (Subtarget.systemSupportsUnalignedAccess())
2114 // Return if load is aligned or if MemVT is neither i32 nor i64.
2115 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2116 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2119 bool IsLittle = Subtarget.isLittle();
2120 EVT VT = Op.getValueType();
2121 ISD::LoadExtType ExtType = LD->getExtensionType();
2122 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2124 assert((VT == MVT::i32) || (VT == MVT::i64));
2127 // (set dst, (i64 (load baseptr)))
2129 // (set tmp, (ldl (add baseptr, 7), undef))
2130 // (set dst, (ldr baseptr, tmp))
2131 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
2132 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
2134 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
2138 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
2140 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
2144 // (set dst, (i32 (load baseptr))) or
2145 // (set dst, (i64 (sextload baseptr))) or
2146 // (set dst, (i64 (extload baseptr)))
2148 // (set tmp, (lwl (add baseptr, 3), undef))
2149 // (set dst, (lwr baseptr, tmp))
2150 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2151 (ExtType == ISD::EXTLOAD))
2154 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2157 // (set dst, (i64 (zextload baseptr)))
2159 // (set tmp0, (lwl (add baseptr, 3), undef))
2160 // (set tmp1, (lwr baseptr, tmp0))
2161 // (set tmp2, (shl tmp1, 32))
2162 // (set dst, (srl tmp2, 32))
2164 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2165 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
2166 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2167 SDValue Ops[] = { SRL, LWR.getValue(1) };
2168 return DAG.getMergeValues(Ops, DL);
2171 static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
2172 SDValue Chain, unsigned Offset) {
2173 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2174 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
2176 SDVTList VTList = DAG.getVTList(MVT::Other);
2179 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
2180 DAG.getConstant(Offset, BasePtrVT));
2182 SDValue Ops[] = { Chain, Value, Ptr };
2183 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
2184 SD->getMemOperand());
2187 // Expand an unaligned 32 or 64-bit integer store node.
2188 static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2190 SDValue Value = SD->getValue(), Chain = SD->getChain();
2191 EVT VT = Value.getValueType();
2194 // (store val, baseptr) or
2195 // (truncstore val, baseptr)
2197 // (swl val, (add baseptr, 3))
2198 // (swr val, baseptr)
2199 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
2200 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
2202 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
2205 assert(VT == MVT::i64);
2208 // (store val, baseptr)
2210 // (sdl val, (add baseptr, 7))
2211 // (sdr val, baseptr)
2212 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2213 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
2216 // Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2217 static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2218 SDValue Val = SD->getValue();
2220 if (Val.getOpcode() != ISD::FP_TO_SINT)
2223 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
2224 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
2227 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
2228 SD->getPointerInfo(), SD->isVolatile(),
2229 SD->isNonTemporal(), SD->getAlignment());
2232 SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2233 StoreSDNode *SD = cast<StoreSDNode>(Op);
2234 EVT MemVT = SD->getMemoryVT();
2236 // Lower unaligned integer stores.
2237 if (!Subtarget.systemSupportsUnalignedAccess() &&
2238 (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2239 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2240 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
2242 return lowerFP_TO_SINT_STORE(SD, DAG);
2245 SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
2246 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2247 || cast<ConstantSDNode>
2248 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2249 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2253 // (add (frameaddr 0), (frame_to_args_offset))
2254 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2255 // (add FrameObject, 0)
2256 // where FrameObject is a fixed StackObject with offset 0 which points to
2257 // the old stack pointer.
2258 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2259 EVT ValTy = Op->getValueType(0);
2260 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2261 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
2262 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
2263 DAG.getConstant(0, ValTy));
2266 SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2267 SelectionDAG &DAG) const {
2268 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
2269 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
2271 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
2274 //===----------------------------------------------------------------------===//
2275 // Calling Convention Implementation
2276 //===----------------------------------------------------------------------===//
2278 //===----------------------------------------------------------------------===//
2279 // TODO: Implement a generic logic using tblgen that can support this.
2280 // Mips O32 ABI rules:
2282 // i32 - Passed in A0, A1, A2, A3 and stack
2283 // f32 - Only passed in f32 registers if no int reg has been used yet to hold
2284 // an argument. Otherwise, passed in A1, A2, A3 and stack.
2285 // f64 - Only passed in two aliased f32 registers if no int reg has been used
2286 // yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
2287 // not used, it must be shadowed. If only A3 is available, shadow it and
2290 // For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
2291 //===----------------------------------------------------------------------===//
2293 static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2294 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2295 CCState &State, const MCPhysReg *F64Regs) {
2296 const MipsSubtarget &Subtarget =
2297 State.getMachineFunction().getTarget()
2298 .getSubtarget<const MipsSubtarget>();
2300 static const unsigned IntRegsSize = 4, FloatRegsSize = 2;
2302 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2303 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
2305 // Do not process byval args here.
2306 if (ArgFlags.isByVal())
2309 // Promote i8 and i16
2310 if (ArgFlags.isInReg() && !Subtarget.isLittle()) {
2311 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) {
2313 if (ArgFlags.isSExt())
2314 LocInfo = CCValAssign::SExtUpper;
2315 else if (ArgFlags.isZExt())
2316 LocInfo = CCValAssign::ZExtUpper;
2318 LocInfo = CCValAssign::AExtUpper;
2322 // Promote i8 and i16
2323 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2325 if (ArgFlags.isSExt())
2326 LocInfo = CCValAssign::SExt;
2327 else if (ArgFlags.isZExt())
2328 LocInfo = CCValAssign::ZExt;
2330 LocInfo = CCValAssign::AExt;
2335 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2336 // is true: function is vararg, argument is 3rd or higher, there is previous
2337 // argument which is not f32 or f64.
2338 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2339 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
2340 unsigned OrigAlign = ArgFlags.getOrigAlign();
2341 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
2343 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
2344 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2345 // If this is the first part of an i64 arg,
2346 // the allocated register must be either A0 or A2.
2347 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2348 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2350 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2351 // Allocate int register and shadow next int register. If first
2352 // available register is Mips::A1 or Mips::A3, shadow it too.
2353 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2354 if (Reg == Mips::A1 || Reg == Mips::A3)
2355 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2356 State.AllocateReg(IntRegs, IntRegsSize);
2358 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2359 // we are guaranteed to find an available float register
2360 if (ValVT == MVT::f32) {
2361 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2362 // Shadow int register
2363 State.AllocateReg(IntRegs, IntRegsSize);
2365 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2366 // Shadow int registers
2367 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2368 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2369 State.AllocateReg(IntRegs, IntRegsSize);
2370 State.AllocateReg(IntRegs, IntRegsSize);
2373 llvm_unreachable("Cannot handle this ValVT.");
2376 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2378 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
2380 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
2385 static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2386 MVT LocVT, CCValAssign::LocInfo LocInfo,
2387 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2388 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
2390 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2393 static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2394 MVT LocVT, CCValAssign::LocInfo LocInfo,
2395 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2396 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
2398 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2401 static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2402 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2403 CCState &State) LLVM_ATTRIBUTE_UNUSED;
2405 #include "MipsGenCallingConv.inc"
2407 //===----------------------------------------------------------------------===//
2408 // Call Calling Convention Implementation
2409 //===----------------------------------------------------------------------===//
2411 // Return next O32 integer argument register.
2412 static unsigned getNextIntArgReg(unsigned Reg) {
2413 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2414 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2418 MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
2419 SDValue Chain, SDValue Arg, SDLoc DL,
2420 bool IsTailCall, SelectionDAG &DAG) const {
2422 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2423 DAG.getIntPtrConstant(Offset));
2424 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2428 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2429 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2430 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2431 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2432 /*isVolatile=*/ true, false, 0);
2435 void MipsTargetLowering::
2436 getOpndList(SmallVectorImpl<SDValue> &Ops,
2437 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2438 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2439 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
2440 SDValue Chain) const {
2441 // Insert node "GP copy globalreg" before call to function.
2443 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2444 // in PIC mode) allow symbols to be resolved via lazy binding.
2445 // The lazy binding stub requires GP to point to the GOT.
2446 // Note that we don't need GP to point to the GOT for indirect calls
2447 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
2448 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
2449 // used for the function (that is, Mips linker doesn't generate lazy binding
2450 // stub for a function whose address is taken in the program).
2451 if (IsPICCall && !InternalLinkage && IsCallReloc) {
2452 unsigned GPReg = Subtarget.isABI_N64() ? Mips::GP_64 : Mips::GP;
2453 EVT Ty = Subtarget.isABI_N64() ? MVT::i64 : MVT::i32;
2454 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2457 // Build a sequence of copy-to-reg nodes chained together with token
2458 // chain and flag operands which copy the outgoing args into registers.
2459 // The InFlag in necessary since all emitted instructions must be
2463 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2464 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2465 RegsToPass[i].second, InFlag);
2466 InFlag = Chain.getValue(1);
2469 // Add argument registers to the end of the list so that they are
2470 // known live into the call.
2471 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2472 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2473 RegsToPass[i].second.getValueType()));
2475 // Add a register mask operand representing the call-preserved registers.
2476 const TargetRegisterInfo *TRI =
2477 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
2478 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2479 assert(Mask && "Missing call preserved mask for calling convention");
2480 if (Subtarget.inMips16HardFloat()) {
2481 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2482 llvm::StringRef Sym = G->getGlobal()->getName();
2483 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
2484 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
2485 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2489 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2491 if (InFlag.getNode())
2492 Ops.push_back(InFlag);
2495 /// LowerCall - functions arguments are copied from virtual regs to
2496 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
2498 MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
2499 SmallVectorImpl<SDValue> &InVals) const {
2500 SelectionDAG &DAG = CLI.DAG;
2502 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2503 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2504 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
2505 SDValue Chain = CLI.Chain;
2506 SDValue Callee = CLI.Callee;
2507 bool &IsTailCall = CLI.IsTailCall;
2508 CallingConv::ID CallConv = CLI.CallConv;
2509 bool IsVarArg = CLI.IsVarArg;
2511 MachineFunction &MF = DAG.getMachineFunction();
2512 MachineFrameInfo *MFI = MF.getFrameInfo();
2513 const TargetFrameLowering *TFL = MF.getSubtarget().getFrameLowering();
2514 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
2515 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
2517 // Analyze operands of the call, assigning locations to each operand.
2518 SmallVector<CCValAssign, 16> ArgLocs;
2520 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(),
2521 MipsCCState::getSpecialCallingConvForCallee(Callee.getNode(), Subtarget));
2523 // Allocate the reserved argument area. It seems strange to do this from the
2524 // caller side but removing it breaks the frame size calculation.
2525 const MipsABIInfo &ABI = Subtarget.getABI();
2526 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
2528 CCInfo.AnalyzeCallOperands(Outs, CC_Mips, CLI.getArgs(), Callee.getNode());
2530 // Get a count of how many bytes are to be pushed on the stack.
2531 unsigned NextStackOffset = CCInfo.getNextStackOffset();
2533 // Check if it's really possible to do a tail call.
2535 IsTailCall = isEligibleForTailCallOptimization(
2536 CCInfo, NextStackOffset, *MF.getInfo<MipsFunctionInfo>());
2538 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2539 report_fatal_error("failed to perform tail call elimination on a call "
2540 "site marked musttail");
2545 // Chain is the output chain of the last Load/Store or CopyToReg node.
2546 // ByValChain is the output chain of the last Memcpy node created for copying
2547 // byval arguments to the stack.
2548 unsigned StackAlignment = TFL->getStackAlignment();
2549 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
2550 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
2553 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
2555 SDValue StackPtr = DAG.getCopyFromReg(
2556 Chain, DL, Subtarget.isABI_N64() ? Mips::SP_64 : Mips::SP,
2559 // With EABI is it possible to have 16 args on registers.
2560 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
2561 SmallVector<SDValue, 8> MemOpChains;
2563 CCInfo.rewindByValRegsInfo();
2565 // Walk the register/memloc assignments, inserting copies/loads.
2566 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2567 SDValue Arg = OutVals[i];
2568 CCValAssign &VA = ArgLocs[i];
2569 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
2570 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2571 bool UseUpperBits = false;
2574 if (Flags.isByVal()) {
2575 unsigned FirstByValReg, LastByValReg;
2576 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2577 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2579 assert(Flags.getByValSize() &&
2580 "ByVal args of size 0 should have been ignored by front-end.");
2581 assert(ByValIdx < CCInfo.getInRegsParamsCount());
2582 assert(!IsTailCall &&
2583 "Do not tail-call optimize if there is a byval argument.");
2584 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
2585 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(),
2587 CCInfo.nextInRegsParam();
2591 // Promote the value if needed.
2592 switch (VA.getLocInfo()) {
2594 llvm_unreachable("Unknown loc info!");
2595 case CCValAssign::Full:
2596 if (VA.isRegLoc()) {
2597 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
2598 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2599 (ValVT == MVT::i64 && LocVT == MVT::f64))
2600 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
2601 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
2602 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2603 Arg, DAG.getConstant(0, MVT::i32));
2604 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2605 Arg, DAG.getConstant(1, MVT::i32));
2606 if (!Subtarget.isLittle())
2608 unsigned LocRegLo = VA.getLocReg();
2609 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2610 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2611 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
2616 case CCValAssign::BCvt:
2617 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
2619 case CCValAssign::SExtUpper:
2620 UseUpperBits = true;
2622 case CCValAssign::SExt:
2623 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
2625 case CCValAssign::ZExtUpper:
2626 UseUpperBits = true;
2628 case CCValAssign::ZExt:
2629 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
2631 case CCValAssign::AExtUpper:
2632 UseUpperBits = true;
2634 case CCValAssign::AExt:
2635 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
2640 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
2641 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2643 ISD::SHL, DL, VA.getLocVT(), Arg,
2644 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2647 // Arguments that can be passed on register must be kept at
2648 // RegsToPass vector
2649 if (VA.isRegLoc()) {
2650 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2654 // Register can't get to this point...
2655 assert(VA.isMemLoc());
2657 // emit ISD::STORE whichs stores the
2658 // parameter value to a stack Location
2659 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
2660 Chain, Arg, DL, IsTailCall, DAG));
2663 // Transform all store nodes into one single node because all store
2664 // nodes are independent of each other.
2665 if (!MemOpChains.empty())
2666 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
2668 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
2669 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2670 // node so that legalize doesn't hack it.
2672 (Subtarget.isABI_N64() || IsPIC); // true if calls are translated to
2674 bool GlobalOrExternal = false, InternalLinkage = false, IsCallReloc = false;
2676 EVT Ty = Callee.getValueType();
2678 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2680 const GlobalValue *Val = G->getGlobal();
2681 InternalLinkage = Val->hasInternalLinkage();
2683 if (InternalLinkage)
2684 Callee = getAddrLocal(G, Ty, DAG,
2685 Subtarget.isABI_N32() || Subtarget.isABI_N64());
2686 else if (LargeGOT) {
2687 Callee = getAddrGlobalLargeGOT(G, Ty, DAG, MipsII::MO_CALL_HI16,
2688 MipsII::MO_CALL_LO16, Chain,
2689 FuncInfo->callPtrInfo(Val));
2692 Callee = getAddrGlobal(G, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2693 FuncInfo->callPtrInfo(Val));
2697 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
2698 MipsII::MO_NO_FLAG);
2699 GlobalOrExternal = true;
2701 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
2702 const char *Sym = S->getSymbol();
2704 if (!Subtarget.isABI_N64() && !IsPIC) // !N64 && static
2705 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(),
2706 MipsII::MO_NO_FLAG);
2707 else if (LargeGOT) {
2708 Callee = getAddrGlobalLargeGOT(S, Ty, DAG, MipsII::MO_CALL_HI16,
2709 MipsII::MO_CALL_LO16, Chain,
2710 FuncInfo->callPtrInfo(Sym));
2712 } else { // N64 || PIC
2713 Callee = getAddrGlobal(S, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2714 FuncInfo->callPtrInfo(Sym));
2718 GlobalOrExternal = true;
2721 SmallVector<SDValue, 8> Ops(1, Chain);
2722 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2724 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2725 IsCallReloc, CLI, Callee, Chain);
2728 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
2730 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
2731 SDValue InFlag = Chain.getValue(1);
2733 // Create the CALLSEQ_END node.
2734 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
2735 DAG.getIntPtrConstant(0, true), InFlag, DL);
2736 InFlag = Chain.getValue(1);
2738 // Handle result values, copying them out of physregs into vregs that we
2740 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
2744 /// LowerCallResult - Lower the result values of a call into the
2745 /// appropriate copies out of appropriate physical registers.
2746 SDValue MipsTargetLowering::LowerCallResult(
2747 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
2748 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
2749 SmallVectorImpl<SDValue> &InVals,
2750 TargetLowering::CallLoweringInfo &CLI) const {
2751 // Assign locations to each value returned by this call.
2752 SmallVector<CCValAssign, 16> RVLocs;
2753 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2755 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI);
2757 // Copy all of the result registers out of their specified physreg.
2758 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2759 CCValAssign &VA = RVLocs[i];
2760 assert(VA.isRegLoc() && "Can only return in registers!");
2762 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
2763 RVLocs[i].getLocVT(), InFlag);
2764 Chain = Val.getValue(1);
2765 InFlag = Val.getValue(2);
2767 if (VA.isUpperBitsInLoc()) {
2768 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
2769 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2771 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2773 Shift, DL, VA.getLocVT(), Val,
2774 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2777 switch (VA.getLocInfo()) {
2779 llvm_unreachable("Unknown loc info!");
2780 case CCValAssign::Full:
2782 case CCValAssign::BCvt:
2783 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
2785 case CCValAssign::AExt:
2786 case CCValAssign::AExtUpper:
2787 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2789 case CCValAssign::ZExt:
2790 case CCValAssign::ZExtUpper:
2791 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
2792 DAG.getValueType(VA.getValVT()));
2793 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2795 case CCValAssign::SExt:
2796 case CCValAssign::SExtUpper:
2797 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
2798 DAG.getValueType(VA.getValVT()));
2799 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2803 InVals.push_back(Val);
2809 static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA,
2810 EVT ArgVT, SDLoc DL, SelectionDAG &DAG) {
2811 MVT LocVT = VA.getLocVT();
2812 EVT ValVT = VA.getValVT();
2814 // Shift into the upper bits if necessary.
2815 switch (VA.getLocInfo()) {
2818 case CCValAssign::AExtUpper:
2819 case CCValAssign::SExtUpper:
2820 case CCValAssign::ZExtUpper: {
2821 unsigned ValSizeInBits = ArgVT.getSizeInBits();
2822 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2824 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2826 Opcode, DL, VA.getLocVT(), Val,
2827 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2832 // If this is an value smaller than the argument slot size (32-bit for O32,
2833 // 64-bit for N32/N64), it has been promoted in some way to the argument slot
2834 // size. Extract the value and insert any appropriate assertions regarding
2835 // sign/zero extension.
2836 switch (VA.getLocInfo()) {
2838 llvm_unreachable("Unknown loc info!");
2839 case CCValAssign::Full:
2841 case CCValAssign::AExtUpper:
2842 case CCValAssign::AExt:
2843 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2845 case CCValAssign::SExtUpper:
2846 case CCValAssign::SExt:
2847 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT));
2848 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2850 case CCValAssign::ZExtUpper:
2851 case CCValAssign::ZExt:
2852 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT));
2853 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2855 case CCValAssign::BCvt:
2856 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
2863 //===----------------------------------------------------------------------===//
2864 // Formal Arguments Calling Convention Implementation
2865 //===----------------------------------------------------------------------===//
2866 /// LowerFormalArguments - transform physical registers into virtual registers
2867 /// and generate load operations for arguments places on the stack.
2869 MipsTargetLowering::LowerFormalArguments(SDValue Chain,
2870 CallingConv::ID CallConv,
2872 const SmallVectorImpl<ISD::InputArg> &Ins,
2873 SDLoc DL, SelectionDAG &DAG,
2874 SmallVectorImpl<SDValue> &InVals)
2876 MachineFunction &MF = DAG.getMachineFunction();
2877 MachineFrameInfo *MFI = MF.getFrameInfo();
2878 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2880 MipsFI->setVarArgsFrameIndex(0);
2882 // Used with vargs to acumulate store chains.
2883 std::vector<SDValue> OutChains;
2885 // Assign locations to all of the incoming arguments.
2886 SmallVector<CCValAssign, 16> ArgLocs;
2887 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
2889 const MipsABIInfo &ABI = Subtarget.getABI();
2890 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
2891 Function::const_arg_iterator FuncArg =
2892 DAG.getMachineFunction().getFunction()->arg_begin();
2894 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg);
2895 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2896 CCInfo.getInRegsParamsCount() > 0);
2898 unsigned CurArgIdx = 0;
2899 CCInfo.rewindByValRegsInfo();
2901 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2902 CCValAssign &VA = ArgLocs[i];
2903 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2904 CurArgIdx = Ins[i].OrigArgIndex;
2905 EVT ValVT = VA.getValVT();
2906 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2907 bool IsRegLoc = VA.isRegLoc();
2909 if (Flags.isByVal()) {
2910 unsigned FirstByValReg, LastByValReg;
2911 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2912 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2914 assert(Flags.getByValSize() &&
2915 "ByVal args of size 0 should have been ignored by front-end.");
2916 assert(ByValIdx < CCInfo.getInRegsParamsCount());
2917 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
2918 FirstByValReg, LastByValReg, VA, CCInfo);
2919 CCInfo.nextInRegsParam();
2923 // Arguments stored on registers
2925 MVT RegVT = VA.getLocVT();
2926 unsigned ArgReg = VA.getLocReg();
2927 const TargetRegisterClass *RC = getRegClassFor(RegVT);
2929 // Transform the arguments stored on
2930 // physical registers into virtual ones
2931 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2932 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
2934 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
2936 // Handle floating point arguments passed in integer registers and
2937 // long double arguments passed in floating point registers.
2938 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
2939 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2940 (RegVT == MVT::f64 && ValVT == MVT::i64))
2941 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
2942 else if (Subtarget.isABI_O32() && RegVT == MVT::i32 &&
2943 ValVT == MVT::f64) {
2944 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
2945 getNextIntArgReg(ArgReg), RC);
2946 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
2947 if (!Subtarget.isLittle())
2948 std::swap(ArgValue, ArgValue2);
2949 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
2950 ArgValue, ArgValue2);
2953 InVals.push_back(ArgValue);
2954 } else { // VA.isRegLoc()
2955 MVT LocVT = VA.getLocVT();
2957 if (Subtarget.isABI_O32()) {
2958 // We ought to be able to use LocVT directly but O32 sets it to i32
2959 // when allocating floating point values to integer registers.
2960 // This shouldn't influence how we load the value into registers unless
2961 // we are targetting softfloat.
2962 if (VA.getValVT().isFloatingPoint() && !Subtarget.abiUsesSoftFloat())
2963 LocVT = VA.getValVT();
2967 assert(VA.isMemLoc());
2969 // The stack pointer offset is relative to the caller stack frame.
2970 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
2971 VA.getLocMemOffset(), true);
2973 // Create load nodes to retrieve arguments from the stack
2974 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2975 SDValue ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
2976 MachinePointerInfo::getFixedStack(FI),
2977 false, false, false, 0);
2978 OutChains.push_back(ArgValue.getValue(1));
2980 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
2982 InVals.push_back(ArgValue);
2986 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2987 // The mips ABIs for returning structs by value requires that we copy
2988 // the sret argument into $v0 for the return. Save the argument into
2989 // a virtual register so that we can access it from the return points.
2990 if (Ins[i].Flags.isSRet()) {
2991 unsigned Reg = MipsFI->getSRetReturnReg();
2993 Reg = MF.getRegInfo().createVirtualRegister(
2994 getRegClassFor(Subtarget.isABI_N64() ? MVT::i64 : MVT::i32));
2995 MipsFI->setSRetReturnReg(Reg);
2997 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
2998 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
3004 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo);
3006 // All stores are grouped in one node to allow the matching between
3007 // the size of Ins and InVals. This only happens when on varg functions
3008 if (!OutChains.empty()) {
3009 OutChains.push_back(Chain);
3010 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
3016 //===----------------------------------------------------------------------===//
3017 // Return Value Calling Convention Implementation
3018 //===----------------------------------------------------------------------===//
3021 MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
3022 MachineFunction &MF, bool IsVarArg,
3023 const SmallVectorImpl<ISD::OutputArg> &Outs,
3024 LLVMContext &Context) const {
3025 SmallVector<CCValAssign, 16> RVLocs;
3026 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
3027 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3031 MipsTargetLowering::LowerReturn(SDValue Chain,
3032 CallingConv::ID CallConv, bool IsVarArg,
3033 const SmallVectorImpl<ISD::OutputArg> &Outs,
3034 const SmallVectorImpl<SDValue> &OutVals,
3035 SDLoc DL, SelectionDAG &DAG) const {
3036 // CCValAssign - represent the assignment of
3037 // the return value to a location
3038 SmallVector<CCValAssign, 16> RVLocs;
3039 MachineFunction &MF = DAG.getMachineFunction();
3041 // CCState - Info about the registers and stack slot.
3042 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
3044 // Analyze return values.
3045 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
3048 SmallVector<SDValue, 4> RetOps(1, Chain);
3050 // Copy the result values into the output registers.
3051 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3052 SDValue Val = OutVals[i];
3053 CCValAssign &VA = RVLocs[i];
3054 assert(VA.isRegLoc() && "Can only return in registers!");
3055 bool UseUpperBits = false;
3057 switch (VA.getLocInfo()) {
3059 llvm_unreachable("Unknown loc info!");
3060 case CCValAssign::Full:
3062 case CCValAssign::BCvt:
3063 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3065 case CCValAssign::AExtUpper:
3066 UseUpperBits = true;
3068 case CCValAssign::AExt:
3069 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3071 case CCValAssign::ZExtUpper:
3072 UseUpperBits = true;
3074 case CCValAssign::ZExt:
3075 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3077 case CCValAssign::SExtUpper:
3078 UseUpperBits = true;
3080 case CCValAssign::SExt:
3081 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3086 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3087 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3089 ISD::SHL, DL, VA.getLocVT(), Val,
3090 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
3093 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
3095 // Guarantee that all emitted copies are stuck together with flags.
3096 Flag = Chain.getValue(1);
3097 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
3100 // The mips ABIs for returning structs by value requires that we copy
3101 // the sret argument into $v0 for the return. We saved the argument into
3102 // a virtual register in the entry block, so now we copy the value out
3104 if (MF.getFunction()->hasStructRetAttr()) {
3105 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3106 unsigned Reg = MipsFI->getSRetReturnReg();
3109 llvm_unreachable("sret virtual register not created in the entry block");
3110 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
3111 unsigned V0 = Subtarget.isABI_N64() ? Mips::V0_64 : Mips::V0;
3113 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
3114 Flag = Chain.getValue(1);
3115 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
3118 RetOps[0] = Chain; // Update chain.
3120 // Add the flag if we have it.
3122 RetOps.push_back(Flag);
3124 // Return on Mips is always a "jr $ra"
3125 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
3128 //===----------------------------------------------------------------------===//
3129 // Mips Inline Assembly Support
3130 //===----------------------------------------------------------------------===//
3132 /// getConstraintType - Given a constraint letter, return the type of
3133 /// constraint it is for this target.
3134 MipsTargetLowering::ConstraintType MipsTargetLowering::
3135 getConstraintType(const std::string &Constraint) const
3137 // Mips specific constraints
3138 // GCC config/mips/constraints.md
3140 // 'd' : An address register. Equivalent to r
3141 // unless generating MIPS16 code.
3142 // 'y' : Equivalent to r; retained for
3143 // backwards compatibility.
3144 // 'c' : A register suitable for use in an indirect
3145 // jump. This will always be $25 for -mabicalls.
3146 // 'l' : The lo register. 1 word storage.
3147 // 'x' : The hilo register pair. Double word storage.
3148 if (Constraint.size() == 1) {
3149 switch (Constraint[0]) {
3157 return C_RegisterClass;
3162 return TargetLowering::getConstraintType(Constraint);
3165 /// Examine constraint type and operand type and determine a weight value.
3166 /// This object must already have been set up with the operand type
3167 /// and the current alternative constraint selected.
3168 TargetLowering::ConstraintWeight
3169 MipsTargetLowering::getSingleConstraintMatchWeight(
3170 AsmOperandInfo &info, const char *constraint) const {
3171 ConstraintWeight weight = CW_Invalid;
3172 Value *CallOperandVal = info.CallOperandVal;
3173 // If we don't have a value, we can't do a match,
3174 // but allow it at the lowest weight.
3175 if (!CallOperandVal)
3177 Type *type = CallOperandVal->getType();
3178 // Look at the constraint type.
3179 switch (*constraint) {
3181 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3185 if (type->isIntegerTy())
3186 weight = CW_Register;
3188 case 'f': // FPU or MSA register
3189 if (Subtarget.hasMSA() && type->isVectorTy() &&
3190 cast<VectorType>(type)->getBitWidth() == 128)
3191 weight = CW_Register;
3192 else if (type->isFloatTy())
3193 weight = CW_Register;
3195 case 'c': // $25 for indirect jumps
3196 case 'l': // lo register
3197 case 'x': // hilo register pair
3198 if (type->isIntegerTy())
3199 weight = CW_SpecificReg;
3201 case 'I': // signed 16 bit immediate
3202 case 'J': // integer zero
3203 case 'K': // unsigned 16 bit immediate
3204 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3205 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3206 case 'O': // signed 15 bit immediate (+- 16383)
3207 case 'P': // immediate in the range of 65535 to 1 (inclusive)
3208 if (isa<ConstantInt>(CallOperandVal))
3209 weight = CW_Constant;
3218 /// This is a helper function to parse a physical register string and split it
3219 /// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
3220 /// that is returned indicates whether parsing was successful. The second flag
3221 /// is true if the numeric part exists.
3222 static std::pair<bool, bool>
3223 parsePhysicalReg(StringRef C, std::string &Prefix,
3224 unsigned long long &Reg) {
3225 if (C.front() != '{' || C.back() != '}')
3226 return std::make_pair(false, false);
3228 // Search for the first numeric character.
3229 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
3230 I = std::find_if(B, E, std::ptr_fun(isdigit));
3232 Prefix.assign(B, I - B);
3234 // The second flag is set to false if no numeric characters were found.
3236 return std::make_pair(true, false);
3238 // Parse the numeric characters.
3239 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
3243 std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
3244 parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
3245 const TargetRegisterInfo *TRI =
3246 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
3247 const TargetRegisterClass *RC;
3249 unsigned long long Reg;
3251 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
3254 return std::make_pair(0U, nullptr);
3256 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
3257 // No numeric characters follow "hi" or "lo".
3259 return std::make_pair(0U, nullptr);
3261 RC = TRI->getRegClass(Prefix == "hi" ?
3262 Mips::HI32RegClassID : Mips::LO32RegClassID);
3263 return std::make_pair(*(RC->begin()), RC);
3264 } else if (Prefix.compare(0, 4, "$msa") == 0) {
3265 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3267 // No numeric characters follow the name.
3269 return std::make_pair(0U, nullptr);
3271 Reg = StringSwitch<unsigned long long>(Prefix)
3272 .Case("$msair", Mips::MSAIR)
3273 .Case("$msacsr", Mips::MSACSR)
3274 .Case("$msaaccess", Mips::MSAAccess)
3275 .Case("$msasave", Mips::MSASave)
3276 .Case("$msamodify", Mips::MSAModify)
3277 .Case("$msarequest", Mips::MSARequest)
3278 .Case("$msamap", Mips::MSAMap)
3279 .Case("$msaunmap", Mips::MSAUnmap)
3283 return std::make_pair(0U, nullptr);
3285 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3286 return std::make_pair(Reg, RC);
3290 return std::make_pair(0U, nullptr);
3292 if (Prefix == "$f") { // Parse $f0-$f31.
3293 // If the size of FP registers is 64-bit or Reg is an even number, select
3294 // the 64-bit register class. Otherwise, select the 32-bit register class.
3295 if (VT == MVT::Other)
3296 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
3298 RC = getRegClassFor(VT);
3300 if (RC == &Mips::AFGR64RegClass) {
3301 assert(Reg % 2 == 0);
3304 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
3305 RC = TRI->getRegClass(Mips::FCCRegClassID);
3306 else if (Prefix == "$w") { // Parse $w0-$w31.
3307 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
3308 } else { // Parse $0-$31.
3309 assert(Prefix == "$");
3310 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3313 assert(Reg < RC->getNumRegs());
3314 return std::make_pair(*(RC->begin() + Reg), RC);
3317 /// Given a register class constraint, like 'r', if this corresponds directly
3318 /// to an LLVM register class, return a register of 0 and the register class
3320 std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
3321 getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
3323 if (Constraint.size() == 1) {
3324 switch (Constraint[0]) {
3325 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3326 case 'y': // Same as 'r'. Exists for compatibility.
3328 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
3329 if (Subtarget.inMips16Mode())
3330 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
3331 return std::make_pair(0U, &Mips::GPR32RegClass);
3333 if (VT == MVT::i64 && !Subtarget.isGP64bit())
3334 return std::make_pair(0U, &Mips::GPR32RegClass);
3335 if (VT == MVT::i64 && Subtarget.isGP64bit())
3336 return std::make_pair(0U, &Mips::GPR64RegClass);
3337 // This will generate an error message
3338 return std::make_pair(0U, nullptr);
3339 case 'f': // FPU or MSA register
3340 if (VT == MVT::v16i8)
3341 return std::make_pair(0U, &Mips::MSA128BRegClass);
3342 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3343 return std::make_pair(0U, &Mips::MSA128HRegClass);
3344 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3345 return std::make_pair(0U, &Mips::MSA128WRegClass);
3346 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3347 return std::make_pair(0U, &Mips::MSA128DRegClass);
3348 else if (VT == MVT::f32)
3349 return std::make_pair(0U, &Mips::FGR32RegClass);
3350 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
3351 if (Subtarget.isFP64bit())
3352 return std::make_pair(0U, &Mips::FGR64RegClass);
3353 return std::make_pair(0U, &Mips::AFGR64RegClass);
3356 case 'c': // register suitable for indirect jump
3358 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
3359 assert(VT == MVT::i64 && "Unexpected type.");
3360 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
3361 case 'l': // register suitable for indirect jump
3363 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3364 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
3365 case 'x': // register suitable for indirect jump
3366 // Fixme: Not triggering the use of both hi and low
3367 // This will generate an error message
3368 return std::make_pair(0U, nullptr);
3372 std::pair<unsigned, const TargetRegisterClass *> R;
3373 R = parseRegForInlineAsmConstraint(Constraint, VT);
3378 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3381 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3382 /// vector. If it is invalid, don't add anything to Ops.
3383 void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3384 std::string &Constraint,
3385 std::vector<SDValue>&Ops,
3386 SelectionDAG &DAG) const {
3389 // Only support length 1 constraints for now.
3390 if (Constraint.length() > 1) return;
3392 char ConstraintLetter = Constraint[0];
3393 switch (ConstraintLetter) {
3394 default: break; // This will fall through to the generic implementation
3395 case 'I': // Signed 16 bit constant
3396 // If this fails, the parent routine will give an error
3397 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3398 EVT Type = Op.getValueType();
3399 int64_t Val = C->getSExtValue();
3400 if (isInt<16>(Val)) {
3401 Result = DAG.getTargetConstant(Val, Type);
3406 case 'J': // integer zero
3407 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3408 EVT Type = Op.getValueType();
3409 int64_t Val = C->getZExtValue();
3411 Result = DAG.getTargetConstant(0, Type);
3416 case 'K': // unsigned 16 bit immediate
3417 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3418 EVT Type = Op.getValueType();
3419 uint64_t Val = (uint64_t)C->getZExtValue();
3420 if (isUInt<16>(Val)) {
3421 Result = DAG.getTargetConstant(Val, Type);
3426 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3427 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3428 EVT Type = Op.getValueType();
3429 int64_t Val = C->getSExtValue();
3430 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3431 Result = DAG.getTargetConstant(Val, Type);
3436 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3437 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3438 EVT Type = Op.getValueType();
3439 int64_t Val = C->getSExtValue();
3440 if ((Val >= -65535) && (Val <= -1)) {
3441 Result = DAG.getTargetConstant(Val, Type);
3446 case 'O': // signed 15 bit immediate
3447 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3448 EVT Type = Op.getValueType();
3449 int64_t Val = C->getSExtValue();
3450 if ((isInt<15>(Val))) {
3451 Result = DAG.getTargetConstant(Val, Type);
3456 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3457 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3458 EVT Type = Op.getValueType();
3459 int64_t Val = C->getSExtValue();
3460 if ((Val <= 65535) && (Val >= 1)) {
3461 Result = DAG.getTargetConstant(Val, Type);
3468 if (Result.getNode()) {
3469 Ops.push_back(Result);
3473 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3476 bool MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM,
3478 // No global is ever allowed as a base.
3483 case 0: // "r+i" or just "i", depending on HasBaseReg.
3486 if (!AM.HasBaseReg) // allow "r+i".
3488 return false; // disallow "r+r" or "r+r+i".
3497 MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3498 // The Mips target isn't yet aware of offsets.
3502 EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
3504 bool IsMemset, bool ZeroMemset,
3506 MachineFunction &MF) const {
3507 if (Subtarget.hasMips64())
3513 bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3514 if (VT != MVT::f32 && VT != MVT::f64)
3516 if (Imm.isNegZero())
3518 return Imm.isZero();
3521 unsigned MipsTargetLowering::getJumpTableEncoding() const {
3522 if (Subtarget.isABI_N64())
3523 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
3525 return TargetLowering::getJumpTableEncoding();
3528 void MipsTargetLowering::copyByValRegs(
3529 SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains, SelectionDAG &DAG,
3530 const ISD::ArgFlagsTy &Flags, SmallVectorImpl<SDValue> &InVals,
3531 const Argument *FuncArg, unsigned FirstReg, unsigned LastReg,
3532 const CCValAssign &VA, MipsCCState &State) const {
3533 MachineFunction &MF = DAG.getMachineFunction();
3534 MachineFrameInfo *MFI = MF.getFrameInfo();
3535 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
3536 unsigned NumRegs = LastReg - FirstReg;
3537 unsigned RegAreaSize = NumRegs * GPRSizeInBytes;
3538 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3540 const MipsABIInfo &ABI = Subtarget.getABI();
3541 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs();
3545 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3546 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes);
3548 FrameObjOffset = VA.getLocMemOffset();
3550 // Create frame object.
3551 EVT PtrTy = getPointerTy();
3552 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3553 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3554 InVals.push_back(FIN);
3559 // Copy arg registers.
3560 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
3561 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3563 for (unsigned I = 0; I < NumRegs; ++I) {
3564 unsigned ArgReg = ByValArgRegs[FirstReg + I];
3565 unsigned VReg = addLiveIn(MF, ArgReg, RC);
3566 unsigned Offset = I * GPRSizeInBytes;
3567 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3568 DAG.getConstant(Offset, PtrTy));
3569 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3570 StorePtr, MachinePointerInfo(FuncArg, Offset),
3572 OutChains.push_back(Store);
3576 // Copy byVal arg to registers and stack.
3577 void MipsTargetLowering::passByValArg(
3578 SDValue Chain, SDLoc DL,
3579 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
3580 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
3581 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg,
3582 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle,
3583 const CCValAssign &VA) const {
3584 unsigned ByValSizeInBytes = Flags.getByValSize();
3585 unsigned OffsetInBytes = 0; // From beginning of struct
3586 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3587 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
3588 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
3589 unsigned NumRegs = LastReg - FirstReg;
3592 const ArrayRef<MCPhysReg> ArgRegs = Subtarget.getABI().GetByValArgRegs();
3593 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes);
3596 // Copy words to registers.
3597 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) {
3598 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3599 DAG.getConstant(OffsetInBytes, PtrTy));
3600 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3601 MachinePointerInfo(), false, false, false,
3603 MemOpChains.push_back(LoadVal.getValue(1));
3604 unsigned ArgReg = ArgRegs[FirstReg + I];
3605 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3608 // Return if the struct has been fully copied.
3609 if (ByValSizeInBytes == OffsetInBytes)
3612 // Copy the remainder of the byval argument with sub-word loads and shifts.
3613 if (LeftoverBytes) {
3616 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
3617 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
3618 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
3620 if (RemainingSizeInBytes < LoadSizeInBytes)
3624 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3625 DAG.getConstant(OffsetInBytes, PtrTy));
3626 SDValue LoadVal = DAG.getExtLoad(
3627 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
3628 MVT::getIntegerVT(LoadSizeInBytes * 8), false, false, false,
3630 MemOpChains.push_back(LoadVal.getValue(1));
3632 // Shift the loaded value.
3636 Shamt = TotalBytesLoaded * 8;
3638 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
3640 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3641 DAG.getConstant(Shamt, MVT::i32));
3644 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3648 OffsetInBytes += LoadSizeInBytes;
3649 TotalBytesLoaded += LoadSizeInBytes;
3650 Alignment = std::min(Alignment, LoadSizeInBytes);
3653 unsigned ArgReg = ArgRegs[FirstReg + I];
3654 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3659 // Copy remainder of byval arg to it with memcpy.
3660 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
3661 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3662 DAG.getConstant(OffsetInBytes, PtrTy));
3663 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3664 DAG.getIntPtrConstant(VA.getLocMemOffset()));
3665 Chain = DAG.getMemcpy(Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, PtrTy),
3666 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
3667 MachinePointerInfo(), MachinePointerInfo());
3668 MemOpChains.push_back(Chain);
3671 void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3672 SDValue Chain, SDLoc DL,
3674 CCState &State) const {
3675 const ArrayRef<MCPhysReg> ArgRegs = Subtarget.getABI().GetVarArgRegs();
3676 unsigned Idx = State.getFirstUnallocated(ArgRegs.data(), ArgRegs.size());
3677 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3678 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
3679 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3680 MachineFunction &MF = DAG.getMachineFunction();
3681 MachineFrameInfo *MFI = MF.getFrameInfo();
3682 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3684 // Offset of the first variable argument from stack pointer.
3687 if (ArgRegs.size() == Idx)
3689 RoundUpToAlignment(State.getNextStackOffset(), RegSizeInBytes);
3691 const MipsABIInfo &ABI = Subtarget.getABI();
3693 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3694 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
3697 // Record the frame index of the first variable argument
3698 // which is a value necessary to VASTART.
3699 int FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
3700 MipsFI->setVarArgsFrameIndex(FI);
3702 // Copy the integer registers that have not been used for argument passing
3703 // to the argument register save area. For O32, the save area is allocated
3704 // in the caller's stack frame, while for N32/64, it is allocated in the
3705 // callee's stack frame.
3706 for (unsigned I = Idx; I < ArgRegs.size();
3707 ++I, VaArgOffset += RegSizeInBytes) {
3708 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
3709 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3710 FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
3711 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3712 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3713 MachinePointerInfo(), false, false, 0);
3714 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
3716 OutChains.push_back(Store);
3720 void MipsTargetLowering::HandleByVal(CCState *State, unsigned &Size,
3721 unsigned Align) const {
3722 MachineFunction &MF = State->getMachineFunction();
3723 const TargetFrameLowering *TFL = MF.getSubtarget().getFrameLowering();
3725 assert(Size && "Byval argument's size shouldn't be 0.");
3727 Align = std::min(Align, TFL->getStackAlignment());
3729 unsigned FirstReg = 0;
3730 unsigned NumRegs = 0;
3732 if (State->getCallingConv() != CallingConv::Fast) {
3733 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3734 const ArrayRef<MCPhysReg> IntArgRegs = Subtarget.getABI().GetByValArgRegs();
3735 // FIXME: The O32 case actually describes no shadow registers.
3736 const MCPhysReg *ShadowRegs =
3737 Subtarget.isABI_O32() ? IntArgRegs.data() : Mips64DPRegs;
3739 // We used to check the size as well but we can't do that anymore since
3740 // CCState::HandleByVal() rounds up the size after calling this function.
3741 assert(!(Align % RegSizeInBytes) &&
3742 "Byval argument's alignment should be a multiple of"
3745 FirstReg = State->getFirstUnallocated(IntArgRegs.data(), IntArgRegs.size());
3747 // If Align > RegSizeInBytes, the first arg register must be even.
3748 // FIXME: This condition happens to do the right thing but it's not the
3749 // right way to test it. We want to check that the stack frame offset
3750 // of the register is aligned.
3751 if ((Align > RegSizeInBytes) && (FirstReg % 2)) {
3752 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]);
3756 // Mark the registers allocated.
3757 Size = RoundUpToAlignment(Size, RegSizeInBytes);
3758 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size());
3759 Size -= RegSizeInBytes, ++I, ++NumRegs)
3760 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3763 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs);
3767 MipsTargetLowering::emitPseudoSELECT(MachineInstr *MI, MachineBasicBlock *BB,
3768 bool isFPCmp, unsigned Opc) const {
3769 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
3770 "Subtarget already supports SELECT nodes with the use of"
3771 "conditional-move instructions.");
3773 const TargetInstrInfo *TII =
3774 getTargetMachine().getSubtargetImpl()->getInstrInfo();
3775 DebugLoc DL = MI->getDebugLoc();
3777 // To "insert" a SELECT instruction, we actually have to insert the
3778 // diamond control-flow pattern. The incoming instruction knows the
3779 // destination vreg to set, the condition code register to branch on, the
3780 // true/false values to select between, and a branch opcode to use.
3781 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3782 MachineFunction::iterator It = BB;
3789 // bNE r1, r0, copy1MBB
3790 // fallthrough --> copy0MBB
3791 MachineBasicBlock *thisMBB = BB;
3792 MachineFunction *F = BB->getParent();
3793 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3794 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
3795 F->insert(It, copy0MBB);
3796 F->insert(It, sinkMBB);
3798 // Transfer the remainder of BB and its successor edges to sinkMBB.
3799 sinkMBB->splice(sinkMBB->begin(), BB,
3800 std::next(MachineBasicBlock::iterator(MI)), BB->end());
3801 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3803 // Next, add the true and fallthrough blocks as its successors.
3804 BB->addSuccessor(copy0MBB);
3805 BB->addSuccessor(sinkMBB);
3808 // bc1[tf] cc, sinkMBB
3809 BuildMI(BB, DL, TII->get(Opc))
3810 .addReg(MI->getOperand(1).getReg())
3813 // bne rs, $0, sinkMBB
3814 BuildMI(BB, DL, TII->get(Opc))
3815 .addReg(MI->getOperand(1).getReg())
3821 // %FalseValue = ...
3822 // # fallthrough to sinkMBB
3825 // Update machine-CFG edges
3826 BB->addSuccessor(sinkMBB);
3829 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
3833 BuildMI(*BB, BB->begin(), DL,
3834 TII->get(Mips::PHI), MI->getOperand(0).getReg())
3835 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
3836 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB);
3838 MI->eraseFromParent(); // The pseudo instruction is gone now.