blob: d9775973a0629c57275501c9aa36674c7ac1341f [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);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +000092 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000093 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
94 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
95 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
96 setOperationAction(ISD::SELECT, MVT::f32, Custom);
97 setOperationAction(ISD::SELECT, MVT::f64, Custom);
98 setOperationAction(ISD::SELECT, MVT::i32, Custom);
99 setOperationAction(ISD::SETCC, MVT::f32, Custom);
100 setOperationAction(ISD::SETCC, MVT::f64, Custom);
101 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
102 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
103 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000104 setOperationAction(ISD::VASTART, MVT::Other, Custom);
105
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000106
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000107 // We custom lower AND/OR to handle the case where the DAG contain 'ands/ors'
108 // with operands comming from setcc fp comparions. This is necessary since
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +0000109 // the result from these setcc are in a flag registers (FCR31).
Owen Anderson825b72b2009-08-11 20:47:22 +0000110 setOperationAction(ISD::AND, MVT::i32, Custom);
111 setOperationAction(ISD::OR, MVT::i32, Custom);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000112
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000113 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000114 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
115 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
116 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
117 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
118 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
120 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
121 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
122 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000123
124 if (!Subtarget->isMips32r2())
125 setOperationAction(ISD::ROTR, MVT::i32, Expand);
126
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
128 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
129 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
130 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
131 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
132 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000133 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000135 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
137 setOperationAction(ISD::FPOW, MVT::f32, Expand);
138 setOperationAction(ISD::FLOG, MVT::f32, Expand);
139 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
140 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
141 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000142
Owen Anderson825b72b2009-08-11 20:47:22 +0000143 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000144
145 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000146 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
147 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
148 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000149
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000150 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000152
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000153 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
155 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000156 }
157
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000158 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000160
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000161 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000162 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000163
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000164 setTargetDAGCombine(ISD::ADDE);
165 setTargetDAGCombine(ISD::SUBE);
166
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000167 setStackPointerRegisterToSaveRestore(Mips::SP);
168 computeRegisterProperties();
169}
170
Owen Anderson825b72b2009-08-11 20:47:22 +0000171MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
172 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000173}
174
Bill Wendlingb4202b82009-07-01 18:50:55 +0000175/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000176unsigned MipsTargetLowering::getFunctionAlignment(const Function *) const {
177 return 2;
178}
Scott Michel5b8f82e2008-03-10 15:42:14 +0000179
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000180// SelectMadd -
181// Transforms a subgraph in CurDAG if the following pattern is found:
182// (addc multLo, Lo0), (adde multHi, Hi0),
183// where,
184// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000185// Lo0: initial value of Lo register
186// Hi0: initial value of Hi register
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000187// Return true if mattern matching was successful.
188static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000189 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000190 // for the matching to be successful.
191 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
192
193 if (ADDCNode->getOpcode() != ISD::ADDC)
194 return false;
195
196 SDValue MultHi = ADDENode->getOperand(0);
197 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000198 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000199 unsigned MultOpc = MultHi.getOpcode();
200
201 // MultHi and MultLo must be generated by the same node,
202 if (MultLo.getNode() != MultNode)
203 return false;
204
205 // and it must be a multiplication.
206 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
207 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000208
209 // MultLo amd MultHi must be the first and second output of MultNode
210 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000211 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
212 return false;
213
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000214 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000215 // of the values of MultNode, in which case MultNode will be removed in later
216 // phases.
217 // If there exist users other than ADDENode or ADDCNode, this function returns
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000218 // here, which will result in MultNode being mapped to a single MULT
219 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000220 // produced.
221 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
222 return false;
223
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000224 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000225 DebugLoc dl = ADDENode->getDebugLoc();
226
227 // create MipsMAdd(u) node
228 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000229
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000230 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
231 MVT::Glue,
232 MultNode->getOperand(0),// Factor 0
233 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000234 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000235 ADDENode->getOperand(1));// Hi0
236
237 // create CopyFromReg nodes
238 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
239 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000240 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000241 Mips::HI, MVT::i32,
242 CopyFromLo.getValue(2));
243
244 // replace uses of adde and addc here
245 if (!SDValue(ADDCNode, 0).use_empty())
246 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
247
248 if (!SDValue(ADDENode, 0).use_empty())
249 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
250
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000251 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000252}
253
254// SelectMsub -
255// Transforms a subgraph in CurDAG if the following pattern is found:
256// (addc Lo0, multLo), (sube Hi0, multHi),
257// where,
258// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000259// Lo0: initial value of Lo register
260// Hi0: initial value of Hi register
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000261// Return true if mattern matching was successful.
262static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000263 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000264 // for the matching to be successful.
265 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
266
267 if (SUBCNode->getOpcode() != ISD::SUBC)
268 return false;
269
270 SDValue MultHi = SUBENode->getOperand(1);
271 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000272 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000273 unsigned MultOpc = MultHi.getOpcode();
274
275 // MultHi and MultLo must be generated by the same node,
276 if (MultLo.getNode() != MultNode)
277 return false;
278
279 // and it must be a multiplication.
280 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
281 return false;
282
283 // MultLo amd MultHi must be the first and second output of MultNode
284 // respectively.
285 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
286 return false;
287
288 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
289 // of the values of MultNode, in which case MultNode will be removed in later
290 // phases.
291 // If there exist users other than SUBENode or SUBCNode, this function returns
292 // here, which will result in MultNode being mapped to a single MULT
293 // instruction node rather than a pair of MULT and MSUB instructions being
294 // produced.
295 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
296 return false;
297
298 SDValue Chain = CurDAG->getEntryNode();
299 DebugLoc dl = SUBENode->getDebugLoc();
300
301 // create MipsSub(u) node
302 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
303
304 SDValue MSub = CurDAG->getNode(MultOpc, dl,
305 MVT::Glue,
306 MultNode->getOperand(0),// Factor 0
307 MultNode->getOperand(1),// Factor 1
308 SUBCNode->getOperand(0),// Lo0
309 SUBENode->getOperand(0));// Hi0
310
311 // create CopyFromReg nodes
312 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
313 MSub);
314 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
315 Mips::HI, MVT::i32,
316 CopyFromLo.getValue(2));
317
318 // replace uses of sube and subc here
319 if (!SDValue(SUBCNode, 0).use_empty())
320 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
321
322 if (!SDValue(SUBENode, 0).use_empty())
323 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
324
325 return true;
326}
327
328static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
329 TargetLowering::DAGCombinerInfo &DCI,
330 const MipsSubtarget* Subtarget) {
331 if (DCI.isBeforeLegalize())
332 return SDValue();
333
334 if (Subtarget->isMips32() && SelectMadd(N, &DAG))
335 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000336
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000337 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000338}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000339
340static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
341 TargetLowering::DAGCombinerInfo &DCI,
342 const MipsSubtarget* Subtarget) {
343 if (DCI.isBeforeLegalize())
344 return SDValue();
345
346 if (Subtarget->isMips32() && SelectMsub(N, &DAG))
347 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000348
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000349 return SDValue();
350}
351
352SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000353 const {
354 SelectionDAG &DAG = DCI.DAG;
355 unsigned opc = N->getOpcode();
356
357 switch (opc) {
358 default: break;
359 case ISD::ADDE:
360 return PerformADDECombine(N, DAG, DCI, Subtarget);
361 case ISD::SUBE:
362 return PerformSUBECombine(N, DAG, DCI, Subtarget);
363 }
364
365 return SDValue();
366}
367
Dan Gohman475871a2008-07-27 21:46:04 +0000368SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000369LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000370{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000371 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000372 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000373 case ISD::AND: return LowerANDOR(Op, DAG);
374 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000375 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
376 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000377 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000378 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000379 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000380 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
381 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
382 case ISD::OR: return LowerANDOR(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000383 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000384 case ISD::SETCC: return LowerSETCC(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000385 case ISD::VASTART: return LowerVASTART(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000386 }
Dan Gohman475871a2008-07-27 21:46:04 +0000387 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000388}
389
390//===----------------------------------------------------------------------===//
391// Lower helper functions
392//===----------------------------------------------------------------------===//
393
394// AddLiveIn - This helper function adds the specified physical register to the
395// MachineFunction as a live in value. It also creates a corresponding
396// virtual register for it.
397static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000398AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000399{
400 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000401 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
402 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000403 return VReg;
404}
405
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000406// Get fp branch code (not opcode) from condition code.
407static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
408 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
409 return Mips::BRANCH_T;
410
411 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
412 return Mips::BRANCH_F;
413
414 return Mips::BRANCH_INVALID;
415}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000416
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000417static unsigned FPBranchCodeToOpc(Mips::FPBranchCode BC) {
418 switch(BC) {
419 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000420 llvm_unreachable("Unknown branch code");
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000421 case Mips::BRANCH_T : return Mips::BC1T;
422 case Mips::BRANCH_F : return Mips::BC1F;
423 case Mips::BRANCH_TL : return Mips::BC1TL;
424 case Mips::BRANCH_FL : return Mips::BC1FL;
425 }
426}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000427
428static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
429 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000430 default: llvm_unreachable("Unknown fp condition code!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000431 case ISD::SETEQ:
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000432 case ISD::SETOEQ: return Mips::FCOND_EQ;
433 case ISD::SETUNE: return Mips::FCOND_OGL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000434 case ISD::SETLT:
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000435 case ISD::SETOLT: return Mips::FCOND_OLT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000436 case ISD::SETGT:
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000437 case ISD::SETOGT: return Mips::FCOND_OGT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000438 case ISD::SETLE:
439 case ISD::SETOLE: return Mips::FCOND_OLE;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000440 case ISD::SETGE:
441 case ISD::SETOGE: return Mips::FCOND_OGE;
442 case ISD::SETULT: return Mips::FCOND_ULT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000443 case ISD::SETULE: return Mips::FCOND_ULE;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000444 case ISD::SETUGT: return Mips::FCOND_UGT;
445 case ISD::SETUGE: return Mips::FCOND_UGE;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000446 case ISD::SETUO: return Mips::FCOND_UN;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000447 case ISD::SETO: return Mips::FCOND_OR;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000448 case ISD::SETNE:
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000449 case ISD::SETONE: return Mips::FCOND_NEQ;
450 case ISD::SETUEQ: return Mips::FCOND_UEQ;
451 }
452}
453
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000454MachineBasicBlock *
455MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000456 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000457 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
458 bool isFPCmp = false;
Dale Johannesen94817572009-02-13 02:34:39 +0000459 DebugLoc dl = MI->getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000460
461 switch (MI->getOpcode()) {
462 default: assert(false && "Unexpected instr type to insert");
463 case Mips::Select_FCC:
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000464 case Mips::Select_FCC_S32:
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000465 case Mips::Select_FCC_D32:
466 isFPCmp = true; // FALL THROUGH
467 case Mips::Select_CC:
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000468 case Mips::Select_CC_S32:
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000469 case Mips::Select_CC_D32: {
470 // To "insert" a SELECT_CC instruction, we actually have to insert the
471 // diamond control-flow pattern. The incoming instruction knows the
472 // destination vreg to set, the condition code register to branch on, the
473 // true/false values to select between, and a branch opcode to use.
474 const BasicBlock *LLVM_BB = BB->getBasicBlock();
475 MachineFunction::iterator It = BB;
476 ++It;
477
478 // thisMBB:
479 // ...
480 // TrueVal = ...
481 // setcc r1, r2, r3
482 // bNE r1, r0, copy1MBB
483 // fallthrough --> copy0MBB
484 MachineBasicBlock *thisMBB = BB;
485 MachineFunction *F = BB->getParent();
486 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
487 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman14152b42010-07-06 20:24:04 +0000488 F->insert(It, copy0MBB);
489 F->insert(It, sinkMBB);
490
491 // Transfer the remainder of BB and its successor edges to sinkMBB.
492 sinkMBB->splice(sinkMBB->begin(), BB,
493 llvm::next(MachineBasicBlock::iterator(MI)),
494 BB->end());
495 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
496
497 // Next, add the true and fallthrough blocks as its successors.
498 BB->addSuccessor(copy0MBB);
499 BB->addSuccessor(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000500
501 // Emit the right instruction according to the type of the operands compared
502 if (isFPCmp) {
503 // Find the condiction code present in the setcc operation.
504 Mips::CondCode CC = (Mips::CondCode)MI->getOperand(4).getImm();
505 // Get the branch opcode from the branch code.
506 unsigned Opc = FPBranchCodeToOpc(GetFPBranchCodeFromCond(CC));
Dale Johannesen94817572009-02-13 02:34:39 +0000507 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000508 } else
Dale Johannesen94817572009-02-13 02:34:39 +0000509 BuildMI(BB, dl, TII->get(Mips::BNE)).addReg(MI->getOperand(1).getReg())
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000510 .addReg(Mips::ZERO).addMBB(sinkMBB);
511
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000512 // copy0MBB:
513 // %FalseValue = ...
514 // # fallthrough to sinkMBB
515 BB = copy0MBB;
516
517 // Update machine-CFG edges
518 BB->addSuccessor(sinkMBB);
519
520 // sinkMBB:
Bruno Cardoso Lopes29e9daa2010-07-20 07:58:51 +0000521 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000522 // ...
523 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +0000524 BuildMI(*BB, BB->begin(), dl,
525 TII->get(Mips::PHI), MI->getOperand(0).getReg())
Bruno Cardoso Lopes29e9daa2010-07-20 07:58:51 +0000526 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
527 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000528
Dan Gohman14152b42010-07-06 20:24:04 +0000529 MI->eraseFromParent(); // The pseudo instruction is gone now.
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000530 return BB;
531 }
532 }
533}
534
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000535//===----------------------------------------------------------------------===//
536// Misc Lower Operation implementation
537//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000538
Dan Gohman475871a2008-07-27 21:46:04 +0000539SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000540LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000541{
542 if (!Subtarget->isMips1())
543 return Op;
544
545 MachineFunction &MF = DAG.getMachineFunction();
546 unsigned CCReg = AddLiveIn(MF, Mips::FCR31, Mips::CCRRegisterClass);
547
548 SDValue Chain = DAG.getEntryNode();
549 DebugLoc dl = Op.getDebugLoc();
550 SDValue Src = Op.getOperand(0);
551
552 // Set the condition register
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 SDValue CondReg = DAG.getCopyFromReg(Chain, dl, CCReg, MVT::i32);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000554 CondReg = DAG.getCopyToReg(Chain, dl, Mips::AT, CondReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 CondReg = DAG.getCopyFromReg(CondReg, dl, Mips::AT, MVT::i32);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000556
Owen Anderson825b72b2009-08-11 20:47:22 +0000557 SDValue Cst = DAG.getConstant(3, MVT::i32);
558 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, CondReg, Cst);
559 Cst = DAG.getConstant(2, MVT::i32);
560 SDValue Xor = DAG.getNode(ISD::XOR, dl, MVT::i32, Or, Cst);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000561
562 SDValue InFlag(0, 0);
563 CondReg = DAG.getCopyToReg(Chain, dl, Mips::FCR31, Xor, InFlag);
564
565 // Emit the round instruction and bit convert to integer
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 SDValue Trunc = DAG.getNode(MipsISD::FPRound, dl, MVT::f32,
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000567 Src, CondReg.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000568 SDValue BitCvt = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Trunc);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000569 return BitCvt;
570}
571
572SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000573LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000574{
575 SDValue Chain = Op.getOperand(0);
576 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +0000577 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000578
579 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000581
582 // Subtract the dynamic size from the actual stack size to
583 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +0000584 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000585
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000586 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000587 // must be placed in the stack pointer register.
Dale Johannesena05dca42009-02-04 23:02:30 +0000588 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000589
590 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000591 // value and a chain
592 SDValue Ops[2] = { Sub, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000593 return DAG.getMergeValues(Ops, 2, dl);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000594}
595
596SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000597LowerANDOR(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000598{
599 SDValue LHS = Op.getOperand(0);
600 SDValue RHS = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +0000601 DebugLoc dl = Op.getDebugLoc();
602
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000603 if (LHS.getOpcode() != MipsISD::FPCmp || RHS.getOpcode() != MipsISD::FPCmp)
604 return Op;
605
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 SDValue True = DAG.getConstant(1, MVT::i32);
607 SDValue False = DAG.getConstant(0, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000608
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000609 SDValue LSEL = DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000610 LHS, True, False, LHS.getOperand(2));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000611 SDValue RSEL = DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000612 RHS, True, False, RHS.getOperand(2));
613
Owen Anderson825b72b2009-08-11 20:47:22 +0000614 return DAG.getNode(Op.getOpcode(), dl, MVT::i32, LSEL, RSEL);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000615}
616
617SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000618LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000619{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000620 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000621 // the block to branch to if the condition is true.
622 SDValue Chain = Op.getOperand(0);
623 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000624 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000625
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000626 if (Op.getOperand(1).getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000627 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000628
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000629 SDValue CondRes = Op.getOperand(1);
630 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000631 Mips::CondCode CC =
632 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000633 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000634
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000635 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000636 Dest, CondRes);
637}
638
639SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000640LowerSETCC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000641{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000642 // The operands to this are the left and right operands to compare (ops #0,
643 // and #1) and the condition code to compare them with (op #2) as a
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000644 // CondCodeSDNode.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000645 SDValue LHS = Op.getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +0000646 SDValue RHS = Op.getOperand(1);
647 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000648
649 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000650
651 return DAG.getNode(MipsISD::FPCmp, dl, Op.getValueType(), LHS, RHS,
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000653}
654
655SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000656LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000657{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000658 SDValue Cond = Op.getOperand(0);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000659 SDValue True = Op.getOperand(1);
660 SDValue False = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000661 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000662
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000663 // if the incomming condition comes from a integer compare, the select
664 // operation must be SelectCC or a conditional move if the subtarget
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000665 // supports it.
666 if (Cond.getOpcode() != MipsISD::FPCmp) {
667 if (Subtarget->hasCondMov() && !True.getValueType().isFloatingPoint())
668 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000669 return DAG.getNode(MipsISD::SelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000670 Cond, True, False);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000671 }
672
673 // if the incomming condition comes from fpcmp, the select
674 // operation must use FPSelectCC.
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000675 SDValue CCNode = Cond.getOperand(2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000676 return DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000677 Cond, True, False, CCNode);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000678}
679
Dan Gohmand858e902010-04-17 15:26:15 +0000680SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
681 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +0000682 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000683 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000684 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000685
Eli Friedmane2c74082009-08-03 02:22:28 +0000686 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +0000687 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000688
Chris Lattnerb71b9092009-08-13 06:28:06 +0000689 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000690
Chris Lattnere3736f82009-08-13 05:41:27 +0000691 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000692 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
693 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000694 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +0000695 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
696 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000697 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +0000698 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000699 // %hi/%lo relocation
Devang Patel0d881da2010-07-06 22:08:15 +0000700 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000701 MipsII::MO_ABS_HILO);
Chris Lattnere3736f82009-08-13 05:41:27 +0000702 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GA, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GA);
704 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000705
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000706 } else {
Devang Patel0d881da2010-07-06 22:08:15 +0000707 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000708 MipsII::MO_GOT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000709 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000710 DAG.getEntryNode(), GA, MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000711 false, false, 0);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000712 // On functions and global targets not internal linked only
713 // a load from got/GP is necessary for PIC to work.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000714 if (!GV->hasLocalLinkage() || isa<Function>(GV))
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000715 return ResNode;
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GA);
717 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000718 }
719
Torok Edwinc23197a2009-07-14 16:55:14 +0000720 llvm_unreachable("Dont know how to handle GlobalAddress");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000721 return SDValue(0,0);
722}
723
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000724SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
725 SelectionDAG &DAG) const {
726 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
727 assert(false && "implement LowerBlockAddress for -static");
728 return SDValue(0, 0);
729 }
730 else {
731 // FIXME there isn't actually debug info here
732 DebugLoc dl = Op.getDebugLoc();
733 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
734 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
735 MipsII::MO_GOT);
736 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
737 MipsII::MO_ABS_HILO);
738 SDValue Load = DAG.getLoad(MVT::i32, dl,
739 DAG.getEntryNode(), BAGOTOffset,
740 MachinePointerInfo(), false, false, 0);
741 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
742 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
743 }
744}
745
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000746SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000747LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000748{
Torok Edwinc23197a2009-07-14 16:55:14 +0000749 llvm_unreachable("TLS not implemented for MIPS.");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000750 return SDValue(); // Not reached
751}
752
753SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000754LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000755{
Dan Gohman475871a2008-07-27 21:46:04 +0000756 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000757 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +0000758 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000759 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000760 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
761 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HILO;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000762
Owen Andersone50ed302009-08-10 22:56:29 +0000763 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000764 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000765
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000766 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
767
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +0000768 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +0000769 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000770 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000771 } else // Emit Load from Global Pointer
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000772 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
773 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000774 false, false, 0);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000775
Owen Anderson825b72b2009-08-11 20:47:22 +0000776 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTI);
777 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000778
779 return ResNode;
780}
781
Dan Gohman475871a2008-07-27 21:46:04 +0000782SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000783LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000784{
Dan Gohman475871a2008-07-27 21:46:04 +0000785 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000786 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +0000787 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +0000788 // FIXME there isn't actually debug info here
789 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000790
791 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000792 // FIXME: we should reference the constant pool using small data sections,
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000793 // but the asm printer currently doens't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000794 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000795 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +0000796 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000797 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
798 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000799 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000800
801 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000802 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000803 N->getOffset(), MipsII::MO_ABS_HILO);
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CP);
805 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CP);
806 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000807 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000808 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000809 N->getOffset(), MipsII::MO_GOT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000810 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000811 CP, MachinePointerInfo::getConstantPool(),
812 false, false, 0);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000813 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CP);
814 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
815 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000816
817 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000818}
819
Dan Gohmand858e902010-04-17 15:26:15 +0000820SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +0000821 MachineFunction &MF = DAG.getMachineFunction();
822 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
823
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000824 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +0000825 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
826 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000827
828 // vastart just stores the address of the VarArgsFrameIndex slot into the
829 // memory location argument.
830 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +0000831 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
832 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +0000833 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000834}
835
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000836//===----------------------------------------------------------------------===//
837// Calling Convention Implementation
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000838//===----------------------------------------------------------------------===//
839
840#include "MipsGenCallingConv.inc"
841
842//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000843// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000844// Mips O32 ABI rules:
845// ---
846// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000847// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000848// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000849// f64 - Only passed in two aliased f32 registers if no int reg has been used
850// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000851// not used, it must be shadowed. If only A3 is avaiable, shadow it and
852// go to stack.
853//===----------------------------------------------------------------------===//
854
Duncan Sands1e96bab2010-11-04 10:49:57 +0000855static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000856 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000857 ISD::ArgFlagsTy ArgFlags, CCState &State) {
858
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000859 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000860
861 static const unsigned IntRegs[] = {
862 Mips::A0, Mips::A1, Mips::A2, Mips::A3
863 };
864 static const unsigned F32Regs[] = {
865 Mips::F12, Mips::F14
866 };
867 static const unsigned F64Regs[] = {
868 Mips::D6, Mips::D7
869 };
870
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000871 unsigned Reg = 0;
872 static bool IntRegUsed = false;
873
874 // This must be the first arg of the call if no regs have been allocated.
875 // Initialize IntRegUsed in that case.
876 if (IntRegs[State.getFirstUnallocated(IntRegs, IntRegsSize)] == Mips::A0 &&
877 F32Regs[State.getFirstUnallocated(F32Regs, FloatRegsSize)] == Mips::F12 &&
878 F64Regs[State.getFirstUnallocated(F64Regs, FloatRegsSize)] == Mips::D6)
879 IntRegUsed = false;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000880
881 // Promote i8 and i16
Owen Anderson825b72b2009-08-11 20:47:22 +0000882 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
883 LocVT = MVT::i32;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000884 if (ArgFlags.isSExt())
885 LocInfo = CCValAssign::SExt;
886 else if (ArgFlags.isZExt())
887 LocInfo = CCValAssign::ZExt;
888 else
889 LocInfo = CCValAssign::AExt;
890 }
891
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000892 if (ValVT == MVT::i32) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000893 Reg = State.AllocateReg(IntRegs, IntRegsSize);
894 IntRegUsed = true;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000895 } else if (ValVT == MVT::f32) {
896 // An int reg has to be marked allocated regardless of whether or not
897 // IntRegUsed is true.
898 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000899
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000900 if (IntRegUsed) {
901 if (Reg) // Int reg is available
902 LocVT = MVT::i32;
903 } else {
904 unsigned FReg = State.AllocateReg(F32Regs, FloatRegsSize);
905 if (FReg) // F32 reg is available
906 Reg = FReg;
907 else if (Reg) // No F32 regs are available, but an int reg is available.
908 LocVT = MVT::i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000909 }
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000910 } else if (ValVT == MVT::f64) {
911 // Int regs have to be marked allocated regardless of whether or not
912 // IntRegUsed is true.
913 Reg = State.AllocateReg(IntRegs, IntRegsSize);
914 if (Reg == Mips::A1)
915 Reg = State.AllocateReg(IntRegs, IntRegsSize);
916 else if (Reg == Mips::A3)
917 Reg = 0;
918 State.AllocateReg(IntRegs, IntRegsSize);
919
920 // At this point, Reg is A0, A2 or 0, and all the unavailable integer regs
921 // are marked as allocated.
922 if (IntRegUsed) {
923 if (Reg)// if int reg is available
924 LocVT = MVT::i32;
925 } else {
926 unsigned FReg = State.AllocateReg(F64Regs, FloatRegsSize);
927 if (FReg) // F64 reg is available.
928 Reg = FReg;
929 else if (Reg) // No F64 regs are available, but an int reg is available.
930 LocVT = MVT::i32;
931 }
932 } else
933 assert(false && "cannot handle this ValVT");
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000934
935 if (!Reg) {
936 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
937 unsigned Offset = State.AllocateStack(SizeInBytes, SizeInBytes);
938 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
939 } else
940 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
941
942 return false; // CC must always match
943}
944
Duncan Sands1e96bab2010-11-04 10:49:57 +0000945static bool CC_MipsO32_VarArgs(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000946 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +0000947 ISD::ArgFlagsTy ArgFlags, CCState &State) {
948
949 static const unsigned IntRegsSize=4;
950
951 static const unsigned IntRegs[] = {
952 Mips::A0, Mips::A1, Mips::A2, Mips::A3
953 };
954
955 // Promote i8 and i16
956 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
957 LocVT = MVT::i32;
958 if (ArgFlags.isSExt())
959 LocInfo = CCValAssign::SExt;
960 else if (ArgFlags.isZExt())
961 LocInfo = CCValAssign::ZExt;
962 else
963 LocInfo = CCValAssign::AExt;
964 }
965
966 if (ValVT == MVT::i32 || ValVT == MVT::f32) {
967 if (unsigned Reg = State.AllocateReg(IntRegs, IntRegsSize)) {
968 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, MVT::i32, LocInfo));
969 return false;
970 }
971 unsigned Off = State.AllocateStack(4, 4);
972 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Off, LocVT, LocInfo));
973 return false;
974 }
975
976 unsigned UnallocIntReg = State.getFirstUnallocated(IntRegs, IntRegsSize);
977 if (ValVT == MVT::f64) {
978 if (IntRegs[UnallocIntReg] == (unsigned (Mips::A1))) {
979 // A1 can't be used anymore, because 64 bit arguments
980 // must be aligned when copied back to the caller stack
981 State.AllocateReg(IntRegs, IntRegsSize);
982 UnallocIntReg++;
983 }
984
985 if (IntRegs[UnallocIntReg] == (unsigned (Mips::A0)) ||
986 IntRegs[UnallocIntReg] == (unsigned (Mips::A2))) {
987 unsigned Reg = State.AllocateReg(IntRegs, IntRegsSize);
988 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, MVT::i32, LocInfo));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000989 // Shadow the next register so it can be used
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +0000990 // later to get the other 32bit part.
991 State.AllocateReg(IntRegs, IntRegsSize);
992 return false;
993 }
994
995 // Register is shadowed to preserve alignment, and the
996 // argument goes to a stack location.
997 if (UnallocIntReg != IntRegsSize)
998 State.AllocateReg(IntRegs, IntRegsSize);
999
1000 unsigned Off = State.AllocateStack(8, 8);
1001 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Off, LocVT, LocInfo));
1002 return false;
1003 }
1004
1005 return true; // CC didn't match
1006}
1007
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001008//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001009// Call Calling Convention Implementation
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001010//===----------------------------------------------------------------------===//
1011
Dan Gohman98ca4f22009-08-05 01:29:28 +00001012/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001013/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001014/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001015SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001016MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001017 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001018 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001019 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001020 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001021 const SmallVectorImpl<ISD::InputArg> &Ins,
1022 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001023 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001024 // MIPs target does not yet support tail call optimization.
1025 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001026
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001027 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001028 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001029 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001030
1031 // Analyze operands of the call, assigning locations to each operand.
1032 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001033 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1034 *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001035
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +00001036 // To meet O32 ABI, Mips must always allocate 16 bytes on
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001037 // the stack (even if less than 4 are used as arguments)
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +00001038 if (Subtarget->isABI_O32()) {
Duncan Sands1e96bab2010-11-04 10:49:57 +00001039 int VTsize = MVT(MVT::i32).getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +00001040 MFI->CreateFixedObject(VTsize, (VTsize*3), true);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001041 CCInfo.AnalyzeCallOperands(Outs,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001042 isVarArg ? CC_MipsO32_VarArgs : CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001043 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001044 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001045
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001046 // Get a count of how many bytes are to be pushed on the stack.
1047 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnere563bbc2008-10-11 22:08:30 +00001048 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001049
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001050 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001051 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1052 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001053
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001054 // First/LastArgStackLoc contains the first/last
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001055 // "at stack" argument location.
1056 int LastArgStackLoc = 0;
1057 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001058
1059 // Walk the register/memloc assignments, inserting copies/loads.
1060 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001061 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001062 CCValAssign &VA = ArgLocs[i];
1063
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001064 // Promote the value if needed.
1065 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001066 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001067 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001068 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001069 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001070 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001071 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001072 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001073 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001074 DAG.getConstant(0, getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00001075 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001076 DAG.getConstant(1, getPointerTy()));
1077 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
1078 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
1079 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001080 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001081 }
1082 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00001083 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001084 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001085 break;
1086 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001087 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001088 break;
1089 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001090 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001091 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001092 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001093
1094 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001095 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001096 if (VA.isRegLoc()) {
1097 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00001098 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001099 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001100
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001101 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00001102 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001103
Chris Lattnere0b12152008-03-17 06:57:02 +00001104 // Create the frame index object for this incoming parameter
1105 // This guarantees that when allocating Local Area the firsts
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001106 // 16 bytes which are alwayes reserved won't be overwritten
1107 // if O32 ABI is used. For EABI the first address is zero.
1108 LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001109 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001110 LastArgStackLoc, true);
Chris Lattnere0b12152008-03-17 06:57:02 +00001111
Dan Gohman475871a2008-07-27 21:46:04 +00001112 SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00001113
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001114 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00001115 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00001116 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
1117 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001118 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001119 }
1120
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001121 // Transform all store nodes into one single node because all store
1122 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001123 if (!MemOpChains.empty())
1124 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001125 &MemOpChains[0], MemOpChains.size());
1126
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001127 // Build a sequence of copy-to-reg nodes chained together with token
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001128 // chain and flag operands which copy the outgoing args into registers.
1129 // The InFlag in necessary since all emited instructions must be
1130 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +00001131 SDValue InFlag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001132 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001133 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001134 RegsToPass[i].second, InFlag);
1135 InFlag = Chain.getValue(1);
1136 }
1137
Bill Wendling056292f2008-09-16 21:48:12 +00001138 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001139 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1140 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001141 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001142 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1143 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001144 getPointerTy(), 0, OpFlag);
Bill Wendling056292f2008-09-16 21:48:12 +00001145 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001146 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001147 getPointerTy(), OpFlag);
Bill Wendling056292f2008-09-16 21:48:12 +00001148
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001149 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001150 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001151 //
1152 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001153 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00001154 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001155 Ops.push_back(Chain);
1156 Ops.push_back(Callee);
1157
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001158 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001159 // known live into the call.
1160 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1161 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1162 RegsToPass[i].second.getValueType()));
1163
Gabor Greifba36cb52008-08-28 21:40:38 +00001164 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001165 Ops.push_back(InFlag);
1166
Dale Johannesen33c960f2009-02-04 20:06:27 +00001167 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001168 InFlag = Chain.getValue(1);
1169
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001170 // Create a stack location to hold GP when PIC is used. This stack
1171 // location is used on function prologue to save GP and also after all
1172 // emited CALL's to restore GP.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001173 if (IsPIC) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001174 // Function can have an arbitrary number of calls, so
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001175 // hold the LastArgStackLoc with the biggest offset.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001176 int FI;
1177 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001178 if (LastArgStackLoc >= MipsFI->getGPStackOffset()) {
1179 LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001180 // Create the frame index only once. SPOffset here can be anything
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001181 // (this will be fixed on processFunctionBeforeFrameFinalized)
1182 if (MipsFI->getGPStackOffset() == -1) {
Evan Chenged2ae132010-07-03 00:40:23 +00001183 FI = MFI->CreateFixedObject(4, 0, true);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001184 MipsFI->setGPFI(FI);
1185 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001186 MipsFI->setGPStackOffset(LastArgStackLoc);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001187 }
1188
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001189 // Reload GP value.
1190 FI = MipsFI->getGPFI();
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001191 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1192 SDValue GPLoad = DAG.getLoad(MVT::i32, dl, Chain, FIN,
1193 MachinePointerInfo::getFixedStack(FI),
David Greenef6fa1862010-02-15 16:56:10 +00001194 false, false, 0);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001195 Chain = GPLoad.getValue(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001196 Chain = DAG.getCopyToReg(Chain, dl, DAG.getRegister(Mips::GP, MVT::i32),
Dan Gohman475871a2008-07-27 21:46:04 +00001197 GPLoad, SDValue(0,0));
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +00001198 InFlag = Chain.getValue(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001199 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001200
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00001201 // Create the CALLSEQ_END node.
1202 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1203 DAG.getIntPtrConstant(0, true), InFlag);
1204 InFlag = Chain.getValue(1);
1205
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001206 // Handle result values, copying them out of physregs into vregs that we
1207 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001208 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
1209 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001210}
1211
Dan Gohman98ca4f22009-08-05 01:29:28 +00001212/// LowerCallResult - Lower the result values of a call into the
1213/// appropriate copies out of appropriate physical registers.
1214SDValue
1215MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001216 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001217 const SmallVectorImpl<ISD::InputArg> &Ins,
1218 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001219 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001220
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001221 // Assign locations to each value returned by this call.
1222 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001223 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001224 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001225
Dan Gohman98ca4f22009-08-05 01:29:28 +00001226 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001227
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001228 // Copy all of the result registers out of their specified physreg.
1229 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001230 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001231 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001232 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001233 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001234 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001235
Dan Gohman98ca4f22009-08-05 01:29:28 +00001236 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001237}
1238
1239//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001240// Formal Arguments Calling Convention Implementation
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001241//===----------------------------------------------------------------------===//
1242
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001243/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001244/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001245SDValue
1246MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001247 CallingConv::ID CallConv, bool isVarArg,
1248 const SmallVectorImpl<ISD::InputArg>
1249 &Ins,
1250 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001251 SmallVectorImpl<SDValue> &InVals)
1252 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001253
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00001254 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001255 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001256 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001257
1258 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Dan Gohman1e93df62010-04-17 14:41:14 +00001259 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001260
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001261 // Used with vargs to acumulate store chains.
1262 std::vector<SDValue> OutChains;
1263
1264 // Keep track of the last register used for arguments
1265 unsigned ArgRegEnd = 0;
1266
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001267 // Assign locations to all of the incoming arguments.
1268 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001269 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1270 ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001271
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001272 if (Subtarget->isABI_O32())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001273 CCInfo.AnalyzeFormalArguments(Ins,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001274 isVarArg ? CC_MipsO32_VarArgs : CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001275 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001276 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001277
Dan Gohman475871a2008-07-27 21:46:04 +00001278 SDValue StackPtr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001279
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001280 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
1281
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001282 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001283 CCValAssign &VA = ArgLocs[i];
1284
1285 // Arguments stored on registers
1286 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001287 EVT RegVT = VA.getLocVT();
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001288 ArgRegEnd = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00001289 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001290
Owen Anderson825b72b2009-08-11 20:47:22 +00001291 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001292 RC = Mips::CPURegsRegisterClass;
1293 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001294 RC = Mips::FGR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001295 else if (RegVT == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001296 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001297 RC = Mips::AFGR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001298 } else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001299 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001300
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001301 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001302 // physical registers into virtual ones
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001303 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegEnd, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001304 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001305
1306 // If this is an 8 or 16-bit value, it has been passed promoted
1307 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001308 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001309 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00001310 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001311 if (VA.getLocInfo() == CCValAssign::SExt)
1312 Opcode = ISD::AssertSext;
1313 else if (VA.getLocInfo() == CCValAssign::ZExt)
1314 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00001315 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001316 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00001317 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00001318 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001319 }
1320
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001321 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001322 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001323 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
1324 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00001325 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001326 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001327 VA.getLocReg()+1, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001328 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Bruno Cardoso Lopesb1fce0a2011-01-18 19:38:25 +00001329 SDValue Pair = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, ArgValue2, ArgValue);
1330 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Pair);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001331 }
1332 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001333
Dan Gohman98ca4f22009-08-05 01:29:28 +00001334 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001335 } else { // VA.isRegLoc()
1336
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001337 // sanity check
1338 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001339
1340 // The last argument is not a register anymore
1341 ArgRegEnd = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001342
1343 // The stack pointer offset is relative to the caller stack frame.
1344 // Since the real stack size is unknown here, a negative SPOffset
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001345 // is used so there's a way to adjust these offsets when the stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001346 // size get known (on EliminateFrameIndex). A dummy SPOffset is
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001347 // used instead of a direct negative address (which is recorded to
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001348 // be used on emitPrologue) to avoid mis-calc of the first stack
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001349 // offset on PEI::calculateFrameObjectOffsets.
1350 // Arguments are always 32-bit.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001351 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +00001352 int FI = MFI->CreateFixedObject(ArgSize, 0, true);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001353 MipsFI->recordLoadArgsFI(FI, -(ArgSize+
1354 (FirstStackArgLoc + VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001355
1356 // Create load nodes to retrieve arguments from the stack
Dan Gohman475871a2008-07-27 21:46:04 +00001357 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001358 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
1359 MachinePointerInfo::getFixedStack(FI),
David Greenef6fa1862010-02-15 16:56:10 +00001360 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001361 }
1362 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001363
1364 // The mips ABIs for returning structs by value requires that we copy
1365 // the sret argument into $v0 for the return. Save the argument into
1366 // a virtual register so that we can access it from the return points.
1367 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1368 unsigned Reg = MipsFI->getSRetReturnReg();
1369 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001370 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001371 MipsFI->setSRetReturnReg(Reg);
1372 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001373 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001374 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001375 }
1376
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001377 // To meet ABI, when VARARGS are passed on registers, the registers
1378 // must have their values written to the caller stack frame. If the last
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001379 // argument was placed in the stack, there's no need to save any register.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001380 if ((isVarArg) && (Subtarget->isABI_O32() && ArgRegEnd)) {
1381 if (StackPtr.getNode() == 0)
1382 StackPtr = DAG.getRegister(StackReg, getPointerTy());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001383
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001384 // The last register argument that must be saved is Mips::A3
1385 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
1386 unsigned StackLoc = ArgLocs.size()-1;
1387
1388 for (++ArgRegEnd; ArgRegEnd <= Mips::A3; ++ArgRegEnd, ++StackLoc) {
1389 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegEnd, RC);
1390 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
1391
Evan Chenged2ae132010-07-03 00:40:23 +00001392 int FI = MFI->CreateFixedObject(4, 0, true);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001393 MipsFI->recordStoreVarArgsFI(FI, -(4+(StackLoc*4)));
1394 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00001395 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
1396 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001397 false, false, 0));
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001398
1399 // Record the frame index of the first variable argument
1400 // which is a value necessary to VASTART.
Dan Gohman1e93df62010-04-17 14:41:14 +00001401 if (!MipsFI->getVarArgsFrameIndex())
1402 MipsFI->setVarArgsFrameIndex(FI);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001403 }
1404 }
1405
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001406 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001407 // the size of Ins and InVals. This only happens when on varg functions
1408 if (!OutChains.empty()) {
1409 OutChains.push_back(Chain);
1410 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1411 &OutChains[0], OutChains.size());
1412 }
1413
Dan Gohman98ca4f22009-08-05 01:29:28 +00001414 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001415}
1416
1417//===----------------------------------------------------------------------===//
1418// Return Value Calling Convention Implementation
1419//===----------------------------------------------------------------------===//
1420
Dan Gohman98ca4f22009-08-05 01:29:28 +00001421SDValue
1422MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001423 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001424 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001425 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001426 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001427
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001428 // CCValAssign - represent the assignment of
1429 // the return value to a location
1430 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001431
1432 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001433 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1434 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001435
Dan Gohman98ca4f22009-08-05 01:29:28 +00001436 // Analize return values.
1437 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001438
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001439 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001440 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001441 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001442 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001443 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001444 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001445 }
1446
Dan Gohman475871a2008-07-27 21:46:04 +00001447 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001448
1449 // Copy the result values into the output registers.
1450 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1451 CCValAssign &VA = RVLocs[i];
1452 assert(VA.isRegLoc() && "Can only return in registers!");
1453
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001454 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001455 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001456
1457 // guarantee that all emitted copies are
1458 // stuck together, avoiding something bad
1459 Flag = Chain.getValue(1);
1460 }
1461
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001462 // The mips ABIs for returning structs by value requires that we copy
1463 // the sret argument into $v0 for the return. We saved the argument into
1464 // a virtual register in the entry block, so now we copy the value out
1465 // and into $v0.
1466 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1467 MachineFunction &MF = DAG.getMachineFunction();
1468 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1469 unsigned Reg = MipsFI->getSRetReturnReg();
1470
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001471 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00001472 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00001473 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001474
Dale Johannesena05dca42009-02-04 23:02:30 +00001475 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001476 Flag = Chain.getValue(1);
1477 }
1478
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001479 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00001480 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001481 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001482 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001483 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001484 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001485 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001486}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001487
1488//===----------------------------------------------------------------------===//
1489// Mips Inline Assembly Support
1490//===----------------------------------------------------------------------===//
1491
1492/// getConstraintType - Given a constraint letter, return the type of
1493/// constraint it is for this target.
1494MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001495getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001496{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001497 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001498 // GCC config/mips/constraints.md
1499 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001500 // 'd' : An address register. Equivalent to r
1501 // unless generating MIPS16 code.
1502 // 'y' : Equivalent to r; retained for
1503 // backwards compatibility.
1504 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001505 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001506 switch (Constraint[0]) {
1507 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001508 case 'd':
1509 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001510 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001511 return C_RegisterClass;
1512 break;
1513 }
1514 }
1515 return TargetLowering::getConstraintType(Constraint);
1516}
1517
John Thompson44ab89e2010-10-29 17:29:13 +00001518/// Examine constraint type and operand type and determine a weight value.
1519/// This object must already have been set up with the operand type
1520/// and the current alternative constraint selected.
1521TargetLowering::ConstraintWeight
1522MipsTargetLowering::getSingleConstraintMatchWeight(
1523 AsmOperandInfo &info, const char *constraint) const {
1524 ConstraintWeight weight = CW_Invalid;
1525 Value *CallOperandVal = info.CallOperandVal;
1526 // If we don't have a value, we can't do a match,
1527 // but allow it at the lowest weight.
1528 if (CallOperandVal == NULL)
1529 return CW_Default;
1530 const Type *type = CallOperandVal->getType();
1531 // Look at the constraint type.
1532 switch (*constraint) {
1533 default:
1534 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
1535 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001536 case 'd':
1537 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00001538 if (type->isIntegerTy())
1539 weight = CW_Register;
1540 break;
1541 case 'f':
1542 if (type->isFloatTy())
1543 weight = CW_Register;
1544 break;
1545 }
1546 return weight;
1547}
1548
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001549/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1550/// return a list of registers that can be used to satisfy the constraint.
1551/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001552std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00001553getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001554{
1555 if (Constraint.size() == 1) {
1556 switch (Constraint[0]) {
1557 case 'r':
1558 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001559 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001560 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001561 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001562 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001563 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1564 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001565 }
1566 }
1567 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1568}
1569
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001570/// Given a register class constraint, like 'r', if this corresponds directly
1571/// to an LLVM register class, return a register of 0 and the register class
1572/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001573std::vector<unsigned> MipsTargetLowering::
1574getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001575 EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001576{
1577 if (Constraint.size() != 1)
1578 return std::vector<unsigned>();
1579
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001580 switch (Constraint[0]) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001581 default : break;
1582 case 'r':
1583 // GCC Mips Constraint Letters
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001584 case 'd':
1585 case 'y':
1586 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
1587 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
1588 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001589 Mips::T8, 0);
1590
1591 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001592 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001593 if (Subtarget->isSingleFloat())
1594 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1595 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1596 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1597 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1598 Mips::F30, Mips::F31, 0);
1599 else
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001600 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
1601 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001602 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +00001603 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001604
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001605 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001606 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001607 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
1608 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001609 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001610 }
1611 return std::vector<unsigned>();
1612}
Dan Gohman6520e202008-10-18 02:06:02 +00001613
1614bool
1615MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1616 // The Mips target isn't yet aware of offsets.
1617 return false;
1618}
Evan Chengeb2f9692009-10-27 19:56:55 +00001619
Evan Chenga1eaa3c2009-10-28 01:43:28 +00001620bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
1621 if (VT != MVT::f32 && VT != MVT::f64)
1622 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00001623 if (Imm.isNegZero())
1624 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00001625 return Imm.isZero();
1626}