blob: e5dfb253f551e9113af02e03191bde88e792228f [file] [log] [blame]
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014
15#define DEBUG_TYPE "mips-lower"
Akira Hatanakab4d8d312011-05-24 00:23:52 +000016//#include <algorithm>
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000017#include "MipsISelLowering.h"
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +000018#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000019#include "MipsTargetMachine.h"
Chris Lattnerb71b9092009-08-13 06:28:06 +000020#include "MipsTargetObjectFile.h"
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000021#include "MipsSubtarget.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000022#include "llvm/DerivedTypes.h"
23#include "llvm/Function.h"
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +000024#include "llvm/GlobalVariable.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000025#include "llvm/Intrinsics.h"
26#include "llvm/CallingConv.h"
27#include "llvm/CodeGen/CallingConvLower.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000032#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033#include "llvm/CodeGen/ValueTypes.h"
34#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000036using namespace llvm;
37
Chris Lattnerf0144122009-07-28 03:13:23 +000038const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
39 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000040 case MipsISD::JmpLink: return "MipsISD::JmpLink";
41 case MipsISD::Hi: return "MipsISD::Hi";
42 case MipsISD::Lo: return "MipsISD::Lo";
43 case MipsISD::GPRel: return "MipsISD::GPRel";
44 case MipsISD::Ret: return "MipsISD::Ret";
45 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
46 case MipsISD::FPCmp: return "MipsISD::FPCmp";
47 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
48 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
49 case MipsISD::FPRound: return "MipsISD::FPRound";
50 case MipsISD::MAdd: return "MipsISD::MAdd";
51 case MipsISD::MAddu: return "MipsISD::MAddu";
52 case MipsISD::MSub: return "MipsISD::MSub";
53 case MipsISD::MSubu: return "MipsISD::MSubu";
54 case MipsISD::DivRem: return "MipsISD::DivRem";
55 case MipsISD::DivRemU: return "MipsISD::DivRemU";
56 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
57 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanaka342837d2011-05-28 01:07:07 +000058 case MipsISD::WrapperPIC: return "MipsISD::WrapperPIC";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000059 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000060 }
61}
62
63MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000064MipsTargetLowering(MipsTargetMachine &TM)
Chris Lattnerb71b9092009-08-13 06:28:06 +000065 : TargetLowering(TM, new MipsTargetObjectFile()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000066 Subtarget = &TM.getSubtarget<MipsSubtarget>();
67
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000068 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000069 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000070 setBooleanContents(ZeroOrOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000071
72 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000073 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
74 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000075
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000076 // When dealing with single precision only, use libcalls
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000077 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000078 if (!Subtarget->isFP64bit())
Owen Anderson825b72b2009-08-11 20:47:22 +000079 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000080
Wesley Peckbf17cfa2010-11-23 03:31:01 +000081 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +000082 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
83 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
84 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000085
Eli Friedman6055a6a2009-07-17 04:07:24 +000086 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +000087 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
88 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +000089
Wesley Peckbf17cfa2010-11-23 03:31:01 +000090 // Used by legalize types to correctly generate the setcc result.
91 // Without this, every float setcc comes with a AND/OR with the result,
92 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000093 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +000094 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000095
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000096 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +000097 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +000098 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000099 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
100 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
101 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
102 setOperationAction(ISD::SELECT, MVT::f32, Custom);
103 setOperationAction(ISD::SELECT, MVT::f64, Custom);
104 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000105 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
106 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000107 setOperationAction(ISD::VASTART, MVT::Other, Custom);
108
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000109 setOperationAction(ISD::SDIV, MVT::i32, Expand);
110 setOperationAction(ISD::SREM, MVT::i32, Expand);
111 setOperationAction(ISD::UDIV, MVT::i32, Expand);
112 setOperationAction(ISD::UREM, MVT::i32, Expand);
113
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000114 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000115 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
116 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
117 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
118 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
119 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
121 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
122 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
123 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000124
125 if (!Subtarget->isMips32r2())
126 setOperationAction(ISD::ROTR, MVT::i32, Expand);
127
Owen Anderson825b72b2009-08-11 20:47:22 +0000128 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
129 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
130 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000131 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
132 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000133 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000134 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000136 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000137 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
138 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000139 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 setOperationAction(ISD::FLOG, MVT::f32, Expand);
141 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
142 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
143 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000144
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000145 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
146 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
147
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000148 setOperationAction(ISD::VAARG, MVT::Other, Expand);
149 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
150 setOperationAction(ISD::VAEND, MVT::Other, Expand);
151
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000152 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
154 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
155 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000156
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000157 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000159
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000160 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000161 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
162 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000163 }
164
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000165 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000166 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000167
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000168 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000169 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000170
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000171 setTargetDAGCombine(ISD::ADDE);
172 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000173 setTargetDAGCombine(ISD::SDIVREM);
174 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000175 setTargetDAGCombine(ISD::SETCC);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000176
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000177 setMinFunctionAlignment(2);
178
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000179 setStackPointerRegisterToSaveRestore(Mips::SP);
180 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000181
182 setExceptionPointerRegister(Mips::A0);
183 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000184}
185
Owen Anderson825b72b2009-08-11 20:47:22 +0000186MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
187 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000188}
189
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000190// SelectMadd -
191// Transforms a subgraph in CurDAG if the following pattern is found:
192// (addc multLo, Lo0), (adde multHi, Hi0),
193// where,
194// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000195// Lo0: initial value of Lo register
196// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000197// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000198static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000199 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000200 // for the matching to be successful.
201 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
202
203 if (ADDCNode->getOpcode() != ISD::ADDC)
204 return false;
205
206 SDValue MultHi = ADDENode->getOperand(0);
207 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000208 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000209 unsigned MultOpc = MultHi.getOpcode();
210
211 // MultHi and MultLo must be generated by the same node,
212 if (MultLo.getNode() != MultNode)
213 return false;
214
215 // and it must be a multiplication.
216 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
217 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000218
219 // MultLo amd MultHi must be the first and second output of MultNode
220 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000221 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
222 return false;
223
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000224 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000225 // of the values of MultNode, in which case MultNode will be removed in later
226 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000227 // If there exist users other than ADDENode or ADDCNode, this function returns
228 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000229 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000230 // produced.
231 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
232 return false;
233
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000234 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000235 DebugLoc dl = ADDENode->getDebugLoc();
236
237 // create MipsMAdd(u) node
238 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000239
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000240 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
241 MVT::Glue,
242 MultNode->getOperand(0),// Factor 0
243 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000244 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000245 ADDENode->getOperand(1));// Hi0
246
247 // create CopyFromReg nodes
248 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
249 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000250 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000251 Mips::HI, MVT::i32,
252 CopyFromLo.getValue(2));
253
254 // replace uses of adde and addc here
255 if (!SDValue(ADDCNode, 0).use_empty())
256 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
257
258 if (!SDValue(ADDENode, 0).use_empty())
259 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
260
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000261 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000262}
263
264// SelectMsub -
265// Transforms a subgraph in CurDAG if the following pattern is found:
266// (addc Lo0, multLo), (sube Hi0, multHi),
267// where,
268// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000269// Lo0: initial value of Lo register
270// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000271// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000272static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000273 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000274 // for the matching to be successful.
275 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
276
277 if (SUBCNode->getOpcode() != ISD::SUBC)
278 return false;
279
280 SDValue MultHi = SUBENode->getOperand(1);
281 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000282 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000283 unsigned MultOpc = MultHi.getOpcode();
284
285 // MultHi and MultLo must be generated by the same node,
286 if (MultLo.getNode() != MultNode)
287 return false;
288
289 // and it must be a multiplication.
290 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
291 return false;
292
293 // MultLo amd MultHi must be the first and second output of MultNode
294 // respectively.
295 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
296 return false;
297
298 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
299 // of the values of MultNode, in which case MultNode will be removed in later
300 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000301 // If there exist users other than SUBENode or SUBCNode, this function returns
302 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000303 // instruction node rather than a pair of MULT and MSUB instructions being
304 // produced.
305 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
306 return false;
307
308 SDValue Chain = CurDAG->getEntryNode();
309 DebugLoc dl = SUBENode->getDebugLoc();
310
311 // create MipsSub(u) node
312 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
313
314 SDValue MSub = CurDAG->getNode(MultOpc, dl,
315 MVT::Glue,
316 MultNode->getOperand(0),// Factor 0
317 MultNode->getOperand(1),// Factor 1
318 SUBCNode->getOperand(0),// Lo0
319 SUBENode->getOperand(0));// Hi0
320
321 // create CopyFromReg nodes
322 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
323 MSub);
324 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
325 Mips::HI, MVT::i32,
326 CopyFromLo.getValue(2));
327
328 // replace uses of sube and subc here
329 if (!SDValue(SUBCNode, 0).use_empty())
330 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
331
332 if (!SDValue(SUBENode, 0).use_empty())
333 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
334
335 return true;
336}
337
338static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
339 TargetLowering::DAGCombinerInfo &DCI,
340 const MipsSubtarget* Subtarget) {
341 if (DCI.isBeforeLegalize())
342 return SDValue();
343
344 if (Subtarget->isMips32() && SelectMadd(N, &DAG))
345 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000346
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000347 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000348}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000349
350static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
351 TargetLowering::DAGCombinerInfo &DCI,
352 const MipsSubtarget* Subtarget) {
353 if (DCI.isBeforeLegalize())
354 return SDValue();
355
356 if (Subtarget->isMips32() && SelectMsub(N, &DAG))
357 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000358
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000359 return SDValue();
360}
361
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000362static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
363 TargetLowering::DAGCombinerInfo &DCI,
364 const MipsSubtarget* Subtarget) {
365 if (DCI.isBeforeLegalizeOps())
366 return SDValue();
367
368 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
369 MipsISD::DivRemU;
370 DebugLoc dl = N->getDebugLoc();
371
372 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
373 N->getOperand(0), N->getOperand(1));
374 SDValue InChain = DAG.getEntryNode();
375 SDValue InGlue = DivRem;
376
377 // insert MFLO
378 if (N->hasAnyUseOfValue(0)) {
379 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, Mips::LO, MVT::i32,
380 InGlue);
381 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
382 InChain = CopyFromLo.getValue(1);
383 InGlue = CopyFromLo.getValue(2);
384 }
385
386 // insert MFHI
387 if (N->hasAnyUseOfValue(1)) {
388 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000389 Mips::HI, MVT::i32, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000390 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
391 }
392
393 return SDValue();
394}
395
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000396static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
397 switch (CC) {
398 default: llvm_unreachable("Unknown fp condition code!");
399 case ISD::SETEQ:
400 case ISD::SETOEQ: return Mips::FCOND_OEQ;
401 case ISD::SETUNE: return Mips::FCOND_UNE;
402 case ISD::SETLT:
403 case ISD::SETOLT: return Mips::FCOND_OLT;
404 case ISD::SETGT:
405 case ISD::SETOGT: return Mips::FCOND_OGT;
406 case ISD::SETLE:
407 case ISD::SETOLE: return Mips::FCOND_OLE;
408 case ISD::SETGE:
409 case ISD::SETOGE: return Mips::FCOND_OGE;
410 case ISD::SETULT: return Mips::FCOND_ULT;
411 case ISD::SETULE: return Mips::FCOND_ULE;
412 case ISD::SETUGT: return Mips::FCOND_UGT;
413 case ISD::SETUGE: return Mips::FCOND_UGE;
414 case ISD::SETUO: return Mips::FCOND_UN;
415 case ISD::SETO: return Mips::FCOND_OR;
416 case ISD::SETNE:
417 case ISD::SETONE: return Mips::FCOND_ONE;
418 case ISD::SETUEQ: return Mips::FCOND_UEQ;
419 }
420}
421
422
423// Returns true if condition code has to be inverted.
424static bool InvertFPCondCode(Mips::CondCode CC) {
425 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
426 return false;
427
428 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
429 return true;
430
431 assert(false && "Illegal Condition Code");
432 return false;
433}
434
435// Creates and returns an FPCmp node from a setcc node.
436// Returns Op if setcc is not a floating point comparison.
437static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
438 // must be a SETCC node
439 if (Op.getOpcode() != ISD::SETCC)
440 return Op;
441
442 SDValue LHS = Op.getOperand(0);
443
444 if (!LHS.getValueType().isFloatingPoint())
445 return Op;
446
447 SDValue RHS = Op.getOperand(1);
448 DebugLoc dl = Op.getDebugLoc();
449
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000450 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
451 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000452 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
453
454 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
455 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
456}
457
458// Creates and returns a CMovFPT/F node.
459static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
460 SDValue False, DebugLoc DL) {
461 bool invert = InvertFPCondCode((Mips::CondCode)
462 cast<ConstantSDNode>(Cond.getOperand(2))
463 ->getSExtValue());
464
465 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
466 True.getValueType(), True, False, Cond);
467}
468
469static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
470 TargetLowering::DAGCombinerInfo &DCI,
471 const MipsSubtarget* Subtarget) {
472 if (DCI.isBeforeLegalizeOps())
473 return SDValue();
474
475 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
476
477 if (Cond.getOpcode() != MipsISD::FPCmp)
478 return SDValue();
479
480 SDValue True = DAG.getConstant(1, MVT::i32);
481 SDValue False = DAG.getConstant(0, MVT::i32);
482
483 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
484}
485
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000486SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000487 const {
488 SelectionDAG &DAG = DCI.DAG;
489 unsigned opc = N->getOpcode();
490
491 switch (opc) {
492 default: break;
493 case ISD::ADDE:
494 return PerformADDECombine(N, DAG, DCI, Subtarget);
495 case ISD::SUBE:
496 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000497 case ISD::SDIVREM:
498 case ISD::UDIVREM:
499 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000500 case ISD::SETCC:
501 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000502 }
503
504 return SDValue();
505}
506
Dan Gohman475871a2008-07-27 21:46:04 +0000507SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000508LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000509{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000510 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000511 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000512 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000513 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
514 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000515 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000516 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000517 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
518 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000519 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000520 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000521 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000522 }
Dan Gohman475871a2008-07-27 21:46:04 +0000523 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000524}
525
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000526//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000527// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000528//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000529
530// AddLiveIn - This helper function adds the specified physical register to the
531// MachineFunction as a live in value. It also creates a corresponding
532// virtual register for it.
533static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000534AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000535{
536 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000537 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
538 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000539 return VReg;
540}
541
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000542// Get fp branch code (not opcode) from condition code.
543static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
544 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
545 return Mips::BRANCH_T;
546
547 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
548 return Mips::BRANCH_F;
549
550 return Mips::BRANCH_INVALID;
551}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000552
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000553MachineBasicBlock *
554MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000555 MachineBasicBlock *BB) const {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000556 // There is no need to expand CMov instructions if target has
557 // conditional moves.
558 if (Subtarget->hasCondMov())
559 return BB;
560
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000561 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
562 bool isFPCmp = false;
Dale Johannesen94817572009-02-13 02:34:39 +0000563 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000564 unsigned Opc;
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000565
566 switch (MI->getOpcode()) {
567 default: assert(false && "Unexpected instr type to insert");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000568 case Mips::MOVT:
569 case Mips::MOVT_S:
570 case Mips::MOVT_D:
571 isFPCmp = true;
572 Opc = Mips::BC1F;
573 break;
574 case Mips::MOVF:
575 case Mips::MOVF_S:
576 case Mips::MOVF_D:
577 isFPCmp = true;
578 Opc = Mips::BC1T;
579 break;
580 case Mips::MOVZ_I:
581 case Mips::MOVZ_S:
582 case Mips::MOVZ_D:
583 Opc = Mips::BNE;
584 break;
585 case Mips::MOVN_I:
586 case Mips::MOVN_S:
587 case Mips::MOVN_D:
588 Opc = Mips::BEQ;
589 break;
590 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000591
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000592 // To "insert" a SELECT_CC instruction, we actually have to insert the
593 // diamond control-flow pattern. The incoming instruction knows the
594 // destination vreg to set, the condition code register to branch on, the
595 // true/false values to select between, and a branch opcode to use.
596 const BasicBlock *LLVM_BB = BB->getBasicBlock();
597 MachineFunction::iterator It = BB;
598 ++It;
Dan Gohman14152b42010-07-06 20:24:04 +0000599
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000600 // thisMBB:
601 // ...
602 // TrueVal = ...
603 // setcc r1, r2, r3
604 // bNE r1, r0, copy1MBB
605 // fallthrough --> copy0MBB
606 MachineBasicBlock *thisMBB = BB;
607 MachineFunction *F = BB->getParent();
608 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
609 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
610 F->insert(It, copy0MBB);
611 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +0000612
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000613 // Transfer the remainder of BB and its successor edges to sinkMBB.
614 sinkMBB->splice(sinkMBB->begin(), BB,
615 llvm::next(MachineBasicBlock::iterator(MI)),
616 BB->end());
617 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000618
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000619 // Next, add the true and fallthrough blocks as its successors.
620 BB->addSuccessor(copy0MBB);
621 BB->addSuccessor(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000622
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000623 // Emit the right instruction according to the type of the operands compared
624 if (isFPCmp)
625 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
626 else
627 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
628 .addReg(Mips::ZERO).addMBB(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000629
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000630
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000631 // copy0MBB:
632 // %FalseValue = ...
633 // # fallthrough to sinkMBB
634 BB = copy0MBB;
635
636 // Update machine-CFG edges
637 BB->addSuccessor(sinkMBB);
638
639 // sinkMBB:
640 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
641 // ...
642 BB = sinkMBB;
643
644 if (isFPCmp)
Dan Gohman14152b42010-07-06 20:24:04 +0000645 BuildMI(*BB, BB->begin(), dl,
646 TII->get(Mips::PHI), MI->getOperand(0).getReg())
Bruno Cardoso Lopes29e9daa2010-07-20 07:58:51 +0000647 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000648 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
649 else
650 BuildMI(*BB, BB->begin(), dl,
651 TII->get(Mips::PHI), MI->getOperand(0).getReg())
652 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
653 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000654
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000655 MI->eraseFromParent(); // The pseudo instruction is gone now.
656 return BB;
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000657}
658
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000659//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000660// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000661//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000662SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000663LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000664{
Akira Hatanaka053546c2011-05-25 02:20:00 +0000665 unsigned StackAlignment =
666 getTargetMachine().getFrameLowering()->getStackAlignment();
667 assert(StackAlignment >=
668 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
669 "Cannot lower if the alignment of the allocated space is larger than \
670 that of the stack.");
671
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000672 SDValue Chain = Op.getOperand(0);
673 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +0000674 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000675
676 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +0000677 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000678
679 // Subtract the dynamic size from the actual stack size to
680 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000682
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000683 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000684 // must be placed in the stack pointer register.
Akira Hatanaka053546c2011-05-25 02:20:00 +0000685 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
686 SDValue());
Akira Hatanakaedacba82011-05-25 17:32:06 +0000687 // Retrieve updated $sp. There is a glue input to prevent instructions that
688 // clobber $sp from being inserted between copytoreg and copyfromreg.
Akira Hatanaka053546c2011-05-25 02:20:00 +0000689 SDValue NewSP = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32,
690 Chain.getValue(1));
691
Akira Hatanakaedacba82011-05-25 17:32:06 +0000692 // The stack space reserved by alloca is located right above the argument
693 // area. It is aligned on a boundary that is a multiple of StackAlignment.
Akira Hatanaka053546c2011-05-25 02:20:00 +0000694 MachineFunction &MF = DAG.getMachineFunction();
695 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
696 unsigned SPOffset = (MipsFI->getMaxCallFrameSize() + StackAlignment - 1) /
697 StackAlignment * StackAlignment;
698 SDValue AllocPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
699 DAG.getConstant(SPOffset, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000700
701 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000702 // value and a chain
Akira Hatanaka053546c2011-05-25 02:20:00 +0000703 SDValue Ops[2] = { AllocPtr, NewSP.getValue(1) };
Dale Johannesena05dca42009-02-04 23:02:30 +0000704 return DAG.getMergeValues(Ops, 2, dl);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000705}
706
707SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000708LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000709{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000710 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000711 // the block to branch to if the condition is true.
712 SDValue Chain = Op.getOperand(0);
713 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000714 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000715
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000716 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
717
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000718 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000719 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000720 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000721
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000722 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000723 Mips::CondCode CC =
724 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000725 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000726
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000727 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000728 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000729}
730
731SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000732LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000733{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000734 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000735
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000736 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000737 if (Cond.getOpcode() != MipsISD::FPCmp)
738 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000739
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000740 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
741 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000742}
743
Dan Gohmand858e902010-04-17 15:26:15 +0000744SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
745 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +0000746 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000747 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000748 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000749
Eli Friedmane2c74082009-08-03 02:22:28 +0000750 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +0000751 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000752
Chris Lattnerb71b9092009-08-13 06:28:06 +0000753 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000754
Chris Lattnere3736f82009-08-13 05:41:27 +0000755 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000756 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
757 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000758 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +0000759 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
760 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000761 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +0000762 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000763 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000764 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
765 MipsII::MO_ABS_HI);
766 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
767 MipsII::MO_ABS_LO);
768 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
769 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000771 } else {
Devang Patel0d881da2010-07-06 22:08:15 +0000772 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000773 MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +0000774 GA = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, GA);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000775 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000776 DAG.getEntryNode(), GA, MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000777 false, false, 0);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000778 // On functions and global targets not internal linked only
779 // a load from got/GP is necessary for PIC to work.
Akira Hatanaka9777e7a2011-04-07 19:51:44 +0000780 if (!GV->hasInternalLinkage() &&
781 (!GV->hasLocalLinkage() || isa<Function>(GV)))
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000782 return ResNode;
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000783 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
784 MipsII::MO_ABS_LO);
785 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000787 }
788
Torok Edwinc23197a2009-07-14 16:55:14 +0000789 llvm_unreachable("Dont know how to handle GlobalAddress");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000790 return SDValue(0,0);
791}
792
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000793SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
794 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +0000795 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
796 // FIXME there isn't actually debug info here
797 DebugLoc dl = Op.getDebugLoc();
798
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000799 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +0000800 // %hi/%lo relocation
801 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
802 MipsII::MO_ABS_HI);
803 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
804 MipsII::MO_ABS_LO);
805 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
806 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
807 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000808 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +0000809
810 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
811 MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +0000812 BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, BAGOTOffset);
Akira Hatanakaf48eb532011-04-25 17:10:45 +0000813 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
814 MipsII::MO_ABS_LO);
815 SDValue Load = DAG.getLoad(MVT::i32, dl,
816 DAG.getEntryNode(), BAGOTOffset,
817 MachinePointerInfo(), false, false, 0);
818 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
819 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000820}
821
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000822SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000823LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000824{
Torok Edwinc23197a2009-07-14 16:55:14 +0000825 llvm_unreachable("TLS not implemented for MIPS.");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000826 return SDValue(); // Not reached
827}
828
829SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000830LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000831{
Dan Gohman475871a2008-07-27 21:46:04 +0000832 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000833 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +0000834 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000835 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000836 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000837 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000838
Owen Andersone50ed302009-08-10 22:56:29 +0000839 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000840 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000841
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000842 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
843
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +0000844 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +0000845 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000846 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Akira Hatanaka342837d2011-05-28 01:07:07 +0000847 } else {// Emit Load from Global Pointer
848 JTI = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, JTI);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000849 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
850 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000851 false, false, 0);
Akira Hatanaka342837d2011-05-28 01:07:07 +0000852 }
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);
Akira Hatanaka342837d2011-05-28 01:07:07 +0000892 CP = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000893 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000894 CP, MachinePointerInfo::getConstantPool(),
895 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000896 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000897 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000898 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000899 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
900 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000901
902 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000903}
904
Dan Gohmand858e902010-04-17 15:26:15 +0000905SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +0000906 MachineFunction &MF = DAG.getMachineFunction();
907 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
908
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000909 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +0000910 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
911 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000912
913 // vastart just stores the address of the VarArgsFrameIndex slot into the
914 // memory location argument.
915 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +0000916 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
917 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +0000918 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000919}
920
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000921static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
922 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
923 DebugLoc dl = Op.getDebugLoc();
924 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
925 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
926 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
927 DAG.getConstant(0x7fffffff, MVT::i32));
928 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
929 DAG.getConstant(0x80000000, MVT::i32));
930 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
931 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
932}
933
934static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
935 // FIXME:
936 // Use ext/ins instructions if target architecture is Mips32r2.
937 // Eliminate redundant mfc1 and mtc1 instructions.
938 unsigned LoIdx = 0, HiIdx = 1;
939
940 if (!isLittle)
941 std::swap(LoIdx, HiIdx);
942
943 DebugLoc dl = Op.getDebugLoc();
944 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
945 Op.getOperand(0),
946 DAG.getConstant(LoIdx, MVT::i32));
947 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
948 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
949 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
950 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
951 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
952 DAG.getConstant(0x7fffffff, MVT::i32));
953 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
954 DAG.getConstant(0x80000000, MVT::i32));
955 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
956
957 if (!isLittle)
958 std::swap(Word0, Word1);
959
960 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
961}
962
963SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
964 const {
965 EVT Ty = Op.getValueType();
966
967 assert(Ty == MVT::f32 || Ty == MVT::f64);
968
969 if (Ty == MVT::f32)
970 return LowerFCOPYSIGN32(Op, DAG);
971 else
972 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
973}
974
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000975//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000976// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000977//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000978
979#include "MipsGenCallingConv.inc"
980
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000981//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000982// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000983// Mips O32 ABI rules:
984// ---
985// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000986// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000987// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000988// f64 - Only passed in two aliased f32 registers if no int reg has been used
989// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000990// not used, it must be shadowed. If only A3 is avaiable, shadow it and
991// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +0000992//
993// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000994//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000995
Duncan Sands1e96bab2010-11-04 10:49:57 +0000996static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000997 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000998 ISD::ArgFlagsTy ArgFlags, CCState &State) {
999
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001000 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001001
1002 static const unsigned IntRegs[] = {
1003 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1004 };
1005 static const unsigned F32Regs[] = {
1006 Mips::F12, Mips::F14
1007 };
1008 static const unsigned F64Regs[] = {
1009 Mips::D6, Mips::D7
1010 };
1011
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001012 // ByVal Args
1013 if (ArgFlags.isByVal()) {
1014 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1015 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1016 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1017 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1018 r < std::min(IntRegsSize, NextReg); ++r)
1019 State.AllocateReg(IntRegs[r]);
1020 return false;
1021 }
1022
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001023 // Promote i8 and i16
1024 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1025 LocVT = MVT::i32;
1026 if (ArgFlags.isSExt())
1027 LocInfo = CCValAssign::SExt;
1028 else if (ArgFlags.isZExt())
1029 LocInfo = CCValAssign::ZExt;
1030 else
1031 LocInfo = CCValAssign::AExt;
1032 }
1033
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001034 unsigned Reg;
1035
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001036 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1037 // is true: function is vararg, argument is 3rd or higher, there is previous
1038 // argument which is not f32 or f64.
1039 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1040 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001041 unsigned OrigAlign = ArgFlags.getOrigAlign();
1042 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001043
1044 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001045 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001046 // If this is the first part of an i64 arg,
1047 // the allocated register must be either A0 or A2.
1048 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1049 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001050 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001051 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1052 // Allocate int register and shadow next int register. If first
1053 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001054 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1055 if (Reg == Mips::A1 || Reg == Mips::A3)
1056 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1057 State.AllocateReg(IntRegs, IntRegsSize);
1058 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001059 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1060 // we are guaranteed to find an available float register
1061 if (ValVT == MVT::f32) {
1062 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1063 // Shadow int register
1064 State.AllocateReg(IntRegs, IntRegsSize);
1065 } else {
1066 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1067 // Shadow int registers
1068 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1069 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1070 State.AllocateReg(IntRegs, IntRegsSize);
1071 State.AllocateReg(IntRegs, IntRegsSize);
1072 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001073 } else
1074 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001075
Akira Hatanakad37776d2011-05-20 21:39:54 +00001076 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1077 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1078
1079 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001080 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001081 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001082 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001083
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001084 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001085}
1086
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001087//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001088// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001089//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001090
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001091static const unsigned O32IntRegsSize = 4;
1092
1093static const unsigned O32IntRegs[] = {
1094 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1095};
1096
1097// Write ByVal Arg to arg registers and stack.
1098static void
1099WriteByValArg(SDValue& Chain, DebugLoc dl,
1100 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1101 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1102 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001103 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
1104 MVT PtrType) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001105 unsigned FirstWord = VA.getLocMemOffset() / 4;
1106 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
1107 unsigned LastWord = FirstWord + NumWords;
1108 unsigned CurWord;
1109
1110 // copy the first 4 words of byval arg to registers A0 - A3
1111 for (CurWord = FirstWord; CurWord < std::min(LastWord, O32IntRegsSize);
1112 ++CurWord) {
1113 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1114 DAG.getConstant((CurWord - FirstWord) * 4,
1115 MVT::i32));
1116 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1117 MachinePointerInfo(),
1118 false, false, 0);
1119 MemOpChains.push_back(LoadVal.getValue(1));
1120 unsigned DstReg = O32IntRegs[CurWord];
1121 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1122 }
1123
1124 // copy remaining part of byval arg to stack.
1125 if (CurWord < LastWord) {
1126 unsigned SizeInBytes = (LastWord - CurWord) * 4;
1127 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1128 DAG.getConstant((CurWord - FirstWord) * 4,
1129 MVT::i32));
1130 LastFI = MFI->CreateFixedObject(SizeInBytes, CurWord * 4, true);
1131 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
1132 Chain = DAG.getMemcpy(Chain, dl, Dst, Src,
1133 DAG.getConstant(SizeInBytes, MVT::i32),
1134 /*Align*/4,
1135 /*isVolatile=*/false, /*AlwaysInline=*/false,
1136 MachinePointerInfo(0), MachinePointerInfo(0));
1137 MemOpChains.push_back(Chain);
1138 }
1139}
1140
Dan Gohman98ca4f22009-08-05 01:29:28 +00001141/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001142/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001143/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001144SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001145MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001146 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001147 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001148 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001149 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001150 const SmallVectorImpl<ISD::InputArg> &Ins,
1151 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001152 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001153 // MIPs target does not yet support tail call optimization.
1154 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001155
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001156 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001157 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00001158 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001159 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001160 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001161
1162 // Analyze operands of the call, assigning locations to each operand.
1163 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001164 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1165 *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001166
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001167 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001168 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001169 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001170 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001171
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001172 // Get a count of how many bytes are to be pushed on the stack.
1173 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnere563bbc2008-10-11 22:08:30 +00001174 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001175
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001176 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001177 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1178 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001179
Akira Hatanakaedacba82011-05-25 17:32:06 +00001180 // If this is the first call, create a stack frame object that points to
1181 // a location to which .cprestore saves $gp. The offset of this frame object
1182 // is set to 0, since we know nothing about the size of the argument area at
1183 // this point.
Akira Hatanaka69c19f72011-05-23 20:16:59 +00001184 if (IsPIC && !MipsFI->getGPFI())
Akira Hatanaka43299772011-05-20 23:22:14 +00001185 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1186
1187 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
1188
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001189 // Walk the register/memloc assignments, inserting copies/loads.
1190 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001191 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001192 CCValAssign &VA = ArgLocs[i];
1193
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001194 // Promote the value if needed.
1195 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001196 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001197 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001198 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001199 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001200 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001201 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001202 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1203 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001204 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1205 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001206 if (!Subtarget->isLittle())
1207 std::swap(Lo, Hi);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001208 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
1209 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
1210 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001211 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001212 }
1213 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00001214 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001215 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001216 break;
1217 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001218 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001219 break;
1220 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001221 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001222 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001223 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001224
1225 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001226 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001227 if (VA.isRegLoc()) {
1228 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00001229 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001230 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001231
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001232 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00001233 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001234
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001235 // ByVal Arg.
1236 ISD::ArgFlagsTy Flags = Outs[i].Flags;
1237 if (Flags.isByVal()) {
1238 assert(Subtarget->isABI_O32() &&
1239 "No support for ByVal args by ABIs other than O32 yet.");
1240 assert(Flags.getByValSize() &&
1241 "ByVal args of size 0 should have been ignored by front-end.");
1242 WriteByValArg(Chain, dl, RegsToPass, MemOpChains, LastFI, MFI, DAG, Arg,
1243 VA, Flags, getPointerTy());
1244 continue;
1245 }
1246
Chris Lattnere0b12152008-03-17 06:57:02 +00001247 // Create the frame index object for this incoming parameter
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001248 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
1249 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00001250 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00001251
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001252 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00001253 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00001254 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
1255 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001256 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001257 }
1258
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001259 // Transform all store nodes into one single node because all store
1260 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001261 if (!MemOpChains.empty())
1262 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001263 &MemOpChains[0], MemOpChains.size());
1264
Bill Wendling056292f2008-09-16 21:48:12 +00001265 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001266 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1267 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001268 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001269 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001270 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001271
1272 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001273 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
1274 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1275 getPointerTy(), 0,MipsII:: MO_GOT);
1276 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
1277 0, MipsII::MO_ABS_LO);
1278 } else {
1279 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1280 getPointerTy(), 0, OpFlag);
1281 }
1282
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001283 LoadSymAddr = true;
1284 }
1285 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001286 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001287 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001288 LoadSymAddr = true;
1289 }
1290
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00001291 SDValue InFlag;
1292
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001293 // Create nodes that load address of callee and copy it to T9
1294 if (IsPIC) {
1295 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001296 // Load callee address
Akira Hatanaka342837d2011-05-28 01:07:07 +00001297 Callee = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, Callee);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001298 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, Chain, Callee,
1299 MachinePointerInfo::getGOT(),
1300 false, false, 0);
1301
1302 // Use GOT+LO if callee has internal linkage.
1303 if (CalleeLo.getNode()) {
1304 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
1305 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
1306 } else
1307 Callee = LoadValue;
1308
1309 // Use chain output from LoadValue
1310 Chain = LoadValue.getValue(1);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001311 }
1312
1313 // copy to T9
1314 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
1315 InFlag = Chain.getValue(1);
1316 Callee = DAG.getRegister(Mips::T9, MVT::i32);
1317 }
Bill Wendling056292f2008-09-16 21:48:12 +00001318
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00001319 // Build a sequence of copy-to-reg nodes chained together with token
1320 // chain and flag operands which copy the outgoing args into registers.
1321 // The InFlag in necessary since all emitted instructions must be
1322 // stuck together.
1323 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1324 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1325 RegsToPass[i].second, InFlag);
1326 InFlag = Chain.getValue(1);
1327 }
1328
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001329 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001330 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001331 //
1332 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001333 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00001334 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001335 Ops.push_back(Chain);
1336 Ops.push_back(Callee);
1337
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001338 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001339 // known live into the call.
1340 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1341 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1342 RegsToPass[i].second.getValueType()));
1343
Gabor Greifba36cb52008-08-28 21:40:38 +00001344 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001345 Ops.push_back(InFlag);
1346
Dale Johannesen33c960f2009-02-04 20:06:27 +00001347 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001348 InFlag = Chain.getValue(1);
1349
Akira Hatanaka4c62f762011-05-25 18:08:32 +00001350 // Function can have an arbitrary number of calls, so
1351 // hold the LastArgStackLoc with the biggest offset.
1352 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1353 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001354
Akira Hatanaka4c62f762011-05-25 18:08:32 +00001355 // For O32, a minimum of four words (16 bytes) of argument space is
1356 // allocated.
1357 if (Subtarget->isABI_O32())
1358 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
Akira Hatanakad37776d2011-05-20 21:39:54 +00001359
Akira Hatanaka4c62f762011-05-25 18:08:32 +00001360 if (MaxCallFrameSize < NextStackOffset) {
1361 MipsFI->setMaxCallFrameSize(NextStackOffset);
Akira Hatanakad37776d2011-05-20 21:39:54 +00001362
Akira Hatanaka4c62f762011-05-25 18:08:32 +00001363 if (IsPIC) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001364 // $gp restore slot must be aligned.
1365 unsigned StackAlignment = TFL->getStackAlignment();
1366 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
1367 StackAlignment * StackAlignment;
1368 int GPFI = MipsFI->getGPFI();
1369 MFI->setObjectOffset(GPFI, NextStackOffset);
1370 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001371 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001372
Akira Hatanaka43299772011-05-20 23:22:14 +00001373 // Extend range of indices of frame objects for outgoing arguments that were
1374 // created during this function call. Skip this step if no such objects were
1375 // created.
1376 if (LastFI)
1377 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
1378
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00001379 // Create the CALLSEQ_END node.
1380 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1381 DAG.getIntPtrConstant(0, true), InFlag);
1382 InFlag = Chain.getValue(1);
1383
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001384 // Handle result values, copying them out of physregs into vregs that we
1385 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001386 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
1387 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001388}
1389
Dan Gohman98ca4f22009-08-05 01:29:28 +00001390/// LowerCallResult - Lower the result values of a call into the
1391/// appropriate copies out of appropriate physical registers.
1392SDValue
1393MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001394 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001395 const SmallVectorImpl<ISD::InputArg> &Ins,
1396 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001397 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001398
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001399 // Assign locations to each value returned by this call.
1400 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001401 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001402 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001403
Dan Gohman98ca4f22009-08-05 01:29:28 +00001404 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001405
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001406 // Copy all of the result registers out of their specified physreg.
1407 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001408 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001409 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001410 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001411 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001412 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001413
Dan Gohman98ca4f22009-08-05 01:29:28 +00001414 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001415}
1416
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001417//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001418// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001419//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001420static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
1421 std::vector<SDValue>& OutChains,
1422 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
1423 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
1424 unsigned LocMem = VA.getLocMemOffset();
1425 unsigned FirstWord = LocMem / 4;
1426
1427 // copy register A0 - A3 to frame object
1428 for (unsigned i = 0; i < NumWords; ++i) {
1429 unsigned CurWord = FirstWord + i;
1430 if (CurWord >= O32IntRegsSize)
1431 break;
1432
1433 unsigned SrcReg = O32IntRegs[CurWord];
1434 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
1435 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
1436 DAG.getConstant(i * 4, MVT::i32));
1437 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
1438 StorePtr, MachinePointerInfo(), false,
1439 false, 0);
1440 OutChains.push_back(Store);
1441 }
1442}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001443
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001444/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001445/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001446SDValue
1447MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001448 CallingConv::ID CallConv,
1449 bool isVarArg,
1450 const SmallVectorImpl<ISD::InputArg>
1451 &Ins,
1452 DebugLoc dl, SelectionDAG &DAG,
1453 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001454 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00001455 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001456 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001457 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001458
Dan Gohman1e93df62010-04-17 14:41:14 +00001459 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001460
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001461 // Used with vargs to acumulate store chains.
1462 std::vector<SDValue> OutChains;
1463
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001464 // Assign locations to all of the incoming arguments.
1465 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001466 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1467 ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001468
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001469 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001470 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001471 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001472 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001473
Akira Hatanaka43299772011-05-20 23:22:14 +00001474 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001475
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001476 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001477 CCValAssign &VA = ArgLocs[i];
1478
1479 // Arguments stored on registers
1480 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001481 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001482 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00001483 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001484
Owen Anderson825b72b2009-08-11 20:47:22 +00001485 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001486 RC = Mips::CPURegsRegisterClass;
1487 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001488 RC = Mips::FGR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001489 else if (RegVT == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001490 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001491 RC = Mips::AFGR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001492 } else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001493 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001494
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001495 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001496 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001497 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001498 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001499
1500 // If this is an 8 or 16-bit value, it has been passed promoted
1501 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001502 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001503 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00001504 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001505 if (VA.getLocInfo() == CCValAssign::SExt)
1506 Opcode = ISD::AssertSext;
1507 else if (VA.getLocInfo() == CCValAssign::ZExt)
1508 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00001509 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001510 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00001511 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00001512 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001513 }
1514
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001515 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001516 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001517 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
1518 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00001519 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001520 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001521 VA.getLocReg()+1, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001522 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001523 if (!Subtarget->isLittle())
1524 std::swap(ArgValue, ArgValue2);
1525 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
1526 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001527 }
1528 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001529
Dan Gohman98ca4f22009-08-05 01:29:28 +00001530 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001531 } else { // VA.isRegLoc()
1532
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001533 // sanity check
1534 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001535
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001536 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1537
1538 if (Flags.isByVal()) {
1539 assert(Subtarget->isABI_O32() &&
1540 "No support for ByVal args by ABIs other than O32 yet.");
1541 assert(Flags.getByValSize() &&
1542 "ByVal args of size 0 should have been ignored by front-end.");
1543 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
1544 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
1545 true);
1546 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
1547 InVals.push_back(FIN);
1548 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
1549
1550 continue;
1551 }
1552
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001553 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001554 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
1555 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001556
1557 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00001558 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001559 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00001560 MachinePointerInfo::getFixedStack(LastFI),
David Greenef6fa1862010-02-15 16:56:10 +00001561 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001562 }
1563 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001564
1565 // The mips ABIs for returning structs by value requires that we copy
1566 // the sret argument into $v0 for the return. Save the argument into
1567 // a virtual register so that we can access it from the return points.
1568 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1569 unsigned Reg = MipsFI->getSRetReturnReg();
1570 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001571 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001572 MipsFI->setSRetReturnReg(Reg);
1573 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001574 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001575 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001576 }
1577
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001578 if (isVarArg && Subtarget->isABI_O32()) {
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001579 // Record the frame index of the first variable argument
1580 // which is a value necessary to VASTART.
1581 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001582 assert(NextStackOffset % 4 == 0 &&
1583 "NextStackOffset must be aligned to 4-byte boundaries.");
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001584 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
1585 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00001586
1587 // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
1588 // copy the integer registers that have not been used for argument passing
1589 // to the caller's stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001590 for (; NextStackOffset < 16; NextStackOffset += 4) {
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001591 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001592 unsigned Idx = NextStackOffset / 4;
1593 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
1594 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Akira Hatanaka69c19f72011-05-23 20:16:59 +00001595 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001596 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
1597 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
1598 MachinePointerInfo(),
1599 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001600 }
1601 }
1602
Akira Hatanaka43299772011-05-20 23:22:14 +00001603 MipsFI->setLastInArgFI(LastFI);
1604
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001605 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001606 // the size of Ins and InVals. This only happens when on varg functions
1607 if (!OutChains.empty()) {
1608 OutChains.push_back(Chain);
1609 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1610 &OutChains[0], OutChains.size());
1611 }
1612
Dan Gohman98ca4f22009-08-05 01:29:28 +00001613 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001614}
1615
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001616//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001617// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001618//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001619
Dan Gohman98ca4f22009-08-05 01:29:28 +00001620SDValue
1621MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001622 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001623 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001624 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001625 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001626
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001627 // CCValAssign - represent the assignment of
1628 // the return value to a location
1629 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001630
1631 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001632 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1633 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001634
Dan Gohman98ca4f22009-08-05 01:29:28 +00001635 // Analize return values.
1636 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001637
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001638 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001639 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001640 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001641 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001642 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001643 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001644 }
1645
Dan Gohman475871a2008-07-27 21:46:04 +00001646 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001647
1648 // Copy the result values into the output registers.
1649 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1650 CCValAssign &VA = RVLocs[i];
1651 assert(VA.isRegLoc() && "Can only return in registers!");
1652
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001653 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001654 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001655
1656 // guarantee that all emitted copies are
1657 // stuck together, avoiding something bad
1658 Flag = Chain.getValue(1);
1659 }
1660
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001661 // The mips ABIs for returning structs by value requires that we copy
1662 // the sret argument into $v0 for the return. We saved the argument into
1663 // a virtual register in the entry block, so now we copy the value out
1664 // and into $v0.
1665 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1666 MachineFunction &MF = DAG.getMachineFunction();
1667 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1668 unsigned Reg = MipsFI->getSRetReturnReg();
1669
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001670 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00001671 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00001672 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001673
Dale Johannesena05dca42009-02-04 23:02:30 +00001674 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001675 Flag = Chain.getValue(1);
1676 }
1677
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001678 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00001679 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001680 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001681 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001682 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001683 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001684 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001685}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001686
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001687//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001688// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001689//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001690
1691/// getConstraintType - Given a constraint letter, return the type of
1692/// constraint it is for this target.
1693MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001694getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001695{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001696 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001697 // GCC config/mips/constraints.md
1698 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001699 // 'd' : An address register. Equivalent to r
1700 // unless generating MIPS16 code.
1701 // 'y' : Equivalent to r; retained for
1702 // backwards compatibility.
1703 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001704 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001705 switch (Constraint[0]) {
1706 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001707 case 'd':
1708 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001709 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001710 return C_RegisterClass;
1711 break;
1712 }
1713 }
1714 return TargetLowering::getConstraintType(Constraint);
1715}
1716
John Thompson44ab89e2010-10-29 17:29:13 +00001717/// Examine constraint type and operand type and determine a weight value.
1718/// This object must already have been set up with the operand type
1719/// and the current alternative constraint selected.
1720TargetLowering::ConstraintWeight
1721MipsTargetLowering::getSingleConstraintMatchWeight(
1722 AsmOperandInfo &info, const char *constraint) const {
1723 ConstraintWeight weight = CW_Invalid;
1724 Value *CallOperandVal = info.CallOperandVal;
1725 // If we don't have a value, we can't do a match,
1726 // but allow it at the lowest weight.
1727 if (CallOperandVal == NULL)
1728 return CW_Default;
1729 const Type *type = CallOperandVal->getType();
1730 // Look at the constraint type.
1731 switch (*constraint) {
1732 default:
1733 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
1734 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001735 case 'd':
1736 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00001737 if (type->isIntegerTy())
1738 weight = CW_Register;
1739 break;
1740 case 'f':
1741 if (type->isFloatTy())
1742 weight = CW_Register;
1743 break;
1744 }
1745 return weight;
1746}
1747
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001748/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1749/// return a list of registers that can be used to satisfy the constraint.
1750/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001751std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00001752getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001753{
1754 if (Constraint.size() == 1) {
1755 switch (Constraint[0]) {
1756 case 'r':
1757 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001758 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001759 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001760 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001761 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001762 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1763 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001764 }
1765 }
1766 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1767}
1768
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001769/// Given a register class constraint, like 'r', if this corresponds directly
1770/// to an LLVM register class, return a register of 0 and the register class
1771/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001772std::vector<unsigned> MipsTargetLowering::
1773getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001774 EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001775{
1776 if (Constraint.size() != 1)
1777 return std::vector<unsigned>();
1778
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001779 switch (Constraint[0]) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001780 default : break;
1781 case 'r':
1782 // GCC Mips Constraint Letters
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001783 case 'd':
1784 case 'y':
1785 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
1786 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
1787 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001788 Mips::T8, 0);
1789
1790 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001791 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001792 if (Subtarget->isSingleFloat())
1793 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1794 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1795 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1796 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1797 Mips::F30, Mips::F31, 0);
1798 else
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001799 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
1800 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001801 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +00001802 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001803
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001804 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001805 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001806 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
1807 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001808 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001809 }
1810 return std::vector<unsigned>();
1811}
Dan Gohman6520e202008-10-18 02:06:02 +00001812
1813bool
1814MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1815 // The Mips target isn't yet aware of offsets.
1816 return false;
1817}
Evan Chengeb2f9692009-10-27 19:56:55 +00001818
Evan Chenga1eaa3c2009-10-28 01:43:28 +00001819bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
1820 if (VT != MVT::f32 && VT != MVT::f64)
1821 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00001822 if (Imm.isNegZero())
1823 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00001824 return Imm.isZero();
1825}