blob: 77a98552295262464651d234d93bc9881a17d2ab [file] [log] [blame]
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014
15#define DEBUG_TYPE "mips-lower"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000016#include "MipsISelLowering.h"
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +000017#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000018#include "MipsTargetMachine.h"
Chris Lattnerb71b9092009-08-13 06:28:06 +000019#include "MipsTargetObjectFile.h"
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000020#include "MipsSubtarget.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000021#include "llvm/DerivedTypes.h"
22#include "llvm/Function.h"
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +000023#include "llvm/GlobalVariable.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000024#include "llvm/Intrinsics.h"
25#include "llvm/CallingConv.h"
26#include "llvm/CodeGen/CallingConvLower.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000031#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000032#include "llvm/CodeGen/ValueTypes.h"
33#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000034#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000035using namespace llvm;
36
Chris Lattnerf0144122009-07-28 03:13:23 +000037const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
38 switch (Opcode) {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000039 case MipsISD::JmpLink : return "MipsISD::JmpLink";
40 case MipsISD::Hi : return "MipsISD::Hi";
41 case MipsISD::Lo : return "MipsISD::Lo";
42 case MipsISD::GPRel : return "MipsISD::GPRel";
43 case MipsISD::Ret : return "MipsISD::Ret";
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000044 case MipsISD::FPBrcond : return "MipsISD::FPBrcond";
45 case MipsISD::FPCmp : return "MipsISD::FPCmp";
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +000046 case MipsISD::CMovFP_T : return "MipsISD::CMovFP_T";
47 case MipsISD::CMovFP_F : return "MipsISD::CMovFP_F";
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +000048 case MipsISD::FPRound : return "MipsISD::FPRound";
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +000049 case MipsISD::MAdd : return "MipsISD::MAdd";
50 case MipsISD::MAddu : return "MipsISD::MAddu";
51 case MipsISD::MSub : return "MipsISD::MSub";
52 case MipsISD::MSubu : return "MipsISD::MSubu";
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +000053 case MipsISD::DivRem : return "MipsISD::DivRem";
54 case MipsISD::DivRemU : return "MipsISD::DivRemU";
Akira Hatanaka99a2e982011-04-15 19:52:08 +000055 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
56 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000057 default : return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000058 }
59}
60
61MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000062MipsTargetLowering(MipsTargetMachine &TM)
Chris Lattnerb71b9092009-08-13 06:28:06 +000063 : TargetLowering(TM, new MipsTargetObjectFile()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000064 Subtarget = &TM.getSubtarget<MipsSubtarget>();
65
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000066 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000067 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000068 setBooleanContents(ZeroOrOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000069
70 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000071 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
72 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000073
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000074 // When dealing with single precision only, use libcalls
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000075 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000076 if (!Subtarget->isFP64bit())
Owen Anderson825b72b2009-08-11 20:47:22 +000077 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000078
Wesley Peckbf17cfa2010-11-23 03:31:01 +000079 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +000080 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
81 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
82 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000083
Eli Friedman6055a6a2009-07-17 04:07:24 +000084 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +000085 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
86 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +000087
Wesley Peckbf17cfa2010-11-23 03:31:01 +000088 // Used by legalize types to correctly generate the setcc result.
89 // Without this, every float setcc comes with a AND/OR with the result,
90 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000091 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +000092 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000093
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000094 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +000095 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +000096 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000097 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
98 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
99 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
100 setOperationAction(ISD::SELECT, MVT::f32, Custom);
101 setOperationAction(ISD::SELECT, MVT::f64, Custom);
102 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000103 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
104 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
105 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000106 setOperationAction(ISD::VASTART, MVT::Other, Custom);
107
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000108 setOperationAction(ISD::SDIV, MVT::i32, Expand);
109 setOperationAction(ISD::SREM, MVT::i32, Expand);
110 setOperationAction(ISD::UDIV, MVT::i32, Expand);
111 setOperationAction(ISD::UREM, MVT::i32, Expand);
112
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000113 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000114 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
115 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
116 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
117 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
118 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
120 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
121 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
122 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000123
124 if (!Subtarget->isMips32r2())
125 setOperationAction(ISD::ROTR, MVT::i32, Expand);
126
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
128 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
129 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
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
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000145 setOperationAction(ISD::VAARG, MVT::Other, Expand);
146 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
147 setOperationAction(ISD::VAEND, MVT::Other, Expand);
148
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000149 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000150 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
151 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
152 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000153
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000154 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000155 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000156
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000157 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
159 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000160 }
161
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000162 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000164
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000165 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000166 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000167
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000168 setTargetDAGCombine(ISD::ADDE);
169 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000170 setTargetDAGCombine(ISD::SDIVREM);
171 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000172 setTargetDAGCombine(ISD::SETCC);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000173
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000174 setMinFunctionAlignment(2);
175
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000176 setStackPointerRegisterToSaveRestore(Mips::SP);
177 computeRegisterProperties();
178}
179
Owen Anderson825b72b2009-08-11 20:47:22 +0000180MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
181 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000182}
183
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000184// SelectMadd -
185// Transforms a subgraph in CurDAG if the following pattern is found:
186// (addc multLo, Lo0), (adde multHi, Hi0),
187// where,
188// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000189// Lo0: initial value of Lo register
190// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000191// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000192static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000193 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000194 // for the matching to be successful.
195 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
196
197 if (ADDCNode->getOpcode() != ISD::ADDC)
198 return false;
199
200 SDValue MultHi = ADDENode->getOperand(0);
201 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000202 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000203 unsigned MultOpc = MultHi.getOpcode();
204
205 // MultHi and MultLo must be generated by the same node,
206 if (MultLo.getNode() != MultNode)
207 return false;
208
209 // and it must be a multiplication.
210 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
211 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000212
213 // MultLo amd MultHi must be the first and second output of MultNode
214 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000215 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
216 return false;
217
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000218 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000219 // of the values of MultNode, in which case MultNode will be removed in later
220 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000221 // If there exist users other than ADDENode or ADDCNode, this function returns
222 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000223 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000224 // produced.
225 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
226 return false;
227
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000228 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000229 DebugLoc dl = ADDENode->getDebugLoc();
230
231 // create MipsMAdd(u) node
232 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000233
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000234 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
235 MVT::Glue,
236 MultNode->getOperand(0),// Factor 0
237 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000238 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000239 ADDENode->getOperand(1));// Hi0
240
241 // create CopyFromReg nodes
242 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
243 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000244 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000245 Mips::HI, MVT::i32,
246 CopyFromLo.getValue(2));
247
248 // replace uses of adde and addc here
249 if (!SDValue(ADDCNode, 0).use_empty())
250 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
251
252 if (!SDValue(ADDENode, 0).use_empty())
253 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
254
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000255 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000256}
257
258// SelectMsub -
259// Transforms a subgraph in CurDAG if the following pattern is found:
260// (addc Lo0, multLo), (sube Hi0, multHi),
261// where,
262// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000263// Lo0: initial value of Lo register
264// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000265// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000266static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000267 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000268 // for the matching to be successful.
269 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
270
271 if (SUBCNode->getOpcode() != ISD::SUBC)
272 return false;
273
274 SDValue MultHi = SUBENode->getOperand(1);
275 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000276 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000277 unsigned MultOpc = MultHi.getOpcode();
278
279 // MultHi and MultLo must be generated by the same node,
280 if (MultLo.getNode() != MultNode)
281 return false;
282
283 // and it must be a multiplication.
284 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
285 return false;
286
287 // MultLo amd MultHi must be the first and second output of MultNode
288 // respectively.
289 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
290 return false;
291
292 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
293 // of the values of MultNode, in which case MultNode will be removed in later
294 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000295 // If there exist users other than SUBENode or SUBCNode, this function returns
296 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000297 // instruction node rather than a pair of MULT and MSUB instructions being
298 // produced.
299 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
300 return false;
301
302 SDValue Chain = CurDAG->getEntryNode();
303 DebugLoc dl = SUBENode->getDebugLoc();
304
305 // create MipsSub(u) node
306 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
307
308 SDValue MSub = CurDAG->getNode(MultOpc, dl,
309 MVT::Glue,
310 MultNode->getOperand(0),// Factor 0
311 MultNode->getOperand(1),// Factor 1
312 SUBCNode->getOperand(0),// Lo0
313 SUBENode->getOperand(0));// Hi0
314
315 // create CopyFromReg nodes
316 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
317 MSub);
318 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
319 Mips::HI, MVT::i32,
320 CopyFromLo.getValue(2));
321
322 // replace uses of sube and subc here
323 if (!SDValue(SUBCNode, 0).use_empty())
324 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
325
326 if (!SDValue(SUBENode, 0).use_empty())
327 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
328
329 return true;
330}
331
332static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
333 TargetLowering::DAGCombinerInfo &DCI,
334 const MipsSubtarget* Subtarget) {
335 if (DCI.isBeforeLegalize())
336 return SDValue();
337
338 if (Subtarget->isMips32() && SelectMadd(N, &DAG))
339 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000340
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000341 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000342}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000343
344static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
345 TargetLowering::DAGCombinerInfo &DCI,
346 const MipsSubtarget* Subtarget) {
347 if (DCI.isBeforeLegalize())
348 return SDValue();
349
350 if (Subtarget->isMips32() && SelectMsub(N, &DAG))
351 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000352
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000353 return SDValue();
354}
355
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000356static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
357 TargetLowering::DAGCombinerInfo &DCI,
358 const MipsSubtarget* Subtarget) {
359 if (DCI.isBeforeLegalizeOps())
360 return SDValue();
361
362 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
363 MipsISD::DivRemU;
364 DebugLoc dl = N->getDebugLoc();
365
366 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
367 N->getOperand(0), N->getOperand(1));
368 SDValue InChain = DAG.getEntryNode();
369 SDValue InGlue = DivRem;
370
371 // insert MFLO
372 if (N->hasAnyUseOfValue(0)) {
373 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, Mips::LO, MVT::i32,
374 InGlue);
375 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
376 InChain = CopyFromLo.getValue(1);
377 InGlue = CopyFromLo.getValue(2);
378 }
379
380 // insert MFHI
381 if (N->hasAnyUseOfValue(1)) {
382 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
383 Mips::HI, MVT::i32, InGlue);
384 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
385 }
386
387 return SDValue();
388}
389
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000390static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
391 switch (CC) {
392 default: llvm_unreachable("Unknown fp condition code!");
393 case ISD::SETEQ:
394 case ISD::SETOEQ: return Mips::FCOND_OEQ;
395 case ISD::SETUNE: return Mips::FCOND_UNE;
396 case ISD::SETLT:
397 case ISD::SETOLT: return Mips::FCOND_OLT;
398 case ISD::SETGT:
399 case ISD::SETOGT: return Mips::FCOND_OGT;
400 case ISD::SETLE:
401 case ISD::SETOLE: return Mips::FCOND_OLE;
402 case ISD::SETGE:
403 case ISD::SETOGE: return Mips::FCOND_OGE;
404 case ISD::SETULT: return Mips::FCOND_ULT;
405 case ISD::SETULE: return Mips::FCOND_ULE;
406 case ISD::SETUGT: return Mips::FCOND_UGT;
407 case ISD::SETUGE: return Mips::FCOND_UGE;
408 case ISD::SETUO: return Mips::FCOND_UN;
409 case ISD::SETO: return Mips::FCOND_OR;
410 case ISD::SETNE:
411 case ISD::SETONE: return Mips::FCOND_ONE;
412 case ISD::SETUEQ: return Mips::FCOND_UEQ;
413 }
414}
415
416
417// Returns true if condition code has to be inverted.
418static bool InvertFPCondCode(Mips::CondCode CC) {
419 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
420 return false;
421
422 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
423 return true;
424
425 assert(false && "Illegal Condition Code");
426 return false;
427}
428
429// Creates and returns an FPCmp node from a setcc node.
430// Returns Op if setcc is not a floating point comparison.
431static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
432 // must be a SETCC node
433 if (Op.getOpcode() != ISD::SETCC)
434 return Op;
435
436 SDValue LHS = Op.getOperand(0);
437
438 if (!LHS.getValueType().isFloatingPoint())
439 return Op;
440
441 SDValue RHS = Op.getOperand(1);
442 DebugLoc dl = Op.getDebugLoc();
443
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000444 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
445 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000446 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
447
448 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
449 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
450}
451
452// Creates and returns a CMovFPT/F node.
453static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
454 SDValue False, DebugLoc DL) {
455 bool invert = InvertFPCondCode((Mips::CondCode)
456 cast<ConstantSDNode>(Cond.getOperand(2))
457 ->getSExtValue());
458
459 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
460 True.getValueType(), True, False, Cond);
461}
462
463static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
464 TargetLowering::DAGCombinerInfo &DCI,
465 const MipsSubtarget* Subtarget) {
466 if (DCI.isBeforeLegalizeOps())
467 return SDValue();
468
469 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
470
471 if (Cond.getOpcode() != MipsISD::FPCmp)
472 return SDValue();
473
474 SDValue True = DAG.getConstant(1, MVT::i32);
475 SDValue False = DAG.getConstant(0, MVT::i32);
476
477 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
478}
479
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000480SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000481 const {
482 SelectionDAG &DAG = DCI.DAG;
483 unsigned opc = N->getOpcode();
484
485 switch (opc) {
486 default: break;
487 case ISD::ADDE:
488 return PerformADDECombine(N, DAG, DCI, Subtarget);
489 case ISD::SUBE:
490 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000491 case ISD::SDIVREM:
492 case ISD::UDIVREM:
493 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000494 case ISD::SETCC:
495 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000496 }
497
498 return SDValue();
499}
500
Dan Gohman475871a2008-07-27 21:46:04 +0000501SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000502LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000503{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000504 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000505 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000506 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000507 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
508 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000509 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000510 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000511 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000512 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
513 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000514 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000515 case ISD::VASTART: return LowerVASTART(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000516 }
Dan Gohman475871a2008-07-27 21:46:04 +0000517 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000518}
519
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000520//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000521// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000522//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000523
524// AddLiveIn - This helper function adds the specified physical register to the
525// MachineFunction as a live in value. It also creates a corresponding
526// virtual register for it.
527static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000528AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000529{
530 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000531 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
532 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000533 return VReg;
534}
535
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000536// Get fp branch code (not opcode) from condition code.
537static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
538 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
539 return Mips::BRANCH_T;
540
541 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
542 return Mips::BRANCH_F;
543
544 return Mips::BRANCH_INVALID;
545}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000546
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000547MachineBasicBlock *
548MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000549 MachineBasicBlock *BB) const {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000550 // There is no need to expand CMov instructions if target has
551 // conditional moves.
552 if (Subtarget->hasCondMov())
553 return BB;
554
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000555 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
556 bool isFPCmp = false;
Dale Johannesen94817572009-02-13 02:34:39 +0000557 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000558 unsigned Opc;
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000559
560 switch (MI->getOpcode()) {
561 default: assert(false && "Unexpected instr type to insert");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000562 case Mips::MOVT:
563 case Mips::MOVT_S:
564 case Mips::MOVT_D:
565 isFPCmp = true;
566 Opc = Mips::BC1F;
567 break;
568 case Mips::MOVF:
569 case Mips::MOVF_S:
570 case Mips::MOVF_D:
571 isFPCmp = true;
572 Opc = Mips::BC1T;
573 break;
574 case Mips::MOVZ_I:
575 case Mips::MOVZ_S:
576 case Mips::MOVZ_D:
577 Opc = Mips::BNE;
578 break;
579 case Mips::MOVN_I:
580 case Mips::MOVN_S:
581 case Mips::MOVN_D:
582 Opc = Mips::BEQ;
583 break;
584 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000585
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000586 // To "insert" a SELECT_CC instruction, we actually have to insert the
587 // diamond control-flow pattern. The incoming instruction knows the
588 // destination vreg to set, the condition code register to branch on, the
589 // true/false values to select between, and a branch opcode to use.
590 const BasicBlock *LLVM_BB = BB->getBasicBlock();
591 MachineFunction::iterator It = BB;
592 ++It;
Dan Gohman14152b42010-07-06 20:24:04 +0000593
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000594 // thisMBB:
595 // ...
596 // TrueVal = ...
597 // setcc r1, r2, r3
598 // bNE r1, r0, copy1MBB
599 // fallthrough --> copy0MBB
600 MachineBasicBlock *thisMBB = BB;
601 MachineFunction *F = BB->getParent();
602 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
603 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
604 F->insert(It, copy0MBB);
605 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +0000606
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000607 // Transfer the remainder of BB and its successor edges to sinkMBB.
608 sinkMBB->splice(sinkMBB->begin(), BB,
609 llvm::next(MachineBasicBlock::iterator(MI)),
610 BB->end());
611 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000612
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000613 // Next, add the true and fallthrough blocks as its successors.
614 BB->addSuccessor(copy0MBB);
615 BB->addSuccessor(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000616
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000617 // Emit the right instruction according to the type of the operands compared
618 if (isFPCmp)
619 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
620 else
621 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
622 .addReg(Mips::ZERO).addMBB(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000623
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000624
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000625 // copy0MBB:
626 // %FalseValue = ...
627 // # fallthrough to sinkMBB
628 BB = copy0MBB;
629
630 // Update machine-CFG edges
631 BB->addSuccessor(sinkMBB);
632
633 // sinkMBB:
634 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
635 // ...
636 BB = sinkMBB;
637
638 if (isFPCmp)
Dan Gohman14152b42010-07-06 20:24:04 +0000639 BuildMI(*BB, BB->begin(), dl,
640 TII->get(Mips::PHI), MI->getOperand(0).getReg())
Bruno Cardoso Lopes29e9daa2010-07-20 07:58:51 +0000641 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000642 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
643 else
644 BuildMI(*BB, BB->begin(), dl,
645 TII->get(Mips::PHI), MI->getOperand(0).getReg())
646 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
647 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000648
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000649 MI->eraseFromParent(); // The pseudo instruction is gone now.
650 return BB;
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000651}
652
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000653//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000654// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000655//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000656
Dan Gohman475871a2008-07-27 21:46:04 +0000657SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000658LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000659{
660 if (!Subtarget->isMips1())
661 return Op;
662
663 MachineFunction &MF = DAG.getMachineFunction();
664 unsigned CCReg = AddLiveIn(MF, Mips::FCR31, Mips::CCRRegisterClass);
665
666 SDValue Chain = DAG.getEntryNode();
667 DebugLoc dl = Op.getDebugLoc();
668 SDValue Src = Op.getOperand(0);
669
670 // Set the condition register
Owen Anderson825b72b2009-08-11 20:47:22 +0000671 SDValue CondReg = DAG.getCopyFromReg(Chain, dl, CCReg, MVT::i32);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000672 CondReg = DAG.getCopyToReg(Chain, dl, Mips::AT, CondReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000673 CondReg = DAG.getCopyFromReg(CondReg, dl, Mips::AT, MVT::i32);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000674
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 SDValue Cst = DAG.getConstant(3, MVT::i32);
676 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, CondReg, Cst);
677 Cst = DAG.getConstant(2, MVT::i32);
678 SDValue Xor = DAG.getNode(ISD::XOR, dl, MVT::i32, Or, Cst);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000679
680 SDValue InFlag(0, 0);
681 CondReg = DAG.getCopyToReg(Chain, dl, Mips::FCR31, Xor, InFlag);
682
683 // Emit the round instruction and bit convert to integer
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 SDValue Trunc = DAG.getNode(MipsISD::FPRound, dl, MVT::f32,
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000685 Src, CondReg.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000686 SDValue BitCvt = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Trunc);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000687 return BitCvt;
688}
689
690SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000691LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000692{
693 SDValue Chain = Op.getOperand(0);
694 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +0000695 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000696
697 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +0000698 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000699
700 // Subtract the dynamic size from the actual stack size to
701 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000703
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000704 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000705 // must be placed in the stack pointer register.
Dale Johannesena05dca42009-02-04 23:02:30 +0000706 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000707
708 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000709 // value and a chain
710 SDValue Ops[2] = { Sub, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000711 return DAG.getMergeValues(Ops, 2, dl);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000712}
713
714SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000715LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000716{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000717 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000718 // the block to branch to if the condition is true.
719 SDValue Chain = Op.getOperand(0);
720 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000721 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000722
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000723 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
724
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000725 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000726 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000727 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000728
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000729 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000730 Mips::CondCode CC =
731 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000732 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000733
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000734 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000735 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000736}
737
738SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000739LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000740{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000741 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000742
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000743 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000744 if (Cond.getOpcode() != MipsISD::FPCmp)
745 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000746
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000747 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
748 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000749}
750
Dan Gohmand858e902010-04-17 15:26:15 +0000751SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
752 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +0000753 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000754 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000755 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000756
Eli Friedmane2c74082009-08-03 02:22:28 +0000757 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +0000758 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000759
Chris Lattnerb71b9092009-08-13 06:28:06 +0000760 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000761
Chris Lattnere3736f82009-08-13 05:41:27 +0000762 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000763 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
764 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000765 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +0000766 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
767 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000768 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +0000769 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000770 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000771 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
772 MipsII::MO_ABS_HI);
773 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
774 MipsII::MO_ABS_LO);
775 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
776 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000777 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000778 } else {
Devang Patel0d881da2010-07-06 22:08:15 +0000779 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000780 MipsII::MO_GOT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000781 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000782 DAG.getEntryNode(), GA, MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000783 false, false, 0);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000784 // On functions and global targets not internal linked only
785 // a load from got/GP is necessary for PIC to work.
Akira Hatanaka9777e7a2011-04-07 19:51:44 +0000786 if (!GV->hasInternalLinkage() &&
787 (!GV->hasLocalLinkage() || isa<Function>(GV)))
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000788 return ResNode;
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000789 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
790 MipsII::MO_ABS_LO);
791 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000793 }
794
Torok Edwinc23197a2009-07-14 16:55:14 +0000795 llvm_unreachable("Dont know how to handle GlobalAddress");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000796 return SDValue(0,0);
797}
798
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000799SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
800 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +0000801 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
802 // FIXME there isn't actually debug info here
803 DebugLoc dl = Op.getDebugLoc();
804
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000805 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +0000806 // %hi/%lo relocation
807 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
808 MipsII::MO_ABS_HI);
809 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
810 MipsII::MO_ABS_LO);
811 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
812 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
813 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000814 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +0000815
816 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
817 MipsII::MO_GOT);
818 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
819 MipsII::MO_ABS_LO);
820 SDValue Load = DAG.getLoad(MVT::i32, dl,
821 DAG.getEntryNode(), BAGOTOffset,
822 MachinePointerInfo(), false, false, 0);
823 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
824 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000825}
826
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000827SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000828LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000829{
Torok Edwinc23197a2009-07-14 16:55:14 +0000830 llvm_unreachable("TLS not implemented for MIPS.");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000831 return SDValue(); // Not reached
832}
833
834SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000835LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000836{
Dan Gohman475871a2008-07-27 21:46:04 +0000837 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000838 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +0000839 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000840 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000841 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000842 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000843
Owen Andersone50ed302009-08-10 22:56:29 +0000844 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000845 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000846
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000847 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
848
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +0000849 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +0000850 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000851 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000852 } else // Emit Load from Global Pointer
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000853 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
854 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000855 false, false, 0);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000856
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000857 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
858 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000859 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000860 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000861
862 return ResNode;
863}
864
Dan Gohman475871a2008-07-27 21:46:04 +0000865SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000866LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000867{
Dan Gohman475871a2008-07-27 21:46:04 +0000868 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000869 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +0000870 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +0000871 // FIXME there isn't actually debug info here
872 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000873
874 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000875 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000876 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000877 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000878 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +0000879 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
881 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000882 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000883
884 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000885 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000886 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000887 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000888 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000889 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
890 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000892 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000893 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000894 N->getOffset(), MipsII::MO_GOT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000895 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000896 CP, MachinePointerInfo::getConstantPool(),
897 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000898 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000899 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000900 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000901 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
902 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000903
904 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000905}
906
Dan Gohmand858e902010-04-17 15:26:15 +0000907SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +0000908 MachineFunction &MF = DAG.getMachineFunction();
909 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
910
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000911 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +0000912 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
913 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000914
915 // vastart just stores the address of the VarArgsFrameIndex slot into the
916 // memory location argument.
917 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +0000918 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
919 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +0000920 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000921}
922
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000923//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000924// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000925//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000926
927#include "MipsGenCallingConv.inc"
928
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000929//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000930// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000931// Mips O32 ABI rules:
932// ---
933// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000934// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000935// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000936// f64 - Only passed in two aliased f32 registers if no int reg has been used
937// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000938// not used, it must be shadowed. If only A3 is avaiable, shadow it and
939// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +0000940//
941// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000942//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000943
Duncan Sands1e96bab2010-11-04 10:49:57 +0000944static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000945 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000946 ISD::ArgFlagsTy ArgFlags, CCState &State) {
947
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000948 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000949
950 static const unsigned IntRegs[] = {
951 Mips::A0, Mips::A1, Mips::A2, Mips::A3
952 };
953 static const unsigned F32Regs[] = {
954 Mips::F12, Mips::F14
955 };
956 static const unsigned F64Regs[] = {
957 Mips::D6, Mips::D7
958 };
959
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +0000960 // Promote i8 and i16
961 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
962 LocVT = MVT::i32;
963 if (ArgFlags.isSExt())
964 LocInfo = CCValAssign::SExt;
965 else if (ArgFlags.isZExt())
966 LocInfo = CCValAssign::ZExt;
967 else
968 LocInfo = CCValAssign::AExt;
969 }
970
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +0000971 unsigned Reg;
972
Akira Hatanaka95b8ae12011-05-19 18:06:05 +0000973 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
974 // is true: function is vararg, argument is 3rd or higher, there is previous
975 // argument which is not f32 or f64.
976 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
977 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +0000978 unsigned OrigAlign = ArgFlags.getOrigAlign();
979 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +0000980
981 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +0000982 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +0000983 // If this is the first part of an i64 arg,
984 // the allocated register must be either A0 or A2.
985 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
986 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +0000987 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +0000988 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
989 // Allocate int register and shadow next int register. If first
990 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +0000991 Reg = State.AllocateReg(IntRegs, IntRegsSize);
992 if (Reg == Mips::A1 || Reg == Mips::A3)
993 Reg = State.AllocateReg(IntRegs, IntRegsSize);
994 State.AllocateReg(IntRegs, IntRegsSize);
995 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +0000996 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
997 // we are guaranteed to find an available float register
998 if (ValVT == MVT::f32) {
999 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1000 // Shadow int register
1001 State.AllocateReg(IntRegs, IntRegsSize);
1002 } else {
1003 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1004 // Shadow int registers
1005 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1006 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1007 State.AllocateReg(IntRegs, IntRegsSize);
1008 State.AllocateReg(IntRegs, IntRegsSize);
1009 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001010 } else
1011 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001012
Akira Hatanakad37776d2011-05-20 21:39:54 +00001013 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1014 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1015
1016 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001017 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001018 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001019 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001020
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001021 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001022}
1023
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001024//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001025// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001026//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001027
Dan Gohman98ca4f22009-08-05 01:29:28 +00001028/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001029/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001030/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001031SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001032MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001033 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001034 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001035 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001036 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001037 const SmallVectorImpl<ISD::InputArg> &Ins,
1038 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001039 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001040 // MIPs target does not yet support tail call optimization.
1041 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001042
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001043 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001044 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00001045 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001046 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001047 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001048
1049 // Analyze operands of the call, assigning locations to each operand.
1050 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001051 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1052 *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001053
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +00001054 if (Subtarget->isABI_O32()) {
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001055 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001056 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001057 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001058
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001059 // Get a count of how many bytes are to be pushed on the stack.
1060 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnere563bbc2008-10-11 22:08:30 +00001061 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001062
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001063 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001064 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1065 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakad37776d2011-05-20 21:39:54 +00001066 unsigned NextStackOffset = (Subtarget->isABI_EABI() ? 0 : 16);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001067
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001068 MipsFI->setHasCall();
1069
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001070 // Walk the register/memloc assignments, inserting copies/loads.
1071 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001072 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001073 CCValAssign &VA = ArgLocs[i];
1074
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001075 // Promote the value if needed.
1076 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001077 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001078 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001079 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001080 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001081 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001082 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001083 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1084 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001085 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1086 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001087 if (!Subtarget->isLittle())
1088 std::swap(Lo, Hi);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001089 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
1090 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
1091 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001092 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001093 }
1094 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00001095 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001096 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001097 break;
1098 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001099 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001100 break;
1101 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001102 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001103 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001104 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001105
1106 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001107 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001108 if (VA.isRegLoc()) {
1109 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00001110 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001111 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001112
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001113 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00001114 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001115
Chris Lattnere0b12152008-03-17 06:57:02 +00001116 // Create the frame index object for this incoming parameter
1117 // This guarantees that when allocating Local Area the firsts
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001118 // 16 bytes which are alwayes reserved won't be overwritten
1119 // if O32 ABI is used. For EABI the first address is zero.
Akira Hatanakad37776d2011-05-20 21:39:54 +00001120 NextStackOffset = VA.getLocMemOffset();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001121 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Akira Hatanakad37776d2011-05-20 21:39:54 +00001122 NextStackOffset, true);
1123 NextStackOffset += VA.getValVT().getSizeInBits()/8;
Chris Lattnere0b12152008-03-17 06:57:02 +00001124
Dan Gohman475871a2008-07-27 21:46:04 +00001125 SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00001126
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001127 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00001128 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00001129 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
1130 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001131 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001132 }
1133
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001134 // Transform all store nodes into one single node because all store
1135 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001136 if (!MemOpChains.empty())
1137 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001138 &MemOpChains[0], MemOpChains.size());
1139
Bill Wendling056292f2008-09-16 21:48:12 +00001140 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001141 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1142 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001143 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001144 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001145 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001146
1147 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001148 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
1149 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1150 getPointerTy(), 0,MipsII:: MO_GOT);
1151 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
1152 0, MipsII::MO_ABS_LO);
1153 } else {
1154 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1155 getPointerTy(), 0, OpFlag);
1156 }
1157
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001158 LoadSymAddr = true;
1159 }
1160 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001161 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001162 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001163 LoadSymAddr = true;
1164 }
1165
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00001166 SDValue InFlag;
1167
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001168 // Create nodes that load address of callee and copy it to T9
1169 if (IsPIC) {
1170 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001171 // Load callee address
1172 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, Chain, Callee,
1173 MachinePointerInfo::getGOT(),
1174 false, false, 0);
1175
1176 // Use GOT+LO if callee has internal linkage.
1177 if (CalleeLo.getNode()) {
1178 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
1179 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
1180 } else
1181 Callee = LoadValue;
1182
1183 // Use chain output from LoadValue
1184 Chain = LoadValue.getValue(1);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001185 }
1186
1187 // copy to T9
1188 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
1189 InFlag = Chain.getValue(1);
1190 Callee = DAG.getRegister(Mips::T9, MVT::i32);
1191 }
Bill Wendling056292f2008-09-16 21:48:12 +00001192
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00001193 // Build a sequence of copy-to-reg nodes chained together with token
1194 // chain and flag operands which copy the outgoing args into registers.
1195 // The InFlag in necessary since all emitted instructions must be
1196 // stuck together.
1197 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1198 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1199 RegsToPass[i].second, InFlag);
1200 InFlag = Chain.getValue(1);
1201 }
1202
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001203 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001204 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001205 //
1206 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001207 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00001208 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001209 Ops.push_back(Chain);
1210 Ops.push_back(Callee);
1211
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001212 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001213 // known live into the call.
1214 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1215 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1216 RegsToPass[i].second.getValueType()));
1217
Gabor Greifba36cb52008-08-28 21:40:38 +00001218 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001219 Ops.push_back(InFlag);
1220
Dale Johannesen33c960f2009-02-04 20:06:27 +00001221 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001222 InFlag = Chain.getValue(1);
1223
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001224 // Create a stack location to hold GP when PIC is used. This stack
1225 // location is used on function prologue to save GP and also after all
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001226 // emitted CALL's to restore GP.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001227 if (IsPIC) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001228 // Function can have an arbitrary number of calls, so
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001229 // hold the LastArgStackLoc with the biggest offset.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001230 int FI;
Akira Hatanakad37776d2011-05-20 21:39:54 +00001231 int MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1232
1233 if (MaxCallFrameSize < (int)NextStackOffset) {
1234 MipsFI->setMaxCallFrameSize(NextStackOffset);
1235
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001236 if (MipsFI->getGPStackOffset() == -1) {
Evan Chenged2ae132010-07-03 00:40:23 +00001237 FI = MFI->CreateFixedObject(4, 0, true);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001238 MipsFI->setGPFI(FI);
1239 }
Akira Hatanakad37776d2011-05-20 21:39:54 +00001240
1241 // $gp restore slot must be aligned.
1242 unsigned StackAlignment = TFL->getStackAlignment();
1243 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
1244 StackAlignment * StackAlignment;
1245 MipsFI->setGPStackOffset(NextStackOffset);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001246 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001247 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001248
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00001249 // Create the CALLSEQ_END node.
1250 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1251 DAG.getIntPtrConstant(0, true), InFlag);
1252 InFlag = Chain.getValue(1);
1253
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001254 // Handle result values, copying them out of physregs into vregs that we
1255 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001256 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
1257 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001258}
1259
Dan Gohman98ca4f22009-08-05 01:29:28 +00001260/// LowerCallResult - Lower the result values of a call into the
1261/// appropriate copies out of appropriate physical registers.
1262SDValue
1263MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001264 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001265 const SmallVectorImpl<ISD::InputArg> &Ins,
1266 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001267 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001268
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001269 // Assign locations to each value returned by this call.
1270 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001271 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001272 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001273
Dan Gohman98ca4f22009-08-05 01:29:28 +00001274 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001275
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001276 // Copy all of the result registers out of their specified physreg.
1277 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001278 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001279 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001280 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001281 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001282 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001283
Dan Gohman98ca4f22009-08-05 01:29:28 +00001284 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001285}
1286
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001287//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001288// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001289//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001290
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001291/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001292/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001293SDValue
1294MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001295 CallingConv::ID CallConv,
1296 bool isVarArg,
1297 const SmallVectorImpl<ISD::InputArg>
1298 &Ins,
1299 DebugLoc dl, SelectionDAG &DAG,
1300 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001301 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001302
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00001303 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001304 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001305 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001306
Dan Gohman1e93df62010-04-17 14:41:14 +00001307 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001308
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001309 // Used with vargs to acumulate store chains.
1310 std::vector<SDValue> OutChains;
1311
1312 // Keep track of the last register used for arguments
1313 unsigned ArgRegEnd = 0;
1314
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001315 // Assign locations to all of the incoming arguments.
1316 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001317 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1318 ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001319
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001320 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001321 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001322 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001323 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001324
Akira Hatanakad37776d2011-05-20 21:39:54 +00001325 unsigned NextStackOffset = (Subtarget->isABI_EABI() ? 0 : 16);
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001326 EVT LastRegArgValVT;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001327
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001328 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001329 CCValAssign &VA = ArgLocs[i];
1330
1331 // Arguments stored on registers
1332 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001333 EVT RegVT = VA.getLocVT();
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001334 ArgRegEnd = VA.getLocReg();
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001335 LastRegArgValVT = VA.getValVT();
Bill Wendling06b8c192008-07-09 05:55:53 +00001336 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001337
Owen Anderson825b72b2009-08-11 20:47:22 +00001338 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001339 RC = Mips::CPURegsRegisterClass;
1340 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001341 RC = Mips::FGR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001342 else if (RegVT == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001343 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001344 RC = Mips::AFGR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001345 } else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001346 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001347
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001348 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001349 // physical registers into virtual ones
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001350 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegEnd, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001351 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001352
1353 // If this is an 8 or 16-bit value, it has been passed promoted
1354 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001355 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001356 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00001357 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001358 if (VA.getLocInfo() == CCValAssign::SExt)
1359 Opcode = ISD::AssertSext;
1360 else if (VA.getLocInfo() == CCValAssign::ZExt)
1361 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00001362 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001363 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00001364 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00001365 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001366 }
1367
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001368 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001369 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001370 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
1371 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00001372 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001373 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001374 VA.getLocReg()+1, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001375 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001376 if (!Subtarget->isLittle())
1377 std::swap(ArgValue, ArgValue2);
1378 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
1379 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001380 }
1381 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001382
Dan Gohman98ca4f22009-08-05 01:29:28 +00001383 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001384 } else { // VA.isRegLoc()
1385
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001386 // sanity check
1387 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001388
1389 // The last argument is not a register anymore
1390 ArgRegEnd = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001391
1392 // The stack pointer offset is relative to the caller stack frame.
1393 // Since the real stack size is unknown here, a negative SPOffset
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001394 // is used so there's a way to adjust these offsets when the stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001395 // size get known (on EliminateFrameIndex). A dummy SPOffset is
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001396 // used instead of a direct negative address (which is recorded to
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001397 // be used on emitPrologue) to avoid mis-calc of the first stack
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001398 // offset on PEI::calculateFrameObjectOffsets.
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001399 unsigned ArgSize = VA.getValVT().getSizeInBits()/8;
Akira Hatanakad37776d2011-05-20 21:39:54 +00001400 NextStackOffset = VA.getLocMemOffset() + ArgSize;
Evan Chenged2ae132010-07-03 00:40:23 +00001401 int FI = MFI->CreateFixedObject(ArgSize, 0, true);
Akira Hatanakad37776d2011-05-20 21:39:54 +00001402 MipsFI->recordLoadArgsFI(FI, -(4 + VA.getLocMemOffset()));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001403
1404 // Create load nodes to retrieve arguments from the stack
Dan Gohman475871a2008-07-27 21:46:04 +00001405 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001406 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
1407 MachinePointerInfo::getFixedStack(FI),
David Greenef6fa1862010-02-15 16:56:10 +00001408 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001409 }
1410 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001411
1412 // The mips ABIs for returning structs by value requires that we copy
1413 // the sret argument into $v0 for the return. Save the argument into
1414 // a virtual register so that we can access it from the return points.
1415 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1416 unsigned Reg = MipsFI->getSRetReturnReg();
1417 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001418 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001419 MipsFI->setSRetReturnReg(Reg);
1420 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001421 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001422 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001423 }
1424
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001425 // To meet ABI, when VARARGS are passed on registers, the registers
1426 // must have their values written to the caller stack frame. If the last
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001427 // argument was placed in the stack, there's no need to save any register.
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001428 if (isVarArg && Subtarget->isABI_O32()) {
1429 if (ArgRegEnd) {
1430 // Last named formal argument is passed in register.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001431
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001432 // The last register argument that must be saved is Mips::A3
1433 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
1434 if (LastRegArgValVT == MVT::f64)
1435 ArgRegEnd++;
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001436
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001437 if (ArgRegEnd < Mips::A3) {
1438 // Both the last named formal argument and the first variable
1439 // argument are passed in registers.
1440 for (++ArgRegEnd; ArgRegEnd <= Mips::A3; ++ArgRegEnd) {
1441 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegEnd, RC);
1442 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001443
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001444 int FI = MFI->CreateFixedObject(4, 0, true);
1445 MipsFI->recordStoreVarArgsFI(FI, -(4+(ArgRegEnd-Mips::A0)*4));
1446 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
1447 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
1448 MachinePointerInfo(),
1449 false, false, 0));
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001450
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001451 // Record the frame index of the first variable argument
1452 // which is a value necessary to VASTART.
1453 if (!MipsFI->getVarArgsFrameIndex()) {
1454 MFI->setObjectAlignment(FI, 4);
1455 MipsFI->setVarArgsFrameIndex(FI);
1456 }
1457 }
1458 } else {
1459 // Last named formal argument is in register Mips::A3, and the first
1460 // variable argument is on stack. Record the frame index of the first
1461 // variable argument.
1462 int FI = MFI->CreateFixedObject(4, 0, true);
1463 MFI->setObjectAlignment(FI, 4);
1464 MipsFI->recordStoreVarArgsFI(FI, -20);
Dan Gohman1e93df62010-04-17 14:41:14 +00001465 MipsFI->setVarArgsFrameIndex(FI);
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001466 }
1467 } else {
1468 // Last named formal argument and all the variable arguments are passed
1469 // on stack. Record the frame index of the first variable argument.
1470 int FI = MFI->CreateFixedObject(4, 0, true);
1471 MFI->setObjectAlignment(FI, 4);
Akira Hatanakad37776d2011-05-20 21:39:54 +00001472 MipsFI->recordStoreVarArgsFI(FI, -(4+NextStackOffset));
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001473 MipsFI->setVarArgsFrameIndex(FI);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001474 }
1475 }
1476
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001477 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001478 // the size of Ins and InVals. This only happens when on varg functions
1479 if (!OutChains.empty()) {
1480 OutChains.push_back(Chain);
1481 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1482 &OutChains[0], OutChains.size());
1483 }
1484
Dan Gohman98ca4f22009-08-05 01:29:28 +00001485 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001486}
1487
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001488//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001489// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001490//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001491
Dan Gohman98ca4f22009-08-05 01:29:28 +00001492SDValue
1493MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001494 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001495 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001496 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001497 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001498
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001499 // CCValAssign - represent the assignment of
1500 // the return value to a location
1501 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001502
1503 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001504 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1505 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001506
Dan Gohman98ca4f22009-08-05 01:29:28 +00001507 // Analize return values.
1508 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001509
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001510 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001511 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001512 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001513 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001514 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001515 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001516 }
1517
Dan Gohman475871a2008-07-27 21:46:04 +00001518 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001519
1520 // Copy the result values into the output registers.
1521 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1522 CCValAssign &VA = RVLocs[i];
1523 assert(VA.isRegLoc() && "Can only return in registers!");
1524
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001525 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001526 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001527
1528 // guarantee that all emitted copies are
1529 // stuck together, avoiding something bad
1530 Flag = Chain.getValue(1);
1531 }
1532
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001533 // The mips ABIs for returning structs by value requires that we copy
1534 // the sret argument into $v0 for the return. We saved the argument into
1535 // a virtual register in the entry block, so now we copy the value out
1536 // and into $v0.
1537 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1538 MachineFunction &MF = DAG.getMachineFunction();
1539 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1540 unsigned Reg = MipsFI->getSRetReturnReg();
1541
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001542 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00001543 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00001544 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001545
Dale Johannesena05dca42009-02-04 23:02:30 +00001546 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001547 Flag = Chain.getValue(1);
1548 }
1549
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001550 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00001551 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001552 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001553 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001554 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001555 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001556 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001557}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001558
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001559//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001560// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001561//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001562
1563/// getConstraintType - Given a constraint letter, return the type of
1564/// constraint it is for this target.
1565MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001566getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001567{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001568 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001569 // GCC config/mips/constraints.md
1570 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001571 // 'd' : An address register. Equivalent to r
1572 // unless generating MIPS16 code.
1573 // 'y' : Equivalent to r; retained for
1574 // backwards compatibility.
1575 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001576 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001577 switch (Constraint[0]) {
1578 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001579 case 'd':
1580 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001581 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001582 return C_RegisterClass;
1583 break;
1584 }
1585 }
1586 return TargetLowering::getConstraintType(Constraint);
1587}
1588
John Thompson44ab89e2010-10-29 17:29:13 +00001589/// Examine constraint type and operand type and determine a weight value.
1590/// This object must already have been set up with the operand type
1591/// and the current alternative constraint selected.
1592TargetLowering::ConstraintWeight
1593MipsTargetLowering::getSingleConstraintMatchWeight(
1594 AsmOperandInfo &info, const char *constraint) const {
1595 ConstraintWeight weight = CW_Invalid;
1596 Value *CallOperandVal = info.CallOperandVal;
1597 // If we don't have a value, we can't do a match,
1598 // but allow it at the lowest weight.
1599 if (CallOperandVal == NULL)
1600 return CW_Default;
1601 const Type *type = CallOperandVal->getType();
1602 // Look at the constraint type.
1603 switch (*constraint) {
1604 default:
1605 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
1606 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001607 case 'd':
1608 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00001609 if (type->isIntegerTy())
1610 weight = CW_Register;
1611 break;
1612 case 'f':
1613 if (type->isFloatTy())
1614 weight = CW_Register;
1615 break;
1616 }
1617 return weight;
1618}
1619
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001620/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1621/// return a list of registers that can be used to satisfy the constraint.
1622/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001623std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00001624getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001625{
1626 if (Constraint.size() == 1) {
1627 switch (Constraint[0]) {
1628 case 'r':
1629 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001630 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001631 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001632 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001633 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001634 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1635 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001636 }
1637 }
1638 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1639}
1640
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001641/// Given a register class constraint, like 'r', if this corresponds directly
1642/// to an LLVM register class, return a register of 0 and the register class
1643/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001644std::vector<unsigned> MipsTargetLowering::
1645getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001646 EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001647{
1648 if (Constraint.size() != 1)
1649 return std::vector<unsigned>();
1650
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001651 switch (Constraint[0]) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001652 default : break;
1653 case 'r':
1654 // GCC Mips Constraint Letters
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001655 case 'd':
1656 case 'y':
1657 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
1658 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
1659 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001660 Mips::T8, 0);
1661
1662 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001663 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001664 if (Subtarget->isSingleFloat())
1665 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1666 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1667 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1668 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1669 Mips::F30, Mips::F31, 0);
1670 else
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001671 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
1672 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001673 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +00001674 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001675
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001676 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001677 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001678 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
1679 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001680 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001681 }
1682 return std::vector<unsigned>();
1683}
Dan Gohman6520e202008-10-18 02:06:02 +00001684
1685bool
1686MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1687 // The Mips target isn't yet aware of offsets.
1688 return false;
1689}
Evan Chengeb2f9692009-10-27 19:56:55 +00001690
Evan Chenga1eaa3c2009-10-28 01:43:28 +00001691bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
1692 if (VT != MVT::f32 && VT != MVT::f64)
1693 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00001694 if (Imm.isNegZero())
1695 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00001696 return Imm.isZero();
1697}