blob: a563d3cbd2c985302b96404d978794f9eb815885 [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";
44 case MipsISD::SelectCC : return "MipsISD::SelectCC";
45 case MipsISD::FPSelectCC : return "MipsISD::FPSelectCC";
46 case MipsISD::FPBrcond : return "MipsISD::FPBrcond";
47 case MipsISD::FPCmp : return "MipsISD::FPCmp";
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 Lopes6d399bd2008-07-29 19:05:28 +000053 default : return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000054 }
55}
56
57MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000058MipsTargetLowering(MipsTargetMachine &TM)
Chris Lattnerb71b9092009-08-13 06:28:06 +000059 : TargetLowering(TM, new MipsTargetObjectFile()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000060 Subtarget = &TM.getSubtarget<MipsSubtarget>();
61
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000062 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000063 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000064 setBooleanContents(ZeroOrOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000065
66 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000067 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
68 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000069
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000070 // When dealing with single precision only, use libcalls
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000071 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000072 if (!Subtarget->isFP64bit())
Owen Anderson825b72b2009-08-11 20:47:22 +000073 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000074
Wesley Peckbf17cfa2010-11-23 03:31:01 +000075 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +000076 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
77 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
78 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000079
Eli Friedman6055a6a2009-07-17 04:07:24 +000080 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +000081 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
82 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +000083
Wesley Peckbf17cfa2010-11-23 03:31:01 +000084 // Used by legalize types to correctly generate the setcc result.
85 // Without this, every float setcc comes with a AND/OR with the result,
86 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000087 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +000088 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000089
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000090 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +000091 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
92 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
93 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
94 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
95 setOperationAction(ISD::SELECT, MVT::f32, Custom);
96 setOperationAction(ISD::SELECT, MVT::f64, Custom);
97 setOperationAction(ISD::SELECT, MVT::i32, Custom);
98 setOperationAction(ISD::SETCC, MVT::f32, Custom);
99 setOperationAction(ISD::SETCC, MVT::f64, Custom);
100 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
101 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
102 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000103 setOperationAction(ISD::VASTART, MVT::Other, Custom);
104
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000105
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000106 // We custom lower AND/OR to handle the case where the DAG contain 'ands/ors'
107 // with operands comming from setcc fp comparions. This is necessary since
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +0000108 // the result from these setcc are in a flag registers (FCR31).
Owen Anderson825b72b2009-08-11 20:47:22 +0000109 setOperationAction(ISD::AND, MVT::i32, Custom);
110 setOperationAction(ISD::OR, MVT::i32, Custom);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000111
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000112 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000113 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
114 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
115 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
116 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
117 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
119 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
120 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
121 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000122
123 if (!Subtarget->isMips32r2())
124 setOperationAction(ISD::ROTR, MVT::i32, Expand);
125
Owen Anderson825b72b2009-08-11 20:47:22 +0000126 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
127 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
128 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
129 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
130 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
131 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000132 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000133 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000134 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
136 setOperationAction(ISD::FPOW, MVT::f32, Expand);
137 setOperationAction(ISD::FLOG, MVT::f32, Expand);
138 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
139 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
140 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000141
Owen Anderson825b72b2009-08-11 20:47:22 +0000142 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000143
144 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
146 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
147 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000148
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000149 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000150 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000151
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000152 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
154 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000155 }
156
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000157 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000159
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000160 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000161 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000162
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000163 setTargetDAGCombine(ISD::ADDE);
164 setTargetDAGCombine(ISD::SUBE);
165
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000166 setStackPointerRegisterToSaveRestore(Mips::SP);
167 computeRegisterProperties();
168}
169
Owen Anderson825b72b2009-08-11 20:47:22 +0000170MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
171 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000172}
173
Bill Wendlingb4202b82009-07-01 18:50:55 +0000174/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000175unsigned MipsTargetLowering::getFunctionAlignment(const Function *) const {
176 return 2;
177}
Scott Michel5b8f82e2008-03-10 15:42:14 +0000178
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000179// SelectMadd -
180// Transforms a subgraph in CurDAG if the following pattern is found:
181// (addc multLo, Lo0), (adde multHi, Hi0),
182// where,
183// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000184// Lo0: initial value of Lo register
185// Hi0: initial value of Hi register
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000186// Return true if mattern matching was successful.
187static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000188 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000189 // for the matching to be successful.
190 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
191
192 if (ADDCNode->getOpcode() != ISD::ADDC)
193 return false;
194
195 SDValue MultHi = ADDENode->getOperand(0);
196 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000197 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000198 unsigned MultOpc = MultHi.getOpcode();
199
200 // MultHi and MultLo must be generated by the same node,
201 if (MultLo.getNode() != MultNode)
202 return false;
203
204 // and it must be a multiplication.
205 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
206 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000207
208 // MultLo amd MultHi must be the first and second output of MultNode
209 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000210 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
211 return false;
212
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000213 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000214 // of the values of MultNode, in which case MultNode will be removed in later
215 // phases.
216 // If there exist users other than ADDENode or ADDCNode, this function returns
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000217 // here, which will result in MultNode being mapped to a single MULT
218 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000219 // produced.
220 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
221 return false;
222
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000223 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000224 DebugLoc dl = ADDENode->getDebugLoc();
225
226 // create MipsMAdd(u) node
227 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000228
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000229 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
230 MVT::Glue,
231 MultNode->getOperand(0),// Factor 0
232 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000233 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000234 ADDENode->getOperand(1));// Hi0
235
236 // create CopyFromReg nodes
237 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
238 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000239 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000240 Mips::HI, MVT::i32,
241 CopyFromLo.getValue(2));
242
243 // replace uses of adde and addc here
244 if (!SDValue(ADDCNode, 0).use_empty())
245 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
246
247 if (!SDValue(ADDENode, 0).use_empty())
248 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
249
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000250 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000251}
252
253// SelectMsub -
254// Transforms a subgraph in CurDAG if the following pattern is found:
255// (addc Lo0, multLo), (sube Hi0, multHi),
256// where,
257// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000258// Lo0: initial value of Lo register
259// Hi0: initial value of Hi register
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000260// Return true if mattern matching was successful.
261static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000262 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000263 // for the matching to be successful.
264 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
265
266 if (SUBCNode->getOpcode() != ISD::SUBC)
267 return false;
268
269 SDValue MultHi = SUBENode->getOperand(1);
270 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000271 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000272 unsigned MultOpc = MultHi.getOpcode();
273
274 // MultHi and MultLo must be generated by the same node,
275 if (MultLo.getNode() != MultNode)
276 return false;
277
278 // and it must be a multiplication.
279 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
280 return false;
281
282 // MultLo amd MultHi must be the first and second output of MultNode
283 // respectively.
284 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
285 return false;
286
287 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
288 // of the values of MultNode, in which case MultNode will be removed in later
289 // phases.
290 // If there exist users other than SUBENode or SUBCNode, this function returns
291 // here, which will result in MultNode being mapped to a single MULT
292 // instruction node rather than a pair of MULT and MSUB instructions being
293 // produced.
294 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
295 return false;
296
297 SDValue Chain = CurDAG->getEntryNode();
298 DebugLoc dl = SUBENode->getDebugLoc();
299
300 // create MipsSub(u) node
301 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
302
303 SDValue MSub = CurDAG->getNode(MultOpc, dl,
304 MVT::Glue,
305 MultNode->getOperand(0),// Factor 0
306 MultNode->getOperand(1),// Factor 1
307 SUBCNode->getOperand(0),// Lo0
308 SUBENode->getOperand(0));// Hi0
309
310 // create CopyFromReg nodes
311 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
312 MSub);
313 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
314 Mips::HI, MVT::i32,
315 CopyFromLo.getValue(2));
316
317 // replace uses of sube and subc here
318 if (!SDValue(SUBCNode, 0).use_empty())
319 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
320
321 if (!SDValue(SUBENode, 0).use_empty())
322 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
323
324 return true;
325}
326
327static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
328 TargetLowering::DAGCombinerInfo &DCI,
329 const MipsSubtarget* Subtarget) {
330 if (DCI.isBeforeLegalize())
331 return SDValue();
332
333 if (Subtarget->isMips32() && SelectMadd(N, &DAG))
334 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000335
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000336 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000337}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000338
339static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
340 TargetLowering::DAGCombinerInfo &DCI,
341 const MipsSubtarget* Subtarget) {
342 if (DCI.isBeforeLegalize())
343 return SDValue();
344
345 if (Subtarget->isMips32() && SelectMsub(N, &DAG))
346 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000347
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000348 return SDValue();
349}
350
351SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000352 const {
353 SelectionDAG &DAG = DCI.DAG;
354 unsigned opc = N->getOpcode();
355
356 switch (opc) {
357 default: break;
358 case ISD::ADDE:
359 return PerformADDECombine(N, DAG, DCI, Subtarget);
360 case ISD::SUBE:
361 return PerformSUBECombine(N, DAG, DCI, Subtarget);
362 }
363
364 return SDValue();
365}
366
Dan Gohman475871a2008-07-27 21:46:04 +0000367SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000368LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000369{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000370 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000371 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000372 case ISD::AND: return LowerANDOR(Op, DAG);
373 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000374 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
375 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000376 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000377 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
378 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
379 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
380 case ISD::OR: return LowerANDOR(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000381 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000382 case ISD::SETCC: return LowerSETCC(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000383 case ISD::VASTART: return LowerVASTART(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000384 }
Dan Gohman475871a2008-07-27 21:46:04 +0000385 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000386}
387
388//===----------------------------------------------------------------------===//
389// Lower helper functions
390//===----------------------------------------------------------------------===//
391
392// AddLiveIn - This helper function adds the specified physical register to the
393// MachineFunction as a live in value. It also creates a corresponding
394// virtual register for it.
395static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000396AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000397{
398 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000399 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
400 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000401 return VReg;
402}
403
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000404// Get fp branch code (not opcode) from condition code.
405static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
406 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
407 return Mips::BRANCH_T;
408
409 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
410 return Mips::BRANCH_F;
411
412 return Mips::BRANCH_INVALID;
413}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000414
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000415static unsigned FPBranchCodeToOpc(Mips::FPBranchCode BC) {
416 switch(BC) {
417 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000418 llvm_unreachable("Unknown branch code");
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000419 case Mips::BRANCH_T : return Mips::BC1T;
420 case Mips::BRANCH_F : return Mips::BC1F;
421 case Mips::BRANCH_TL : return Mips::BC1TL;
422 case Mips::BRANCH_FL : return Mips::BC1FL;
423 }
424}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000425
426static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
427 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000428 default: llvm_unreachable("Unknown fp condition code!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000429 case ISD::SETEQ:
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000430 case ISD::SETOEQ: return Mips::FCOND_EQ;
431 case ISD::SETUNE: return Mips::FCOND_OGL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000432 case ISD::SETLT:
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000433 case ISD::SETOLT: return Mips::FCOND_OLT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000434 case ISD::SETGT:
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000435 case ISD::SETOGT: return Mips::FCOND_OGT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000436 case ISD::SETLE:
437 case ISD::SETOLE: return Mips::FCOND_OLE;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000438 case ISD::SETGE:
439 case ISD::SETOGE: return Mips::FCOND_OGE;
440 case ISD::SETULT: return Mips::FCOND_ULT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000441 case ISD::SETULE: return Mips::FCOND_ULE;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000442 case ISD::SETUGT: return Mips::FCOND_UGT;
443 case ISD::SETUGE: return Mips::FCOND_UGE;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000444 case ISD::SETUO: return Mips::FCOND_UN;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000445 case ISD::SETO: return Mips::FCOND_OR;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000446 case ISD::SETNE:
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000447 case ISD::SETONE: return Mips::FCOND_NEQ;
448 case ISD::SETUEQ: return Mips::FCOND_UEQ;
449 }
450}
451
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000452MachineBasicBlock *
453MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000454 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000455 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
456 bool isFPCmp = false;
Dale Johannesen94817572009-02-13 02:34:39 +0000457 DebugLoc dl = MI->getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000458
459 switch (MI->getOpcode()) {
460 default: assert(false && "Unexpected instr type to insert");
461 case Mips::Select_FCC:
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000462 case Mips::Select_FCC_S32:
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000463 case Mips::Select_FCC_D32:
464 isFPCmp = true; // FALL THROUGH
465 case Mips::Select_CC:
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000466 case Mips::Select_CC_S32:
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000467 case Mips::Select_CC_D32: {
468 // To "insert" a SELECT_CC instruction, we actually have to insert the
469 // diamond control-flow pattern. The incoming instruction knows the
470 // destination vreg to set, the condition code register to branch on, the
471 // true/false values to select between, and a branch opcode to use.
472 const BasicBlock *LLVM_BB = BB->getBasicBlock();
473 MachineFunction::iterator It = BB;
474 ++It;
475
476 // thisMBB:
477 // ...
478 // TrueVal = ...
479 // setcc r1, r2, r3
480 // bNE r1, r0, copy1MBB
481 // fallthrough --> copy0MBB
482 MachineBasicBlock *thisMBB = BB;
483 MachineFunction *F = BB->getParent();
484 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
485 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman14152b42010-07-06 20:24:04 +0000486 F->insert(It, copy0MBB);
487 F->insert(It, sinkMBB);
488
489 // Transfer the remainder of BB and its successor edges to sinkMBB.
490 sinkMBB->splice(sinkMBB->begin(), BB,
491 llvm::next(MachineBasicBlock::iterator(MI)),
492 BB->end());
493 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
494
495 // Next, add the true and fallthrough blocks as its successors.
496 BB->addSuccessor(copy0MBB);
497 BB->addSuccessor(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000498
499 // Emit the right instruction according to the type of the operands compared
500 if (isFPCmp) {
501 // Find the condiction code present in the setcc operation.
502 Mips::CondCode CC = (Mips::CondCode)MI->getOperand(4).getImm();
503 // Get the branch opcode from the branch code.
504 unsigned Opc = FPBranchCodeToOpc(GetFPBranchCodeFromCond(CC));
Dale Johannesen94817572009-02-13 02:34:39 +0000505 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000506 } else
Dale Johannesen94817572009-02-13 02:34:39 +0000507 BuildMI(BB, dl, TII->get(Mips::BNE)).addReg(MI->getOperand(1).getReg())
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000508 .addReg(Mips::ZERO).addMBB(sinkMBB);
509
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000510 // copy0MBB:
511 // %FalseValue = ...
512 // # fallthrough to sinkMBB
513 BB = copy0MBB;
514
515 // Update machine-CFG edges
516 BB->addSuccessor(sinkMBB);
517
518 // sinkMBB:
Bruno Cardoso Lopes29e9daa2010-07-20 07:58:51 +0000519 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000520 // ...
521 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +0000522 BuildMI(*BB, BB->begin(), dl,
523 TII->get(Mips::PHI), MI->getOperand(0).getReg())
Bruno Cardoso Lopes29e9daa2010-07-20 07:58:51 +0000524 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
525 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000526
Dan Gohman14152b42010-07-06 20:24:04 +0000527 MI->eraseFromParent(); // The pseudo instruction is gone now.
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000528 return BB;
529 }
530 }
531}
532
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000533//===----------------------------------------------------------------------===//
534// Misc Lower Operation implementation
535//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000536
Dan Gohman475871a2008-07-27 21:46:04 +0000537SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000538LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000539{
540 if (!Subtarget->isMips1())
541 return Op;
542
543 MachineFunction &MF = DAG.getMachineFunction();
544 unsigned CCReg = AddLiveIn(MF, Mips::FCR31, Mips::CCRRegisterClass);
545
546 SDValue Chain = DAG.getEntryNode();
547 DebugLoc dl = Op.getDebugLoc();
548 SDValue Src = Op.getOperand(0);
549
550 // Set the condition register
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 SDValue CondReg = DAG.getCopyFromReg(Chain, dl, CCReg, MVT::i32);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000552 CondReg = DAG.getCopyToReg(Chain, dl, Mips::AT, CondReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 CondReg = DAG.getCopyFromReg(CondReg, dl, Mips::AT, MVT::i32);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000554
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 SDValue Cst = DAG.getConstant(3, MVT::i32);
556 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, CondReg, Cst);
557 Cst = DAG.getConstant(2, MVT::i32);
558 SDValue Xor = DAG.getNode(ISD::XOR, dl, MVT::i32, Or, Cst);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000559
560 SDValue InFlag(0, 0);
561 CondReg = DAG.getCopyToReg(Chain, dl, Mips::FCR31, Xor, InFlag);
562
563 // Emit the round instruction and bit convert to integer
Owen Anderson825b72b2009-08-11 20:47:22 +0000564 SDValue Trunc = DAG.getNode(MipsISD::FPRound, dl, MVT::f32,
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000565 Src, CondReg.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000566 SDValue BitCvt = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Trunc);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000567 return BitCvt;
568}
569
570SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000571LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000572{
573 SDValue Chain = Op.getOperand(0);
574 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +0000575 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000576
577 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +0000578 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000579
580 // Subtract the dynamic size from the actual stack size to
581 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000583
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000584 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000585 // must be placed in the stack pointer register.
Dale Johannesena05dca42009-02-04 23:02:30 +0000586 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000587
588 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000589 // value and a chain
590 SDValue Ops[2] = { Sub, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000591 return DAG.getMergeValues(Ops, 2, dl);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000592}
593
594SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000595LowerANDOR(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000596{
597 SDValue LHS = Op.getOperand(0);
598 SDValue RHS = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +0000599 DebugLoc dl = Op.getDebugLoc();
600
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000601 if (LHS.getOpcode() != MipsISD::FPCmp || RHS.getOpcode() != MipsISD::FPCmp)
602 return Op;
603
Owen Anderson825b72b2009-08-11 20:47:22 +0000604 SDValue True = DAG.getConstant(1, MVT::i32);
605 SDValue False = DAG.getConstant(0, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000606
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000607 SDValue LSEL = DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000608 LHS, True, False, LHS.getOperand(2));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000609 SDValue RSEL = DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000610 RHS, True, False, RHS.getOperand(2));
611
Owen Anderson825b72b2009-08-11 20:47:22 +0000612 return DAG.getNode(Op.getOpcode(), dl, MVT::i32, LSEL, RSEL);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000613}
614
615SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000616LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000617{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000618 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000619 // the block to branch to if the condition is true.
620 SDValue Chain = Op.getOperand(0);
621 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000622 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000623
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000624 if (Op.getOperand(1).getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000625 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000626
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000627 SDValue CondRes = Op.getOperand(1);
628 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000629 Mips::CondCode CC =
630 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000631 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000632
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000633 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000634 Dest, CondRes);
635}
636
637SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000638LowerSETCC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000639{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000640 // The operands to this are the left and right operands to compare (ops #0,
641 // and #1) and the condition code to compare them with (op #2) as a
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000642 // CondCodeSDNode.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000643 SDValue LHS = Op.getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +0000644 SDValue RHS = Op.getOperand(1);
645 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000646
647 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000648
649 return DAG.getNode(MipsISD::FPCmp, dl, Op.getValueType(), LHS, RHS,
Owen Anderson825b72b2009-08-11 20:47:22 +0000650 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000651}
652
653SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000654LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000655{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000656 SDValue Cond = Op.getOperand(0);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000657 SDValue True = Op.getOperand(1);
658 SDValue False = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000659 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000660
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000661 // if the incomming condition comes from a integer compare, the select
662 // operation must be SelectCC or a conditional move if the subtarget
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000663 // supports it.
664 if (Cond.getOpcode() != MipsISD::FPCmp) {
665 if (Subtarget->hasCondMov() && !True.getValueType().isFloatingPoint())
666 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000667 return DAG.getNode(MipsISD::SelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000668 Cond, True, False);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000669 }
670
671 // if the incomming condition comes from fpcmp, the select
672 // operation must use FPSelectCC.
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000673 SDValue CCNode = Cond.getOperand(2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000674 return DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000675 Cond, True, False, CCNode);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000676}
677
Dan Gohmand858e902010-04-17 15:26:15 +0000678SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
679 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +0000680 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000681 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000682 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000683
Eli Friedmane2c74082009-08-03 02:22:28 +0000684 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +0000685 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000686
Chris Lattnerb71b9092009-08-13 06:28:06 +0000687 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000688
Chris Lattnere3736f82009-08-13 05:41:27 +0000689 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000690 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
691 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000692 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +0000693 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
694 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000695 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +0000696 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000697 // %hi/%lo relocation
Devang Patel0d881da2010-07-06 22:08:15 +0000698 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000699 MipsII::MO_ABS_HILO);
Chris Lattnere3736f82009-08-13 05:41:27 +0000700 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GA, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GA);
702 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000703
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000704 } else {
Devang Patel0d881da2010-07-06 22:08:15 +0000705 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000706 MipsII::MO_GOT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000707 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000708 DAG.getEntryNode(), GA, MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000709 false, false, 0);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000710 // On functions and global targets not internal linked only
711 // a load from got/GP is necessary for PIC to work.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000712 if (!GV->hasLocalLinkage() || isa<Function>(GV))
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000713 return ResNode;
Owen Anderson825b72b2009-08-11 20:47:22 +0000714 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GA);
715 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000716 }
717
Torok Edwinc23197a2009-07-14 16:55:14 +0000718 llvm_unreachable("Dont know how to handle GlobalAddress");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000719 return SDValue(0,0);
720}
721
722SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000723LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000724{
Torok Edwinc23197a2009-07-14 16:55:14 +0000725 llvm_unreachable("TLS not implemented for MIPS.");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000726 return SDValue(); // Not reached
727}
728
729SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000730LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000731{
Dan Gohman475871a2008-07-27 21:46:04 +0000732 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000733 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +0000734 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000735 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000736 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
737 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HILO;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000738
Owen Andersone50ed302009-08-10 22:56:29 +0000739 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000740 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000741
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000742 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
743
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +0000744 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +0000745 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000746 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000747 } else // Emit Load from Global Pointer
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000748 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
749 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000750 false, false, 0);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000751
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTI);
753 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000754
755 return ResNode;
756}
757
Dan Gohman475871a2008-07-27 21:46:04 +0000758SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000759LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000760{
Dan Gohman475871a2008-07-27 21:46:04 +0000761 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000762 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +0000763 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +0000764 // FIXME there isn't actually debug info here
765 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000766
767 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000768 // FIXME: we should reference the constant pool using small data sections,
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000769 // but the asm printer currently doens't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000770 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000771 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +0000772 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000773 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
774 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000775 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000776
777 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000778 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000779 N->getOffset(), MipsII::MO_ABS_HILO);
Owen Anderson825b72b2009-08-11 20:47:22 +0000780 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CP);
781 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CP);
782 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000783 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000784 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000785 N->getOffset(), MipsII::MO_GOT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000786 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000787 CP, MachinePointerInfo::getConstantPool(),
788 false, false, 0);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000789 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CP);
790 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
791 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000792
793 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000794}
795
Dan Gohmand858e902010-04-17 15:26:15 +0000796SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +0000797 MachineFunction &MF = DAG.getMachineFunction();
798 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
799
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000800 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +0000801 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
802 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000803
804 // vastart just stores the address of the VarArgsFrameIndex slot into the
805 // memory location argument.
806 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +0000807 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
808 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +0000809 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000810}
811
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000812//===----------------------------------------------------------------------===//
813// Calling Convention Implementation
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000814//===----------------------------------------------------------------------===//
815
816#include "MipsGenCallingConv.inc"
817
818//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000819// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000820// Mips O32 ABI rules:
821// ---
822// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000823// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000824// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000825// f64 - Only passed in two aliased f32 registers if no int reg has been used
826// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000827// not used, it must be shadowed. If only A3 is avaiable, shadow it and
828// go to stack.
829//===----------------------------------------------------------------------===//
830
Duncan Sands1e96bab2010-11-04 10:49:57 +0000831static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000832 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000833 ISD::ArgFlagsTy ArgFlags, CCState &State) {
834
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000835 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000836
837 static const unsigned IntRegs[] = {
838 Mips::A0, Mips::A1, Mips::A2, Mips::A3
839 };
840 static const unsigned F32Regs[] = {
841 Mips::F12, Mips::F14
842 };
843 static const unsigned F64Regs[] = {
844 Mips::D6, Mips::D7
845 };
846
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000847 unsigned Reg = 0;
848 static bool IntRegUsed = false;
849
850 // This must be the first arg of the call if no regs have been allocated.
851 // Initialize IntRegUsed in that case.
852 if (IntRegs[State.getFirstUnallocated(IntRegs, IntRegsSize)] == Mips::A0 &&
853 F32Regs[State.getFirstUnallocated(F32Regs, FloatRegsSize)] == Mips::F12 &&
854 F64Regs[State.getFirstUnallocated(F64Regs, FloatRegsSize)] == Mips::D6)
855 IntRegUsed = false;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000856
857 // Promote i8 and i16
Owen Anderson825b72b2009-08-11 20:47:22 +0000858 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
859 LocVT = MVT::i32;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000860 if (ArgFlags.isSExt())
861 LocInfo = CCValAssign::SExt;
862 else if (ArgFlags.isZExt())
863 LocInfo = CCValAssign::ZExt;
864 else
865 LocInfo = CCValAssign::AExt;
866 }
867
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000868 if (ValVT == MVT::i32) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000869 Reg = State.AllocateReg(IntRegs, IntRegsSize);
870 IntRegUsed = true;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000871 } else if (ValVT == MVT::f32) {
872 // An int reg has to be marked allocated regardless of whether or not
873 // IntRegUsed is true.
874 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000875
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000876 if (IntRegUsed) {
877 if (Reg) // Int reg is available
878 LocVT = MVT::i32;
879 } else {
880 unsigned FReg = State.AllocateReg(F32Regs, FloatRegsSize);
881 if (FReg) // F32 reg is available
882 Reg = FReg;
883 else if (Reg) // No F32 regs are available, but an int reg is available.
884 LocVT = MVT::i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000885 }
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000886 } else if (ValVT == MVT::f64) {
887 // Int regs have to be marked allocated regardless of whether or not
888 // IntRegUsed is true.
889 Reg = State.AllocateReg(IntRegs, IntRegsSize);
890 if (Reg == Mips::A1)
891 Reg = State.AllocateReg(IntRegs, IntRegsSize);
892 else if (Reg == Mips::A3)
893 Reg = 0;
894 State.AllocateReg(IntRegs, IntRegsSize);
895
896 // At this point, Reg is A0, A2 or 0, and all the unavailable integer regs
897 // are marked as allocated.
898 if (IntRegUsed) {
899 if (Reg)// if int reg is available
900 LocVT = MVT::i32;
901 } else {
902 unsigned FReg = State.AllocateReg(F64Regs, FloatRegsSize);
903 if (FReg) // F64 reg is available.
904 Reg = FReg;
905 else if (Reg) // No F64 regs are available, but an int reg is available.
906 LocVT = MVT::i32;
907 }
908 } else
909 assert(false && "cannot handle this ValVT");
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000910
911 if (!Reg) {
912 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
913 unsigned Offset = State.AllocateStack(SizeInBytes, SizeInBytes);
914 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
915 } else
916 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
917
918 return false; // CC must always match
919}
920
Duncan Sands1e96bab2010-11-04 10:49:57 +0000921static bool CC_MipsO32_VarArgs(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000922 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +0000923 ISD::ArgFlagsTy ArgFlags, CCState &State) {
924
925 static const unsigned IntRegsSize=4;
926
927 static const unsigned IntRegs[] = {
928 Mips::A0, Mips::A1, Mips::A2, Mips::A3
929 };
930
931 // Promote i8 and i16
932 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
933 LocVT = MVT::i32;
934 if (ArgFlags.isSExt())
935 LocInfo = CCValAssign::SExt;
936 else if (ArgFlags.isZExt())
937 LocInfo = CCValAssign::ZExt;
938 else
939 LocInfo = CCValAssign::AExt;
940 }
941
942 if (ValVT == MVT::i32 || ValVT == MVT::f32) {
943 if (unsigned Reg = State.AllocateReg(IntRegs, IntRegsSize)) {
944 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, MVT::i32, LocInfo));
945 return false;
946 }
947 unsigned Off = State.AllocateStack(4, 4);
948 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Off, LocVT, LocInfo));
949 return false;
950 }
951
952 unsigned UnallocIntReg = State.getFirstUnallocated(IntRegs, IntRegsSize);
953 if (ValVT == MVT::f64) {
954 if (IntRegs[UnallocIntReg] == (unsigned (Mips::A1))) {
955 // A1 can't be used anymore, because 64 bit arguments
956 // must be aligned when copied back to the caller stack
957 State.AllocateReg(IntRegs, IntRegsSize);
958 UnallocIntReg++;
959 }
960
961 if (IntRegs[UnallocIntReg] == (unsigned (Mips::A0)) ||
962 IntRegs[UnallocIntReg] == (unsigned (Mips::A2))) {
963 unsigned Reg = State.AllocateReg(IntRegs, IntRegsSize);
964 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, MVT::i32, LocInfo));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000965 // Shadow the next register so it can be used
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +0000966 // later to get the other 32bit part.
967 State.AllocateReg(IntRegs, IntRegsSize);
968 return false;
969 }
970
971 // Register is shadowed to preserve alignment, and the
972 // argument goes to a stack location.
973 if (UnallocIntReg != IntRegsSize)
974 State.AllocateReg(IntRegs, IntRegsSize);
975
976 unsigned Off = State.AllocateStack(8, 8);
977 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Off, LocVT, LocInfo));
978 return false;
979 }
980
981 return true; // CC didn't match
982}
983
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000984//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +0000985// Call Calling Convention Implementation
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000986//===----------------------------------------------------------------------===//
987
Dan Gohman98ca4f22009-08-05 01:29:28 +0000988/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +0000989/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +0000990/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000991SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000992MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000993 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000994 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000995 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000996 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000997 const SmallVectorImpl<ISD::InputArg> &Ins,
998 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000999 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001000 // MIPs target does not yet support tail call optimization.
1001 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001002
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001003 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001004 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001005 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001006
1007 // Analyze operands of the call, assigning locations to each operand.
1008 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001009 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1010 *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001011
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +00001012 // To meet O32 ABI, Mips must always allocate 16 bytes on
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001013 // the stack (even if less than 4 are used as arguments)
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +00001014 if (Subtarget->isABI_O32()) {
Duncan Sands1e96bab2010-11-04 10:49:57 +00001015 int VTsize = MVT(MVT::i32).getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +00001016 MFI->CreateFixedObject(VTsize, (VTsize*3), true);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001017 CCInfo.AnalyzeCallOperands(Outs,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001018 isVarArg ? CC_MipsO32_VarArgs : CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001019 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001020 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001021
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001022 // Get a count of how many bytes are to be pushed on the stack.
1023 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnere563bbc2008-10-11 22:08:30 +00001024 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001025
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001026 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001027 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1028 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001029
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001030 // First/LastArgStackLoc contains the first/last
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001031 // "at stack" argument location.
1032 int LastArgStackLoc = 0;
1033 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001034
1035 // Walk the register/memloc assignments, inserting copies/loads.
1036 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001037 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001038 CCValAssign &VA = ArgLocs[i];
1039
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001040 // Promote the value if needed.
1041 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001042 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001043 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001044 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001045 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001046 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001047 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001048 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001049 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001050 DAG.getConstant(0, getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00001051 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001052 DAG.getConstant(1, getPointerTy()));
1053 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
1054 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
1055 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001056 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001057 }
1058 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00001059 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001060 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001061 break;
1062 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001063 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001064 break;
1065 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001066 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001067 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001068 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001069
1070 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001071 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001072 if (VA.isRegLoc()) {
1073 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00001074 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001075 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001076
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001077 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00001078 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001079
Chris Lattnere0b12152008-03-17 06:57:02 +00001080 // Create the frame index object for this incoming parameter
1081 // This guarantees that when allocating Local Area the firsts
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001082 // 16 bytes which are alwayes reserved won't be overwritten
1083 // if O32 ABI is used. For EABI the first address is zero.
1084 LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001085 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001086 LastArgStackLoc, true);
Chris Lattnere0b12152008-03-17 06:57:02 +00001087
Dan Gohman475871a2008-07-27 21:46:04 +00001088 SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00001089
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001090 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00001091 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00001092 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
1093 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001094 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001095 }
1096
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001097 // Transform all store nodes into one single node because all store
1098 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001099 if (!MemOpChains.empty())
1100 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001101 &MemOpChains[0], MemOpChains.size());
1102
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001103 // Build a sequence of copy-to-reg nodes chained together with token
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001104 // chain and flag operands which copy the outgoing args into registers.
1105 // The InFlag in necessary since all emited instructions must be
1106 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +00001107 SDValue InFlag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001108 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001109 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001110 RegsToPass[i].second, InFlag);
1111 InFlag = Chain.getValue(1);
1112 }
1113
Bill Wendling056292f2008-09-16 21:48:12 +00001114 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001115 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1116 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001117 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001118 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1119 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001120 getPointerTy(), 0, OpFlag);
Bill Wendling056292f2008-09-16 21:48:12 +00001121 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001122 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001123 getPointerTy(), OpFlag);
Bill Wendling056292f2008-09-16 21:48:12 +00001124
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001125 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001126 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001127 //
1128 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001129 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00001130 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001131 Ops.push_back(Chain);
1132 Ops.push_back(Callee);
1133
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001134 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001135 // known live into the call.
1136 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1137 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1138 RegsToPass[i].second.getValueType()));
1139
Gabor Greifba36cb52008-08-28 21:40:38 +00001140 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001141 Ops.push_back(InFlag);
1142
Dale Johannesen33c960f2009-02-04 20:06:27 +00001143 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001144 InFlag = Chain.getValue(1);
1145
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001146 // Create a stack location to hold GP when PIC is used. This stack
1147 // location is used on function prologue to save GP and also after all
1148 // emited CALL's to restore GP.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001149 if (IsPIC) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001150 // Function can have an arbitrary number of calls, so
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001151 // hold the LastArgStackLoc with the biggest offset.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001152 int FI;
1153 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001154 if (LastArgStackLoc >= MipsFI->getGPStackOffset()) {
1155 LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001156 // Create the frame index only once. SPOffset here can be anything
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001157 // (this will be fixed on processFunctionBeforeFrameFinalized)
1158 if (MipsFI->getGPStackOffset() == -1) {
Evan Chenged2ae132010-07-03 00:40:23 +00001159 FI = MFI->CreateFixedObject(4, 0, true);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001160 MipsFI->setGPFI(FI);
1161 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001162 MipsFI->setGPStackOffset(LastArgStackLoc);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001163 }
1164
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001165 // Reload GP value.
1166 FI = MipsFI->getGPFI();
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001167 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1168 SDValue GPLoad = DAG.getLoad(MVT::i32, dl, Chain, FIN,
1169 MachinePointerInfo::getFixedStack(FI),
David Greenef6fa1862010-02-15 16:56:10 +00001170 false, false, 0);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001171 Chain = GPLoad.getValue(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001172 Chain = DAG.getCopyToReg(Chain, dl, DAG.getRegister(Mips::GP, MVT::i32),
Dan Gohman475871a2008-07-27 21:46:04 +00001173 GPLoad, SDValue(0,0));
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +00001174 InFlag = Chain.getValue(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001175 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001176
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00001177 // Create the CALLSEQ_END node.
1178 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1179 DAG.getIntPtrConstant(0, true), InFlag);
1180 InFlag = Chain.getValue(1);
1181
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001182 // Handle result values, copying them out of physregs into vregs that we
1183 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001184 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
1185 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001186}
1187
Dan Gohman98ca4f22009-08-05 01:29:28 +00001188/// LowerCallResult - Lower the result values of a call into the
1189/// appropriate copies out of appropriate physical registers.
1190SDValue
1191MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001192 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001193 const SmallVectorImpl<ISD::InputArg> &Ins,
1194 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001195 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001196
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001197 // Assign locations to each value returned by this call.
1198 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001199 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001200 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001201
Dan Gohman98ca4f22009-08-05 01:29:28 +00001202 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001203
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001204 // Copy all of the result registers out of their specified physreg.
1205 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001206 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001207 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001208 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001209 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001210 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001211
Dan Gohman98ca4f22009-08-05 01:29:28 +00001212 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001213}
1214
1215//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001216// Formal Arguments Calling Convention Implementation
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001217//===----------------------------------------------------------------------===//
1218
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001219/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001220/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001221SDValue
1222MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001223 CallingConv::ID CallConv, bool isVarArg,
1224 const SmallVectorImpl<ISD::InputArg>
1225 &Ins,
1226 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001227 SmallVectorImpl<SDValue> &InVals)
1228 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001229
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00001230 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001231 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001232 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001233
1234 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Dan Gohman1e93df62010-04-17 14:41:14 +00001235 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001236
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001237 // Used with vargs to acumulate store chains.
1238 std::vector<SDValue> OutChains;
1239
1240 // Keep track of the last register used for arguments
1241 unsigned ArgRegEnd = 0;
1242
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001243 // Assign locations to all of the incoming arguments.
1244 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001245 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1246 ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001247
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001248 if (Subtarget->isABI_O32())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001249 CCInfo.AnalyzeFormalArguments(Ins,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001250 isVarArg ? CC_MipsO32_VarArgs : CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001251 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001252 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001253
Dan Gohman475871a2008-07-27 21:46:04 +00001254 SDValue StackPtr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001255
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001256 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
1257
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001258 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001259 CCValAssign &VA = ArgLocs[i];
1260
1261 // Arguments stored on registers
1262 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001263 EVT RegVT = VA.getLocVT();
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001264 ArgRegEnd = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00001265 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001266
Owen Anderson825b72b2009-08-11 20:47:22 +00001267 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001268 RC = Mips::CPURegsRegisterClass;
1269 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001270 RC = Mips::FGR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001271 else if (RegVT == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001272 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001273 RC = Mips::AFGR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001274 } else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001275 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001276
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001277 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001278 // physical registers into virtual ones
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001279 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegEnd, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001280 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001281
1282 // If this is an 8 or 16-bit value, it has been passed promoted
1283 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001284 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001285 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00001286 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001287 if (VA.getLocInfo() == CCValAssign::SExt)
1288 Opcode = ISD::AssertSext;
1289 else if (VA.getLocInfo() == CCValAssign::ZExt)
1290 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00001291 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001292 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00001293 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00001294 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001295 }
1296
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001297 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001298 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001299 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
1300 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00001301 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001302 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001303 VA.getLocReg()+1, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001304 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Bruno Cardoso Lopesb1fce0a2011-01-18 19:38:25 +00001305 SDValue Pair = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, ArgValue2, ArgValue);
1306 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Pair);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001307 }
1308 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001309
Dan Gohman98ca4f22009-08-05 01:29:28 +00001310 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001311 } else { // VA.isRegLoc()
1312
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001313 // sanity check
1314 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001315
1316 // The last argument is not a register anymore
1317 ArgRegEnd = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001318
1319 // The stack pointer offset is relative to the caller stack frame.
1320 // Since the real stack size is unknown here, a negative SPOffset
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001321 // is used so there's a way to adjust these offsets when the stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001322 // size get known (on EliminateFrameIndex). A dummy SPOffset is
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001323 // used instead of a direct negative address (which is recorded to
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001324 // be used on emitPrologue) to avoid mis-calc of the first stack
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001325 // offset on PEI::calculateFrameObjectOffsets.
1326 // Arguments are always 32-bit.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001327 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +00001328 int FI = MFI->CreateFixedObject(ArgSize, 0, true);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001329 MipsFI->recordLoadArgsFI(FI, -(ArgSize+
1330 (FirstStackArgLoc + VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001331
1332 // Create load nodes to retrieve arguments from the stack
Dan Gohman475871a2008-07-27 21:46:04 +00001333 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001334 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
1335 MachinePointerInfo::getFixedStack(FI),
David Greenef6fa1862010-02-15 16:56:10 +00001336 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001337 }
1338 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001339
1340 // The mips ABIs for returning structs by value requires that we copy
1341 // the sret argument into $v0 for the return. Save the argument into
1342 // a virtual register so that we can access it from the return points.
1343 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1344 unsigned Reg = MipsFI->getSRetReturnReg();
1345 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001346 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001347 MipsFI->setSRetReturnReg(Reg);
1348 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001349 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001350 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001351 }
1352
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001353 // To meet ABI, when VARARGS are passed on registers, the registers
1354 // must have their values written to the caller stack frame. If the last
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001355 // argument was placed in the stack, there's no need to save any register.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001356 if ((isVarArg) && (Subtarget->isABI_O32() && ArgRegEnd)) {
1357 if (StackPtr.getNode() == 0)
1358 StackPtr = DAG.getRegister(StackReg, getPointerTy());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001359
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001360 // The last register argument that must be saved is Mips::A3
1361 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
1362 unsigned StackLoc = ArgLocs.size()-1;
1363
1364 for (++ArgRegEnd; ArgRegEnd <= Mips::A3; ++ArgRegEnd, ++StackLoc) {
1365 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegEnd, RC);
1366 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
1367
Evan Chenged2ae132010-07-03 00:40:23 +00001368 int FI = MFI->CreateFixedObject(4, 0, true);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001369 MipsFI->recordStoreVarArgsFI(FI, -(4+(StackLoc*4)));
1370 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00001371 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
1372 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001373 false, false, 0));
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001374
1375 // Record the frame index of the first variable argument
1376 // which is a value necessary to VASTART.
Dan Gohman1e93df62010-04-17 14:41:14 +00001377 if (!MipsFI->getVarArgsFrameIndex())
1378 MipsFI->setVarArgsFrameIndex(FI);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001379 }
1380 }
1381
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001382 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001383 // the size of Ins and InVals. This only happens when on varg functions
1384 if (!OutChains.empty()) {
1385 OutChains.push_back(Chain);
1386 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1387 &OutChains[0], OutChains.size());
1388 }
1389
Dan Gohman98ca4f22009-08-05 01:29:28 +00001390 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001391}
1392
1393//===----------------------------------------------------------------------===//
1394// Return Value Calling Convention Implementation
1395//===----------------------------------------------------------------------===//
1396
Dan Gohman98ca4f22009-08-05 01:29:28 +00001397SDValue
1398MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001399 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001400 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001401 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001402 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001403
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001404 // CCValAssign - represent the assignment of
1405 // the return value to a location
1406 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001407
1408 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001409 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1410 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001411
Dan Gohman98ca4f22009-08-05 01:29:28 +00001412 // Analize return values.
1413 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001414
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001415 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001416 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001417 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001418 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001419 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001420 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001421 }
1422
Dan Gohman475871a2008-07-27 21:46:04 +00001423 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001424
1425 // Copy the result values into the output registers.
1426 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1427 CCValAssign &VA = RVLocs[i];
1428 assert(VA.isRegLoc() && "Can only return in registers!");
1429
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001430 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001431 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001432
1433 // guarantee that all emitted copies are
1434 // stuck together, avoiding something bad
1435 Flag = Chain.getValue(1);
1436 }
1437
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001438 // The mips ABIs for returning structs by value requires that we copy
1439 // the sret argument into $v0 for the return. We saved the argument into
1440 // a virtual register in the entry block, so now we copy the value out
1441 // and into $v0.
1442 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1443 MachineFunction &MF = DAG.getMachineFunction();
1444 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1445 unsigned Reg = MipsFI->getSRetReturnReg();
1446
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001447 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00001448 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00001449 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001450
Dale Johannesena05dca42009-02-04 23:02:30 +00001451 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001452 Flag = Chain.getValue(1);
1453 }
1454
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001455 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00001456 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001457 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001458 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001459 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001460 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001461 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001462}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001463
1464//===----------------------------------------------------------------------===//
1465// Mips Inline Assembly Support
1466//===----------------------------------------------------------------------===//
1467
1468/// getConstraintType - Given a constraint letter, return the type of
1469/// constraint it is for this target.
1470MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001471getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001472{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001473 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001474 // GCC config/mips/constraints.md
1475 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001476 // 'd' : An address register. Equivalent to r
1477 // unless generating MIPS16 code.
1478 // 'y' : Equivalent to r; retained for
1479 // backwards compatibility.
1480 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001481 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001482 switch (Constraint[0]) {
1483 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001484 case 'd':
1485 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001486 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001487 return C_RegisterClass;
1488 break;
1489 }
1490 }
1491 return TargetLowering::getConstraintType(Constraint);
1492}
1493
John Thompson44ab89e2010-10-29 17:29:13 +00001494/// Examine constraint type and operand type and determine a weight value.
1495/// This object must already have been set up with the operand type
1496/// and the current alternative constraint selected.
1497TargetLowering::ConstraintWeight
1498MipsTargetLowering::getSingleConstraintMatchWeight(
1499 AsmOperandInfo &info, const char *constraint) const {
1500 ConstraintWeight weight = CW_Invalid;
1501 Value *CallOperandVal = info.CallOperandVal;
1502 // If we don't have a value, we can't do a match,
1503 // but allow it at the lowest weight.
1504 if (CallOperandVal == NULL)
1505 return CW_Default;
1506 const Type *type = CallOperandVal->getType();
1507 // Look at the constraint type.
1508 switch (*constraint) {
1509 default:
1510 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
1511 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001512 case 'd':
1513 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00001514 if (type->isIntegerTy())
1515 weight = CW_Register;
1516 break;
1517 case 'f':
1518 if (type->isFloatTy())
1519 weight = CW_Register;
1520 break;
1521 }
1522 return weight;
1523}
1524
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001525/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1526/// return a list of registers that can be used to satisfy the constraint.
1527/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001528std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00001529getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001530{
1531 if (Constraint.size() == 1) {
1532 switch (Constraint[0]) {
1533 case 'r':
1534 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001535 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001536 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001537 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001538 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001539 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1540 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001541 }
1542 }
1543 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1544}
1545
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001546/// Given a register class constraint, like 'r', if this corresponds directly
1547/// to an LLVM register class, return a register of 0 and the register class
1548/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001549std::vector<unsigned> MipsTargetLowering::
1550getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001551 EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001552{
1553 if (Constraint.size() != 1)
1554 return std::vector<unsigned>();
1555
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001556 switch (Constraint[0]) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001557 default : break;
1558 case 'r':
1559 // GCC Mips Constraint Letters
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001560 case 'd':
1561 case 'y':
1562 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
1563 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
1564 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001565 Mips::T8, 0);
1566
1567 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001568 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001569 if (Subtarget->isSingleFloat())
1570 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1571 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1572 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1573 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1574 Mips::F30, Mips::F31, 0);
1575 else
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001576 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
1577 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001578 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +00001579 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001580
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001581 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001582 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001583 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
1584 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001585 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001586 }
1587 return std::vector<unsigned>();
1588}
Dan Gohman6520e202008-10-18 02:06:02 +00001589
1590bool
1591MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1592 // The Mips target isn't yet aware of offsets.
1593 return false;
1594}
Evan Chengeb2f9692009-10-27 19:56:55 +00001595
Evan Chenga1eaa3c2009-10-28 01:43:28 +00001596bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
1597 if (VT != MVT::f32 && VT != MVT::f64)
1598 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00001599 if (Imm.isNegZero())
1600 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00001601 return Imm.isZero();
1602}