blob: 834b1bf53aff470316eca232a1ab99268b049aa4 [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";
44 case MipsISD::Ret: return "MipsISD::Ret";
45 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
46 case MipsISD::FPCmp: return "MipsISD::FPCmp";
47 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
48 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
49 case MipsISD::FPRound: return "MipsISD::FPRound";
50 case MipsISD::MAdd: return "MipsISD::MAdd";
51 case MipsISD::MAddu: return "MipsISD::MAddu";
52 case MipsISD::MSub: return "MipsISD::MSub";
53 case MipsISD::MSubu: return "MipsISD::MSubu";
54 case MipsISD::DivRem: return "MipsISD::DivRem";
55 case MipsISD::DivRemU: return "MipsISD::DivRemU";
56 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
57 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
58 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000059 }
60}
61
62MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000063MipsTargetLowering(MipsTargetMachine &TM)
Chris Lattnerb71b9092009-08-13 06:28:06 +000064 : TargetLowering(TM, new MipsTargetObjectFile()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000065 Subtarget = &TM.getSubtarget<MipsSubtarget>();
66
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000067 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000068 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000069 setBooleanContents(ZeroOrOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000070
71 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000072 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
73 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000074
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000075 // When dealing with single precision only, use libcalls
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000076 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000077 if (!Subtarget->isFP64bit())
Owen Anderson825b72b2009-08-11 20:47:22 +000078 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000079
Wesley Peckbf17cfa2010-11-23 03:31:01 +000080 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +000081 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
82 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
83 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000084
Eli Friedman6055a6a2009-07-17 04:07:24 +000085 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +000086 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
87 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +000088
Wesley Peckbf17cfa2010-11-23 03:31:01 +000089 // Used by legalize types to correctly generate the setcc result.
90 // Without this, every float setcc comes with a AND/OR with the result,
91 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000092 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +000093 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000094
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000095 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +000096 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +000097 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000098 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
99 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
100 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
101 setOperationAction(ISD::SELECT, MVT::f32, Custom);
102 setOperationAction(ISD::SELECT, MVT::f64, Custom);
103 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000104 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
105 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000106 setOperationAction(ISD::VASTART, MVT::Other, Custom);
107
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000108 setOperationAction(ISD::SDIV, MVT::i32, Expand);
109 setOperationAction(ISD::SREM, MVT::i32, Expand);
110 setOperationAction(ISD::UDIV, MVT::i32, Expand);
111 setOperationAction(ISD::UREM, MVT::i32, Expand);
112
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000113 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000114 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
115 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
116 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
117 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
118 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
120 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
121 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
122 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000123
124 if (!Subtarget->isMips32r2())
125 setOperationAction(ISD::ROTR, MVT::i32, Expand);
126
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
128 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
129 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000130 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
131 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000133 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000135 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
137 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000138 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 setOperationAction(ISD::FLOG, MVT::f32, Expand);
140 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
141 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
142 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000143
Owen Anderson825b72b2009-08-11 20:47:22 +0000144 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000145
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000146 setOperationAction(ISD::VAARG, MVT::Other, Expand);
147 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
148 setOperationAction(ISD::VAEND, MVT::Other, Expand);
149
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000150 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
152 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
153 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000154
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000155 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000156 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000157
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000158 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
160 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000161 }
162
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000163 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000164 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000165
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000166 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000167 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000168
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000169 setTargetDAGCombine(ISD::ADDE);
170 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000171 setTargetDAGCombine(ISD::SDIVREM);
172 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000173 setTargetDAGCombine(ISD::SETCC);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000174
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000175 setMinFunctionAlignment(2);
176
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000177 setStackPointerRegisterToSaveRestore(Mips::SP);
178 computeRegisterProperties();
179}
180
Owen Anderson825b72b2009-08-11 20:47:22 +0000181MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
182 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000183}
184
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000185// SelectMadd -
186// Transforms a subgraph in CurDAG if the following pattern is found:
187// (addc multLo, Lo0), (adde multHi, Hi0),
188// where,
189// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000190// Lo0: initial value of Lo register
191// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000192// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000193static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000194 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000195 // for the matching to be successful.
196 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
197
198 if (ADDCNode->getOpcode() != ISD::ADDC)
199 return false;
200
201 SDValue MultHi = ADDENode->getOperand(0);
202 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000203 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000204 unsigned MultOpc = MultHi.getOpcode();
205
206 // MultHi and MultLo must be generated by the same node,
207 if (MultLo.getNode() != MultNode)
208 return false;
209
210 // and it must be a multiplication.
211 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
212 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000213
214 // MultLo amd MultHi must be the first and second output of MultNode
215 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000216 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
217 return false;
218
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000219 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000220 // of the values of MultNode, in which case MultNode will be removed in later
221 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000222 // If there exist users other than ADDENode or ADDCNode, this function returns
223 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000224 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000225 // produced.
226 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
227 return false;
228
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000229 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000230 DebugLoc dl = ADDENode->getDebugLoc();
231
232 // create MipsMAdd(u) node
233 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000234
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000235 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
236 MVT::Glue,
237 MultNode->getOperand(0),// Factor 0
238 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000239 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000240 ADDENode->getOperand(1));// Hi0
241
242 // create CopyFromReg nodes
243 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
244 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000245 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000246 Mips::HI, MVT::i32,
247 CopyFromLo.getValue(2));
248
249 // replace uses of adde and addc here
250 if (!SDValue(ADDCNode, 0).use_empty())
251 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
252
253 if (!SDValue(ADDENode, 0).use_empty())
254 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
255
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000256 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000257}
258
259// SelectMsub -
260// Transforms a subgraph in CurDAG if the following pattern is found:
261// (addc Lo0, multLo), (sube Hi0, multHi),
262// where,
263// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000264// Lo0: initial value of Lo register
265// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000266// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000267static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000268 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000269 // for the matching to be successful.
270 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
271
272 if (SUBCNode->getOpcode() != ISD::SUBC)
273 return false;
274
275 SDValue MultHi = SUBENode->getOperand(1);
276 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000277 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000278 unsigned MultOpc = MultHi.getOpcode();
279
280 // MultHi and MultLo must be generated by the same node,
281 if (MultLo.getNode() != MultNode)
282 return false;
283
284 // and it must be a multiplication.
285 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
286 return false;
287
288 // MultLo amd MultHi must be the first and second output of MultNode
289 // respectively.
290 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
291 return false;
292
293 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
294 // of the values of MultNode, in which case MultNode will be removed in later
295 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000296 // If there exist users other than SUBENode or SUBCNode, this function returns
297 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000298 // instruction node rather than a pair of MULT and MSUB instructions being
299 // produced.
300 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
301 return false;
302
303 SDValue Chain = CurDAG->getEntryNode();
304 DebugLoc dl = SUBENode->getDebugLoc();
305
306 // create MipsSub(u) node
307 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
308
309 SDValue MSub = CurDAG->getNode(MultOpc, dl,
310 MVT::Glue,
311 MultNode->getOperand(0),// Factor 0
312 MultNode->getOperand(1),// Factor 1
313 SUBCNode->getOperand(0),// Lo0
314 SUBENode->getOperand(0));// Hi0
315
316 // create CopyFromReg nodes
317 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
318 MSub);
319 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
320 Mips::HI, MVT::i32,
321 CopyFromLo.getValue(2));
322
323 // replace uses of sube and subc here
324 if (!SDValue(SUBCNode, 0).use_empty())
325 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
326
327 if (!SDValue(SUBENode, 0).use_empty())
328 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
329
330 return true;
331}
332
333static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
334 TargetLowering::DAGCombinerInfo &DCI,
335 const MipsSubtarget* Subtarget) {
336 if (DCI.isBeforeLegalize())
337 return SDValue();
338
339 if (Subtarget->isMips32() && SelectMadd(N, &DAG))
340 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000341
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000342 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000343}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000344
345static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
346 TargetLowering::DAGCombinerInfo &DCI,
347 const MipsSubtarget* Subtarget) {
348 if (DCI.isBeforeLegalize())
349 return SDValue();
350
351 if (Subtarget->isMips32() && SelectMsub(N, &DAG))
352 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000353
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000354 return SDValue();
355}
356
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000357static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
358 TargetLowering::DAGCombinerInfo &DCI,
359 const MipsSubtarget* Subtarget) {
360 if (DCI.isBeforeLegalizeOps())
361 return SDValue();
362
363 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
364 MipsISD::DivRemU;
365 DebugLoc dl = N->getDebugLoc();
366
367 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
368 N->getOperand(0), N->getOperand(1));
369 SDValue InChain = DAG.getEntryNode();
370 SDValue InGlue = DivRem;
371
372 // insert MFLO
373 if (N->hasAnyUseOfValue(0)) {
374 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, Mips::LO, MVT::i32,
375 InGlue);
376 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
377 InChain = CopyFromLo.getValue(1);
378 InGlue = CopyFromLo.getValue(2);
379 }
380
381 // insert MFHI
382 if (N->hasAnyUseOfValue(1)) {
383 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000384 Mips::HI, MVT::i32, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000385 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
386 }
387
388 return SDValue();
389}
390
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000391static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
392 switch (CC) {
393 default: llvm_unreachable("Unknown fp condition code!");
394 case ISD::SETEQ:
395 case ISD::SETOEQ: return Mips::FCOND_OEQ;
396 case ISD::SETUNE: return Mips::FCOND_UNE;
397 case ISD::SETLT:
398 case ISD::SETOLT: return Mips::FCOND_OLT;
399 case ISD::SETGT:
400 case ISD::SETOGT: return Mips::FCOND_OGT;
401 case ISD::SETLE:
402 case ISD::SETOLE: return Mips::FCOND_OLE;
403 case ISD::SETGE:
404 case ISD::SETOGE: return Mips::FCOND_OGE;
405 case ISD::SETULT: return Mips::FCOND_ULT;
406 case ISD::SETULE: return Mips::FCOND_ULE;
407 case ISD::SETUGT: return Mips::FCOND_UGT;
408 case ISD::SETUGE: return Mips::FCOND_UGE;
409 case ISD::SETUO: return Mips::FCOND_UN;
410 case ISD::SETO: return Mips::FCOND_OR;
411 case ISD::SETNE:
412 case ISD::SETONE: return Mips::FCOND_ONE;
413 case ISD::SETUEQ: return Mips::FCOND_UEQ;
414 }
415}
416
417
418// Returns true if condition code has to be inverted.
419static bool InvertFPCondCode(Mips::CondCode CC) {
420 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
421 return false;
422
423 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
424 return true;
425
426 assert(false && "Illegal Condition Code");
427 return false;
428}
429
430// Creates and returns an FPCmp node from a setcc node.
431// Returns Op if setcc is not a floating point comparison.
432static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
433 // must be a SETCC node
434 if (Op.getOpcode() != ISD::SETCC)
435 return Op;
436
437 SDValue LHS = Op.getOperand(0);
438
439 if (!LHS.getValueType().isFloatingPoint())
440 return Op;
441
442 SDValue RHS = Op.getOperand(1);
443 DebugLoc dl = Op.getDebugLoc();
444
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000445 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
446 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000447 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
448
449 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
450 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
451}
452
453// Creates and returns a CMovFPT/F node.
454static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
455 SDValue False, DebugLoc DL) {
456 bool invert = InvertFPCondCode((Mips::CondCode)
457 cast<ConstantSDNode>(Cond.getOperand(2))
458 ->getSExtValue());
459
460 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
461 True.getValueType(), True, False, Cond);
462}
463
464static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
465 TargetLowering::DAGCombinerInfo &DCI,
466 const MipsSubtarget* Subtarget) {
467 if (DCI.isBeforeLegalizeOps())
468 return SDValue();
469
470 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
471
472 if (Cond.getOpcode() != MipsISD::FPCmp)
473 return SDValue();
474
475 SDValue True = DAG.getConstant(1, MVT::i32);
476 SDValue False = DAG.getConstant(0, MVT::i32);
477
478 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
479}
480
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000481SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000482 const {
483 SelectionDAG &DAG = DCI.DAG;
484 unsigned opc = N->getOpcode();
485
486 switch (opc) {
487 default: break;
488 case ISD::ADDE:
489 return PerformADDECombine(N, DAG, DCI, Subtarget);
490 case ISD::SUBE:
491 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000492 case ISD::SDIVREM:
493 case ISD::UDIVREM:
494 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000495 case ISD::SETCC:
496 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000497 }
498
499 return SDValue();
500}
501
Dan Gohman475871a2008-07-27 21:46:04 +0000502SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000503LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000504{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000505 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000506 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000507 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000508 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
509 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000510 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000511 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000512 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
513 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000514 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000515 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000516 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000517 }
Dan Gohman475871a2008-07-27 21:46:04 +0000518 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000519}
520
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000521//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000522// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000523//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000524
525// AddLiveIn - This helper function adds the specified physical register to the
526// MachineFunction as a live in value. It also creates a corresponding
527// virtual register for it.
528static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000529AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000530{
531 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000532 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
533 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000534 return VReg;
535}
536
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000537// Get fp branch code (not opcode) from condition code.
538static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
539 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
540 return Mips::BRANCH_T;
541
542 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
543 return Mips::BRANCH_F;
544
545 return Mips::BRANCH_INVALID;
546}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000547
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000548MachineBasicBlock *
549MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000550 MachineBasicBlock *BB) const {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000551 // There is no need to expand CMov instructions if target has
552 // conditional moves.
553 if (Subtarget->hasCondMov())
554 return BB;
555
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000556 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
557 bool isFPCmp = false;
Dale Johannesen94817572009-02-13 02:34:39 +0000558 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000559 unsigned Opc;
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000560
561 switch (MI->getOpcode()) {
562 default: assert(false && "Unexpected instr type to insert");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000563 case Mips::MOVT:
564 case Mips::MOVT_S:
565 case Mips::MOVT_D:
566 isFPCmp = true;
567 Opc = Mips::BC1F;
568 break;
569 case Mips::MOVF:
570 case Mips::MOVF_S:
571 case Mips::MOVF_D:
572 isFPCmp = true;
573 Opc = Mips::BC1T;
574 break;
575 case Mips::MOVZ_I:
576 case Mips::MOVZ_S:
577 case Mips::MOVZ_D:
578 Opc = Mips::BNE;
579 break;
580 case Mips::MOVN_I:
581 case Mips::MOVN_S:
582 case Mips::MOVN_D:
583 Opc = Mips::BEQ;
584 break;
585 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000586
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000587 // To "insert" a SELECT_CC instruction, we actually have to insert the
588 // diamond control-flow pattern. The incoming instruction knows the
589 // destination vreg to set, the condition code register to branch on, the
590 // true/false values to select between, and a branch opcode to use.
591 const BasicBlock *LLVM_BB = BB->getBasicBlock();
592 MachineFunction::iterator It = BB;
593 ++It;
Dan Gohman14152b42010-07-06 20:24:04 +0000594
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000595 // thisMBB:
596 // ...
597 // TrueVal = ...
598 // setcc r1, r2, r3
599 // bNE r1, r0, copy1MBB
600 // fallthrough --> copy0MBB
601 MachineBasicBlock *thisMBB = BB;
602 MachineFunction *F = BB->getParent();
603 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
604 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
605 F->insert(It, copy0MBB);
606 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +0000607
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000608 // Transfer the remainder of BB and its successor edges to sinkMBB.
609 sinkMBB->splice(sinkMBB->begin(), BB,
610 llvm::next(MachineBasicBlock::iterator(MI)),
611 BB->end());
612 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000613
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000614 // Next, add the true and fallthrough blocks as its successors.
615 BB->addSuccessor(copy0MBB);
616 BB->addSuccessor(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000617
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000618 // Emit the right instruction according to the type of the operands compared
619 if (isFPCmp)
620 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
621 else
622 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
623 .addReg(Mips::ZERO).addMBB(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000624
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000625
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000626 // copy0MBB:
627 // %FalseValue = ...
628 // # fallthrough to sinkMBB
629 BB = copy0MBB;
630
631 // Update machine-CFG edges
632 BB->addSuccessor(sinkMBB);
633
634 // sinkMBB:
635 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
636 // ...
637 BB = sinkMBB;
638
639 if (isFPCmp)
Dan Gohman14152b42010-07-06 20:24:04 +0000640 BuildMI(*BB, BB->begin(), dl,
641 TII->get(Mips::PHI), MI->getOperand(0).getReg())
Bruno Cardoso Lopes29e9daa2010-07-20 07:58:51 +0000642 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000643 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
644 else
645 BuildMI(*BB, BB->begin(), dl,
646 TII->get(Mips::PHI), MI->getOperand(0).getReg())
647 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
648 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000649
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000650 MI->eraseFromParent(); // The pseudo instruction is gone now.
651 return BB;
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000652}
653
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000654//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000655// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000656//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000657SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000658LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000659{
Akira Hatanaka053546c2011-05-25 02:20:00 +0000660 unsigned StackAlignment =
661 getTargetMachine().getFrameLowering()->getStackAlignment();
662 assert(StackAlignment >=
663 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
664 "Cannot lower if the alignment of the allocated space is larger than \
665 that of the stack.");
666
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000667 SDValue Chain = Op.getOperand(0);
668 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +0000669 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000670
671 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000673
674 // Subtract the dynamic size from the actual stack size to
675 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000677
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000678 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000679 // must be placed in the stack pointer register.
Akira Hatanaka053546c2011-05-25 02:20:00 +0000680 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
681 SDValue());
Akira Hatanakaedacba82011-05-25 17:32:06 +0000682 // Retrieve updated $sp. There is a glue input to prevent instructions that
683 // clobber $sp from being inserted between copytoreg and copyfromreg.
Akira Hatanaka053546c2011-05-25 02:20:00 +0000684 SDValue NewSP = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32,
685 Chain.getValue(1));
686
Akira Hatanakaedacba82011-05-25 17:32:06 +0000687 // The stack space reserved by alloca is located right above the argument
688 // area. It is aligned on a boundary that is a multiple of StackAlignment.
Akira Hatanaka053546c2011-05-25 02:20:00 +0000689 MachineFunction &MF = DAG.getMachineFunction();
690 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
691 unsigned SPOffset = (MipsFI->getMaxCallFrameSize() + StackAlignment - 1) /
692 StackAlignment * StackAlignment;
693 SDValue AllocPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
694 DAG.getConstant(SPOffset, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000695
696 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000697 // value and a chain
Akira Hatanaka053546c2011-05-25 02:20:00 +0000698 SDValue Ops[2] = { AllocPtr, NewSP.getValue(1) };
Dale Johannesena05dca42009-02-04 23:02:30 +0000699 return DAG.getMergeValues(Ops, 2, dl);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000700}
701
702SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000703LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000704{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000705 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000706 // the block to branch to if the condition is true.
707 SDValue Chain = Op.getOperand(0);
708 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000709 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000710
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000711 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
712
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000713 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000714 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000715 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000716
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000717 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000718 Mips::CondCode CC =
719 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000720 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000721
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000722 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000723 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000724}
725
726SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000727LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000728{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000729 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000730
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000731 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000732 if (Cond.getOpcode() != MipsISD::FPCmp)
733 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000734
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000735 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
736 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000737}
738
Dan Gohmand858e902010-04-17 15:26:15 +0000739SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
740 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +0000741 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000742 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000743 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000744
Eli Friedmane2c74082009-08-03 02:22:28 +0000745 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +0000746 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000747
Chris Lattnerb71b9092009-08-13 06:28:06 +0000748 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000749
Chris Lattnere3736f82009-08-13 05:41:27 +0000750 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000751 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
752 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000753 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +0000754 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
755 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000756 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +0000757 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000758 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000759 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
760 MipsII::MO_ABS_HI);
761 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
762 MipsII::MO_ABS_LO);
763 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
764 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000765 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000766 } else {
Devang Patel0d881da2010-07-06 22:08:15 +0000767 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000768 MipsII::MO_GOT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000769 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000770 DAG.getEntryNode(), GA, MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000771 false, false, 0);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000772 // On functions and global targets not internal linked only
773 // a load from got/GP is necessary for PIC to work.
Akira Hatanaka9777e7a2011-04-07 19:51:44 +0000774 if (!GV->hasInternalLinkage() &&
775 (!GV->hasLocalLinkage() || isa<Function>(GV)))
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000776 return ResNode;
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000777 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
778 MipsII::MO_ABS_LO);
779 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000780 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000781 }
782
Torok Edwinc23197a2009-07-14 16:55:14 +0000783 llvm_unreachable("Dont know how to handle GlobalAddress");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000784 return SDValue(0,0);
785}
786
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000787SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
788 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +0000789 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
790 // FIXME there isn't actually debug info here
791 DebugLoc dl = Op.getDebugLoc();
792
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000793 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +0000794 // %hi/%lo relocation
795 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
796 MipsII::MO_ABS_HI);
797 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
798 MipsII::MO_ABS_LO);
799 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
800 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
801 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000802 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +0000803
804 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
805 MipsII::MO_GOT);
806 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
807 MipsII::MO_ABS_LO);
808 SDValue Load = DAG.getLoad(MVT::i32, dl,
809 DAG.getEntryNode(), BAGOTOffset,
810 MachinePointerInfo(), false, false, 0);
811 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
812 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000813}
814
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000815SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000816LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000817{
Torok Edwinc23197a2009-07-14 16:55:14 +0000818 llvm_unreachable("TLS not implemented for MIPS.");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000819 return SDValue(); // Not reached
820}
821
822SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000823LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000824{
Dan Gohman475871a2008-07-27 21:46:04 +0000825 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000826 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +0000827 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000828 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000829 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000830 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000831
Owen Andersone50ed302009-08-10 22:56:29 +0000832 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000833 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000834
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000835 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
836
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +0000837 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +0000838 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000839 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000840 } else // Emit Load from Global Pointer
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000841 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
842 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000843 false, false, 0);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000844
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000845 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
846 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000847 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000848 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000849
850 return ResNode;
851}
852
Dan Gohman475871a2008-07-27 21:46:04 +0000853SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000854LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000855{
Dan Gohman475871a2008-07-27 21:46:04 +0000856 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000857 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +0000858 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +0000859 // FIXME there isn't actually debug info here
860 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000861
862 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000863 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000864 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000865 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000866 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +0000867 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000868 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
869 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000870 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000871
872 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000873 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000874 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000875 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000876 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000877 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
878 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000880 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000881 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000882 N->getOffset(), MipsII::MO_GOT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000883 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000884 CP, MachinePointerInfo::getConstantPool(),
885 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000886 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000887 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000888 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000889 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
890 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000891
892 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000893}
894
Dan Gohmand858e902010-04-17 15:26:15 +0000895SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +0000896 MachineFunction &MF = DAG.getMachineFunction();
897 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
898
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000899 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +0000900 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
901 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000902
903 // vastart just stores the address of the VarArgsFrameIndex slot into the
904 // memory location argument.
905 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +0000906 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
907 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +0000908 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000909}
910
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000911static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
912 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
913 DebugLoc dl = Op.getDebugLoc();
914 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
915 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
916 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
917 DAG.getConstant(0x7fffffff, MVT::i32));
918 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
919 DAG.getConstant(0x80000000, MVT::i32));
920 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
921 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
922}
923
924static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
925 // FIXME:
926 // Use ext/ins instructions if target architecture is Mips32r2.
927 // Eliminate redundant mfc1 and mtc1 instructions.
928 unsigned LoIdx = 0, HiIdx = 1;
929
930 if (!isLittle)
931 std::swap(LoIdx, HiIdx);
932
933 DebugLoc dl = Op.getDebugLoc();
934 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
935 Op.getOperand(0),
936 DAG.getConstant(LoIdx, MVT::i32));
937 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
938 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
939 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
940 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
941 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
942 DAG.getConstant(0x7fffffff, MVT::i32));
943 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
944 DAG.getConstant(0x80000000, MVT::i32));
945 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
946
947 if (!isLittle)
948 std::swap(Word0, Word1);
949
950 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
951}
952
953SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
954 const {
955 EVT Ty = Op.getValueType();
956
957 assert(Ty == MVT::f32 || Ty == MVT::f64);
958
959 if (Ty == MVT::f32)
960 return LowerFCOPYSIGN32(Op, DAG);
961 else
962 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
963}
964
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000965//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000966// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000967//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000968
969#include "MipsGenCallingConv.inc"
970
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000971//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000972// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000973// Mips O32 ABI rules:
974// ---
975// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000976// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000977// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000978// f64 - Only passed in two aliased f32 registers if no int reg has been used
979// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000980// not used, it must be shadowed. If only A3 is avaiable, shadow it and
981// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +0000982//
983// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000984//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000985
Duncan Sands1e96bab2010-11-04 10:49:57 +0000986static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000987 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000988 ISD::ArgFlagsTy ArgFlags, CCState &State) {
989
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000990 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000991
992 static const unsigned IntRegs[] = {
993 Mips::A0, Mips::A1, Mips::A2, Mips::A3
994 };
995 static const unsigned F32Regs[] = {
996 Mips::F12, Mips::F14
997 };
998 static const unsigned F64Regs[] = {
999 Mips::D6, Mips::D7
1000 };
1001
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001002 // ByVal Args
1003 if (ArgFlags.isByVal()) {
1004 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1005 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1006 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1007 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1008 r < std::min(IntRegsSize, NextReg); ++r)
1009 State.AllocateReg(IntRegs[r]);
1010 return false;
1011 }
1012
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001013 // Promote i8 and i16
1014 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1015 LocVT = MVT::i32;
1016 if (ArgFlags.isSExt())
1017 LocInfo = CCValAssign::SExt;
1018 else if (ArgFlags.isZExt())
1019 LocInfo = CCValAssign::ZExt;
1020 else
1021 LocInfo = CCValAssign::AExt;
1022 }
1023
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001024 unsigned Reg;
1025
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001026 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1027 // is true: function is vararg, argument is 3rd or higher, there is previous
1028 // argument which is not f32 or f64.
1029 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1030 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001031 unsigned OrigAlign = ArgFlags.getOrigAlign();
1032 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001033
1034 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001035 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001036 // If this is the first part of an i64 arg,
1037 // the allocated register must be either A0 or A2.
1038 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1039 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001040 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001041 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1042 // Allocate int register and shadow next int register. If first
1043 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001044 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1045 if (Reg == Mips::A1 || Reg == Mips::A3)
1046 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1047 State.AllocateReg(IntRegs, IntRegsSize);
1048 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001049 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1050 // we are guaranteed to find an available float register
1051 if (ValVT == MVT::f32) {
1052 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1053 // Shadow int register
1054 State.AllocateReg(IntRegs, IntRegsSize);
1055 } else {
1056 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1057 // Shadow int registers
1058 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1059 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1060 State.AllocateReg(IntRegs, IntRegsSize);
1061 State.AllocateReg(IntRegs, IntRegsSize);
1062 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001063 } else
1064 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001065
Akira Hatanakad37776d2011-05-20 21:39:54 +00001066 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1067 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1068
1069 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001070 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001071 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001072 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001073
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001074 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001075}
1076
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001077//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001078// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001079//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001080
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001081static const unsigned O32IntRegsSize = 4;
1082
1083static const unsigned O32IntRegs[] = {
1084 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1085};
1086
1087// Write ByVal Arg to arg registers and stack.
1088static void
1089WriteByValArg(SDValue& Chain, DebugLoc dl,
1090 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1091 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1092 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001093 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
1094 MVT PtrType) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001095 unsigned FirstWord = VA.getLocMemOffset() / 4;
1096 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
1097 unsigned LastWord = FirstWord + NumWords;
1098 unsigned CurWord;
1099
1100 // copy the first 4 words of byval arg to registers A0 - A3
1101 for (CurWord = FirstWord; CurWord < std::min(LastWord, O32IntRegsSize);
1102 ++CurWord) {
1103 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1104 DAG.getConstant((CurWord - FirstWord) * 4,
1105 MVT::i32));
1106 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1107 MachinePointerInfo(),
1108 false, false, 0);
1109 MemOpChains.push_back(LoadVal.getValue(1));
1110 unsigned DstReg = O32IntRegs[CurWord];
1111 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1112 }
1113
1114 // copy remaining part of byval arg to stack.
1115 if (CurWord < LastWord) {
1116 unsigned SizeInBytes = (LastWord - CurWord) * 4;
1117 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1118 DAG.getConstant((CurWord - FirstWord) * 4,
1119 MVT::i32));
1120 LastFI = MFI->CreateFixedObject(SizeInBytes, CurWord * 4, true);
1121 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
1122 Chain = DAG.getMemcpy(Chain, dl, Dst, Src,
1123 DAG.getConstant(SizeInBytes, MVT::i32),
1124 /*Align*/4,
1125 /*isVolatile=*/false, /*AlwaysInline=*/false,
1126 MachinePointerInfo(0), MachinePointerInfo(0));
1127 MemOpChains.push_back(Chain);
1128 }
1129}
1130
Dan Gohman98ca4f22009-08-05 01:29:28 +00001131/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001132/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001133/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001134SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001135MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001136 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001137 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001138 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001139 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001140 const SmallVectorImpl<ISD::InputArg> &Ins,
1141 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001142 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001143 // MIPs target does not yet support tail call optimization.
1144 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001145
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001146 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001147 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00001148 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001149 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001150 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001151
1152 // Analyze operands of the call, assigning locations to each operand.
1153 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001154 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1155 *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001156
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001157 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001158 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001159 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001160 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001161
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001162 // Get a count of how many bytes are to be pushed on the stack.
1163 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnere563bbc2008-10-11 22:08:30 +00001164 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001165
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001166 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001167 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1168 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001169
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001170 MipsFI->setHasCall();
1171
Akira Hatanakaedacba82011-05-25 17:32:06 +00001172 // If this is the first call, create a stack frame object that points to
1173 // a location to which .cprestore saves $gp. The offset of this frame object
1174 // is set to 0, since we know nothing about the size of the argument area at
1175 // this point.
Akira Hatanaka69c19f72011-05-23 20:16:59 +00001176 if (IsPIC && !MipsFI->getGPFI())
Akira Hatanaka43299772011-05-20 23:22:14 +00001177 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1178
1179 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
1180
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001181 // Walk the register/memloc assignments, inserting copies/loads.
1182 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001183 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001184 CCValAssign &VA = ArgLocs[i];
1185
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001186 // Promote the value if needed.
1187 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001188 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001189 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001190 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001191 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001192 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001193 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001194 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1195 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001196 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1197 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001198 if (!Subtarget->isLittle())
1199 std::swap(Lo, Hi);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001200 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
1201 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
1202 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001203 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001204 }
1205 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00001206 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001207 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001208 break;
1209 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001210 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001211 break;
1212 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001213 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001214 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001215 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001216
1217 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001218 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001219 if (VA.isRegLoc()) {
1220 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00001221 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001222 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001223
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001224 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00001225 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001226
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001227 // ByVal Arg.
1228 ISD::ArgFlagsTy Flags = Outs[i].Flags;
1229 if (Flags.isByVal()) {
1230 assert(Subtarget->isABI_O32() &&
1231 "No support for ByVal args by ABIs other than O32 yet.");
1232 assert(Flags.getByValSize() &&
1233 "ByVal args of size 0 should have been ignored by front-end.");
1234 WriteByValArg(Chain, dl, RegsToPass, MemOpChains, LastFI, MFI, DAG, Arg,
1235 VA, Flags, getPointerTy());
1236 continue;
1237 }
1238
Chris Lattnere0b12152008-03-17 06:57:02 +00001239 // Create the frame index object for this incoming parameter
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001240 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
1241 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00001242 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00001243
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001244 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00001245 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00001246 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
1247 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001248 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001249 }
1250
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001251 // Transform all store nodes into one single node because all store
1252 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001253 if (!MemOpChains.empty())
1254 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001255 &MemOpChains[0], MemOpChains.size());
1256
Bill Wendling056292f2008-09-16 21:48:12 +00001257 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001258 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1259 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001260 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001261 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001262 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001263
1264 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001265 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
1266 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1267 getPointerTy(), 0,MipsII:: MO_GOT);
1268 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
1269 0, MipsII::MO_ABS_LO);
1270 } else {
1271 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1272 getPointerTy(), 0, OpFlag);
1273 }
1274
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001275 LoadSymAddr = true;
1276 }
1277 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001278 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001279 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001280 LoadSymAddr = true;
1281 }
1282
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00001283 SDValue InFlag;
1284
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001285 // Create nodes that load address of callee and copy it to T9
1286 if (IsPIC) {
1287 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001288 // Load callee address
1289 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, Chain, Callee,
1290 MachinePointerInfo::getGOT(),
1291 false, false, 0);
1292
1293 // Use GOT+LO if callee has internal linkage.
1294 if (CalleeLo.getNode()) {
1295 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
1296 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
1297 } else
1298 Callee = LoadValue;
1299
1300 // Use chain output from LoadValue
1301 Chain = LoadValue.getValue(1);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001302 }
1303
1304 // copy to T9
1305 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
1306 InFlag = Chain.getValue(1);
1307 Callee = DAG.getRegister(Mips::T9, MVT::i32);
1308 }
Bill Wendling056292f2008-09-16 21:48:12 +00001309
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00001310 // Build a sequence of copy-to-reg nodes chained together with token
1311 // chain and flag operands which copy the outgoing args into registers.
1312 // The InFlag in necessary since all emitted instructions must be
1313 // stuck together.
1314 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1315 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1316 RegsToPass[i].second, InFlag);
1317 InFlag = Chain.getValue(1);
1318 }
1319
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001320 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001321 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001322 //
1323 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001324 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00001325 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001326 Ops.push_back(Chain);
1327 Ops.push_back(Callee);
1328
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001329 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001330 // known live into the call.
1331 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1332 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1333 RegsToPass[i].second.getValueType()));
1334
Gabor Greifba36cb52008-08-28 21:40:38 +00001335 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001336 Ops.push_back(InFlag);
1337
Dale Johannesen33c960f2009-02-04 20:06:27 +00001338 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001339 InFlag = Chain.getValue(1);
1340
Akira Hatanaka4c62f762011-05-25 18:08:32 +00001341 // Function can have an arbitrary number of calls, so
1342 // hold the LastArgStackLoc with the biggest offset.
1343 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1344 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001345
Akira Hatanaka4c62f762011-05-25 18:08:32 +00001346 // For O32, a minimum of four words (16 bytes) of argument space is
1347 // allocated.
1348 if (Subtarget->isABI_O32())
1349 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
Akira Hatanakad37776d2011-05-20 21:39:54 +00001350
Akira Hatanaka4c62f762011-05-25 18:08:32 +00001351 if (MaxCallFrameSize < NextStackOffset) {
1352 MipsFI->setMaxCallFrameSize(NextStackOffset);
Akira Hatanakad37776d2011-05-20 21:39:54 +00001353
Akira Hatanaka4c62f762011-05-25 18:08:32 +00001354 if (IsPIC) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001355 // $gp restore slot must be aligned.
1356 unsigned StackAlignment = TFL->getStackAlignment();
1357 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
1358 StackAlignment * StackAlignment;
1359 int GPFI = MipsFI->getGPFI();
1360 MFI->setObjectOffset(GPFI, NextStackOffset);
1361 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001362 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001363
Akira Hatanaka43299772011-05-20 23:22:14 +00001364 // Extend range of indices of frame objects for outgoing arguments that were
1365 // created during this function call. Skip this step if no such objects were
1366 // created.
1367 if (LastFI)
1368 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
1369
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00001370 // Create the CALLSEQ_END node.
1371 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1372 DAG.getIntPtrConstant(0, true), InFlag);
1373 InFlag = Chain.getValue(1);
1374
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001375 // Handle result values, copying them out of physregs into vregs that we
1376 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001377 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
1378 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001379}
1380
Dan Gohman98ca4f22009-08-05 01:29:28 +00001381/// LowerCallResult - Lower the result values of a call into the
1382/// appropriate copies out of appropriate physical registers.
1383SDValue
1384MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001385 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001386 const SmallVectorImpl<ISD::InputArg> &Ins,
1387 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001388 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001389
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001390 // Assign locations to each value returned by this call.
1391 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001392 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001393 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001394
Dan Gohman98ca4f22009-08-05 01:29:28 +00001395 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001396
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001397 // Copy all of the result registers out of their specified physreg.
1398 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001399 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001400 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001401 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001402 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001403 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001404
Dan Gohman98ca4f22009-08-05 01:29:28 +00001405 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001406}
1407
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001408//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001409// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001410//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001411static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
1412 std::vector<SDValue>& OutChains,
1413 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
1414 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
1415 unsigned LocMem = VA.getLocMemOffset();
1416 unsigned FirstWord = LocMem / 4;
1417
1418 // copy register A0 - A3 to frame object
1419 for (unsigned i = 0; i < NumWords; ++i) {
1420 unsigned CurWord = FirstWord + i;
1421 if (CurWord >= O32IntRegsSize)
1422 break;
1423
1424 unsigned SrcReg = O32IntRegs[CurWord];
1425 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
1426 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
1427 DAG.getConstant(i * 4, MVT::i32));
1428 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
1429 StorePtr, MachinePointerInfo(), false,
1430 false, 0);
1431 OutChains.push_back(Store);
1432 }
1433}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001434
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001435/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001436/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001437SDValue
1438MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001439 CallingConv::ID CallConv,
1440 bool isVarArg,
1441 const SmallVectorImpl<ISD::InputArg>
1442 &Ins,
1443 DebugLoc dl, SelectionDAG &DAG,
1444 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001445 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00001446 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001447 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001448 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001449
Dan Gohman1e93df62010-04-17 14:41:14 +00001450 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001451
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001452 // Used with vargs to acumulate store chains.
1453 std::vector<SDValue> OutChains;
1454
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001455 // Assign locations to all of the incoming arguments.
1456 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001457 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1458 ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001459
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001460 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001461 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001462 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001463 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001464
Akira Hatanaka43299772011-05-20 23:22:14 +00001465 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001466
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001467 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001468 CCValAssign &VA = ArgLocs[i];
1469
1470 // Arguments stored on registers
1471 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001472 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001473 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00001474 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001475
Owen Anderson825b72b2009-08-11 20:47:22 +00001476 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001477 RC = Mips::CPURegsRegisterClass;
1478 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001479 RC = Mips::FGR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001480 else if (RegVT == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001481 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001482 RC = Mips::AFGR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001483 } else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001484 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001485
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001486 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001487 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001488 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001489 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001490
1491 // If this is an 8 or 16-bit value, it has been passed promoted
1492 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001493 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001494 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00001495 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001496 if (VA.getLocInfo() == CCValAssign::SExt)
1497 Opcode = ISD::AssertSext;
1498 else if (VA.getLocInfo() == CCValAssign::ZExt)
1499 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00001500 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001501 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00001502 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00001503 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001504 }
1505
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001506 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001507 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001508 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
1509 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00001510 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001511 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001512 VA.getLocReg()+1, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001513 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001514 if (!Subtarget->isLittle())
1515 std::swap(ArgValue, ArgValue2);
1516 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
1517 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001518 }
1519 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001520
Dan Gohman98ca4f22009-08-05 01:29:28 +00001521 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001522 } else { // VA.isRegLoc()
1523
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001524 // sanity check
1525 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001526
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001527 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1528
1529 if (Flags.isByVal()) {
1530 assert(Subtarget->isABI_O32() &&
1531 "No support for ByVal args by ABIs other than O32 yet.");
1532 assert(Flags.getByValSize() &&
1533 "ByVal args of size 0 should have been ignored by front-end.");
1534 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
1535 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
1536 true);
1537 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
1538 InVals.push_back(FIN);
1539 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
1540
1541 continue;
1542 }
1543
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001544 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001545 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
1546 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001547
1548 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00001549 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001550 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00001551 MachinePointerInfo::getFixedStack(LastFI),
David Greenef6fa1862010-02-15 16:56:10 +00001552 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001553 }
1554 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001555
1556 // The mips ABIs for returning structs by value requires that we copy
1557 // the sret argument into $v0 for the return. Save the argument into
1558 // a virtual register so that we can access it from the return points.
1559 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1560 unsigned Reg = MipsFI->getSRetReturnReg();
1561 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001562 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001563 MipsFI->setSRetReturnReg(Reg);
1564 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001565 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001566 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001567 }
1568
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001569 if (isVarArg && Subtarget->isABI_O32()) {
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001570 // Record the frame index of the first variable argument
1571 // which is a value necessary to VASTART.
1572 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001573 assert(NextStackOffset % 4 == 0 &&
1574 "NextStackOffset must be aligned to 4-byte boundaries.");
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001575 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
1576 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00001577
1578 // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
1579 // copy the integer registers that have not been used for argument passing
1580 // to the caller's stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001581 for (; NextStackOffset < 16; NextStackOffset += 4) {
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001582 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001583 unsigned Idx = NextStackOffset / 4;
1584 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
1585 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Akira Hatanaka69c19f72011-05-23 20:16:59 +00001586 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001587 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
1588 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
1589 MachinePointerInfo(),
1590 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001591 }
1592 }
1593
Akira Hatanaka43299772011-05-20 23:22:14 +00001594 MipsFI->setLastInArgFI(LastFI);
1595
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001596 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001597 // the size of Ins and InVals. This only happens when on varg functions
1598 if (!OutChains.empty()) {
1599 OutChains.push_back(Chain);
1600 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1601 &OutChains[0], OutChains.size());
1602 }
1603
Dan Gohman98ca4f22009-08-05 01:29:28 +00001604 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001605}
1606
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001607//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001608// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001609//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001610
Dan Gohman98ca4f22009-08-05 01:29:28 +00001611SDValue
1612MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001613 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001614 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001615 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001616 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001617
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001618 // CCValAssign - represent the assignment of
1619 // the return value to a location
1620 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001621
1622 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001623 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1624 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001625
Dan Gohman98ca4f22009-08-05 01:29:28 +00001626 // Analize return values.
1627 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001628
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001629 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001630 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001631 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001632 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001633 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001634 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001635 }
1636
Dan Gohman475871a2008-07-27 21:46:04 +00001637 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001638
1639 // Copy the result values into the output registers.
1640 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1641 CCValAssign &VA = RVLocs[i];
1642 assert(VA.isRegLoc() && "Can only return in registers!");
1643
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001644 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001645 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001646
1647 // guarantee that all emitted copies are
1648 // stuck together, avoiding something bad
1649 Flag = Chain.getValue(1);
1650 }
1651
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001652 // The mips ABIs for returning structs by value requires that we copy
1653 // the sret argument into $v0 for the return. We saved the argument into
1654 // a virtual register in the entry block, so now we copy the value out
1655 // and into $v0.
1656 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1657 MachineFunction &MF = DAG.getMachineFunction();
1658 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1659 unsigned Reg = MipsFI->getSRetReturnReg();
1660
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001661 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00001662 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00001663 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001664
Dale Johannesena05dca42009-02-04 23:02:30 +00001665 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001666 Flag = Chain.getValue(1);
1667 }
1668
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001669 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00001670 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001671 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001672 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001673 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001674 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001675 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001676}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001677
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001678//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001679// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001680//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001681
1682/// getConstraintType - Given a constraint letter, return the type of
1683/// constraint it is for this target.
1684MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001685getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001686{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001687 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001688 // GCC config/mips/constraints.md
1689 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001690 // 'd' : An address register. Equivalent to r
1691 // unless generating MIPS16 code.
1692 // 'y' : Equivalent to r; retained for
1693 // backwards compatibility.
1694 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001695 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001696 switch (Constraint[0]) {
1697 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001698 case 'd':
1699 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001700 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001701 return C_RegisterClass;
1702 break;
1703 }
1704 }
1705 return TargetLowering::getConstraintType(Constraint);
1706}
1707
John Thompson44ab89e2010-10-29 17:29:13 +00001708/// Examine constraint type and operand type and determine a weight value.
1709/// This object must already have been set up with the operand type
1710/// and the current alternative constraint selected.
1711TargetLowering::ConstraintWeight
1712MipsTargetLowering::getSingleConstraintMatchWeight(
1713 AsmOperandInfo &info, const char *constraint) const {
1714 ConstraintWeight weight = CW_Invalid;
1715 Value *CallOperandVal = info.CallOperandVal;
1716 // If we don't have a value, we can't do a match,
1717 // but allow it at the lowest weight.
1718 if (CallOperandVal == NULL)
1719 return CW_Default;
1720 const Type *type = CallOperandVal->getType();
1721 // Look at the constraint type.
1722 switch (*constraint) {
1723 default:
1724 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
1725 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001726 case 'd':
1727 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00001728 if (type->isIntegerTy())
1729 weight = CW_Register;
1730 break;
1731 case 'f':
1732 if (type->isFloatTy())
1733 weight = CW_Register;
1734 break;
1735 }
1736 return weight;
1737}
1738
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001739/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1740/// return a list of registers that can be used to satisfy the constraint.
1741/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001742std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00001743getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001744{
1745 if (Constraint.size() == 1) {
1746 switch (Constraint[0]) {
1747 case 'r':
1748 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001749 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001750 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001751 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001752 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001753 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1754 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001755 }
1756 }
1757 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1758}
1759
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001760/// Given a register class constraint, like 'r', if this corresponds directly
1761/// to an LLVM register class, return a register of 0 and the register class
1762/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001763std::vector<unsigned> MipsTargetLowering::
1764getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001765 EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001766{
1767 if (Constraint.size() != 1)
1768 return std::vector<unsigned>();
1769
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001770 switch (Constraint[0]) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001771 default : break;
1772 case 'r':
1773 // GCC Mips Constraint Letters
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001774 case 'd':
1775 case 'y':
1776 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
1777 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
1778 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001779 Mips::T8, 0);
1780
1781 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001782 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001783 if (Subtarget->isSingleFloat())
1784 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1785 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1786 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1787 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1788 Mips::F30, Mips::F31, 0);
1789 else
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001790 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
1791 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001792 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +00001793 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001794
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001795 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001796 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001797 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
1798 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001799 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001800 }
1801 return std::vector<unsigned>();
1802}
Dan Gohman6520e202008-10-18 02:06:02 +00001803
1804bool
1805MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1806 // The Mips target isn't yet aware of offsets.
1807 return false;
1808}
Evan Chengeb2f9692009-10-27 19:56:55 +00001809
Evan Chenga1eaa3c2009-10-28 01:43:28 +00001810bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
1811 if (VT != MVT::f32 && VT != MVT::f64)
1812 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00001813 if (Imm.isNegZero())
1814 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00001815 return Imm.isZero();
1816}