blob: bb67cb9b8e4aecf09b7c11b79508dedba025ab7b [file] [log] [blame]
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014
15#define DEBUG_TYPE "mips-lower"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000016#include "MipsISelLowering.h"
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +000017#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000018#include "MipsTargetMachine.h"
Chris Lattnerb71b9092009-08-13 06:28:06 +000019#include "MipsTargetObjectFile.h"
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000020#include "MipsSubtarget.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000021#include "llvm/DerivedTypes.h"
22#include "llvm/Function.h"
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +000023#include "llvm/GlobalVariable.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000024#include "llvm/Intrinsics.h"
25#include "llvm/CallingConv.h"
Akira Hatanaka794bf172011-07-07 23:56:50 +000026#include "InstPrinter/MipsInstPrinter.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000027#include "llvm/CodeGen/CallingConvLower.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000032#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033#include "llvm/CodeGen/ValueTypes.h"
34#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000036using namespace llvm;
37
Chris Lattnerf0144122009-07-28 03:13:23 +000038const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
39 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000040 case MipsISD::JmpLink: return "MipsISD::JmpLink";
41 case MipsISD::Hi: return "MipsISD::Hi";
42 case MipsISD::Lo: return "MipsISD::Lo";
43 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +000044 case MipsISD::TlsGd: return "MipsISD::TlsGd";
45 case MipsISD::TprelHi: return "MipsISD::TprelHi";
46 case MipsISD::TprelLo: return "MipsISD::TprelLo";
47 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000048 case MipsISD::Ret: return "MipsISD::Ret";
49 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
50 case MipsISD::FPCmp: return "MipsISD::FPCmp";
51 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
52 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
53 case MipsISD::FPRound: return "MipsISD::FPRound";
54 case MipsISD::MAdd: return "MipsISD::MAdd";
55 case MipsISD::MAddu: return "MipsISD::MAddu";
56 case MipsISD::MSub: return "MipsISD::MSub";
57 case MipsISD::MSubu: return "MipsISD::MSubu";
58 case MipsISD::DivRem: return "MipsISD::DivRem";
59 case MipsISD::DivRemU: return "MipsISD::DivRemU";
60 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
61 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanaka342837d2011-05-28 01:07:07 +000062 case MipsISD::WrapperPIC: return "MipsISD::WrapperPIC";
Akira Hatanaka21afc632011-06-21 00:40:49 +000063 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
Akira Hatanaka0f843822011-06-07 18:58:42 +000064 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000065 }
66}
67
68MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000069MipsTargetLowering(MipsTargetMachine &TM)
Chris Lattnerb71b9092009-08-13 06:28:06 +000070 : TargetLowering(TM, new MipsTargetObjectFile()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000071 Subtarget = &TM.getSubtarget<MipsSubtarget>();
72
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000073 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000074 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000075 setBooleanContents(ZeroOrOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000076
77 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000078 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
79 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000080
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000081 // When dealing with single precision only, use libcalls
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000082 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000083 if (!Subtarget->isFP64bit())
Owen Anderson825b72b2009-08-11 20:47:22 +000084 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000085
Wesley Peckbf17cfa2010-11-23 03:31:01 +000086 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +000087 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
88 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
89 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000090
Eli Friedman6055a6a2009-07-17 04:07:24 +000091 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +000092 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
93 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +000094
Wesley Peckbf17cfa2010-11-23 03:31:01 +000095 // Used by legalize types to correctly generate the setcc result.
96 // Without this, every float setcc comes with a AND/OR with the result,
97 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000098 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +000099 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000100
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000101 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +0000102 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000103 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000104 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
105 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
106 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
107 setOperationAction(ISD::SELECT, MVT::f32, Custom);
108 setOperationAction(ISD::SELECT, MVT::f64, Custom);
109 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000110 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
111 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000112 setOperationAction(ISD::VASTART, MVT::Other, Custom);
113
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000114 setOperationAction(ISD::SDIV, MVT::i32, Expand);
115 setOperationAction(ISD::SREM, MVT::i32, Expand);
116 setOperationAction(ISD::UDIV, MVT::i32, Expand);
117 setOperationAction(ISD::UREM, MVT::i32, Expand);
118
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000119 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000120 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
121 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
122 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
123 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
124 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
125 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
126 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
127 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
128 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000129
130 if (!Subtarget->isMips32r2())
131 setOperationAction(ISD::ROTR, MVT::i32, Expand);
132
Owen Anderson825b72b2009-08-11 20:47:22 +0000133 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
134 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
135 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000136 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
137 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000138 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000139 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000141 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000142 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
143 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000144 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 setOperationAction(ISD::FLOG, MVT::f32, Expand);
146 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
147 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
148 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000149 setOperationAction(ISD::FMA, MVT::f32, Expand);
150 setOperationAction(ISD::FMA, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000151
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000152 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
153 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000154
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000155 setOperationAction(ISD::VAARG, MVT::Other, Expand);
156 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
157 setOperationAction(ISD::VAEND, MVT::Other, Expand);
158
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000159 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
161 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
162 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000163
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000164 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000166
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000167 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
169 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000170 }
171
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000172 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000173 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000174
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000175 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000176 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000177
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000178 setTargetDAGCombine(ISD::ADDE);
179 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000180 setTargetDAGCombine(ISD::SDIVREM);
181 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000182 setTargetDAGCombine(ISD::SETCC);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000183
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000184 setMinFunctionAlignment(2);
185
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000186 setStackPointerRegisterToSaveRestore(Mips::SP);
187 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000188
189 setExceptionPointerRegister(Mips::A0);
190 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000191}
192
Owen Anderson825b72b2009-08-11 20:47:22 +0000193MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
194 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000195}
196
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000197// SelectMadd -
198// Transforms a subgraph in CurDAG if the following pattern is found:
199// (addc multLo, Lo0), (adde multHi, Hi0),
200// where,
201// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000202// Lo0: initial value of Lo register
203// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000204// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000205static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000206 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000207 // for the matching to be successful.
208 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
209
210 if (ADDCNode->getOpcode() != ISD::ADDC)
211 return false;
212
213 SDValue MultHi = ADDENode->getOperand(0);
214 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000215 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000216 unsigned MultOpc = MultHi.getOpcode();
217
218 // MultHi and MultLo must be generated by the same node,
219 if (MultLo.getNode() != MultNode)
220 return false;
221
222 // and it must be a multiplication.
223 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
224 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000225
226 // MultLo amd MultHi must be the first and second output of MultNode
227 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000228 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
229 return false;
230
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000231 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000232 // of the values of MultNode, in which case MultNode will be removed in later
233 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000234 // If there exist users other than ADDENode or ADDCNode, this function returns
235 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000236 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000237 // produced.
238 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
239 return false;
240
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000241 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000242 DebugLoc dl = ADDENode->getDebugLoc();
243
244 // create MipsMAdd(u) node
245 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000246
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000247 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
248 MVT::Glue,
249 MultNode->getOperand(0),// Factor 0
250 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000251 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000252 ADDENode->getOperand(1));// Hi0
253
254 // create CopyFromReg nodes
255 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
256 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000257 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000258 Mips::HI, MVT::i32,
259 CopyFromLo.getValue(2));
260
261 // replace uses of adde and addc here
262 if (!SDValue(ADDCNode, 0).use_empty())
263 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
264
265 if (!SDValue(ADDENode, 0).use_empty())
266 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
267
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000268 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000269}
270
271// SelectMsub -
272// Transforms a subgraph in CurDAG if the following pattern is found:
273// (addc Lo0, multLo), (sube Hi0, multHi),
274// where,
275// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000276// Lo0: initial value of Lo register
277// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000278// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000279static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000280 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000281 // for the matching to be successful.
282 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
283
284 if (SUBCNode->getOpcode() != ISD::SUBC)
285 return false;
286
287 SDValue MultHi = SUBENode->getOperand(1);
288 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000289 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000290 unsigned MultOpc = MultHi.getOpcode();
291
292 // MultHi and MultLo must be generated by the same node,
293 if (MultLo.getNode() != MultNode)
294 return false;
295
296 // and it must be a multiplication.
297 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
298 return false;
299
300 // MultLo amd MultHi must be the first and second output of MultNode
301 // respectively.
302 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
303 return false;
304
305 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
306 // of the values of MultNode, in which case MultNode will be removed in later
307 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000308 // If there exist users other than SUBENode or SUBCNode, this function returns
309 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000310 // instruction node rather than a pair of MULT and MSUB instructions being
311 // produced.
312 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
313 return false;
314
315 SDValue Chain = CurDAG->getEntryNode();
316 DebugLoc dl = SUBENode->getDebugLoc();
317
318 // create MipsSub(u) node
319 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
320
321 SDValue MSub = CurDAG->getNode(MultOpc, dl,
322 MVT::Glue,
323 MultNode->getOperand(0),// Factor 0
324 MultNode->getOperand(1),// Factor 1
325 SUBCNode->getOperand(0),// Lo0
326 SUBENode->getOperand(0));// Hi0
327
328 // create CopyFromReg nodes
329 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
330 MSub);
331 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
332 Mips::HI, MVT::i32,
333 CopyFromLo.getValue(2));
334
335 // replace uses of sube and subc here
336 if (!SDValue(SUBCNode, 0).use_empty())
337 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
338
339 if (!SDValue(SUBENode, 0).use_empty())
340 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
341
342 return true;
343}
344
345static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
346 TargetLowering::DAGCombinerInfo &DCI,
347 const MipsSubtarget* Subtarget) {
348 if (DCI.isBeforeLegalize())
349 return SDValue();
350
351 if (Subtarget->isMips32() && SelectMadd(N, &DAG))
352 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000353
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000354 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000355}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000356
357static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
358 TargetLowering::DAGCombinerInfo &DCI,
359 const MipsSubtarget* Subtarget) {
360 if (DCI.isBeforeLegalize())
361 return SDValue();
362
363 if (Subtarget->isMips32() && SelectMsub(N, &DAG))
364 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000365
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000366 return SDValue();
367}
368
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000369static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
370 TargetLowering::DAGCombinerInfo &DCI,
371 const MipsSubtarget* Subtarget) {
372 if (DCI.isBeforeLegalizeOps())
373 return SDValue();
374
375 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
376 MipsISD::DivRemU;
377 DebugLoc dl = N->getDebugLoc();
378
379 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
380 N->getOperand(0), N->getOperand(1));
381 SDValue InChain = DAG.getEntryNode();
382 SDValue InGlue = DivRem;
383
384 // insert MFLO
385 if (N->hasAnyUseOfValue(0)) {
386 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, Mips::LO, MVT::i32,
387 InGlue);
388 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
389 InChain = CopyFromLo.getValue(1);
390 InGlue = CopyFromLo.getValue(2);
391 }
392
393 // insert MFHI
394 if (N->hasAnyUseOfValue(1)) {
395 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000396 Mips::HI, MVT::i32, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000397 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
398 }
399
400 return SDValue();
401}
402
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000403static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
404 switch (CC) {
405 default: llvm_unreachable("Unknown fp condition code!");
406 case ISD::SETEQ:
407 case ISD::SETOEQ: return Mips::FCOND_OEQ;
408 case ISD::SETUNE: return Mips::FCOND_UNE;
409 case ISD::SETLT:
410 case ISD::SETOLT: return Mips::FCOND_OLT;
411 case ISD::SETGT:
412 case ISD::SETOGT: return Mips::FCOND_OGT;
413 case ISD::SETLE:
414 case ISD::SETOLE: return Mips::FCOND_OLE;
415 case ISD::SETGE:
416 case ISD::SETOGE: return Mips::FCOND_OGE;
417 case ISD::SETULT: return Mips::FCOND_ULT;
418 case ISD::SETULE: return Mips::FCOND_ULE;
419 case ISD::SETUGT: return Mips::FCOND_UGT;
420 case ISD::SETUGE: return Mips::FCOND_UGE;
421 case ISD::SETUO: return Mips::FCOND_UN;
422 case ISD::SETO: return Mips::FCOND_OR;
423 case ISD::SETNE:
424 case ISD::SETONE: return Mips::FCOND_ONE;
425 case ISD::SETUEQ: return Mips::FCOND_UEQ;
426 }
427}
428
429
430// Returns true if condition code has to be inverted.
431static bool InvertFPCondCode(Mips::CondCode CC) {
432 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
433 return false;
434
435 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
436 return true;
437
438 assert(false && "Illegal Condition Code");
439 return false;
440}
441
442// Creates and returns an FPCmp node from a setcc node.
443// Returns Op if setcc is not a floating point comparison.
444static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
445 // must be a SETCC node
446 if (Op.getOpcode() != ISD::SETCC)
447 return Op;
448
449 SDValue LHS = Op.getOperand(0);
450
451 if (!LHS.getValueType().isFloatingPoint())
452 return Op;
453
454 SDValue RHS = Op.getOperand(1);
455 DebugLoc dl = Op.getDebugLoc();
456
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000457 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
458 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000459 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
460
461 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
462 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
463}
464
465// Creates and returns a CMovFPT/F node.
466static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
467 SDValue False, DebugLoc DL) {
468 bool invert = InvertFPCondCode((Mips::CondCode)
469 cast<ConstantSDNode>(Cond.getOperand(2))
470 ->getSExtValue());
471
472 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
473 True.getValueType(), True, False, Cond);
474}
475
476static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
477 TargetLowering::DAGCombinerInfo &DCI,
478 const MipsSubtarget* Subtarget) {
479 if (DCI.isBeforeLegalizeOps())
480 return SDValue();
481
482 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
483
484 if (Cond.getOpcode() != MipsISD::FPCmp)
485 return SDValue();
486
487 SDValue True = DAG.getConstant(1, MVT::i32);
488 SDValue False = DAG.getConstant(0, MVT::i32);
489
490 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
491}
492
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000493SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000494 const {
495 SelectionDAG &DAG = DCI.DAG;
496 unsigned opc = N->getOpcode();
497
498 switch (opc) {
499 default: break;
500 case ISD::ADDE:
501 return PerformADDECombine(N, DAG, DCI, Subtarget);
502 case ISD::SUBE:
503 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000504 case ISD::SDIVREM:
505 case ISD::UDIVREM:
506 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000507 case ISD::SETCC:
508 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000509 }
510
511 return SDValue();
512}
513
Dan Gohman475871a2008-07-27 21:46:04 +0000514SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000515LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000516{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000517 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000518 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000519 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000520 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
521 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000522 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000523 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000524 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
525 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000526 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000527 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000528 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000529 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000530 }
Dan Gohman475871a2008-07-27 21:46:04 +0000531 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000532}
533
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000534//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000535// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000536//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000537
538// AddLiveIn - This helper function adds the specified physical register to the
539// MachineFunction as a live in value. It also creates a corresponding
540// virtual register for it.
541static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000542AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000543{
544 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000545 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
546 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000547 return VReg;
548}
549
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000550// Get fp branch code (not opcode) from condition code.
551static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
552 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
553 return Mips::BRANCH_T;
554
555 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
556 return Mips::BRANCH_F;
557
558 return Mips::BRANCH_INVALID;
559}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000560
Akira Hatanaka14487d42011-06-07 19:28:39 +0000561static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
562 DebugLoc dl,
563 const MipsSubtarget* Subtarget,
564 const TargetInstrInfo *TII,
565 bool isFPCmp, unsigned Opc) {
566 // There is no need to expand CMov instructions if target has
567 // conditional moves.
568 if (Subtarget->hasCondMov())
569 return BB;
570
571 // To "insert" a SELECT_CC instruction, we actually have to insert the
572 // diamond control-flow pattern. The incoming instruction knows the
573 // destination vreg to set, the condition code register to branch on, the
574 // true/false values to select between, and a branch opcode to use.
575 const BasicBlock *LLVM_BB = BB->getBasicBlock();
576 MachineFunction::iterator It = BB;
577 ++It;
578
579 // thisMBB:
580 // ...
581 // TrueVal = ...
582 // setcc r1, r2, r3
583 // bNE r1, r0, copy1MBB
584 // fallthrough --> copy0MBB
585 MachineBasicBlock *thisMBB = BB;
586 MachineFunction *F = BB->getParent();
587 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
588 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
589 F->insert(It, copy0MBB);
590 F->insert(It, sinkMBB);
591
592 // Transfer the remainder of BB and its successor edges to sinkMBB.
593 sinkMBB->splice(sinkMBB->begin(), BB,
594 llvm::next(MachineBasicBlock::iterator(MI)),
595 BB->end());
596 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
597
598 // Next, add the true and fallthrough blocks as its successors.
599 BB->addSuccessor(copy0MBB);
600 BB->addSuccessor(sinkMBB);
601
602 // Emit the right instruction according to the type of the operands compared
603 if (isFPCmp)
604 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
605 else
606 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
607 .addReg(Mips::ZERO).addMBB(sinkMBB);
608
609 // copy0MBB:
610 // %FalseValue = ...
611 // # fallthrough to sinkMBB
612 BB = copy0MBB;
613
614 // Update machine-CFG edges
615 BB->addSuccessor(sinkMBB);
616
617 // sinkMBB:
618 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
619 // ...
620 BB = sinkMBB;
621
622 if (isFPCmp)
623 BuildMI(*BB, BB->begin(), dl,
624 TII->get(Mips::PHI), MI->getOperand(0).getReg())
625 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
626 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
627 else
628 BuildMI(*BB, BB->begin(), dl,
629 TII->get(Mips::PHI), MI->getOperand(0).getReg())
630 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
631 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
632
633 MI->eraseFromParent(); // The pseudo instruction is gone now.
634 return BB;
635}
636
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000637MachineBasicBlock *
638MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000639 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000640 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen94817572009-02-13 02:34:39 +0000641 DebugLoc dl = MI->getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000642
643 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000644 default:
645 assert(false && "Unexpected instr type to insert");
646 return NULL;
647 case Mips::MOVT:
648 case Mips::MOVT_S:
649 case Mips::MOVT_D:
650 return ExpandCondMov(MI, BB, dl, Subtarget, TII, true, Mips::BC1F);
651 case Mips::MOVF:
652 case Mips::MOVF_S:
653 case Mips::MOVF_D:
654 return ExpandCondMov(MI, BB, dl, Subtarget, TII, true, Mips::BC1T);
655 case Mips::MOVZ_I:
656 case Mips::MOVZ_S:
657 case Mips::MOVZ_D:
658 return ExpandCondMov(MI, BB, dl, Subtarget, TII, false, Mips::BNE);
659 case Mips::MOVN_I:
660 case Mips::MOVN_S:
661 case Mips::MOVN_D:
662 return ExpandCondMov(MI, BB, dl, Subtarget, TII, false, Mips::BEQ);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000663
664 case Mips::ATOMIC_LOAD_ADD_I8:
665 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
666 case Mips::ATOMIC_LOAD_ADD_I16:
667 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
668 case Mips::ATOMIC_LOAD_ADD_I32:
669 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
670
671 case Mips::ATOMIC_LOAD_AND_I8:
672 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
673 case Mips::ATOMIC_LOAD_AND_I16:
674 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
675 case Mips::ATOMIC_LOAD_AND_I32:
676 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
677
678 case Mips::ATOMIC_LOAD_OR_I8:
679 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
680 case Mips::ATOMIC_LOAD_OR_I16:
681 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
682 case Mips::ATOMIC_LOAD_OR_I32:
683 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
684
685 case Mips::ATOMIC_LOAD_XOR_I8:
686 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
687 case Mips::ATOMIC_LOAD_XOR_I16:
688 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
689 case Mips::ATOMIC_LOAD_XOR_I32:
690 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
691
692 case Mips::ATOMIC_LOAD_NAND_I8:
693 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
694 case Mips::ATOMIC_LOAD_NAND_I16:
695 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
696 case Mips::ATOMIC_LOAD_NAND_I32:
697 return EmitAtomicBinary(MI, BB, 4, 0, true);
698
699 case Mips::ATOMIC_LOAD_SUB_I8:
700 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
701 case Mips::ATOMIC_LOAD_SUB_I16:
702 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
703 case Mips::ATOMIC_LOAD_SUB_I32:
704 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
705
706 case Mips::ATOMIC_SWAP_I8:
707 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
708 case Mips::ATOMIC_SWAP_I16:
709 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
710 case Mips::ATOMIC_SWAP_I32:
711 return EmitAtomicBinary(MI, BB, 4, 0);
712
713 case Mips::ATOMIC_CMP_SWAP_I8:
714 return EmitAtomicCmpSwapPartword(MI, BB, 1);
715 case Mips::ATOMIC_CMP_SWAP_I16:
716 return EmitAtomicCmpSwapPartword(MI, BB, 2);
717 case Mips::ATOMIC_CMP_SWAP_I32:
718 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000719 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000720}
721
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000722// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
723// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
724MachineBasicBlock *
725MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000726 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000727 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000728 assert(Size == 4 && "Unsupported size for EmitAtomicBinary.");
729
730 MachineFunction *MF = BB->getParent();
731 MachineRegisterInfo &RegInfo = MF->getRegInfo();
732 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
733 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
734 DebugLoc dl = MI->getDebugLoc();
735
Akira Hatanaka4061da12011-07-19 20:11:17 +0000736 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000737 unsigned Ptr = MI->getOperand(1).getReg();
738 unsigned Incr = MI->getOperand(2).getReg();
739
Akira Hatanaka4061da12011-07-19 20:11:17 +0000740 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
741 unsigned AndRes = RegInfo.createVirtualRegister(RC);
742 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000743
744 // insert new blocks after the current block
745 const BasicBlock *LLVM_BB = BB->getBasicBlock();
746 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
747 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
748 MachineFunction::iterator It = BB;
749 ++It;
750 MF->insert(It, loopMBB);
751 MF->insert(It, exitMBB);
752
753 // Transfer the remainder of BB and its successor edges to exitMBB.
754 exitMBB->splice(exitMBB->begin(), BB,
755 llvm::next(MachineBasicBlock::iterator(MI)),
756 BB->end());
757 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
758
759 // thisMBB:
760 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000761 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000762 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000763 loopMBB->addSuccessor(loopMBB);
764 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000765
766 // loopMBB:
767 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000768 // <binop> storeval, oldval, incr
769 // sc success, storeval, 0(ptr)
770 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000771 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000772 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000773 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000774 // and andres, oldval, incr
775 // nor storeval, $0, andres
776 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr);
777 BuildMI(BB, dl, TII->get(Mips::NOR), StoreVal)
778 .addReg(Mips::ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000779 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000780 // <binop> storeval, oldval, incr
781 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000782 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000783 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000784 }
Akira Hatanaka4061da12011-07-19 20:11:17 +0000785 BuildMI(BB, dl, TII->get(Mips::SC), Success)
786 .addReg(StoreVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000787 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +0000788 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000789
790 MI->eraseFromParent(); // The instruction is gone now.
791
Akira Hatanaka939ece12011-07-19 03:42:13 +0000792 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000793}
794
795MachineBasicBlock *
796MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000797 MachineBasicBlock *BB,
798 unsigned Size, unsigned BinOpcode,
799 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000800 assert((Size == 1 || Size == 2) &&
801 "Unsupported size for EmitAtomicBinaryPartial.");
802
803 MachineFunction *MF = BB->getParent();
804 MachineRegisterInfo &RegInfo = MF->getRegInfo();
805 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
806 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
807 DebugLoc dl = MI->getDebugLoc();
808
809 unsigned Dest = MI->getOperand(0).getReg();
810 unsigned Ptr = MI->getOperand(1).getReg();
811 unsigned Incr = MI->getOperand(2).getReg();
812
Akira Hatanaka4061da12011-07-19 20:11:17 +0000813 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
814 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000815 unsigned Mask = RegInfo.createVirtualRegister(RC);
816 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000817 unsigned NewVal = RegInfo.createVirtualRegister(RC);
818 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000819 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000820 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
821 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
822 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
823 unsigned AndRes = RegInfo.createVirtualRegister(RC);
824 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000825 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000826 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
827 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
828 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
829 unsigned SllRes = RegInfo.createVirtualRegister(RC);
830 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000831
832 // insert new blocks after the current block
833 const BasicBlock *LLVM_BB = BB->getBasicBlock();
834 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000835 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000836 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
837 MachineFunction::iterator It = BB;
838 ++It;
839 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000840 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000841 MF->insert(It, exitMBB);
842
843 // Transfer the remainder of BB and its successor edges to exitMBB.
844 exitMBB->splice(exitMBB->begin(), BB,
845 llvm::next(MachineBasicBlock::iterator(MI)),
846 BB->end());
847 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
848
Akira Hatanaka81b44112011-07-19 17:09:53 +0000849 BB->addSuccessor(loopMBB);
850 loopMBB->addSuccessor(loopMBB);
851 loopMBB->addSuccessor(sinkMBB);
852 sinkMBB->addSuccessor(exitMBB);
853
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000854 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000855 // addiu masklsb2,$0,-4 # 0xfffffffc
856 // and alignedaddr,ptr,masklsb2
857 // andi ptrlsb2,ptr,3
858 // sll shiftamt,ptrlsb2,3
859 // ori maskupper,$0,255 # 0xff
860 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000861 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +0000862 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000863
864 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000865 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
866 .addReg(Mips::ZERO).addImm(-4);
867 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
868 .addReg(Ptr).addReg(MaskLSB2);
869 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
870 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
871 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
872 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +0000873 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
874 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000875 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +0000876 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +0000877
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000878
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +0000879 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000880 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000881 // ll oldval,0(alignedaddr)
882 // binop binopres,oldval,incr2
883 // and newval,binopres,mask
884 // and maskedoldval0,oldval,mask2
885 // or storeval,maskedoldval0,newval
886 // sc success,storeval,0(alignedaddr)
887 // beq success,$0,loopMBB
888
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +0000889 // atomic.swap
890 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000891 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +0000892 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +0000893 // and maskedoldval0,oldval,mask2
894 // or storeval,maskedoldval0,newval
895 // sc success,storeval,0(alignedaddr)
896 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +0000897
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000898 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000899 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000900 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000901 // and andres, oldval, incr2
902 // nor binopres, $0, andres
903 // and newval, binopres, mask
904 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
905 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
906 .addReg(Mips::ZERO).addReg(AndRes);
907 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000908 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000909 // <binop> binopres, oldval, incr2
910 // and newval, binopres, mask
911 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
912 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +0000913 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +0000914 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +0000915 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +0000916 }
917
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000918 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000919 .addReg(OldVal).addReg(Mask2);
920 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000921 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000922 BuildMI(BB, dl, TII->get(Mips::SC), Success)
923 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000924 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +0000925 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000926
Akira Hatanaka939ece12011-07-19 03:42:13 +0000927 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000928 // and maskedoldval1,oldval,mask
929 // srl srlres,maskedoldval1,shiftamt
930 // sll sllres,srlres,24
931 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +0000932 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000933 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +0000934
Akira Hatanaka4061da12011-07-19 20:11:17 +0000935 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
936 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +0000937 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
938 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000939 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
940 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000941 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000942 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000943
944 MI->eraseFromParent(); // The instruction is gone now.
945
Akira Hatanaka939ece12011-07-19 03:42:13 +0000946 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000947}
948
949MachineBasicBlock *
950MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000951 MachineBasicBlock *BB,
952 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000953 assert(Size == 4 && "Unsupported size for EmitAtomicCmpSwap.");
954
955 MachineFunction *MF = BB->getParent();
956 MachineRegisterInfo &RegInfo = MF->getRegInfo();
957 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
958 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
959 DebugLoc dl = MI->getDebugLoc();
960
961 unsigned Dest = MI->getOperand(0).getReg();
962 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +0000963 unsigned OldVal = MI->getOperand(2).getReg();
964 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000965
Akira Hatanaka4061da12011-07-19 20:11:17 +0000966 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000967
968 // insert new blocks after the current block
969 const BasicBlock *LLVM_BB = BB->getBasicBlock();
970 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
971 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
972 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
973 MachineFunction::iterator It = BB;
974 ++It;
975 MF->insert(It, loop1MBB);
976 MF->insert(It, loop2MBB);
977 MF->insert(It, exitMBB);
978
979 // Transfer the remainder of BB and its successor edges to exitMBB.
980 exitMBB->splice(exitMBB->begin(), BB,
981 llvm::next(MachineBasicBlock::iterator(MI)),
982 BB->end());
983 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
984
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000985 // thisMBB:
986 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000987 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000988 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000989 loop1MBB->addSuccessor(exitMBB);
990 loop1MBB->addSuccessor(loop2MBB);
991 loop2MBB->addSuccessor(loop1MBB);
992 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000993
994 // loop1MBB:
995 // ll dest, 0(ptr)
996 // bne dest, oldval, exitMBB
997 BB = loop1MBB;
Akira Hatanakad3ac47f2011-07-07 18:57:00 +0000998 BuildMI(BB, dl, TII->get(Mips::LL), Dest).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000999 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001000 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001001
1002 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001003 // sc success, newval, 0(ptr)
1004 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001005 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001006 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1007 .addReg(NewVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001008 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001009 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001010
1011 MI->eraseFromParent(); // The instruction is gone now.
1012
Akira Hatanaka939ece12011-07-19 03:42:13 +00001013 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001014}
1015
1016MachineBasicBlock *
1017MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001018 MachineBasicBlock *BB,
1019 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001020 assert((Size == 1 || Size == 2) &&
1021 "Unsupported size for EmitAtomicCmpSwapPartial.");
1022
1023 MachineFunction *MF = BB->getParent();
1024 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1025 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1026 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1027 DebugLoc dl = MI->getDebugLoc();
1028
1029 unsigned Dest = MI->getOperand(0).getReg();
1030 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001031 unsigned CmpVal = MI->getOperand(2).getReg();
1032 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001033
Akira Hatanaka4061da12011-07-19 20:11:17 +00001034 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1035 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001036 unsigned Mask = RegInfo.createVirtualRegister(RC);
1037 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001038 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1039 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1040 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1041 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1042 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1043 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1044 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1045 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1046 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1047 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1048 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1049 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1050 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1051 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001052
1053 // insert new blocks after the current block
1054 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1055 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1056 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001057 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001058 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1059 MachineFunction::iterator It = BB;
1060 ++It;
1061 MF->insert(It, loop1MBB);
1062 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001063 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001064 MF->insert(It, exitMBB);
1065
1066 // Transfer the remainder of BB and its successor edges to exitMBB.
1067 exitMBB->splice(exitMBB->begin(), BB,
1068 llvm::next(MachineBasicBlock::iterator(MI)),
1069 BB->end());
1070 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1071
Akira Hatanaka81b44112011-07-19 17:09:53 +00001072 BB->addSuccessor(loop1MBB);
1073 loop1MBB->addSuccessor(sinkMBB);
1074 loop1MBB->addSuccessor(loop2MBB);
1075 loop2MBB->addSuccessor(loop1MBB);
1076 loop2MBB->addSuccessor(sinkMBB);
1077 sinkMBB->addSuccessor(exitMBB);
1078
Akira Hatanaka70564a92011-07-19 18:14:26 +00001079 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001080 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001081 // addiu masklsb2,$0,-4 # 0xfffffffc
1082 // and alignedaddr,ptr,masklsb2
1083 // andi ptrlsb2,ptr,3
1084 // sll shiftamt,ptrlsb2,3
1085 // ori maskupper,$0,255 # 0xff
1086 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001087 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001088 // andi maskedcmpval,cmpval,255
1089 // sll shiftedcmpval,maskedcmpval,shiftamt
1090 // andi maskednewval,newval,255
1091 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001092 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001093 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1094 .addReg(Mips::ZERO).addImm(-4);
1095 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1096 .addReg(Ptr).addReg(MaskLSB2);
1097 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1098 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1099 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1100 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001101 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1102 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001103 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001104 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1105 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001106 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1107 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001108 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1109 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001110 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1111 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001112
1113 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001114 // ll oldval,0(alginedaddr)
1115 // and maskedoldval0,oldval,mask
1116 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001117 BB = loop1MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001118 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
1119 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1120 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001121 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001122 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001123
1124 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001125 // and maskedoldval1,oldval,mask2
1126 // or storeval,maskedoldval1,shiftednewval
1127 // sc success,storeval,0(alignedaddr)
1128 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001129 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001130 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1131 .addReg(OldVal).addReg(Mask2);
1132 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1133 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1134 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1135 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001136 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001137 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001138
Akira Hatanaka939ece12011-07-19 03:42:13 +00001139 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001140 // srl srlres,maskedoldval0,shiftamt
1141 // sll sllres,srlres,24
1142 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001143 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001144 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001145
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001146 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1147 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001148 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1149 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001150 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001151 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001152
1153 MI->eraseFromParent(); // The instruction is gone now.
1154
Akira Hatanaka939ece12011-07-19 03:42:13 +00001155 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001156}
1157
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001158//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001159// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001160//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001161SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001162LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001163{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001164 MachineFunction &MF = DAG.getMachineFunction();
1165 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1166
1167 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001168 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1169 "Cannot lower if the alignment of the allocated space is larger than \
1170 that of the stack.");
1171
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001172 SDValue Chain = Op.getOperand(0);
1173 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001174 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001175
1176 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +00001177 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001178
1179 // Subtract the dynamic size from the actual stack size to
1180 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +00001181 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001182
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001183 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001184 // must be placed in the stack pointer register.
Akira Hatanaka053546c2011-05-25 02:20:00 +00001185 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
1186 SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001187
1188 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001189 // value and a chain
Akira Hatanaka21afc632011-06-21 00:40:49 +00001190 SDVTList VTLs = DAG.getVTList(MVT::i32, MVT::Other);
1191 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1192 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1193
1194 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001195}
1196
1197SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001198LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001199{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001200 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001201 // the block to branch to if the condition is true.
1202 SDValue Chain = Op.getOperand(0);
1203 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001204 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001205
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001206 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1207
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001208 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001209 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001210 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001211
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001212 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001213 Mips::CondCode CC =
1214 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001215 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001216
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001217 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001218 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001219}
1220
1221SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001222LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001223{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001224 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001225
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001226 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001227 if (Cond.getOpcode() != MipsISD::FPCmp)
1228 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001229
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001230 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1231 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001232}
1233
Dan Gohmand858e902010-04-17 15:26:15 +00001234SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1235 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001236 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001237 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001238 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001239
Eli Friedmane2c74082009-08-03 02:22:28 +00001240 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001241 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001242
Chris Lattnerb71b9092009-08-13 06:28:06 +00001243 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001244
Chris Lattnere3736f82009-08-13 05:41:27 +00001245 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001246 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1247 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001248 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001249 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1250 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001251 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001252 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001253 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001254 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1255 MipsII::MO_ABS_HI);
1256 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1257 MipsII::MO_ABS_LO);
1258 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1259 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001260 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001261 }
1262
Akira Hatanaka0f843822011-06-07 18:58:42 +00001263 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1264 MipsII::MO_GOT);
1265 GA = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, GA);
1266 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
1267 DAG.getEntryNode(), GA, MachinePointerInfo(),
1268 false, false, 0);
1269 // On functions and global targets not internal linked only
1270 // a load from got/GP is necessary for PIC to work.
1271 if (!GV->hasInternalLinkage() &&
1272 (!GV->hasLocalLinkage() || isa<Function>(GV)))
1273 return ResNode;
1274 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1275 MipsII::MO_ABS_LO);
1276 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
1277 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001278}
1279
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001280SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1281 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001282 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1283 // FIXME there isn't actually debug info here
1284 DebugLoc dl = Op.getDebugLoc();
1285
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001286 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001287 // %hi/%lo relocation
1288 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1289 MipsII::MO_ABS_HI);
1290 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1291 MipsII::MO_ABS_LO);
1292 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1293 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1294 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001295 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001296
1297 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1298 MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001299 BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, BAGOTOffset);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001300 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1301 MipsII::MO_ABS_LO);
1302 SDValue Load = DAG.getLoad(MVT::i32, dl,
1303 DAG.getEntryNode(), BAGOTOffset,
1304 MachinePointerInfo(), false, false, 0);
1305 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
1306 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001307}
1308
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001309SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001310LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001311{
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001312 // If the relocation model is PIC, use the General Dynamic TLS Model,
1313 // otherwise use the Initial Exec or Local Exec TLS Model.
1314 // TODO: implement Local Dynamic TLS model
1315
1316 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1317 DebugLoc dl = GA->getDebugLoc();
1318 const GlobalValue *GV = GA->getGlobal();
1319 EVT PtrVT = getPointerTy();
1320
1321 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1322 // General Dynamic TLS Model
1323 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001324 0, MipsII::MO_TLSGD);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001325 SDValue Tlsgd = DAG.getNode(MipsISD::TlsGd, dl, MVT::i32, TGA);
1326 SDValue GP = DAG.getRegister(Mips::GP, MVT::i32);
1327 SDValue Argument = DAG.getNode(ISD::ADD, dl, MVT::i32, GP, Tlsgd);
1328
1329 ArgListTy Args;
1330 ArgListEntry Entry;
1331 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001332 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001333 Args.push_back(Entry);
1334 std::pair<SDValue, SDValue> CallResult =
1335 LowerCallTo(DAG.getEntryNode(),
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001336 (Type *) Type::getInt32Ty(*DAG.getContext()),
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001337 false, false, false, false, 0, CallingConv::C, false, true,
1338 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG,
1339 dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001340
1341 return CallResult.first;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001342 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001343
1344 SDValue Offset;
1345 if (GV->isDeclaration()) {
1346 // Initial Exec TLS Model
1347 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1348 MipsII::MO_GOTTPREL);
1349 Offset = DAG.getLoad(MVT::i32, dl,
1350 DAG.getEntryNode(), TGA, MachinePointerInfo(),
1351 false, false, 0);
1352 } else {
1353 // Local Exec TLS Model
1354 SDVTList VTs = DAG.getVTList(MVT::i32);
1355 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1356 MipsII::MO_TPREL_HI);
1357 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1358 MipsII::MO_TPREL_LO);
1359 SDValue Hi = DAG.getNode(MipsISD::TprelHi, dl, VTs, &TGAHi, 1);
1360 SDValue Lo = DAG.getNode(MipsISD::TprelLo, dl, MVT::i32, TGALo);
1361 Offset = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1362 }
1363
1364 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1365 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001366}
1367
1368SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001369LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001370{
Dan Gohman475871a2008-07-27 21:46:04 +00001371 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001372 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +00001373 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001374 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001375 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001376 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001377
Owen Andersone50ed302009-08-10 22:56:29 +00001378 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001379 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001380
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001381 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
1382
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +00001383 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001384 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001385 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001386 } else {// Emit Load from Global Pointer
1387 JTI = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, JTI);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001388 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
1389 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001390 false, false, 0);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001391 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001392
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001393 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1394 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001395 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001396 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001397
1398 return ResNode;
1399}
1400
Dan Gohman475871a2008-07-27 21:46:04 +00001401SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001402LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001403{
Dan Gohman475871a2008-07-27 21:46:04 +00001404 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001405 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001406 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001407 // FIXME there isn't actually debug info here
1408 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001409
1410 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001411 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001412 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001413 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001414 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001415 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001416 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1417 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001418 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001419
1420 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001421 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001422 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001423 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001424 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001425 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1426 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001427 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001428 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001429 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001430 N->getOffset(), MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001431 CP = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001432 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001433 CP, MachinePointerInfo::getConstantPool(),
1434 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001435 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001436 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001437 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001438 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
1439 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001440
1441 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001442}
1443
Dan Gohmand858e902010-04-17 15:26:15 +00001444SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001445 MachineFunction &MF = DAG.getMachineFunction();
1446 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1447
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001448 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001449 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1450 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001451
1452 // vastart just stores the address of the VarArgsFrameIndex slot into the
1453 // memory location argument.
1454 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001455 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1456 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +00001457 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001458}
1459
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001460static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
1461 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
1462 DebugLoc dl = Op.getDebugLoc();
1463 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
1464 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
1465 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
1466 DAG.getConstant(0x7fffffff, MVT::i32));
1467 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
1468 DAG.getConstant(0x80000000, MVT::i32));
1469 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1470 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
1471}
1472
1473static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001474 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001475 // Use ext/ins instructions if target architecture is Mips32r2.
1476 // Eliminate redundant mfc1 and mtc1 instructions.
1477 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001478
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001479 if (!isLittle)
1480 std::swap(LoIdx, HiIdx);
1481
1482 DebugLoc dl = Op.getDebugLoc();
1483 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1484 Op.getOperand(0),
1485 DAG.getConstant(LoIdx, MVT::i32));
1486 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1487 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1488 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1489 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1490 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1491 DAG.getConstant(0x7fffffff, MVT::i32));
1492 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1493 DAG.getConstant(0x80000000, MVT::i32));
1494 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1495
1496 if (!isLittle)
1497 std::swap(Word0, Word1);
1498
1499 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1500}
1501
1502SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1503 const {
1504 EVT Ty = Op.getValueType();
1505
1506 assert(Ty == MVT::f32 || Ty == MVT::f64);
1507
1508 if (Ty == MVT::f32)
1509 return LowerFCOPYSIGN32(Op, DAG);
1510 else
1511 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
1512}
1513
Akira Hatanaka2e591472011-06-02 00:24:44 +00001514SDValue MipsTargetLowering::
1515LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001516 // check the depth
1517 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001518 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001519
1520 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1521 MFI->setFrameAddressIsTaken(true);
1522 EVT VT = Op.getValueType();
1523 DebugLoc dl = Op.getDebugLoc();
1524 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Mips::FP, VT);
1525 return FrameAddr;
1526}
1527
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001528//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001529// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001530//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001531
1532#include "MipsGenCallingConv.inc"
1533
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001534//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001535// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001536// Mips O32 ABI rules:
1537// ---
1538// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001539// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001540// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001541// f64 - Only passed in two aliased f32 registers if no int reg has been used
1542// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001543// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1544// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001545//
1546// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001547//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001548
Duncan Sands1e96bab2010-11-04 10:49:57 +00001549static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001550 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001551 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1552
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001553 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001554
1555 static const unsigned IntRegs[] = {
1556 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1557 };
1558 static const unsigned F32Regs[] = {
1559 Mips::F12, Mips::F14
1560 };
1561 static const unsigned F64Regs[] = {
1562 Mips::D6, Mips::D7
1563 };
1564
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001565 // ByVal Args
1566 if (ArgFlags.isByVal()) {
1567 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1568 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1569 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1570 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1571 r < std::min(IntRegsSize, NextReg); ++r)
1572 State.AllocateReg(IntRegs[r]);
1573 return false;
1574 }
1575
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001576 // Promote i8 and i16
1577 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1578 LocVT = MVT::i32;
1579 if (ArgFlags.isSExt())
1580 LocInfo = CCValAssign::SExt;
1581 else if (ArgFlags.isZExt())
1582 LocInfo = CCValAssign::ZExt;
1583 else
1584 LocInfo = CCValAssign::AExt;
1585 }
1586
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001587 unsigned Reg;
1588
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001589 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1590 // is true: function is vararg, argument is 3rd or higher, there is previous
1591 // argument which is not f32 or f64.
1592 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1593 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001594 unsigned OrigAlign = ArgFlags.getOrigAlign();
1595 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001596
1597 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001598 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001599 // If this is the first part of an i64 arg,
1600 // the allocated register must be either A0 or A2.
1601 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1602 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001603 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001604 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1605 // Allocate int register and shadow next int register. If first
1606 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001607 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1608 if (Reg == Mips::A1 || Reg == Mips::A3)
1609 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1610 State.AllocateReg(IntRegs, IntRegsSize);
1611 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001612 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1613 // we are guaranteed to find an available float register
1614 if (ValVT == MVT::f32) {
1615 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1616 // Shadow int register
1617 State.AllocateReg(IntRegs, IntRegsSize);
1618 } else {
1619 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1620 // Shadow int registers
1621 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1622 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1623 State.AllocateReg(IntRegs, IntRegsSize);
1624 State.AllocateReg(IntRegs, IntRegsSize);
1625 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001626 } else
1627 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001628
Akira Hatanakad37776d2011-05-20 21:39:54 +00001629 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1630 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1631
1632 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001633 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001634 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001635 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001636
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001637 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001638}
1639
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001640//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001641// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001642//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001643
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001644static const unsigned O32IntRegsSize = 4;
1645
1646static const unsigned O32IntRegs[] = {
1647 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1648};
1649
1650// Write ByVal Arg to arg registers and stack.
1651static void
1652WriteByValArg(SDValue& Chain, DebugLoc dl,
1653 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1654 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1655 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001656 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
1657 MVT PtrType) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001658 unsigned FirstWord = VA.getLocMemOffset() / 4;
1659 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
1660 unsigned LastWord = FirstWord + NumWords;
1661 unsigned CurWord;
1662
1663 // copy the first 4 words of byval arg to registers A0 - A3
1664 for (CurWord = FirstWord; CurWord < std::min(LastWord, O32IntRegsSize);
1665 ++CurWord) {
1666 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1667 DAG.getConstant((CurWord - FirstWord) * 4,
1668 MVT::i32));
1669 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1670 MachinePointerInfo(),
1671 false, false, 0);
1672 MemOpChains.push_back(LoadVal.getValue(1));
1673 unsigned DstReg = O32IntRegs[CurWord];
1674 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1675 }
1676
1677 // copy remaining part of byval arg to stack.
1678 if (CurWord < LastWord) {
Eric Christopher471e4222011-06-08 23:55:35 +00001679 unsigned SizeInBytes = (LastWord - CurWord) * 4;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001680 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1681 DAG.getConstant((CurWord - FirstWord) * 4,
1682 MVT::i32));
1683 LastFI = MFI->CreateFixedObject(SizeInBytes, CurWord * 4, true);
1684 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
1685 Chain = DAG.getMemcpy(Chain, dl, Dst, Src,
1686 DAG.getConstant(SizeInBytes, MVT::i32),
1687 /*Align*/4,
1688 /*isVolatile=*/false, /*AlwaysInline=*/false,
1689 MachinePointerInfo(0), MachinePointerInfo(0));
1690 MemOpChains.push_back(Chain);
1691 }
1692}
1693
Dan Gohman98ca4f22009-08-05 01:29:28 +00001694/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001695/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001696/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001697SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001698MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001699 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001700 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001701 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001702 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001703 const SmallVectorImpl<ISD::InputArg> &Ins,
1704 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001705 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001706 // MIPs target does not yet support tail call optimization.
1707 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001708
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001709 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001710 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00001711 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001712 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001713 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001714
1715 // Analyze operands of the call, assigning locations to each operand.
1716 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001717 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1718 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001719
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001720 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001721 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001722 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001723 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001724
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001725 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001726 unsigned NextStackOffset = CCInfo.getNextStackOffset();
1727
1728 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NextStackOffset,
1729 true));
1730
1731 // If this is the first call, create a stack frame object that points to
1732 // a location to which .cprestore saves $gp.
1733 if (IsPIC && !MipsFI->getGPFI())
1734 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1735
Akira Hatanaka21afc632011-06-21 00:40:49 +00001736 // Get the frame index of the stack frame object that points to the location
1737 // of dynamically allocated area on the stack.
1738 int DynAllocFI = MipsFI->getDynAllocFI();
1739
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001740 // Update size of the maximum argument space.
1741 // For O32, a minimum of four words (16 bytes) of argument space is
1742 // allocated.
1743 if (Subtarget->isABI_O32())
1744 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
1745
1746 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1747
1748 if (MaxCallFrameSize < NextStackOffset) {
1749 MipsFI->setMaxCallFrameSize(NextStackOffset);
1750
Akira Hatanaka21afc632011-06-21 00:40:49 +00001751 // Set the offsets relative to $sp of the $gp restore slot and dynamically
1752 // allocated stack space. These offsets must be aligned to a boundary
1753 // determined by the stack alignment of the ABI.
1754 unsigned StackAlignment = TFL->getStackAlignment();
1755 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
1756 StackAlignment * StackAlignment;
1757
1758 if (IsPIC)
1759 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
1760
1761 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001762 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001763
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001764 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001765 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1766 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001767
Eric Christopher471e4222011-06-08 23:55:35 +00001768 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00001769
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001770 // Walk the register/memloc assignments, inserting copies/loads.
1771 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001772 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001773 CCValAssign &VA = ArgLocs[i];
1774
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001775 // Promote the value if needed.
1776 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001777 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001778 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001779 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001780 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001781 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001782 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001783 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1784 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001785 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1786 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001787 if (!Subtarget->isLittle())
1788 std::swap(Lo, Hi);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001789 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
1790 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
1791 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001792 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001793 }
1794 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00001795 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001796 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001797 break;
1798 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001799 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001800 break;
1801 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001802 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001803 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001804 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001805
1806 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001807 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001808 if (VA.isRegLoc()) {
1809 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00001810 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001811 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001812
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001813 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00001814 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001815
Eric Christopher471e4222011-06-08 23:55:35 +00001816 // ByVal Arg.
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001817 ISD::ArgFlagsTy Flags = Outs[i].Flags;
1818 if (Flags.isByVal()) {
1819 assert(Subtarget->isABI_O32() &&
1820 "No support for ByVal args by ABIs other than O32 yet.");
1821 assert(Flags.getByValSize() &&
1822 "ByVal args of size 0 should have been ignored by front-end.");
1823 WriteByValArg(Chain, dl, RegsToPass, MemOpChains, LastFI, MFI, DAG, Arg,
1824 VA, Flags, getPointerTy());
1825 continue;
1826 }
1827
Chris Lattnere0b12152008-03-17 06:57:02 +00001828 // Create the frame index object for this incoming parameter
Eric Christopher471e4222011-06-08 23:55:35 +00001829 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001830 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00001831 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00001832
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001833 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00001834 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00001835 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
1836 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001837 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001838 }
1839
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001840 // Extend range of indices of frame objects for outgoing arguments that were
1841 // created during this function call. Skip this step if no such objects were
1842 // created.
1843 if (LastFI)
1844 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
1845
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001846 // Transform all store nodes into one single node because all store
1847 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001848 if (!MemOpChains.empty())
1849 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001850 &MemOpChains[0], MemOpChains.size());
1851
Bill Wendling056292f2008-09-16 21:48:12 +00001852 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001853 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1854 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001855 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001856 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001857 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001858
1859 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001860 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
1861 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1862 getPointerTy(), 0,MipsII:: MO_GOT);
1863 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
1864 0, MipsII::MO_ABS_LO);
1865 } else {
1866 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1867 getPointerTy(), 0, OpFlag);
1868 }
1869
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001870 LoadSymAddr = true;
1871 }
1872 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001873 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001874 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001875 LoadSymAddr = true;
1876 }
1877
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00001878 SDValue InFlag;
1879
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001880 // Create nodes that load address of callee and copy it to T9
1881 if (IsPIC) {
1882 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001883 // Load callee address
Akira Hatanaka342837d2011-05-28 01:07:07 +00001884 Callee = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, Callee);
Akira Hatanaka25eba392011-06-24 19:01:25 +00001885 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), Callee,
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001886 MachinePointerInfo::getGOT(),
1887 false, false, 0);
1888
1889 // Use GOT+LO if callee has internal linkage.
1890 if (CalleeLo.getNode()) {
1891 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
1892 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
1893 } else
1894 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001895 }
1896
1897 // copy to T9
1898 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
1899 InFlag = Chain.getValue(1);
1900 Callee = DAG.getRegister(Mips::T9, MVT::i32);
1901 }
Bill Wendling056292f2008-09-16 21:48:12 +00001902
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00001903 // Build a sequence of copy-to-reg nodes chained together with token
1904 // chain and flag operands which copy the outgoing args into registers.
1905 // The InFlag in necessary since all emitted instructions must be
1906 // stuck together.
1907 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1908 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1909 RegsToPass[i].second, InFlag);
1910 InFlag = Chain.getValue(1);
1911 }
1912
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001913 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001914 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001915 //
1916 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001917 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00001918 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001919 Ops.push_back(Chain);
1920 Ops.push_back(Callee);
1921
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001922 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001923 // known live into the call.
1924 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1925 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1926 RegsToPass[i].second.getValueType()));
1927
Gabor Greifba36cb52008-08-28 21:40:38 +00001928 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001929 Ops.push_back(InFlag);
1930
Dale Johannesen33c960f2009-02-04 20:06:27 +00001931 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001932 InFlag = Chain.getValue(1);
1933
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00001934 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001935 Chain = DAG.getCALLSEQ_END(Chain,
1936 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00001937 DAG.getIntPtrConstant(0, true), InFlag);
1938 InFlag = Chain.getValue(1);
1939
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001940 // Handle result values, copying them out of physregs into vregs that we
1941 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001942 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
1943 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001944}
1945
Dan Gohman98ca4f22009-08-05 01:29:28 +00001946/// LowerCallResult - Lower the result values of a call into the
1947/// appropriate copies out of appropriate physical registers.
1948SDValue
1949MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001950 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001951 const SmallVectorImpl<ISD::InputArg> &Ins,
1952 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001953 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001954 // Assign locations to each value returned by this call.
1955 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001956 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1957 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001958
Dan Gohman98ca4f22009-08-05 01:29:28 +00001959 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001960
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001961 // Copy all of the result registers out of their specified physreg.
1962 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001963 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001964 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001965 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001966 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001967 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001968
Dan Gohman98ca4f22009-08-05 01:29:28 +00001969 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001970}
1971
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001972//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001973// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001974//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001975static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
1976 std::vector<SDValue>& OutChains,
1977 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
1978 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
1979 unsigned LocMem = VA.getLocMemOffset();
1980 unsigned FirstWord = LocMem / 4;
1981
1982 // copy register A0 - A3 to frame object
1983 for (unsigned i = 0; i < NumWords; ++i) {
1984 unsigned CurWord = FirstWord + i;
1985 if (CurWord >= O32IntRegsSize)
1986 break;
1987
1988 unsigned SrcReg = O32IntRegs[CurWord];
1989 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
1990 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
1991 DAG.getConstant(i * 4, MVT::i32));
1992 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
1993 StorePtr, MachinePointerInfo(), false,
1994 false, 0);
1995 OutChains.push_back(Store);
1996 }
1997}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001998
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001999/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002000/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002001SDValue
2002MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002003 CallingConv::ID CallConv,
2004 bool isVarArg,
2005 const SmallVectorImpl<ISD::InputArg>
2006 &Ins,
2007 DebugLoc dl, SelectionDAG &DAG,
2008 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002009 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002010 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002011 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002012 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002013
Dan Gohman1e93df62010-04-17 14:41:14 +00002014 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002015
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002016 // Used with vargs to acumulate store chains.
2017 std::vector<SDValue> OutChains;
2018
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002019 // Assign locations to all of the incoming arguments.
2020 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002021 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2022 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002023
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002024 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002025 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002026 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002027 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002028
Akira Hatanaka43299772011-05-20 23:22:14 +00002029 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002030
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002031 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002032 CCValAssign &VA = ArgLocs[i];
2033
2034 // Arguments stored on registers
2035 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002036 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002037 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002038 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002039
Owen Anderson825b72b2009-08-11 20:47:22 +00002040 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002041 RC = Mips::CPURegsRegisterClass;
2042 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002043 RC = Mips::FGR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002044 else if (RegVT == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002045 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002046 RC = Mips::AFGR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002047 } else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002048 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002049
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002050 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002051 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002052 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002053 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002054
2055 // If this is an 8 or 16-bit value, it has been passed promoted
2056 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002057 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002058 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002059 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002060 if (VA.getLocInfo() == CCValAssign::SExt)
2061 Opcode = ISD::AssertSext;
2062 else if (VA.getLocInfo() == CCValAssign::ZExt)
2063 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002064 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002065 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00002066 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00002067 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002068 }
2069
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002070 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002071 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002072 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
2073 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00002074 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002075 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002076 VA.getLocReg()+1, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002077 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002078 if (!Subtarget->isLittle())
2079 std::swap(ArgValue, ArgValue2);
2080 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2081 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002082 }
2083 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002084
Dan Gohman98ca4f22009-08-05 01:29:28 +00002085 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002086 } else { // VA.isRegLoc()
2087
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002088 // sanity check
2089 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002090
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002091 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2092
2093 if (Flags.isByVal()) {
2094 assert(Subtarget->isABI_O32() &&
2095 "No support for ByVal args by ABIs other than O32 yet.");
2096 assert(Flags.getByValSize() &&
2097 "ByVal args of size 0 should have been ignored by front-end.");
2098 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2099 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2100 true);
2101 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2102 InVals.push_back(FIN);
2103 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2104
2105 continue;
2106 }
2107
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002108 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002109 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
2110 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002111
2112 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002113 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002114 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002115 MachinePointerInfo::getFixedStack(LastFI),
David Greenef6fa1862010-02-15 16:56:10 +00002116 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002117 }
2118 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002119
2120 // The mips ABIs for returning structs by value requires that we copy
2121 // the sret argument into $v0 for the return. Save the argument into
2122 // a virtual register so that we can access it from the return points.
2123 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2124 unsigned Reg = MipsFI->getSRetReturnReg();
2125 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002126 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002127 MipsFI->setSRetReturnReg(Reg);
2128 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002129 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002130 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002131 }
2132
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002133 if (isVarArg && Subtarget->isABI_O32()) {
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002134 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002135 // which is a value necessary to VASTART.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002136 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002137 assert(NextStackOffset % 4 == 0 &&
2138 "NextStackOffset must be aligned to 4-byte boundaries.");
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002139 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
2140 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002141
2142 // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
2143 // copy the integer registers that have not been used for argument passing
2144 // to the caller's stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002145 for (; NextStackOffset < 16; NextStackOffset += 4) {
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002146 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002147 unsigned Idx = NextStackOffset / 4;
2148 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
2149 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Akira Hatanaka69c19f72011-05-23 20:16:59 +00002150 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002151 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2152 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2153 MachinePointerInfo(),
2154 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002155 }
2156 }
2157
Akira Hatanaka43299772011-05-20 23:22:14 +00002158 MipsFI->setLastInArgFI(LastFI);
2159
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002160 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002161 // the size of Ins and InVals. This only happens when on varg functions
2162 if (!OutChains.empty()) {
2163 OutChains.push_back(Chain);
2164 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2165 &OutChains[0], OutChains.size());
2166 }
2167
Dan Gohman98ca4f22009-08-05 01:29:28 +00002168 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002169}
2170
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002171//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002172// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002173//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002174
Dan Gohman98ca4f22009-08-05 01:29:28 +00002175SDValue
2176MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002177 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002178 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002179 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002180 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002181
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002182 // CCValAssign - represent the assignment of
2183 // the return value to a location
2184 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002185
2186 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002187 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2188 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002189
Dan Gohman98ca4f22009-08-05 01:29:28 +00002190 // Analize return values.
2191 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002192
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002193 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002194 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002195 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002196 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002197 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002198 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002199 }
2200
Dan Gohman475871a2008-07-27 21:46:04 +00002201 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002202
2203 // Copy the result values into the output registers.
2204 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2205 CCValAssign &VA = RVLocs[i];
2206 assert(VA.isRegLoc() && "Can only return in registers!");
2207
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002208 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002209 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002210
2211 // guarantee that all emitted copies are
2212 // stuck together, avoiding something bad
2213 Flag = Chain.getValue(1);
2214 }
2215
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002216 // The mips ABIs for returning structs by value requires that we copy
2217 // the sret argument into $v0 for the return. We saved the argument into
2218 // a virtual register in the entry block, so now we copy the value out
2219 // and into $v0.
2220 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2221 MachineFunction &MF = DAG.getMachineFunction();
2222 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2223 unsigned Reg = MipsFI->getSRetReturnReg();
2224
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002225 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002226 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002227 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002228
Dale Johannesena05dca42009-02-04 23:02:30 +00002229 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002230 Flag = Chain.getValue(1);
2231 }
2232
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002233 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002234 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002235 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002236 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002237 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002238 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002239 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002240}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002241
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002242//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002243// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002244//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002245
2246/// getConstraintType - Given a constraint letter, return the type of
2247/// constraint it is for this target.
2248MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002249getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002250{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002251 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002252 // GCC config/mips/constraints.md
2253 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002254 // 'd' : An address register. Equivalent to r
2255 // unless generating MIPS16 code.
2256 // 'y' : Equivalent to r; retained for
2257 // backwards compatibility.
2258 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002259 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002260 switch (Constraint[0]) {
2261 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002262 case 'd':
2263 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002264 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002265 return C_RegisterClass;
2266 break;
2267 }
2268 }
2269 return TargetLowering::getConstraintType(Constraint);
2270}
2271
John Thompson44ab89e2010-10-29 17:29:13 +00002272/// Examine constraint type and operand type and determine a weight value.
2273/// This object must already have been set up with the operand type
2274/// and the current alternative constraint selected.
2275TargetLowering::ConstraintWeight
2276MipsTargetLowering::getSingleConstraintMatchWeight(
2277 AsmOperandInfo &info, const char *constraint) const {
2278 ConstraintWeight weight = CW_Invalid;
2279 Value *CallOperandVal = info.CallOperandVal;
2280 // If we don't have a value, we can't do a match,
2281 // but allow it at the lowest weight.
2282 if (CallOperandVal == NULL)
2283 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002284 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002285 // Look at the constraint type.
2286 switch (*constraint) {
2287 default:
2288 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2289 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002290 case 'd':
2291 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002292 if (type->isIntegerTy())
2293 weight = CW_Register;
2294 break;
2295 case 'f':
2296 if (type->isFloatTy())
2297 weight = CW_Register;
2298 break;
2299 }
2300 return weight;
2301}
2302
Eric Christopher38d64262011-06-29 19:33:04 +00002303/// Given a register class constraint, like 'r', if this corresponds directly
2304/// to an LLVM register class, return a register of 0 and the register class
2305/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002306std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002307getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002308{
2309 if (Constraint.size() == 1) {
2310 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002311 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2312 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002313 case 'r':
2314 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002315 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002316 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002317 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002318 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002319 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2320 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002321 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002322 }
2323 }
2324 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2325}
2326
Dan Gohman6520e202008-10-18 02:06:02 +00002327bool
2328MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2329 // The Mips target isn't yet aware of offsets.
2330 return false;
2331}
Evan Chengeb2f9692009-10-27 19:56:55 +00002332
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002333bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2334 if (VT != MVT::f32 && VT != MVT::f64)
2335 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002336 if (Imm.isNegZero())
2337 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002338 return Imm.isZero();
2339}