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 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "mips-lower"
16 #include "MipsISelLowering.h"
17 #include "MipsMachineFunction.h"
18 #include "MipsTargetMachine.h"
19 #include "MipsTargetObjectFile.h"
20 #include "MipsSubtarget.h"
21 #include "InstPrinter/MipsInstPrinter.h"
22 #include "MCTargetDesc/MipsBaseInfo.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Function.h"
25 #include "llvm/GlobalVariable.h"
26 #include "llvm/Intrinsics.h"
27 #include "llvm/CallingConv.h"
28 #include "llvm/ADT/Statistic.h"
29 #include "llvm/CodeGen/CallingConvLower.h"
30 #include "llvm/CodeGen/MachineFrameInfo.h"
31 #include "llvm/CodeGen/MachineFunction.h"
32 #include "llvm/CodeGen/MachineInstrBuilder.h"
33 #include "llvm/CodeGen/MachineRegisterInfo.h"
34 #include "llvm/CodeGen/SelectionDAGISel.h"
35 #include "llvm/CodeGen/ValueTypes.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/raw_ostream.h"
43 STATISTIC(NumTailCalls, "Number of tail calls");
46 EnableMipsTailCalls("enable-mips-tail-calls", cl::Hidden,
47 cl::desc("MIPS: Enable tail calls."), cl::init(false));
49 static const uint16_t O32IntRegs[4] = {
50 Mips::A0, Mips::A1, Mips::A2, Mips::A3
53 static const uint16_t Mips64IntRegs[8] = {
54 Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
55 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64
58 static const uint16_t Mips64DPRegs[8] = {
59 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
60 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
63 // If I is a shifted mask, set the size (Size) and the first bit of the
64 // mask (Pos), and return true.
65 // For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
66 static bool IsShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
67 if (!isShiftedMask_64(I))
70 Size = CountPopulation_64(I);
71 Pos = CountTrailingZeros_64(I);
75 static SDValue GetGlobalReg(SelectionDAG &DAG, EVT Ty) {
76 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
77 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
80 const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
82 case MipsISD::JmpLink: return "MipsISD::JmpLink";
83 case MipsISD::TailCall: return "MipsISD::TailCall";
84 case MipsISD::Hi: return "MipsISD::Hi";
85 case MipsISD::Lo: return "MipsISD::Lo";
86 case MipsISD::GPRel: return "MipsISD::GPRel";
87 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
88 case MipsISD::Ret: return "MipsISD::Ret";
89 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
90 case MipsISD::FPCmp: return "MipsISD::FPCmp";
91 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
92 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
93 case MipsISD::FPRound: return "MipsISD::FPRound";
94 case MipsISD::MAdd: return "MipsISD::MAdd";
95 case MipsISD::MAddu: return "MipsISD::MAddu";
96 case MipsISD::MSub: return "MipsISD::MSub";
97 case MipsISD::MSubu: return "MipsISD::MSubu";
98 case MipsISD::DivRem: return "MipsISD::DivRem";
99 case MipsISD::DivRemU: return "MipsISD::DivRemU";
100 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
101 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
102 case MipsISD::Wrapper: return "MipsISD::Wrapper";
103 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
104 case MipsISD::Sync: return "MipsISD::Sync";
105 case MipsISD::Ext: return "MipsISD::Ext";
106 case MipsISD::Ins: return "MipsISD::Ins";
107 case MipsISD::LWL: return "MipsISD::LWL";
108 case MipsISD::LWR: return "MipsISD::LWR";
109 case MipsISD::SWL: return "MipsISD::SWL";
110 case MipsISD::SWR: return "MipsISD::SWR";
111 case MipsISD::LDL: return "MipsISD::LDL";
112 case MipsISD::LDR: return "MipsISD::LDR";
113 case MipsISD::SDL: return "MipsISD::SDL";
114 case MipsISD::SDR: return "MipsISD::SDR";
115 case MipsISD::EXTP: return "MipsISD::EXTP";
116 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
117 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
118 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
119 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
120 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
121 case MipsISD::SHILO: return "MipsISD::SHILO";
122 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
123 case MipsISD::MULT: return "MipsISD::MULT";
124 case MipsISD::MULTU: return "MipsISD::MULTU";
125 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSPDSP";
126 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
127 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
128 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
129 default: return NULL;
134 MipsTargetLowering(MipsTargetMachine &TM)
135 : TargetLowering(TM, new MipsTargetObjectFile()),
136 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
137 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
138 IsO32(Subtarget->isABI_O32()) {
140 // Mips does not have i1 type, so use i32 for
141 // setcc operations results (slt, sgt, ...).
142 setBooleanContents(ZeroOrOneBooleanContent);
143 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
145 // Set up the register classes
146 addRegisterClass(MVT::i32, &Mips::CPURegsRegClass);
149 addRegisterClass(MVT::i64, &Mips::CPU64RegsRegClass);
151 if (Subtarget->inMips16Mode()) {
152 addRegisterClass(MVT::i32, &Mips::CPU16RegsRegClass);
155 if (Subtarget->hasDSP()) {
156 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
158 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
159 addRegisterClass(VecTys[i], &Mips::DSPRegsRegClass);
161 // Expand all builtin opcodes.
162 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
163 setOperationAction(Opc, VecTys[i], Expand);
165 setOperationAction(ISD::LOAD, VecTys[i], Legal);
166 setOperationAction(ISD::STORE, VecTys[i], Legal);
167 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
171 if (!TM.Options.UseSoftFloat) {
172 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
174 // When dealing with single precision only, use libcalls
175 if (!Subtarget->isSingleFloat()) {
177 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
179 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
183 // Load extented operations for i1 types must be promoted
184 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
185 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
186 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
188 // MIPS doesn't have extending float->double load/store
189 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
190 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
192 // Used by legalize types to correctly generate the setcc result.
193 // Without this, every float setcc comes with a AND/OR with the result,
194 // we don't want this, since the fpcmp result goes to a flag register,
195 // which is used implicitly by brcond and select operations.
196 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
198 // Mips Custom Operations
199 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
200 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
201 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
202 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
203 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
204 setOperationAction(ISD::SELECT, MVT::f32, Custom);
205 setOperationAction(ISD::SELECT, MVT::f64, Custom);
206 setOperationAction(ISD::SELECT, MVT::i32, Custom);
207 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
208 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
209 setOperationAction(ISD::SETCC, MVT::f32, Custom);
210 setOperationAction(ISD::SETCC, MVT::f64, Custom);
211 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
212 setOperationAction(ISD::VASTART, MVT::Other, Custom);
213 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
214 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
215 if (Subtarget->inMips16Mode()) {
216 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
217 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
220 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
221 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
223 if (!Subtarget->inMips16Mode()) {
224 setOperationAction(ISD::LOAD, MVT::i32, Custom);
225 setOperationAction(ISD::STORE, MVT::i32, Custom);
228 if (!TM.Options.NoNaNsFPMath) {
229 setOperationAction(ISD::FABS, MVT::f32, Custom);
230 setOperationAction(ISD::FABS, MVT::f64, Custom);
234 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
235 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
236 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
237 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
238 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
239 setOperationAction(ISD::SELECT, MVT::i64, Custom);
240 setOperationAction(ISD::LOAD, MVT::i64, Custom);
241 setOperationAction(ISD::STORE, MVT::i64, Custom);
245 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
246 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
247 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
250 setOperationAction(ISD::ADD, MVT::i32, Custom);
252 setOperationAction(ISD::ADD, MVT::i64, Custom);
254 setOperationAction(ISD::SDIV, MVT::i32, Expand);
255 setOperationAction(ISD::SREM, MVT::i32, Expand);
256 setOperationAction(ISD::UDIV, MVT::i32, Expand);
257 setOperationAction(ISD::UREM, MVT::i32, Expand);
258 setOperationAction(ISD::SDIV, MVT::i64, Expand);
259 setOperationAction(ISD::SREM, MVT::i64, Expand);
260 setOperationAction(ISD::UDIV, MVT::i64, Expand);
261 setOperationAction(ISD::UREM, MVT::i64, Expand);
263 // Operations not directly supported by Mips.
264 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
265 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
266 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
267 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
268 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
269 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
270 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
271 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
272 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
273 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
274 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
275 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
276 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
277 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
278 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
279 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
280 setOperationAction(ISD::ROTL, MVT::i32, Expand);
281 setOperationAction(ISD::ROTL, MVT::i64, Expand);
282 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
283 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
285 if (!Subtarget->hasMips32r2())
286 setOperationAction(ISD::ROTR, MVT::i32, Expand);
288 if (!Subtarget->hasMips64r2())
289 setOperationAction(ISD::ROTR, MVT::i64, Expand);
291 setOperationAction(ISD::FSIN, MVT::f32, Expand);
292 setOperationAction(ISD::FSIN, MVT::f64, Expand);
293 setOperationAction(ISD::FCOS, MVT::f32, Expand);
294 setOperationAction(ISD::FCOS, MVT::f64, Expand);
295 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
296 setOperationAction(ISD::FPOW, MVT::f32, Expand);
297 setOperationAction(ISD::FPOW, MVT::f64, Expand);
298 setOperationAction(ISD::FLOG, MVT::f32, Expand);
299 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
300 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
301 setOperationAction(ISD::FEXP, MVT::f32, Expand);
302 setOperationAction(ISD::FMA, MVT::f32, Expand);
303 setOperationAction(ISD::FMA, MVT::f64, Expand);
304 setOperationAction(ISD::FREM, MVT::f32, Expand);
305 setOperationAction(ISD::FREM, MVT::f64, Expand);
307 if (!TM.Options.NoNaNsFPMath) {
308 setOperationAction(ISD::FNEG, MVT::f32, Expand);
309 setOperationAction(ISD::FNEG, MVT::f64, Expand);
312 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
313 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
314 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
315 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
317 setOperationAction(ISD::VAARG, MVT::Other, Expand);
318 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
319 setOperationAction(ISD::VAEND, MVT::Other, Expand);
321 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
322 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
324 // Use the default for now
325 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
326 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
328 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
329 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
330 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
331 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
333 if (Subtarget->inMips16Mode()) {
334 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
335 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
336 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
337 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
338 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
339 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
340 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
341 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
342 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
343 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
344 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
345 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
348 setInsertFencesForAtomic(true);
350 if (!Subtarget->hasSEInReg()) {
351 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
352 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
355 if (!Subtarget->hasBitCount()) {
356 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
357 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
360 if (!Subtarget->hasSwap()) {
361 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
362 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
366 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
367 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
368 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
369 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
372 setTargetDAGCombine(ISD::ADDE);
373 setTargetDAGCombine(ISD::SUBE);
374 setTargetDAGCombine(ISD::SDIVREM);
375 setTargetDAGCombine(ISD::UDIVREM);
376 setTargetDAGCombine(ISD::SELECT);
377 setTargetDAGCombine(ISD::AND);
378 setTargetDAGCombine(ISD::OR);
379 setTargetDAGCombine(ISD::ADD);
381 setMinFunctionAlignment(HasMips64 ? 3 : 2);
383 setStackPointerRegisterToSaveRestore(IsN64 ? Mips::SP_64 : Mips::SP);
384 computeRegisterProperties();
386 setExceptionPointerRegister(IsN64 ? Mips::A0_64 : Mips::A0);
387 setExceptionSelectorRegister(IsN64 ? Mips::A1_64 : Mips::A1);
389 maxStoresPerMemcpy = 16;
392 bool MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
393 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
395 if (Subtarget->inMips16Mode())
407 EVT MipsTargetLowering::getSetCCResultType(EVT VT) const {
412 // Transforms a subgraph in CurDAG if the following pattern is found:
413 // (addc multLo, Lo0), (adde multHi, Hi0),
415 // multHi/Lo: product of multiplication
416 // Lo0: initial value of Lo register
417 // Hi0: initial value of Hi register
418 // Return true if pattern matching was successful.
419 static bool SelectMadd(SDNode *ADDENode, SelectionDAG *CurDAG) {
420 // ADDENode's second operand must be a flag output of an ADDC node in order
421 // for the matching to be successful.
422 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
424 if (ADDCNode->getOpcode() != ISD::ADDC)
427 SDValue MultHi = ADDENode->getOperand(0);
428 SDValue MultLo = ADDCNode->getOperand(0);
429 SDNode *MultNode = MultHi.getNode();
430 unsigned MultOpc = MultHi.getOpcode();
432 // MultHi and MultLo must be generated by the same node,
433 if (MultLo.getNode() != MultNode)
436 // and it must be a multiplication.
437 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
440 // MultLo amd MultHi must be the first and second output of MultNode
442 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
445 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
446 // of the values of MultNode, in which case MultNode will be removed in later
448 // If there exist users other than ADDENode or ADDCNode, this function returns
449 // here, which will result in MultNode being mapped to a single MULT
450 // instruction node rather than a pair of MULT and MADD instructions being
452 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
455 SDValue Chain = CurDAG->getEntryNode();
456 DebugLoc dl = ADDENode->getDebugLoc();
458 // create MipsMAdd(u) node
459 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
461 SDValue MAdd = CurDAG->getNode(MultOpc, dl, MVT::Glue,
462 MultNode->getOperand(0),// Factor 0
463 MultNode->getOperand(1),// Factor 1
464 ADDCNode->getOperand(1),// Lo0
465 ADDENode->getOperand(1));// Hi0
467 // create CopyFromReg nodes
468 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
470 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
472 CopyFromLo.getValue(2));
474 // replace uses of adde and addc here
475 if (!SDValue(ADDCNode, 0).use_empty())
476 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
478 if (!SDValue(ADDENode, 0).use_empty())
479 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
485 // Transforms a subgraph in CurDAG if the following pattern is found:
486 // (addc Lo0, multLo), (sube Hi0, multHi),
488 // multHi/Lo: product of multiplication
489 // Lo0: initial value of Lo register
490 // Hi0: initial value of Hi register
491 // Return true if pattern matching was successful.
492 static bool SelectMsub(SDNode *SUBENode, SelectionDAG *CurDAG) {
493 // SUBENode's second operand must be a flag output of an SUBC node in order
494 // for the matching to be successful.
495 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
497 if (SUBCNode->getOpcode() != ISD::SUBC)
500 SDValue MultHi = SUBENode->getOperand(1);
501 SDValue MultLo = SUBCNode->getOperand(1);
502 SDNode *MultNode = MultHi.getNode();
503 unsigned MultOpc = MultHi.getOpcode();
505 // MultHi and MultLo must be generated by the same node,
506 if (MultLo.getNode() != MultNode)
509 // and it must be a multiplication.
510 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
513 // MultLo amd MultHi must be the first and second output of MultNode
515 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
518 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
519 // of the values of MultNode, in which case MultNode will be removed in later
521 // If there exist users other than SUBENode or SUBCNode, this function returns
522 // here, which will result in MultNode being mapped to a single MULT
523 // instruction node rather than a pair of MULT and MSUB instructions being
525 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
528 SDValue Chain = CurDAG->getEntryNode();
529 DebugLoc dl = SUBENode->getDebugLoc();
531 // create MipsSub(u) node
532 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
534 SDValue MSub = CurDAG->getNode(MultOpc, dl, MVT::Glue,
535 MultNode->getOperand(0),// Factor 0
536 MultNode->getOperand(1),// Factor 1
537 SUBCNode->getOperand(0),// Lo0
538 SUBENode->getOperand(0));// Hi0
540 // create CopyFromReg nodes
541 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
543 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
545 CopyFromLo.getValue(2));
547 // replace uses of sube and subc here
548 if (!SDValue(SUBCNode, 0).use_empty())
549 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
551 if (!SDValue(SUBENode, 0).use_empty())
552 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
557 static SDValue PerformADDECombine(SDNode *N, SelectionDAG &DAG,
558 TargetLowering::DAGCombinerInfo &DCI,
559 const MipsSubtarget *Subtarget) {
560 if (DCI.isBeforeLegalize())
563 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
565 return SDValue(N, 0);
570 static SDValue PerformSUBECombine(SDNode *N, SelectionDAG &DAG,
571 TargetLowering::DAGCombinerInfo &DCI,
572 const MipsSubtarget *Subtarget) {
573 if (DCI.isBeforeLegalize())
576 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
578 return SDValue(N, 0);
583 static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG &DAG,
584 TargetLowering::DAGCombinerInfo &DCI,
585 const MipsSubtarget *Subtarget) {
586 if (DCI.isBeforeLegalizeOps())
589 EVT Ty = N->getValueType(0);
590 unsigned LO = (Ty == MVT::i32) ? Mips::LO : Mips::LO64;
591 unsigned HI = (Ty == MVT::i32) ? Mips::HI : Mips::HI64;
592 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
594 DebugLoc dl = N->getDebugLoc();
596 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
597 N->getOperand(0), N->getOperand(1));
598 SDValue InChain = DAG.getEntryNode();
599 SDValue InGlue = DivRem;
602 if (N->hasAnyUseOfValue(0)) {
603 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, LO, Ty,
605 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
606 InChain = CopyFromLo.getValue(1);
607 InGlue = CopyFromLo.getValue(2);
611 if (N->hasAnyUseOfValue(1)) {
612 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
614 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
620 static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
622 default: llvm_unreachable("Unknown fp condition code!");
624 case ISD::SETOEQ: return Mips::FCOND_OEQ;
625 case ISD::SETUNE: return Mips::FCOND_UNE;
627 case ISD::SETOLT: return Mips::FCOND_OLT;
629 case ISD::SETOGT: return Mips::FCOND_OGT;
631 case ISD::SETOLE: return Mips::FCOND_OLE;
633 case ISD::SETOGE: return Mips::FCOND_OGE;
634 case ISD::SETULT: return Mips::FCOND_ULT;
635 case ISD::SETULE: return Mips::FCOND_ULE;
636 case ISD::SETUGT: return Mips::FCOND_UGT;
637 case ISD::SETUGE: return Mips::FCOND_UGE;
638 case ISD::SETUO: return Mips::FCOND_UN;
639 case ISD::SETO: return Mips::FCOND_OR;
641 case ISD::SETONE: return Mips::FCOND_ONE;
642 case ISD::SETUEQ: return Mips::FCOND_UEQ;
647 // Returns true if condition code has to be inverted.
648 static bool InvertFPCondCode(Mips::CondCode CC) {
649 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
652 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
653 "Illegal Condition Code");
658 // Creates and returns an FPCmp node from a setcc node.
659 // Returns Op if setcc is not a floating point comparison.
660 static SDValue CreateFPCmp(SelectionDAG &DAG, const SDValue &Op) {
661 // must be a SETCC node
662 if (Op.getOpcode() != ISD::SETCC)
665 SDValue LHS = Op.getOperand(0);
667 if (!LHS.getValueType().isFloatingPoint())
670 SDValue RHS = Op.getOperand(1);
671 DebugLoc dl = Op.getDebugLoc();
673 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
674 // node if necessary.
675 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
677 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
678 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
681 // Creates and returns a CMovFPT/F node.
682 static SDValue CreateCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
683 SDValue False, DebugLoc DL) {
684 bool invert = InvertFPCondCode((Mips::CondCode)
685 cast<ConstantSDNode>(Cond.getOperand(2))
688 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
689 True.getValueType(), True, False, Cond);
692 static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
693 TargetLowering::DAGCombinerInfo &DCI,
694 const MipsSubtarget *Subtarget) {
695 if (DCI.isBeforeLegalizeOps())
698 SDValue SetCC = N->getOperand(0);
700 if ((SetCC.getOpcode() != ISD::SETCC) ||
701 !SetCC.getOperand(0).getValueType().isInteger())
704 SDValue False = N->getOperand(2);
705 EVT FalseTy = False.getValueType();
707 if (!FalseTy.isInteger())
710 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(False);
712 if (!CN || CN->getZExtValue())
715 const DebugLoc DL = N->getDebugLoc();
716 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
717 SDValue True = N->getOperand(1);
719 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
720 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
722 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
725 static SDValue PerformANDCombine(SDNode *N, SelectionDAG &DAG,
726 TargetLowering::DAGCombinerInfo &DCI,
727 const MipsSubtarget *Subtarget) {
728 // Pattern match EXT.
729 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
730 // => ext $dst, $src, size, pos
731 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
734 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
735 unsigned ShiftRightOpc = ShiftRight.getOpcode();
737 // Op's first operand must be a shift right.
738 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
741 // The second operand of the shift must be an immediate.
743 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
746 uint64_t Pos = CN->getZExtValue();
747 uint64_t SMPos, SMSize;
749 // Op's second operand must be a shifted mask.
750 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
751 !IsShiftedMask(CN->getZExtValue(), SMPos, SMSize))
754 // Return if the shifted mask does not start at bit 0 or the sum of its size
755 // and Pos exceeds the word's size.
756 EVT ValTy = N->getValueType(0);
757 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
760 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), ValTy,
761 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
762 DAG.getConstant(SMSize, MVT::i32));
765 static SDValue PerformORCombine(SDNode *N, SelectionDAG &DAG,
766 TargetLowering::DAGCombinerInfo &DCI,
767 const MipsSubtarget *Subtarget) {
768 // Pattern match INS.
769 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
770 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
771 // => ins $dst, $src, size, pos, $src1
772 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
775 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
776 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
779 // See if Op's first operand matches (and $src1 , mask0).
780 if (And0.getOpcode() != ISD::AND)
783 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
784 !IsShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
787 // See if Op's second operand matches (and (shl $src, pos), mask1).
788 if (And1.getOpcode() != ISD::AND)
791 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
792 !IsShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
795 // The shift masks must have the same position and size.
796 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
799 SDValue Shl = And1.getOperand(0);
800 if (Shl.getOpcode() != ISD::SHL)
803 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
806 unsigned Shamt = CN->getZExtValue();
808 // Return if the shift amount and the first bit position of mask are not the
810 EVT ValTy = N->getValueType(0);
811 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
814 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), ValTy, Shl.getOperand(0),
815 DAG.getConstant(SMPos0, MVT::i32),
816 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
819 static SDValue PerformADDCombine(SDNode *N, SelectionDAG &DAG,
820 TargetLowering::DAGCombinerInfo &DCI,
821 const MipsSubtarget *Subtarget) {
822 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
824 if (DCI.isBeforeLegalizeOps())
827 SDValue Add = N->getOperand(1);
829 if (Add.getOpcode() != ISD::ADD)
832 SDValue Lo = Add.getOperand(1);
834 if ((Lo.getOpcode() != MipsISD::Lo) ||
835 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
838 EVT ValTy = N->getValueType(0);
839 DebugLoc DL = N->getDebugLoc();
841 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
843 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
846 SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
848 SelectionDAG &DAG = DCI.DAG;
849 unsigned opc = N->getOpcode();
854 return PerformADDECombine(N, DAG, DCI, Subtarget);
856 return PerformSUBECombine(N, DAG, DCI, Subtarget);
859 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
861 return PerformSELECTCombine(N, DAG, DCI, Subtarget);
863 return PerformANDCombine(N, DAG, DCI, Subtarget);
865 return PerformORCombine(N, DAG, DCI, Subtarget);
867 return PerformADDCombine(N, DAG, DCI, Subtarget);
874 MipsTargetLowering::LowerOperationWrapper(SDNode *N,
875 SmallVectorImpl<SDValue> &Results,
876 SelectionDAG &DAG) const {
877 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
879 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
880 Results.push_back(Res.getValue(I));
884 MipsTargetLowering::ReplaceNodeResults(SDNode *N,
885 SmallVectorImpl<SDValue> &Results,
886 SelectionDAG &DAG) const {
887 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
889 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
890 Results.push_back(Res.getValue(I));
893 SDValue MipsTargetLowering::
894 LowerOperation(SDValue Op, SelectionDAG &DAG) const
896 switch (Op.getOpcode())
898 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
899 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
900 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
901 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
902 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
903 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
904 case ISD::SELECT: return LowerSELECT(Op, DAG);
905 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
906 case ISD::SETCC: return LowerSETCC(Op, DAG);
907 case ISD::VASTART: return LowerVASTART(Op, DAG);
908 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
909 case ISD::FABS: return LowerFABS(Op, DAG);
910 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
911 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
912 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
913 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
914 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
915 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG, true);
916 case ISD::SRL_PARTS: return LowerShiftRightParts(Op, DAG, false);
917 case ISD::LOAD: return LowerLOAD(Op, DAG);
918 case ISD::STORE: return LowerSTORE(Op, DAG);
919 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
920 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
921 case ISD::ADD: return LowerADD(Op, DAG);
926 //===----------------------------------------------------------------------===//
927 // Lower helper functions
928 //===----------------------------------------------------------------------===//
930 // AddLiveIn - This helper function adds the specified physical register to the
931 // MachineFunction as a live in value. It also creates a corresponding
932 // virtual register for it.
934 AddLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
936 assert(RC->contains(PReg) && "Not the correct regclass!");
937 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
938 MF.getRegInfo().addLiveIn(PReg, VReg);
942 // Get fp branch code (not opcode) from condition code.
943 static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
944 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
945 return Mips::BRANCH_T;
947 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
948 "Invalid CondCode.");
950 return Mips::BRANCH_F;
954 static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
956 const MipsSubtarget *Subtarget,
957 const TargetInstrInfo *TII,
958 bool isFPCmp, unsigned Opc) {
959 // There is no need to expand CMov instructions if target has
960 // conditional moves.
961 if (Subtarget->hasCondMov())
964 // To "insert" a SELECT_CC instruction, we actually have to insert the
965 // diamond control-flow pattern. The incoming instruction knows the
966 // destination vreg to set, the condition code register to branch on, the
967 // true/false values to select between, and a branch opcode to use.
968 const BasicBlock *LLVM_BB = BB->getBasicBlock();
969 MachineFunction::iterator It = BB;
976 // bNE r1, r0, copy1MBB
977 // fallthrough --> copy0MBB
978 MachineBasicBlock *thisMBB = BB;
979 MachineFunction *F = BB->getParent();
980 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
981 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
982 F->insert(It, copy0MBB);
983 F->insert(It, sinkMBB);
985 // Transfer the remainder of BB and its successor edges to sinkMBB.
986 sinkMBB->splice(sinkMBB->begin(), BB,
987 llvm::next(MachineBasicBlock::iterator(MI)),
989 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
991 // Next, add the true and fallthrough blocks as its successors.
992 BB->addSuccessor(copy0MBB);
993 BB->addSuccessor(sinkMBB);
995 // Emit the right instruction according to the type of the operands compared
997 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
999 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
1000 .addReg(Mips::ZERO).addMBB(sinkMBB);
1003 // %FalseValue = ...
1004 // # fallthrough to sinkMBB
1007 // Update machine-CFG edges
1008 BB->addSuccessor(sinkMBB);
1011 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
1016 BuildMI(*BB, BB->begin(), dl,
1017 TII->get(Mips::PHI), MI->getOperand(0).getReg())
1018 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
1019 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
1021 BuildMI(*BB, BB->begin(), dl,
1022 TII->get(Mips::PHI), MI->getOperand(0).getReg())
1023 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
1024 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
1026 MI->eraseFromParent(); // The pseudo instruction is gone now.
1032 MipsTargetLowering::EmitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
1034 // bposge32_pseudo $vr0
1044 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
1046 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
1047 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1048 const TargetRegisterClass *RC = &Mips::CPURegsRegClass;
1049 DebugLoc DL = MI->getDebugLoc();
1050 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1051 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
1052 MachineFunction *F = BB->getParent();
1053 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
1054 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
1055 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
1058 F->insert(It, Sink);
1060 // Transfer the remainder of BB and its successor edges to Sink.
1061 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
1063 Sink->transferSuccessorsAndUpdatePHIs(BB);
1066 BB->addSuccessor(FBB);
1067 BB->addSuccessor(TBB);
1068 FBB->addSuccessor(Sink);
1069 TBB->addSuccessor(Sink);
1071 // Insert the real bposge32 instruction to $BB.
1072 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
1075 unsigned VR2 = RegInfo.createVirtualRegister(RC);
1076 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
1077 .addReg(Mips::ZERO).addImm(0);
1078 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
1081 unsigned VR1 = RegInfo.createVirtualRegister(RC);
1082 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
1083 .addReg(Mips::ZERO).addImm(1);
1085 // Insert phi function to $Sink.
1086 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
1087 MI->getOperand(0).getReg())
1088 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
1090 MI->eraseFromParent(); // The pseudo instruction is gone now.
1095 MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1096 MachineBasicBlock *BB) const {
1097 switch (MI->getOpcode()) {
1098 default: llvm_unreachable("Unexpected instr type to insert");
1099 case Mips::ATOMIC_LOAD_ADD_I8:
1100 case Mips::ATOMIC_LOAD_ADD_I8_P8:
1101 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
1102 case Mips::ATOMIC_LOAD_ADD_I16:
1103 case Mips::ATOMIC_LOAD_ADD_I16_P8:
1104 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
1105 case Mips::ATOMIC_LOAD_ADD_I32:
1106 case Mips::ATOMIC_LOAD_ADD_I32_P8:
1107 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
1108 case Mips::ATOMIC_LOAD_ADD_I64:
1109 case Mips::ATOMIC_LOAD_ADD_I64_P8:
1110 return EmitAtomicBinary(MI, BB, 8, Mips::DADDu);
1112 case Mips::ATOMIC_LOAD_AND_I8:
1113 case Mips::ATOMIC_LOAD_AND_I8_P8:
1114 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
1115 case Mips::ATOMIC_LOAD_AND_I16:
1116 case Mips::ATOMIC_LOAD_AND_I16_P8:
1117 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
1118 case Mips::ATOMIC_LOAD_AND_I32:
1119 case Mips::ATOMIC_LOAD_AND_I32_P8:
1120 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
1121 case Mips::ATOMIC_LOAD_AND_I64:
1122 case Mips::ATOMIC_LOAD_AND_I64_P8:
1123 return EmitAtomicBinary(MI, BB, 8, Mips::AND64);
1125 case Mips::ATOMIC_LOAD_OR_I8:
1126 case Mips::ATOMIC_LOAD_OR_I8_P8:
1127 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
1128 case Mips::ATOMIC_LOAD_OR_I16:
1129 case Mips::ATOMIC_LOAD_OR_I16_P8:
1130 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
1131 case Mips::ATOMIC_LOAD_OR_I32:
1132 case Mips::ATOMIC_LOAD_OR_I32_P8:
1133 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
1134 case Mips::ATOMIC_LOAD_OR_I64:
1135 case Mips::ATOMIC_LOAD_OR_I64_P8:
1136 return EmitAtomicBinary(MI, BB, 8, Mips::OR64);
1138 case Mips::ATOMIC_LOAD_XOR_I8:
1139 case Mips::ATOMIC_LOAD_XOR_I8_P8:
1140 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
1141 case Mips::ATOMIC_LOAD_XOR_I16:
1142 case Mips::ATOMIC_LOAD_XOR_I16_P8:
1143 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
1144 case Mips::ATOMIC_LOAD_XOR_I32:
1145 case Mips::ATOMIC_LOAD_XOR_I32_P8:
1146 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
1147 case Mips::ATOMIC_LOAD_XOR_I64:
1148 case Mips::ATOMIC_LOAD_XOR_I64_P8:
1149 return EmitAtomicBinary(MI, BB, 8, Mips::XOR64);
1151 case Mips::ATOMIC_LOAD_NAND_I8:
1152 case Mips::ATOMIC_LOAD_NAND_I8_P8:
1153 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
1154 case Mips::ATOMIC_LOAD_NAND_I16:
1155 case Mips::ATOMIC_LOAD_NAND_I16_P8:
1156 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
1157 case Mips::ATOMIC_LOAD_NAND_I32:
1158 case Mips::ATOMIC_LOAD_NAND_I32_P8:
1159 return EmitAtomicBinary(MI, BB, 4, 0, true);
1160 case Mips::ATOMIC_LOAD_NAND_I64:
1161 case Mips::ATOMIC_LOAD_NAND_I64_P8:
1162 return EmitAtomicBinary(MI, BB, 8, 0, true);
1164 case Mips::ATOMIC_LOAD_SUB_I8:
1165 case Mips::ATOMIC_LOAD_SUB_I8_P8:
1166 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
1167 case Mips::ATOMIC_LOAD_SUB_I16:
1168 case Mips::ATOMIC_LOAD_SUB_I16_P8:
1169 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
1170 case Mips::ATOMIC_LOAD_SUB_I32:
1171 case Mips::ATOMIC_LOAD_SUB_I32_P8:
1172 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
1173 case Mips::ATOMIC_LOAD_SUB_I64:
1174 case Mips::ATOMIC_LOAD_SUB_I64_P8:
1175 return EmitAtomicBinary(MI, BB, 8, Mips::DSUBu);
1177 case Mips::ATOMIC_SWAP_I8:
1178 case Mips::ATOMIC_SWAP_I8_P8:
1179 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
1180 case Mips::ATOMIC_SWAP_I16:
1181 case Mips::ATOMIC_SWAP_I16_P8:
1182 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
1183 case Mips::ATOMIC_SWAP_I32:
1184 case Mips::ATOMIC_SWAP_I32_P8:
1185 return EmitAtomicBinary(MI, BB, 4, 0);
1186 case Mips::ATOMIC_SWAP_I64:
1187 case Mips::ATOMIC_SWAP_I64_P8:
1188 return EmitAtomicBinary(MI, BB, 8, 0);
1190 case Mips::ATOMIC_CMP_SWAP_I8:
1191 case Mips::ATOMIC_CMP_SWAP_I8_P8:
1192 return EmitAtomicCmpSwapPartword(MI, BB, 1);
1193 case Mips::ATOMIC_CMP_SWAP_I16:
1194 case Mips::ATOMIC_CMP_SWAP_I16_P8:
1195 return EmitAtomicCmpSwapPartword(MI, BB, 2);
1196 case Mips::ATOMIC_CMP_SWAP_I32:
1197 case Mips::ATOMIC_CMP_SWAP_I32_P8:
1198 return EmitAtomicCmpSwap(MI, BB, 4);
1199 case Mips::ATOMIC_CMP_SWAP_I64:
1200 case Mips::ATOMIC_CMP_SWAP_I64_P8:
1201 return EmitAtomicCmpSwap(MI, BB, 8);
1202 case Mips::BPOSGE32_PSEUDO:
1203 return EmitBPOSGE32(MI, BB);
1207 // This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1208 // Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
1210 MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
1211 unsigned Size, unsigned BinOpcode,
1213 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
1215 MachineFunction *MF = BB->getParent();
1216 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1217 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
1218 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1219 DebugLoc dl = MI->getDebugLoc();
1220 unsigned LL, SC, AND, NOR, ZERO, BEQ;
1223 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1224 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1231 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1232 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1235 ZERO = Mips::ZERO_64;
1239 unsigned OldVal = MI->getOperand(0).getReg();
1240 unsigned Ptr = MI->getOperand(1).getReg();
1241 unsigned Incr = MI->getOperand(2).getReg();
1243 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1244 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1245 unsigned Success = RegInfo.createVirtualRegister(RC);
1247 // insert new blocks after the current block
1248 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1249 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1250 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1251 MachineFunction::iterator It = BB;
1253 MF->insert(It, loopMBB);
1254 MF->insert(It, exitMBB);
1256 // Transfer the remainder of BB and its successor edges to exitMBB.
1257 exitMBB->splice(exitMBB->begin(), BB,
1258 llvm::next(MachineBasicBlock::iterator(MI)),
1260 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1264 // fallthrough --> loopMBB
1265 BB->addSuccessor(loopMBB);
1266 loopMBB->addSuccessor(loopMBB);
1267 loopMBB->addSuccessor(exitMBB);
1270 // ll oldval, 0(ptr)
1271 // <binop> storeval, oldval, incr
1272 // sc success, storeval, 0(ptr)
1273 // beq success, $0, loopMBB
1275 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
1277 // and andres, oldval, incr
1278 // nor storeval, $0, andres
1279 BuildMI(BB, dl, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1280 BuildMI(BB, dl, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
1281 } else if (BinOpcode) {
1282 // <binop> storeval, oldval, incr
1283 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
1287 BuildMI(BB, dl, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1288 BuildMI(BB, dl, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
1290 MI->eraseFromParent(); // The instruction is gone now.
1296 MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
1297 MachineBasicBlock *BB,
1298 unsigned Size, unsigned BinOpcode,
1300 assert((Size == 1 || Size == 2) &&
1301 "Unsupported size for EmitAtomicBinaryPartial.");
1303 MachineFunction *MF = BB->getParent();
1304 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1305 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1306 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1307 DebugLoc dl = MI->getDebugLoc();
1308 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1309 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1311 unsigned Dest = MI->getOperand(0).getReg();
1312 unsigned Ptr = MI->getOperand(1).getReg();
1313 unsigned Incr = MI->getOperand(2).getReg();
1315 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1316 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
1317 unsigned Mask = RegInfo.createVirtualRegister(RC);
1318 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
1319 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1320 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1321 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
1322 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1323 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1324 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1325 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1326 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
1327 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1328 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1329 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1330 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1331 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1332 unsigned Success = RegInfo.createVirtualRegister(RC);
1334 // insert new blocks after the current block
1335 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1336 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1337 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1338 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1339 MachineFunction::iterator It = BB;
1341 MF->insert(It, loopMBB);
1342 MF->insert(It, sinkMBB);
1343 MF->insert(It, exitMBB);
1345 // Transfer the remainder of BB and its successor edges to exitMBB.
1346 exitMBB->splice(exitMBB->begin(), BB,
1347 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
1348 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1350 BB->addSuccessor(loopMBB);
1351 loopMBB->addSuccessor(loopMBB);
1352 loopMBB->addSuccessor(sinkMBB);
1353 sinkMBB->addSuccessor(exitMBB);
1356 // addiu masklsb2,$0,-4 # 0xfffffffc
1357 // and alignedaddr,ptr,masklsb2
1358 // andi ptrlsb2,ptr,3
1359 // sll shiftamt,ptrlsb2,3
1360 // ori maskupper,$0,255 # 0xff
1361 // sll mask,maskupper,shiftamt
1362 // nor mask2,$0,mask
1363 // sll incr2,incr,shiftamt
1365 int64_t MaskImm = (Size == 1) ? 255 : 65535;
1366 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1367 .addReg(Mips::ZERO).addImm(-4);
1368 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1369 .addReg(Ptr).addReg(MaskLSB2);
1370 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1371 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1372 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1373 .addReg(Mips::ZERO).addImm(MaskImm);
1374 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1375 .addReg(ShiftAmt).addReg(MaskUpper);
1376 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1377 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
1379 // atomic.load.binop
1381 // ll oldval,0(alignedaddr)
1382 // binop binopres,oldval,incr2
1383 // and newval,binopres,mask
1384 // and maskedoldval0,oldval,mask2
1385 // or storeval,maskedoldval0,newval
1386 // sc success,storeval,0(alignedaddr)
1387 // beq success,$0,loopMBB
1391 // ll oldval,0(alignedaddr)
1392 // and newval,incr2,mask
1393 // and maskedoldval0,oldval,mask2
1394 // or storeval,maskedoldval0,newval
1395 // sc success,storeval,0(alignedaddr)
1396 // beq success,$0,loopMBB
1399 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
1401 // and andres, oldval, incr2
1402 // nor binopres, $0, andres
1403 // and newval, binopres, mask
1404 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1405 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1406 .addReg(Mips::ZERO).addReg(AndRes);
1407 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
1408 } else if (BinOpcode) {
1409 // <binop> binopres, oldval, incr2
1410 // and newval, binopres, mask
1411 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1412 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
1413 } else {// atomic.swap
1414 // and newval, incr2, mask
1415 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
1418 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1419 .addReg(OldVal).addReg(Mask2);
1420 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1421 .addReg(MaskedOldVal0).addReg(NewVal);
1422 BuildMI(BB, dl, TII->get(SC), Success)
1423 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
1424 BuildMI(BB, dl, TII->get(Mips::BEQ))
1425 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
1428 // and maskedoldval1,oldval,mask
1429 // srl srlres,maskedoldval1,shiftamt
1430 // sll sllres,srlres,24
1431 // sra dest,sllres,24
1433 int64_t ShiftImm = (Size == 1) ? 24 : 16;
1435 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1436 .addReg(OldVal).addReg(Mask);
1437 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1438 .addReg(ShiftAmt).addReg(MaskedOldVal1);
1439 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1440 .addReg(SrlRes).addImm(ShiftImm);
1441 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
1442 .addReg(SllRes).addImm(ShiftImm);
1444 MI->eraseFromParent(); // The instruction is gone now.
1450 MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
1451 MachineBasicBlock *BB,
1452 unsigned Size) const {
1453 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
1455 MachineFunction *MF = BB->getParent();
1456 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1457 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
1458 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1459 DebugLoc dl = MI->getDebugLoc();
1460 unsigned LL, SC, ZERO, BNE, BEQ;
1463 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1464 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1470 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1471 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1472 ZERO = Mips::ZERO_64;
1477 unsigned Dest = MI->getOperand(0).getReg();
1478 unsigned Ptr = MI->getOperand(1).getReg();
1479 unsigned OldVal = MI->getOperand(2).getReg();
1480 unsigned NewVal = MI->getOperand(3).getReg();
1482 unsigned Success = RegInfo.createVirtualRegister(RC);
1484 // insert new blocks after the current block
1485 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1486 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1487 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1488 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1489 MachineFunction::iterator It = BB;
1491 MF->insert(It, loop1MBB);
1492 MF->insert(It, loop2MBB);
1493 MF->insert(It, exitMBB);
1495 // Transfer the remainder of BB and its successor edges to exitMBB.
1496 exitMBB->splice(exitMBB->begin(), BB,
1497 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
1498 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1502 // fallthrough --> loop1MBB
1503 BB->addSuccessor(loop1MBB);
1504 loop1MBB->addSuccessor(exitMBB);
1505 loop1MBB->addSuccessor(loop2MBB);
1506 loop2MBB->addSuccessor(loop1MBB);
1507 loop2MBB->addSuccessor(exitMBB);
1511 // bne dest, oldval, exitMBB
1513 BuildMI(BB, dl, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1514 BuildMI(BB, dl, TII->get(BNE))
1515 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
1518 // sc success, newval, 0(ptr)
1519 // beq success, $0, loop1MBB
1521 BuildMI(BB, dl, TII->get(SC), Success)
1522 .addReg(NewVal).addReg(Ptr).addImm(0);
1523 BuildMI(BB, dl, TII->get(BEQ))
1524 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
1526 MI->eraseFromParent(); // The instruction is gone now.
1532 MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
1533 MachineBasicBlock *BB,
1534 unsigned Size) const {
1535 assert((Size == 1 || Size == 2) &&
1536 "Unsupported size for EmitAtomicCmpSwapPartial.");
1538 MachineFunction *MF = BB->getParent();
1539 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1540 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1541 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1542 DebugLoc dl = MI->getDebugLoc();
1543 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1544 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1546 unsigned Dest = MI->getOperand(0).getReg();
1547 unsigned Ptr = MI->getOperand(1).getReg();
1548 unsigned CmpVal = MI->getOperand(2).getReg();
1549 unsigned NewVal = MI->getOperand(3).getReg();
1551 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1552 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
1553 unsigned Mask = RegInfo.createVirtualRegister(RC);
1554 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
1555 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1556 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1557 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1558 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1559 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1560 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1561 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1562 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1563 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1564 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1565 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1566 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1567 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1568 unsigned Success = RegInfo.createVirtualRegister(RC);
1570 // insert new blocks after the current block
1571 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1572 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1573 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1574 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1575 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1576 MachineFunction::iterator It = BB;
1578 MF->insert(It, loop1MBB);
1579 MF->insert(It, loop2MBB);
1580 MF->insert(It, sinkMBB);
1581 MF->insert(It, exitMBB);
1583 // Transfer the remainder of BB and its successor edges to exitMBB.
1584 exitMBB->splice(exitMBB->begin(), BB,
1585 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
1586 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1588 BB->addSuccessor(loop1MBB);
1589 loop1MBB->addSuccessor(sinkMBB);
1590 loop1MBB->addSuccessor(loop2MBB);
1591 loop2MBB->addSuccessor(loop1MBB);
1592 loop2MBB->addSuccessor(sinkMBB);
1593 sinkMBB->addSuccessor(exitMBB);
1595 // FIXME: computation of newval2 can be moved to loop2MBB.
1597 // addiu masklsb2,$0,-4 # 0xfffffffc
1598 // and alignedaddr,ptr,masklsb2
1599 // andi ptrlsb2,ptr,3
1600 // sll shiftamt,ptrlsb2,3
1601 // ori maskupper,$0,255 # 0xff
1602 // sll mask,maskupper,shiftamt
1603 // nor mask2,$0,mask
1604 // andi maskedcmpval,cmpval,255
1605 // sll shiftedcmpval,maskedcmpval,shiftamt
1606 // andi maskednewval,newval,255
1607 // sll shiftednewval,maskednewval,shiftamt
1608 int64_t MaskImm = (Size == 1) ? 255 : 65535;
1609 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1610 .addReg(Mips::ZERO).addImm(-4);
1611 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1612 .addReg(Ptr).addReg(MaskLSB2);
1613 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1614 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1615 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1616 .addReg(Mips::ZERO).addImm(MaskImm);
1617 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1618 .addReg(ShiftAmt).addReg(MaskUpper);
1619 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1620 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1621 .addReg(CmpVal).addImm(MaskImm);
1622 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1623 .addReg(ShiftAmt).addReg(MaskedCmpVal);
1624 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1625 .addReg(NewVal).addImm(MaskImm);
1626 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1627 .addReg(ShiftAmt).addReg(MaskedNewVal);
1630 // ll oldval,0(alginedaddr)
1631 // and maskedoldval0,oldval,mask
1632 // bne maskedoldval0,shiftedcmpval,sinkMBB
1634 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
1635 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1636 .addReg(OldVal).addReg(Mask);
1637 BuildMI(BB, dl, TII->get(Mips::BNE))
1638 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
1641 // and maskedoldval1,oldval,mask2
1642 // or storeval,maskedoldval1,shiftednewval
1643 // sc success,storeval,0(alignedaddr)
1644 // beq success,$0,loop1MBB
1646 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1647 .addReg(OldVal).addReg(Mask2);
1648 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1649 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1650 BuildMI(BB, dl, TII->get(SC), Success)
1651 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
1652 BuildMI(BB, dl, TII->get(Mips::BEQ))
1653 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
1656 // srl srlres,maskedoldval0,shiftamt
1657 // sll sllres,srlres,24
1658 // sra dest,sllres,24
1660 int64_t ShiftImm = (Size == 1) ? 24 : 16;
1662 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1663 .addReg(ShiftAmt).addReg(MaskedOldVal0);
1664 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1665 .addReg(SrlRes).addImm(ShiftImm);
1666 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
1667 .addReg(SllRes).addImm(ShiftImm);
1669 MI->eraseFromParent(); // The instruction is gone now.
1674 //===----------------------------------------------------------------------===//
1675 // Misc Lower Operation implementation
1676 //===----------------------------------------------------------------------===//
1677 SDValue MipsTargetLowering::
1678 LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
1680 // The first operand is the chain, the second is the condition, the third is
1681 // the block to branch to if the condition is true.
1682 SDValue Chain = Op.getOperand(0);
1683 SDValue Dest = Op.getOperand(2);
1684 DebugLoc dl = Op.getDebugLoc();
1686 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1688 // Return if flag is not set by a floating point comparison.
1689 if (CondRes.getOpcode() != MipsISD::FPCmp)
1692 SDValue CCNode = CondRes.getOperand(2);
1694 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
1695 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
1697 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
1701 SDValue MipsTargetLowering::
1702 LowerSELECT(SDValue Op, SelectionDAG &DAG) const
1704 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
1706 // Return if flag is not set by a floating point comparison.
1707 if (Cond.getOpcode() != MipsISD::FPCmp)
1710 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1714 SDValue MipsTargetLowering::
1715 LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
1717 DebugLoc DL = Op.getDebugLoc();
1718 EVT Ty = Op.getOperand(0).getValueType();
1719 SDValue Cond = DAG.getNode(ISD::SETCC, DL, getSetCCResultType(Ty),
1720 Op.getOperand(0), Op.getOperand(1),
1723 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1727 SDValue MipsTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1728 SDValue Cond = CreateFPCmp(DAG, Op);
1730 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1731 "Floating point operand expected.");
1733 SDValue True = DAG.getConstant(1, MVT::i32);
1734 SDValue False = DAG.getConstant(0, MVT::i32);
1736 return CreateCMovFP(DAG, Cond, True, False, Op.getDebugLoc());
1739 SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1740 SelectionDAG &DAG) const {
1741 // FIXME there isn't actually debug info here
1742 DebugLoc dl = Op.getDebugLoc();
1743 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1745 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
1746 SDVTList VTs = DAG.getVTList(MVT::i32);
1748 const MipsTargetObjectFile &TLOF =
1749 (const MipsTargetObjectFile&)getObjFileLowering();
1751 // %gp_rel relocation
1752 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1753 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1755 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1756 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
1757 return DAG.getNode(ISD::ADD, dl, MVT::i32, GPReg, GPRelNode);
1759 // %hi/%lo relocation
1760 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1762 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1764 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1765 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
1766 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
1769 EVT ValTy = Op.getValueType();
1770 bool HasGotOfst = (GV->hasInternalLinkage() ||
1771 (GV->hasLocalLinkage() && !isa<Function>(GV)));
1772 unsigned GotFlag = HasMips64 ?
1773 (HasGotOfst ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT_DISP) :
1774 (HasGotOfst ? MipsII::MO_GOT : MipsII::MO_GOT16);
1775 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0, GotFlag);
1776 GA = DAG.getNode(MipsISD::Wrapper, dl, ValTy, GetGlobalReg(DAG, ValTy), GA);
1777 SDValue ResNode = DAG.getLoad(ValTy, dl, DAG.getEntryNode(), GA,
1778 MachinePointerInfo(), false, false, false, 0);
1779 // On functions and global targets not internal linked only
1780 // a load from got/GP is necessary for PIC to work.
1783 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0,
1784 HasMips64 ? MipsII::MO_GOT_OFST :
1786 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, GALo);
1787 return DAG.getNode(ISD::ADD, dl, ValTy, ResNode, Lo);
1790 SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1791 SelectionDAG &DAG) const {
1792 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1793 // FIXME there isn't actually debug info here
1794 DebugLoc dl = Op.getDebugLoc();
1796 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
1797 // %hi/%lo relocation
1799 DAG.getTargetBlockAddress(BA, MVT::i32, 0, MipsII::MO_ABS_HI);
1801 DAG.getTargetBlockAddress(BA, MVT::i32, 0, MipsII::MO_ABS_LO);
1802 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1803 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1804 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1807 EVT ValTy = Op.getValueType();
1808 unsigned GOTFlag = HasMips64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1809 unsigned OFSTFlag = HasMips64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1810 SDValue BAGOTOffset = DAG.getTargetBlockAddress(BA, ValTy, 0, GOTFlag);
1811 BAGOTOffset = DAG.getNode(MipsISD::Wrapper, dl, ValTy,
1812 GetGlobalReg(DAG, ValTy), BAGOTOffset);
1813 SDValue BALOOffset = DAG.getTargetBlockAddress(BA, ValTy, 0, OFSTFlag);
1814 SDValue Load = DAG.getLoad(ValTy, dl, DAG.getEntryNode(), BAGOTOffset,
1815 MachinePointerInfo(), false, false, false, 0);
1816 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, BALOOffset);
1817 return DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
1820 SDValue MipsTargetLowering::
1821 LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
1823 // If the relocation model is PIC, use the General Dynamic TLS Model or
1824 // Local Dynamic TLS model, otherwise use the Initial Exec or
1825 // Local Exec TLS Model.
1827 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1828 DebugLoc dl = GA->getDebugLoc();
1829 const GlobalValue *GV = GA->getGlobal();
1830 EVT PtrVT = getPointerTy();
1832 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1834 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
1835 // General Dynamic and Local Dynamic TLS Model.
1836 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1839 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, Flag);
1840 SDValue Argument = DAG.getNode(MipsISD::Wrapper, dl, PtrVT,
1841 GetGlobalReg(DAG, PtrVT), TGA);
1842 unsigned PtrSize = PtrVT.getSizeInBits();
1843 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1845 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
1849 Entry.Node = Argument;
1851 Args.push_back(Entry);
1853 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
1854 false, false, false, false, 0, CallingConv::C,
1855 /*isTailCall=*/false, /*doesNotRet=*/false,
1856 /*isReturnValueUsed=*/true,
1857 TlsGetAddr, Args, DAG, dl);
1858 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
1860 SDValue Ret = CallResult.first;
1862 if (model != TLSModel::LocalDynamic)
1865 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1866 MipsII::MO_DTPREL_HI);
1867 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, PtrVT, TGAHi);
1868 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1869 MipsII::MO_DTPREL_LO);
1870 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, TGALo);
1871 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Ret);
1872 return DAG.getNode(ISD::ADD, dl, PtrVT, Add, Lo);
1876 if (model == TLSModel::InitialExec) {
1877 // Initial Exec TLS Model
1878 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1879 MipsII::MO_GOTTPREL);
1880 TGA = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, GetGlobalReg(DAG, PtrVT),
1882 Offset = DAG.getLoad(PtrVT, dl,
1883 DAG.getEntryNode(), TGA, MachinePointerInfo(),
1884 false, false, false, 0);
1886 // Local Exec TLS Model
1887 assert(model == TLSModel::LocalExec);
1888 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1889 MipsII::MO_TPREL_HI);
1890 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1891 MipsII::MO_TPREL_LO);
1892 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, PtrVT, TGAHi);
1893 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, TGALo);
1894 Offset = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
1897 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1898 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
1901 SDValue MipsTargetLowering::
1902 LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
1904 SDValue HiPart, JTI, JTILo;
1905 // FIXME there isn't actually debug info here
1906 DebugLoc dl = Op.getDebugLoc();
1907 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
1908 EVT PtrVT = Op.getValueType();
1909 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
1911 if (!IsPIC && !IsN64) {
1912 JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_HI);
1913 HiPart = DAG.getNode(MipsISD::Hi, dl, PtrVT, JTI);
1914 JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_LO);
1915 } else {// Emit Load from Global Pointer
1916 unsigned GOTFlag = HasMips64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1917 unsigned OfstFlag = HasMips64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1918 JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, GOTFlag);
1919 JTI = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, GetGlobalReg(DAG, PtrVT),
1921 HiPart = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), JTI,
1922 MachinePointerInfo(), false, false, false, 0);
1923 JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OfstFlag);
1926 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, JTILo);
1927 return DAG.getNode(ISD::ADD, dl, PtrVT, HiPart, Lo);
1930 SDValue MipsTargetLowering::
1931 LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
1934 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1935 const Constant *C = N->getConstVal();
1936 // FIXME there isn't actually debug info here
1937 DebugLoc dl = Op.getDebugLoc();
1939 // gp_rel relocation
1940 // FIXME: we should reference the constant pool using small data sections,
1941 // but the asm printer currently doesn't support this feature without
1942 // hacking it. This feature should come soon so we can uncomment the
1944 //if (IsInSmallSection(C->getType())) {
1945 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1946 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
1947 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
1949 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
1950 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
1951 N->getOffset(), MipsII::MO_ABS_HI);
1952 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
1953 N->getOffset(), MipsII::MO_ABS_LO);
1954 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1955 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
1956 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
1958 EVT ValTy = Op.getValueType();
1959 unsigned GOTFlag = HasMips64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1960 unsigned OFSTFlag = HasMips64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1961 SDValue CP = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1962 N->getOffset(), GOTFlag);
1963 CP = DAG.getNode(MipsISD::Wrapper, dl, ValTy, GetGlobalReg(DAG, ValTy), CP);
1964 SDValue Load = DAG.getLoad(ValTy, dl, DAG.getEntryNode(), CP,
1965 MachinePointerInfo::getConstantPool(), false,
1967 SDValue CPLo = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1968 N->getOffset(), OFSTFlag);
1969 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, CPLo);
1970 ResNode = DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
1976 SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1977 MachineFunction &MF = DAG.getMachineFunction();
1978 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1980 DebugLoc dl = Op.getDebugLoc();
1981 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1984 // vastart just stores the address of the VarArgsFrameIndex slot into the
1985 // memory location argument.
1986 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1987 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1988 MachinePointerInfo(SV), false, false, 0);
1991 static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
1992 EVT TyX = Op.getOperand(0).getValueType();
1993 EVT TyY = Op.getOperand(1).getValueType();
1994 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1995 SDValue Const31 = DAG.getConstant(31, MVT::i32);
1996 DebugLoc DL = Op.getDebugLoc();
1999 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2001 SDValue X = (TyX == MVT::f32) ?
2002 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
2003 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2005 SDValue Y = (TyY == MVT::f32) ?
2006 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
2007 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
2011 // ext E, Y, 31, 1 ; extract bit31 of Y
2012 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
2013 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
2014 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
2017 // srl SrlX, SllX, 1
2019 // sll SllY, SrlX, 31
2020 // or Or, SrlX, SllY
2021 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2022 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2023 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
2024 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
2025 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
2028 if (TyX == MVT::f32)
2029 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
2031 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2032 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
2033 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
2036 static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
2037 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
2038 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
2039 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
2040 SDValue Const1 = DAG.getConstant(1, MVT::i32);
2041 DebugLoc DL = Op.getDebugLoc();
2043 // Bitcast to integer nodes.
2044 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
2045 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
2048 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
2049 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
2050 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
2051 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
2053 if (WidthX > WidthY)
2054 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
2055 else if (WidthY > WidthX)
2056 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
2058 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
2059 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
2060 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
2063 // (d)sll SllX, X, 1
2064 // (d)srl SrlX, SllX, 1
2065 // (d)srl SrlY, Y, width(Y)-1
2066 // (d)sll SllY, SrlX, width(Y)-1
2067 // or Or, SrlX, SllY
2068 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
2069 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
2070 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
2071 DAG.getConstant(WidthY - 1, MVT::i32));
2073 if (WidthX > WidthY)
2074 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
2075 else if (WidthY > WidthX)
2076 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
2078 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
2079 DAG.getConstant(WidthX - 1, MVT::i32));
2080 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
2081 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
2085 MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
2086 if (Subtarget->hasMips64())
2087 return LowerFCOPYSIGN64(Op, DAG, Subtarget->hasMips32r2());
2089 return LowerFCOPYSIGN32(Op, DAG, Subtarget->hasMips32r2());
2092 static SDValue LowerFABS32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
2093 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
2094 DebugLoc DL = Op.getDebugLoc();
2096 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2098 SDValue X = (Op.getValueType() == MVT::f32) ?
2099 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
2100 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2105 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
2106 DAG.getRegister(Mips::ZERO, MVT::i32),
2107 DAG.getConstant(31, MVT::i32), Const1, X);
2109 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2110 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2113 if (Op.getValueType() == MVT::f32)
2114 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
2116 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2117 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
2118 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
2121 static SDValue LowerFABS64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
2122 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
2123 DebugLoc DL = Op.getDebugLoc();
2125 // Bitcast to integer node.
2126 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
2130 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
2131 DAG.getRegister(Mips::ZERO_64, MVT::i64),
2132 DAG.getConstant(63, MVT::i32), Const1, X);
2134 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
2135 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
2138 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
2142 MipsTargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
2143 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
2144 return LowerFABS64(Op, DAG, Subtarget->hasMips32r2());
2146 return LowerFABS32(Op, DAG, Subtarget->hasMips32r2());
2149 SDValue MipsTargetLowering::
2150 LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
2152 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
2153 "Frame address can only be determined for current frame.");
2155 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2156 MFI->setFrameAddressIsTaken(true);
2157 EVT VT = Op.getValueType();
2158 DebugLoc dl = Op.getDebugLoc();
2159 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
2160 IsN64 ? Mips::FP_64 : Mips::FP, VT);
2164 SDValue MipsTargetLowering::LowerRETURNADDR(SDValue Op,
2165 SelectionDAG &DAG) const {
2167 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
2168 "Return address can be determined only for current frame.");
2170 MachineFunction &MF = DAG.getMachineFunction();
2171 MachineFrameInfo *MFI = MF.getFrameInfo();
2172 EVT VT = Op.getValueType();
2173 unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
2174 MFI->setReturnAddressIsTaken(true);
2176 // Return RA, which contains the return address. Mark it an implicit live-in.
2177 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
2178 return DAG.getCopyFromReg(DAG.getEntryNode(), Op.getDebugLoc(), Reg, VT);
2181 // TODO: set SType according to the desired memory barrier behavior.
2183 MipsTargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const {
2185 DebugLoc dl = Op.getDebugLoc();
2186 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
2187 DAG.getConstant(SType, MVT::i32));
2190 SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
2191 SelectionDAG &DAG) const {
2192 // FIXME: Need pseudo-fence for 'singlethread' fences
2193 // FIXME: Set SType for weaker fences where supported/appropriate.
2195 DebugLoc dl = Op.getDebugLoc();
2196 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
2197 DAG.getConstant(SType, MVT::i32));
2200 SDValue MipsTargetLowering::LowerShiftLeftParts(SDValue Op,
2201 SelectionDAG &DAG) const {
2202 DebugLoc DL = Op.getDebugLoc();
2203 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2204 SDValue Shamt = Op.getOperand(2);
2207 // lo = (shl lo, shamt)
2208 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2211 // hi = (shl lo, shamt[4:0])
2212 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2213 DAG.getConstant(-1, MVT::i32));
2214 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
2215 DAG.getConstant(1, MVT::i32));
2216 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
2218 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
2219 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2220 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
2221 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2222 DAG.getConstant(0x20, MVT::i32));
2223 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2224 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
2225 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
2227 SDValue Ops[2] = {Lo, Hi};
2228 return DAG.getMergeValues(Ops, 2, DL);
2231 SDValue MipsTargetLowering::LowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
2233 DebugLoc DL = Op.getDebugLoc();
2234 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2235 SDValue Shamt = Op.getOperand(2);
2238 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2240 // hi = (sra hi, shamt)
2242 // hi = (srl hi, shamt)
2245 // lo = (sra hi, shamt[4:0])
2246 // hi = (sra hi, 31)
2248 // lo = (srl hi, shamt[4:0])
2250 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2251 DAG.getConstant(-1, MVT::i32));
2252 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
2253 DAG.getConstant(1, MVT::i32));
2254 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
2255 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
2256 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2257 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
2259 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2260 DAG.getConstant(0x20, MVT::i32));
2261 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
2262 DAG.getConstant(31, MVT::i32));
2263 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
2264 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2265 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
2268 SDValue Ops[2] = {Lo, Hi};
2269 return DAG.getMergeValues(Ops, 2, DL);
2272 static SDValue CreateLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
2273 SDValue Chain, SDValue Src, unsigned Offset) {
2274 SDValue Ptr = LD->getBasePtr();
2275 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
2276 EVT BasePtrVT = Ptr.getValueType();
2277 DebugLoc DL = LD->getDebugLoc();
2278 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2281 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
2282 DAG.getConstant(Offset, BasePtrVT));
2284 SDValue Ops[] = { Chain, Ptr, Src };
2285 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2286 LD->getMemOperand());
2289 // Expand an unaligned 32 or 64-bit integer load node.
2290 SDValue MipsTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
2291 LoadSDNode *LD = cast<LoadSDNode>(Op);
2292 EVT MemVT = LD->getMemoryVT();
2294 // Return if load is aligned or if MemVT is neither i32 nor i64.
2295 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2296 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2299 bool IsLittle = Subtarget->isLittle();
2300 EVT VT = Op.getValueType();
2301 ISD::LoadExtType ExtType = LD->getExtensionType();
2302 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2304 assert((VT == MVT::i32) || (VT == MVT::i64));
2307 // (set dst, (i64 (load baseptr)))
2309 // (set tmp, (ldl (add baseptr, 7), undef))
2310 // (set dst, (ldr baseptr, tmp))
2311 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
2312 SDValue LDL = CreateLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
2314 return CreateLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
2318 SDValue LWL = CreateLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
2320 SDValue LWR = CreateLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
2324 // (set dst, (i32 (load baseptr))) or
2325 // (set dst, (i64 (sextload baseptr))) or
2326 // (set dst, (i64 (extload baseptr)))
2328 // (set tmp, (lwl (add baseptr, 3), undef))
2329 // (set dst, (lwr baseptr, tmp))
2330 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2331 (ExtType == ISD::EXTLOAD))
2334 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2337 // (set dst, (i64 (zextload baseptr)))
2339 // (set tmp0, (lwl (add baseptr, 3), undef))
2340 // (set tmp1, (lwr baseptr, tmp0))
2341 // (set tmp2, (shl tmp1, 32))
2342 // (set dst, (srl tmp2, 32))
2343 DebugLoc DL = LD->getDebugLoc();
2344 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2345 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
2346 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2347 SDValue Ops[] = { SRL, LWR.getValue(1) };
2348 return DAG.getMergeValues(Ops, 2, DL);
2351 static SDValue CreateStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
2352 SDValue Chain, unsigned Offset) {
2353 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2354 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
2355 DebugLoc DL = SD->getDebugLoc();
2356 SDVTList VTList = DAG.getVTList(MVT::Other);
2359 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
2360 DAG.getConstant(Offset, BasePtrVT));
2362 SDValue Ops[] = { Chain, Value, Ptr };
2363 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2364 SD->getMemOperand());
2367 // Expand an unaligned 32 or 64-bit integer store node.
2368 SDValue MipsTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2369 StoreSDNode *SD = cast<StoreSDNode>(Op);
2370 EVT MemVT = SD->getMemoryVT();
2372 // Return if store is aligned or if MemVT is neither i32 nor i64.
2373 if ((SD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2374 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2377 bool IsLittle = Subtarget->isLittle();
2378 SDValue Value = SD->getValue(), Chain = SD->getChain();
2379 EVT VT = Value.getValueType();
2382 // (store val, baseptr) or
2383 // (truncstore val, baseptr)
2385 // (swl val, (add baseptr, 3))
2386 // (swr val, baseptr)
2387 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
2388 SDValue SWL = CreateStoreLR(MipsISD::SWL, DAG, SD, Chain,
2390 return CreateStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
2393 assert(VT == MVT::i64);
2396 // (store val, baseptr)
2398 // (sdl val, (add baseptr, 7))
2399 // (sdr val, baseptr)
2400 SDValue SDL = CreateStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2401 return CreateStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
2404 // This function expands mips intrinsic nodes which have 64-bit input operands
2405 // or output values.
2407 // out64 = intrinsic-node in64
2409 // lo = copy (extract-element (in64, 0))
2410 // hi = copy (extract-element (in64, 1))
2411 // mips-specific-node
2414 // out64 = merge-values (v0, v1)
2416 static SDValue LowerDSPIntr(SDValue Op, SelectionDAG &DAG,
2417 unsigned Opc, bool HasI64In, bool HasI64Out) {
2418 DebugLoc DL = Op.getDebugLoc();
2419 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
2420 SDValue Chain = HasChainIn ? Op->getOperand(0) : DAG.getEntryNode();
2421 SmallVector<SDValue, 3> Ops;
2424 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
2425 Op->getOperand(1 + HasChainIn),
2426 DAG.getConstant(0, MVT::i32));
2427 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
2428 Op->getOperand(1 + HasChainIn),
2429 DAG.getConstant(1, MVT::i32));
2431 Chain = DAG.getCopyToReg(Chain, DL, Mips::LO, InLo, SDValue());
2432 Chain = DAG.getCopyToReg(Chain, DL, Mips::HI, InHi, Chain.getValue(1));
2434 Ops.push_back(Chain);
2435 Ops.append(Op->op_begin() + HasChainIn + 2, Op->op_end());
2436 Ops.push_back(Chain.getValue(1));
2438 Ops.push_back(Chain);
2439 Ops.append(Op->op_begin() + HasChainIn + 1, Op->op_end());
2443 return DAG.getNode(Opc, DL, Op->value_begin(), Op->getNumValues(),
2444 Ops.begin(), Ops.size());
2446 SDValue Intr = DAG.getNode(Opc, DL, DAG.getVTList(MVT::Other, MVT::Glue),
2447 Ops.begin(), Ops.size());
2448 SDValue OutLo = DAG.getCopyFromReg(Intr.getValue(0), DL, Mips::LO, MVT::i32,
2450 SDValue OutHi = DAG.getCopyFromReg(OutLo.getValue(1), DL, Mips::HI, MVT::i32,
2452 SDValue Out = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, OutLo, OutHi);
2457 SDValue Vals[] = { Out, OutHi.getValue(1) };
2458 return DAG.getMergeValues(Vals, 2, DL);
2461 SDValue MipsTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
2462 SelectionDAG &DAG) const {
2463 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
2466 case Intrinsic::mips_shilo:
2467 return LowerDSPIntr(Op, DAG, MipsISD::SHILO, true, true);
2468 case Intrinsic::mips_dpau_h_qbl:
2469 return LowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL, true, true);
2470 case Intrinsic::mips_dpau_h_qbr:
2471 return LowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR, true, true);
2472 case Intrinsic::mips_dpsu_h_qbl:
2473 return LowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL, true, true);
2474 case Intrinsic::mips_dpsu_h_qbr:
2475 return LowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR, true, true);
2476 case Intrinsic::mips_dpa_w_ph:
2477 return LowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH, true, true);
2478 case Intrinsic::mips_dps_w_ph:
2479 return LowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH, true, true);
2480 case Intrinsic::mips_dpax_w_ph:
2481 return LowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH, true, true);
2482 case Intrinsic::mips_dpsx_w_ph:
2483 return LowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH, true, true);
2484 case Intrinsic::mips_mulsa_w_ph:
2485 return LowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH, true, true);
2486 case Intrinsic::mips_mult:
2487 return LowerDSPIntr(Op, DAG, MipsISD::MULT, false, true);
2488 case Intrinsic::mips_multu:
2489 return LowerDSPIntr(Op, DAG, MipsISD::MULTU, false, true);
2490 case Intrinsic::mips_madd:
2491 return LowerDSPIntr(Op, DAG, MipsISD::MADD_DSP, true, true);
2492 case Intrinsic::mips_maddu:
2493 return LowerDSPIntr(Op, DAG, MipsISD::MADDU_DSP, true, true);
2494 case Intrinsic::mips_msub:
2495 return LowerDSPIntr(Op, DAG, MipsISD::MSUB_DSP, true, true);
2496 case Intrinsic::mips_msubu:
2497 return LowerDSPIntr(Op, DAG, MipsISD::MSUBU_DSP, true, true);
2501 SDValue MipsTargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
2502 SelectionDAG &DAG) const {
2503 switch (cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue()) {
2506 case Intrinsic::mips_extp:
2507 return LowerDSPIntr(Op, DAG, MipsISD::EXTP, true, false);
2508 case Intrinsic::mips_extpdp:
2509 return LowerDSPIntr(Op, DAG, MipsISD::EXTPDP, true, false);
2510 case Intrinsic::mips_extr_w:
2511 return LowerDSPIntr(Op, DAG, MipsISD::EXTR_W, true, false);
2512 case Intrinsic::mips_extr_r_w:
2513 return LowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W, true, false);
2514 case Intrinsic::mips_extr_rs_w:
2515 return LowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W, true, false);
2516 case Intrinsic::mips_extr_s_h:
2517 return LowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H, true, false);
2518 case Intrinsic::mips_mthlip:
2519 return LowerDSPIntr(Op, DAG, MipsISD::MTHLIP, true, true);
2520 case Intrinsic::mips_mulsaq_s_w_ph:
2521 return LowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH, true, true);
2522 case Intrinsic::mips_maq_s_w_phl:
2523 return LowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL, true, true);
2524 case Intrinsic::mips_maq_s_w_phr:
2525 return LowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR, true, true);
2526 case Intrinsic::mips_maq_sa_w_phl:
2527 return LowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL, true, true);
2528 case Intrinsic::mips_maq_sa_w_phr:
2529 return LowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR, true, true);
2530 case Intrinsic::mips_dpaq_s_w_ph:
2531 return LowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH, true, true);
2532 case Intrinsic::mips_dpsq_s_w_ph:
2533 return LowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH, true, true);
2534 case Intrinsic::mips_dpaq_sa_l_w:
2535 return LowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W, true, true);
2536 case Intrinsic::mips_dpsq_sa_l_w:
2537 return LowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W, true, true);
2538 case Intrinsic::mips_dpaqx_s_w_ph:
2539 return LowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH, true, true);
2540 case Intrinsic::mips_dpaqx_sa_w_ph:
2541 return LowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH, true, true);
2542 case Intrinsic::mips_dpsqx_s_w_ph:
2543 return LowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH, true, true);
2544 case Intrinsic::mips_dpsqx_sa_w_ph:
2545 return LowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH, true, true);
2549 SDValue MipsTargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
2550 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2551 || cast<ConstantSDNode>
2552 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2553 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2557 // (add (frameaddr 0), (frame_to_args_offset))
2558 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2559 // (add FrameObject, 0)
2560 // where FrameObject is a fixed StackObject with offset 0 which points to
2561 // the old stack pointer.
2562 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2563 EVT ValTy = Op->getValueType(0);
2564 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2565 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
2566 return DAG.getNode(ISD::ADD, Op->getDebugLoc(), ValTy, InArgsAddr,
2567 DAG.getConstant(0, ValTy));
2570 //===----------------------------------------------------------------------===//
2571 // Calling Convention Implementation
2572 //===----------------------------------------------------------------------===//
2574 //===----------------------------------------------------------------------===//
2575 // TODO: Implement a generic logic using tblgen that can support this.
2576 // Mips O32 ABI rules:
2578 // i32 - Passed in A0, A1, A2, A3 and stack
2579 // f32 - Only passed in f32 registers if no int reg has been used yet to hold
2580 // an argument. Otherwise, passed in A1, A2, A3 and stack.
2581 // f64 - Only passed in two aliased f32 registers if no int reg has been used
2582 // yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
2583 // not used, it must be shadowed. If only A3 is avaiable, shadow it and
2586 // For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
2587 //===----------------------------------------------------------------------===//
2589 static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
2590 MVT LocVT, CCValAssign::LocInfo LocInfo,
2591 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2593 static const unsigned IntRegsSize=4, FloatRegsSize=2;
2595 static const uint16_t IntRegs[] = {
2596 Mips::A0, Mips::A1, Mips::A2, Mips::A3
2598 static const uint16_t F32Regs[] = {
2599 Mips::F12, Mips::F14
2601 static const uint16_t F64Regs[] = {
2605 // Do not process byval args here.
2606 if (ArgFlags.isByVal())
2609 // Promote i8 and i16
2610 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2612 if (ArgFlags.isSExt())
2613 LocInfo = CCValAssign::SExt;
2614 else if (ArgFlags.isZExt())
2615 LocInfo = CCValAssign::ZExt;
2617 LocInfo = CCValAssign::AExt;
2622 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2623 // is true: function is vararg, argument is 3rd or higher, there is previous
2624 // argument which is not f32 or f64.
2625 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2626 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
2627 unsigned OrigAlign = ArgFlags.getOrigAlign();
2628 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
2630 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
2631 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2632 // If this is the first part of an i64 arg,
2633 // the allocated register must be either A0 or A2.
2634 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2635 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2637 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2638 // Allocate int register and shadow next int register. If first
2639 // available register is Mips::A1 or Mips::A3, shadow it too.
2640 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2641 if (Reg == Mips::A1 || Reg == Mips::A3)
2642 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2643 State.AllocateReg(IntRegs, IntRegsSize);
2645 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2646 // we are guaranteed to find an available float register
2647 if (ValVT == MVT::f32) {
2648 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2649 // Shadow int register
2650 State.AllocateReg(IntRegs, IntRegsSize);
2652 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2653 // Shadow int registers
2654 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2655 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2656 State.AllocateReg(IntRegs, IntRegsSize);
2657 State.AllocateReg(IntRegs, IntRegsSize);
2660 llvm_unreachable("Cannot handle this ValVT.");
2663 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2665 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
2667 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
2672 #include "MipsGenCallingConv.inc"
2674 //===----------------------------------------------------------------------===//
2675 // Call Calling Convention Implementation
2676 //===----------------------------------------------------------------------===//
2678 static const unsigned O32IntRegsSize = 4;
2680 // Return next O32 integer argument register.
2681 static unsigned getNextIntArgReg(unsigned Reg) {
2682 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2683 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2686 /// IsEligibleForTailCallOptimization - Check whether the call is eligible
2687 /// for tail call optimization.
2688 bool MipsTargetLowering::
2689 IsEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
2690 unsigned NextStackOffset,
2691 const MipsFunctionInfo& FI) const {
2692 if (!EnableMipsTailCalls)
2695 // No tail call optimization for mips16.
2696 if (Subtarget->inMips16Mode())
2699 // Return false if either the callee or caller has a byval argument.
2700 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
2703 // Return true if the callee's argument area is no larger than the
2705 return NextStackOffset <= FI.getIncomingArgSize();
2709 MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
2710 SDValue Chain, SDValue Arg, DebugLoc DL,
2711 bool IsTailCall, SelectionDAG &DAG) const {
2713 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2714 DAG.getIntPtrConstant(Offset));
2715 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2719 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2720 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2721 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2722 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2723 /*isVolatile=*/ true, false, 0);
2726 /// LowerCall - functions arguments are copied from virtual regs to
2727 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
2729 MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
2730 SmallVectorImpl<SDValue> &InVals) const {
2731 SelectionDAG &DAG = CLI.DAG;
2732 DebugLoc &dl = CLI.DL;
2733 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2734 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2735 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2736 SDValue Chain = CLI.Chain;
2737 SDValue Callee = CLI.Callee;
2738 bool &isTailCall = CLI.IsTailCall;
2739 CallingConv::ID CallConv = CLI.CallConv;
2740 bool isVarArg = CLI.IsVarArg;
2742 MachineFunction &MF = DAG.getMachineFunction();
2743 MachineFrameInfo *MFI = MF.getFrameInfo();
2744 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
2745 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
2747 // Analyze operands of the call, assigning locations to each operand.
2748 SmallVector<CCValAssign, 16> ArgLocs;
2749 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2750 getTargetMachine(), ArgLocs, *DAG.getContext());
2751 MipsCC MipsCCInfo(CallConv, isVarArg, IsO32, CCInfo);
2753 MipsCCInfo.analyzeCallOperands(Outs);
2755 // Get a count of how many bytes are to be pushed on the stack.
2756 unsigned NextStackOffset = CCInfo.getNextStackOffset();
2758 // Check if it's really possible to do a tail call.
2761 IsEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
2762 *MF.getInfo<MipsFunctionInfo>());
2767 // Chain is the output chain of the last Load/Store or CopyToReg node.
2768 // ByValChain is the output chain of the last Memcpy node created for copying
2769 // byval arguments to the stack.
2770 unsigned StackAlignment = TFL->getStackAlignment();
2771 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
2772 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
2775 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal);
2777 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl,
2778 IsN64 ? Mips::SP_64 : Mips::SP,
2781 // With EABI is it possible to have 16 args on registers.
2782 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
2783 SmallVector<SDValue, 8> MemOpChains;
2784 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
2786 // Walk the register/memloc assignments, inserting copies/loads.
2787 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2788 SDValue Arg = OutVals[i];
2789 CCValAssign &VA = ArgLocs[i];
2790 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
2791 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2794 if (Flags.isByVal()) {
2795 assert(Flags.getByValSize() &&
2796 "ByVal args of size 0 should have been ignored by front-end.");
2797 assert(ByValArg != MipsCCInfo.byval_end());
2798 assert(!isTailCall &&
2799 "Do not tail-call optimize if there is a byval argument.");
2800 passByValArg(Chain, dl, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
2801 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
2806 // Promote the value if needed.
2807 switch (VA.getLocInfo()) {
2808 default: llvm_unreachable("Unknown loc info!");
2809 case CCValAssign::Full:
2810 if (VA.isRegLoc()) {
2811 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
2812 (ValVT == MVT::f64 && LocVT == MVT::i64))
2813 Arg = DAG.getNode(ISD::BITCAST, dl, LocVT, Arg);
2814 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
2815 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2816 Arg, DAG.getConstant(0, MVT::i32));
2817 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2818 Arg, DAG.getConstant(1, MVT::i32));
2819 if (!Subtarget->isLittle())
2821 unsigned LocRegLo = VA.getLocReg();
2822 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2823 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2824 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
2829 case CCValAssign::SExt:
2830 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, LocVT, Arg);
2832 case CCValAssign::ZExt:
2833 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, LocVT, Arg);
2835 case CCValAssign::AExt:
2836 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, LocVT, Arg);
2840 // Arguments that can be passed on register must be kept at
2841 // RegsToPass vector
2842 if (VA.isRegLoc()) {
2843 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2847 // Register can't get to this point...
2848 assert(VA.isMemLoc());
2850 // emit ISD::STORE whichs stores the
2851 // parameter value to a stack Location
2852 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
2853 Chain, Arg, dl, isTailCall, DAG));
2856 // Transform all store nodes into one single node because all store
2857 // nodes are independent of each other.
2858 if (!MemOpChains.empty())
2859 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2860 &MemOpChains[0], MemOpChains.size());
2862 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
2863 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2864 // node so that legalize doesn't hack it.
2865 unsigned char OpFlag;
2866 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
2867 bool GlobalOrExternal = false;
2870 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2871 if (IsPICCall && G->getGlobal()->hasInternalLinkage()) {
2872 OpFlag = IsO32 ? MipsII::MO_GOT : MipsII::MO_GOT_PAGE;
2873 unsigned char LoFlag = IsO32 ? MipsII::MO_ABS_LO : MipsII::MO_GOT_OFST;
2874 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(), 0,
2876 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
2879 OpFlag = IsPICCall ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
2880 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2881 getPointerTy(), 0, OpFlag);
2884 GlobalOrExternal = true;
2886 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
2887 if (IsN64 || (!IsO32 && IsPIC))
2888 OpFlag = MipsII::MO_GOT_DISP;
2889 else if (!IsPIC) // !N64 && static
2890 OpFlag = MipsII::MO_NO_FLAG;
2892 OpFlag = MipsII::MO_GOT_CALL;
2893 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2895 GlobalOrExternal = true;
2900 // Create nodes that load address of callee and copy it to T9
2902 if (GlobalOrExternal) {
2903 // Load callee address
2904 Callee = DAG.getNode(MipsISD::Wrapper, dl, getPointerTy(),
2905 GetGlobalReg(DAG, getPointerTy()), Callee);
2906 SDValue LoadValue = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
2907 Callee, MachinePointerInfo::getGOT(),
2908 false, false, false, 0);
2910 // Use GOT+LO if callee has internal linkage.
2911 if (CalleeLo.getNode()) {
2912 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, getPointerTy(), CalleeLo);
2913 Callee = DAG.getNode(ISD::ADD, dl, getPointerTy(), LoadValue, Lo);
2919 // T9 register operand.
2922 // T9 should contain the address of the callee function if
2923 // -reloction-model=pic or it is an indirect call.
2924 if (IsPICCall || !GlobalOrExternal) {
2926 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
2927 Chain = DAG.getCopyToReg(Chain, dl, T9Reg, Callee, SDValue(0, 0));
2928 InFlag = Chain.getValue(1);
2930 if (Subtarget->inMips16Mode())
2931 T9 = DAG.getRegister(T9Reg, getPointerTy());
2933 Callee = DAG.getRegister(T9Reg, getPointerTy());
2936 // Insert node "GP copy globalreg" before call to function.
2937 // Lazy-binding stubs require GP to point to the GOT.
2939 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
2940 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2941 RegsToPass.push_back(std::make_pair(GPReg, GetGlobalReg(DAG, Ty)));
2944 // Build a sequence of copy-to-reg nodes chained together with token
2945 // chain and flag operands which copy the outgoing args into registers.
2946 // The InFlag in necessary since all emitted instructions must be
2948 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2949 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2950 RegsToPass[i].second, InFlag);
2951 InFlag = Chain.getValue(1);
2954 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
2955 // = Chain, Callee, Reg#1, Reg#2, ...
2957 // Returns a chain & a flag for retval copy to use.
2958 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2959 SmallVector<SDValue, 8> Ops;
2960 Ops.push_back(Chain);
2961 Ops.push_back(Callee);
2963 // Add argument registers to the end of the list so that they are
2964 // known live into the call.
2965 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2966 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2967 RegsToPass[i].second.getValueType()));
2969 // Add T9 register operand.
2973 // Add a register mask operand representing the call-preserved registers.
2974 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2975 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2976 assert(Mask && "Missing call preserved mask for calling convention");
2977 Ops.push_back(DAG.getRegisterMask(Mask));
2979 if (InFlag.getNode())
2980 Ops.push_back(InFlag);
2983 return DAG.getNode(MipsISD::TailCall, dl, MVT::Other, &Ops[0], Ops.size());
2985 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
2986 InFlag = Chain.getValue(1);
2988 // Create the CALLSEQ_END node.
2989 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
2990 DAG.getIntPtrConstant(0, true), InFlag);
2991 InFlag = Chain.getValue(1);
2993 // Handle result values, copying them out of physregs into vregs that we
2995 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2996 Ins, dl, DAG, InVals);
2999 /// LowerCallResult - Lower the result values of a call into the
3000 /// appropriate copies out of appropriate physical registers.
3002 MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
3003 CallingConv::ID CallConv, bool isVarArg,
3004 const SmallVectorImpl<ISD::InputArg> &Ins,
3005 DebugLoc dl, SelectionDAG &DAG,
3006 SmallVectorImpl<SDValue> &InVals) const {
3007 // Assign locations to each value returned by this call.
3008 SmallVector<CCValAssign, 16> RVLocs;
3009 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
3010 getTargetMachine(), RVLocs, *DAG.getContext());
3012 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
3014 // Copy all of the result registers out of their specified physreg.
3015 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3016 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
3017 RVLocs[i].getValVT(), InFlag).getValue(1);
3018 InFlag = Chain.getValue(2);
3019 InVals.push_back(Chain.getValue(0));
3025 //===----------------------------------------------------------------------===//
3026 // Formal Arguments Calling Convention Implementation
3027 //===----------------------------------------------------------------------===//
3028 /// LowerFormalArguments - transform physical registers into virtual registers
3029 /// and generate load operations for arguments places on the stack.
3031 MipsTargetLowering::LowerFormalArguments(SDValue Chain,
3032 CallingConv::ID CallConv,
3034 const SmallVectorImpl<ISD::InputArg> &Ins,
3035 DebugLoc dl, SelectionDAG &DAG,
3036 SmallVectorImpl<SDValue> &InVals)
3038 MachineFunction &MF = DAG.getMachineFunction();
3039 MachineFrameInfo *MFI = MF.getFrameInfo();
3040 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3042 MipsFI->setVarArgsFrameIndex(0);
3044 // Used with vargs to acumulate store chains.
3045 std::vector<SDValue> OutChains;
3047 // Assign locations to all of the incoming arguments.
3048 SmallVector<CCValAssign, 16> ArgLocs;
3049 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
3050 getTargetMachine(), ArgLocs, *DAG.getContext());
3051 MipsCC MipsCCInfo(CallConv, isVarArg, IsO32, CCInfo);
3053 MipsCCInfo.analyzeFormalArguments(Ins);
3054 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
3055 MipsCCInfo.hasByValArg());
3057 Function::const_arg_iterator FuncArg =
3058 DAG.getMachineFunction().getFunction()->arg_begin();
3059 unsigned CurArgIdx = 0;
3060 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
3062 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3063 CCValAssign &VA = ArgLocs[i];
3064 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
3065 CurArgIdx = Ins[i].OrigArgIndex;
3066 EVT ValVT = VA.getValVT();
3067 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3068 bool IsRegLoc = VA.isRegLoc();
3070 if (Flags.isByVal()) {
3071 assert(Flags.getByValSize() &&
3072 "ByVal args of size 0 should have been ignored by front-end.");
3073 assert(ByValArg != MipsCCInfo.byval_end());
3074 copyByValRegs(Chain, dl, OutChains, DAG, Flags, InVals, &*FuncArg,
3075 MipsCCInfo, *ByValArg);
3080 // Arguments stored on registers
3082 EVT RegVT = VA.getLocVT();
3083 unsigned ArgReg = VA.getLocReg();
3084 const TargetRegisterClass *RC;
3086 if (RegVT == MVT::i32)
3087 RC = &Mips::CPURegsRegClass;
3088 else if (RegVT == MVT::i64)
3089 RC = &Mips::CPU64RegsRegClass;
3090 else if (RegVT == MVT::f32)
3091 RC = &Mips::FGR32RegClass;
3092 else if (RegVT == MVT::f64)
3093 RC = HasMips64 ? &Mips::FGR64RegClass : &Mips::AFGR64RegClass;
3095 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
3097 // Transform the arguments stored on
3098 // physical registers into virtual ones
3099 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
3100 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
3102 // If this is an 8 or 16-bit value, it has been passed promoted
3103 // to 32 bits. Insert an assert[sz]ext to capture this, then
3104 // truncate to the right size.
3105 if (VA.getLocInfo() != CCValAssign::Full) {
3106 unsigned Opcode = 0;
3107 if (VA.getLocInfo() == CCValAssign::SExt)
3108 Opcode = ISD::AssertSext;
3109 else if (VA.getLocInfo() == CCValAssign::ZExt)
3110 Opcode = ISD::AssertZext;
3112 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
3113 DAG.getValueType(ValVT));
3114 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, ValVT, ArgValue);
3117 // Handle floating point arguments passed in integer registers.
3118 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
3119 (RegVT == MVT::i64 && ValVT == MVT::f64))
3120 ArgValue = DAG.getNode(ISD::BITCAST, dl, ValVT, ArgValue);
3121 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
3122 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
3123 getNextIntArgReg(ArgReg), RC);
3124 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
3125 if (!Subtarget->isLittle())
3126 std::swap(ArgValue, ArgValue2);
3127 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
3128 ArgValue, ArgValue2);
3131 InVals.push_back(ArgValue);
3132 } else { // VA.isRegLoc()
3135 assert(VA.isMemLoc());
3137 // The stack pointer offset is relative to the caller stack frame.
3138 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
3139 VA.getLocMemOffset(), true);
3141 // Create load nodes to retrieve arguments from the stack
3142 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3143 InVals.push_back(DAG.getLoad(ValVT, dl, Chain, FIN,
3144 MachinePointerInfo::getFixedStack(FI),
3145 false, false, false, 0));
3149 // The mips ABIs for returning structs by value requires that we copy
3150 // the sret argument into $v0 for the return. Save the argument into
3151 // a virtual register so that we can access it from the return points.
3152 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
3153 unsigned Reg = MipsFI->getSRetReturnReg();
3155 Reg = MF.getRegInfo().
3156 createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
3157 MipsFI->setSRetReturnReg(Reg);
3159 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
3160 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
3164 writeVarArgRegs(OutChains, MipsCCInfo, Chain, dl, DAG);
3166 // All stores are grouped in one node to allow the matching between
3167 // the size of Ins and InVals. This only happens when on varg functions
3168 if (!OutChains.empty()) {
3169 OutChains.push_back(Chain);
3170 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
3171 &OutChains[0], OutChains.size());
3177 //===----------------------------------------------------------------------===//
3178 // Return Value Calling Convention Implementation
3179 //===----------------------------------------------------------------------===//
3182 MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
3183 MachineFunction &MF, bool isVarArg,
3184 const SmallVectorImpl<ISD::OutputArg> &Outs,
3185 LLVMContext &Context) const {
3186 SmallVector<CCValAssign, 16> RVLocs;
3187 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
3189 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3193 MipsTargetLowering::LowerReturn(SDValue Chain,
3194 CallingConv::ID CallConv, bool isVarArg,
3195 const SmallVectorImpl<ISD::OutputArg> &Outs,
3196 const SmallVectorImpl<SDValue> &OutVals,
3197 DebugLoc dl, SelectionDAG &DAG) const {
3199 // CCValAssign - represent the assignment of
3200 // the return value to a location
3201 SmallVector<CCValAssign, 16> RVLocs;
3203 // CCState - Info about the registers and stack slot.
3204 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
3205 getTargetMachine(), RVLocs, *DAG.getContext());
3207 // Analize return values.
3208 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
3210 // If this is the first return lowered for this function, add
3211 // the regs to the liveout set for the function.
3212 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
3213 for (unsigned i = 0; i != RVLocs.size(); ++i)
3214 if (RVLocs[i].isRegLoc())
3215 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
3220 // Copy the result values into the output registers.
3221 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3222 CCValAssign &VA = RVLocs[i];
3223 assert(VA.isRegLoc() && "Can only return in registers!");
3225 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
3227 // guarantee that all emitted copies are
3228 // stuck together, avoiding something bad
3229 Flag = Chain.getValue(1);
3232 // The mips ABIs for returning structs by value requires that we copy
3233 // the sret argument into $v0 for the return. We saved the argument into
3234 // a virtual register in the entry block, so now we copy the value out
3236 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
3237 MachineFunction &MF = DAG.getMachineFunction();
3238 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3239 unsigned Reg = MipsFI->getSRetReturnReg();
3242 llvm_unreachable("sret virtual register not created in the entry block");
3243 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
3244 unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
3246 Chain = DAG.getCopyToReg(Chain, dl, V0, Val, Flag);
3247 Flag = Chain.getValue(1);
3248 MF.getRegInfo().addLiveOut(V0);
3251 // Return on Mips is always a "jr $ra"
3253 return DAG.getNode(MipsISD::Ret, dl, MVT::Other, Chain, Flag);
3256 return DAG.getNode(MipsISD::Ret, dl, MVT::Other, Chain);
3259 //===----------------------------------------------------------------------===//
3260 // Mips Inline Assembly Support
3261 //===----------------------------------------------------------------------===//
3263 /// getConstraintType - Given a constraint letter, return the type of
3264 /// constraint it is for this target.
3265 MipsTargetLowering::ConstraintType MipsTargetLowering::
3266 getConstraintType(const std::string &Constraint) const
3268 // Mips specific constrainy
3269 // GCC config/mips/constraints.md
3271 // 'd' : An address register. Equivalent to r
3272 // unless generating MIPS16 code.
3273 // 'y' : Equivalent to r; retained for
3274 // backwards compatibility.
3275 // 'c' : A register suitable for use in an indirect
3276 // jump. This will always be $25 for -mabicalls.
3277 // 'l' : The lo register. 1 word storage.
3278 // 'x' : The hilo register pair. Double word storage.
3279 if (Constraint.size() == 1) {
3280 switch (Constraint[0]) {
3288 return C_RegisterClass;
3291 return TargetLowering::getConstraintType(Constraint);
3294 /// Examine constraint type and operand type and determine a weight value.
3295 /// This object must already have been set up with the operand type
3296 /// and the current alternative constraint selected.
3297 TargetLowering::ConstraintWeight
3298 MipsTargetLowering::getSingleConstraintMatchWeight(
3299 AsmOperandInfo &info, const char *constraint) const {
3300 ConstraintWeight weight = CW_Invalid;
3301 Value *CallOperandVal = info.CallOperandVal;
3302 // If we don't have a value, we can't do a match,
3303 // but allow it at the lowest weight.
3304 if (CallOperandVal == NULL)
3306 Type *type = CallOperandVal->getType();
3307 // Look at the constraint type.
3308 switch (*constraint) {
3310 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3314 if (type->isIntegerTy())
3315 weight = CW_Register;
3318 if (type->isFloatTy())
3319 weight = CW_Register;
3321 case 'c': // $25 for indirect jumps
3322 case 'l': // lo register
3323 case 'x': // hilo register pair
3324 if (type->isIntegerTy())
3325 weight = CW_SpecificReg;
3327 case 'I': // signed 16 bit immediate
3328 case 'J': // integer zero
3329 case 'K': // unsigned 16 bit immediate
3330 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3331 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3332 case 'O': // signed 15 bit immediate (+- 16383)
3333 case 'P': // immediate in the range of 65535 to 1 (inclusive)
3334 if (isa<ConstantInt>(CallOperandVal))
3335 weight = CW_Constant;
3341 /// Given a register class constraint, like 'r', if this corresponds directly
3342 /// to an LLVM register class, return a register of 0 and the register class
3344 std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
3345 getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
3347 if (Constraint.size() == 1) {
3348 switch (Constraint[0]) {
3349 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3350 case 'y': // Same as 'r'. Exists for compatibility.
3352 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
3353 if (Subtarget->inMips16Mode())
3354 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
3355 return std::make_pair(0U, &Mips::CPURegsRegClass);
3357 if (VT == MVT::i64 && !HasMips64)
3358 return std::make_pair(0U, &Mips::CPURegsRegClass);
3359 if (VT == MVT::i64 && HasMips64)
3360 return std::make_pair(0U, &Mips::CPU64RegsRegClass);
3361 // This will generate an error message
3362 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
3365 return std::make_pair(0U, &Mips::FGR32RegClass);
3366 if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
3367 if (Subtarget->isFP64bit())
3368 return std::make_pair(0U, &Mips::FGR64RegClass);
3369 return std::make_pair(0U, &Mips::AFGR64RegClass);
3372 case 'c': // register suitable for indirect jump
3374 return std::make_pair((unsigned)Mips::T9, &Mips::CPURegsRegClass);
3375 assert(VT == MVT::i64 && "Unexpected type.");
3376 return std::make_pair((unsigned)Mips::T9_64, &Mips::CPU64RegsRegClass);
3377 case 'l': // register suitable for indirect jump
3379 return std::make_pair((unsigned)Mips::LO, &Mips::HILORegClass);
3380 return std::make_pair((unsigned)Mips::LO64, &Mips::HILO64RegClass);
3381 case 'x': // register suitable for indirect jump
3382 // Fixme: Not triggering the use of both hi and low
3383 // This will generate an error message
3384 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
3387 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3390 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3391 /// vector. If it is invalid, don't add anything to Ops.
3392 void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3393 std::string &Constraint,
3394 std::vector<SDValue>&Ops,
3395 SelectionDAG &DAG) const {
3396 SDValue Result(0, 0);
3398 // Only support length 1 constraints for now.
3399 if (Constraint.length() > 1) return;
3401 char ConstraintLetter = Constraint[0];
3402 switch (ConstraintLetter) {
3403 default: break; // This will fall through to the generic implementation
3404 case 'I': // Signed 16 bit constant
3405 // If this fails, the parent routine will give an error
3406 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3407 EVT Type = Op.getValueType();
3408 int64_t Val = C->getSExtValue();
3409 if (isInt<16>(Val)) {
3410 Result = DAG.getTargetConstant(Val, Type);
3415 case 'J': // integer zero
3416 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3417 EVT Type = Op.getValueType();
3418 int64_t Val = C->getZExtValue();
3420 Result = DAG.getTargetConstant(0, Type);
3425 case 'K': // unsigned 16 bit immediate
3426 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3427 EVT Type = Op.getValueType();
3428 uint64_t Val = (uint64_t)C->getZExtValue();
3429 if (isUInt<16>(Val)) {
3430 Result = DAG.getTargetConstant(Val, Type);
3435 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3436 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3437 EVT Type = Op.getValueType();
3438 int64_t Val = C->getSExtValue();
3439 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3440 Result = DAG.getTargetConstant(Val, Type);
3445 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3446 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3447 EVT Type = Op.getValueType();
3448 int64_t Val = C->getSExtValue();
3449 if ((Val >= -65535) && (Val <= -1)) {
3450 Result = DAG.getTargetConstant(Val, Type);
3455 case 'O': // signed 15 bit immediate
3456 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3457 EVT Type = Op.getValueType();
3458 int64_t Val = C->getSExtValue();
3459 if ((isInt<15>(Val))) {
3460 Result = DAG.getTargetConstant(Val, Type);
3465 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3466 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3467 EVT Type = Op.getValueType();
3468 int64_t Val = C->getSExtValue();
3469 if ((Val <= 65535) && (Val >= 1)) {
3470 Result = DAG.getTargetConstant(Val, Type);
3477 if (Result.getNode()) {
3478 Ops.push_back(Result);
3482 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3486 MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM, Type *Ty) const {
3487 // No global is ever allowed as a base.
3492 case 0: // "r+i" or just "i", depending on HasBaseReg.
3495 if (!AM.HasBaseReg) // allow "r+i".
3497 return false; // disallow "r+r" or "r+r+i".
3506 MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3507 // The Mips target isn't yet aware of offsets.
3511 EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
3512 unsigned SrcAlign, bool IsZeroVal,
3514 MachineFunction &MF) const {
3515 if (Subtarget->hasMips64())
3521 bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3522 if (VT != MVT::f32 && VT != MVT::f64)
3524 if (Imm.isNegZero())
3526 return Imm.isZero();
3529 unsigned MipsTargetLowering::getJumpTableEncoding() const {
3531 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
3533 return TargetLowering::getJumpTableEncoding();
3536 MipsTargetLowering::MipsCC::MipsCC(CallingConv::ID CallConv, bool IsVarArg,
3537 bool IsO32, CCState &Info) : CCInfo(Info) {
3538 UseRegsForByval = true;
3542 NumIntArgRegs = array_lengthof(O32IntRegs);
3543 ReservedArgArea = 16;
3544 IntArgRegs = ShadowRegs = O32IntRegs;
3545 FixedFn = VarFn = CC_MipsO32;
3548 NumIntArgRegs = array_lengthof(Mips64IntRegs);
3549 ReservedArgArea = 0;
3550 IntArgRegs = Mips64IntRegs;
3551 ShadowRegs = Mips64DPRegs;
3553 VarFn = CC_MipsN_VarArg;
3556 if (CallConv == CallingConv::Fast) {
3558 UseRegsForByval = false;
3559 ReservedArgArea = 0;
3560 FixedFn = VarFn = CC_Mips_FastCC;
3563 // Pre-allocate reserved argument area.
3564 CCInfo.AllocateStack(ReservedArgArea, 1);
3567 void MipsTargetLowering::MipsCC::
3568 analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args) {
3569 unsigned NumOpnds = Args.size();
3571 for (unsigned I = 0; I != NumOpnds; ++I) {
3572 MVT ArgVT = Args[I].VT;
3573 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3576 if (ArgFlags.isByVal()) {
3577 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3581 if (Args[I].IsFixed)
3582 R = FixedFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
3584 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
3588 dbgs() << "Call operand #" << I << " has unhandled type "
3589 << EVT(ArgVT).getEVTString();
3591 llvm_unreachable(0);
3596 void MipsTargetLowering::MipsCC::
3597 analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args) {
3598 unsigned NumArgs = Args.size();
3600 for (unsigned I = 0; I != NumArgs; ++I) {
3601 MVT ArgVT = Args[I].VT;
3602 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3604 if (ArgFlags.isByVal()) {
3605 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3609 if (!FixedFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo))
3613 dbgs() << "Formal Arg #" << I << " has unhandled type "
3614 << EVT(ArgVT).getEVTString();
3616 llvm_unreachable(0);
3621 MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3623 CCValAssign::LocInfo LocInfo,
3624 ISD::ArgFlagsTy ArgFlags) {
3625 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3627 struct ByValArgInfo ByVal;
3628 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3629 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3632 if (UseRegsForByval)
3633 allocateRegs(ByVal, ByValSize, Align);
3635 // Allocate space on caller's stack.
3636 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3638 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3640 ByValArgs.push_back(ByVal);
3643 void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3646 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3647 "Byval argument's size and alignment should be a multiple of"
3650 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3652 // If Align > RegSize, the first arg register must be even.
3653 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3654 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3658 // Mark the registers allocated.
3659 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3660 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3661 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3664 void MipsTargetLowering::
3665 copyByValRegs(SDValue Chain, DebugLoc DL, std::vector<SDValue> &OutChains,
3666 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3667 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3668 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3669 MachineFunction &MF = DAG.getMachineFunction();
3670 MachineFrameInfo *MFI = MF.getFrameInfo();
3671 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
3672 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3676 FrameObjOffset = (int)CC.reservedArgArea() -
3677 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
3679 FrameObjOffset = ByVal.Address;
3681 // Create frame object.
3682 EVT PtrTy = getPointerTy();
3683 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3684 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3685 InVals.push_back(FIN);
3690 // Copy arg registers.
3691 EVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
3692 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3694 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3695 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
3696 unsigned VReg = AddLiveIn(MF, ArgReg, RC);
3697 unsigned Offset = I * CC.regSize();
3698 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3699 DAG.getConstant(Offset, PtrTy));
3700 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3701 StorePtr, MachinePointerInfo(FuncArg, Offset),
3703 OutChains.push_back(Store);
3707 // Copy byVal arg to registers and stack.
3708 void MipsTargetLowering::
3709 passByValArg(SDValue Chain, DebugLoc DL,
3710 SmallVector<std::pair<unsigned, SDValue>, 16> &RegsToPass,
3711 SmallVector<SDValue, 8> &MemOpChains, SDValue StackPtr,
3712 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3713 const MipsCC &CC, const ByValArgInfo &ByVal,
3714 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
3715 unsigned ByValSize = Flags.getByValSize();
3716 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
3717 unsigned RegSize = CC.regSize();
3718 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
3719 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
3721 if (ByVal.NumRegs) {
3722 const uint16_t *ArgRegs = CC.intArgRegs();
3723 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
3726 // Copy words to registers.
3727 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
3728 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3729 DAG.getConstant(Offset, PtrTy));
3730 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3731 MachinePointerInfo(), false, false, false,
3733 MemOpChains.push_back(LoadVal.getValue(1));
3734 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3735 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3738 // Return if the struct has been fully copied.
3739 if (ByValSize == Offset)
3742 // Copy the remainder of the byval argument with sub-word loads and shifts.
3743 if (LeftoverBytes) {
3744 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
3745 "Size of the remainder should be smaller than RegSize.");
3748 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
3749 Offset < ByValSize; LoadSize /= 2) {
3750 unsigned RemSize = ByValSize - Offset;
3752 if (RemSize < LoadSize)
3756 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3757 DAG.getConstant(Offset, PtrTy));
3759 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
3760 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
3761 false, false, Alignment);
3762 MemOpChains.push_back(LoadVal.getValue(1));
3764 // Shift the loaded value.
3768 Shamt = TotalSizeLoaded;
3770 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
3772 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3773 DAG.getConstant(Shamt, MVT::i32));
3776 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3781 TotalSizeLoaded += LoadSize;
3782 Alignment = std::min(Alignment, LoadSize);
3785 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3786 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3791 // Copy remainder of byval arg to it with memcpy.
3792 unsigned MemCpySize = ByValSize - Offset;
3793 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3794 DAG.getConstant(Offset, PtrTy));
3795 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3796 DAG.getIntPtrConstant(ByVal.Address));
3797 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3798 DAG.getConstant(MemCpySize, PtrTy), Alignment,
3799 /*isVolatile=*/false, /*AlwaysInline=*/false,
3800 MachinePointerInfo(0), MachinePointerInfo(0));
3801 MemOpChains.push_back(Chain);
3805 MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3806 const MipsCC &CC, SDValue Chain,
3807 DebugLoc DL, SelectionDAG &DAG) const {
3808 unsigned NumRegs = CC.numIntArgRegs();
3809 const uint16_t *ArgRegs = CC.intArgRegs();
3810 const CCState &CCInfo = CC.getCCInfo();
3811 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
3812 unsigned RegSize = CC.regSize();
3813 EVT RegTy = MVT::getIntegerVT(RegSize * 8);
3814 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3815 MachineFunction &MF = DAG.getMachineFunction();
3816 MachineFrameInfo *MFI = MF.getFrameInfo();
3817 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3819 // Offset of the first variable argument from stack pointer.
3823 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
3826 (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
3828 // Record the frame index of the first variable argument
3829 // which is a value necessary to VASTART.
3830 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3831 MipsFI->setVarArgsFrameIndex(FI);
3833 // Copy the integer registers that have not been used for argument passing
3834 // to the argument register save area. For O32, the save area is allocated
3835 // in the caller's stack frame, while for N32/64, it is allocated in the
3836 // callee's stack frame.
3837 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
3838 unsigned Reg = AddLiveIn(MF, ArgRegs[I], RC);
3839 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3840 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3841 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3842 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3843 MachinePointerInfo(), false, false, 0);
3844 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
3845 OutChains.push_back(Store);