blob: c452dee8d08e5cecd7cfd658a66066dc755fb6ea [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"
Reed Kotler8453b3f2013-01-24 04:24:02 +000015#include <set>
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000016#include "MipsISelLowering.h"
Craig Topper79aa3412012-03-17 18:46:09 +000017#include "InstPrinter/MipsInstPrinter.h"
18#include "MCTargetDesc/MipsBaseInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "MipsMachineFunction.h"
20#include "MipsSubtarget.h"
21#include "MipsTargetMachine.h"
22#include "MipsTargetObjectFile.h"
Akira Hatanaka2b861be2012-10-19 21:47:33 +000023#include "llvm/ADT/Statistic.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000024#include "llvm/CodeGen/CallingConvLower.h"
25#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000029#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000030#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000031#include "llvm/IR/CallingConv.h"
32#include "llvm/IR/DerivedTypes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000033#include "llvm/IR/GlobalVariable.h"
34#include "llvm/IR/Intrinsics.h"
Akira Hatanaka2b861be2012-10-19 21:47:33 +000035#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000036#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumi89593932012-04-21 15:31:45 +000038#include "llvm/Support/raw_ostream.h"
39
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000040using namespace llvm;
41
Akira Hatanaka2b861be2012-10-19 21:47:33 +000042STATISTIC(NumTailCalls, "Number of tail calls");
43
44static cl::opt<bool>
45EnableMipsTailCalls("enable-mips-tail-calls", cl::Hidden,
46 cl::desc("MIPS: Enable tail calls."), cl::init(false));
47
Akira Hatanaka81784cb2012-11-21 20:21:11 +000048static cl::opt<bool>
49LargeGOT("mxgot", cl::Hidden,
50 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
51
Reed Kotlered23fa82012-12-15 00:20:05 +000052static cl::opt<bool>
53Mips16HardFloat("mips16-hard-float", cl::NotHidden,
54 cl::desc("MIPS: mips16 hard float enable."),
55 cl::init(false));
56
Reed Kotlerffbe4322013-02-21 04:22:38 +000057static cl::opt<bool> DontExpandCondPseudos16(
58 "mips16-dont-expand-cond-pseudo",
59 cl::init(false),
60 cl::desc("Dont expand conditional move related "
61 "pseudos for Mips 16"),
62 cl::Hidden);
Reed Kotlered23fa82012-12-15 00:20:05 +000063
64
Akira Hatanakafe30a9b2012-10-27 00:29:43 +000065static const uint16_t O32IntRegs[4] = {
66 Mips::A0, Mips::A1, Mips::A2, Mips::A3
67};
68
69static const uint16_t Mips64IntRegs[8] = {
70 Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
71 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64
72};
73
74static const uint16_t Mips64DPRegs[8] = {
75 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
76 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
77};
78
Jia Liubb481f82012-02-28 07:46:26 +000079// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanakadbe9a312011-08-18 20:07:42 +000080// mask (Pos), and return true.
Jia Liubb481f82012-02-28 07:46:26 +000081// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanakaf635ef42013-03-12 00:16:36 +000082static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanakad6bc5232011-12-05 21:26:34 +000083 if (!isShiftedMask_64(I))
Akira Hatanaka854a7db2011-08-19 22:59:00 +000084 return false;
Akira Hatanakabb15e112011-08-17 02:05:42 +000085
Akira Hatanakad6bc5232011-12-05 21:26:34 +000086 Size = CountPopulation_64(I);
87 Pos = CountTrailingZeros_64(I);
Akira Hatanakadbe9a312011-08-18 20:07:42 +000088 return true;
Akira Hatanakabb15e112011-08-17 02:05:42 +000089}
90
Akira Hatanakaf635ef42013-03-12 00:16:36 +000091static SDValue getGlobalReg(SelectionDAG &DAG, EVT Ty) {
Akira Hatanaka648f00c2012-02-24 22:34:47 +000092 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
93 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
94}
95
Akira Hatanaka6b28b802012-11-21 20:26:38 +000096static SDValue getTargetNode(SDValue Op, SelectionDAG &DAG, unsigned Flag) {
97 EVT Ty = Op.getValueType();
98
99 if (GlobalAddressSDNode *N = dyn_cast<GlobalAddressSDNode>(Op))
100 return DAG.getTargetGlobalAddress(N->getGlobal(), Op.getDebugLoc(), Ty, 0,
101 Flag);
102 if (ExternalSymbolSDNode *N = dyn_cast<ExternalSymbolSDNode>(Op))
103 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
104 if (BlockAddressSDNode *N = dyn_cast<BlockAddressSDNode>(Op))
105 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
106 if (JumpTableSDNode *N = dyn_cast<JumpTableSDNode>(Op))
107 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
108 if (ConstantPoolSDNode *N = dyn_cast<ConstantPoolSDNode>(Op))
109 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
110 N->getOffset(), Flag);
111
112 llvm_unreachable("Unexpected node type.");
113 return SDValue();
114}
115
116static SDValue getAddrNonPIC(SDValue Op, SelectionDAG &DAG) {
117 DebugLoc DL = Op.getDebugLoc();
118 EVT Ty = Op.getValueType();
119 SDValue Hi = getTargetNode(Op, DAG, MipsII::MO_ABS_HI);
120 SDValue Lo = getTargetNode(Op, DAG, MipsII::MO_ABS_LO);
121 return DAG.getNode(ISD::ADD, DL, Ty,
122 DAG.getNode(MipsISD::Hi, DL, Ty, Hi),
123 DAG.getNode(MipsISD::Lo, DL, Ty, Lo));
124}
125
126static SDValue getAddrLocal(SDValue Op, SelectionDAG &DAG, bool HasMips64) {
127 DebugLoc DL = Op.getDebugLoc();
128 EVT Ty = Op.getValueType();
129 unsigned GOTFlag = HasMips64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000130 SDValue GOT = DAG.getNode(MipsISD::Wrapper, DL, Ty, getGlobalReg(DAG, Ty),
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000131 getTargetNode(Op, DAG, GOTFlag));
132 SDValue Load = DAG.getLoad(Ty, DL, DAG.getEntryNode(), GOT,
133 MachinePointerInfo::getGOT(), false, false, false,
134 0);
135 unsigned LoFlag = HasMips64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
136 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, Ty, getTargetNode(Op, DAG, LoFlag));
137 return DAG.getNode(ISD::ADD, DL, Ty, Load, Lo);
138}
139
140static SDValue getAddrGlobal(SDValue Op, SelectionDAG &DAG, unsigned Flag) {
141 DebugLoc DL = Op.getDebugLoc();
142 EVT Ty = Op.getValueType();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000143 SDValue Tgt = DAG.getNode(MipsISD::Wrapper, DL, Ty, getGlobalReg(DAG, Ty),
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000144 getTargetNode(Op, DAG, Flag));
145 return DAG.getLoad(Ty, DL, DAG.getEntryNode(), Tgt,
146 MachinePointerInfo::getGOT(), false, false, false, 0);
147}
148
149static SDValue getAddrGlobalLargeGOT(SDValue Op, SelectionDAG &DAG,
150 unsigned HiFlag, unsigned LoFlag) {
151 DebugLoc DL = Op.getDebugLoc();
152 EVT Ty = Op.getValueType();
153 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, Ty, getTargetNode(Op, DAG, HiFlag));
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000154 Hi = DAG.getNode(ISD::ADD, DL, Ty, Hi, getGlobalReg(DAG, Ty));
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000155 SDValue Wrapper = DAG.getNode(MipsISD::Wrapper, DL, Ty, Hi,
156 getTargetNode(Op, DAG, LoFlag));
157 return DAG.getLoad(Ty, DL, DAG.getEntryNode(), Wrapper,
158 MachinePointerInfo::getGOT(), false, false, false, 0);
159}
160
Chris Lattnerf0144122009-07-28 03:13:23 +0000161const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
162 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000163 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka58d1e3f2012-10-19 20:59:39 +0000164 case MipsISD::TailCall: return "MipsISD::TailCall";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000165 case MipsISD::Hi: return "MipsISD::Hi";
166 case MipsISD::Lo: return "MipsISD::Lo";
167 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000168 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000169 case MipsISD::Ret: return "MipsISD::Ret";
Akira Hatanaka544cc212013-01-30 00:26:49 +0000170 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000171 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
172 case MipsISD::FPCmp: return "MipsISD::FPCmp";
173 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
174 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
175 case MipsISD::FPRound: return "MipsISD::FPRound";
176 case MipsISD::MAdd: return "MipsISD::MAdd";
177 case MipsISD::MAddu: return "MipsISD::MAddu";
178 case MipsISD::MSub: return "MipsISD::MSub";
179 case MipsISD::MSubu: return "MipsISD::MSubu";
180 case MipsISD::DivRem: return "MipsISD::DivRem";
181 case MipsISD::DivRemU: return "MipsISD::DivRemU";
182 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
183 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakabfcb83f2011-12-12 22:38:19 +0000184 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanakadb548262011-07-19 23:30:50 +0000185 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanakabb15e112011-08-17 02:05:42 +0000186 case MipsISD::Ext: return "MipsISD::Ext";
187 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab6f1dc22012-06-02 00:03:12 +0000188 case MipsISD::LWL: return "MipsISD::LWL";
189 case MipsISD::LWR: return "MipsISD::LWR";
190 case MipsISD::SWL: return "MipsISD::SWL";
191 case MipsISD::SWR: return "MipsISD::SWR";
192 case MipsISD::LDL: return "MipsISD::LDL";
193 case MipsISD::LDR: return "MipsISD::LDR";
194 case MipsISD::SDL: return "MipsISD::SDL";
195 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka6fad5e72012-09-21 23:52:47 +0000196 case MipsISD::EXTP: return "MipsISD::EXTP";
197 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
198 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
199 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
200 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
201 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
202 case MipsISD::SHILO: return "MipsISD::SHILO";
203 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
204 case MipsISD::MULT: return "MipsISD::MULT";
205 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liub3ea8802013-03-04 01:06:54 +0000206 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka6fad5e72012-09-21 23:52:47 +0000207 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
208 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
209 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka0f843822011-06-07 18:58:42 +0000210 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000211 }
212}
213
Reed Kotler8453b3f2013-01-24 04:24:02 +0000214namespace {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000215 struct LTStr {
216 bool operator()(const char *S1, const char *S2) const
Reed Kotler8453b3f2013-01-24 04:24:02 +0000217 {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000218 return strcmp(S1, S2) < 0;
Reed Kotler8453b3f2013-01-24 04:24:02 +0000219 }
220 };
221
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000222 std::set<const char*, LTStr> NoHelperNeeded;
Reed Kotler8453b3f2013-01-24 04:24:02 +0000223}
224
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000225void MipsTargetLowering::setMips16LibcallName
226 (RTLIB::Libcall L, const char *Name) {
227 setLibcallName(L, Name);
228 NoHelperNeeded.insert(Name);
Reed Kotlerbc49cf72013-01-28 02:46:49 +0000229}
230
Reed Kotlered23fa82012-12-15 00:20:05 +0000231void MipsTargetLowering::setMips16HardFloatLibCalls() {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000232 setMips16LibcallName(RTLIB::ADD_F32, "__mips16_addsf3");
233 setMips16LibcallName(RTLIB::ADD_F64, "__mips16_adddf3");
234 setMips16LibcallName(RTLIB::SUB_F32, "__mips16_subsf3");
235 setMips16LibcallName(RTLIB::SUB_F64, "__mips16_subdf3");
236 setMips16LibcallName(RTLIB::MUL_F32, "__mips16_mulsf3");
237 setMips16LibcallName(RTLIB::MUL_F64, "__mips16_muldf3");
238 setMips16LibcallName(RTLIB::DIV_F32, "__mips16_divsf3");
239 setMips16LibcallName(RTLIB::DIV_F64, "__mips16_divdf3");
240 setMips16LibcallName(RTLIB::FPEXT_F32_F64, "__mips16_extendsfdf2");
241 setMips16LibcallName(RTLIB::FPROUND_F64_F32, "__mips16_truncdfsf2");
242 setMips16LibcallName(RTLIB::FPTOSINT_F32_I32, "__mips16_fix_truncsfsi");
243 setMips16LibcallName(RTLIB::FPTOSINT_F64_I32, "__mips16_fix_truncdfsi");
244 setMips16LibcallName(RTLIB::SINTTOFP_I32_F32, "__mips16_floatsisf");
245 setMips16LibcallName(RTLIB::SINTTOFP_I32_F64, "__mips16_floatsidf");
246 setMips16LibcallName(RTLIB::UINTTOFP_I32_F32, "__mips16_floatunsisf");
247 setMips16LibcallName(RTLIB::UINTTOFP_I32_F64, "__mips16_floatunsidf");
248 setMips16LibcallName(RTLIB::OEQ_F32, "__mips16_eqsf2");
249 setMips16LibcallName(RTLIB::OEQ_F64, "__mips16_eqdf2");
250 setMips16LibcallName(RTLIB::UNE_F32, "__mips16_nesf2");
251 setMips16LibcallName(RTLIB::UNE_F64, "__mips16_nedf2");
252 setMips16LibcallName(RTLIB::OGE_F32, "__mips16_gesf2");
253 setMips16LibcallName(RTLIB::OGE_F64, "__mips16_gedf2");
254 setMips16LibcallName(RTLIB::OLT_F32, "__mips16_ltsf2");
255 setMips16LibcallName(RTLIB::OLT_F64, "__mips16_ltdf2");
256 setMips16LibcallName(RTLIB::OLE_F32, "__mips16_lesf2");
257 setMips16LibcallName(RTLIB::OLE_F64, "__mips16_ledf2");
258 setMips16LibcallName(RTLIB::OGT_F32, "__mips16_gtsf2");
259 setMips16LibcallName(RTLIB::OGT_F64, "__mips16_gtdf2");
260 setMips16LibcallName(RTLIB::UO_F32, "__mips16_unordsf2");
261 setMips16LibcallName(RTLIB::UO_F64, "__mips16_unorddf2");
262 setMips16LibcallName(RTLIB::O_F32, "__mips16_unordsf2");
263 setMips16LibcallName(RTLIB::O_F64, "__mips16_unorddf2");
Reed Kotlered23fa82012-12-15 00:20:05 +0000264}
265
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000266MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +0000267MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka8b4198d2011-09-26 21:47:02 +0000268 : TargetLowering(TM, new MipsTargetObjectFile()),
269 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +0000270 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
271 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000272
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000273 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000274 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +0000275 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000276 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000277
278 // Set up the register classes
Craig Topper420761a2012-04-20 07:30:17 +0000279 addRegisterClass(MVT::i32, &Mips::CPURegsRegClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000280
Akira Hatanaka95934842011-09-24 01:34:44 +0000281 if (HasMips64)
Craig Topper420761a2012-04-20 07:30:17 +0000282 addRegisterClass(MVT::i64, &Mips::CPU64RegsRegClass);
Akira Hatanaka95934842011-09-24 01:34:44 +0000283
Akira Hatanaka28ee4fd2012-05-31 02:59:44 +0000284 if (Subtarget->inMips16Mode()) {
285 addRegisterClass(MVT::i32, &Mips::CPU16RegsRegClass);
Reed Kotlered23fa82012-12-15 00:20:05 +0000286 if (Mips16HardFloat)
287 setMips16HardFloatLibCalls();
Akira Hatanaka28ee4fd2012-05-31 02:59:44 +0000288 }
289
Akira Hatanakab430cec2012-09-21 23:58:31 +0000290 if (Subtarget->hasDSP()) {
291 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
292
293 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
294 addRegisterClass(VecTys[i], &Mips::DSPRegsRegClass);
295
296 // Expand all builtin opcodes.
297 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
298 setOperationAction(Opc, VecTys[i], Expand);
299
300 setOperationAction(ISD::LOAD, VecTys[i], Legal);
301 setOperationAction(ISD::STORE, VecTys[i], Legal);
302 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
303 }
304 }
305
Akira Hatanakab0e7af72012-01-04 19:29:11 +0000306 if (!TM.Options.UseSoftFloat) {
Craig Topper420761a2012-04-20 07:30:17 +0000307 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
Akira Hatanakab0e7af72012-01-04 19:29:11 +0000308
309 // When dealing with single precision only, use libcalls
310 if (!Subtarget->isSingleFloat()) {
311 if (HasMips64)
Craig Topper420761a2012-04-20 07:30:17 +0000312 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
Akira Hatanakab0e7af72012-01-04 19:29:11 +0000313 else
Craig Topper420761a2012-04-20 07:30:17 +0000314 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
Akira Hatanakab0e7af72012-01-04 19:29:11 +0000315 }
Akira Hatanaka792016b2011-09-23 18:28:39 +0000316 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000317
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000318 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
320 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
321 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000322
Eli Friedman6055a6a2009-07-17 04:07:24 +0000323 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
325 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000326
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000327 // Used by legalize types to correctly generate the setcc result.
328 // Without this, every float setcc comes with a AND/OR with the result,
329 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000330 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000331 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000332
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000333 // Mips Custom Operations
Akira Hatanakab7656a92013-03-06 21:32:03 +0000334 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000335 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000336 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
338 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
339 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
340 setOperationAction(ISD::SELECT, MVT::f32, Custom);
341 setOperationAction(ISD::SELECT, MVT::f64, Custom);
342 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +0000343 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
344 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanaka0a40c232012-03-09 23:46:03 +0000345 setOperationAction(ISD::SETCC, MVT::f32, Custom);
346 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000347 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000348 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000349 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
350 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Reed Kotler8834a202012-10-29 16:16:54 +0000351 if (Subtarget->inMips16Mode()) {
352 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
353 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
354 }
355 else {
356 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
357 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
358 }
Akira Hatanakaf934d152012-09-15 01:02:03 +0000359 if (!Subtarget->inMips16Mode()) {
360 setOperationAction(ISD::LOAD, MVT::i32, Custom);
361 setOperationAction(ISD::STORE, MVT::i32, Custom);
362 }
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000363
Akira Hatanakac12a6e62012-04-11 22:49:04 +0000364 if (!TM.Options.NoNaNsFPMath) {
365 setOperationAction(ISD::FABS, MVT::f32, Custom);
366 setOperationAction(ISD::FABS, MVT::f64, Custom);
367 }
368
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000369 if (HasMips64) {
370 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
371 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
372 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
373 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
374 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
375 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka7664f052012-06-02 00:04:42 +0000376 setOperationAction(ISD::LOAD, MVT::i64, Custom);
377 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000378 }
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000379
Akira Hatanakaa284acb2012-05-09 00:55:21 +0000380 if (!HasMips64) {
381 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
382 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
383 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
384 }
385
Akira Hatanakae90a3bc2012-11-07 19:10:58 +0000386 setOperationAction(ISD::ADD, MVT::i32, Custom);
387 if (HasMips64)
388 setOperationAction(ISD::ADD, MVT::i64, Custom);
389
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000390 setOperationAction(ISD::SDIV, MVT::i32, Expand);
391 setOperationAction(ISD::SREM, MVT::i32, Expand);
392 setOperationAction(ISD::UDIV, MVT::i32, Expand);
393 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000394 setOperationAction(ISD::SDIV, MVT::i64, Expand);
395 setOperationAction(ISD::SREM, MVT::i64, Expand);
396 setOperationAction(ISD::UDIV, MVT::i64, Expand);
397 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000398
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000399 // Operations not directly supported by Mips.
Tom Stellard3ef53832013-03-08 15:36:57 +0000400 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
401 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
402 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
403 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
405 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000406 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000408 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000409 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
410 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000411 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000413 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000414 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
415 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
416 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
417 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000418 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000419 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka1d165f12012-07-31 20:54:48 +0000420 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
421 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000422
Akira Hatanaka56633442011-09-20 23:53:09 +0000423 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000424 setOperationAction(ISD::ROTR, MVT::i32, Expand);
425
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000426 if (!Subtarget->hasMips64r2())
427 setOperationAction(ISD::ROTR, MVT::i64, Expand);
428
Owen Anderson825b72b2009-08-11 20:47:22 +0000429 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000430 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000432 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000433 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
434 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
436 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000437 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000438 setOperationAction(ISD::FLOG, MVT::f32, Expand);
439 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
440 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
441 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000442 setOperationAction(ISD::FMA, MVT::f32, Expand);
443 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka21ecc2f2012-03-29 18:43:11 +0000444 setOperationAction(ISD::FREM, MVT::f32, Expand);
445 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000446
Akira Hatanaka1cc63332012-04-11 22:59:08 +0000447 if (!TM.Options.NoNaNsFPMath) {
448 setOperationAction(ISD::FNEG, MVT::f32, Expand);
449 setOperationAction(ISD::FNEG, MVT::f64, Expand);
450 }
451
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000452 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
Akira Hatanaka590baca2012-02-02 03:13:40 +0000453 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000454 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Akira Hatanaka590baca2012-02-02 03:13:40 +0000455 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000456
Akira Hatanaka544cc212013-01-30 00:26:49 +0000457 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
458
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000459 setOperationAction(ISD::VAARG, MVT::Other, Expand);
460 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
461 setOperationAction(ISD::VAEND, MVT::Other, Expand);
462
Akira Hatanakab430cec2012-09-21 23:58:31 +0000463 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
464 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
465
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000466 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
468 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000469
Jia Liubb481f82012-02-28 07:46:26 +0000470 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
471 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
472 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
473 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman4db5aca2011-08-29 18:23:02 +0000474
Reed Kotler8834a202012-10-29 16:16:54 +0000475 if (Subtarget->inMips16Mode()) {
476 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
477 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
478 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
479 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
480 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
481 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
482 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
483 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
484 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
485 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
486 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
487 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
488 }
489
Eli Friedman26689ac2011-08-03 21:06:02 +0000490 setInsertFencesForAtomic(true);
491
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000492 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000493 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
494 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000495 }
496
Akira Hatanakac79507a2011-12-21 00:20:27 +0000497 if (!Subtarget->hasBitCount()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000498 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Akira Hatanakac79507a2011-12-21 00:20:27 +0000499 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
500 }
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000501
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000502 if (!Subtarget->hasSwap()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000503 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000504 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
505 }
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000506
Akira Hatanaka7664f052012-06-02 00:04:42 +0000507 if (HasMips64) {
508 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
509 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
510 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
511 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
512 }
513
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000514 setTargetDAGCombine(ISD::ADDE);
515 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000516 setTargetDAGCombine(ISD::SDIVREM);
517 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanakaee8c3b02012-03-08 03:26:37 +0000518 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000519 setTargetDAGCombine(ISD::AND);
520 setTargetDAGCombine(ISD::OR);
Akira Hatanaka87827072012-06-13 20:33:18 +0000521 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000522
Akira Hatanaka5fdf5002012-03-08 01:59:33 +0000523 setMinFunctionAlignment(HasMips64 ? 3 : 2);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000524
Akira Hatanaka3f5b1072012-02-02 03:17:04 +0000525 setStackPointerRegisterToSaveRestore(IsN64 ? Mips::SP_64 : Mips::SP);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000526 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000527
Akira Hatanaka590baca2012-02-02 03:13:40 +0000528 setExceptionPointerRegister(IsN64 ? Mips::A0_64 : Mips::A0);
529 setExceptionSelectorRegister(IsN64 ? Mips::A1_64 : Mips::A1);
Akira Hatanakae193b322012-06-13 19:33:32 +0000530
Jim Grosbach3450f802013-02-20 21:13:59 +0000531 MaxStoresPerMemcpy = 16;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000532}
533
Evan Cheng376642e2012-12-10 23:21:26 +0000534bool
535MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
Akira Hatanaka511961a2011-08-17 18:49:18 +0000536 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
Jia Liubb481f82012-02-28 07:46:26 +0000537
Akira Hatanakaf934d152012-09-15 01:02:03 +0000538 if (Subtarget->inMips16Mode())
539 return false;
540
Akira Hatanaka44b6c712012-02-28 02:55:02 +0000541 switch (SVT) {
542 case MVT::i64:
543 case MVT::i32:
Evan Cheng376642e2012-12-10 23:21:26 +0000544 if (Fast)
545 *Fast = true;
Akira Hatanaka44b6c712012-02-28 02:55:02 +0000546 return true;
Akira Hatanaka44b6c712012-02-28 02:55:02 +0000547 default:
548 return false;
549 }
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000550}
551
Duncan Sands28b77e92011-09-06 19:07:46 +0000552EVT MipsTargetLowering::getSetCCResultType(EVT VT) const {
Akira Hatanakae13f4412013-01-04 20:06:01 +0000553 if (!VT.isVector())
554 return MVT::i32;
555 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +0000556}
557
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000558// selectMADD -
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000559// Transforms a subgraph in CurDAG if the following pattern is found:
560// (addc multLo, Lo0), (adde multHi, Hi0),
561// where,
562// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000563// Lo0: initial value of Lo register
564// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000565// Return true if pattern matching was successful.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000566static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000567 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000568 // for the matching to be successful.
Akira Hatanaka864f6602012-06-14 21:10:56 +0000569 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000570
571 if (ADDCNode->getOpcode() != ISD::ADDC)
572 return false;
573
574 SDValue MultHi = ADDENode->getOperand(0);
575 SDValue MultLo = ADDCNode->getOperand(0);
Akira Hatanaka864f6602012-06-14 21:10:56 +0000576 SDNode *MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000577 unsigned MultOpc = MultHi.getOpcode();
578
579 // MultHi and MultLo must be generated by the same node,
580 if (MultLo.getNode() != MultNode)
581 return false;
582
583 // and it must be a multiplication.
584 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
585 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000586
587 // MultLo amd MultHi must be the first and second output of MultNode
588 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000589 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
590 return false;
591
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000592 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000593 // of the values of MultNode, in which case MultNode will be removed in later
594 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000595 // If there exist users other than ADDENode or ADDCNode, this function returns
596 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000597 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000598 // produced.
599 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
600 return false;
601
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000602 SDValue Chain = CurDAG->getEntryNode();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000603 DebugLoc DL = ADDENode->getDebugLoc();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000604
605 // create MipsMAdd(u) node
606 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000607
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000608 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Glue,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000609 MultNode->getOperand(0),// Factor 0
610 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000611 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000612 ADDENode->getOperand(1));// Hi0
613
614 // create CopyFromReg nodes
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000615 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, DL, Mips::LO, MVT::i32,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000616 MAdd);
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000617 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), DL,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000618 Mips::HI, MVT::i32,
619 CopyFromLo.getValue(2));
620
621 // replace uses of adde and addc here
622 if (!SDValue(ADDCNode, 0).use_empty())
623 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
624
625 if (!SDValue(ADDENode, 0).use_empty())
626 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
627
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000628 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000629}
630
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000631// selectMSUB -
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000632// Transforms a subgraph in CurDAG if the following pattern is found:
633// (addc Lo0, multLo), (sube Hi0, multHi),
634// where,
635// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000636// Lo0: initial value of Lo register
637// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000638// Return true if pattern matching was successful.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000639static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000640 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000641 // for the matching to be successful.
Akira Hatanaka864f6602012-06-14 21:10:56 +0000642 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000643
644 if (SUBCNode->getOpcode() != ISD::SUBC)
645 return false;
646
647 SDValue MultHi = SUBENode->getOperand(1);
648 SDValue MultLo = SUBCNode->getOperand(1);
Akira Hatanaka864f6602012-06-14 21:10:56 +0000649 SDNode *MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000650 unsigned MultOpc = MultHi.getOpcode();
651
652 // MultHi and MultLo must be generated by the same node,
653 if (MultLo.getNode() != MultNode)
654 return false;
655
656 // and it must be a multiplication.
657 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
658 return false;
659
660 // MultLo amd MultHi must be the first and second output of MultNode
661 // respectively.
662 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
663 return false;
664
665 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
666 // of the values of MultNode, in which case MultNode will be removed in later
667 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000668 // If there exist users other than SUBENode or SUBCNode, this function returns
669 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000670 // instruction node rather than a pair of MULT and MSUB instructions being
671 // produced.
672 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
673 return false;
674
675 SDValue Chain = CurDAG->getEntryNode();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000676 DebugLoc DL = SUBENode->getDebugLoc();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000677
678 // create MipsSub(u) node
679 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
680
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000681 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000682 MultNode->getOperand(0),// Factor 0
683 MultNode->getOperand(1),// Factor 1
684 SUBCNode->getOperand(0),// Lo0
685 SUBENode->getOperand(0));// Hi0
686
687 // create CopyFromReg nodes
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000688 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, DL, Mips::LO, MVT::i32,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000689 MSub);
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000690 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), DL,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000691 Mips::HI, MVT::i32,
692 CopyFromLo.getValue(2));
693
694 // replace uses of sube and subc here
695 if (!SDValue(SUBCNode, 0).use_empty())
696 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
697
698 if (!SDValue(SUBENode, 0).use_empty())
699 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
700
701 return true;
702}
703
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000704static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000705 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000706 const MipsSubtarget *Subtarget) {
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000707 if (DCI.isBeforeLegalize())
708 return SDValue();
709
Akira Hatanakae184fec2011-11-11 04:18:21 +0000710 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000711 selectMADD(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000712 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000713
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000714 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000715}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000716
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000717static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000718 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000719 const MipsSubtarget *Subtarget) {
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000720 if (DCI.isBeforeLegalize())
721 return SDValue();
722
Akira Hatanakae184fec2011-11-11 04:18:21 +0000723 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000724 selectMSUB(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000725 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000726
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000727 return SDValue();
728}
729
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000730static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000731 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000732 const MipsSubtarget *Subtarget) {
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000733 if (DCI.isBeforeLegalizeOps())
734 return SDValue();
735
Akira Hatanakadda4a072011-10-03 21:06:13 +0000736 EVT Ty = N->getValueType(0);
Jia Liubb481f82012-02-28 07:46:26 +0000737 unsigned LO = (Ty == MVT::i32) ? Mips::LO : Mips::LO64;
738 unsigned HI = (Ty == MVT::i32) ? Mips::HI : Mips::HI64;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000739 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000740 MipsISD::DivRemU;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000741 DebugLoc DL = N->getDebugLoc();
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000742
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000743 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000744 N->getOperand(0), N->getOperand(1));
745 SDValue InChain = DAG.getEntryNode();
746 SDValue InGlue = DivRem;
747
748 // insert MFLO
749 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000750 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000751 InGlue);
752 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
753 InChain = CopyFromLo.getValue(1);
754 InGlue = CopyFromLo.getValue(2);
755 }
756
757 // insert MFHI
758 if (N->hasAnyUseOfValue(1)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000759 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000760 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000761 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
762 }
763
764 return SDValue();
765}
766
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000767static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
768 switch (CC) {
769 default: llvm_unreachable("Unknown fp condition code!");
770 case ISD::SETEQ:
771 case ISD::SETOEQ: return Mips::FCOND_OEQ;
772 case ISD::SETUNE: return Mips::FCOND_UNE;
773 case ISD::SETLT:
774 case ISD::SETOLT: return Mips::FCOND_OLT;
775 case ISD::SETGT:
776 case ISD::SETOGT: return Mips::FCOND_OGT;
777 case ISD::SETLE:
778 case ISD::SETOLE: return Mips::FCOND_OLE;
779 case ISD::SETGE:
780 case ISD::SETOGE: return Mips::FCOND_OGE;
781 case ISD::SETULT: return Mips::FCOND_ULT;
782 case ISD::SETULE: return Mips::FCOND_ULE;
783 case ISD::SETUGT: return Mips::FCOND_UGT;
784 case ISD::SETUGE: return Mips::FCOND_UGE;
785 case ISD::SETUO: return Mips::FCOND_UN;
786 case ISD::SETO: return Mips::FCOND_OR;
787 case ISD::SETNE:
788 case ISD::SETONE: return Mips::FCOND_ONE;
789 case ISD::SETUEQ: return Mips::FCOND_UEQ;
790 }
791}
792
793
794// Returns true if condition code has to be inverted.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000795static bool invertFPCondCode(Mips::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000796 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
797 return false;
798
Akira Hatanaka82099682011-12-19 19:52:25 +0000799 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
800 "Illegal Condition Code");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000801
Akira Hatanaka82099682011-12-19 19:52:25 +0000802 return true;
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000803}
804
805// Creates and returns an FPCmp node from a setcc node.
806// Returns Op if setcc is not a floating point comparison.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000807static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000808 // must be a SETCC node
809 if (Op.getOpcode() != ISD::SETCC)
810 return Op;
811
812 SDValue LHS = Op.getOperand(0);
813
814 if (!LHS.getValueType().isFloatingPoint())
815 return Op;
816
817 SDValue RHS = Op.getOperand(1);
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000818 DebugLoc DL = Op.getDebugLoc();
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000819
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000820 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
821 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000822 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
823
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000824 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000825 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
826}
827
828// Creates and returns a CMovFPT/F node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000829static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000830 SDValue False, DebugLoc DL) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000831 bool invert = invertFPCondCode((Mips::CondCode)
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000832 cast<ConstantSDNode>(Cond.getOperand(2))
833 ->getSExtValue());
834
835 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
836 True.getValueType(), True, False, Cond);
837}
838
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000839static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000840 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000841 const MipsSubtarget *Subtarget) {
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000842 if (DCI.isBeforeLegalizeOps())
843 return SDValue();
844
845 SDValue SetCC = N->getOperand(0);
846
847 if ((SetCC.getOpcode() != ISD::SETCC) ||
848 !SetCC.getOperand(0).getValueType().isInteger())
849 return SDValue();
850
851 SDValue False = N->getOperand(2);
852 EVT FalseTy = False.getValueType();
853
854 if (!FalseTy.isInteger())
855 return SDValue();
856
857 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(False);
858
859 if (!CN || CN->getZExtValue())
860 return SDValue();
861
862 const DebugLoc DL = N->getDebugLoc();
863 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
864 SDValue True = N->getOperand(1);
Akira Hatanaka864f6602012-06-14 21:10:56 +0000865
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000866 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
867 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
Akira Hatanaka864f6602012-06-14 21:10:56 +0000868
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000869 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
870}
871
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000872static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000873 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000874 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000875 // Pattern match EXT.
876 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
877 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000878 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000879 return SDValue();
880
881 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000882 unsigned ShiftRightOpc = ShiftRight.getOpcode();
883
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000884 // Op's first operand must be a shift right.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000885 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000886 return SDValue();
887
888 // The second operand of the shift must be an immediate.
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000889 ConstantSDNode *CN;
890 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
891 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000892
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000893 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000894 uint64_t SMPos, SMSize;
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000895
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000896 // Op's second operand must be a shifted mask.
897 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000898 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000899 return SDValue();
900
901 // Return if the shifted mask does not start at bit 0 or the sum of its size
902 // and Pos exceeds the word's size.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000903 EVT ValTy = N->getValueType(0);
904 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000905 return SDValue();
906
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000907 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), ValTy,
Akira Hatanaka82099682011-12-19 19:52:25 +0000908 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000909 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000910}
Jia Liubb481f82012-02-28 07:46:26 +0000911
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000912static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000913 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000914 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000915 // Pattern match INS.
916 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liubb481f82012-02-28 07:46:26 +0000917 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000918 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000919 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000920 return SDValue();
921
922 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
923 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
924 ConstantSDNode *CN;
925
926 // See if Op's first operand matches (and $src1 , mask0).
927 if (And0.getOpcode() != ISD::AND)
928 return SDValue();
929
930 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000931 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000932 return SDValue();
933
934 // See if Op's second operand matches (and (shl $src, pos), mask1).
935 if (And1.getOpcode() != ISD::AND)
936 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000937
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000938 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000939 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000940 return SDValue();
941
942 // The shift masks must have the same position and size.
943 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
944 return SDValue();
945
946 SDValue Shl = And1.getOperand(0);
947 if (Shl.getOpcode() != ISD::SHL)
948 return SDValue();
949
950 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
951 return SDValue();
952
953 unsigned Shamt = CN->getZExtValue();
954
955 // Return if the shift amount and the first bit position of mask are not the
Jia Liubb481f82012-02-28 07:46:26 +0000956 // same.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000957 EVT ValTy = N->getValueType(0);
958 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000959 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000960
Akira Hatanaka82099682011-12-19 19:52:25 +0000961 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), ValTy, Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000962 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka82099682011-12-19 19:52:25 +0000963 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000964}
Jia Liubb481f82012-02-28 07:46:26 +0000965
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000966static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka87827072012-06-13 20:33:18 +0000967 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000968 const MipsSubtarget *Subtarget) {
Akira Hatanaka87827072012-06-13 20:33:18 +0000969 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
970
971 if (DCI.isBeforeLegalizeOps())
972 return SDValue();
973
974 SDValue Add = N->getOperand(1);
975
976 if (Add.getOpcode() != ISD::ADD)
977 return SDValue();
978
979 SDValue Lo = Add.getOperand(1);
980
981 if ((Lo.getOpcode() != MipsISD::Lo) ||
982 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
983 return SDValue();
984
985 EVT ValTy = N->getValueType(0);
986 DebugLoc DL = N->getDebugLoc();
987
988 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
989 Add.getOperand(0));
990 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
991}
992
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000993SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000994 const {
995 SelectionDAG &DAG = DCI.DAG;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000996 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000997
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000998 switch (Opc) {
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000999 default: break;
1000 case ISD::ADDE:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001001 return performADDECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +00001002 case ISD::SUBE:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001003 return performSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +00001004 case ISD::SDIVREM:
1005 case ISD::UDIVREM:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001006 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +00001007 case ISD::SELECT:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001008 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +00001009 case ISD::AND:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001010 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +00001011 case ISD::OR:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001012 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka87827072012-06-13 20:33:18 +00001013 case ISD::ADD:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001014 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +00001015 }
1016
1017 return SDValue();
1018}
1019
Akira Hatanakab430cec2012-09-21 23:58:31 +00001020void
1021MipsTargetLowering::LowerOperationWrapper(SDNode *N,
1022 SmallVectorImpl<SDValue> &Results,
1023 SelectionDAG &DAG) const {
1024 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
1025
1026 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
1027 Results.push_back(Res.getValue(I));
1028}
1029
1030void
1031MipsTargetLowering::ReplaceNodeResults(SDNode *N,
1032 SmallVectorImpl<SDValue> &Results,
1033 SelectionDAG &DAG) const {
1034 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
1035
1036 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
1037 Results.push_back(Res.getValue(I));
1038}
1039
Dan Gohman475871a2008-07-27 21:46:04 +00001040SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001041LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001042{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001043 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001044 {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001045 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
1046 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
1047 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
1048 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
1049 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
1050 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
1051 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
1052 case ISD::SELECT: return lowerSELECT(Op, DAG);
1053 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
1054 case ISD::SETCC: return lowerSETCC(Op, DAG);
1055 case ISD::VASTART: return lowerVASTART(Op, DAG);
1056 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
1057 case ISD::FABS: return lowerFABS(Op, DAG);
1058 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
1059 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
1060 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
1061 case ISD::MEMBARRIER: return lowerMEMBARRIER(Op, DAG);
1062 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
1063 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
1064 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
1065 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
1066 case ISD::LOAD: return lowerLOAD(Op, DAG);
1067 case ISD::STORE: return lowerSTORE(Op, DAG);
1068 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
1069 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
1070 case ISD::ADD: return lowerADD(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001071 }
Dan Gohman475871a2008-07-27 21:46:04 +00001072 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001073}
1074
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001075//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001076// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001077//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001078
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001079// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001080// MachineFunction as a live in value. It also creates a corresponding
1081// virtual register for it.
1082static unsigned
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001083addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001084{
Chris Lattner84bc5422007-12-31 04:13:23 +00001085 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1086 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001087 return VReg;
1088}
1089
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001090// Get fp branch code (not opcode) from condition code.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001091static Mips::FPBranchCode getFPBranchCodeFromCond(Mips::CondCode CC) {
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001092 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
1093 return Mips::BRANCH_T;
1094
Akira Hatanaka82099682011-12-19 19:52:25 +00001095 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
1096 "Invalid CondCode.");
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001097
Akira Hatanaka82099682011-12-19 19:52:25 +00001098 return Mips::BRANCH_F;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001099}
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001100
Akira Hatanaka01f70892012-09-27 02:15:57 +00001101MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001102MipsTargetLowering::emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
Akira Hatanaka01f70892012-09-27 02:15:57 +00001103 // $bb:
1104 // bposge32_pseudo $vr0
1105 // =>
1106 // $bb:
1107 // bposge32 $tbb
1108 // $fbb:
1109 // li $vr2, 0
1110 // b $sink
1111 // $tbb:
1112 // li $vr1, 1
1113 // $sink:
1114 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
1115
1116 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
1117 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1118 const TargetRegisterClass *RC = &Mips::CPURegsRegClass;
1119 DebugLoc DL = MI->getDebugLoc();
1120 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1121 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
1122 MachineFunction *F = BB->getParent();
1123 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
1124 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
1125 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
1126 F->insert(It, FBB);
1127 F->insert(It, TBB);
1128 F->insert(It, Sink);
1129
1130 // Transfer the remainder of BB and its successor edges to Sink.
1131 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
1132 BB->end());
1133 Sink->transferSuccessorsAndUpdatePHIs(BB);
1134
1135 // Add successors.
1136 BB->addSuccessor(FBB);
1137 BB->addSuccessor(TBB);
1138 FBB->addSuccessor(Sink);
1139 TBB->addSuccessor(Sink);
1140
1141 // Insert the real bposge32 instruction to $BB.
1142 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
1143
1144 // Fill $FBB.
1145 unsigned VR2 = RegInfo.createVirtualRegister(RC);
1146 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
1147 .addReg(Mips::ZERO).addImm(0);
1148 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
1149
1150 // Fill $TBB.
1151 unsigned VR1 = RegInfo.createVirtualRegister(RC);
1152 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
1153 .addReg(Mips::ZERO).addImm(1);
1154
1155 // Insert phi function to $Sink.
1156 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
1157 MI->getOperand(0).getReg())
1158 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
1159
1160 MI->eraseFromParent(); // The pseudo instruction is gone now.
1161 return Sink;
1162}
1163
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001164MachineBasicBlock *MipsTargetLowering::emitSel16(unsigned Opc, MachineInstr *MI,
Reed Kotlerffbe4322013-02-21 04:22:38 +00001165 MachineBasicBlock *BB) const {
1166 if (DontExpandCondPseudos16)
1167 return BB;
1168 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001169 DebugLoc DL = MI->getDebugLoc();
Reed Kotlerffbe4322013-02-21 04:22:38 +00001170 // To "insert" a SELECT_CC instruction, we actually have to insert the
1171 // diamond control-flow pattern. The incoming instruction knows the
1172 // destination vreg to set, the condition code register to branch on, the
1173 // true/false values to select between, and a branch opcode to use.
1174 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1175 MachineFunction::iterator It = BB;
1176 ++It;
1177
1178 // thisMBB:
1179 // ...
1180 // TrueVal = ...
1181 // setcc r1, r2, r3
1182 // bNE r1, r0, copy1MBB
1183 // fallthrough --> copy0MBB
1184 MachineBasicBlock *thisMBB = BB;
1185 MachineFunction *F = BB->getParent();
1186 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1187 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
1188 F->insert(It, copy0MBB);
1189 F->insert(It, sinkMBB);
1190
1191 // Transfer the remainder of BB and its successor edges to sinkMBB.
1192 sinkMBB->splice(sinkMBB->begin(), BB,
1193 llvm::next(MachineBasicBlock::iterator(MI)),
1194 BB->end());
1195 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
1196
1197 // Next, add the true and fallthrough blocks as its successors.
1198 BB->addSuccessor(copy0MBB);
1199 BB->addSuccessor(sinkMBB);
1200
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001201 BuildMI(BB, DL, TII->get(Opc)).addReg(MI->getOperand(3).getReg())
Reed Kotlerffbe4322013-02-21 04:22:38 +00001202 .addMBB(sinkMBB);
1203
1204 // copy0MBB:
1205 // %FalseValue = ...
1206 // # fallthrough to sinkMBB
1207 BB = copy0MBB;
1208
1209 // Update machine-CFG edges
1210 BB->addSuccessor(sinkMBB);
1211
1212 // sinkMBB:
1213 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
1214 // ...
1215 BB = sinkMBB;
1216
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001217 BuildMI(*BB, BB->begin(), DL,
Reed Kotlerffbe4322013-02-21 04:22:38 +00001218 TII->get(Mips::PHI), MI->getOperand(0).getReg())
1219 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB)
1220 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB);
1221
1222 MI->eraseFromParent(); // The pseudo instruction is gone now.
1223 return BB;
1224}
1225
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001226MachineBasicBlock *MipsTargetLowering::emitSelT16
Reed Kotler50354a32013-02-23 03:09:56 +00001227 (unsigned Opc1, unsigned Opc2,
1228 MachineInstr *MI, MachineBasicBlock *BB) const {
1229 if (DontExpandCondPseudos16)
1230 return BB;
1231 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001232 DebugLoc DL = MI->getDebugLoc();
Reed Kotler50354a32013-02-23 03:09:56 +00001233 // To "insert" a SELECT_CC instruction, we actually have to insert the
1234 // diamond control-flow pattern. The incoming instruction knows the
1235 // destination vreg to set, the condition code register to branch on, the
1236 // true/false values to select between, and a branch opcode to use.
1237 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1238 MachineFunction::iterator It = BB;
1239 ++It;
1240
1241 // thisMBB:
1242 // ...
1243 // TrueVal = ...
1244 // setcc r1, r2, r3
1245 // bNE r1, r0, copy1MBB
1246 // fallthrough --> copy0MBB
1247 MachineBasicBlock *thisMBB = BB;
1248 MachineFunction *F = BB->getParent();
1249 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1250 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
1251 F->insert(It, copy0MBB);
1252 F->insert(It, sinkMBB);
1253
1254 // Transfer the remainder of BB and its successor edges to sinkMBB.
1255 sinkMBB->splice(sinkMBB->begin(), BB,
1256 llvm::next(MachineBasicBlock::iterator(MI)),
1257 BB->end());
1258 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
1259
1260 // Next, add the true and fallthrough blocks as its successors.
1261 BB->addSuccessor(copy0MBB);
1262 BB->addSuccessor(sinkMBB);
1263
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001264 BuildMI(BB, DL, TII->get(Opc2)).addReg(MI->getOperand(3).getReg())
Reed Kotler50354a32013-02-23 03:09:56 +00001265 .addReg(MI->getOperand(4).getReg());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001266 BuildMI(BB, DL, TII->get(Opc1)).addMBB(sinkMBB);
Reed Kotler50354a32013-02-23 03:09:56 +00001267
1268 // copy0MBB:
1269 // %FalseValue = ...
1270 // # fallthrough to sinkMBB
1271 BB = copy0MBB;
1272
1273 // Update machine-CFG edges
1274 BB->addSuccessor(sinkMBB);
1275
1276 // sinkMBB:
1277 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
1278 // ...
1279 BB = sinkMBB;
1280
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001281 BuildMI(*BB, BB->begin(), DL,
Reed Kotler50354a32013-02-23 03:09:56 +00001282 TII->get(Mips::PHI), MI->getOperand(0).getReg())
1283 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB)
1284 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB);
1285
1286 MI->eraseFromParent(); // The pseudo instruction is gone now.
1287 return BB;
1288
1289}
1290
1291
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001292MachineBasicBlock *MipsTargetLowering::emitSeliT16
Reed Kotler7617d0322013-02-22 05:10:51 +00001293 (unsigned Opc1, unsigned Opc2,
1294 MachineInstr *MI, MachineBasicBlock *BB) const {
1295 if (DontExpandCondPseudos16)
1296 return BB;
1297 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001298 DebugLoc DL = MI->getDebugLoc();
Reed Kotler7617d0322013-02-22 05:10:51 +00001299 // To "insert" a SELECT_CC instruction, we actually have to insert the
1300 // diamond control-flow pattern. The incoming instruction knows the
1301 // destination vreg to set, the condition code register to branch on, the
1302 // true/false values to select between, and a branch opcode to use.
1303 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1304 MachineFunction::iterator It = BB;
1305 ++It;
1306
1307 // thisMBB:
1308 // ...
1309 // TrueVal = ...
1310 // setcc r1, r2, r3
1311 // bNE r1, r0, copy1MBB
1312 // fallthrough --> copy0MBB
1313 MachineBasicBlock *thisMBB = BB;
1314 MachineFunction *F = BB->getParent();
1315 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1316 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
1317 F->insert(It, copy0MBB);
1318 F->insert(It, sinkMBB);
1319
1320 // Transfer the remainder of BB and its successor edges to sinkMBB.
1321 sinkMBB->splice(sinkMBB->begin(), BB,
1322 llvm::next(MachineBasicBlock::iterator(MI)),
1323 BB->end());
1324 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
1325
1326 // Next, add the true and fallthrough blocks as its successors.
1327 BB->addSuccessor(copy0MBB);
1328 BB->addSuccessor(sinkMBB);
1329
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001330 BuildMI(BB, DL, TII->get(Opc2)).addReg(MI->getOperand(3).getReg())
Reed Kotler7617d0322013-02-22 05:10:51 +00001331 .addImm(MI->getOperand(4).getImm());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001332 BuildMI(BB, DL, TII->get(Opc1)).addMBB(sinkMBB);
Reed Kotler7617d0322013-02-22 05:10:51 +00001333
1334 // copy0MBB:
1335 // %FalseValue = ...
1336 // # fallthrough to sinkMBB
1337 BB = copy0MBB;
1338
1339 // Update machine-CFG edges
1340 BB->addSuccessor(sinkMBB);
1341
1342 // sinkMBB:
1343 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
1344 // ...
1345 BB = sinkMBB;
1346
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001347 BuildMI(*BB, BB->begin(), DL,
Reed Kotler7617d0322013-02-22 05:10:51 +00001348 TII->get(Mips::PHI), MI->getOperand(0).getReg())
1349 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB)
1350 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB);
1351
1352 MI->eraseFromParent(); // The pseudo instruction is gone now.
1353 return BB;
1354
1355}
1356
Reed Kotler459d35c2013-02-24 06:16:39 +00001357
1358MachineBasicBlock
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001359 *MipsTargetLowering::emitFEXT_T8I816_ins(unsigned BtOpc, unsigned CmpOpc,
Reed Kotler459d35c2013-02-24 06:16:39 +00001360 MachineInstr *MI,
1361 MachineBasicBlock *BB) const {
Reed Kotlerde89ecd2013-02-25 02:25:47 +00001362 if (DontExpandCondPseudos16)
1363 return BB;
Reed Kotler459d35c2013-02-24 06:16:39 +00001364 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1365 unsigned regX = MI->getOperand(0).getReg();
1366 unsigned regY = MI->getOperand(1).getReg();
1367 MachineBasicBlock *target = MI->getOperand(2).getMBB();
1368 BuildMI(*BB, MI, MI->getDebugLoc(), TII->get(CmpOpc)).addReg(regX).addReg(regY);
1369 BuildMI(*BB, MI, MI->getDebugLoc(), TII->get(BtOpc)).addMBB(target);
1370 MI->eraseFromParent(); // The pseudo instruction is gone now.
1371 return BB;
1372}
Reed Kotler29cb2592013-02-24 23:17:51 +00001373
1374
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001375MachineBasicBlock *MipsTargetLowering::emitFEXT_T8I8I16_ins(
Reed Kotler29cb2592013-02-24 23:17:51 +00001376 unsigned BtOpc, unsigned CmpiOpc, unsigned CmpiXOpc,
1377 MachineInstr *MI, MachineBasicBlock *BB) const {
Reed Kotlerde89ecd2013-02-25 02:25:47 +00001378 if (DontExpandCondPseudos16)
1379 return BB;
Reed Kotler29cb2592013-02-24 23:17:51 +00001380 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1381 unsigned regX = MI->getOperand(0).getReg();
1382 int64_t imm = MI->getOperand(1).getImm();
1383 MachineBasicBlock *target = MI->getOperand(2).getMBB();
1384 unsigned CmpOpc;
1385 if (isUInt<8>(imm))
1386 CmpOpc = CmpiOpc;
1387 else if (isUInt<16>(imm))
1388 CmpOpc = CmpiXOpc;
1389 else
1390 llvm_unreachable("immediate field not usable");
1391 BuildMI(*BB, MI, MI->getDebugLoc(), TII->get(CmpOpc)).addReg(regX).addImm(imm);
1392 BuildMI(*BB, MI, MI->getDebugLoc(), TII->get(BtOpc)).addMBB(target);
1393 MI->eraseFromParent(); // The pseudo instruction is gone now.
1394 return BB;
1395}
1396
Reed Kotlerde89ecd2013-02-25 02:25:47 +00001397
1398static unsigned Mips16WhichOp8uOr16simm
1399 (unsigned shortOp, unsigned longOp, int64_t Imm) {
1400 if (isUInt<8>(Imm))
1401 return shortOp;
1402 else if (isInt<16>(Imm))
1403 return longOp;
1404 else
1405 llvm_unreachable("immediate field not usable");
1406}
1407
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001408MachineBasicBlock *MipsTargetLowering::emitFEXT_CCRX16_ins(
Reed Kotlerde89ecd2013-02-25 02:25:47 +00001409 unsigned SltOpc,
1410 MachineInstr *MI, MachineBasicBlock *BB) const {
1411 if (DontExpandCondPseudos16)
1412 return BB;
1413 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1414 unsigned CC = MI->getOperand(0).getReg();
1415 unsigned regX = MI->getOperand(1).getReg();
1416 unsigned regY = MI->getOperand(2).getReg();
1417 BuildMI(*BB, MI, MI->getDebugLoc(),
1418 TII->get(SltOpc)).addReg(regX).addReg(regY);
1419 BuildMI(*BB, MI, MI->getDebugLoc(),
1420 TII->get(Mips::MoveR3216), CC).addReg(Mips::T8);
1421 MI->eraseFromParent(); // The pseudo instruction is gone now.
1422 return BB;
1423}
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001424MachineBasicBlock *MipsTargetLowering::emitFEXT_CCRXI16_ins(
Reed Kotlerde89ecd2013-02-25 02:25:47 +00001425 unsigned SltiOpc, unsigned SltiXOpc,
1426 MachineInstr *MI, MachineBasicBlock *BB )const {
1427 if (DontExpandCondPseudos16)
1428 return BB;
1429 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1430 unsigned CC = MI->getOperand(0).getReg();
1431 unsigned regX = MI->getOperand(1).getReg();
1432 int64_t Imm = MI->getOperand(2).getImm();
1433 unsigned SltOpc = Mips16WhichOp8uOr16simm(SltiOpc, SltiXOpc, Imm);
1434 BuildMI(*BB, MI, MI->getDebugLoc(),
1435 TII->get(SltOpc)).addReg(regX).addImm(Imm);
1436 BuildMI(*BB, MI, MI->getDebugLoc(),
1437 TII->get(Mips::MoveR3216), CC).addReg(Mips::T8);
1438 MI->eraseFromParent(); // The pseudo instruction is gone now.
1439 return BB;
1440
1441}
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001442MachineBasicBlock *
1443MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00001444 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001445 switch (MI->getOpcode()) {
Reed Kotlerffbe4322013-02-21 04:22:38 +00001446 default:
1447 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001448 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +00001449 case Mips::ATOMIC_LOAD_ADD_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001450 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001451 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +00001452 case Mips::ATOMIC_LOAD_ADD_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001453 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001454 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +00001455 case Mips::ATOMIC_LOAD_ADD_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001456 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +00001457 case Mips::ATOMIC_LOAD_ADD_I64:
1458 case Mips::ATOMIC_LOAD_ADD_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001459 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001460
1461 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +00001462 case Mips::ATOMIC_LOAD_AND_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001463 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001464 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +00001465 case Mips::ATOMIC_LOAD_AND_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001466 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001467 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +00001468 case Mips::ATOMIC_LOAD_AND_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001469 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +00001470 case Mips::ATOMIC_LOAD_AND_I64:
1471 case Mips::ATOMIC_LOAD_AND_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001472 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001473
1474 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +00001475 case Mips::ATOMIC_LOAD_OR_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001476 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001477 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +00001478 case Mips::ATOMIC_LOAD_OR_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001479 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001480 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +00001481 case Mips::ATOMIC_LOAD_OR_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001482 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +00001483 case Mips::ATOMIC_LOAD_OR_I64:
1484 case Mips::ATOMIC_LOAD_OR_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001485 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001486
1487 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +00001488 case Mips::ATOMIC_LOAD_XOR_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001489 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001490 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +00001491 case Mips::ATOMIC_LOAD_XOR_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001492 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001493 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +00001494 case Mips::ATOMIC_LOAD_XOR_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001495 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +00001496 case Mips::ATOMIC_LOAD_XOR_I64:
1497 case Mips::ATOMIC_LOAD_XOR_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001498 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001499
1500 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +00001501 case Mips::ATOMIC_LOAD_NAND_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001502 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001503 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +00001504 case Mips::ATOMIC_LOAD_NAND_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001505 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001506 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +00001507 case Mips::ATOMIC_LOAD_NAND_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001508 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +00001509 case Mips::ATOMIC_LOAD_NAND_I64:
1510 case Mips::ATOMIC_LOAD_NAND_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001511 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001512
1513 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +00001514 case Mips::ATOMIC_LOAD_SUB_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001515 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001516 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +00001517 case Mips::ATOMIC_LOAD_SUB_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001518 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001519 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +00001520 case Mips::ATOMIC_LOAD_SUB_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001521 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +00001522 case Mips::ATOMIC_LOAD_SUB_I64:
1523 case Mips::ATOMIC_LOAD_SUB_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001524 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001525
1526 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +00001527 case Mips::ATOMIC_SWAP_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001528 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001529 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +00001530 case Mips::ATOMIC_SWAP_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001531 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001532 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +00001533 case Mips::ATOMIC_SWAP_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001534 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +00001535 case Mips::ATOMIC_SWAP_I64:
1536 case Mips::ATOMIC_SWAP_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001537 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001538
1539 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +00001540 case Mips::ATOMIC_CMP_SWAP_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001541 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001542 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +00001543 case Mips::ATOMIC_CMP_SWAP_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001544 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001545 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +00001546 case Mips::ATOMIC_CMP_SWAP_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001547 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +00001548 case Mips::ATOMIC_CMP_SWAP_I64:
1549 case Mips::ATOMIC_CMP_SWAP_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001550 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka01f70892012-09-27 02:15:57 +00001551 case Mips::BPOSGE32_PSEUDO:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001552 return emitBPOSGE32(MI, BB);
Reed Kotlerffbe4322013-02-21 04:22:38 +00001553 case Mips::SelBeqZ:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001554 return emitSel16(Mips::BeqzRxImm16, MI, BB);
Reed Kotlerffbe4322013-02-21 04:22:38 +00001555 case Mips::SelBneZ:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001556 return emitSel16(Mips::BnezRxImm16, MI, BB);
Reed Kotler7617d0322013-02-22 05:10:51 +00001557 case Mips::SelTBteqZCmpi:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001558 return emitSeliT16(Mips::BteqzX16, Mips::CmpiRxImmX16, MI, BB);
Reed Kotler7617d0322013-02-22 05:10:51 +00001559 case Mips::SelTBteqZSlti:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001560 return emitSeliT16(Mips::BteqzX16, Mips::SltiRxImmX16, MI, BB);
Reed Kotler7617d0322013-02-22 05:10:51 +00001561 case Mips::SelTBteqZSltiu:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001562 return emitSeliT16(Mips::BteqzX16, Mips::SltiuRxImmX16, MI, BB);
Reed Kotler7617d0322013-02-22 05:10:51 +00001563 case Mips::SelTBtneZCmpi:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001564 return emitSeliT16(Mips::BtnezX16, Mips::CmpiRxImmX16, MI, BB);
Reed Kotler7617d0322013-02-22 05:10:51 +00001565 case Mips::SelTBtneZSlti:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001566 return emitSeliT16(Mips::BtnezX16, Mips::SltiRxImmX16, MI, BB);
Reed Kotler7617d0322013-02-22 05:10:51 +00001567 case Mips::SelTBtneZSltiu:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001568 return emitSeliT16(Mips::BtnezX16, Mips::SltiuRxImmX16, MI, BB);
Reed Kotler50354a32013-02-23 03:09:56 +00001569 case Mips::SelTBteqZCmp:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001570 return emitSelT16(Mips::BteqzX16, Mips::CmpRxRy16, MI, BB);
Reed Kotler50354a32013-02-23 03:09:56 +00001571 case Mips::SelTBteqZSlt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001572 return emitSelT16(Mips::BteqzX16, Mips::SltRxRy16, MI, BB);
Reed Kotler50354a32013-02-23 03:09:56 +00001573 case Mips::SelTBteqZSltu:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001574 return emitSelT16(Mips::BteqzX16, Mips::SltuRxRy16, MI, BB);
Reed Kotler50354a32013-02-23 03:09:56 +00001575 case Mips::SelTBtneZCmp:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001576 return emitSelT16(Mips::BtnezX16, Mips::CmpRxRy16, MI, BB);
Reed Kotler50354a32013-02-23 03:09:56 +00001577 case Mips::SelTBtneZSlt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001578 return emitSelT16(Mips::BtnezX16, Mips::SltRxRy16, MI, BB);
Reed Kotler50354a32013-02-23 03:09:56 +00001579 case Mips::SelTBtneZSltu:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001580 return emitSelT16(Mips::BtnezX16, Mips::SltuRxRy16, MI, BB);
Reed Kotler459d35c2013-02-24 06:16:39 +00001581 case Mips::BteqzT8CmpX16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001582 return emitFEXT_T8I816_ins(Mips::BteqzX16, Mips::CmpRxRy16, MI, BB);
Reed Kotler459d35c2013-02-24 06:16:39 +00001583 case Mips::BteqzT8SltX16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001584 return emitFEXT_T8I816_ins(Mips::BteqzX16, Mips::SltRxRy16, MI, BB);
Reed Kotler459d35c2013-02-24 06:16:39 +00001585 case Mips::BteqzT8SltuX16:
1586 // TBD: figure out a way to get this or remove the instruction
1587 // altogether.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001588 return emitFEXT_T8I816_ins(Mips::BteqzX16, Mips::SltuRxRy16, MI, BB);
Reed Kotler459d35c2013-02-24 06:16:39 +00001589 case Mips::BtnezT8CmpX16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001590 return emitFEXT_T8I816_ins(Mips::BtnezX16, Mips::CmpRxRy16, MI, BB);
Reed Kotler459d35c2013-02-24 06:16:39 +00001591 case Mips::BtnezT8SltX16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001592 return emitFEXT_T8I816_ins(Mips::BtnezX16, Mips::SltRxRy16, MI, BB);
Reed Kotler459d35c2013-02-24 06:16:39 +00001593 case Mips::BtnezT8SltuX16:
1594 // TBD: figure out a way to get this or remove the instruction
1595 // altogether.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001596 return emitFEXT_T8I816_ins(Mips::BtnezX16, Mips::SltuRxRy16, MI, BB);
1597 case Mips::BteqzT8CmpiX16: return emitFEXT_T8I8I16_ins(
Reed Kotler29cb2592013-02-24 23:17:51 +00001598 Mips::BteqzX16, Mips::CmpiRxImm16, Mips::CmpiRxImmX16, MI, BB);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001599 case Mips::BteqzT8SltiX16: return emitFEXT_T8I8I16_ins(
Reed Kotler29cb2592013-02-24 23:17:51 +00001600 Mips::BteqzX16, Mips::SltiRxImm16, Mips::SltiRxImmX16, MI, BB);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001601 case Mips::BteqzT8SltiuX16: return emitFEXT_T8I8I16_ins(
Reed Kotler29cb2592013-02-24 23:17:51 +00001602 Mips::BteqzX16, Mips::SltiuRxImm16, Mips::SltiuRxImmX16, MI, BB);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001603 case Mips::BtnezT8CmpiX16: return emitFEXT_T8I8I16_ins(
Reed Kotler29cb2592013-02-24 23:17:51 +00001604 Mips::BtnezX16, Mips::CmpiRxImm16, Mips::CmpiRxImmX16, MI, BB);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001605 case Mips::BtnezT8SltiX16: return emitFEXT_T8I8I16_ins(
Reed Kotler29cb2592013-02-24 23:17:51 +00001606 Mips::BtnezX16, Mips::SltiRxImm16, Mips::SltiRxImmX16, MI, BB);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001607 case Mips::BtnezT8SltiuX16: return emitFEXT_T8I8I16_ins(
Reed Kotler29cb2592013-02-24 23:17:51 +00001608 Mips::BtnezX16, Mips::SltiuRxImm16, Mips::SltiuRxImmX16, MI, BB);
1609 break;
Reed Kotlerde89ecd2013-02-25 02:25:47 +00001610 case Mips::SltCCRxRy16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001611 return emitFEXT_CCRX16_ins(Mips::SltRxRy16, MI, BB);
Reed Kotlerde89ecd2013-02-25 02:25:47 +00001612 break;
1613 case Mips::SltiCCRxImmX16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001614 return emitFEXT_CCRXI16_ins
Reed Kotlerde89ecd2013-02-25 02:25:47 +00001615 (Mips::SltiRxImm16, Mips::SltiRxImmX16, MI, BB);
1616 case Mips::SltiuCCRxImmX16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001617 return emitFEXT_CCRXI16_ins
Reed Kotlerde89ecd2013-02-25 02:25:47 +00001618 (Mips::SltiuRxImm16, Mips::SltiuRxImmX16, MI, BB);
1619 case Mips::SltuCCRxRy16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001620 return emitFEXT_CCRX16_ins
Reed Kotlerde89ecd2013-02-25 02:25:47 +00001621 (Mips::SltuRxRy16, MI, BB);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001622 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001623}
1624
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001625// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1626// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
1627MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001628MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +00001629 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001630 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001631 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001632
1633 MachineFunction *MF = BB->getParent();
1634 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001635 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001636 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001637 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001638 unsigned LL, SC, AND, NOR, ZERO, BEQ;
1639
1640 if (Size == 4) {
1641 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1642 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1643 AND = Mips::AND;
1644 NOR = Mips::NOR;
1645 ZERO = Mips::ZERO;
1646 BEQ = Mips::BEQ;
1647 }
1648 else {
1649 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1650 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1651 AND = Mips::AND64;
1652 NOR = Mips::NOR64;
1653 ZERO = Mips::ZERO_64;
1654 BEQ = Mips::BEQ64;
1655 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001656
Akira Hatanaka4061da12011-07-19 20:11:17 +00001657 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001658 unsigned Ptr = MI->getOperand(1).getReg();
1659 unsigned Incr = MI->getOperand(2).getReg();
1660
Akira Hatanaka4061da12011-07-19 20:11:17 +00001661 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1662 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1663 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001664
1665 // insert new blocks after the current block
1666 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1667 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1668 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1669 MachineFunction::iterator It = BB;
1670 ++It;
1671 MF->insert(It, loopMBB);
1672 MF->insert(It, exitMBB);
1673
1674 // Transfer the remainder of BB and its successor edges to exitMBB.
1675 exitMBB->splice(exitMBB->begin(), BB,
1676 llvm::next(MachineBasicBlock::iterator(MI)),
1677 BB->end());
1678 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1679
1680 // thisMBB:
1681 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001682 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001683 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001684 loopMBB->addSuccessor(loopMBB);
1685 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001686
1687 // loopMBB:
1688 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001689 // <binop> storeval, oldval, incr
1690 // sc success, storeval, 0(ptr)
1691 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001692 BB = loopMBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001693 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001694 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001695 // and andres, oldval, incr
1696 // nor storeval, $0, andres
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001697 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1698 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001699 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001700 // <binop> storeval, oldval, incr
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001701 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001702 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001703 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001704 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001705 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1706 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001707
1708 MI->eraseFromParent(); // The instruction is gone now.
1709
Akira Hatanaka939ece12011-07-19 03:42:13 +00001710 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001711}
1712
1713MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001714MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001715 MachineBasicBlock *BB,
1716 unsigned Size, unsigned BinOpcode,
1717 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001718 assert((Size == 1 || Size == 2) &&
1719 "Unsupported size for EmitAtomicBinaryPartial.");
1720
1721 MachineFunction *MF = BB->getParent();
1722 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1723 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1724 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001725 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001726 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1727 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001728
1729 unsigned Dest = MI->getOperand(0).getReg();
1730 unsigned Ptr = MI->getOperand(1).getReg();
1731 unsigned Incr = MI->getOperand(2).getReg();
1732
Akira Hatanaka4061da12011-07-19 20:11:17 +00001733 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1734 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001735 unsigned Mask = RegInfo.createVirtualRegister(RC);
1736 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001737 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1738 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001739 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001740 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1741 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1742 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1743 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1744 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001745 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001746 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1747 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1748 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1749 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1750 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001751
1752 // insert new blocks after the current block
1753 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1754 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001755 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001756 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1757 MachineFunction::iterator It = BB;
1758 ++It;
1759 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001760 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001761 MF->insert(It, exitMBB);
1762
1763 // Transfer the remainder of BB and its successor edges to exitMBB.
1764 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001765 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001766 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1767
Akira Hatanaka81b44112011-07-19 17:09:53 +00001768 BB->addSuccessor(loopMBB);
1769 loopMBB->addSuccessor(loopMBB);
1770 loopMBB->addSuccessor(sinkMBB);
1771 sinkMBB->addSuccessor(exitMBB);
1772
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001773 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001774 // addiu masklsb2,$0,-4 # 0xfffffffc
1775 // and alignedaddr,ptr,masklsb2
1776 // andi ptrlsb2,ptr,3
1777 // sll shiftamt,ptrlsb2,3
1778 // ori maskupper,$0,255 # 0xff
1779 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001780 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001781 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001782
1783 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001784 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001785 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001786 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001787 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001788 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1789 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1790 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001791 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001792 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001793 .addReg(ShiftAmt).addReg(MaskUpper);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001794 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1795 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001796
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001797 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001798 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001799 // ll oldval,0(alignedaddr)
1800 // binop binopres,oldval,incr2
1801 // and newval,binopres,mask
1802 // and maskedoldval0,oldval,mask2
1803 // or storeval,maskedoldval0,newval
1804 // sc success,storeval,0(alignedaddr)
1805 // beq success,$0,loopMBB
1806
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001807 // atomic.swap
1808 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001809 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001810 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001811 // and maskedoldval0,oldval,mask2
1812 // or storeval,maskedoldval0,newval
1813 // sc success,storeval,0(alignedaddr)
1814 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001815
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001816 BB = loopMBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001817 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001818 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001819 // and andres, oldval, incr2
1820 // nor binopres, $0, andres
1821 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001822 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1823 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001824 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001825 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001826 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001827 // <binop> binopres, oldval, incr2
1828 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001829 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1830 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001831 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001832 // and newval, incr2, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001833 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001834 }
Jia Liubb481f82012-02-28 07:46:26 +00001835
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001836 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001837 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001838 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001839 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001840 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001841 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001842 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001843 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001844
Akira Hatanaka939ece12011-07-19 03:42:13 +00001845 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001846 // and maskedoldval1,oldval,mask
1847 // srl srlres,maskedoldval1,shiftamt
1848 // sll sllres,srlres,24
1849 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001850 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001851 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001852
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001853 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001854 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001855 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001856 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001857 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001858 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001859 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001860 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001861
1862 MI->eraseFromParent(); // The instruction is gone now.
1863
Akira Hatanaka939ece12011-07-19 03:42:13 +00001864 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001865}
1866
1867MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001868MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001869 MachineBasicBlock *BB,
1870 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001871 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001872
1873 MachineFunction *MF = BB->getParent();
1874 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001875 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001876 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001877 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001878 unsigned LL, SC, ZERO, BNE, BEQ;
1879
1880 if (Size == 4) {
1881 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1882 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1883 ZERO = Mips::ZERO;
1884 BNE = Mips::BNE;
1885 BEQ = Mips::BEQ;
1886 }
1887 else {
1888 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1889 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1890 ZERO = Mips::ZERO_64;
1891 BNE = Mips::BNE64;
1892 BEQ = Mips::BEQ64;
1893 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001894
1895 unsigned Dest = MI->getOperand(0).getReg();
1896 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001897 unsigned OldVal = MI->getOperand(2).getReg();
1898 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001899
Akira Hatanaka4061da12011-07-19 20:11:17 +00001900 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001901
1902 // insert new blocks after the current block
1903 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1904 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1905 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1906 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1907 MachineFunction::iterator It = BB;
1908 ++It;
1909 MF->insert(It, loop1MBB);
1910 MF->insert(It, loop2MBB);
1911 MF->insert(It, exitMBB);
1912
1913 // Transfer the remainder of BB and its successor edges to exitMBB.
1914 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001915 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001916 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1917
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001918 // thisMBB:
1919 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001920 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001921 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001922 loop1MBB->addSuccessor(exitMBB);
1923 loop1MBB->addSuccessor(loop2MBB);
1924 loop2MBB->addSuccessor(loop1MBB);
1925 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001926
1927 // loop1MBB:
1928 // ll dest, 0(ptr)
1929 // bne dest, oldval, exitMBB
1930 BB = loop1MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001931 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1932 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001933 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001934
1935 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001936 // sc success, newval, 0(ptr)
1937 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001938 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001939 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001940 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001941 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka59068062011-11-11 04:14:30 +00001942 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001943
1944 MI->eraseFromParent(); // The instruction is gone now.
1945
Akira Hatanaka939ece12011-07-19 03:42:13 +00001946 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001947}
1948
1949MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001950MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001951 MachineBasicBlock *BB,
1952 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001953 assert((Size == 1 || Size == 2) &&
1954 "Unsupported size for EmitAtomicCmpSwapPartial.");
1955
1956 MachineFunction *MF = BB->getParent();
1957 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1958 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1959 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001960 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001961 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1962 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001963
1964 unsigned Dest = MI->getOperand(0).getReg();
1965 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001966 unsigned CmpVal = MI->getOperand(2).getReg();
1967 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001968
Akira Hatanaka4061da12011-07-19 20:11:17 +00001969 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1970 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001971 unsigned Mask = RegInfo.createVirtualRegister(RC);
1972 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001973 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1974 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1975 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1976 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1977 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1978 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1979 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1980 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1981 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1982 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1983 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1984 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1985 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1986 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001987
1988 // insert new blocks after the current block
1989 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1990 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1991 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001992 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001993 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1994 MachineFunction::iterator It = BB;
1995 ++It;
1996 MF->insert(It, loop1MBB);
1997 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001998 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001999 MF->insert(It, exitMBB);
2000
2001 // Transfer the remainder of BB and its successor edges to exitMBB.
2002 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00002003 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00002004 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
2005
Akira Hatanaka81b44112011-07-19 17:09:53 +00002006 BB->addSuccessor(loop1MBB);
2007 loop1MBB->addSuccessor(sinkMBB);
2008 loop1MBB->addSuccessor(loop2MBB);
2009 loop2MBB->addSuccessor(loop1MBB);
2010 loop2MBB->addSuccessor(sinkMBB);
2011 sinkMBB->addSuccessor(exitMBB);
2012
Akira Hatanaka70564a92011-07-19 18:14:26 +00002013 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00002014 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00002015 // addiu masklsb2,$0,-4 # 0xfffffffc
2016 // and alignedaddr,ptr,masklsb2
2017 // andi ptrlsb2,ptr,3
2018 // sll shiftamt,ptrlsb2,3
2019 // ori maskupper,$0,255 # 0xff
2020 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00002021 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00002022 // andi maskedcmpval,cmpval,255
2023 // sll shiftedcmpval,maskedcmpval,shiftamt
2024 // andi maskednewval,newval,255
2025 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00002026 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002027 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00002028 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002029 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00002030 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002031 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
2032 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
2033 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00002034 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002035 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00002036 .addReg(ShiftAmt).addReg(MaskUpper);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002037 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
2038 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00002039 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002040 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00002041 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002042 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00002043 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002044 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00002045 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00002046
2047 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00002048 // ll oldval,0(alginedaddr)
2049 // and maskedoldval0,oldval,mask
2050 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00002051 BB = loop1MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002052 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
2053 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00002054 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002055 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00002056 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00002057
2058 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00002059 // and maskedoldval1,oldval,mask2
2060 // or storeval,maskedoldval1,shiftednewval
2061 // sc success,storeval,0(alignedaddr)
2062 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00002063 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002064 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00002065 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002066 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00002067 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002068 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00002069 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002070 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00002071 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00002072
Akira Hatanaka939ece12011-07-19 03:42:13 +00002073 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00002074 // srl srlres,maskedoldval0,shiftamt
2075 // sll sllres,srlres,24
2076 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00002077 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00002078 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00002079
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002080 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00002081 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002082 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00002083 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002084 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00002085 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00002086
2087 MI->eraseFromParent(); // The instruction is gone now.
2088
Akira Hatanaka939ece12011-07-19 03:42:13 +00002089 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00002090}
2091
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002092//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002093// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002094//===----------------------------------------------------------------------===//
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002095SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakab7656a92013-03-06 21:32:03 +00002096 SDValue Chain = Op.getOperand(0);
2097 SDValue Table = Op.getOperand(1);
2098 SDValue Index = Op.getOperand(2);
2099 DebugLoc DL = Op.getDebugLoc();
2100 EVT PTy = getPointerTy();
2101 unsigned EntrySize =
2102 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
2103
2104 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
2105 DAG.getConstant(EntrySize, PTy));
2106 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
2107
2108 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
2109 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
2110 MachinePointerInfo::getJumpTable(), MemVT, false, false,
2111 0);
2112 Chain = Addr.getValue(1);
2113
2114 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || IsN64) {
2115 // For PIC, the sequence is:
2116 // BRIND(load(Jumptable + index) + RelocBase)
2117 // RelocBase can be JumpTable, GOT or some sort of global base.
2118 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
2119 getPICJumpTableRelocBase(Table, DAG));
2120 }
2121
2122 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
2123}
2124
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00002125SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002126lowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00002127{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002128 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00002129 // the block to branch to if the condition is true.
2130 SDValue Chain = Op.getOperand(0);
2131 SDValue Dest = Op.getOperand(2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002132 DebugLoc DL = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00002133
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002134 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00002135
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002136 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00002137 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00002138 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002139
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00002140 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002141 Mips::CondCode CC =
2142 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002143 SDValue BrCode = DAG.getConstant(getFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00002144
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002145 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00002146 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00002147}
2148
2149SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002150lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00002151{
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002152 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00002153
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002154 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00002155 if (Cond.getOpcode() != MipsISD::FPCmp)
2156 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00002157
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002158 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00002159 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00002160}
2161
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00002162SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002163lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00002164{
2165 DebugLoc DL = Op.getDebugLoc();
2166 EVT Ty = Op.getOperand(0).getValueType();
2167 SDValue Cond = DAG.getNode(ISD::SETCC, DL, getSetCCResultType(Ty),
2168 Op.getOperand(0), Op.getOperand(1),
2169 Op.getOperand(4));
2170
2171 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
2172 Op.getOperand(3));
2173}
2174
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002175SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
2176 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanaka0a40c232012-03-09 23:46:03 +00002177
2178 assert(Cond.getOpcode() == MipsISD::FPCmp &&
2179 "Floating point operand expected.");
2180
2181 SDValue True = DAG.getConstant(1, MVT::i32);
2182 SDValue False = DAG.getConstant(0, MVT::i32);
2183
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002184 return createCMovFP(DAG, Cond, True, False, Op.getDebugLoc());
Akira Hatanaka0a40c232012-03-09 23:46:03 +00002185}
2186
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002187SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002188 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00002189 // FIXME there isn't actually debug info here
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002190 DebugLoc DL = Op.getDebugLoc();
Jia Liubb481f82012-02-28 07:46:26 +00002191 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00002192
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00002193 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002194 const MipsTargetObjectFile &TLOF =
2195 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002196
Chris Lattnere3736f82009-08-13 05:41:27 +00002197 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002198 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002199 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002200 MipsII::MO_GPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002201 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002202 DAG.getVTList(MVT::i32), &GA, 1);
Akira Hatanakae7338cd2012-08-22 03:18:13 +00002203 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002204 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00002205 }
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002206
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00002207 // %hi/%lo relocation
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002208 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00002209 }
2210
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002211 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
2212 return getAddrLocal(Op, DAG, HasMips64);
2213
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002214 if (LargeGOT)
2215 return getAddrGlobalLargeGOT(Op, DAG, MipsII::MO_GOT_HI16,
2216 MipsII::MO_GOT_LO16);
2217
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002218 return getAddrGlobal(Op, DAG,
2219 HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00002220}
2221
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002222SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00002223 SelectionDAG &DAG) const {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002224 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
2225 return getAddrNonPIC(Op, DAG);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00002226
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002227 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00002228}
2229
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00002230SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002231lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00002232{
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00002233 // If the relocation model is PIC, use the General Dynamic TLS Model or
2234 // Local Dynamic TLS model, otherwise use the Initial Exec or
2235 // Local Exec TLS Model.
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00002236
2237 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002238 DebugLoc DL = GA->getDebugLoc();
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00002239 const GlobalValue *GV = GA->getGlobal();
2240 EVT PtrVT = getPointerTy();
2241
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002242 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
2243
2244 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg70a07c72012-06-04 14:02:08 +00002245 // General Dynamic and Local Dynamic TLS Model.
2246 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
2247 : MipsII::MO_TLSGD;
2248
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002249 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
2250 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
2251 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00002252 unsigned PtrSize = PtrVT.getSizeInBits();
2253 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
2254
Benjamin Kramer5eccf672011-12-11 12:21:34 +00002255 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00002256
2257 ArgListTy Args;
2258 ArgListEntry Entry;
2259 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00002260 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00002261 Args.push_back(Entry);
Jia Liubb481f82012-02-28 07:46:26 +00002262
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002263 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002264 false, false, false, false, 0, CallingConv::C,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002265 /*IsTailCall=*/false, /*doesNotRet=*/false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002266 /*isReturnValueUsed=*/true,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002267 TlsGetAddr, Args, DAG, DL);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002268 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00002269
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00002270 SDValue Ret = CallResult.first;
2271
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002272 if (model != TLSModel::LocalDynamic)
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00002273 return Ret;
2274
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002275 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00002276 MipsII::MO_DTPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002277 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
2278 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00002279 MipsII::MO_DTPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002280 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
2281 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
2282 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00002283 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002284
2285 SDValue Offset;
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002286 if (model == TLSModel::InitialExec) {
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002287 // Initial Exec TLS Model
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002288 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002289 MipsII::MO_GOTTPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002290 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanaka648f00c2012-02-24 22:34:47 +00002291 TGA);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002292 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002293 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002294 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002295 } else {
2296 // Local Exec TLS Model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002297 assert(model == TLSModel::LocalExec);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002298 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002299 MipsII::MO_TPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002300 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002301 MipsII::MO_TPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002302 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
2303 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
2304 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002305 }
2306
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002307 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
2308 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00002309}
2310
2311SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002312lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00002313{
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002314 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
2315 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00002316
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002317 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00002318}
2319
Dan Gohman475871a2008-07-27 21:46:04 +00002320SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002321lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00002322{
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00002323 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002324 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002325 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002326 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00002327 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00002328 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002329 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
2330 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002331 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00002332
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002333 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
2334 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00002335
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002336 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00002337}
2338
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002339SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00002340 MachineFunction &MF = DAG.getMachineFunction();
2341 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
2342
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002343 DebugLoc DL = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00002344 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
2345 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00002346
2347 // vastart just stores the address of the VarArgsFrameIndex slot into the
2348 // memory location argument.
2349 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002350 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka82099682011-12-19 19:52:25 +00002351 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00002352}
Jia Liubb481f82012-02-28 07:46:26 +00002353
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002354static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00002355 EVT TyX = Op.getOperand(0).getValueType();
2356 EVT TyY = Op.getOperand(1).getValueType();
2357 SDValue Const1 = DAG.getConstant(1, MVT::i32);
2358 SDValue Const31 = DAG.getConstant(31, MVT::i32);
2359 DebugLoc DL = Op.getDebugLoc();
2360 SDValue Res;
2361
2362 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2363 // to i32.
2364 SDValue X = (TyX == MVT::f32) ?
2365 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
2366 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2367 Const1);
2368 SDValue Y = (TyY == MVT::f32) ?
2369 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
2370 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
2371 Const1);
2372
2373 if (HasR2) {
2374 // ext E, Y, 31, 1 ; extract bit31 of Y
2375 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
2376 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
2377 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
2378 } else {
2379 // sll SllX, X, 1
2380 // srl SrlX, SllX, 1
2381 // srl SrlY, Y, 31
2382 // sll SllY, SrlX, 31
2383 // or Or, SrlX, SllY
2384 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2385 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2386 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
2387 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
2388 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
2389 }
2390
2391 if (TyX == MVT::f32)
2392 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
2393
2394 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2395 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
2396 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00002397}
2398
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002399static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00002400 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
2401 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
2402 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
2403 SDValue Const1 = DAG.getConstant(1, MVT::i32);
2404 DebugLoc DL = Op.getDebugLoc();
Eric Christopher471e4222011-06-08 23:55:35 +00002405
Akira Hatanaka056c51e2012-04-11 22:13:04 +00002406 // Bitcast to integer nodes.
2407 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
2408 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00002409
Akira Hatanaka056c51e2012-04-11 22:13:04 +00002410 if (HasR2) {
2411 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
2412 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
2413 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
2414 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00002415
Akira Hatanaka056c51e2012-04-11 22:13:04 +00002416 if (WidthX > WidthY)
2417 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
2418 else if (WidthY > WidthX)
2419 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00002420
Akira Hatanaka056c51e2012-04-11 22:13:04 +00002421 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
2422 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
2423 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
2424 }
2425
2426 // (d)sll SllX, X, 1
2427 // (d)srl SrlX, SllX, 1
2428 // (d)srl SrlY, Y, width(Y)-1
2429 // (d)sll SllY, SrlX, width(Y)-1
2430 // or Or, SrlX, SllY
2431 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
2432 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
2433 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
2434 DAG.getConstant(WidthY - 1, MVT::i32));
2435
2436 if (WidthX > WidthY)
2437 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
2438 else if (WidthY > WidthX)
2439 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
2440
2441 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
2442 DAG.getConstant(WidthX - 1, MVT::i32));
2443 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
2444 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00002445}
2446
Akira Hatanaka82099682011-12-19 19:52:25 +00002447SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002448MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00002449 if (Subtarget->hasMips64())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002450 return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00002451
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002452 return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00002453}
2454
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002455static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00002456 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
2457 DebugLoc DL = Op.getDebugLoc();
2458
2459 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2460 // to i32.
2461 SDValue X = (Op.getValueType() == MVT::f32) ?
2462 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
2463 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2464 Const1);
2465
2466 // Clear MSB.
2467 if (HasR2)
2468 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
2469 DAG.getRegister(Mips::ZERO, MVT::i32),
2470 DAG.getConstant(31, MVT::i32), Const1, X);
2471 else {
2472 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2473 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2474 }
2475
2476 if (Op.getValueType() == MVT::f32)
2477 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
2478
2479 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2480 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
2481 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
2482}
2483
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002484static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00002485 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
2486 DebugLoc DL = Op.getDebugLoc();
2487
2488 // Bitcast to integer node.
2489 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
2490
2491 // Clear MSB.
2492 if (HasR2)
2493 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
2494 DAG.getRegister(Mips::ZERO_64, MVT::i64),
2495 DAG.getConstant(63, MVT::i32), Const1, X);
2496 else {
2497 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
2498 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
2499 }
2500
2501 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
2502}
2503
2504SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002505MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00002506 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002507 return lowerFABS64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00002508
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002509 return lowerFABS32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00002510}
2511
Akira Hatanaka2e591472011-06-02 00:24:44 +00002512SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002513lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00002514 // check the depth
2515 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00002516 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00002517
2518 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2519 MFI->setFrameAddressIsTaken(true);
2520 EVT VT = Op.getValueType();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002521 DebugLoc DL = Op.getDebugLoc();
2522 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Akira Hatanaka46ac4392011-11-11 04:11:56 +00002523 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00002524 return FrameAddr;
2525}
2526
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002527SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanakaba584fe2012-07-11 00:53:32 +00002528 SelectionDAG &DAG) const {
2529 // check the depth
2530 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
2531 "Return address can be determined only for current frame.");
2532
2533 MachineFunction &MF = DAG.getMachineFunction();
2534 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00002535 MVT VT = Op.getSimpleValueType();
Akira Hatanakaba584fe2012-07-11 00:53:32 +00002536 unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
2537 MFI->setReturnAddressIsTaken(true);
2538
2539 // Return RA, which contains the return address. Mark it an implicit live-in.
2540 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
2541 return DAG.getCopyFromReg(DAG.getEntryNode(), Op.getDebugLoc(), Reg, VT);
2542}
2543
Akira Hatanaka544cc212013-01-30 00:26:49 +00002544// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
2545// generated from __builtin_eh_return (offset, handler)
2546// The effect of this is to adjust the stack pointer by "offset"
2547// and then branch to "handler".
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002548SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanaka544cc212013-01-30 00:26:49 +00002549 const {
2550 MachineFunction &MF = DAG.getMachineFunction();
2551 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2552
2553 MipsFI->setCallsEhReturn();
2554 SDValue Chain = Op.getOperand(0);
2555 SDValue Offset = Op.getOperand(1);
2556 SDValue Handler = Op.getOperand(2);
2557 DebugLoc DL = Op.getDebugLoc();
2558 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2559
2560 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2561 // EH_RETURN nodes, so that instructions are emitted back-to-back.
2562 unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1;
2563 unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0;
2564 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2565 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2566 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2567 DAG.getRegister(OffsetReg, Ty),
2568 DAG.getRegister(AddrReg, getPointerTy()),
2569 Chain.getValue(1));
2570}
2571
Akira Hatanakadb548262011-07-19 23:30:50 +00002572// TODO: set SType according to the desired memory barrier behavior.
Akira Hatanaka82099682011-12-19 19:52:25 +00002573SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002574MipsTargetLowering::lowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakadb548262011-07-19 23:30:50 +00002575 unsigned SType = 0;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002576 DebugLoc DL = Op.getDebugLoc();
2577 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Akira Hatanakadb548262011-07-19 23:30:50 +00002578 DAG.getConstant(SType, MVT::i32));
2579}
2580
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002581SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00002582 SelectionDAG &DAG) const {
Eli Friedman14648462011-07-27 22:21:52 +00002583 // FIXME: Need pseudo-fence for 'singlethread' fences
2584 // FIXME: Set SType for weaker fences where supported/appropriate.
2585 unsigned SType = 0;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002586 DebugLoc DL = Op.getDebugLoc();
2587 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002588 DAG.getConstant(SType, MVT::i32));
2589}
2590
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002591SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00002592 SelectionDAG &DAG) const {
Akira Hatanakaa284acb2012-05-09 00:55:21 +00002593 DebugLoc DL = Op.getDebugLoc();
2594 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2595 SDValue Shamt = Op.getOperand(2);
2596
2597 // if shamt < 32:
2598 // lo = (shl lo, shamt)
2599 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2600 // else:
2601 // lo = 0
2602 // hi = (shl lo, shamt[4:0])
2603 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2604 DAG.getConstant(-1, MVT::i32));
2605 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
2606 DAG.getConstant(1, MVT::i32));
2607 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
2608 Not);
2609 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
2610 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2611 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
2612 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2613 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka864f6602012-06-14 21:10:56 +00002614 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2615 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00002616 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
2617
2618 SDValue Ops[2] = {Lo, Hi};
2619 return DAG.getMergeValues(Ops, 2, DL);
2620}
2621
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002622SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanakaa284acb2012-05-09 00:55:21 +00002623 bool IsSRA) const {
2624 DebugLoc DL = Op.getDebugLoc();
2625 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2626 SDValue Shamt = Op.getOperand(2);
2627
2628 // if shamt < 32:
2629 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2630 // if isSRA:
2631 // hi = (sra hi, shamt)
2632 // else:
2633 // hi = (srl hi, shamt)
2634 // else:
2635 // if isSRA:
2636 // lo = (sra hi, shamt[4:0])
2637 // hi = (sra hi, 31)
2638 // else:
2639 // lo = (srl hi, shamt[4:0])
2640 // hi = 0
2641 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2642 DAG.getConstant(-1, MVT::i32));
2643 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
2644 DAG.getConstant(1, MVT::i32));
2645 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
2646 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
2647 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2648 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
2649 Hi, Shamt);
2650 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2651 DAG.getConstant(0x20, MVT::i32));
2652 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
2653 DAG.getConstant(31, MVT::i32));
2654 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
2655 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2656 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
2657 ShiftRightHi);
2658
2659 SDValue Ops[2] = {Lo, Hi};
2660 return DAG.getMergeValues(Ops, 2, DL);
2661}
2662
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002663static SDValue CreateLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
2664 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002665 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002666 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002667 EVT BasePtrVT = Ptr.getValueType();
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002668 DebugLoc DL = LD->getDebugLoc();
2669 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2670
2671 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002672 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002673 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002674
2675 SDValue Ops[] = { Chain, Ptr, Src };
2676 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2677 LD->getMemOperand());
2678}
2679
2680// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002681SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002682 LoadSDNode *LD = cast<LoadSDNode>(Op);
2683 EVT MemVT = LD->getMemoryVT();
2684
2685 // Return if load is aligned or if MemVT is neither i32 nor i64.
2686 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2687 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2688 return SDValue();
2689
2690 bool IsLittle = Subtarget->isLittle();
2691 EVT VT = Op.getValueType();
2692 ISD::LoadExtType ExtType = LD->getExtensionType();
2693 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2694
2695 assert((VT == MVT::i32) || (VT == MVT::i64));
2696
2697 // Expand
2698 // (set dst, (i64 (load baseptr)))
2699 // to
2700 // (set tmp, (ldl (add baseptr, 7), undef))
2701 // (set dst, (ldr baseptr, tmp))
2702 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
2703 SDValue LDL = CreateLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
2704 IsLittle ? 7 : 0);
2705 return CreateLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
2706 IsLittle ? 0 : 7);
2707 }
2708
2709 SDValue LWL = CreateLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
2710 IsLittle ? 3 : 0);
2711 SDValue LWR = CreateLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
2712 IsLittle ? 0 : 3);
2713
2714 // Expand
2715 // (set dst, (i32 (load baseptr))) or
2716 // (set dst, (i64 (sextload baseptr))) or
2717 // (set dst, (i64 (extload baseptr)))
2718 // to
2719 // (set tmp, (lwl (add baseptr, 3), undef))
2720 // (set dst, (lwr baseptr, tmp))
2721 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2722 (ExtType == ISD::EXTLOAD))
2723 return LWR;
2724
2725 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2726
2727 // Expand
2728 // (set dst, (i64 (zextload baseptr)))
2729 // to
2730 // (set tmp0, (lwl (add baseptr, 3), undef))
2731 // (set tmp1, (lwr baseptr, tmp0))
2732 // (set tmp2, (shl tmp1, 32))
2733 // (set dst, (srl tmp2, 32))
2734 DebugLoc DL = LD->getDebugLoc();
2735 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2736 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka94ccee22012-06-04 17:46:29 +00002737 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2738 SDValue Ops[] = { SRL, LWR.getValue(1) };
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002739 return DAG.getMergeValues(Ops, 2, DL);
2740}
2741
2742static SDValue CreateStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
2743 SDValue Chain, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002744 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2745 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002746 DebugLoc DL = SD->getDebugLoc();
2747 SDVTList VTList = DAG.getVTList(MVT::Other);
2748
2749 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002750 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002751 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002752
2753 SDValue Ops[] = { Chain, Value, Ptr };
2754 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2755 SD->getMemOperand());
2756}
2757
2758// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002759SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002760 StoreSDNode *SD = cast<StoreSDNode>(Op);
2761 EVT MemVT = SD->getMemoryVT();
2762
2763 // Return if store is aligned or if MemVT is neither i32 nor i64.
2764 if ((SD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2765 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2766 return SDValue();
2767
2768 bool IsLittle = Subtarget->isLittle();
2769 SDValue Value = SD->getValue(), Chain = SD->getChain();
2770 EVT VT = Value.getValueType();
2771
2772 // Expand
2773 // (store val, baseptr) or
2774 // (truncstore val, baseptr)
2775 // to
2776 // (swl val, (add baseptr, 3))
2777 // (swr val, baseptr)
2778 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
2779 SDValue SWL = CreateStoreLR(MipsISD::SWL, DAG, SD, Chain,
2780 IsLittle ? 3 : 0);
2781 return CreateStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
2782 }
2783
2784 assert(VT == MVT::i64);
2785
2786 // Expand
2787 // (store val, baseptr)
2788 // to
2789 // (sdl val, (add baseptr, 7))
2790 // (sdr val, baseptr)
2791 SDValue SDL = CreateStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2792 return CreateStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
2793}
2794
Akira Hatanakafd89e6f2012-09-27 02:05:42 +00002795// This function expands mips intrinsic nodes which have 64-bit input operands
2796// or output values.
2797//
2798// out64 = intrinsic-node in64
2799// =>
2800// lo = copy (extract-element (in64, 0))
2801// hi = copy (extract-element (in64, 1))
2802// mips-specific-node
2803// v0 = copy lo
2804// v1 = copy hi
2805// out64 = merge-values (v0, v1)
2806//
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002807static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG,
Akira Hatanakafd89e6f2012-09-27 02:05:42 +00002808 unsigned Opc, bool HasI64In, bool HasI64Out) {
2809 DebugLoc DL = Op.getDebugLoc();
2810 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
2811 SDValue Chain = HasChainIn ? Op->getOperand(0) : DAG.getEntryNode();
2812 SmallVector<SDValue, 3> Ops;
2813
2814 if (HasI64In) {
2815 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
2816 Op->getOperand(1 + HasChainIn),
2817 DAG.getConstant(0, MVT::i32));
2818 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
2819 Op->getOperand(1 + HasChainIn),
2820 DAG.getConstant(1, MVT::i32));
2821
2822 Chain = DAG.getCopyToReg(Chain, DL, Mips::LO, InLo, SDValue());
2823 Chain = DAG.getCopyToReg(Chain, DL, Mips::HI, InHi, Chain.getValue(1));
2824
2825 Ops.push_back(Chain);
2826 Ops.append(Op->op_begin() + HasChainIn + 2, Op->op_end());
2827 Ops.push_back(Chain.getValue(1));
2828 } else {
2829 Ops.push_back(Chain);
2830 Ops.append(Op->op_begin() + HasChainIn + 1, Op->op_end());
2831 }
2832
2833 if (!HasI64Out)
2834 return DAG.getNode(Opc, DL, Op->value_begin(), Op->getNumValues(),
2835 Ops.begin(), Ops.size());
2836
2837 SDValue Intr = DAG.getNode(Opc, DL, DAG.getVTList(MVT::Other, MVT::Glue),
2838 Ops.begin(), Ops.size());
2839 SDValue OutLo = DAG.getCopyFromReg(Intr.getValue(0), DL, Mips::LO, MVT::i32,
2840 Intr.getValue(1));
2841 SDValue OutHi = DAG.getCopyFromReg(OutLo.getValue(1), DL, Mips::HI, MVT::i32,
2842 OutLo.getValue(2));
2843 SDValue Out = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, OutLo, OutHi);
2844
2845 if (!HasChainIn)
2846 return Out;
2847
2848 SDValue Vals[] = { Out, OutHi.getValue(1) };
2849 return DAG.getMergeValues(Vals, 2, DL);
2850}
2851
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002852SDValue MipsTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
Akira Hatanakafd89e6f2012-09-27 02:05:42 +00002853 SelectionDAG &DAG) const {
2854 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
2855 default:
2856 return SDValue();
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002857 case Intrinsic::mips_shilo:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002858 return lowerDSPIntr(Op, DAG, MipsISD::SHILO, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002859 case Intrinsic::mips_dpau_h_qbl:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002860 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002861 case Intrinsic::mips_dpau_h_qbr:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002862 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002863 case Intrinsic::mips_dpsu_h_qbl:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002864 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002865 case Intrinsic::mips_dpsu_h_qbr:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002866 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002867 case Intrinsic::mips_dpa_w_ph:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002868 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002869 case Intrinsic::mips_dps_w_ph:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002870 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002871 case Intrinsic::mips_dpax_w_ph:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002872 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002873 case Intrinsic::mips_dpsx_w_ph:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002874 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002875 case Intrinsic::mips_mulsa_w_ph:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002876 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002877 case Intrinsic::mips_mult:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002878 return lowerDSPIntr(Op, DAG, MipsISD::MULT, false, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002879 case Intrinsic::mips_multu:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002880 return lowerDSPIntr(Op, DAG, MipsISD::MULTU, false, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002881 case Intrinsic::mips_madd:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002882 return lowerDSPIntr(Op, DAG, MipsISD::MADD_DSP, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002883 case Intrinsic::mips_maddu:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002884 return lowerDSPIntr(Op, DAG, MipsISD::MADDU_DSP, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002885 case Intrinsic::mips_msub:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002886 return lowerDSPIntr(Op, DAG, MipsISD::MSUB_DSP, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002887 case Intrinsic::mips_msubu:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002888 return lowerDSPIntr(Op, DAG, MipsISD::MSUBU_DSP, true, true);
Akira Hatanakafd89e6f2012-09-27 02:05:42 +00002889 }
2890}
2891
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002892SDValue MipsTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
Akira Hatanakafd89e6f2012-09-27 02:05:42 +00002893 SelectionDAG &DAG) const {
2894 switch (cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue()) {
2895 default:
2896 return SDValue();
2897 case Intrinsic::mips_extp:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002898 return lowerDSPIntr(Op, DAG, MipsISD::EXTP, true, false);
Akira Hatanakafd89e6f2012-09-27 02:05:42 +00002899 case Intrinsic::mips_extpdp:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002900 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP, true, false);
Akira Hatanakafd89e6f2012-09-27 02:05:42 +00002901 case Intrinsic::mips_extr_w:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002902 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W, true, false);
Akira Hatanakafd89e6f2012-09-27 02:05:42 +00002903 case Intrinsic::mips_extr_r_w:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002904 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W, true, false);
Akira Hatanakafd89e6f2012-09-27 02:05:42 +00002905 case Intrinsic::mips_extr_rs_w:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002906 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W, true, false);
Akira Hatanakafd89e6f2012-09-27 02:05:42 +00002907 case Intrinsic::mips_extr_s_h:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002908 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H, true, false);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002909 case Intrinsic::mips_mthlip:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002910 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002911 case Intrinsic::mips_mulsaq_s_w_ph:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002912 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002913 case Intrinsic::mips_maq_s_w_phl:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002914 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002915 case Intrinsic::mips_maq_s_w_phr:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002916 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002917 case Intrinsic::mips_maq_sa_w_phl:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002918 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002919 case Intrinsic::mips_maq_sa_w_phr:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002920 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002921 case Intrinsic::mips_dpaq_s_w_ph:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002922 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002923 case Intrinsic::mips_dpsq_s_w_ph:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002924 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002925 case Intrinsic::mips_dpaq_sa_l_w:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002926 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002927 case Intrinsic::mips_dpsq_sa_l_w:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002928 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002929 case Intrinsic::mips_dpaqx_s_w_ph:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002930 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002931 case Intrinsic::mips_dpaqx_sa_w_ph:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002932 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002933 case Intrinsic::mips_dpsqx_s_w_ph:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002934 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH, true, true);
Akira Hatanaka2df483e2012-09-27 02:11:20 +00002935 case Intrinsic::mips_dpsqx_sa_w_ph:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002936 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH, true, true);
Akira Hatanakafd89e6f2012-09-27 02:05:42 +00002937 }
2938}
2939
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002940SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002941 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2942 || cast<ConstantSDNode>
2943 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2944 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2945 return SDValue();
2946
2947 // The pattern
2948 // (add (frameaddr 0), (frame_to_args_offset))
2949 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2950 // (add FrameObject, 0)
2951 // where FrameObject is a fixed StackObject with offset 0 which points to
2952 // the old stack pointer.
2953 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2954 EVT ValTy = Op->getValueType(0);
2955 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2956 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
2957 return DAG.getNode(ISD::ADD, Op->getDebugLoc(), ValTy, InArgsAddr,
2958 DAG.getConstant(0, ValTy));
2959}
2960
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002961//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002962// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002963//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002964
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002965//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002966// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002967// Mips O32 ABI rules:
2968// ---
2969// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002970// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002971// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002972// f64 - Only passed in two aliased f32 registers if no int reg has been used
2973// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002974// not used, it must be shadowed. If only A3 is avaiable, shadow it and
2975// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002976//
2977// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002978//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002979
Duncan Sands1e96bab2010-11-04 10:49:57 +00002980static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00002981 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002982 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2983
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002984 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002985
Craig Topperc5eaae42012-03-11 07:57:25 +00002986 static const uint16_t IntRegs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002987 Mips::A0, Mips::A1, Mips::A2, Mips::A3
2988 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002989 static const uint16_t F32Regs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002990 Mips::F12, Mips::F14
2991 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002992 static const uint16_t F64Regs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002993 Mips::D6, Mips::D7
2994 };
2995
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002996 // Do not process byval args here.
2997 if (ArgFlags.isByVal())
2998 return true;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002999
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00003000 // Promote i8 and i16
3001 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
3002 LocVT = MVT::i32;
3003 if (ArgFlags.isSExt())
3004 LocInfo = CCValAssign::SExt;
3005 else if (ArgFlags.isZExt())
3006 LocInfo = CCValAssign::ZExt;
3007 else
3008 LocInfo = CCValAssign::AExt;
3009 }
3010
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00003011 unsigned Reg;
3012
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00003013 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
3014 // is true: function is vararg, argument is 3rd or higher, there is previous
3015 // argument which is not f32 or f64.
3016 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
3017 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00003018 unsigned OrigAlign = ArgFlags.getOrigAlign();
3019 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00003020
3021 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00003022 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00003023 // If this is the first part of an i64 arg,
3024 // the allocated register must be either A0 or A2.
3025 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
3026 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00003027 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00003028 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
3029 // Allocate int register and shadow next int register. If first
3030 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00003031 Reg = State.AllocateReg(IntRegs, IntRegsSize);
3032 if (Reg == Mips::A1 || Reg == Mips::A3)
3033 Reg = State.AllocateReg(IntRegs, IntRegsSize);
3034 State.AllocateReg(IntRegs, IntRegsSize);
3035 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00003036 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
3037 // we are guaranteed to find an available float register
3038 if (ValVT == MVT::f32) {
3039 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
3040 // Shadow int register
3041 State.AllocateReg(IntRegs, IntRegsSize);
3042 } else {
3043 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
3044 // Shadow int registers
3045 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
3046 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
3047 State.AllocateReg(IntRegs, IntRegsSize);
3048 State.AllocateReg(IntRegs, IntRegsSize);
3049 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00003050 } else
3051 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00003052
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00003053 if (!Reg) {
3054 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
3055 OrigAlign);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00003056 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00003057 } else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00003058 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00003059
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00003060 return false;
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00003061}
3062
3063#include "MipsGenCallingConv.inc"
3064
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00003065//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00003066// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00003067//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003068
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00003069static const unsigned O32IntRegsSize = 4;
3070
Akira Hatanaka373e3a42011-09-23 00:58:33 +00003071// Return next O32 integer argument register.
3072static unsigned getNextIntArgReg(unsigned Reg) {
3073 assert((Reg == Mips::A0) || (Reg == Mips::A2));
3074 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
3075}
3076
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003077/// isEligibleForTailCallOptimization - Check whether the call is eligible
Akira Hatanaka2b861be2012-10-19 21:47:33 +00003078/// for tail call optimization.
3079bool MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003080isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
Akira Hatanaka2f34d752012-10-30 20:16:31 +00003081 unsigned NextStackOffset,
3082 const MipsFunctionInfo& FI) const {
Akira Hatanaka2b861be2012-10-19 21:47:33 +00003083 if (!EnableMipsTailCalls)
3084 return false;
3085
Akira Hatanakae7b406d2012-10-30 19:07:58 +00003086 // No tail call optimization for mips16.
3087 if (Subtarget->inMips16Mode())
3088 return false;
3089
Akira Hatanaka2f34d752012-10-30 20:16:31 +00003090 // Return false if either the callee or caller has a byval argument.
3091 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
Akira Hatanaka2b861be2012-10-19 21:47:33 +00003092 return false;
3093
Akira Hatanaka70852212012-11-07 19:04:26 +00003094 // Return true if the callee's argument area is no larger than the
Akira Hatanaka2f34d752012-10-30 20:16:31 +00003095 // caller's.
Akira Hatanaka70852212012-11-07 19:04:26 +00003096 return NextStackOffset <= FI.getIncomingArgSize();
Akira Hatanaka2b861be2012-10-19 21:47:33 +00003097}
3098
Akira Hatanaka7d712092012-10-30 19:23:25 +00003099SDValue
3100MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
3101 SDValue Chain, SDValue Arg, DebugLoc DL,
3102 bool IsTailCall, SelectionDAG &DAG) const {
3103 if (!IsTailCall) {
3104 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
3105 DAG.getIntPtrConstant(Offset));
3106 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
3107 false, 0);
3108 }
3109
3110 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3111 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
3112 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3113 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
3114 /*isVolatile=*/ true, false, 0);
3115}
3116
Reed Kotler8453b3f2013-01-24 04:24:02 +00003117//
3118// The Mips16 hard float is a crazy quilt inherited from gcc. I have a much
3119// cleaner way to do all of this but it will have to wait until the traditional
3120// gcc mechanism is completed.
3121//
3122// For Pic, in order for Mips16 code to call Mips32 code which according the abi
3123// have either arguments or returned values placed in floating point registers,
3124// we use a set of helper functions. (This includes functions which return type
3125// complex which on Mips are returned in a pair of floating point registers).
3126//
3127// This is an encoding that we inherited from gcc.
3128// In Mips traditional O32, N32 ABI, floating point numbers are passed in
3129// floating point argument registers 1,2 only when the first and optionally
3130// the second arguments are float (sf) or double (df).
3131// For Mips16 we are only concerned with the situations where floating point
3132// arguments are being passed in floating point registers by the ABI, because
3133// Mips16 mode code cannot execute floating point instructions to load those
3134// values and hence helper functions are needed.
3135// The possibilities are (), (sf), (sf, sf), (sf, df), (df), (df, sf), (df, df)
3136// the helper function suffixs for these are:
3137// 0, 1, 5, 9, 2, 6, 10
3138// this suffix can then be calculated as follows:
3139// for a given argument Arg:
3140// Arg1x, Arg2x = 1 : Arg is sf
3141// 2 : Arg is df
3142// 0: Arg is neither sf or df
3143// So this stub is the string for number Arg1x + Arg2x*4.
3144// However not all numbers between 0 and 10 are possible, we check anyway and
3145// assert if the impossible exists.
3146//
3147
3148unsigned int MipsTargetLowering::getMips16HelperFunctionStubNumber
3149 (ArgListTy &Args) const {
3150 unsigned int resultNum = 0;
3151 if (Args.size() >= 1) {
3152 Type *t = Args[0].Ty;
3153 if (t->isFloatTy()) {
3154 resultNum = 1;
3155 }
3156 else if (t->isDoubleTy()) {
3157 resultNum = 2;
3158 }
3159 }
3160 if (resultNum) {
3161 if (Args.size() >=2) {
3162 Type *t = Args[1].Ty;
3163 if (t->isFloatTy()) {
3164 resultNum += 4;
3165 }
3166 else if (t->isDoubleTy()) {
3167 resultNum += 8;
3168 }
3169 }
3170 }
3171 return resultNum;
3172}
3173
3174//
3175// prefixs are attached to stub numbers depending on the return type .
3176// return type: float sf_
3177// double df_
3178// single complex sc_
3179// double complext dc_
3180// others NO PREFIX
3181//
3182//
3183// The full name of a helper function is__mips16_call_stub +
3184// return type dependent prefix + stub number
3185//
3186//
3187// This is something that probably should be in a different source file and
3188// perhaps done differently but my main purpose is to not waste runtime
3189// on something that we can enumerate in the source. Another possibility is
3190// to have a python script to generate these mapping tables. This will do
3191// for now. There are a whole series of helper function mapping arrays, one
3192// for each return type class as outlined above. There there are 11 possible
3193// entries. Ones with 0 are ones which should never be selected
3194//
3195// All the arrays are similar except for ones which return neither
3196// sf, df, sc, dc, in which only care about ones which have sf or df as a
3197// first parameter.
3198//
3199#define P_ "__mips16_call_stub_"
3200#define MAX_STUB_NUMBER 10
3201#define T1 P "1", P "2", 0, 0, P "5", P "6", 0, 0, P "9", P "10"
3202#define T P "0" , T1
3203#define P P_
3204static char const * vMips16Helper[MAX_STUB_NUMBER+1] =
3205 {0, T1 };
3206#undef P
3207#define P P_ "sf_"
3208static char const * sfMips16Helper[MAX_STUB_NUMBER+1] =
3209 { T };
3210#undef P
3211#define P P_ "df_"
3212static char const * dfMips16Helper[MAX_STUB_NUMBER+1] =
3213 { T };
3214#undef P
3215#define P P_ "sc_"
3216static char const * scMips16Helper[MAX_STUB_NUMBER+1] =
3217 { T };
3218#undef P
3219#define P P_ "dc_"
3220static char const * dcMips16Helper[MAX_STUB_NUMBER+1] =
3221 { T };
3222#undef P
3223#undef P_
3224
3225
3226const char* MipsTargetLowering::
3227 getMips16HelperFunction
3228 (Type* RetTy, ArgListTy &Args, bool &needHelper) const {
Reed Kotler8453b3f2013-01-24 04:24:02 +00003229 const unsigned int stubNum = getMips16HelperFunctionStubNumber(Args);
NAKAMURA Takumi00cdf602013-01-24 05:54:23 +00003230#ifndef NDEBUG
3231 const unsigned int maxStubNum = 10;
Reed Kotler8453b3f2013-01-24 04:24:02 +00003232 assert(stubNum <= maxStubNum);
NAKAMURA Takumid5a336c2013-01-24 05:47:29 +00003233 const bool validStubNum[maxStubNum+1] =
3234 {true, true, true, false, false, true, true, false, false, true, true};
3235 assert(validStubNum[stubNum]);
3236#endif
Reed Kotler8453b3f2013-01-24 04:24:02 +00003237 const char *result;
3238 if (RetTy->isFloatTy()) {
3239 result = sfMips16Helper[stubNum];
3240 }
3241 else if (RetTy ->isDoubleTy()) {
3242 result = dfMips16Helper[stubNum];
3243 }
3244 else if (RetTy->isStructTy()) {
3245 // check if it's complex
3246 if (RetTy->getNumContainedTypes() == 2) {
3247 if ((RetTy->getContainedType(0)->isFloatTy()) &&
3248 (RetTy->getContainedType(1)->isFloatTy())) {
3249 result = scMips16Helper[stubNum];
3250 }
3251 else if ((RetTy->getContainedType(0)->isDoubleTy()) &&
3252 (RetTy->getContainedType(1)->isDoubleTy())) {
3253 result = dcMips16Helper[stubNum];
3254 }
NAKAMURA Takumib3105b92013-01-24 06:08:06 +00003255 else {
3256 llvm_unreachable("Uncovered condition");
3257 }
3258 }
3259 else {
3260 llvm_unreachable("Uncovered condition");
Reed Kotler8453b3f2013-01-24 04:24:02 +00003261 }
3262 }
3263 else {
3264 if (stubNum == 0) {
3265 needHelper = false;
3266 return "";
3267 }
3268 result = vMips16Helper[stubNum];
3269 }
3270 needHelper = true;
3271 return result;
3272}
3273
Dan Gohman98ca4f22009-08-05 01:29:28 +00003274/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00003275/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohman98ca4f22009-08-05 01:29:28 +00003276SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003277MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00003278 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003279 SelectionDAG &DAG = CLI.DAG;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003280 DebugLoc &DL = CLI.DL;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003281 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
3282 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
3283 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
Akira Hatanakae2d529a2012-07-31 18:46:41 +00003284 SDValue Chain = CLI.Chain;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003285 SDValue Callee = CLI.Callee;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003286 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003287 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003288 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003289
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003290 const char* Mips16HelperFunction = 0;
3291 bool NeedMips16Helper = false;
Reed Kotler8453b3f2013-01-24 04:24:02 +00003292
3293 if (Subtarget->inMips16Mode() && getTargetMachine().Options.UseSoftFloat &&
3294 Mips16HardFloat) {
3295 //
3296 // currently we don't have symbols tagged with the mips16 or mips32
3297 // qualifier so we will assume that we don't know what kind it is.
3298 // and generate the helper
3299 //
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003300 bool LookupHelper = true;
Reed Kotler8453b3f2013-01-24 04:24:02 +00003301 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003302 if (NoHelperNeeded.find(S->getSymbol()) != NoHelperNeeded.end()) {
3303 LookupHelper = false;
Reed Kotler8453b3f2013-01-24 04:24:02 +00003304 }
3305 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003306 if (LookupHelper) Mips16HelperFunction =
3307 getMips16HelperFunction(CLI.RetTy, CLI.Args, NeedMips16Helper);
Reed Kotler8453b3f2013-01-24 04:24:02 +00003308
3309 }
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00003310 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00003311 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00003312 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00003313 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003314
3315 // Analyze operands of the call, assigning locations to each operand.
3316 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003317 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00003318 getTargetMachine(), ArgLocs, *DAG.getContext());
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003319 MipsCC MipsCCInfo(CallConv, IsO32, CCInfo);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00003320
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003321 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003322 getTargetMachine().Options.UseSoftFloat,
3323 Callee.getNode(), CLI.Args);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003324
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003325 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00003326 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka480eeb52012-07-26 23:27:01 +00003327
Akira Hatanaka2b861be2012-10-19 21:47:33 +00003328 // Check if it's really possible to do a tail call.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003329 if (IsTailCall)
3330 IsTailCall =
3331 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka2f34d752012-10-30 20:16:31 +00003332 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00003333
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003334 if (IsTailCall)
Akira Hatanaka2b861be2012-10-19 21:47:33 +00003335 ++NumTailCalls;
3336
Akira Hatanakada7f5f12011-09-19 20:26:02 +00003337 // Chain is the output chain of the last Load/Store or CopyToReg node.
3338 // ByValChain is the output chain of the last Memcpy node created for copying
3339 // byval arguments to the stack.
Akira Hatanaka2f34d752012-10-30 20:16:31 +00003340 unsigned StackAlignment = TFL->getStackAlignment();
3341 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00003342 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka2b861be2012-10-19 21:47:33 +00003343
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003344 if (!IsTailCall)
Akira Hatanaka2b861be2012-10-19 21:47:33 +00003345 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal);
Akira Hatanakae2d529a2012-07-31 18:46:41 +00003346
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003347 SDValue StackPtr = DAG.getCopyFromReg(Chain, DL,
Akira Hatanakae2d529a2012-07-31 18:46:41 +00003348 IsN64 ? Mips::SP_64 : Mips::SP,
3349 getPointerTy());
Akira Hatanaka3d21c242011-06-08 17:39:33 +00003350
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003351 // With EABI is it possible to have 16 args on registers.
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00003352 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman475871a2008-07-27 21:46:04 +00003353 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00003354 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003355
3356 // Walk the register/memloc assignments, inserting copies/loads.
3357 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00003358 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003359 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00003360 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00003361 ISD::ArgFlagsTy Flags = Outs[i].Flags;
3362
3363 // ByVal Arg.
3364 if (Flags.isByVal()) {
3365 assert(Flags.getByValSize() &&
3366 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00003367 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003368 assert(!IsTailCall &&
Akira Hatanaka2f34d752012-10-30 20:16:31 +00003369 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003370 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00003371 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
3372 ++ByValArg;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00003373 continue;
3374 }
Jia Liubb481f82012-02-28 07:46:26 +00003375
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003376 // Promote the value if needed.
3377 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003378 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003379 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00003380 if (VA.isRegLoc()) {
3381 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003382 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
3383 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003384 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00003385 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003386 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00003387 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003388 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00003389 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00003390 if (!Subtarget->isLittle())
3391 std::swap(Lo, Hi);
Jia Liubb481f82012-02-28 07:46:26 +00003392 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka373e3a42011-09-23 00:58:33 +00003393 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
3394 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
3395 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00003396 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003397 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00003398 }
3399 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00003400 case CCValAssign::SExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003401 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00003402 break;
3403 case CCValAssign::ZExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003404 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00003405 break;
3406 case CCValAssign::AExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003407 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00003408 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003409 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003410
3411 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00003412 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003413 if (VA.isRegLoc()) {
3414 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00003415 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003416 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003417
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00003418 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00003419 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003420
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003421 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00003422 // parameter value to a stack Location
Akira Hatanaka2f34d752012-10-30 20:16:31 +00003423 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003424 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003425 }
3426
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003427 // Transform all store nodes into one single node because all store
3428 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003429 if (!MemOpChains.empty())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003430 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003431 &MemOpChains[0], MemOpChains.size());
3432
Bill Wendling056292f2008-09-16 21:48:12 +00003433 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003434 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
3435 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00003436 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakaed185da2012-12-13 03:17:29 +00003437 bool GlobalOrExternal = false, InternalLinkage = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00003438 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00003439
3440 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00003441 if (IsPICCall) {
Akira Hatanakaed185da2012-12-13 03:17:29 +00003442 InternalLinkage = G->getGlobal()->hasInternalLinkage();
3443
3444 if (InternalLinkage)
Akira Hatanakad43e06d2012-11-21 20:30:40 +00003445 Callee = getAddrLocal(Callee, DAG, HasMips64);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00003446 else if (LargeGOT)
3447 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
3448 MipsII::MO_CALL_LO16);
Akira Hatanakad43e06d2012-11-21 20:30:40 +00003449 else
3450 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
3451 } else
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003452 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00003453 MipsII::MO_NO_FLAG);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00003454 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00003455 }
3456 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaf09a0372012-11-21 20:40:38 +00003457 if (!IsN64 && !IsPIC) // !N64 && static
Akira Hatanakad43e06d2012-11-21 20:30:40 +00003458 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
3459 MipsII::MO_NO_FLAG);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00003460 else if (LargeGOT)
3461 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
3462 MipsII::MO_CALL_LO16);
Akira Hatanaka60689322013-02-22 21:10:03 +00003463 else // N64 || PIC
Akira Hatanakad43e06d2012-11-21 20:30:40 +00003464 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
3465
Akira Hatanaka0dca9452011-12-09 01:45:12 +00003466 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00003467 }
3468
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00003469 SDValue JumpTarget = Callee;
Akira Hatanakae11246c2012-07-26 02:24:43 +00003470
Jia Liubb481f82012-02-28 07:46:26 +00003471 // T9 should contain the address of the callee function if
Akira Hatanaka0dca9452011-12-09 01:45:12 +00003472 // -reloction-model=pic or it is an indirect call.
3473 if (IsPICCall || !GlobalOrExternal) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00003474 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
Reed Kotler8453b3f2013-01-24 04:24:02 +00003475 unsigned V0Reg = Mips::V0;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003476 if (NeedMips16Helper) {
Reed Kotler8453b3f2013-01-24 04:24:02 +00003477 RegsToPass.push_front(std::make_pair(V0Reg, Callee));
3478 JumpTarget = DAG.getExternalSymbol(
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003479 Mips16HelperFunction, getPointerTy());
Reed Kotler8453b3f2013-01-24 04:24:02 +00003480 JumpTarget = getAddrGlobal(JumpTarget, DAG, MipsII::MO_GOT);
3481 }
3482 else {
3483 RegsToPass.push_front(std::make_pair(T9Reg, Callee));
Akira Hatanakae11246c2012-07-26 02:24:43 +00003484
Reed Kotler8453b3f2013-01-24 04:24:02 +00003485 if (!Subtarget->inMips16Mode())
3486 JumpTarget = SDValue();
3487 }
Akira Hatanakaf49fde22011-04-04 17:11:07 +00003488 }
Bill Wendling056292f2008-09-16 21:48:12 +00003489
Akira Hatanaka92d4aec2012-05-12 03:19:04 +00003490 // Insert node "GP copy globalreg" before call to function.
Akira Hatanakaed185da2012-12-13 03:17:29 +00003491 //
3492 // R_MIPS_CALL* operators (emitted when non-internal functions are called
3493 // in PIC mode) allow symbols to be resolved via lazy binding.
3494 // The lazy binding stub requires GP to point to the GOT.
3495 if (IsPICCall && !InternalLinkage) {
Akira Hatanaka92d4aec2012-05-12 03:19:04 +00003496 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
3497 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003498 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(DAG, Ty)));
Akira Hatanaka92d4aec2012-05-12 03:19:04 +00003499 }
3500
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00003501 // Build a sequence of copy-to-reg nodes chained together with token
3502 // chain and flag operands which copy the outgoing args into registers.
3503 // The InFlag in necessary since all emitted instructions must be
3504 // stuck together.
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00003505 SDValue InFlag;
3506
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00003507 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003508 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[i].first,
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00003509 RegsToPass[i].second, InFlag);
3510 InFlag = Chain.getValue(1);
3511 }
3512
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003513 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003514 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003515 //
3516 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003517 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00003518 SmallVector<SDValue, 8> Ops(1, Chain);
3519
3520 if (JumpTarget.getNode())
3521 Ops.push_back(JumpTarget);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003522
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003523 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003524 // known live into the call.
3525 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3526 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3527 RegsToPass[i].second.getValueType()));
3528
Akira Hatanakab2930b92012-03-01 22:27:29 +00003529 // Add a register mask operand representing the call-preserved registers.
3530 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
3531 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
3532 assert(Mask && "Missing call preserved mask for calling convention");
3533 Ops.push_back(DAG.getRegisterMask(Mask));
3534
Gabor Greifba36cb52008-08-28 21:40:38 +00003535 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003536 Ops.push_back(InFlag);
3537
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003538 if (IsTailCall)
3539 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, &Ops[0], Ops.size());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00003540
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003541 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003542 InFlag = Chain.getValue(1);
3543
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00003544 // Create the CALLSEQ_END node.
Akira Hatanaka480eeb52012-07-26 23:27:01 +00003545 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00003546 DAG.getIntPtrConstant(0, true), InFlag);
3547 InFlag = Chain.getValue(1);
3548
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003549 // Handle result values, copying them out of physregs into vregs that we
3550 // return.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003551 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
3552 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003553}
3554
Dan Gohman98ca4f22009-08-05 01:29:28 +00003555/// LowerCallResult - Lower the result values of a call into the
3556/// appropriate copies out of appropriate physical registers.
3557SDValue
3558MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003559 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003560 const SmallVectorImpl<ISD::InputArg> &Ins,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003561 DebugLoc DL, SelectionDAG &DAG,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003562 SmallVectorImpl<SDValue> &InVals,
3563 const SDNode *CallNode,
3564 const Type *RetTy) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003565 // Assign locations to each value returned by this call.
3566 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003567 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka864f6602012-06-14 21:10:56 +00003568 getTargetMachine(), RVLocs, *DAG.getContext());
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003569 MipsCC MipsCCInfo(CallConv, IsO32, CCInfo);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00003570
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003571 MipsCCInfo.analyzeCallResult(Ins, getTargetMachine().Options.UseSoftFloat,
3572 CallNode, RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003573
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003574 // Copy all of the result registers out of their specified physreg.
3575 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003576 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003577 RVLocs[i].getLocVT(), InFlag);
3578 Chain = Val.getValue(1);
3579 InFlag = Val.getValue(2);
3580
3581 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003582 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003583
3584 InVals.push_back(Val);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003585 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00003586
Dan Gohman98ca4f22009-08-05 01:29:28 +00003587 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003588}
3589
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00003590//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00003591// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00003592//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003593/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00003594/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00003595SDValue
3596MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00003597 CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003598 bool IsVarArg,
Akira Hatanaka82099682011-12-19 19:52:25 +00003599 const SmallVectorImpl<ISD::InputArg> &Ins,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003600 DebugLoc DL, SelectionDAG &DAG,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00003601 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00003602 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00003603 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003604 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00003605 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00003606
Dan Gohman1e93df62010-04-17 14:41:14 +00003607 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003608
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00003609 // Used with vargs to acumulate store chains.
3610 std::vector<SDValue> OutChains;
3611
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003612 // Assign locations to all of the incoming arguments.
3613 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003614 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00003615 getTargetMachine(), ArgLocs, *DAG.getContext());
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003616 MipsCC MipsCCInfo(CallConv, IsO32, CCInfo);
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003617 Function::const_arg_iterator FuncArg =
3618 DAG.getMachineFunction().getFunction()->arg_begin();
3619 bool UseSoftFloat = getTargetMachine().Options.UseSoftFloat;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00003620
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003621 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
Akira Hatanakab33b34a2012-10-30 19:37:25 +00003622 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
3623 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00003624
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00003625 unsigned CurArgIdx = 0;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00003626 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003627
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00003628 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003629 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00003630 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
3631 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00003632 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00003633 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3634 bool IsRegLoc = VA.isRegLoc();
3635
3636 if (Flags.isByVal()) {
3637 assert(Flags.getByValSize() &&
3638 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00003639 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003640 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00003641 MipsCCInfo, *ByValArg);
3642 ++ByValArg;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00003643 continue;
3644 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003645
3646 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00003647 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00003648 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00003649 unsigned ArgReg = VA.getLocReg();
Craig Topper44d23822012-02-22 05:59:10 +00003650 const TargetRegisterClass *RC;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00003651
Owen Anderson825b72b2009-08-11 20:47:22 +00003652 if (RegVT == MVT::i32)
Reed Kotlerbacbf1c2012-12-20 06:06:35 +00003653 RC = Subtarget->inMips16Mode()? &Mips::CPU16RegsRegClass :
3654 &Mips::CPURegsRegClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00003655 else if (RegVT == MVT::i64)
Craig Topper420761a2012-04-20 07:30:17 +00003656 RC = &Mips::CPU64RegsRegClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003657 else if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00003658 RC = &Mips::FGR32RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00003659 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00003660 RC = HasMips64 ? &Mips::FGR64RegClass : &Mips::AFGR64RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00003661 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00003662 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003663
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003664 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003665 // physical registers into virtual ones
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003666 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
3667 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003668
3669 // If this is an 8 or 16-bit value, it has been passed promoted
3670 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003671 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00003672 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00003673 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00003674 if (VA.getLocInfo() == CCValAssign::SExt)
3675 Opcode = ISD::AssertSext;
3676 else if (VA.getLocInfo() == CCValAssign::ZExt)
3677 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00003678 if (Opcode)
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003679 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00003680 DAG.getValueType(ValVT));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003681 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00003682 }
3683
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003684 // Handle floating point arguments passed in integer registers and
3685 // long double arguments passed in floating point registers.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00003686 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003687 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
3688 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003689 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00003690 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003691 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00003692 getNextIntArgReg(ArgReg), RC);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003693 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00003694 if (!Subtarget->isLittle())
3695 std::swap(ArgValue, ArgValue2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003696 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00003697 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00003698 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003699
Dan Gohman98ca4f22009-08-05 01:29:28 +00003700 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003701 } else { // VA.isRegLoc()
3702
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003703 // sanity check
3704 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00003705
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003706 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00003707 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00003708 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003709
3710 // Create load nodes to retrieve arguments from the stack
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00003711 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003712 InVals.push_back(DAG.getLoad(ValVT, DL, Chain, FIN,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00003713 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003714 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003715 }
3716 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003717
3718 // The mips ABIs for returning structs by value requires that we copy
3719 // the sret argument into $v0 for the return. Save the argument into
3720 // a virtual register so that we can access it from the return points.
3721 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
3722 unsigned Reg = MipsFI->getSRetReturnReg();
3723 if (!Reg) {
Akira Hatanaka30580ce2012-10-19 22:11:40 +00003724 Reg = MF.getRegInfo().
3725 createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003726 MipsFI->setSRetReturnReg(Reg);
3727 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003728 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);
3729 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003730 }
3731
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003732 if (IsVarArg)
3733 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00003734
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003735 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00003736 // the size of Ins and InVals. This only happens when on varg functions
3737 if (!OutChains.empty()) {
3738 OutChains.push_back(Chain);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003739 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00003740 &OutChains[0], OutChains.size());
3741 }
3742
Dan Gohman98ca4f22009-08-05 01:29:28 +00003743 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003744}
3745
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00003746//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003747// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00003748//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003749
Akira Hatanaka97d9f082012-10-10 01:27:09 +00003750bool
3751MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003752 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka97d9f082012-10-10 01:27:09 +00003753 const SmallVectorImpl<ISD::OutputArg> &Outs,
3754 LLVMContext &Context) const {
3755 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003756 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
Akira Hatanaka97d9f082012-10-10 01:27:09 +00003757 RVLocs, Context);
3758 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3759}
3760
Dan Gohman98ca4f22009-08-05 01:29:28 +00003761SDValue
3762MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003763 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003764 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00003765 const SmallVectorImpl<SDValue> &OutVals,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003766 DebugLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003767 // CCValAssign - represent the assignment of
3768 // the return value to a location
3769 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003770 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003771
3772 // CCState - Info about the registers and stack slot.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003773 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003774 *DAG.getContext());
3775 MipsCC MipsCCInfo(CallConv, IsO32, CCInfo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003776
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003777 // Analyze return values.
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003778 MipsCCInfo.analyzeReturn(Outs, getTargetMachine().Options.UseSoftFloat,
3779 MF.getFunction()->getReturnType());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003780
Dan Gohman475871a2008-07-27 21:46:04 +00003781 SDValue Flag;
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00003782 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003783
3784 // Copy the result values into the output registers.
3785 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003786 SDValue Val = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003787 CCValAssign &VA = RVLocs[i];
3788 assert(VA.isRegLoc() && "Can only return in registers!");
3789
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003790 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003791 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003792
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003793 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003794
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00003795 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003796 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00003797 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003798 }
3799
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003800 // The mips ABIs for returning structs by value requires that we copy
3801 // the sret argument into $v0 for the return. We saved the argument into
3802 // a virtual register in the entry block, so now we copy the value out
3803 // and into $v0.
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003804 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003805 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3806 unsigned Reg = MipsFI->getSRetReturnReg();
3807
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003808 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00003809 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003810 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Akira Hatanaka2ef5bd32012-10-24 02:10:54 +00003811 unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003812
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003813 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003814 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00003815 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003816 }
3817
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00003818 RetOps[0] = Chain; // Update chain.
Akira Hatanaka182ef6f2012-07-10 00:19:06 +00003819
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00003820 // Add the flag if we have it.
3821 if (Flag.getNode())
3822 RetOps.push_back(Flag);
3823
3824 // Return on Mips is always a "jr $ra"
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003825 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, &RetOps[0], RetOps.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00003826}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003827
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00003828//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003829// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00003830//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003831
3832/// getConstraintType - Given a constraint letter, return the type of
3833/// constraint it is for this target.
3834MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003835getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003836{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003837 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003838 // GCC config/mips/constraints.md
3839 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003840 // 'd' : An address register. Equivalent to r
3841 // unless generating MIPS16 code.
3842 // 'y' : Equivalent to r; retained for
3843 // backwards compatibility.
Eric Christopher1d5a3922012-05-07 06:25:10 +00003844 // 'c' : A register suitable for use in an indirect
3845 // jump. This will always be $25 for -mabicalls.
Eric Christopheraf97f732012-05-07 06:25:19 +00003846 // 'l' : The lo register. 1 word storage.
3847 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003848 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003849 switch (Constraint[0]) {
3850 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003851 case 'd':
3852 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003853 case 'f':
Eric Christopher1d5a3922012-05-07 06:25:10 +00003854 case 'c':
Eric Christopher4adbefe2012-05-07 06:25:15 +00003855 case 'l':
Eric Christopheraf97f732012-05-07 06:25:19 +00003856 case 'x':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003857 return C_RegisterClass;
Jack Carter0b9675d2013-03-04 21:33:15 +00003858 case 'R':
3859 return C_Memory;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003860 }
3861 }
3862 return TargetLowering::getConstraintType(Constraint);
3863}
3864
John Thompson44ab89e2010-10-29 17:29:13 +00003865/// Examine constraint type and operand type and determine a weight value.
3866/// This object must already have been set up with the operand type
3867/// and the current alternative constraint selected.
3868TargetLowering::ConstraintWeight
3869MipsTargetLowering::getSingleConstraintMatchWeight(
3870 AsmOperandInfo &info, const char *constraint) const {
3871 ConstraintWeight weight = CW_Invalid;
3872 Value *CallOperandVal = info.CallOperandVal;
3873 // If we don't have a value, we can't do a match,
3874 // but allow it at the lowest weight.
3875 if (CallOperandVal == NULL)
3876 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003877 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00003878 // Look at the constraint type.
3879 switch (*constraint) {
3880 default:
3881 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3882 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003883 case 'd':
3884 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00003885 if (type->isIntegerTy())
3886 weight = CW_Register;
3887 break;
3888 case 'f':
3889 if (type->isFloatTy())
3890 weight = CW_Register;
3891 break;
Eric Christopher1d5a3922012-05-07 06:25:10 +00003892 case 'c': // $25 for indirect jumps
Eric Christopher4adbefe2012-05-07 06:25:15 +00003893 case 'l': // lo register
Eric Christopheraf97f732012-05-07 06:25:19 +00003894 case 'x': // hilo register pair
Eric Christopher1d5a3922012-05-07 06:25:10 +00003895 if (type->isIntegerTy())
3896 weight = CW_SpecificReg;
3897 break;
Eric Christopher50ab0392012-05-07 03:13:32 +00003898 case 'I': // signed 16 bit immediate
Eric Christophere5076d42012-05-07 03:13:42 +00003899 case 'J': // integer zero
Eric Christopherf49f8462012-05-07 05:46:29 +00003900 case 'K': // unsigned 16 bit immediate
Eric Christopher5ac47bb2012-05-07 05:46:37 +00003901 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christopher60cfc792012-05-07 05:46:43 +00003902 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher1ce20342012-05-07 05:46:48 +00003903 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopher54412a72012-05-07 06:25:02 +00003904 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher50ab0392012-05-07 03:13:32 +00003905 if (isa<ConstantInt>(CallOperandVal))
3906 weight = CW_Constant;
3907 break;
Jack Carter0b9675d2013-03-04 21:33:15 +00003908 case 'R':
3909 weight = CW_Memory;
3910 break;
John Thompson44ab89e2010-10-29 17:29:13 +00003911 }
3912 return weight;
3913}
3914
Eric Christopher38d64262011-06-29 19:33:04 +00003915/// Given a register class constraint, like 'r', if this corresponds directly
3916/// to an LLVM register class, return a register of 0 and the register class
3917/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003918std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00003919getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003920{
3921 if (Constraint.size() == 1) {
3922 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00003923 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3924 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003925 case 'r':
Akira Hatanakaafc945b2012-09-12 23:27:55 +00003926 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
3927 if (Subtarget->inMips16Mode())
3928 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Craig Topper420761a2012-04-20 07:30:17 +00003929 return std::make_pair(0U, &Mips::CPURegsRegClass);
Akira Hatanakaafc945b2012-09-12 23:27:55 +00003930 }
Jack Carter10de0252012-07-02 23:35:23 +00003931 if (VT == MVT::i64 && !HasMips64)
3932 return std::make_pair(0U, &Mips::CPURegsRegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00003933 if (VT == MVT::i64 && HasMips64)
3934 return std::make_pair(0U, &Mips::CPU64RegsRegClass);
3935 // This will generate an error message
3936 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003937 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00003938 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00003939 return std::make_pair(0U, &Mips::FGR32RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00003940 if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
3941 if (Subtarget->isFP64bit())
Craig Topper420761a2012-04-20 07:30:17 +00003942 return std::make_pair(0U, &Mips::FGR64RegClass);
3943 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00003944 }
Eric Christopher1d5a3922012-05-07 06:25:10 +00003945 break;
3946 case 'c': // register suitable for indirect jump
3947 if (VT == MVT::i32)
3948 return std::make_pair((unsigned)Mips::T9, &Mips::CPURegsRegClass);
3949 assert(VT == MVT::i64 && "Unexpected type.");
3950 return std::make_pair((unsigned)Mips::T9_64, &Mips::CPU64RegsRegClass);
Eric Christopher4adbefe2012-05-07 06:25:15 +00003951 case 'l': // register suitable for indirect jump
3952 if (VT == MVT::i32)
3953 return std::make_pair((unsigned)Mips::LO, &Mips::HILORegClass);
3954 return std::make_pair((unsigned)Mips::LO64, &Mips::HILO64RegClass);
Eric Christopheraf97f732012-05-07 06:25:19 +00003955 case 'x': // register suitable for indirect jump
3956 // Fixme: Not triggering the use of both hi and low
3957 // This will generate an error message
3958 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003959 }
3960 }
3961 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3962}
3963
Eric Christopher50ab0392012-05-07 03:13:32 +00003964/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3965/// vector. If it is invalid, don't add anything to Ops.
3966void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3967 std::string &Constraint,
3968 std::vector<SDValue>&Ops,
3969 SelectionDAG &DAG) const {
3970 SDValue Result(0, 0);
3971
3972 // Only support length 1 constraints for now.
3973 if (Constraint.length() > 1) return;
3974
3975 char ConstraintLetter = Constraint[0];
3976 switch (ConstraintLetter) {
3977 default: break; // This will fall through to the generic implementation
3978 case 'I': // Signed 16 bit constant
3979 // If this fails, the parent routine will give an error
3980 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3981 EVT Type = Op.getValueType();
3982 int64_t Val = C->getSExtValue();
3983 if (isInt<16>(Val)) {
3984 Result = DAG.getTargetConstant(Val, Type);
3985 break;
3986 }
3987 }
3988 return;
Eric Christophere5076d42012-05-07 03:13:42 +00003989 case 'J': // integer zero
3990 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3991 EVT Type = Op.getValueType();
3992 int64_t Val = C->getZExtValue();
3993 if (Val == 0) {
3994 Result = DAG.getTargetConstant(0, Type);
3995 break;
3996 }
3997 }
3998 return;
Eric Christopherf49f8462012-05-07 05:46:29 +00003999 case 'K': // unsigned 16 bit immediate
4000 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4001 EVT Type = Op.getValueType();
4002 uint64_t Val = (uint64_t)C->getZExtValue();
4003 if (isUInt<16>(Val)) {
4004 Result = DAG.getTargetConstant(Val, Type);
4005 break;
4006 }
4007 }
4008 return;
Eric Christopher5ac47bb2012-05-07 05:46:37 +00004009 case 'L': // signed 32 bit immediate where lower 16 bits are 0
4010 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4011 EVT Type = Op.getValueType();
4012 int64_t Val = C->getSExtValue();
4013 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
4014 Result = DAG.getTargetConstant(Val, Type);
4015 break;
4016 }
4017 }
4018 return;
Eric Christopher60cfc792012-05-07 05:46:43 +00004019 case 'N': // immediate in the range of -65535 to -1 (inclusive)
4020 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4021 EVT Type = Op.getValueType();
4022 int64_t Val = C->getSExtValue();
4023 if ((Val >= -65535) && (Val <= -1)) {
4024 Result = DAG.getTargetConstant(Val, Type);
4025 break;
4026 }
4027 }
4028 return;
Eric Christopher1ce20342012-05-07 05:46:48 +00004029 case 'O': // signed 15 bit immediate
4030 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4031 EVT Type = Op.getValueType();
4032 int64_t Val = C->getSExtValue();
4033 if ((isInt<15>(Val))) {
4034 Result = DAG.getTargetConstant(Val, Type);
4035 break;
4036 }
4037 }
4038 return;
Eric Christopher54412a72012-05-07 06:25:02 +00004039 case 'P': // immediate in the range of 1 to 65535 (inclusive)
4040 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4041 EVT Type = Op.getValueType();
4042 int64_t Val = C->getSExtValue();
4043 if ((Val <= 65535) && (Val >= 1)) {
4044 Result = DAG.getTargetConstant(Val, Type);
4045 break;
4046 }
4047 }
4048 return;
Eric Christopher50ab0392012-05-07 03:13:32 +00004049 }
4050
4051 if (Result.getNode()) {
4052 Ops.push_back(Result);
4053 return;
4054 }
4055
4056 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
4057}
4058
Dan Gohman6520e202008-10-18 02:06:02 +00004059bool
Akira Hatanaka94e47282012-11-17 00:25:41 +00004060MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM, Type *Ty) const {
4061 // No global is ever allowed as a base.
4062 if (AM.BaseGV)
4063 return false;
4064
4065 switch (AM.Scale) {
4066 case 0: // "r+i" or just "i", depending on HasBaseReg.
4067 break;
4068 case 1:
4069 if (!AM.HasBaseReg) // allow "r+i".
4070 break;
4071 return false; // disallow "r+r" or "r+r+i".
4072 default:
4073 return false;
4074 }
4075
4076 return true;
4077}
4078
4079bool
Dan Gohman6520e202008-10-18 02:06:02 +00004080MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4081 // The Mips target isn't yet aware of offsets.
4082 return false;
4083}
Evan Chengeb2f9692009-10-27 19:56:55 +00004084
Akira Hatanakae193b322012-06-13 19:33:32 +00004085EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00004086 unsigned SrcAlign,
4087 bool IsMemset, bool ZeroMemset,
Akira Hatanakae193b322012-06-13 19:33:32 +00004088 bool MemcpyStrSrc,
4089 MachineFunction &MF) const {
4090 if (Subtarget->hasMips64())
4091 return MVT::i64;
4092
4093 return MVT::i32;
4094}
4095
Evan Chenga1eaa3c2009-10-28 01:43:28 +00004096bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4097 if (VT != MVT::f32 && VT != MVT::f64)
4098 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00004099 if (Imm.isNegZero())
4100 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00004101 return Imm.isZero();
4102}
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00004103
4104unsigned MipsTargetLowering::getJumpTableEncoding() const {
4105 if (IsN64)
4106 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liubb481f82012-02-28 07:46:26 +00004107
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00004108 return TargetLowering::getJumpTableEncoding();
4109}
Akira Hatanaka7887c902012-10-26 23:56:38 +00004110
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00004111/// This function returns true if CallSym is a long double emulation routine.
4112static bool isF128SoftLibCall(const char *CallSym) {
4113 const char *const LibCalls[] =
4114 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
4115 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
4116 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
4117 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
4118 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
4119 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
4120 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
4121 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
4122 "truncl"};
4123
4124 const char * const *End = LibCalls + array_lengthof(LibCalls);
4125
4126 // Check that LibCalls is sorted alphabetically.
4127#ifndef NDEBUG
Akira Hatanakaf635ef42013-03-12 00:16:36 +00004128 LTStr Comp;
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00004129
4130 for (const char * const *I = LibCalls; I < End - 1; ++I)
4131 assert(Comp(*I, *(I + 1)));
4132#endif
4133
Akira Hatanakaf635ef42013-03-12 00:16:36 +00004134 return std::binary_search(LibCalls, End, CallSym, LTStr());
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00004135}
4136
4137/// This function returns true if Ty is fp128 or i128 which was originally a
4138/// fp128.
4139static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
4140 if (Ty->isFP128Ty())
4141 return true;
4142
4143 const ExternalSymbolSDNode *ES =
4144 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
4145
4146 // If the Ty is i128 and the function being called is a long double emulation
4147 // routine, then the original type is f128.
4148 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
4149}
4150
Akira Hatanakaffd28a42013-02-15 21:45:11 +00004151MipsTargetLowering::MipsCC::MipsCC(CallingConv::ID CC, bool IsO32_,
4152 CCState &Info)
4153 : CCInfo(Info), CallConv(CC), IsO32(IsO32_) {
Akira Hatanaka7887c902012-10-26 23:56:38 +00004154 // Pre-allocate reserved argument area.
Akira Hatanakaffd28a42013-02-15 21:45:11 +00004155 CCInfo.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka7887c902012-10-26 23:56:38 +00004156}
4157
4158void MipsTargetLowering::MipsCC::
Akira Hatanakaffd28a42013-02-15 21:45:11 +00004159analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00004160 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
4161 std::vector<ArgListEntry> &FuncArgs) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00004162 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
4163 "CallingConv::Fast shouldn't be used for vararg functions.");
4164
Akira Hatanaka7887c902012-10-26 23:56:38 +00004165 unsigned NumOpnds = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00004166 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
Akira Hatanaka7887c902012-10-26 23:56:38 +00004167
4168 for (unsigned I = 0; I != NumOpnds; ++I) {
4169 MVT ArgVT = Args[I].VT;
4170 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
4171 bool R;
4172
4173 if (ArgFlags.isByVal()) {
4174 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
4175 continue;
4176 }
4177
Akira Hatanakaffd28a42013-02-15 21:45:11 +00004178 if (IsVarArg && !Args[I].IsFixed)
Akira Hatanaka7887c902012-10-26 23:56:38 +00004179 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00004180 else {
4181 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
4182 IsSoftFloat);
4183 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
4184 }
Akira Hatanaka7887c902012-10-26 23:56:38 +00004185
4186 if (R) {
4187#ifndef NDEBUG
4188 dbgs() << "Call operand #" << I << " has unhandled type "
4189 << EVT(ArgVT).getEVTString();
4190#endif
4191 llvm_unreachable(0);
4192 }
4193 }
4194}
4195
4196void MipsTargetLowering::MipsCC::
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00004197analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
4198 bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
Akira Hatanaka7887c902012-10-26 23:56:38 +00004199 unsigned NumArgs = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00004200 llvm::CCAssignFn *FixedFn = fixedArgFn();
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00004201 unsigned CurArgIdx = 0;
Akira Hatanaka7887c902012-10-26 23:56:38 +00004202
4203 for (unsigned I = 0; I != NumArgs; ++I) {
4204 MVT ArgVT = Args[I].VT;
4205 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00004206 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
4207 CurArgIdx = Args[I].OrigArgIndex;
Akira Hatanaka7887c902012-10-26 23:56:38 +00004208
4209 if (ArgFlags.isByVal()) {
4210 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
4211 continue;
4212 }
4213
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00004214 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), 0, IsSoftFloat);
4215
4216 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
Akira Hatanaka7887c902012-10-26 23:56:38 +00004217 continue;
4218
4219#ifndef NDEBUG
4220 dbgs() << "Formal Arg #" << I << " has unhandled type "
4221 << EVT(ArgVT).getEVTString();
4222#endif
4223 llvm_unreachable(0);
4224 }
4225}
4226
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00004227template<typename Ty>
4228void MipsTargetLowering::MipsCC::
4229analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
4230 const SDNode *CallNode, const Type *RetTy) const {
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00004231 CCAssignFn *Fn;
4232
4233 if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
4234 Fn = RetCC_F128Soft;
4235 else
4236 Fn = RetCC_Mips;
4237
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00004238 for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
4239 MVT VT = RetVals[I].VT;
4240 ISD::ArgFlagsTy Flags = RetVals[I].Flags;
4241 MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
4242
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00004243 if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00004244#ifndef NDEBUG
4245 dbgs() << "Call result #" << I << " has unhandled type "
4246 << EVT(VT).getEVTString() << '\n';
4247#endif
4248 llvm_unreachable(0);
4249 }
4250 }
4251}
4252
4253void MipsTargetLowering::MipsCC::
4254analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
4255 const SDNode *CallNode, const Type *RetTy) const {
4256 analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
4257}
4258
4259void MipsTargetLowering::MipsCC::
4260analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
4261 const Type *RetTy) const {
4262 analyzeReturn(Outs, IsSoftFloat, 0, RetTy);
4263}
4264
Akira Hatanaka7887c902012-10-26 23:56:38 +00004265void
4266MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
4267 MVT LocVT,
4268 CCValAssign::LocInfo LocInfo,
4269 ISD::ArgFlagsTy ArgFlags) {
4270 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
4271
4272 struct ByValArgInfo ByVal;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00004273 unsigned RegSize = regSize();
Akira Hatanaka7887c902012-10-26 23:56:38 +00004274 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
4275 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
4276 RegSize * 2);
4277
Akira Hatanakaffd28a42013-02-15 21:45:11 +00004278 if (useRegsForByval())
Akira Hatanaka7887c902012-10-26 23:56:38 +00004279 allocateRegs(ByVal, ByValSize, Align);
4280
4281 // Allocate space on caller's stack.
4282 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
4283 Align);
4284 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
4285 LocInfo));
4286 ByValArgs.push_back(ByVal);
4287}
4288
Akira Hatanakaffd28a42013-02-15 21:45:11 +00004289unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
4290 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
4291}
4292
4293unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
4294 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
4295}
4296
4297const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const {
4298 return IsO32 ? O32IntRegs : Mips64IntRegs;
4299}
4300
4301llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
4302 if (CallConv == CallingConv::Fast)
4303 return CC_Mips_FastCC;
4304
4305 return IsO32 ? CC_MipsO32 : CC_MipsN;
4306}
4307
4308llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
4309 return IsO32 ? CC_MipsO32 : CC_MipsN_VarArg;
4310}
4311
4312const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const {
4313 return IsO32 ? O32IntRegs : Mips64DPRegs;
4314}
4315
Akira Hatanaka7887c902012-10-26 23:56:38 +00004316void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
4317 unsigned ByValSize,
4318 unsigned Align) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00004319 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
4320 const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
Akira Hatanaka7887c902012-10-26 23:56:38 +00004321 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
4322 "Byval argument's size and alignment should be a multiple of"
4323 "RegSize.");
4324
4325 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
4326
4327 // If Align > RegSize, the first arg register must be even.
4328 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
4329 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
4330 ++ByVal.FirstIdx;
4331 }
4332
4333 // Mark the registers allocated.
4334 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
4335 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
4336 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
4337}
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00004338
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00004339MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
4340 const SDNode *CallNode,
4341 bool IsSoftFloat) const {
4342 if (IsSoftFloat || IsO32)
4343 return VT;
4344
4345 // Check if the original type was fp128.
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00004346 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00004347 assert(VT == MVT::i64);
4348 return MVT::f64;
4349 }
4350
4351 return VT;
4352}
4353
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00004354void MipsTargetLowering::
4355copyByValRegs(SDValue Chain, DebugLoc DL, std::vector<SDValue> &OutChains,
4356 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
4357 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
4358 const MipsCC &CC, const ByValArgInfo &ByVal) const {
4359 MachineFunction &MF = DAG.getMachineFunction();
4360 MachineFrameInfo *MFI = MF.getFrameInfo();
4361 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
4362 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
4363 int FrameObjOffset;
4364
4365 if (RegAreaSize)
4366 FrameObjOffset = (int)CC.reservedArgArea() -
4367 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
4368 else
4369 FrameObjOffset = ByVal.Address;
4370
4371 // Create frame object.
4372 EVT PtrTy = getPointerTy();
4373 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
4374 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4375 InVals.push_back(FIN);
4376
4377 if (!ByVal.NumRegs)
4378 return;
4379
4380 // Copy arg registers.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00004381 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00004382 const TargetRegisterClass *RC = getRegClassFor(RegTy);
4383
4384 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
4385 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanakaf635ef42013-03-12 00:16:36 +00004386 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00004387 unsigned Offset = I * CC.regSize();
4388 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
4389 DAG.getConstant(Offset, PtrTy));
4390 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
4391 StorePtr, MachinePointerInfo(FuncArg, Offset),
4392 false, false, 0);
4393 OutChains.push_back(Store);
4394 }
4395}
Akira Hatanakadb40ede2012-10-27 00:16:36 +00004396
4397// Copy byVal arg to registers and stack.
4398void MipsTargetLowering::
4399passByValArg(SDValue Chain, DebugLoc DL,
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00004400 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Akira Hatanakadb40ede2012-10-27 00:16:36 +00004401 SmallVector<SDValue, 8> &MemOpChains, SDValue StackPtr,
4402 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
4403 const MipsCC &CC, const ByValArgInfo &ByVal,
4404 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
4405 unsigned ByValSize = Flags.getByValSize();
4406 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
4407 unsigned RegSize = CC.regSize();
4408 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
4409 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
4410
4411 if (ByVal.NumRegs) {
4412 const uint16_t *ArgRegs = CC.intArgRegs();
4413 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
4414 unsigned I = 0;
4415
4416 // Copy words to registers.
4417 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
4418 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
4419 DAG.getConstant(Offset, PtrTy));
4420 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
4421 MachinePointerInfo(), false, false, false,
4422 Alignment);
4423 MemOpChains.push_back(LoadVal.getValue(1));
4424 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
4425 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
4426 }
4427
4428 // Return if the struct has been fully copied.
4429 if (ByValSize == Offset)
4430 return;
4431
4432 // Copy the remainder of the byval argument with sub-word loads and shifts.
4433 if (LeftoverBytes) {
4434 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
4435 "Size of the remainder should be smaller than RegSize.");
4436 SDValue Val;
4437
4438 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
4439 Offset < ByValSize; LoadSize /= 2) {
4440 unsigned RemSize = ByValSize - Offset;
4441
4442 if (RemSize < LoadSize)
4443 continue;
4444
4445 // Load subword.
4446 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
4447 DAG.getConstant(Offset, PtrTy));
4448 SDValue LoadVal =
4449 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
4450 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
4451 false, false, Alignment);
4452 MemOpChains.push_back(LoadVal.getValue(1));
4453
4454 // Shift the loaded value.
4455 unsigned Shamt;
4456
4457 if (isLittle)
4458 Shamt = TotalSizeLoaded;
4459 else
4460 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
4461
4462 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
4463 DAG.getConstant(Shamt, MVT::i32));
4464
4465 if (Val.getNode())
4466 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
4467 else
4468 Val = Shift;
4469
4470 Offset += LoadSize;
4471 TotalSizeLoaded += LoadSize;
4472 Alignment = std::min(Alignment, LoadSize);
4473 }
4474
4475 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
4476 RegsToPass.push_back(std::make_pair(ArgReg, Val));
4477 return;
4478 }
4479 }
4480
4481 // Copy remainder of byval arg to it with memcpy.
4482 unsigned MemCpySize = ByValSize - Offset;
4483 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
4484 DAG.getConstant(Offset, PtrTy));
4485 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
4486 DAG.getIntPtrConstant(ByVal.Address));
4487 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
4488 DAG.getConstant(MemCpySize, PtrTy), Alignment,
4489 /*isVolatile=*/false, /*AlwaysInline=*/false,
4490 MachinePointerInfo(0), MachinePointerInfo(0));
4491 MemOpChains.push_back(Chain);
4492}
Akira Hatanakaf0848472012-10-27 00:21:13 +00004493
4494void
4495MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
4496 const MipsCC &CC, SDValue Chain,
4497 DebugLoc DL, SelectionDAG &DAG) const {
4498 unsigned NumRegs = CC.numIntArgRegs();
4499 const uint16_t *ArgRegs = CC.intArgRegs();
4500 const CCState &CCInfo = CC.getCCInfo();
4501 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
4502 unsigned RegSize = CC.regSize();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00004503 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
Akira Hatanakaf0848472012-10-27 00:21:13 +00004504 const TargetRegisterClass *RC = getRegClassFor(RegTy);
4505 MachineFunction &MF = DAG.getMachineFunction();
4506 MachineFrameInfo *MFI = MF.getFrameInfo();
4507 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
4508
4509 // Offset of the first variable argument from stack pointer.
4510 int VaArgOffset;
4511
4512 if (NumRegs == Idx)
4513 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
4514 else
4515 VaArgOffset =
4516 (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
4517
4518 // Record the frame index of the first variable argument
4519 // which is a value necessary to VASTART.
4520 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
4521 MipsFI->setVarArgsFrameIndex(FI);
4522
4523 // Copy the integer registers that have not been used for argument passing
4524 // to the argument register save area. For O32, the save area is allocated
4525 // in the caller's stack frame, while for N32/64, it is allocated in the
4526 // callee's stack frame.
4527 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00004528 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanakaf0848472012-10-27 00:21:13 +00004529 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
4530 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
4531 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
4532 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
4533 MachinePointerInfo(), false, false, 0);
4534 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
4535 OutChains.push_back(Store);
4536 }
4537}