blob: 1ee51722da044e457872fe0cc8391d91355e12f9 [file] [log] [blame]
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014
15#define DEBUG_TYPE "mips-lower"
Akira Hatanakab4d8d312011-05-24 00:23:52 +000016//#include <algorithm>
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000017#include "MipsISelLowering.h"
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +000018#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000019#include "MipsTargetMachine.h"
Chris Lattnerb71b9092009-08-13 06:28:06 +000020#include "MipsTargetObjectFile.h"
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000021#include "MipsSubtarget.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000022#include "llvm/DerivedTypes.h"
23#include "llvm/Function.h"
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +000024#include "llvm/GlobalVariable.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000025#include "llvm/Intrinsics.h"
26#include "llvm/CallingConv.h"
27#include "llvm/CodeGen/CallingConvLower.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000032#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033#include "llvm/CodeGen/ValueTypes.h"
34#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000036using namespace llvm;
37
Chris Lattnerf0144122009-07-28 03:13:23 +000038const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
39 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000040 case MipsISD::JmpLink: return "MipsISD::JmpLink";
41 case MipsISD::Hi: return "MipsISD::Hi";
42 case MipsISD::Lo: return "MipsISD::Lo";
43 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +000044 case MipsISD::TlsGd: return "MipsISD::TlsGd";
45 case MipsISD::TprelHi: return "MipsISD::TprelHi";
46 case MipsISD::TprelLo: return "MipsISD::TprelLo";
47 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000048 case MipsISD::Ret: return "MipsISD::Ret";
49 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
50 case MipsISD::FPCmp: return "MipsISD::FPCmp";
51 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
52 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
53 case MipsISD::FPRound: return "MipsISD::FPRound";
54 case MipsISD::MAdd: return "MipsISD::MAdd";
55 case MipsISD::MAddu: return "MipsISD::MAddu";
56 case MipsISD::MSub: return "MipsISD::MSub";
57 case MipsISD::MSubu: return "MipsISD::MSubu";
58 case MipsISD::DivRem: return "MipsISD::DivRem";
59 case MipsISD::DivRemU: return "MipsISD::DivRemU";
60 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
61 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanaka342837d2011-05-28 01:07:07 +000062 case MipsISD::WrapperPIC: return "MipsISD::WrapperPIC";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000063 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000064 }
65}
66
67MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000068MipsTargetLowering(MipsTargetMachine &TM)
Chris Lattnerb71b9092009-08-13 06:28:06 +000069 : TargetLowering(TM, new MipsTargetObjectFile()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000070 Subtarget = &TM.getSubtarget<MipsSubtarget>();
71
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000072 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000073 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000074 setBooleanContents(ZeroOrOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000075
76 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000077 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
78 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000079
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000080 // When dealing with single precision only, use libcalls
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000081 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000082 if (!Subtarget->isFP64bit())
Owen Anderson825b72b2009-08-11 20:47:22 +000083 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000084
Wesley Peckbf17cfa2010-11-23 03:31:01 +000085 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +000086 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
87 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
88 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000089
Eli Friedman6055a6a2009-07-17 04:07:24 +000090 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +000091 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
92 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +000093
Wesley Peckbf17cfa2010-11-23 03:31:01 +000094 // Used by legalize types to correctly generate the setcc result.
95 // Without this, every float setcc comes with a AND/OR with the result,
96 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000097 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +000098 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000099
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000100 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +0000101 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000102 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000103 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
104 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
105 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
106 setOperationAction(ISD::SELECT, MVT::f32, Custom);
107 setOperationAction(ISD::SELECT, MVT::f64, Custom);
108 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000109 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
110 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000111 setOperationAction(ISD::VASTART, MVT::Other, Custom);
112
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000113 setOperationAction(ISD::SDIV, MVT::i32, Expand);
114 setOperationAction(ISD::SREM, MVT::i32, Expand);
115 setOperationAction(ISD::UDIV, MVT::i32, Expand);
116 setOperationAction(ISD::UREM, MVT::i32, Expand);
117
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000118 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000119 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
120 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
121 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
122 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
123 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
125 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
126 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
127 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000128
129 if (!Subtarget->isMips32r2())
130 setOperationAction(ISD::ROTR, MVT::i32, Expand);
131
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
133 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
134 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000135 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
136 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000137 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000138 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000140 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000141 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
142 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000143 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000144 setOperationAction(ISD::FLOG, MVT::f32, Expand);
145 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
146 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
147 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000148
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000149 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
150 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
151
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000152 setOperationAction(ISD::VAARG, MVT::Other, Expand);
153 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
154 setOperationAction(ISD::VAEND, MVT::Other, Expand);
155
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000156 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
158 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
159 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000160
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000161 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000162 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000163
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000164 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
166 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000167 }
168
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000169 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000170 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000171
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000172 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000173 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000174
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000175 setTargetDAGCombine(ISD::ADDE);
176 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000177 setTargetDAGCombine(ISD::SDIVREM);
178 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000179 setTargetDAGCombine(ISD::SETCC);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000180
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000181 setMinFunctionAlignment(2);
182
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000183 setStackPointerRegisterToSaveRestore(Mips::SP);
184 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000185
186 setExceptionPointerRegister(Mips::A0);
187 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000188}
189
Owen Anderson825b72b2009-08-11 20:47:22 +0000190MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
191 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000192}
193
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000194// SelectMadd -
195// Transforms a subgraph in CurDAG if the following pattern is found:
196// (addc multLo, Lo0), (adde multHi, Hi0),
197// where,
198// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000199// Lo0: initial value of Lo register
200// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000201// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000202static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000203 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000204 // for the matching to be successful.
205 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
206
207 if (ADDCNode->getOpcode() != ISD::ADDC)
208 return false;
209
210 SDValue MultHi = ADDENode->getOperand(0);
211 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000212 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000213 unsigned MultOpc = MultHi.getOpcode();
214
215 // MultHi and MultLo must be generated by the same node,
216 if (MultLo.getNode() != MultNode)
217 return false;
218
219 // and it must be a multiplication.
220 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
221 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000222
223 // MultLo amd MultHi must be the first and second output of MultNode
224 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000225 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
226 return false;
227
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000228 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000229 // of the values of MultNode, in which case MultNode will be removed in later
230 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000231 // If there exist users other than ADDENode or ADDCNode, this function returns
232 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000233 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000234 // produced.
235 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
236 return false;
237
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000238 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000239 DebugLoc dl = ADDENode->getDebugLoc();
240
241 // create MipsMAdd(u) node
242 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000243
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000244 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
245 MVT::Glue,
246 MultNode->getOperand(0),// Factor 0
247 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000248 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000249 ADDENode->getOperand(1));// Hi0
250
251 // create CopyFromReg nodes
252 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
253 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000254 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000255 Mips::HI, MVT::i32,
256 CopyFromLo.getValue(2));
257
258 // replace uses of adde and addc here
259 if (!SDValue(ADDCNode, 0).use_empty())
260 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
261
262 if (!SDValue(ADDENode, 0).use_empty())
263 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
264
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000265 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000266}
267
268// SelectMsub -
269// Transforms a subgraph in CurDAG if the following pattern is found:
270// (addc Lo0, multLo), (sube Hi0, multHi),
271// where,
272// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000273// Lo0: initial value of Lo register
274// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000275// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000276static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000277 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000278 // for the matching to be successful.
279 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
280
281 if (SUBCNode->getOpcode() != ISD::SUBC)
282 return false;
283
284 SDValue MultHi = SUBENode->getOperand(1);
285 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000286 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000287 unsigned MultOpc = MultHi.getOpcode();
288
289 // MultHi and MultLo must be generated by the same node,
290 if (MultLo.getNode() != MultNode)
291 return false;
292
293 // and it must be a multiplication.
294 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
295 return false;
296
297 // MultLo amd MultHi must be the first and second output of MultNode
298 // respectively.
299 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
300 return false;
301
302 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
303 // of the values of MultNode, in which case MultNode will be removed in later
304 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000305 // If there exist users other than SUBENode or SUBCNode, this function returns
306 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000307 // instruction node rather than a pair of MULT and MSUB instructions being
308 // produced.
309 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
310 return false;
311
312 SDValue Chain = CurDAG->getEntryNode();
313 DebugLoc dl = SUBENode->getDebugLoc();
314
315 // create MipsSub(u) node
316 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
317
318 SDValue MSub = CurDAG->getNode(MultOpc, dl,
319 MVT::Glue,
320 MultNode->getOperand(0),// Factor 0
321 MultNode->getOperand(1),// Factor 1
322 SUBCNode->getOperand(0),// Lo0
323 SUBENode->getOperand(0));// Hi0
324
325 // create CopyFromReg nodes
326 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
327 MSub);
328 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
329 Mips::HI, MVT::i32,
330 CopyFromLo.getValue(2));
331
332 // replace uses of sube and subc here
333 if (!SDValue(SUBCNode, 0).use_empty())
334 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
335
336 if (!SDValue(SUBENode, 0).use_empty())
337 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
338
339 return true;
340}
341
342static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
343 TargetLowering::DAGCombinerInfo &DCI,
344 const MipsSubtarget* Subtarget) {
345 if (DCI.isBeforeLegalize())
346 return SDValue();
347
348 if (Subtarget->isMips32() && SelectMadd(N, &DAG))
349 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000350
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000351 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000352}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000353
354static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
355 TargetLowering::DAGCombinerInfo &DCI,
356 const MipsSubtarget* Subtarget) {
357 if (DCI.isBeforeLegalize())
358 return SDValue();
359
360 if (Subtarget->isMips32() && SelectMsub(N, &DAG))
361 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000362
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000363 return SDValue();
364}
365
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000366static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
367 TargetLowering::DAGCombinerInfo &DCI,
368 const MipsSubtarget* Subtarget) {
369 if (DCI.isBeforeLegalizeOps())
370 return SDValue();
371
372 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
373 MipsISD::DivRemU;
374 DebugLoc dl = N->getDebugLoc();
375
376 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
377 N->getOperand(0), N->getOperand(1));
378 SDValue InChain = DAG.getEntryNode();
379 SDValue InGlue = DivRem;
380
381 // insert MFLO
382 if (N->hasAnyUseOfValue(0)) {
383 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, Mips::LO, MVT::i32,
384 InGlue);
385 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
386 InChain = CopyFromLo.getValue(1);
387 InGlue = CopyFromLo.getValue(2);
388 }
389
390 // insert MFHI
391 if (N->hasAnyUseOfValue(1)) {
392 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000393 Mips::HI, MVT::i32, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000394 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
395 }
396
397 return SDValue();
398}
399
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000400static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
401 switch (CC) {
402 default: llvm_unreachable("Unknown fp condition code!");
403 case ISD::SETEQ:
404 case ISD::SETOEQ: return Mips::FCOND_OEQ;
405 case ISD::SETUNE: return Mips::FCOND_UNE;
406 case ISD::SETLT:
407 case ISD::SETOLT: return Mips::FCOND_OLT;
408 case ISD::SETGT:
409 case ISD::SETOGT: return Mips::FCOND_OGT;
410 case ISD::SETLE:
411 case ISD::SETOLE: return Mips::FCOND_OLE;
412 case ISD::SETGE:
413 case ISD::SETOGE: return Mips::FCOND_OGE;
414 case ISD::SETULT: return Mips::FCOND_ULT;
415 case ISD::SETULE: return Mips::FCOND_ULE;
416 case ISD::SETUGT: return Mips::FCOND_UGT;
417 case ISD::SETUGE: return Mips::FCOND_UGE;
418 case ISD::SETUO: return Mips::FCOND_UN;
419 case ISD::SETO: return Mips::FCOND_OR;
420 case ISD::SETNE:
421 case ISD::SETONE: return Mips::FCOND_ONE;
422 case ISD::SETUEQ: return Mips::FCOND_UEQ;
423 }
424}
425
426
427// Returns true if condition code has to be inverted.
428static bool InvertFPCondCode(Mips::CondCode CC) {
429 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
430 return false;
431
432 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
433 return true;
434
435 assert(false && "Illegal Condition Code");
436 return false;
437}
438
439// Creates and returns an FPCmp node from a setcc node.
440// Returns Op if setcc is not a floating point comparison.
441static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
442 // must be a SETCC node
443 if (Op.getOpcode() != ISD::SETCC)
444 return Op;
445
446 SDValue LHS = Op.getOperand(0);
447
448 if (!LHS.getValueType().isFloatingPoint())
449 return Op;
450
451 SDValue RHS = Op.getOperand(1);
452 DebugLoc dl = Op.getDebugLoc();
453
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000454 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
455 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000456 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
457
458 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
459 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
460}
461
462// Creates and returns a CMovFPT/F node.
463static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
464 SDValue False, DebugLoc DL) {
465 bool invert = InvertFPCondCode((Mips::CondCode)
466 cast<ConstantSDNode>(Cond.getOperand(2))
467 ->getSExtValue());
468
469 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
470 True.getValueType(), True, False, Cond);
471}
472
473static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
474 TargetLowering::DAGCombinerInfo &DCI,
475 const MipsSubtarget* Subtarget) {
476 if (DCI.isBeforeLegalizeOps())
477 return SDValue();
478
479 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
480
481 if (Cond.getOpcode() != MipsISD::FPCmp)
482 return SDValue();
483
484 SDValue True = DAG.getConstant(1, MVT::i32);
485 SDValue False = DAG.getConstant(0, MVT::i32);
486
487 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
488}
489
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000490SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000491 const {
492 SelectionDAG &DAG = DCI.DAG;
493 unsigned opc = N->getOpcode();
494
495 switch (opc) {
496 default: break;
497 case ISD::ADDE:
498 return PerformADDECombine(N, DAG, DCI, Subtarget);
499 case ISD::SUBE:
500 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000501 case ISD::SDIVREM:
502 case ISD::UDIVREM:
503 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000504 case ISD::SETCC:
505 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000506 }
507
508 return SDValue();
509}
510
Dan Gohman475871a2008-07-27 21:46:04 +0000511SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000512LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000513{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000514 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000515 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000516 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000517 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
518 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000519 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000520 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000521 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
522 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000523 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000524 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000525 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000526 }
Dan Gohman475871a2008-07-27 21:46:04 +0000527 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000528}
529
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000530//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000531// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000532//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000533
534// AddLiveIn - This helper function adds the specified physical register to the
535// MachineFunction as a live in value. It also creates a corresponding
536// virtual register for it.
537static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000538AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000539{
540 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000541 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
542 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000543 return VReg;
544}
545
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000546// Get fp branch code (not opcode) from condition code.
547static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
548 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
549 return Mips::BRANCH_T;
550
551 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
552 return Mips::BRANCH_F;
553
554 return Mips::BRANCH_INVALID;
555}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000556
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000557MachineBasicBlock *
558MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000559 MachineBasicBlock *BB) const {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000560 // There is no need to expand CMov instructions if target has
561 // conditional moves.
562 if (Subtarget->hasCondMov())
563 return BB;
564
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000565 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
566 bool isFPCmp = false;
Dale Johannesen94817572009-02-13 02:34:39 +0000567 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000568 unsigned Opc;
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000569
570 switch (MI->getOpcode()) {
571 default: assert(false && "Unexpected instr type to insert");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000572 case Mips::MOVT:
573 case Mips::MOVT_S:
574 case Mips::MOVT_D:
575 isFPCmp = true;
576 Opc = Mips::BC1F;
577 break;
578 case Mips::MOVF:
579 case Mips::MOVF_S:
580 case Mips::MOVF_D:
581 isFPCmp = true;
582 Opc = Mips::BC1T;
583 break;
584 case Mips::MOVZ_I:
585 case Mips::MOVZ_S:
586 case Mips::MOVZ_D:
587 Opc = Mips::BNE;
588 break;
589 case Mips::MOVN_I:
590 case Mips::MOVN_S:
591 case Mips::MOVN_D:
592 Opc = Mips::BEQ;
593 break;
594 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000595
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000596 // To "insert" a SELECT_CC instruction, we actually have to insert the
597 // diamond control-flow pattern. The incoming instruction knows the
598 // destination vreg to set, the condition code register to branch on, the
599 // true/false values to select between, and a branch opcode to use.
600 const BasicBlock *LLVM_BB = BB->getBasicBlock();
601 MachineFunction::iterator It = BB;
602 ++It;
Dan Gohman14152b42010-07-06 20:24:04 +0000603
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000604 // thisMBB:
605 // ...
606 // TrueVal = ...
607 // setcc r1, r2, r3
608 // bNE r1, r0, copy1MBB
609 // fallthrough --> copy0MBB
610 MachineBasicBlock *thisMBB = BB;
611 MachineFunction *F = BB->getParent();
612 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
613 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
614 F->insert(It, copy0MBB);
615 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +0000616
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000617 // Transfer the remainder of BB and its successor edges to sinkMBB.
618 sinkMBB->splice(sinkMBB->begin(), BB,
619 llvm::next(MachineBasicBlock::iterator(MI)),
620 BB->end());
621 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000622
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000623 // Next, add the true and fallthrough blocks as its successors.
624 BB->addSuccessor(copy0MBB);
625 BB->addSuccessor(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000626
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000627 // Emit the right instruction according to the type of the operands compared
628 if (isFPCmp)
629 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
630 else
631 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
632 .addReg(Mips::ZERO).addMBB(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000633
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000634
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000635 // copy0MBB:
636 // %FalseValue = ...
637 // # fallthrough to sinkMBB
638 BB = copy0MBB;
639
640 // Update machine-CFG edges
641 BB->addSuccessor(sinkMBB);
642
643 // sinkMBB:
644 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
645 // ...
646 BB = sinkMBB;
647
648 if (isFPCmp)
Dan Gohman14152b42010-07-06 20:24:04 +0000649 BuildMI(*BB, BB->begin(), dl,
650 TII->get(Mips::PHI), MI->getOperand(0).getReg())
Bruno Cardoso Lopes29e9daa2010-07-20 07:58:51 +0000651 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000652 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
653 else
654 BuildMI(*BB, BB->begin(), dl,
655 TII->get(Mips::PHI), MI->getOperand(0).getReg())
656 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
657 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000658
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000659 MI->eraseFromParent(); // The pseudo instruction is gone now.
660 return BB;
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000661}
662
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000663//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000664// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000665//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000666SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000667LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000668{
Akira Hatanaka053546c2011-05-25 02:20:00 +0000669 unsigned StackAlignment =
670 getTargetMachine().getFrameLowering()->getStackAlignment();
671 assert(StackAlignment >=
672 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
673 "Cannot lower if the alignment of the allocated space is larger than \
674 that of the stack.");
675
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000676 SDValue Chain = Op.getOperand(0);
677 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +0000678 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000679
680 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000682
683 // Subtract the dynamic size from the actual stack size to
684 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000686
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000687 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000688 // must be placed in the stack pointer register.
Akira Hatanaka053546c2011-05-25 02:20:00 +0000689 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
690 SDValue());
Akira Hatanakaedacba82011-05-25 17:32:06 +0000691 // Retrieve updated $sp. There is a glue input to prevent instructions that
692 // clobber $sp from being inserted between copytoreg and copyfromreg.
Akira Hatanaka053546c2011-05-25 02:20:00 +0000693 SDValue NewSP = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32,
694 Chain.getValue(1));
695
Akira Hatanakaedacba82011-05-25 17:32:06 +0000696 // The stack space reserved by alloca is located right above the argument
697 // area. It is aligned on a boundary that is a multiple of StackAlignment.
Akira Hatanaka053546c2011-05-25 02:20:00 +0000698 MachineFunction &MF = DAG.getMachineFunction();
699 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
700 unsigned SPOffset = (MipsFI->getMaxCallFrameSize() + StackAlignment - 1) /
701 StackAlignment * StackAlignment;
702 SDValue AllocPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
703 DAG.getConstant(SPOffset, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000704
705 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000706 // value and a chain
Akira Hatanaka053546c2011-05-25 02:20:00 +0000707 SDValue Ops[2] = { AllocPtr, NewSP.getValue(1) };
Dale Johannesena05dca42009-02-04 23:02:30 +0000708 return DAG.getMergeValues(Ops, 2, dl);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000709}
710
711SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000712LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000713{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000714 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000715 // the block to branch to if the condition is true.
716 SDValue Chain = Op.getOperand(0);
717 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000718 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000719
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000720 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
721
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000722 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000723 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000724 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000725
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000726 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000727 Mips::CondCode CC =
728 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000729 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000730
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000731 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000732 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000733}
734
735SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000736LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000737{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000738 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000739
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000740 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000741 if (Cond.getOpcode() != MipsISD::FPCmp)
742 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000743
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000744 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
745 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000746}
747
Dan Gohmand858e902010-04-17 15:26:15 +0000748SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
749 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +0000750 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000751 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000752 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000753
Eli Friedmane2c74082009-08-03 02:22:28 +0000754 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +0000755 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000756
Chris Lattnerb71b9092009-08-13 06:28:06 +0000757 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000758
Chris Lattnere3736f82009-08-13 05:41:27 +0000759 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000760 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
761 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000762 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +0000763 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
764 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000765 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +0000766 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000767 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000768 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
769 MipsII::MO_ABS_HI);
770 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
771 MipsII::MO_ABS_LO);
772 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
773 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000774 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000775 } else {
Devang Patel0d881da2010-07-06 22:08:15 +0000776 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000777 MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +0000778 GA = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, GA);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000779 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000780 DAG.getEntryNode(), GA, MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000781 false, false, 0);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000782 // On functions and global targets not internal linked only
783 // a load from got/GP is necessary for PIC to work.
Akira Hatanaka9777e7a2011-04-07 19:51:44 +0000784 if (!GV->hasInternalLinkage() &&
785 (!GV->hasLocalLinkage() || isa<Function>(GV)))
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000786 return ResNode;
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000787 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
788 MipsII::MO_ABS_LO);
789 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000791 }
792
Torok Edwinc23197a2009-07-14 16:55:14 +0000793 llvm_unreachable("Dont know how to handle GlobalAddress");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000794 return SDValue(0,0);
795}
796
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000797SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
798 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +0000799 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
800 // FIXME there isn't actually debug info here
801 DebugLoc dl = Op.getDebugLoc();
802
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000803 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +0000804 // %hi/%lo relocation
805 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
806 MipsII::MO_ABS_HI);
807 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
808 MipsII::MO_ABS_LO);
809 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
810 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
811 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000812 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +0000813
814 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
815 MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +0000816 BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, BAGOTOffset);
Akira Hatanakaf48eb532011-04-25 17:10:45 +0000817 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
818 MipsII::MO_ABS_LO);
819 SDValue Load = DAG.getLoad(MVT::i32, dl,
820 DAG.getEntryNode(), BAGOTOffset,
821 MachinePointerInfo(), false, false, 0);
822 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
823 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000824}
825
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000826SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000827LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000828{
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000829 // If the relocation model is PIC, use the General Dynamic TLS Model,
830 // otherwise use the Initial Exec or Local Exec TLS Model.
831 // TODO: implement Local Dynamic TLS model
832
833 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
834 DebugLoc dl = GA->getDebugLoc();
835 const GlobalValue *GV = GA->getGlobal();
836 EVT PtrVT = getPointerTy();
837
838 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
839 // General Dynamic TLS Model
840 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32,
841 0, MipsII::MO_TLSGD);
842 SDValue Tlsgd = DAG.getNode(MipsISD::TlsGd, dl, MVT::i32, TGA);
843 SDValue GP = DAG.getRegister(Mips::GP, MVT::i32);
844 SDValue Argument = DAG.getNode(ISD::ADD, dl, MVT::i32, GP, Tlsgd);
845
846 ArgListTy Args;
847 ArgListEntry Entry;
848 Entry.Node = Argument;
849 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
850 Args.push_back(Entry);
851 std::pair<SDValue, SDValue> CallResult =
852 LowerCallTo(DAG.getEntryNode(),
853 (const Type *) Type::getInt32Ty(*DAG.getContext()),
854 false, false, false, false,
855 0, CallingConv::C, false, true,
856 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
857
858 return CallResult.first;
859 } else {
860 SDValue Offset;
861 if (GV->isDeclaration()) {
862 // Initial Exec TLS Model
863 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
864 MipsII::MO_GOTTPREL);
865 Offset = DAG.getLoad(MVT::i32, dl,
866 DAG.getEntryNode(), TGA, MachinePointerInfo(),
867 false, false, 0);
868 } else {
869 // Local Exec TLS Model
870 SDVTList VTs = DAG.getVTList(MVT::i32);
871 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
872 MipsII::MO_TPREL_HI);
873 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
874 MipsII::MO_TPREL_LO);
875 SDValue Hi = DAG.getNode(MipsISD::TprelHi, dl, VTs, &TGAHi, 1);
876 SDValue Lo = DAG.getNode(MipsISD::TprelLo, dl, MVT::i32, TGALo);
877 Offset = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
878 }
879
880 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
881 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
882 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000883}
884
885SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000886LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000887{
Dan Gohman475871a2008-07-27 21:46:04 +0000888 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000889 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +0000890 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000891 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000892 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000893 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000894
Owen Andersone50ed302009-08-10 22:56:29 +0000895 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000896 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000897
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000898 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
899
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +0000900 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +0000901 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000902 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Akira Hatanaka342837d2011-05-28 01:07:07 +0000903 } else {// Emit Load from Global Pointer
904 JTI = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, JTI);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000905 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
906 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000907 false, false, 0);
Akira Hatanaka342837d2011-05-28 01:07:07 +0000908 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000909
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000910 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
911 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000912 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000913 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000914
915 return ResNode;
916}
917
Dan Gohman475871a2008-07-27 21:46:04 +0000918SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000919LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000920{
Dan Gohman475871a2008-07-27 21:46:04 +0000921 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000922 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +0000923 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +0000924 // FIXME there isn't actually debug info here
925 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000926
927 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000928 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000929 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000930 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000931 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +0000932 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000933 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
934 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000935 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000936
937 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000938 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000939 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000940 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000941 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000942 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
943 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000945 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000946 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000947 N->getOffset(), MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +0000948 CP = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000949 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000950 CP, MachinePointerInfo::getConstantPool(),
951 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000952 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000953 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000954 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000955 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
956 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000957
958 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000959}
960
Dan Gohmand858e902010-04-17 15:26:15 +0000961SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +0000962 MachineFunction &MF = DAG.getMachineFunction();
963 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
964
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000965 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +0000966 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
967 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000968
969 // vastart just stores the address of the VarArgsFrameIndex slot into the
970 // memory location argument.
971 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +0000972 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
973 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +0000974 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000975}
976
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000977static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
978 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
979 DebugLoc dl = Op.getDebugLoc();
980 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
981 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
982 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
983 DAG.getConstant(0x7fffffff, MVT::i32));
984 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
985 DAG.getConstant(0x80000000, MVT::i32));
986 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
987 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
988}
989
990static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
991 // FIXME:
992 // Use ext/ins instructions if target architecture is Mips32r2.
993 // Eliminate redundant mfc1 and mtc1 instructions.
994 unsigned LoIdx = 0, HiIdx = 1;
995
996 if (!isLittle)
997 std::swap(LoIdx, HiIdx);
998
999 DebugLoc dl = Op.getDebugLoc();
1000 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1001 Op.getOperand(0),
1002 DAG.getConstant(LoIdx, MVT::i32));
1003 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1004 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1005 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1006 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1007 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1008 DAG.getConstant(0x7fffffff, MVT::i32));
1009 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1010 DAG.getConstant(0x80000000, MVT::i32));
1011 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1012
1013 if (!isLittle)
1014 std::swap(Word0, Word1);
1015
1016 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1017}
1018
1019SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1020 const {
1021 EVT Ty = Op.getValueType();
1022
1023 assert(Ty == MVT::f32 || Ty == MVT::f64);
1024
1025 if (Ty == MVT::f32)
1026 return LowerFCOPYSIGN32(Op, DAG);
1027 else
1028 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
1029}
1030
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001031//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001032// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001033//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001034
1035#include "MipsGenCallingConv.inc"
1036
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001037//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001038// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001039// Mips O32 ABI rules:
1040// ---
1041// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001042// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001043// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001044// f64 - Only passed in two aliased f32 registers if no int reg has been used
1045// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001046// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1047// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001048//
1049// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001050//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001051
Duncan Sands1e96bab2010-11-04 10:49:57 +00001052static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001053 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001054 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1055
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001056 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001057
1058 static const unsigned IntRegs[] = {
1059 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1060 };
1061 static const unsigned F32Regs[] = {
1062 Mips::F12, Mips::F14
1063 };
1064 static const unsigned F64Regs[] = {
1065 Mips::D6, Mips::D7
1066 };
1067
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001068 // ByVal Args
1069 if (ArgFlags.isByVal()) {
1070 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1071 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1072 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1073 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1074 r < std::min(IntRegsSize, NextReg); ++r)
1075 State.AllocateReg(IntRegs[r]);
1076 return false;
1077 }
1078
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001079 // Promote i8 and i16
1080 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1081 LocVT = MVT::i32;
1082 if (ArgFlags.isSExt())
1083 LocInfo = CCValAssign::SExt;
1084 else if (ArgFlags.isZExt())
1085 LocInfo = CCValAssign::ZExt;
1086 else
1087 LocInfo = CCValAssign::AExt;
1088 }
1089
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001090 unsigned Reg;
1091
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001092 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1093 // is true: function is vararg, argument is 3rd or higher, there is previous
1094 // argument which is not f32 or f64.
1095 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1096 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001097 unsigned OrigAlign = ArgFlags.getOrigAlign();
1098 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001099
1100 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001101 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001102 // If this is the first part of an i64 arg,
1103 // the allocated register must be either A0 or A2.
1104 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1105 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001106 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001107 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1108 // Allocate int register and shadow next int register. If first
1109 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001110 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1111 if (Reg == Mips::A1 || Reg == Mips::A3)
1112 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1113 State.AllocateReg(IntRegs, IntRegsSize);
1114 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001115 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1116 // we are guaranteed to find an available float register
1117 if (ValVT == MVT::f32) {
1118 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1119 // Shadow int register
1120 State.AllocateReg(IntRegs, IntRegsSize);
1121 } else {
1122 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1123 // Shadow int registers
1124 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1125 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1126 State.AllocateReg(IntRegs, IntRegsSize);
1127 State.AllocateReg(IntRegs, IntRegsSize);
1128 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001129 } else
1130 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001131
Akira Hatanakad37776d2011-05-20 21:39:54 +00001132 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1133 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1134
1135 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001136 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001137 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001138 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001139
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001140 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001141}
1142
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001143//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001144// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001145//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001146
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001147static const unsigned O32IntRegsSize = 4;
1148
1149static const unsigned O32IntRegs[] = {
1150 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1151};
1152
1153// Write ByVal Arg to arg registers and stack.
1154static void
1155WriteByValArg(SDValue& Chain, DebugLoc dl,
1156 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1157 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1158 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001159 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
1160 MVT PtrType) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001161 unsigned FirstWord = VA.getLocMemOffset() / 4;
1162 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
1163 unsigned LastWord = FirstWord + NumWords;
1164 unsigned CurWord;
1165
1166 // copy the first 4 words of byval arg to registers A0 - A3
1167 for (CurWord = FirstWord; CurWord < std::min(LastWord, O32IntRegsSize);
1168 ++CurWord) {
1169 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1170 DAG.getConstant((CurWord - FirstWord) * 4,
1171 MVT::i32));
1172 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1173 MachinePointerInfo(),
1174 false, false, 0);
1175 MemOpChains.push_back(LoadVal.getValue(1));
1176 unsigned DstReg = O32IntRegs[CurWord];
1177 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1178 }
1179
1180 // copy remaining part of byval arg to stack.
1181 if (CurWord < LastWord) {
1182 unsigned SizeInBytes = (LastWord - CurWord) * 4;
1183 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1184 DAG.getConstant((CurWord - FirstWord) * 4,
1185 MVT::i32));
1186 LastFI = MFI->CreateFixedObject(SizeInBytes, CurWord * 4, true);
1187 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
1188 Chain = DAG.getMemcpy(Chain, dl, Dst, Src,
1189 DAG.getConstant(SizeInBytes, MVT::i32),
1190 /*Align*/4,
1191 /*isVolatile=*/false, /*AlwaysInline=*/false,
1192 MachinePointerInfo(0), MachinePointerInfo(0));
1193 MemOpChains.push_back(Chain);
1194 }
1195}
1196
Dan Gohman98ca4f22009-08-05 01:29:28 +00001197/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001198/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001199/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001200SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001201MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001202 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001203 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001204 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001205 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001206 const SmallVectorImpl<ISD::InputArg> &Ins,
1207 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001208 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001209 // MIPs target does not yet support tail call optimization.
1210 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001211
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001212 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001213 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00001214 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001215 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001216 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001217
1218 // Analyze operands of the call, assigning locations to each operand.
1219 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001220 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1221 *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001222
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001223 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001224 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001225 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001226 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001227
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001228 // Get a count of how many bytes are to be pushed on the stack.
1229 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnere563bbc2008-10-11 22:08:30 +00001230 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001231
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001232 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001233 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1234 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001235
Akira Hatanakaedacba82011-05-25 17:32:06 +00001236 // If this is the first call, create a stack frame object that points to
1237 // a location to which .cprestore saves $gp. The offset of this frame object
1238 // is set to 0, since we know nothing about the size of the argument area at
1239 // this point.
Akira Hatanaka69c19f72011-05-23 20:16:59 +00001240 if (IsPIC && !MipsFI->getGPFI())
Akira Hatanaka43299772011-05-20 23:22:14 +00001241 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1242
1243 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
1244
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001245 // Walk the register/memloc assignments, inserting copies/loads.
1246 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001247 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001248 CCValAssign &VA = ArgLocs[i];
1249
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001250 // Promote the value if needed.
1251 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001252 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001253 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001254 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001255 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001256 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001257 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001258 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1259 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001260 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1261 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001262 if (!Subtarget->isLittle())
1263 std::swap(Lo, Hi);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001264 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
1265 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
1266 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001267 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001268 }
1269 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00001270 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001271 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001272 break;
1273 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001274 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001275 break;
1276 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001277 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001278 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001279 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001280
1281 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001282 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001283 if (VA.isRegLoc()) {
1284 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00001285 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001286 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001287
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001288 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00001289 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001290
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001291 // ByVal Arg.
1292 ISD::ArgFlagsTy Flags = Outs[i].Flags;
1293 if (Flags.isByVal()) {
1294 assert(Subtarget->isABI_O32() &&
1295 "No support for ByVal args by ABIs other than O32 yet.");
1296 assert(Flags.getByValSize() &&
1297 "ByVal args of size 0 should have been ignored by front-end.");
1298 WriteByValArg(Chain, dl, RegsToPass, MemOpChains, LastFI, MFI, DAG, Arg,
1299 VA, Flags, getPointerTy());
1300 continue;
1301 }
1302
Chris Lattnere0b12152008-03-17 06:57:02 +00001303 // Create the frame index object for this incoming parameter
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001304 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
1305 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00001306 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00001307
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001308 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00001309 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00001310 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
1311 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001312 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001313 }
1314
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001315 // Transform all store nodes into one single node because all store
1316 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001317 if (!MemOpChains.empty())
1318 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001319 &MemOpChains[0], MemOpChains.size());
1320
Bill Wendling056292f2008-09-16 21:48:12 +00001321 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001322 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1323 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001324 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001325 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001326 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001327
1328 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001329 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
1330 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1331 getPointerTy(), 0,MipsII:: MO_GOT);
1332 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
1333 0, MipsII::MO_ABS_LO);
1334 } else {
1335 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1336 getPointerTy(), 0, OpFlag);
1337 }
1338
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001339 LoadSymAddr = true;
1340 }
1341 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001342 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001343 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001344 LoadSymAddr = true;
1345 }
1346
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00001347 SDValue InFlag;
1348
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001349 // Create nodes that load address of callee and copy it to T9
1350 if (IsPIC) {
1351 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001352 // Load callee address
Akira Hatanaka342837d2011-05-28 01:07:07 +00001353 Callee = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, Callee);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001354 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, Chain, Callee,
1355 MachinePointerInfo::getGOT(),
1356 false, false, 0);
1357
1358 // Use GOT+LO if callee has internal linkage.
1359 if (CalleeLo.getNode()) {
1360 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
1361 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
1362 } else
1363 Callee = LoadValue;
1364
1365 // Use chain output from LoadValue
1366 Chain = LoadValue.getValue(1);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001367 }
1368
1369 // copy to T9
1370 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
1371 InFlag = Chain.getValue(1);
1372 Callee = DAG.getRegister(Mips::T9, MVT::i32);
1373 }
Bill Wendling056292f2008-09-16 21:48:12 +00001374
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00001375 // Build a sequence of copy-to-reg nodes chained together with token
1376 // chain and flag operands which copy the outgoing args into registers.
1377 // The InFlag in necessary since all emitted instructions must be
1378 // stuck together.
1379 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1380 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1381 RegsToPass[i].second, InFlag);
1382 InFlag = Chain.getValue(1);
1383 }
1384
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001385 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001386 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001387 //
1388 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001389 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00001390 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001391 Ops.push_back(Chain);
1392 Ops.push_back(Callee);
1393
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001394 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001395 // known live into the call.
1396 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1397 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1398 RegsToPass[i].second.getValueType()));
1399
Gabor Greifba36cb52008-08-28 21:40:38 +00001400 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001401 Ops.push_back(InFlag);
1402
Dale Johannesen33c960f2009-02-04 20:06:27 +00001403 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001404 InFlag = Chain.getValue(1);
1405
Akira Hatanaka4c62f762011-05-25 18:08:32 +00001406 // Function can have an arbitrary number of calls, so
1407 // hold the LastArgStackLoc with the biggest offset.
1408 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1409 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001410
Akira Hatanaka4c62f762011-05-25 18:08:32 +00001411 // For O32, a minimum of four words (16 bytes) of argument space is
1412 // allocated.
1413 if (Subtarget->isABI_O32())
1414 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
Akira Hatanakad37776d2011-05-20 21:39:54 +00001415
Akira Hatanaka4c62f762011-05-25 18:08:32 +00001416 if (MaxCallFrameSize < NextStackOffset) {
1417 MipsFI->setMaxCallFrameSize(NextStackOffset);
Akira Hatanakad37776d2011-05-20 21:39:54 +00001418
Akira Hatanaka4c62f762011-05-25 18:08:32 +00001419 if (IsPIC) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001420 // $gp restore slot must be aligned.
1421 unsigned StackAlignment = TFL->getStackAlignment();
1422 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
1423 StackAlignment * StackAlignment;
1424 int GPFI = MipsFI->getGPFI();
1425 MFI->setObjectOffset(GPFI, NextStackOffset);
1426 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001427 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001428
Akira Hatanaka43299772011-05-20 23:22:14 +00001429 // Extend range of indices of frame objects for outgoing arguments that were
1430 // created during this function call. Skip this step if no such objects were
1431 // created.
1432 if (LastFI)
1433 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
1434
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00001435 // Create the CALLSEQ_END node.
1436 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1437 DAG.getIntPtrConstant(0, true), InFlag);
1438 InFlag = Chain.getValue(1);
1439
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001440 // Handle result values, copying them out of physregs into vregs that we
1441 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001442 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
1443 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001444}
1445
Dan Gohman98ca4f22009-08-05 01:29:28 +00001446/// LowerCallResult - Lower the result values of a call into the
1447/// appropriate copies out of appropriate physical registers.
1448SDValue
1449MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001450 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001451 const SmallVectorImpl<ISD::InputArg> &Ins,
1452 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001453 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001454
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001455 // Assign locations to each value returned by this call.
1456 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001457 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001458 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001459
Dan Gohman98ca4f22009-08-05 01:29:28 +00001460 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001461
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001462 // Copy all of the result registers out of their specified physreg.
1463 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001464 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001465 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001466 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001467 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001468 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001469
Dan Gohman98ca4f22009-08-05 01:29:28 +00001470 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001471}
1472
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001473//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001474// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001475//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001476static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
1477 std::vector<SDValue>& OutChains,
1478 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
1479 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
1480 unsigned LocMem = VA.getLocMemOffset();
1481 unsigned FirstWord = LocMem / 4;
1482
1483 // copy register A0 - A3 to frame object
1484 for (unsigned i = 0; i < NumWords; ++i) {
1485 unsigned CurWord = FirstWord + i;
1486 if (CurWord >= O32IntRegsSize)
1487 break;
1488
1489 unsigned SrcReg = O32IntRegs[CurWord];
1490 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
1491 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
1492 DAG.getConstant(i * 4, MVT::i32));
1493 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
1494 StorePtr, MachinePointerInfo(), false,
1495 false, 0);
1496 OutChains.push_back(Store);
1497 }
1498}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001499
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001500/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001501/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001502SDValue
1503MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001504 CallingConv::ID CallConv,
1505 bool isVarArg,
1506 const SmallVectorImpl<ISD::InputArg>
1507 &Ins,
1508 DebugLoc dl, SelectionDAG &DAG,
1509 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001510 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00001511 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001512 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001513 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001514
Dan Gohman1e93df62010-04-17 14:41:14 +00001515 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001516
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001517 // Used with vargs to acumulate store chains.
1518 std::vector<SDValue> OutChains;
1519
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001520 // Assign locations to all of the incoming arguments.
1521 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001522 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1523 ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001524
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001525 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001526 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001527 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001528 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001529
Akira Hatanaka43299772011-05-20 23:22:14 +00001530 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001531
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001532 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001533 CCValAssign &VA = ArgLocs[i];
1534
1535 // Arguments stored on registers
1536 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001537 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001538 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00001539 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001540
Owen Anderson825b72b2009-08-11 20:47:22 +00001541 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001542 RC = Mips::CPURegsRegisterClass;
1543 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001544 RC = Mips::FGR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001545 else if (RegVT == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001546 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001547 RC = Mips::AFGR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001548 } else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001549 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001550
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001551 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001552 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001553 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001554 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001555
1556 // If this is an 8 or 16-bit value, it has been passed promoted
1557 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001558 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001559 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00001560 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001561 if (VA.getLocInfo() == CCValAssign::SExt)
1562 Opcode = ISD::AssertSext;
1563 else if (VA.getLocInfo() == CCValAssign::ZExt)
1564 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00001565 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001566 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00001567 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00001568 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001569 }
1570
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001571 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001572 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001573 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
1574 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00001575 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001576 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001577 VA.getLocReg()+1, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001578 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001579 if (!Subtarget->isLittle())
1580 std::swap(ArgValue, ArgValue2);
1581 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
1582 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001583 }
1584 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001585
Dan Gohman98ca4f22009-08-05 01:29:28 +00001586 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001587 } else { // VA.isRegLoc()
1588
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001589 // sanity check
1590 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001591
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001592 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1593
1594 if (Flags.isByVal()) {
1595 assert(Subtarget->isABI_O32() &&
1596 "No support for ByVal args by ABIs other than O32 yet.");
1597 assert(Flags.getByValSize() &&
1598 "ByVal args of size 0 should have been ignored by front-end.");
1599 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
1600 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
1601 true);
1602 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
1603 InVals.push_back(FIN);
1604 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
1605
1606 continue;
1607 }
1608
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001609 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001610 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
1611 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001612
1613 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00001614 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001615 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00001616 MachinePointerInfo::getFixedStack(LastFI),
David Greenef6fa1862010-02-15 16:56:10 +00001617 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001618 }
1619 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001620
1621 // The mips ABIs for returning structs by value requires that we copy
1622 // the sret argument into $v0 for the return. Save the argument into
1623 // a virtual register so that we can access it from the return points.
1624 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1625 unsigned Reg = MipsFI->getSRetReturnReg();
1626 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001627 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001628 MipsFI->setSRetReturnReg(Reg);
1629 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001630 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001631 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001632 }
1633
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001634 if (isVarArg && Subtarget->isABI_O32()) {
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001635 // Record the frame index of the first variable argument
1636 // which is a value necessary to VASTART.
1637 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001638 assert(NextStackOffset % 4 == 0 &&
1639 "NextStackOffset must be aligned to 4-byte boundaries.");
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001640 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
1641 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00001642
1643 // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
1644 // copy the integer registers that have not been used for argument passing
1645 // to the caller's stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001646 for (; NextStackOffset < 16; NextStackOffset += 4) {
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001647 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001648 unsigned Idx = NextStackOffset / 4;
1649 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
1650 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Akira Hatanaka69c19f72011-05-23 20:16:59 +00001651 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001652 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
1653 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
1654 MachinePointerInfo(),
1655 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001656 }
1657 }
1658
Akira Hatanaka43299772011-05-20 23:22:14 +00001659 MipsFI->setLastInArgFI(LastFI);
1660
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001661 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001662 // the size of Ins and InVals. This only happens when on varg functions
1663 if (!OutChains.empty()) {
1664 OutChains.push_back(Chain);
1665 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1666 &OutChains[0], OutChains.size());
1667 }
1668
Dan Gohman98ca4f22009-08-05 01:29:28 +00001669 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001670}
1671
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001672//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001673// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001674//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001675
Dan Gohman98ca4f22009-08-05 01:29:28 +00001676SDValue
1677MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001678 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001679 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001680 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001681 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001682
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001683 // CCValAssign - represent the assignment of
1684 // the return value to a location
1685 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001686
1687 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001688 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1689 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001690
Dan Gohman98ca4f22009-08-05 01:29:28 +00001691 // Analize return values.
1692 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001693
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001694 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001695 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001696 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001697 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001698 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001699 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001700 }
1701
Dan Gohman475871a2008-07-27 21:46:04 +00001702 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001703
1704 // Copy the result values into the output registers.
1705 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1706 CCValAssign &VA = RVLocs[i];
1707 assert(VA.isRegLoc() && "Can only return in registers!");
1708
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001709 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001710 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001711
1712 // guarantee that all emitted copies are
1713 // stuck together, avoiding something bad
1714 Flag = Chain.getValue(1);
1715 }
1716
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001717 // The mips ABIs for returning structs by value requires that we copy
1718 // the sret argument into $v0 for the return. We saved the argument into
1719 // a virtual register in the entry block, so now we copy the value out
1720 // and into $v0.
1721 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1722 MachineFunction &MF = DAG.getMachineFunction();
1723 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1724 unsigned Reg = MipsFI->getSRetReturnReg();
1725
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001726 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00001727 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00001728 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001729
Dale Johannesena05dca42009-02-04 23:02:30 +00001730 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001731 Flag = Chain.getValue(1);
1732 }
1733
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001734 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00001735 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001736 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001737 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001738 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001739 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001740 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001741}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001742
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001743//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001744// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001745//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001746
1747/// getConstraintType - Given a constraint letter, return the type of
1748/// constraint it is for this target.
1749MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001750getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001751{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001752 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001753 // GCC config/mips/constraints.md
1754 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001755 // 'd' : An address register. Equivalent to r
1756 // unless generating MIPS16 code.
1757 // 'y' : Equivalent to r; retained for
1758 // backwards compatibility.
1759 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001760 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001761 switch (Constraint[0]) {
1762 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001763 case 'd':
1764 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001765 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001766 return C_RegisterClass;
1767 break;
1768 }
1769 }
1770 return TargetLowering::getConstraintType(Constraint);
1771}
1772
John Thompson44ab89e2010-10-29 17:29:13 +00001773/// Examine constraint type and operand type and determine a weight value.
1774/// This object must already have been set up with the operand type
1775/// and the current alternative constraint selected.
1776TargetLowering::ConstraintWeight
1777MipsTargetLowering::getSingleConstraintMatchWeight(
1778 AsmOperandInfo &info, const char *constraint) const {
1779 ConstraintWeight weight = CW_Invalid;
1780 Value *CallOperandVal = info.CallOperandVal;
1781 // If we don't have a value, we can't do a match,
1782 // but allow it at the lowest weight.
1783 if (CallOperandVal == NULL)
1784 return CW_Default;
1785 const Type *type = CallOperandVal->getType();
1786 // Look at the constraint type.
1787 switch (*constraint) {
1788 default:
1789 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
1790 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001791 case 'd':
1792 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00001793 if (type->isIntegerTy())
1794 weight = CW_Register;
1795 break;
1796 case 'f':
1797 if (type->isFloatTy())
1798 weight = CW_Register;
1799 break;
1800 }
1801 return weight;
1802}
1803
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001804/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1805/// return a list of registers that can be used to satisfy the constraint.
1806/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001807std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00001808getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001809{
1810 if (Constraint.size() == 1) {
1811 switch (Constraint[0]) {
1812 case 'r':
1813 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001814 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001815 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001816 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001817 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001818 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1819 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001820 }
1821 }
1822 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1823}
1824
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001825/// Given a register class constraint, like 'r', if this corresponds directly
1826/// to an LLVM register class, return a register of 0 and the register class
1827/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001828std::vector<unsigned> MipsTargetLowering::
1829getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001830 EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001831{
1832 if (Constraint.size() != 1)
1833 return std::vector<unsigned>();
1834
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001835 switch (Constraint[0]) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001836 default : break;
1837 case 'r':
1838 // GCC Mips Constraint Letters
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001839 case 'd':
1840 case 'y':
1841 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
1842 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
1843 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001844 Mips::T8, 0);
1845
1846 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001847 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001848 if (Subtarget->isSingleFloat())
1849 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1850 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1851 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1852 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1853 Mips::F30, Mips::F31, 0);
1854 else
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001855 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
1856 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001857 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +00001858 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001859
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001860 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001861 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001862 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
1863 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001864 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001865 }
1866 return std::vector<unsigned>();
1867}
Dan Gohman6520e202008-10-18 02:06:02 +00001868
1869bool
1870MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1871 // The Mips target isn't yet aware of offsets.
1872 return false;
1873}
Evan Chengeb2f9692009-10-27 19:56:55 +00001874
Evan Chenga1eaa3c2009-10-28 01:43:28 +00001875bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
1876 if (VT != MVT::f32 && VT != MVT::f64)
1877 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00001878 if (Imm.isNegZero())
1879 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00001880 return Imm.isZero();
1881}