blob: cde9fb33500a2cfd5bb670a51391bc2f0d1deb1b [file] [log] [blame]
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014
15#define DEBUG_TYPE "mips-lower"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000016#include "MipsISelLowering.h"
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +000017#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000018#include "MipsTargetMachine.h"
Chris Lattnerb71b9092009-08-13 06:28:06 +000019#include "MipsTargetObjectFile.h"
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000020#include "MipsSubtarget.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000021#include "llvm/DerivedTypes.h"
22#include "llvm/Function.h"
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +000023#include "llvm/GlobalVariable.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000024#include "llvm/Intrinsics.h"
25#include "llvm/CallingConv.h"
26#include "llvm/CodeGen/CallingConvLower.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000031#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000032#include "llvm/CodeGen/ValueTypes.h"
33#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000034#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000035using namespace llvm;
36
Chris Lattnerf0144122009-07-28 03:13:23 +000037const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
38 switch (Opcode) {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000039 case MipsISD::JmpLink : return "MipsISD::JmpLink";
40 case MipsISD::Hi : return "MipsISD::Hi";
41 case MipsISD::Lo : return "MipsISD::Lo";
42 case MipsISD::GPRel : return "MipsISD::GPRel";
43 case MipsISD::Ret : return "MipsISD::Ret";
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000044 case MipsISD::FPBrcond : return "MipsISD::FPBrcond";
45 case MipsISD::FPCmp : return "MipsISD::FPCmp";
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +000046 case MipsISD::CMovFP_T : return "MipsISD::CMovFP_T";
47 case MipsISD::CMovFP_F : return "MipsISD::CMovFP_F";
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +000048 case MipsISD::FPRound : return "MipsISD::FPRound";
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +000049 case MipsISD::MAdd : return "MipsISD::MAdd";
50 case MipsISD::MAddu : return "MipsISD::MAddu";
51 case MipsISD::MSub : return "MipsISD::MSub";
52 case MipsISD::MSubu : return "MipsISD::MSubu";
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +000053 case MipsISD::DivRem : return "MipsISD::DivRem";
54 case MipsISD::DivRemU : return "MipsISD::DivRemU";
Akira Hatanaka99a2e982011-04-15 19:52:08 +000055 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
56 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000057 default : return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000058 }
59}
60
61MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000062MipsTargetLowering(MipsTargetMachine &TM)
Chris Lattnerb71b9092009-08-13 06:28:06 +000063 : TargetLowering(TM, new MipsTargetObjectFile()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000064 Subtarget = &TM.getSubtarget<MipsSubtarget>();
65
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000066 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000067 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000068 setBooleanContents(ZeroOrOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000069
70 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000071 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
72 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000073
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000074 // When dealing with single precision only, use libcalls
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000075 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000076 if (!Subtarget->isFP64bit())
Owen Anderson825b72b2009-08-11 20:47:22 +000077 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000078
Wesley Peckbf17cfa2010-11-23 03:31:01 +000079 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +000080 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
81 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
82 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000083
Eli Friedman6055a6a2009-07-17 04:07:24 +000084 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +000085 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
86 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +000087
Wesley Peckbf17cfa2010-11-23 03:31:01 +000088 // Used by legalize types to correctly generate the setcc result.
89 // Without this, every float setcc comes with a AND/OR with the result,
90 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000091 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +000092 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000093
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000094 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +000095 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +000096 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000097 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
98 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
99 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
100 setOperationAction(ISD::SELECT, MVT::f32, Custom);
101 setOperationAction(ISD::SELECT, MVT::f64, Custom);
102 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000103 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
104 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
105 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000106 setOperationAction(ISD::VASTART, MVT::Other, Custom);
107
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000108 setOperationAction(ISD::SDIV, MVT::i32, Expand);
109 setOperationAction(ISD::SREM, MVT::i32, Expand);
110 setOperationAction(ISD::UDIV, MVT::i32, Expand);
111 setOperationAction(ISD::UREM, MVT::i32, Expand);
112
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000113 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000114 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
115 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
116 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
117 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
118 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
120 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
121 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
122 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000123
124 if (!Subtarget->isMips32r2())
125 setOperationAction(ISD::ROTR, MVT::i32, Expand);
126
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
128 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
129 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
130 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
131 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
132 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000133 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000135 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
137 setOperationAction(ISD::FPOW, MVT::f32, Expand);
138 setOperationAction(ISD::FLOG, MVT::f32, Expand);
139 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
140 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
141 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000142
Owen Anderson825b72b2009-08-11 20:47:22 +0000143 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000144
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000145 setOperationAction(ISD::VAARG, MVT::Other, Expand);
146 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
147 setOperationAction(ISD::VAEND, MVT::Other, Expand);
148
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000149 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000150 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
151 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
152 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000153
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000154 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000155 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000156
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000157 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
159 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000160 }
161
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000162 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000164
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000165 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000166 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000167
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000168 setTargetDAGCombine(ISD::ADDE);
169 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000170 setTargetDAGCombine(ISD::SDIVREM);
171 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000172 setTargetDAGCombine(ISD::SETCC);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000173
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000174 setStackPointerRegisterToSaveRestore(Mips::SP);
175 computeRegisterProperties();
176}
177
Owen Anderson825b72b2009-08-11 20:47:22 +0000178MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
179 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000180}
181
Bill Wendlingb4202b82009-07-01 18:50:55 +0000182/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000183unsigned MipsTargetLowering::getFunctionAlignment(const Function *) const {
184 return 2;
185}
Scott Michel5b8f82e2008-03-10 15:42:14 +0000186
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000187// SelectMadd -
188// Transforms a subgraph in CurDAG if the following pattern is found:
189// (addc multLo, Lo0), (adde multHi, Hi0),
190// where,
191// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000192// Lo0: initial value of Lo register
193// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000194// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000195static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000196 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000197 // for the matching to be successful.
198 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
199
200 if (ADDCNode->getOpcode() != ISD::ADDC)
201 return false;
202
203 SDValue MultHi = ADDENode->getOperand(0);
204 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000205 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000206 unsigned MultOpc = MultHi.getOpcode();
207
208 // MultHi and MultLo must be generated by the same node,
209 if (MultLo.getNode() != MultNode)
210 return false;
211
212 // and it must be a multiplication.
213 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
214 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000215
216 // MultLo amd MultHi must be the first and second output of MultNode
217 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000218 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
219 return false;
220
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000221 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000222 // of the values of MultNode, in which case MultNode will be removed in later
223 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000224 // If there exist users other than ADDENode or ADDCNode, this function returns
225 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000226 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000227 // produced.
228 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
229 return false;
230
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000231 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000232 DebugLoc dl = ADDENode->getDebugLoc();
233
234 // create MipsMAdd(u) node
235 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000236
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000237 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
238 MVT::Glue,
239 MultNode->getOperand(0),// Factor 0
240 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000241 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000242 ADDENode->getOperand(1));// Hi0
243
244 // create CopyFromReg nodes
245 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
246 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000247 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000248 Mips::HI, MVT::i32,
249 CopyFromLo.getValue(2));
250
251 // replace uses of adde and addc here
252 if (!SDValue(ADDCNode, 0).use_empty())
253 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
254
255 if (!SDValue(ADDENode, 0).use_empty())
256 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
257
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000258 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000259}
260
261// SelectMsub -
262// Transforms a subgraph in CurDAG if the following pattern is found:
263// (addc Lo0, multLo), (sube Hi0, multHi),
264// where,
265// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000266// Lo0: initial value of Lo register
267// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000268// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000269static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000270 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000271 // for the matching to be successful.
272 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
273
274 if (SUBCNode->getOpcode() != ISD::SUBC)
275 return false;
276
277 SDValue MultHi = SUBENode->getOperand(1);
278 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000279 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000280 unsigned MultOpc = MultHi.getOpcode();
281
282 // MultHi and MultLo must be generated by the same node,
283 if (MultLo.getNode() != MultNode)
284 return false;
285
286 // and it must be a multiplication.
287 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
288 return false;
289
290 // MultLo amd MultHi must be the first and second output of MultNode
291 // respectively.
292 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
293 return false;
294
295 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
296 // of the values of MultNode, in which case MultNode will be removed in later
297 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000298 // If there exist users other than SUBENode or SUBCNode, this function returns
299 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000300 // instruction node rather than a pair of MULT and MSUB instructions being
301 // produced.
302 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
303 return false;
304
305 SDValue Chain = CurDAG->getEntryNode();
306 DebugLoc dl = SUBENode->getDebugLoc();
307
308 // create MipsSub(u) node
309 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
310
311 SDValue MSub = CurDAG->getNode(MultOpc, dl,
312 MVT::Glue,
313 MultNode->getOperand(0),// Factor 0
314 MultNode->getOperand(1),// Factor 1
315 SUBCNode->getOperand(0),// Lo0
316 SUBENode->getOperand(0));// Hi0
317
318 // create CopyFromReg nodes
319 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
320 MSub);
321 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
322 Mips::HI, MVT::i32,
323 CopyFromLo.getValue(2));
324
325 // replace uses of sube and subc here
326 if (!SDValue(SUBCNode, 0).use_empty())
327 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
328
329 if (!SDValue(SUBENode, 0).use_empty())
330 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
331
332 return true;
333}
334
335static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
336 TargetLowering::DAGCombinerInfo &DCI,
337 const MipsSubtarget* Subtarget) {
338 if (DCI.isBeforeLegalize())
339 return SDValue();
340
341 if (Subtarget->isMips32() && SelectMadd(N, &DAG))
342 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000343
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000344 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000345}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000346
347static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
348 TargetLowering::DAGCombinerInfo &DCI,
349 const MipsSubtarget* Subtarget) {
350 if (DCI.isBeforeLegalize())
351 return SDValue();
352
353 if (Subtarget->isMips32() && SelectMsub(N, &DAG))
354 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000355
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000356 return SDValue();
357}
358
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000359static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
360 TargetLowering::DAGCombinerInfo &DCI,
361 const MipsSubtarget* Subtarget) {
362 if (DCI.isBeforeLegalizeOps())
363 return SDValue();
364
365 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
366 MipsISD::DivRemU;
367 DebugLoc dl = N->getDebugLoc();
368
369 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
370 N->getOperand(0), N->getOperand(1));
371 SDValue InChain = DAG.getEntryNode();
372 SDValue InGlue = DivRem;
373
374 // insert MFLO
375 if (N->hasAnyUseOfValue(0)) {
376 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, Mips::LO, MVT::i32,
377 InGlue);
378 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
379 InChain = CopyFromLo.getValue(1);
380 InGlue = CopyFromLo.getValue(2);
381 }
382
383 // insert MFHI
384 if (N->hasAnyUseOfValue(1)) {
385 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
386 Mips::HI, MVT::i32, InGlue);
387 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
388 }
389
390 return SDValue();
391}
392
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000393static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
394 switch (CC) {
395 default: llvm_unreachable("Unknown fp condition code!");
396 case ISD::SETEQ:
397 case ISD::SETOEQ: return Mips::FCOND_OEQ;
398 case ISD::SETUNE: return Mips::FCOND_UNE;
399 case ISD::SETLT:
400 case ISD::SETOLT: return Mips::FCOND_OLT;
401 case ISD::SETGT:
402 case ISD::SETOGT: return Mips::FCOND_OGT;
403 case ISD::SETLE:
404 case ISD::SETOLE: return Mips::FCOND_OLE;
405 case ISD::SETGE:
406 case ISD::SETOGE: return Mips::FCOND_OGE;
407 case ISD::SETULT: return Mips::FCOND_ULT;
408 case ISD::SETULE: return Mips::FCOND_ULE;
409 case ISD::SETUGT: return Mips::FCOND_UGT;
410 case ISD::SETUGE: return Mips::FCOND_UGE;
411 case ISD::SETUO: return Mips::FCOND_UN;
412 case ISD::SETO: return Mips::FCOND_OR;
413 case ISD::SETNE:
414 case ISD::SETONE: return Mips::FCOND_ONE;
415 case ISD::SETUEQ: return Mips::FCOND_UEQ;
416 }
417}
418
419
420// Returns true if condition code has to be inverted.
421static bool InvertFPCondCode(Mips::CondCode CC) {
422 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
423 return false;
424
425 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
426 return true;
427
428 assert(false && "Illegal Condition Code");
429 return false;
430}
431
432// Creates and returns an FPCmp node from a setcc node.
433// Returns Op if setcc is not a floating point comparison.
434static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
435 // must be a SETCC node
436 if (Op.getOpcode() != ISD::SETCC)
437 return Op;
438
439 SDValue LHS = Op.getOperand(0);
440
441 if (!LHS.getValueType().isFloatingPoint())
442 return Op;
443
444 SDValue RHS = Op.getOperand(1);
445 DebugLoc dl = Op.getDebugLoc();
446
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000447 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
448 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000449 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
450
451 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
452 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
453}
454
455// Creates and returns a CMovFPT/F node.
456static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
457 SDValue False, DebugLoc DL) {
458 bool invert = InvertFPCondCode((Mips::CondCode)
459 cast<ConstantSDNode>(Cond.getOperand(2))
460 ->getSExtValue());
461
462 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
463 True.getValueType(), True, False, Cond);
464}
465
466static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
467 TargetLowering::DAGCombinerInfo &DCI,
468 const MipsSubtarget* Subtarget) {
469 if (DCI.isBeforeLegalizeOps())
470 return SDValue();
471
472 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
473
474 if (Cond.getOpcode() != MipsISD::FPCmp)
475 return SDValue();
476
477 SDValue True = DAG.getConstant(1, MVT::i32);
478 SDValue False = DAG.getConstant(0, MVT::i32);
479
480 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
481}
482
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000483SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000484 const {
485 SelectionDAG &DAG = DCI.DAG;
486 unsigned opc = N->getOpcode();
487
488 switch (opc) {
489 default: break;
490 case ISD::ADDE:
491 return PerformADDECombine(N, DAG, DCI, Subtarget);
492 case ISD::SUBE:
493 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000494 case ISD::SDIVREM:
495 case ISD::UDIVREM:
496 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000497 case ISD::SETCC:
498 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000499 }
500
501 return SDValue();
502}
503
Dan Gohman475871a2008-07-27 21:46:04 +0000504SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000505LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000506{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000507 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000508 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000509 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000510 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
511 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000512 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000513 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000514 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000515 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
516 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000517 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000518 case ISD::VASTART: return LowerVASTART(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000519 }
Dan Gohman475871a2008-07-27 21:46:04 +0000520 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000521}
522
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000523//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000524// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000525//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000526
527// AddLiveIn - This helper function adds the specified physical register to the
528// MachineFunction as a live in value. It also creates a corresponding
529// virtual register for it.
530static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000531AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000532{
533 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000534 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
535 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000536 return VReg;
537}
538
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000539// Get fp branch code (not opcode) from condition code.
540static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
541 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
542 return Mips::BRANCH_T;
543
544 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
545 return Mips::BRANCH_F;
546
547 return Mips::BRANCH_INVALID;
548}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000549
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000550MachineBasicBlock *
551MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000552 MachineBasicBlock *BB) const {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000553 // There is no need to expand CMov instructions if target has
554 // conditional moves.
555 if (Subtarget->hasCondMov())
556 return BB;
557
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000558 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
559 bool isFPCmp = false;
Dale Johannesen94817572009-02-13 02:34:39 +0000560 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000561 unsigned Opc;
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000562
563 switch (MI->getOpcode()) {
564 default: assert(false && "Unexpected instr type to insert");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000565 case Mips::MOVT:
566 case Mips::MOVT_S:
567 case Mips::MOVT_D:
568 isFPCmp = true;
569 Opc = Mips::BC1F;
570 break;
571 case Mips::MOVF:
572 case Mips::MOVF_S:
573 case Mips::MOVF_D:
574 isFPCmp = true;
575 Opc = Mips::BC1T;
576 break;
577 case Mips::MOVZ_I:
578 case Mips::MOVZ_S:
579 case Mips::MOVZ_D:
580 Opc = Mips::BNE;
581 break;
582 case Mips::MOVN_I:
583 case Mips::MOVN_S:
584 case Mips::MOVN_D:
585 Opc = Mips::BEQ;
586 break;
587 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000588
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000589 // To "insert" a SELECT_CC instruction, we actually have to insert the
590 // diamond control-flow pattern. The incoming instruction knows the
591 // destination vreg to set, the condition code register to branch on, the
592 // true/false values to select between, and a branch opcode to use.
593 const BasicBlock *LLVM_BB = BB->getBasicBlock();
594 MachineFunction::iterator It = BB;
595 ++It;
Dan Gohman14152b42010-07-06 20:24:04 +0000596
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000597 // thisMBB:
598 // ...
599 // TrueVal = ...
600 // setcc r1, r2, r3
601 // bNE r1, r0, copy1MBB
602 // fallthrough --> copy0MBB
603 MachineBasicBlock *thisMBB = BB;
604 MachineFunction *F = BB->getParent();
605 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
606 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
607 F->insert(It, copy0MBB);
608 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +0000609
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000610 // Transfer the remainder of BB and its successor edges to sinkMBB.
611 sinkMBB->splice(sinkMBB->begin(), BB,
612 llvm::next(MachineBasicBlock::iterator(MI)),
613 BB->end());
614 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000615
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000616 // Next, add the true and fallthrough blocks as its successors.
617 BB->addSuccessor(copy0MBB);
618 BB->addSuccessor(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000619
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000620 // Emit the right instruction according to the type of the operands compared
621 if (isFPCmp)
622 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
623 else
624 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
625 .addReg(Mips::ZERO).addMBB(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000626
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000627
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000628 // copy0MBB:
629 // %FalseValue = ...
630 // # fallthrough to sinkMBB
631 BB = copy0MBB;
632
633 // Update machine-CFG edges
634 BB->addSuccessor(sinkMBB);
635
636 // sinkMBB:
637 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
638 // ...
639 BB = sinkMBB;
640
641 if (isFPCmp)
Dan Gohman14152b42010-07-06 20:24:04 +0000642 BuildMI(*BB, BB->begin(), dl,
643 TII->get(Mips::PHI), MI->getOperand(0).getReg())
Bruno Cardoso Lopes29e9daa2010-07-20 07:58:51 +0000644 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000645 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
646 else
647 BuildMI(*BB, BB->begin(), dl,
648 TII->get(Mips::PHI), MI->getOperand(0).getReg())
649 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
650 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000651
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000652 MI->eraseFromParent(); // The pseudo instruction is gone now.
653 return BB;
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000654}
655
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000656//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000657// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000658//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000659
Dan Gohman475871a2008-07-27 21:46:04 +0000660SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000661LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000662{
663 if (!Subtarget->isMips1())
664 return Op;
665
666 MachineFunction &MF = DAG.getMachineFunction();
667 unsigned CCReg = AddLiveIn(MF, Mips::FCR31, Mips::CCRRegisterClass);
668
669 SDValue Chain = DAG.getEntryNode();
670 DebugLoc dl = Op.getDebugLoc();
671 SDValue Src = Op.getOperand(0);
672
673 // Set the condition register
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 SDValue CondReg = DAG.getCopyFromReg(Chain, dl, CCReg, MVT::i32);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000675 CondReg = DAG.getCopyToReg(Chain, dl, Mips::AT, CondReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 CondReg = DAG.getCopyFromReg(CondReg, dl, Mips::AT, MVT::i32);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000677
Owen Anderson825b72b2009-08-11 20:47:22 +0000678 SDValue Cst = DAG.getConstant(3, MVT::i32);
679 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, CondReg, Cst);
680 Cst = DAG.getConstant(2, MVT::i32);
681 SDValue Xor = DAG.getNode(ISD::XOR, dl, MVT::i32, Or, Cst);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000682
683 SDValue InFlag(0, 0);
684 CondReg = DAG.getCopyToReg(Chain, dl, Mips::FCR31, Xor, InFlag);
685
686 // Emit the round instruction and bit convert to integer
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 SDValue Trunc = DAG.getNode(MipsISD::FPRound, dl, MVT::f32,
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000688 Src, CondReg.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000689 SDValue BitCvt = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Trunc);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000690 return BitCvt;
691}
692
693SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000694LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000695{
696 SDValue Chain = Op.getOperand(0);
697 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +0000698 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000699
700 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000702
703 // Subtract the dynamic size from the actual stack size to
704 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +0000705 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000706
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000707 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000708 // must be placed in the stack pointer register.
Dale Johannesena05dca42009-02-04 23:02:30 +0000709 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000710
711 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000712 // value and a chain
713 SDValue Ops[2] = { Sub, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000714 return DAG.getMergeValues(Ops, 2, dl);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000715}
716
717SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000718LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000719{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000720 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000721 // the block to branch to if the condition is true.
722 SDValue Chain = Op.getOperand(0);
723 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000724 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000725
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000726 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
727
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000728 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000729 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000730 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000731
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000732 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000733 Mips::CondCode CC =
734 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000735 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000736
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000737 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000738 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000739}
740
741SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000742LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000743{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000744 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000745
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000746 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000747 if (Cond.getOpcode() != MipsISD::FPCmp)
748 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000749
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000750 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
751 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000752}
753
Dan Gohmand858e902010-04-17 15:26:15 +0000754SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
755 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +0000756 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000757 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000758 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000759
Eli Friedmane2c74082009-08-03 02:22:28 +0000760 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +0000761 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000762
Chris Lattnerb71b9092009-08-13 06:28:06 +0000763 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000764
Chris Lattnere3736f82009-08-13 05:41:27 +0000765 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000766 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
767 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000768 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +0000769 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
770 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000771 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +0000772 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000773 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000774 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
775 MipsII::MO_ABS_HI);
776 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
777 MipsII::MO_ABS_LO);
778 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
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, HiPart, Lo);
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000781 } else {
Devang Patel0d881da2010-07-06 22:08:15 +0000782 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000783 MipsII::MO_GOT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000784 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000785 DAG.getEntryNode(), GA, MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000786 false, false, 0);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000787 // On functions and global targets not internal linked only
788 // a load from got/GP is necessary for PIC to work.
Akira Hatanaka9777e7a2011-04-07 19:51:44 +0000789 if (!GV->hasInternalLinkage() &&
790 (!GV->hasLocalLinkage() || isa<Function>(GV)))
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000791 return ResNode;
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000792 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
793 MipsII::MO_ABS_LO);
794 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000795 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000796 }
797
Torok Edwinc23197a2009-07-14 16:55:14 +0000798 llvm_unreachable("Dont know how to handle GlobalAddress");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000799 return SDValue(0,0);
800}
801
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000802SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
803 SelectionDAG &DAG) const {
804 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
805 assert(false && "implement LowerBlockAddress for -static");
806 return SDValue(0, 0);
807 }
808 else {
809 // FIXME there isn't actually debug info here
810 DebugLoc dl = Op.getDebugLoc();
811 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
812 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
813 MipsII::MO_GOT);
814 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000815 MipsII::MO_ABS_LO);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000816 SDValue Load = DAG.getLoad(MVT::i32, dl,
817 DAG.getEntryNode(), BAGOTOffset,
818 MachinePointerInfo(), false, false, 0);
819 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
820 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
821 }
822}
823
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000824SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000825LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000826{
Torok Edwinc23197a2009-07-14 16:55:14 +0000827 llvm_unreachable("TLS not implemented for MIPS.");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000828 return SDValue(); // Not reached
829}
830
831SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000832LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000833{
Dan Gohman475871a2008-07-27 21:46:04 +0000834 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000835 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +0000836 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000837 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000838 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000839 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000840
Owen Andersone50ed302009-08-10 22:56:29 +0000841 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000842 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000843
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000844 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
845
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +0000846 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +0000847 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000848 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000849 } else // Emit Load from Global Pointer
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000850 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
851 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000852 false, false, 0);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000853
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000854 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
855 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000856 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000857 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000858
859 return ResNode;
860}
861
Dan Gohman475871a2008-07-27 21:46:04 +0000862SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000863LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000864{
Dan Gohman475871a2008-07-27 21:46:04 +0000865 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000866 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +0000867 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +0000868 // FIXME there isn't actually debug info here
869 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000870
871 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000872 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000873 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000874 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000875 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +0000876 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000877 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
878 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000879 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000880
881 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000882 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000883 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000884 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000885 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000886 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
887 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000889 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000890 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000891 N->getOffset(), MipsII::MO_GOT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000892 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000893 CP, MachinePointerInfo::getConstantPool(),
894 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000895 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000896 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000897 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000898 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
899 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000900
901 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000902}
903
Dan Gohmand858e902010-04-17 15:26:15 +0000904SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +0000905 MachineFunction &MF = DAG.getMachineFunction();
906 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
907
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000908 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +0000909 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
910 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000911
912 // vastart just stores the address of the VarArgsFrameIndex slot into the
913 // memory location argument.
914 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +0000915 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
916 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +0000917 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000918}
919
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000920//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000921// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000922//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000923
924#include "MipsGenCallingConv.inc"
925
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000926//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000927// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000928// Mips O32 ABI rules:
929// ---
930// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000931// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000932// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000933// f64 - Only passed in two aliased f32 registers if no int reg has been used
934// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000935// not used, it must be shadowed. If only A3 is avaiable, shadow it and
936// go to stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000937//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000938
Duncan Sands1e96bab2010-11-04 10:49:57 +0000939static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000940 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000941 ISD::ArgFlagsTy ArgFlags, CCState &State) {
942
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000943 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000944
945 static const unsigned IntRegs[] = {
946 Mips::A0, Mips::A1, Mips::A2, Mips::A3
947 };
948 static const unsigned F32Regs[] = {
949 Mips::F12, Mips::F14
950 };
951 static const unsigned F64Regs[] = {
952 Mips::D6, Mips::D7
953 };
954
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000955 unsigned Reg = 0;
956 static bool IntRegUsed = false;
957
958 // This must be the first arg of the call if no regs have been allocated.
959 // Initialize IntRegUsed in that case.
960 if (IntRegs[State.getFirstUnallocated(IntRegs, IntRegsSize)] == Mips::A0 &&
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000961 F32Regs[State.getFirstUnallocated(F32Regs, FloatRegsSize)] == Mips::F12 &&
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000962 F64Regs[State.getFirstUnallocated(F64Regs, FloatRegsSize)] == Mips::D6)
963 IntRegUsed = false;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000964
965 // Promote i8 and i16
Owen Anderson825b72b2009-08-11 20:47:22 +0000966 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
967 LocVT = MVT::i32;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000968 if (ArgFlags.isSExt())
969 LocInfo = CCValAssign::SExt;
970 else if (ArgFlags.isZExt())
971 LocInfo = CCValAssign::ZExt;
972 else
973 LocInfo = CCValAssign::AExt;
974 }
975
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000976 if (ValVT == MVT::i32) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000977 Reg = State.AllocateReg(IntRegs, IntRegsSize);
978 IntRegUsed = true;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000979 } else if (ValVT == MVT::f32) {
980 // An int reg has to be marked allocated regardless of whether or not
981 // IntRegUsed is true.
982 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000983
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000984 if (IntRegUsed) {
985 if (Reg) // Int reg is available
986 LocVT = MVT::i32;
987 } else {
988 unsigned FReg = State.AllocateReg(F32Regs, FloatRegsSize);
989 if (FReg) // F32 reg is available
990 Reg = FReg;
991 else if (Reg) // No F32 regs are available, but an int reg is available.
992 LocVT = MVT::i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000993 }
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000994 } else if (ValVT == MVT::f64) {
995 // Int regs have to be marked allocated regardless of whether or not
996 // IntRegUsed is true.
997 Reg = State.AllocateReg(IntRegs, IntRegsSize);
998 if (Reg == Mips::A1)
999 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1000 else if (Reg == Mips::A3)
1001 Reg = 0;
1002 State.AllocateReg(IntRegs, IntRegsSize);
1003
1004 // At this point, Reg is A0, A2 or 0, and all the unavailable integer regs
1005 // are marked as allocated.
1006 if (IntRegUsed) {
1007 if (Reg)// if int reg is available
1008 LocVT = MVT::i32;
1009 } else {
1010 unsigned FReg = State.AllocateReg(F64Regs, FloatRegsSize);
1011 if (FReg) // F64 reg is available.
1012 Reg = FReg;
1013 else if (Reg) // No F64 regs are available, but an int reg is available.
1014 LocVT = MVT::i32;
1015 }
1016 } else
1017 assert(false && "cannot handle this ValVT");
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001018
1019 if (!Reg) {
1020 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1021 unsigned Offset = State.AllocateStack(SizeInBytes, SizeInBytes);
1022 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
1023 } else
1024 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
1025
1026 return false; // CC must always match
1027}
1028
Duncan Sands1e96bab2010-11-04 10:49:57 +00001029static bool CC_MipsO32_VarArgs(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001030 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001031 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1032
1033 static const unsigned IntRegsSize=4;
1034
1035 static const unsigned IntRegs[] = {
1036 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1037 };
1038
1039 // Promote i8 and i16
1040 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1041 LocVT = MVT::i32;
1042 if (ArgFlags.isSExt())
1043 LocInfo = CCValAssign::SExt;
1044 else if (ArgFlags.isZExt())
1045 LocInfo = CCValAssign::ZExt;
1046 else
1047 LocInfo = CCValAssign::AExt;
1048 }
1049
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001050 unsigned Reg;
1051
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001052 if (ValVT == MVT::i32 || ValVT == MVT::f32) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001053 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1054 LocVT = MVT::i32;
1055 } else if (ValVT == MVT::f64) {
1056 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1057 if (Reg == Mips::A1 || Reg == Mips::A3)
1058 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1059 State.AllocateReg(IntRegs, IntRegsSize);
1060 LocVT = MVT::i32;
1061 } else
1062 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001063
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001064 if (!Reg) {
1065 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1066 unsigned Offset = State.AllocateStack(SizeInBytes, SizeInBytes);
1067 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
1068 } else
1069 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001070
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001071 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001072}
1073
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001074//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001075// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001076//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001077
Dan Gohman98ca4f22009-08-05 01:29:28 +00001078/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001079/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001080/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001081SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001082MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001083 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001084 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001085 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001086 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001087 const SmallVectorImpl<ISD::InputArg> &Ins,
1088 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001089 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001090 // MIPs target does not yet support tail call optimization.
1091 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001092
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001093 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001094 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001095 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001096
1097 // Analyze operands of the call, assigning locations to each operand.
1098 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001099 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1100 *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001101
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +00001102 // To meet O32 ABI, Mips must always allocate 16 bytes on
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001103 // the stack (even if less than 4 are used as arguments)
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +00001104 if (Subtarget->isABI_O32()) {
Duncan Sands1e96bab2010-11-04 10:49:57 +00001105 int VTsize = MVT(MVT::i32).getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +00001106 MFI->CreateFixedObject(VTsize, (VTsize*3), true);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001107 CCInfo.AnalyzeCallOperands(Outs,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001108 isVarArg ? CC_MipsO32_VarArgs : CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001109 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001110 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001111
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001112 // Get a count of how many bytes are to be pushed on the stack.
1113 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnere563bbc2008-10-11 22:08:30 +00001114 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001115
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001116 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001117 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1118 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001119
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001120 // First/LastArgStackLoc contains the first/last
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001121 // "at stack" argument location.
1122 int LastArgStackLoc = 0;
1123 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001124
1125 // Walk the register/memloc assignments, inserting copies/loads.
1126 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001127 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001128 CCValAssign &VA = ArgLocs[i];
1129
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001130 // Promote the value if needed.
1131 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001132 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001133 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001134 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001135 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001136 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001137 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001138 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1139 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001140 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1141 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001142 if (!Subtarget->isLittle())
1143 std::swap(Lo, Hi);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001144 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
1145 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
1146 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001147 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001148 }
1149 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00001150 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001151 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001152 break;
1153 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001154 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001155 break;
1156 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001157 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001158 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001159 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001160
1161 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001162 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001163 if (VA.isRegLoc()) {
1164 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00001165 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001166 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001167
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001168 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00001169 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001170
Chris Lattnere0b12152008-03-17 06:57:02 +00001171 // Create the frame index object for this incoming parameter
1172 // This guarantees that when allocating Local Area the firsts
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001173 // 16 bytes which are alwayes reserved won't be overwritten
1174 // if O32 ABI is used. For EABI the first address is zero.
1175 LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001176 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001177 LastArgStackLoc, true);
Chris Lattnere0b12152008-03-17 06:57:02 +00001178
Dan Gohman475871a2008-07-27 21:46:04 +00001179 SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00001180
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001181 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00001182 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00001183 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
1184 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001185 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001186 }
1187
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001188 // Transform all store nodes into one single node because all store
1189 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001190 if (!MemOpChains.empty())
1191 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001192 &MemOpChains[0], MemOpChains.size());
1193
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001194 // Build a sequence of copy-to-reg nodes chained together with token
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001195 // chain and flag operands which copy the outgoing args into registers.
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001196 // The InFlag in necessary since all emitted instructions must be
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001197 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +00001198 SDValue InFlag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001199 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001200 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001201 RegsToPass[i].second, InFlag);
1202 InFlag = Chain.getValue(1);
1203 }
1204
Bill Wendling056292f2008-09-16 21:48:12 +00001205 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001206 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1207 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001208 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001209 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001210 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001211
1212 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001213 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
1214 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1215 getPointerTy(), 0,MipsII:: MO_GOT);
1216 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
1217 0, MipsII::MO_ABS_LO);
1218 } else {
1219 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1220 getPointerTy(), 0, OpFlag);
1221 }
1222
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001223 LoadSymAddr = true;
1224 }
1225 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001226 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001227 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001228 LoadSymAddr = true;
1229 }
1230
1231 // Create nodes that load address of callee and copy it to T9
1232 if (IsPIC) {
1233 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001234 // Load callee address
1235 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, Chain, Callee,
1236 MachinePointerInfo::getGOT(),
1237 false, false, 0);
1238
1239 // Use GOT+LO if callee has internal linkage.
1240 if (CalleeLo.getNode()) {
1241 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
1242 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
1243 } else
1244 Callee = LoadValue;
1245
1246 // Use chain output from LoadValue
1247 Chain = LoadValue.getValue(1);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001248 }
1249
1250 // copy to T9
1251 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
1252 InFlag = Chain.getValue(1);
1253 Callee = DAG.getRegister(Mips::T9, MVT::i32);
1254 }
Bill Wendling056292f2008-09-16 21:48:12 +00001255
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001256 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001257 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001258 //
1259 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001260 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00001261 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001262 Ops.push_back(Chain);
1263 Ops.push_back(Callee);
1264
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001265 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001266 // known live into the call.
1267 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1268 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1269 RegsToPass[i].second.getValueType()));
1270
Gabor Greifba36cb52008-08-28 21:40:38 +00001271 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001272 Ops.push_back(InFlag);
1273
Dale Johannesen33c960f2009-02-04 20:06:27 +00001274 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001275 InFlag = Chain.getValue(1);
1276
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001277 // Create a stack location to hold GP when PIC is used. This stack
1278 // location is used on function prologue to save GP and also after all
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001279 // emitted CALL's to restore GP.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001280 if (IsPIC) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001281 // Function can have an arbitrary number of calls, so
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001282 // hold the LastArgStackLoc with the biggest offset.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001283 int FI;
1284 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001285 if (LastArgStackLoc >= MipsFI->getGPStackOffset()) {
1286 LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001287 // Create the frame index only once. SPOffset here can be anything
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001288 // (this will be fixed on processFunctionBeforeFrameFinalized)
1289 if (MipsFI->getGPStackOffset() == -1) {
Evan Chenged2ae132010-07-03 00:40:23 +00001290 FI = MFI->CreateFixedObject(4, 0, true);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001291 MipsFI->setGPFI(FI);
1292 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001293 MipsFI->setGPStackOffset(LastArgStackLoc);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001294 }
1295
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001296 // Reload GP value.
1297 FI = MipsFI->getGPFI();
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001298 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1299 SDValue GPLoad = DAG.getLoad(MVT::i32, dl, Chain, FIN,
1300 MachinePointerInfo::getFixedStack(FI),
David Greenef6fa1862010-02-15 16:56:10 +00001301 false, false, 0);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001302 Chain = GPLoad.getValue(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001303 Chain = DAG.getCopyToReg(Chain, dl, DAG.getRegister(Mips::GP, MVT::i32),
Dan Gohman475871a2008-07-27 21:46:04 +00001304 GPLoad, SDValue(0,0));
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +00001305 InFlag = Chain.getValue(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001306 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001307
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00001308 // Create the CALLSEQ_END node.
1309 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1310 DAG.getIntPtrConstant(0, true), InFlag);
1311 InFlag = Chain.getValue(1);
1312
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001313 // Handle result values, copying them out of physregs into vregs that we
1314 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001315 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
1316 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001317}
1318
Dan Gohman98ca4f22009-08-05 01:29:28 +00001319/// LowerCallResult - Lower the result values of a call into the
1320/// appropriate copies out of appropriate physical registers.
1321SDValue
1322MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001323 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001324 const SmallVectorImpl<ISD::InputArg> &Ins,
1325 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001326 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001327
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001328 // Assign locations to each value returned by this call.
1329 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001330 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001331 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001332
Dan Gohman98ca4f22009-08-05 01:29:28 +00001333 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001334
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001335 // Copy all of the result registers out of their specified physreg.
1336 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001337 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001338 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001339 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001340 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001341 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001342
Dan Gohman98ca4f22009-08-05 01:29:28 +00001343 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001344}
1345
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001346//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001347// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001348//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001349
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001350/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001351/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001352SDValue
1353MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001354 CallingConv::ID CallConv,
1355 bool isVarArg,
1356 const SmallVectorImpl<ISD::InputArg>
1357 &Ins,
1358 DebugLoc dl, SelectionDAG &DAG,
1359 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001360 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001361
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00001362 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001363 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001364 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001365
Dan Gohman1e93df62010-04-17 14:41:14 +00001366 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001367
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001368 // Used with vargs to acumulate store chains.
1369 std::vector<SDValue> OutChains;
1370
1371 // Keep track of the last register used for arguments
1372 unsigned ArgRegEnd = 0;
1373
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001374 // Assign locations to all of the incoming arguments.
1375 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001376 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1377 ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001378
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001379 if (Subtarget->isABI_O32())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001380 CCInfo.AnalyzeFormalArguments(Ins,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001381 isVarArg ? CC_MipsO32_VarArgs : CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001382 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001383 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001384
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001385 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
Chris Lattner109d6db2011-03-11 02:12:51 +00001386 unsigned LastStackArgEndOffset = 0;
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001387 EVT LastRegArgValVT;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001388
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001389 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001390 CCValAssign &VA = ArgLocs[i];
1391
1392 // Arguments stored on registers
1393 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001394 EVT RegVT = VA.getLocVT();
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001395 ArgRegEnd = VA.getLocReg();
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001396 LastRegArgValVT = VA.getValVT();
Bill Wendling06b8c192008-07-09 05:55:53 +00001397 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001398
Owen Anderson825b72b2009-08-11 20:47:22 +00001399 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001400 RC = Mips::CPURegsRegisterClass;
1401 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001402 RC = Mips::FGR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001403 else if (RegVT == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001404 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001405 RC = Mips::AFGR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001406 } else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001407 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001408
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001409 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001410 // physical registers into virtual ones
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001411 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegEnd, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001412 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001413
1414 // If this is an 8 or 16-bit value, it has been passed promoted
1415 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001416 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001417 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00001418 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001419 if (VA.getLocInfo() == CCValAssign::SExt)
1420 Opcode = ISD::AssertSext;
1421 else if (VA.getLocInfo() == CCValAssign::ZExt)
1422 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00001423 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001424 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00001425 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00001426 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001427 }
1428
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001429 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001430 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001431 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
1432 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00001433 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001434 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001435 VA.getLocReg()+1, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001436 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001437 if (!Subtarget->isLittle())
1438 std::swap(ArgValue, ArgValue2);
1439 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
1440 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001441 }
1442 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001443
Dan Gohman98ca4f22009-08-05 01:29:28 +00001444 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001445 } else { // VA.isRegLoc()
1446
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001447 // sanity check
1448 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001449
1450 // The last argument is not a register anymore
1451 ArgRegEnd = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001452
1453 // The stack pointer offset is relative to the caller stack frame.
1454 // Since the real stack size is unknown here, a negative SPOffset
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001455 // is used so there's a way to adjust these offsets when the stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001456 // size get known (on EliminateFrameIndex). A dummy SPOffset is
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001457 // used instead of a direct negative address (which is recorded to
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001458 // be used on emitPrologue) to avoid mis-calc of the first stack
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001459 // offset on PEI::calculateFrameObjectOffsets.
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001460 unsigned ArgSize = VA.getValVT().getSizeInBits()/8;
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001461 LastStackArgEndOffset = FirstStackArgLoc + VA.getLocMemOffset() + ArgSize;
Evan Chenged2ae132010-07-03 00:40:23 +00001462 int FI = MFI->CreateFixedObject(ArgSize, 0, true);
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001463 MipsFI->recordLoadArgsFI(FI, -(4 +
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001464 (FirstStackArgLoc + VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001465
1466 // Create load nodes to retrieve arguments from the stack
Dan Gohman475871a2008-07-27 21:46:04 +00001467 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001468 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
1469 MachinePointerInfo::getFixedStack(FI),
David Greenef6fa1862010-02-15 16:56:10 +00001470 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001471 }
1472 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001473
1474 // The mips ABIs for returning structs by value requires that we copy
1475 // the sret argument into $v0 for the return. Save the argument into
1476 // a virtual register so that we can access it from the return points.
1477 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1478 unsigned Reg = MipsFI->getSRetReturnReg();
1479 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001480 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001481 MipsFI->setSRetReturnReg(Reg);
1482 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001483 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001484 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001485 }
1486
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001487 // To meet ABI, when VARARGS are passed on registers, the registers
1488 // must have their values written to the caller stack frame. If the last
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001489 // argument was placed in the stack, there's no need to save any register.
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001490 if (isVarArg && Subtarget->isABI_O32()) {
1491 if (ArgRegEnd) {
1492 // Last named formal argument is passed in register.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001493
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001494 // The last register argument that must be saved is Mips::A3
1495 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
1496 if (LastRegArgValVT == MVT::f64)
1497 ArgRegEnd++;
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001498
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001499 if (ArgRegEnd < Mips::A3) {
1500 // Both the last named formal argument and the first variable
1501 // argument are passed in registers.
1502 for (++ArgRegEnd; ArgRegEnd <= Mips::A3; ++ArgRegEnd) {
1503 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegEnd, RC);
1504 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001505
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001506 int FI = MFI->CreateFixedObject(4, 0, true);
1507 MipsFI->recordStoreVarArgsFI(FI, -(4+(ArgRegEnd-Mips::A0)*4));
1508 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
1509 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
1510 MachinePointerInfo(),
1511 false, false, 0));
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001512
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001513 // Record the frame index of the first variable argument
1514 // which is a value necessary to VASTART.
1515 if (!MipsFI->getVarArgsFrameIndex()) {
1516 MFI->setObjectAlignment(FI, 4);
1517 MipsFI->setVarArgsFrameIndex(FI);
1518 }
1519 }
1520 } else {
1521 // Last named formal argument is in register Mips::A3, and the first
1522 // variable argument is on stack. Record the frame index of the first
1523 // variable argument.
1524 int FI = MFI->CreateFixedObject(4, 0, true);
1525 MFI->setObjectAlignment(FI, 4);
1526 MipsFI->recordStoreVarArgsFI(FI, -20);
Dan Gohman1e93df62010-04-17 14:41:14 +00001527 MipsFI->setVarArgsFrameIndex(FI);
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001528 }
1529 } else {
1530 // Last named formal argument and all the variable arguments are passed
1531 // on stack. Record the frame index of the first variable argument.
1532 int FI = MFI->CreateFixedObject(4, 0, true);
1533 MFI->setObjectAlignment(FI, 4);
1534 MipsFI->recordStoreVarArgsFI(FI, -(4+LastStackArgEndOffset));
1535 MipsFI->setVarArgsFrameIndex(FI);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001536 }
1537 }
1538
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001539 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001540 // the size of Ins and InVals. This only happens when on varg functions
1541 if (!OutChains.empty()) {
1542 OutChains.push_back(Chain);
1543 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1544 &OutChains[0], OutChains.size());
1545 }
1546
Dan Gohman98ca4f22009-08-05 01:29:28 +00001547 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001548}
1549
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001550//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001551// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001552//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001553
Dan Gohman98ca4f22009-08-05 01:29:28 +00001554SDValue
1555MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001556 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001557 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001558 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001559 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001560
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001561 // CCValAssign - represent the assignment of
1562 // the return value to a location
1563 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001564
1565 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001566 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1567 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001568
Dan Gohman98ca4f22009-08-05 01:29:28 +00001569 // Analize return values.
1570 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001571
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001572 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001573 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001574 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001575 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001576 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001577 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001578 }
1579
Dan Gohman475871a2008-07-27 21:46:04 +00001580 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001581
1582 // Copy the result values into the output registers.
1583 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1584 CCValAssign &VA = RVLocs[i];
1585 assert(VA.isRegLoc() && "Can only return in registers!");
1586
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001587 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001588 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001589
1590 // guarantee that all emitted copies are
1591 // stuck together, avoiding something bad
1592 Flag = Chain.getValue(1);
1593 }
1594
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001595 // The mips ABIs for returning structs by value requires that we copy
1596 // the sret argument into $v0 for the return. We saved the argument into
1597 // a virtual register in the entry block, so now we copy the value out
1598 // and into $v0.
1599 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1600 MachineFunction &MF = DAG.getMachineFunction();
1601 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1602 unsigned Reg = MipsFI->getSRetReturnReg();
1603
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001604 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00001605 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00001606 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001607
Dale Johannesena05dca42009-02-04 23:02:30 +00001608 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001609 Flag = Chain.getValue(1);
1610 }
1611
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001612 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00001613 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001614 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001615 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001616 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001617 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001618 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001619}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001620
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001621//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001622// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001623//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001624
1625/// getConstraintType - Given a constraint letter, return the type of
1626/// constraint it is for this target.
1627MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001628getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001629{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001630 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001631 // GCC config/mips/constraints.md
1632 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001633 // 'd' : An address register. Equivalent to r
1634 // unless generating MIPS16 code.
1635 // 'y' : Equivalent to r; retained for
1636 // backwards compatibility.
1637 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001638 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001639 switch (Constraint[0]) {
1640 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001641 case 'd':
1642 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001643 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001644 return C_RegisterClass;
1645 break;
1646 }
1647 }
1648 return TargetLowering::getConstraintType(Constraint);
1649}
1650
John Thompson44ab89e2010-10-29 17:29:13 +00001651/// Examine constraint type and operand type and determine a weight value.
1652/// This object must already have been set up with the operand type
1653/// and the current alternative constraint selected.
1654TargetLowering::ConstraintWeight
1655MipsTargetLowering::getSingleConstraintMatchWeight(
1656 AsmOperandInfo &info, const char *constraint) const {
1657 ConstraintWeight weight = CW_Invalid;
1658 Value *CallOperandVal = info.CallOperandVal;
1659 // If we don't have a value, we can't do a match,
1660 // but allow it at the lowest weight.
1661 if (CallOperandVal == NULL)
1662 return CW_Default;
1663 const Type *type = CallOperandVal->getType();
1664 // Look at the constraint type.
1665 switch (*constraint) {
1666 default:
1667 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
1668 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001669 case 'd':
1670 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00001671 if (type->isIntegerTy())
1672 weight = CW_Register;
1673 break;
1674 case 'f':
1675 if (type->isFloatTy())
1676 weight = CW_Register;
1677 break;
1678 }
1679 return weight;
1680}
1681
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001682/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1683/// return a list of registers that can be used to satisfy the constraint.
1684/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001685std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00001686getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001687{
1688 if (Constraint.size() == 1) {
1689 switch (Constraint[0]) {
1690 case 'r':
1691 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001692 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001693 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001694 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001695 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001696 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1697 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001698 }
1699 }
1700 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1701}
1702
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001703/// Given a register class constraint, like 'r', if this corresponds directly
1704/// to an LLVM register class, return a register of 0 and the register class
1705/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001706std::vector<unsigned> MipsTargetLowering::
1707getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001708 EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001709{
1710 if (Constraint.size() != 1)
1711 return std::vector<unsigned>();
1712
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001713 switch (Constraint[0]) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001714 default : break;
1715 case 'r':
1716 // GCC Mips Constraint Letters
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001717 case 'd':
1718 case 'y':
1719 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
1720 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
1721 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001722 Mips::T8, 0);
1723
1724 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001725 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001726 if (Subtarget->isSingleFloat())
1727 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1728 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1729 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1730 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1731 Mips::F30, Mips::F31, 0);
1732 else
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001733 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
1734 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001735 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +00001736 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001737
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001738 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001739 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001740 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
1741 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001742 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001743 }
1744 return std::vector<unsigned>();
1745}
Dan Gohman6520e202008-10-18 02:06:02 +00001746
1747bool
1748MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1749 // The Mips target isn't yet aware of offsets.
1750 return false;
1751}
Evan Chengeb2f9692009-10-27 19:56:55 +00001752
Evan Chenga1eaa3c2009-10-28 01:43:28 +00001753bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
1754 if (VT != MVT::f32 && VT != MVT::f64)
1755 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00001756 if (Imm.isNegZero())
1757 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00001758 return Imm.isZero();
1759}