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 "llvm/DerivedTypes.h"
22 #include "llvm/Function.h"
23 #include "llvm/GlobalVariable.h"
24 #include "llvm/Intrinsics.h"
25 #include "llvm/CallingConv.h"
26 #include "llvm/CodeGen/CallingConvLower.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/SelectionDAGISel.h"
32 #include "llvm/CodeGen/ValueTypes.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
37 const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
39 case MipsISD::JmpLink: return "MipsISD::JmpLink";
40 case MipsISD::Hi: return "MipsISD::Hi";
41 case MipsISD::Lo: return "MipsISD::Lo";
42 case MipsISD::GPRel: return "MipsISD::GPRel";
43 case MipsISD::TlsGd: return "MipsISD::TlsGd";
44 case MipsISD::TprelHi: return "MipsISD::TprelHi";
45 case MipsISD::TprelLo: return "MipsISD::TprelLo";
46 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
47 case MipsISD::Ret: return "MipsISD::Ret";
48 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
49 case MipsISD::FPCmp: return "MipsISD::FPCmp";
50 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
51 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
52 case MipsISD::FPRound: return "MipsISD::FPRound";
53 case MipsISD::MAdd: return "MipsISD::MAdd";
54 case MipsISD::MAddu: return "MipsISD::MAddu";
55 case MipsISD::MSub: return "MipsISD::MSub";
56 case MipsISD::MSubu: return "MipsISD::MSubu";
57 case MipsISD::DivRem: return "MipsISD::DivRem";
58 case MipsISD::DivRemU: return "MipsISD::DivRemU";
59 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
60 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
61 case MipsISD::WrapperPIC: return "MipsISD::WrapperPIC";
67 MipsTargetLowering(MipsTargetMachine &TM)
68 : TargetLowering(TM, new MipsTargetObjectFile()) {
69 Subtarget = &TM.getSubtarget<MipsSubtarget>();
71 // Mips does not have i1 type, so use i32 for
72 // setcc operations results (slt, sgt, ...).
73 setBooleanContents(ZeroOrOneBooleanContent);
75 // Set up the register classes
76 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
77 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
79 // When dealing with single precision only, use libcalls
80 if (!Subtarget->isSingleFloat())
81 if (!Subtarget->isFP64bit())
82 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
84 // Load extented operations for i1 types must be promoted
85 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
86 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
87 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
89 // MIPS doesn't have extending float->double load/store
90 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
91 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
93 // Used by legalize types to correctly generate the setcc result.
94 // Without this, every float setcc comes with a AND/OR with the result,
95 // we don't want this, since the fpcmp result goes to a flag register,
96 // which is used implicitly by brcond and select operations.
97 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
99 // Mips Custom Operations
100 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
101 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
102 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
103 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
104 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
105 setOperationAction(ISD::SELECT, MVT::f32, Custom);
106 setOperationAction(ISD::SELECT, MVT::f64, Custom);
107 setOperationAction(ISD::SELECT, MVT::i32, Custom);
108 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
109 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
110 setOperationAction(ISD::VASTART, MVT::Other, Custom);
112 setOperationAction(ISD::SDIV, MVT::i32, Expand);
113 setOperationAction(ISD::SREM, MVT::i32, Expand);
114 setOperationAction(ISD::UDIV, MVT::i32, Expand);
115 setOperationAction(ISD::UREM, MVT::i32, Expand);
117 // Operations not directly supported by Mips.
118 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
119 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
120 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
121 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
122 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
124 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
125 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
126 setOperationAction(ISD::ROTL, MVT::i32, Expand);
128 if (!Subtarget->isMips32r2())
129 setOperationAction(ISD::ROTR, MVT::i32, Expand);
131 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
132 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
133 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
134 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
135 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
136 setOperationAction(ISD::FSIN, MVT::f32, Expand);
137 setOperationAction(ISD::FSIN, MVT::f64, Expand);
138 setOperationAction(ISD::FCOS, MVT::f32, Expand);
139 setOperationAction(ISD::FCOS, MVT::f64, Expand);
140 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
141 setOperationAction(ISD::FPOW, MVT::f32, Expand);
142 setOperationAction(ISD::FPOW, MVT::f64, Expand);
143 setOperationAction(ISD::FLOG, MVT::f32, Expand);
144 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
145 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
146 setOperationAction(ISD::FEXP, MVT::f32, Expand);
148 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
149 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
151 setOperationAction(ISD::VAARG, MVT::Other, Expand);
152 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
153 setOperationAction(ISD::VAEND, MVT::Other, Expand);
155 // Use the default for now
156 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
157 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
158 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
160 if (Subtarget->isSingleFloat())
161 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
163 if (!Subtarget->hasSEInReg()) {
164 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
165 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
168 if (!Subtarget->hasBitCount())
169 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
171 if (!Subtarget->hasSwap())
172 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
174 setTargetDAGCombine(ISD::ADDE);
175 setTargetDAGCombine(ISD::SUBE);
176 setTargetDAGCombine(ISD::SDIVREM);
177 setTargetDAGCombine(ISD::UDIVREM);
178 setTargetDAGCombine(ISD::SETCC);
180 setMinFunctionAlignment(2);
182 setStackPointerRegisterToSaveRestore(Mips::SP);
183 computeRegisterProperties();
185 setExceptionPointerRegister(Mips::A0);
186 setExceptionSelectorRegister(Mips::A1);
189 MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
194 // Transforms a subgraph in CurDAG if the following pattern is found:
195 // (addc multLo, Lo0), (adde multHi, Hi0),
197 // multHi/Lo: product of multiplication
198 // Lo0: initial value of Lo register
199 // Hi0: initial value of Hi register
200 // Return true if pattern matching was successful.
201 static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
202 // ADDENode's second operand must be a flag output of an ADDC node in order
203 // for the matching to be successful.
204 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
206 if (ADDCNode->getOpcode() != ISD::ADDC)
209 SDValue MultHi = ADDENode->getOperand(0);
210 SDValue MultLo = ADDCNode->getOperand(0);
211 SDNode* MultNode = MultHi.getNode();
212 unsigned MultOpc = MultHi.getOpcode();
214 // MultHi and MultLo must be generated by the same node,
215 if (MultLo.getNode() != MultNode)
218 // and it must be a multiplication.
219 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
222 // MultLo amd MultHi must be the first and second output of MultNode
224 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
227 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
228 // of the values of MultNode, in which case MultNode will be removed in later
230 // If there exist users other than ADDENode or ADDCNode, this function returns
231 // here, which will result in MultNode being mapped to a single MULT
232 // instruction node rather than a pair of MULT and MADD instructions being
234 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
237 SDValue Chain = CurDAG->getEntryNode();
238 DebugLoc dl = ADDENode->getDebugLoc();
240 // create MipsMAdd(u) node
241 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
243 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
245 MultNode->getOperand(0),// Factor 0
246 MultNode->getOperand(1),// Factor 1
247 ADDCNode->getOperand(1),// Lo0
248 ADDENode->getOperand(1));// Hi0
250 // create CopyFromReg nodes
251 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
253 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
255 CopyFromLo.getValue(2));
257 // replace uses of adde and addc here
258 if (!SDValue(ADDCNode, 0).use_empty())
259 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
261 if (!SDValue(ADDENode, 0).use_empty())
262 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
268 // Transforms a subgraph in CurDAG if the following pattern is found:
269 // (addc Lo0, multLo), (sube Hi0, multHi),
271 // multHi/Lo: product of multiplication
272 // Lo0: initial value of Lo register
273 // Hi0: initial value of Hi register
274 // Return true if pattern matching was successful.
275 static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
276 // SUBENode's second operand must be a flag output of an SUBC node in order
277 // for the matching to be successful.
278 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
280 if (SUBCNode->getOpcode() != ISD::SUBC)
283 SDValue MultHi = SUBENode->getOperand(1);
284 SDValue MultLo = SUBCNode->getOperand(1);
285 SDNode* MultNode = MultHi.getNode();
286 unsigned MultOpc = MultHi.getOpcode();
288 // MultHi and MultLo must be generated by the same node,
289 if (MultLo.getNode() != MultNode)
292 // and it must be a multiplication.
293 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
296 // MultLo amd MultHi must be the first and second output of MultNode
298 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
301 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
302 // of the values of MultNode, in which case MultNode will be removed in later
304 // If there exist users other than SUBENode or SUBCNode, this function returns
305 // here, which will result in MultNode being mapped to a single MULT
306 // instruction node rather than a pair of MULT and MSUB instructions being
308 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
311 SDValue Chain = CurDAG->getEntryNode();
312 DebugLoc dl = SUBENode->getDebugLoc();
314 // create MipsSub(u) node
315 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
317 SDValue MSub = CurDAG->getNode(MultOpc, dl,
319 MultNode->getOperand(0),// Factor 0
320 MultNode->getOperand(1),// Factor 1
321 SUBCNode->getOperand(0),// Lo0
322 SUBENode->getOperand(0));// Hi0
324 // create CopyFromReg nodes
325 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
327 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
329 CopyFromLo.getValue(2));
331 // replace uses of sube and subc here
332 if (!SDValue(SUBCNode, 0).use_empty())
333 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
335 if (!SDValue(SUBENode, 0).use_empty())
336 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
341 static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
342 TargetLowering::DAGCombinerInfo &DCI,
343 const MipsSubtarget* Subtarget) {
344 if (DCI.isBeforeLegalize())
347 if (Subtarget->isMips32() && SelectMadd(N, &DAG))
348 return SDValue(N, 0);
353 static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
354 TargetLowering::DAGCombinerInfo &DCI,
355 const MipsSubtarget* Subtarget) {
356 if (DCI.isBeforeLegalize())
359 if (Subtarget->isMips32() && SelectMsub(N, &DAG))
360 return SDValue(N, 0);
365 static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
366 TargetLowering::DAGCombinerInfo &DCI,
367 const MipsSubtarget* Subtarget) {
368 if (DCI.isBeforeLegalizeOps())
371 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
373 DebugLoc dl = N->getDebugLoc();
375 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
376 N->getOperand(0), N->getOperand(1));
377 SDValue InChain = DAG.getEntryNode();
378 SDValue InGlue = DivRem;
381 if (N->hasAnyUseOfValue(0)) {
382 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, Mips::LO, MVT::i32,
384 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
385 InChain = CopyFromLo.getValue(1);
386 InGlue = CopyFromLo.getValue(2);
390 if (N->hasAnyUseOfValue(1)) {
391 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
392 Mips::HI, MVT::i32, InGlue);
393 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
399 static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
401 default: llvm_unreachable("Unknown fp condition code!");
403 case ISD::SETOEQ: return Mips::FCOND_OEQ;
404 case ISD::SETUNE: return Mips::FCOND_UNE;
406 case ISD::SETOLT: return Mips::FCOND_OLT;
408 case ISD::SETOGT: return Mips::FCOND_OGT;
410 case ISD::SETOLE: return Mips::FCOND_OLE;
412 case ISD::SETOGE: return Mips::FCOND_OGE;
413 case ISD::SETULT: return Mips::FCOND_ULT;
414 case ISD::SETULE: return Mips::FCOND_ULE;
415 case ISD::SETUGT: return Mips::FCOND_UGT;
416 case ISD::SETUGE: return Mips::FCOND_UGE;
417 case ISD::SETUO: return Mips::FCOND_UN;
418 case ISD::SETO: return Mips::FCOND_OR;
420 case ISD::SETONE: return Mips::FCOND_ONE;
421 case ISD::SETUEQ: return Mips::FCOND_UEQ;
426 // Returns true if condition code has to be inverted.
427 static bool InvertFPCondCode(Mips::CondCode CC) {
428 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
431 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
434 assert(false && "Illegal Condition Code");
438 // Creates and returns an FPCmp node from a setcc node.
439 // Returns Op if setcc is not a floating point comparison.
440 static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
441 // must be a SETCC node
442 if (Op.getOpcode() != ISD::SETCC)
445 SDValue LHS = Op.getOperand(0);
447 if (!LHS.getValueType().isFloatingPoint())
450 SDValue RHS = Op.getOperand(1);
451 DebugLoc dl = Op.getDebugLoc();
453 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
454 // node if necessary.
455 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
457 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
458 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
461 // Creates and returns a CMovFPT/F node.
462 static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
463 SDValue False, DebugLoc DL) {
464 bool invert = InvertFPCondCode((Mips::CondCode)
465 cast<ConstantSDNode>(Cond.getOperand(2))
468 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
469 True.getValueType(), True, False, Cond);
472 static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
473 TargetLowering::DAGCombinerInfo &DCI,
474 const MipsSubtarget* Subtarget) {
475 if (DCI.isBeforeLegalizeOps())
478 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
480 if (Cond.getOpcode() != MipsISD::FPCmp)
483 SDValue True = DAG.getConstant(1, MVT::i32);
484 SDValue False = DAG.getConstant(0, MVT::i32);
486 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
489 SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
491 SelectionDAG &DAG = DCI.DAG;
492 unsigned opc = N->getOpcode();
497 return PerformADDECombine(N, DAG, DCI, Subtarget);
499 return PerformSUBECombine(N, DAG, DCI, Subtarget);
502 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
504 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
510 SDValue MipsTargetLowering::
511 LowerOperation(SDValue Op, SelectionDAG &DAG) const
513 switch (Op.getOpcode())
515 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
516 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
517 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
518 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
519 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
520 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
521 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
522 case ISD::SELECT: return LowerSELECT(Op, DAG);
523 case ISD::VASTART: return LowerVASTART(Op, DAG);
524 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
525 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
530 //===----------------------------------------------------------------------===//
531 // Lower helper functions
532 //===----------------------------------------------------------------------===//
534 // AddLiveIn - This helper function adds the specified physical register to the
535 // MachineFunction as a live in value. It also creates a corresponding
536 // virtual register for it.
538 AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
540 assert(RC->contains(PReg) && "Not the correct regclass!");
541 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
542 MF.getRegInfo().addLiveIn(PReg, VReg);
546 // Get fp branch code (not opcode) from condition code.
547 static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
548 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
549 return Mips::BRANCH_T;
551 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
552 return Mips::BRANCH_F;
554 return Mips::BRANCH_INVALID;
558 MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
559 MachineBasicBlock *BB) const {
560 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
561 bool isFPCmp = false;
562 DebugLoc dl = MI->getDebugLoc();
565 switch (MI->getOpcode()) {
566 default: assert(false && "Unexpected instr type to insert");
568 case Mips::ATOMIC_LOAD_ADD_I8:
569 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
570 case Mips::ATOMIC_LOAD_ADD_I16:
571 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
572 case Mips::ATOMIC_LOAD_ADD_I32:
573 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
575 case Mips::ATOMIC_LOAD_AND_I8:
576 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
577 case Mips::ATOMIC_LOAD_AND_I16:
578 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
579 case Mips::ATOMIC_LOAD_AND_I32:
580 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
582 case Mips::ATOMIC_LOAD_OR_I8:
583 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
584 case Mips::ATOMIC_LOAD_OR_I16:
585 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
586 case Mips::ATOMIC_LOAD_OR_I32:
587 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
589 case Mips::ATOMIC_LOAD_XOR_I8:
590 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
591 case Mips::ATOMIC_LOAD_XOR_I16:
592 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
593 case Mips::ATOMIC_LOAD_XOR_I32:
594 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
596 case Mips::ATOMIC_LOAD_NAND_I8:
597 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
598 case Mips::ATOMIC_LOAD_NAND_I16:
599 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
600 case Mips::ATOMIC_LOAD_NAND_I32:
601 return EmitAtomicBinary(MI, BB, 4, 0, true);
603 case Mips::ATOMIC_LOAD_SUB_I8:
604 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
605 case Mips::ATOMIC_LOAD_SUB_I16:
606 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
607 case Mips::ATOMIC_LOAD_SUB_I32:
608 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
610 case Mips::ATOMIC_SWAP_I8:
611 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
612 case Mips::ATOMIC_SWAP_I16:
613 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
614 case Mips::ATOMIC_SWAP_I32:
615 return EmitAtomicBinary(MI, BB, 4, 0);
617 case Mips::ATOMIC_CMP_SWAP_I8:
618 return EmitAtomicCmpSwapPartword(MI, BB, 1);
619 case Mips::ATOMIC_CMP_SWAP_I16:
620 return EmitAtomicCmpSwapPartword(MI, BB, 2);
621 case Mips::ATOMIC_CMP_SWAP_I32:
622 return EmitAtomicCmpSwap(MI, BB, 4);
648 // There is no need to expand CMov instructions if target has
649 // conditional moves.
650 if (Subtarget->hasCondMov())
653 // To "insert" a SELECT_CC instruction, we actually have to insert the
654 // diamond control-flow pattern. The incoming instruction knows the
655 // destination vreg to set, the condition code register to branch on, the
656 // true/false values to select between, and a branch opcode to use.
657 const BasicBlock *LLVM_BB = BB->getBasicBlock();
658 MachineFunction::iterator It = BB;
665 // bNE r1, r0, copy1MBB
666 // fallthrough --> copy0MBB
667 MachineBasicBlock *thisMBB = BB;
668 MachineFunction *F = BB->getParent();
669 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
670 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
671 F->insert(It, copy0MBB);
672 F->insert(It, sinkMBB);
674 // Transfer the remainder of BB and its successor edges to sinkMBB.
675 sinkMBB->splice(sinkMBB->begin(), BB,
676 llvm::next(MachineBasicBlock::iterator(MI)),
678 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
680 // Next, add the true and fallthrough blocks as its successors.
681 BB->addSuccessor(copy0MBB);
682 BB->addSuccessor(sinkMBB);
684 // Emit the right instruction according to the type of the operands compared
686 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
688 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
689 .addReg(Mips::ZERO).addMBB(sinkMBB);
694 // # fallthrough to sinkMBB
697 // Update machine-CFG edges
698 BB->addSuccessor(sinkMBB);
701 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
706 BuildMI(*BB, BB->begin(), dl,
707 TII->get(Mips::PHI), MI->getOperand(0).getReg())
708 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
709 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
711 BuildMI(*BB, BB->begin(), dl,
712 TII->get(Mips::PHI), MI->getOperand(0).getReg())
713 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
714 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
716 MI->eraseFromParent(); // The pseudo instruction is gone now.
720 // This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
721 // Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
723 MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
724 unsigned Size, unsigned BinOpcode,
726 assert(Size == 4 && "Unsupported size for EmitAtomicBinary.");
728 MachineFunction *MF = BB->getParent();
729 MachineRegisterInfo &RegInfo = MF->getRegInfo();
730 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
731 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
732 DebugLoc dl = MI->getDebugLoc();
734 unsigned Dest = MI->getOperand(0).getReg();
735 unsigned Ptr = MI->getOperand(1).getReg();
736 unsigned Incr = MI->getOperand(2).getReg();
738 unsigned Oldval = RegInfo.createVirtualRegister(RC);
739 unsigned Tmp1 = RegInfo.createVirtualRegister(RC);
740 unsigned Tmp2 = RegInfo.createVirtualRegister(RC);
742 // insert new blocks after the current block
743 const BasicBlock *LLVM_BB = BB->getBasicBlock();
744 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
745 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
746 MachineFunction::iterator It = BB;
748 MF->insert(It, loopMBB);
749 MF->insert(It, exitMBB);
751 // Transfer the remainder of BB and its successor edges to exitMBB.
752 exitMBB->splice(exitMBB->begin(), BB,
753 llvm::next(MachineBasicBlock::iterator(MI)),
755 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
759 // sw incr, fi(sp) // store incr to stack (when BinOpcode == 0)
760 // fallthrough --> loopMBB
762 // Note: for atomic.swap (when BinOpcode == 0), storing incr to stack before
763 // the loop and then loading it from stack in block loopMBB is necessary to
764 // prevent MachineLICM pass to hoist "or" instruction out of the block
768 if (BinOpcode == 0 && !Nand) {
769 // Get or create a temporary stack location.
770 MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
771 fi = MipsFI->getAtomicFrameIndex();
773 fi = MF->getFrameInfo()->CreateStackObject(Size, Size, false);
774 MipsFI->setAtomicFrameIndex(fi);
777 BuildMI(BB, dl, TII->get(Mips::SW))
778 .addReg(Incr).addImm(0).addFrameIndex(fi);
780 BB->addSuccessor(loopMBB);
784 // or dest, $0, oldval
785 // <binop> tmp1, oldval, incr
787 // beq tmp1, $0, loopMBB
789 BuildMI(BB, dl, TII->get(Mips::LL), Oldval).addImm(0).addReg(Ptr);
790 BuildMI(BB, dl, TII->get(Mips::OR), Dest).addReg(Mips::ZERO).addReg(Oldval);
792 // and tmp2, oldval, incr
793 // nor tmp1, $0, tmp2
794 BuildMI(BB, dl, TII->get(Mips::AND), Tmp2).addReg(Oldval).addReg(Incr);
795 BuildMI(BB, dl, TII->get(Mips::NOR), Tmp1).addReg(Mips::ZERO).addReg(Tmp2);
796 } else if (BinOpcode) {
797 // <binop> tmp1, oldval, incr
798 BuildMI(BB, dl, TII->get(BinOpcode), Tmp1).addReg(Oldval).addReg(Incr);
800 // lw tmp2, fi(sp) // load incr from stack
801 // or tmp1, $zero, tmp2
802 BuildMI(BB, dl, TII->get(Mips::LW), Tmp2).addImm(0).addFrameIndex(fi);;
803 BuildMI(BB, dl, TII->get(Mips::OR), Tmp1).addReg(Mips::ZERO).addReg(Tmp2);
805 BuildMI(BB, dl, TII->get(Mips::SC), Tmp1).addReg(Tmp1).addImm(0).addReg(Ptr);
806 BuildMI(BB, dl, TII->get(Mips::BEQ))
807 .addReg(Tmp1).addReg(Mips::ZERO).addMBB(loopMBB);
808 BB->addSuccessor(loopMBB);
809 BB->addSuccessor(exitMBB);
811 MI->eraseFromParent(); // The instruction is gone now.
817 MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
818 MachineBasicBlock *BB,
819 unsigned Size, unsigned BinOpcode,
821 assert((Size == 1 || Size == 2) &&
822 "Unsupported size for EmitAtomicBinaryPartial.");
824 MachineFunction *MF = BB->getParent();
825 MachineRegisterInfo &RegInfo = MF->getRegInfo();
826 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
827 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
828 DebugLoc dl = MI->getDebugLoc();
830 unsigned Dest = MI->getOperand(0).getReg();
831 unsigned Ptr = MI->getOperand(1).getReg();
832 unsigned Incr = MI->getOperand(2).getReg();
834 unsigned Addr = RegInfo.createVirtualRegister(RC);
835 unsigned Shift = RegInfo.createVirtualRegister(RC);
836 unsigned Mask = RegInfo.createVirtualRegister(RC);
837 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
838 unsigned Newval = RegInfo.createVirtualRegister(RC);
839 unsigned Oldval = RegInfo.createVirtualRegister(RC);
840 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
841 unsigned Tmp1 = RegInfo.createVirtualRegister(RC);
842 unsigned Tmp2 = RegInfo.createVirtualRegister(RC);
843 unsigned Tmp3 = RegInfo.createVirtualRegister(RC);
844 unsigned Tmp4 = RegInfo.createVirtualRegister(RC);
845 unsigned Tmp5 = RegInfo.createVirtualRegister(RC);
846 unsigned Tmp6 = RegInfo.createVirtualRegister(RC);
847 unsigned Tmp7 = RegInfo.createVirtualRegister(RC);
848 unsigned Tmp8 = RegInfo.createVirtualRegister(RC);
849 unsigned Tmp9 = RegInfo.createVirtualRegister(RC);
850 unsigned Tmp10 = RegInfo.createVirtualRegister(RC);
851 unsigned Tmp11 = RegInfo.createVirtualRegister(RC);
852 unsigned Tmp12 = RegInfo.createVirtualRegister(RC);
854 // insert new blocks after the current block
855 const BasicBlock *LLVM_BB = BB->getBasicBlock();
856 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
857 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
858 MachineFunction::iterator It = BB;
860 MF->insert(It, loopMBB);
861 MF->insert(It, exitMBB);
863 // Transfer the remainder of BB and its successor edges to exitMBB.
864 exitMBB->splice(exitMBB->begin(), BB,
865 llvm::next(MachineBasicBlock::iterator(MI)),
867 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
870 // addiu tmp1,$0,-4 # 0xfffffffc
874 // ori tmp3,$0,255 # 0xff
875 // sll mask,tmp3,shift
877 // andi tmp4,incr,255
878 // sll incr2,tmp4,shift
879 // sw incr2, fi(sp) // store incr2 to stack (when BinOpcode == 0)
881 // Note: for atomic.swap (when BinOpcode == 0), storing incr2 to stack before
882 // the loop and then loading it from stack in block loopMBB is necessary to
883 // prevent MachineLICM pass to hoist "or" instruction out of the block
886 int64_t MaskImm = (Size == 1) ? 255 : 65535;
887 BuildMI(BB, dl, TII->get(Mips::ADDiu), Tmp1).addReg(Mips::ZERO).addImm(-4);
888 BuildMI(BB, dl, TII->get(Mips::AND), Addr).addReg(Ptr).addReg(Tmp1);
889 BuildMI(BB, dl, TII->get(Mips::ANDi), Tmp2).addReg(Ptr).addImm(3);
890 BuildMI(BB, dl, TII->get(Mips::SLL), Shift).addReg(Tmp2).addImm(3);
891 BuildMI(BB, dl, TII->get(Mips::ORi), Tmp3).addReg(Mips::ZERO).addImm(MaskImm);
892 BuildMI(BB, dl, TII->get(Mips::SLL), Mask).addReg(Tmp3).addReg(Shift);
893 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
894 if (BinOpcode != Mips::SUBu) {
895 BuildMI(BB, dl, TII->get(Mips::ANDi), Tmp4).addReg(Incr).addImm(MaskImm);
896 BuildMI(BB, dl, TII->get(Mips::SLL), Incr2).addReg(Tmp4).addReg(Shift);
898 BuildMI(BB, dl, TII->get(Mips::SUBu), Tmp4).addReg(Mips::ZERO).addReg(Incr);
899 BuildMI(BB, dl, TII->get(Mips::ANDi), Tmp5).addReg(Tmp4).addImm(MaskImm);
900 BuildMI(BB, dl, TII->get(Mips::SLL), Incr2).addReg(Tmp5).addReg(Shift);
904 if (BinOpcode == 0 && !Nand) {
905 // Get or create a temporary stack location.
906 MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
907 fi = MipsFI->getAtomicFrameIndex();
909 fi = MF->getFrameInfo()->CreateStackObject(Size, Size, false);
910 MipsFI->setAtomicFrameIndex(fi);
913 BuildMI(BB, dl, TII->get(Mips::SW))
914 .addReg(Incr2).addImm(0).addFrameIndex(fi);
916 BB->addSuccessor(loopMBB);
920 // binop tmp7,oldval,incr2
921 // and newval,tmp7,mask
922 // and tmp8,oldval,mask2
923 // or tmp9,tmp8,newval
925 // beq tmp9,$0,loopMBB
927 BuildMI(BB, dl, TII->get(Mips::LL), Oldval).addImm(0).addReg(Addr);
929 // and tmp6, oldval, incr2
930 // nor tmp7, $0, tmp6
931 BuildMI(BB, dl, TII->get(Mips::AND), Tmp6).addReg(Oldval).addReg(Incr2);
932 BuildMI(BB, dl, TII->get(Mips::NOR), Tmp7).addReg(Mips::ZERO).addReg(Tmp6);
933 } else if (BinOpcode == Mips::SUBu) {
934 // addu tmp7, oldval, incr2
935 BuildMI(BB, dl, TII->get(Mips::ADDu), Tmp7).addReg(Oldval).addReg(Incr2);
936 } else if (BinOpcode) {
937 // <binop> tmp7, oldval, incr2
938 BuildMI(BB, dl, TII->get(BinOpcode), Tmp7).addReg(Oldval).addReg(Incr2);
940 // lw tmp6, fi(sp) // load incr2 from stack
941 // or tmp7, $zero, tmp6
942 BuildMI(BB, dl, TII->get(Mips::LW), Tmp6).addImm(0).addFrameIndex(fi);;
943 BuildMI(BB, dl, TII->get(Mips::OR), Tmp7).addReg(Mips::ZERO).addReg(Tmp6);
945 BuildMI(BB, dl, TII->get(Mips::AND), Newval).addReg(Tmp7).addReg(Mask);
946 BuildMI(BB, dl, TII->get(Mips::AND), Tmp8).addReg(Oldval).addReg(Mask2);
947 BuildMI(BB, dl, TII->get(Mips::OR), Tmp9).addReg(Tmp8).addReg(Newval);
948 BuildMI(BB, dl, TII->get(Mips::SC), Tmp9).addReg(Tmp9).addImm(0).addReg(Addr);
949 BuildMI(BB, dl, TII->get(Mips::BEQ))
950 .addReg(Tmp9).addReg(Mips::ZERO).addMBB(loopMBB);
951 BB->addSuccessor(loopMBB);
952 BB->addSuccessor(exitMBB);
955 // and tmp10,oldval,mask
956 // srl tmp11,tmp10,shift
957 // sll tmp12,tmp11,24
960 int64_t ShiftImm = (Size == 1) ? 24 : 16;
962 BuildMI(*BB, BB->begin(), dl, TII->get(Mips::SRA), Dest)
963 .addReg(Tmp12).addImm(ShiftImm);
964 BuildMI(*BB, BB->begin(), dl, TII->get(Mips::SLL), Tmp12)
965 .addReg(Tmp11).addImm(ShiftImm);
966 BuildMI(*BB, BB->begin(), dl, TII->get(Mips::SRL), Tmp11)
967 .addReg(Tmp10).addReg(Shift);
968 BuildMI(*BB, BB->begin(), dl, TII->get(Mips::AND), Tmp10)
969 .addReg(Oldval).addReg(Mask);
971 MI->eraseFromParent(); // The instruction is gone now.
977 MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
978 MachineBasicBlock *BB,
979 unsigned Size) const {
980 assert(Size == 4 && "Unsupported size for EmitAtomicCmpSwap.");
982 MachineFunction *MF = BB->getParent();
983 MachineRegisterInfo &RegInfo = MF->getRegInfo();
984 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
985 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
986 DebugLoc dl = MI->getDebugLoc();
988 unsigned Dest = MI->getOperand(0).getReg();
989 unsigned Ptr = MI->getOperand(1).getReg();
990 unsigned Oldval = MI->getOperand(2).getReg();
991 unsigned Newval = MI->getOperand(3).getReg();
993 unsigned Tmp1 = RegInfo.createVirtualRegister(RC);
994 unsigned Tmp2 = RegInfo.createVirtualRegister(RC);
996 // insert new blocks after the current block
997 const BasicBlock *LLVM_BB = BB->getBasicBlock();
998 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
999 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1000 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1001 MachineFunction::iterator It = BB;
1003 MF->insert(It, loop1MBB);
1004 MF->insert(It, loop2MBB);
1005 MF->insert(It, exitMBB);
1007 // Transfer the remainder of BB and its successor edges to exitMBB.
1008 exitMBB->splice(exitMBB->begin(), BB,
1009 llvm::next(MachineBasicBlock::iterator(MI)),
1011 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1013 // Get or create a temporary stack location.
1014 MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
1015 int fi = MipsFI->getAtomicFrameIndex();
1017 fi = MF->getFrameInfo()->CreateStackObject(Size, Size, false);
1018 MipsFI->setAtomicFrameIndex(fi);
1023 // sw newval, fi(sp) // store newval to stack
1024 // fallthrough --> loop1MBB
1026 // Note: storing newval to stack before the loop and then loading it from
1027 // stack in block loop2MBB is necessary to prevent MachineLICM pass to
1028 // hoist "or" instruction out of the block loop2MBB.
1030 BuildMI(BB, dl, TII->get(Mips::SW))
1031 .addReg(Newval).addImm(0).addFrameIndex(fi);
1032 BB->addSuccessor(loop1MBB);
1036 // bne dest, oldval, exitMBB
1038 BuildMI(BB, dl, TII->get(Mips::LL), Dest).addImm(0).addReg(Ptr);
1039 BuildMI(BB, dl, TII->get(Mips::BNE))
1040 .addReg(Dest).addReg(Oldval).addMBB(exitMBB);
1041 BB->addSuccessor(exitMBB);
1042 BB->addSuccessor(loop2MBB);
1045 // lw tmp2, fi(sp) // load newval from stack
1046 // or tmp1, $0, tmp2
1048 // beq tmp1, $0, loop1MBB
1050 BuildMI(BB, dl, TII->get(Mips::LW), Tmp2).addImm(0).addFrameIndex(fi);;
1051 BuildMI(BB, dl, TII->get(Mips::OR), Tmp1).addReg(Mips::ZERO).addReg(Tmp2);
1052 BuildMI(BB, dl, TII->get(Mips::SC), Tmp1).addReg(Tmp1).addImm(0).addReg(Ptr);
1053 BuildMI(BB, dl, TII->get(Mips::BEQ))
1054 .addReg(Tmp1).addReg(Mips::ZERO).addMBB(loop1MBB);
1055 BB->addSuccessor(loop1MBB);
1056 BB->addSuccessor(exitMBB);
1058 MI->eraseFromParent(); // The instruction is gone now.
1064 MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
1065 MachineBasicBlock *BB,
1066 unsigned Size) const {
1067 assert((Size == 1 || Size == 2) &&
1068 "Unsupported size for EmitAtomicCmpSwapPartial.");
1070 MachineFunction *MF = BB->getParent();
1071 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1072 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1073 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1074 DebugLoc dl = MI->getDebugLoc();
1076 unsigned Dest = MI->getOperand(0).getReg();
1077 unsigned Ptr = MI->getOperand(1).getReg();
1078 unsigned Oldval = MI->getOperand(2).getReg();
1079 unsigned Newval = MI->getOperand(3).getReg();
1081 unsigned Addr = RegInfo.createVirtualRegister(RC);
1082 unsigned Shift = RegInfo.createVirtualRegister(RC);
1083 unsigned Mask = RegInfo.createVirtualRegister(RC);
1084 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
1085 unsigned Oldval2 = RegInfo.createVirtualRegister(RC);
1086 unsigned Oldval3 = RegInfo.createVirtualRegister(RC);
1087 unsigned Oldval4 = RegInfo.createVirtualRegister(RC);
1088 unsigned Newval2 = RegInfo.createVirtualRegister(RC);
1089 unsigned Tmp1 = RegInfo.createVirtualRegister(RC);
1090 unsigned Tmp2 = RegInfo.createVirtualRegister(RC);
1091 unsigned Tmp3 = RegInfo.createVirtualRegister(RC);
1092 unsigned Tmp4 = RegInfo.createVirtualRegister(RC);
1093 unsigned Tmp5 = RegInfo.createVirtualRegister(RC);
1094 unsigned Tmp6 = RegInfo.createVirtualRegister(RC);
1095 unsigned Tmp7 = RegInfo.createVirtualRegister(RC);
1096 unsigned Tmp8 = RegInfo.createVirtualRegister(RC);
1097 unsigned Tmp9 = RegInfo.createVirtualRegister(RC);
1099 // insert new blocks after the current block
1100 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1101 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1102 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1103 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1104 MachineFunction::iterator It = BB;
1106 MF->insert(It, loop1MBB);
1107 MF->insert(It, loop2MBB);
1108 MF->insert(It, exitMBB);
1110 // Transfer the remainder of BB and its successor edges to exitMBB.
1111 exitMBB->splice(exitMBB->begin(), BB,
1112 llvm::next(MachineBasicBlock::iterator(MI)),
1114 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1117 // addiu tmp1,$0,-4 # 0xfffffffc
1118 // and addr,ptr,tmp1
1121 // ori tmp3,$0,255 # 0xff
1122 // sll mask,tmp3,shift
1123 // nor mask2,$0,mask
1124 // andi tmp4,oldval,255
1125 // sll oldval2,tmp4,shift
1126 // andi tmp5,newval,255
1127 // sll newval2,tmp5,shift
1128 int64_t MaskImm = (Size == 1) ? 255 : 65535;
1129 BuildMI(BB, dl, TII->get(Mips::ADDiu), Tmp1).addReg(Mips::ZERO).addImm(-4);
1130 BuildMI(BB, dl, TII->get(Mips::AND), Addr).addReg(Ptr).addReg(Tmp1);
1131 BuildMI(BB, dl, TII->get(Mips::ANDi), Tmp2).addReg(Ptr).addImm(3);
1132 BuildMI(BB, dl, TII->get(Mips::SLL), Shift).addReg(Tmp2).addImm(3);
1133 BuildMI(BB, dl, TII->get(Mips::ORi), Tmp3).addReg(Mips::ZERO).addImm(MaskImm);
1134 BuildMI(BB, dl, TII->get(Mips::SLL), Mask).addReg(Tmp3).addReg(Shift);
1135 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1136 BuildMI(BB, dl, TII->get(Mips::ANDi), Tmp4).addReg(Oldval).addImm(MaskImm);
1137 BuildMI(BB, dl, TII->get(Mips::SLL), Oldval2).addReg(Tmp4).addReg(Shift);
1138 BuildMI(BB, dl, TII->get(Mips::ANDi), Tmp5).addReg(Newval).addImm(MaskImm);
1139 BuildMI(BB, dl, TII->get(Mips::SLL), Newval2).addReg(Tmp5).addReg(Shift);
1140 BB->addSuccessor(loop1MBB);
1143 // ll oldval3,0(addr)
1144 // and oldval4,oldval3,mask
1145 // bne oldval4,oldval2,exitMBB
1147 BuildMI(BB, dl, TII->get(Mips::LL), Oldval3).addImm(0).addReg(Addr);
1148 BuildMI(BB, dl, TII->get(Mips::AND), Oldval4).addReg(Oldval3).addReg(Mask);
1149 BuildMI(BB, dl, TII->get(Mips::BNE))
1150 .addReg(Oldval4).addReg(Oldval2).addMBB(exitMBB);
1151 BB->addSuccessor(exitMBB);
1152 BB->addSuccessor(loop2MBB);
1155 // and tmp6,oldval3,mask2
1156 // or tmp7,tmp6,newval2
1158 // beq tmp7,$0,loop1MBB
1160 BuildMI(BB, dl, TII->get(Mips::AND), Tmp6).addReg(Oldval3).addReg(Mask2);
1161 BuildMI(BB, dl, TII->get(Mips::OR), Tmp7).addReg(Tmp6).addReg(Newval2);
1162 BuildMI(BB, dl, TII->get(Mips::SC), Tmp7)
1163 .addReg(Tmp7).addImm(0).addReg(Addr);
1164 BuildMI(BB, dl, TII->get(Mips::BEQ))
1165 .addReg(Tmp7).addReg(Mips::ZERO).addMBB(loop1MBB);
1166 BB->addSuccessor(loop1MBB);
1167 BB->addSuccessor(exitMBB);
1170 // srl tmp8,oldval4,shift
1174 int64_t ShiftImm = (Size == 1) ? 24 : 16;
1176 BuildMI(*BB, BB->begin(), dl, TII->get(Mips::SRA), Dest)
1177 .addReg(Tmp9).addImm(ShiftImm);
1178 BuildMI(*BB, BB->begin(), dl, TII->get(Mips::SLL), Tmp9)
1179 .addReg(Tmp8).addImm(ShiftImm);
1180 BuildMI(*BB, BB->begin(), dl, TII->get(Mips::SRL), Tmp8)
1181 .addReg(Oldval4).addReg(Shift);
1183 MI->eraseFromParent(); // The instruction is gone now.
1188 //===----------------------------------------------------------------------===//
1189 // Misc Lower Operation implementation
1190 //===----------------------------------------------------------------------===//
1191 SDValue MipsTargetLowering::
1192 LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
1194 unsigned StackAlignment =
1195 getTargetMachine().getFrameLowering()->getStackAlignment();
1196 assert(StackAlignment >=
1197 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1198 "Cannot lower if the alignment of the allocated space is larger than \
1199 that of the stack.");
1201 SDValue Chain = Op.getOperand(0);
1202 SDValue Size = Op.getOperand(1);
1203 DebugLoc dl = Op.getDebugLoc();
1205 // Get a reference from Mips stack pointer
1206 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
1208 // Subtract the dynamic size from the actual stack size to
1209 // obtain the new stack size.
1210 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
1212 // The Sub result contains the new stack start address, so it
1213 // must be placed in the stack pointer register.
1214 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
1216 // Retrieve updated $sp. There is a glue input to prevent instructions that
1217 // clobber $sp from being inserted between copytoreg and copyfromreg.
1218 SDValue NewSP = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32,
1221 // The stack space reserved by alloca is located right above the argument
1222 // area. It is aligned on a boundary that is a multiple of StackAlignment.
1223 MachineFunction &MF = DAG.getMachineFunction();
1224 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1225 unsigned SPOffset = (MipsFI->getMaxCallFrameSize() + StackAlignment - 1) /
1226 StackAlignment * StackAlignment;
1227 SDValue AllocPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
1228 DAG.getConstant(SPOffset, MVT::i32));
1230 // This node always has two return values: a new stack pointer
1231 // value and a chain
1232 SDValue Ops[2] = { AllocPtr, NewSP.getValue(1) };
1233 return DAG.getMergeValues(Ops, 2, dl);
1236 SDValue MipsTargetLowering::
1237 LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
1239 // The first operand is the chain, the second is the condition, the third is
1240 // the block to branch to if the condition is true.
1241 SDValue Chain = Op.getOperand(0);
1242 SDValue Dest = Op.getOperand(2);
1243 DebugLoc dl = Op.getDebugLoc();
1245 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1247 // Return if flag is not set by a floating point comparison.
1248 if (CondRes.getOpcode() != MipsISD::FPCmp)
1251 SDValue CCNode = CondRes.getOperand(2);
1253 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
1254 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
1256 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
1260 SDValue MipsTargetLowering::
1261 LowerSELECT(SDValue Op, SelectionDAG &DAG) const
1263 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
1265 // Return if flag is not set by a floating point comparison.
1266 if (Cond.getOpcode() != MipsISD::FPCmp)
1269 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1273 SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1274 SelectionDAG &DAG) const {
1275 // FIXME there isn't actually debug info here
1276 DebugLoc dl = Op.getDebugLoc();
1277 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1279 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
1280 SDVTList VTs = DAG.getVTList(MVT::i32);
1282 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
1284 // %gp_rel relocation
1285 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1286 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1288 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1289 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
1290 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
1292 // %hi/%lo relocation
1293 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1295 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1297 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1298 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
1299 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
1302 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1304 GA = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, GA);
1305 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
1306 DAG.getEntryNode(), GA, MachinePointerInfo(),
1308 // On functions and global targets not internal linked only
1309 // a load from got/GP is necessary for PIC to work.
1310 if (!GV->hasInternalLinkage() &&
1311 (!GV->hasLocalLinkage() || isa<Function>(GV)))
1313 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1315 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
1316 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
1319 SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1320 SelectionDAG &DAG) const {
1321 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1322 // FIXME there isn't actually debug info here
1323 DebugLoc dl = Op.getDebugLoc();
1325 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
1326 // %hi/%lo relocation
1327 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1329 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1331 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1332 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1333 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1336 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1338 BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, BAGOTOffset);
1339 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1341 SDValue Load = DAG.getLoad(MVT::i32, dl,
1342 DAG.getEntryNode(), BAGOTOffset,
1343 MachinePointerInfo(), false, false, 0);
1344 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
1345 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
1348 SDValue MipsTargetLowering::
1349 LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
1351 // If the relocation model is PIC, use the General Dynamic TLS Model,
1352 // otherwise use the Initial Exec or Local Exec TLS Model.
1353 // TODO: implement Local Dynamic TLS model
1355 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1356 DebugLoc dl = GA->getDebugLoc();
1357 const GlobalValue *GV = GA->getGlobal();
1358 EVT PtrVT = getPointerTy();
1360 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1361 // General Dynamic TLS Model
1362 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32,
1363 0, MipsII::MO_TLSGD);
1364 SDValue Tlsgd = DAG.getNode(MipsISD::TlsGd, dl, MVT::i32, TGA);
1365 SDValue GP = DAG.getRegister(Mips::GP, MVT::i32);
1366 SDValue Argument = DAG.getNode(ISD::ADD, dl, MVT::i32, GP, Tlsgd);
1370 Entry.Node = Argument;
1371 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
1372 Args.push_back(Entry);
1373 std::pair<SDValue, SDValue> CallResult =
1374 LowerCallTo(DAG.getEntryNode(),
1375 (const Type *) Type::getInt32Ty(*DAG.getContext()),
1376 false, false, false, false,
1377 0, CallingConv::C, false, true,
1378 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
1380 return CallResult.first;
1383 if (GV->isDeclaration()) {
1384 // Initial Exec TLS Model
1385 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1386 MipsII::MO_GOTTPREL);
1387 Offset = DAG.getLoad(MVT::i32, dl,
1388 DAG.getEntryNode(), TGA, MachinePointerInfo(),
1391 // Local Exec TLS Model
1392 SDVTList VTs = DAG.getVTList(MVT::i32);
1393 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1394 MipsII::MO_TPREL_HI);
1395 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1396 MipsII::MO_TPREL_LO);
1397 SDValue Hi = DAG.getNode(MipsISD::TprelHi, dl, VTs, &TGAHi, 1);
1398 SDValue Lo = DAG.getNode(MipsISD::TprelLo, dl, MVT::i32, TGALo);
1399 Offset = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1402 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1403 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
1407 SDValue MipsTargetLowering::
1408 LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
1412 // FIXME there isn't actually debug info here
1413 DebugLoc dl = Op.getDebugLoc();
1414 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
1415 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
1417 EVT PtrVT = Op.getValueType();
1418 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
1420 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
1423 SDValue Ops[] = { JTI };
1424 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
1425 } else {// Emit Load from Global Pointer
1426 JTI = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, JTI);
1427 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
1428 MachinePointerInfo(),
1432 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1434 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
1435 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
1440 SDValue MipsTargetLowering::
1441 LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
1444 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1445 const Constant *C = N->getConstVal();
1446 // FIXME there isn't actually debug info here
1447 DebugLoc dl = Op.getDebugLoc();
1449 // gp_rel relocation
1450 // FIXME: we should reference the constant pool using small data sections,
1451 // but the asm printer currently doesn't support this feature without
1452 // hacking it. This feature should come soon so we can uncomment the
1454 //if (IsInSmallSection(C->getType())) {
1455 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1456 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
1457 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
1459 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
1460 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
1461 N->getOffset(), MipsII::MO_ABS_HI);
1462 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
1463 N->getOffset(), MipsII::MO_ABS_LO);
1464 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1465 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
1466 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
1468 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
1469 N->getOffset(), MipsII::MO_GOT);
1470 CP = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, CP);
1471 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
1472 CP, MachinePointerInfo::getConstantPool(),
1474 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
1475 N->getOffset(), MipsII::MO_ABS_LO);
1476 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
1477 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
1483 SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1484 MachineFunction &MF = DAG.getMachineFunction();
1485 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1487 DebugLoc dl = Op.getDebugLoc();
1488 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1491 // vastart just stores the address of the VarArgsFrameIndex slot into the
1492 // memory location argument.
1493 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1494 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1495 MachinePointerInfo(SV),
1499 static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
1500 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
1501 DebugLoc dl = Op.getDebugLoc();
1502 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
1503 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
1504 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
1505 DAG.getConstant(0x7fffffff, MVT::i32));
1506 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
1507 DAG.getConstant(0x80000000, MVT::i32));
1508 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1509 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
1512 static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
1514 // Use ext/ins instructions if target architecture is Mips32r2.
1515 // Eliminate redundant mfc1 and mtc1 instructions.
1516 unsigned LoIdx = 0, HiIdx = 1;
1519 std::swap(LoIdx, HiIdx);
1521 DebugLoc dl = Op.getDebugLoc();
1522 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1524 DAG.getConstant(LoIdx, MVT::i32));
1525 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1526 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1527 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1528 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1529 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1530 DAG.getConstant(0x7fffffff, MVT::i32));
1531 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1532 DAG.getConstant(0x80000000, MVT::i32));
1533 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1536 std::swap(Word0, Word1);
1538 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1541 SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1543 EVT Ty = Op.getValueType();
1545 assert(Ty == MVT::f32 || Ty == MVT::f64);
1548 return LowerFCOPYSIGN32(Op, DAG);
1550 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
1553 SDValue MipsTargetLowering::
1554 LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
1555 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1556 assert((Depth == 0) &&
1557 "Frame address can only be determined for current frame.");
1559 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1560 MFI->setFrameAddressIsTaken(true);
1561 EVT VT = Op.getValueType();
1562 DebugLoc dl = Op.getDebugLoc();
1563 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Mips::FP, VT);
1567 //===----------------------------------------------------------------------===//
1568 // Calling Convention Implementation
1569 //===----------------------------------------------------------------------===//
1571 #include "MipsGenCallingConv.inc"
1573 //===----------------------------------------------------------------------===//
1574 // TODO: Implement a generic logic using tblgen that can support this.
1575 // Mips O32 ABI rules:
1577 // i32 - Passed in A0, A1, A2, A3 and stack
1578 // f32 - Only passed in f32 registers if no int reg has been used yet to hold
1579 // an argument. Otherwise, passed in A1, A2, A3 and stack.
1580 // f64 - Only passed in two aliased f32 registers if no int reg has been used
1581 // yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
1582 // not used, it must be shadowed. If only A3 is avaiable, shadow it and
1585 // For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
1586 //===----------------------------------------------------------------------===//
1588 static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
1589 MVT LocVT, CCValAssign::LocInfo LocInfo,
1590 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1592 static const unsigned IntRegsSize=4, FloatRegsSize=2;
1594 static const unsigned IntRegs[] = {
1595 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1597 static const unsigned F32Regs[] = {
1598 Mips::F12, Mips::F14
1600 static const unsigned F64Regs[] = {
1605 if (ArgFlags.isByVal()) {
1606 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1607 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1608 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1609 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1610 r < std::min(IntRegsSize, NextReg); ++r)
1611 State.AllocateReg(IntRegs[r]);
1615 // Promote i8 and i16
1616 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1618 if (ArgFlags.isSExt())
1619 LocInfo = CCValAssign::SExt;
1620 else if (ArgFlags.isZExt())
1621 LocInfo = CCValAssign::ZExt;
1623 LocInfo = CCValAssign::AExt;
1628 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1629 // is true: function is vararg, argument is 3rd or higher, there is previous
1630 // argument which is not f32 or f64.
1631 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1632 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
1633 unsigned OrigAlign = ArgFlags.getOrigAlign();
1634 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
1636 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
1637 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1638 // If this is the first part of an i64 arg,
1639 // the allocated register must be either A0 or A2.
1640 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1641 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1643 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1644 // Allocate int register and shadow next int register. If first
1645 // available register is Mips::A1 or Mips::A3, shadow it too.
1646 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1647 if (Reg == Mips::A1 || Reg == Mips::A3)
1648 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1649 State.AllocateReg(IntRegs, IntRegsSize);
1651 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1652 // we are guaranteed to find an available float register
1653 if (ValVT == MVT::f32) {
1654 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1655 // Shadow int register
1656 State.AllocateReg(IntRegs, IntRegsSize);
1658 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1659 // Shadow int registers
1660 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1661 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1662 State.AllocateReg(IntRegs, IntRegsSize);
1663 State.AllocateReg(IntRegs, IntRegsSize);
1666 llvm_unreachable("Cannot handle this ValVT.");
1668 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1669 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1672 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
1674 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
1676 return false; // CC must always match
1679 //===----------------------------------------------------------------------===//
1680 // Call Calling Convention Implementation
1681 //===----------------------------------------------------------------------===//
1683 static const unsigned O32IntRegsSize = 4;
1685 static const unsigned O32IntRegs[] = {
1686 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1689 // Write ByVal Arg to arg registers and stack.
1691 WriteByValArg(SDValue& Chain, DebugLoc dl,
1692 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1693 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1694 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
1695 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
1697 unsigned FirstWord = VA.getLocMemOffset() / 4;
1698 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
1699 unsigned LastWord = FirstWord + NumWords;
1702 // copy the first 4 words of byval arg to registers A0 - A3
1703 for (CurWord = FirstWord; CurWord < std::min(LastWord, O32IntRegsSize);
1705 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1706 DAG.getConstant((CurWord - FirstWord) * 4,
1708 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1709 MachinePointerInfo(),
1711 MemOpChains.push_back(LoadVal.getValue(1));
1712 unsigned DstReg = O32IntRegs[CurWord];
1713 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1716 // copy remaining part of byval arg to stack.
1717 if (CurWord < LastWord) {
1718 unsigned SizeInBytes = (LastWord - CurWord) * 4;
1719 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1720 DAG.getConstant((CurWord - FirstWord) * 4,
1722 LastFI = MFI->CreateFixedObject(SizeInBytes, CurWord * 4, true);
1723 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
1724 Chain = DAG.getMemcpy(Chain, dl, Dst, Src,
1725 DAG.getConstant(SizeInBytes, MVT::i32),
1727 /*isVolatile=*/false, /*AlwaysInline=*/false,
1728 MachinePointerInfo(0), MachinePointerInfo(0));
1729 MemOpChains.push_back(Chain);
1733 /// LowerCall - functions arguments are copied from virtual regs to
1734 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
1735 /// TODO: isTailCall.
1737 MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
1738 CallingConv::ID CallConv, bool isVarArg,
1740 const SmallVectorImpl<ISD::OutputArg> &Outs,
1741 const SmallVectorImpl<SDValue> &OutVals,
1742 const SmallVectorImpl<ISD::InputArg> &Ins,
1743 DebugLoc dl, SelectionDAG &DAG,
1744 SmallVectorImpl<SDValue> &InVals) const {
1745 // MIPs target does not yet support tail call optimization.
1748 MachineFunction &MF = DAG.getMachineFunction();
1749 MachineFrameInfo *MFI = MF.getFrameInfo();
1750 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
1751 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
1752 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1754 // Analyze operands of the call, assigning locations to each operand.
1755 SmallVector<CCValAssign, 16> ArgLocs;
1756 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1759 if (Subtarget->isABI_O32())
1760 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
1762 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
1764 // Get a count of how many bytes are to be pushed on the stack.
1765 unsigned NumBytes = CCInfo.getNextStackOffset();
1766 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
1768 // With EABI is it possible to have 16 args on registers.
1769 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1770 SmallVector<SDValue, 8> MemOpChains;
1772 // If this is the first call, create a stack frame object that points to
1773 // a location to which .cprestore saves $gp. The offset of this frame object
1774 // is set to 0, since we know nothing about the size of the argument area at
1776 if (IsPIC && !MipsFI->getGPFI())
1777 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1779 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
1781 // Walk the register/memloc assignments, inserting copies/loads.
1782 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1783 SDValue Arg = OutVals[i];
1784 CCValAssign &VA = ArgLocs[i];
1786 // Promote the value if needed.
1787 switch (VA.getLocInfo()) {
1788 default: llvm_unreachable("Unknown loc info!");
1789 case CCValAssign::Full:
1790 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
1791 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
1792 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
1793 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
1794 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1795 Arg, DAG.getConstant(0, MVT::i32));
1796 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1797 Arg, DAG.getConstant(1, MVT::i32));
1798 if (!Subtarget->isLittle())
1800 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
1801 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
1806 case CCValAssign::SExt:
1807 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1809 case CCValAssign::ZExt:
1810 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1812 case CCValAssign::AExt:
1813 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1817 // Arguments that can be passed on register must be kept at
1818 // RegsToPass vector
1819 if (VA.isRegLoc()) {
1820 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1824 // Register can't get to this point...
1825 assert(VA.isMemLoc());
1828 ISD::ArgFlagsTy Flags = Outs[i].Flags;
1829 if (Flags.isByVal()) {
1830 assert(Subtarget->isABI_O32() &&
1831 "No support for ByVal args by ABIs other than O32 yet.");
1832 assert(Flags.getByValSize() &&
1833 "ByVal args of size 0 should have been ignored by front-end.");
1834 WriteByValArg(Chain, dl, RegsToPass, MemOpChains, LastFI, MFI, DAG, Arg,
1835 VA, Flags, getPointerTy());
1839 // Create the frame index object for this incoming parameter
1840 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
1841 VA.getLocMemOffset(), true);
1842 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
1844 // emit ISD::STORE whichs stores the
1845 // parameter value to a stack Location
1846 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
1847 MachinePointerInfo(),
1851 // Transform all store nodes into one single node because all store
1852 // nodes are independent of each other.
1853 if (!MemOpChains.empty())
1854 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1855 &MemOpChains[0], MemOpChains.size());
1857 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1858 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1859 // node so that legalize doesn't hack it.
1860 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
1861 bool LoadSymAddr = false;
1864 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1865 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
1866 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1867 getPointerTy(), 0,MipsII:: MO_GOT);
1868 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
1869 0, MipsII::MO_ABS_LO);
1871 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1872 getPointerTy(), 0, OpFlag);
1877 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1878 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
1879 getPointerTy(), OpFlag);
1885 // Create nodes that load address of callee and copy it to T9
1888 // Load callee address
1889 Callee = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, Callee);
1890 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, Chain, Callee,
1891 MachinePointerInfo::getGOT(),
1894 // Use GOT+LO if callee has internal linkage.
1895 if (CalleeLo.getNode()) {
1896 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
1897 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
1901 // Use chain output from LoadValue
1902 Chain = LoadValue.getValue(1);
1906 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
1907 InFlag = Chain.getValue(1);
1908 Callee = DAG.getRegister(Mips::T9, MVT::i32);
1911 // Build a sequence of copy-to-reg nodes chained together with token
1912 // chain and flag operands which copy the outgoing args into registers.
1913 // The InFlag in necessary since all emitted instructions must be
1915 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1916 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1917 RegsToPass[i].second, InFlag);
1918 InFlag = Chain.getValue(1);
1921 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
1922 // = Chain, Callee, Reg#1, Reg#2, ...
1924 // Returns a chain & a flag for retval copy to use.
1925 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1926 SmallVector<SDValue, 8> Ops;
1927 Ops.push_back(Chain);
1928 Ops.push_back(Callee);
1930 // Add argument registers to the end of the list so that they are
1931 // known live into the call.
1932 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1933 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1934 RegsToPass[i].second.getValueType()));
1936 if (InFlag.getNode())
1937 Ops.push_back(InFlag);
1939 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
1940 InFlag = Chain.getValue(1);
1942 // Function can have an arbitrary number of calls, so
1943 // hold the LastArgStackLoc with the biggest offset.
1944 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1945 unsigned NextStackOffset = CCInfo.getNextStackOffset();
1947 // For O32, a minimum of four words (16 bytes) of argument space is
1949 if (Subtarget->isABI_O32())
1950 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
1952 if (MaxCallFrameSize < NextStackOffset) {
1953 MipsFI->setMaxCallFrameSize(NextStackOffset);
1956 // $gp restore slot must be aligned.
1957 unsigned StackAlignment = TFL->getStackAlignment();
1958 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
1959 StackAlignment * StackAlignment;
1960 int GPFI = MipsFI->getGPFI();
1961 MFI->setObjectOffset(GPFI, NextStackOffset);
1965 // Extend range of indices of frame objects for outgoing arguments that were
1966 // created during this function call. Skip this step if no such objects were
1969 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
1971 // Create the CALLSEQ_END node.
1972 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1973 DAG.getIntPtrConstant(0, true), InFlag);
1974 InFlag = Chain.getValue(1);
1976 // Handle result values, copying them out of physregs into vregs that we
1978 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
1979 Ins, dl, DAG, InVals);
1982 /// LowerCallResult - Lower the result values of a call into the
1983 /// appropriate copies out of appropriate physical registers.
1985 MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
1986 CallingConv::ID CallConv, bool isVarArg,
1987 const SmallVectorImpl<ISD::InputArg> &Ins,
1988 DebugLoc dl, SelectionDAG &DAG,
1989 SmallVectorImpl<SDValue> &InVals) const {
1990 // Assign locations to each value returned by this call.
1991 SmallVector<CCValAssign, 16> RVLocs;
1992 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1993 RVLocs, *DAG.getContext());
1995 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
1997 // Copy all of the result registers out of their specified physreg.
1998 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1999 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
2000 RVLocs[i].getValVT(), InFlag).getValue(1);
2001 InFlag = Chain.getValue(2);
2002 InVals.push_back(Chain.getValue(0));
2008 //===----------------------------------------------------------------------===//
2009 // Formal Arguments Calling Convention Implementation
2010 //===----------------------------------------------------------------------===//
2011 static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2012 std::vector<SDValue>& OutChains,
2013 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2014 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2015 unsigned LocMem = VA.getLocMemOffset();
2016 unsigned FirstWord = LocMem / 4;
2018 // copy register A0 - A3 to frame object
2019 for (unsigned i = 0; i < NumWords; ++i) {
2020 unsigned CurWord = FirstWord + i;
2021 if (CurWord >= O32IntRegsSize)
2024 unsigned SrcReg = O32IntRegs[CurWord];
2025 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2026 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2027 DAG.getConstant(i * 4, MVT::i32));
2028 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2029 StorePtr, MachinePointerInfo(), false,
2031 OutChains.push_back(Store);
2035 /// LowerFormalArguments - transform physical registers into virtual registers
2036 /// and generate load operations for arguments places on the stack.
2038 MipsTargetLowering::LowerFormalArguments(SDValue Chain,
2039 CallingConv::ID CallConv,
2041 const SmallVectorImpl<ISD::InputArg>
2043 DebugLoc dl, SelectionDAG &DAG,
2044 SmallVectorImpl<SDValue> &InVals)
2046 MachineFunction &MF = DAG.getMachineFunction();
2047 MachineFrameInfo *MFI = MF.getFrameInfo();
2048 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2050 MipsFI->setVarArgsFrameIndex(0);
2052 // Used with vargs to acumulate store chains.
2053 std::vector<SDValue> OutChains;
2055 // Assign locations to all of the incoming arguments.
2056 SmallVector<CCValAssign, 16> ArgLocs;
2057 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
2058 ArgLocs, *DAG.getContext());
2060 if (Subtarget->isABI_O32())
2061 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
2063 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
2065 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
2067 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2068 CCValAssign &VA = ArgLocs[i];
2070 // Arguments stored on registers
2071 if (VA.isRegLoc()) {
2072 EVT RegVT = VA.getLocVT();
2073 unsigned ArgReg = VA.getLocReg();
2074 TargetRegisterClass *RC = 0;
2076 if (RegVT == MVT::i32)
2077 RC = Mips::CPURegsRegisterClass;
2078 else if (RegVT == MVT::f32)
2079 RC = Mips::FGR32RegisterClass;
2080 else if (RegVT == MVT::f64) {
2081 if (!Subtarget->isSingleFloat())
2082 RC = Mips::AFGR64RegisterClass;
2084 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
2086 // Transform the arguments stored on
2087 // physical registers into virtual ones
2088 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2089 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
2091 // If this is an 8 or 16-bit value, it has been passed promoted
2092 // to 32 bits. Insert an assert[sz]ext to capture this, then
2093 // truncate to the right size.
2094 if (VA.getLocInfo() != CCValAssign::Full) {
2095 unsigned Opcode = 0;
2096 if (VA.getLocInfo() == CCValAssign::SExt)
2097 Opcode = ISD::AssertSext;
2098 else if (VA.getLocInfo() == CCValAssign::ZExt)
2099 Opcode = ISD::AssertZext;
2101 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
2102 DAG.getValueType(VA.getValVT()));
2103 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2106 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
2107 if (Subtarget->isABI_O32()) {
2108 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
2109 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
2110 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
2111 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
2112 VA.getLocReg()+1, RC);
2113 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
2114 if (!Subtarget->isLittle())
2115 std::swap(ArgValue, ArgValue2);
2116 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2117 ArgValue, ArgValue2);
2121 InVals.push_back(ArgValue);
2122 } else { // VA.isRegLoc()
2125 assert(VA.isMemLoc());
2127 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2129 if (Flags.isByVal()) {
2130 assert(Subtarget->isABI_O32() &&
2131 "No support for ByVal args by ABIs other than O32 yet.");
2132 assert(Flags.getByValSize() &&
2133 "ByVal args of size 0 should have been ignored by front-end.");
2134 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2135 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2137 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2138 InVals.push_back(FIN);
2139 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2144 // The stack pointer offset is relative to the caller stack frame.
2145 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
2146 VA.getLocMemOffset(), true);
2148 // Create load nodes to retrieve arguments from the stack
2149 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2150 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2151 MachinePointerInfo::getFixedStack(LastFI),
2156 // The mips ABIs for returning structs by value requires that we copy
2157 // the sret argument into $v0 for the return. Save the argument into
2158 // a virtual register so that we can access it from the return points.
2159 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2160 unsigned Reg = MipsFI->getSRetReturnReg();
2162 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
2163 MipsFI->setSRetReturnReg(Reg);
2165 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
2166 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
2169 if (isVarArg && Subtarget->isABI_O32()) {
2170 // Record the frame index of the first variable argument
2171 // which is a value necessary to VASTART.
2172 unsigned NextStackOffset = CCInfo.getNextStackOffset();
2173 assert(NextStackOffset % 4 == 0 &&
2174 "NextStackOffset must be aligned to 4-byte boundaries.");
2175 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
2176 MipsFI->setVarArgsFrameIndex(LastFI);
2178 // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
2179 // copy the integer registers that have not been used for argument passing
2180 // to the caller's stack frame.
2181 for (; NextStackOffset < 16; NextStackOffset += 4) {
2182 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
2183 unsigned Idx = NextStackOffset / 4;
2184 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
2185 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
2186 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
2187 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2188 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2189 MachinePointerInfo(),
2194 MipsFI->setLastInArgFI(LastFI);
2196 // All stores are grouped in one node to allow the matching between
2197 // the size of Ins and InVals. This only happens when on varg functions
2198 if (!OutChains.empty()) {
2199 OutChains.push_back(Chain);
2200 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2201 &OutChains[0], OutChains.size());
2207 //===----------------------------------------------------------------------===//
2208 // Return Value Calling Convention Implementation
2209 //===----------------------------------------------------------------------===//
2212 MipsTargetLowering::LowerReturn(SDValue Chain,
2213 CallingConv::ID CallConv, bool isVarArg,
2214 const SmallVectorImpl<ISD::OutputArg> &Outs,
2215 const SmallVectorImpl<SDValue> &OutVals,
2216 DebugLoc dl, SelectionDAG &DAG) const {
2218 // CCValAssign - represent the assignment of
2219 // the return value to a location
2220 SmallVector<CCValAssign, 16> RVLocs;
2222 // CCState - Info about the registers and stack slot.
2223 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
2224 RVLocs, *DAG.getContext());
2226 // Analize return values.
2227 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
2229 // If this is the first return lowered for this function, add
2230 // the regs to the liveout set for the function.
2231 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
2232 for (unsigned i = 0; i != RVLocs.size(); ++i)
2233 if (RVLocs[i].isRegLoc())
2234 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2239 // Copy the result values into the output registers.
2240 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2241 CCValAssign &VA = RVLocs[i];
2242 assert(VA.isRegLoc() && "Can only return in registers!");
2244 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2247 // guarantee that all emitted copies are
2248 // stuck together, avoiding something bad
2249 Flag = Chain.getValue(1);
2252 // The mips ABIs for returning structs by value requires that we copy
2253 // the sret argument into $v0 for the return. We saved the argument into
2254 // a virtual register in the entry block, so now we copy the value out
2256 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2257 MachineFunction &MF = DAG.getMachineFunction();
2258 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2259 unsigned Reg = MipsFI->getSRetReturnReg();
2262 llvm_unreachable("sret virtual register not created in the entry block");
2263 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
2265 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
2266 Flag = Chain.getValue(1);
2269 // Return on Mips is always a "jr $ra"
2271 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
2272 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
2274 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
2275 Chain, DAG.getRegister(Mips::RA, MVT::i32));
2278 //===----------------------------------------------------------------------===//
2279 // Mips Inline Assembly Support
2280 //===----------------------------------------------------------------------===//
2282 /// getConstraintType - Given a constraint letter, return the type of
2283 /// constraint it is for this target.
2284 MipsTargetLowering::ConstraintType MipsTargetLowering::
2285 getConstraintType(const std::string &Constraint) const
2287 // Mips specific constrainy
2288 // GCC config/mips/constraints.md
2290 // 'd' : An address register. Equivalent to r
2291 // unless generating MIPS16 code.
2292 // 'y' : Equivalent to r; retained for
2293 // backwards compatibility.
2294 // 'f' : Floating Point registers.
2295 if (Constraint.size() == 1) {
2296 switch (Constraint[0]) {
2301 return C_RegisterClass;
2305 return TargetLowering::getConstraintType(Constraint);
2308 /// Examine constraint type and operand type and determine a weight value.
2309 /// This object must already have been set up with the operand type
2310 /// and the current alternative constraint selected.
2311 TargetLowering::ConstraintWeight
2312 MipsTargetLowering::getSingleConstraintMatchWeight(
2313 AsmOperandInfo &info, const char *constraint) const {
2314 ConstraintWeight weight = CW_Invalid;
2315 Value *CallOperandVal = info.CallOperandVal;
2316 // If we don't have a value, we can't do a match,
2317 // but allow it at the lowest weight.
2318 if (CallOperandVal == NULL)
2320 const Type *type = CallOperandVal->getType();
2321 // Look at the constraint type.
2322 switch (*constraint) {
2324 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2328 if (type->isIntegerTy())
2329 weight = CW_Register;
2332 if (type->isFloatTy())
2333 weight = CW_Register;
2339 /// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
2340 /// return a list of registers that can be used to satisfy the constraint.
2341 /// This should only be used for C_RegisterClass constraints.
2342 std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
2343 getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
2345 if (Constraint.size() == 1) {
2346 switch (Constraint[0]) {
2348 return std::make_pair(0U, Mips::CPURegsRegisterClass);
2351 return std::make_pair(0U, Mips::FGR32RegisterClass);
2353 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2354 return std::make_pair(0U, Mips::AFGR64RegisterClass);
2357 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2360 /// Given a register class constraint, like 'r', if this corresponds directly
2361 /// to an LLVM register class, return a register of 0 and the register class
2363 std::vector<unsigned> MipsTargetLowering::
2364 getRegClassForInlineAsmConstraint(const std::string &Constraint,
2367 if (Constraint.size() != 1)
2368 return std::vector<unsigned>();
2370 switch (Constraint[0]) {
2373 // GCC Mips Constraint Letters
2376 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
2377 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
2378 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
2382 if (VT == MVT::f32) {
2383 if (Subtarget->isSingleFloat())
2384 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
2385 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
2386 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
2387 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
2388 Mips::F30, Mips::F31, 0);
2390 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
2391 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
2392 Mips::F28, Mips::F30, 0);
2396 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2397 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
2398 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
2399 Mips::D14, Mips::D15, 0);
2401 return std::vector<unsigned>();
2405 MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2406 // The Mips target isn't yet aware of offsets.
2410 bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2411 if (VT != MVT::f32 && VT != MVT::f64)
2413 if (Imm.isNegZero())
2415 return Imm.isZero();