blob: d2a5541bc9497d77faf7b5a9fe5d0e3b4ae70c65 [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
Akira Hatanakabb15e112011-08-17 02:05:42 +000038namespace {
39 // If I is a shifted mask, set the size (Size) and the first bit of the
40 // mask (Pos), and return true.
41 bool IsShiftedMask(uint64_t I, unsigned SizeInBits, uint64_t &Pos,
42 uint64_t &Size) {
43 assert(SizeInBits == 32 || SizeInBits == 64);
44 bool Is32Bits = (SizeInBits == 32);
45
46 if ((Is32Bits == 32 && !isShiftedMask_32(I)) ||
47 (!Is32Bits && !isShiftedMask_64(I)))
48 return false;
49
50 Size = Is32Bits ? CountPopulation_32(I) : CountPopulation_64(I);
51 Pos = Is32Bits ? CountTrailingZeros_32(I) : CountTrailingZeros_64(I);
52 return true;
53 }
54}
55
Chris Lattnerf0144122009-07-28 03:13:23 +000056const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
57 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000058 case MipsISD::JmpLink: return "MipsISD::JmpLink";
59 case MipsISD::Hi: return "MipsISD::Hi";
60 case MipsISD::Lo: return "MipsISD::Lo";
61 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +000062 case MipsISD::TlsGd: return "MipsISD::TlsGd";
63 case MipsISD::TprelHi: return "MipsISD::TprelHi";
64 case MipsISD::TprelLo: return "MipsISD::TprelLo";
65 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000066 case MipsISD::Ret: return "MipsISD::Ret";
67 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
68 case MipsISD::FPCmp: return "MipsISD::FPCmp";
69 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
70 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
71 case MipsISD::FPRound: return "MipsISD::FPRound";
72 case MipsISD::MAdd: return "MipsISD::MAdd";
73 case MipsISD::MAddu: return "MipsISD::MAddu";
74 case MipsISD::MSub: return "MipsISD::MSub";
75 case MipsISD::MSubu: return "MipsISD::MSubu";
76 case MipsISD::DivRem: return "MipsISD::DivRem";
77 case MipsISD::DivRemU: return "MipsISD::DivRemU";
78 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
79 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanaka342837d2011-05-28 01:07:07 +000080 case MipsISD::WrapperPIC: return "MipsISD::WrapperPIC";
Akira Hatanaka21afc632011-06-21 00:40:49 +000081 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
Akira Hatanakadb548262011-07-19 23:30:50 +000082 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanakabb15e112011-08-17 02:05:42 +000083 case MipsISD::Ext: return "MipsISD::Ext";
84 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanaka0f843822011-06-07 18:58:42 +000085 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000086 }
87}
88
89MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000090MipsTargetLowering(MipsTargetMachine &TM)
Chris Lattnerb71b9092009-08-13 06:28:06 +000091 : TargetLowering(TM, new MipsTargetObjectFile()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000092 Subtarget = &TM.getSubtarget<MipsSubtarget>();
93
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000094 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000095 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000096 setBooleanContents(ZeroOrOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000097
98 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000099 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
100 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000101
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000102 // When dealing with single precision only, use libcalls
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000103 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000104 if (!Subtarget->isFP64bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000105 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000106
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000107 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000108 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
109 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
110 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000111
Eli Friedman6055a6a2009-07-17 04:07:24 +0000112 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000113 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
114 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000115
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000116 // Used by legalize types to correctly generate the setcc result.
117 // Without this, every float setcc comes with a AND/OR with the result,
118 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000119 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000120 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000121
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000122 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +0000123 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000124 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
126 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
127 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
128 setOperationAction(ISD::SELECT, MVT::f32, Custom);
129 setOperationAction(ISD::SELECT, MVT::f64, Custom);
130 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000131 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
132 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000133 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Akira Hatanakabb15e112011-08-17 02:05:42 +0000134 setOperationAction(ISD::AND, MVT::i32, Custom);
135 setOperationAction(ISD::OR, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000136
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000137 setOperationAction(ISD::SDIV, MVT::i32, Expand);
138 setOperationAction(ISD::SREM, MVT::i32, Expand);
139 setOperationAction(ISD::UDIV, MVT::i32, Expand);
140 setOperationAction(ISD::UREM, MVT::i32, Expand);
141
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000142 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000143 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
144 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
145 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
146 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
147 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
148 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
149 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
150 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
151 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000152
153 if (!Subtarget->isMips32r2())
154 setOperationAction(ISD::ROTR, MVT::i32, Expand);
155
Owen Anderson825b72b2009-08-11 20:47:22 +0000156 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
157 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
158 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000159 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
160 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000161 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000162 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000164 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
166 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000167 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 setOperationAction(ISD::FLOG, MVT::f32, Expand);
169 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
170 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
171 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000172 setOperationAction(ISD::FMA, MVT::f32, Expand);
173 setOperationAction(ISD::FMA, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000174
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000175 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
176 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000177
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000178 setOperationAction(ISD::VAARG, MVT::Other, Expand);
179 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
180 setOperationAction(ISD::VAEND, MVT::Other, Expand);
181
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000182 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000183 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
184 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000185
Akira Hatanakadb548262011-07-19 23:30:50 +0000186 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000187 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000188
Eli Friedman26689ac2011-08-03 21:06:02 +0000189 setInsertFencesForAtomic(true);
190
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000191 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000193
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000194 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
196 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000197 }
198
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000199 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000200 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000201
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000202 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000204
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000205 setTargetDAGCombine(ISD::ADDE);
206 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000207 setTargetDAGCombine(ISD::SDIVREM);
208 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000209 setTargetDAGCombine(ISD::SETCC);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000210
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000211 setMinFunctionAlignment(2);
212
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000213 setStackPointerRegisterToSaveRestore(Mips::SP);
214 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000215
216 setExceptionPointerRegister(Mips::A0);
217 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000218}
219
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000220bool MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
221 // FIXME: allow unaligned memory accesses for other types too.
222 return VT.getSimpleVT().SimpleTy == MVT::i32;
223}
224
Owen Anderson825b72b2009-08-11 20:47:22 +0000225MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
226 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000227}
228
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000229// SelectMadd -
230// Transforms a subgraph in CurDAG if the following pattern is found:
231// (addc multLo, Lo0), (adde multHi, Hi0),
232// where,
233// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000234// Lo0: initial value of Lo register
235// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000236// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000237static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000238 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000239 // for the matching to be successful.
240 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
241
242 if (ADDCNode->getOpcode() != ISD::ADDC)
243 return false;
244
245 SDValue MultHi = ADDENode->getOperand(0);
246 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000247 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000248 unsigned MultOpc = MultHi.getOpcode();
249
250 // MultHi and MultLo must be generated by the same node,
251 if (MultLo.getNode() != MultNode)
252 return false;
253
254 // and it must be a multiplication.
255 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
256 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000257
258 // MultLo amd MultHi must be the first and second output of MultNode
259 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000260 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
261 return false;
262
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000263 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000264 // of the values of MultNode, in which case MultNode will be removed in later
265 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000266 // If there exist users other than ADDENode or ADDCNode, this function returns
267 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000268 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000269 // produced.
270 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
271 return false;
272
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000273 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000274 DebugLoc dl = ADDENode->getDebugLoc();
275
276 // create MipsMAdd(u) node
277 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000278
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000279 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
280 MVT::Glue,
281 MultNode->getOperand(0),// Factor 0
282 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000283 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000284 ADDENode->getOperand(1));// Hi0
285
286 // create CopyFromReg nodes
287 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
288 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000289 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000290 Mips::HI, MVT::i32,
291 CopyFromLo.getValue(2));
292
293 // replace uses of adde and addc here
294 if (!SDValue(ADDCNode, 0).use_empty())
295 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
296
297 if (!SDValue(ADDENode, 0).use_empty())
298 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
299
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000300 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000301}
302
303// SelectMsub -
304// Transforms a subgraph in CurDAG if the following pattern is found:
305// (addc Lo0, multLo), (sube Hi0, multHi),
306// where,
307// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000308// Lo0: initial value of Lo register
309// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000310// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000311static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000312 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000313 // for the matching to be successful.
314 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
315
316 if (SUBCNode->getOpcode() != ISD::SUBC)
317 return false;
318
319 SDValue MultHi = SUBENode->getOperand(1);
320 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000321 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000322 unsigned MultOpc = MultHi.getOpcode();
323
324 // MultHi and MultLo must be generated by the same node,
325 if (MultLo.getNode() != MultNode)
326 return false;
327
328 // and it must be a multiplication.
329 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
330 return false;
331
332 // MultLo amd MultHi must be the first and second output of MultNode
333 // respectively.
334 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
335 return false;
336
337 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
338 // of the values of MultNode, in which case MultNode will be removed in later
339 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000340 // If there exist users other than SUBENode or SUBCNode, this function returns
341 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000342 // instruction node rather than a pair of MULT and MSUB instructions being
343 // produced.
344 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
345 return false;
346
347 SDValue Chain = CurDAG->getEntryNode();
348 DebugLoc dl = SUBENode->getDebugLoc();
349
350 // create MipsSub(u) node
351 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
352
353 SDValue MSub = CurDAG->getNode(MultOpc, dl,
354 MVT::Glue,
355 MultNode->getOperand(0),// Factor 0
356 MultNode->getOperand(1),// Factor 1
357 SUBCNode->getOperand(0),// Lo0
358 SUBENode->getOperand(0));// Hi0
359
360 // create CopyFromReg nodes
361 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
362 MSub);
363 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
364 Mips::HI, MVT::i32,
365 CopyFromLo.getValue(2));
366
367 // replace uses of sube and subc here
368 if (!SDValue(SUBCNode, 0).use_empty())
369 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
370
371 if (!SDValue(SUBENode, 0).use_empty())
372 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
373
374 return true;
375}
376
377static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
378 TargetLowering::DAGCombinerInfo &DCI,
379 const MipsSubtarget* Subtarget) {
380 if (DCI.isBeforeLegalize())
381 return SDValue();
382
383 if (Subtarget->isMips32() && SelectMadd(N, &DAG))
384 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000385
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000386 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000387}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000388
389static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
390 TargetLowering::DAGCombinerInfo &DCI,
391 const MipsSubtarget* Subtarget) {
392 if (DCI.isBeforeLegalize())
393 return SDValue();
394
395 if (Subtarget->isMips32() && SelectMsub(N, &DAG))
396 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000397
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000398 return SDValue();
399}
400
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000401static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
402 TargetLowering::DAGCombinerInfo &DCI,
403 const MipsSubtarget* Subtarget) {
404 if (DCI.isBeforeLegalizeOps())
405 return SDValue();
406
407 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
408 MipsISD::DivRemU;
409 DebugLoc dl = N->getDebugLoc();
410
411 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
412 N->getOperand(0), N->getOperand(1));
413 SDValue InChain = DAG.getEntryNode();
414 SDValue InGlue = DivRem;
415
416 // insert MFLO
417 if (N->hasAnyUseOfValue(0)) {
418 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, Mips::LO, MVT::i32,
419 InGlue);
420 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
421 InChain = CopyFromLo.getValue(1);
422 InGlue = CopyFromLo.getValue(2);
423 }
424
425 // insert MFHI
426 if (N->hasAnyUseOfValue(1)) {
427 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000428 Mips::HI, MVT::i32, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000429 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
430 }
431
432 return SDValue();
433}
434
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000435static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
436 switch (CC) {
437 default: llvm_unreachable("Unknown fp condition code!");
438 case ISD::SETEQ:
439 case ISD::SETOEQ: return Mips::FCOND_OEQ;
440 case ISD::SETUNE: return Mips::FCOND_UNE;
441 case ISD::SETLT:
442 case ISD::SETOLT: return Mips::FCOND_OLT;
443 case ISD::SETGT:
444 case ISD::SETOGT: return Mips::FCOND_OGT;
445 case ISD::SETLE:
446 case ISD::SETOLE: return Mips::FCOND_OLE;
447 case ISD::SETGE:
448 case ISD::SETOGE: return Mips::FCOND_OGE;
449 case ISD::SETULT: return Mips::FCOND_ULT;
450 case ISD::SETULE: return Mips::FCOND_ULE;
451 case ISD::SETUGT: return Mips::FCOND_UGT;
452 case ISD::SETUGE: return Mips::FCOND_UGE;
453 case ISD::SETUO: return Mips::FCOND_UN;
454 case ISD::SETO: return Mips::FCOND_OR;
455 case ISD::SETNE:
456 case ISD::SETONE: return Mips::FCOND_ONE;
457 case ISD::SETUEQ: return Mips::FCOND_UEQ;
458 }
459}
460
461
462// Returns true if condition code has to be inverted.
463static bool InvertFPCondCode(Mips::CondCode CC) {
464 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
465 return false;
466
467 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
468 return true;
469
470 assert(false && "Illegal Condition Code");
471 return false;
472}
473
474// Creates and returns an FPCmp node from a setcc node.
475// Returns Op if setcc is not a floating point comparison.
476static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
477 // must be a SETCC node
478 if (Op.getOpcode() != ISD::SETCC)
479 return Op;
480
481 SDValue LHS = Op.getOperand(0);
482
483 if (!LHS.getValueType().isFloatingPoint())
484 return Op;
485
486 SDValue RHS = Op.getOperand(1);
487 DebugLoc dl = Op.getDebugLoc();
488
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000489 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
490 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000491 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
492
493 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
494 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
495}
496
497// Creates and returns a CMovFPT/F node.
498static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
499 SDValue False, DebugLoc DL) {
500 bool invert = InvertFPCondCode((Mips::CondCode)
501 cast<ConstantSDNode>(Cond.getOperand(2))
502 ->getSExtValue());
503
504 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
505 True.getValueType(), True, False, Cond);
506}
507
508static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
509 TargetLowering::DAGCombinerInfo &DCI,
510 const MipsSubtarget* Subtarget) {
511 if (DCI.isBeforeLegalizeOps())
512 return SDValue();
513
514 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
515
516 if (Cond.getOpcode() != MipsISD::FPCmp)
517 return SDValue();
518
519 SDValue True = DAG.getConstant(1, MVT::i32);
520 SDValue False = DAG.getConstant(0, MVT::i32);
521
522 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
523}
524
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000525SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000526 const {
527 SelectionDAG &DAG = DCI.DAG;
528 unsigned opc = N->getOpcode();
529
530 switch (opc) {
531 default: break;
532 case ISD::ADDE:
533 return PerformADDECombine(N, DAG, DCI, Subtarget);
534 case ISD::SUBE:
535 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000536 case ISD::SDIVREM:
537 case ISD::UDIVREM:
538 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000539 case ISD::SETCC:
540 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000541 }
542
543 return SDValue();
544}
545
Dan Gohman475871a2008-07-27 21:46:04 +0000546SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000547LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000548{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000549 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000550 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000551 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000552 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
553 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000554 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000555 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000556 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
557 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000558 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000559 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000560 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000561 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000562 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000563 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Akira Hatanakabb15e112011-08-17 02:05:42 +0000564 case ISD::AND: return LowerAND(Op, DAG);
565 case ISD::OR: return LowerOR(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000566 }
Dan Gohman475871a2008-07-27 21:46:04 +0000567 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000568}
569
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000570//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000571// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000572//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000573
574// AddLiveIn - This helper function adds the specified physical register to the
575// MachineFunction as a live in value. It also creates a corresponding
576// virtual register for it.
577static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000578AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000579{
580 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000581 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
582 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000583 return VReg;
584}
585
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000586// Get fp branch code (not opcode) from condition code.
587static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
588 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
589 return Mips::BRANCH_T;
590
591 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
592 return Mips::BRANCH_F;
593
594 return Mips::BRANCH_INVALID;
595}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000596
Akira Hatanaka14487d42011-06-07 19:28:39 +0000597static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
598 DebugLoc dl,
599 const MipsSubtarget* Subtarget,
600 const TargetInstrInfo *TII,
601 bool isFPCmp, unsigned Opc) {
602 // There is no need to expand CMov instructions if target has
603 // conditional moves.
604 if (Subtarget->hasCondMov())
605 return BB;
606
607 // To "insert" a SELECT_CC instruction, we actually have to insert the
608 // diamond control-flow pattern. The incoming instruction knows the
609 // destination vreg to set, the condition code register to branch on, the
610 // true/false values to select between, and a branch opcode to use.
611 const BasicBlock *LLVM_BB = BB->getBasicBlock();
612 MachineFunction::iterator It = BB;
613 ++It;
614
615 // thisMBB:
616 // ...
617 // TrueVal = ...
618 // setcc r1, r2, r3
619 // bNE r1, r0, copy1MBB
620 // fallthrough --> copy0MBB
621 MachineBasicBlock *thisMBB = BB;
622 MachineFunction *F = BB->getParent();
623 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
624 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
625 F->insert(It, copy0MBB);
626 F->insert(It, sinkMBB);
627
628 // Transfer the remainder of BB and its successor edges to sinkMBB.
629 sinkMBB->splice(sinkMBB->begin(), BB,
630 llvm::next(MachineBasicBlock::iterator(MI)),
631 BB->end());
632 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
633
634 // Next, add the true and fallthrough blocks as its successors.
635 BB->addSuccessor(copy0MBB);
636 BB->addSuccessor(sinkMBB);
637
638 // Emit the right instruction according to the type of the operands compared
639 if (isFPCmp)
640 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
641 else
642 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
643 .addReg(Mips::ZERO).addMBB(sinkMBB);
644
645 // copy0MBB:
646 // %FalseValue = ...
647 // # fallthrough to sinkMBB
648 BB = copy0MBB;
649
650 // Update machine-CFG edges
651 BB->addSuccessor(sinkMBB);
652
653 // sinkMBB:
654 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
655 // ...
656 BB = sinkMBB;
657
658 if (isFPCmp)
659 BuildMI(*BB, BB->begin(), dl,
660 TII->get(Mips::PHI), MI->getOperand(0).getReg())
661 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
662 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
663 else
664 BuildMI(*BB, BB->begin(), dl,
665 TII->get(Mips::PHI), MI->getOperand(0).getReg())
666 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
667 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
668
669 MI->eraseFromParent(); // The pseudo instruction is gone now.
670 return BB;
671}
672
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000673MachineBasicBlock *
674MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000675 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000676 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen94817572009-02-13 02:34:39 +0000677 DebugLoc dl = MI->getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000678
679 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000680 default:
681 assert(false && "Unexpected instr type to insert");
682 return NULL;
683 case Mips::MOVT:
684 case Mips::MOVT_S:
685 case Mips::MOVT_D:
686 return ExpandCondMov(MI, BB, dl, Subtarget, TII, true, Mips::BC1F);
687 case Mips::MOVF:
688 case Mips::MOVF_S:
689 case Mips::MOVF_D:
690 return ExpandCondMov(MI, BB, dl, Subtarget, TII, true, Mips::BC1T);
691 case Mips::MOVZ_I:
692 case Mips::MOVZ_S:
693 case Mips::MOVZ_D:
694 return ExpandCondMov(MI, BB, dl, Subtarget, TII, false, Mips::BNE);
695 case Mips::MOVN_I:
696 case Mips::MOVN_S:
697 case Mips::MOVN_D:
698 return ExpandCondMov(MI, BB, dl, Subtarget, TII, false, Mips::BEQ);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000699
700 case Mips::ATOMIC_LOAD_ADD_I8:
701 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
702 case Mips::ATOMIC_LOAD_ADD_I16:
703 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
704 case Mips::ATOMIC_LOAD_ADD_I32:
705 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
706
707 case Mips::ATOMIC_LOAD_AND_I8:
708 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
709 case Mips::ATOMIC_LOAD_AND_I16:
710 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
711 case Mips::ATOMIC_LOAD_AND_I32:
712 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
713
714 case Mips::ATOMIC_LOAD_OR_I8:
715 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
716 case Mips::ATOMIC_LOAD_OR_I16:
717 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
718 case Mips::ATOMIC_LOAD_OR_I32:
719 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
720
721 case Mips::ATOMIC_LOAD_XOR_I8:
722 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
723 case Mips::ATOMIC_LOAD_XOR_I16:
724 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
725 case Mips::ATOMIC_LOAD_XOR_I32:
726 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
727
728 case Mips::ATOMIC_LOAD_NAND_I8:
729 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
730 case Mips::ATOMIC_LOAD_NAND_I16:
731 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
732 case Mips::ATOMIC_LOAD_NAND_I32:
733 return EmitAtomicBinary(MI, BB, 4, 0, true);
734
735 case Mips::ATOMIC_LOAD_SUB_I8:
736 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
737 case Mips::ATOMIC_LOAD_SUB_I16:
738 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
739 case Mips::ATOMIC_LOAD_SUB_I32:
740 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
741
742 case Mips::ATOMIC_SWAP_I8:
743 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
744 case Mips::ATOMIC_SWAP_I16:
745 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
746 case Mips::ATOMIC_SWAP_I32:
747 return EmitAtomicBinary(MI, BB, 4, 0);
748
749 case Mips::ATOMIC_CMP_SWAP_I8:
750 return EmitAtomicCmpSwapPartword(MI, BB, 1);
751 case Mips::ATOMIC_CMP_SWAP_I16:
752 return EmitAtomicCmpSwapPartword(MI, BB, 2);
753 case Mips::ATOMIC_CMP_SWAP_I32:
754 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000755 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000756}
757
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000758// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
759// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
760MachineBasicBlock *
761MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000762 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000763 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000764 assert(Size == 4 && "Unsupported size for EmitAtomicBinary.");
765
766 MachineFunction *MF = BB->getParent();
767 MachineRegisterInfo &RegInfo = MF->getRegInfo();
768 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
769 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
770 DebugLoc dl = MI->getDebugLoc();
771
Akira Hatanaka4061da12011-07-19 20:11:17 +0000772 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000773 unsigned Ptr = MI->getOperand(1).getReg();
774 unsigned Incr = MI->getOperand(2).getReg();
775
Akira Hatanaka4061da12011-07-19 20:11:17 +0000776 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
777 unsigned AndRes = RegInfo.createVirtualRegister(RC);
778 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000779
780 // insert new blocks after the current block
781 const BasicBlock *LLVM_BB = BB->getBasicBlock();
782 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
783 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
784 MachineFunction::iterator It = BB;
785 ++It;
786 MF->insert(It, loopMBB);
787 MF->insert(It, exitMBB);
788
789 // Transfer the remainder of BB and its successor edges to exitMBB.
790 exitMBB->splice(exitMBB->begin(), BB,
791 llvm::next(MachineBasicBlock::iterator(MI)),
792 BB->end());
793 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
794
795 // thisMBB:
796 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000797 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000798 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000799 loopMBB->addSuccessor(loopMBB);
800 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000801
802 // loopMBB:
803 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000804 // <binop> storeval, oldval, incr
805 // sc success, storeval, 0(ptr)
806 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000807 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000808 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000809 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000810 // and andres, oldval, incr
811 // nor storeval, $0, andres
812 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr);
813 BuildMI(BB, dl, TII->get(Mips::NOR), StoreVal)
814 .addReg(Mips::ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000815 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000816 // <binop> storeval, oldval, incr
817 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000818 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000819 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000820 }
Akira Hatanaka4061da12011-07-19 20:11:17 +0000821 BuildMI(BB, dl, TII->get(Mips::SC), Success)
822 .addReg(StoreVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000823 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +0000824 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000825
826 MI->eraseFromParent(); // The instruction is gone now.
827
Akira Hatanaka939ece12011-07-19 03:42:13 +0000828 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000829}
830
831MachineBasicBlock *
832MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000833 MachineBasicBlock *BB,
834 unsigned Size, unsigned BinOpcode,
835 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000836 assert((Size == 1 || Size == 2) &&
837 "Unsupported size for EmitAtomicBinaryPartial.");
838
839 MachineFunction *MF = BB->getParent();
840 MachineRegisterInfo &RegInfo = MF->getRegInfo();
841 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
842 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
843 DebugLoc dl = MI->getDebugLoc();
844
845 unsigned Dest = MI->getOperand(0).getReg();
846 unsigned Ptr = MI->getOperand(1).getReg();
847 unsigned Incr = MI->getOperand(2).getReg();
848
Akira Hatanaka4061da12011-07-19 20:11:17 +0000849 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
850 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000851 unsigned Mask = RegInfo.createVirtualRegister(RC);
852 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000853 unsigned NewVal = RegInfo.createVirtualRegister(RC);
854 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000855 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000856 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
857 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
858 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
859 unsigned AndRes = RegInfo.createVirtualRegister(RC);
860 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000861 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000862 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
863 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
864 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
865 unsigned SllRes = RegInfo.createVirtualRegister(RC);
866 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000867
868 // insert new blocks after the current block
869 const BasicBlock *LLVM_BB = BB->getBasicBlock();
870 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000871 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000872 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
873 MachineFunction::iterator It = BB;
874 ++It;
875 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000876 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000877 MF->insert(It, exitMBB);
878
879 // Transfer the remainder of BB and its successor edges to exitMBB.
880 exitMBB->splice(exitMBB->begin(), BB,
881 llvm::next(MachineBasicBlock::iterator(MI)),
882 BB->end());
883 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
884
Akira Hatanaka81b44112011-07-19 17:09:53 +0000885 BB->addSuccessor(loopMBB);
886 loopMBB->addSuccessor(loopMBB);
887 loopMBB->addSuccessor(sinkMBB);
888 sinkMBB->addSuccessor(exitMBB);
889
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000890 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000891 // addiu masklsb2,$0,-4 # 0xfffffffc
892 // and alignedaddr,ptr,masklsb2
893 // andi ptrlsb2,ptr,3
894 // sll shiftamt,ptrlsb2,3
895 // ori maskupper,$0,255 # 0xff
896 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000897 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +0000898 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000899
900 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000901 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
902 .addReg(Mips::ZERO).addImm(-4);
903 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
904 .addReg(Ptr).addReg(MaskLSB2);
905 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
906 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
907 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
908 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +0000909 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
910 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000911 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +0000912 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +0000913
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000914
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +0000915 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000916 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000917 // ll oldval,0(alignedaddr)
918 // binop binopres,oldval,incr2
919 // and newval,binopres,mask
920 // and maskedoldval0,oldval,mask2
921 // or storeval,maskedoldval0,newval
922 // sc success,storeval,0(alignedaddr)
923 // beq success,$0,loopMBB
924
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +0000925 // atomic.swap
926 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000927 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +0000928 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +0000929 // and maskedoldval0,oldval,mask2
930 // or storeval,maskedoldval0,newval
931 // sc success,storeval,0(alignedaddr)
932 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +0000933
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000934 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000935 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000936 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000937 // and andres, oldval, incr2
938 // nor binopres, $0, andres
939 // and newval, binopres, mask
940 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
941 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
942 .addReg(Mips::ZERO).addReg(AndRes);
943 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000944 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000945 // <binop> binopres, oldval, incr2
946 // and newval, binopres, mask
947 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
948 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +0000949 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +0000950 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +0000951 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +0000952 }
953
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000954 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000955 .addReg(OldVal).addReg(Mask2);
956 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000957 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000958 BuildMI(BB, dl, TII->get(Mips::SC), Success)
959 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000960 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +0000961 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000962
Akira Hatanaka939ece12011-07-19 03:42:13 +0000963 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000964 // and maskedoldval1,oldval,mask
965 // srl srlres,maskedoldval1,shiftamt
966 // sll sllres,srlres,24
967 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +0000968 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000969 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +0000970
Akira Hatanaka4061da12011-07-19 20:11:17 +0000971 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
972 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +0000973 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
974 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000975 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
976 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000977 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000978 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000979
980 MI->eraseFromParent(); // The instruction is gone now.
981
Akira Hatanaka939ece12011-07-19 03:42:13 +0000982 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000983}
984
985MachineBasicBlock *
986MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000987 MachineBasicBlock *BB,
988 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000989 assert(Size == 4 && "Unsupported size for EmitAtomicCmpSwap.");
990
991 MachineFunction *MF = BB->getParent();
992 MachineRegisterInfo &RegInfo = MF->getRegInfo();
993 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
994 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
995 DebugLoc dl = MI->getDebugLoc();
996
997 unsigned Dest = MI->getOperand(0).getReg();
998 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +0000999 unsigned OldVal = MI->getOperand(2).getReg();
1000 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001001
Akira Hatanaka4061da12011-07-19 20:11:17 +00001002 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001003
1004 // insert new blocks after the current block
1005 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1006 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1007 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1008 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1009 MachineFunction::iterator It = BB;
1010 ++It;
1011 MF->insert(It, loop1MBB);
1012 MF->insert(It, loop2MBB);
1013 MF->insert(It, exitMBB);
1014
1015 // Transfer the remainder of BB and its successor edges to exitMBB.
1016 exitMBB->splice(exitMBB->begin(), BB,
1017 llvm::next(MachineBasicBlock::iterator(MI)),
1018 BB->end());
1019 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1020
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001021 // thisMBB:
1022 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001023 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001024 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001025 loop1MBB->addSuccessor(exitMBB);
1026 loop1MBB->addSuccessor(loop2MBB);
1027 loop2MBB->addSuccessor(loop1MBB);
1028 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001029
1030 // loop1MBB:
1031 // ll dest, 0(ptr)
1032 // bne dest, oldval, exitMBB
1033 BB = loop1MBB;
Akira Hatanakad3ac47f2011-07-07 18:57:00 +00001034 BuildMI(BB, dl, TII->get(Mips::LL), Dest).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001035 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001036 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001037
1038 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001039 // sc success, newval, 0(ptr)
1040 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001041 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001042 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1043 .addReg(NewVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001044 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001045 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001046
1047 MI->eraseFromParent(); // The instruction is gone now.
1048
Akira Hatanaka939ece12011-07-19 03:42:13 +00001049 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001050}
1051
1052MachineBasicBlock *
1053MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001054 MachineBasicBlock *BB,
1055 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001056 assert((Size == 1 || Size == 2) &&
1057 "Unsupported size for EmitAtomicCmpSwapPartial.");
1058
1059 MachineFunction *MF = BB->getParent();
1060 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1061 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1062 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1063 DebugLoc dl = MI->getDebugLoc();
1064
1065 unsigned Dest = MI->getOperand(0).getReg();
1066 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001067 unsigned CmpVal = MI->getOperand(2).getReg();
1068 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001069
Akira Hatanaka4061da12011-07-19 20:11:17 +00001070 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1071 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001072 unsigned Mask = RegInfo.createVirtualRegister(RC);
1073 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001074 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1075 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1076 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1077 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1078 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1079 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1080 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1081 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1082 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1083 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1084 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1085 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1086 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1087 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001088
1089 // insert new blocks after the current block
1090 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1091 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1092 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001093 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001094 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1095 MachineFunction::iterator It = BB;
1096 ++It;
1097 MF->insert(It, loop1MBB);
1098 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001099 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001100 MF->insert(It, exitMBB);
1101
1102 // Transfer the remainder of BB and its successor edges to exitMBB.
1103 exitMBB->splice(exitMBB->begin(), BB,
1104 llvm::next(MachineBasicBlock::iterator(MI)),
1105 BB->end());
1106 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1107
Akira Hatanaka81b44112011-07-19 17:09:53 +00001108 BB->addSuccessor(loop1MBB);
1109 loop1MBB->addSuccessor(sinkMBB);
1110 loop1MBB->addSuccessor(loop2MBB);
1111 loop2MBB->addSuccessor(loop1MBB);
1112 loop2MBB->addSuccessor(sinkMBB);
1113 sinkMBB->addSuccessor(exitMBB);
1114
Akira Hatanaka70564a92011-07-19 18:14:26 +00001115 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001116 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001117 // addiu masklsb2,$0,-4 # 0xfffffffc
1118 // and alignedaddr,ptr,masklsb2
1119 // andi ptrlsb2,ptr,3
1120 // sll shiftamt,ptrlsb2,3
1121 // ori maskupper,$0,255 # 0xff
1122 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001123 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001124 // andi maskedcmpval,cmpval,255
1125 // sll shiftedcmpval,maskedcmpval,shiftamt
1126 // andi maskednewval,newval,255
1127 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001128 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001129 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1130 .addReg(Mips::ZERO).addImm(-4);
1131 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1132 .addReg(Ptr).addReg(MaskLSB2);
1133 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1134 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1135 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1136 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001137 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1138 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001139 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001140 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1141 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001142 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1143 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001144 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1145 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001146 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1147 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001148
1149 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001150 // ll oldval,0(alginedaddr)
1151 // and maskedoldval0,oldval,mask
1152 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001153 BB = loop1MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001154 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
1155 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1156 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001157 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001158 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001159
1160 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001161 // and maskedoldval1,oldval,mask2
1162 // or storeval,maskedoldval1,shiftednewval
1163 // sc success,storeval,0(alignedaddr)
1164 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001165 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001166 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1167 .addReg(OldVal).addReg(Mask2);
1168 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1169 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1170 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1171 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001172 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001173 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001174
Akira Hatanaka939ece12011-07-19 03:42:13 +00001175 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001176 // srl srlres,maskedoldval0,shiftamt
1177 // sll sllres,srlres,24
1178 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001179 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001180 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001181
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001182 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1183 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001184 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1185 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001186 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001187 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001188
1189 MI->eraseFromParent(); // The instruction is gone now.
1190
Akira Hatanaka939ece12011-07-19 03:42:13 +00001191 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001192}
1193
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001194//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001195// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001196//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001197SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001198LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001199{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001200 MachineFunction &MF = DAG.getMachineFunction();
1201 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1202
1203 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001204 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1205 "Cannot lower if the alignment of the allocated space is larger than \
1206 that of the stack.");
1207
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001208 SDValue Chain = Op.getOperand(0);
1209 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001210 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001211
1212 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +00001213 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001214
1215 // Subtract the dynamic size from the actual stack size to
1216 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +00001217 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001218
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001219 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001220 // must be placed in the stack pointer register.
Akira Hatanaka053546c2011-05-25 02:20:00 +00001221 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
1222 SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001223
1224 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001225 // value and a chain
Akira Hatanaka21afc632011-06-21 00:40:49 +00001226 SDVTList VTLs = DAG.getVTList(MVT::i32, MVT::Other);
1227 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1228 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1229
1230 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001231}
1232
1233SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001234LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001235{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001236 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001237 // the block to branch to if the condition is true.
1238 SDValue Chain = Op.getOperand(0);
1239 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001240 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001241
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001242 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1243
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001244 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001245 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001246 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001247
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001248 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001249 Mips::CondCode CC =
1250 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001251 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001252
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001253 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001254 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001255}
1256
1257SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001258LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001259{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001260 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001261
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001262 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001263 if (Cond.getOpcode() != MipsISD::FPCmp)
1264 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001265
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001266 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1267 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001268}
1269
Dan Gohmand858e902010-04-17 15:26:15 +00001270SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1271 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001272 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001273 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001274 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001275
Eli Friedmane2c74082009-08-03 02:22:28 +00001276 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001277 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001278
Chris Lattnerb71b9092009-08-13 06:28:06 +00001279 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001280
Chris Lattnere3736f82009-08-13 05:41:27 +00001281 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001282 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1283 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001284 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001285 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1286 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001287 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001288 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001289 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001290 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1291 MipsII::MO_ABS_HI);
1292 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1293 MipsII::MO_ABS_LO);
1294 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1295 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001296 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001297 }
1298
Akira Hatanaka0f843822011-06-07 18:58:42 +00001299 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1300 MipsII::MO_GOT);
1301 GA = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, GA);
1302 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
1303 DAG.getEntryNode(), GA, MachinePointerInfo(),
1304 false, false, 0);
1305 // On functions and global targets not internal linked only
1306 // a load from got/GP is necessary for PIC to work.
1307 if (!GV->hasInternalLinkage() &&
1308 (!GV->hasLocalLinkage() || isa<Function>(GV)))
1309 return ResNode;
1310 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1311 MipsII::MO_ABS_LO);
1312 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
1313 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001314}
1315
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001316SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1317 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001318 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1319 // FIXME there isn't actually debug info here
1320 DebugLoc dl = Op.getDebugLoc();
1321
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001322 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001323 // %hi/%lo relocation
1324 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1325 MipsII::MO_ABS_HI);
1326 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1327 MipsII::MO_ABS_LO);
1328 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1329 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1330 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001331 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001332
1333 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1334 MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001335 BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, BAGOTOffset);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001336 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1337 MipsII::MO_ABS_LO);
1338 SDValue Load = DAG.getLoad(MVT::i32, dl,
1339 DAG.getEntryNode(), BAGOTOffset,
1340 MachinePointerInfo(), false, false, 0);
1341 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
1342 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001343}
1344
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001345SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001346LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001347{
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001348 // If the relocation model is PIC, use the General Dynamic TLS Model,
1349 // otherwise use the Initial Exec or Local Exec TLS Model.
1350 // TODO: implement Local Dynamic TLS model
1351
1352 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1353 DebugLoc dl = GA->getDebugLoc();
1354 const GlobalValue *GV = GA->getGlobal();
1355 EVT PtrVT = getPointerTy();
1356
1357 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1358 // General Dynamic TLS Model
1359 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001360 0, MipsII::MO_TLSGD);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001361 SDValue Tlsgd = DAG.getNode(MipsISD::TlsGd, dl, MVT::i32, TGA);
1362 SDValue GP = DAG.getRegister(Mips::GP, MVT::i32);
1363 SDValue Argument = DAG.getNode(ISD::ADD, dl, MVT::i32, GP, Tlsgd);
1364
1365 ArgListTy Args;
1366 ArgListEntry Entry;
1367 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001368 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001369 Args.push_back(Entry);
1370 std::pair<SDValue, SDValue> CallResult =
1371 LowerCallTo(DAG.getEntryNode(),
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001372 (Type *) Type::getInt32Ty(*DAG.getContext()),
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001373 false, false, false, false, 0, CallingConv::C, false, true,
1374 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG,
1375 dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001376
1377 return CallResult.first;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001378 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001379
1380 SDValue Offset;
1381 if (GV->isDeclaration()) {
1382 // Initial Exec TLS Model
1383 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1384 MipsII::MO_GOTTPREL);
1385 Offset = DAG.getLoad(MVT::i32, dl,
1386 DAG.getEntryNode(), TGA, MachinePointerInfo(),
1387 false, false, 0);
1388 } else {
1389 // Local Exec TLS Model
1390 SDVTList VTs = DAG.getVTList(MVT::i32);
1391 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1392 MipsII::MO_TPREL_HI);
1393 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1394 MipsII::MO_TPREL_LO);
1395 SDValue Hi = DAG.getNode(MipsISD::TprelHi, dl, VTs, &TGAHi, 1);
1396 SDValue Lo = DAG.getNode(MipsISD::TprelLo, dl, MVT::i32, TGALo);
1397 Offset = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1398 }
1399
1400 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1401 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001402}
1403
1404SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001405LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001406{
Dan Gohman475871a2008-07-27 21:46:04 +00001407 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001408 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +00001409 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001410 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001411 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001412 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001413
Owen Andersone50ed302009-08-10 22:56:29 +00001414 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001415 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001416
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001417 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
1418
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +00001419 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001420 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001421 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001422 } else {// Emit Load from Global Pointer
1423 JTI = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, JTI);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001424 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
1425 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001426 false, false, 0);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001427 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001428
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001429 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1430 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001431 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001432 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001433
1434 return ResNode;
1435}
1436
Dan Gohman475871a2008-07-27 21:46:04 +00001437SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001438LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001439{
Dan Gohman475871a2008-07-27 21:46:04 +00001440 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001441 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001442 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001443 // FIXME there isn't actually debug info here
1444 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001445
1446 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001447 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001448 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001449 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001450 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001451 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001452 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1453 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001454 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001455
1456 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001457 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001458 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001459 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001460 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001461 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1462 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001463 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001464 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001465 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001466 N->getOffset(), MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001467 CP = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001468 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001469 CP, MachinePointerInfo::getConstantPool(),
1470 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001471 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001472 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001473 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001474 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
1475 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001476
1477 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001478}
1479
Dan Gohmand858e902010-04-17 15:26:15 +00001480SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001481 MachineFunction &MF = DAG.getMachineFunction();
1482 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1483
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001484 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001485 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1486 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001487
1488 // vastart just stores the address of the VarArgsFrameIndex slot into the
1489 // memory location argument.
1490 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001491 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1492 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +00001493 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001494}
1495
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001496static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
1497 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
1498 DebugLoc dl = Op.getDebugLoc();
1499 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
1500 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
1501 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
1502 DAG.getConstant(0x7fffffff, MVT::i32));
1503 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
1504 DAG.getConstant(0x80000000, MVT::i32));
1505 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1506 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
1507}
1508
1509static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001510 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001511 // Use ext/ins instructions if target architecture is Mips32r2.
1512 // Eliminate redundant mfc1 and mtc1 instructions.
1513 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001514
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001515 if (!isLittle)
1516 std::swap(LoIdx, HiIdx);
1517
1518 DebugLoc dl = Op.getDebugLoc();
1519 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1520 Op.getOperand(0),
1521 DAG.getConstant(LoIdx, MVT::i32));
1522 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1523 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1524 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1525 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1526 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1527 DAG.getConstant(0x7fffffff, MVT::i32));
1528 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1529 DAG.getConstant(0x80000000, MVT::i32));
1530 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1531
1532 if (!isLittle)
1533 std::swap(Word0, Word1);
1534
1535 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1536}
1537
1538SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1539 const {
1540 EVT Ty = Op.getValueType();
1541
1542 assert(Ty == MVT::f32 || Ty == MVT::f64);
1543
1544 if (Ty == MVT::f32)
1545 return LowerFCOPYSIGN32(Op, DAG);
1546 else
1547 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
1548}
1549
Akira Hatanaka2e591472011-06-02 00:24:44 +00001550SDValue MipsTargetLowering::
1551LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001552 // check the depth
1553 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001554 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001555
1556 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1557 MFI->setFrameAddressIsTaken(true);
1558 EVT VT = Op.getValueType();
1559 DebugLoc dl = Op.getDebugLoc();
1560 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Mips::FP, VT);
1561 return FrameAddr;
1562}
1563
Akira Hatanakadb548262011-07-19 23:30:50 +00001564// TODO: set SType according to the desired memory barrier behavior.
1565SDValue MipsTargetLowering::LowerMEMBARRIER(SDValue Op,
1566 SelectionDAG& DAG) const {
1567 unsigned SType = 0;
1568 DebugLoc dl = Op.getDebugLoc();
1569 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1570 DAG.getConstant(SType, MVT::i32));
1571}
1572
Eli Friedman14648462011-07-27 22:21:52 +00001573SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1574 SelectionDAG& DAG) const {
1575 // FIXME: Need pseudo-fence for 'singlethread' fences
1576 // FIXME: Set SType for weaker fences where supported/appropriate.
1577 unsigned SType = 0;
1578 DebugLoc dl = Op.getDebugLoc();
1579 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1580 DAG.getConstant(SType, MVT::i32));
1581}
1582
Akira Hatanakabb15e112011-08-17 02:05:42 +00001583SDValue MipsTargetLowering::LowerAND(SDValue Op, SelectionDAG& DAG) const {
1584 // Pattern match EXT.
1585 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
1586 // => ext $dst, $src, size, pos
1587 if (!Subtarget->isMips32r2())
1588 return Op;
1589
1590 SDValue ShiftRight = Op.getOperand(0), Mask = Op.getOperand(1);
1591
1592 // Op's first operand must be a shift right.
1593 if (ShiftRight.getOpcode() != ISD::SRA && ShiftRight.getOpcode() != ISD::SRL)
1594 return Op;
1595
1596 // The second operand of the shift must be an immediate.
1597 uint64_t Pos;
1598 ConstantSDNode *CN;
1599 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
1600 return Op;
1601
1602 Pos = CN->getZExtValue();
1603
1604 uint64_t SMPos, SMSize;
1605 // Op's second operand must be a shifted mask.
1606 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
1607 !IsShiftedMask(CN->getZExtValue(), 32, SMPos, SMSize))
1608 return Op;
1609
1610 // Return if the shifted mask does not start at bit 0 or the sum of its size
1611 // and Pos exceeds the word's size.
1612 if (SMPos != 0 || Pos + SMSize > 32)
1613 return Op;
1614
1615 return DAG.getNode(MipsISD::Ext, Op.getDebugLoc(), MVT::i32,
1616 ShiftRight.getOperand(0),
1617 DAG.getConstant(SMSize, MVT::i32),
1618 DAG.getConstant(Pos, MVT::i32));
1619}
1620
1621SDValue MipsTargetLowering::LowerOR(SDValue Op, SelectionDAG& DAG) const {
1622 // Pattern match INS.
1623 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
1624 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
1625 // => ins $dst, $src, size, pos
1626 if (!Subtarget->isMips32r2())
1627 return Op;
1628
1629 SDValue And0 = Op.getOperand(0), And1 = Op.getOperand(1);
1630 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
1631 ConstantSDNode *CN;
1632
1633 // See if Op's first operand matches (and $src1 , mask0).
1634 if (And0.getOpcode() != ISD::AND)
1635 return Op;
1636
1637 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
1638 !IsShiftedMask(~CN->getZExtValue(), 32, SMPos0, SMSize0))
1639 return Op;
1640
1641 // See if Op's second operand matches (and (shl $src, pos), mask1).
1642 if (And1.getOpcode() != ISD::AND)
1643 return Op;
1644
1645 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
1646 !IsShiftedMask(CN->getZExtValue(), CN->getValueSizeInBits(0), SMPos1,
1647 SMSize1))
1648 return Op;
1649
1650 // The shift masks must have the same position and size.
1651 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
1652 return Op;
1653
1654 SDValue Shl = And1.getOperand(0);
1655 if (Shl.getOpcode() != ISD::SHL)
1656 return Op;
1657
1658 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
1659 return Op;
1660
1661 unsigned Shamt = CN->getZExtValue();
1662
1663 // Return if the shift amount and the first bit position of mask are not the
1664 // same.
1665 if (Shamt != SMPos0)
1666 return Op;
1667
1668 return DAG.getNode(MipsISD::Ins, Op.getDebugLoc(), MVT::i32,
1669 Shl.getOperand(0),
1670 DAG.getConstant(SMSize0, MVT::i32),
1671 DAG.getConstant(SMPos0, MVT::i32),
1672 And0.getOperand(0));
1673}
1674
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001675//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001676// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001677//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001678
1679#include "MipsGenCallingConv.inc"
1680
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001681//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001682// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001683// Mips O32 ABI rules:
1684// ---
1685// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001686// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001687// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001688// f64 - Only passed in two aliased f32 registers if no int reg has been used
1689// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001690// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1691// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001692//
1693// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001694//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001695
Duncan Sands1e96bab2010-11-04 10:49:57 +00001696static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001697 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001698 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1699
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001700 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001701
1702 static const unsigned IntRegs[] = {
1703 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1704 };
1705 static const unsigned F32Regs[] = {
1706 Mips::F12, Mips::F14
1707 };
1708 static const unsigned F64Regs[] = {
1709 Mips::D6, Mips::D7
1710 };
1711
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001712 // ByVal Args
1713 if (ArgFlags.isByVal()) {
1714 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1715 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1716 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1717 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1718 r < std::min(IntRegsSize, NextReg); ++r)
1719 State.AllocateReg(IntRegs[r]);
1720 return false;
1721 }
1722
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001723 // Promote i8 and i16
1724 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1725 LocVT = MVT::i32;
1726 if (ArgFlags.isSExt())
1727 LocInfo = CCValAssign::SExt;
1728 else if (ArgFlags.isZExt())
1729 LocInfo = CCValAssign::ZExt;
1730 else
1731 LocInfo = CCValAssign::AExt;
1732 }
1733
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001734 unsigned Reg;
1735
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001736 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1737 // is true: function is vararg, argument is 3rd or higher, there is previous
1738 // argument which is not f32 or f64.
1739 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1740 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001741 unsigned OrigAlign = ArgFlags.getOrigAlign();
1742 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001743
1744 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001745 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001746 // If this is the first part of an i64 arg,
1747 // the allocated register must be either A0 or A2.
1748 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1749 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001750 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001751 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1752 // Allocate int register and shadow next int register. If first
1753 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001754 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1755 if (Reg == Mips::A1 || Reg == Mips::A3)
1756 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1757 State.AllocateReg(IntRegs, IntRegsSize);
1758 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001759 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1760 // we are guaranteed to find an available float register
1761 if (ValVT == MVT::f32) {
1762 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1763 // Shadow int register
1764 State.AllocateReg(IntRegs, IntRegsSize);
1765 } else {
1766 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1767 // Shadow int registers
1768 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1769 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1770 State.AllocateReg(IntRegs, IntRegsSize);
1771 State.AllocateReg(IntRegs, IntRegsSize);
1772 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001773 } else
1774 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001775
Akira Hatanakad37776d2011-05-20 21:39:54 +00001776 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1777 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1778
1779 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001780 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001781 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001782 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001783
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001784 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001785}
1786
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001787//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001788// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001789//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001790
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001791static const unsigned O32IntRegsSize = 4;
1792
1793static const unsigned O32IntRegs[] = {
1794 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1795};
1796
1797// Write ByVal Arg to arg registers and stack.
1798static void
1799WriteByValArg(SDValue& Chain, DebugLoc dl,
1800 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1801 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1802 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001803 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
1804 MVT PtrType) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001805 unsigned FirstWord = VA.getLocMemOffset() / 4;
1806 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
1807 unsigned LastWord = FirstWord + NumWords;
1808 unsigned CurWord;
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001809 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001810
1811 // copy the first 4 words of byval arg to registers A0 - A3
1812 for (CurWord = FirstWord; CurWord < std::min(LastWord, O32IntRegsSize);
1813 ++CurWord) {
1814 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1815 DAG.getConstant((CurWord - FirstWord) * 4,
1816 MVT::i32));
1817 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1818 MachinePointerInfo(),
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001819 false, false, std::min(ByValAlign,
1820 (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001821 MemOpChains.push_back(LoadVal.getValue(1));
1822 unsigned DstReg = O32IntRegs[CurWord];
1823 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1824 }
1825
1826 // copy remaining part of byval arg to stack.
1827 if (CurWord < LastWord) {
Eric Christopher471e4222011-06-08 23:55:35 +00001828 unsigned SizeInBytes = (LastWord - CurWord) * 4;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001829 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1830 DAG.getConstant((CurWord - FirstWord) * 4,
1831 MVT::i32));
1832 LastFI = MFI->CreateFixedObject(SizeInBytes, CurWord * 4, true);
1833 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
1834 Chain = DAG.getMemcpy(Chain, dl, Dst, Src,
1835 DAG.getConstant(SizeInBytes, MVT::i32),
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001836 /*Align*/ByValAlign,
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001837 /*isVolatile=*/false, /*AlwaysInline=*/false,
1838 MachinePointerInfo(0), MachinePointerInfo(0));
1839 MemOpChains.push_back(Chain);
1840 }
1841}
1842
Dan Gohman98ca4f22009-08-05 01:29:28 +00001843/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001844/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001845/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001846SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001847MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001848 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001849 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001850 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001851 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001852 const SmallVectorImpl<ISD::InputArg> &Ins,
1853 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001854 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001855 // MIPs target does not yet support tail call optimization.
1856 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001857
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001858 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001859 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00001860 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001861 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001862 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001863
1864 // Analyze operands of the call, assigning locations to each operand.
1865 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001866 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1867 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001868
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001869 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001870 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001871 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001872 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001873
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001874 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001875 unsigned NextStackOffset = CCInfo.getNextStackOffset();
1876
1877 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NextStackOffset,
1878 true));
1879
1880 // If this is the first call, create a stack frame object that points to
1881 // a location to which .cprestore saves $gp.
1882 if (IsPIC && !MipsFI->getGPFI())
1883 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1884
Akira Hatanaka21afc632011-06-21 00:40:49 +00001885 // Get the frame index of the stack frame object that points to the location
1886 // of dynamically allocated area on the stack.
1887 int DynAllocFI = MipsFI->getDynAllocFI();
1888
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001889 // Update size of the maximum argument space.
1890 // For O32, a minimum of four words (16 bytes) of argument space is
1891 // allocated.
1892 if (Subtarget->isABI_O32())
1893 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
1894
1895 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1896
1897 if (MaxCallFrameSize < NextStackOffset) {
1898 MipsFI->setMaxCallFrameSize(NextStackOffset);
1899
Akira Hatanaka21afc632011-06-21 00:40:49 +00001900 // Set the offsets relative to $sp of the $gp restore slot and dynamically
1901 // allocated stack space. These offsets must be aligned to a boundary
1902 // determined by the stack alignment of the ABI.
1903 unsigned StackAlignment = TFL->getStackAlignment();
1904 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
1905 StackAlignment * StackAlignment;
1906
1907 if (IsPIC)
1908 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
1909
1910 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001911 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001912
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001913 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001914 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1915 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001916
Eric Christopher471e4222011-06-08 23:55:35 +00001917 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00001918
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001919 // Walk the register/memloc assignments, inserting copies/loads.
1920 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001921 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001922 CCValAssign &VA = ArgLocs[i];
1923
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001924 // Promote the value if needed.
1925 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001926 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001927 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001928 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001929 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001930 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001931 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001932 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1933 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001934 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1935 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001936 if (!Subtarget->isLittle())
1937 std::swap(Lo, Hi);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001938 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
1939 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
1940 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001941 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001942 }
1943 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00001944 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001945 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001946 break;
1947 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001948 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001949 break;
1950 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001951 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001952 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001953 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001954
1955 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001956 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001957 if (VA.isRegLoc()) {
1958 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00001959 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001960 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001961
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001962 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00001963 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001964
Eric Christopher471e4222011-06-08 23:55:35 +00001965 // ByVal Arg.
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001966 ISD::ArgFlagsTy Flags = Outs[i].Flags;
1967 if (Flags.isByVal()) {
1968 assert(Subtarget->isABI_O32() &&
1969 "No support for ByVal args by ABIs other than O32 yet.");
1970 assert(Flags.getByValSize() &&
1971 "ByVal args of size 0 should have been ignored by front-end.");
1972 WriteByValArg(Chain, dl, RegsToPass, MemOpChains, LastFI, MFI, DAG, Arg,
1973 VA, Flags, getPointerTy());
1974 continue;
1975 }
1976
Chris Lattnere0b12152008-03-17 06:57:02 +00001977 // Create the frame index object for this incoming parameter
Eric Christopher471e4222011-06-08 23:55:35 +00001978 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001979 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00001980 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00001981
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001982 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00001983 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00001984 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
1985 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001986 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001987 }
1988
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001989 // Extend range of indices of frame objects for outgoing arguments that were
1990 // created during this function call. Skip this step if no such objects were
1991 // created.
1992 if (LastFI)
1993 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
1994
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001995 // Transform all store nodes into one single node because all store
1996 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001997 if (!MemOpChains.empty())
1998 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001999 &MemOpChains[0], MemOpChains.size());
2000
Bill Wendling056292f2008-09-16 21:48:12 +00002001 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002002 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2003 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002004 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002005 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002006 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002007
2008 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002009 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
2010 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2011 getPointerTy(), 0,MipsII:: MO_GOT);
2012 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
2013 0, MipsII::MO_ABS_LO);
2014 } else {
2015 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2016 getPointerTy(), 0, OpFlag);
2017 }
2018
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002019 LoadSymAddr = true;
2020 }
2021 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002022 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002023 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002024 LoadSymAddr = true;
2025 }
2026
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002027 SDValue InFlag;
2028
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002029 // Create nodes that load address of callee and copy it to T9
2030 if (IsPIC) {
2031 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002032 // Load callee address
Akira Hatanaka342837d2011-05-28 01:07:07 +00002033 Callee = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, Callee);
Akira Hatanaka25eba392011-06-24 19:01:25 +00002034 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), Callee,
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002035 MachinePointerInfo::getGOT(),
2036 false, false, 0);
2037
2038 // Use GOT+LO if callee has internal linkage.
2039 if (CalleeLo.getNode()) {
2040 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
2041 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
2042 } else
2043 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002044 }
2045
2046 // copy to T9
2047 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
2048 InFlag = Chain.getValue(1);
2049 Callee = DAG.getRegister(Mips::T9, MVT::i32);
2050 }
Bill Wendling056292f2008-09-16 21:48:12 +00002051
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002052 // Build a sequence of copy-to-reg nodes chained together with token
2053 // chain and flag operands which copy the outgoing args into registers.
2054 // The InFlag in necessary since all emitted instructions must be
2055 // stuck together.
2056 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2057 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2058 RegsToPass[i].second, InFlag);
2059 InFlag = Chain.getValue(1);
2060 }
2061
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002062 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002063 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002064 //
2065 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002066 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002067 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002068 Ops.push_back(Chain);
2069 Ops.push_back(Callee);
2070
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002071 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002072 // known live into the call.
2073 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2074 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2075 RegsToPass[i].second.getValueType()));
2076
Gabor Greifba36cb52008-08-28 21:40:38 +00002077 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002078 Ops.push_back(InFlag);
2079
Dale Johannesen33c960f2009-02-04 20:06:27 +00002080 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002081 InFlag = Chain.getValue(1);
2082
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002083 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002084 Chain = DAG.getCALLSEQ_END(Chain,
2085 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002086 DAG.getIntPtrConstant(0, true), InFlag);
2087 InFlag = Chain.getValue(1);
2088
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002089 // Handle result values, copying them out of physregs into vregs that we
2090 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002091 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2092 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002093}
2094
Dan Gohman98ca4f22009-08-05 01:29:28 +00002095/// LowerCallResult - Lower the result values of a call into the
2096/// appropriate copies out of appropriate physical registers.
2097SDValue
2098MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002099 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002100 const SmallVectorImpl<ISD::InputArg> &Ins,
2101 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002102 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002103 // Assign locations to each value returned by this call.
2104 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002105 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2106 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002107
Dan Gohman98ca4f22009-08-05 01:29:28 +00002108 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002109
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002110 // Copy all of the result registers out of their specified physreg.
2111 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002112 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002113 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002114 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002115 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002116 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002117
Dan Gohman98ca4f22009-08-05 01:29:28 +00002118 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002119}
2120
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002121//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002122// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002123//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002124static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2125 std::vector<SDValue>& OutChains,
2126 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2127 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2128 unsigned LocMem = VA.getLocMemOffset();
2129 unsigned FirstWord = LocMem / 4;
2130
2131 // copy register A0 - A3 to frame object
2132 for (unsigned i = 0; i < NumWords; ++i) {
2133 unsigned CurWord = FirstWord + i;
2134 if (CurWord >= O32IntRegsSize)
2135 break;
2136
2137 unsigned SrcReg = O32IntRegs[CurWord];
2138 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2139 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2140 DAG.getConstant(i * 4, MVT::i32));
2141 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2142 StorePtr, MachinePointerInfo(), false,
2143 false, 0);
2144 OutChains.push_back(Store);
2145 }
2146}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002147
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002148/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002149/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002150SDValue
2151MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002152 CallingConv::ID CallConv,
2153 bool isVarArg,
2154 const SmallVectorImpl<ISD::InputArg>
2155 &Ins,
2156 DebugLoc dl, SelectionDAG &DAG,
2157 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002158 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002159 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002160 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002161 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002162
Dan Gohman1e93df62010-04-17 14:41:14 +00002163 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002164
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002165 // Used with vargs to acumulate store chains.
2166 std::vector<SDValue> OutChains;
2167
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002168 // Assign locations to all of the incoming arguments.
2169 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002170 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2171 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002172
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002173 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002174 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002175 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002176 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002177
Akira Hatanaka43299772011-05-20 23:22:14 +00002178 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002179
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002180 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002181 CCValAssign &VA = ArgLocs[i];
2182
2183 // Arguments stored on registers
2184 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002185 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002186 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002187 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002188
Owen Anderson825b72b2009-08-11 20:47:22 +00002189 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002190 RC = Mips::CPURegsRegisterClass;
2191 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002192 RC = Mips::FGR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002193 else if (RegVT == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002194 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002195 RC = Mips::AFGR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002196 } else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002197 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002198
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002199 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002200 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002201 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002202 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002203
2204 // If this is an 8 or 16-bit value, it has been passed promoted
2205 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002206 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002207 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002208 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002209 if (VA.getLocInfo() == CCValAssign::SExt)
2210 Opcode = ISD::AssertSext;
2211 else if (VA.getLocInfo() == CCValAssign::ZExt)
2212 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002213 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002214 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00002215 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00002216 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002217 }
2218
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002219 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002220 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002221 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
2222 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00002223 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002224 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002225 VA.getLocReg()+1, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002226 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002227 if (!Subtarget->isLittle())
2228 std::swap(ArgValue, ArgValue2);
2229 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2230 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002231 }
2232 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002233
Dan Gohman98ca4f22009-08-05 01:29:28 +00002234 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002235 } else { // VA.isRegLoc()
2236
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002237 // sanity check
2238 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002239
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002240 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2241
2242 if (Flags.isByVal()) {
2243 assert(Subtarget->isABI_O32() &&
2244 "No support for ByVal args by ABIs other than O32 yet.");
2245 assert(Flags.getByValSize() &&
2246 "ByVal args of size 0 should have been ignored by front-end.");
2247 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2248 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2249 true);
2250 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2251 InVals.push_back(FIN);
2252 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2253
2254 continue;
2255 }
2256
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002257 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002258 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
2259 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002260
2261 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002262 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002263 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002264 MachinePointerInfo::getFixedStack(LastFI),
David Greenef6fa1862010-02-15 16:56:10 +00002265 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002266 }
2267 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002268
2269 // The mips ABIs for returning structs by value requires that we copy
2270 // the sret argument into $v0 for the return. Save the argument into
2271 // a virtual register so that we can access it from the return points.
2272 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2273 unsigned Reg = MipsFI->getSRetReturnReg();
2274 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002275 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002276 MipsFI->setSRetReturnReg(Reg);
2277 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002278 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002279 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002280 }
2281
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002282 if (isVarArg && Subtarget->isABI_O32()) {
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002283 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002284 // which is a value necessary to VASTART.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002285 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002286 assert(NextStackOffset % 4 == 0 &&
2287 "NextStackOffset must be aligned to 4-byte boundaries.");
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002288 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
2289 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002290
2291 // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
2292 // copy the integer registers that have not been used for argument passing
2293 // to the caller's stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002294 for (; NextStackOffset < 16; NextStackOffset += 4) {
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002295 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002296 unsigned Idx = NextStackOffset / 4;
2297 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
2298 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Akira Hatanaka69c19f72011-05-23 20:16:59 +00002299 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002300 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2301 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2302 MachinePointerInfo(),
2303 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002304 }
2305 }
2306
Akira Hatanaka43299772011-05-20 23:22:14 +00002307 MipsFI->setLastInArgFI(LastFI);
2308
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002309 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002310 // the size of Ins and InVals. This only happens when on varg functions
2311 if (!OutChains.empty()) {
2312 OutChains.push_back(Chain);
2313 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2314 &OutChains[0], OutChains.size());
2315 }
2316
Dan Gohman98ca4f22009-08-05 01:29:28 +00002317 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002318}
2319
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002320//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002321// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002322//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002323
Dan Gohman98ca4f22009-08-05 01:29:28 +00002324SDValue
2325MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002326 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002327 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002328 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002329 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002330
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002331 // CCValAssign - represent the assignment of
2332 // the return value to a location
2333 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002334
2335 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002336 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2337 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002338
Dan Gohman98ca4f22009-08-05 01:29:28 +00002339 // Analize return values.
2340 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002341
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002342 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002343 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002344 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002345 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002346 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002347 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002348 }
2349
Dan Gohman475871a2008-07-27 21:46:04 +00002350 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002351
2352 // Copy the result values into the output registers.
2353 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2354 CCValAssign &VA = RVLocs[i];
2355 assert(VA.isRegLoc() && "Can only return in registers!");
2356
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002357 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002358 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002359
2360 // guarantee that all emitted copies are
2361 // stuck together, avoiding something bad
2362 Flag = Chain.getValue(1);
2363 }
2364
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002365 // The mips ABIs for returning structs by value requires that we copy
2366 // the sret argument into $v0 for the return. We saved the argument into
2367 // a virtual register in the entry block, so now we copy the value out
2368 // and into $v0.
2369 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2370 MachineFunction &MF = DAG.getMachineFunction();
2371 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2372 unsigned Reg = MipsFI->getSRetReturnReg();
2373
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002374 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002375 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002376 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002377
Dale Johannesena05dca42009-02-04 23:02:30 +00002378 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002379 Flag = Chain.getValue(1);
2380 }
2381
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002382 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002383 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002384 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002385 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002386 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002387 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002388 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002389}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002390
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002391//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002392// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002393//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002394
2395/// getConstraintType - Given a constraint letter, return the type of
2396/// constraint it is for this target.
2397MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002398getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002399{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002400 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002401 // GCC config/mips/constraints.md
2402 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002403 // 'd' : An address register. Equivalent to r
2404 // unless generating MIPS16 code.
2405 // 'y' : Equivalent to r; retained for
2406 // backwards compatibility.
2407 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002408 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002409 switch (Constraint[0]) {
2410 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002411 case 'd':
2412 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002413 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002414 return C_RegisterClass;
2415 break;
2416 }
2417 }
2418 return TargetLowering::getConstraintType(Constraint);
2419}
2420
John Thompson44ab89e2010-10-29 17:29:13 +00002421/// Examine constraint type and operand type and determine a weight value.
2422/// This object must already have been set up with the operand type
2423/// and the current alternative constraint selected.
2424TargetLowering::ConstraintWeight
2425MipsTargetLowering::getSingleConstraintMatchWeight(
2426 AsmOperandInfo &info, const char *constraint) const {
2427 ConstraintWeight weight = CW_Invalid;
2428 Value *CallOperandVal = info.CallOperandVal;
2429 // If we don't have a value, we can't do a match,
2430 // but allow it at the lowest weight.
2431 if (CallOperandVal == NULL)
2432 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002433 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002434 // Look at the constraint type.
2435 switch (*constraint) {
2436 default:
2437 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2438 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002439 case 'd':
2440 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002441 if (type->isIntegerTy())
2442 weight = CW_Register;
2443 break;
2444 case 'f':
2445 if (type->isFloatTy())
2446 weight = CW_Register;
2447 break;
2448 }
2449 return weight;
2450}
2451
Eric Christopher38d64262011-06-29 19:33:04 +00002452/// Given a register class constraint, like 'r', if this corresponds directly
2453/// to an LLVM register class, return a register of 0 and the register class
2454/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002455std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002456getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002457{
2458 if (Constraint.size() == 1) {
2459 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002460 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2461 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002462 case 'r':
2463 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002464 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002465 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002466 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002467 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002468 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2469 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002470 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002471 }
2472 }
2473 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2474}
2475
Dan Gohman6520e202008-10-18 02:06:02 +00002476bool
2477MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2478 // The Mips target isn't yet aware of offsets.
2479 return false;
2480}
Evan Chengeb2f9692009-10-27 19:56:55 +00002481
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002482bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2483 if (VT != MVT::f32 && VT != MVT::f64)
2484 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002485 if (Imm.isNegZero())
2486 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002487 return Imm.isZero();
2488}