blob: 3e1e0090022f33390cd699396dee03792fa76b33 [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 Hatanakadbe9a312011-08-18 20:07:42 +000038// If I is a shifted mask, set the size (Size) and the first bit of the
39// mask (Pos), and return true.
Akira Hatanaka854a7db2011-08-19 22:59:00 +000040// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
41static bool IsShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
42 if (!isUInt<32>(I) || !isShiftedMask_32(I))
43 return false;
Akira Hatanakabb15e112011-08-17 02:05:42 +000044
Akira Hatanaka854a7db2011-08-19 22:59:00 +000045 Size = CountPopulation_32(I);
46 Pos = CountTrailingZeros_32(I);
Akira Hatanakadbe9a312011-08-18 20:07:42 +000047 return true;
Akira Hatanakabb15e112011-08-17 02:05:42 +000048}
49
Chris Lattnerf0144122009-07-28 03:13:23 +000050const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
51 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000052 case MipsISD::JmpLink: return "MipsISD::JmpLink";
53 case MipsISD::Hi: return "MipsISD::Hi";
54 case MipsISD::Lo: return "MipsISD::Lo";
55 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +000056 case MipsISD::TlsGd: return "MipsISD::TlsGd";
57 case MipsISD::TprelHi: return "MipsISD::TprelHi";
58 case MipsISD::TprelLo: return "MipsISD::TprelLo";
59 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000060 case MipsISD::Ret: return "MipsISD::Ret";
61 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
62 case MipsISD::FPCmp: return "MipsISD::FPCmp";
63 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
64 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
65 case MipsISD::FPRound: return "MipsISD::FPRound";
66 case MipsISD::MAdd: return "MipsISD::MAdd";
67 case MipsISD::MAddu: return "MipsISD::MAddu";
68 case MipsISD::MSub: return "MipsISD::MSub";
69 case MipsISD::MSubu: return "MipsISD::MSubu";
70 case MipsISD::DivRem: return "MipsISD::DivRem";
71 case MipsISD::DivRemU: return "MipsISD::DivRemU";
72 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
73 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanaka342837d2011-05-28 01:07:07 +000074 case MipsISD::WrapperPIC: return "MipsISD::WrapperPIC";
Akira Hatanaka21afc632011-06-21 00:40:49 +000075 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
Akira Hatanakadb548262011-07-19 23:30:50 +000076 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanakabb15e112011-08-17 02:05:42 +000077 case MipsISD::Ext: return "MipsISD::Ext";
78 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanaka0f843822011-06-07 18:58:42 +000079 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000080 }
81}
82
83MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000084MipsTargetLowering(MipsTargetMachine &TM)
Chris Lattnerb71b9092009-08-13 06:28:06 +000085 : TargetLowering(TM, new MipsTargetObjectFile()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000086 Subtarget = &TM.getSubtarget<MipsSubtarget>();
87
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000088 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000089 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000090 setBooleanContents(ZeroOrOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000091
92 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000093 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
94 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000095
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000096 // When dealing with single precision only, use libcalls
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000097 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000098 if (!Subtarget->isFP64bit())
Owen Anderson825b72b2009-08-11 20:47:22 +000099 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000100
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000101 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000102 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
103 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
104 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000105
Eli Friedman6055a6a2009-07-17 04:07:24 +0000106 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000107 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
108 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000109
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000110 // Used by legalize types to correctly generate the setcc result.
111 // Without this, every float setcc comes with a AND/OR with the result,
112 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000113 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000114 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000115
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000116 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +0000117 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000118 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000119 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
120 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
121 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
122 setOperationAction(ISD::SELECT, MVT::f32, Custom);
123 setOperationAction(ISD::SELECT, MVT::f64, Custom);
124 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
126 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000127 setOperationAction(ISD::VASTART, MVT::Other, Custom);
128
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000129 setOperationAction(ISD::SDIV, MVT::i32, Expand);
130 setOperationAction(ISD::SREM, MVT::i32, Expand);
131 setOperationAction(ISD::UDIV, MVT::i32, Expand);
132 setOperationAction(ISD::UREM, MVT::i32, Expand);
133
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000134 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
136 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
137 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
138 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
139 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
140 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
141 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
142 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
143 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000144
145 if (!Subtarget->isMips32r2())
146 setOperationAction(ISD::ROTR, MVT::i32, Expand);
147
Owen Anderson825b72b2009-08-11 20:47:22 +0000148 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
149 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
150 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000151 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
152 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000154 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000155 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000156 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
158 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000159 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 setOperationAction(ISD::FLOG, MVT::f32, Expand);
161 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
162 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
163 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000164 setOperationAction(ISD::FMA, MVT::f32, Expand);
165 setOperationAction(ISD::FMA, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000166
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000167 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
168 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000169
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000170 setOperationAction(ISD::VAARG, MVT::Other, Expand);
171 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
172 setOperationAction(ISD::VAEND, MVT::Other, Expand);
173
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000174 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000175 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
176 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000177
Akira Hatanakadb548262011-07-19 23:30:50 +0000178 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000179 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000180
Eli Friedman4db5aca2011-08-29 18:23:02 +0000181 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
182 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
183
Eli Friedman26689ac2011-08-03 21:06:02 +0000184 setInsertFencesForAtomic(true);
185
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000186 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000187 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000188
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000189 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000190 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
191 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000192 }
193
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000194 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000196
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000197 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000198 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000199
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000200 setTargetDAGCombine(ISD::ADDE);
201 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000202 setTargetDAGCombine(ISD::SDIVREM);
203 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000204 setTargetDAGCombine(ISD::SETCC);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000205 setTargetDAGCombine(ISD::AND);
206 setTargetDAGCombine(ISD::OR);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000207
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000208 setMinFunctionAlignment(2);
209
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000210 setStackPointerRegisterToSaveRestore(Mips::SP);
211 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000212
213 setExceptionPointerRegister(Mips::A0);
214 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000215}
216
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000217bool MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Akira Hatanaka511961a2011-08-17 18:49:18 +0000218 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
219 return SVT == MVT::i32 || SVT == MVT::i16;
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000220}
221
Owen Anderson825b72b2009-08-11 20:47:22 +0000222MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
223 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000224}
225
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000226// SelectMadd -
227// Transforms a subgraph in CurDAG if the following pattern is found:
228// (addc multLo, Lo0), (adde multHi, Hi0),
229// where,
230// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000231// Lo0: initial value of Lo register
232// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000233// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000234static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000235 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000236 // for the matching to be successful.
237 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
238
239 if (ADDCNode->getOpcode() != ISD::ADDC)
240 return false;
241
242 SDValue MultHi = ADDENode->getOperand(0);
243 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000244 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000245 unsigned MultOpc = MultHi.getOpcode();
246
247 // MultHi and MultLo must be generated by the same node,
248 if (MultLo.getNode() != MultNode)
249 return false;
250
251 // and it must be a multiplication.
252 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
253 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000254
255 // MultLo amd MultHi must be the first and second output of MultNode
256 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000257 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
258 return false;
259
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000260 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000261 // of the values of MultNode, in which case MultNode will be removed in later
262 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000263 // If there exist users other than ADDENode or ADDCNode, this function returns
264 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000265 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000266 // produced.
267 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
268 return false;
269
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000270 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000271 DebugLoc dl = ADDENode->getDebugLoc();
272
273 // create MipsMAdd(u) node
274 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000275
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000276 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
277 MVT::Glue,
278 MultNode->getOperand(0),// Factor 0
279 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000280 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000281 ADDENode->getOperand(1));// Hi0
282
283 // create CopyFromReg nodes
284 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
285 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000286 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000287 Mips::HI, MVT::i32,
288 CopyFromLo.getValue(2));
289
290 // replace uses of adde and addc here
291 if (!SDValue(ADDCNode, 0).use_empty())
292 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
293
294 if (!SDValue(ADDENode, 0).use_empty())
295 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
296
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000297 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000298}
299
300// SelectMsub -
301// Transforms a subgraph in CurDAG if the following pattern is found:
302// (addc Lo0, multLo), (sube Hi0, multHi),
303// where,
304// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000305// Lo0: initial value of Lo register
306// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000307// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000308static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000309 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000310 // for the matching to be successful.
311 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
312
313 if (SUBCNode->getOpcode() != ISD::SUBC)
314 return false;
315
316 SDValue MultHi = SUBENode->getOperand(1);
317 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000318 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000319 unsigned MultOpc = MultHi.getOpcode();
320
321 // MultHi and MultLo must be generated by the same node,
322 if (MultLo.getNode() != MultNode)
323 return false;
324
325 // and it must be a multiplication.
326 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
327 return false;
328
329 // MultLo amd MultHi must be the first and second output of MultNode
330 // respectively.
331 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
332 return false;
333
334 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
335 // of the values of MultNode, in which case MultNode will be removed in later
336 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000337 // If there exist users other than SUBENode or SUBCNode, this function returns
338 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000339 // instruction node rather than a pair of MULT and MSUB instructions being
340 // produced.
341 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
342 return false;
343
344 SDValue Chain = CurDAG->getEntryNode();
345 DebugLoc dl = SUBENode->getDebugLoc();
346
347 // create MipsSub(u) node
348 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
349
350 SDValue MSub = CurDAG->getNode(MultOpc, dl,
351 MVT::Glue,
352 MultNode->getOperand(0),// Factor 0
353 MultNode->getOperand(1),// Factor 1
354 SUBCNode->getOperand(0),// Lo0
355 SUBENode->getOperand(0));// Hi0
356
357 // create CopyFromReg nodes
358 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
359 MSub);
360 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
361 Mips::HI, MVT::i32,
362 CopyFromLo.getValue(2));
363
364 // replace uses of sube and subc here
365 if (!SDValue(SUBCNode, 0).use_empty())
366 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
367
368 if (!SDValue(SUBENode, 0).use_empty())
369 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
370
371 return true;
372}
373
374static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
375 TargetLowering::DAGCombinerInfo &DCI,
376 const MipsSubtarget* Subtarget) {
377 if (DCI.isBeforeLegalize())
378 return SDValue();
379
380 if (Subtarget->isMips32() && SelectMadd(N, &DAG))
381 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000382
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000383 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000384}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000385
386static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
387 TargetLowering::DAGCombinerInfo &DCI,
388 const MipsSubtarget* Subtarget) {
389 if (DCI.isBeforeLegalize())
390 return SDValue();
391
392 if (Subtarget->isMips32() && SelectMsub(N, &DAG))
393 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000394
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000395 return SDValue();
396}
397
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000398static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
399 TargetLowering::DAGCombinerInfo &DCI,
400 const MipsSubtarget* Subtarget) {
401 if (DCI.isBeforeLegalizeOps())
402 return SDValue();
403
404 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
405 MipsISD::DivRemU;
406 DebugLoc dl = N->getDebugLoc();
407
408 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
409 N->getOperand(0), N->getOperand(1));
410 SDValue InChain = DAG.getEntryNode();
411 SDValue InGlue = DivRem;
412
413 // insert MFLO
414 if (N->hasAnyUseOfValue(0)) {
415 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, Mips::LO, MVT::i32,
416 InGlue);
417 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
418 InChain = CopyFromLo.getValue(1);
419 InGlue = CopyFromLo.getValue(2);
420 }
421
422 // insert MFHI
423 if (N->hasAnyUseOfValue(1)) {
424 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000425 Mips::HI, MVT::i32, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000426 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
427 }
428
429 return SDValue();
430}
431
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000432static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
433 switch (CC) {
434 default: llvm_unreachable("Unknown fp condition code!");
435 case ISD::SETEQ:
436 case ISD::SETOEQ: return Mips::FCOND_OEQ;
437 case ISD::SETUNE: return Mips::FCOND_UNE;
438 case ISD::SETLT:
439 case ISD::SETOLT: return Mips::FCOND_OLT;
440 case ISD::SETGT:
441 case ISD::SETOGT: return Mips::FCOND_OGT;
442 case ISD::SETLE:
443 case ISD::SETOLE: return Mips::FCOND_OLE;
444 case ISD::SETGE:
445 case ISD::SETOGE: return Mips::FCOND_OGE;
446 case ISD::SETULT: return Mips::FCOND_ULT;
447 case ISD::SETULE: return Mips::FCOND_ULE;
448 case ISD::SETUGT: return Mips::FCOND_UGT;
449 case ISD::SETUGE: return Mips::FCOND_UGE;
450 case ISD::SETUO: return Mips::FCOND_UN;
451 case ISD::SETO: return Mips::FCOND_OR;
452 case ISD::SETNE:
453 case ISD::SETONE: return Mips::FCOND_ONE;
454 case ISD::SETUEQ: return Mips::FCOND_UEQ;
455 }
456}
457
458
459// Returns true if condition code has to be inverted.
460static bool InvertFPCondCode(Mips::CondCode CC) {
461 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
462 return false;
463
464 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
465 return true;
466
467 assert(false && "Illegal Condition Code");
468 return false;
469}
470
471// Creates and returns an FPCmp node from a setcc node.
472// Returns Op if setcc is not a floating point comparison.
473static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
474 // must be a SETCC node
475 if (Op.getOpcode() != ISD::SETCC)
476 return Op;
477
478 SDValue LHS = Op.getOperand(0);
479
480 if (!LHS.getValueType().isFloatingPoint())
481 return Op;
482
483 SDValue RHS = Op.getOperand(1);
484 DebugLoc dl = Op.getDebugLoc();
485
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000486 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
487 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000488 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
489
490 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
491 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
492}
493
494// Creates and returns a CMovFPT/F node.
495static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
496 SDValue False, DebugLoc DL) {
497 bool invert = InvertFPCondCode((Mips::CondCode)
498 cast<ConstantSDNode>(Cond.getOperand(2))
499 ->getSExtValue());
500
501 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
502 True.getValueType(), True, False, Cond);
503}
504
505static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
506 TargetLowering::DAGCombinerInfo &DCI,
507 const MipsSubtarget* Subtarget) {
508 if (DCI.isBeforeLegalizeOps())
509 return SDValue();
510
511 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
512
513 if (Cond.getOpcode() != MipsISD::FPCmp)
514 return SDValue();
515
516 SDValue True = DAG.getConstant(1, MVT::i32);
517 SDValue False = DAG.getConstant(0, MVT::i32);
518
519 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
520}
521
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000522static SDValue PerformANDCombine(SDNode *N, SelectionDAG& DAG,
523 TargetLowering::DAGCombinerInfo &DCI,
524 const MipsSubtarget* Subtarget) {
525 // Pattern match EXT.
526 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
527 // => ext $dst, $src, size, pos
528 if (DCI.isBeforeLegalizeOps() || !Subtarget->isMips32r2())
529 return SDValue();
530
531 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
532
533 // Op's first operand must be a shift right.
534 if (ShiftRight.getOpcode() != ISD::SRA && ShiftRight.getOpcode() != ISD::SRL)
535 return SDValue();
536
537 // The second operand of the shift must be an immediate.
538 uint64_t Pos;
539 ConstantSDNode *CN;
540 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
541 return SDValue();
542
543 Pos = CN->getZExtValue();
544
545 uint64_t SMPos, SMSize;
546 // Op's second operand must be a shifted mask.
547 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000548 !IsShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000549 return SDValue();
550
551 // Return if the shifted mask does not start at bit 0 or the sum of its size
552 // and Pos exceeds the word's size.
553 if (SMPos != 0 || Pos + SMSize > 32)
554 return SDValue();
555
556 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), MVT::i32,
557 ShiftRight.getOperand(0),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000558 DAG.getConstant(Pos, MVT::i32),
559 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000560}
561
562static SDValue PerformORCombine(SDNode *N, SelectionDAG& DAG,
563 TargetLowering::DAGCombinerInfo &DCI,
564 const MipsSubtarget* Subtarget) {
565 // Pattern match INS.
566 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
567 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
568 // => ins $dst, $src, size, pos, $src1
569 if (DCI.isBeforeLegalizeOps() || !Subtarget->isMips32r2())
570 return SDValue();
571
572 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
573 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
574 ConstantSDNode *CN;
575
576 // See if Op's first operand matches (and $src1 , mask0).
577 if (And0.getOpcode() != ISD::AND)
578 return SDValue();
579
580 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000581 !IsShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000582 return SDValue();
583
584 // See if Op's second operand matches (and (shl $src, pos), mask1).
585 if (And1.getOpcode() != ISD::AND)
586 return SDValue();
587
588 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000589 !IsShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000590 return SDValue();
591
592 // The shift masks must have the same position and size.
593 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
594 return SDValue();
595
596 SDValue Shl = And1.getOperand(0);
597 if (Shl.getOpcode() != ISD::SHL)
598 return SDValue();
599
600 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
601 return SDValue();
602
603 unsigned Shamt = CN->getZExtValue();
604
605 // Return if the shift amount and the first bit position of mask are not the
606 // same.
607 if (Shamt != SMPos0)
608 return SDValue();
609
610 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), MVT::i32,
611 Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000612 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000613 DAG.getConstant(SMSize0, MVT::i32),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000614 And0.getOperand(0));
615}
616
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000617SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000618 const {
619 SelectionDAG &DAG = DCI.DAG;
620 unsigned opc = N->getOpcode();
621
622 switch (opc) {
623 default: break;
624 case ISD::ADDE:
625 return PerformADDECombine(N, DAG, DCI, Subtarget);
626 case ISD::SUBE:
627 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000628 case ISD::SDIVREM:
629 case ISD::UDIVREM:
630 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000631 case ISD::SETCC:
632 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000633 case ISD::AND:
634 return PerformANDCombine(N, DAG, DCI, Subtarget);
635 case ISD::OR:
636 return PerformORCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000637 }
638
639 return SDValue();
640}
641
Dan Gohman475871a2008-07-27 21:46:04 +0000642SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000643LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000644{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000645 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000646 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000647 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000648 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
649 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000650 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000651 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000652 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
653 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000654 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000655 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000656 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000657 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000658 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000659 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000660 }
Dan Gohman475871a2008-07-27 21:46:04 +0000661 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000662}
663
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000664//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000665// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000666//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000667
668// AddLiveIn - This helper function adds the specified physical register to the
669// MachineFunction as a live in value. It also creates a corresponding
670// virtual register for it.
671static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000672AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000673{
674 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000675 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
676 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000677 return VReg;
678}
679
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000680// Get fp branch code (not opcode) from condition code.
681static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
682 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
683 return Mips::BRANCH_T;
684
685 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
686 return Mips::BRANCH_F;
687
688 return Mips::BRANCH_INVALID;
689}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000690
Akira Hatanaka14487d42011-06-07 19:28:39 +0000691static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
692 DebugLoc dl,
693 const MipsSubtarget* Subtarget,
694 const TargetInstrInfo *TII,
695 bool isFPCmp, unsigned Opc) {
696 // There is no need to expand CMov instructions if target has
697 // conditional moves.
698 if (Subtarget->hasCondMov())
699 return BB;
700
701 // To "insert" a SELECT_CC instruction, we actually have to insert the
702 // diamond control-flow pattern. The incoming instruction knows the
703 // destination vreg to set, the condition code register to branch on, the
704 // true/false values to select between, and a branch opcode to use.
705 const BasicBlock *LLVM_BB = BB->getBasicBlock();
706 MachineFunction::iterator It = BB;
707 ++It;
708
709 // thisMBB:
710 // ...
711 // TrueVal = ...
712 // setcc r1, r2, r3
713 // bNE r1, r0, copy1MBB
714 // fallthrough --> copy0MBB
715 MachineBasicBlock *thisMBB = BB;
716 MachineFunction *F = BB->getParent();
717 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
718 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
719 F->insert(It, copy0MBB);
720 F->insert(It, sinkMBB);
721
722 // Transfer the remainder of BB and its successor edges to sinkMBB.
723 sinkMBB->splice(sinkMBB->begin(), BB,
724 llvm::next(MachineBasicBlock::iterator(MI)),
725 BB->end());
726 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
727
728 // Next, add the true and fallthrough blocks as its successors.
729 BB->addSuccessor(copy0MBB);
730 BB->addSuccessor(sinkMBB);
731
732 // Emit the right instruction according to the type of the operands compared
733 if (isFPCmp)
734 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
735 else
736 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
737 .addReg(Mips::ZERO).addMBB(sinkMBB);
738
739 // copy0MBB:
740 // %FalseValue = ...
741 // # fallthrough to sinkMBB
742 BB = copy0MBB;
743
744 // Update machine-CFG edges
745 BB->addSuccessor(sinkMBB);
746
747 // sinkMBB:
748 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
749 // ...
750 BB = sinkMBB;
751
752 if (isFPCmp)
753 BuildMI(*BB, BB->begin(), dl,
754 TII->get(Mips::PHI), MI->getOperand(0).getReg())
755 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
756 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
757 else
758 BuildMI(*BB, BB->begin(), dl,
759 TII->get(Mips::PHI), MI->getOperand(0).getReg())
760 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
761 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
762
763 MI->eraseFromParent(); // The pseudo instruction is gone now.
764 return BB;
765}
766
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000767MachineBasicBlock *
768MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000769 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000770 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen94817572009-02-13 02:34:39 +0000771 DebugLoc dl = MI->getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000772
773 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000774 default:
775 assert(false && "Unexpected instr type to insert");
776 return NULL;
777 case Mips::MOVT:
778 case Mips::MOVT_S:
779 case Mips::MOVT_D:
780 return ExpandCondMov(MI, BB, dl, Subtarget, TII, true, Mips::BC1F);
781 case Mips::MOVF:
782 case Mips::MOVF_S:
783 case Mips::MOVF_D:
784 return ExpandCondMov(MI, BB, dl, Subtarget, TII, true, Mips::BC1T);
785 case Mips::MOVZ_I:
786 case Mips::MOVZ_S:
787 case Mips::MOVZ_D:
788 return ExpandCondMov(MI, BB, dl, Subtarget, TII, false, Mips::BNE);
789 case Mips::MOVN_I:
790 case Mips::MOVN_S:
791 case Mips::MOVN_D:
792 return ExpandCondMov(MI, BB, dl, Subtarget, TII, false, Mips::BEQ);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000793
794 case Mips::ATOMIC_LOAD_ADD_I8:
795 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
796 case Mips::ATOMIC_LOAD_ADD_I16:
797 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
798 case Mips::ATOMIC_LOAD_ADD_I32:
799 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
800
801 case Mips::ATOMIC_LOAD_AND_I8:
802 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
803 case Mips::ATOMIC_LOAD_AND_I16:
804 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
805 case Mips::ATOMIC_LOAD_AND_I32:
806 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
807
808 case Mips::ATOMIC_LOAD_OR_I8:
809 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
810 case Mips::ATOMIC_LOAD_OR_I16:
811 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
812 case Mips::ATOMIC_LOAD_OR_I32:
813 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
814
815 case Mips::ATOMIC_LOAD_XOR_I8:
816 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
817 case Mips::ATOMIC_LOAD_XOR_I16:
818 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
819 case Mips::ATOMIC_LOAD_XOR_I32:
820 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
821
822 case Mips::ATOMIC_LOAD_NAND_I8:
823 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
824 case Mips::ATOMIC_LOAD_NAND_I16:
825 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
826 case Mips::ATOMIC_LOAD_NAND_I32:
827 return EmitAtomicBinary(MI, BB, 4, 0, true);
828
829 case Mips::ATOMIC_LOAD_SUB_I8:
830 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
831 case Mips::ATOMIC_LOAD_SUB_I16:
832 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
833 case Mips::ATOMIC_LOAD_SUB_I32:
834 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
835
836 case Mips::ATOMIC_SWAP_I8:
837 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
838 case Mips::ATOMIC_SWAP_I16:
839 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
840 case Mips::ATOMIC_SWAP_I32:
841 return EmitAtomicBinary(MI, BB, 4, 0);
842
843 case Mips::ATOMIC_CMP_SWAP_I8:
844 return EmitAtomicCmpSwapPartword(MI, BB, 1);
845 case Mips::ATOMIC_CMP_SWAP_I16:
846 return EmitAtomicCmpSwapPartword(MI, BB, 2);
847 case Mips::ATOMIC_CMP_SWAP_I32:
848 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000849 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000850}
851
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000852// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
853// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
854MachineBasicBlock *
855MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000856 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000857 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000858 assert(Size == 4 && "Unsupported size for EmitAtomicBinary.");
859
860 MachineFunction *MF = BB->getParent();
861 MachineRegisterInfo &RegInfo = MF->getRegInfo();
862 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
863 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
864 DebugLoc dl = MI->getDebugLoc();
865
Akira Hatanaka4061da12011-07-19 20:11:17 +0000866 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000867 unsigned Ptr = MI->getOperand(1).getReg();
868 unsigned Incr = MI->getOperand(2).getReg();
869
Akira Hatanaka4061da12011-07-19 20:11:17 +0000870 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
871 unsigned AndRes = RegInfo.createVirtualRegister(RC);
872 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000873
874 // insert new blocks after the current block
875 const BasicBlock *LLVM_BB = BB->getBasicBlock();
876 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
877 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
878 MachineFunction::iterator It = BB;
879 ++It;
880 MF->insert(It, loopMBB);
881 MF->insert(It, exitMBB);
882
883 // Transfer the remainder of BB and its successor edges to exitMBB.
884 exitMBB->splice(exitMBB->begin(), BB,
885 llvm::next(MachineBasicBlock::iterator(MI)),
886 BB->end());
887 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
888
889 // thisMBB:
890 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000891 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000892 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000893 loopMBB->addSuccessor(loopMBB);
894 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000895
896 // loopMBB:
897 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000898 // <binop> storeval, oldval, incr
899 // sc success, storeval, 0(ptr)
900 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000901 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000902 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000903 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000904 // and andres, oldval, incr
905 // nor storeval, $0, andres
906 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr);
907 BuildMI(BB, dl, TII->get(Mips::NOR), StoreVal)
908 .addReg(Mips::ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000909 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000910 // <binop> storeval, oldval, incr
911 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000912 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000913 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000914 }
Akira Hatanaka4061da12011-07-19 20:11:17 +0000915 BuildMI(BB, dl, TII->get(Mips::SC), Success)
916 .addReg(StoreVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000917 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +0000918 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000919
920 MI->eraseFromParent(); // The instruction is gone now.
921
Akira Hatanaka939ece12011-07-19 03:42:13 +0000922 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000923}
924
925MachineBasicBlock *
926MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000927 MachineBasicBlock *BB,
928 unsigned Size, unsigned BinOpcode,
929 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000930 assert((Size == 1 || Size == 2) &&
931 "Unsupported size for EmitAtomicBinaryPartial.");
932
933 MachineFunction *MF = BB->getParent();
934 MachineRegisterInfo &RegInfo = MF->getRegInfo();
935 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
936 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
937 DebugLoc dl = MI->getDebugLoc();
938
939 unsigned Dest = MI->getOperand(0).getReg();
940 unsigned Ptr = MI->getOperand(1).getReg();
941 unsigned Incr = MI->getOperand(2).getReg();
942
Akira Hatanaka4061da12011-07-19 20:11:17 +0000943 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
944 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000945 unsigned Mask = RegInfo.createVirtualRegister(RC);
946 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000947 unsigned NewVal = RegInfo.createVirtualRegister(RC);
948 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000949 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000950 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
951 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
952 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
953 unsigned AndRes = RegInfo.createVirtualRegister(RC);
954 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000955 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000956 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
957 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
958 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
959 unsigned SllRes = RegInfo.createVirtualRegister(RC);
960 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000961
962 // insert new blocks after the current block
963 const BasicBlock *LLVM_BB = BB->getBasicBlock();
964 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000965 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000966 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
967 MachineFunction::iterator It = BB;
968 ++It;
969 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000970 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000971 MF->insert(It, exitMBB);
972
973 // Transfer the remainder of BB and its successor edges to exitMBB.
974 exitMBB->splice(exitMBB->begin(), BB,
975 llvm::next(MachineBasicBlock::iterator(MI)),
976 BB->end());
977 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
978
Akira Hatanaka81b44112011-07-19 17:09:53 +0000979 BB->addSuccessor(loopMBB);
980 loopMBB->addSuccessor(loopMBB);
981 loopMBB->addSuccessor(sinkMBB);
982 sinkMBB->addSuccessor(exitMBB);
983
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000984 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000985 // addiu masklsb2,$0,-4 # 0xfffffffc
986 // and alignedaddr,ptr,masklsb2
987 // andi ptrlsb2,ptr,3
988 // sll shiftamt,ptrlsb2,3
989 // ori maskupper,$0,255 # 0xff
990 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000991 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +0000992 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000993
994 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000995 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
996 .addReg(Mips::ZERO).addImm(-4);
997 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
998 .addReg(Ptr).addReg(MaskLSB2);
999 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1000 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1001 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1002 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001003 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1004 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001005 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001006 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001007
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001008
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001009 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001010 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001011 // ll oldval,0(alignedaddr)
1012 // binop binopres,oldval,incr2
1013 // and newval,binopres,mask
1014 // and maskedoldval0,oldval,mask2
1015 // or storeval,maskedoldval0,newval
1016 // sc success,storeval,0(alignedaddr)
1017 // beq success,$0,loopMBB
1018
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001019 // atomic.swap
1020 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001021 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001022 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001023 // and maskedoldval0,oldval,mask2
1024 // or storeval,maskedoldval0,newval
1025 // sc success,storeval,0(alignedaddr)
1026 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001027
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001028 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001029 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001030 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001031 // and andres, oldval, incr2
1032 // nor binopres, $0, andres
1033 // and newval, binopres, mask
1034 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1035 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1036 .addReg(Mips::ZERO).addReg(AndRes);
1037 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001038 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001039 // <binop> binopres, oldval, incr2
1040 // and newval, binopres, mask
1041 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1042 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001043 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001044 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001045 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001046 }
1047
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001048 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001049 .addReg(OldVal).addReg(Mask2);
1050 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001051 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001052 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1053 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001054 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001055 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001056
Akira Hatanaka939ece12011-07-19 03:42:13 +00001057 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001058 // and maskedoldval1,oldval,mask
1059 // srl srlres,maskedoldval1,shiftamt
1060 // sll sllres,srlres,24
1061 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001062 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001063 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001064
Akira Hatanaka4061da12011-07-19 20:11:17 +00001065 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1066 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001067 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1068 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001069 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1070 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001071 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001072 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001073
1074 MI->eraseFromParent(); // The instruction is gone now.
1075
Akira Hatanaka939ece12011-07-19 03:42:13 +00001076 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001077}
1078
1079MachineBasicBlock *
1080MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001081 MachineBasicBlock *BB,
1082 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001083 assert(Size == 4 && "Unsupported size for EmitAtomicCmpSwap.");
1084
1085 MachineFunction *MF = BB->getParent();
1086 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1087 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1088 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1089 DebugLoc dl = MI->getDebugLoc();
1090
1091 unsigned Dest = MI->getOperand(0).getReg();
1092 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001093 unsigned OldVal = MI->getOperand(2).getReg();
1094 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001095
Akira Hatanaka4061da12011-07-19 20:11:17 +00001096 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001097
1098 // insert new blocks after the current block
1099 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1100 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1101 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1102 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1103 MachineFunction::iterator It = BB;
1104 ++It;
1105 MF->insert(It, loop1MBB);
1106 MF->insert(It, loop2MBB);
1107 MF->insert(It, exitMBB);
1108
1109 // Transfer the remainder of BB and its successor edges to exitMBB.
1110 exitMBB->splice(exitMBB->begin(), BB,
1111 llvm::next(MachineBasicBlock::iterator(MI)),
1112 BB->end());
1113 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1114
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001115 // thisMBB:
1116 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001117 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001118 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001119 loop1MBB->addSuccessor(exitMBB);
1120 loop1MBB->addSuccessor(loop2MBB);
1121 loop2MBB->addSuccessor(loop1MBB);
1122 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001123
1124 // loop1MBB:
1125 // ll dest, 0(ptr)
1126 // bne dest, oldval, exitMBB
1127 BB = loop1MBB;
Akira Hatanakad3ac47f2011-07-07 18:57:00 +00001128 BuildMI(BB, dl, TII->get(Mips::LL), Dest).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001129 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001130 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001131
1132 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001133 // sc success, newval, 0(ptr)
1134 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001135 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001136 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1137 .addReg(NewVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001138 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001139 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001140
1141 MI->eraseFromParent(); // The instruction is gone now.
1142
Akira Hatanaka939ece12011-07-19 03:42:13 +00001143 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001144}
1145
1146MachineBasicBlock *
1147MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001148 MachineBasicBlock *BB,
1149 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001150 assert((Size == 1 || Size == 2) &&
1151 "Unsupported size for EmitAtomicCmpSwapPartial.");
1152
1153 MachineFunction *MF = BB->getParent();
1154 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1155 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1156 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1157 DebugLoc dl = MI->getDebugLoc();
1158
1159 unsigned Dest = MI->getOperand(0).getReg();
1160 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001161 unsigned CmpVal = MI->getOperand(2).getReg();
1162 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001163
Akira Hatanaka4061da12011-07-19 20:11:17 +00001164 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1165 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001166 unsigned Mask = RegInfo.createVirtualRegister(RC);
1167 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001168 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1169 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1170 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1171 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1172 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1173 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1174 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1175 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1176 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1177 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1178 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1179 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1180 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1181 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001182
1183 // insert new blocks after the current block
1184 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1185 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1186 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001187 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001188 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1189 MachineFunction::iterator It = BB;
1190 ++It;
1191 MF->insert(It, loop1MBB);
1192 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001193 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001194 MF->insert(It, exitMBB);
1195
1196 // Transfer the remainder of BB and its successor edges to exitMBB.
1197 exitMBB->splice(exitMBB->begin(), BB,
1198 llvm::next(MachineBasicBlock::iterator(MI)),
1199 BB->end());
1200 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1201
Akira Hatanaka81b44112011-07-19 17:09:53 +00001202 BB->addSuccessor(loop1MBB);
1203 loop1MBB->addSuccessor(sinkMBB);
1204 loop1MBB->addSuccessor(loop2MBB);
1205 loop2MBB->addSuccessor(loop1MBB);
1206 loop2MBB->addSuccessor(sinkMBB);
1207 sinkMBB->addSuccessor(exitMBB);
1208
Akira Hatanaka70564a92011-07-19 18:14:26 +00001209 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001210 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001211 // addiu masklsb2,$0,-4 # 0xfffffffc
1212 // and alignedaddr,ptr,masklsb2
1213 // andi ptrlsb2,ptr,3
1214 // sll shiftamt,ptrlsb2,3
1215 // ori maskupper,$0,255 # 0xff
1216 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001217 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001218 // andi maskedcmpval,cmpval,255
1219 // sll shiftedcmpval,maskedcmpval,shiftamt
1220 // andi maskednewval,newval,255
1221 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001222 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001223 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1224 .addReg(Mips::ZERO).addImm(-4);
1225 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1226 .addReg(Ptr).addReg(MaskLSB2);
1227 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1228 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1229 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1230 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001231 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1232 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001233 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001234 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1235 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001236 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1237 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001238 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1239 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001240 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1241 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001242
1243 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001244 // ll oldval,0(alginedaddr)
1245 // and maskedoldval0,oldval,mask
1246 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001247 BB = loop1MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001248 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
1249 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1250 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001251 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001252 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001253
1254 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001255 // and maskedoldval1,oldval,mask2
1256 // or storeval,maskedoldval1,shiftednewval
1257 // sc success,storeval,0(alignedaddr)
1258 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001259 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001260 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1261 .addReg(OldVal).addReg(Mask2);
1262 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1263 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1264 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1265 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001266 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001267 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001268
Akira Hatanaka939ece12011-07-19 03:42:13 +00001269 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001270 // srl srlres,maskedoldval0,shiftamt
1271 // sll sllres,srlres,24
1272 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001273 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001274 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001275
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001276 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1277 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001278 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1279 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001280 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001281 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001282
1283 MI->eraseFromParent(); // The instruction is gone now.
1284
Akira Hatanaka939ece12011-07-19 03:42:13 +00001285 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001286}
1287
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001288//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001289// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001290//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001291SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001292LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001293{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001294 MachineFunction &MF = DAG.getMachineFunction();
1295 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1296
1297 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001298 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1299 "Cannot lower if the alignment of the allocated space is larger than \
1300 that of the stack.");
1301
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001302 SDValue Chain = Op.getOperand(0);
1303 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001304 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001305
1306 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +00001307 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001308
1309 // Subtract the dynamic size from the actual stack size to
1310 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +00001311 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001312
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001313 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001314 // must be placed in the stack pointer register.
Akira Hatanaka053546c2011-05-25 02:20:00 +00001315 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
1316 SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001317
1318 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001319 // value and a chain
Akira Hatanaka21afc632011-06-21 00:40:49 +00001320 SDVTList VTLs = DAG.getVTList(MVT::i32, MVT::Other);
1321 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1322 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1323
1324 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001325}
1326
1327SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001328LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001329{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001330 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001331 // the block to branch to if the condition is true.
1332 SDValue Chain = Op.getOperand(0);
1333 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001334 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001335
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001336 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1337
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001338 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001339 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001340 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001341
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001342 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001343 Mips::CondCode CC =
1344 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001345 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001346
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001347 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001348 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001349}
1350
1351SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001352LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001353{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001354 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001355
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001356 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001357 if (Cond.getOpcode() != MipsISD::FPCmp)
1358 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001359
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001360 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1361 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001362}
1363
Dan Gohmand858e902010-04-17 15:26:15 +00001364SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1365 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001366 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001367 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001368 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001369
Eli Friedmane2c74082009-08-03 02:22:28 +00001370 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001371 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001372
Chris Lattnerb71b9092009-08-13 06:28:06 +00001373 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001374
Chris Lattnere3736f82009-08-13 05:41:27 +00001375 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001376 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1377 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001378 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001379 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1380 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001381 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001382 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001383 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001384 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1385 MipsII::MO_ABS_HI);
1386 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1387 MipsII::MO_ABS_LO);
1388 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1389 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001390 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001391 }
1392
Akira Hatanaka0f843822011-06-07 18:58:42 +00001393 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1394 MipsII::MO_GOT);
1395 GA = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, GA);
1396 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
1397 DAG.getEntryNode(), GA, MachinePointerInfo(),
1398 false, false, 0);
1399 // On functions and global targets not internal linked only
1400 // a load from got/GP is necessary for PIC to work.
1401 if (!GV->hasInternalLinkage() &&
1402 (!GV->hasLocalLinkage() || isa<Function>(GV)))
1403 return ResNode;
1404 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1405 MipsII::MO_ABS_LO);
1406 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
1407 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001408}
1409
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001410SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1411 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001412 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1413 // FIXME there isn't actually debug info here
1414 DebugLoc dl = Op.getDebugLoc();
1415
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001416 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001417 // %hi/%lo relocation
1418 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1419 MipsII::MO_ABS_HI);
1420 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1421 MipsII::MO_ABS_LO);
1422 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1423 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1424 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001425 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001426
1427 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1428 MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001429 BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, BAGOTOffset);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001430 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1431 MipsII::MO_ABS_LO);
1432 SDValue Load = DAG.getLoad(MVT::i32, dl,
1433 DAG.getEntryNode(), BAGOTOffset,
1434 MachinePointerInfo(), false, false, 0);
1435 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
1436 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001437}
1438
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001439SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001440LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001441{
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001442 // If the relocation model is PIC, use the General Dynamic TLS Model,
1443 // otherwise use the Initial Exec or Local Exec TLS Model.
1444 // TODO: implement Local Dynamic TLS model
1445
1446 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1447 DebugLoc dl = GA->getDebugLoc();
1448 const GlobalValue *GV = GA->getGlobal();
1449 EVT PtrVT = getPointerTy();
1450
1451 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1452 // General Dynamic TLS Model
1453 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001454 0, MipsII::MO_TLSGD);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001455 SDValue Tlsgd = DAG.getNode(MipsISD::TlsGd, dl, MVT::i32, TGA);
1456 SDValue GP = DAG.getRegister(Mips::GP, MVT::i32);
1457 SDValue Argument = DAG.getNode(ISD::ADD, dl, MVT::i32, GP, Tlsgd);
1458
1459 ArgListTy Args;
1460 ArgListEntry Entry;
1461 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001462 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001463 Args.push_back(Entry);
1464 std::pair<SDValue, SDValue> CallResult =
1465 LowerCallTo(DAG.getEntryNode(),
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001466 (Type *) Type::getInt32Ty(*DAG.getContext()),
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001467 false, false, false, false, 0, CallingConv::C, false, true,
1468 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG,
1469 dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001470
1471 return CallResult.first;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001472 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001473
1474 SDValue Offset;
1475 if (GV->isDeclaration()) {
1476 // Initial Exec TLS Model
1477 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1478 MipsII::MO_GOTTPREL);
1479 Offset = DAG.getLoad(MVT::i32, dl,
1480 DAG.getEntryNode(), TGA, MachinePointerInfo(),
1481 false, false, 0);
1482 } else {
1483 // Local Exec TLS Model
1484 SDVTList VTs = DAG.getVTList(MVT::i32);
1485 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1486 MipsII::MO_TPREL_HI);
1487 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1488 MipsII::MO_TPREL_LO);
1489 SDValue Hi = DAG.getNode(MipsISD::TprelHi, dl, VTs, &TGAHi, 1);
1490 SDValue Lo = DAG.getNode(MipsISD::TprelLo, dl, MVT::i32, TGALo);
1491 Offset = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1492 }
1493
1494 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1495 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001496}
1497
1498SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001499LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001500{
Dan Gohman475871a2008-07-27 21:46:04 +00001501 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001502 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +00001503 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001504 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001505 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001506 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001507
Owen Andersone50ed302009-08-10 22:56:29 +00001508 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001509 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001510
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001511 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
1512
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +00001513 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001514 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001515 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001516 } else {// Emit Load from Global Pointer
1517 JTI = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, JTI);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001518 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
1519 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001520 false, false, 0);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001521 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001522
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001523 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1524 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001525 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001526 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001527
1528 return ResNode;
1529}
1530
Dan Gohman475871a2008-07-27 21:46:04 +00001531SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001532LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001533{
Dan Gohman475871a2008-07-27 21:46:04 +00001534 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001535 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001536 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001537 // FIXME there isn't actually debug info here
1538 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001539
1540 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001541 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001542 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001543 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001544 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001545 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001546 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1547 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001548 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001549
1550 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001551 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001552 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001553 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001554 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001555 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1556 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001557 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001558 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001559 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001560 N->getOffset(), MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001561 CP = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001562 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001563 CP, MachinePointerInfo::getConstantPool(),
1564 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001565 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001566 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001567 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001568 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
1569 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001570
1571 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001572}
1573
Dan Gohmand858e902010-04-17 15:26:15 +00001574SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001575 MachineFunction &MF = DAG.getMachineFunction();
1576 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1577
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001578 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001579 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1580 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001581
1582 // vastart just stores the address of the VarArgsFrameIndex slot into the
1583 // memory location argument.
1584 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001585 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1586 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +00001587 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001588}
1589
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001590static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
1591 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
1592 DebugLoc dl = Op.getDebugLoc();
1593 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
1594 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
1595 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
1596 DAG.getConstant(0x7fffffff, MVT::i32));
1597 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
1598 DAG.getConstant(0x80000000, MVT::i32));
1599 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1600 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
1601}
1602
1603static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001604 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001605 // Use ext/ins instructions if target architecture is Mips32r2.
1606 // Eliminate redundant mfc1 and mtc1 instructions.
1607 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001608
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001609 if (!isLittle)
1610 std::swap(LoIdx, HiIdx);
1611
1612 DebugLoc dl = Op.getDebugLoc();
1613 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1614 Op.getOperand(0),
1615 DAG.getConstant(LoIdx, MVT::i32));
1616 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1617 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1618 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1619 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1620 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1621 DAG.getConstant(0x7fffffff, MVT::i32));
1622 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1623 DAG.getConstant(0x80000000, MVT::i32));
1624 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1625
1626 if (!isLittle)
1627 std::swap(Word0, Word1);
1628
1629 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1630}
1631
1632SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1633 const {
1634 EVT Ty = Op.getValueType();
1635
1636 assert(Ty == MVT::f32 || Ty == MVT::f64);
1637
1638 if (Ty == MVT::f32)
1639 return LowerFCOPYSIGN32(Op, DAG);
1640 else
1641 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
1642}
1643
Akira Hatanaka2e591472011-06-02 00:24:44 +00001644SDValue MipsTargetLowering::
1645LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001646 // check the depth
1647 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001648 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001649
1650 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1651 MFI->setFrameAddressIsTaken(true);
1652 EVT VT = Op.getValueType();
1653 DebugLoc dl = Op.getDebugLoc();
1654 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Mips::FP, VT);
1655 return FrameAddr;
1656}
1657
Akira Hatanakadb548262011-07-19 23:30:50 +00001658// TODO: set SType according to the desired memory barrier behavior.
1659SDValue MipsTargetLowering::LowerMEMBARRIER(SDValue Op,
1660 SelectionDAG& DAG) const {
1661 unsigned SType = 0;
1662 DebugLoc dl = Op.getDebugLoc();
1663 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1664 DAG.getConstant(SType, MVT::i32));
1665}
1666
Eli Friedman14648462011-07-27 22:21:52 +00001667SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1668 SelectionDAG& DAG) const {
1669 // FIXME: Need pseudo-fence for 'singlethread' fences
1670 // FIXME: Set SType for weaker fences where supported/appropriate.
1671 unsigned SType = 0;
1672 DebugLoc dl = Op.getDebugLoc();
1673 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1674 DAG.getConstant(SType, MVT::i32));
1675}
1676
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001677//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001678// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001679//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001680
1681#include "MipsGenCallingConv.inc"
1682
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001683//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001684// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001685// Mips O32 ABI rules:
1686// ---
1687// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001688// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001689// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001690// f64 - Only passed in two aliased f32 registers if no int reg has been used
1691// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001692// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1693// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001694//
1695// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001696//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001697
Duncan Sands1e96bab2010-11-04 10:49:57 +00001698static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001699 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001700 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1701
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001702 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001703
1704 static const unsigned IntRegs[] = {
1705 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1706 };
1707 static const unsigned F32Regs[] = {
1708 Mips::F12, Mips::F14
1709 };
1710 static const unsigned F64Regs[] = {
1711 Mips::D6, Mips::D7
1712 };
1713
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001714 // ByVal Args
1715 if (ArgFlags.isByVal()) {
1716 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1717 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1718 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1719 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1720 r < std::min(IntRegsSize, NextReg); ++r)
1721 State.AllocateReg(IntRegs[r]);
1722 return false;
1723 }
1724
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001725 // Promote i8 and i16
1726 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1727 LocVT = MVT::i32;
1728 if (ArgFlags.isSExt())
1729 LocInfo = CCValAssign::SExt;
1730 else if (ArgFlags.isZExt())
1731 LocInfo = CCValAssign::ZExt;
1732 else
1733 LocInfo = CCValAssign::AExt;
1734 }
1735
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001736 unsigned Reg;
1737
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001738 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1739 // is true: function is vararg, argument is 3rd or higher, there is previous
1740 // argument which is not f32 or f64.
1741 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1742 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001743 unsigned OrigAlign = ArgFlags.getOrigAlign();
1744 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001745
1746 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001747 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001748 // If this is the first part of an i64 arg,
1749 // the allocated register must be either A0 or A2.
1750 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1751 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001752 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001753 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1754 // Allocate int register and shadow next int register. If first
1755 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001756 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1757 if (Reg == Mips::A1 || Reg == Mips::A3)
1758 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1759 State.AllocateReg(IntRegs, IntRegsSize);
1760 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001761 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1762 // we are guaranteed to find an available float register
1763 if (ValVT == MVT::f32) {
1764 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1765 // Shadow int register
1766 State.AllocateReg(IntRegs, IntRegsSize);
1767 } else {
1768 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1769 // Shadow int registers
1770 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1771 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1772 State.AllocateReg(IntRegs, IntRegsSize);
1773 State.AllocateReg(IntRegs, IntRegsSize);
1774 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001775 } else
1776 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001777
Akira Hatanakad37776d2011-05-20 21:39:54 +00001778 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1779 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1780
1781 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001782 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001783 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001784 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001785
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001786 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001787}
1788
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001789//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001790// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001791//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001792
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001793static const unsigned O32IntRegsSize = 4;
1794
1795static const unsigned O32IntRegs[] = {
1796 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1797};
1798
1799// Write ByVal Arg to arg registers and stack.
1800static void
1801WriteByValArg(SDValue& Chain, DebugLoc dl,
1802 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1803 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1804 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001805 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001806 MVT PtrType, bool isLittle) {
1807 unsigned LocMemOffset = VA.getLocMemOffset();
1808 unsigned Offset = 0;
1809 uint32_t RemainingSize = Flags.getByValSize();
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001810 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001811
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001812 // Copy the first 4 words of byval arg to registers A0 - A3.
1813 // FIXME: Use a stricter alignment if it enables better optimization in passes
1814 // run later.
1815 for (; RemainingSize >= 4 && LocMemOffset < 4 * 4;
1816 Offset += 4, RemainingSize -= 4, LocMemOffset += 4) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001817 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001818 DAG.getConstant(Offset, MVT::i32));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001819 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1820 MachinePointerInfo(),
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001821 false, false, std::min(ByValAlign,
1822 (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001823 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001824 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001825 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1826 }
1827
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001828 if (RemainingSize == 0)
1829 return;
1830
1831 // If there still is a register available for argument passing, write the
1832 // remaining part of the structure to it using subword loads and shifts.
1833 if (LocMemOffset < 4 * 4) {
1834 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
1835 "There must be one to three bytes remaining.");
1836 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
1837 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1838 DAG.getConstant(Offset, MVT::i32));
1839 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
1840 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
1841 LoadPtr, MachinePointerInfo(),
1842 MVT::getIntegerVT(LoadSize * 8), false,
1843 false, Alignment);
1844 MemOpChains.push_back(LoadVal.getValue(1));
1845
1846 // If target is big endian, shift it to the most significant half-word or
1847 // byte.
1848 if (!isLittle)
1849 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
1850 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
1851
1852 Offset += LoadSize;
1853 RemainingSize -= LoadSize;
1854
1855 // Read second subword if necessary.
1856 if (RemainingSize != 0) {
1857 assert(RemainingSize == 1 && "There must be one byte remaining.");
1858 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1859 DAG.getConstant(Offset, MVT::i32));
1860 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
1861 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
1862 LoadPtr, MachinePointerInfo(),
1863 MVT::i8, false, false, Alignment);
1864 MemOpChains.push_back(Subword.getValue(1));
1865 // Insert the loaded byte to LoadVal.
1866 // FIXME: Use INS if supported by target.
1867 unsigned ShiftAmt = isLittle ? 16 : 8;
1868 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
1869 DAG.getConstant(ShiftAmt, MVT::i32));
1870 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
1871 }
1872
1873 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
1874 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1875 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001876 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001877
1878 // Create a fixed object on stack at offset LocMemOffset and copy
1879 // remaining part of byval arg to it using memcpy.
1880 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1881 DAG.getConstant(Offset, MVT::i32));
1882 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
1883 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
1884 Chain = DAG.getMemcpy(Chain, dl, Dst, Src,
1885 DAG.getConstant(RemainingSize, MVT::i32),
1886 std::min(ByValAlign, (unsigned)4),
1887 /*isVolatile=*/false, /*AlwaysInline=*/false,
1888 MachinePointerInfo(0), MachinePointerInfo(0));
1889 MemOpChains.push_back(Chain);
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001890}
1891
Dan Gohman98ca4f22009-08-05 01:29:28 +00001892/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001893/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001894/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001895SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001896MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001897 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001898 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001899 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001900 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001901 const SmallVectorImpl<ISD::InputArg> &Ins,
1902 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001903 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001904 // MIPs target does not yet support tail call optimization.
1905 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001906
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001907 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001908 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00001909 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001910 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001911 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001912
1913 // Analyze operands of the call, assigning locations to each operand.
1914 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001915 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1916 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001917
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001918 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001919 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001920 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001921 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001922
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001923 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001924 unsigned NextStackOffset = CCInfo.getNextStackOffset();
1925
1926 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NextStackOffset,
1927 true));
1928
1929 // If this is the first call, create a stack frame object that points to
1930 // a location to which .cprestore saves $gp.
1931 if (IsPIC && !MipsFI->getGPFI())
1932 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1933
Akira Hatanaka21afc632011-06-21 00:40:49 +00001934 // Get the frame index of the stack frame object that points to the location
1935 // of dynamically allocated area on the stack.
1936 int DynAllocFI = MipsFI->getDynAllocFI();
1937
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001938 // Update size of the maximum argument space.
1939 // For O32, a minimum of four words (16 bytes) of argument space is
1940 // allocated.
1941 if (Subtarget->isABI_O32())
1942 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
1943
1944 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1945
1946 if (MaxCallFrameSize < NextStackOffset) {
1947 MipsFI->setMaxCallFrameSize(NextStackOffset);
1948
Akira Hatanaka21afc632011-06-21 00:40:49 +00001949 // Set the offsets relative to $sp of the $gp restore slot and dynamically
1950 // allocated stack space. These offsets must be aligned to a boundary
1951 // determined by the stack alignment of the ABI.
1952 unsigned StackAlignment = TFL->getStackAlignment();
1953 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
1954 StackAlignment * StackAlignment;
1955
1956 if (IsPIC)
1957 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
1958
1959 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001960 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001961
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001962 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001963 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1964 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001965
Eric Christopher471e4222011-06-08 23:55:35 +00001966 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00001967
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001968 // Walk the register/memloc assignments, inserting copies/loads.
1969 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001970 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001971 CCValAssign &VA = ArgLocs[i];
1972
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001973 // Promote the value if needed.
1974 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001975 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001976 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001977 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001978 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001979 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001980 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001981 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1982 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001983 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1984 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001985 if (!Subtarget->isLittle())
1986 std::swap(Lo, Hi);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001987 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
1988 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
1989 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001990 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001991 }
1992 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00001993 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001994 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001995 break;
1996 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001997 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001998 break;
1999 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00002000 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002001 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002002 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002003
2004 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002005 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002006 if (VA.isRegLoc()) {
2007 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002008 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002009 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002010
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002011 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002012 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002013
Eric Christopher471e4222011-06-08 23:55:35 +00002014 // ByVal Arg.
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002015 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2016 if (Flags.isByVal()) {
2017 assert(Subtarget->isABI_O32() &&
2018 "No support for ByVal args by ABIs other than O32 yet.");
2019 assert(Flags.getByValSize() &&
2020 "ByVal args of size 0 should have been ignored by front-end.");
2021 WriteByValArg(Chain, dl, RegsToPass, MemOpChains, LastFI, MFI, DAG, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002022 VA, Flags, getPointerTy(), Subtarget->isLittle());
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002023 continue;
2024 }
2025
Chris Lattnere0b12152008-03-17 06:57:02 +00002026 // Create the frame index object for this incoming parameter
Eric Christopher471e4222011-06-08 23:55:35 +00002027 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002028 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002029 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002030
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002031 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002032 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002033 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
2034 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00002035 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002036 }
2037
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002038 // Extend range of indices of frame objects for outgoing arguments that were
2039 // created during this function call. Skip this step if no such objects were
2040 // created.
2041 if (LastFI)
2042 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2043
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002044 // Transform all store nodes into one single node because all store
2045 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002046 if (!MemOpChains.empty())
2047 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002048 &MemOpChains[0], MemOpChains.size());
2049
Bill Wendling056292f2008-09-16 21:48:12 +00002050 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002051 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2052 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002053 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002054 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002055 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002056
2057 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002058 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
2059 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2060 getPointerTy(), 0,MipsII:: MO_GOT);
2061 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
2062 0, MipsII::MO_ABS_LO);
2063 } else {
2064 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2065 getPointerTy(), 0, OpFlag);
2066 }
2067
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002068 LoadSymAddr = true;
2069 }
2070 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002071 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002072 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002073 LoadSymAddr = true;
2074 }
2075
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002076 SDValue InFlag;
2077
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002078 // Create nodes that load address of callee and copy it to T9
2079 if (IsPIC) {
2080 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002081 // Load callee address
Akira Hatanaka342837d2011-05-28 01:07:07 +00002082 Callee = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, Callee);
Akira Hatanaka25eba392011-06-24 19:01:25 +00002083 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), Callee,
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002084 MachinePointerInfo::getGOT(),
2085 false, false, 0);
2086
2087 // Use GOT+LO if callee has internal linkage.
2088 if (CalleeLo.getNode()) {
2089 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
2090 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
2091 } else
2092 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002093 }
2094
2095 // copy to T9
2096 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
2097 InFlag = Chain.getValue(1);
2098 Callee = DAG.getRegister(Mips::T9, MVT::i32);
2099 }
Bill Wendling056292f2008-09-16 21:48:12 +00002100
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002101 // Build a sequence of copy-to-reg nodes chained together with token
2102 // chain and flag operands which copy the outgoing args into registers.
2103 // The InFlag in necessary since all emitted instructions must be
2104 // stuck together.
2105 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2106 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2107 RegsToPass[i].second, InFlag);
2108 InFlag = Chain.getValue(1);
2109 }
2110
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002111 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002112 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002113 //
2114 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002115 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002116 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002117 Ops.push_back(Chain);
2118 Ops.push_back(Callee);
2119
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002120 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002121 // known live into the call.
2122 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2123 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2124 RegsToPass[i].second.getValueType()));
2125
Gabor Greifba36cb52008-08-28 21:40:38 +00002126 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002127 Ops.push_back(InFlag);
2128
Dale Johannesen33c960f2009-02-04 20:06:27 +00002129 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002130 InFlag = Chain.getValue(1);
2131
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002132 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002133 Chain = DAG.getCALLSEQ_END(Chain,
2134 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002135 DAG.getIntPtrConstant(0, true), InFlag);
2136 InFlag = Chain.getValue(1);
2137
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002138 // Handle result values, copying them out of physregs into vregs that we
2139 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002140 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2141 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002142}
2143
Dan Gohman98ca4f22009-08-05 01:29:28 +00002144/// LowerCallResult - Lower the result values of a call into the
2145/// appropriate copies out of appropriate physical registers.
2146SDValue
2147MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002148 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002149 const SmallVectorImpl<ISD::InputArg> &Ins,
2150 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002151 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002152 // Assign locations to each value returned by this call.
2153 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002154 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2155 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002156
Dan Gohman98ca4f22009-08-05 01:29:28 +00002157 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002158
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002159 // Copy all of the result registers out of their specified physreg.
2160 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002161 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002162 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002163 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002164 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002165 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002166
Dan Gohman98ca4f22009-08-05 01:29:28 +00002167 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002168}
2169
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002170//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002171// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002172//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002173static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2174 std::vector<SDValue>& OutChains,
2175 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2176 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2177 unsigned LocMem = VA.getLocMemOffset();
2178 unsigned FirstWord = LocMem / 4;
2179
2180 // copy register A0 - A3 to frame object
2181 for (unsigned i = 0; i < NumWords; ++i) {
2182 unsigned CurWord = FirstWord + i;
2183 if (CurWord >= O32IntRegsSize)
2184 break;
2185
2186 unsigned SrcReg = O32IntRegs[CurWord];
2187 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2188 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2189 DAG.getConstant(i * 4, MVT::i32));
2190 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2191 StorePtr, MachinePointerInfo(), false,
2192 false, 0);
2193 OutChains.push_back(Store);
2194 }
2195}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002196
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002197/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002198/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002199SDValue
2200MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002201 CallingConv::ID CallConv,
2202 bool isVarArg,
2203 const SmallVectorImpl<ISD::InputArg>
2204 &Ins,
2205 DebugLoc dl, SelectionDAG &DAG,
2206 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002207 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002208 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002209 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002210 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002211
Dan Gohman1e93df62010-04-17 14:41:14 +00002212 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002213
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002214 // Used with vargs to acumulate store chains.
2215 std::vector<SDValue> OutChains;
2216
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002217 // Assign locations to all of the incoming arguments.
2218 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002219 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2220 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002221
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002222 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002223 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002224 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002225 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002226
Akira Hatanaka43299772011-05-20 23:22:14 +00002227 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002228
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002229 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002230 CCValAssign &VA = ArgLocs[i];
2231
2232 // Arguments stored on registers
2233 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002234 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002235 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002236 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002237
Owen Anderson825b72b2009-08-11 20:47:22 +00002238 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002239 RC = Mips::CPURegsRegisterClass;
2240 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002241 RC = Mips::FGR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002242 else if (RegVT == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002243 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002244 RC = Mips::AFGR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002245 } else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002246 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002247
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002248 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002249 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002250 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002251 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002252
2253 // If this is an 8 or 16-bit value, it has been passed promoted
2254 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002255 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002256 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002257 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002258 if (VA.getLocInfo() == CCValAssign::SExt)
2259 Opcode = ISD::AssertSext;
2260 else if (VA.getLocInfo() == CCValAssign::ZExt)
2261 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002262 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002263 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00002264 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00002265 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002266 }
2267
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002268 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002269 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002270 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
2271 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00002272 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002273 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002274 VA.getLocReg()+1, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002275 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002276 if (!Subtarget->isLittle())
2277 std::swap(ArgValue, ArgValue2);
2278 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2279 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002280 }
2281 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002282
Dan Gohman98ca4f22009-08-05 01:29:28 +00002283 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002284 } else { // VA.isRegLoc()
2285
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002286 // sanity check
2287 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002288
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002289 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2290
2291 if (Flags.isByVal()) {
2292 assert(Subtarget->isABI_O32() &&
2293 "No support for ByVal args by ABIs other than O32 yet.");
2294 assert(Flags.getByValSize() &&
2295 "ByVal args of size 0 should have been ignored by front-end.");
2296 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2297 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2298 true);
2299 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2300 InVals.push_back(FIN);
2301 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2302
2303 continue;
2304 }
2305
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002306 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002307 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
2308 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002309
2310 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002311 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002312 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002313 MachinePointerInfo::getFixedStack(LastFI),
David Greenef6fa1862010-02-15 16:56:10 +00002314 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002315 }
2316 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002317
2318 // The mips ABIs for returning structs by value requires that we copy
2319 // the sret argument into $v0 for the return. Save the argument into
2320 // a virtual register so that we can access it from the return points.
2321 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2322 unsigned Reg = MipsFI->getSRetReturnReg();
2323 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002324 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002325 MipsFI->setSRetReturnReg(Reg);
2326 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002327 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002328 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002329 }
2330
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002331 if (isVarArg && Subtarget->isABI_O32()) {
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002332 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002333 // which is a value necessary to VASTART.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002334 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002335 assert(NextStackOffset % 4 == 0 &&
2336 "NextStackOffset must be aligned to 4-byte boundaries.");
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002337 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
2338 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002339
2340 // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
2341 // copy the integer registers that have not been used for argument passing
2342 // to the caller's stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002343 for (; NextStackOffset < 16; NextStackOffset += 4) {
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002344 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002345 unsigned Idx = NextStackOffset / 4;
2346 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
2347 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Akira Hatanaka69c19f72011-05-23 20:16:59 +00002348 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002349 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2350 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2351 MachinePointerInfo(),
2352 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002353 }
2354 }
2355
Akira Hatanaka43299772011-05-20 23:22:14 +00002356 MipsFI->setLastInArgFI(LastFI);
2357
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002358 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002359 // the size of Ins and InVals. This only happens when on varg functions
2360 if (!OutChains.empty()) {
2361 OutChains.push_back(Chain);
2362 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2363 &OutChains[0], OutChains.size());
2364 }
2365
Dan Gohman98ca4f22009-08-05 01:29:28 +00002366 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002367}
2368
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002369//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002370// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002371//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002372
Dan Gohman98ca4f22009-08-05 01:29:28 +00002373SDValue
2374MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002375 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002376 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002377 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002378 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002379
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002380 // CCValAssign - represent the assignment of
2381 // the return value to a location
2382 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002383
2384 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002385 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2386 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002387
Dan Gohman98ca4f22009-08-05 01:29:28 +00002388 // Analize return values.
2389 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002390
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002391 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002392 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002393 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002394 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002395 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002396 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002397 }
2398
Dan Gohman475871a2008-07-27 21:46:04 +00002399 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002400
2401 // Copy the result values into the output registers.
2402 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2403 CCValAssign &VA = RVLocs[i];
2404 assert(VA.isRegLoc() && "Can only return in registers!");
2405
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002406 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002407 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002408
2409 // guarantee that all emitted copies are
2410 // stuck together, avoiding something bad
2411 Flag = Chain.getValue(1);
2412 }
2413
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002414 // The mips ABIs for returning structs by value requires that we copy
2415 // the sret argument into $v0 for the return. We saved the argument into
2416 // a virtual register in the entry block, so now we copy the value out
2417 // and into $v0.
2418 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2419 MachineFunction &MF = DAG.getMachineFunction();
2420 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2421 unsigned Reg = MipsFI->getSRetReturnReg();
2422
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002423 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002424 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002425 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002426
Dale Johannesena05dca42009-02-04 23:02:30 +00002427 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002428 Flag = Chain.getValue(1);
2429 }
2430
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002431 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002432 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002433 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002434 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002435 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002436 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002437 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002438}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002439
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002440//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002441// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002442//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002443
2444/// getConstraintType - Given a constraint letter, return the type of
2445/// constraint it is for this target.
2446MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002447getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002448{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002449 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002450 // GCC config/mips/constraints.md
2451 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002452 // 'd' : An address register. Equivalent to r
2453 // unless generating MIPS16 code.
2454 // 'y' : Equivalent to r; retained for
2455 // backwards compatibility.
2456 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002457 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002458 switch (Constraint[0]) {
2459 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002460 case 'd':
2461 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002462 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002463 return C_RegisterClass;
2464 break;
2465 }
2466 }
2467 return TargetLowering::getConstraintType(Constraint);
2468}
2469
John Thompson44ab89e2010-10-29 17:29:13 +00002470/// Examine constraint type and operand type and determine a weight value.
2471/// This object must already have been set up with the operand type
2472/// and the current alternative constraint selected.
2473TargetLowering::ConstraintWeight
2474MipsTargetLowering::getSingleConstraintMatchWeight(
2475 AsmOperandInfo &info, const char *constraint) const {
2476 ConstraintWeight weight = CW_Invalid;
2477 Value *CallOperandVal = info.CallOperandVal;
2478 // If we don't have a value, we can't do a match,
2479 // but allow it at the lowest weight.
2480 if (CallOperandVal == NULL)
2481 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002482 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002483 // Look at the constraint type.
2484 switch (*constraint) {
2485 default:
2486 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2487 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002488 case 'd':
2489 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002490 if (type->isIntegerTy())
2491 weight = CW_Register;
2492 break;
2493 case 'f':
2494 if (type->isFloatTy())
2495 weight = CW_Register;
2496 break;
2497 }
2498 return weight;
2499}
2500
Eric Christopher38d64262011-06-29 19:33:04 +00002501/// Given a register class constraint, like 'r', if this corresponds directly
2502/// to an LLVM register class, return a register of 0 and the register class
2503/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002504std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002505getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002506{
2507 if (Constraint.size() == 1) {
2508 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002509 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2510 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002511 case 'r':
2512 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002513 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002514 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002515 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002516 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002517 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2518 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002519 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002520 }
2521 }
2522 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2523}
2524
Dan Gohman6520e202008-10-18 02:06:02 +00002525bool
2526MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2527 // The Mips target isn't yet aware of offsets.
2528 return false;
2529}
Evan Chengeb2f9692009-10-27 19:56:55 +00002530
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002531bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2532 if (VT != MVT::f32 && VT != MVT::f64)
2533 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002534 if (Imm.isNegZero())
2535 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002536 return Imm.isZero();
2537}