blob: 5aab3f8dd2f5285ca10718e856baf4453065516c [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#define DEBUG_TYPE "mips-lower"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000015#include "MipsISelLowering.h"
Craig Topper79aa3412012-03-17 18:46:09 +000016#include "InstPrinter/MipsInstPrinter.h"
17#include "MCTargetDesc/MipsBaseInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "MipsMachineFunction.h"
19#include "MipsSubtarget.h"
20#include "MipsTargetMachine.h"
21#include "MipsTargetObjectFile.h"
Akira Hatanaka2b861be2012-10-19 21:47:33 +000022#include "llvm/ADT/Statistic.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000023#include "llvm/CodeGen/CallingConvLower.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000028#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000029#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000030#include "llvm/IR/CallingConv.h"
31#include "llvm/IR/DerivedTypes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000032#include "llvm/IR/GlobalVariable.h"
Akira Hatanaka2b861be2012-10-19 21:47:33 +000033#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000034#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumi89593932012-04-21 15:31:45 +000036#include "llvm/Support/raw_ostream.h"
Akira Hatanakabfb07b12013-08-14 00:21:25 +000037#include <cctype>
NAKAMURA Takumi89593932012-04-21 15:31:45 +000038
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000039using namespace llvm;
40
Akira Hatanaka2b861be2012-10-19 21:47:33 +000041STATISTIC(NumTailCalls, "Number of tail calls");
42
43static cl::opt<bool>
Akira Hatanaka81784cb2012-11-21 20:21:11 +000044LargeGOT("mxgot", cl::Hidden,
45 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
46
Akira Hatanakaf8941992013-05-20 18:07:43 +000047static cl::opt<bool>
Akira Hatanaka2591b5c2013-05-21 17:17:59 +000048NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
Akira Hatanakaf8941992013-05-20 18:07:43 +000049 cl::desc("MIPS: Don't trap on integer division by zero."),
50 cl::init(false));
51
Akira Hatanakafe30a9b2012-10-27 00:29:43 +000052static const uint16_t O32IntRegs[4] = {
53 Mips::A0, Mips::A1, Mips::A2, Mips::A3
54};
55
56static const uint16_t Mips64IntRegs[8] = {
57 Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
58 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64
59};
60
61static const uint16_t Mips64DPRegs[8] = {
62 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
63 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
64};
65
Jia Liubb481f82012-02-28 07:46:26 +000066// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanakadbe9a312011-08-18 20:07:42 +000067// mask (Pos), and return true.
Jia Liubb481f82012-02-28 07:46:26 +000068// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanakaf635ef42013-03-12 00:16:36 +000069static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanakad6bc5232011-12-05 21:26:34 +000070 if (!isShiftedMask_64(I))
Akira Hatanaka854a7db2011-08-19 22:59:00 +000071 return false;
Akira Hatanakabb15e112011-08-17 02:05:42 +000072
Akira Hatanakad6bc5232011-12-05 21:26:34 +000073 Size = CountPopulation_64(I);
Michael J. Spencerc6af2432013-05-24 22:23:49 +000074 Pos = countTrailingZeros(I);
Akira Hatanakadbe9a312011-08-18 20:07:42 +000075 return true;
Akira Hatanakabb15e112011-08-17 02:05:42 +000076}
77
Akira Hatanaka5ac065a2013-03-13 00:54:29 +000078SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
Akira Hatanaka648f00c2012-02-24 22:34:47 +000079 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
80 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
81}
82
Akira Hatanaka200a7432013-09-27 19:51:35 +000083SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
84 SelectionDAG &DAG,
Akira Hatanaka5ac065a2013-03-13 00:54:29 +000085 unsigned Flag) const {
Akira Hatanaka200a7432013-09-27 19:51:35 +000086 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
Akira Hatanaka6b28b802012-11-21 20:26:38 +000087}
88
Akira Hatanaka200a7432013-09-27 19:51:35 +000089SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty,
90 SelectionDAG &DAG,
91 unsigned Flag) const {
92 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
93}
94
95SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty,
96 SelectionDAG &DAG,
97 unsigned Flag) const {
98 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
99}
100
101SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
102 SelectionDAG &DAG,
103 unsigned Flag) const {
104 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
105}
106
107SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
108 SelectionDAG &DAG,
109 unsigned Flag) const {
110 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
111 N->getOffset(), Flag);
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000112}
113
Chris Lattnerf0144122009-07-28 03:13:23 +0000114const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
115 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000116 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka58d1e3f2012-10-19 20:59:39 +0000117 case MipsISD::TailCall: return "MipsISD::TailCall";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000118 case MipsISD::Hi: return "MipsISD::Hi";
119 case MipsISD::Lo: return "MipsISD::Lo";
120 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000121 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000122 case MipsISD::Ret: return "MipsISD::Ret";
Akira Hatanaka544cc212013-01-30 00:26:49 +0000123 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000124 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
125 case MipsISD::FPCmp: return "MipsISD::FPCmp";
126 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
127 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000128 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakadd958922013-03-30 01:14:04 +0000129 case MipsISD::ExtractLOHI: return "MipsISD::ExtractLOHI";
130 case MipsISD::InsertLOHI: return "MipsISD::InsertLOHI";
131 case MipsISD::Mult: return "MipsISD::Mult";
132 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000133 case MipsISD::MAdd: return "MipsISD::MAdd";
134 case MipsISD::MAddu: return "MipsISD::MAddu";
135 case MipsISD::MSub: return "MipsISD::MSub";
136 case MipsISD::MSubu: return "MipsISD::MSubu";
137 case MipsISD::DivRem: return "MipsISD::DivRem";
138 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanakadd958922013-03-30 01:14:04 +0000139 case MipsISD::DivRem16: return "MipsISD::DivRem16";
140 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000141 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
142 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakabfcb83f2011-12-12 22:38:19 +0000143 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanakadb548262011-07-19 23:30:50 +0000144 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanakabb15e112011-08-17 02:05:42 +0000145 case MipsISD::Ext: return "MipsISD::Ext";
146 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab6f1dc22012-06-02 00:03:12 +0000147 case MipsISD::LWL: return "MipsISD::LWL";
148 case MipsISD::LWR: return "MipsISD::LWR";
149 case MipsISD::SWL: return "MipsISD::SWL";
150 case MipsISD::SWR: return "MipsISD::SWR";
151 case MipsISD::LDL: return "MipsISD::LDL";
152 case MipsISD::LDR: return "MipsISD::LDR";
153 case MipsISD::SDL: return "MipsISD::SDL";
154 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka6fad5e72012-09-21 23:52:47 +0000155 case MipsISD::EXTP: return "MipsISD::EXTP";
156 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
157 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
158 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
159 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
160 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
161 case MipsISD::SHILO: return "MipsISD::SHILO";
162 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
163 case MipsISD::MULT: return "MipsISD::MULT";
164 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liub3ea8802013-03-04 01:06:54 +0000165 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka6fad5e72012-09-21 23:52:47 +0000166 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
167 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
168 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka97a62bf2013-04-19 23:21:32 +0000169 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
170 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
171 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000172 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
173 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sanders3c380d52013-08-28 12:14:50 +0000174 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
175 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
176 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
177 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersae1fb8f2013-09-24 10:46:19 +0000178 case MipsISD::VCEQ: return "MipsISD::VCEQ";
179 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
180 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
181 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
182 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders89d13c12013-09-24 12:18:31 +0000183 case MipsISD::VSMAX: return "MipsISD::VSMAX";
184 case MipsISD::VSMIN: return "MipsISD::VSMIN";
185 case MipsISD::VUMAX: return "MipsISD::VUMAX";
186 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +0000187 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
188 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sanders915432c2013-09-23 13:22:24 +0000189 case MipsISD::VNOR: return "MipsISD::VNOR";
Daniel Sanders7e0df9a2013-09-24 14:02:15 +0000190 case MipsISD::VSHF: return "MipsISD::VSHF";
Daniel Sanders93d99572013-09-24 14:20:00 +0000191 case MipsISD::SHF: return "MipsISD::SHF";
Daniel Sandersf5159642013-09-24 14:36:12 +0000192 case MipsISD::ILVEV: return "MipsISD::ILVEV";
193 case MipsISD::ILVOD: return "MipsISD::ILVOD";
194 case MipsISD::ILVL: return "MipsISD::ILVL";
195 case MipsISD::ILVR: return "MipsISD::ILVR";
Daniel Sanders3706eda2013-09-24 14:53:25 +0000196 case MipsISD::PCKEV: return "MipsISD::PCKEV";
197 case MipsISD::PCKOD: return "MipsISD::PCKOD";
Akira Hatanaka0f843822011-06-07 18:58:42 +0000198 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000199 }
200}
201
202MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +0000203MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka8b4198d2011-09-26 21:47:02 +0000204 : TargetLowering(TM, new MipsTargetObjectFile()),
205 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +0000206 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
207 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000208 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000209 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +0000210 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000211 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000212
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000213 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000214 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
215 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
216 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000217
Eli Friedman6055a6a2009-07-17 04:07:24 +0000218 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000219 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
220 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000221
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000222 // Used by legalize types to correctly generate the setcc result.
223 // Without this, every float setcc comes with a AND/OR with the result,
224 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000225 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000227
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000228 // Mips Custom Operations
Akira Hatanakab7656a92013-03-06 21:32:03 +0000229 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000231 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
233 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
234 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
235 setOperationAction(ISD::SELECT, MVT::f32, Custom);
236 setOperationAction(ISD::SELECT, MVT::f64, Custom);
237 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +0000238 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
239 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanaka0a40c232012-03-09 23:46:03 +0000240 setOperationAction(ISD::SETCC, MVT::f32, Custom);
241 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000243 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000244 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
245 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000246 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000247
Akira Hatanakac12a6e62012-04-11 22:49:04 +0000248 if (!TM.Options.NoNaNsFPMath) {
249 setOperationAction(ISD::FABS, MVT::f32, Custom);
250 setOperationAction(ISD::FABS, MVT::f64, Custom);
251 }
252
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000253 if (HasMips64) {
254 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
255 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
256 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
257 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
258 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
259 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka7664f052012-06-02 00:04:42 +0000260 setOperationAction(ISD::LOAD, MVT::i64, Custom);
261 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000262 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000263 }
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000264
Akira Hatanakaa284acb2012-05-09 00:55:21 +0000265 if (!HasMips64) {
266 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
267 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
268 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
269 }
270
Akira Hatanakae90a3bc2012-11-07 19:10:58 +0000271 setOperationAction(ISD::ADD, MVT::i32, Custom);
272 if (HasMips64)
273 setOperationAction(ISD::ADD, MVT::i64, Custom);
274
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000275 setOperationAction(ISD::SDIV, MVT::i32, Expand);
276 setOperationAction(ISD::SREM, MVT::i32, Expand);
277 setOperationAction(ISD::UDIV, MVT::i32, Expand);
278 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000279 setOperationAction(ISD::SDIV, MVT::i64, Expand);
280 setOperationAction(ISD::SREM, MVT::i64, Expand);
281 setOperationAction(ISD::UDIV, MVT::i64, Expand);
282 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000283
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000284 // Operations not directly supported by Mips.
Tom Stellard3ef53832013-03-08 15:36:57 +0000285 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
286 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
287 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
288 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
290 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000291 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000293 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000294 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
295 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000296 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000297 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000298 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000299 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
300 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
301 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
302 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000304 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka1d165f12012-07-31 20:54:48 +0000305 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
306 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000307
Akira Hatanaka56633442011-09-20 23:53:09 +0000308 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000309 setOperationAction(ISD::ROTR, MVT::i32, Expand);
310
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000311 if (!Subtarget->hasMips64r2())
312 setOperationAction(ISD::ROTR, MVT::i64, Expand);
313
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000315 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000316 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000317 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000318 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
319 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
321 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000322 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FLOG, MVT::f32, Expand);
324 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
325 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
326 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000327 setOperationAction(ISD::FMA, MVT::f32, Expand);
328 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka21ecc2f2012-03-29 18:43:11 +0000329 setOperationAction(ISD::FREM, MVT::f32, Expand);
330 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000331
Akira Hatanaka1cc63332012-04-11 22:59:08 +0000332 if (!TM.Options.NoNaNsFPMath) {
333 setOperationAction(ISD::FNEG, MVT::f32, Expand);
334 setOperationAction(ISD::FNEG, MVT::f64, Expand);
335 }
336
Akira Hatanaka544cc212013-01-30 00:26:49 +0000337 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
338
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000339 setOperationAction(ISD::VAARG, MVT::Other, Expand);
340 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
341 setOperationAction(ISD::VAEND, MVT::Other, Expand);
342
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000343 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
345 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000346
Jia Liubb481f82012-02-28 07:46:26 +0000347 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
348 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
349 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
350 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman4db5aca2011-08-29 18:23:02 +0000351
Eli Friedman26689ac2011-08-03 21:06:02 +0000352 setInsertFencesForAtomic(true);
353
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000354 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
356 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000357 }
358
Akira Hatanakac79507a2011-12-21 00:20:27 +0000359 if (!Subtarget->hasBitCount()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000360 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Akira Hatanakac79507a2011-12-21 00:20:27 +0000361 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
362 }
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000363
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000364 if (!Subtarget->hasSwap()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000365 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000366 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
367 }
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000368
Akira Hatanaka7664f052012-06-02 00:04:42 +0000369 if (HasMips64) {
370 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
371 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
372 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
373 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
374 }
375
Akira Hatanaka97585622013-07-26 20:58:55 +0000376 setOperationAction(ISD::TRAP, MVT::Other, Legal);
377
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000378 setTargetDAGCombine(ISD::SDIVREM);
379 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanakaee8c3b02012-03-08 03:26:37 +0000380 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000381 setTargetDAGCombine(ISD::AND);
382 setTargetDAGCombine(ISD::OR);
Akira Hatanaka87827072012-06-13 20:33:18 +0000383 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000384
Akira Hatanaka5fdf5002012-03-08 01:59:33 +0000385 setMinFunctionAlignment(HasMips64 ? 3 : 2);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000386
Akira Hatanaka3f5b1072012-02-02 03:17:04 +0000387 setStackPointerRegisterToSaveRestore(IsN64 ? Mips::SP_64 : Mips::SP);
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000388
Akira Hatanaka590baca2012-02-02 03:13:40 +0000389 setExceptionPointerRegister(IsN64 ? Mips::A0_64 : Mips::A0);
390 setExceptionSelectorRegister(IsN64 ? Mips::A1_64 : Mips::A1);
Akira Hatanakae193b322012-06-13 19:33:32 +0000391
Jim Grosbach3450f802013-02-20 21:13:59 +0000392 MaxStoresPerMemcpy = 16;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000393}
394
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000395const MipsTargetLowering *MipsTargetLowering::create(MipsTargetMachine &TM) {
396 if (TM.getSubtargetImpl()->inMips16Mode())
397 return llvm::createMips16TargetLowering(TM);
Jia Liubb481f82012-02-28 07:46:26 +0000398
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000399 return llvm::createMipsSETargetLowering(TM);
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000400}
401
Matt Arsenault225ed702013-05-18 00:21:46 +0000402EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakae13f4412013-01-04 20:06:01 +0000403 if (!VT.isVector())
404 return MVT::i32;
405 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +0000406}
407
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000408static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000409 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000410 const MipsSubtarget *Subtarget) {
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000411 if (DCI.isBeforeLegalizeOps())
412 return SDValue();
413
Akira Hatanakadda4a072011-10-03 21:06:13 +0000414 EVT Ty = N->getValueType(0);
Akira Hatanakacbaf6d02013-08-14 00:47:08 +0000415 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
416 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000417 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
418 MipsISD::DivRemU16;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000419 SDLoc DL(N);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000420
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000421 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000422 N->getOperand(0), N->getOperand(1));
423 SDValue InChain = DAG.getEntryNode();
424 SDValue InGlue = DivRem;
425
426 // insert MFLO
427 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000428 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000429 InGlue);
430 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
431 InChain = CopyFromLo.getValue(1);
432 InGlue = CopyFromLo.getValue(2);
433 }
434
435 // insert MFHI
436 if (N->hasAnyUseOfValue(1)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000437 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000438 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000439 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
440 }
441
442 return SDValue();
443}
444
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000445static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000446 switch (CC) {
447 default: llvm_unreachable("Unknown fp condition code!");
448 case ISD::SETEQ:
449 case ISD::SETOEQ: return Mips::FCOND_OEQ;
450 case ISD::SETUNE: return Mips::FCOND_UNE;
451 case ISD::SETLT:
452 case ISD::SETOLT: return Mips::FCOND_OLT;
453 case ISD::SETGT:
454 case ISD::SETOGT: return Mips::FCOND_OGT;
455 case ISD::SETLE:
456 case ISD::SETOLE: return Mips::FCOND_OLE;
457 case ISD::SETGE:
458 case ISD::SETOGE: return Mips::FCOND_OGE;
459 case ISD::SETULT: return Mips::FCOND_ULT;
460 case ISD::SETULE: return Mips::FCOND_ULE;
461 case ISD::SETUGT: return Mips::FCOND_UGT;
462 case ISD::SETUGE: return Mips::FCOND_UGE;
463 case ISD::SETUO: return Mips::FCOND_UN;
464 case ISD::SETO: return Mips::FCOND_OR;
465 case ISD::SETNE:
466 case ISD::SETONE: return Mips::FCOND_ONE;
467 case ISD::SETUEQ: return Mips::FCOND_UEQ;
468 }
469}
470
471
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000472/// This function returns true if the floating point conditional branches and
473/// conditional moves which use condition code CC should be inverted.
474static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000475 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
476 return false;
477
Akira Hatanaka82099682011-12-19 19:52:25 +0000478 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
479 "Illegal Condition Code");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000480
Akira Hatanaka82099682011-12-19 19:52:25 +0000481 return true;
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000482}
483
484// Creates and returns an FPCmp node from a setcc node.
485// Returns Op if setcc is not a floating point comparison.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000486static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000487 // must be a SETCC node
488 if (Op.getOpcode() != ISD::SETCC)
489 return Op;
490
491 SDValue LHS = Op.getOperand(0);
492
493 if (!LHS.getValueType().isFloatingPoint())
494 return Op;
495
496 SDValue RHS = Op.getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000497 SDLoc DL(Op);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000498
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000499 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
500 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000501 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
502
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000503 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000504 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000505}
506
507// Creates and returns a CMovFPT/F node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000508static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000509 SDValue False, SDLoc DL) {
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000510 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
511 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka407883b2013-07-26 20:51:20 +0000512 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000513
514 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka407883b2013-07-26 20:51:20 +0000515 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000516}
517
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000518static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000519 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000520 const MipsSubtarget *Subtarget) {
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000521 if (DCI.isBeforeLegalizeOps())
522 return SDValue();
523
524 SDValue SetCC = N->getOperand(0);
525
526 if ((SetCC.getOpcode() != ISD::SETCC) ||
527 !SetCC.getOperand(0).getValueType().isInteger())
528 return SDValue();
529
530 SDValue False = N->getOperand(2);
531 EVT FalseTy = False.getValueType();
532
533 if (!FalseTy.isInteger())
534 return SDValue();
535
536 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(False);
537
538 if (!CN || CN->getZExtValue())
539 return SDValue();
540
Andrew Trickac6d9be2013-05-25 02:42:55 +0000541 const SDLoc DL(N);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000542 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
543 SDValue True = N->getOperand(1);
Akira Hatanaka864f6602012-06-14 21:10:56 +0000544
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000545 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
546 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
Akira Hatanaka864f6602012-06-14 21:10:56 +0000547
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000548 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
549}
550
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000551static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000552 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000553 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000554 // Pattern match EXT.
555 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
556 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000557 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000558 return SDValue();
559
560 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000561 unsigned ShiftRightOpc = ShiftRight.getOpcode();
562
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000563 // Op's first operand must be a shift right.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000564 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000565 return SDValue();
566
567 // The second operand of the shift must be an immediate.
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000568 ConstantSDNode *CN;
569 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
570 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000571
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000572 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000573 uint64_t SMPos, SMSize;
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000574
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000575 // Op's second operand must be a shifted mask.
576 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000577 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000578 return SDValue();
579
580 // Return if the shifted mask does not start at bit 0 or the sum of its size
581 // and Pos exceeds the word's size.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000582 EVT ValTy = N->getValueType(0);
583 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000584 return SDValue();
585
Andrew Trickac6d9be2013-05-25 02:42:55 +0000586 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka82099682011-12-19 19:52:25 +0000587 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000588 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000589}
Jia Liubb481f82012-02-28 07:46:26 +0000590
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000591static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000592 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000593 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000594 // Pattern match INS.
595 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liubb481f82012-02-28 07:46:26 +0000596 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000597 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000598 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000599 return SDValue();
600
601 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
602 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
603 ConstantSDNode *CN;
604
605 // See if Op's first operand matches (and $src1 , mask0).
606 if (And0.getOpcode() != ISD::AND)
607 return SDValue();
608
609 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000610 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000611 return SDValue();
612
613 // See if Op's second operand matches (and (shl $src, pos), mask1).
614 if (And1.getOpcode() != ISD::AND)
615 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000616
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000617 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000618 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000619 return SDValue();
620
621 // The shift masks must have the same position and size.
622 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
623 return SDValue();
624
625 SDValue Shl = And1.getOperand(0);
626 if (Shl.getOpcode() != ISD::SHL)
627 return SDValue();
628
629 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
630 return SDValue();
631
632 unsigned Shamt = CN->getZExtValue();
633
634 // Return if the shift amount and the first bit position of mask are not the
Jia Liubb481f82012-02-28 07:46:26 +0000635 // same.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000636 EVT ValTy = N->getValueType(0);
637 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000638 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000639
Andrew Trickac6d9be2013-05-25 02:42:55 +0000640 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000641 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka82099682011-12-19 19:52:25 +0000642 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000643}
Jia Liubb481f82012-02-28 07:46:26 +0000644
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000645static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka87827072012-06-13 20:33:18 +0000646 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000647 const MipsSubtarget *Subtarget) {
Akira Hatanaka87827072012-06-13 20:33:18 +0000648 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
649
650 if (DCI.isBeforeLegalizeOps())
651 return SDValue();
652
653 SDValue Add = N->getOperand(1);
654
655 if (Add.getOpcode() != ISD::ADD)
656 return SDValue();
657
658 SDValue Lo = Add.getOperand(1);
659
660 if ((Lo.getOpcode() != MipsISD::Lo) ||
661 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
662 return SDValue();
663
664 EVT ValTy = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000665 SDLoc DL(N);
Akira Hatanaka87827072012-06-13 20:33:18 +0000666
667 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
668 Add.getOperand(0));
669 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
670}
671
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000672SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000673 const {
674 SelectionDAG &DAG = DCI.DAG;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000675 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000676
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000677 switch (Opc) {
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000678 default: break;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000679 case ISD::SDIVREM:
680 case ISD::UDIVREM:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000681 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000682 case ISD::SELECT:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000683 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000684 case ISD::AND:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000685 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000686 case ISD::OR:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000687 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka87827072012-06-13 20:33:18 +0000688 case ISD::ADD:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000689 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000690 }
691
692 return SDValue();
693}
694
Akira Hatanakab430cec2012-09-21 23:58:31 +0000695void
696MipsTargetLowering::LowerOperationWrapper(SDNode *N,
697 SmallVectorImpl<SDValue> &Results,
698 SelectionDAG &DAG) const {
699 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
700
701 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
702 Results.push_back(Res.getValue(I));
703}
704
705void
706MipsTargetLowering::ReplaceNodeResults(SDNode *N,
707 SmallVectorImpl<SDValue> &Results,
708 SelectionDAG &DAG) const {
Akira Hatanaka13ec4812013-04-30 21:17:07 +0000709 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakab430cec2012-09-21 23:58:31 +0000710}
711
Dan Gohman475871a2008-07-27 21:46:04 +0000712SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000713LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000714{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000715 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000716 {
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000717 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
718 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
719 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
720 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
721 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
722 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
723 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
724 case ISD::SELECT: return lowerSELECT(Op, DAG);
725 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
726 case ISD::SETCC: return lowerSETCC(Op, DAG);
727 case ISD::VASTART: return lowerVASTART(Op, DAG);
728 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
729 case ISD::FABS: return lowerFABS(Op, DAG);
730 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
731 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
732 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000733 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
734 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
735 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
736 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
737 case ISD::LOAD: return lowerLOAD(Op, DAG);
738 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000739 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000740 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000741 }
Dan Gohman475871a2008-07-27 21:46:04 +0000742 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000743}
744
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000745//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000746// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000747//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000748
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000749// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000750// MachineFunction as a live in value. It also creates a corresponding
751// virtual register for it.
752static unsigned
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000753addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000754{
Chris Lattner84bc5422007-12-31 04:13:23 +0000755 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
756 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000757 return VReg;
758}
759
Akira Hatanakaf8941992013-05-20 18:07:43 +0000760static MachineBasicBlock *expandPseudoDIV(MachineInstr *MI,
761 MachineBasicBlock &MBB,
762 const TargetInstrInfo &TII,
763 bool Is64Bit) {
764 if (NoZeroDivCheck)
765 return &MBB;
766
767 // Insert instruction "teq $divisor_reg, $zero, 7".
768 MachineBasicBlock::iterator I(MI);
769 MachineInstrBuilder MIB;
770 MIB = BuildMI(MBB, llvm::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
771 .addOperand(MI->getOperand(2)).addReg(Mips::ZERO).addImm(7);
772
773 // Use the 32-bit sub-register if this is a 64-bit division.
774 if (Is64Bit)
775 MIB->getOperand(0).setSubReg(Mips::sub_32);
776
777 return &MBB;
778}
779
Akira Hatanaka01f70892012-09-27 02:15:57 +0000780MachineBasicBlock *
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000781MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000782 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000783 switch (MI->getOpcode()) {
Reed Kotlerffbe4322013-02-21 04:22:38 +0000784 default:
785 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000786 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000787 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000788 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000789 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000790 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000791 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000792 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000793 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000794
795 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000796 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000797 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000798 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000799 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000800 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000801 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000802 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000803
804 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000805 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000806 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000807 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000808 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000809 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000810 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000811 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000812
813 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000814 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000815 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000816 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000817 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000818 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000819 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000820 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000821
822 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000823 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000824 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000825 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000826 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000827 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000828 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000829 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000830
831 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000832 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000833 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000834 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000835 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000836 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000837 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000838 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000839
840 case Mips::ATOMIC_SWAP_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000841 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000842 case Mips::ATOMIC_SWAP_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000843 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000844 case Mips::ATOMIC_SWAP_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000845 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000846 case Mips::ATOMIC_SWAP_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000847 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000848
849 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000850 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000851 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000852 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000853 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000854 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000855 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000856 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanakaf8941992013-05-20 18:07:43 +0000857 case Mips::PseudoSDIV:
858 case Mips::PseudoUDIV:
859 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), false);
860 case Mips::PseudoDSDIV:
861 case Mips::PseudoDUDIV:
862 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), true);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000863 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000864}
865
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000866// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
867// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
868MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000869MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000870 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000871 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000872 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000873
874 MachineFunction *MF = BB->getParent();
875 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000876 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000877 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000878 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000879 unsigned LL, SC, AND, NOR, ZERO, BEQ;
880
881 if (Size == 4) {
Akira Hatanakaa98a4862013-08-20 21:08:22 +0000882 LL = Mips::LL;
883 SC = Mips::SC;
Akira Hatanaka59068062011-11-11 04:14:30 +0000884 AND = Mips::AND;
885 NOR = Mips::NOR;
886 ZERO = Mips::ZERO;
887 BEQ = Mips::BEQ;
888 }
889 else {
Akira Hatanakaa98a4862013-08-20 21:08:22 +0000890 LL = Mips::LLD;
891 SC = Mips::SCD;
Akira Hatanaka59068062011-11-11 04:14:30 +0000892 AND = Mips::AND64;
893 NOR = Mips::NOR64;
894 ZERO = Mips::ZERO_64;
895 BEQ = Mips::BEQ64;
896 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000897
Akira Hatanaka4061da12011-07-19 20:11:17 +0000898 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000899 unsigned Ptr = MI->getOperand(1).getReg();
900 unsigned Incr = MI->getOperand(2).getReg();
901
Akira Hatanaka4061da12011-07-19 20:11:17 +0000902 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
903 unsigned AndRes = RegInfo.createVirtualRegister(RC);
904 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000905
906 // insert new blocks after the current block
907 const BasicBlock *LLVM_BB = BB->getBasicBlock();
908 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
909 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
910 MachineFunction::iterator It = BB;
911 ++It;
912 MF->insert(It, loopMBB);
913 MF->insert(It, exitMBB);
914
915 // Transfer the remainder of BB and its successor edges to exitMBB.
916 exitMBB->splice(exitMBB->begin(), BB,
917 llvm::next(MachineBasicBlock::iterator(MI)),
918 BB->end());
919 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
920
921 // thisMBB:
922 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000923 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000924 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000925 loopMBB->addSuccessor(loopMBB);
926 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000927
928 // loopMBB:
929 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000930 // <binop> storeval, oldval, incr
931 // sc success, storeval, 0(ptr)
932 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000933 BB = loopMBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000934 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000935 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000936 // and andres, oldval, incr
937 // nor storeval, $0, andres
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000938 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
939 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000940 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000941 // <binop> storeval, oldval, incr
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000942 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000943 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000944 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000945 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000946 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
947 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000948
949 MI->eraseFromParent(); // The instruction is gone now.
950
Akira Hatanaka939ece12011-07-19 03:42:13 +0000951 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000952}
953
954MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000955MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000956 MachineBasicBlock *BB,
957 unsigned Size, unsigned BinOpcode,
958 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000959 assert((Size == 1 || Size == 2) &&
960 "Unsupported size for EmitAtomicBinaryPartial.");
961
962 MachineFunction *MF = BB->getParent();
963 MachineRegisterInfo &RegInfo = MF->getRegInfo();
964 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
965 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000966 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000967
968 unsigned Dest = MI->getOperand(0).getReg();
969 unsigned Ptr = MI->getOperand(1).getReg();
970 unsigned Incr = MI->getOperand(2).getReg();
971
Akira Hatanaka4061da12011-07-19 20:11:17 +0000972 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
973 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000974 unsigned Mask = RegInfo.createVirtualRegister(RC);
975 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000976 unsigned NewVal = RegInfo.createVirtualRegister(RC);
977 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000978 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000979 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
980 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
981 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
982 unsigned AndRes = RegInfo.createVirtualRegister(RC);
983 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000984 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000985 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
986 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
987 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
988 unsigned SllRes = RegInfo.createVirtualRegister(RC);
989 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000990
991 // insert new blocks after the current block
992 const BasicBlock *LLVM_BB = BB->getBasicBlock();
993 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000994 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000995 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
996 MachineFunction::iterator It = BB;
997 ++It;
998 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000999 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001000 MF->insert(It, exitMBB);
1001
1002 // Transfer the remainder of BB and its successor edges to exitMBB.
1003 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001004 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001005 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1006
Akira Hatanaka81b44112011-07-19 17:09:53 +00001007 BB->addSuccessor(loopMBB);
1008 loopMBB->addSuccessor(loopMBB);
1009 loopMBB->addSuccessor(sinkMBB);
1010 sinkMBB->addSuccessor(exitMBB);
1011
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001012 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001013 // addiu masklsb2,$0,-4 # 0xfffffffc
1014 // and alignedaddr,ptr,masklsb2
1015 // andi ptrlsb2,ptr,3
1016 // sll shiftamt,ptrlsb2,3
1017 // ori maskupper,$0,255 # 0xff
1018 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001019 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001020 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001021
1022 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001023 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001024 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001025 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001026 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001027 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001028 if (Subtarget->isLittle()) {
1029 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1030 } else {
1031 unsigned Off = RegInfo.createVirtualRegister(RC);
1032 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1033 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1034 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1035 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001036 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001037 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001038 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001039 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001040 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka51122432013-07-01 20:39:53 +00001041 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001042
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001043 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001044 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001045 // ll oldval,0(alignedaddr)
1046 // binop binopres,oldval,incr2
1047 // and newval,binopres,mask
1048 // and maskedoldval0,oldval,mask2
1049 // or storeval,maskedoldval0,newval
1050 // sc success,storeval,0(alignedaddr)
1051 // beq success,$0,loopMBB
1052
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001053 // atomic.swap
1054 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001055 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001056 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001057 // and maskedoldval0,oldval,mask2
1058 // or storeval,maskedoldval0,newval
1059 // sc success,storeval,0(alignedaddr)
1060 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001061
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001062 BB = loopMBB;
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001063 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001064 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001065 // and andres, oldval, incr2
1066 // nor binopres, $0, andres
1067 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001068 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1069 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001070 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001071 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001072 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001073 // <binop> binopres, oldval, incr2
1074 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001075 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1076 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001077 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001078 // and newval, incr2, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001079 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001080 }
Jia Liubb481f82012-02-28 07:46:26 +00001081
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001082 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001083 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001084 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001085 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001086 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001087 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001088 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001089 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001090
Akira Hatanaka939ece12011-07-19 03:42:13 +00001091 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001092 // and maskedoldval1,oldval,mask
1093 // srl srlres,maskedoldval1,shiftamt
1094 // sll sllres,srlres,24
1095 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001096 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001097 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001098
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001099 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001100 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001101 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001102 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001103 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001104 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001105 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001106 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001107
1108 MI->eraseFromParent(); // The instruction is gone now.
1109
Akira Hatanaka939ece12011-07-19 03:42:13 +00001110 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001111}
1112
1113MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001114MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001115 MachineBasicBlock *BB,
1116 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001117 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001118
1119 MachineFunction *MF = BB->getParent();
1120 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001121 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001122 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001123 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001124 unsigned LL, SC, ZERO, BNE, BEQ;
1125
1126 if (Size == 4) {
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001127 LL = Mips::LL;
1128 SC = Mips::SC;
Akira Hatanaka59068062011-11-11 04:14:30 +00001129 ZERO = Mips::ZERO;
1130 BNE = Mips::BNE;
1131 BEQ = Mips::BEQ;
1132 }
1133 else {
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001134 LL = Mips::LLD;
1135 SC = Mips::SCD;
Akira Hatanaka59068062011-11-11 04:14:30 +00001136 ZERO = Mips::ZERO_64;
1137 BNE = Mips::BNE64;
1138 BEQ = Mips::BEQ64;
1139 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001140
1141 unsigned Dest = MI->getOperand(0).getReg();
1142 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001143 unsigned OldVal = MI->getOperand(2).getReg();
1144 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001145
Akira Hatanaka4061da12011-07-19 20:11:17 +00001146 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001147
1148 // insert new blocks after the current block
1149 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1150 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1151 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1152 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1153 MachineFunction::iterator It = BB;
1154 ++It;
1155 MF->insert(It, loop1MBB);
1156 MF->insert(It, loop2MBB);
1157 MF->insert(It, exitMBB);
1158
1159 // Transfer the remainder of BB and its successor edges to exitMBB.
1160 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001161 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001162 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1163
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001164 // thisMBB:
1165 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001166 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001167 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001168 loop1MBB->addSuccessor(exitMBB);
1169 loop1MBB->addSuccessor(loop2MBB);
1170 loop2MBB->addSuccessor(loop1MBB);
1171 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001172
1173 // loop1MBB:
1174 // ll dest, 0(ptr)
1175 // bne dest, oldval, exitMBB
1176 BB = loop1MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001177 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1178 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001179 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001180
1181 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001182 // sc success, newval, 0(ptr)
1183 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001184 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001185 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001186 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001187 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka59068062011-11-11 04:14:30 +00001188 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001189
1190 MI->eraseFromParent(); // The instruction is gone now.
1191
Akira Hatanaka939ece12011-07-19 03:42:13 +00001192 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001193}
1194
1195MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001196MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001197 MachineBasicBlock *BB,
1198 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001199 assert((Size == 1 || Size == 2) &&
1200 "Unsupported size for EmitAtomicCmpSwapPartial.");
1201
1202 MachineFunction *MF = BB->getParent();
1203 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1204 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1205 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001206 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001207
1208 unsigned Dest = MI->getOperand(0).getReg();
1209 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001210 unsigned CmpVal = MI->getOperand(2).getReg();
1211 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001212
Akira Hatanaka4061da12011-07-19 20:11:17 +00001213 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1214 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001215 unsigned Mask = RegInfo.createVirtualRegister(RC);
1216 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001217 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1218 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1219 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1220 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1221 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1222 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1223 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1224 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1225 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1226 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1227 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1228 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1229 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1230 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001231
1232 // insert new blocks after the current block
1233 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1234 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1235 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001236 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001237 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1238 MachineFunction::iterator It = BB;
1239 ++It;
1240 MF->insert(It, loop1MBB);
1241 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001242 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001243 MF->insert(It, exitMBB);
1244
1245 // Transfer the remainder of BB and its successor edges to exitMBB.
1246 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001247 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001248 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1249
Akira Hatanaka81b44112011-07-19 17:09:53 +00001250 BB->addSuccessor(loop1MBB);
1251 loop1MBB->addSuccessor(sinkMBB);
1252 loop1MBB->addSuccessor(loop2MBB);
1253 loop2MBB->addSuccessor(loop1MBB);
1254 loop2MBB->addSuccessor(sinkMBB);
1255 sinkMBB->addSuccessor(exitMBB);
1256
Akira Hatanaka70564a92011-07-19 18:14:26 +00001257 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001258 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001259 // addiu masklsb2,$0,-4 # 0xfffffffc
1260 // and alignedaddr,ptr,masklsb2
1261 // andi ptrlsb2,ptr,3
1262 // sll shiftamt,ptrlsb2,3
1263 // ori maskupper,$0,255 # 0xff
1264 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001265 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001266 // andi maskedcmpval,cmpval,255
1267 // sll shiftedcmpval,maskedcmpval,shiftamt
1268 // andi maskednewval,newval,255
1269 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001270 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001271 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001272 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001273 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001274 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001275 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001276 if (Subtarget->isLittle()) {
1277 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1278 } else {
1279 unsigned Off = RegInfo.createVirtualRegister(RC);
1280 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1281 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1282 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1283 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001284 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001285 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001286 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001287 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001288 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1289 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001290 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001291 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001292 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001293 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001294 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001295 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001296 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001297
1298 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001299 // ll oldval,0(alginedaddr)
1300 // and maskedoldval0,oldval,mask
1301 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001302 BB = loop1MBB;
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001303 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001304 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001305 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001306 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001307 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001308
1309 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001310 // and maskedoldval1,oldval,mask2
1311 // or storeval,maskedoldval1,shiftednewval
1312 // sc success,storeval,0(alignedaddr)
1313 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001314 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001315 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001316 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001317 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001318 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001319 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001320 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001321 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001322 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001323
Akira Hatanaka939ece12011-07-19 03:42:13 +00001324 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001325 // srl srlres,maskedoldval0,shiftamt
1326 // sll sllres,srlres,24
1327 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001328 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001329 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001330
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001331 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001332 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001333 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001334 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001335 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001336 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001337
1338 MI->eraseFromParent(); // The instruction is gone now.
1339
Akira Hatanaka939ece12011-07-19 03:42:13 +00001340 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001341}
1342
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001343//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001344// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001345//===----------------------------------------------------------------------===//
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001346SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakab7656a92013-03-06 21:32:03 +00001347 SDValue Chain = Op.getOperand(0);
1348 SDValue Table = Op.getOperand(1);
1349 SDValue Index = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001350 SDLoc DL(Op);
Akira Hatanakab7656a92013-03-06 21:32:03 +00001351 EVT PTy = getPointerTy();
1352 unsigned EntrySize =
1353 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1354
1355 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1356 DAG.getConstant(EntrySize, PTy));
1357 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1358
1359 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1360 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1361 MachinePointerInfo::getJumpTable(), MemVT, false, false,
1362 0);
1363 Chain = Addr.getValue(1);
1364
1365 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || IsN64) {
1366 // For PIC, the sequence is:
1367 // BRIND(load(Jumptable + index) + RelocBase)
1368 // RelocBase can be JumpTable, GOT or some sort of global base.
1369 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1370 getPICJumpTableRelocBase(Table, DAG));
1371 }
1372
1373 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1374}
1375
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001376SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001377lowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001378{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001379 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001380 // the block to branch to if the condition is true.
1381 SDValue Chain = Op.getOperand(0);
1382 SDValue Dest = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001383 SDLoc DL(Op);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001384
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001385 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001386
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001387 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001388 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001389 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001390
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001391 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001392 Mips::CondCode CC =
1393 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanaka9cf07242013-03-30 01:16:38 +00001394 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1395 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001396 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001397 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001398 FCC0, Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001399}
1400
1401SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001402lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001403{
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001404 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001405
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001406 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001407 if (Cond.getOpcode() != MipsISD::FPCmp)
1408 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001409
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001410 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001411 SDLoc(Op));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001412}
1413
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001414SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001415lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001416{
Andrew Trickac6d9be2013-05-25 02:42:55 +00001417 SDLoc DL(Op);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001418 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault225ed702013-05-18 00:21:46 +00001419 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1420 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001421 Op.getOperand(0), Op.getOperand(1),
1422 Op.getOperand(4));
1423
1424 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1425 Op.getOperand(3));
1426}
1427
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001428SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1429 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001430
1431 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1432 "Floating point operand expected.");
1433
1434 SDValue True = DAG.getConstant(1, MVT::i32);
1435 SDValue False = DAG.getConstant(0, MVT::i32);
1436
Andrew Trickac6d9be2013-05-25 02:42:55 +00001437 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001438}
1439
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001440SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001441 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001442 // FIXME there isn't actually debug info here
Andrew Trickac6d9be2013-05-25 02:42:55 +00001443 SDLoc DL(Op);
Akira Hatanaka200a7432013-09-27 19:51:35 +00001444 EVT Ty = Op.getValueType();
1445 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1446 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001447
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001448 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaafc945b2012-09-12 23:27:55 +00001449 const MipsTargetObjectFile &TLOF =
1450 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001451
Chris Lattnere3736f82009-08-13 05:41:27 +00001452 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001453 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001454 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001455 MipsII::MO_GPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001456 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001457 DAG.getVTList(MVT::i32), &GA, 1);
Akira Hatanakae7338cd2012-08-22 03:18:13 +00001458 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001459 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001460 }
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001461
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001462 // %hi/%lo relocation
Akira Hatanaka200a7432013-09-27 19:51:35 +00001463 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001464 }
1465
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001466 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
Akira Hatanaka200a7432013-09-27 19:51:35 +00001467 return getAddrLocal(N, Ty, DAG, HasMips64);
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001468
Akira Hatanakaf09a0372012-11-21 20:40:38 +00001469 if (LargeGOT)
Akira Hatanaka200a7432013-09-27 19:51:35 +00001470 return getAddrGlobalLargeGOT(N, Ty, DAG, MipsII::MO_GOT_HI16,
Akira Hatanakaf09a0372012-11-21 20:40:38 +00001471 MipsII::MO_GOT_LO16);
1472
Akira Hatanaka200a7432013-09-27 19:51:35 +00001473 return getAddrGlobal(N, Ty, DAG,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001474 HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001475}
1476
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001477SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001478 SelectionDAG &DAG) const {
Akira Hatanaka200a7432013-09-27 19:51:35 +00001479 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1480 EVT Ty = Op.getValueType();
Akira Hatanaka79380342013-09-25 00:30:25 +00001481
Akira Hatanaka200a7432013-09-27 19:51:35 +00001482 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1483 return getAddrNonPIC(N, Ty, DAG);
1484
1485 return getAddrLocal(N, Ty, DAG, HasMips64);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001486}
1487
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001488SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001489lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001490{
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001491 // If the relocation model is PIC, use the General Dynamic TLS Model or
1492 // Local Dynamic TLS model, otherwise use the Initial Exec or
1493 // Local Exec TLS Model.
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001494
1495 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001496 SDLoc DL(GA);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001497 const GlobalValue *GV = GA->getGlobal();
1498 EVT PtrVT = getPointerTy();
1499
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001500 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1501
1502 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg70a07c72012-06-04 14:02:08 +00001503 // General Dynamic and Local Dynamic TLS Model.
1504 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1505 : MipsII::MO_TLSGD;
1506
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001507 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1508 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1509 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001510 unsigned PtrSize = PtrVT.getSizeInBits();
1511 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1512
Benjamin Kramer5eccf672011-12-11 12:21:34 +00001513 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001514
1515 ArgListTy Args;
1516 ArgListEntry Entry;
1517 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00001518 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001519 Args.push_back(Entry);
Jia Liubb481f82012-02-28 07:46:26 +00001520
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001521 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001522 false, false, false, false, 0, CallingConv::C,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001523 /*IsTailCall=*/false, /*doesNotRet=*/false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001524 /*isReturnValueUsed=*/true,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001525 TlsGetAddr, Args, DAG, DL);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001526 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001527
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001528 SDValue Ret = CallResult.first;
1529
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001530 if (model != TLSModel::LocalDynamic)
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001531 return Ret;
1532
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001533 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001534 MipsII::MO_DTPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001535 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1536 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001537 MipsII::MO_DTPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001538 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1539 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1540 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001541 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001542
1543 SDValue Offset;
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001544 if (model == TLSModel::InitialExec) {
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001545 // Initial Exec TLS Model
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001546 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001547 MipsII::MO_GOTTPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001548 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanaka648f00c2012-02-24 22:34:47 +00001549 TGA);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001550 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001551 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001552 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001553 } else {
1554 // Local Exec TLS Model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001555 assert(model == TLSModel::LocalExec);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001556 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001557 MipsII::MO_TPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001558 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001559 MipsII::MO_TPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001560 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1561 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1562 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001563 }
1564
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001565 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1566 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001567}
1568
1569SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001570lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001571{
Akira Hatanaka200a7432013-09-27 19:51:35 +00001572 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1573 EVT Ty = Op.getValueType();
Akira Hatanaka79380342013-09-25 00:30:25 +00001574
Akira Hatanaka200a7432013-09-27 19:51:35 +00001575 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1576 return getAddrNonPIC(N, Ty, DAG);
1577
1578 return getAddrLocal(N, Ty, DAG, HasMips64);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001579}
1580
Dan Gohman475871a2008-07-27 21:46:04 +00001581SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001582lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001583{
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001584 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001585 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001586 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001587 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001588 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001589 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001590 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1591 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001592 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Akira Hatanaka200a7432013-09-27 19:51:35 +00001593 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1594 EVT Ty = Op.getValueType();
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001595
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001596 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
Akira Hatanaka200a7432013-09-27 19:51:35 +00001597 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001598
Akira Hatanaka200a7432013-09-27 19:51:35 +00001599 return getAddrLocal(N, Ty, DAG, HasMips64);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001600}
1601
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001602SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001603 MachineFunction &MF = DAG.getMachineFunction();
1604 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1605
Andrew Trickac6d9be2013-05-25 02:42:55 +00001606 SDLoc DL(Op);
Dan Gohman1e93df62010-04-17 14:41:14 +00001607 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1608 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001609
1610 // vastart just stores the address of the VarArgsFrameIndex slot into the
1611 // memory location argument.
1612 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001613 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka82099682011-12-19 19:52:25 +00001614 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001615}
Jia Liubb481f82012-02-28 07:46:26 +00001616
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001617static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001618 EVT TyX = Op.getOperand(0).getValueType();
1619 EVT TyY = Op.getOperand(1).getValueType();
1620 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1621 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001622 SDLoc DL(Op);
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001623 SDValue Res;
1624
1625 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1626 // to i32.
1627 SDValue X = (TyX == MVT::f32) ?
1628 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1629 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1630 Const1);
1631 SDValue Y = (TyY == MVT::f32) ?
1632 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1633 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1634 Const1);
1635
1636 if (HasR2) {
1637 // ext E, Y, 31, 1 ; extract bit31 of Y
1638 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1639 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1640 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1641 } else {
1642 // sll SllX, X, 1
1643 // srl SrlX, SllX, 1
1644 // srl SrlY, Y, 31
1645 // sll SllY, SrlX, 31
1646 // or Or, SrlX, SllY
1647 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1648 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1649 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1650 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1651 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1652 }
1653
1654 if (TyX == MVT::f32)
1655 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1656
1657 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1658 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1659 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001660}
1661
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001662static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001663 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1664 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1665 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1666 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001667 SDLoc DL(Op);
Eric Christopher471e4222011-06-08 23:55:35 +00001668
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001669 // Bitcast to integer nodes.
1670 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1671 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001672
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001673 if (HasR2) {
1674 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1675 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1676 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1677 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001678
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001679 if (WidthX > WidthY)
1680 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1681 else if (WidthY > WidthX)
1682 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001683
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001684 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1685 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1686 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1687 }
1688
1689 // (d)sll SllX, X, 1
1690 // (d)srl SrlX, SllX, 1
1691 // (d)srl SrlY, Y, width(Y)-1
1692 // (d)sll SllY, SrlX, width(Y)-1
1693 // or Or, SrlX, SllY
1694 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1695 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1696 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1697 DAG.getConstant(WidthY - 1, MVT::i32));
1698
1699 if (WidthX > WidthY)
1700 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1701 else if (WidthY > WidthX)
1702 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1703
1704 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1705 DAG.getConstant(WidthX - 1, MVT::i32));
1706 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1707 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001708}
1709
Akira Hatanaka82099682011-12-19 19:52:25 +00001710SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001711MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001712 if (Subtarget->hasMips64())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001713 return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001714
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001715 return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001716}
1717
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001718static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001719 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001720 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001721
1722 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1723 // to i32.
1724 SDValue X = (Op.getValueType() == MVT::f32) ?
1725 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1726 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1727 Const1);
1728
1729 // Clear MSB.
1730 if (HasR2)
1731 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
1732 DAG.getRegister(Mips::ZERO, MVT::i32),
1733 DAG.getConstant(31, MVT::i32), Const1, X);
1734 else {
1735 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1736 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1737 }
1738
1739 if (Op.getValueType() == MVT::f32)
1740 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
1741
1742 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1743 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1744 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1745}
1746
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001747static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001748 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001749 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001750
1751 // Bitcast to integer node.
1752 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
1753
1754 // Clear MSB.
1755 if (HasR2)
1756 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
1757 DAG.getRegister(Mips::ZERO_64, MVT::i64),
1758 DAG.getConstant(63, MVT::i32), Const1, X);
1759 else {
1760 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
1761 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
1762 }
1763
1764 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
1765}
1766
1767SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001768MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001769 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001770 return lowerFABS64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001771
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001772 return lowerFABS32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001773}
1774
Akira Hatanaka2e591472011-06-02 00:24:44 +00001775SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001776lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001777 // check the depth
1778 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001779 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001780
1781 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1782 MFI->setFrameAddressIsTaken(true);
1783 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001784 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001785 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001786 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001787 return FrameAddr;
1788}
1789
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001790SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001791 SelectionDAG &DAG) const {
1792 // check the depth
1793 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1794 "Return address can be determined only for current frame.");
1795
1796 MachineFunction &MF = DAG.getMachineFunction();
1797 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001798 MVT VT = Op.getSimpleValueType();
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001799 unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
1800 MFI->setReturnAddressIsTaken(true);
1801
1802 // Return RA, which contains the return address. Mark it an implicit live-in.
1803 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001804 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001805}
1806
Akira Hatanaka544cc212013-01-30 00:26:49 +00001807// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1808// generated from __builtin_eh_return (offset, handler)
1809// The effect of this is to adjust the stack pointer by "offset"
1810// and then branch to "handler".
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001811SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanaka544cc212013-01-30 00:26:49 +00001812 const {
1813 MachineFunction &MF = DAG.getMachineFunction();
1814 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1815
1816 MipsFI->setCallsEhReturn();
1817 SDValue Chain = Op.getOperand(0);
1818 SDValue Offset = Op.getOperand(1);
1819 SDValue Handler = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001820 SDLoc DL(Op);
Akira Hatanaka544cc212013-01-30 00:26:49 +00001821 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
1822
1823 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1824 // EH_RETURN nodes, so that instructions are emitted back-to-back.
1825 unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1;
1826 unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0;
1827 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1828 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1829 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1830 DAG.getRegister(OffsetReg, Ty),
1831 DAG.getRegister(AddrReg, getPointerTy()),
1832 Chain.getValue(1));
1833}
1834
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001835SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001836 SelectionDAG &DAG) const {
Eli Friedman14648462011-07-27 22:21:52 +00001837 // FIXME: Need pseudo-fence for 'singlethread' fences
1838 // FIXME: Set SType for weaker fences where supported/appropriate.
1839 unsigned SType = 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001840 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001841 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00001842 DAG.getConstant(SType, MVT::i32));
1843}
1844
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001845SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001846 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001847 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001848 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1849 SDValue Shamt = Op.getOperand(2);
1850
1851 // if shamt < 32:
1852 // lo = (shl lo, shamt)
1853 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
1854 // else:
1855 // lo = 0
1856 // hi = (shl lo, shamt[4:0])
1857 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1858 DAG.getConstant(-1, MVT::i32));
1859 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
1860 DAG.getConstant(1, MVT::i32));
1861 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
1862 Not);
1863 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
1864 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1865 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
1866 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1867 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka864f6602012-06-14 21:10:56 +00001868 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1869 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001870 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
1871
1872 SDValue Ops[2] = {Lo, Hi};
1873 return DAG.getMergeValues(Ops, 2, DL);
1874}
1875
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001876SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001877 bool IsSRA) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001878 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001879 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1880 SDValue Shamt = Op.getOperand(2);
1881
1882 // if shamt < 32:
1883 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
1884 // if isSRA:
1885 // hi = (sra hi, shamt)
1886 // else:
1887 // hi = (srl hi, shamt)
1888 // else:
1889 // if isSRA:
1890 // lo = (sra hi, shamt[4:0])
1891 // hi = (sra hi, 31)
1892 // else:
1893 // lo = (srl hi, shamt[4:0])
1894 // hi = 0
1895 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1896 DAG.getConstant(-1, MVT::i32));
1897 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
1898 DAG.getConstant(1, MVT::i32));
1899 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
1900 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
1901 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1902 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
1903 Hi, Shamt);
1904 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1905 DAG.getConstant(0x20, MVT::i32));
1906 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
1907 DAG.getConstant(31, MVT::i32));
1908 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
1909 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1910 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
1911 ShiftRightHi);
1912
1913 SDValue Ops[2] = {Lo, Hi};
1914 return DAG.getMergeValues(Ops, 2, DL);
1915}
1916
Akira Hatanakafee62c12013-04-11 19:07:14 +00001917static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001918 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001919 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001920 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001921 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001922 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001923 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1924
1925 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001926 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001927 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001928
1929 SDValue Ops[] = { Chain, Ptr, Src };
1930 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
1931 LD->getMemOperand());
1932}
1933
1934// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001935SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001936 LoadSDNode *LD = cast<LoadSDNode>(Op);
1937 EVT MemVT = LD->getMemoryVT();
1938
1939 // Return if load is aligned or if MemVT is neither i32 nor i64.
1940 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
1941 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
1942 return SDValue();
1943
1944 bool IsLittle = Subtarget->isLittle();
1945 EVT VT = Op.getValueType();
1946 ISD::LoadExtType ExtType = LD->getExtensionType();
1947 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
1948
1949 assert((VT == MVT::i32) || (VT == MVT::i64));
1950
1951 // Expand
1952 // (set dst, (i64 (load baseptr)))
1953 // to
1954 // (set tmp, (ldl (add baseptr, 7), undef))
1955 // (set dst, (ldr baseptr, tmp))
1956 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00001957 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001958 IsLittle ? 7 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00001959 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001960 IsLittle ? 0 : 7);
1961 }
1962
Akira Hatanakafee62c12013-04-11 19:07:14 +00001963 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001964 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00001965 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001966 IsLittle ? 0 : 3);
1967
1968 // Expand
1969 // (set dst, (i32 (load baseptr))) or
1970 // (set dst, (i64 (sextload baseptr))) or
1971 // (set dst, (i64 (extload baseptr)))
1972 // to
1973 // (set tmp, (lwl (add baseptr, 3), undef))
1974 // (set dst, (lwr baseptr, tmp))
1975 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
1976 (ExtType == ISD::EXTLOAD))
1977 return LWR;
1978
1979 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
1980
1981 // Expand
1982 // (set dst, (i64 (zextload baseptr)))
1983 // to
1984 // (set tmp0, (lwl (add baseptr, 3), undef))
1985 // (set tmp1, (lwr baseptr, tmp0))
1986 // (set tmp2, (shl tmp1, 32))
1987 // (set dst, (srl tmp2, 32))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001988 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001989 SDValue Const32 = DAG.getConstant(32, MVT::i32);
1990 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka94ccee22012-06-04 17:46:29 +00001991 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
1992 SDValue Ops[] = { SRL, LWR.getValue(1) };
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001993 return DAG.getMergeValues(Ops, 2, DL);
1994}
1995
Akira Hatanakafee62c12013-04-11 19:07:14 +00001996static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001997 SDValue Chain, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001998 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
1999 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002000 SDLoc DL(SD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002001 SDVTList VTList = DAG.getVTList(MVT::Other);
2002
2003 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002004 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002005 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002006
2007 SDValue Ops[] = { Chain, Value, Ptr };
2008 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2009 SD->getMemOperand());
2010}
2011
2012// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanaka63451432013-05-16 20:45:17 +00002013static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2014 bool IsLittle) {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002015 SDValue Value = SD->getValue(), Chain = SD->getChain();
2016 EVT VT = Value.getValueType();
2017
2018 // Expand
2019 // (store val, baseptr) or
2020 // (truncstore val, baseptr)
2021 // to
2022 // (swl val, (add baseptr, 3))
2023 // (swr val, baseptr)
2024 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00002025 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002026 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00002027 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002028 }
2029
2030 assert(VT == MVT::i64);
2031
2032 // Expand
2033 // (store val, baseptr)
2034 // to
2035 // (sdl val, (add baseptr, 7))
2036 // (sdr val, baseptr)
Akira Hatanakafee62c12013-04-11 19:07:14 +00002037 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2038 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002039}
2040
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002041// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2042static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2043 SDValue Val = SD->getValue();
2044
2045 if (Val.getOpcode() != ISD::FP_TO_SINT)
2046 return SDValue();
2047
2048 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002049 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002050 Val.getOperand(0));
2051
Andrew Trickac6d9be2013-05-25 02:42:55 +00002052 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002053 SD->getPointerInfo(), SD->isVolatile(),
2054 SD->isNonTemporal(), SD->getAlignment());
2055}
2056
Akira Hatanaka63451432013-05-16 20:45:17 +00002057SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2058 StoreSDNode *SD = cast<StoreSDNode>(Op);
2059 EVT MemVT = SD->getMemoryVT();
2060
2061 // Lower unaligned integer stores.
2062 if ((SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2063 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2064 return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle());
2065
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002066 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanaka63451432013-05-16 20:45:17 +00002067}
2068
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002069SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002070 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2071 || cast<ConstantSDNode>
2072 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2073 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2074 return SDValue();
2075
2076 // The pattern
2077 // (add (frameaddr 0), (frame_to_args_offset))
2078 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2079 // (add FrameObject, 0)
2080 // where FrameObject is a fixed StackObject with offset 0 which points to
2081 // the old stack pointer.
2082 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2083 EVT ValTy = Op->getValueType(0);
2084 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2085 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002086 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002087 DAG.getConstant(0, ValTy));
2088}
2089
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002090SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2091 SelectionDAG &DAG) const {
2092 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002093 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002094 Op.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002095 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002096}
2097
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002098//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002099// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002100//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002101
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002102//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002103// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002104// Mips O32 ABI rules:
2105// ---
2106// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002107// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002108// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002109// f64 - Only passed in two aliased f32 registers if no int reg has been used
2110// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002111// not used, it must be shadowed. If only A3 is avaiable, shadow it and
2112// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002113//
2114// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002115//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002116
Duncan Sands1e96bab2010-11-04 10:49:57 +00002117static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00002118 MVT LocVT, CCValAssign::LocInfo LocInfo,
Akira Hatanakaad341d42013-08-20 23:38:40 +00002119 ISD::ArgFlagsTy ArgFlags, CCState &State,
2120 const uint16_t *F64Regs) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002121
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002122 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002123
Craig Topperc5eaae42012-03-11 07:57:25 +00002124 static const uint16_t IntRegs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002125 Mips::A0, Mips::A1, Mips::A2, Mips::A3
2126 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002127 static const uint16_t F32Regs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002128 Mips::F12, Mips::F14
2129 };
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002130
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002131 // Do not process byval args here.
2132 if (ArgFlags.isByVal())
2133 return true;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002134
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002135 // Promote i8 and i16
2136 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2137 LocVT = MVT::i32;
2138 if (ArgFlags.isSExt())
2139 LocInfo = CCValAssign::SExt;
2140 else if (ArgFlags.isZExt())
2141 LocInfo = CCValAssign::ZExt;
2142 else
2143 LocInfo = CCValAssign::AExt;
2144 }
2145
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002146 unsigned Reg;
2147
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002148 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2149 // is true: function is vararg, argument is 3rd or higher, there is previous
2150 // argument which is not f32 or f64.
2151 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2152 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002153 unsigned OrigAlign = ArgFlags.getOrigAlign();
2154 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002155
2156 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002157 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002158 // If this is the first part of an i64 arg,
2159 // the allocated register must be either A0 or A2.
2160 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2161 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002162 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002163 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2164 // Allocate int register and shadow next int register. If first
2165 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002166 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2167 if (Reg == Mips::A1 || Reg == Mips::A3)
2168 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2169 State.AllocateReg(IntRegs, IntRegsSize);
2170 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002171 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2172 // we are guaranteed to find an available float register
2173 if (ValVT == MVT::f32) {
2174 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2175 // Shadow int register
2176 State.AllocateReg(IntRegs, IntRegsSize);
2177 } else {
2178 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2179 // Shadow int registers
2180 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2181 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2182 State.AllocateReg(IntRegs, IntRegsSize);
2183 State.AllocateReg(IntRegs, IntRegsSize);
2184 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002185 } else
2186 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002187
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002188 if (!Reg) {
2189 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2190 OrigAlign);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002191 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002192 } else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002193 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002194
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002195 return false;
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002196}
2197
Akira Hatanakaad341d42013-08-20 23:38:40 +00002198static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2199 MVT LocVT, CCValAssign::LocInfo LocInfo,
2200 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2201 static const uint16_t F64Regs[] = { Mips::D6, Mips::D7 };
2202
2203 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2204}
2205
2206static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2207 MVT LocVT, CCValAssign::LocInfo LocInfo,
2208 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2209 static const uint16_t F64Regs[] = { Mips::D12_64, Mips::D12_64 };
2210
2211 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2212}
2213
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002214#include "MipsGenCallingConv.inc"
2215
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002216//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002217// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002218//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002219
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002220// Return next O32 integer argument register.
2221static unsigned getNextIntArgReg(unsigned Reg) {
2222 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2223 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2224}
2225
Akira Hatanaka7d712092012-10-30 19:23:25 +00002226SDValue
2227MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002228 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka7d712092012-10-30 19:23:25 +00002229 bool IsTailCall, SelectionDAG &DAG) const {
2230 if (!IsTailCall) {
2231 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2232 DAG.getIntPtrConstant(Offset));
2233 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2234 false, 0);
2235 }
2236
2237 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2238 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2239 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2240 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2241 /*isVolatile=*/ true, false, 0);
2242}
2243
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002244void MipsTargetLowering::
2245getOpndList(SmallVectorImpl<SDValue> &Ops,
2246 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2247 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2248 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
2249 // Insert node "GP copy globalreg" before call to function.
2250 //
2251 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2252 // in PIC mode) allow symbols to be resolved via lazy binding.
2253 // The lazy binding stub requires GP to point to the GOT.
2254 if (IsPICCall && !InternalLinkage) {
2255 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
2256 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2257 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2258 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002259
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002260 // Build a sequence of copy-to-reg nodes chained together with token
2261 // chain and flag operands which copy the outgoing args into registers.
2262 // The InFlag in necessary since all emitted instructions must be
2263 // stuck together.
2264 SDValue InFlag;
Reed Kotler8453b3f2013-01-24 04:24:02 +00002265
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002266 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2267 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2268 RegsToPass[i].second, InFlag);
2269 InFlag = Chain.getValue(1);
2270 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002271
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002272 // Add argument registers to the end of the list so that they are
2273 // known live into the call.
2274 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2275 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2276 RegsToPass[i].second.getValueType()));
Reed Kotler8453b3f2013-01-24 04:24:02 +00002277
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002278 // Add a register mask operand representing the call-preserved registers.
2279 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2280 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2281 assert(Mask && "Missing call preserved mask for calling convention");
Reed Kotler46090912013-05-10 22:25:39 +00002282 if (Subtarget->inMips16HardFloat()) {
2283 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2284 llvm::StringRef Sym = G->getGlobal()->getName();
2285 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
2286 if (F->hasFnAttribute("__Mips16RetHelper")) {
2287 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2288 }
2289 }
2290 }
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002291 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2292
2293 if (InFlag.getNode())
2294 Ops.push_back(InFlag);
Reed Kotler8453b3f2013-01-24 04:24:02 +00002295}
2296
Dan Gohman98ca4f22009-08-05 01:29:28 +00002297/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002298/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002299SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002300MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002301 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002302 SelectionDAG &DAG = CLI.DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002303 SDLoc DL = CLI.DL;
Craig Toppera0ec3f92013-07-14 04:42:23 +00002304 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2305 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2306 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002307 SDValue Chain = CLI.Chain;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002308 SDValue Callee = CLI.Callee;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002309 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002310 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002311 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002312
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002313 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002314 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002315 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002316 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002317
2318 // Analyze operands of the call, assigning locations to each operand.
2319 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002320 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002321 getTargetMachine(), ArgLocs, *DAG.getContext());
Reed Kotler46090912013-05-10 22:25:39 +00002322 MipsCC::SpecialCallingConvType SpecialCallingConv =
2323 getSpecialCallingConv(Callee);
Akira Hatanakaad341d42013-08-20 23:38:40 +00002324 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo,
2325 SpecialCallingConv);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002326
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002327 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002328 Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002329 Callee.getNode(), CLI.Args);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002330
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002331 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002332 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002333
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002334 // Check if it's really possible to do a tail call.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002335 if (IsTailCall)
2336 IsTailCall =
2337 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002338 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002339
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002340 if (IsTailCall)
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002341 ++NumTailCalls;
2342
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002343 // Chain is the output chain of the last Load/Store or CopyToReg node.
2344 // ByValChain is the output chain of the last Memcpy node created for copying
2345 // byval arguments to the stack.
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002346 unsigned StackAlignment = TFL->getStackAlignment();
2347 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002348 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002349
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002350 if (!IsTailCall)
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002351 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002352
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002353 SDValue StackPtr = DAG.getCopyFromReg(Chain, DL,
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002354 IsN64 ? Mips::SP_64 : Mips::SP,
2355 getPointerTy());
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002356
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002357 // With EABI is it possible to have 16 args on registers.
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002358 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman475871a2008-07-27 21:46:04 +00002359 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002360 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002361
2362 // Walk the register/memloc assignments, inserting copies/loads.
2363 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002364 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002365 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002366 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002367 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2368
2369 // ByVal Arg.
2370 if (Flags.isByVal()) {
2371 assert(Flags.getByValSize() &&
2372 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002373 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002374 assert(!IsTailCall &&
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002375 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002376 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002377 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
2378 ++ByValArg;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002379 continue;
2380 }
Jia Liubb481f82012-02-28 07:46:26 +00002381
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002382 // Promote the value if needed.
2383 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002384 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002385 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002386 if (VA.isRegLoc()) {
2387 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002388 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2389 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002390 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002391 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002392 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002393 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002394 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002395 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002396 if (!Subtarget->isLittle())
2397 std::swap(Lo, Hi);
Jia Liubb481f82012-02-28 07:46:26 +00002398 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002399 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2400 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2401 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002402 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002403 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002404 }
2405 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002406 case CCValAssign::SExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002407 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002408 break;
2409 case CCValAssign::ZExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002410 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002411 break;
2412 case CCValAssign::AExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002413 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002414 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002415 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002416
2417 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002418 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002419 if (VA.isRegLoc()) {
2420 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002421 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002422 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002423
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002424 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002425 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002426
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002427 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002428 // parameter value to a stack Location
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002429 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002430 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002431 }
2432
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002433 // Transform all store nodes into one single node because all store
2434 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002435 if (!MemOpChains.empty())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002436 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002437 &MemOpChains[0], MemOpChains.size());
2438
Bill Wendling056292f2008-09-16 21:48:12 +00002439 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002440 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2441 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002442 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakaed185da2012-12-13 03:17:29 +00002443 bool GlobalOrExternal = false, InternalLinkage = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002444 SDValue CalleeLo;
Akira Hatanaka200a7432013-09-27 19:51:35 +00002445 EVT Ty = Callee.getValueType();
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002446
2447 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002448 if (IsPICCall) {
Akira Hatanakaed185da2012-12-13 03:17:29 +00002449 InternalLinkage = G->getGlobal()->hasInternalLinkage();
2450
2451 if (InternalLinkage)
Akira Hatanaka200a7432013-09-27 19:51:35 +00002452 Callee = getAddrLocal(G, Ty, DAG, HasMips64);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002453 else if (LargeGOT)
Akira Hatanaka200a7432013-09-27 19:51:35 +00002454 Callee = getAddrGlobalLargeGOT(G, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002455 MipsII::MO_CALL_LO16);
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002456 else
Akira Hatanaka200a7432013-09-27 19:51:35 +00002457 Callee = getAddrGlobal(G, Ty, DAG, MipsII::MO_GOT_CALL);
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002458 } else
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002459 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002460 MipsII::MO_NO_FLAG);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002461 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002462 }
2463 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002464 if (!IsN64 && !IsPIC) // !N64 && static
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002465 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2466 MipsII::MO_NO_FLAG);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002467 else if (LargeGOT)
Akira Hatanaka200a7432013-09-27 19:51:35 +00002468 Callee = getAddrGlobalLargeGOT(S, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002469 MipsII::MO_CALL_LO16);
Akira Hatanaka60689322013-02-22 21:10:03 +00002470 else // N64 || PIC
Akira Hatanaka200a7432013-09-27 19:51:35 +00002471 Callee = getAddrGlobal(S, Ty, DAG, MipsII::MO_GOT_CALL);
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002472
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002473 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002474 }
2475
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002476 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002477 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002478
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002479 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2480 CLI, Callee, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002481
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002482 if (IsTailCall)
2483 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, &Ops[0], Ops.size());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002484
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002485 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, &Ops[0], Ops.size());
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002486 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002487
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002488 // Create the CALLSEQ_END node.
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002489 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002490 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002491 InFlag = Chain.getValue(1);
2492
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002493 // Handle result values, copying them out of physregs into vregs that we
2494 // return.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002495 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
2496 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002497}
2498
Dan Gohman98ca4f22009-08-05 01:29:28 +00002499/// LowerCallResult - Lower the result values of a call into the
2500/// appropriate copies out of appropriate physical registers.
2501SDValue
2502MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002503 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002504 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002505 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002506 SmallVectorImpl<SDValue> &InVals,
2507 const SDNode *CallNode,
2508 const Type *RetTy) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002509 // Assign locations to each value returned by this call.
2510 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002511 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka864f6602012-06-14 21:10:56 +00002512 getTargetMachine(), RVLocs, *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002513 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002514
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002515 MipsCCInfo.analyzeCallResult(Ins, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002516 CallNode, RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002517
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002518 // Copy all of the result registers out of their specified physreg.
2519 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002520 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002521 RVLocs[i].getLocVT(), InFlag);
2522 Chain = Val.getValue(1);
2523 InFlag = Val.getValue(2);
2524
2525 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002526 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002527
2528 InVals.push_back(Val);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002529 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002530
Dan Gohman98ca4f22009-08-05 01:29:28 +00002531 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002532}
2533
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002534//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002535// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002536//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002537/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002538/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002539SDValue
2540MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002541 CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002542 bool IsVarArg,
Akira Hatanaka82099682011-12-19 19:52:25 +00002543 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002544 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002545 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002546 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002547 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002548 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002549 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002550
Dan Gohman1e93df62010-04-17 14:41:14 +00002551 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002552
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002553 // Used with vargs to acumulate store chains.
2554 std::vector<SDValue> OutChains;
2555
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002556 // Assign locations to all of the incoming arguments.
2557 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002558 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002559 getTargetMachine(), ArgLocs, *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002560 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002561 Function::const_arg_iterator FuncArg =
2562 DAG.getMachineFunction().getFunction()->arg_begin();
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002563 bool UseSoftFloat = Subtarget->mipsSEUsesSoftFloat();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002564
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002565 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
Akira Hatanakab33b34a2012-10-30 19:37:25 +00002566 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2567 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002568
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002569 unsigned CurArgIdx = 0;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002570 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002571
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002572 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002573 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002574 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2575 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002576 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002577 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2578 bool IsRegLoc = VA.isRegLoc();
2579
2580 if (Flags.isByVal()) {
2581 assert(Flags.getByValSize() &&
2582 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002583 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002584 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002585 MipsCCInfo, *ByValArg);
2586 ++ByValArg;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002587 continue;
2588 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002589
2590 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002591 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002592 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002593 unsigned ArgReg = VA.getLocReg();
Craig Topper44d23822012-02-22 05:59:10 +00002594 const TargetRegisterClass *RC;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002595
Owen Anderson825b72b2009-08-11 20:47:22 +00002596 if (RegVT == MVT::i32)
Reed Kotlerbacbf1c2012-12-20 06:06:35 +00002597 RC = Subtarget->inMips16Mode()? &Mips::CPU16RegsRegClass :
Akira Hatanaka18587862013-08-06 23:08:38 +00002598 &Mips::GPR32RegClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002599 else if (RegVT == MVT::i64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002600 RC = &Mips::GPR64RegClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002601 else if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002602 RC = &Mips::FGR32RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002603 else if (RegVT == MVT::f64)
Akira Hatanakaad341d42013-08-20 23:38:40 +00002604 RC = Subtarget->isFP64bit() ? &Mips::FGR64RegClass :
2605 &Mips::AFGR64RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002606 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002607 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002608
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002609 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002610 // physical registers into virtual ones
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002611 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2612 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002613
2614 // If this is an 8 or 16-bit value, it has been passed promoted
2615 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002616 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002617 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002618 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002619 if (VA.getLocInfo() == CCValAssign::SExt)
2620 Opcode = ISD::AssertSext;
2621 else if (VA.getLocInfo() == CCValAssign::ZExt)
2622 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002623 if (Opcode)
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002624 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002625 DAG.getValueType(ValVT));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002626 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002627 }
2628
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002629 // Handle floating point arguments passed in integer registers and
2630 // long double arguments passed in floating point registers.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002631 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002632 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2633 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002634 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002635 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002636 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002637 getNextIntArgReg(ArgReg), RC);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002638 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002639 if (!Subtarget->isLittle())
2640 std::swap(ArgValue, ArgValue2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002641 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002642 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002643 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002644
Dan Gohman98ca4f22009-08-05 01:29:28 +00002645 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002646 } else { // VA.isRegLoc()
2647
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002648 // sanity check
2649 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002650
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002651 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002652 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002653 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002654
2655 // Create load nodes to retrieve arguments from the stack
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002656 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002657 InVals.push_back(DAG.getLoad(ValVT, DL, Chain, FIN,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002658 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002659 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002660 }
2661 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002662
2663 // The mips ABIs for returning structs by value requires that we copy
2664 // the sret argument into $v0 for the return. Save the argument into
2665 // a virtual register so that we can access it from the return points.
2666 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2667 unsigned Reg = MipsFI->getSRetReturnReg();
2668 if (!Reg) {
Akira Hatanaka30580ce2012-10-19 22:11:40 +00002669 Reg = MF.getRegInfo().
2670 createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002671 MipsFI->setSRetReturnReg(Reg);
2672 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002673 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);
2674 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002675 }
2676
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002677 if (IsVarArg)
2678 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002679
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002680 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002681 // the size of Ins and InVals. This only happens when on varg functions
2682 if (!OutChains.empty()) {
2683 OutChains.push_back(Chain);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002684 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002685 &OutChains[0], OutChains.size());
2686 }
2687
Dan Gohman98ca4f22009-08-05 01:29:28 +00002688 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002689}
2690
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002691//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002692// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002693//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002694
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002695bool
2696MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002697 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002698 const SmallVectorImpl<ISD::OutputArg> &Outs,
2699 LLVMContext &Context) const {
2700 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002701 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002702 RVLocs, Context);
2703 return CCInfo.CheckReturn(Outs, RetCC_Mips);
2704}
2705
Dan Gohman98ca4f22009-08-05 01:29:28 +00002706SDValue
2707MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002708 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002709 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002710 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002711 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002712 // CCValAssign - represent the assignment of
2713 // the return value to a location
2714 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002715 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002716
2717 // CCState - Info about the registers and stack slot.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002718 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002719 *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002720 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002721
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002722 // Analyze return values.
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002723 MipsCCInfo.analyzeReturn(Outs, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002724 MF.getFunction()->getReturnType());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002725
Dan Gohman475871a2008-07-27 21:46:04 +00002726 SDValue Flag;
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002727 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002728
2729 // Copy the result values into the output registers.
2730 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002731 SDValue Val = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002732 CCValAssign &VA = RVLocs[i];
2733 assert(VA.isRegLoc() && "Can only return in registers!");
2734
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002735 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002736 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002737
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002738 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002739
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002740 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002741 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002742 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002743 }
2744
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002745 // The mips ABIs for returning structs by value requires that we copy
2746 // the sret argument into $v0 for the return. We saved the argument into
2747 // a virtual register in the entry block, so now we copy the value out
2748 // and into $v0.
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002749 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002750 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2751 unsigned Reg = MipsFI->getSRetReturnReg();
2752
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002753 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002754 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002755 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Akira Hatanaka2ef5bd32012-10-24 02:10:54 +00002756 unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002757
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002758 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002759 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002760 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002761 }
2762
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002763 RetOps[0] = Chain; // Update chain.
Akira Hatanaka182ef6f2012-07-10 00:19:06 +00002764
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002765 // Add the flag if we have it.
2766 if (Flag.getNode())
2767 RetOps.push_back(Flag);
2768
2769 // Return on Mips is always a "jr $ra"
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002770 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, &RetOps[0], RetOps.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002771}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002772
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002773//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002774// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002775//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002776
2777/// getConstraintType - Given a constraint letter, return the type of
2778/// constraint it is for this target.
2779MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002780getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002781{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002782 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002783 // GCC config/mips/constraints.md
2784 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002785 // 'd' : An address register. Equivalent to r
2786 // unless generating MIPS16 code.
2787 // 'y' : Equivalent to r; retained for
2788 // backwards compatibility.
Eric Christopher1d5a3922012-05-07 06:25:10 +00002789 // 'c' : A register suitable for use in an indirect
2790 // jump. This will always be $25 for -mabicalls.
Eric Christopheraf97f732012-05-07 06:25:19 +00002791 // 'l' : The lo register. 1 word storage.
2792 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002793 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002794 switch (Constraint[0]) {
2795 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002796 case 'd':
2797 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002798 case 'f':
Eric Christopher1d5a3922012-05-07 06:25:10 +00002799 case 'c':
Eric Christopher4adbefe2012-05-07 06:25:15 +00002800 case 'l':
Eric Christopheraf97f732012-05-07 06:25:19 +00002801 case 'x':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002802 return C_RegisterClass;
Jack Carter0b9675d2013-03-04 21:33:15 +00002803 case 'R':
2804 return C_Memory;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002805 }
2806 }
2807 return TargetLowering::getConstraintType(Constraint);
2808}
2809
John Thompson44ab89e2010-10-29 17:29:13 +00002810/// Examine constraint type and operand type and determine a weight value.
2811/// This object must already have been set up with the operand type
2812/// and the current alternative constraint selected.
2813TargetLowering::ConstraintWeight
2814MipsTargetLowering::getSingleConstraintMatchWeight(
2815 AsmOperandInfo &info, const char *constraint) const {
2816 ConstraintWeight weight = CW_Invalid;
2817 Value *CallOperandVal = info.CallOperandVal;
2818 // If we don't have a value, we can't do a match,
2819 // but allow it at the lowest weight.
2820 if (CallOperandVal == NULL)
2821 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002822 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002823 // Look at the constraint type.
2824 switch (*constraint) {
2825 default:
2826 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2827 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002828 case 'd':
2829 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002830 if (type->isIntegerTy())
2831 weight = CW_Register;
2832 break;
2833 case 'f':
2834 if (type->isFloatTy())
2835 weight = CW_Register;
2836 break;
Eric Christopher1d5a3922012-05-07 06:25:10 +00002837 case 'c': // $25 for indirect jumps
Eric Christopher4adbefe2012-05-07 06:25:15 +00002838 case 'l': // lo register
Eric Christopheraf97f732012-05-07 06:25:19 +00002839 case 'x': // hilo register pair
Eric Christopher1d5a3922012-05-07 06:25:10 +00002840 if (type->isIntegerTy())
2841 weight = CW_SpecificReg;
2842 break;
Eric Christopher50ab0392012-05-07 03:13:32 +00002843 case 'I': // signed 16 bit immediate
Eric Christophere5076d42012-05-07 03:13:42 +00002844 case 'J': // integer zero
Eric Christopherf49f8462012-05-07 05:46:29 +00002845 case 'K': // unsigned 16 bit immediate
Eric Christopher5ac47bb2012-05-07 05:46:37 +00002846 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christopher60cfc792012-05-07 05:46:43 +00002847 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher1ce20342012-05-07 05:46:48 +00002848 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopher54412a72012-05-07 06:25:02 +00002849 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher50ab0392012-05-07 03:13:32 +00002850 if (isa<ConstantInt>(CallOperandVal))
2851 weight = CW_Constant;
2852 break;
Jack Carter0b9675d2013-03-04 21:33:15 +00002853 case 'R':
2854 weight = CW_Memory;
2855 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002856 }
2857 return weight;
2858}
2859
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002860/// This is a helper function to parse a physical register string and split it
2861/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
2862/// that is returned indicates whether parsing was successful. The second flag
2863/// is true if the numeric part exists.
2864static std::pair<bool, bool>
2865parsePhysicalReg(const StringRef &C, std::string &Prefix,
2866 unsigned long long &Reg) {
2867 if (C.front() != '{' || C.back() != '}')
2868 return std::make_pair(false, false);
2869
2870 // Search for the first numeric character.
2871 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
2872 I = std::find_if(B, E, std::ptr_fun(isdigit));
2873
2874 Prefix.assign(B, I - B);
2875
2876 // The second flag is set to false if no numeric characters were found.
2877 if (I == E)
2878 return std::make_pair(true, false);
2879
2880 // Parse the numeric characters.
2881 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
2882 true);
2883}
2884
2885std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
2886parseRegForInlineAsmConstraint(const StringRef &C, MVT VT) const {
2887 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2888 const TargetRegisterClass *RC;
2889 std::string Prefix;
2890 unsigned long long Reg;
2891
2892 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
2893
2894 if (!R.first)
2895 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2896
2897 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
2898 // No numeric characters follow "hi" or "lo".
2899 if (R.second)
2900 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2901
2902 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002903 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002904 return std::make_pair(*(RC->begin()), RC);
2905 }
2906
2907 if (!R.second)
2908 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2909
2910 if (Prefix == "$f") { // Parse $f0-$f31.
2911 // If the size of FP registers is 64-bit or Reg is an even number, select
2912 // the 64-bit register class. Otherwise, select the 32-bit register class.
2913 if (VT == MVT::Other)
2914 VT = (Subtarget->isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
2915
2916 RC= getRegClassFor(VT);
2917
2918 if (RC == &Mips::AFGR64RegClass) {
2919 assert(Reg % 2 == 0);
2920 Reg >>= 1;
2921 }
2922 } else if (Prefix == "$fcc") { // Parse $fcc0-$fcc7.
2923 RC = TRI->getRegClass(Mips::FCCRegClassID);
2924 } else { // Parse $0-$31.
2925 assert(Prefix == "$");
2926 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
2927 }
2928
2929 assert(Reg < RC->getNumRegs());
2930 return std::make_pair(*(RC->begin() + Reg), RC);
2931}
2932
Eric Christopher38d64262011-06-29 19:33:04 +00002933/// Given a register class constraint, like 'r', if this corresponds directly
2934/// to an LLVM register class, return a register of 0 and the register class
2935/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002936std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier5b3fca52013-06-22 18:37:38 +00002937getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002938{
2939 if (Constraint.size() == 1) {
2940 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002941 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2942 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002943 case 'r':
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002944 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
2945 if (Subtarget->inMips16Mode())
2946 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka18587862013-08-06 23:08:38 +00002947 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002948 }
Jack Carter10de0252012-07-02 23:35:23 +00002949 if (VT == MVT::i64 && !HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002950 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002951 if (VT == MVT::i64 && HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002952 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002953 // This will generate an error message
2954 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002955 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002956 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002957 return std::make_pair(0U, &Mips::FGR32RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002958 if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
2959 if (Subtarget->isFP64bit())
Craig Topper420761a2012-04-20 07:30:17 +00002960 return std::make_pair(0U, &Mips::FGR64RegClass);
2961 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002962 }
Eric Christopher1d5a3922012-05-07 06:25:10 +00002963 break;
2964 case 'c': // register suitable for indirect jump
2965 if (VT == MVT::i32)
Akira Hatanaka18587862013-08-06 23:08:38 +00002966 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christopher1d5a3922012-05-07 06:25:10 +00002967 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka18587862013-08-06 23:08:38 +00002968 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher4adbefe2012-05-07 06:25:15 +00002969 case 'l': // register suitable for indirect jump
2970 if (VT == MVT::i32)
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002971 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
2972 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopheraf97f732012-05-07 06:25:19 +00002973 case 'x': // register suitable for indirect jump
2974 // Fixme: Not triggering the use of both hi and low
2975 // This will generate an error message
2976 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002977 }
2978 }
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002979
2980 std::pair<unsigned, const TargetRegisterClass *> R;
2981 R = parseRegForInlineAsmConstraint(Constraint, VT);
2982
2983 if (R.second)
2984 return R;
2985
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002986 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2987}
2988
Eric Christopher50ab0392012-05-07 03:13:32 +00002989/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
2990/// vector. If it is invalid, don't add anything to Ops.
2991void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
2992 std::string &Constraint,
2993 std::vector<SDValue>&Ops,
2994 SelectionDAG &DAG) const {
2995 SDValue Result(0, 0);
2996
2997 // Only support length 1 constraints for now.
2998 if (Constraint.length() > 1) return;
2999
3000 char ConstraintLetter = Constraint[0];
3001 switch (ConstraintLetter) {
3002 default: break; // This will fall through to the generic implementation
3003 case 'I': // Signed 16 bit constant
3004 // If this fails, the parent routine will give an error
3005 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3006 EVT Type = Op.getValueType();
3007 int64_t Val = C->getSExtValue();
3008 if (isInt<16>(Val)) {
3009 Result = DAG.getTargetConstant(Val, Type);
3010 break;
3011 }
3012 }
3013 return;
Eric Christophere5076d42012-05-07 03:13:42 +00003014 case 'J': // integer zero
3015 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3016 EVT Type = Op.getValueType();
3017 int64_t Val = C->getZExtValue();
3018 if (Val == 0) {
3019 Result = DAG.getTargetConstant(0, Type);
3020 break;
3021 }
3022 }
3023 return;
Eric Christopherf49f8462012-05-07 05:46:29 +00003024 case 'K': // unsigned 16 bit immediate
3025 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3026 EVT Type = Op.getValueType();
3027 uint64_t Val = (uint64_t)C->getZExtValue();
3028 if (isUInt<16>(Val)) {
3029 Result = DAG.getTargetConstant(Val, Type);
3030 break;
3031 }
3032 }
3033 return;
Eric Christopher5ac47bb2012-05-07 05:46:37 +00003034 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3035 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3036 EVT Type = Op.getValueType();
3037 int64_t Val = C->getSExtValue();
3038 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3039 Result = DAG.getTargetConstant(Val, Type);
3040 break;
3041 }
3042 }
3043 return;
Eric Christopher60cfc792012-05-07 05:46:43 +00003044 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3045 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3046 EVT Type = Op.getValueType();
3047 int64_t Val = C->getSExtValue();
3048 if ((Val >= -65535) && (Val <= -1)) {
3049 Result = DAG.getTargetConstant(Val, Type);
3050 break;
3051 }
3052 }
3053 return;
Eric Christopher1ce20342012-05-07 05:46:48 +00003054 case 'O': // signed 15 bit immediate
3055 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3056 EVT Type = Op.getValueType();
3057 int64_t Val = C->getSExtValue();
3058 if ((isInt<15>(Val))) {
3059 Result = DAG.getTargetConstant(Val, Type);
3060 break;
3061 }
3062 }
3063 return;
Eric Christopher54412a72012-05-07 06:25:02 +00003064 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3065 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3066 EVT Type = Op.getValueType();
3067 int64_t Val = C->getSExtValue();
3068 if ((Val <= 65535) && (Val >= 1)) {
3069 Result = DAG.getTargetConstant(Val, Type);
3070 break;
3071 }
3072 }
3073 return;
Eric Christopher50ab0392012-05-07 03:13:32 +00003074 }
3075
3076 if (Result.getNode()) {
3077 Ops.push_back(Result);
3078 return;
3079 }
3080
3081 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3082}
3083
Dan Gohman6520e202008-10-18 02:06:02 +00003084bool
Akira Hatanaka94e47282012-11-17 00:25:41 +00003085MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM, Type *Ty) const {
3086 // No global is ever allowed as a base.
3087 if (AM.BaseGV)
3088 return false;
3089
3090 switch (AM.Scale) {
3091 case 0: // "r+i" or just "i", depending on HasBaseReg.
3092 break;
3093 case 1:
3094 if (!AM.HasBaseReg) // allow "r+i".
3095 break;
3096 return false; // disallow "r+r" or "r+r+i".
3097 default:
3098 return false;
3099 }
3100
3101 return true;
3102}
3103
3104bool
Dan Gohman6520e202008-10-18 02:06:02 +00003105MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3106 // The Mips target isn't yet aware of offsets.
3107 return false;
3108}
Evan Chengeb2f9692009-10-27 19:56:55 +00003109
Akira Hatanakae193b322012-06-13 19:33:32 +00003110EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003111 unsigned SrcAlign,
3112 bool IsMemset, bool ZeroMemset,
Akira Hatanakae193b322012-06-13 19:33:32 +00003113 bool MemcpyStrSrc,
3114 MachineFunction &MF) const {
3115 if (Subtarget->hasMips64())
3116 return MVT::i64;
3117
3118 return MVT::i32;
3119}
3120
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003121bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3122 if (VT != MVT::f32 && VT != MVT::f64)
3123 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00003124 if (Imm.isNegZero())
3125 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00003126 return Imm.isZero();
3127}
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003128
3129unsigned MipsTargetLowering::getJumpTableEncoding() const {
3130 if (IsN64)
3131 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liubb481f82012-02-28 07:46:26 +00003132
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003133 return TargetLowering::getJumpTableEncoding();
3134}
Akira Hatanaka7887c902012-10-26 23:56:38 +00003135
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003136/// This function returns true if CallSym is a long double emulation routine.
3137static bool isF128SoftLibCall(const char *CallSym) {
3138 const char *const LibCalls[] =
3139 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3140 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3141 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3142 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3143 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3144 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3145 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3146 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3147 "truncl"};
3148
3149 const char * const *End = LibCalls + array_lengthof(LibCalls);
3150
3151 // Check that LibCalls is sorted alphabetically.
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003152 MipsTargetLowering::LTStr Comp;
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003153
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003154#ifndef NDEBUG
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003155 for (const char * const *I = LibCalls; I < End - 1; ++I)
3156 assert(Comp(*I, *(I + 1)));
3157#endif
3158
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003159 return std::binary_search(LibCalls, End, CallSym, Comp);
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003160}
3161
3162/// This function returns true if Ty is fp128 or i128 which was originally a
3163/// fp128.
3164static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3165 if (Ty->isFP128Ty())
3166 return true;
3167
3168 const ExternalSymbolSDNode *ES =
3169 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3170
3171 // If the Ty is i128 and the function being called is a long double emulation
3172 // routine, then the original type is f128.
3173 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3174}
3175
Reed Kotler46090912013-05-10 22:25:39 +00003176MipsTargetLowering::MipsCC::SpecialCallingConvType
3177 MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
3178 MipsCC::SpecialCallingConvType SpecialCallingConv =
3179 MipsCC::NoSpecialCallingConv;;
3180 if (Subtarget->inMips16HardFloat()) {
3181 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3182 llvm::StringRef Sym = G->getGlobal()->getName();
3183 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
3184 if (F->hasFnAttribute("__Mips16RetHelper")) {
3185 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3186 }
3187 }
3188 }
3189 return SpecialCallingConv;
3190}
3191
3192MipsTargetLowering::MipsCC::MipsCC(
Akira Hatanakaad341d42013-08-20 23:38:40 +00003193 CallingConv::ID CC, bool IsO32_, bool IsFP64_, CCState &Info,
Reed Kotler46090912013-05-10 22:25:39 +00003194 MipsCC::SpecialCallingConvType SpecialCallingConv_)
Akira Hatanakaad341d42013-08-20 23:38:40 +00003195 : CCInfo(Info), CallConv(CC), IsO32(IsO32_), IsFP64(IsFP64_),
Reed Kotler46090912013-05-10 22:25:39 +00003196 SpecialCallingConv(SpecialCallingConv_){
Akira Hatanaka7887c902012-10-26 23:56:38 +00003197 // Pre-allocate reserved argument area.
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003198 CCInfo.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka7887c902012-10-26 23:56:38 +00003199}
3200
Reed Kotler46090912013-05-10 22:25:39 +00003201
Akira Hatanaka7887c902012-10-26 23:56:38 +00003202void MipsTargetLowering::MipsCC::
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003203analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003204 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
3205 std::vector<ArgListEntry> &FuncArgs) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003206 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3207 "CallingConv::Fast shouldn't be used for vararg functions.");
3208
Akira Hatanaka7887c902012-10-26 23:56:38 +00003209 unsigned NumOpnds = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003210 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003211
3212 for (unsigned I = 0; I != NumOpnds; ++I) {
3213 MVT ArgVT = Args[I].VT;
3214 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3215 bool R;
3216
3217 if (ArgFlags.isByVal()) {
3218 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3219 continue;
3220 }
3221
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003222 if (IsVarArg && !Args[I].IsFixed)
Akira Hatanaka7887c902012-10-26 23:56:38 +00003223 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003224 else {
3225 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3226 IsSoftFloat);
3227 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
3228 }
Akira Hatanaka7887c902012-10-26 23:56:38 +00003229
3230 if (R) {
3231#ifndef NDEBUG
3232 dbgs() << "Call operand #" << I << " has unhandled type "
3233 << EVT(ArgVT).getEVTString();
3234#endif
3235 llvm_unreachable(0);
3236 }
3237 }
3238}
3239
3240void MipsTargetLowering::MipsCC::
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003241analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
3242 bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
Akira Hatanaka7887c902012-10-26 23:56:38 +00003243 unsigned NumArgs = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003244 llvm::CCAssignFn *FixedFn = fixedArgFn();
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003245 unsigned CurArgIdx = 0;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003246
3247 for (unsigned I = 0; I != NumArgs; ++I) {
3248 MVT ArgVT = Args[I].VT;
3249 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003250 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3251 CurArgIdx = Args[I].OrigArgIndex;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003252
3253 if (ArgFlags.isByVal()) {
3254 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3255 continue;
3256 }
3257
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003258 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), 0, IsSoftFloat);
3259
3260 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
Akira Hatanaka7887c902012-10-26 23:56:38 +00003261 continue;
3262
3263#ifndef NDEBUG
3264 dbgs() << "Formal Arg #" << I << " has unhandled type "
3265 << EVT(ArgVT).getEVTString();
3266#endif
3267 llvm_unreachable(0);
3268 }
3269}
3270
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003271template<typename Ty>
3272void MipsTargetLowering::MipsCC::
3273analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
3274 const SDNode *CallNode, const Type *RetTy) const {
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003275 CCAssignFn *Fn;
3276
3277 if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
3278 Fn = RetCC_F128Soft;
3279 else
3280 Fn = RetCC_Mips;
3281
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003282 for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
3283 MVT VT = RetVals[I].VT;
3284 ISD::ArgFlagsTy Flags = RetVals[I].Flags;
3285 MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
3286
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003287 if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003288#ifndef NDEBUG
3289 dbgs() << "Call result #" << I << " has unhandled type "
3290 << EVT(VT).getEVTString() << '\n';
3291#endif
3292 llvm_unreachable(0);
3293 }
3294 }
3295}
3296
3297void MipsTargetLowering::MipsCC::
3298analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
3299 const SDNode *CallNode, const Type *RetTy) const {
3300 analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
3301}
3302
3303void MipsTargetLowering::MipsCC::
3304analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
3305 const Type *RetTy) const {
3306 analyzeReturn(Outs, IsSoftFloat, 0, RetTy);
3307}
3308
Akira Hatanaka7887c902012-10-26 23:56:38 +00003309void
3310MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3311 MVT LocVT,
3312 CCValAssign::LocInfo LocInfo,
3313 ISD::ArgFlagsTy ArgFlags) {
3314 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3315
3316 struct ByValArgInfo ByVal;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003317 unsigned RegSize = regSize();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003318 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3319 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3320 RegSize * 2);
3321
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003322 if (useRegsForByval())
Akira Hatanaka7887c902012-10-26 23:56:38 +00003323 allocateRegs(ByVal, ByValSize, Align);
3324
3325 // Allocate space on caller's stack.
3326 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3327 Align);
3328 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3329 LocInfo));
3330 ByValArgs.push_back(ByVal);
3331}
3332
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003333unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
3334 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
3335}
3336
3337unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
3338 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
3339}
3340
3341const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const {
3342 return IsO32 ? O32IntRegs : Mips64IntRegs;
3343}
3344
3345llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3346 if (CallConv == CallingConv::Fast)
3347 return CC_Mips_FastCC;
3348
Reed Kotler46090912013-05-10 22:25:39 +00003349 if (SpecialCallingConv == Mips16RetHelperConv)
3350 return CC_Mips16RetHelper;
Akira Hatanakaad341d42013-08-20 23:38:40 +00003351 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003352}
3353
3354llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
Akira Hatanakaad341d42013-08-20 23:38:40 +00003355 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN_VarArg;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003356}
3357
3358const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const {
3359 return IsO32 ? O32IntRegs : Mips64DPRegs;
3360}
3361
Akira Hatanaka7887c902012-10-26 23:56:38 +00003362void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3363 unsigned ByValSize,
3364 unsigned Align) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003365 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
3366 const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003367 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3368 "Byval argument's size and alignment should be a multiple of"
3369 "RegSize.");
3370
3371 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3372
3373 // If Align > RegSize, the first arg register must be even.
3374 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3375 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3376 ++ByVal.FirstIdx;
3377 }
3378
3379 // Mark the registers allocated.
3380 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3381 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3382 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3383}
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003384
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003385MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3386 const SDNode *CallNode,
3387 bool IsSoftFloat) const {
3388 if (IsSoftFloat || IsO32)
3389 return VT;
3390
3391 // Check if the original type was fp128.
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003392 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003393 assert(VT == MVT::i64);
3394 return MVT::f64;
3395 }
3396
3397 return VT;
3398}
3399
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003400void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003401copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003402 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3403 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3404 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3405 MachineFunction &MF = DAG.getMachineFunction();
3406 MachineFrameInfo *MFI = MF.getFrameInfo();
3407 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
3408 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3409 int FrameObjOffset;
3410
3411 if (RegAreaSize)
3412 FrameObjOffset = (int)CC.reservedArgArea() -
3413 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
3414 else
3415 FrameObjOffset = ByVal.Address;
3416
3417 // Create frame object.
3418 EVT PtrTy = getPointerTy();
3419 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3420 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3421 InVals.push_back(FIN);
3422
3423 if (!ByVal.NumRegs)
3424 return;
3425
3426 // Copy arg registers.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003427 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003428 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3429
3430 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3431 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003432 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003433 unsigned Offset = I * CC.regSize();
3434 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3435 DAG.getConstant(Offset, PtrTy));
3436 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3437 StorePtr, MachinePointerInfo(FuncArg, Offset),
3438 false, false, 0);
3439 OutChains.push_back(Store);
3440 }
3441}
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003442
3443// Copy byVal arg to registers and stack.
3444void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003445passByValArg(SDValue Chain, SDLoc DL,
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00003446 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Craig Toppera0ec3f92013-07-14 04:42:23 +00003447 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003448 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3449 const MipsCC &CC, const ByValArgInfo &ByVal,
3450 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
3451 unsigned ByValSize = Flags.getByValSize();
3452 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
3453 unsigned RegSize = CC.regSize();
3454 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
3455 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
3456
3457 if (ByVal.NumRegs) {
3458 const uint16_t *ArgRegs = CC.intArgRegs();
3459 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
3460 unsigned I = 0;
3461
3462 // Copy words to registers.
3463 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
3464 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3465 DAG.getConstant(Offset, PtrTy));
3466 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3467 MachinePointerInfo(), false, false, false,
3468 Alignment);
3469 MemOpChains.push_back(LoadVal.getValue(1));
3470 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3471 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3472 }
3473
3474 // Return if the struct has been fully copied.
3475 if (ByValSize == Offset)
3476 return;
3477
3478 // Copy the remainder of the byval argument with sub-word loads and shifts.
3479 if (LeftoverBytes) {
3480 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
3481 "Size of the remainder should be smaller than RegSize.");
3482 SDValue Val;
3483
3484 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
3485 Offset < ByValSize; LoadSize /= 2) {
3486 unsigned RemSize = ByValSize - Offset;
3487
3488 if (RemSize < LoadSize)
3489 continue;
3490
3491 // Load subword.
3492 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3493 DAG.getConstant(Offset, PtrTy));
3494 SDValue LoadVal =
3495 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
3496 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
3497 false, false, Alignment);
3498 MemOpChains.push_back(LoadVal.getValue(1));
3499
3500 // Shift the loaded value.
3501 unsigned Shamt;
3502
3503 if (isLittle)
3504 Shamt = TotalSizeLoaded;
3505 else
3506 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
3507
3508 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3509 DAG.getConstant(Shamt, MVT::i32));
3510
3511 if (Val.getNode())
3512 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3513 else
3514 Val = Shift;
3515
3516 Offset += LoadSize;
3517 TotalSizeLoaded += LoadSize;
3518 Alignment = std::min(Alignment, LoadSize);
3519 }
3520
3521 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3522 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3523 return;
3524 }
3525 }
3526
3527 // Copy remainder of byval arg to it with memcpy.
3528 unsigned MemCpySize = ByValSize - Offset;
3529 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3530 DAG.getConstant(Offset, PtrTy));
3531 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3532 DAG.getIntPtrConstant(ByVal.Address));
3533 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3534 DAG.getConstant(MemCpySize, PtrTy), Alignment,
3535 /*isVolatile=*/false, /*AlwaysInline=*/false,
3536 MachinePointerInfo(0), MachinePointerInfo(0));
3537 MemOpChains.push_back(Chain);
3538}
Akira Hatanakaf0848472012-10-27 00:21:13 +00003539
3540void
3541MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3542 const MipsCC &CC, SDValue Chain,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003543 SDLoc DL, SelectionDAG &DAG) const {
Akira Hatanakaf0848472012-10-27 00:21:13 +00003544 unsigned NumRegs = CC.numIntArgRegs();
3545 const uint16_t *ArgRegs = CC.intArgRegs();
3546 const CCState &CCInfo = CC.getCCInfo();
3547 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
3548 unsigned RegSize = CC.regSize();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003549 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003550 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3551 MachineFunction &MF = DAG.getMachineFunction();
3552 MachineFrameInfo *MFI = MF.getFrameInfo();
3553 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3554
3555 // Offset of the first variable argument from stack pointer.
3556 int VaArgOffset;
3557
3558 if (NumRegs == Idx)
3559 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
3560 else
3561 VaArgOffset =
3562 (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
3563
3564 // Record the frame index of the first variable argument
3565 // which is a value necessary to VASTART.
3566 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3567 MipsFI->setVarArgsFrameIndex(FI);
3568
3569 // Copy the integer registers that have not been used for argument passing
3570 // to the argument register save area. For O32, the save area is allocated
3571 // in the caller's stack frame, while for N32/64, it is allocated in the
3572 // callee's stack frame.
3573 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003574 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003575 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3576 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3577 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3578 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3579 MachinePointerInfo(), false, false, 0);
3580 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
3581 OutChains.push_back(Store);
3582 }
3583}