blob: eb049f1868a6e88a820701fe1db69093f269826b [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);
134
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000135 setOperationAction(ISD::SDIV, MVT::i32, Expand);
136 setOperationAction(ISD::SREM, MVT::i32, Expand);
137 setOperationAction(ISD::UDIV, MVT::i32, Expand);
138 setOperationAction(ISD::UREM, MVT::i32, Expand);
139
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000140 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000141 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
142 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
143 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
144 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
145 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
146 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
147 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
148 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
149 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000150
151 if (!Subtarget->isMips32r2())
152 setOperationAction(ISD::ROTR, MVT::i32, Expand);
153
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
155 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
156 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000157 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
158 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000160 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000161 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000162 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
164 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000165 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000166 setOperationAction(ISD::FLOG, MVT::f32, Expand);
167 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
168 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
169 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000170 setOperationAction(ISD::FMA, MVT::f32, Expand);
171 setOperationAction(ISD::FMA, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000172
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000173 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
174 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000175
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000176 setOperationAction(ISD::VAARG, MVT::Other, Expand);
177 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
178 setOperationAction(ISD::VAEND, MVT::Other, Expand);
179
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000180 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000181 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
182 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000183
Akira Hatanakadb548262011-07-19 23:30:50 +0000184 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000185 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000186
Eli Friedman26689ac2011-08-03 21:06:02 +0000187 setInsertFencesForAtomic(true);
188
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000189 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000190 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000191
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000192 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000193 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
194 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000195 }
196
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000197 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000198 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000199
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000200 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000202
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000203 setTargetDAGCombine(ISD::ADDE);
204 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000205 setTargetDAGCombine(ISD::SDIVREM);
206 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000207 setTargetDAGCombine(ISD::SETCC);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000208 setTargetDAGCombine(ISD::AND);
209 setTargetDAGCombine(ISD::OR);
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
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000525static SDValue PerformANDCombine(SDNode *N, SelectionDAG& DAG,
526 TargetLowering::DAGCombinerInfo &DCI,
527 const MipsSubtarget* Subtarget) {
528 // Pattern match EXT.
529 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
530 // => ext $dst, $src, size, pos
531 if (DCI.isBeforeLegalizeOps() || !Subtarget->isMips32r2())
532 return SDValue();
533
534 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
535
536 // Op's first operand must be a shift right.
537 if (ShiftRight.getOpcode() != ISD::SRA && ShiftRight.getOpcode() != ISD::SRL)
538 return SDValue();
539
540 // The second operand of the shift must be an immediate.
541 uint64_t Pos;
542 ConstantSDNode *CN;
543 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
544 return SDValue();
545
546 Pos = CN->getZExtValue();
547
548 uint64_t SMPos, SMSize;
549 // Op's second operand must be a shifted mask.
550 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
551 !IsShiftedMask(CN->getZExtValue(), 32, SMPos, SMSize))
552 return SDValue();
553
554 // Return if the shifted mask does not start at bit 0 or the sum of its size
555 // and Pos exceeds the word's size.
556 if (SMPos != 0 || Pos + SMSize > 32)
557 return SDValue();
558
559 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), MVT::i32,
560 ShiftRight.getOperand(0),
561 DAG.getConstant(SMSize, MVT::i32),
562 DAG.getConstant(Pos, MVT::i32));
563}
564
565static SDValue PerformORCombine(SDNode *N, SelectionDAG& DAG,
566 TargetLowering::DAGCombinerInfo &DCI,
567 const MipsSubtarget* Subtarget) {
568 // Pattern match INS.
569 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
570 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
571 // => ins $dst, $src, size, pos, $src1
572 if (DCI.isBeforeLegalizeOps() || !Subtarget->isMips32r2())
573 return SDValue();
574
575 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
576 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
577 ConstantSDNode *CN;
578
579 // See if Op's first operand matches (and $src1 , mask0).
580 if (And0.getOpcode() != ISD::AND)
581 return SDValue();
582
583 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
584 !IsShiftedMask(~CN->getZExtValue(), 32, SMPos0, SMSize0))
585 return SDValue();
586
587 // See if Op's second operand matches (and (shl $src, pos), mask1).
588 if (And1.getOpcode() != ISD::AND)
589 return SDValue();
590
591 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
592 !IsShiftedMask(CN->getZExtValue(), CN->getValueSizeInBits(0), SMPos1,
593 SMSize1))
594 return SDValue();
595
596 // The shift masks must have the same position and size.
597 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
598 return SDValue();
599
600 SDValue Shl = And1.getOperand(0);
601 if (Shl.getOpcode() != ISD::SHL)
602 return SDValue();
603
604 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
605 return SDValue();
606
607 unsigned Shamt = CN->getZExtValue();
608
609 // Return if the shift amount and the first bit position of mask are not the
610 // same.
611 if (Shamt != SMPos0)
612 return SDValue();
613
614 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), MVT::i32,
615 Shl.getOperand(0),
616 DAG.getConstant(SMSize0, MVT::i32),
617 DAG.getConstant(SMPos0, MVT::i32),
618 And0.getOperand(0));
619}
620
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000621SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000622 const {
623 SelectionDAG &DAG = DCI.DAG;
624 unsigned opc = N->getOpcode();
625
626 switch (opc) {
627 default: break;
628 case ISD::ADDE:
629 return PerformADDECombine(N, DAG, DCI, Subtarget);
630 case ISD::SUBE:
631 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000632 case ISD::SDIVREM:
633 case ISD::UDIVREM:
634 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000635 case ISD::SETCC:
636 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000637 case ISD::AND:
638 return PerformANDCombine(N, DAG, DCI, Subtarget);
639 case ISD::OR:
640 return PerformORCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000641 }
642
643 return SDValue();
644}
645
Dan Gohman475871a2008-07-27 21:46:04 +0000646SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000647LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000648{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000649 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000650 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000651 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000652 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
653 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000654 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000655 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000656 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
657 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000658 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000659 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000660 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000661 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000662 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000663 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000664 }
Dan Gohman475871a2008-07-27 21:46:04 +0000665 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000666}
667
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000668//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000669// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000670//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000671
672// AddLiveIn - This helper function adds the specified physical register to the
673// MachineFunction as a live in value. It also creates a corresponding
674// virtual register for it.
675static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000676AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000677{
678 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000679 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
680 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000681 return VReg;
682}
683
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000684// Get fp branch code (not opcode) from condition code.
685static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
686 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
687 return Mips::BRANCH_T;
688
689 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
690 return Mips::BRANCH_F;
691
692 return Mips::BRANCH_INVALID;
693}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000694
Akira Hatanaka14487d42011-06-07 19:28:39 +0000695static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
696 DebugLoc dl,
697 const MipsSubtarget* Subtarget,
698 const TargetInstrInfo *TII,
699 bool isFPCmp, unsigned Opc) {
700 // There is no need to expand CMov instructions if target has
701 // conditional moves.
702 if (Subtarget->hasCondMov())
703 return BB;
704
705 // To "insert" a SELECT_CC instruction, we actually have to insert the
706 // diamond control-flow pattern. The incoming instruction knows the
707 // destination vreg to set, the condition code register to branch on, the
708 // true/false values to select between, and a branch opcode to use.
709 const BasicBlock *LLVM_BB = BB->getBasicBlock();
710 MachineFunction::iterator It = BB;
711 ++It;
712
713 // thisMBB:
714 // ...
715 // TrueVal = ...
716 // setcc r1, r2, r3
717 // bNE r1, r0, copy1MBB
718 // fallthrough --> copy0MBB
719 MachineBasicBlock *thisMBB = BB;
720 MachineFunction *F = BB->getParent();
721 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
722 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
723 F->insert(It, copy0MBB);
724 F->insert(It, sinkMBB);
725
726 // Transfer the remainder of BB and its successor edges to sinkMBB.
727 sinkMBB->splice(sinkMBB->begin(), BB,
728 llvm::next(MachineBasicBlock::iterator(MI)),
729 BB->end());
730 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
731
732 // Next, add the true and fallthrough blocks as its successors.
733 BB->addSuccessor(copy0MBB);
734 BB->addSuccessor(sinkMBB);
735
736 // Emit the right instruction according to the type of the operands compared
737 if (isFPCmp)
738 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
739 else
740 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
741 .addReg(Mips::ZERO).addMBB(sinkMBB);
742
743 // copy0MBB:
744 // %FalseValue = ...
745 // # fallthrough to sinkMBB
746 BB = copy0MBB;
747
748 // Update machine-CFG edges
749 BB->addSuccessor(sinkMBB);
750
751 // sinkMBB:
752 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
753 // ...
754 BB = sinkMBB;
755
756 if (isFPCmp)
757 BuildMI(*BB, BB->begin(), dl,
758 TII->get(Mips::PHI), MI->getOperand(0).getReg())
759 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
760 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
761 else
762 BuildMI(*BB, BB->begin(), dl,
763 TII->get(Mips::PHI), MI->getOperand(0).getReg())
764 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
765 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
766
767 MI->eraseFromParent(); // The pseudo instruction is gone now.
768 return BB;
769}
770
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000771MachineBasicBlock *
772MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000773 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000774 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen94817572009-02-13 02:34:39 +0000775 DebugLoc dl = MI->getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000776
777 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000778 default:
779 assert(false && "Unexpected instr type to insert");
780 return NULL;
781 case Mips::MOVT:
782 case Mips::MOVT_S:
783 case Mips::MOVT_D:
784 return ExpandCondMov(MI, BB, dl, Subtarget, TII, true, Mips::BC1F);
785 case Mips::MOVF:
786 case Mips::MOVF_S:
787 case Mips::MOVF_D:
788 return ExpandCondMov(MI, BB, dl, Subtarget, TII, true, Mips::BC1T);
789 case Mips::MOVZ_I:
790 case Mips::MOVZ_S:
791 case Mips::MOVZ_D:
792 return ExpandCondMov(MI, BB, dl, Subtarget, TII, false, Mips::BNE);
793 case Mips::MOVN_I:
794 case Mips::MOVN_S:
795 case Mips::MOVN_D:
796 return ExpandCondMov(MI, BB, dl, Subtarget, TII, false, Mips::BEQ);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000797
798 case Mips::ATOMIC_LOAD_ADD_I8:
799 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
800 case Mips::ATOMIC_LOAD_ADD_I16:
801 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
802 case Mips::ATOMIC_LOAD_ADD_I32:
803 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
804
805 case Mips::ATOMIC_LOAD_AND_I8:
806 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
807 case Mips::ATOMIC_LOAD_AND_I16:
808 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
809 case Mips::ATOMIC_LOAD_AND_I32:
810 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
811
812 case Mips::ATOMIC_LOAD_OR_I8:
813 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
814 case Mips::ATOMIC_LOAD_OR_I16:
815 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
816 case Mips::ATOMIC_LOAD_OR_I32:
817 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
818
819 case Mips::ATOMIC_LOAD_XOR_I8:
820 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
821 case Mips::ATOMIC_LOAD_XOR_I16:
822 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
823 case Mips::ATOMIC_LOAD_XOR_I32:
824 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
825
826 case Mips::ATOMIC_LOAD_NAND_I8:
827 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
828 case Mips::ATOMIC_LOAD_NAND_I16:
829 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
830 case Mips::ATOMIC_LOAD_NAND_I32:
831 return EmitAtomicBinary(MI, BB, 4, 0, true);
832
833 case Mips::ATOMIC_LOAD_SUB_I8:
834 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
835 case Mips::ATOMIC_LOAD_SUB_I16:
836 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
837 case Mips::ATOMIC_LOAD_SUB_I32:
838 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
839
840 case Mips::ATOMIC_SWAP_I8:
841 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
842 case Mips::ATOMIC_SWAP_I16:
843 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
844 case Mips::ATOMIC_SWAP_I32:
845 return EmitAtomicBinary(MI, BB, 4, 0);
846
847 case Mips::ATOMIC_CMP_SWAP_I8:
848 return EmitAtomicCmpSwapPartword(MI, BB, 1);
849 case Mips::ATOMIC_CMP_SWAP_I16:
850 return EmitAtomicCmpSwapPartword(MI, BB, 2);
851 case Mips::ATOMIC_CMP_SWAP_I32:
852 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000853 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000854}
855
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000856// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
857// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
858MachineBasicBlock *
859MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000860 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000861 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000862 assert(Size == 4 && "Unsupported size for EmitAtomicBinary.");
863
864 MachineFunction *MF = BB->getParent();
865 MachineRegisterInfo &RegInfo = MF->getRegInfo();
866 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
867 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
868 DebugLoc dl = MI->getDebugLoc();
869
Akira Hatanaka4061da12011-07-19 20:11:17 +0000870 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000871 unsigned Ptr = MI->getOperand(1).getReg();
872 unsigned Incr = MI->getOperand(2).getReg();
873
Akira Hatanaka4061da12011-07-19 20:11:17 +0000874 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
875 unsigned AndRes = RegInfo.createVirtualRegister(RC);
876 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000877
878 // insert new blocks after the current block
879 const BasicBlock *LLVM_BB = BB->getBasicBlock();
880 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
881 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
882 MachineFunction::iterator It = BB;
883 ++It;
884 MF->insert(It, loopMBB);
885 MF->insert(It, exitMBB);
886
887 // Transfer the remainder of BB and its successor edges to exitMBB.
888 exitMBB->splice(exitMBB->begin(), BB,
889 llvm::next(MachineBasicBlock::iterator(MI)),
890 BB->end());
891 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
892
893 // thisMBB:
894 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000895 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000896 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000897 loopMBB->addSuccessor(loopMBB);
898 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000899
900 // loopMBB:
901 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000902 // <binop> storeval, oldval, incr
903 // sc success, storeval, 0(ptr)
904 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000905 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000906 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000907 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000908 // and andres, oldval, incr
909 // nor storeval, $0, andres
910 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr);
911 BuildMI(BB, dl, TII->get(Mips::NOR), StoreVal)
912 .addReg(Mips::ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000913 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000914 // <binop> storeval, oldval, incr
915 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000916 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000917 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000918 }
Akira Hatanaka4061da12011-07-19 20:11:17 +0000919 BuildMI(BB, dl, TII->get(Mips::SC), Success)
920 .addReg(StoreVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000921 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +0000922 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000923
924 MI->eraseFromParent(); // The instruction is gone now.
925
Akira Hatanaka939ece12011-07-19 03:42:13 +0000926 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000927}
928
929MachineBasicBlock *
930MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000931 MachineBasicBlock *BB,
932 unsigned Size, unsigned BinOpcode,
933 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000934 assert((Size == 1 || Size == 2) &&
935 "Unsupported size for EmitAtomicBinaryPartial.");
936
937 MachineFunction *MF = BB->getParent();
938 MachineRegisterInfo &RegInfo = MF->getRegInfo();
939 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
940 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
941 DebugLoc dl = MI->getDebugLoc();
942
943 unsigned Dest = MI->getOperand(0).getReg();
944 unsigned Ptr = MI->getOperand(1).getReg();
945 unsigned Incr = MI->getOperand(2).getReg();
946
Akira Hatanaka4061da12011-07-19 20:11:17 +0000947 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
948 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000949 unsigned Mask = RegInfo.createVirtualRegister(RC);
950 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000951 unsigned NewVal = RegInfo.createVirtualRegister(RC);
952 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000953 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000954 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
955 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
956 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
957 unsigned AndRes = RegInfo.createVirtualRegister(RC);
958 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000959 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000960 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
961 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
962 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
963 unsigned SllRes = RegInfo.createVirtualRegister(RC);
964 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000965
966 // insert new blocks after the current block
967 const BasicBlock *LLVM_BB = BB->getBasicBlock();
968 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000969 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000970 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
971 MachineFunction::iterator It = BB;
972 ++It;
973 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000974 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000975 MF->insert(It, exitMBB);
976
977 // Transfer the remainder of BB and its successor edges to exitMBB.
978 exitMBB->splice(exitMBB->begin(), BB,
979 llvm::next(MachineBasicBlock::iterator(MI)),
980 BB->end());
981 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
982
Akira Hatanaka81b44112011-07-19 17:09:53 +0000983 BB->addSuccessor(loopMBB);
984 loopMBB->addSuccessor(loopMBB);
985 loopMBB->addSuccessor(sinkMBB);
986 sinkMBB->addSuccessor(exitMBB);
987
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000988 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000989 // addiu masklsb2,$0,-4 # 0xfffffffc
990 // and alignedaddr,ptr,masklsb2
991 // andi ptrlsb2,ptr,3
992 // sll shiftamt,ptrlsb2,3
993 // ori maskupper,$0,255 # 0xff
994 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000995 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +0000996 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000997
998 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000999 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1000 .addReg(Mips::ZERO).addImm(-4);
1001 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1002 .addReg(Ptr).addReg(MaskLSB2);
1003 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1004 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1005 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1006 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001007 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1008 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001009 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001010 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001011
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001012
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001013 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001014 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001015 // ll oldval,0(alignedaddr)
1016 // binop binopres,oldval,incr2
1017 // and newval,binopres,mask
1018 // and maskedoldval0,oldval,mask2
1019 // or storeval,maskedoldval0,newval
1020 // sc success,storeval,0(alignedaddr)
1021 // beq success,$0,loopMBB
1022
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001023 // atomic.swap
1024 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001025 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001026 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001027 // and maskedoldval0,oldval,mask2
1028 // or storeval,maskedoldval0,newval
1029 // sc success,storeval,0(alignedaddr)
1030 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001031
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001032 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001033 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001034 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001035 // and andres, oldval, incr2
1036 // nor binopres, $0, andres
1037 // and newval, binopres, mask
1038 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1039 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1040 .addReg(Mips::ZERO).addReg(AndRes);
1041 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001042 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001043 // <binop> binopres, oldval, incr2
1044 // and newval, binopres, mask
1045 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1046 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001047 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001048 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001049 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001050 }
1051
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001052 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001053 .addReg(OldVal).addReg(Mask2);
1054 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001055 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001056 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1057 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001058 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001059 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001060
Akira Hatanaka939ece12011-07-19 03:42:13 +00001061 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001062 // and maskedoldval1,oldval,mask
1063 // srl srlres,maskedoldval1,shiftamt
1064 // sll sllres,srlres,24
1065 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001066 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001067 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001068
Akira Hatanaka4061da12011-07-19 20:11:17 +00001069 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1070 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001071 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1072 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001073 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1074 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001075 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001076 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001077
1078 MI->eraseFromParent(); // The instruction is gone now.
1079
Akira Hatanaka939ece12011-07-19 03:42:13 +00001080 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001081}
1082
1083MachineBasicBlock *
1084MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001085 MachineBasicBlock *BB,
1086 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001087 assert(Size == 4 && "Unsupported size for EmitAtomicCmpSwap.");
1088
1089 MachineFunction *MF = BB->getParent();
1090 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1091 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1092 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1093 DebugLoc dl = MI->getDebugLoc();
1094
1095 unsigned Dest = MI->getOperand(0).getReg();
1096 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001097 unsigned OldVal = MI->getOperand(2).getReg();
1098 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001099
Akira Hatanaka4061da12011-07-19 20:11:17 +00001100 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001101
1102 // insert new blocks after the current block
1103 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1104 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1105 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1106 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1107 MachineFunction::iterator It = BB;
1108 ++It;
1109 MF->insert(It, loop1MBB);
1110 MF->insert(It, loop2MBB);
1111 MF->insert(It, exitMBB);
1112
1113 // Transfer the remainder of BB and its successor edges to exitMBB.
1114 exitMBB->splice(exitMBB->begin(), BB,
1115 llvm::next(MachineBasicBlock::iterator(MI)),
1116 BB->end());
1117 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1118
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001119 // thisMBB:
1120 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001121 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001122 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001123 loop1MBB->addSuccessor(exitMBB);
1124 loop1MBB->addSuccessor(loop2MBB);
1125 loop2MBB->addSuccessor(loop1MBB);
1126 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001127
1128 // loop1MBB:
1129 // ll dest, 0(ptr)
1130 // bne dest, oldval, exitMBB
1131 BB = loop1MBB;
Akira Hatanakad3ac47f2011-07-07 18:57:00 +00001132 BuildMI(BB, dl, TII->get(Mips::LL), Dest).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001133 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001134 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001135
1136 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001137 // sc success, newval, 0(ptr)
1138 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001139 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001140 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1141 .addReg(NewVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001142 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001143 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001144
1145 MI->eraseFromParent(); // The instruction is gone now.
1146
Akira Hatanaka939ece12011-07-19 03:42:13 +00001147 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001148}
1149
1150MachineBasicBlock *
1151MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001152 MachineBasicBlock *BB,
1153 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001154 assert((Size == 1 || Size == 2) &&
1155 "Unsupported size for EmitAtomicCmpSwapPartial.");
1156
1157 MachineFunction *MF = BB->getParent();
1158 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1159 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1160 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1161 DebugLoc dl = MI->getDebugLoc();
1162
1163 unsigned Dest = MI->getOperand(0).getReg();
1164 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001165 unsigned CmpVal = MI->getOperand(2).getReg();
1166 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001167
Akira Hatanaka4061da12011-07-19 20:11:17 +00001168 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1169 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001170 unsigned Mask = RegInfo.createVirtualRegister(RC);
1171 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001172 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1173 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1174 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1175 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1176 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1177 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1178 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1179 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1180 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1181 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1182 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1183 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1184 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1185 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001186
1187 // insert new blocks after the current block
1188 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1189 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1190 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001191 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001192 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1193 MachineFunction::iterator It = BB;
1194 ++It;
1195 MF->insert(It, loop1MBB);
1196 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001197 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001198 MF->insert(It, exitMBB);
1199
1200 // Transfer the remainder of BB and its successor edges to exitMBB.
1201 exitMBB->splice(exitMBB->begin(), BB,
1202 llvm::next(MachineBasicBlock::iterator(MI)),
1203 BB->end());
1204 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1205
Akira Hatanaka81b44112011-07-19 17:09:53 +00001206 BB->addSuccessor(loop1MBB);
1207 loop1MBB->addSuccessor(sinkMBB);
1208 loop1MBB->addSuccessor(loop2MBB);
1209 loop2MBB->addSuccessor(loop1MBB);
1210 loop2MBB->addSuccessor(sinkMBB);
1211 sinkMBB->addSuccessor(exitMBB);
1212
Akira Hatanaka70564a92011-07-19 18:14:26 +00001213 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001214 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001215 // addiu masklsb2,$0,-4 # 0xfffffffc
1216 // and alignedaddr,ptr,masklsb2
1217 // andi ptrlsb2,ptr,3
1218 // sll shiftamt,ptrlsb2,3
1219 // ori maskupper,$0,255 # 0xff
1220 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001221 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001222 // andi maskedcmpval,cmpval,255
1223 // sll shiftedcmpval,maskedcmpval,shiftamt
1224 // andi maskednewval,newval,255
1225 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001226 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001227 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1228 .addReg(Mips::ZERO).addImm(-4);
1229 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1230 .addReg(Ptr).addReg(MaskLSB2);
1231 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1232 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1233 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1234 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001235 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1236 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001237 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001238 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1239 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001240 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1241 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001242 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1243 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001244 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1245 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001246
1247 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001248 // ll oldval,0(alginedaddr)
1249 // and maskedoldval0,oldval,mask
1250 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001251 BB = loop1MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001252 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
1253 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1254 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001255 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001256 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001257
1258 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001259 // and maskedoldval1,oldval,mask2
1260 // or storeval,maskedoldval1,shiftednewval
1261 // sc success,storeval,0(alignedaddr)
1262 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001263 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001264 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1265 .addReg(OldVal).addReg(Mask2);
1266 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1267 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1268 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1269 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001270 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001271 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001272
Akira Hatanaka939ece12011-07-19 03:42:13 +00001273 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001274 // srl srlres,maskedoldval0,shiftamt
1275 // sll sllres,srlres,24
1276 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001277 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001278 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001279
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001280 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1281 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001282 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1283 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001284 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001285 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001286
1287 MI->eraseFromParent(); // The instruction is gone now.
1288
Akira Hatanaka939ece12011-07-19 03:42:13 +00001289 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001290}
1291
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001292//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001293// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001294//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001295SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001296LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001297{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001298 MachineFunction &MF = DAG.getMachineFunction();
1299 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1300
1301 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001302 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1303 "Cannot lower if the alignment of the allocated space is larger than \
1304 that of the stack.");
1305
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001306 SDValue Chain = Op.getOperand(0);
1307 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001308 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001309
1310 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +00001311 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001312
1313 // Subtract the dynamic size from the actual stack size to
1314 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +00001315 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001316
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001317 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001318 // must be placed in the stack pointer register.
Akira Hatanaka053546c2011-05-25 02:20:00 +00001319 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
1320 SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001321
1322 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001323 // value and a chain
Akira Hatanaka21afc632011-06-21 00:40:49 +00001324 SDVTList VTLs = DAG.getVTList(MVT::i32, MVT::Other);
1325 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1326 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1327
1328 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001329}
1330
1331SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001332LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001333{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001334 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001335 // the block to branch to if the condition is true.
1336 SDValue Chain = Op.getOperand(0);
1337 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001338 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001339
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001340 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1341
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001342 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001343 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001344 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001345
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001346 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001347 Mips::CondCode CC =
1348 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001349 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001350
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001351 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001352 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001353}
1354
1355SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001356LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001357{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001358 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001359
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001360 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001361 if (Cond.getOpcode() != MipsISD::FPCmp)
1362 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001363
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001364 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1365 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001366}
1367
Dan Gohmand858e902010-04-17 15:26:15 +00001368SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1369 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001370 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001371 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001372 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001373
Eli Friedmane2c74082009-08-03 02:22:28 +00001374 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001375 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001376
Chris Lattnerb71b9092009-08-13 06:28:06 +00001377 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001378
Chris Lattnere3736f82009-08-13 05:41:27 +00001379 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001380 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1381 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001382 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001383 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1384 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001385 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001386 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001387 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001388 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1389 MipsII::MO_ABS_HI);
1390 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1391 MipsII::MO_ABS_LO);
1392 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1393 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001394 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001395 }
1396
Akira Hatanaka0f843822011-06-07 18:58:42 +00001397 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1398 MipsII::MO_GOT);
1399 GA = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, GA);
1400 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
1401 DAG.getEntryNode(), GA, MachinePointerInfo(),
1402 false, false, 0);
1403 // On functions and global targets not internal linked only
1404 // a load from got/GP is necessary for PIC to work.
1405 if (!GV->hasInternalLinkage() &&
1406 (!GV->hasLocalLinkage() || isa<Function>(GV)))
1407 return ResNode;
1408 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1409 MipsII::MO_ABS_LO);
1410 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
1411 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001412}
1413
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001414SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1415 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001416 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1417 // FIXME there isn't actually debug info here
1418 DebugLoc dl = Op.getDebugLoc();
1419
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001420 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001421 // %hi/%lo relocation
1422 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1423 MipsII::MO_ABS_HI);
1424 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1425 MipsII::MO_ABS_LO);
1426 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1427 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1428 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001429 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001430
1431 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1432 MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001433 BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, BAGOTOffset);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001434 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1435 MipsII::MO_ABS_LO);
1436 SDValue Load = DAG.getLoad(MVT::i32, dl,
1437 DAG.getEntryNode(), BAGOTOffset,
1438 MachinePointerInfo(), false, false, 0);
1439 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
1440 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001441}
1442
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001443SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001444LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001445{
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001446 // If the relocation model is PIC, use the General Dynamic TLS Model,
1447 // otherwise use the Initial Exec or Local Exec TLS Model.
1448 // TODO: implement Local Dynamic TLS model
1449
1450 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1451 DebugLoc dl = GA->getDebugLoc();
1452 const GlobalValue *GV = GA->getGlobal();
1453 EVT PtrVT = getPointerTy();
1454
1455 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1456 // General Dynamic TLS Model
1457 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001458 0, MipsII::MO_TLSGD);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001459 SDValue Tlsgd = DAG.getNode(MipsISD::TlsGd, dl, MVT::i32, TGA);
1460 SDValue GP = DAG.getRegister(Mips::GP, MVT::i32);
1461 SDValue Argument = DAG.getNode(ISD::ADD, dl, MVT::i32, GP, Tlsgd);
1462
1463 ArgListTy Args;
1464 ArgListEntry Entry;
1465 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001466 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001467 Args.push_back(Entry);
1468 std::pair<SDValue, SDValue> CallResult =
1469 LowerCallTo(DAG.getEntryNode(),
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001470 (Type *) Type::getInt32Ty(*DAG.getContext()),
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001471 false, false, false, false, 0, CallingConv::C, false, true,
1472 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG,
1473 dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001474
1475 return CallResult.first;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001476 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001477
1478 SDValue Offset;
1479 if (GV->isDeclaration()) {
1480 // Initial Exec TLS Model
1481 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1482 MipsII::MO_GOTTPREL);
1483 Offset = DAG.getLoad(MVT::i32, dl,
1484 DAG.getEntryNode(), TGA, MachinePointerInfo(),
1485 false, false, 0);
1486 } else {
1487 // Local Exec TLS Model
1488 SDVTList VTs = DAG.getVTList(MVT::i32);
1489 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1490 MipsII::MO_TPREL_HI);
1491 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1492 MipsII::MO_TPREL_LO);
1493 SDValue Hi = DAG.getNode(MipsISD::TprelHi, dl, VTs, &TGAHi, 1);
1494 SDValue Lo = DAG.getNode(MipsISD::TprelLo, dl, MVT::i32, TGALo);
1495 Offset = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1496 }
1497
1498 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1499 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001500}
1501
1502SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001503LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001504{
Dan Gohman475871a2008-07-27 21:46:04 +00001505 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001506 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +00001507 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001508 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001509 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001510 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001511
Owen Andersone50ed302009-08-10 22:56:29 +00001512 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001513 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001514
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001515 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
1516
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +00001517 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001518 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001519 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001520 } else {// Emit Load from Global Pointer
1521 JTI = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, JTI);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001522 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
1523 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001524 false, false, 0);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001525 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001526
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001527 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1528 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001529 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001530 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001531
1532 return ResNode;
1533}
1534
Dan Gohman475871a2008-07-27 21:46:04 +00001535SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001536LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001537{
Dan Gohman475871a2008-07-27 21:46:04 +00001538 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001539 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001540 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001541 // FIXME there isn't actually debug info here
1542 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001543
1544 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001545 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001546 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001547 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001548 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001549 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001550 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1551 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001552 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001553
1554 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001555 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001556 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001557 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001558 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001559 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1560 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001561 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001562 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001563 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001564 N->getOffset(), MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001565 CP = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001566 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001567 CP, MachinePointerInfo::getConstantPool(),
1568 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001569 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001570 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001571 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001572 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
1573 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001574
1575 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001576}
1577
Dan Gohmand858e902010-04-17 15:26:15 +00001578SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001579 MachineFunction &MF = DAG.getMachineFunction();
1580 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1581
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001582 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001583 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1584 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001585
1586 // vastart just stores the address of the VarArgsFrameIndex slot into the
1587 // memory location argument.
1588 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001589 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1590 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +00001591 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001592}
1593
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001594static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
1595 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
1596 DebugLoc dl = Op.getDebugLoc();
1597 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
1598 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
1599 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
1600 DAG.getConstant(0x7fffffff, MVT::i32));
1601 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
1602 DAG.getConstant(0x80000000, MVT::i32));
1603 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1604 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
1605}
1606
1607static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001608 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001609 // Use ext/ins instructions if target architecture is Mips32r2.
1610 // Eliminate redundant mfc1 and mtc1 instructions.
1611 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001612
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001613 if (!isLittle)
1614 std::swap(LoIdx, HiIdx);
1615
1616 DebugLoc dl = Op.getDebugLoc();
1617 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1618 Op.getOperand(0),
1619 DAG.getConstant(LoIdx, MVT::i32));
1620 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1621 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1622 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1623 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1624 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1625 DAG.getConstant(0x7fffffff, MVT::i32));
1626 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1627 DAG.getConstant(0x80000000, MVT::i32));
1628 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1629
1630 if (!isLittle)
1631 std::swap(Word0, Word1);
1632
1633 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1634}
1635
1636SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1637 const {
1638 EVT Ty = Op.getValueType();
1639
1640 assert(Ty == MVT::f32 || Ty == MVT::f64);
1641
1642 if (Ty == MVT::f32)
1643 return LowerFCOPYSIGN32(Op, DAG);
1644 else
1645 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
1646}
1647
Akira Hatanaka2e591472011-06-02 00:24:44 +00001648SDValue MipsTargetLowering::
1649LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001650 // check the depth
1651 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001652 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001653
1654 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1655 MFI->setFrameAddressIsTaken(true);
1656 EVT VT = Op.getValueType();
1657 DebugLoc dl = Op.getDebugLoc();
1658 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Mips::FP, VT);
1659 return FrameAddr;
1660}
1661
Akira Hatanakadb548262011-07-19 23:30:50 +00001662// TODO: set SType according to the desired memory barrier behavior.
1663SDValue MipsTargetLowering::LowerMEMBARRIER(SDValue Op,
1664 SelectionDAG& DAG) const {
1665 unsigned SType = 0;
1666 DebugLoc dl = Op.getDebugLoc();
1667 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1668 DAG.getConstant(SType, MVT::i32));
1669}
1670
Eli Friedman14648462011-07-27 22:21:52 +00001671SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1672 SelectionDAG& DAG) const {
1673 // FIXME: Need pseudo-fence for 'singlethread' fences
1674 // FIXME: Set SType for weaker fences where supported/appropriate.
1675 unsigned SType = 0;
1676 DebugLoc dl = Op.getDebugLoc();
1677 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1678 DAG.getConstant(SType, MVT::i32));
1679}
1680
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001681//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001682// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001683//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001684
1685#include "MipsGenCallingConv.inc"
1686
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001687//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001688// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001689// Mips O32 ABI rules:
1690// ---
1691// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001692// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001693// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001694// f64 - Only passed in two aliased f32 registers if no int reg has been used
1695// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001696// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1697// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001698//
1699// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001700//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001701
Duncan Sands1e96bab2010-11-04 10:49:57 +00001702static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001703 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001704 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1705
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001706 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001707
1708 static const unsigned IntRegs[] = {
1709 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1710 };
1711 static const unsigned F32Regs[] = {
1712 Mips::F12, Mips::F14
1713 };
1714 static const unsigned F64Regs[] = {
1715 Mips::D6, Mips::D7
1716 };
1717
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001718 // ByVal Args
1719 if (ArgFlags.isByVal()) {
1720 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1721 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1722 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1723 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1724 r < std::min(IntRegsSize, NextReg); ++r)
1725 State.AllocateReg(IntRegs[r]);
1726 return false;
1727 }
1728
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001729 // Promote i8 and i16
1730 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1731 LocVT = MVT::i32;
1732 if (ArgFlags.isSExt())
1733 LocInfo = CCValAssign::SExt;
1734 else if (ArgFlags.isZExt())
1735 LocInfo = CCValAssign::ZExt;
1736 else
1737 LocInfo = CCValAssign::AExt;
1738 }
1739
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001740 unsigned Reg;
1741
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001742 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1743 // is true: function is vararg, argument is 3rd or higher, there is previous
1744 // argument which is not f32 or f64.
1745 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1746 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001747 unsigned OrigAlign = ArgFlags.getOrigAlign();
1748 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001749
1750 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001751 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001752 // If this is the first part of an i64 arg,
1753 // the allocated register must be either A0 or A2.
1754 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1755 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001756 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001757 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1758 // Allocate int register and shadow next int register. If first
1759 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001760 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1761 if (Reg == Mips::A1 || Reg == Mips::A3)
1762 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1763 State.AllocateReg(IntRegs, IntRegsSize);
1764 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001765 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1766 // we are guaranteed to find an available float register
1767 if (ValVT == MVT::f32) {
1768 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1769 // Shadow int register
1770 State.AllocateReg(IntRegs, IntRegsSize);
1771 } else {
1772 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1773 // Shadow int registers
1774 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1775 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1776 State.AllocateReg(IntRegs, IntRegsSize);
1777 State.AllocateReg(IntRegs, IntRegsSize);
1778 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001779 } else
1780 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001781
Akira Hatanakad37776d2011-05-20 21:39:54 +00001782 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1783 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1784
1785 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001786 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001787 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001788 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001789
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001790 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001791}
1792
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001793//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001794// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001795//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001796
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001797static const unsigned O32IntRegsSize = 4;
1798
1799static const unsigned O32IntRegs[] = {
1800 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1801};
1802
1803// Write ByVal Arg to arg registers and stack.
1804static void
1805WriteByValArg(SDValue& Chain, DebugLoc dl,
1806 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1807 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1808 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001809 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
1810 MVT PtrType) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001811 unsigned FirstWord = VA.getLocMemOffset() / 4;
1812 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
1813 unsigned LastWord = FirstWord + NumWords;
1814 unsigned CurWord;
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001815 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001816
1817 // copy the first 4 words of byval arg to registers A0 - A3
1818 for (CurWord = FirstWord; CurWord < std::min(LastWord, O32IntRegsSize);
1819 ++CurWord) {
1820 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1821 DAG.getConstant((CurWord - FirstWord) * 4,
1822 MVT::i32));
1823 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1824 MachinePointerInfo(),
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001825 false, false, std::min(ByValAlign,
1826 (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001827 MemOpChains.push_back(LoadVal.getValue(1));
1828 unsigned DstReg = O32IntRegs[CurWord];
1829 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1830 }
1831
1832 // copy remaining part of byval arg to stack.
1833 if (CurWord < LastWord) {
Eric Christopher471e4222011-06-08 23:55:35 +00001834 unsigned SizeInBytes = (LastWord - CurWord) * 4;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001835 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1836 DAG.getConstant((CurWord - FirstWord) * 4,
1837 MVT::i32));
1838 LastFI = MFI->CreateFixedObject(SizeInBytes, CurWord * 4, true);
1839 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
1840 Chain = DAG.getMemcpy(Chain, dl, Dst, Src,
1841 DAG.getConstant(SizeInBytes, MVT::i32),
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001842 /*Align*/ByValAlign,
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001843 /*isVolatile=*/false, /*AlwaysInline=*/false,
1844 MachinePointerInfo(0), MachinePointerInfo(0));
1845 MemOpChains.push_back(Chain);
1846 }
1847}
1848
Dan Gohman98ca4f22009-08-05 01:29:28 +00001849/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001850/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001851/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001852SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001853MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001854 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001855 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001856 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001857 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001858 const SmallVectorImpl<ISD::InputArg> &Ins,
1859 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001860 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001861 // MIPs target does not yet support tail call optimization.
1862 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001863
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001864 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001865 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00001866 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001867 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001868 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001869
1870 // Analyze operands of the call, assigning locations to each operand.
1871 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001872 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1873 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001874
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001875 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001876 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001877 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001878 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001879
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001880 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001881 unsigned NextStackOffset = CCInfo.getNextStackOffset();
1882
1883 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NextStackOffset,
1884 true));
1885
1886 // If this is the first call, create a stack frame object that points to
1887 // a location to which .cprestore saves $gp.
1888 if (IsPIC && !MipsFI->getGPFI())
1889 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1890
Akira Hatanaka21afc632011-06-21 00:40:49 +00001891 // Get the frame index of the stack frame object that points to the location
1892 // of dynamically allocated area on the stack.
1893 int DynAllocFI = MipsFI->getDynAllocFI();
1894
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001895 // Update size of the maximum argument space.
1896 // For O32, a minimum of four words (16 bytes) of argument space is
1897 // allocated.
1898 if (Subtarget->isABI_O32())
1899 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
1900
1901 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1902
1903 if (MaxCallFrameSize < NextStackOffset) {
1904 MipsFI->setMaxCallFrameSize(NextStackOffset);
1905
Akira Hatanaka21afc632011-06-21 00:40:49 +00001906 // Set the offsets relative to $sp of the $gp restore slot and dynamically
1907 // allocated stack space. These offsets must be aligned to a boundary
1908 // determined by the stack alignment of the ABI.
1909 unsigned StackAlignment = TFL->getStackAlignment();
1910 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
1911 StackAlignment * StackAlignment;
1912
1913 if (IsPIC)
1914 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
1915
1916 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001917 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001918
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001919 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001920 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1921 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001922
Eric Christopher471e4222011-06-08 23:55:35 +00001923 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00001924
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001925 // Walk the register/memloc assignments, inserting copies/loads.
1926 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001927 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001928 CCValAssign &VA = ArgLocs[i];
1929
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001930 // Promote the value if needed.
1931 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001932 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001933 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001934 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001935 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001936 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001937 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001938 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1939 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001940 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1941 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001942 if (!Subtarget->isLittle())
1943 std::swap(Lo, Hi);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001944 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
1945 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
1946 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001947 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001948 }
1949 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00001950 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001951 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001952 break;
1953 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001954 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001955 break;
1956 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001957 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001958 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001959 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001960
1961 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001962 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001963 if (VA.isRegLoc()) {
1964 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00001965 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001966 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001967
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001968 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00001969 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001970
Eric Christopher471e4222011-06-08 23:55:35 +00001971 // ByVal Arg.
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001972 ISD::ArgFlagsTy Flags = Outs[i].Flags;
1973 if (Flags.isByVal()) {
1974 assert(Subtarget->isABI_O32() &&
1975 "No support for ByVal args by ABIs other than O32 yet.");
1976 assert(Flags.getByValSize() &&
1977 "ByVal args of size 0 should have been ignored by front-end.");
1978 WriteByValArg(Chain, dl, RegsToPass, MemOpChains, LastFI, MFI, DAG, Arg,
1979 VA, Flags, getPointerTy());
1980 continue;
1981 }
1982
Chris Lattnere0b12152008-03-17 06:57:02 +00001983 // Create the frame index object for this incoming parameter
Eric Christopher471e4222011-06-08 23:55:35 +00001984 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001985 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00001986 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00001987
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001988 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00001989 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00001990 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
1991 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001992 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001993 }
1994
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001995 // Extend range of indices of frame objects for outgoing arguments that were
1996 // created during this function call. Skip this step if no such objects were
1997 // created.
1998 if (LastFI)
1999 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2000
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002001 // Transform all store nodes into one single node because all store
2002 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002003 if (!MemOpChains.empty())
2004 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002005 &MemOpChains[0], MemOpChains.size());
2006
Bill Wendling056292f2008-09-16 21:48:12 +00002007 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002008 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2009 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002010 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002011 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002012 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002013
2014 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002015 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
2016 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2017 getPointerTy(), 0,MipsII:: MO_GOT);
2018 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
2019 0, MipsII::MO_ABS_LO);
2020 } else {
2021 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2022 getPointerTy(), 0, OpFlag);
2023 }
2024
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002025 LoadSymAddr = true;
2026 }
2027 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002028 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002029 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002030 LoadSymAddr = true;
2031 }
2032
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002033 SDValue InFlag;
2034
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002035 // Create nodes that load address of callee and copy it to T9
2036 if (IsPIC) {
2037 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002038 // Load callee address
Akira Hatanaka342837d2011-05-28 01:07:07 +00002039 Callee = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, Callee);
Akira Hatanaka25eba392011-06-24 19:01:25 +00002040 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), Callee,
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002041 MachinePointerInfo::getGOT(),
2042 false, false, 0);
2043
2044 // Use GOT+LO if callee has internal linkage.
2045 if (CalleeLo.getNode()) {
2046 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
2047 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
2048 } else
2049 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002050 }
2051
2052 // copy to T9
2053 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
2054 InFlag = Chain.getValue(1);
2055 Callee = DAG.getRegister(Mips::T9, MVT::i32);
2056 }
Bill Wendling056292f2008-09-16 21:48:12 +00002057
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002058 // Build a sequence of copy-to-reg nodes chained together with token
2059 // chain and flag operands which copy the outgoing args into registers.
2060 // The InFlag in necessary since all emitted instructions must be
2061 // stuck together.
2062 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2063 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2064 RegsToPass[i].second, InFlag);
2065 InFlag = Chain.getValue(1);
2066 }
2067
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002068 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002069 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002070 //
2071 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002072 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002073 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002074 Ops.push_back(Chain);
2075 Ops.push_back(Callee);
2076
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002077 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002078 // known live into the call.
2079 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2080 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2081 RegsToPass[i].second.getValueType()));
2082
Gabor Greifba36cb52008-08-28 21:40:38 +00002083 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002084 Ops.push_back(InFlag);
2085
Dale Johannesen33c960f2009-02-04 20:06:27 +00002086 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002087 InFlag = Chain.getValue(1);
2088
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002089 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002090 Chain = DAG.getCALLSEQ_END(Chain,
2091 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002092 DAG.getIntPtrConstant(0, true), InFlag);
2093 InFlag = Chain.getValue(1);
2094
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002095 // Handle result values, copying them out of physregs into vregs that we
2096 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002097 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2098 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002099}
2100
Dan Gohman98ca4f22009-08-05 01:29:28 +00002101/// LowerCallResult - Lower the result values of a call into the
2102/// appropriate copies out of appropriate physical registers.
2103SDValue
2104MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002105 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002106 const SmallVectorImpl<ISD::InputArg> &Ins,
2107 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002108 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002109 // Assign locations to each value returned by this call.
2110 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002111 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2112 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002113
Dan Gohman98ca4f22009-08-05 01:29:28 +00002114 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002115
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002116 // Copy all of the result registers out of their specified physreg.
2117 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002118 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002119 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002120 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002121 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002122 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002123
Dan Gohman98ca4f22009-08-05 01:29:28 +00002124 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002125}
2126
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002127//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002128// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002129//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002130static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2131 std::vector<SDValue>& OutChains,
2132 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2133 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2134 unsigned LocMem = VA.getLocMemOffset();
2135 unsigned FirstWord = LocMem / 4;
2136
2137 // copy register A0 - A3 to frame object
2138 for (unsigned i = 0; i < NumWords; ++i) {
2139 unsigned CurWord = FirstWord + i;
2140 if (CurWord >= O32IntRegsSize)
2141 break;
2142
2143 unsigned SrcReg = O32IntRegs[CurWord];
2144 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2145 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2146 DAG.getConstant(i * 4, MVT::i32));
2147 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2148 StorePtr, MachinePointerInfo(), false,
2149 false, 0);
2150 OutChains.push_back(Store);
2151 }
2152}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002153
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002154/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002155/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002156SDValue
2157MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002158 CallingConv::ID CallConv,
2159 bool isVarArg,
2160 const SmallVectorImpl<ISD::InputArg>
2161 &Ins,
2162 DebugLoc dl, SelectionDAG &DAG,
2163 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002164 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002165 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002166 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002167 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002168
Dan Gohman1e93df62010-04-17 14:41:14 +00002169 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002170
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002171 // Used with vargs to acumulate store chains.
2172 std::vector<SDValue> OutChains;
2173
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002174 // Assign locations to all of the incoming arguments.
2175 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002176 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2177 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002178
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002179 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002180 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002181 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002182 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002183
Akira Hatanaka43299772011-05-20 23:22:14 +00002184 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002185
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002186 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002187 CCValAssign &VA = ArgLocs[i];
2188
2189 // Arguments stored on registers
2190 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002191 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002192 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002193 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002194
Owen Anderson825b72b2009-08-11 20:47:22 +00002195 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002196 RC = Mips::CPURegsRegisterClass;
2197 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002198 RC = Mips::FGR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002199 else if (RegVT == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002200 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002201 RC = Mips::AFGR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002202 } else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002203 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002204
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002205 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002206 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002207 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002208 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002209
2210 // If this is an 8 or 16-bit value, it has been passed promoted
2211 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002212 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002213 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002214 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002215 if (VA.getLocInfo() == CCValAssign::SExt)
2216 Opcode = ISD::AssertSext;
2217 else if (VA.getLocInfo() == CCValAssign::ZExt)
2218 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002219 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002220 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00002221 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00002222 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002223 }
2224
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002225 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002226 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002227 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
2228 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00002229 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002230 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002231 VA.getLocReg()+1, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002232 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002233 if (!Subtarget->isLittle())
2234 std::swap(ArgValue, ArgValue2);
2235 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2236 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002237 }
2238 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002239
Dan Gohman98ca4f22009-08-05 01:29:28 +00002240 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002241 } else { // VA.isRegLoc()
2242
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002243 // sanity check
2244 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002245
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002246 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2247
2248 if (Flags.isByVal()) {
2249 assert(Subtarget->isABI_O32() &&
2250 "No support for ByVal args by ABIs other than O32 yet.");
2251 assert(Flags.getByValSize() &&
2252 "ByVal args of size 0 should have been ignored by front-end.");
2253 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2254 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2255 true);
2256 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2257 InVals.push_back(FIN);
2258 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2259
2260 continue;
2261 }
2262
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002263 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002264 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
2265 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002266
2267 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002268 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002269 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002270 MachinePointerInfo::getFixedStack(LastFI),
David Greenef6fa1862010-02-15 16:56:10 +00002271 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002272 }
2273 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002274
2275 // The mips ABIs for returning structs by value requires that we copy
2276 // the sret argument into $v0 for the return. Save the argument into
2277 // a virtual register so that we can access it from the return points.
2278 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2279 unsigned Reg = MipsFI->getSRetReturnReg();
2280 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002281 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002282 MipsFI->setSRetReturnReg(Reg);
2283 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002284 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002285 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002286 }
2287
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002288 if (isVarArg && Subtarget->isABI_O32()) {
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002289 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002290 // which is a value necessary to VASTART.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002291 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002292 assert(NextStackOffset % 4 == 0 &&
2293 "NextStackOffset must be aligned to 4-byte boundaries.");
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002294 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
2295 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002296
2297 // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
2298 // copy the integer registers that have not been used for argument passing
2299 // to the caller's stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002300 for (; NextStackOffset < 16; NextStackOffset += 4) {
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002301 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002302 unsigned Idx = NextStackOffset / 4;
2303 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
2304 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Akira Hatanaka69c19f72011-05-23 20:16:59 +00002305 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002306 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2307 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2308 MachinePointerInfo(),
2309 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002310 }
2311 }
2312
Akira Hatanaka43299772011-05-20 23:22:14 +00002313 MipsFI->setLastInArgFI(LastFI);
2314
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002315 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002316 // the size of Ins and InVals. This only happens when on varg functions
2317 if (!OutChains.empty()) {
2318 OutChains.push_back(Chain);
2319 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2320 &OutChains[0], OutChains.size());
2321 }
2322
Dan Gohman98ca4f22009-08-05 01:29:28 +00002323 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002324}
2325
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002326//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002327// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002328//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002329
Dan Gohman98ca4f22009-08-05 01:29:28 +00002330SDValue
2331MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002332 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002333 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002334 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002335 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002336
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002337 // CCValAssign - represent the assignment of
2338 // the return value to a location
2339 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002340
2341 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002342 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2343 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002344
Dan Gohman98ca4f22009-08-05 01:29:28 +00002345 // Analize return values.
2346 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002347
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002348 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002349 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002350 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002351 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002352 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002353 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002354 }
2355
Dan Gohman475871a2008-07-27 21:46:04 +00002356 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002357
2358 // Copy the result values into the output registers.
2359 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2360 CCValAssign &VA = RVLocs[i];
2361 assert(VA.isRegLoc() && "Can only return in registers!");
2362
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002363 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002364 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002365
2366 // guarantee that all emitted copies are
2367 // stuck together, avoiding something bad
2368 Flag = Chain.getValue(1);
2369 }
2370
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002371 // The mips ABIs for returning structs by value requires that we copy
2372 // the sret argument into $v0 for the return. We saved the argument into
2373 // a virtual register in the entry block, so now we copy the value out
2374 // and into $v0.
2375 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2376 MachineFunction &MF = DAG.getMachineFunction();
2377 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2378 unsigned Reg = MipsFI->getSRetReturnReg();
2379
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002380 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002381 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002382 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002383
Dale Johannesena05dca42009-02-04 23:02:30 +00002384 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002385 Flag = Chain.getValue(1);
2386 }
2387
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002388 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002389 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002390 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002391 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002392 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002393 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002394 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002395}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002396
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002397//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002398// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002399//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002400
2401/// getConstraintType - Given a constraint letter, return the type of
2402/// constraint it is for this target.
2403MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002404getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002405{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002406 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002407 // GCC config/mips/constraints.md
2408 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002409 // 'd' : An address register. Equivalent to r
2410 // unless generating MIPS16 code.
2411 // 'y' : Equivalent to r; retained for
2412 // backwards compatibility.
2413 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002414 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002415 switch (Constraint[0]) {
2416 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002417 case 'd':
2418 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002419 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002420 return C_RegisterClass;
2421 break;
2422 }
2423 }
2424 return TargetLowering::getConstraintType(Constraint);
2425}
2426
John Thompson44ab89e2010-10-29 17:29:13 +00002427/// Examine constraint type and operand type and determine a weight value.
2428/// This object must already have been set up with the operand type
2429/// and the current alternative constraint selected.
2430TargetLowering::ConstraintWeight
2431MipsTargetLowering::getSingleConstraintMatchWeight(
2432 AsmOperandInfo &info, const char *constraint) const {
2433 ConstraintWeight weight = CW_Invalid;
2434 Value *CallOperandVal = info.CallOperandVal;
2435 // If we don't have a value, we can't do a match,
2436 // but allow it at the lowest weight.
2437 if (CallOperandVal == NULL)
2438 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002439 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002440 // Look at the constraint type.
2441 switch (*constraint) {
2442 default:
2443 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2444 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002445 case 'd':
2446 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002447 if (type->isIntegerTy())
2448 weight = CW_Register;
2449 break;
2450 case 'f':
2451 if (type->isFloatTy())
2452 weight = CW_Register;
2453 break;
2454 }
2455 return weight;
2456}
2457
Eric Christopher38d64262011-06-29 19:33:04 +00002458/// Given a register class constraint, like 'r', if this corresponds directly
2459/// to an LLVM register class, return a register of 0 and the register class
2460/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002461std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002462getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002463{
2464 if (Constraint.size() == 1) {
2465 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002466 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2467 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002468 case 'r':
2469 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002470 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002471 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002472 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002473 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002474 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2475 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002476 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002477 }
2478 }
2479 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2480}
2481
Dan Gohman6520e202008-10-18 02:06:02 +00002482bool
2483MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2484 // The Mips target isn't yet aware of offsets.
2485 return false;
2486}
Evan Chengeb2f9692009-10-27 19:56:55 +00002487
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002488bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2489 if (VT != MVT::f32 && VT != MVT::f64)
2490 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002491 if (Imm.isNegZero())
2492 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002493 return Imm.isZero();
2494}