blob: 42a35ad1b5a1141e497e154a7503aff57aef1af8 [file] [log] [blame]
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014
15#define DEBUG_TYPE "mips-lower"
Akira Hatanakab4d8d312011-05-24 00:23:52 +000016//#include <algorithm>
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000017#include "MipsISelLowering.h"
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +000018#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000019#include "MipsTargetMachine.h"
Chris Lattnerb71b9092009-08-13 06:28:06 +000020#include "MipsTargetObjectFile.h"
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000021#include "MipsSubtarget.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000022#include "llvm/DerivedTypes.h"
23#include "llvm/Function.h"
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +000024#include "llvm/GlobalVariable.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000025#include "llvm/Intrinsics.h"
26#include "llvm/CallingConv.h"
27#include "llvm/CodeGen/CallingConvLower.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000032#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033#include "llvm/CodeGen/ValueTypes.h"
34#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000036using namespace llvm;
37
Chris Lattnerf0144122009-07-28 03:13:23 +000038const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
39 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000040 case MipsISD::JmpLink: return "MipsISD::JmpLink";
41 case MipsISD::Hi: return "MipsISD::Hi";
42 case MipsISD::Lo: return "MipsISD::Lo";
43 case MipsISD::GPRel: return "MipsISD::GPRel";
44 case MipsISD::Ret: return "MipsISD::Ret";
45 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
46 case MipsISD::FPCmp: return "MipsISD::FPCmp";
47 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
48 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
49 case MipsISD::FPRound: return "MipsISD::FPRound";
50 case MipsISD::MAdd: return "MipsISD::MAdd";
51 case MipsISD::MAddu: return "MipsISD::MAddu";
52 case MipsISD::MSub: return "MipsISD::MSub";
53 case MipsISD::MSubu: return "MipsISD::MSubu";
54 case MipsISD::DivRem: return "MipsISD::DivRem";
55 case MipsISD::DivRemU: return "MipsISD::DivRemU";
56 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
57 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
58 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000059 }
60}
61
62MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000063MipsTargetLowering(MipsTargetMachine &TM)
Chris Lattnerb71b9092009-08-13 06:28:06 +000064 : TargetLowering(TM, new MipsTargetObjectFile()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000065 Subtarget = &TM.getSubtarget<MipsSubtarget>();
66
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000067 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000068 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000069 setBooleanContents(ZeroOrOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000070
71 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000072 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
73 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000074
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000075 // When dealing with single precision only, use libcalls
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000076 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000077 if (!Subtarget->isFP64bit())
Owen Anderson825b72b2009-08-11 20:47:22 +000078 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000079
Wesley Peckbf17cfa2010-11-23 03:31:01 +000080 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +000081 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
82 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
83 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000084
Eli Friedman6055a6a2009-07-17 04:07:24 +000085 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +000086 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
87 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +000088
Wesley Peckbf17cfa2010-11-23 03:31:01 +000089 // Used by legalize types to correctly generate the setcc result.
90 // Without this, every float setcc comes with a AND/OR with the result,
91 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000092 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +000093 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000094
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000095 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +000096 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +000097 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000098 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
99 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
100 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
101 setOperationAction(ISD::SELECT, MVT::f32, Custom);
102 setOperationAction(ISD::SELECT, MVT::f64, Custom);
103 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000104 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
105 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
106 setOperationAction(ISD::FP_TO_SINT, 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
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000146
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000147 setOperationAction(ISD::VAARG, MVT::Other, Expand);
148 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
149 setOperationAction(ISD::VAEND, MVT::Other, Expand);
150
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000151 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000152 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
153 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
154 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000155
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000156 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000158
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000159 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
161 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000162 }
163
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000164 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000166
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000167 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000169
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000170 setTargetDAGCombine(ISD::ADDE);
171 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000172 setTargetDAGCombine(ISD::SDIVREM);
173 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000174 setTargetDAGCombine(ISD::SETCC);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000175
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000176 setMinFunctionAlignment(2);
177
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000178 setStackPointerRegisterToSaveRestore(Mips::SP);
179 computeRegisterProperties();
180}
181
Owen Anderson825b72b2009-08-11 20:47:22 +0000182MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
183 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000184}
185
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000186// SelectMadd -
187// Transforms a subgraph in CurDAG if the following pattern is found:
188// (addc multLo, Lo0), (adde multHi, Hi0),
189// where,
190// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000191// Lo0: initial value of Lo register
192// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000193// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000194static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000195 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000196 // for the matching to be successful.
197 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
198
199 if (ADDCNode->getOpcode() != ISD::ADDC)
200 return false;
201
202 SDValue MultHi = ADDENode->getOperand(0);
203 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000204 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000205 unsigned MultOpc = MultHi.getOpcode();
206
207 // MultHi and MultLo must be generated by the same node,
208 if (MultLo.getNode() != MultNode)
209 return false;
210
211 // and it must be a multiplication.
212 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
213 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000214
215 // MultLo amd MultHi must be the first and second output of MultNode
216 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000217 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
218 return false;
219
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000220 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000221 // of the values of MultNode, in which case MultNode will be removed in later
222 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000223 // If there exist users other than ADDENode or ADDCNode, this function returns
224 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000225 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000226 // produced.
227 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
228 return false;
229
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000230 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000231 DebugLoc dl = ADDENode->getDebugLoc();
232
233 // create MipsMAdd(u) node
234 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000235
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000236 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
237 MVT::Glue,
238 MultNode->getOperand(0),// Factor 0
239 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000240 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000241 ADDENode->getOperand(1));// Hi0
242
243 // create CopyFromReg nodes
244 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
245 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000246 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000247 Mips::HI, MVT::i32,
248 CopyFromLo.getValue(2));
249
250 // replace uses of adde and addc here
251 if (!SDValue(ADDCNode, 0).use_empty())
252 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
253
254 if (!SDValue(ADDENode, 0).use_empty())
255 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
256
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000257 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000258}
259
260// SelectMsub -
261// Transforms a subgraph in CurDAG if the following pattern is found:
262// (addc Lo0, multLo), (sube Hi0, multHi),
263// where,
264// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000265// Lo0: initial value of Lo register
266// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000267// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000268static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000269 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000270 // for the matching to be successful.
271 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
272
273 if (SUBCNode->getOpcode() != ISD::SUBC)
274 return false;
275
276 SDValue MultHi = SUBENode->getOperand(1);
277 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000278 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000279 unsigned MultOpc = MultHi.getOpcode();
280
281 // MultHi and MultLo must be generated by the same node,
282 if (MultLo.getNode() != MultNode)
283 return false;
284
285 // and it must be a multiplication.
286 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
287 return false;
288
289 // MultLo amd MultHi must be the first and second output of MultNode
290 // respectively.
291 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
292 return false;
293
294 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
295 // of the values of MultNode, in which case MultNode will be removed in later
296 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000297 // If there exist users other than SUBENode or SUBCNode, this function returns
298 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000299 // instruction node rather than a pair of MULT and MSUB instructions being
300 // produced.
301 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
302 return false;
303
304 SDValue Chain = CurDAG->getEntryNode();
305 DebugLoc dl = SUBENode->getDebugLoc();
306
307 // create MipsSub(u) node
308 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
309
310 SDValue MSub = CurDAG->getNode(MultOpc, dl,
311 MVT::Glue,
312 MultNode->getOperand(0),// Factor 0
313 MultNode->getOperand(1),// Factor 1
314 SUBCNode->getOperand(0),// Lo0
315 SUBENode->getOperand(0));// Hi0
316
317 // create CopyFromReg nodes
318 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
319 MSub);
320 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
321 Mips::HI, MVT::i32,
322 CopyFromLo.getValue(2));
323
324 // replace uses of sube and subc here
325 if (!SDValue(SUBCNode, 0).use_empty())
326 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
327
328 if (!SDValue(SUBENode, 0).use_empty())
329 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
330
331 return true;
332}
333
334static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
335 TargetLowering::DAGCombinerInfo &DCI,
336 const MipsSubtarget* Subtarget) {
337 if (DCI.isBeforeLegalize())
338 return SDValue();
339
340 if (Subtarget->isMips32() && SelectMadd(N, &DAG))
341 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000342
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000343 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000344}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000345
346static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
347 TargetLowering::DAGCombinerInfo &DCI,
348 const MipsSubtarget* Subtarget) {
349 if (DCI.isBeforeLegalize())
350 return SDValue();
351
352 if (Subtarget->isMips32() && SelectMsub(N, &DAG))
353 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000354
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000355 return SDValue();
356}
357
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000358static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
359 TargetLowering::DAGCombinerInfo &DCI,
360 const MipsSubtarget* Subtarget) {
361 if (DCI.isBeforeLegalizeOps())
362 return SDValue();
363
364 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
365 MipsISD::DivRemU;
366 DebugLoc dl = N->getDebugLoc();
367
368 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
369 N->getOperand(0), N->getOperand(1));
370 SDValue InChain = DAG.getEntryNode();
371 SDValue InGlue = DivRem;
372
373 // insert MFLO
374 if (N->hasAnyUseOfValue(0)) {
375 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, Mips::LO, MVT::i32,
376 InGlue);
377 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
378 InChain = CopyFromLo.getValue(1);
379 InGlue = CopyFromLo.getValue(2);
380 }
381
382 // insert MFHI
383 if (N->hasAnyUseOfValue(1)) {
384 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000385 Mips::HI, MVT::i32, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000386 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
387 }
388
389 return SDValue();
390}
391
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000392static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
393 switch (CC) {
394 default: llvm_unreachable("Unknown fp condition code!");
395 case ISD::SETEQ:
396 case ISD::SETOEQ: return Mips::FCOND_OEQ;
397 case ISD::SETUNE: return Mips::FCOND_UNE;
398 case ISD::SETLT:
399 case ISD::SETOLT: return Mips::FCOND_OLT;
400 case ISD::SETGT:
401 case ISD::SETOGT: return Mips::FCOND_OGT;
402 case ISD::SETLE:
403 case ISD::SETOLE: return Mips::FCOND_OLE;
404 case ISD::SETGE:
405 case ISD::SETOGE: return Mips::FCOND_OGE;
406 case ISD::SETULT: return Mips::FCOND_ULT;
407 case ISD::SETULE: return Mips::FCOND_ULE;
408 case ISD::SETUGT: return Mips::FCOND_UGT;
409 case ISD::SETUGE: return Mips::FCOND_UGE;
410 case ISD::SETUO: return Mips::FCOND_UN;
411 case ISD::SETO: return Mips::FCOND_OR;
412 case ISD::SETNE:
413 case ISD::SETONE: return Mips::FCOND_ONE;
414 case ISD::SETUEQ: return Mips::FCOND_UEQ;
415 }
416}
417
418
419// Returns true if condition code has to be inverted.
420static bool InvertFPCondCode(Mips::CondCode CC) {
421 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
422 return false;
423
424 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
425 return true;
426
427 assert(false && "Illegal Condition Code");
428 return false;
429}
430
431// Creates and returns an FPCmp node from a setcc node.
432// Returns Op if setcc is not a floating point comparison.
433static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
434 // must be a SETCC node
435 if (Op.getOpcode() != ISD::SETCC)
436 return Op;
437
438 SDValue LHS = Op.getOperand(0);
439
440 if (!LHS.getValueType().isFloatingPoint())
441 return Op;
442
443 SDValue RHS = Op.getOperand(1);
444 DebugLoc dl = Op.getDebugLoc();
445
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000446 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
447 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000448 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
449
450 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
451 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
452}
453
454// Creates and returns a CMovFPT/F node.
455static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
456 SDValue False, DebugLoc DL) {
457 bool invert = InvertFPCondCode((Mips::CondCode)
458 cast<ConstantSDNode>(Cond.getOperand(2))
459 ->getSExtValue());
460
461 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
462 True.getValueType(), True, False, Cond);
463}
464
465static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
466 TargetLowering::DAGCombinerInfo &DCI,
467 const MipsSubtarget* Subtarget) {
468 if (DCI.isBeforeLegalizeOps())
469 return SDValue();
470
471 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
472
473 if (Cond.getOpcode() != MipsISD::FPCmp)
474 return SDValue();
475
476 SDValue True = DAG.getConstant(1, MVT::i32);
477 SDValue False = DAG.getConstant(0, MVT::i32);
478
479 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
480}
481
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000482SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000483 const {
484 SelectionDAG &DAG = DCI.DAG;
485 unsigned opc = N->getOpcode();
486
487 switch (opc) {
488 default: break;
489 case ISD::ADDE:
490 return PerformADDECombine(N, DAG, DCI, Subtarget);
491 case ISD::SUBE:
492 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000493 case ISD::SDIVREM:
494 case ISD::UDIVREM:
495 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000496 case ISD::SETCC:
497 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000498 }
499
500 return SDValue();
501}
502
Dan Gohman475871a2008-07-27 21:46:04 +0000503SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000504LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000505{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000506 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000507 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000508 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000509 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
510 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000511 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000512 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000513 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000514 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
515 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000516 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000517 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000518 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000519 }
Dan Gohman475871a2008-07-27 21:46:04 +0000520 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000521}
522
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000523//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000524// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000525//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000526
527// AddLiveIn - This helper function adds the specified physical register to the
528// MachineFunction as a live in value. It also creates a corresponding
529// virtual register for it.
530static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000531AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000532{
533 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000534 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
535 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000536 return VReg;
537}
538
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000539// Get fp branch code (not opcode) from condition code.
540static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
541 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
542 return Mips::BRANCH_T;
543
544 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
545 return Mips::BRANCH_F;
546
547 return Mips::BRANCH_INVALID;
548}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000549
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000550MachineBasicBlock *
551MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000552 MachineBasicBlock *BB) const {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000553 // There is no need to expand CMov instructions if target has
554 // conditional moves.
555 if (Subtarget->hasCondMov())
556 return BB;
557
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000558 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
559 bool isFPCmp = false;
Dale Johannesen94817572009-02-13 02:34:39 +0000560 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000561 unsigned Opc;
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000562
563 switch (MI->getOpcode()) {
564 default: assert(false && "Unexpected instr type to insert");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000565 case Mips::MOVT:
566 case Mips::MOVT_S:
567 case Mips::MOVT_D:
568 isFPCmp = true;
569 Opc = Mips::BC1F;
570 break;
571 case Mips::MOVF:
572 case Mips::MOVF_S:
573 case Mips::MOVF_D:
574 isFPCmp = true;
575 Opc = Mips::BC1T;
576 break;
577 case Mips::MOVZ_I:
578 case Mips::MOVZ_S:
579 case Mips::MOVZ_D:
580 Opc = Mips::BNE;
581 break;
582 case Mips::MOVN_I:
583 case Mips::MOVN_S:
584 case Mips::MOVN_D:
585 Opc = Mips::BEQ;
586 break;
587 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000588
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000589 // To "insert" a SELECT_CC instruction, we actually have to insert the
590 // diamond control-flow pattern. The incoming instruction knows the
591 // destination vreg to set, the condition code register to branch on, the
592 // true/false values to select between, and a branch opcode to use.
593 const BasicBlock *LLVM_BB = BB->getBasicBlock();
594 MachineFunction::iterator It = BB;
595 ++It;
Dan Gohman14152b42010-07-06 20:24:04 +0000596
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000597 // thisMBB:
598 // ...
599 // TrueVal = ...
600 // setcc r1, r2, r3
601 // bNE r1, r0, copy1MBB
602 // fallthrough --> copy0MBB
603 MachineBasicBlock *thisMBB = BB;
604 MachineFunction *F = BB->getParent();
605 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
606 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
607 F->insert(It, copy0MBB);
608 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +0000609
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000610 // Transfer the remainder of BB and its successor edges to sinkMBB.
611 sinkMBB->splice(sinkMBB->begin(), BB,
612 llvm::next(MachineBasicBlock::iterator(MI)),
613 BB->end());
614 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000615
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000616 // Next, add the true and fallthrough blocks as its successors.
617 BB->addSuccessor(copy0MBB);
618 BB->addSuccessor(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000619
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000620 // Emit the right instruction according to the type of the operands compared
621 if (isFPCmp)
622 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
623 else
624 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
625 .addReg(Mips::ZERO).addMBB(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000626
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000627
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000628 // copy0MBB:
629 // %FalseValue = ...
630 // # fallthrough to sinkMBB
631 BB = copy0MBB;
632
633 // Update machine-CFG edges
634 BB->addSuccessor(sinkMBB);
635
636 // sinkMBB:
637 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
638 // ...
639 BB = sinkMBB;
640
641 if (isFPCmp)
Dan Gohman14152b42010-07-06 20:24:04 +0000642 BuildMI(*BB, BB->begin(), dl,
643 TII->get(Mips::PHI), MI->getOperand(0).getReg())
Bruno Cardoso Lopes29e9daa2010-07-20 07:58:51 +0000644 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000645 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
646 else
647 BuildMI(*BB, BB->begin(), dl,
648 TII->get(Mips::PHI), MI->getOperand(0).getReg())
649 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
650 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000651
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000652 MI->eraseFromParent(); // The pseudo instruction is gone now.
653 return BB;
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000654}
655
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000656//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000657// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000658//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000659
Dan Gohman475871a2008-07-27 21:46:04 +0000660SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000661LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000662{
663 if (!Subtarget->isMips1())
664 return Op;
665
666 MachineFunction &MF = DAG.getMachineFunction();
667 unsigned CCReg = AddLiveIn(MF, Mips::FCR31, Mips::CCRRegisterClass);
668
669 SDValue Chain = DAG.getEntryNode();
670 DebugLoc dl = Op.getDebugLoc();
671 SDValue Src = Op.getOperand(0);
672
673 // Set the condition register
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 SDValue CondReg = DAG.getCopyFromReg(Chain, dl, CCReg, MVT::i32);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000675 CondReg = DAG.getCopyToReg(Chain, dl, Mips::AT, CondReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 CondReg = DAG.getCopyFromReg(CondReg, dl, Mips::AT, MVT::i32);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000677
Owen Anderson825b72b2009-08-11 20:47:22 +0000678 SDValue Cst = DAG.getConstant(3, MVT::i32);
679 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, CondReg, Cst);
680 Cst = DAG.getConstant(2, MVT::i32);
681 SDValue Xor = DAG.getNode(ISD::XOR, dl, MVT::i32, Or, Cst);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000682
683 SDValue InFlag(0, 0);
684 CondReg = DAG.getCopyToReg(Chain, dl, Mips::FCR31, Xor, InFlag);
685
686 // Emit the round instruction and bit convert to integer
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 SDValue Trunc = DAG.getNode(MipsISD::FPRound, dl, MVT::f32,
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000688 Src, CondReg.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000689 SDValue BitCvt = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Trunc);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000690 return BitCvt;
691}
692
693SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000694LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000695{
Akira Hatanaka053546c2011-05-25 02:20:00 +0000696 unsigned StackAlignment =
697 getTargetMachine().getFrameLowering()->getStackAlignment();
698 assert(StackAlignment >=
699 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
700 "Cannot lower if the alignment of the allocated space is larger than \
701 that of the stack.");
702
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000703 SDValue Chain = Op.getOperand(0);
704 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +0000705 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000706
707 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +0000708 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000709
710 // Subtract the dynamic size from the actual stack size to
711 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +0000712 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000713
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000714 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000715 // must be placed in the stack pointer register.
Akira Hatanaka053546c2011-05-25 02:20:00 +0000716 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
717 SDValue());
Akira Hatanakaedacba82011-05-25 17:32:06 +0000718 // Retrieve updated $sp. There is a glue input to prevent instructions that
719 // clobber $sp from being inserted between copytoreg and copyfromreg.
Akira Hatanaka053546c2011-05-25 02:20:00 +0000720 SDValue NewSP = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32,
721 Chain.getValue(1));
722
Akira Hatanakaedacba82011-05-25 17:32:06 +0000723 // The stack space reserved by alloca is located right above the argument
724 // area. It is aligned on a boundary that is a multiple of StackAlignment.
Akira Hatanaka053546c2011-05-25 02:20:00 +0000725 MachineFunction &MF = DAG.getMachineFunction();
726 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
727 unsigned SPOffset = (MipsFI->getMaxCallFrameSize() + StackAlignment - 1) /
728 StackAlignment * StackAlignment;
729 SDValue AllocPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
730 DAG.getConstant(SPOffset, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000731
732 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000733 // value and a chain
Akira Hatanaka053546c2011-05-25 02:20:00 +0000734 SDValue Ops[2] = { AllocPtr, NewSP.getValue(1) };
Dale Johannesena05dca42009-02-04 23:02:30 +0000735 return DAG.getMergeValues(Ops, 2, dl);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000736}
737
738SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000739LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000740{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000741 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000742 // the block to branch to if the condition is true.
743 SDValue Chain = Op.getOperand(0);
744 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000745 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000746
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000747 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
748
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000749 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000750 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000751 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000752
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000753 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000754 Mips::CondCode CC =
755 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000756 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000757
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000758 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000759 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000760}
761
762SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000763LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000764{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000765 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000766
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000767 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000768 if (Cond.getOpcode() != MipsISD::FPCmp)
769 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000770
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000771 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
772 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000773}
774
Dan Gohmand858e902010-04-17 15:26:15 +0000775SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
776 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +0000777 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000778 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000779 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000780
Eli Friedmane2c74082009-08-03 02:22:28 +0000781 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +0000782 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000783
Chris Lattnerb71b9092009-08-13 06:28:06 +0000784 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000785
Chris Lattnere3736f82009-08-13 05:41:27 +0000786 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000787 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
788 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000789 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +0000790 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
791 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000792 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +0000793 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000794 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000795 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
796 MipsII::MO_ABS_HI);
797 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
798 MipsII::MO_ABS_LO);
799 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
800 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000801 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000802 } else {
Devang Patel0d881da2010-07-06 22:08:15 +0000803 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000804 MipsII::MO_GOT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000805 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000806 DAG.getEntryNode(), GA, MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000807 false, false, 0);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000808 // On functions and global targets not internal linked only
809 // a load from got/GP is necessary for PIC to work.
Akira Hatanaka9777e7a2011-04-07 19:51:44 +0000810 if (!GV->hasInternalLinkage() &&
811 (!GV->hasLocalLinkage() || isa<Function>(GV)))
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000812 return ResNode;
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000813 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
814 MipsII::MO_ABS_LO);
815 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000817 }
818
Torok Edwinc23197a2009-07-14 16:55:14 +0000819 llvm_unreachable("Dont know how to handle GlobalAddress");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000820 return SDValue(0,0);
821}
822
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000823SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
824 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +0000825 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
826 // FIXME there isn't actually debug info here
827 DebugLoc dl = Op.getDebugLoc();
828
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000829 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +0000830 // %hi/%lo relocation
831 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
832 MipsII::MO_ABS_HI);
833 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
834 MipsII::MO_ABS_LO);
835 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
836 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
837 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000838 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +0000839
840 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
841 MipsII::MO_GOT);
842 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
843 MipsII::MO_ABS_LO);
844 SDValue Load = DAG.getLoad(MVT::i32, dl,
845 DAG.getEntryNode(), BAGOTOffset,
846 MachinePointerInfo(), false, false, 0);
847 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
848 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000849}
850
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000851SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000852LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000853{
Torok Edwinc23197a2009-07-14 16:55:14 +0000854 llvm_unreachable("TLS not implemented for MIPS.");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000855 return SDValue(); // Not reached
856}
857
858SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000859LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000860{
Dan Gohman475871a2008-07-27 21:46:04 +0000861 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000862 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +0000863 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000864 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000865 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000866 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000867
Owen Andersone50ed302009-08-10 22:56:29 +0000868 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000869 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000870
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000871 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
872
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +0000873 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +0000874 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000875 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000876 } else // Emit Load from Global Pointer
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000877 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
878 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000879 false, false, 0);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000880
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000881 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
882 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000883 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000885
886 return ResNode;
887}
888
Dan Gohman475871a2008-07-27 21:46:04 +0000889SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000890LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000891{
Dan Gohman475871a2008-07-27 21:46:04 +0000892 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000893 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +0000894 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +0000895 // FIXME there isn't actually debug info here
896 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000897
898 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000899 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000900 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000901 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000902 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +0000903 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000904 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
905 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000906 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000907
908 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000909 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000910 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000911 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000912 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000913 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
914 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000915 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000916 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000917 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000918 N->getOffset(), MipsII::MO_GOT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000919 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000920 CP, MachinePointerInfo::getConstantPool(),
921 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000922 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000923 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +0000924 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000925 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
926 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000927
928 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000929}
930
Dan Gohmand858e902010-04-17 15:26:15 +0000931SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +0000932 MachineFunction &MF = DAG.getMachineFunction();
933 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
934
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000935 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +0000936 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
937 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000938
939 // vastart just stores the address of the VarArgsFrameIndex slot into the
940 // memory location argument.
941 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +0000942 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
943 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +0000944 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000945}
946
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000947static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
948 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
949 DebugLoc dl = Op.getDebugLoc();
950 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
951 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
952 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
953 DAG.getConstant(0x7fffffff, MVT::i32));
954 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
955 DAG.getConstant(0x80000000, MVT::i32));
956 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
957 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
958}
959
960static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
961 // FIXME:
962 // Use ext/ins instructions if target architecture is Mips32r2.
963 // Eliminate redundant mfc1 and mtc1 instructions.
964 unsigned LoIdx = 0, HiIdx = 1;
965
966 if (!isLittle)
967 std::swap(LoIdx, HiIdx);
968
969 DebugLoc dl = Op.getDebugLoc();
970 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
971 Op.getOperand(0),
972 DAG.getConstant(LoIdx, MVT::i32));
973 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
974 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
975 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
976 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
977 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
978 DAG.getConstant(0x7fffffff, MVT::i32));
979 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
980 DAG.getConstant(0x80000000, MVT::i32));
981 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
982
983 if (!isLittle)
984 std::swap(Word0, Word1);
985
986 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
987}
988
989SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
990 const {
991 EVT Ty = Op.getValueType();
992
993 assert(Ty == MVT::f32 || Ty == MVT::f64);
994
995 if (Ty == MVT::f32)
996 return LowerFCOPYSIGN32(Op, DAG);
997 else
998 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
999}
1000
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001001//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001002// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001003//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001004
1005#include "MipsGenCallingConv.inc"
1006
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001007//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001008// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001009// Mips O32 ABI rules:
1010// ---
1011// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001012// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001013// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001014// f64 - Only passed in two aliased f32 registers if no int reg has been used
1015// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001016// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1017// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001018//
1019// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001020//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001021
Duncan Sands1e96bab2010-11-04 10:49:57 +00001022static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001023 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001024 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1025
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001026 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001027
1028 static const unsigned IntRegs[] = {
1029 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1030 };
1031 static const unsigned F32Regs[] = {
1032 Mips::F12, Mips::F14
1033 };
1034 static const unsigned F64Regs[] = {
1035 Mips::D6, Mips::D7
1036 };
1037
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001038 // ByVal Args
1039 if (ArgFlags.isByVal()) {
1040 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1041 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1042 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1043 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1044 r < std::min(IntRegsSize, NextReg); ++r)
1045 State.AllocateReg(IntRegs[r]);
1046 return false;
1047 }
1048
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001049 // Promote i8 and i16
1050 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1051 LocVT = MVT::i32;
1052 if (ArgFlags.isSExt())
1053 LocInfo = CCValAssign::SExt;
1054 else if (ArgFlags.isZExt())
1055 LocInfo = CCValAssign::ZExt;
1056 else
1057 LocInfo = CCValAssign::AExt;
1058 }
1059
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001060 unsigned Reg;
1061
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001062 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1063 // is true: function is vararg, argument is 3rd or higher, there is previous
1064 // argument which is not f32 or f64.
1065 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1066 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001067 unsigned OrigAlign = ArgFlags.getOrigAlign();
1068 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001069
1070 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001071 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001072 // If this is the first part of an i64 arg,
1073 // the allocated register must be either A0 or A2.
1074 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1075 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001076 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001077 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1078 // Allocate int register and shadow next int register. If first
1079 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001080 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1081 if (Reg == Mips::A1 || Reg == Mips::A3)
1082 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1083 State.AllocateReg(IntRegs, IntRegsSize);
1084 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001085 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1086 // we are guaranteed to find an available float register
1087 if (ValVT == MVT::f32) {
1088 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1089 // Shadow int register
1090 State.AllocateReg(IntRegs, IntRegsSize);
1091 } else {
1092 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1093 // Shadow int registers
1094 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1095 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1096 State.AllocateReg(IntRegs, IntRegsSize);
1097 State.AllocateReg(IntRegs, IntRegsSize);
1098 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001099 } else
1100 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001101
Akira Hatanakad37776d2011-05-20 21:39:54 +00001102 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1103 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1104
1105 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001106 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001107 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001108 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001109
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001110 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001111}
1112
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001113//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001114// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001115//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001116
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001117static const unsigned O32IntRegsSize = 4;
1118
1119static const unsigned O32IntRegs[] = {
1120 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1121};
1122
1123// Write ByVal Arg to arg registers and stack.
1124static void
1125WriteByValArg(SDValue& Chain, DebugLoc dl,
1126 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1127 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1128 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001129 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
1130 MVT PtrType) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001131 unsigned FirstWord = VA.getLocMemOffset() / 4;
1132 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
1133 unsigned LastWord = FirstWord + NumWords;
1134 unsigned CurWord;
1135
1136 // copy the first 4 words of byval arg to registers A0 - A3
1137 for (CurWord = FirstWord; CurWord < std::min(LastWord, O32IntRegsSize);
1138 ++CurWord) {
1139 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1140 DAG.getConstant((CurWord - FirstWord) * 4,
1141 MVT::i32));
1142 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1143 MachinePointerInfo(),
1144 false, false, 0);
1145 MemOpChains.push_back(LoadVal.getValue(1));
1146 unsigned DstReg = O32IntRegs[CurWord];
1147 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1148 }
1149
1150 // copy remaining part of byval arg to stack.
1151 if (CurWord < LastWord) {
1152 unsigned SizeInBytes = (LastWord - CurWord) * 4;
1153 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1154 DAG.getConstant((CurWord - FirstWord) * 4,
1155 MVT::i32));
1156 LastFI = MFI->CreateFixedObject(SizeInBytes, CurWord * 4, true);
1157 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
1158 Chain = DAG.getMemcpy(Chain, dl, Dst, Src,
1159 DAG.getConstant(SizeInBytes, MVT::i32),
1160 /*Align*/4,
1161 /*isVolatile=*/false, /*AlwaysInline=*/false,
1162 MachinePointerInfo(0), MachinePointerInfo(0));
1163 MemOpChains.push_back(Chain);
1164 }
1165}
1166
Dan Gohman98ca4f22009-08-05 01:29:28 +00001167/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001168/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001169/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001170SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001171MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001172 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001173 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001174 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001175 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001176 const SmallVectorImpl<ISD::InputArg> &Ins,
1177 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001178 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001179 // MIPs target does not yet support tail call optimization.
1180 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001181
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001182 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001183 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00001184 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001185 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001186 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001187
1188 // Analyze operands of the call, assigning locations to each operand.
1189 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001190 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1191 *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001192
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001193 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001194 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001195 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001196 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001197
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001198 // Get a count of how many bytes are to be pushed on the stack.
1199 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnere563bbc2008-10-11 22:08:30 +00001200 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001201
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001202 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001203 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1204 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001205
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001206 MipsFI->setHasCall();
1207
Akira Hatanakaedacba82011-05-25 17:32:06 +00001208 // If this is the first call, create a stack frame object that points to
1209 // a location to which .cprestore saves $gp. The offset of this frame object
1210 // is set to 0, since we know nothing about the size of the argument area at
1211 // this point.
Akira Hatanaka69c19f72011-05-23 20:16:59 +00001212 if (IsPIC && !MipsFI->getGPFI())
Akira Hatanaka43299772011-05-20 23:22:14 +00001213 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1214
1215 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
1216
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001217 // Walk the register/memloc assignments, inserting copies/loads.
1218 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001219 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001220 CCValAssign &VA = ArgLocs[i];
1221
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001222 // Promote the value if needed.
1223 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001224 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001225 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001226 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001227 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001228 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001229 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001230 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1231 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001232 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1233 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001234 if (!Subtarget->isLittle())
1235 std::swap(Lo, Hi);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001236 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
1237 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
1238 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001239 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001240 }
1241 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00001242 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001243 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001244 break;
1245 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001246 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001247 break;
1248 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001249 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001250 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001251 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001252
1253 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001254 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001255 if (VA.isRegLoc()) {
1256 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00001257 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001258 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001259
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001260 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00001261 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001262
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001263 // ByVal Arg.
1264 ISD::ArgFlagsTy Flags = Outs[i].Flags;
1265 if (Flags.isByVal()) {
1266 assert(Subtarget->isABI_O32() &&
1267 "No support for ByVal args by ABIs other than O32 yet.");
1268 assert(Flags.getByValSize() &&
1269 "ByVal args of size 0 should have been ignored by front-end.");
1270 WriteByValArg(Chain, dl, RegsToPass, MemOpChains, LastFI, MFI, DAG, Arg,
1271 VA, Flags, getPointerTy());
1272 continue;
1273 }
1274
Chris Lattnere0b12152008-03-17 06:57:02 +00001275 // Create the frame index object for this incoming parameter
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001276 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
1277 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00001278 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00001279
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001280 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00001281 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00001282 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
1283 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001284 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001285 }
1286
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001287 // Transform all store nodes into one single node because all store
1288 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001289 if (!MemOpChains.empty())
1290 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001291 &MemOpChains[0], MemOpChains.size());
1292
Bill Wendling056292f2008-09-16 21:48:12 +00001293 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001294 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1295 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001296 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001297 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001298 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001299
1300 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001301 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
1302 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1303 getPointerTy(), 0,MipsII:: MO_GOT);
1304 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
1305 0, MipsII::MO_ABS_LO);
1306 } else {
1307 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1308 getPointerTy(), 0, OpFlag);
1309 }
1310
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001311 LoadSymAddr = true;
1312 }
1313 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001314 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001315 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001316 LoadSymAddr = true;
1317 }
1318
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00001319 SDValue InFlag;
1320
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001321 // Create nodes that load address of callee and copy it to T9
1322 if (IsPIC) {
1323 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001324 // Load callee address
1325 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, Chain, Callee,
1326 MachinePointerInfo::getGOT(),
1327 false, false, 0);
1328
1329 // Use GOT+LO if callee has internal linkage.
1330 if (CalleeLo.getNode()) {
1331 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
1332 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
1333 } else
1334 Callee = LoadValue;
1335
1336 // Use chain output from LoadValue
1337 Chain = LoadValue.getValue(1);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001338 }
1339
1340 // copy to T9
1341 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
1342 InFlag = Chain.getValue(1);
1343 Callee = DAG.getRegister(Mips::T9, MVT::i32);
1344 }
Bill Wendling056292f2008-09-16 21:48:12 +00001345
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00001346 // Build a sequence of copy-to-reg nodes chained together with token
1347 // chain and flag operands which copy the outgoing args into registers.
1348 // The InFlag in necessary since all emitted instructions must be
1349 // stuck together.
1350 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1351 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1352 RegsToPass[i].second, InFlag);
1353 InFlag = Chain.getValue(1);
1354 }
1355
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001356 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001357 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001358 //
1359 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001360 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00001361 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001362 Ops.push_back(Chain);
1363 Ops.push_back(Callee);
1364
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001365 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001366 // known live into the call.
1367 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1368 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1369 RegsToPass[i].second.getValueType()));
1370
Gabor Greifba36cb52008-08-28 21:40:38 +00001371 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001372 Ops.push_back(InFlag);
1373
Dale Johannesen33c960f2009-02-04 20:06:27 +00001374 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001375 InFlag = Chain.getValue(1);
1376
Akira Hatanaka4c62f762011-05-25 18:08:32 +00001377 // Function can have an arbitrary number of calls, so
1378 // hold the LastArgStackLoc with the biggest offset.
1379 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1380 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001381
Akira Hatanaka4c62f762011-05-25 18:08:32 +00001382 // For O32, a minimum of four words (16 bytes) of argument space is
1383 // allocated.
1384 if (Subtarget->isABI_O32())
1385 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
Akira Hatanakad37776d2011-05-20 21:39:54 +00001386
Akira Hatanaka4c62f762011-05-25 18:08:32 +00001387 if (MaxCallFrameSize < NextStackOffset) {
1388 MipsFI->setMaxCallFrameSize(NextStackOffset);
Akira Hatanakad37776d2011-05-20 21:39:54 +00001389
Akira Hatanaka4c62f762011-05-25 18:08:32 +00001390 if (IsPIC) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001391 // $gp restore slot must be aligned.
1392 unsigned StackAlignment = TFL->getStackAlignment();
1393 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
1394 StackAlignment * StackAlignment;
1395 int GPFI = MipsFI->getGPFI();
1396 MFI->setObjectOffset(GPFI, NextStackOffset);
1397 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001398 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001399
Akira Hatanaka43299772011-05-20 23:22:14 +00001400 // Extend range of indices of frame objects for outgoing arguments that were
1401 // created during this function call. Skip this step if no such objects were
1402 // created.
1403 if (LastFI)
1404 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
1405
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00001406 // Create the CALLSEQ_END node.
1407 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1408 DAG.getIntPtrConstant(0, true), InFlag);
1409 InFlag = Chain.getValue(1);
1410
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001411 // Handle result values, copying them out of physregs into vregs that we
1412 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001413 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
1414 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001415}
1416
Dan Gohman98ca4f22009-08-05 01:29:28 +00001417/// LowerCallResult - Lower the result values of a call into the
1418/// appropriate copies out of appropriate physical registers.
1419SDValue
1420MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001421 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001422 const SmallVectorImpl<ISD::InputArg> &Ins,
1423 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001424 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001425
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001426 // Assign locations to each value returned by this call.
1427 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001428 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001429 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001430
Dan Gohman98ca4f22009-08-05 01:29:28 +00001431 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001432
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001433 // Copy all of the result registers out of their specified physreg.
1434 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001435 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001436 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001437 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001438 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001439 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001440
Dan Gohman98ca4f22009-08-05 01:29:28 +00001441 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001442}
1443
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001444//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001445// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001446//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001447static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
1448 std::vector<SDValue>& OutChains,
1449 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
1450 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
1451 unsigned LocMem = VA.getLocMemOffset();
1452 unsigned FirstWord = LocMem / 4;
1453
1454 // copy register A0 - A3 to frame object
1455 for (unsigned i = 0; i < NumWords; ++i) {
1456 unsigned CurWord = FirstWord + i;
1457 if (CurWord >= O32IntRegsSize)
1458 break;
1459
1460 unsigned SrcReg = O32IntRegs[CurWord];
1461 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
1462 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
1463 DAG.getConstant(i * 4, MVT::i32));
1464 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
1465 StorePtr, MachinePointerInfo(), false,
1466 false, 0);
1467 OutChains.push_back(Store);
1468 }
1469}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001470
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001471/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001472/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001473SDValue
1474MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001475 CallingConv::ID CallConv,
1476 bool isVarArg,
1477 const SmallVectorImpl<ISD::InputArg>
1478 &Ins,
1479 DebugLoc dl, SelectionDAG &DAG,
1480 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001481 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00001482 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001483 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001484 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001485
Dan Gohman1e93df62010-04-17 14:41:14 +00001486 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001487
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001488 // Used with vargs to acumulate store chains.
1489 std::vector<SDValue> OutChains;
1490
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001491 // Assign locations to all of the incoming arguments.
1492 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001493 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1494 ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001495
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001496 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001497 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001498 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001499 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001500
Akira Hatanaka43299772011-05-20 23:22:14 +00001501 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001502
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001503 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001504 CCValAssign &VA = ArgLocs[i];
1505
1506 // Arguments stored on registers
1507 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001508 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001509 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00001510 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001511
Owen Anderson825b72b2009-08-11 20:47:22 +00001512 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001513 RC = Mips::CPURegsRegisterClass;
1514 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001515 RC = Mips::FGR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001516 else if (RegVT == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001517 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001518 RC = Mips::AFGR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001519 } else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001520 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001521
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001522 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001523 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001524 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001525 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001526
1527 // If this is an 8 or 16-bit value, it has been passed promoted
1528 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001529 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001530 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00001531 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001532 if (VA.getLocInfo() == CCValAssign::SExt)
1533 Opcode = ISD::AssertSext;
1534 else if (VA.getLocInfo() == CCValAssign::ZExt)
1535 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00001536 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001537 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00001538 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00001539 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001540 }
1541
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001542 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001543 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001544 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
1545 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00001546 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001547 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001548 VA.getLocReg()+1, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001549 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001550 if (!Subtarget->isLittle())
1551 std::swap(ArgValue, ArgValue2);
1552 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
1553 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001554 }
1555 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001556
Dan Gohman98ca4f22009-08-05 01:29:28 +00001557 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001558 } else { // VA.isRegLoc()
1559
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001560 // sanity check
1561 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001562
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001563 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1564
1565 if (Flags.isByVal()) {
1566 assert(Subtarget->isABI_O32() &&
1567 "No support for ByVal args by ABIs other than O32 yet.");
1568 assert(Flags.getByValSize() &&
1569 "ByVal args of size 0 should have been ignored by front-end.");
1570 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
1571 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
1572 true);
1573 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
1574 InVals.push_back(FIN);
1575 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
1576
1577 continue;
1578 }
1579
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001580 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001581 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
1582 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001583
1584 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00001585 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001586 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00001587 MachinePointerInfo::getFixedStack(LastFI),
David Greenef6fa1862010-02-15 16:56:10 +00001588 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001589 }
1590 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001591
1592 // The mips ABIs for returning structs by value requires that we copy
1593 // the sret argument into $v0 for the return. Save the argument into
1594 // a virtual register so that we can access it from the return points.
1595 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1596 unsigned Reg = MipsFI->getSRetReturnReg();
1597 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001598 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001599 MipsFI->setSRetReturnReg(Reg);
1600 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001601 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001602 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001603 }
1604
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001605 if (isVarArg && Subtarget->isABI_O32()) {
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001606 // Record the frame index of the first variable argument
1607 // which is a value necessary to VASTART.
1608 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001609 assert(NextStackOffset % 4 == 0 &&
1610 "NextStackOffset must be aligned to 4-byte boundaries.");
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001611 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
1612 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00001613
1614 // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
1615 // copy the integer registers that have not been used for argument passing
1616 // to the caller's stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001617 for (; NextStackOffset < 16; NextStackOffset += 4) {
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00001618 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001619 unsigned Idx = NextStackOffset / 4;
1620 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
1621 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Akira Hatanaka69c19f72011-05-23 20:16:59 +00001622 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001623 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
1624 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
1625 MachinePointerInfo(),
1626 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001627 }
1628 }
1629
Akira Hatanaka43299772011-05-20 23:22:14 +00001630 MipsFI->setLastInArgFI(LastFI);
1631
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001632 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001633 // the size of Ins and InVals. This only happens when on varg functions
1634 if (!OutChains.empty()) {
1635 OutChains.push_back(Chain);
1636 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1637 &OutChains[0], OutChains.size());
1638 }
1639
Dan Gohman98ca4f22009-08-05 01:29:28 +00001640 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001641}
1642
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001643//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001644// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001645//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001646
Dan Gohman98ca4f22009-08-05 01:29:28 +00001647SDValue
1648MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001649 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001650 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001651 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001652 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001653
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001654 // CCValAssign - represent the assignment of
1655 // the return value to a location
1656 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001657
1658 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001659 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1660 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001661
Dan Gohman98ca4f22009-08-05 01:29:28 +00001662 // Analize return values.
1663 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001664
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001665 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001666 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001667 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001668 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001669 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001670 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001671 }
1672
Dan Gohman475871a2008-07-27 21:46:04 +00001673 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001674
1675 // Copy the result values into the output registers.
1676 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1677 CCValAssign &VA = RVLocs[i];
1678 assert(VA.isRegLoc() && "Can only return in registers!");
1679
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001680 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001681 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001682
1683 // guarantee that all emitted copies are
1684 // stuck together, avoiding something bad
1685 Flag = Chain.getValue(1);
1686 }
1687
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001688 // The mips ABIs for returning structs by value requires that we copy
1689 // the sret argument into $v0 for the return. We saved the argument into
1690 // a virtual register in the entry block, so now we copy the value out
1691 // and into $v0.
1692 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1693 MachineFunction &MF = DAG.getMachineFunction();
1694 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1695 unsigned Reg = MipsFI->getSRetReturnReg();
1696
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001697 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00001698 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00001699 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001700
Dale Johannesena05dca42009-02-04 23:02:30 +00001701 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001702 Flag = Chain.getValue(1);
1703 }
1704
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001705 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00001706 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001707 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001708 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001709 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001710 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001711 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001712}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001713
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001714//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001715// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001716//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001717
1718/// getConstraintType - Given a constraint letter, return the type of
1719/// constraint it is for this target.
1720MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001721getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001722{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001723 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001724 // GCC config/mips/constraints.md
1725 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001726 // 'd' : An address register. Equivalent to r
1727 // unless generating MIPS16 code.
1728 // 'y' : Equivalent to r; retained for
1729 // backwards compatibility.
1730 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001731 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001732 switch (Constraint[0]) {
1733 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001734 case 'd':
1735 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001736 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001737 return C_RegisterClass;
1738 break;
1739 }
1740 }
1741 return TargetLowering::getConstraintType(Constraint);
1742}
1743
John Thompson44ab89e2010-10-29 17:29:13 +00001744/// Examine constraint type and operand type and determine a weight value.
1745/// This object must already have been set up with the operand type
1746/// and the current alternative constraint selected.
1747TargetLowering::ConstraintWeight
1748MipsTargetLowering::getSingleConstraintMatchWeight(
1749 AsmOperandInfo &info, const char *constraint) const {
1750 ConstraintWeight weight = CW_Invalid;
1751 Value *CallOperandVal = info.CallOperandVal;
1752 // If we don't have a value, we can't do a match,
1753 // but allow it at the lowest weight.
1754 if (CallOperandVal == NULL)
1755 return CW_Default;
1756 const Type *type = CallOperandVal->getType();
1757 // Look at the constraint type.
1758 switch (*constraint) {
1759 default:
1760 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
1761 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001762 case 'd':
1763 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00001764 if (type->isIntegerTy())
1765 weight = CW_Register;
1766 break;
1767 case 'f':
1768 if (type->isFloatTy())
1769 weight = CW_Register;
1770 break;
1771 }
1772 return weight;
1773}
1774
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001775/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1776/// return a list of registers that can be used to satisfy the constraint.
1777/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001778std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00001779getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001780{
1781 if (Constraint.size() == 1) {
1782 switch (Constraint[0]) {
1783 case 'r':
1784 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001785 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001786 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001787 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001788 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001789 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1790 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001791 }
1792 }
1793 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1794}
1795
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001796/// Given a register class constraint, like 'r', if this corresponds directly
1797/// to an LLVM register class, return a register of 0 and the register class
1798/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001799std::vector<unsigned> MipsTargetLowering::
1800getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001801 EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001802{
1803 if (Constraint.size() != 1)
1804 return std::vector<unsigned>();
1805
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001806 switch (Constraint[0]) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001807 default : break;
1808 case 'r':
1809 // GCC Mips Constraint Letters
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001810 case 'd':
1811 case 'y':
1812 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
1813 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
1814 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001815 Mips::T8, 0);
1816
1817 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001818 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001819 if (Subtarget->isSingleFloat())
1820 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1821 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1822 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1823 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1824 Mips::F30, Mips::F31, 0);
1825 else
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001826 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
1827 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001828 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +00001829 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001830
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001831 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001832 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001833 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
1834 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001835 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001836 }
1837 return std::vector<unsigned>();
1838}
Dan Gohman6520e202008-10-18 02:06:02 +00001839
1840bool
1841MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1842 // The Mips target isn't yet aware of offsets.
1843 return false;
1844}
Evan Chengeb2f9692009-10-27 19:56:55 +00001845
Evan Chenga1eaa3c2009-10-28 01:43:28 +00001846bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
1847 if (VT != MVT::f32 && VT != MVT::f64)
1848 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00001849 if (Imm.isNegZero())
1850 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00001851 return Imm.isZero();
1852}