blob: 0e193f29532384d4851fca5cd18ed2064a2f261a [file] [log] [blame]
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
2//
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//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "mips-lower"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000016#include "MipsISelLowering.h"
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +000017#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000018#include "MipsTargetMachine.h"
Chris Lattnerb71b9092009-08-13 06:28:06 +000019#include "MipsTargetObjectFile.h"
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000020#include "MipsSubtarget.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000021#include "llvm/DerivedTypes.h"
22#include "llvm/Function.h"
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +000023#include "llvm/GlobalVariable.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000024#include "llvm/Intrinsics.h"
25#include "llvm/CallingConv.h"
26#include "llvm/CodeGen/CallingConvLower.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000031#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000032#include "llvm/CodeGen/ValueTypes.h"
33#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000034#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000035using namespace llvm;
36
Chris Lattnerf0144122009-07-28 03:13:23 +000037const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
38 switch (Opcode) {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000039 case MipsISD::JmpLink : return "MipsISD::JmpLink";
40 case MipsISD::Hi : return "MipsISD::Hi";
41 case MipsISD::Lo : return "MipsISD::Lo";
42 case MipsISD::GPRel : return "MipsISD::GPRel";
43 case MipsISD::Ret : return "MipsISD::Ret";
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000044 case MipsISD::FPBrcond : return "MipsISD::FPBrcond";
45 case MipsISD::FPCmp : return "MipsISD::FPCmp";
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +000046 case MipsISD::CMovFP_T : return "MipsISD::CMovFP_T";
47 case MipsISD::CMovFP_F : return "MipsISD::CMovFP_F";
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +000048 case MipsISD::FPRound : return "MipsISD::FPRound";
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +000049 case MipsISD::MAdd : return "MipsISD::MAdd";
50 case MipsISD::MAddu : return "MipsISD::MAddu";
51 case MipsISD::MSub : return "MipsISD::MSub";
52 case MipsISD::MSubu : return "MipsISD::MSubu";
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +000053 case MipsISD::DivRem : return "MipsISD::DivRem";
54 case MipsISD::DivRemU : return "MipsISD::DivRemU";
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000055 default : return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000056 }
57}
58
59MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000060MipsTargetLowering(MipsTargetMachine &TM)
Chris Lattnerb71b9092009-08-13 06:28:06 +000061 : TargetLowering(TM, new MipsTargetObjectFile()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000062 Subtarget = &TM.getSubtarget<MipsSubtarget>();
63
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000064 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000065 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000066 setBooleanContents(ZeroOrOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000067
68 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000069 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
70 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000071
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000072 // When dealing with single precision only, use libcalls
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000073 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000074 if (!Subtarget->isFP64bit())
Owen Anderson825b72b2009-08-11 20:47:22 +000075 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000076
Wesley Peckbf17cfa2010-11-23 03:31:01 +000077 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +000078 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
79 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
80 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000081
Eli Friedman6055a6a2009-07-17 04:07:24 +000082 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +000083 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
84 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +000085
Wesley Peckbf17cfa2010-11-23 03:31:01 +000086 // Used by legalize types to correctly generate the setcc result.
87 // Without this, every float setcc comes with a AND/OR with the result,
88 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000089 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +000090 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000091
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000092 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +000093 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +000094 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000095 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
96 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
97 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
98 setOperationAction(ISD::SELECT, MVT::f32, Custom);
99 setOperationAction(ISD::SELECT, MVT::f64, Custom);
100 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000101 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
102 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
103 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000104 setOperationAction(ISD::VASTART, MVT::Other, Custom);
105
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000106 setOperationAction(ISD::SDIV, MVT::i32, Expand);
107 setOperationAction(ISD::SREM, MVT::i32, Expand);
108 setOperationAction(ISD::UDIV, MVT::i32, Expand);
109 setOperationAction(ISD::UREM, MVT::i32, Expand);
110
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000111 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000112 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
113 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
114 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
115 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
116 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
117 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
118 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
119 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
120 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000121
122 if (!Subtarget->isMips32r2())
123 setOperationAction(ISD::ROTR, MVT::i32, Expand);
124
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
126 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
127 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
128 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
129 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
130 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000131 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000133 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
135 setOperationAction(ISD::FPOW, MVT::f32, Expand);
136 setOperationAction(ISD::FLOG, MVT::f32, Expand);
137 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
138 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
139 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000140
Owen Anderson825b72b2009-08-11 20:47:22 +0000141 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000142
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000143 setOperationAction(ISD::VAARG, MVT::Other, Expand);
144 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
145 setOperationAction(ISD::VAEND, MVT::Other, Expand);
146
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000147 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000148 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
149 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
150 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000151
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000152 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000154
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000155 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000156 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
157 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000158 }
159
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000160 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000161 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000162
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000163 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000164 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000165
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000166 setTargetDAGCombine(ISD::ADDE);
167 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000168 setTargetDAGCombine(ISD::SDIVREM);
169 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000170 setTargetDAGCombine(ISD::SETCC);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000171
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000172 setStackPointerRegisterToSaveRestore(Mips::SP);
173 computeRegisterProperties();
174}
175
Owen Anderson825b72b2009-08-11 20:47:22 +0000176MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
177 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000178}
179
Bill Wendlingb4202b82009-07-01 18:50:55 +0000180/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000181unsigned MipsTargetLowering::getFunctionAlignment(const Function *) const {
182 return 2;
183}
Scott Michel5b8f82e2008-03-10 15:42:14 +0000184
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000185// SelectMadd -
186// Transforms a subgraph in CurDAG if the following pattern is found:
187// (addc multLo, Lo0), (adde multHi, Hi0),
188// where,
189// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000190// Lo0: initial value of Lo register
191// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000192// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000193static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000194 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000195 // for the matching to be successful.
196 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
197
198 if (ADDCNode->getOpcode() != ISD::ADDC)
199 return false;
200
201 SDValue MultHi = ADDENode->getOperand(0);
202 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000203 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000204 unsigned MultOpc = MultHi.getOpcode();
205
206 // MultHi and MultLo must be generated by the same node,
207 if (MultLo.getNode() != MultNode)
208 return false;
209
210 // and it must be a multiplication.
211 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
212 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000213
214 // MultLo amd MultHi must be the first and second output of MultNode
215 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000216 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
217 return false;
218
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000219 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000220 // of the values of MultNode, in which case MultNode will be removed in later
221 // phases.
222 // If there exist users other than ADDENode or ADDCNode, this function returns
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000223 // here, which will result in MultNode being mapped to a single MULT
224 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000225 // produced.
226 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
227 return false;
228
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000229 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000230 DebugLoc dl = ADDENode->getDebugLoc();
231
232 // create MipsMAdd(u) node
233 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000234
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000235 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
236 MVT::Glue,
237 MultNode->getOperand(0),// Factor 0
238 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000239 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000240 ADDENode->getOperand(1));// Hi0
241
242 // create CopyFromReg nodes
243 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
244 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000245 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000246 Mips::HI, MVT::i32,
247 CopyFromLo.getValue(2));
248
249 // replace uses of adde and addc here
250 if (!SDValue(ADDCNode, 0).use_empty())
251 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
252
253 if (!SDValue(ADDENode, 0).use_empty())
254 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
255
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000256 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000257}
258
259// SelectMsub -
260// Transforms a subgraph in CurDAG if the following pattern is found:
261// (addc Lo0, multLo), (sube Hi0, multHi),
262// where,
263// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000264// Lo0: initial value of Lo register
265// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000266// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000267static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000268 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000269 // for the matching to be successful.
270 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
271
272 if (SUBCNode->getOpcode() != ISD::SUBC)
273 return false;
274
275 SDValue MultHi = SUBENode->getOperand(1);
276 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000277 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000278 unsigned MultOpc = MultHi.getOpcode();
279
280 // MultHi and MultLo must be generated by the same node,
281 if (MultLo.getNode() != MultNode)
282 return false;
283
284 // and it must be a multiplication.
285 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
286 return false;
287
288 // MultLo amd MultHi must be the first and second output of MultNode
289 // respectively.
290 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
291 return false;
292
293 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
294 // of the values of MultNode, in which case MultNode will be removed in later
295 // phases.
296 // If there exist users other than SUBENode or SUBCNode, this function returns
297 // here, which will result in MultNode being mapped to a single MULT
298 // instruction node rather than a pair of MULT and MSUB instructions being
299 // produced.
300 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
301 return false;
302
303 SDValue Chain = CurDAG->getEntryNode();
304 DebugLoc dl = SUBENode->getDebugLoc();
305
306 // create MipsSub(u) node
307 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
308
309 SDValue MSub = CurDAG->getNode(MultOpc, dl,
310 MVT::Glue,
311 MultNode->getOperand(0),// Factor 0
312 MultNode->getOperand(1),// Factor 1
313 SUBCNode->getOperand(0),// Lo0
314 SUBENode->getOperand(0));// Hi0
315
316 // create CopyFromReg nodes
317 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
318 MSub);
319 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
320 Mips::HI, MVT::i32,
321 CopyFromLo.getValue(2));
322
323 // replace uses of sube and subc here
324 if (!SDValue(SUBCNode, 0).use_empty())
325 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
326
327 if (!SDValue(SUBENode, 0).use_empty())
328 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
329
330 return true;
331}
332
333static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
334 TargetLowering::DAGCombinerInfo &DCI,
335 const MipsSubtarget* Subtarget) {
336 if (DCI.isBeforeLegalize())
337 return SDValue();
338
339 if (Subtarget->isMips32() && SelectMadd(N, &DAG))
340 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000341
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000342 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000343}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000344
345static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
346 TargetLowering::DAGCombinerInfo &DCI,
347 const MipsSubtarget* Subtarget) {
348 if (DCI.isBeforeLegalize())
349 return SDValue();
350
351 if (Subtarget->isMips32() && SelectMsub(N, &DAG))
352 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000353
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000354 return SDValue();
355}
356
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000357static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
358 TargetLowering::DAGCombinerInfo &DCI,
359 const MipsSubtarget* Subtarget) {
360 if (DCI.isBeforeLegalizeOps())
361 return SDValue();
362
363 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
364 MipsISD::DivRemU;
365 DebugLoc dl = N->getDebugLoc();
366
367 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
368 N->getOperand(0), N->getOperand(1));
369 SDValue InChain = DAG.getEntryNode();
370 SDValue InGlue = DivRem;
371
372 // insert MFLO
373 if (N->hasAnyUseOfValue(0)) {
374 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, Mips::LO, MVT::i32,
375 InGlue);
376 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
377 InChain = CopyFromLo.getValue(1);
378 InGlue = CopyFromLo.getValue(2);
379 }
380
381 // insert MFHI
382 if (N->hasAnyUseOfValue(1)) {
383 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
384 Mips::HI, MVT::i32, InGlue);
385 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
386 }
387
388 return SDValue();
389}
390
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000391static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
392 switch (CC) {
393 default: llvm_unreachable("Unknown fp condition code!");
394 case ISD::SETEQ:
395 case ISD::SETOEQ: return Mips::FCOND_OEQ;
396 case ISD::SETUNE: return Mips::FCOND_UNE;
397 case ISD::SETLT:
398 case ISD::SETOLT: return Mips::FCOND_OLT;
399 case ISD::SETGT:
400 case ISD::SETOGT: return Mips::FCOND_OGT;
401 case ISD::SETLE:
402 case ISD::SETOLE: return Mips::FCOND_OLE;
403 case ISD::SETGE:
404 case ISD::SETOGE: return Mips::FCOND_OGE;
405 case ISD::SETULT: return Mips::FCOND_ULT;
406 case ISD::SETULE: return Mips::FCOND_ULE;
407 case ISD::SETUGT: return Mips::FCOND_UGT;
408 case ISD::SETUGE: return Mips::FCOND_UGE;
409 case ISD::SETUO: return Mips::FCOND_UN;
410 case ISD::SETO: return Mips::FCOND_OR;
411 case ISD::SETNE:
412 case ISD::SETONE: return Mips::FCOND_ONE;
413 case ISD::SETUEQ: return Mips::FCOND_UEQ;
414 }
415}
416
417
418// Returns true if condition code has to be inverted.
419static bool InvertFPCondCode(Mips::CondCode CC) {
420 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
421 return false;
422
423 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
424 return true;
425
426 assert(false && "Illegal Condition Code");
427 return false;
428}
429
430// Creates and returns an FPCmp node from a setcc node.
431// Returns Op if setcc is not a floating point comparison.
432static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
433 // must be a SETCC node
434 if (Op.getOpcode() != ISD::SETCC)
435 return Op;
436
437 SDValue LHS = Op.getOperand(0);
438
439 if (!LHS.getValueType().isFloatingPoint())
440 return Op;
441
442 SDValue RHS = Op.getOperand(1);
443 DebugLoc dl = Op.getDebugLoc();
444
445 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of node
446 // if necessary.
447 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
448
449 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
450 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
451}
452
453// Creates and returns a CMovFPT/F node.
454static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
455 SDValue False, DebugLoc DL) {
456 bool invert = InvertFPCondCode((Mips::CondCode)
457 cast<ConstantSDNode>(Cond.getOperand(2))
458 ->getSExtValue());
459
460 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
461 True.getValueType(), True, False, Cond);
462}
463
464static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
465 TargetLowering::DAGCombinerInfo &DCI,
466 const MipsSubtarget* Subtarget) {
467 if (DCI.isBeforeLegalizeOps())
468 return SDValue();
469
470 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
471
472 if (Cond.getOpcode() != MipsISD::FPCmp)
473 return SDValue();
474
475 SDValue True = DAG.getConstant(1, MVT::i32);
476 SDValue False = DAG.getConstant(0, MVT::i32);
477
478 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
479}
480
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000481SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000482 const {
483 SelectionDAG &DAG = DCI.DAG;
484 unsigned opc = N->getOpcode();
485
486 switch (opc) {
487 default: break;
488 case ISD::ADDE:
489 return PerformADDECombine(N, DAG, DCI, Subtarget);
490 case ISD::SUBE:
491 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000492 case ISD::SDIVREM:
493 case ISD::UDIVREM:
494 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000495 case ISD::SETCC:
496 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000497 }
498
499 return SDValue();
500}
501
Dan Gohman475871a2008-07-27 21:46:04 +0000502SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000503LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000504{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000505 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000506 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000507 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000508 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
509 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000510 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000511 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000512 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000513 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
514 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000515 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000516 case ISD::VASTART: return LowerVASTART(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000517 }
Dan Gohman475871a2008-07-27 21:46:04 +0000518 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000519}
520
521//===----------------------------------------------------------------------===//
522// Lower helper functions
523//===----------------------------------------------------------------------===//
524
525// AddLiveIn - This helper function adds the specified physical register to the
526// MachineFunction as a live in value. It also creates a corresponding
527// virtual register for it.
528static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000529AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000530{
531 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000532 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
533 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000534 return VReg;
535}
536
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000537// Get fp branch code (not opcode) from condition code.
538static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
539 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
540 return Mips::BRANCH_T;
541
542 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
543 return Mips::BRANCH_F;
544
545 return Mips::BRANCH_INVALID;
546}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000547
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000548MachineBasicBlock *
549MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000550 MachineBasicBlock *BB) const {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000551 // There is no need to expand CMov instructions if target has
552 // conditional moves.
553 if (Subtarget->hasCondMov())
554 return BB;
555
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000556 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
557 bool isFPCmp = false;
Dale Johannesen94817572009-02-13 02:34:39 +0000558 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000559 unsigned Opc;
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000560
561 switch (MI->getOpcode()) {
562 default: assert(false && "Unexpected instr type to insert");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000563 case Mips::MOVT:
564 case Mips::MOVT_S:
565 case Mips::MOVT_D:
566 isFPCmp = true;
567 Opc = Mips::BC1F;
568 break;
569 case Mips::MOVF:
570 case Mips::MOVF_S:
571 case Mips::MOVF_D:
572 isFPCmp = true;
573 Opc = Mips::BC1T;
574 break;
575 case Mips::MOVZ_I:
576 case Mips::MOVZ_S:
577 case Mips::MOVZ_D:
578 Opc = Mips::BNE;
579 break;
580 case Mips::MOVN_I:
581 case Mips::MOVN_S:
582 case Mips::MOVN_D:
583 Opc = Mips::BEQ;
584 break;
585 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000586
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000587 // To "insert" a SELECT_CC instruction, we actually have to insert the
588 // diamond control-flow pattern. The incoming instruction knows the
589 // destination vreg to set, the condition code register to branch on, the
590 // true/false values to select between, and a branch opcode to use.
591 const BasicBlock *LLVM_BB = BB->getBasicBlock();
592 MachineFunction::iterator It = BB;
593 ++It;
Dan Gohman14152b42010-07-06 20:24:04 +0000594
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000595 // thisMBB:
596 // ...
597 // TrueVal = ...
598 // setcc r1, r2, r3
599 // bNE r1, r0, copy1MBB
600 // fallthrough --> copy0MBB
601 MachineBasicBlock *thisMBB = BB;
602 MachineFunction *F = BB->getParent();
603 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
604 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
605 F->insert(It, copy0MBB);
606 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +0000607
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000608 // Transfer the remainder of BB and its successor edges to sinkMBB.
609 sinkMBB->splice(sinkMBB->begin(), BB,
610 llvm::next(MachineBasicBlock::iterator(MI)),
611 BB->end());
612 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000613
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000614 // Next, add the true and fallthrough blocks as its successors.
615 BB->addSuccessor(copy0MBB);
616 BB->addSuccessor(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000617
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000618 // Emit the right instruction according to the type of the operands compared
619 if (isFPCmp)
620 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
621 else
622 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
623 .addReg(Mips::ZERO).addMBB(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000624
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000625
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000626 // copy0MBB:
627 // %FalseValue = ...
628 // # fallthrough to sinkMBB
629 BB = copy0MBB;
630
631 // Update machine-CFG edges
632 BB->addSuccessor(sinkMBB);
633
634 // sinkMBB:
635 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
636 // ...
637 BB = sinkMBB;
638
639 if (isFPCmp)
Dan Gohman14152b42010-07-06 20:24:04 +0000640 BuildMI(*BB, BB->begin(), dl,
641 TII->get(Mips::PHI), MI->getOperand(0).getReg())
Bruno Cardoso Lopes29e9daa2010-07-20 07:58:51 +0000642 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000643 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
644 else
645 BuildMI(*BB, BB->begin(), dl,
646 TII->get(Mips::PHI), MI->getOperand(0).getReg())
647 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
648 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000649
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000650 MI->eraseFromParent(); // The pseudo instruction is gone now.
651 return BB;
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000652}
653
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000654//===----------------------------------------------------------------------===//
655// Misc Lower Operation implementation
656//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000657
Dan Gohman475871a2008-07-27 21:46:04 +0000658SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000659LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000660{
661 if (!Subtarget->isMips1())
662 return Op;
663
664 MachineFunction &MF = DAG.getMachineFunction();
665 unsigned CCReg = AddLiveIn(MF, Mips::FCR31, Mips::CCRRegisterClass);
666
667 SDValue Chain = DAG.getEntryNode();
668 DebugLoc dl = Op.getDebugLoc();
669 SDValue Src = Op.getOperand(0);
670
671 // Set the condition register
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 SDValue CondReg = DAG.getCopyFromReg(Chain, dl, CCReg, MVT::i32);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000673 CondReg = DAG.getCopyToReg(Chain, dl, Mips::AT, CondReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 CondReg = DAG.getCopyFromReg(CondReg, dl, Mips::AT, MVT::i32);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000675
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 SDValue Cst = DAG.getConstant(3, MVT::i32);
677 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, CondReg, Cst);
678 Cst = DAG.getConstant(2, MVT::i32);
679 SDValue Xor = DAG.getNode(ISD::XOR, dl, MVT::i32, Or, Cst);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000680
681 SDValue InFlag(0, 0);
682 CondReg = DAG.getCopyToReg(Chain, dl, Mips::FCR31, Xor, InFlag);
683
684 // Emit the round instruction and bit convert to integer
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 SDValue Trunc = DAG.getNode(MipsISD::FPRound, dl, MVT::f32,
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000686 Src, CondReg.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000687 SDValue BitCvt = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Trunc);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000688 return BitCvt;
689}
690
691SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000692LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000693{
694 SDValue Chain = Op.getOperand(0);
695 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +0000696 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000697
698 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +0000699 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000700
701 // Subtract the dynamic size from the actual stack size to
702 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000704
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000705 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000706 // must be placed in the stack pointer register.
Dale Johannesena05dca42009-02-04 23:02:30 +0000707 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000708
709 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000710 // value and a chain
711 SDValue Ops[2] = { Sub, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000712 return DAG.getMergeValues(Ops, 2, dl);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000713}
714
715SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000716LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000717{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000718 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000719 // the block to branch to if the condition is true.
720 SDValue Chain = Op.getOperand(0);
721 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000722 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000723
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000724 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
725
726 // Return if flag is not set by a floating point comparision.
727 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000728 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000729
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000730 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000731 Mips::CondCode CC =
732 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000733 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000734
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000735 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000736 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000737}
738
739SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000740LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000741{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000742 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000743
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000744 // Return if flag is not set by a floating point comparision.
745 if (Cond.getOpcode() != MipsISD::FPCmp)
746 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000747
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000748 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
749 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000750}
751
Dan Gohmand858e902010-04-17 15:26:15 +0000752SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
753 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +0000754 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000755 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000756 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000757
Eli Friedmane2c74082009-08-03 02:22:28 +0000758 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +0000759 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000760
Chris Lattnerb71b9092009-08-13 06:28:06 +0000761 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000762
Chris Lattnere3736f82009-08-13 05:41:27 +0000763 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000764 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
765 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000766 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +0000767 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
768 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000769 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +0000770 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000771 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000772 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
773 MipsII::MO_ABS_HI);
774 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
775 MipsII::MO_ABS_LO);
776 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
777 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000778 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000779 } else {
Devang Patel0d881da2010-07-06 22:08:15 +0000780 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000781 MipsII::MO_GOT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000782 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000783 DAG.getEntryNode(), GA, MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000784 false, false, 0);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000785 // On functions and global targets not internal linked only
786 // a load from got/GP is necessary for PIC to work.
Akira Hatanaka9777e7a2011-04-07 19:51:44 +0000787 if (!GV->hasInternalLinkage() &&
788 (!GV->hasLocalLinkage() || isa<Function>(GV)))
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000789 return ResNode;
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000790 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
791 MipsII::MO_ABS_LO);
792 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000793 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000794 }
795
Torok Edwinc23197a2009-07-14 16:55:14 +0000796 llvm_unreachable("Dont know how to handle GlobalAddress");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000797 return SDValue(0,0);
798}
799
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000800SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
801 SelectionDAG &DAG) const {
802 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
803 assert(false && "implement LowerBlockAddress for -static");
804 return SDValue(0, 0);
805 }
806 else {
807 // FIXME there isn't actually debug info here
808 DebugLoc dl = Op.getDebugLoc();
809 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
810 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
811 MipsII::MO_GOT);
812 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000813 MipsII::MO_ABS_LO);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000814 SDValue Load = DAG.getLoad(MVT::i32, dl,
815 DAG.getEntryNode(), BAGOTOffset,
816 MachinePointerInfo(), false, false, 0);
817 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
818 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
819 }
820}
821
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000822SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000823LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000824{
Torok Edwinc23197a2009-07-14 16:55:14 +0000825 llvm_unreachable("TLS not implemented for MIPS.");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000826 return SDValue(); // Not reached
827}
828
829SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000830LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000831{
Dan Gohman475871a2008-07-27 21:46:04 +0000832 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000833 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +0000834 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000835 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000836 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000837 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000838
Owen Andersone50ed302009-08-10 22:56:29 +0000839 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000840 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000841
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000842 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
843
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +0000844 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +0000845 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000846 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000847 } else // Emit Load from Global Pointer
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000848 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
849 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000850 false, false, 0);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000851
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000852 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_LO);
853 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000854 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000855
856 return ResNode;
857}
858
Dan Gohman475871a2008-07-27 21:46:04 +0000859SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000860LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000861{
Dan Gohman475871a2008-07-27 21:46:04 +0000862 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000863 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +0000864 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +0000865 // FIXME there isn't actually debug info here
866 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000867
868 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000869 // FIXME: we should reference the constant pool using small data sections,
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000870 // but the asm printer currently doens't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000871 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000872 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +0000873 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000874 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
875 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000876 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000877
878 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000879 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
880 N->getOffset(), MipsII::MO_ABS_HI);
881 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
882 N->getOffset(), MipsII::MO_ABS_LO);
883 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
884 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000885 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000886 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000887 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000888 N->getOffset(), MipsII::MO_GOT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000889 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000890 CP, MachinePointerInfo::getConstantPool(),
891 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000892 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
893 N->getOffset(), MipsII::MO_ABS_LO);
894 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000895 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
896 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000897
898 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000899}
900
Dan Gohmand858e902010-04-17 15:26:15 +0000901SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +0000902 MachineFunction &MF = DAG.getMachineFunction();
903 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
904
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000905 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +0000906 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
907 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000908
909 // vastart just stores the address of the VarArgsFrameIndex slot into the
910 // memory location argument.
911 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +0000912 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
913 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +0000914 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000915}
916
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000917//===----------------------------------------------------------------------===//
918// Calling Convention Implementation
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000919//===----------------------------------------------------------------------===//
920
921#include "MipsGenCallingConv.inc"
922
923//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000924// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000925// Mips O32 ABI rules:
926// ---
927// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000928// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000929// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000930// f64 - Only passed in two aliased f32 registers if no int reg has been used
931// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000932// not used, it must be shadowed. If only A3 is avaiable, shadow it and
933// go to stack.
934//===----------------------------------------------------------------------===//
935
Duncan Sands1e96bab2010-11-04 10:49:57 +0000936static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000937 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000938 ISD::ArgFlagsTy ArgFlags, CCState &State) {
939
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000940 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000941
942 static const unsigned IntRegs[] = {
943 Mips::A0, Mips::A1, Mips::A2, Mips::A3
944 };
945 static const unsigned F32Regs[] = {
946 Mips::F12, Mips::F14
947 };
948 static const unsigned F64Regs[] = {
949 Mips::D6, Mips::D7
950 };
951
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000952 unsigned Reg = 0;
953 static bool IntRegUsed = false;
954
955 // This must be the first arg of the call if no regs have been allocated.
956 // Initialize IntRegUsed in that case.
957 if (IntRegs[State.getFirstUnallocated(IntRegs, IntRegsSize)] == Mips::A0 &&
958 F32Regs[State.getFirstUnallocated(F32Regs, FloatRegsSize)] == Mips::F12 &&
959 F64Regs[State.getFirstUnallocated(F64Regs, FloatRegsSize)] == Mips::D6)
960 IntRegUsed = false;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000961
962 // Promote i8 and i16
Owen Anderson825b72b2009-08-11 20:47:22 +0000963 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
964 LocVT = MVT::i32;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000965 if (ArgFlags.isSExt())
966 LocInfo = CCValAssign::SExt;
967 else if (ArgFlags.isZExt())
968 LocInfo = CCValAssign::ZExt;
969 else
970 LocInfo = CCValAssign::AExt;
971 }
972
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000973 if (ValVT == MVT::i32) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000974 Reg = State.AllocateReg(IntRegs, IntRegsSize);
975 IntRegUsed = true;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000976 } else if (ValVT == MVT::f32) {
977 // An int reg has to be marked allocated regardless of whether or not
978 // IntRegUsed is true.
979 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000980
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000981 if (IntRegUsed) {
982 if (Reg) // Int reg is available
983 LocVT = MVT::i32;
984 } else {
985 unsigned FReg = State.AllocateReg(F32Regs, FloatRegsSize);
986 if (FReg) // F32 reg is available
987 Reg = FReg;
988 else if (Reg) // No F32 regs are available, but an int reg is available.
989 LocVT = MVT::i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000990 }
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000991 } else if (ValVT == MVT::f64) {
992 // Int regs have to be marked allocated regardless of whether or not
993 // IntRegUsed is true.
994 Reg = State.AllocateReg(IntRegs, IntRegsSize);
995 if (Reg == Mips::A1)
996 Reg = State.AllocateReg(IntRegs, IntRegsSize);
997 else if (Reg == Mips::A3)
998 Reg = 0;
999 State.AllocateReg(IntRegs, IntRegsSize);
1000
1001 // At this point, Reg is A0, A2 or 0, and all the unavailable integer regs
1002 // are marked as allocated.
1003 if (IntRegUsed) {
1004 if (Reg)// if int reg is available
1005 LocVT = MVT::i32;
1006 } else {
1007 unsigned FReg = State.AllocateReg(F64Regs, FloatRegsSize);
1008 if (FReg) // F64 reg is available.
1009 Reg = FReg;
1010 else if (Reg) // No F64 regs are available, but an int reg is available.
1011 LocVT = MVT::i32;
1012 }
1013 } else
1014 assert(false && "cannot handle this ValVT");
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001015
1016 if (!Reg) {
1017 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1018 unsigned Offset = State.AllocateStack(SizeInBytes, SizeInBytes);
1019 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
1020 } else
1021 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
1022
1023 return false; // CC must always match
1024}
1025
Duncan Sands1e96bab2010-11-04 10:49:57 +00001026static bool CC_MipsO32_VarArgs(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001027 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001028 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1029
1030 static const unsigned IntRegsSize=4;
1031
1032 static const unsigned IntRegs[] = {
1033 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1034 };
1035
1036 // Promote i8 and i16
1037 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1038 LocVT = MVT::i32;
1039 if (ArgFlags.isSExt())
1040 LocInfo = CCValAssign::SExt;
1041 else if (ArgFlags.isZExt())
1042 LocInfo = CCValAssign::ZExt;
1043 else
1044 LocInfo = CCValAssign::AExt;
1045 }
1046
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001047 unsigned Reg;
1048
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001049 if (ValVT == MVT::i32 || ValVT == MVT::f32) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001050 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1051 LocVT = MVT::i32;
1052 } else if (ValVT == MVT::f64) {
1053 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1054 if (Reg == Mips::A1 || Reg == Mips::A3)
1055 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1056 State.AllocateReg(IntRegs, IntRegsSize);
1057 LocVT = MVT::i32;
1058 } else
1059 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001060
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001061 if (!Reg) {
1062 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1063 unsigned Offset = State.AllocateStack(SizeInBytes, SizeInBytes);
1064 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
1065 } else
1066 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001067
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001068 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001069}
1070
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001071//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001072// Call Calling Convention Implementation
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001073//===----------------------------------------------------------------------===//
1074
Dan Gohman98ca4f22009-08-05 01:29:28 +00001075/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001076/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001077/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001078SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001079MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001080 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001081 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001082 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001083 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001084 const SmallVectorImpl<ISD::InputArg> &Ins,
1085 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001086 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001087 // MIPs target does not yet support tail call optimization.
1088 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001089
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001090 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001091 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001092 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001093
1094 // Analyze operands of the call, assigning locations to each operand.
1095 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001096 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1097 *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001098
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +00001099 // To meet O32 ABI, Mips must always allocate 16 bytes on
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001100 // the stack (even if less than 4 are used as arguments)
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +00001101 if (Subtarget->isABI_O32()) {
Duncan Sands1e96bab2010-11-04 10:49:57 +00001102 int VTsize = MVT(MVT::i32).getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +00001103 MFI->CreateFixedObject(VTsize, (VTsize*3), true);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001104 CCInfo.AnalyzeCallOperands(Outs,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001105 isVarArg ? CC_MipsO32_VarArgs : CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001106 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001107 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001108
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001109 // Get a count of how many bytes are to be pushed on the stack.
1110 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnere563bbc2008-10-11 22:08:30 +00001111 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001112
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001113 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001114 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1115 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001116
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001117 // First/LastArgStackLoc contains the first/last
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001118 // "at stack" argument location.
1119 int LastArgStackLoc = 0;
1120 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001121
1122 // Walk the register/memloc assignments, inserting copies/loads.
1123 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001124 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001125 CCValAssign &VA = ArgLocs[i];
1126
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001127 // Promote the value if needed.
1128 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001129 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001130 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001131 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001132 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001133 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001134 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001135 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001136 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001137 DAG.getConstant(0, getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00001138 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001139 DAG.getConstant(1, getPointerTy()));
1140 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
1141 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
1142 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001143 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001144 }
1145 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00001146 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001147 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001148 break;
1149 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001150 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001151 break;
1152 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001153 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001154 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001155 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001156
1157 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001158 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001159 if (VA.isRegLoc()) {
1160 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00001161 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001162 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001163
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001164 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00001165 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001166
Chris Lattnere0b12152008-03-17 06:57:02 +00001167 // Create the frame index object for this incoming parameter
1168 // This guarantees that when allocating Local Area the firsts
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001169 // 16 bytes which are alwayes reserved won't be overwritten
1170 // if O32 ABI is used. For EABI the first address is zero.
1171 LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001172 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001173 LastArgStackLoc, true);
Chris Lattnere0b12152008-03-17 06:57:02 +00001174
Dan Gohman475871a2008-07-27 21:46:04 +00001175 SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00001176
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001177 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00001178 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00001179 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
1180 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001181 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001182 }
1183
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001184 // Transform all store nodes into one single node because all store
1185 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001186 if (!MemOpChains.empty())
1187 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001188 &MemOpChains[0], MemOpChains.size());
1189
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001190 // Build a sequence of copy-to-reg nodes chained together with token
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001191 // chain and flag operands which copy the outgoing args into registers.
1192 // The InFlag in necessary since all emited instructions must be
1193 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +00001194 SDValue InFlag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001195 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001196 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001197 RegsToPass[i].second, InFlag);
1198 InFlag = Chain.getValue(1);
1199 }
1200
Bill Wendling056292f2008-09-16 21:48:12 +00001201 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001202 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1203 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001204 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001205 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001206 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001207
1208 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001209 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
1210 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1211 getPointerTy(), 0,MipsII:: MO_GOT);
1212 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
1213 0, MipsII::MO_ABS_LO);
1214 } else {
1215 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1216 getPointerTy(), 0, OpFlag);
1217 }
1218
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001219 LoadSymAddr = true;
1220 }
1221 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001222 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001223 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001224 LoadSymAddr = true;
1225 }
1226
1227 // Create nodes that load address of callee and copy it to T9
1228 if (IsPIC) {
1229 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001230 // Load callee address
1231 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, Chain, Callee,
1232 MachinePointerInfo::getGOT(),
1233 false, false, 0);
1234
1235 // Use GOT+LO if callee has internal linkage.
1236 if (CalleeLo.getNode()) {
1237 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
1238 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
1239 } else
1240 Callee = LoadValue;
1241
1242 // Use chain output from LoadValue
1243 Chain = LoadValue.getValue(1);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001244 }
1245
1246 // copy to T9
1247 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
1248 InFlag = Chain.getValue(1);
1249 Callee = DAG.getRegister(Mips::T9, MVT::i32);
1250 }
Bill Wendling056292f2008-09-16 21:48:12 +00001251
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001252 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001253 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001254 //
1255 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001256 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00001257 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001258 Ops.push_back(Chain);
1259 Ops.push_back(Callee);
1260
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001261 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001262 // known live into the call.
1263 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1264 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1265 RegsToPass[i].second.getValueType()));
1266
Gabor Greifba36cb52008-08-28 21:40:38 +00001267 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001268 Ops.push_back(InFlag);
1269
Dale Johannesen33c960f2009-02-04 20:06:27 +00001270 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001271 InFlag = Chain.getValue(1);
1272
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001273 // Create a stack location to hold GP when PIC is used. This stack
1274 // location is used on function prologue to save GP and also after all
1275 // emited CALL's to restore GP.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001276 if (IsPIC) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001277 // Function can have an arbitrary number of calls, so
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001278 // hold the LastArgStackLoc with the biggest offset.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001279 int FI;
1280 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001281 if (LastArgStackLoc >= MipsFI->getGPStackOffset()) {
1282 LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001283 // Create the frame index only once. SPOffset here can be anything
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001284 // (this will be fixed on processFunctionBeforeFrameFinalized)
1285 if (MipsFI->getGPStackOffset() == -1) {
Evan Chenged2ae132010-07-03 00:40:23 +00001286 FI = MFI->CreateFixedObject(4, 0, true);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001287 MipsFI->setGPFI(FI);
1288 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001289 MipsFI->setGPStackOffset(LastArgStackLoc);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001290 }
1291
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001292 // Reload GP value.
1293 FI = MipsFI->getGPFI();
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001294 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1295 SDValue GPLoad = DAG.getLoad(MVT::i32, dl, Chain, FIN,
1296 MachinePointerInfo::getFixedStack(FI),
David Greenef6fa1862010-02-15 16:56:10 +00001297 false, false, 0);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001298 Chain = GPLoad.getValue(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001299 Chain = DAG.getCopyToReg(Chain, dl, DAG.getRegister(Mips::GP, MVT::i32),
Dan Gohman475871a2008-07-27 21:46:04 +00001300 GPLoad, SDValue(0,0));
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +00001301 InFlag = Chain.getValue(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001302 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001303
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00001304 // Create the CALLSEQ_END node.
1305 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1306 DAG.getIntPtrConstant(0, true), InFlag);
1307 InFlag = Chain.getValue(1);
1308
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001309 // Handle result values, copying them out of physregs into vregs that we
1310 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001311 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
1312 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001313}
1314
Dan Gohman98ca4f22009-08-05 01:29:28 +00001315/// LowerCallResult - Lower the result values of a call into the
1316/// appropriate copies out of appropriate physical registers.
1317SDValue
1318MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001319 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001320 const SmallVectorImpl<ISD::InputArg> &Ins,
1321 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001322 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001323
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001324 // Assign locations to each value returned by this call.
1325 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001326 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001327 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001328
Dan Gohman98ca4f22009-08-05 01:29:28 +00001329 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001330
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001331 // Copy all of the result registers out of their specified physreg.
1332 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001333 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001334 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001335 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001336 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001337 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001338
Dan Gohman98ca4f22009-08-05 01:29:28 +00001339 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001340}
1341
1342//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001343// Formal Arguments Calling Convention Implementation
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001344//===----------------------------------------------------------------------===//
1345
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001346/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001347/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001348SDValue
1349MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001350 CallingConv::ID CallConv, bool isVarArg,
1351 const SmallVectorImpl<ISD::InputArg>
1352 &Ins,
1353 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001354 SmallVectorImpl<SDValue> &InVals)
1355 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001356
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00001357 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001358 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001359 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001360
Dan Gohman1e93df62010-04-17 14:41:14 +00001361 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001362
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001363 // Used with vargs to acumulate store chains.
1364 std::vector<SDValue> OutChains;
1365
1366 // Keep track of the last register used for arguments
1367 unsigned ArgRegEnd = 0;
1368
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001369 // Assign locations to all of the incoming arguments.
1370 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001371 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1372 ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001373
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001374 if (Subtarget->isABI_O32())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001375 CCInfo.AnalyzeFormalArguments(Ins,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001376 isVarArg ? CC_MipsO32_VarArgs : CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001377 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001378 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001379
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001380 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
Chris Lattner109d6db2011-03-11 02:12:51 +00001381 unsigned LastStackArgEndOffset = 0;
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001382 EVT LastRegArgValVT;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001383
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001384 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001385 CCValAssign &VA = ArgLocs[i];
1386
1387 // Arguments stored on registers
1388 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001389 EVT RegVT = VA.getLocVT();
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001390 ArgRegEnd = VA.getLocReg();
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001391 LastRegArgValVT = VA.getValVT();
Bill Wendling06b8c192008-07-09 05:55:53 +00001392 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001393
Owen Anderson825b72b2009-08-11 20:47:22 +00001394 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001395 RC = Mips::CPURegsRegisterClass;
1396 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001397 RC = Mips::FGR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001398 else if (RegVT == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001399 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001400 RC = Mips::AFGR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001401 } else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001402 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001403
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001404 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001405 // physical registers into virtual ones
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001406 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegEnd, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001407 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001408
1409 // If this is an 8 or 16-bit value, it has been passed promoted
1410 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001411 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001412 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00001413 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001414 if (VA.getLocInfo() == CCValAssign::SExt)
1415 Opcode = ISD::AssertSext;
1416 else if (VA.getLocInfo() == CCValAssign::ZExt)
1417 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00001418 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001419 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00001420 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00001421 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001422 }
1423
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001424 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001425 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001426 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
1427 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00001428 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001429 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001430 VA.getLocReg()+1, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001431 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001432 SDValue Pair = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, ArgValue,
1433 ArgValue2);
Bruno Cardoso Lopesb1fce0a2011-01-18 19:38:25 +00001434 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Pair);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001435 }
1436 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001437
Dan Gohman98ca4f22009-08-05 01:29:28 +00001438 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001439 } else { // VA.isRegLoc()
1440
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001441 // sanity check
1442 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001443
1444 // The last argument is not a register anymore
1445 ArgRegEnd = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001446
1447 // The stack pointer offset is relative to the caller stack frame.
1448 // Since the real stack size is unknown here, a negative SPOffset
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001449 // is used so there's a way to adjust these offsets when the stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001450 // size get known (on EliminateFrameIndex). A dummy SPOffset is
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001451 // used instead of a direct negative address (which is recorded to
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001452 // be used on emitPrologue) to avoid mis-calc of the first stack
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001453 // offset on PEI::calculateFrameObjectOffsets.
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001454 unsigned ArgSize = VA.getValVT().getSizeInBits()/8;
1455 LastStackArgEndOffset = FirstStackArgLoc + VA.getLocMemOffset() + ArgSize;
Evan Chenged2ae132010-07-03 00:40:23 +00001456 int FI = MFI->CreateFixedObject(ArgSize, 0, true);
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001457 MipsFI->recordLoadArgsFI(FI, -(4 +
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001458 (FirstStackArgLoc + VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001459
1460 // Create load nodes to retrieve arguments from the stack
Dan Gohman475871a2008-07-27 21:46:04 +00001461 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001462 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
1463 MachinePointerInfo::getFixedStack(FI),
David Greenef6fa1862010-02-15 16:56:10 +00001464 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001465 }
1466 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001467
1468 // The mips ABIs for returning structs by value requires that we copy
1469 // the sret argument into $v0 for the return. Save the argument into
1470 // a virtual register so that we can access it from the return points.
1471 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1472 unsigned Reg = MipsFI->getSRetReturnReg();
1473 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001474 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001475 MipsFI->setSRetReturnReg(Reg);
1476 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001477 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001478 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001479 }
1480
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001481 // To meet ABI, when VARARGS are passed on registers, the registers
1482 // must have their values written to the caller stack frame. If the last
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001483 // argument was placed in the stack, there's no need to save any register.
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001484 if (isVarArg && Subtarget->isABI_O32()) {
1485 if (ArgRegEnd) {
1486 // Last named formal argument is passed in register.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001487
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001488 // The last register argument that must be saved is Mips::A3
1489 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
1490 if (LastRegArgValVT == MVT::f64)
1491 ArgRegEnd++;
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001492
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001493 if (ArgRegEnd < Mips::A3) {
1494 // Both the last named formal argument and the first variable
1495 // argument are passed in registers.
1496 for (++ArgRegEnd; ArgRegEnd <= Mips::A3; ++ArgRegEnd) {
1497 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegEnd, RC);
1498 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001499
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001500 int FI = MFI->CreateFixedObject(4, 0, true);
1501 MipsFI->recordStoreVarArgsFI(FI, -(4+(ArgRegEnd-Mips::A0)*4));
1502 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
1503 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
1504 MachinePointerInfo(),
1505 false, false, 0));
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001506
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001507 // Record the frame index of the first variable argument
1508 // which is a value necessary to VASTART.
1509 if (!MipsFI->getVarArgsFrameIndex()) {
1510 MFI->setObjectAlignment(FI, 4);
1511 MipsFI->setVarArgsFrameIndex(FI);
1512 }
1513 }
1514 } else {
1515 // Last named formal argument is in register Mips::A3, and the first
1516 // variable argument is on stack. Record the frame index of the first
1517 // variable argument.
1518 int FI = MFI->CreateFixedObject(4, 0, true);
1519 MFI->setObjectAlignment(FI, 4);
1520 MipsFI->recordStoreVarArgsFI(FI, -20);
Dan Gohman1e93df62010-04-17 14:41:14 +00001521 MipsFI->setVarArgsFrameIndex(FI);
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001522 }
1523 } else {
1524 // Last named formal argument and all the variable arguments are passed
1525 // on stack. Record the frame index of the first variable argument.
1526 int FI = MFI->CreateFixedObject(4, 0, true);
1527 MFI->setObjectAlignment(FI, 4);
1528 MipsFI->recordStoreVarArgsFI(FI, -(4+LastStackArgEndOffset));
1529 MipsFI->setVarArgsFrameIndex(FI);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001530 }
1531 }
1532
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001533 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001534 // the size of Ins and InVals. This only happens when on varg functions
1535 if (!OutChains.empty()) {
1536 OutChains.push_back(Chain);
1537 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1538 &OutChains[0], OutChains.size());
1539 }
1540
Dan Gohman98ca4f22009-08-05 01:29:28 +00001541 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001542}
1543
1544//===----------------------------------------------------------------------===//
1545// Return Value Calling Convention Implementation
1546//===----------------------------------------------------------------------===//
1547
Dan Gohman98ca4f22009-08-05 01:29:28 +00001548SDValue
1549MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001550 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001551 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001552 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001553 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001554
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001555 // CCValAssign - represent the assignment of
1556 // the return value to a location
1557 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001558
1559 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001560 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1561 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001562
Dan Gohman98ca4f22009-08-05 01:29:28 +00001563 // Analize return values.
1564 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001565
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001566 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001567 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001568 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001569 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001570 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001571 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001572 }
1573
Dan Gohman475871a2008-07-27 21:46:04 +00001574 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001575
1576 // Copy the result values into the output registers.
1577 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1578 CCValAssign &VA = RVLocs[i];
1579 assert(VA.isRegLoc() && "Can only return in registers!");
1580
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001581 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001582 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001583
1584 // guarantee that all emitted copies are
1585 // stuck together, avoiding something bad
1586 Flag = Chain.getValue(1);
1587 }
1588
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001589 // The mips ABIs for returning structs by value requires that we copy
1590 // the sret argument into $v0 for the return. We saved the argument into
1591 // a virtual register in the entry block, so now we copy the value out
1592 // and into $v0.
1593 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1594 MachineFunction &MF = DAG.getMachineFunction();
1595 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1596 unsigned Reg = MipsFI->getSRetReturnReg();
1597
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001598 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00001599 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00001600 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001601
Dale Johannesena05dca42009-02-04 23:02:30 +00001602 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001603 Flag = Chain.getValue(1);
1604 }
1605
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001606 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00001607 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001608 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001609 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001610 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001611 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001612 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001613}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001614
1615//===----------------------------------------------------------------------===//
1616// Mips Inline Assembly Support
1617//===----------------------------------------------------------------------===//
1618
1619/// getConstraintType - Given a constraint letter, return the type of
1620/// constraint it is for this target.
1621MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001622getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001623{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001624 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001625 // GCC config/mips/constraints.md
1626 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001627 // 'd' : An address register. Equivalent to r
1628 // unless generating MIPS16 code.
1629 // 'y' : Equivalent to r; retained for
1630 // backwards compatibility.
1631 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001632 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001633 switch (Constraint[0]) {
1634 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001635 case 'd':
1636 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001637 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001638 return C_RegisterClass;
1639 break;
1640 }
1641 }
1642 return TargetLowering::getConstraintType(Constraint);
1643}
1644
John Thompson44ab89e2010-10-29 17:29:13 +00001645/// Examine constraint type and operand type and determine a weight value.
1646/// This object must already have been set up with the operand type
1647/// and the current alternative constraint selected.
1648TargetLowering::ConstraintWeight
1649MipsTargetLowering::getSingleConstraintMatchWeight(
1650 AsmOperandInfo &info, const char *constraint) const {
1651 ConstraintWeight weight = CW_Invalid;
1652 Value *CallOperandVal = info.CallOperandVal;
1653 // If we don't have a value, we can't do a match,
1654 // but allow it at the lowest weight.
1655 if (CallOperandVal == NULL)
1656 return CW_Default;
1657 const Type *type = CallOperandVal->getType();
1658 // Look at the constraint type.
1659 switch (*constraint) {
1660 default:
1661 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
1662 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001663 case 'd':
1664 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00001665 if (type->isIntegerTy())
1666 weight = CW_Register;
1667 break;
1668 case 'f':
1669 if (type->isFloatTy())
1670 weight = CW_Register;
1671 break;
1672 }
1673 return weight;
1674}
1675
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001676/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1677/// return a list of registers that can be used to satisfy the constraint.
1678/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001679std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00001680getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001681{
1682 if (Constraint.size() == 1) {
1683 switch (Constraint[0]) {
1684 case 'r':
1685 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001686 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001687 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001688 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001689 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001690 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1691 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001692 }
1693 }
1694 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1695}
1696
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001697/// Given a register class constraint, like 'r', if this corresponds directly
1698/// to an LLVM register class, return a register of 0 and the register class
1699/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001700std::vector<unsigned> MipsTargetLowering::
1701getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001702 EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001703{
1704 if (Constraint.size() != 1)
1705 return std::vector<unsigned>();
1706
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001707 switch (Constraint[0]) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001708 default : break;
1709 case 'r':
1710 // GCC Mips Constraint Letters
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001711 case 'd':
1712 case 'y':
1713 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
1714 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
1715 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001716 Mips::T8, 0);
1717
1718 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001719 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001720 if (Subtarget->isSingleFloat())
1721 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1722 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1723 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1724 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1725 Mips::F30, Mips::F31, 0);
1726 else
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001727 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
1728 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001729 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +00001730 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001731
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001732 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001733 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001734 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
1735 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001736 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001737 }
1738 return std::vector<unsigned>();
1739}
Dan Gohman6520e202008-10-18 02:06:02 +00001740
1741bool
1742MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1743 // The Mips target isn't yet aware of offsets.
1744 return false;
1745}
Evan Chengeb2f9692009-10-27 19:56:55 +00001746
Evan Chenga1eaa3c2009-10-28 01:43:28 +00001747bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
1748 if (VT != MVT::f32 && VT != MVT::f64)
1749 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00001750 if (Imm.isNegZero())
1751 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00001752 return Imm.isZero();
1753}