blob: d44bdca03d778e0d8e3575a6bcfbddc90a48c057 [file] [log] [blame]
Jia Liubb481f82012-02-28 07:46:26 +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"
Craig Topper79aa3412012-03-17 18:46:09 +000021#include "InstPrinter/MipsInstPrinter.h"
22#include "MCTargetDesc/MipsBaseInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000023#include "llvm/DerivedTypes.h"
24#include "llvm/Function.h"
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +000025#include "llvm/GlobalVariable.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000026#include "llvm/Intrinsics.h"
27#include "llvm/CallingConv.h"
28#include "llvm/CodeGen/CallingConvLower.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000034#include "llvm/CodeGen/ValueTypes.h"
35#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000036#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumi89593932012-04-21 15:31:45 +000037#include "llvm/Support/raw_ostream.h"
38
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000039using namespace llvm;
40
Jia Liubb481f82012-02-28 07:46:26 +000041// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanakadbe9a312011-08-18 20:07:42 +000042// mask (Pos), and return true.
Jia Liubb481f82012-02-28 07:46:26 +000043// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanaka854a7db2011-08-19 22:59:00 +000044static bool IsShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanakad6bc5232011-12-05 21:26:34 +000045 if (!isShiftedMask_64(I))
Akira Hatanaka854a7db2011-08-19 22:59:00 +000046 return false;
Akira Hatanakabb15e112011-08-17 02:05:42 +000047
Akira Hatanakad6bc5232011-12-05 21:26:34 +000048 Size = CountPopulation_64(I);
49 Pos = CountTrailingZeros_64(I);
Akira Hatanakadbe9a312011-08-18 20:07:42 +000050 return true;
Akira Hatanakabb15e112011-08-17 02:05:42 +000051}
52
Akira Hatanaka648f00c2012-02-24 22:34:47 +000053static SDValue GetGlobalReg(SelectionDAG &DAG, EVT Ty) {
54 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
55 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
56}
57
Chris Lattnerf0144122009-07-28 03:13:23 +000058const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
59 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000060 case MipsISD::JmpLink: return "MipsISD::JmpLink";
61 case MipsISD::Hi: return "MipsISD::Hi";
62 case MipsISD::Lo: return "MipsISD::Lo";
63 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +000064 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000065 case MipsISD::Ret: return "MipsISD::Ret";
66 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
67 case MipsISD::FPCmp: return "MipsISD::FPCmp";
68 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
69 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
70 case MipsISD::FPRound: return "MipsISD::FPRound";
71 case MipsISD::MAdd: return "MipsISD::MAdd";
72 case MipsISD::MAddu: return "MipsISD::MAddu";
73 case MipsISD::MSub: return "MipsISD::MSub";
74 case MipsISD::MSubu: return "MipsISD::MSubu";
75 case MipsISD::DivRem: return "MipsISD::DivRem";
76 case MipsISD::DivRemU: return "MipsISD::DivRemU";
77 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
78 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakabfcb83f2011-12-12 22:38:19 +000079 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanaka21afc632011-06-21 00:40:49 +000080 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
Akira Hatanakadb548262011-07-19 23:30:50 +000081 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanakabb15e112011-08-17 02:05:42 +000082 case MipsISD::Ext: return "MipsISD::Ext";
83 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab6f1dc22012-06-02 00:03:12 +000084 case MipsISD::LWL: return "MipsISD::LWL";
85 case MipsISD::LWR: return "MipsISD::LWR";
86 case MipsISD::SWL: return "MipsISD::SWL";
87 case MipsISD::SWR: return "MipsISD::SWR";
88 case MipsISD::LDL: return "MipsISD::LDL";
89 case MipsISD::LDR: return "MipsISD::LDR";
90 case MipsISD::SDL: return "MipsISD::SDL";
91 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka0f843822011-06-07 18:58:42 +000092 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000093 }
94}
95
96MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000097MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka8b4198d2011-09-26 21:47:02 +000098 : TargetLowering(TM, new MipsTargetObjectFile()),
99 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +0000100 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
101 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000102
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000103 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000104 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +0000105 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000106 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000107
108 // Set up the register classes
Craig Topper420761a2012-04-20 07:30:17 +0000109 addRegisterClass(MVT::i32, &Mips::CPURegsRegClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000110
Akira Hatanaka95934842011-09-24 01:34:44 +0000111 if (HasMips64)
Craig Topper420761a2012-04-20 07:30:17 +0000112 addRegisterClass(MVT::i64, &Mips::CPU64RegsRegClass);
Akira Hatanaka95934842011-09-24 01:34:44 +0000113
Akira Hatanaka28ee4fd2012-05-31 02:59:44 +0000114 if (Subtarget->inMips16Mode()) {
115 addRegisterClass(MVT::i32, &Mips::CPU16RegsRegClass);
116 addRegisterClass(MVT::i32, &Mips::CPURARegRegClass);
117 }
118
Akira Hatanakab0e7af72012-01-04 19:29:11 +0000119 if (!TM.Options.UseSoftFloat) {
Craig Topper420761a2012-04-20 07:30:17 +0000120 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
Akira Hatanakab0e7af72012-01-04 19:29:11 +0000121
122 // When dealing with single precision only, use libcalls
123 if (!Subtarget->isSingleFloat()) {
124 if (HasMips64)
Craig Topper420761a2012-04-20 07:30:17 +0000125 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
Akira Hatanakab0e7af72012-01-04 19:29:11 +0000126 else
Craig Topper420761a2012-04-20 07:30:17 +0000127 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
Akira Hatanakab0e7af72012-01-04 19:29:11 +0000128 }
Akira Hatanaka792016b2011-09-23 18:28:39 +0000129 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000130
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000131 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
133 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
134 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000135
Eli Friedman6055a6a2009-07-17 04:07:24 +0000136 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000137 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
138 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000139
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000140 // Used by legalize types to correctly generate the setcc result.
141 // Without this, every float setcc comes with a AND/OR with the result,
142 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000143 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000144 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000145
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000146 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +0000147 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000148 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000149 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
150 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
151 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
152 setOperationAction(ISD::SELECT, MVT::f32, Custom);
153 setOperationAction(ISD::SELECT, MVT::f64, Custom);
154 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka0a40c232012-03-09 23:46:03 +0000155 setOperationAction(ISD::SETCC, MVT::f32, Custom);
156 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
158 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000159 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000160 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
161 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
162 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
163 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Akira Hatanaka7664f052012-06-02 00:04:42 +0000164 setOperationAction(ISD::LOAD, MVT::i32, Custom);
165 setOperationAction(ISD::STORE, MVT::i32, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000166
Akira Hatanakac12a6e62012-04-11 22:49:04 +0000167 if (!TM.Options.NoNaNsFPMath) {
168 setOperationAction(ISD::FABS, MVT::f32, Custom);
169 setOperationAction(ISD::FABS, MVT::f64, Custom);
170 }
171
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000172 if (HasMips64) {
173 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
174 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
175 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
176 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
177 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
178 setOperationAction(ISD::SELECT, MVT::i64, Custom);
179 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
Akira Hatanaka7664f052012-06-02 00:04:42 +0000180 setOperationAction(ISD::LOAD, MVT::i64, Custom);
181 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000182 }
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000183
Akira Hatanakaa284acb2012-05-09 00:55:21 +0000184 if (!HasMips64) {
185 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
186 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
187 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
188 }
189
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000190 setOperationAction(ISD::SDIV, MVT::i32, Expand);
191 setOperationAction(ISD::SREM, MVT::i32, Expand);
192 setOperationAction(ISD::UDIV, MVT::i32, Expand);
193 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000194 setOperationAction(ISD::SDIV, MVT::i64, Expand);
195 setOperationAction(ISD::SREM, MVT::i64, Expand);
196 setOperationAction(ISD::UDIV, MVT::i64, Expand);
197 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000198
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000199 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000200 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
201 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
202 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
203 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000204 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000205 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000206 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000207 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
208 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000209 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000211 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000212 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
213 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
214 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
215 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000216 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000217 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000218
Akira Hatanaka56633442011-09-20 23:53:09 +0000219 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000220 setOperationAction(ISD::ROTR, MVT::i32, Expand);
221
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000222 if (!Subtarget->hasMips64r2())
223 setOperationAction(ISD::ROTR, MVT::i64, Expand);
224
Owen Anderson825b72b2009-08-11 20:47:22 +0000225 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000226 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000228 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
230 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000231 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 setOperationAction(ISD::FLOG, MVT::f32, Expand);
233 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
234 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
235 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000236 setOperationAction(ISD::FMA, MVT::f32, Expand);
237 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka21ecc2f2012-03-29 18:43:11 +0000238 setOperationAction(ISD::FREM, MVT::f32, Expand);
239 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000240
Akira Hatanaka1cc63332012-04-11 22:59:08 +0000241 if (!TM.Options.NoNaNsFPMath) {
242 setOperationAction(ISD::FNEG, MVT::f32, Expand);
243 setOperationAction(ISD::FNEG, MVT::f64, Expand);
244 }
245
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000246 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
Akira Hatanaka590baca2012-02-02 03:13:40 +0000247 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000248 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Akira Hatanaka590baca2012-02-02 03:13:40 +0000249 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000250
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000251 setOperationAction(ISD::VAARG, MVT::Other, Expand);
252 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
253 setOperationAction(ISD::VAEND, MVT::Other, Expand);
254
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000255 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
257 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000258
Jia Liubb481f82012-02-28 07:46:26 +0000259 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
260 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
261 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
262 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman4db5aca2011-08-29 18:23:02 +0000263
Eli Friedman26689ac2011-08-03 21:06:02 +0000264 setInsertFencesForAtomic(true);
265
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000266 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000268
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000269 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
271 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000272 }
273
Akira Hatanakac79507a2011-12-21 00:20:27 +0000274 if (!Subtarget->hasBitCount()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Akira Hatanakac79507a2011-12-21 00:20:27 +0000276 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
277 }
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000278
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000279 if (!Subtarget->hasSwap()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000281 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
282 }
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000283
Akira Hatanaka7664f052012-06-02 00:04:42 +0000284 if (HasMips64) {
285 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
286 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
287 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
288 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
289 }
290
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000291 setTargetDAGCombine(ISD::ADDE);
292 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000293 setTargetDAGCombine(ISD::SDIVREM);
294 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanakaee8c3b02012-03-08 03:26:37 +0000295 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000296 setTargetDAGCombine(ISD::AND);
297 setTargetDAGCombine(ISD::OR);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000298
Akira Hatanaka5fdf5002012-03-08 01:59:33 +0000299 setMinFunctionAlignment(HasMips64 ? 3 : 2);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000300
Akira Hatanaka3f5b1072012-02-02 03:17:04 +0000301 setStackPointerRegisterToSaveRestore(IsN64 ? Mips::SP_64 : Mips::SP);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000302 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000303
Akira Hatanaka590baca2012-02-02 03:13:40 +0000304 setExceptionPointerRegister(IsN64 ? Mips::A0_64 : Mips::A0);
305 setExceptionSelectorRegister(IsN64 ? Mips::A1_64 : Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000306}
307
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000308bool MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Akira Hatanaka511961a2011-08-17 18:49:18 +0000309 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
Jia Liubb481f82012-02-28 07:46:26 +0000310
Akira Hatanaka44b6c712012-02-28 02:55:02 +0000311 switch (SVT) {
312 case MVT::i64:
313 case MVT::i32:
Akira Hatanaka44b6c712012-02-28 02:55:02 +0000314 return true;
315 case MVT::f32:
Akira Hatanaka44b6c712012-02-28 02:55:02 +0000316 return Subtarget->hasMips32r2Or64();
317 default:
318 return false;
319 }
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000320}
321
Duncan Sands28b77e92011-09-06 19:07:46 +0000322EVT MipsTargetLowering::getSetCCResultType(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000324}
325
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000326// SelectMadd -
327// Transforms a subgraph in CurDAG if the following pattern is found:
328// (addc multLo, Lo0), (adde multHi, Hi0),
329// where,
330// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000331// Lo0: initial value of Lo register
332// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000333// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000334static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000335 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000336 // for the matching to be successful.
337 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
338
339 if (ADDCNode->getOpcode() != ISD::ADDC)
340 return false;
341
342 SDValue MultHi = ADDENode->getOperand(0);
343 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000344 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000345 unsigned MultOpc = MultHi.getOpcode();
346
347 // MultHi and MultLo must be generated by the same node,
348 if (MultLo.getNode() != MultNode)
349 return false;
350
351 // and it must be a multiplication.
352 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
353 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000354
355 // MultLo amd MultHi must be the first and second output of MultNode
356 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000357 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
358 return false;
359
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000360 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000361 // of the values of MultNode, in which case MultNode will be removed in later
362 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000363 // If there exist users other than ADDENode or ADDCNode, this function returns
364 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000365 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000366 // produced.
367 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
368 return false;
369
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000370 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000371 DebugLoc dl = ADDENode->getDebugLoc();
372
373 // create MipsMAdd(u) node
374 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000375
Akira Hatanaka82099682011-12-19 19:52:25 +0000376 SDValue MAdd = CurDAG->getNode(MultOpc, dl, MVT::Glue,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000377 MultNode->getOperand(0),// Factor 0
378 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000379 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000380 ADDENode->getOperand(1));// Hi0
381
382 // create CopyFromReg nodes
383 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
384 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000385 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000386 Mips::HI, MVT::i32,
387 CopyFromLo.getValue(2));
388
389 // replace uses of adde and addc here
390 if (!SDValue(ADDCNode, 0).use_empty())
391 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
392
393 if (!SDValue(ADDENode, 0).use_empty())
394 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
395
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000396 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000397}
398
399// SelectMsub -
400// Transforms a subgraph in CurDAG if the following pattern is found:
401// (addc Lo0, multLo), (sube Hi0, multHi),
402// where,
403// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000404// Lo0: initial value of Lo register
405// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000406// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000407static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000408 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000409 // for the matching to be successful.
410 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
411
412 if (SUBCNode->getOpcode() != ISD::SUBC)
413 return false;
414
415 SDValue MultHi = SUBENode->getOperand(1);
416 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000417 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000418 unsigned MultOpc = MultHi.getOpcode();
419
420 // MultHi and MultLo must be generated by the same node,
421 if (MultLo.getNode() != MultNode)
422 return false;
423
424 // and it must be a multiplication.
425 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
426 return false;
427
428 // MultLo amd MultHi must be the first and second output of MultNode
429 // respectively.
430 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
431 return false;
432
433 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
434 // of the values of MultNode, in which case MultNode will be removed in later
435 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000436 // If there exist users other than SUBENode or SUBCNode, this function returns
437 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000438 // instruction node rather than a pair of MULT and MSUB instructions being
439 // produced.
440 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
441 return false;
442
443 SDValue Chain = CurDAG->getEntryNode();
444 DebugLoc dl = SUBENode->getDebugLoc();
445
446 // create MipsSub(u) node
447 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
448
Akira Hatanaka82099682011-12-19 19:52:25 +0000449 SDValue MSub = CurDAG->getNode(MultOpc, dl, MVT::Glue,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000450 MultNode->getOperand(0),// Factor 0
451 MultNode->getOperand(1),// Factor 1
452 SUBCNode->getOperand(0),// Lo0
453 SUBENode->getOperand(0));// Hi0
454
455 // create CopyFromReg nodes
456 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
457 MSub);
458 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
459 Mips::HI, MVT::i32,
460 CopyFromLo.getValue(2));
461
462 // replace uses of sube and subc here
463 if (!SDValue(SUBCNode, 0).use_empty())
464 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
465
466 if (!SDValue(SUBENode, 0).use_empty())
467 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
468
469 return true;
470}
471
472static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
473 TargetLowering::DAGCombinerInfo &DCI,
474 const MipsSubtarget* Subtarget) {
475 if (DCI.isBeforeLegalize())
476 return SDValue();
477
Akira Hatanakae184fec2011-11-11 04:18:21 +0000478 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
479 SelectMadd(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000480 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000481
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000482 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000483}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000484
485static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
486 TargetLowering::DAGCombinerInfo &DCI,
487 const MipsSubtarget* Subtarget) {
488 if (DCI.isBeforeLegalize())
489 return SDValue();
490
Akira Hatanakae184fec2011-11-11 04:18:21 +0000491 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
492 SelectMsub(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000493 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000494
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000495 return SDValue();
496}
497
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000498static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
499 TargetLowering::DAGCombinerInfo &DCI,
500 const MipsSubtarget* Subtarget) {
501 if (DCI.isBeforeLegalizeOps())
502 return SDValue();
503
Akira Hatanakadda4a072011-10-03 21:06:13 +0000504 EVT Ty = N->getValueType(0);
Jia Liubb481f82012-02-28 07:46:26 +0000505 unsigned LO = (Ty == MVT::i32) ? Mips::LO : Mips::LO64;
506 unsigned HI = (Ty == MVT::i32) ? Mips::HI : Mips::HI64;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000507 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
508 MipsISD::DivRemU;
509 DebugLoc dl = N->getDebugLoc();
510
511 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
512 N->getOperand(0), N->getOperand(1));
513 SDValue InChain = DAG.getEntryNode();
514 SDValue InGlue = DivRem;
515
516 // insert MFLO
517 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakadda4a072011-10-03 21:06:13 +0000518 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000519 InGlue);
520 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
521 InChain = CopyFromLo.getValue(1);
522 InGlue = CopyFromLo.getValue(2);
523 }
524
525 // insert MFHI
526 if (N->hasAnyUseOfValue(1)) {
527 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000528 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000529 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
530 }
531
532 return SDValue();
533}
534
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000535static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
536 switch (CC) {
537 default: llvm_unreachable("Unknown fp condition code!");
538 case ISD::SETEQ:
539 case ISD::SETOEQ: return Mips::FCOND_OEQ;
540 case ISD::SETUNE: return Mips::FCOND_UNE;
541 case ISD::SETLT:
542 case ISD::SETOLT: return Mips::FCOND_OLT;
543 case ISD::SETGT:
544 case ISD::SETOGT: return Mips::FCOND_OGT;
545 case ISD::SETLE:
546 case ISD::SETOLE: return Mips::FCOND_OLE;
547 case ISD::SETGE:
548 case ISD::SETOGE: return Mips::FCOND_OGE;
549 case ISD::SETULT: return Mips::FCOND_ULT;
550 case ISD::SETULE: return Mips::FCOND_ULE;
551 case ISD::SETUGT: return Mips::FCOND_UGT;
552 case ISD::SETUGE: return Mips::FCOND_UGE;
553 case ISD::SETUO: return Mips::FCOND_UN;
554 case ISD::SETO: return Mips::FCOND_OR;
555 case ISD::SETNE:
556 case ISD::SETONE: return Mips::FCOND_ONE;
557 case ISD::SETUEQ: return Mips::FCOND_UEQ;
558 }
559}
560
561
562// Returns true if condition code has to be inverted.
563static bool InvertFPCondCode(Mips::CondCode CC) {
564 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
565 return false;
566
Akira Hatanaka82099682011-12-19 19:52:25 +0000567 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
568 "Illegal Condition Code");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000569
Akira Hatanaka82099682011-12-19 19:52:25 +0000570 return true;
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000571}
572
573// Creates and returns an FPCmp node from a setcc node.
574// Returns Op if setcc is not a floating point comparison.
575static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
576 // must be a SETCC node
577 if (Op.getOpcode() != ISD::SETCC)
578 return Op;
579
580 SDValue LHS = Op.getOperand(0);
581
582 if (!LHS.getValueType().isFloatingPoint())
583 return Op;
584
585 SDValue RHS = Op.getOperand(1);
586 DebugLoc dl = Op.getDebugLoc();
587
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000588 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
589 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000590 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
591
592 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
593 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
594}
595
596// Creates and returns a CMovFPT/F node.
597static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
598 SDValue False, DebugLoc DL) {
599 bool invert = InvertFPCondCode((Mips::CondCode)
600 cast<ConstantSDNode>(Cond.getOperand(2))
601 ->getSExtValue());
602
603 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
604 True.getValueType(), True, False, Cond);
605}
606
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000607static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG& DAG,
608 TargetLowering::DAGCombinerInfo &DCI,
609 const MipsSubtarget* Subtarget) {
610 if (DCI.isBeforeLegalizeOps())
611 return SDValue();
612
613 SDValue SetCC = N->getOperand(0);
614
615 if ((SetCC.getOpcode() != ISD::SETCC) ||
616 !SetCC.getOperand(0).getValueType().isInteger())
617 return SDValue();
618
619 SDValue False = N->getOperand(2);
620 EVT FalseTy = False.getValueType();
621
622 if (!FalseTy.isInteger())
623 return SDValue();
624
625 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(False);
626
627 if (!CN || CN->getZExtValue())
628 return SDValue();
629
630 const DebugLoc DL = N->getDebugLoc();
631 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
632 SDValue True = N->getOperand(1);
633
634 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
635 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
636
637 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
638}
639
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000640static SDValue PerformANDCombine(SDNode *N, SelectionDAG& DAG,
641 TargetLowering::DAGCombinerInfo &DCI,
642 const MipsSubtarget* Subtarget) {
643 // Pattern match EXT.
644 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
645 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000646 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000647 return SDValue();
648
649 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000650 unsigned ShiftRightOpc = ShiftRight.getOpcode();
651
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000652 // Op's first operand must be a shift right.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000653 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000654 return SDValue();
655
656 // The second operand of the shift must be an immediate.
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000657 ConstantSDNode *CN;
658 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
659 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000660
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000661 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000662 uint64_t SMPos, SMSize;
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000663
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000664 // Op's second operand must be a shifted mask.
665 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000666 !IsShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000667 return SDValue();
668
669 // Return if the shifted mask does not start at bit 0 or the sum of its size
670 // and Pos exceeds the word's size.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000671 EVT ValTy = N->getValueType(0);
672 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000673 return SDValue();
674
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000675 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), ValTy,
Akira Hatanaka82099682011-12-19 19:52:25 +0000676 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000677 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000678}
Jia Liubb481f82012-02-28 07:46:26 +0000679
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000680static SDValue PerformORCombine(SDNode *N, SelectionDAG& DAG,
681 TargetLowering::DAGCombinerInfo &DCI,
682 const MipsSubtarget* Subtarget) {
683 // Pattern match INS.
684 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liubb481f82012-02-28 07:46:26 +0000685 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000686 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000687 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000688 return SDValue();
689
690 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
691 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
692 ConstantSDNode *CN;
693
694 // See if Op's first operand matches (and $src1 , mask0).
695 if (And0.getOpcode() != ISD::AND)
696 return SDValue();
697
698 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000699 !IsShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000700 return SDValue();
701
702 // See if Op's second operand matches (and (shl $src, pos), mask1).
703 if (And1.getOpcode() != ISD::AND)
704 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000705
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000706 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000707 !IsShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000708 return SDValue();
709
710 // The shift masks must have the same position and size.
711 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
712 return SDValue();
713
714 SDValue Shl = And1.getOperand(0);
715 if (Shl.getOpcode() != ISD::SHL)
716 return SDValue();
717
718 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
719 return SDValue();
720
721 unsigned Shamt = CN->getZExtValue();
722
723 // Return if the shift amount and the first bit position of mask are not the
Jia Liubb481f82012-02-28 07:46:26 +0000724 // same.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000725 EVT ValTy = N->getValueType(0);
726 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000727 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000728
Akira Hatanaka82099682011-12-19 19:52:25 +0000729 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), ValTy, Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000730 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka82099682011-12-19 19:52:25 +0000731 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000732}
Jia Liubb481f82012-02-28 07:46:26 +0000733
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000734SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000735 const {
736 SelectionDAG &DAG = DCI.DAG;
737 unsigned opc = N->getOpcode();
738
739 switch (opc) {
740 default: break;
741 case ISD::ADDE:
742 return PerformADDECombine(N, DAG, DCI, Subtarget);
743 case ISD::SUBE:
744 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000745 case ISD::SDIVREM:
746 case ISD::UDIVREM:
747 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000748 case ISD::SELECT:
749 return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000750 case ISD::AND:
751 return PerformANDCombine(N, DAG, DCI, Subtarget);
752 case ISD::OR:
753 return PerformORCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000754 }
755
756 return SDValue();
757}
758
Dan Gohman475871a2008-07-27 21:46:04 +0000759SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000760LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000761{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000762 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000763 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000764 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000765 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
766 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000767 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000768 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000769 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
770 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000771 case ISD::SELECT: return LowerSELECT(Op, DAG);
Akira Hatanaka0a40c232012-03-09 23:46:03 +0000772 case ISD::SETCC: return LowerSETCC(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000773 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000774 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanakac12a6e62012-04-11 22:49:04 +0000775 case ISD::FABS: return LowerFABS(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000776 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000777 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000778 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Akira Hatanakaa284acb2012-05-09 00:55:21 +0000779 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
780 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG, true);
781 case ISD::SRL_PARTS: return LowerShiftRightParts(Op, DAG, false);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +0000782 case ISD::LOAD: return LowerLOAD(Op, DAG);
783 case ISD::STORE: return LowerSTORE(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000784 }
Dan Gohman475871a2008-07-27 21:46:04 +0000785 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000786}
787
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000788//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000789// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000790//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000791
792// AddLiveIn - This helper function adds the specified physical register to the
793// MachineFunction as a live in value. It also creates a corresponding
794// virtual register for it.
795static unsigned
Craig Topper44d23822012-02-22 05:59:10 +0000796AddLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000797{
798 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000799 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
800 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000801 return VReg;
802}
803
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000804// Get fp branch code (not opcode) from condition code.
805static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
806 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
807 return Mips::BRANCH_T;
808
Akira Hatanaka82099682011-12-19 19:52:25 +0000809 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
810 "Invalid CondCode.");
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000811
Akira Hatanaka82099682011-12-19 19:52:25 +0000812 return Mips::BRANCH_F;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000813}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000814
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000815/*
Akira Hatanaka14487d42011-06-07 19:28:39 +0000816static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
817 DebugLoc dl,
818 const MipsSubtarget* Subtarget,
819 const TargetInstrInfo *TII,
820 bool isFPCmp, unsigned Opc) {
821 // There is no need to expand CMov instructions if target has
822 // conditional moves.
823 if (Subtarget->hasCondMov())
824 return BB;
825
826 // To "insert" a SELECT_CC instruction, we actually have to insert the
827 // diamond control-flow pattern. The incoming instruction knows the
828 // destination vreg to set, the condition code register to branch on, the
829 // true/false values to select between, and a branch opcode to use.
830 const BasicBlock *LLVM_BB = BB->getBasicBlock();
831 MachineFunction::iterator It = BB;
832 ++It;
833
834 // thisMBB:
835 // ...
836 // TrueVal = ...
837 // setcc r1, r2, r3
838 // bNE r1, r0, copy1MBB
839 // fallthrough --> copy0MBB
840 MachineBasicBlock *thisMBB = BB;
841 MachineFunction *F = BB->getParent();
842 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
843 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
844 F->insert(It, copy0MBB);
845 F->insert(It, sinkMBB);
846
847 // Transfer the remainder of BB and its successor edges to sinkMBB.
848 sinkMBB->splice(sinkMBB->begin(), BB,
849 llvm::next(MachineBasicBlock::iterator(MI)),
850 BB->end());
851 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
852
853 // Next, add the true and fallthrough blocks as its successors.
854 BB->addSuccessor(copy0MBB);
855 BB->addSuccessor(sinkMBB);
856
857 // Emit the right instruction according to the type of the operands compared
858 if (isFPCmp)
859 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
860 else
861 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
862 .addReg(Mips::ZERO).addMBB(sinkMBB);
863
864 // copy0MBB:
865 // %FalseValue = ...
866 // # fallthrough to sinkMBB
867 BB = copy0MBB;
868
869 // Update machine-CFG edges
870 BB->addSuccessor(sinkMBB);
871
872 // sinkMBB:
873 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
874 // ...
875 BB = sinkMBB;
876
877 if (isFPCmp)
878 BuildMI(*BB, BB->begin(), dl,
879 TII->get(Mips::PHI), MI->getOperand(0).getReg())
880 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
881 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
882 else
883 BuildMI(*BB, BB->begin(), dl,
884 TII->get(Mips::PHI), MI->getOperand(0).getReg())
885 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
886 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
887
888 MI->eraseFromParent(); // The pseudo instruction is gone now.
889 return BB;
890}
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000891*/
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000892MachineBasicBlock *
893MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000894 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000895 switch (MI->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +0000896 default: llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000897 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000898 case Mips::ATOMIC_LOAD_ADD_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000899 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
900 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000901 case Mips::ATOMIC_LOAD_ADD_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000902 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
903 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000904 case Mips::ATOMIC_LOAD_ADD_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000905 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000906 case Mips::ATOMIC_LOAD_ADD_I64:
907 case Mips::ATOMIC_LOAD_ADD_I64_P8:
908 return EmitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000909
910 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000911 case Mips::ATOMIC_LOAD_AND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000912 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
913 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000914 case Mips::ATOMIC_LOAD_AND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000915 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
916 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000917 case Mips::ATOMIC_LOAD_AND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000918 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000919 case Mips::ATOMIC_LOAD_AND_I64:
920 case Mips::ATOMIC_LOAD_AND_I64_P8:
Akira Hatanaka73866122011-11-12 02:38:12 +0000921 return EmitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000922
923 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000924 case Mips::ATOMIC_LOAD_OR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000925 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
926 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000927 case Mips::ATOMIC_LOAD_OR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000928 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
929 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000930 case Mips::ATOMIC_LOAD_OR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000931 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000932 case Mips::ATOMIC_LOAD_OR_I64:
933 case Mips::ATOMIC_LOAD_OR_I64_P8:
934 return EmitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000935
936 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000937 case Mips::ATOMIC_LOAD_XOR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000938 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
939 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000940 case Mips::ATOMIC_LOAD_XOR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000941 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
942 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000943 case Mips::ATOMIC_LOAD_XOR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000944 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000945 case Mips::ATOMIC_LOAD_XOR_I64:
946 case Mips::ATOMIC_LOAD_XOR_I64_P8:
947 return EmitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000948
949 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000950 case Mips::ATOMIC_LOAD_NAND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000951 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
952 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000953 case Mips::ATOMIC_LOAD_NAND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000954 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
955 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000956 case Mips::ATOMIC_LOAD_NAND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000957 return EmitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000958 case Mips::ATOMIC_LOAD_NAND_I64:
959 case Mips::ATOMIC_LOAD_NAND_I64_P8:
960 return EmitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000961
962 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000963 case Mips::ATOMIC_LOAD_SUB_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000964 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
965 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000966 case Mips::ATOMIC_LOAD_SUB_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000967 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
968 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000969 case Mips::ATOMIC_LOAD_SUB_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000970 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000971 case Mips::ATOMIC_LOAD_SUB_I64:
972 case Mips::ATOMIC_LOAD_SUB_I64_P8:
973 return EmitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000974
975 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000976 case Mips::ATOMIC_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000977 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
978 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000979 case Mips::ATOMIC_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000980 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
981 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000982 case Mips::ATOMIC_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000983 return EmitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000984 case Mips::ATOMIC_SWAP_I64:
985 case Mips::ATOMIC_SWAP_I64_P8:
986 return EmitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000987
988 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000989 case Mips::ATOMIC_CMP_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000990 return EmitAtomicCmpSwapPartword(MI, BB, 1);
991 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000992 case Mips::ATOMIC_CMP_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000993 return EmitAtomicCmpSwapPartword(MI, BB, 2);
994 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000995 case Mips::ATOMIC_CMP_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000996 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000997 case Mips::ATOMIC_CMP_SWAP_I64:
998 case Mips::ATOMIC_CMP_SWAP_I64_P8:
999 return EmitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001000 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001001}
1002
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001003// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1004// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
1005MachineBasicBlock *
1006MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +00001007 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001008 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001009 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001010
1011 MachineFunction *MF = BB->getParent();
1012 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001013 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001014 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1015 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001016 unsigned LL, SC, AND, NOR, ZERO, BEQ;
1017
1018 if (Size == 4) {
1019 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1020 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1021 AND = Mips::AND;
1022 NOR = Mips::NOR;
1023 ZERO = Mips::ZERO;
1024 BEQ = Mips::BEQ;
1025 }
1026 else {
1027 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1028 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1029 AND = Mips::AND64;
1030 NOR = Mips::NOR64;
1031 ZERO = Mips::ZERO_64;
1032 BEQ = Mips::BEQ64;
1033 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001034
Akira Hatanaka4061da12011-07-19 20:11:17 +00001035 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001036 unsigned Ptr = MI->getOperand(1).getReg();
1037 unsigned Incr = MI->getOperand(2).getReg();
1038
Akira Hatanaka4061da12011-07-19 20:11:17 +00001039 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1040 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1041 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001042
1043 // insert new blocks after the current block
1044 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1045 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1046 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1047 MachineFunction::iterator It = BB;
1048 ++It;
1049 MF->insert(It, loopMBB);
1050 MF->insert(It, exitMBB);
1051
1052 // Transfer the remainder of BB and its successor edges to exitMBB.
1053 exitMBB->splice(exitMBB->begin(), BB,
1054 llvm::next(MachineBasicBlock::iterator(MI)),
1055 BB->end());
1056 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1057
1058 // thisMBB:
1059 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001060 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001061 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001062 loopMBB->addSuccessor(loopMBB);
1063 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001064
1065 // loopMBB:
1066 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001067 // <binop> storeval, oldval, incr
1068 // sc success, storeval, 0(ptr)
1069 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001070 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001071 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001072 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001073 // and andres, oldval, incr
1074 // nor storeval, $0, andres
Akira Hatanaka59068062011-11-11 04:14:30 +00001075 BuildMI(BB, dl, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1076 BuildMI(BB, dl, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001077 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001078 // <binop> storeval, oldval, incr
1079 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001080 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001081 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001082 }
Akira Hatanaka59068062011-11-11 04:14:30 +00001083 BuildMI(BB, dl, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1084 BuildMI(BB, dl, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001085
1086 MI->eraseFromParent(); // The instruction is gone now.
1087
Akira Hatanaka939ece12011-07-19 03:42:13 +00001088 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001089}
1090
1091MachineBasicBlock *
1092MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001093 MachineBasicBlock *BB,
1094 unsigned Size, unsigned BinOpcode,
1095 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001096 assert((Size == 1 || Size == 2) &&
1097 "Unsupported size for EmitAtomicBinaryPartial.");
1098
1099 MachineFunction *MF = BB->getParent();
1100 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1101 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1102 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1103 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001104 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1105 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001106
1107 unsigned Dest = MI->getOperand(0).getReg();
1108 unsigned Ptr = MI->getOperand(1).getReg();
1109 unsigned Incr = MI->getOperand(2).getReg();
1110
Akira Hatanaka4061da12011-07-19 20:11:17 +00001111 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1112 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001113 unsigned Mask = RegInfo.createVirtualRegister(RC);
1114 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001115 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1116 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001117 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001118 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1119 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1120 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1121 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1122 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001123 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001124 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1125 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1126 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1127 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1128 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001129
1130 // insert new blocks after the current block
1131 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1132 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001133 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001134 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1135 MachineFunction::iterator It = BB;
1136 ++It;
1137 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001138 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001139 MF->insert(It, exitMBB);
1140
1141 // Transfer the remainder of BB and its successor edges to exitMBB.
1142 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001143 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001144 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1145
Akira Hatanaka81b44112011-07-19 17:09:53 +00001146 BB->addSuccessor(loopMBB);
1147 loopMBB->addSuccessor(loopMBB);
1148 loopMBB->addSuccessor(sinkMBB);
1149 sinkMBB->addSuccessor(exitMBB);
1150
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001151 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001152 // addiu masklsb2,$0,-4 # 0xfffffffc
1153 // and alignedaddr,ptr,masklsb2
1154 // andi ptrlsb2,ptr,3
1155 // sll shiftamt,ptrlsb2,3
1156 // ori maskupper,$0,255 # 0xff
1157 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001158 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001159 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001160
1161 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001162 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1163 .addReg(Mips::ZERO).addImm(-4);
1164 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1165 .addReg(Ptr).addReg(MaskLSB2);
1166 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1167 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1168 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1169 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001170 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1171 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001172 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001173 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001174
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001175 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001176 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001177 // ll oldval,0(alignedaddr)
1178 // binop binopres,oldval,incr2
1179 // and newval,binopres,mask
1180 // and maskedoldval0,oldval,mask2
1181 // or storeval,maskedoldval0,newval
1182 // sc success,storeval,0(alignedaddr)
1183 // beq success,$0,loopMBB
1184
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001185 // atomic.swap
1186 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001187 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001188 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001189 // and maskedoldval0,oldval,mask2
1190 // or storeval,maskedoldval0,newval
1191 // sc success,storeval,0(alignedaddr)
1192 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001193
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001194 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001195 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001196 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001197 // and andres, oldval, incr2
1198 // nor binopres, $0, andres
1199 // and newval, binopres, mask
1200 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1201 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1202 .addReg(Mips::ZERO).addReg(AndRes);
1203 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001204 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001205 // <binop> binopres, oldval, incr2
1206 // and newval, binopres, mask
1207 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1208 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001209 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001210 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001211 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001212 }
Jia Liubb481f82012-02-28 07:46:26 +00001213
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001214 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001215 .addReg(OldVal).addReg(Mask2);
1216 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001217 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001218 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001219 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001220 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001221 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001222
Akira Hatanaka939ece12011-07-19 03:42:13 +00001223 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001224 // and maskedoldval1,oldval,mask
1225 // srl srlres,maskedoldval1,shiftamt
1226 // sll sllres,srlres,24
1227 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001228 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001229 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001230
Akira Hatanaka4061da12011-07-19 20:11:17 +00001231 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1232 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001233 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1234 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001235 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1236 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001237 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001238 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001239
1240 MI->eraseFromParent(); // The instruction is gone now.
1241
Akira Hatanaka939ece12011-07-19 03:42:13 +00001242 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001243}
1244
1245MachineBasicBlock *
1246MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001247 MachineBasicBlock *BB,
1248 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001249 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001250
1251 MachineFunction *MF = BB->getParent();
1252 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001253 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001254 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1255 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001256 unsigned LL, SC, ZERO, BNE, BEQ;
1257
1258 if (Size == 4) {
1259 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1260 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1261 ZERO = Mips::ZERO;
1262 BNE = Mips::BNE;
1263 BEQ = Mips::BEQ;
1264 }
1265 else {
1266 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1267 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1268 ZERO = Mips::ZERO_64;
1269 BNE = Mips::BNE64;
1270 BEQ = Mips::BEQ64;
1271 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001272
1273 unsigned Dest = MI->getOperand(0).getReg();
1274 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001275 unsigned OldVal = MI->getOperand(2).getReg();
1276 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001277
Akira Hatanaka4061da12011-07-19 20:11:17 +00001278 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001279
1280 // insert new blocks after the current block
1281 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1282 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1283 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1284 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1285 MachineFunction::iterator It = BB;
1286 ++It;
1287 MF->insert(It, loop1MBB);
1288 MF->insert(It, loop2MBB);
1289 MF->insert(It, exitMBB);
1290
1291 // Transfer the remainder of BB and its successor edges to exitMBB.
1292 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001293 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001294 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1295
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001296 // thisMBB:
1297 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001298 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001299 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001300 loop1MBB->addSuccessor(exitMBB);
1301 loop1MBB->addSuccessor(loop2MBB);
1302 loop2MBB->addSuccessor(loop1MBB);
1303 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001304
1305 // loop1MBB:
1306 // ll dest, 0(ptr)
1307 // bne dest, oldval, exitMBB
1308 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001309 BuildMI(BB, dl, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1310 BuildMI(BB, dl, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001311 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001312
1313 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001314 // sc success, newval, 0(ptr)
1315 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001316 BB = loop2MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001317 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001318 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka59068062011-11-11 04:14:30 +00001319 BuildMI(BB, dl, TII->get(BEQ))
1320 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001321
1322 MI->eraseFromParent(); // The instruction is gone now.
1323
Akira Hatanaka939ece12011-07-19 03:42:13 +00001324 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001325}
1326
1327MachineBasicBlock *
1328MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001329 MachineBasicBlock *BB,
1330 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001331 assert((Size == 1 || Size == 2) &&
1332 "Unsupported size for EmitAtomicCmpSwapPartial.");
1333
1334 MachineFunction *MF = BB->getParent();
1335 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1336 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1337 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1338 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001339 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1340 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001341
1342 unsigned Dest = MI->getOperand(0).getReg();
1343 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001344 unsigned CmpVal = MI->getOperand(2).getReg();
1345 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001346
Akira Hatanaka4061da12011-07-19 20:11:17 +00001347 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1348 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001349 unsigned Mask = RegInfo.createVirtualRegister(RC);
1350 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001351 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1352 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1353 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1354 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1355 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1356 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1357 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1358 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1359 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1360 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1361 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1362 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1363 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1364 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001365
1366 // insert new blocks after the current block
1367 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1368 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1369 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001370 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001371 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1372 MachineFunction::iterator It = BB;
1373 ++It;
1374 MF->insert(It, loop1MBB);
1375 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001376 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001377 MF->insert(It, exitMBB);
1378
1379 // Transfer the remainder of BB and its successor edges to exitMBB.
1380 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001381 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001382 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1383
Akira Hatanaka81b44112011-07-19 17:09:53 +00001384 BB->addSuccessor(loop1MBB);
1385 loop1MBB->addSuccessor(sinkMBB);
1386 loop1MBB->addSuccessor(loop2MBB);
1387 loop2MBB->addSuccessor(loop1MBB);
1388 loop2MBB->addSuccessor(sinkMBB);
1389 sinkMBB->addSuccessor(exitMBB);
1390
Akira Hatanaka70564a92011-07-19 18:14:26 +00001391 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001392 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001393 // addiu masklsb2,$0,-4 # 0xfffffffc
1394 // and alignedaddr,ptr,masklsb2
1395 // andi ptrlsb2,ptr,3
1396 // sll shiftamt,ptrlsb2,3
1397 // ori maskupper,$0,255 # 0xff
1398 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001399 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001400 // andi maskedcmpval,cmpval,255
1401 // sll shiftedcmpval,maskedcmpval,shiftamt
1402 // andi maskednewval,newval,255
1403 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001404 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001405 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1406 .addReg(Mips::ZERO).addImm(-4);
1407 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1408 .addReg(Ptr).addReg(MaskLSB2);
1409 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1410 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1411 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1412 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001413 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1414 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001415 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001416 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1417 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001418 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1419 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001420 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1421 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001422 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1423 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001424
1425 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001426 // ll oldval,0(alginedaddr)
1427 // and maskedoldval0,oldval,mask
1428 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001429 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001430 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001431 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1432 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001433 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001434 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001435
1436 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001437 // and maskedoldval1,oldval,mask2
1438 // or storeval,maskedoldval1,shiftednewval
1439 // sc success,storeval,0(alignedaddr)
1440 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001441 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001442 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1443 .addReg(OldVal).addReg(Mask2);
1444 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1445 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001446 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001447 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001448 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001449 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001450
Akira Hatanaka939ece12011-07-19 03:42:13 +00001451 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001452 // srl srlres,maskedoldval0,shiftamt
1453 // sll sllres,srlres,24
1454 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001455 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001456 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001457
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001458 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1459 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001460 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1461 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001462 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001463 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001464
1465 MI->eraseFromParent(); // The instruction is gone now.
1466
Akira Hatanaka939ece12011-07-19 03:42:13 +00001467 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001468}
1469
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001470//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001471// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001472//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001473SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001474LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001475{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001476 MachineFunction &MF = DAG.getMachineFunction();
1477 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001478 unsigned SP = IsN64 ? Mips::SP_64 : Mips::SP;
Akira Hatanaka21afc632011-06-21 00:40:49 +00001479
1480 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001481 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1482 "Cannot lower if the alignment of the allocated space is larger than \
1483 that of the stack.");
1484
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001485 SDValue Chain = Op.getOperand(0);
1486 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001487 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001488
1489 // Get a reference from Mips stack pointer
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001490 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, SP, getPointerTy());
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001491
1492 // Subtract the dynamic size from the actual stack size to
1493 // obtain the new stack size.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001494 SDValue Sub = DAG.getNode(ISD::SUB, dl, getPointerTy(), StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001495
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001496 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001497 // must be placed in the stack pointer register.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001498 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, SP, Sub, SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001499
1500 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001501 // value and a chain
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001502 SDVTList VTLs = DAG.getVTList(getPointerTy(), MVT::Other);
Akira Hatanaka21afc632011-06-21 00:40:49 +00001503 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1504 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1505
1506 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001507}
1508
1509SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001510LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001511{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001512 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001513 // the block to branch to if the condition is true.
1514 SDValue Chain = Op.getOperand(0);
1515 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001516 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001517
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001518 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1519
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001520 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001521 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001522 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001523
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001524 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001525 Mips::CondCode CC =
1526 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001527 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001528
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001529 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001530 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001531}
1532
1533SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001534LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001535{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001536 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001537
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001538 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001539 if (Cond.getOpcode() != MipsISD::FPCmp)
1540 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001541
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001542 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1543 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001544}
1545
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001546SDValue MipsTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1547 SDValue Cond = CreateFPCmp(DAG, Op);
1548
1549 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1550 "Floating point operand expected.");
1551
1552 SDValue True = DAG.getConstant(1, MVT::i32);
1553 SDValue False = DAG.getConstant(0, MVT::i32);
1554
1555 return CreateCMovFP(DAG, Cond, True, False, Op.getDebugLoc());
1556}
1557
Dan Gohmand858e902010-04-17 15:26:15 +00001558SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1559 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001560 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001561 DebugLoc dl = Op.getDebugLoc();
Jia Liubb481f82012-02-28 07:46:26 +00001562 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001563
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001564 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001565 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001566
Chris Lattnerb71b9092009-08-13 06:28:06 +00001567 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001568
Chris Lattnere3736f82009-08-13 05:41:27 +00001569 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001570 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1571 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001572 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001573 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1574 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001575 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001576 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001577 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001578 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1579 MipsII::MO_ABS_HI);
1580 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1581 MipsII::MO_ABS_LO);
1582 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1583 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001584 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001585 }
1586
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001587 EVT ValTy = Op.getValueType();
1588 bool HasGotOfst = (GV->hasInternalLinkage() ||
1589 (GV->hasLocalLinkage() && !isa<Function>(GV)));
Akira Hatanaka56ce6b32012-04-04 22:16:36 +00001590 unsigned GotFlag = HasMips64 ?
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001591 (HasGotOfst ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT_DISP) :
Bruno Cardoso Lopese3d35722011-12-07 00:28:57 +00001592 (HasGotOfst ? MipsII::MO_GOT : MipsII::MO_GOT16);
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001593 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0, GotFlag);
Akira Hatanaka648f00c2012-02-24 22:34:47 +00001594 GA = DAG.getNode(MipsISD::Wrapper, dl, ValTy, GetGlobalReg(DAG, ValTy), GA);
Akira Hatanaka82099682011-12-19 19:52:25 +00001595 SDValue ResNode = DAG.getLoad(ValTy, dl, DAG.getEntryNode(), GA,
1596 MachinePointerInfo(), false, false, false, 0);
Akira Hatanaka0f843822011-06-07 18:58:42 +00001597 // On functions and global targets not internal linked only
1598 // a load from got/GP is necessary for PIC to work.
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001599 if (!HasGotOfst)
Akira Hatanaka0f843822011-06-07 18:58:42 +00001600 return ResNode;
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001601 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0,
Akira Hatanaka56ce6b32012-04-04 22:16:36 +00001602 HasMips64 ? MipsII::MO_GOT_OFST :
1603 MipsII::MO_ABS_LO);
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001604 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, GALo);
1605 return DAG.getNode(ISD::ADD, dl, ValTy, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001606}
1607
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001608SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1609 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001610 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1611 // FIXME there isn't actually debug info here
1612 DebugLoc dl = Op.getDebugLoc();
1613
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001614 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001615 // %hi/%lo relocation
Akira Hatanaka82099682011-12-19 19:52:25 +00001616 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true, MipsII::MO_ABS_HI);
1617 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true, MipsII::MO_ABS_LO);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001618 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1619 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1620 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001621 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001622
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001623 EVT ValTy = Op.getValueType();
Akira Hatanaka03d830e2012-04-04 18:22:53 +00001624 unsigned GOTFlag = HasMips64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1625 unsigned OFSTFlag = HasMips64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001626 SDValue BAGOTOffset = DAG.getBlockAddress(BA, ValTy, true, GOTFlag);
Akira Hatanaka648f00c2012-02-24 22:34:47 +00001627 BAGOTOffset = DAG.getNode(MipsISD::Wrapper, dl, ValTy,
1628 GetGlobalReg(DAG, ValTy), BAGOTOffset);
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001629 SDValue BALOOffset = DAG.getBlockAddress(BA, ValTy, true, OFSTFlag);
Akira Hatanaka82099682011-12-19 19:52:25 +00001630 SDValue Load = DAG.getLoad(ValTy, dl, DAG.getEntryNode(), BAGOTOffset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001631 MachinePointerInfo(), false, false, false, 0);
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001632 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, BALOOffset);
1633 return DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001634}
1635
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001636SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001637LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001638{
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001639 // If the relocation model is PIC, use the General Dynamic TLS Model or
1640 // Local Dynamic TLS model, otherwise use the Initial Exec or
1641 // Local Exec TLS Model.
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001642
1643 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1644 DebugLoc dl = GA->getDebugLoc();
1645 const GlobalValue *GV = GA->getGlobal();
1646 EVT PtrVT = getPointerTy();
1647
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001648 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1649
1650 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg70a07c72012-06-04 14:02:08 +00001651 // General Dynamic and Local Dynamic TLS Model.
1652 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1653 : MipsII::MO_TLSGD;
1654
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001655 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, Flag);
Akira Hatanaka648f00c2012-02-24 22:34:47 +00001656 SDValue Argument = DAG.getNode(MipsISD::Wrapper, dl, PtrVT,
1657 GetGlobalReg(DAG, PtrVT), TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001658 unsigned PtrSize = PtrVT.getSizeInBits();
1659 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1660
Benjamin Kramer5eccf672011-12-11 12:21:34 +00001661 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001662
1663 ArgListTy Args;
1664 ArgListEntry Entry;
1665 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00001666 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001667 Args.push_back(Entry);
Jia Liubb481f82012-02-28 07:46:26 +00001668
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001669 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001670 false, false, false, false, 0, CallingConv::C,
1671 /*isTailCall=*/false, /*doesNotRet=*/false,
1672 /*isReturnValueUsed=*/true,
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001673 TlsGetAddr, Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001674 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001675
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001676 SDValue Ret = CallResult.first;
1677
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001678 if (model != TLSModel::LocalDynamic)
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001679 return Ret;
1680
1681 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1682 MipsII::MO_DTPREL_HI);
1683 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, PtrVT, TGAHi);
1684 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1685 MipsII::MO_DTPREL_LO);
1686 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, TGALo);
1687 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Ret);
1688 return DAG.getNode(ISD::ADD, dl, PtrVT, Add, Lo);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001689 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001690
1691 SDValue Offset;
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001692 if (model == TLSModel::InitialExec) {
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001693 // Initial Exec TLS Model
Akira Hatanakaca074792011-12-08 20:34:32 +00001694 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001695 MipsII::MO_GOTTPREL);
Akira Hatanaka648f00c2012-02-24 22:34:47 +00001696 TGA = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, GetGlobalReg(DAG, PtrVT),
1697 TGA);
Akira Hatanakaca074792011-12-08 20:34:32 +00001698 Offset = DAG.getLoad(PtrVT, dl,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001699 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001700 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001701 } else {
1702 // Local Exec TLS Model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001703 assert(model == TLSModel::LocalExec);
Akira Hatanakaca074792011-12-08 20:34:32 +00001704 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001705 MipsII::MO_TPREL_HI);
Akira Hatanakaca074792011-12-08 20:34:32 +00001706 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001707 MipsII::MO_TPREL_LO);
Akira Hatanakaca074792011-12-08 20:34:32 +00001708 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, PtrVT, TGAHi);
1709 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, TGALo);
1710 Offset = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001711 }
1712
1713 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1714 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001715}
1716
1717SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001718LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001719{
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001720 SDValue HiPart, JTI, JTILo;
Dale Johannesende064702009-02-06 21:50:26 +00001721 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001722 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001723 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Owen Andersone50ed302009-08-10 22:56:29 +00001724 EVT PtrVT = Op.getValueType();
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001725 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001726
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001727 if (!IsPIC && !IsN64) {
1728 JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_HI);
1729 HiPart = DAG.getNode(MipsISD::Hi, dl, PtrVT, JTI);
1730 JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_LO);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001731 } else {// Emit Load from Global Pointer
Akira Hatanakac75ceb72012-04-04 18:31:32 +00001732 unsigned GOTFlag = HasMips64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1733 unsigned OfstFlag = HasMips64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001734 JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, GOTFlag);
Akira Hatanaka648f00c2012-02-24 22:34:47 +00001735 JTI = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, GetGlobalReg(DAG, PtrVT),
1736 JTI);
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001737 HiPart = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), JTI,
1738 MachinePointerInfo(), false, false, false, 0);
1739 JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OfstFlag);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001740 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001741
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001742 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, JTILo);
1743 return DAG.getNode(ISD::ADD, dl, PtrVT, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001744}
1745
Dan Gohman475871a2008-07-27 21:46:04 +00001746SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001747LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001748{
Dan Gohman475871a2008-07-27 21:46:04 +00001749 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001750 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001751 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001752 // FIXME there isn't actually debug info here
1753 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001754
1755 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001756 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001757 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001758 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001759 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001760 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001761 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1762 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001763 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001764
Akira Hatanaka13daee32012-03-27 02:55:31 +00001765 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001766 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001767 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001768 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001769 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001770 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1771 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001772 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001773 } else {
Akira Hatanaka620db892011-11-16 22:44:38 +00001774 EVT ValTy = Op.getValueType();
Akira Hatanaka86a27332012-04-04 18:26:12 +00001775 unsigned GOTFlag = HasMips64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1776 unsigned OFSTFlag = HasMips64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
Akira Hatanaka620db892011-11-16 22:44:38 +00001777 SDValue CP = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1778 N->getOffset(), GOTFlag);
Akira Hatanaka648f00c2012-02-24 22:34:47 +00001779 CP = DAG.getNode(MipsISD::Wrapper, dl, ValTy, GetGlobalReg(DAG, ValTy), CP);
Akira Hatanaka82099682011-12-19 19:52:25 +00001780 SDValue Load = DAG.getLoad(ValTy, dl, DAG.getEntryNode(), CP,
1781 MachinePointerInfo::getConstantPool(), false,
1782 false, false, 0);
Akira Hatanaka620db892011-11-16 22:44:38 +00001783 SDValue CPLo = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1784 N->getOffset(), OFSTFlag);
1785 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, CPLo);
1786 ResNode = DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001787 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001788
1789 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001790}
1791
Dan Gohmand858e902010-04-17 15:26:15 +00001792SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001793 MachineFunction &MF = DAG.getMachineFunction();
1794 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1795
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001796 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001797 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1798 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001799
1800 // vastart just stores the address of the VarArgsFrameIndex slot into the
1801 // memory location argument.
1802 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001803 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
Akira Hatanaka82099682011-12-19 19:52:25 +00001804 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001805}
Jia Liubb481f82012-02-28 07:46:26 +00001806
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001807static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
1808 EVT TyX = Op.getOperand(0).getValueType();
1809 EVT TyY = Op.getOperand(1).getValueType();
1810 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1811 SDValue Const31 = DAG.getConstant(31, MVT::i32);
1812 DebugLoc DL = Op.getDebugLoc();
1813 SDValue Res;
1814
1815 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1816 // to i32.
1817 SDValue X = (TyX == MVT::f32) ?
1818 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1819 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1820 Const1);
1821 SDValue Y = (TyY == MVT::f32) ?
1822 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1823 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1824 Const1);
1825
1826 if (HasR2) {
1827 // ext E, Y, 31, 1 ; extract bit31 of Y
1828 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1829 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1830 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1831 } else {
1832 // sll SllX, X, 1
1833 // srl SrlX, SllX, 1
1834 // srl SrlY, Y, 31
1835 // sll SllY, SrlX, 31
1836 // or Or, SrlX, SllY
1837 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1838 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1839 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1840 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1841 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1842 }
1843
1844 if (TyX == MVT::f32)
1845 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1846
1847 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1848 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1849 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001850}
1851
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001852static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
1853 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1854 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1855 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1856 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1857 DebugLoc DL = Op.getDebugLoc();
Eric Christopher471e4222011-06-08 23:55:35 +00001858
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001859 // Bitcast to integer nodes.
1860 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1861 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001862
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001863 if (HasR2) {
1864 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1865 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1866 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1867 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001868
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001869 if (WidthX > WidthY)
1870 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1871 else if (WidthY > WidthX)
1872 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001873
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001874 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1875 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1876 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1877 }
1878
1879 // (d)sll SllX, X, 1
1880 // (d)srl SrlX, SllX, 1
1881 // (d)srl SrlY, Y, width(Y)-1
1882 // (d)sll SllY, SrlX, width(Y)-1
1883 // or Or, SrlX, SllY
1884 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1885 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1886 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1887 DAG.getConstant(WidthY - 1, MVT::i32));
1888
1889 if (WidthX > WidthY)
1890 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1891 else if (WidthY > WidthX)
1892 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1893
1894 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1895 DAG.getConstant(WidthX - 1, MVT::i32));
1896 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1897 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001898}
1899
Akira Hatanaka82099682011-12-19 19:52:25 +00001900SDValue
1901MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001902 if (Subtarget->hasMips64())
1903 return LowerFCOPYSIGN64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001904
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001905 return LowerFCOPYSIGN32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001906}
1907
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001908static SDValue LowerFABS32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
1909 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
1910 DebugLoc DL = Op.getDebugLoc();
1911
1912 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1913 // to i32.
1914 SDValue X = (Op.getValueType() == MVT::f32) ?
1915 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1916 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1917 Const1);
1918
1919 // Clear MSB.
1920 if (HasR2)
1921 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
1922 DAG.getRegister(Mips::ZERO, MVT::i32),
1923 DAG.getConstant(31, MVT::i32), Const1, X);
1924 else {
1925 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1926 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1927 }
1928
1929 if (Op.getValueType() == MVT::f32)
1930 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
1931
1932 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1933 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1934 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1935}
1936
1937static SDValue LowerFABS64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
1938 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
1939 DebugLoc DL = Op.getDebugLoc();
1940
1941 // Bitcast to integer node.
1942 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
1943
1944 // Clear MSB.
1945 if (HasR2)
1946 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
1947 DAG.getRegister(Mips::ZERO_64, MVT::i64),
1948 DAG.getConstant(63, MVT::i32), Const1, X);
1949 else {
1950 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
1951 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
1952 }
1953
1954 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
1955}
1956
1957SDValue
1958MipsTargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
1959 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
1960 return LowerFABS64(Op, DAG, Subtarget->hasMips32r2());
1961
1962 return LowerFABS32(Op, DAG, Subtarget->hasMips32r2());
1963}
1964
Akira Hatanaka2e591472011-06-02 00:24:44 +00001965SDValue MipsTargetLowering::
1966LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001967 // check the depth
1968 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001969 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001970
1971 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1972 MFI->setFrameAddressIsTaken(true);
1973 EVT VT = Op.getValueType();
1974 DebugLoc dl = Op.getDebugLoc();
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001975 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
1976 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001977 return FrameAddr;
1978}
1979
Akira Hatanakadb548262011-07-19 23:30:50 +00001980// TODO: set SType according to the desired memory barrier behavior.
Akira Hatanaka82099682011-12-19 19:52:25 +00001981SDValue
1982MipsTargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG& DAG) const {
Akira Hatanakadb548262011-07-19 23:30:50 +00001983 unsigned SType = 0;
1984 DebugLoc dl = Op.getDebugLoc();
1985 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1986 DAG.getConstant(SType, MVT::i32));
1987}
1988
Eli Friedman14648462011-07-27 22:21:52 +00001989SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1990 SelectionDAG& DAG) const {
1991 // FIXME: Need pseudo-fence for 'singlethread' fences
1992 // FIXME: Set SType for weaker fences where supported/appropriate.
1993 unsigned SType = 0;
1994 DebugLoc dl = Op.getDebugLoc();
1995 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1996 DAG.getConstant(SType, MVT::i32));
1997}
1998
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001999SDValue MipsTargetLowering::LowerShiftLeftParts(SDValue Op,
2000 SelectionDAG& DAG) const {
2001 DebugLoc DL = Op.getDebugLoc();
2002 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2003 SDValue Shamt = Op.getOperand(2);
2004
2005 // if shamt < 32:
2006 // lo = (shl lo, shamt)
2007 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2008 // else:
2009 // lo = 0
2010 // hi = (shl lo, shamt[4:0])
2011 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2012 DAG.getConstant(-1, MVT::i32));
2013 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
2014 DAG.getConstant(1, MVT::i32));
2015 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
2016 Not);
2017 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
2018 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2019 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
2020 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2021 DAG.getConstant(0x20, MVT::i32));
2022 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, DAG.getConstant(0, MVT::i32),
2023 ShiftLeftLo);
2024 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
2025
2026 SDValue Ops[2] = {Lo, Hi};
2027 return DAG.getMergeValues(Ops, 2, DL);
2028}
2029
2030SDValue MipsTargetLowering::LowerShiftRightParts(SDValue Op, SelectionDAG& DAG,
2031 bool IsSRA) const {
2032 DebugLoc DL = Op.getDebugLoc();
2033 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2034 SDValue Shamt = Op.getOperand(2);
2035
2036 // if shamt < 32:
2037 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2038 // if isSRA:
2039 // hi = (sra hi, shamt)
2040 // else:
2041 // hi = (srl hi, shamt)
2042 // else:
2043 // if isSRA:
2044 // lo = (sra hi, shamt[4:0])
2045 // hi = (sra hi, 31)
2046 // else:
2047 // lo = (srl hi, shamt[4:0])
2048 // hi = 0
2049 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2050 DAG.getConstant(-1, MVT::i32));
2051 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
2052 DAG.getConstant(1, MVT::i32));
2053 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
2054 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
2055 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2056 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
2057 Hi, Shamt);
2058 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2059 DAG.getConstant(0x20, MVT::i32));
2060 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
2061 DAG.getConstant(31, MVT::i32));
2062 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
2063 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2064 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
2065 ShiftRightHi);
2066
2067 SDValue Ops[2] = {Lo, Hi};
2068 return DAG.getMergeValues(Ops, 2, DL);
2069}
2070
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002071static SDValue CreateLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
2072 SDValue Chain, SDValue Src, unsigned Offset) {
2073 SDValue BasePtr = LD->getBasePtr(), Ptr;
2074 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
2075 EVT BasePtrVT = BasePtr.getValueType();
2076 DebugLoc DL = LD->getDebugLoc();
2077 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2078
2079 if (Offset)
2080 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, BasePtr,
2081 DAG.getConstant(Offset, BasePtrVT));
2082 else
2083 Ptr = BasePtr;
2084
2085 SDValue Ops[] = { Chain, Ptr, Src };
2086 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2087 LD->getMemOperand());
2088}
2089
2090// Expand an unaligned 32 or 64-bit integer load node.
2091SDValue MipsTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
2092 LoadSDNode *LD = cast<LoadSDNode>(Op);
2093 EVT MemVT = LD->getMemoryVT();
2094
2095 // Return if load is aligned or if MemVT is neither i32 nor i64.
2096 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2097 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2098 return SDValue();
2099
2100 bool IsLittle = Subtarget->isLittle();
2101 EVT VT = Op.getValueType();
2102 ISD::LoadExtType ExtType = LD->getExtensionType();
2103 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2104
2105 assert((VT == MVT::i32) || (VT == MVT::i64));
2106
2107 // Expand
2108 // (set dst, (i64 (load baseptr)))
2109 // to
2110 // (set tmp, (ldl (add baseptr, 7), undef))
2111 // (set dst, (ldr baseptr, tmp))
2112 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
2113 SDValue LDL = CreateLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
2114 IsLittle ? 7 : 0);
2115 return CreateLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
2116 IsLittle ? 0 : 7);
2117 }
2118
2119 SDValue LWL = CreateLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
2120 IsLittle ? 3 : 0);
2121 SDValue LWR = CreateLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
2122 IsLittle ? 0 : 3);
2123
2124 // Expand
2125 // (set dst, (i32 (load baseptr))) or
2126 // (set dst, (i64 (sextload baseptr))) or
2127 // (set dst, (i64 (extload baseptr)))
2128 // to
2129 // (set tmp, (lwl (add baseptr, 3), undef))
2130 // (set dst, (lwr baseptr, tmp))
2131 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2132 (ExtType == ISD::EXTLOAD))
2133 return LWR;
2134
2135 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2136
2137 // Expand
2138 // (set dst, (i64 (zextload baseptr)))
2139 // to
2140 // (set tmp0, (lwl (add baseptr, 3), undef))
2141 // (set tmp1, (lwr baseptr, tmp0))
2142 // (set tmp2, (shl tmp1, 32))
2143 // (set dst, (srl tmp2, 32))
2144 DebugLoc DL = LD->getDebugLoc();
2145 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2146 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
2147 SDValue Ops[] = { SLL, LWR.getValue(1) };
2148 return DAG.getMergeValues(Ops, 2, DL);
2149}
2150
2151static SDValue CreateStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
2152 SDValue Chain, unsigned Offset) {
2153 SDValue BasePtr = SD->getBasePtr(), Ptr, Value = SD->getValue();
Chris Lattner00edc3d2012-06-02 01:03:42 +00002154 EVT MemVT = SD->getMemoryVT();
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002155 EVT BasePtrVT = BasePtr.getValueType();
2156 DebugLoc DL = SD->getDebugLoc();
2157 SDVTList VTList = DAG.getVTList(MVT::Other);
2158
2159 if (Offset)
2160 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, BasePtr,
2161 DAG.getConstant(Offset, BasePtrVT));
2162 else
2163 Ptr = BasePtr;
2164
2165 SDValue Ops[] = { Chain, Value, Ptr };
2166 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2167 SD->getMemOperand());
2168}
2169
2170// Expand an unaligned 32 or 64-bit integer store node.
2171SDValue MipsTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2172 StoreSDNode *SD = cast<StoreSDNode>(Op);
2173 EVT MemVT = SD->getMemoryVT();
2174
2175 // Return if store is aligned or if MemVT is neither i32 nor i64.
2176 if ((SD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2177 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2178 return SDValue();
2179
2180 bool IsLittle = Subtarget->isLittle();
2181 SDValue Value = SD->getValue(), Chain = SD->getChain();
2182 EVT VT = Value.getValueType();
2183
2184 // Expand
2185 // (store val, baseptr) or
2186 // (truncstore val, baseptr)
2187 // to
2188 // (swl val, (add baseptr, 3))
2189 // (swr val, baseptr)
2190 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
2191 SDValue SWL = CreateStoreLR(MipsISD::SWL, DAG, SD, Chain,
2192 IsLittle ? 3 : 0);
2193 return CreateStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
2194 }
2195
2196 assert(VT == MVT::i64);
2197
2198 // Expand
2199 // (store val, baseptr)
2200 // to
2201 // (sdl val, (add baseptr, 7))
2202 // (sdr val, baseptr)
2203 SDValue SDL = CreateStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2204 return CreateStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
2205}
2206
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002207//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002208// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002209//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002210
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002211//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002212// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002213// Mips O32 ABI rules:
2214// ---
2215// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002216// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002217// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002218// f64 - Only passed in two aliased f32 registers if no int reg has been used
2219// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002220// not used, it must be shadowed. If only A3 is avaiable, shadow it and
2221// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002222//
2223// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002224//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002225
Duncan Sands1e96bab2010-11-04 10:49:57 +00002226static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00002227 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002228 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2229
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002230 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002231
Craig Topperc5eaae42012-03-11 07:57:25 +00002232 static const uint16_t IntRegs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002233 Mips::A0, Mips::A1, Mips::A2, Mips::A3
2234 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002235 static const uint16_t F32Regs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002236 Mips::F12, Mips::F14
2237 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002238 static const uint16_t F64Regs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002239 Mips::D6, Mips::D7
2240 };
2241
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002242 // ByVal Args
2243 if (ArgFlags.isByVal()) {
2244 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
2245 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
2246 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
2247 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
2248 r < std::min(IntRegsSize, NextReg); ++r)
2249 State.AllocateReg(IntRegs[r]);
2250 return false;
2251 }
2252
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002253 // Promote i8 and i16
2254 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2255 LocVT = MVT::i32;
2256 if (ArgFlags.isSExt())
2257 LocInfo = CCValAssign::SExt;
2258 else if (ArgFlags.isZExt())
2259 LocInfo = CCValAssign::ZExt;
2260 else
2261 LocInfo = CCValAssign::AExt;
2262 }
2263
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002264 unsigned Reg;
2265
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002266 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2267 // is true: function is vararg, argument is 3rd or higher, there is previous
2268 // argument which is not f32 or f64.
2269 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2270 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002271 unsigned OrigAlign = ArgFlags.getOrigAlign();
2272 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002273
2274 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002275 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002276 // If this is the first part of an i64 arg,
2277 // the allocated register must be either A0 or A2.
2278 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2279 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002280 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002281 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2282 // Allocate int register and shadow next int register. If first
2283 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002284 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2285 if (Reg == Mips::A1 || Reg == Mips::A3)
2286 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2287 State.AllocateReg(IntRegs, IntRegsSize);
2288 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002289 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2290 // we are guaranteed to find an available float register
2291 if (ValVT == MVT::f32) {
2292 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2293 // Shadow int register
2294 State.AllocateReg(IntRegs, IntRegsSize);
2295 } else {
2296 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2297 // Shadow int registers
2298 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2299 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2300 State.AllocateReg(IntRegs, IntRegsSize);
2301 State.AllocateReg(IntRegs, IntRegsSize);
2302 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002303 } else
2304 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002305
Akira Hatanakad37776d2011-05-20 21:39:54 +00002306 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
2307 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
2308
2309 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002310 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00002311 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002312 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002313
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002314 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002315}
2316
Craig Topperc5eaae42012-03-11 07:57:25 +00002317static const uint16_t Mips64IntRegs[8] =
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002318 {Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
2319 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64};
Craig Topperc5eaae42012-03-11 07:57:25 +00002320static const uint16_t Mips64DPRegs[8] =
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002321 {Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
2322 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64};
2323
2324static bool CC_Mips64Byval(unsigned ValNo, MVT ValVT, MVT LocVT,
2325 CCValAssign::LocInfo LocInfo,
2326 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2327 unsigned Align = std::max(ArgFlags.getByValAlign(), (unsigned)8);
2328 unsigned Size = (ArgFlags.getByValSize() + 7) / 8 * 8;
2329 unsigned FirstIdx = State.getFirstUnallocated(Mips64IntRegs, 8);
2330
2331 assert(Align <= 16 && "Cannot handle alignments larger than 16.");
2332
Jia Liubb481f82012-02-28 07:46:26 +00002333 // If byval is 16-byte aligned, the first arg register must be even.
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002334 if ((Align == 16) && (FirstIdx % 2)) {
2335 State.AllocateReg(Mips64IntRegs[FirstIdx], Mips64DPRegs[FirstIdx]);
2336 ++FirstIdx;
2337 }
2338
2339 // Mark the registers allocated.
2340 for (unsigned I = FirstIdx; Size && (I < 8); Size -= 8, ++I)
2341 State.AllocateReg(Mips64IntRegs[I], Mips64DPRegs[I]);
2342
2343 // Allocate space on caller's stack.
2344 unsigned Offset = State.AllocateStack(Size, Align);
Jia Liubb481f82012-02-28 07:46:26 +00002345
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002346 if (FirstIdx < 8)
2347 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Mips64IntRegs[FirstIdx],
Jia Liubb481f82012-02-28 07:46:26 +00002348 LocVT, LocInfo));
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002349 else
2350 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
2351
2352 return true;
2353}
2354
2355#include "MipsGenCallingConv.inc"
2356
Akira Hatanaka49617092011-11-14 19:02:54 +00002357static void
Akira Hatanaka08067b22012-01-24 22:07:36 +00002358AnalyzeMips64CallOperands(CCState &CCInfo,
Akira Hatanaka49617092011-11-14 19:02:54 +00002359 const SmallVectorImpl<ISD::OutputArg> &Outs) {
2360 unsigned NumOps = Outs.size();
2361 for (unsigned i = 0; i != NumOps; ++i) {
2362 MVT ArgVT = Outs[i].VT;
2363 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
2364 bool R;
2365
2366 if (Outs[i].IsFixed)
2367 R = CC_MipsN(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
2368 else
2369 R = CC_MipsN_VarArg(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
Jia Liubb481f82012-02-28 07:46:26 +00002370
Akira Hatanaka49617092011-11-14 19:02:54 +00002371 if (R) {
Benjamin Kramer6296ee32011-11-14 19:51:48 +00002372#ifndef NDEBUG
Akira Hatanaka49617092011-11-14 19:02:54 +00002373 dbgs() << "Call operand #" << i << " has unhandled type "
2374 << EVT(ArgVT).getEVTString();
2375#endif
2376 llvm_unreachable(0);
2377 }
2378 }
2379}
2380
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002381//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002382// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002383//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002384
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002385static const unsigned O32IntRegsSize = 4;
2386
Craig Topperc5eaae42012-03-11 07:57:25 +00002387static const uint16_t O32IntRegs[] = {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002388 Mips::A0, Mips::A1, Mips::A2, Mips::A3
2389};
2390
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002391// Return next O32 integer argument register.
2392static unsigned getNextIntArgReg(unsigned Reg) {
2393 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2394 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2395}
2396
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002397// Write ByVal Arg to arg registers and stack.
2398static void
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002399WriteByValArg(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002400 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
2401 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
2402 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00002403 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002404 MVT PtrType, bool isLittle) {
2405 unsigned LocMemOffset = VA.getLocMemOffset();
2406 unsigned Offset = 0;
2407 uint32_t RemainingSize = Flags.getByValSize();
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00002408 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002409
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002410 // Copy the first 4 words of byval arg to registers A0 - A3.
2411 // FIXME: Use a stricter alignment if it enables better optimization in passes
2412 // run later.
2413 for (; RemainingSize >= 4 && LocMemOffset < 4 * 4;
2414 Offset += 4, RemainingSize -= 4, LocMemOffset += 4) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002415 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002416 DAG.getConstant(Offset, MVT::i32));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002417 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
Akira Hatanaka82099682011-12-19 19:52:25 +00002418 MachinePointerInfo(), false, false, false,
2419 std::min(ByValAlign, (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002420 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002421 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002422 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
2423 }
2424
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002425 if (RemainingSize == 0)
2426 return;
2427
2428 // If there still is a register available for argument passing, write the
2429 // remaining part of the structure to it using subword loads and shifts.
2430 if (LocMemOffset < 4 * 4) {
2431 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
2432 "There must be one to three bytes remaining.");
2433 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
2434 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2435 DAG.getConstant(Offset, MVT::i32));
2436 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
2437 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2438 LoadPtr, MachinePointerInfo(),
2439 MVT::getIntegerVT(LoadSize * 8), false,
2440 false, Alignment);
2441 MemOpChains.push_back(LoadVal.getValue(1));
2442
2443 // If target is big endian, shift it to the most significant half-word or
2444 // byte.
2445 if (!isLittle)
2446 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
2447 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
2448
2449 Offset += LoadSize;
2450 RemainingSize -= LoadSize;
2451
2452 // Read second subword if necessary.
2453 if (RemainingSize != 0) {
2454 assert(RemainingSize == 1 && "There must be one byte remaining.");
Jia Liubb481f82012-02-28 07:46:26 +00002455 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002456 DAG.getConstant(Offset, MVT::i32));
2457 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
2458 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2459 LoadPtr, MachinePointerInfo(),
2460 MVT::i8, false, false, Alignment);
2461 MemOpChains.push_back(Subword.getValue(1));
2462 // Insert the loaded byte to LoadVal.
2463 // FIXME: Use INS if supported by target.
2464 unsigned ShiftAmt = isLittle ? 16 : 8;
2465 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
2466 DAG.getConstant(ShiftAmt, MVT::i32));
2467 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
2468 }
2469
2470 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
2471 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
2472 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002473 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002474
2475 // Create a fixed object on stack at offset LocMemOffset and copy
2476 // remaining part of byval arg to it using memcpy.
2477 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2478 DAG.getConstant(Offset, MVT::i32));
2479 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
2480 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002481 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2482 DAG.getConstant(RemainingSize, MVT::i32),
2483 std::min(ByValAlign, (unsigned)4),
2484 /*isVolatile=*/false, /*AlwaysInline=*/false,
2485 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002486}
2487
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002488// Copy Mips64 byVal arg to registers and stack.
2489void static
2490PassByValArg64(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
2491 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
2492 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
2493 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
2494 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2495 EVT PtrTy, bool isLittle) {
2496 unsigned ByValSize = Flags.getByValSize();
2497 unsigned Alignment = std::min(Flags.getByValAlign(), (unsigned)8);
2498 bool IsRegLoc = VA.isRegLoc();
2499 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
2500 unsigned LocMemOffset = 0;
Akira Hatanaka16040852011-11-15 18:42:25 +00002501 unsigned MemCpySize = ByValSize;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002502
2503 if (!IsRegLoc)
2504 LocMemOffset = VA.getLocMemOffset();
2505 else {
Craig Topperc5eaae42012-03-11 07:57:25 +00002506 const uint16_t *Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8,
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002507 VA.getLocReg());
Craig Topperc5eaae42012-03-11 07:57:25 +00002508 const uint16_t *RegEnd = Mips64IntRegs + 8;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002509
2510 // Copy double words to registers.
2511 for (; (Reg != RegEnd) && (ByValSize >= Offset + 8); ++Reg, Offset += 8) {
2512 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2513 DAG.getConstant(Offset, PtrTy));
2514 SDValue LoadVal = DAG.getLoad(MVT::i64, dl, Chain, LoadPtr,
2515 MachinePointerInfo(), false, false, false,
2516 Alignment);
2517 MemOpChains.push_back(LoadVal.getValue(1));
2518 RegsToPass.push_back(std::make_pair(*Reg, LoadVal));
2519 }
2520
Jia Liubb481f82012-02-28 07:46:26 +00002521 // Return if the struct has been fully copied.
Akira Hatanaka16040852011-11-15 18:42:25 +00002522 if (!(MemCpySize = ByValSize - Offset))
2523 return;
2524
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002525 // If there is an argument register available, copy the remainder of the
2526 // byval argument with sub-doubleword loads and shifts.
Akira Hatanaka16040852011-11-15 18:42:25 +00002527 if (Reg != RegEnd) {
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002528 assert((ByValSize < Offset + 8) &&
2529 "Size of the remainder should be smaller than 8-byte.");
2530 SDValue Val;
2531 for (unsigned LoadSize = 4; Offset < ByValSize; LoadSize /= 2) {
2532 unsigned RemSize = ByValSize - Offset;
2533
2534 if (RemSize < LoadSize)
2535 continue;
Jia Liubb481f82012-02-28 07:46:26 +00002536
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002537 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2538 DAG.getConstant(Offset, PtrTy));
Jia Liubb481f82012-02-28 07:46:26 +00002539 SDValue LoadVal =
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002540 DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i64, Chain, LoadPtr,
2541 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
2542 false, false, Alignment);
2543 MemOpChains.push_back(LoadVal.getValue(1));
2544
2545 // Offset in number of bits from double word boundary.
2546 unsigned OffsetDW = (Offset % 8) * 8;
2547 unsigned Shamt = isLittle ? OffsetDW : 64 - (OffsetDW + LoadSize * 8);
2548 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i64, LoadVal,
2549 DAG.getConstant(Shamt, MVT::i32));
Jia Liubb481f82012-02-28 07:46:26 +00002550
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002551 Val = Val.getNode() ? DAG.getNode(ISD::OR, dl, MVT::i64, Val, Shift) :
2552 Shift;
2553 Offset += LoadSize;
2554 Alignment = std::min(Alignment, LoadSize);
2555 }
Jia Liubb481f82012-02-28 07:46:26 +00002556
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002557 RegsToPass.push_back(std::make_pair(*Reg, Val));
2558 return;
2559 }
2560 }
2561
Akira Hatanaka16040852011-11-15 18:42:25 +00002562 assert(MemCpySize && "MemCpySize must not be zero.");
2563
2564 // Create a fixed object on stack at offset LocMemOffset and copy
2565 // remainder of byval arg to it with memcpy.
2566 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2567 DAG.getConstant(Offset, PtrTy));
2568 LastFI = MFI->CreateFixedObject(MemCpySize, LocMemOffset, true);
2569 SDValue Dst = DAG.getFrameIndex(LastFI, PtrTy);
2570 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2571 DAG.getConstant(MemCpySize, PtrTy), Alignment,
2572 /*isVolatile=*/false, /*AlwaysInline=*/false,
2573 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002574}
2575
Dan Gohman98ca4f22009-08-05 01:29:28 +00002576/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002577/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002578/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002579SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002580MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002581 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002582 SelectionDAG &DAG = CLI.DAG;
2583 DebugLoc &dl = CLI.DL;
2584 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2585 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2586 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2587 SDValue InChain = CLI.Chain;
2588 SDValue Callee = CLI.Callee;
2589 bool &isTailCall = CLI.IsTailCall;
2590 CallingConv::ID CallConv = CLI.CallConv;
2591 bool isVarArg = CLI.IsVarArg;
2592
Evan Cheng0c439eb2010-01-27 00:07:07 +00002593 // MIPs target does not yet support tail call optimization.
2594 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002595
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002596 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002597 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002598 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002599 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00002600 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002601
2602 // Analyze operands of the call, assigning locations to each operand.
2603 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002604 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002605 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002606
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002607 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002608 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanaka49617092011-11-14 19:02:54 +00002609 else if (HasMips64)
2610 AnalyzeMips64CallOperands(CCInfo, Outs);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00002611 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002612 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002613
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002614 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002615 unsigned NextStackOffset = CCInfo.getNextStackOffset();
2616
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002617 // Chain is the output chain of the last Load/Store or CopyToReg node.
2618 // ByValChain is the output chain of the last Memcpy node created for copying
2619 // byval arguments to the stack.
2620 SDValue Chain, CallSeqStart, ByValChain;
2621 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
2622 Chain = CallSeqStart = DAG.getCALLSEQ_START(InChain, NextStackOffsetVal);
2623 ByValChain = InChain;
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002624
Akira Hatanaka21afc632011-06-21 00:40:49 +00002625 // Get the frame index of the stack frame object that points to the location
2626 // of dynamically allocated area on the stack.
2627 int DynAllocFI = MipsFI->getDynAllocFI();
2628
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002629 // Update size of the maximum argument space.
2630 // For O32, a minimum of four words (16 bytes) of argument space is
2631 // allocated.
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002632 if (IsO32)
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002633 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
2634
2635 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
2636
2637 if (MaxCallFrameSize < NextStackOffset) {
2638 MipsFI->setMaxCallFrameSize(NextStackOffset);
2639
Akira Hatanaka21afc632011-06-21 00:40:49 +00002640 // Set the offsets relative to $sp of the $gp restore slot and dynamically
2641 // allocated stack space. These offsets must be aligned to a boundary
2642 // determined by the stack alignment of the ABI.
2643 unsigned StackAlignment = TFL->getStackAlignment();
2644 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
2645 StackAlignment * StackAlignment;
2646
Akira Hatanaka21afc632011-06-21 00:40:49 +00002647 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002648 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002649
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002650 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002651 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
2652 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002653
Eric Christopher471e4222011-06-08 23:55:35 +00002654 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00002655
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002656 // Walk the register/memloc assignments, inserting copies/loads.
2657 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002658 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002659 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002660 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002661 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2662
2663 // ByVal Arg.
2664 if (Flags.isByVal()) {
2665 assert(Flags.getByValSize() &&
2666 "ByVal args of size 0 should have been ignored by front-end.");
2667 if (IsO32)
2668 WriteByValArg(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2669 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2670 Subtarget->isLittle());
2671 else
2672 PassByValArg64(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
Jia Liubb481f82012-02-28 07:46:26 +00002673 MFI, DAG, Arg, VA, Flags, getPointerTy(),
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002674 Subtarget->isLittle());
2675 continue;
2676 }
Jia Liubb481f82012-02-28 07:46:26 +00002677
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002678 // Promote the value if needed.
2679 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002680 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002681 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002682 if (VA.isRegLoc()) {
2683 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
2684 (ValVT == MVT::f64 && LocVT == MVT::i64))
2685 Arg = DAG.getNode(ISD::BITCAST, dl, LocVT, Arg);
2686 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002687 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2688 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002689 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2690 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002691 if (!Subtarget->isLittle())
2692 std::swap(Lo, Hi);
Jia Liubb481f82012-02-28 07:46:26 +00002693 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002694 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2695 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2696 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002697 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002698 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002699 }
2700 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002701 case CCValAssign::SExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002702 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002703 break;
2704 case CCValAssign::ZExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002705 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002706 break;
2707 case CCValAssign::AExt:
Akira Hatanaka38bdc572012-02-17 02:20:26 +00002708 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002709 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002710 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002711
2712 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002713 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002714 if (VA.isRegLoc()) {
2715 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002716 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002717 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002718
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002719 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002720 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002721
Chris Lattnere0b12152008-03-17 06:57:02 +00002722 // Create the frame index object for this incoming parameter
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002723 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002724 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002725 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002726
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002727 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002728 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002729 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Akira Hatanaka82099682011-12-19 19:52:25 +00002730 MachinePointerInfo(), false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002731 }
2732
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002733 // Extend range of indices of frame objects for outgoing arguments that were
2734 // created during this function call. Skip this step if no such objects were
2735 // created.
2736 if (LastFI)
2737 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2738
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002739 // If a memcpy has been created to copy a byval arg to a stack, replace the
2740 // chain input of CallSeqStart with ByValChain.
2741 if (InChain != ByValChain)
2742 DAG.UpdateNodeOperands(CallSeqStart.getNode(), ByValChain,
2743 NextStackOffsetVal);
2744
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002745 // Transform all store nodes into one single node because all store
2746 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002747 if (!MemOpChains.empty())
2748 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002749 &MemOpChains[0], MemOpChains.size());
2750
Bill Wendling056292f2008-09-16 21:48:12 +00002751 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002752 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2753 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002754 unsigned char OpFlag;
2755 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002756 bool GlobalOrExternal = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002757 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002758
2759 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002760 if (IsPICCall && G->getGlobal()->hasInternalLinkage()) {
2761 OpFlag = IsO32 ? MipsII::MO_GOT : MipsII::MO_GOT_PAGE;
2762 unsigned char LoFlag = IsO32 ? MipsII::MO_ABS_LO : MipsII::MO_GOT_OFST;
2763 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(), 0,
2764 OpFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002765 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002766 0, LoFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002767 } else {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002768 OpFlag = IsPICCall ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002769 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2770 getPointerTy(), 0, OpFlag);
2771 }
2772
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002773 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002774 }
2775 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002776 if (IsN64 || (!IsO32 && IsPIC))
2777 OpFlag = MipsII::MO_GOT_DISP;
2778 else if (!IsPIC) // !N64 && static
2779 OpFlag = MipsII::MO_NO_FLAG;
2780 else // O32 & PIC
2781 OpFlag = MipsII::MO_GOT_CALL;
Akira Hatanaka82099682011-12-19 19:52:25 +00002782 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2783 OpFlag);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002784 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002785 }
2786
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002787 SDValue InFlag;
2788
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002789 // Create nodes that load address of callee and copy it to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002790 if (IsPICCall) {
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002791 if (GlobalOrExternal) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002792 // Load callee address
Akira Hatanaka648f00c2012-02-24 22:34:47 +00002793 Callee = DAG.getNode(MipsISD::Wrapper, dl, getPointerTy(),
2794 GetGlobalReg(DAG, getPointerTy()), Callee);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002795 SDValue LoadValue = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
2796 Callee, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002797 false, false, false, 0);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002798
2799 // Use GOT+LO if callee has internal linkage.
2800 if (CalleeLo.getNode()) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002801 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, getPointerTy(), CalleeLo);
2802 Callee = DAG.getNode(ISD::ADD, dl, getPointerTy(), LoadValue, Lo);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002803 } else
2804 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002805 }
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002806 }
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002807
Jia Liubb481f82012-02-28 07:46:26 +00002808 // T9 should contain the address of the callee function if
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002809 // -reloction-model=pic or it is an indirect call.
2810 if (IsPICCall || !GlobalOrExternal) {
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002811 // copy to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002812 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
2813 Chain = DAG.getCopyToReg(Chain, dl, T9Reg, Callee, SDValue(0, 0));
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002814 InFlag = Chain.getValue(1);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002815 Callee = DAG.getRegister(T9Reg, getPointerTy());
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002816 }
Bill Wendling056292f2008-09-16 21:48:12 +00002817
Akira Hatanaka92d4aec2012-05-12 03:19:04 +00002818 // Insert node "GP copy globalreg" before call to function.
2819 // Lazy-binding stubs require GP to point to the GOT.
2820 if (IsPICCall) {
2821 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
2822 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2823 RegsToPass.push_back(std::make_pair(GPReg, GetGlobalReg(DAG, Ty)));
2824 }
2825
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002826 // Build a sequence of copy-to-reg nodes chained together with token
2827 // chain and flag operands which copy the outgoing args into registers.
2828 // The InFlag in necessary since all emitted instructions must be
2829 // stuck together.
2830 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2831 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2832 RegsToPass[i].second, InFlag);
2833 InFlag = Chain.getValue(1);
2834 }
2835
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002836 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002837 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002838 //
2839 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002840 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002841 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002842 Ops.push_back(Chain);
2843 Ops.push_back(Callee);
2844
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002845 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002846 // known live into the call.
2847 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2848 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2849 RegsToPass[i].second.getValueType()));
2850
Akira Hatanakab2930b92012-03-01 22:27:29 +00002851 // Add a register mask operand representing the call-preserved registers.
2852 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2853 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2854 assert(Mask && "Missing call preserved mask for calling convention");
2855 Ops.push_back(DAG.getRegisterMask(Mask));
2856
Gabor Greifba36cb52008-08-28 21:40:38 +00002857 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002858 Ops.push_back(InFlag);
2859
Dale Johannesen33c960f2009-02-04 20:06:27 +00002860 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002861 InFlag = Chain.getValue(1);
2862
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002863 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002864 Chain = DAG.getCALLSEQ_END(Chain,
2865 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002866 DAG.getIntPtrConstant(0, true), InFlag);
2867 InFlag = Chain.getValue(1);
2868
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002869 // Handle result values, copying them out of physregs into vregs that we
2870 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002871 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2872 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002873}
2874
Dan Gohman98ca4f22009-08-05 01:29:28 +00002875/// LowerCallResult - Lower the result values of a call into the
2876/// appropriate copies out of appropriate physical registers.
2877SDValue
2878MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002879 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002880 const SmallVectorImpl<ISD::InputArg> &Ins,
2881 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002882 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002883 // Assign locations to each value returned by this call.
2884 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002885 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2886 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002887
Dan Gohman98ca4f22009-08-05 01:29:28 +00002888 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002889
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002890 // Copy all of the result registers out of their specified physreg.
2891 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002892 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002893 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002894 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002895 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002896 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002897
Dan Gohman98ca4f22009-08-05 01:29:28 +00002898 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002899}
2900
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002901//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002902// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002903//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002904static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2905 std::vector<SDValue>& OutChains,
2906 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
Akira Hatanakab4549e12012-03-27 03:13:56 +00002907 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2908 const Argument *FuncArg) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002909 unsigned LocMem = VA.getLocMemOffset();
2910 unsigned FirstWord = LocMem / 4;
2911
2912 // copy register A0 - A3 to frame object
2913 for (unsigned i = 0; i < NumWords; ++i) {
2914 unsigned CurWord = FirstWord + i;
2915 if (CurWord >= O32IntRegsSize)
2916 break;
2917
2918 unsigned SrcReg = O32IntRegs[CurWord];
Craig Topper420761a2012-04-20 07:30:17 +00002919 unsigned Reg = AddLiveIn(MF, SrcReg, &Mips::CPURegsRegClass);
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002920 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2921 DAG.getConstant(i * 4, MVT::i32));
2922 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
Akira Hatanakab4549e12012-03-27 03:13:56 +00002923 StorePtr, MachinePointerInfo(FuncArg, i * 4),
2924 false, false, 0);
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002925 OutChains.push_back(Store);
2926 }
2927}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002928
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002929// Create frame object on stack and copy registers used for byval passing to it.
2930static unsigned
2931CopyMips64ByValRegs(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2932 std::vector<SDValue>& OutChains, SelectionDAG &DAG,
2933 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2934 MachineFrameInfo *MFI, bool IsRegLoc,
2935 SmallVectorImpl<SDValue> &InVals, MipsFunctionInfo *MipsFI,
Akira Hatanakab4549e12012-03-27 03:13:56 +00002936 EVT PtrTy, const Argument *FuncArg) {
Craig Topperc5eaae42012-03-11 07:57:25 +00002937 const uint16_t *Reg = Mips64IntRegs + 8;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002938 int FOOffset; // Frame object offset from virtual frame pointer.
2939
2940 if (IsRegLoc) {
2941 Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8, VA.getLocReg());
2942 FOOffset = (Reg - Mips64IntRegs) * 8 - 8 * 8;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002943 }
2944 else
2945 FOOffset = VA.getLocMemOffset();
2946
2947 // Create frame object.
2948 unsigned NumRegs = (Flags.getByValSize() + 7) / 8;
2949 unsigned LastFI = MFI->CreateFixedObject(NumRegs * 8, FOOffset, true);
2950 SDValue FIN = DAG.getFrameIndex(LastFI, PtrTy);
2951 InVals.push_back(FIN);
2952
2953 // Copy arg registers.
2954 for (unsigned I = 0; (Reg != Mips64IntRegs + 8) && (I < NumRegs);
2955 ++Reg, ++I) {
Craig Topper420761a2012-04-20 07:30:17 +00002956 unsigned VReg = AddLiveIn(MF, *Reg, &Mips::CPU64RegsRegClass);
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002957 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, PtrTy, FIN,
2958 DAG.getConstant(I * 8, PtrTy));
2959 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(VReg, MVT::i64),
Akira Hatanakab4549e12012-03-27 03:13:56 +00002960 StorePtr, MachinePointerInfo(FuncArg, I * 8),
2961 false, false, 0);
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002962 OutChains.push_back(Store);
2963 }
Jia Liubb481f82012-02-28 07:46:26 +00002964
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002965 return LastFI;
2966}
2967
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002968/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002969/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002970SDValue
2971MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002972 CallingConv::ID CallConv,
2973 bool isVarArg,
Akira Hatanaka82099682011-12-19 19:52:25 +00002974 const SmallVectorImpl<ISD::InputArg> &Ins,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002975 DebugLoc dl, SelectionDAG &DAG,
2976 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002977 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002978 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002979 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002980 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002981
Dan Gohman1e93df62010-04-17 14:41:14 +00002982 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002983
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002984 // Used with vargs to acumulate store chains.
2985 std::vector<SDValue> OutChains;
2986
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002987 // Assign locations to all of the incoming arguments.
2988 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002989 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002990 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002991
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002992 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002993 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002994 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002995 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002996
Akira Hatanakab4549e12012-03-27 03:13:56 +00002997 Function::const_arg_iterator FuncArg =
2998 DAG.getMachineFunction().getFunction()->arg_begin();
Akira Hatanaka43299772011-05-20 23:22:14 +00002999 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003000
Akira Hatanakab4549e12012-03-27 03:13:56 +00003001 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i, ++FuncArg) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003002 CCValAssign &VA = ArgLocs[i];
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00003003 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00003004 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3005 bool IsRegLoc = VA.isRegLoc();
3006
3007 if (Flags.isByVal()) {
3008 assert(Flags.getByValSize() &&
3009 "ByVal args of size 0 should have been ignored by front-end.");
3010 if (IsO32) {
3011 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
3012 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
3013 true);
3014 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
3015 InVals.push_back(FIN);
Akira Hatanakab4549e12012-03-27 03:13:56 +00003016 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags,
3017 &*FuncArg);
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00003018 } else // N32/64
3019 LastFI = CopyMips64ByValRegs(MF, Chain, dl, OutChains, DAG, VA, Flags,
3020 MFI, IsRegLoc, InVals, MipsFI,
Akira Hatanakab4549e12012-03-27 03:13:56 +00003021 getPointerTy(), &*FuncArg);
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00003022 continue;
3023 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003024
3025 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00003026 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00003027 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00003028 unsigned ArgReg = VA.getLocReg();
Craig Topper44d23822012-02-22 05:59:10 +00003029 const TargetRegisterClass *RC;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00003030
Owen Anderson825b72b2009-08-11 20:47:22 +00003031 if (RegVT == MVT::i32)
Craig Topper420761a2012-04-20 07:30:17 +00003032 RC = &Mips::CPURegsRegClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00003033 else if (RegVT == MVT::i64)
Craig Topper420761a2012-04-20 07:30:17 +00003034 RC = &Mips::CPU64RegsRegClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003035 else if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00003036 RC = &Mips::FGR32RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00003037 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00003038 RC = HasMips64 ? &Mips::FGR64RegClass : &Mips::AFGR64RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00003039 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00003040 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003041
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003042 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003043 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00003044 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00003045 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003046
3047 // If this is an 8 or 16-bit value, it has been passed promoted
3048 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003049 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00003050 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00003051 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00003052 if (VA.getLocInfo() == CCValAssign::SExt)
3053 Opcode = ISD::AssertSext;
3054 else if (VA.getLocInfo() == CCValAssign::ZExt)
3055 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00003056 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003057 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00003058 DAG.getValueType(ValVT));
3059 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00003060 }
3061
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00003062 // Handle floating point arguments passed in integer registers.
3063 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
3064 (RegVT == MVT::i64 && ValVT == MVT::f64))
3065 ArgValue = DAG.getNode(ISD::BITCAST, dl, ValVT, ArgValue);
3066 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
3067 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
3068 getNextIntArgReg(ArgReg), RC);
3069 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
3070 if (!Subtarget->isLittle())
3071 std::swap(ArgValue, ArgValue2);
3072 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
3073 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00003074 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003075
Dan Gohman98ca4f22009-08-05 01:29:28 +00003076 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003077 } else { // VA.isRegLoc()
3078
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003079 // sanity check
3080 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00003081
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003082 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00003083 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00003084 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003085
3086 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00003087 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00003088 InVals.push_back(DAG.getLoad(ValVT, dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00003089 MachinePointerInfo::getFixedStack(LastFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003090 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003091 }
3092 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003093
3094 // The mips ABIs for returning structs by value requires that we copy
3095 // the sret argument into $v0 for the return. Save the argument into
3096 // a virtual register so that we can access it from the return points.
3097 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
3098 unsigned Reg = MipsFI->getSRetReturnReg();
3099 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003100 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003101 MipsFI->setSRetReturnReg(Reg);
3102 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00003103 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00003104 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003105 }
3106
Akira Hatanakabad53f42011-11-14 19:01:09 +00003107 if (isVarArg) {
3108 unsigned NumOfRegs = IsO32 ? 4 : 8;
Craig Topperc5eaae42012-03-11 07:57:25 +00003109 const uint16_t *ArgRegs = IsO32 ? O32IntRegs : Mips64IntRegs;
Akira Hatanakabad53f42011-11-14 19:01:09 +00003110 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumOfRegs);
3111 int FirstRegSlotOffset = IsO32 ? 0 : -64 ; // offset of $a0's slot.
Craig Topper420761a2012-04-20 07:30:17 +00003112 const TargetRegisterClass *RC = IsO32 ?
3113 (const TargetRegisterClass*)&Mips::CPURegsRegClass :
3114 (const TargetRegisterClass*)&Mips::CPU64RegsRegClass;
Akira Hatanakabad53f42011-11-14 19:01:09 +00003115 unsigned RegSize = RC->getSize();
3116 int RegSlotOffset = FirstRegSlotOffset + Idx * RegSize;
3117
3118 // Offset of the first variable argument from stack pointer.
3119 int FirstVaArgOffset;
3120
3121 if (IsO32 || (Idx == NumOfRegs)) {
3122 FirstVaArgOffset =
3123 (CCInfo.getNextStackOffset() + RegSize - 1) / RegSize * RegSize;
3124 } else
3125 FirstVaArgOffset = RegSlotOffset;
3126
Akira Hatanakab4d8d312011-05-24 00:23:52 +00003127 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00003128 // which is a value necessary to VASTART.
Akira Hatanakabad53f42011-11-14 19:01:09 +00003129 LastFI = MFI->CreateFixedObject(RegSize, FirstVaArgOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00003130 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00003131
Akira Hatanakabad53f42011-11-14 19:01:09 +00003132 // Copy the integer registers that have not been used for argument passing
3133 // to the argument register save area. For O32, the save area is allocated
3134 // in the caller's stack frame, while for N32/64, it is allocated in the
3135 // callee's stack frame.
3136 for (int StackOffset = RegSlotOffset;
3137 Idx < NumOfRegs; ++Idx, StackOffset += RegSize) {
3138 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegs[Idx], RC);
3139 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
3140 MVT::getIntegerVT(RegSize * 8));
3141 LastFI = MFI->CreateFixedObject(RegSize, StackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00003142 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
3143 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
Akira Hatanaka82099682011-12-19 19:52:25 +00003144 MachinePointerInfo(), false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00003145 }
3146 }
3147
Akira Hatanaka43299772011-05-20 23:22:14 +00003148 MipsFI->setLastInArgFI(LastFI);
3149
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003150 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00003151 // the size of Ins and InVals. This only happens when on varg functions
3152 if (!OutChains.empty()) {
3153 OutChains.push_back(Chain);
3154 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
3155 &OutChains[0], OutChains.size());
3156 }
3157
Dan Gohman98ca4f22009-08-05 01:29:28 +00003158 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003159}
3160
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00003161//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003162// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00003163//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003164
Dan Gohman98ca4f22009-08-05 01:29:28 +00003165SDValue
3166MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003167 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003168 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00003169 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00003170 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00003171
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003172 // CCValAssign - represent the assignment of
3173 // the return value to a location
3174 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003175
3176 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00003177 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
3178 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003179
Dan Gohman98ca4f22009-08-05 01:29:28 +00003180 // Analize return values.
3181 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003182
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003183 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003184 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00003185 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003186 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00003187 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00003188 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003189 }
3190
Dan Gohman475871a2008-07-27 21:46:04 +00003191 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003192
3193 // Copy the result values into the output registers.
3194 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3195 CCValAssign &VA = RVLocs[i];
3196 assert(VA.isRegLoc() && "Can only return in registers!");
3197
Akira Hatanaka82099682011-12-19 19:52:25 +00003198 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003199
3200 // guarantee that all emitted copies are
3201 // stuck together, avoiding something bad
3202 Flag = Chain.getValue(1);
3203 }
3204
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003205 // The mips ABIs for returning structs by value requires that we copy
3206 // the sret argument into $v0 for the return. We saved the argument into
3207 // a virtual register in the entry block, so now we copy the value out
3208 // and into $v0.
3209 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
3210 MachineFunction &MF = DAG.getMachineFunction();
3211 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3212 unsigned Reg = MipsFI->getSRetReturnReg();
3213
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003214 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00003215 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00003216 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003217
Dale Johannesena05dca42009-02-04 23:02:30 +00003218 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003219 Flag = Chain.getValue(1);
3220 }
3221
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003222 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00003223 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003224 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00003225 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003226 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003227 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00003228 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003229}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003230
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00003231//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003232// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00003233//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003234
3235/// getConstraintType - Given a constraint letter, return the type of
3236/// constraint it is for this target.
3237MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003238getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003239{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003240 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003241 // GCC config/mips/constraints.md
3242 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003243 // 'd' : An address register. Equivalent to r
3244 // unless generating MIPS16 code.
3245 // 'y' : Equivalent to r; retained for
3246 // backwards compatibility.
Eric Christopher1d5a3922012-05-07 06:25:10 +00003247 // 'c' : A register suitable for use in an indirect
3248 // jump. This will always be $25 for -mabicalls.
Eric Christopheraf97f732012-05-07 06:25:19 +00003249 // 'l' : The lo register. 1 word storage.
3250 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003251 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003252 switch (Constraint[0]) {
3253 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003254 case 'd':
3255 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003256 case 'f':
Eric Christopher1d5a3922012-05-07 06:25:10 +00003257 case 'c':
Eric Christopher4adbefe2012-05-07 06:25:15 +00003258 case 'l':
Eric Christopheraf97f732012-05-07 06:25:19 +00003259 case 'x':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003260 return C_RegisterClass;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003261 }
3262 }
3263 return TargetLowering::getConstraintType(Constraint);
3264}
3265
John Thompson44ab89e2010-10-29 17:29:13 +00003266/// Examine constraint type and operand type and determine a weight value.
3267/// This object must already have been set up with the operand type
3268/// and the current alternative constraint selected.
3269TargetLowering::ConstraintWeight
3270MipsTargetLowering::getSingleConstraintMatchWeight(
3271 AsmOperandInfo &info, const char *constraint) const {
3272 ConstraintWeight weight = CW_Invalid;
3273 Value *CallOperandVal = info.CallOperandVal;
3274 // If we don't have a value, we can't do a match,
3275 // but allow it at the lowest weight.
3276 if (CallOperandVal == NULL)
3277 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003278 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00003279 // Look at the constraint type.
3280 switch (*constraint) {
3281 default:
3282 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3283 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003284 case 'd':
3285 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00003286 if (type->isIntegerTy())
3287 weight = CW_Register;
3288 break;
3289 case 'f':
3290 if (type->isFloatTy())
3291 weight = CW_Register;
3292 break;
Eric Christopher1d5a3922012-05-07 06:25:10 +00003293 case 'c': // $25 for indirect jumps
Eric Christopher4adbefe2012-05-07 06:25:15 +00003294 case 'l': // lo register
Eric Christopheraf97f732012-05-07 06:25:19 +00003295 case 'x': // hilo register pair
Eric Christopher1d5a3922012-05-07 06:25:10 +00003296 if (type->isIntegerTy())
3297 weight = CW_SpecificReg;
3298 break;
Eric Christopher50ab0392012-05-07 03:13:32 +00003299 case 'I': // signed 16 bit immediate
Eric Christophere5076d42012-05-07 03:13:42 +00003300 case 'J': // integer zero
Eric Christopherf49f8462012-05-07 05:46:29 +00003301 case 'K': // unsigned 16 bit immediate
Eric Christopher5ac47bb2012-05-07 05:46:37 +00003302 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christopher60cfc792012-05-07 05:46:43 +00003303 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher1ce20342012-05-07 05:46:48 +00003304 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopher54412a72012-05-07 06:25:02 +00003305 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher50ab0392012-05-07 03:13:32 +00003306 if (isa<ConstantInt>(CallOperandVal))
3307 weight = CW_Constant;
3308 break;
John Thompson44ab89e2010-10-29 17:29:13 +00003309 }
3310 return weight;
3311}
3312
Eric Christopher38d64262011-06-29 19:33:04 +00003313/// Given a register class constraint, like 'r', if this corresponds directly
3314/// to an LLVM register class, return a register of 0 and the register class
3315/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003316std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00003317getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003318{
3319 if (Constraint.size() == 1) {
3320 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00003321 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3322 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003323 case 'r':
Eric Christopher3ccbd472012-05-07 03:13:16 +00003324 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8)
Craig Topper420761a2012-04-20 07:30:17 +00003325 return std::make_pair(0U, &Mips::CPURegsRegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00003326 if (VT == MVT::i64 && HasMips64)
3327 return std::make_pair(0U, &Mips::CPU64RegsRegClass);
3328 // This will generate an error message
3329 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003330 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00003331 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00003332 return std::make_pair(0U, &Mips::FGR32RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00003333 if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
3334 if (Subtarget->isFP64bit())
Craig Topper420761a2012-04-20 07:30:17 +00003335 return std::make_pair(0U, &Mips::FGR64RegClass);
3336 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00003337 }
Eric Christopher1d5a3922012-05-07 06:25:10 +00003338 break;
3339 case 'c': // register suitable for indirect jump
3340 if (VT == MVT::i32)
3341 return std::make_pair((unsigned)Mips::T9, &Mips::CPURegsRegClass);
3342 assert(VT == MVT::i64 && "Unexpected type.");
3343 return std::make_pair((unsigned)Mips::T9_64, &Mips::CPU64RegsRegClass);
Eric Christopher4adbefe2012-05-07 06:25:15 +00003344 case 'l': // register suitable for indirect jump
3345 if (VT == MVT::i32)
3346 return std::make_pair((unsigned)Mips::LO, &Mips::HILORegClass);
3347 return std::make_pair((unsigned)Mips::LO64, &Mips::HILO64RegClass);
Eric Christopheraf97f732012-05-07 06:25:19 +00003348 case 'x': // register suitable for indirect jump
3349 // Fixme: Not triggering the use of both hi and low
3350 // This will generate an error message
3351 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003352 }
3353 }
3354 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3355}
3356
Eric Christopher50ab0392012-05-07 03:13:32 +00003357/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3358/// vector. If it is invalid, don't add anything to Ops.
3359void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3360 std::string &Constraint,
3361 std::vector<SDValue>&Ops,
3362 SelectionDAG &DAG) const {
3363 SDValue Result(0, 0);
3364
3365 // Only support length 1 constraints for now.
3366 if (Constraint.length() > 1) return;
3367
3368 char ConstraintLetter = Constraint[0];
3369 switch (ConstraintLetter) {
3370 default: break; // This will fall through to the generic implementation
3371 case 'I': // Signed 16 bit constant
3372 // If this fails, the parent routine will give an error
3373 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3374 EVT Type = Op.getValueType();
3375 int64_t Val = C->getSExtValue();
3376 if (isInt<16>(Val)) {
3377 Result = DAG.getTargetConstant(Val, Type);
3378 break;
3379 }
3380 }
3381 return;
Eric Christophere5076d42012-05-07 03:13:42 +00003382 case 'J': // integer zero
3383 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3384 EVT Type = Op.getValueType();
3385 int64_t Val = C->getZExtValue();
3386 if (Val == 0) {
3387 Result = DAG.getTargetConstant(0, Type);
3388 break;
3389 }
3390 }
3391 return;
Eric Christopherf49f8462012-05-07 05:46:29 +00003392 case 'K': // unsigned 16 bit immediate
3393 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3394 EVT Type = Op.getValueType();
3395 uint64_t Val = (uint64_t)C->getZExtValue();
3396 if (isUInt<16>(Val)) {
3397 Result = DAG.getTargetConstant(Val, Type);
3398 break;
3399 }
3400 }
3401 return;
Eric Christopher5ac47bb2012-05-07 05:46:37 +00003402 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3403 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3404 EVT Type = Op.getValueType();
3405 int64_t Val = C->getSExtValue();
3406 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3407 Result = DAG.getTargetConstant(Val, Type);
3408 break;
3409 }
3410 }
3411 return;
Eric Christopher60cfc792012-05-07 05:46:43 +00003412 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3413 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3414 EVT Type = Op.getValueType();
3415 int64_t Val = C->getSExtValue();
3416 if ((Val >= -65535) && (Val <= -1)) {
3417 Result = DAG.getTargetConstant(Val, Type);
3418 break;
3419 }
3420 }
3421 return;
Eric Christopher1ce20342012-05-07 05:46:48 +00003422 case 'O': // signed 15 bit immediate
3423 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3424 EVT Type = Op.getValueType();
3425 int64_t Val = C->getSExtValue();
3426 if ((isInt<15>(Val))) {
3427 Result = DAG.getTargetConstant(Val, Type);
3428 break;
3429 }
3430 }
3431 return;
Eric Christopher54412a72012-05-07 06:25:02 +00003432 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3433 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3434 EVT Type = Op.getValueType();
3435 int64_t Val = C->getSExtValue();
3436 if ((Val <= 65535) && (Val >= 1)) {
3437 Result = DAG.getTargetConstant(Val, Type);
3438 break;
3439 }
3440 }
3441 return;
Eric Christopher50ab0392012-05-07 03:13:32 +00003442 }
3443
3444 if (Result.getNode()) {
3445 Ops.push_back(Result);
3446 return;
3447 }
3448
3449 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3450}
3451
Dan Gohman6520e202008-10-18 02:06:02 +00003452bool
3453MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3454 // The Mips target isn't yet aware of offsets.
3455 return false;
3456}
Evan Chengeb2f9692009-10-27 19:56:55 +00003457
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003458bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3459 if (VT != MVT::f32 && VT != MVT::f64)
3460 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00003461 if (Imm.isNegZero())
3462 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00003463 return Imm.isZero();
3464}
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003465
3466unsigned MipsTargetLowering::getJumpTableEncoding() const {
3467 if (IsN64)
3468 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liubb481f82012-02-28 07:46:26 +00003469
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003470 return TargetLowering::getJumpTableEncoding();
3471}