blob: 7aaf13ca59f434847eabdcad582ce8a2d315b497 [file] [log] [blame]
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014
15#define DEBUG_TYPE "mips-lower"
Akira Hatanakab4d8d312011-05-24 00:23:52 +000016//#include <algorithm>
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000017#include "MipsISelLowering.h"
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +000018#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000019#include "MipsTargetMachine.h"
Chris Lattnerb71b9092009-08-13 06:28:06 +000020#include "MipsTargetObjectFile.h"
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000021#include "MipsSubtarget.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000022#include "llvm/DerivedTypes.h"
23#include "llvm/Function.h"
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +000024#include "llvm/GlobalVariable.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000025#include "llvm/Intrinsics.h"
26#include "llvm/CallingConv.h"
27#include "llvm/CodeGen/CallingConvLower.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000032#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033#include "llvm/CodeGen/ValueTypes.h"
34#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000036using namespace llvm;
37
Chris Lattnerf0144122009-07-28 03:13:23 +000038const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
39 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000040 case MipsISD::JmpLink: return "MipsISD::JmpLink";
41 case MipsISD::Hi: return "MipsISD::Hi";
42 case MipsISD::Lo: return "MipsISD::Lo";
43 case MipsISD::GPRel: return "MipsISD::GPRel";
44 case MipsISD::Ret: return "MipsISD::Ret";
45 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
46 case MipsISD::FPCmp: return "MipsISD::FPCmp";
47 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
48 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
49 case MipsISD::FPRound: return "MipsISD::FPRound";
50 case MipsISD::MAdd: return "MipsISD::MAdd";
51 case MipsISD::MAddu: return "MipsISD::MAddu";
52 case MipsISD::MSub: return "MipsISD::MSub";
53 case MipsISD::MSubu: return "MipsISD::MSubu";
54 case MipsISD::DivRem: return "MipsISD::DivRem";
55 case MipsISD::DivRemU: return "MipsISD::DivRemU";
56 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
57 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
58 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000059 }
60}
61
62MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000063MipsTargetLowering(MipsTargetMachine &TM)
Chris Lattnerb71b9092009-08-13 06:28:06 +000064 : TargetLowering(TM, new MipsTargetObjectFile()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000065 Subtarget = &TM.getSubtarget<MipsSubtarget>();
66
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000067 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000068 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000069 setBooleanContents(ZeroOrOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000070
71 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000072 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
73 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000074
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000075 // When dealing with single precision only, use libcalls
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000076 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000077 if (!Subtarget->isFP64bit())
Owen Anderson825b72b2009-08-11 20:47:22 +000078 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000079
Wesley Peckbf17cfa2010-11-23 03:31:01 +000080 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +000081 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
82 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
83 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000084
Eli Friedman6055a6a2009-07-17 04:07:24 +000085 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +000086 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
87 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +000088
Wesley Peckbf17cfa2010-11-23 03:31:01 +000089 // Used by legalize types to correctly generate the setcc result.
90 // Without this, every float setcc comes with a AND/OR with the result,
91 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000092 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +000093 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000094
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000095 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +000096 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +000097 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000098 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
99 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
100 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
101 setOperationAction(ISD::SELECT, MVT::f32, Custom);
102 setOperationAction(ISD::SELECT, MVT::f64, Custom);
103 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000104 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
105 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000106 setOperationAction(ISD::VASTART, MVT::Other, Custom);
107
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000108 setOperationAction(ISD::SDIV, MVT::i32, Expand);
109 setOperationAction(ISD::SREM, MVT::i32, Expand);
110 setOperationAction(ISD::UDIV, MVT::i32, Expand);
111 setOperationAction(ISD::UREM, MVT::i32, Expand);
112
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000113 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000114 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
115 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
116 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
117 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
118 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
120 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
121 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
122 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000123
124 if (!Subtarget->isMips32r2())
125 setOperationAction(ISD::ROTR, MVT::i32, Expand);
126
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
128 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
129 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000130 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
131 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000133 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000135 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
137 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000138 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 setOperationAction(ISD::FLOG, MVT::f32, Expand);
140 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
141 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
142 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000143
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000144 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
145 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
146
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000147 setOperationAction(ISD::VAARG, MVT::Other, Expand);
148 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
149 setOperationAction(ISD::VAEND, MVT::Other, Expand);
150
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000151 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000152 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
153 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
154 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000155
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000156 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000158
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000159 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
161 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000162 }
163
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000164 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000166
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000167 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000169
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000170 setTargetDAGCombine(ISD::ADDE);
171 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000172 setTargetDAGCombine(ISD::SDIVREM);
173 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000174 setTargetDAGCombine(ISD::SETCC);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000175
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000176 setMinFunctionAlignment(2);
177
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000178 setStackPointerRegisterToSaveRestore(Mips::SP);
179 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000180
181 setExceptionPointerRegister(Mips::A0);
182 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000183}
184
Owen Anderson825b72b2009-08-11 20:47:22 +0000185MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
186 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000187}
188
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000189// SelectMadd -
190// Transforms a subgraph in CurDAG if the following pattern is found:
191// (addc multLo, Lo0), (adde multHi, Hi0),
192// where,
193// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000194// Lo0: initial value of Lo register
195// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000196// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000197static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000198 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000199 // for the matching to be successful.
200 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
201
202 if (ADDCNode->getOpcode() != ISD::ADDC)
203 return false;
204
205 SDValue MultHi = ADDENode->getOperand(0);
206 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000207 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000208 unsigned MultOpc = MultHi.getOpcode();
209
210 // MultHi and MultLo must be generated by the same node,
211 if (MultLo.getNode() != MultNode)
212 return false;
213
214 // and it must be a multiplication.
215 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
216 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000217
218 // MultLo amd MultHi must be the first and second output of MultNode
219 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000220 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
221 return false;
222
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000223 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000224 // of the values of MultNode, in which case MultNode will be removed in later
225 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000226 // If there exist users other than ADDENode or ADDCNode, this function returns
227 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000228 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000229 // produced.
230 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
231 return false;
232
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000233 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000234 DebugLoc dl = ADDENode->getDebugLoc();
235
236 // create MipsMAdd(u) node
237 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000238
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000239 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
240 MVT::Glue,
241 MultNode->getOperand(0),// Factor 0
242 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000243 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000244 ADDENode->getOperand(1));// Hi0
245
246 // create CopyFromReg nodes
247 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
248 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000249 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000250 Mips::HI, MVT::i32,
251 CopyFromLo.getValue(2));
252
253 // replace uses of adde and addc here
254 if (!SDValue(ADDCNode, 0).use_empty())
255 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
256
257 if (!SDValue(ADDENode, 0).use_empty())
258 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
259
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000260 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000261}
262
263// SelectMsub -
264// Transforms a subgraph in CurDAG if the following pattern is found:
265// (addc Lo0, multLo), (sube Hi0, multHi),
266// where,
267// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000268// Lo0: initial value of Lo register
269// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000270// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000271static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000272 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000273 // for the matching to be successful.
274 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
275
276 if (SUBCNode->getOpcode() != ISD::SUBC)
277 return false;
278
279 SDValue MultHi = SUBENode->getOperand(1);
280 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000281 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000282 unsigned MultOpc = MultHi.getOpcode();
283
284 // MultHi and MultLo must be generated by the same node,
285 if (MultLo.getNode() != MultNode)
286 return false;
287
288 // and it must be a multiplication.
289 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
290 return false;
291
292 // MultLo amd MultHi must be the first and second output of MultNode
293 // respectively.
294 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
295 return false;
296
297 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
298 // of the values of MultNode, in which case MultNode will be removed in later
299 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000300 // If there exist users other than SUBENode or SUBCNode, this function returns
301 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000302 // instruction node rather than a pair of MULT and MSUB instructions being
303 // produced.
304 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
305 return false;
306
307 SDValue Chain = CurDAG->getEntryNode();
308 DebugLoc dl = SUBENode->getDebugLoc();
309
310 // create MipsSub(u) node
311 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
312
313 SDValue MSub = CurDAG->getNode(MultOpc, dl,
314 MVT::Glue,
315 MultNode->getOperand(0),// Factor 0
316 MultNode->getOperand(1),// Factor 1
317 SUBCNode->getOperand(0),// Lo0
318 SUBENode->getOperand(0));// Hi0
319
320 // create CopyFromReg nodes
321 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
322 MSub);
323 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
324 Mips::HI, MVT::i32,
325 CopyFromLo.getValue(2));
326
327 // replace uses of sube and subc here
328 if (!SDValue(SUBCNode, 0).use_empty())
329 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
330
331 if (!SDValue(SUBENode, 0).use_empty())
332 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
333
334 return true;
335}
336
337static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
338 TargetLowering::DAGCombinerInfo &DCI,
339 const MipsSubtarget* Subtarget) {
340 if (DCI.isBeforeLegalize())
341 return SDValue();
342
343 if (Subtarget->isMips32() && SelectMadd(N, &DAG))
344 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000345
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000346 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000347}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000348
349static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
350 TargetLowering::DAGCombinerInfo &DCI,
351 const MipsSubtarget* Subtarget) {
352 if (DCI.isBeforeLegalize())
353 return SDValue();
354
355 if (Subtarget->isMips32() && SelectMsub(N, &DAG))
356 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000357
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000358 return SDValue();
359}
360
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000361static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
362 TargetLowering::DAGCombinerInfo &DCI,
363 const MipsSubtarget* Subtarget) {
364 if (DCI.isBeforeLegalizeOps())
365 return SDValue();
366
367 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
368 MipsISD::DivRemU;
369 DebugLoc dl = N->getDebugLoc();
370
371 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
372 N->getOperand(0), N->getOperand(1));
373 SDValue InChain = DAG.getEntryNode();
374 SDValue InGlue = DivRem;
375
376 // insert MFLO
377 if (N->hasAnyUseOfValue(0)) {
378 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, Mips::LO, MVT::i32,
379 InGlue);
380 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
381 InChain = CopyFromLo.getValue(1);
382 InGlue = CopyFromLo.getValue(2);
383 }
384
385 // insert MFHI
386 if (N->hasAnyUseOfValue(1)) {
387 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000388 Mips::HI, MVT::i32, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000389 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
390 }
391
392 return SDValue();
393}
394
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000395static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
396 switch (CC) {
397 default: llvm_unreachable("Unknown fp condition code!");
398 case ISD::SETEQ:
399 case ISD::SETOEQ: return Mips::FCOND_OEQ;
400 case ISD::SETUNE: return Mips::FCOND_UNE;
401 case ISD::SETLT:
402 case ISD::SETOLT: return Mips::FCOND_OLT;
403 case ISD::SETGT:
404 case ISD::SETOGT: return Mips::FCOND_OGT;
405 case ISD::SETLE:
406 case ISD::SETOLE: return Mips::FCOND_OLE;
407 case ISD::SETGE:
408 case ISD::SETOGE: return Mips::FCOND_OGE;
409 case ISD::SETULT: return Mips::FCOND_ULT;
410 case ISD::SETULE: return Mips::FCOND_ULE;
411 case ISD::SETUGT: return Mips::FCOND_UGT;
412 case ISD::SETUGE: return Mips::FCOND_UGE;
413 case ISD::SETUO: return Mips::FCOND_UN;
414 case ISD::SETO: return Mips::FCOND_OR;
415 case ISD::SETNE:
416 case ISD::SETONE: return Mips::FCOND_ONE;
417 case ISD::SETUEQ: return Mips::FCOND_UEQ;
418 }
419}
420
421
422// Returns true if condition code has to be inverted.
423static bool InvertFPCondCode(Mips::CondCode CC) {
424 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
425 return false;
426
427 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
428 return true;
429
430 assert(false && "Illegal Condition Code");
431 return false;
432}
433
434// Creates and returns an FPCmp node from a setcc node.
435// Returns Op if setcc is not a floating point comparison.
436static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
437 // must be a SETCC node
438 if (Op.getOpcode() != ISD::SETCC)
439 return Op;
440
441 SDValue LHS = Op.getOperand(0);
442
443 if (!LHS.getValueType().isFloatingPoint())
444 return Op;
445
446 SDValue RHS = Op.getOperand(1);
447 DebugLoc dl = Op.getDebugLoc();
448
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000449 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
450 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000451 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
452
453 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
454 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
455}
456
457// Creates and returns a CMovFPT/F node.
458static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
459 SDValue False, DebugLoc DL) {
460 bool invert = InvertFPCondCode((Mips::CondCode)
461 cast<ConstantSDNode>(Cond.getOperand(2))
462 ->getSExtValue());
463
464 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
465 True.getValueType(), True, False, Cond);
466}
467
468static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
469 TargetLowering::DAGCombinerInfo &DCI,
470 const MipsSubtarget* Subtarget) {
471 if (DCI.isBeforeLegalizeOps())
472 return SDValue();
473
474 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
475
476 if (Cond.getOpcode() != MipsISD::FPCmp)
477 return SDValue();
478
479 SDValue True = DAG.getConstant(1, MVT::i32);
480 SDValue False = DAG.getConstant(0, MVT::i32);
481
482 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
483}
484
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000485SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000486 const {
487 SelectionDAG &DAG = DCI.DAG;
488 unsigned opc = N->getOpcode();
489
490 switch (opc) {
491 default: break;
492 case ISD::ADDE:
493 return PerformADDECombine(N, DAG, DCI, Subtarget);
494 case ISD::SUBE:
495 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000496 case ISD::SDIVREM:
497 case ISD::UDIVREM:
498 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000499 case ISD::SETCC:
500 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000501 }
502
503 return SDValue();
504}
505
Dan Gohman475871a2008-07-27 21:46:04 +0000506SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000507LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000508{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000509 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000510 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000511 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000512 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
513 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000514 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000515 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000516 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
517 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000518 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000519 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000520 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000521 }
Dan Gohman475871a2008-07-27 21:46:04 +0000522 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000523}
524
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000525//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000526// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000527//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000528
529// AddLiveIn - This helper function adds the specified physical register to the
530// MachineFunction as a live in value. It also creates a corresponding
531// virtual register for it.
532static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000533AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000534{
535 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000536 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
537 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000538 return VReg;
539}
540
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000541// Get fp branch code (not opcode) from condition code.
542static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
543 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
544 return Mips::BRANCH_T;
545
546 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
547 return Mips::BRANCH_F;
548
549 return Mips::BRANCH_INVALID;
550}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000551
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000552MachineBasicBlock *
553MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000554 MachineBasicBlock *BB) const {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000555 // There is no need to expand CMov instructions if target has
556 // conditional moves.
557 if (Subtarget->hasCondMov())
558 return BB;
559
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000560 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
561 bool isFPCmp = false;
Dale Johannesen94817572009-02-13 02:34:39 +0000562 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000563 unsigned Opc;
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000564
565 switch (MI->getOpcode()) {
566 default: assert(false && "Unexpected instr type to insert");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000567 case Mips::MOVT:
568 case Mips::MOVT_S:
569 case Mips::MOVT_D:
570 isFPCmp = true;
571 Opc = Mips::BC1F;
572 break;
573 case Mips::MOVF:
574 case Mips::MOVF_S:
575 case Mips::MOVF_D:
576 isFPCmp = true;
577 Opc = Mips::BC1T;
578 break;
579 case Mips::MOVZ_I:
580 case Mips::MOVZ_S:
581 case Mips::MOVZ_D:
582 Opc = Mips::BNE;
583 break;
584 case Mips::MOVN_I:
585 case Mips::MOVN_S:
586 case Mips::MOVN_D:
587 Opc = Mips::BEQ;
588 break;
589 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000590
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000591 // To "insert" a SELECT_CC instruction, we actually have to insert the
592 // diamond control-flow pattern. The incoming instruction knows the
593 // destination vreg to set, the condition code register to branch on, the
594 // true/false values to select between, and a branch opcode to use.
595 const BasicBlock *LLVM_BB = BB->getBasicBlock();
596 MachineFunction::iterator It = BB;
597 ++It;
Dan Gohman14152b42010-07-06 20:24:04 +0000598
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000599 // thisMBB:
600 // ...
601 // TrueVal = ...
602 // setcc r1, r2, r3
603 // bNE r1, r0, copy1MBB
604 // fallthrough --> copy0MBB
605 MachineBasicBlock *thisMBB = BB;
606 MachineFunction *F = BB->getParent();
607 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
608 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
609 F->insert(It, copy0MBB);
610 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +0000611
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000612 // Transfer the remainder of BB and its successor edges to sinkMBB.
613 sinkMBB->splice(sinkMBB->begin(), BB,
614 llvm::next(MachineBasicBlock::iterator(MI)),
615 BB->end());
616 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000617
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000618 // Next, add the true and fallthrough blocks as its successors.
619 BB->addSuccessor(copy0MBB);
620 BB->addSuccessor(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000621
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000622 // Emit the right instruction according to the type of the operands compared
623 if (isFPCmp)
624 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
625 else
626 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
627 .addReg(Mips::ZERO).addMBB(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000628
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000629
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000630 // copy0MBB:
631 // %FalseValue = ...
632 // # fallthrough to sinkMBB
633 BB = copy0MBB;
634
635 // Update machine-CFG edges
636 BB->addSuccessor(sinkMBB);
637
638 // sinkMBB:
639 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
640 // ...
641 BB = sinkMBB;
642
643 if (isFPCmp)
Dan Gohman14152b42010-07-06 20:24:04 +0000644 BuildMI(*BB, BB->begin(), dl,
645 TII->get(Mips::PHI), MI->getOperand(0).getReg())
Bruno Cardoso Lopes29e9daa2010-07-20 07:58:51 +0000646 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000647 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
648 else
649 BuildMI(*BB, BB->begin(), dl,
650 TII->get(Mips::PHI), MI->getOperand(0).getReg())
651 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
652 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000653
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000654 MI->eraseFromParent(); // The pseudo instruction is gone now.
655 return BB;
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000656}
657
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000658//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000659// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000660//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000661SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000662LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000663{
Akira Hatanaka053546c2011-05-25 02:20:00 +0000664 unsigned StackAlignment =
665 getTargetMachine().getFrameLowering()->getStackAlignment();
666 assert(StackAlignment >=
667 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
668 "Cannot lower if the alignment of the allocated space is larger than \
669 that of the stack.");
670
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000671 SDValue Chain = Op.getOperand(0);
672 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +0000673 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000674
675 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000677
678 // Subtract the dynamic size from the actual stack size to
679 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000681
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000682 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000683 // must be placed in the stack pointer register.
Akira Hatanaka053546c2011-05-25 02:20:00 +0000684 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
685 SDValue());
Akira Hatanakaedacba82011-05-25 17:32:06 +0000686 // Retrieve updated $sp. There is a glue input to prevent instructions that
687 // clobber $sp from being inserted between copytoreg and copyfromreg.
Akira Hatanaka053546c2011-05-25 02:20:00 +0000688 SDValue NewSP = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32,
689 Chain.getValue(1));
690
Akira Hatanakaedacba82011-05-25 17:32:06 +0000691 // The stack space reserved by alloca is located right above the argument
692 // area. It is aligned on a boundary that is a multiple of StackAlignment.
Akira Hatanaka053546c2011-05-25 02:20:00 +0000693 MachineFunction &MF = DAG.getMachineFunction();
694 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
695 unsigned SPOffset = (MipsFI->getMaxCallFrameSize() + StackAlignment - 1) /
696 StackAlignment * StackAlignment;
697 SDValue AllocPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
698 DAG.getConstant(SPOffset, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000699
700 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000701 // value and a chain
Akira Hatanaka053546c2011-05-25 02:20:00 +0000702 SDValue Ops[2] = { AllocPtr, NewSP.getValue(1) };
Dale Johannesena05dca42009-02-04 23:02:30 +0000703 return DAG.getMergeValues(Ops, 2, dl);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000704}
705
706SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000707LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000708{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000709 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000710 // the block to branch to if the condition is true.
711 SDValue Chain = Op.getOperand(0);
712 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000713 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000714
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000715 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
716
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000717 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000718 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000719 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000720
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000721 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000722 Mips::CondCode CC =
723 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000724 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000725
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000726 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000727 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000728}
729
730SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000731LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000732{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000733 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000734
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000735 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000736 if (Cond.getOpcode() != MipsISD::FPCmp)
737 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000738
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000739 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
740 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000741}
742
Dan Gohmand858e902010-04-17 15:26:15 +0000743SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
744 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +0000745 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000746 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000747 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000748
Eli Friedmane2c74082009-08-03 02:22:28 +0000749 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +0000750 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000751
Chris Lattnerb71b9092009-08-13 06:28:06 +0000752 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000753
Chris Lattnere3736f82009-08-13 05:41:27 +0000754 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000755 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
756 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000757 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +0000758 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
759 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000760 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +0000761 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000762 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000763 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
764 MipsII::MO_ABS_HI);
765 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
766 MipsII::MO_ABS_LO);
767 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
768 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000770 } else {
Devang Patel0d881da2010-07-06 22:08:15 +0000771 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000772 MipsII::MO_GOT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000773 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000774 DAG.getEntryNode(), GA, MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000775 false, false, 0);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000776 // On functions and global targets not internal linked only
777 // a load from got/GP is necessary for PIC to work.
Akira Hatanaka9777e7a2011-04-07 19:51:44 +0000778 if (!GV->hasInternalLinkage() &&
779 (!GV->hasLocalLinkage() || isa<Function>(GV)))
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000780 return ResNode;
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000781 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
782 MipsII::MO_ABS_LO);
783 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000784 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000785 }
786
Torok Edwinc23197a2009-07-14 16:55:14 +0000787 llvm_unreachable("Dont know how to handle GlobalAddress");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000788 return SDValue(0,0);
789}
790
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000791SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
792 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +0000793 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
794 // FIXME there isn't actually debug info here
795 DebugLoc dl = Op.getDebugLoc();
796
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000797 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +0000798 // %hi/%lo relocation
799 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
800 MipsII::MO_ABS_HI);
801 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
802 MipsII::MO_ABS_LO);
803 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
804 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
805 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000806 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +0000807
808 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
809 MipsII::MO_GOT);
810 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
811 MipsII::MO_ABS_LO);
812 SDValue Load = DAG.getLoad(MVT::i32, dl,
813 DAG.getEntryNode(), BAGOTOffset,
814 MachinePointerInfo(), false, false, 0);
815 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
816 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000817}
818
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000819SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000820LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000821{
Torok Edwinc23197a2009-07-14 16:55:14 +0000822 llvm_unreachable("TLS not implemented for MIPS.");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000823 return SDValue(); // Not reached
824}
825
826SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000827LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000828{
Dan Gohman475871a2008-07-27 21:46:04 +0000829 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000830 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +0000831 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000832 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000833 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000834 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000835
Owen Andersone50ed302009-08-10 22:56:29 +0000836 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000837 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000838
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000839 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
840
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +0000841 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +0000842 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000843 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000844 } else // Emit Load from Global Pointer
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000845 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
846 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000847 false, false, 0);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000848
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000849 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
850 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000851 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000852 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000853
854 return ResNode;
855}
856
Dan Gohman475871a2008-07-27 21:46:04 +0000857SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000858LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000859{
Dan Gohman475871a2008-07-27 21:46:04 +0000860 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000861 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +0000862 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +0000863 // FIXME there isn't actually debug info here
864 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000865
866 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000867 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000868 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000869 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000870 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +0000871 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000872 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
873 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000874 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000875
876 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000877 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000878 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000879 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000880 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000881 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
882 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000883 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000884 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000885 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000886 N->getOffset(), MipsII::MO_GOT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000887 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000888 CP, MachinePointerInfo::getConstantPool(),
889 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000890 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000891 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000892 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000893 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
894 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000895
896 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000897}
898
Dan Gohmand858e902010-04-17 15:26:15 +0000899SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +0000900 MachineFunction &MF = DAG.getMachineFunction();
901 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
902
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000903 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +0000904 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
905 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000906
907 // vastart just stores the address of the VarArgsFrameIndex slot into the
908 // memory location argument.
909 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +0000910 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
911 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +0000912 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000913}
914
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000915static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
916 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
917 DebugLoc dl = Op.getDebugLoc();
918 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
919 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
920 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
921 DAG.getConstant(0x7fffffff, MVT::i32));
922 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
923 DAG.getConstant(0x80000000, MVT::i32));
924 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
925 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
926}
927
928static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
929 // FIXME:
930 // Use ext/ins instructions if target architecture is Mips32r2.
931 // Eliminate redundant mfc1 and mtc1 instructions.
932 unsigned LoIdx = 0, HiIdx = 1;
933
934 if (!isLittle)
935 std::swap(LoIdx, HiIdx);
936
937 DebugLoc dl = Op.getDebugLoc();
938 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
939 Op.getOperand(0),
940 DAG.getConstant(LoIdx, MVT::i32));
941 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
942 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
943 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
944 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
945 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
946 DAG.getConstant(0x7fffffff, MVT::i32));
947 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
948 DAG.getConstant(0x80000000, MVT::i32));
949 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
950
951 if (!isLittle)
952 std::swap(Word0, Word1);
953
954 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
955}
956
957SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
958 const {
959 EVT Ty = Op.getValueType();
960
961 assert(Ty == MVT::f32 || Ty == MVT::f64);
962
963 if (Ty == MVT::f32)
964 return LowerFCOPYSIGN32(Op, DAG);
965 else
966 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
967}
968
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000969//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000970// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000971//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000972
973#include "MipsGenCallingConv.inc"
974
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000975//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000976// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000977// Mips O32 ABI rules:
978// ---
979// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000980// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000981// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000982// f64 - Only passed in two aliased f32 registers if no int reg has been used
983// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000984// not used, it must be shadowed. If only A3 is avaiable, shadow it and
985// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +0000986//
987// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000988//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000989
Duncan Sands1e96bab2010-11-04 10:49:57 +0000990static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000991 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000992 ISD::ArgFlagsTy ArgFlags, CCState &State) {
993
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000994 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000995
996 static const unsigned IntRegs[] = {
997 Mips::A0, Mips::A1, Mips::A2, Mips::A3
998 };
999 static const unsigned F32Regs[] = {
1000 Mips::F12, Mips::F14
1001 };
1002 static const unsigned F64Regs[] = {
1003 Mips::D6, Mips::D7
1004 };
1005
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001006 // ByVal Args
1007 if (ArgFlags.isByVal()) {
1008 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1009 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1010 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1011 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1012 r < std::min(IntRegsSize, NextReg); ++r)
1013 State.AllocateReg(IntRegs[r]);
1014 return false;
1015 }
1016
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001017 // Promote i8 and i16
1018 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1019 LocVT = MVT::i32;
1020 if (ArgFlags.isSExt())
1021 LocInfo = CCValAssign::SExt;
1022 else if (ArgFlags.isZExt())
1023 LocInfo = CCValAssign::ZExt;
1024 else
1025 LocInfo = CCValAssign::AExt;
1026 }
1027
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001028 unsigned Reg;
1029
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001030 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1031 // is true: function is vararg, argument is 3rd or higher, there is previous
1032 // argument which is not f32 or f64.
1033 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1034 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001035 unsigned OrigAlign = ArgFlags.getOrigAlign();
1036 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001037
1038 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001039 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001040 // If this is the first part of an i64 arg,
1041 // the allocated register must be either A0 or A2.
1042 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1043 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001044 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001045 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1046 // Allocate int register and shadow next int register. If first
1047 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001048 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1049 if (Reg == Mips::A1 || Reg == Mips::A3)
1050 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1051 State.AllocateReg(IntRegs, IntRegsSize);
1052 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001053 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1054 // we are guaranteed to find an available float register
1055 if (ValVT == MVT::f32) {
1056 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1057 // Shadow int register
1058 State.AllocateReg(IntRegs, IntRegsSize);
1059 } else {
1060 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1061 // Shadow int registers
1062 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1063 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1064 State.AllocateReg(IntRegs, IntRegsSize);
1065 State.AllocateReg(IntRegs, IntRegsSize);
1066 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001067 } else
1068 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001069
Akira Hatanakad37776d2011-05-20 21:39:54 +00001070 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1071 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1072
1073 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001074 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001075 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001076 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001077
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001078 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001079}
1080
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001081//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001082// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001083//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001084
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001085static const unsigned O32IntRegsSize = 4;
1086
1087static const unsigned O32IntRegs[] = {
1088 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1089};
1090
1091// Write ByVal Arg to arg registers and stack.
1092static void
1093WriteByValArg(SDValue& Chain, DebugLoc dl,
1094 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1095 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1096 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001097 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
1098 MVT PtrType) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001099 unsigned FirstWord = VA.getLocMemOffset() / 4;
1100 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
1101 unsigned LastWord = FirstWord + NumWords;
1102 unsigned CurWord;
1103
1104 // copy the first 4 words of byval arg to registers A0 - A3
1105 for (CurWord = FirstWord; CurWord < std::min(LastWord, O32IntRegsSize);
1106 ++CurWord) {
1107 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1108 DAG.getConstant((CurWord - FirstWord) * 4,
1109 MVT::i32));
1110 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1111 MachinePointerInfo(),
1112 false, false, 0);
1113 MemOpChains.push_back(LoadVal.getValue(1));
1114 unsigned DstReg = O32IntRegs[CurWord];
1115 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1116 }
1117
1118 // copy remaining part of byval arg to stack.
1119 if (CurWord < LastWord) {
1120 unsigned SizeInBytes = (LastWord - CurWord) * 4;
1121 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1122 DAG.getConstant((CurWord - FirstWord) * 4,
1123 MVT::i32));
1124 LastFI = MFI->CreateFixedObject(SizeInBytes, CurWord * 4, true);
1125 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
1126 Chain = DAG.getMemcpy(Chain, dl, Dst, Src,
1127 DAG.getConstant(SizeInBytes, MVT::i32),
1128 /*Align*/4,
1129 /*isVolatile=*/false, /*AlwaysInline=*/false,
1130 MachinePointerInfo(0), MachinePointerInfo(0));
1131 MemOpChains.push_back(Chain);
1132 }
1133}
1134
Dan Gohman98ca4f22009-08-05 01:29:28 +00001135/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001136/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001137/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001138SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001139MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001140 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001141 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001142 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001143 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001144 const SmallVectorImpl<ISD::InputArg> &Ins,
1145 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001146 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001147 // MIPs target does not yet support tail call optimization.
1148 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001149
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001150 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001151 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00001152 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001153 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001154 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001155
1156 // Analyze operands of the call, assigning locations to each operand.
1157 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001158 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1159 *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001160
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001161 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001162 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001163 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001164 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001165
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001166 // Get a count of how many bytes are to be pushed on the stack.
1167 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnere563bbc2008-10-11 22:08:30 +00001168 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001169
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001170 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001171 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1172 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001173
Akira Hatanakaedacba82011-05-25 17:32:06 +00001174 // If this is the first call, create a stack frame object that points to
1175 // a location to which .cprestore saves $gp. The offset of this frame object
1176 // is set to 0, since we know nothing about the size of the argument area at
1177 // this point.
Akira Hatanaka69c19f72011-05-23 20:16:59 +00001178 if (IsPIC && !MipsFI->getGPFI())
Akira Hatanaka43299772011-05-20 23:22:14 +00001179 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1180
1181 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
1182
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001183 // Walk the register/memloc assignments, inserting copies/loads.
1184 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001185 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001186 CCValAssign &VA = ArgLocs[i];
1187
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001188 // Promote the value if needed.
1189 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001190 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001191 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001192 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001193 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001194 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001195 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001196 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1197 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001198 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1199 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001200 if (!Subtarget->isLittle())
1201 std::swap(Lo, Hi);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001202 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
1203 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
1204 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001205 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001206 }
1207 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00001208 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001209 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001210 break;
1211 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001212 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001213 break;
1214 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001215 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001216 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001217 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001218
1219 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001220 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001221 if (VA.isRegLoc()) {
1222 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00001223 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001224 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001225
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001226 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00001227 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001228
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001229 // ByVal Arg.
1230 ISD::ArgFlagsTy Flags = Outs[i].Flags;
1231 if (Flags.isByVal()) {
1232 assert(Subtarget->isABI_O32() &&
1233 "No support for ByVal args by ABIs other than O32 yet.");
1234 assert(Flags.getByValSize() &&
1235 "ByVal args of size 0 should have been ignored by front-end.");
1236 WriteByValArg(Chain, dl, RegsToPass, MemOpChains, LastFI, MFI, DAG, Arg,
1237 VA, Flags, getPointerTy());
1238 continue;
1239 }
1240
Chris Lattnere0b12152008-03-17 06:57:02 +00001241 // Create the frame index object for this incoming parameter
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001242 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
1243 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00001244 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00001245
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001246 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00001247 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00001248 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
1249 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001250 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001251 }
1252
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001253 // Transform all store nodes into one single node because all store
1254 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001255 if (!MemOpChains.empty())
1256 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001257 &MemOpChains[0], MemOpChains.size());
1258
Bill Wendling056292f2008-09-16 21:48:12 +00001259 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001260 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1261 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001262 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001263 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001264 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001265
1266 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001267 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
1268 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1269 getPointerTy(), 0,MipsII:: MO_GOT);
1270 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
1271 0, MipsII::MO_ABS_LO);
1272 } else {
1273 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1274 getPointerTy(), 0, OpFlag);
1275 }
1276
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001277 LoadSymAddr = true;
1278 }
1279 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001280 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001281 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001282 LoadSymAddr = true;
1283 }
1284
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00001285 SDValue InFlag;
1286
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001287 // Create nodes that load address of callee and copy it to T9
1288 if (IsPIC) {
1289 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001290 // Load callee address
1291 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, Chain, Callee,
1292 MachinePointerInfo::getGOT(),
1293 false, false, 0);
1294
1295 // Use GOT+LO if callee has internal linkage.
1296 if (CalleeLo.getNode()) {
1297 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
1298 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
1299 } else
1300 Callee = LoadValue;
1301
1302 // Use chain output from LoadValue
1303 Chain = LoadValue.getValue(1);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001304 }
1305
1306 // copy to T9
1307 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
1308 InFlag = Chain.getValue(1);
1309 Callee = DAG.getRegister(Mips::T9, MVT::i32);
1310 }
Bill Wendling056292f2008-09-16 21:48:12 +00001311
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00001312 // Build a sequence of copy-to-reg nodes chained together with token
1313 // chain and flag operands which copy the outgoing args into registers.
1314 // The InFlag in necessary since all emitted instructions must be
1315 // stuck together.
1316 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1317 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1318 RegsToPass[i].second, InFlag);
1319 InFlag = Chain.getValue(1);
1320 }
1321
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001322 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001323 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001324 //
1325 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001326 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00001327 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001328 Ops.push_back(Chain);
1329 Ops.push_back(Callee);
1330
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001331 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001332 // known live into the call.
1333 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1334 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1335 RegsToPass[i].second.getValueType()));
1336
Gabor Greifba36cb52008-08-28 21:40:38 +00001337 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001338 Ops.push_back(InFlag);
1339
Dale Johannesen33c960f2009-02-04 20:06:27 +00001340 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001341 InFlag = Chain.getValue(1);
1342
Akira Hatanaka4c62f762011-05-25 18:08:32 +00001343 // Function can have an arbitrary number of calls, so
1344 // hold the LastArgStackLoc with the biggest offset.
1345 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1346 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001347
Akira Hatanaka4c62f762011-05-25 18:08:32 +00001348 // For O32, a minimum of four words (16 bytes) of argument space is
1349 // allocated.
1350 if (Subtarget->isABI_O32())
1351 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
Akira Hatanakad37776d2011-05-20 21:39:54 +00001352
Akira Hatanaka4c62f762011-05-25 18:08:32 +00001353 if (MaxCallFrameSize < NextStackOffset) {
1354 MipsFI->setMaxCallFrameSize(NextStackOffset);
Akira Hatanakad37776d2011-05-20 21:39:54 +00001355
Akira Hatanaka4c62f762011-05-25 18:08:32 +00001356 if (IsPIC) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001357 // $gp restore slot must be aligned.
1358 unsigned StackAlignment = TFL->getStackAlignment();
1359 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
1360 StackAlignment * StackAlignment;
1361 int GPFI = MipsFI->getGPFI();
1362 MFI->setObjectOffset(GPFI, NextStackOffset);
1363 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001364 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001365
Akira Hatanaka43299772011-05-20 23:22:14 +00001366 // Extend range of indices of frame objects for outgoing arguments that were
1367 // created during this function call. Skip this step if no such objects were
1368 // created.
1369 if (LastFI)
1370 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
1371
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00001372 // Create the CALLSEQ_END node.
1373 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1374 DAG.getIntPtrConstant(0, true), InFlag);
1375 InFlag = Chain.getValue(1);
1376
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001377 // Handle result values, copying them out of physregs into vregs that we
1378 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001379 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
1380 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001381}
1382
Dan Gohman98ca4f22009-08-05 01:29:28 +00001383/// LowerCallResult - Lower the result values of a call into the
1384/// appropriate copies out of appropriate physical registers.
1385SDValue
1386MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001387 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001388 const SmallVectorImpl<ISD::InputArg> &Ins,
1389 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001390 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001391
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001392 // Assign locations to each value returned by this call.
1393 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001394 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001395 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001396
Dan Gohman98ca4f22009-08-05 01:29:28 +00001397 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001398
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001399 // Copy all of the result registers out of their specified physreg.
1400 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001401 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001402 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001403 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001404 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001405 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001406
Dan Gohman98ca4f22009-08-05 01:29:28 +00001407 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001408}
1409
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001410//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001411// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001412//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001413static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
1414 std::vector<SDValue>& OutChains,
1415 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
1416 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
1417 unsigned LocMem = VA.getLocMemOffset();
1418 unsigned FirstWord = LocMem / 4;
1419
1420 // copy register A0 - A3 to frame object
1421 for (unsigned i = 0; i < NumWords; ++i) {
1422 unsigned CurWord = FirstWord + i;
1423 if (CurWord >= O32IntRegsSize)
1424 break;
1425
1426 unsigned SrcReg = O32IntRegs[CurWord];
1427 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
1428 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
1429 DAG.getConstant(i * 4, MVT::i32));
1430 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
1431 StorePtr, MachinePointerInfo(), false,
1432 false, 0);
1433 OutChains.push_back(Store);
1434 }
1435}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001436
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001437/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001438/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001439SDValue
1440MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001441 CallingConv::ID CallConv,
1442 bool isVarArg,
1443 const SmallVectorImpl<ISD::InputArg>
1444 &Ins,
1445 DebugLoc dl, SelectionDAG &DAG,
1446 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001447 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00001448 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001449 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001450 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001451
Dan Gohman1e93df62010-04-17 14:41:14 +00001452 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001453
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001454 // Used with vargs to acumulate store chains.
1455 std::vector<SDValue> OutChains;
1456
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001457 // Assign locations to all of the incoming arguments.
1458 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001459 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1460 ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001461
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001462 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001463 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001464 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001465 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001466
Akira Hatanaka43299772011-05-20 23:22:14 +00001467 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001468
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001469 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001470 CCValAssign &VA = ArgLocs[i];
1471
1472 // Arguments stored on registers
1473 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001474 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001475 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00001476 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001477
Owen Anderson825b72b2009-08-11 20:47:22 +00001478 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001479 RC = Mips::CPURegsRegisterClass;
1480 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001481 RC = Mips::FGR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001482 else if (RegVT == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001483 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001484 RC = Mips::AFGR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001485 } else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001486 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001487
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001488 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001489 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001490 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001491 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001492
1493 // If this is an 8 or 16-bit value, it has been passed promoted
1494 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001495 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001496 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00001497 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001498 if (VA.getLocInfo() == CCValAssign::SExt)
1499 Opcode = ISD::AssertSext;
1500 else if (VA.getLocInfo() == CCValAssign::ZExt)
1501 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00001502 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001503 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00001504 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00001505 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001506 }
1507
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001508 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001509 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001510 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
1511 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00001512 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001513 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001514 VA.getLocReg()+1, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001515 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001516 if (!Subtarget->isLittle())
1517 std::swap(ArgValue, ArgValue2);
1518 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
1519 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001520 }
1521 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001522
Dan Gohman98ca4f22009-08-05 01:29:28 +00001523 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001524 } else { // VA.isRegLoc()
1525
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001526 // sanity check
1527 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001528
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001529 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1530
1531 if (Flags.isByVal()) {
1532 assert(Subtarget->isABI_O32() &&
1533 "No support for ByVal args by ABIs other than O32 yet.");
1534 assert(Flags.getByValSize() &&
1535 "ByVal args of size 0 should have been ignored by front-end.");
1536 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
1537 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
1538 true);
1539 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
1540 InVals.push_back(FIN);
1541 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
1542
1543 continue;
1544 }
1545
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001546 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001547 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
1548 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001549
1550 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00001551 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001552 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00001553 MachinePointerInfo::getFixedStack(LastFI),
David Greenef6fa1862010-02-15 16:56:10 +00001554 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001555 }
1556 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001557
1558 // The mips ABIs for returning structs by value requires that we copy
1559 // the sret argument into $v0 for the return. Save the argument into
1560 // a virtual register so that we can access it from the return points.
1561 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1562 unsigned Reg = MipsFI->getSRetReturnReg();
1563 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001564 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001565 MipsFI->setSRetReturnReg(Reg);
1566 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001567 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001568 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001569 }
1570
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001571 if (isVarArg && Subtarget->isABI_O32()) {
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001572 // Record the frame index of the first variable argument
1573 // which is a value necessary to VASTART.
1574 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001575 assert(NextStackOffset % 4 == 0 &&
1576 "NextStackOffset must be aligned to 4-byte boundaries.");
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001577 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
1578 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00001579
1580 // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
1581 // copy the integer registers that have not been used for argument passing
1582 // to the caller's stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001583 for (; NextStackOffset < 16; NextStackOffset += 4) {
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001584 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001585 unsigned Idx = NextStackOffset / 4;
1586 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
1587 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Akira Hatanaka69c19f72011-05-23 20:16:59 +00001588 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001589 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
1590 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
1591 MachinePointerInfo(),
1592 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001593 }
1594 }
1595
Akira Hatanaka43299772011-05-20 23:22:14 +00001596 MipsFI->setLastInArgFI(LastFI);
1597
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001598 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001599 // the size of Ins and InVals. This only happens when on varg functions
1600 if (!OutChains.empty()) {
1601 OutChains.push_back(Chain);
1602 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1603 &OutChains[0], OutChains.size());
1604 }
1605
Dan Gohman98ca4f22009-08-05 01:29:28 +00001606 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001607}
1608
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001609//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001610// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001611//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001612
Dan Gohman98ca4f22009-08-05 01:29:28 +00001613SDValue
1614MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001615 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001616 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001617 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001618 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001619
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001620 // CCValAssign - represent the assignment of
1621 // the return value to a location
1622 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001623
1624 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001625 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1626 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001627
Dan Gohman98ca4f22009-08-05 01:29:28 +00001628 // Analize return values.
1629 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001630
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001631 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001632 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001633 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001634 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001635 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001636 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001637 }
1638
Dan Gohman475871a2008-07-27 21:46:04 +00001639 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001640
1641 // Copy the result values into the output registers.
1642 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1643 CCValAssign &VA = RVLocs[i];
1644 assert(VA.isRegLoc() && "Can only return in registers!");
1645
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001646 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001647 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001648
1649 // guarantee that all emitted copies are
1650 // stuck together, avoiding something bad
1651 Flag = Chain.getValue(1);
1652 }
1653
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001654 // The mips ABIs for returning structs by value requires that we copy
1655 // the sret argument into $v0 for the return. We saved the argument into
1656 // a virtual register in the entry block, so now we copy the value out
1657 // and into $v0.
1658 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1659 MachineFunction &MF = DAG.getMachineFunction();
1660 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1661 unsigned Reg = MipsFI->getSRetReturnReg();
1662
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001663 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00001664 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00001665 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001666
Dale Johannesena05dca42009-02-04 23:02:30 +00001667 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001668 Flag = Chain.getValue(1);
1669 }
1670
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001671 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00001672 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001673 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001674 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001675 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001676 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001677 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001678}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001679
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001680//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001681// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001682//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001683
1684/// getConstraintType - Given a constraint letter, return the type of
1685/// constraint it is for this target.
1686MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001687getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001688{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001689 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001690 // GCC config/mips/constraints.md
1691 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001692 // 'd' : An address register. Equivalent to r
1693 // unless generating MIPS16 code.
1694 // 'y' : Equivalent to r; retained for
1695 // backwards compatibility.
1696 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001697 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001698 switch (Constraint[0]) {
1699 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001700 case 'd':
1701 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001702 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001703 return C_RegisterClass;
1704 break;
1705 }
1706 }
1707 return TargetLowering::getConstraintType(Constraint);
1708}
1709
John Thompson44ab89e2010-10-29 17:29:13 +00001710/// Examine constraint type and operand type and determine a weight value.
1711/// This object must already have been set up with the operand type
1712/// and the current alternative constraint selected.
1713TargetLowering::ConstraintWeight
1714MipsTargetLowering::getSingleConstraintMatchWeight(
1715 AsmOperandInfo &info, const char *constraint) const {
1716 ConstraintWeight weight = CW_Invalid;
1717 Value *CallOperandVal = info.CallOperandVal;
1718 // If we don't have a value, we can't do a match,
1719 // but allow it at the lowest weight.
1720 if (CallOperandVal == NULL)
1721 return CW_Default;
1722 const Type *type = CallOperandVal->getType();
1723 // Look at the constraint type.
1724 switch (*constraint) {
1725 default:
1726 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
1727 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001728 case 'd':
1729 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00001730 if (type->isIntegerTy())
1731 weight = CW_Register;
1732 break;
1733 case 'f':
1734 if (type->isFloatTy())
1735 weight = CW_Register;
1736 break;
1737 }
1738 return weight;
1739}
1740
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001741/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1742/// return a list of registers that can be used to satisfy the constraint.
1743/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001744std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00001745getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001746{
1747 if (Constraint.size() == 1) {
1748 switch (Constraint[0]) {
1749 case 'r':
1750 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001751 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001752 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001753 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001754 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001755 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1756 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001757 }
1758 }
1759 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1760}
1761
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001762/// Given a register class constraint, like 'r', if this corresponds directly
1763/// to an LLVM register class, return a register of 0 and the register class
1764/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001765std::vector<unsigned> MipsTargetLowering::
1766getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001767 EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001768{
1769 if (Constraint.size() != 1)
1770 return std::vector<unsigned>();
1771
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001772 switch (Constraint[0]) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001773 default : break;
1774 case 'r':
1775 // GCC Mips Constraint Letters
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001776 case 'd':
1777 case 'y':
1778 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
1779 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
1780 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001781 Mips::T8, 0);
1782
1783 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001784 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001785 if (Subtarget->isSingleFloat())
1786 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1787 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1788 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1789 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1790 Mips::F30, Mips::F31, 0);
1791 else
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001792 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
1793 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001794 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +00001795 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001796
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001797 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001798 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001799 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
1800 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001801 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001802 }
1803 return std::vector<unsigned>();
1804}
Dan Gohman6520e202008-10-18 02:06:02 +00001805
1806bool
1807MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1808 // The Mips target isn't yet aware of offsets.
1809 return false;
1810}
Evan Chengeb2f9692009-10-27 19:56:55 +00001811
Evan Chenga1eaa3c2009-10-28 01:43:28 +00001812bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
1813 if (VT != MVT::f32 && VT != MVT::f64)
1814 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00001815 if (Imm.isNegZero())
1816 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00001817 return Imm.isZero();
1818}