blob: a62e84f9aaf243c72255d58afb9e2698c8880afd [file] [log] [blame]
Jia Liubb481f82012-02-28 07:46:26 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014#define DEBUG_TYPE "mips-lower"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000015#include "MipsISelLowering.h"
Craig Topper79aa3412012-03-17 18:46:09 +000016#include "InstPrinter/MipsInstPrinter.h"
17#include "MCTargetDesc/MipsBaseInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "MipsMachineFunction.h"
19#include "MipsSubtarget.h"
20#include "MipsTargetMachine.h"
21#include "MipsTargetObjectFile.h"
Akira Hatanaka2b861be2012-10-19 21:47:33 +000022#include "llvm/ADT/Statistic.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000023#include "llvm/CodeGen/CallingConvLower.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000028#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000029#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000030#include "llvm/IR/CallingConv.h"
31#include "llvm/IR/DerivedTypes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000032#include "llvm/IR/GlobalVariable.h"
Akira Hatanaka2b861be2012-10-19 21:47:33 +000033#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000034#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumi89593932012-04-21 15:31:45 +000036#include "llvm/Support/raw_ostream.h"
37
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000038using namespace llvm;
39
Akira Hatanaka2b861be2012-10-19 21:47:33 +000040STATISTIC(NumTailCalls, "Number of tail calls");
41
42static cl::opt<bool>
Akira Hatanaka81784cb2012-11-21 20:21:11 +000043LargeGOT("mxgot", cl::Hidden,
44 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
45
Akira Hatanakaf8941992013-05-20 18:07:43 +000046static cl::opt<bool>
Akira Hatanaka2591b5c2013-05-21 17:17:59 +000047NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
Akira Hatanakaf8941992013-05-20 18:07:43 +000048 cl::desc("MIPS: Don't trap on integer division by zero."),
49 cl::init(false));
50
Akira Hatanakafe30a9b2012-10-27 00:29:43 +000051static const uint16_t O32IntRegs[4] = {
52 Mips::A0, Mips::A1, Mips::A2, Mips::A3
53};
54
55static const uint16_t Mips64IntRegs[8] = {
56 Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
57 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64
58};
59
60static const uint16_t Mips64DPRegs[8] = {
61 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
62 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
63};
64
Jia Liubb481f82012-02-28 07:46:26 +000065// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanakadbe9a312011-08-18 20:07:42 +000066// mask (Pos), and return true.
Jia Liubb481f82012-02-28 07:46:26 +000067// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanakaf635ef42013-03-12 00:16:36 +000068static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanakad6bc5232011-12-05 21:26:34 +000069 if (!isShiftedMask_64(I))
Akira Hatanaka854a7db2011-08-19 22:59:00 +000070 return false;
Akira Hatanakabb15e112011-08-17 02:05:42 +000071
Akira Hatanakad6bc5232011-12-05 21:26:34 +000072 Size = CountPopulation_64(I);
Michael J. Spencerc6af2432013-05-24 22:23:49 +000073 Pos = countTrailingZeros(I);
Akira Hatanakadbe9a312011-08-18 20:07:42 +000074 return true;
Akira Hatanakabb15e112011-08-17 02:05:42 +000075}
76
Akira Hatanaka5ac065a2013-03-13 00:54:29 +000077SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
Akira Hatanaka648f00c2012-02-24 22:34:47 +000078 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
79 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
80}
81
Akira Hatanaka6b28b802012-11-21 20:26:38 +000082static SDValue getTargetNode(SDValue Op, SelectionDAG &DAG, unsigned Flag) {
83 EVT Ty = Op.getValueType();
84
85 if (GlobalAddressSDNode *N = dyn_cast<GlobalAddressSDNode>(Op))
Andrew Trickac6d9be2013-05-25 02:42:55 +000086 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(Op), Ty, 0,
Akira Hatanaka6b28b802012-11-21 20:26:38 +000087 Flag);
88 if (ExternalSymbolSDNode *N = dyn_cast<ExternalSymbolSDNode>(Op))
89 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
90 if (BlockAddressSDNode *N = dyn_cast<BlockAddressSDNode>(Op))
91 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
92 if (JumpTableSDNode *N = dyn_cast<JumpTableSDNode>(Op))
93 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
94 if (ConstantPoolSDNode *N = dyn_cast<ConstantPoolSDNode>(Op))
95 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
96 N->getOffset(), Flag);
97
98 llvm_unreachable("Unexpected node type.");
99 return SDValue();
100}
101
102static SDValue getAddrNonPIC(SDValue Op, SelectionDAG &DAG) {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000103 SDLoc DL(Op);
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000104 EVT Ty = Op.getValueType();
105 SDValue Hi = getTargetNode(Op, DAG, MipsII::MO_ABS_HI);
106 SDValue Lo = getTargetNode(Op, DAG, MipsII::MO_ABS_LO);
107 return DAG.getNode(ISD::ADD, DL, Ty,
108 DAG.getNode(MipsISD::Hi, DL, Ty, Hi),
109 DAG.getNode(MipsISD::Lo, DL, Ty, Lo));
110}
111
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000112SDValue MipsTargetLowering::getAddrLocal(SDValue Op, SelectionDAG &DAG,
113 bool HasMips64) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000114 SDLoc DL(Op);
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000115 EVT Ty = Op.getValueType();
116 unsigned GOTFlag = HasMips64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000117 SDValue GOT = DAG.getNode(MipsISD::Wrapper, DL, Ty, getGlobalReg(DAG, Ty),
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000118 getTargetNode(Op, DAG, GOTFlag));
119 SDValue Load = DAG.getLoad(Ty, DL, DAG.getEntryNode(), GOT,
120 MachinePointerInfo::getGOT(), false, false, false,
121 0);
122 unsigned LoFlag = HasMips64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
123 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, Ty, getTargetNode(Op, DAG, LoFlag));
124 return DAG.getNode(ISD::ADD, DL, Ty, Load, Lo);
125}
126
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000127SDValue MipsTargetLowering::getAddrGlobal(SDValue Op, SelectionDAG &DAG,
128 unsigned Flag) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000129 SDLoc DL(Op);
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000130 EVT Ty = Op.getValueType();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000131 SDValue Tgt = DAG.getNode(MipsISD::Wrapper, DL, Ty, getGlobalReg(DAG, Ty),
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000132 getTargetNode(Op, DAG, Flag));
133 return DAG.getLoad(Ty, DL, DAG.getEntryNode(), Tgt,
134 MachinePointerInfo::getGOT(), false, false, false, 0);
135}
136
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000137SDValue MipsTargetLowering::getAddrGlobalLargeGOT(SDValue Op, SelectionDAG &DAG,
138 unsigned HiFlag,
139 unsigned LoFlag) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000140 SDLoc DL(Op);
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000141 EVT Ty = Op.getValueType();
142 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, Ty, getTargetNode(Op, DAG, HiFlag));
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000143 Hi = DAG.getNode(ISD::ADD, DL, Ty, Hi, getGlobalReg(DAG, Ty));
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000144 SDValue Wrapper = DAG.getNode(MipsISD::Wrapper, DL, Ty, Hi,
145 getTargetNode(Op, DAG, LoFlag));
146 return DAG.getLoad(Ty, DL, DAG.getEntryNode(), Wrapper,
147 MachinePointerInfo::getGOT(), false, false, false, 0);
148}
149
Chris Lattnerf0144122009-07-28 03:13:23 +0000150const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
151 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000152 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka58d1e3f2012-10-19 20:59:39 +0000153 case MipsISD::TailCall: return "MipsISD::TailCall";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000154 case MipsISD::Hi: return "MipsISD::Hi";
155 case MipsISD::Lo: return "MipsISD::Lo";
156 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000157 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000158 case MipsISD::Ret: return "MipsISD::Ret";
Akira Hatanaka544cc212013-01-30 00:26:49 +0000159 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000160 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
161 case MipsISD::FPCmp: return "MipsISD::FPCmp";
162 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
163 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000164 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakadd958922013-03-30 01:14:04 +0000165 case MipsISD::ExtractLOHI: return "MipsISD::ExtractLOHI";
166 case MipsISD::InsertLOHI: return "MipsISD::InsertLOHI";
167 case MipsISD::Mult: return "MipsISD::Mult";
168 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000169 case MipsISD::MAdd: return "MipsISD::MAdd";
170 case MipsISD::MAddu: return "MipsISD::MAddu";
171 case MipsISD::MSub: return "MipsISD::MSub";
172 case MipsISD::MSubu: return "MipsISD::MSubu";
173 case MipsISD::DivRem: return "MipsISD::DivRem";
174 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanakadd958922013-03-30 01:14:04 +0000175 case MipsISD::DivRem16: return "MipsISD::DivRem16";
176 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000177 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
178 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakabfcb83f2011-12-12 22:38:19 +0000179 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanakadb548262011-07-19 23:30:50 +0000180 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanakabb15e112011-08-17 02:05:42 +0000181 case MipsISD::Ext: return "MipsISD::Ext";
182 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab6f1dc22012-06-02 00:03:12 +0000183 case MipsISD::LWL: return "MipsISD::LWL";
184 case MipsISD::LWR: return "MipsISD::LWR";
185 case MipsISD::SWL: return "MipsISD::SWL";
186 case MipsISD::SWR: return "MipsISD::SWR";
187 case MipsISD::LDL: return "MipsISD::LDL";
188 case MipsISD::LDR: return "MipsISD::LDR";
189 case MipsISD::SDL: return "MipsISD::SDL";
190 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka6fad5e72012-09-21 23:52:47 +0000191 case MipsISD::EXTP: return "MipsISD::EXTP";
192 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
193 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
194 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
195 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
196 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
197 case MipsISD::SHILO: return "MipsISD::SHILO";
198 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
199 case MipsISD::MULT: return "MipsISD::MULT";
200 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liub3ea8802013-03-04 01:06:54 +0000201 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka6fad5e72012-09-21 23:52:47 +0000202 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
203 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
204 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka97a62bf2013-04-19 23:21:32 +0000205 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
206 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
207 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000208 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
209 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Akira Hatanaka0f843822011-06-07 18:58:42 +0000210 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000211 }
212}
213
214MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +0000215MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka8b4198d2011-09-26 21:47:02 +0000216 : TargetLowering(TM, new MipsTargetObjectFile()),
217 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +0000218 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
219 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000220 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000221 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +0000222 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000223 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000224
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000225 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
227 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
228 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000229
Eli Friedman6055a6a2009-07-17 04:07:24 +0000230 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
232 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000233
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000234 // Used by legalize types to correctly generate the setcc result.
235 // Without this, every float setcc comes with a AND/OR with the result,
236 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000237 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000239
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000240 // Mips Custom Operations
Akira Hatanakab7656a92013-03-06 21:32:03 +0000241 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000243 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
245 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
246 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
247 setOperationAction(ISD::SELECT, MVT::f32, Custom);
248 setOperationAction(ISD::SELECT, MVT::f64, Custom);
249 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +0000250 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
251 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanaka0a40c232012-03-09 23:46:03 +0000252 setOperationAction(ISD::SETCC, MVT::f32, Custom);
253 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000254 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000255 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000256 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
257 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000258 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000259
Akira Hatanakac12a6e62012-04-11 22:49:04 +0000260 if (!TM.Options.NoNaNsFPMath) {
261 setOperationAction(ISD::FABS, MVT::f32, Custom);
262 setOperationAction(ISD::FABS, MVT::f64, Custom);
263 }
264
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000265 if (HasMips64) {
266 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
267 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
268 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
269 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
270 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
271 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka7664f052012-06-02 00:04:42 +0000272 setOperationAction(ISD::LOAD, MVT::i64, Custom);
273 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000274 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000275 }
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000276
Akira Hatanakaa284acb2012-05-09 00:55:21 +0000277 if (!HasMips64) {
278 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
279 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
280 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
281 }
282
Akira Hatanakae90a3bc2012-11-07 19:10:58 +0000283 setOperationAction(ISD::ADD, MVT::i32, Custom);
284 if (HasMips64)
285 setOperationAction(ISD::ADD, MVT::i64, Custom);
286
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000287 setOperationAction(ISD::SDIV, MVT::i32, Expand);
288 setOperationAction(ISD::SREM, MVT::i32, Expand);
289 setOperationAction(ISD::UDIV, MVT::i32, Expand);
290 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000291 setOperationAction(ISD::SDIV, MVT::i64, Expand);
292 setOperationAction(ISD::SREM, MVT::i64, Expand);
293 setOperationAction(ISD::UDIV, MVT::i64, Expand);
294 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000295
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000296 // Operations not directly supported by Mips.
Tom Stellard3ef53832013-03-08 15:36:57 +0000297 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
298 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
299 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
300 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
302 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000303 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000305 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
307 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000308 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000310 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000311 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
312 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
313 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
314 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000316 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka1d165f12012-07-31 20:54:48 +0000317 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
318 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000319
Akira Hatanaka56633442011-09-20 23:53:09 +0000320 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000321 setOperationAction(ISD::ROTR, MVT::i32, Expand);
322
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000323 if (!Subtarget->hasMips64r2())
324 setOperationAction(ISD::ROTR, MVT::i64, Expand);
325
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000327 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000329 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000330 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
331 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
333 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000334 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000335 setOperationAction(ISD::FLOG, MVT::f32, Expand);
336 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
337 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
338 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000339 setOperationAction(ISD::FMA, MVT::f32, Expand);
340 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka21ecc2f2012-03-29 18:43:11 +0000341 setOperationAction(ISD::FREM, MVT::f32, Expand);
342 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000343
Akira Hatanaka1cc63332012-04-11 22:59:08 +0000344 if (!TM.Options.NoNaNsFPMath) {
345 setOperationAction(ISD::FNEG, MVT::f32, Expand);
346 setOperationAction(ISD::FNEG, MVT::f64, Expand);
347 }
348
Akira Hatanaka544cc212013-01-30 00:26:49 +0000349 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
350
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000351 setOperationAction(ISD::VAARG, MVT::Other, Expand);
352 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
353 setOperationAction(ISD::VAEND, MVT::Other, Expand);
354
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000355 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000356 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
357 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000358
Jia Liubb481f82012-02-28 07:46:26 +0000359 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
360 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
361 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
362 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman4db5aca2011-08-29 18:23:02 +0000363
Eli Friedman26689ac2011-08-03 21:06:02 +0000364 setInsertFencesForAtomic(true);
365
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000366 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
368 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000369 }
370
Akira Hatanakac79507a2011-12-21 00:20:27 +0000371 if (!Subtarget->hasBitCount()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Akira Hatanakac79507a2011-12-21 00:20:27 +0000373 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
374 }
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000375
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000376 if (!Subtarget->hasSwap()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000377 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000378 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
379 }
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000380
Akira Hatanaka7664f052012-06-02 00:04:42 +0000381 if (HasMips64) {
382 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
383 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
384 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
385 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
386 }
387
Akira Hatanaka97585622013-07-26 20:58:55 +0000388 setOperationAction(ISD::TRAP, MVT::Other, Legal);
389
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000390 setTargetDAGCombine(ISD::SDIVREM);
391 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanakaee8c3b02012-03-08 03:26:37 +0000392 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000393 setTargetDAGCombine(ISD::AND);
394 setTargetDAGCombine(ISD::OR);
Akira Hatanaka87827072012-06-13 20:33:18 +0000395 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000396
Akira Hatanaka5fdf5002012-03-08 01:59:33 +0000397 setMinFunctionAlignment(HasMips64 ? 3 : 2);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000398
Akira Hatanaka3f5b1072012-02-02 03:17:04 +0000399 setStackPointerRegisterToSaveRestore(IsN64 ? Mips::SP_64 : Mips::SP);
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000400
Akira Hatanaka590baca2012-02-02 03:13:40 +0000401 setExceptionPointerRegister(IsN64 ? Mips::A0_64 : Mips::A0);
402 setExceptionSelectorRegister(IsN64 ? Mips::A1_64 : Mips::A1);
Akira Hatanakae193b322012-06-13 19:33:32 +0000403
Jim Grosbach3450f802013-02-20 21:13:59 +0000404 MaxStoresPerMemcpy = 16;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000405}
406
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000407const MipsTargetLowering *MipsTargetLowering::create(MipsTargetMachine &TM) {
408 if (TM.getSubtargetImpl()->inMips16Mode())
409 return llvm::createMips16TargetLowering(TM);
Jia Liubb481f82012-02-28 07:46:26 +0000410
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000411 return llvm::createMipsSETargetLowering(TM);
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000412}
413
Matt Arsenault225ed702013-05-18 00:21:46 +0000414EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakae13f4412013-01-04 20:06:01 +0000415 if (!VT.isVector())
416 return MVT::i32;
417 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +0000418}
419
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000420static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000421 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000422 const MipsSubtarget *Subtarget) {
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000423 if (DCI.isBeforeLegalizeOps())
424 return SDValue();
425
Akira Hatanakadda4a072011-10-03 21:06:13 +0000426 EVT Ty = N->getValueType(0);
Jia Liubb481f82012-02-28 07:46:26 +0000427 unsigned LO = (Ty == MVT::i32) ? Mips::LO : Mips::LO64;
428 unsigned HI = (Ty == MVT::i32) ? Mips::HI : Mips::HI64;
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000429 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
430 MipsISD::DivRemU16;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000431 SDLoc DL(N);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000432
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000433 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000434 N->getOperand(0), N->getOperand(1));
435 SDValue InChain = DAG.getEntryNode();
436 SDValue InGlue = DivRem;
437
438 // insert MFLO
439 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000440 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000441 InGlue);
442 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
443 InChain = CopyFromLo.getValue(1);
444 InGlue = CopyFromLo.getValue(2);
445 }
446
447 // insert MFHI
448 if (N->hasAnyUseOfValue(1)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000449 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000450 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000451 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
452 }
453
454 return SDValue();
455}
456
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000457static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000458 switch (CC) {
459 default: llvm_unreachable("Unknown fp condition code!");
460 case ISD::SETEQ:
461 case ISD::SETOEQ: return Mips::FCOND_OEQ;
462 case ISD::SETUNE: return Mips::FCOND_UNE;
463 case ISD::SETLT:
464 case ISD::SETOLT: return Mips::FCOND_OLT;
465 case ISD::SETGT:
466 case ISD::SETOGT: return Mips::FCOND_OGT;
467 case ISD::SETLE:
468 case ISD::SETOLE: return Mips::FCOND_OLE;
469 case ISD::SETGE:
470 case ISD::SETOGE: return Mips::FCOND_OGE;
471 case ISD::SETULT: return Mips::FCOND_ULT;
472 case ISD::SETULE: return Mips::FCOND_ULE;
473 case ISD::SETUGT: return Mips::FCOND_UGT;
474 case ISD::SETUGE: return Mips::FCOND_UGE;
475 case ISD::SETUO: return Mips::FCOND_UN;
476 case ISD::SETO: return Mips::FCOND_OR;
477 case ISD::SETNE:
478 case ISD::SETONE: return Mips::FCOND_ONE;
479 case ISD::SETUEQ: return Mips::FCOND_UEQ;
480 }
481}
482
483
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000484/// This function returns true if the floating point conditional branches and
485/// conditional moves which use condition code CC should be inverted.
486static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000487 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
488 return false;
489
Akira Hatanaka82099682011-12-19 19:52:25 +0000490 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
491 "Illegal Condition Code");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000492
Akira Hatanaka82099682011-12-19 19:52:25 +0000493 return true;
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000494}
495
496// Creates and returns an FPCmp node from a setcc node.
497// Returns Op if setcc is not a floating point comparison.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000498static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000499 // must be a SETCC node
500 if (Op.getOpcode() != ISD::SETCC)
501 return Op;
502
503 SDValue LHS = Op.getOperand(0);
504
505 if (!LHS.getValueType().isFloatingPoint())
506 return Op;
507
508 SDValue RHS = Op.getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000509 SDLoc DL(Op);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000510
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000511 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
512 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000513 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
514
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000515 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000516 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000517}
518
519// Creates and returns a CMovFPT/F node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000520static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000521 SDValue False, SDLoc DL) {
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000522 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
523 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka407883b2013-07-26 20:51:20 +0000524 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000525
526 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka407883b2013-07-26 20:51:20 +0000527 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000528}
529
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000530static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000531 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000532 const MipsSubtarget *Subtarget) {
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000533 if (DCI.isBeforeLegalizeOps())
534 return SDValue();
535
536 SDValue SetCC = N->getOperand(0);
537
538 if ((SetCC.getOpcode() != ISD::SETCC) ||
539 !SetCC.getOperand(0).getValueType().isInteger())
540 return SDValue();
541
542 SDValue False = N->getOperand(2);
543 EVT FalseTy = False.getValueType();
544
545 if (!FalseTy.isInteger())
546 return SDValue();
547
548 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(False);
549
550 if (!CN || CN->getZExtValue())
551 return SDValue();
552
Andrew Trickac6d9be2013-05-25 02:42:55 +0000553 const SDLoc DL(N);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000554 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
555 SDValue True = N->getOperand(1);
Akira Hatanaka864f6602012-06-14 21:10:56 +0000556
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000557 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
558 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
Akira Hatanaka864f6602012-06-14 21:10:56 +0000559
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000560 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
561}
562
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000563static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000564 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000565 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000566 // Pattern match EXT.
567 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
568 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000569 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000570 return SDValue();
571
572 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000573 unsigned ShiftRightOpc = ShiftRight.getOpcode();
574
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000575 // Op's first operand must be a shift right.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000576 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000577 return SDValue();
578
579 // The second operand of the shift must be an immediate.
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000580 ConstantSDNode *CN;
581 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
582 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000583
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000584 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000585 uint64_t SMPos, SMSize;
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000586
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000587 // Op's second operand must be a shifted mask.
588 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000589 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000590 return SDValue();
591
592 // Return if the shifted mask does not start at bit 0 or the sum of its size
593 // and Pos exceeds the word's size.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000594 EVT ValTy = N->getValueType(0);
595 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000596 return SDValue();
597
Andrew Trickac6d9be2013-05-25 02:42:55 +0000598 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka82099682011-12-19 19:52:25 +0000599 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000600 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000601}
Jia Liubb481f82012-02-28 07:46:26 +0000602
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000603static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000604 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000605 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000606 // Pattern match INS.
607 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liubb481f82012-02-28 07:46:26 +0000608 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000609 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000610 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000611 return SDValue();
612
613 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
614 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
615 ConstantSDNode *CN;
616
617 // See if Op's first operand matches (and $src1 , mask0).
618 if (And0.getOpcode() != ISD::AND)
619 return SDValue();
620
621 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000622 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000623 return SDValue();
624
625 // See if Op's second operand matches (and (shl $src, pos), mask1).
626 if (And1.getOpcode() != ISD::AND)
627 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000628
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000629 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000630 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000631 return SDValue();
632
633 // The shift masks must have the same position and size.
634 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
635 return SDValue();
636
637 SDValue Shl = And1.getOperand(0);
638 if (Shl.getOpcode() != ISD::SHL)
639 return SDValue();
640
641 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
642 return SDValue();
643
644 unsigned Shamt = CN->getZExtValue();
645
646 // Return if the shift amount and the first bit position of mask are not the
Jia Liubb481f82012-02-28 07:46:26 +0000647 // same.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000648 EVT ValTy = N->getValueType(0);
649 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000650 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000651
Andrew Trickac6d9be2013-05-25 02:42:55 +0000652 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000653 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka82099682011-12-19 19:52:25 +0000654 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000655}
Jia Liubb481f82012-02-28 07:46:26 +0000656
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000657static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka87827072012-06-13 20:33:18 +0000658 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000659 const MipsSubtarget *Subtarget) {
Akira Hatanaka87827072012-06-13 20:33:18 +0000660 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
661
662 if (DCI.isBeforeLegalizeOps())
663 return SDValue();
664
665 SDValue Add = N->getOperand(1);
666
667 if (Add.getOpcode() != ISD::ADD)
668 return SDValue();
669
670 SDValue Lo = Add.getOperand(1);
671
672 if ((Lo.getOpcode() != MipsISD::Lo) ||
673 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
674 return SDValue();
675
676 EVT ValTy = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000677 SDLoc DL(N);
Akira Hatanaka87827072012-06-13 20:33:18 +0000678
679 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
680 Add.getOperand(0));
681 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
682}
683
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000684SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000685 const {
686 SelectionDAG &DAG = DCI.DAG;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000687 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000688
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000689 switch (Opc) {
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000690 default: break;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000691 case ISD::SDIVREM:
692 case ISD::UDIVREM:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000693 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000694 case ISD::SELECT:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000695 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000696 case ISD::AND:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000697 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000698 case ISD::OR:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000699 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka87827072012-06-13 20:33:18 +0000700 case ISD::ADD:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000701 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000702 }
703
704 return SDValue();
705}
706
Akira Hatanakab430cec2012-09-21 23:58:31 +0000707void
708MipsTargetLowering::LowerOperationWrapper(SDNode *N,
709 SmallVectorImpl<SDValue> &Results,
710 SelectionDAG &DAG) const {
711 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
712
713 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
714 Results.push_back(Res.getValue(I));
715}
716
717void
718MipsTargetLowering::ReplaceNodeResults(SDNode *N,
719 SmallVectorImpl<SDValue> &Results,
720 SelectionDAG &DAG) const {
Akira Hatanaka13ec4812013-04-30 21:17:07 +0000721 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakab430cec2012-09-21 23:58:31 +0000722}
723
Dan Gohman475871a2008-07-27 21:46:04 +0000724SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000725LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000726{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000727 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000728 {
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000729 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
730 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
731 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
732 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
733 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
734 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
735 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
736 case ISD::SELECT: return lowerSELECT(Op, DAG);
737 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
738 case ISD::SETCC: return lowerSETCC(Op, DAG);
739 case ISD::VASTART: return lowerVASTART(Op, DAG);
740 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
741 case ISD::FABS: return lowerFABS(Op, DAG);
742 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
743 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
744 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000745 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
746 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
747 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
748 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
749 case ISD::LOAD: return lowerLOAD(Op, DAG);
750 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000751 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000752 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000753 }
Dan Gohman475871a2008-07-27 21:46:04 +0000754 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000755}
756
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000757//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000758// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000759//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000760
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000761// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000762// MachineFunction as a live in value. It also creates a corresponding
763// virtual register for it.
764static unsigned
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000765addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000766{
Chris Lattner84bc5422007-12-31 04:13:23 +0000767 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
768 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000769 return VReg;
770}
771
Akira Hatanakaf8941992013-05-20 18:07:43 +0000772static MachineBasicBlock *expandPseudoDIV(MachineInstr *MI,
773 MachineBasicBlock &MBB,
774 const TargetInstrInfo &TII,
775 bool Is64Bit) {
776 if (NoZeroDivCheck)
777 return &MBB;
778
779 // Insert instruction "teq $divisor_reg, $zero, 7".
780 MachineBasicBlock::iterator I(MI);
781 MachineInstrBuilder MIB;
782 MIB = BuildMI(MBB, llvm::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
783 .addOperand(MI->getOperand(2)).addReg(Mips::ZERO).addImm(7);
784
785 // Use the 32-bit sub-register if this is a 64-bit division.
786 if (Is64Bit)
787 MIB->getOperand(0).setSubReg(Mips::sub_32);
788
789 return &MBB;
790}
791
Akira Hatanaka01f70892012-09-27 02:15:57 +0000792MachineBasicBlock *
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000793MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000794 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000795 switch (MI->getOpcode()) {
Reed Kotlerffbe4322013-02-21 04:22:38 +0000796 default:
797 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000798 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000799 case Mips::ATOMIC_LOAD_ADD_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000800 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000801 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000802 case Mips::ATOMIC_LOAD_ADD_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000803 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000804 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000805 case Mips::ATOMIC_LOAD_ADD_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000806 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000807 case Mips::ATOMIC_LOAD_ADD_I64:
808 case Mips::ATOMIC_LOAD_ADD_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000809 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000810
811 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000812 case Mips::ATOMIC_LOAD_AND_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000813 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000814 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000815 case Mips::ATOMIC_LOAD_AND_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000816 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000817 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000818 case Mips::ATOMIC_LOAD_AND_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000819 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000820 case Mips::ATOMIC_LOAD_AND_I64:
821 case Mips::ATOMIC_LOAD_AND_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000822 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000823
824 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000825 case Mips::ATOMIC_LOAD_OR_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000826 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000827 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000828 case Mips::ATOMIC_LOAD_OR_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000829 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000830 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000831 case Mips::ATOMIC_LOAD_OR_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000832 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000833 case Mips::ATOMIC_LOAD_OR_I64:
834 case Mips::ATOMIC_LOAD_OR_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000835 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000836
837 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000838 case Mips::ATOMIC_LOAD_XOR_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000839 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000840 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000841 case Mips::ATOMIC_LOAD_XOR_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000842 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000843 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000844 case Mips::ATOMIC_LOAD_XOR_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000845 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000846 case Mips::ATOMIC_LOAD_XOR_I64:
847 case Mips::ATOMIC_LOAD_XOR_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000848 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000849
850 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000851 case Mips::ATOMIC_LOAD_NAND_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000852 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000853 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000854 case Mips::ATOMIC_LOAD_NAND_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000855 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000856 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000857 case Mips::ATOMIC_LOAD_NAND_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000858 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000859 case Mips::ATOMIC_LOAD_NAND_I64:
860 case Mips::ATOMIC_LOAD_NAND_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000861 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000862
863 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000864 case Mips::ATOMIC_LOAD_SUB_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000865 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000866 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000867 case Mips::ATOMIC_LOAD_SUB_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000868 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000869 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000870 case Mips::ATOMIC_LOAD_SUB_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000871 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000872 case Mips::ATOMIC_LOAD_SUB_I64:
873 case Mips::ATOMIC_LOAD_SUB_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000874 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000875
876 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000877 case Mips::ATOMIC_SWAP_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000878 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000879 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000880 case Mips::ATOMIC_SWAP_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000881 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000882 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000883 case Mips::ATOMIC_SWAP_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000884 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000885 case Mips::ATOMIC_SWAP_I64:
886 case Mips::ATOMIC_SWAP_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000887 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000888
889 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000890 case Mips::ATOMIC_CMP_SWAP_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000891 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000892 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000893 case Mips::ATOMIC_CMP_SWAP_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000894 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000895 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000896 case Mips::ATOMIC_CMP_SWAP_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000897 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000898 case Mips::ATOMIC_CMP_SWAP_I64:
899 case Mips::ATOMIC_CMP_SWAP_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000900 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanakaf8941992013-05-20 18:07:43 +0000901 case Mips::PseudoSDIV:
902 case Mips::PseudoUDIV:
903 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), false);
904 case Mips::PseudoDSDIV:
905 case Mips::PseudoDUDIV:
906 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), true);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000907 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000908}
909
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000910// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
911// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
912MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000913MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000914 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000915 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000916 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000917
918 MachineFunction *MF = BB->getParent();
919 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000920 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000921 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000922 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000923 unsigned LL, SC, AND, NOR, ZERO, BEQ;
924
925 if (Size == 4) {
926 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
927 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
928 AND = Mips::AND;
929 NOR = Mips::NOR;
930 ZERO = Mips::ZERO;
931 BEQ = Mips::BEQ;
932 }
933 else {
934 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
935 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
936 AND = Mips::AND64;
937 NOR = Mips::NOR64;
938 ZERO = Mips::ZERO_64;
939 BEQ = Mips::BEQ64;
940 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000941
Akira Hatanaka4061da12011-07-19 20:11:17 +0000942 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000943 unsigned Ptr = MI->getOperand(1).getReg();
944 unsigned Incr = MI->getOperand(2).getReg();
945
Akira Hatanaka4061da12011-07-19 20:11:17 +0000946 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
947 unsigned AndRes = RegInfo.createVirtualRegister(RC);
948 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000949
950 // insert new blocks after the current block
951 const BasicBlock *LLVM_BB = BB->getBasicBlock();
952 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
953 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
954 MachineFunction::iterator It = BB;
955 ++It;
956 MF->insert(It, loopMBB);
957 MF->insert(It, exitMBB);
958
959 // Transfer the remainder of BB and its successor edges to exitMBB.
960 exitMBB->splice(exitMBB->begin(), BB,
961 llvm::next(MachineBasicBlock::iterator(MI)),
962 BB->end());
963 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
964
965 // thisMBB:
966 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000967 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000968 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000969 loopMBB->addSuccessor(loopMBB);
970 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000971
972 // loopMBB:
973 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000974 // <binop> storeval, oldval, incr
975 // sc success, storeval, 0(ptr)
976 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000977 BB = loopMBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000978 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000979 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000980 // and andres, oldval, incr
981 // nor storeval, $0, andres
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000982 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
983 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000984 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000985 // <binop> storeval, oldval, incr
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000986 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000987 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000988 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000989 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000990 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
991 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000992
993 MI->eraseFromParent(); // The instruction is gone now.
994
Akira Hatanaka939ece12011-07-19 03:42:13 +0000995 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000996}
997
998MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000999MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001000 MachineBasicBlock *BB,
1001 unsigned Size, unsigned BinOpcode,
1002 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001003 assert((Size == 1 || Size == 2) &&
1004 "Unsupported size for EmitAtomicBinaryPartial.");
1005
1006 MachineFunction *MF = BB->getParent();
1007 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1008 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1009 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001010 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001011 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1012 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001013
1014 unsigned Dest = MI->getOperand(0).getReg();
1015 unsigned Ptr = MI->getOperand(1).getReg();
1016 unsigned Incr = MI->getOperand(2).getReg();
1017
Akira Hatanaka4061da12011-07-19 20:11:17 +00001018 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1019 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001020 unsigned Mask = RegInfo.createVirtualRegister(RC);
1021 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001022 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1023 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001024 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001025 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1026 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1027 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1028 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1029 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001030 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001031 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1032 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1033 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1034 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1035 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001036
1037 // insert new blocks after the current block
1038 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1039 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001040 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001041 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1042 MachineFunction::iterator It = BB;
1043 ++It;
1044 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001045 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001046 MF->insert(It, exitMBB);
1047
1048 // Transfer the remainder of BB and its successor edges to exitMBB.
1049 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001050 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001051 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1052
Akira Hatanaka81b44112011-07-19 17:09:53 +00001053 BB->addSuccessor(loopMBB);
1054 loopMBB->addSuccessor(loopMBB);
1055 loopMBB->addSuccessor(sinkMBB);
1056 sinkMBB->addSuccessor(exitMBB);
1057
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001058 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001059 // addiu masklsb2,$0,-4 # 0xfffffffc
1060 // and alignedaddr,ptr,masklsb2
1061 // andi ptrlsb2,ptr,3
1062 // sll shiftamt,ptrlsb2,3
1063 // ori maskupper,$0,255 # 0xff
1064 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001065 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001066 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001067
1068 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001069 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001070 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001071 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001072 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001073 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001074 if (Subtarget->isLittle()) {
1075 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1076 } else {
1077 unsigned Off = RegInfo.createVirtualRegister(RC);
1078 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1079 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1080 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1081 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001082 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001083 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001084 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001085 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001086 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka51122432013-07-01 20:39:53 +00001087 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001088
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001089 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001090 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001091 // ll oldval,0(alignedaddr)
1092 // binop binopres,oldval,incr2
1093 // and newval,binopres,mask
1094 // and maskedoldval0,oldval,mask2
1095 // or storeval,maskedoldval0,newval
1096 // sc success,storeval,0(alignedaddr)
1097 // beq success,$0,loopMBB
1098
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001099 // atomic.swap
1100 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001101 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001102 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001103 // and maskedoldval0,oldval,mask2
1104 // or storeval,maskedoldval0,newval
1105 // sc success,storeval,0(alignedaddr)
1106 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001107
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001108 BB = loopMBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001109 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001110 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001111 // and andres, oldval, incr2
1112 // nor binopres, $0, andres
1113 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001114 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1115 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001116 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001117 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001118 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001119 // <binop> binopres, oldval, incr2
1120 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001121 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1122 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001123 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001124 // and newval, incr2, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001125 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001126 }
Jia Liubb481f82012-02-28 07:46:26 +00001127
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001128 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001129 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001130 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001131 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001132 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001133 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001134 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001135 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001136
Akira Hatanaka939ece12011-07-19 03:42:13 +00001137 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001138 // and maskedoldval1,oldval,mask
1139 // srl srlres,maskedoldval1,shiftamt
1140 // sll sllres,srlres,24
1141 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001142 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001143 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001144
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001145 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001146 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001147 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001148 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001149 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001150 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001151 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001152 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001153
1154 MI->eraseFromParent(); // The instruction is gone now.
1155
Akira Hatanaka939ece12011-07-19 03:42:13 +00001156 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001157}
1158
1159MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001160MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001161 MachineBasicBlock *BB,
1162 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001163 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001164
1165 MachineFunction *MF = BB->getParent();
1166 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001167 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001168 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001169 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001170 unsigned LL, SC, ZERO, BNE, BEQ;
1171
1172 if (Size == 4) {
1173 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1174 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1175 ZERO = Mips::ZERO;
1176 BNE = Mips::BNE;
1177 BEQ = Mips::BEQ;
1178 }
1179 else {
1180 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1181 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1182 ZERO = Mips::ZERO_64;
1183 BNE = Mips::BNE64;
1184 BEQ = Mips::BEQ64;
1185 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001186
1187 unsigned Dest = MI->getOperand(0).getReg();
1188 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001189 unsigned OldVal = MI->getOperand(2).getReg();
1190 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001191
Akira Hatanaka4061da12011-07-19 20:11:17 +00001192 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001193
1194 // insert new blocks after the current block
1195 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1196 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1197 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1198 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1199 MachineFunction::iterator It = BB;
1200 ++It;
1201 MF->insert(It, loop1MBB);
1202 MF->insert(It, loop2MBB);
1203 MF->insert(It, exitMBB);
1204
1205 // Transfer the remainder of BB and its successor edges to exitMBB.
1206 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001207 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001208 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1209
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001210 // thisMBB:
1211 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001212 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001213 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001214 loop1MBB->addSuccessor(exitMBB);
1215 loop1MBB->addSuccessor(loop2MBB);
1216 loop2MBB->addSuccessor(loop1MBB);
1217 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001218
1219 // loop1MBB:
1220 // ll dest, 0(ptr)
1221 // bne dest, oldval, exitMBB
1222 BB = loop1MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001223 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1224 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001225 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001226
1227 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001228 // sc success, newval, 0(ptr)
1229 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001230 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001231 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001232 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001233 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka59068062011-11-11 04:14:30 +00001234 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001235
1236 MI->eraseFromParent(); // The instruction is gone now.
1237
Akira Hatanaka939ece12011-07-19 03:42:13 +00001238 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001239}
1240
1241MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001242MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001243 MachineBasicBlock *BB,
1244 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001245 assert((Size == 1 || Size == 2) &&
1246 "Unsupported size for EmitAtomicCmpSwapPartial.");
1247
1248 MachineFunction *MF = BB->getParent();
1249 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1250 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1251 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001252 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001253 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1254 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001255
1256 unsigned Dest = MI->getOperand(0).getReg();
1257 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001258 unsigned CmpVal = MI->getOperand(2).getReg();
1259 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001260
Akira Hatanaka4061da12011-07-19 20:11:17 +00001261 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1262 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001263 unsigned Mask = RegInfo.createVirtualRegister(RC);
1264 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001265 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1266 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1267 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1268 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1269 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1270 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1271 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1272 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1273 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1274 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1275 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1276 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1277 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1278 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001279
1280 // insert new blocks after the current block
1281 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1282 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1283 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001284 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001285 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1286 MachineFunction::iterator It = BB;
1287 ++It;
1288 MF->insert(It, loop1MBB);
1289 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001290 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001291 MF->insert(It, exitMBB);
1292
1293 // Transfer the remainder of BB and its successor edges to exitMBB.
1294 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001295 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001296 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1297
Akira Hatanaka81b44112011-07-19 17:09:53 +00001298 BB->addSuccessor(loop1MBB);
1299 loop1MBB->addSuccessor(sinkMBB);
1300 loop1MBB->addSuccessor(loop2MBB);
1301 loop2MBB->addSuccessor(loop1MBB);
1302 loop2MBB->addSuccessor(sinkMBB);
1303 sinkMBB->addSuccessor(exitMBB);
1304
Akira Hatanaka70564a92011-07-19 18:14:26 +00001305 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001306 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001307 // addiu masklsb2,$0,-4 # 0xfffffffc
1308 // and alignedaddr,ptr,masklsb2
1309 // andi ptrlsb2,ptr,3
1310 // sll shiftamt,ptrlsb2,3
1311 // ori maskupper,$0,255 # 0xff
1312 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001313 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001314 // andi maskedcmpval,cmpval,255
1315 // sll shiftedcmpval,maskedcmpval,shiftamt
1316 // andi maskednewval,newval,255
1317 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001318 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001319 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001320 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001321 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001322 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001323 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001324 if (Subtarget->isLittle()) {
1325 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1326 } else {
1327 unsigned Off = RegInfo.createVirtualRegister(RC);
1328 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1329 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1330 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1331 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001332 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001333 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001334 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001335 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001336 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1337 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001338 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001339 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001340 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001341 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001342 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001343 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001344 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001345
1346 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001347 // ll oldval,0(alginedaddr)
1348 // and maskedoldval0,oldval,mask
1349 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001350 BB = loop1MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001351 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
1352 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001353 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001354 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001355 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001356
1357 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001358 // and maskedoldval1,oldval,mask2
1359 // or storeval,maskedoldval1,shiftednewval
1360 // sc success,storeval,0(alignedaddr)
1361 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001362 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001363 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001364 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001365 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001366 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001367 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001368 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001369 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001370 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001371
Akira Hatanaka939ece12011-07-19 03:42:13 +00001372 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001373 // srl srlres,maskedoldval0,shiftamt
1374 // sll sllres,srlres,24
1375 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001376 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001377 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001378
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001379 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001380 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001381 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001382 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001383 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001384 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001385
1386 MI->eraseFromParent(); // The instruction is gone now.
1387
Akira Hatanaka939ece12011-07-19 03:42:13 +00001388 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001389}
1390
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001391//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001392// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001393//===----------------------------------------------------------------------===//
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001394SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakab7656a92013-03-06 21:32:03 +00001395 SDValue Chain = Op.getOperand(0);
1396 SDValue Table = Op.getOperand(1);
1397 SDValue Index = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001398 SDLoc DL(Op);
Akira Hatanakab7656a92013-03-06 21:32:03 +00001399 EVT PTy = getPointerTy();
1400 unsigned EntrySize =
1401 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1402
1403 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1404 DAG.getConstant(EntrySize, PTy));
1405 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1406
1407 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1408 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1409 MachinePointerInfo::getJumpTable(), MemVT, false, false,
1410 0);
1411 Chain = Addr.getValue(1);
1412
1413 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || IsN64) {
1414 // For PIC, the sequence is:
1415 // BRIND(load(Jumptable + index) + RelocBase)
1416 // RelocBase can be JumpTable, GOT or some sort of global base.
1417 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1418 getPICJumpTableRelocBase(Table, DAG));
1419 }
1420
1421 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1422}
1423
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001424SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001425lowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001426{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001427 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001428 // the block to branch to if the condition is true.
1429 SDValue Chain = Op.getOperand(0);
1430 SDValue Dest = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001431 SDLoc DL(Op);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001432
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001433 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001434
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001435 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001436 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001437 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001438
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001439 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001440 Mips::CondCode CC =
1441 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanaka9cf07242013-03-30 01:16:38 +00001442 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1443 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001444 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001445 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001446 FCC0, Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001447}
1448
1449SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001450lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001451{
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001452 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001453
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001454 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001455 if (Cond.getOpcode() != MipsISD::FPCmp)
1456 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001457
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001458 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001459 SDLoc(Op));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001460}
1461
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001462SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001463lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001464{
Andrew Trickac6d9be2013-05-25 02:42:55 +00001465 SDLoc DL(Op);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001466 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault225ed702013-05-18 00:21:46 +00001467 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1468 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001469 Op.getOperand(0), Op.getOperand(1),
1470 Op.getOperand(4));
1471
1472 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1473 Op.getOperand(3));
1474}
1475
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001476SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1477 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001478
1479 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1480 "Floating point operand expected.");
1481
1482 SDValue True = DAG.getConstant(1, MVT::i32);
1483 SDValue False = DAG.getConstant(0, MVT::i32);
1484
Andrew Trickac6d9be2013-05-25 02:42:55 +00001485 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001486}
1487
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001488SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001489 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001490 // FIXME there isn't actually debug info here
Andrew Trickac6d9be2013-05-25 02:42:55 +00001491 SDLoc DL(Op);
Jia Liubb481f82012-02-28 07:46:26 +00001492 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001493
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001494 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaafc945b2012-09-12 23:27:55 +00001495 const MipsTargetObjectFile &TLOF =
1496 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001497
Chris Lattnere3736f82009-08-13 05:41:27 +00001498 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001499 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001500 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001501 MipsII::MO_GPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001502 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001503 DAG.getVTList(MVT::i32), &GA, 1);
Akira Hatanakae7338cd2012-08-22 03:18:13 +00001504 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001505 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001506 }
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001507
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001508 // %hi/%lo relocation
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001509 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001510 }
1511
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001512 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
1513 return getAddrLocal(Op, DAG, HasMips64);
1514
Akira Hatanakaf09a0372012-11-21 20:40:38 +00001515 if (LargeGOT)
1516 return getAddrGlobalLargeGOT(Op, DAG, MipsII::MO_GOT_HI16,
1517 MipsII::MO_GOT_LO16);
1518
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001519 return getAddrGlobal(Op, DAG,
1520 HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001521}
1522
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001523SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001524 SelectionDAG &DAG) const {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001525 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1526 return getAddrNonPIC(Op, DAG);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001527
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001528 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001529}
1530
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001531SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001532lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001533{
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001534 // If the relocation model is PIC, use the General Dynamic TLS Model or
1535 // Local Dynamic TLS model, otherwise use the Initial Exec or
1536 // Local Exec TLS Model.
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001537
1538 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001539 SDLoc DL(GA);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001540 const GlobalValue *GV = GA->getGlobal();
1541 EVT PtrVT = getPointerTy();
1542
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001543 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1544
1545 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg70a07c72012-06-04 14:02:08 +00001546 // General Dynamic and Local Dynamic TLS Model.
1547 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1548 : MipsII::MO_TLSGD;
1549
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001550 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1551 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1552 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001553 unsigned PtrSize = PtrVT.getSizeInBits();
1554 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1555
Benjamin Kramer5eccf672011-12-11 12:21:34 +00001556 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001557
1558 ArgListTy Args;
1559 ArgListEntry Entry;
1560 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00001561 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001562 Args.push_back(Entry);
Jia Liubb481f82012-02-28 07:46:26 +00001563
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001564 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001565 false, false, false, false, 0, CallingConv::C,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001566 /*IsTailCall=*/false, /*doesNotRet=*/false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001567 /*isReturnValueUsed=*/true,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001568 TlsGetAddr, Args, DAG, DL);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001569 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001570
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001571 SDValue Ret = CallResult.first;
1572
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001573 if (model != TLSModel::LocalDynamic)
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001574 return Ret;
1575
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001576 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001577 MipsII::MO_DTPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001578 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1579 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001580 MipsII::MO_DTPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001581 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1582 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1583 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001584 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001585
1586 SDValue Offset;
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001587 if (model == TLSModel::InitialExec) {
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001588 // Initial Exec TLS Model
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001589 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001590 MipsII::MO_GOTTPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001591 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanaka648f00c2012-02-24 22:34:47 +00001592 TGA);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001593 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001594 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001595 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001596 } else {
1597 // Local Exec TLS Model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001598 assert(model == TLSModel::LocalExec);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001599 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001600 MipsII::MO_TPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001601 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001602 MipsII::MO_TPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001603 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1604 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1605 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001606 }
1607
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001608 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1609 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001610}
1611
1612SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001613lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001614{
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001615 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1616 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001617
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001618 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001619}
1620
Dan Gohman475871a2008-07-27 21:46:04 +00001621SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001622lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001623{
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001624 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001625 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001626 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001627 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001628 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001629 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001630 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1631 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001632 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001633
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001634 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1635 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001636
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001637 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001638}
1639
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001640SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001641 MachineFunction &MF = DAG.getMachineFunction();
1642 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1643
Andrew Trickac6d9be2013-05-25 02:42:55 +00001644 SDLoc DL(Op);
Dan Gohman1e93df62010-04-17 14:41:14 +00001645 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1646 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001647
1648 // vastart just stores the address of the VarArgsFrameIndex slot into the
1649 // memory location argument.
1650 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001651 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka82099682011-12-19 19:52:25 +00001652 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001653}
Jia Liubb481f82012-02-28 07:46:26 +00001654
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001655static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001656 EVT TyX = Op.getOperand(0).getValueType();
1657 EVT TyY = Op.getOperand(1).getValueType();
1658 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1659 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001660 SDLoc DL(Op);
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001661 SDValue Res;
1662
1663 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1664 // to i32.
1665 SDValue X = (TyX == MVT::f32) ?
1666 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1667 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1668 Const1);
1669 SDValue Y = (TyY == MVT::f32) ?
1670 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1671 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1672 Const1);
1673
1674 if (HasR2) {
1675 // ext E, Y, 31, 1 ; extract bit31 of Y
1676 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1677 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1678 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1679 } else {
1680 // sll SllX, X, 1
1681 // srl SrlX, SllX, 1
1682 // srl SrlY, Y, 31
1683 // sll SllY, SrlX, 31
1684 // or Or, SrlX, SllY
1685 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1686 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1687 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1688 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1689 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1690 }
1691
1692 if (TyX == MVT::f32)
1693 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1694
1695 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1696 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1697 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001698}
1699
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001700static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001701 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1702 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1703 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1704 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001705 SDLoc DL(Op);
Eric Christopher471e4222011-06-08 23:55:35 +00001706
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001707 // Bitcast to integer nodes.
1708 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1709 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001710
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001711 if (HasR2) {
1712 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1713 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1714 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1715 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001716
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001717 if (WidthX > WidthY)
1718 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1719 else if (WidthY > WidthX)
1720 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001721
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001722 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1723 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1724 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1725 }
1726
1727 // (d)sll SllX, X, 1
1728 // (d)srl SrlX, SllX, 1
1729 // (d)srl SrlY, Y, width(Y)-1
1730 // (d)sll SllY, SrlX, width(Y)-1
1731 // or Or, SrlX, SllY
1732 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1733 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1734 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1735 DAG.getConstant(WidthY - 1, MVT::i32));
1736
1737 if (WidthX > WidthY)
1738 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1739 else if (WidthY > WidthX)
1740 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1741
1742 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1743 DAG.getConstant(WidthX - 1, MVT::i32));
1744 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1745 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001746}
1747
Akira Hatanaka82099682011-12-19 19:52:25 +00001748SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001749MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001750 if (Subtarget->hasMips64())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001751 return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001752
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001753 return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001754}
1755
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001756static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001757 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001758 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001759
1760 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1761 // to i32.
1762 SDValue X = (Op.getValueType() == MVT::f32) ?
1763 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1764 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1765 Const1);
1766
1767 // Clear MSB.
1768 if (HasR2)
1769 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
1770 DAG.getRegister(Mips::ZERO, MVT::i32),
1771 DAG.getConstant(31, MVT::i32), Const1, X);
1772 else {
1773 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1774 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1775 }
1776
1777 if (Op.getValueType() == MVT::f32)
1778 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
1779
1780 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1781 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1782 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1783}
1784
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001785static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001786 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001787 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001788
1789 // Bitcast to integer node.
1790 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
1791
1792 // Clear MSB.
1793 if (HasR2)
1794 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
1795 DAG.getRegister(Mips::ZERO_64, MVT::i64),
1796 DAG.getConstant(63, MVT::i32), Const1, X);
1797 else {
1798 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
1799 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
1800 }
1801
1802 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
1803}
1804
1805SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001806MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001807 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001808 return lowerFABS64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001809
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001810 return lowerFABS32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001811}
1812
Akira Hatanaka2e591472011-06-02 00:24:44 +00001813SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001814lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001815 // check the depth
1816 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001817 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001818
1819 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1820 MFI->setFrameAddressIsTaken(true);
1821 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001822 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001823 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001824 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001825 return FrameAddr;
1826}
1827
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001828SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001829 SelectionDAG &DAG) const {
1830 // check the depth
1831 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1832 "Return address can be determined only for current frame.");
1833
1834 MachineFunction &MF = DAG.getMachineFunction();
1835 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001836 MVT VT = Op.getSimpleValueType();
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001837 unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
1838 MFI->setReturnAddressIsTaken(true);
1839
1840 // Return RA, which contains the return address. Mark it an implicit live-in.
1841 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001842 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001843}
1844
Akira Hatanaka544cc212013-01-30 00:26:49 +00001845// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1846// generated from __builtin_eh_return (offset, handler)
1847// The effect of this is to adjust the stack pointer by "offset"
1848// and then branch to "handler".
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001849SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanaka544cc212013-01-30 00:26:49 +00001850 const {
1851 MachineFunction &MF = DAG.getMachineFunction();
1852 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1853
1854 MipsFI->setCallsEhReturn();
1855 SDValue Chain = Op.getOperand(0);
1856 SDValue Offset = Op.getOperand(1);
1857 SDValue Handler = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001858 SDLoc DL(Op);
Akira Hatanaka544cc212013-01-30 00:26:49 +00001859 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
1860
1861 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1862 // EH_RETURN nodes, so that instructions are emitted back-to-back.
1863 unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1;
1864 unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0;
1865 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1866 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1867 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1868 DAG.getRegister(OffsetReg, Ty),
1869 DAG.getRegister(AddrReg, getPointerTy()),
1870 Chain.getValue(1));
1871}
1872
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001873SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001874 SelectionDAG &DAG) const {
Eli Friedman14648462011-07-27 22:21:52 +00001875 // FIXME: Need pseudo-fence for 'singlethread' fences
1876 // FIXME: Set SType for weaker fences where supported/appropriate.
1877 unsigned SType = 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001878 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001879 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00001880 DAG.getConstant(SType, MVT::i32));
1881}
1882
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001883SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001884 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001885 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001886 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1887 SDValue Shamt = Op.getOperand(2);
1888
1889 // if shamt < 32:
1890 // lo = (shl lo, shamt)
1891 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
1892 // else:
1893 // lo = 0
1894 // hi = (shl lo, shamt[4:0])
1895 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1896 DAG.getConstant(-1, MVT::i32));
1897 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
1898 DAG.getConstant(1, MVT::i32));
1899 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
1900 Not);
1901 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
1902 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1903 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
1904 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1905 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka864f6602012-06-14 21:10:56 +00001906 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1907 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001908 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
1909
1910 SDValue Ops[2] = {Lo, Hi};
1911 return DAG.getMergeValues(Ops, 2, DL);
1912}
1913
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001914SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001915 bool IsSRA) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001916 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001917 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1918 SDValue Shamt = Op.getOperand(2);
1919
1920 // if shamt < 32:
1921 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
1922 // if isSRA:
1923 // hi = (sra hi, shamt)
1924 // else:
1925 // hi = (srl hi, shamt)
1926 // else:
1927 // if isSRA:
1928 // lo = (sra hi, shamt[4:0])
1929 // hi = (sra hi, 31)
1930 // else:
1931 // lo = (srl hi, shamt[4:0])
1932 // hi = 0
1933 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1934 DAG.getConstant(-1, MVT::i32));
1935 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
1936 DAG.getConstant(1, MVT::i32));
1937 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
1938 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
1939 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1940 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
1941 Hi, Shamt);
1942 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1943 DAG.getConstant(0x20, MVT::i32));
1944 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
1945 DAG.getConstant(31, MVT::i32));
1946 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
1947 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1948 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
1949 ShiftRightHi);
1950
1951 SDValue Ops[2] = {Lo, Hi};
1952 return DAG.getMergeValues(Ops, 2, DL);
1953}
1954
Akira Hatanakafee62c12013-04-11 19:07:14 +00001955static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001956 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001957 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001958 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001959 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001960 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001961 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1962
1963 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001964 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001965 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001966
1967 SDValue Ops[] = { Chain, Ptr, Src };
1968 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
1969 LD->getMemOperand());
1970}
1971
1972// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001973SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001974 LoadSDNode *LD = cast<LoadSDNode>(Op);
1975 EVT MemVT = LD->getMemoryVT();
1976
1977 // Return if load is aligned or if MemVT is neither i32 nor i64.
1978 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
1979 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
1980 return SDValue();
1981
1982 bool IsLittle = Subtarget->isLittle();
1983 EVT VT = Op.getValueType();
1984 ISD::LoadExtType ExtType = LD->getExtensionType();
1985 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
1986
1987 assert((VT == MVT::i32) || (VT == MVT::i64));
1988
1989 // Expand
1990 // (set dst, (i64 (load baseptr)))
1991 // to
1992 // (set tmp, (ldl (add baseptr, 7), undef))
1993 // (set dst, (ldr baseptr, tmp))
1994 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00001995 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001996 IsLittle ? 7 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00001997 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001998 IsLittle ? 0 : 7);
1999 }
2000
Akira Hatanakafee62c12013-04-11 19:07:14 +00002001 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002002 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00002003 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002004 IsLittle ? 0 : 3);
2005
2006 // Expand
2007 // (set dst, (i32 (load baseptr))) or
2008 // (set dst, (i64 (sextload baseptr))) or
2009 // (set dst, (i64 (extload baseptr)))
2010 // to
2011 // (set tmp, (lwl (add baseptr, 3), undef))
2012 // (set dst, (lwr baseptr, tmp))
2013 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2014 (ExtType == ISD::EXTLOAD))
2015 return LWR;
2016
2017 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2018
2019 // Expand
2020 // (set dst, (i64 (zextload baseptr)))
2021 // to
2022 // (set tmp0, (lwl (add baseptr, 3), undef))
2023 // (set tmp1, (lwr baseptr, tmp0))
2024 // (set tmp2, (shl tmp1, 32))
2025 // (set dst, (srl tmp2, 32))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002026 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002027 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2028 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka94ccee22012-06-04 17:46:29 +00002029 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2030 SDValue Ops[] = { SRL, LWR.getValue(1) };
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002031 return DAG.getMergeValues(Ops, 2, DL);
2032}
2033
Akira Hatanakafee62c12013-04-11 19:07:14 +00002034static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002035 SDValue Chain, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002036 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2037 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002038 SDLoc DL(SD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002039 SDVTList VTList = DAG.getVTList(MVT::Other);
2040
2041 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002042 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002043 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002044
2045 SDValue Ops[] = { Chain, Value, Ptr };
2046 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2047 SD->getMemOperand());
2048}
2049
2050// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanaka63451432013-05-16 20:45:17 +00002051static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2052 bool IsLittle) {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002053 SDValue Value = SD->getValue(), Chain = SD->getChain();
2054 EVT VT = Value.getValueType();
2055
2056 // Expand
2057 // (store val, baseptr) or
2058 // (truncstore val, baseptr)
2059 // to
2060 // (swl val, (add baseptr, 3))
2061 // (swr val, baseptr)
2062 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00002063 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002064 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00002065 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002066 }
2067
2068 assert(VT == MVT::i64);
2069
2070 // Expand
2071 // (store val, baseptr)
2072 // to
2073 // (sdl val, (add baseptr, 7))
2074 // (sdr val, baseptr)
Akira Hatanakafee62c12013-04-11 19:07:14 +00002075 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2076 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002077}
2078
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002079// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2080static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2081 SDValue Val = SD->getValue();
2082
2083 if (Val.getOpcode() != ISD::FP_TO_SINT)
2084 return SDValue();
2085
2086 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002087 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002088 Val.getOperand(0));
2089
Andrew Trickac6d9be2013-05-25 02:42:55 +00002090 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002091 SD->getPointerInfo(), SD->isVolatile(),
2092 SD->isNonTemporal(), SD->getAlignment());
2093}
2094
Akira Hatanaka63451432013-05-16 20:45:17 +00002095SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2096 StoreSDNode *SD = cast<StoreSDNode>(Op);
2097 EVT MemVT = SD->getMemoryVT();
2098
2099 // Lower unaligned integer stores.
2100 if ((SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2101 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2102 return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle());
2103
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002104 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanaka63451432013-05-16 20:45:17 +00002105}
2106
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002107SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002108 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2109 || cast<ConstantSDNode>
2110 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2111 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2112 return SDValue();
2113
2114 // The pattern
2115 // (add (frameaddr 0), (frame_to_args_offset))
2116 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2117 // (add FrameObject, 0)
2118 // where FrameObject is a fixed StackObject with offset 0 which points to
2119 // the old stack pointer.
2120 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2121 EVT ValTy = Op->getValueType(0);
2122 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2123 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002124 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002125 DAG.getConstant(0, ValTy));
2126}
2127
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002128SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2129 SelectionDAG &DAG) const {
2130 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002131 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002132 Op.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002133 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002134}
2135
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002136//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002137// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002138//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002139
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002140//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002141// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002142// Mips O32 ABI rules:
2143// ---
2144// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002145// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002146// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002147// f64 - Only passed in two aliased f32 registers if no int reg has been used
2148// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002149// not used, it must be shadowed. If only A3 is avaiable, shadow it and
2150// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002151//
2152// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002153//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002154
Duncan Sands1e96bab2010-11-04 10:49:57 +00002155static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00002156 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002157 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2158
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002159 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002160
Craig Topperc5eaae42012-03-11 07:57:25 +00002161 static const uint16_t IntRegs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002162 Mips::A0, Mips::A1, Mips::A2, Mips::A3
2163 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002164 static const uint16_t F32Regs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002165 Mips::F12, Mips::F14
2166 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002167 static const uint16_t F64Regs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002168 Mips::D6, Mips::D7
2169 };
2170
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002171 // Do not process byval args here.
2172 if (ArgFlags.isByVal())
2173 return true;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002174
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002175 // Promote i8 and i16
2176 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2177 LocVT = MVT::i32;
2178 if (ArgFlags.isSExt())
2179 LocInfo = CCValAssign::SExt;
2180 else if (ArgFlags.isZExt())
2181 LocInfo = CCValAssign::ZExt;
2182 else
2183 LocInfo = CCValAssign::AExt;
2184 }
2185
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002186 unsigned Reg;
2187
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002188 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2189 // is true: function is vararg, argument is 3rd or higher, there is previous
2190 // argument which is not f32 or f64.
2191 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2192 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002193 unsigned OrigAlign = ArgFlags.getOrigAlign();
2194 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002195
2196 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002197 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002198 // If this is the first part of an i64 arg,
2199 // the allocated register must be either A0 or A2.
2200 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2201 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002202 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002203 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2204 // Allocate int register and shadow next int register. If first
2205 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002206 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2207 if (Reg == Mips::A1 || Reg == Mips::A3)
2208 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2209 State.AllocateReg(IntRegs, IntRegsSize);
2210 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002211 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2212 // we are guaranteed to find an available float register
2213 if (ValVT == MVT::f32) {
2214 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2215 // Shadow int register
2216 State.AllocateReg(IntRegs, IntRegsSize);
2217 } else {
2218 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2219 // Shadow int registers
2220 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2221 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2222 State.AllocateReg(IntRegs, IntRegsSize);
2223 State.AllocateReg(IntRegs, IntRegsSize);
2224 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002225 } else
2226 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002227
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002228 if (!Reg) {
2229 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2230 OrigAlign);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002231 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002232 } else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002233 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002234
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002235 return false;
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002236}
2237
2238#include "MipsGenCallingConv.inc"
2239
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002240//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002241// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002242//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002243
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002244static const unsigned O32IntRegsSize = 4;
2245
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002246// Return next O32 integer argument register.
2247static unsigned getNextIntArgReg(unsigned Reg) {
2248 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2249 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2250}
2251
Akira Hatanaka7d712092012-10-30 19:23:25 +00002252SDValue
2253MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002254 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka7d712092012-10-30 19:23:25 +00002255 bool IsTailCall, SelectionDAG &DAG) const {
2256 if (!IsTailCall) {
2257 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2258 DAG.getIntPtrConstant(Offset));
2259 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2260 false, 0);
2261 }
2262
2263 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2264 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2265 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2266 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2267 /*isVolatile=*/ true, false, 0);
2268}
2269
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002270void MipsTargetLowering::
2271getOpndList(SmallVectorImpl<SDValue> &Ops,
2272 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2273 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2274 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
2275 // Insert node "GP copy globalreg" before call to function.
2276 //
2277 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2278 // in PIC mode) allow symbols to be resolved via lazy binding.
2279 // The lazy binding stub requires GP to point to the GOT.
2280 if (IsPICCall && !InternalLinkage) {
2281 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
2282 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2283 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2284 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002285
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002286 // Build a sequence of copy-to-reg nodes chained together with token
2287 // chain and flag operands which copy the outgoing args into registers.
2288 // The InFlag in necessary since all emitted instructions must be
2289 // stuck together.
2290 SDValue InFlag;
Reed Kotler8453b3f2013-01-24 04:24:02 +00002291
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002292 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2293 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2294 RegsToPass[i].second, InFlag);
2295 InFlag = Chain.getValue(1);
2296 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002297
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002298 // Add argument registers to the end of the list so that they are
2299 // known live into the call.
2300 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2301 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2302 RegsToPass[i].second.getValueType()));
Reed Kotler8453b3f2013-01-24 04:24:02 +00002303
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002304 // Add a register mask operand representing the call-preserved registers.
2305 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2306 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2307 assert(Mask && "Missing call preserved mask for calling convention");
Reed Kotler46090912013-05-10 22:25:39 +00002308 if (Subtarget->inMips16HardFloat()) {
2309 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2310 llvm::StringRef Sym = G->getGlobal()->getName();
2311 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
2312 if (F->hasFnAttribute("__Mips16RetHelper")) {
2313 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2314 }
2315 }
2316 }
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002317 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2318
2319 if (InFlag.getNode())
2320 Ops.push_back(InFlag);
Reed Kotler8453b3f2013-01-24 04:24:02 +00002321}
2322
Dan Gohman98ca4f22009-08-05 01:29:28 +00002323/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002324/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002325SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002326MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002327 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002328 SelectionDAG &DAG = CLI.DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002329 SDLoc DL = CLI.DL;
Craig Toppera0ec3f92013-07-14 04:42:23 +00002330 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2331 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2332 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002333 SDValue Chain = CLI.Chain;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002334 SDValue Callee = CLI.Callee;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002335 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002336 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002337 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002338
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002339 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002340 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002341 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002342 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002343
2344 // Analyze operands of the call, assigning locations to each operand.
2345 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002346 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002347 getTargetMachine(), ArgLocs, *DAG.getContext());
Reed Kotler46090912013-05-10 22:25:39 +00002348 MipsCC::SpecialCallingConvType SpecialCallingConv =
2349 getSpecialCallingConv(Callee);
2350 MipsCC MipsCCInfo(CallConv, IsO32, CCInfo, SpecialCallingConv);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002351
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002352 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002353 getTargetMachine().Options.UseSoftFloat,
2354 Callee.getNode(), CLI.Args);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002355
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002356 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002357 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002358
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002359 // Check if it's really possible to do a tail call.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002360 if (IsTailCall)
2361 IsTailCall =
2362 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002363 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002364
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002365 if (IsTailCall)
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002366 ++NumTailCalls;
2367
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002368 // Chain is the output chain of the last Load/Store or CopyToReg node.
2369 // ByValChain is the output chain of the last Memcpy node created for copying
2370 // byval arguments to the stack.
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002371 unsigned StackAlignment = TFL->getStackAlignment();
2372 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002373 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002374
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002375 if (!IsTailCall)
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002376 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002377
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002378 SDValue StackPtr = DAG.getCopyFromReg(Chain, DL,
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002379 IsN64 ? Mips::SP_64 : Mips::SP,
2380 getPointerTy());
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002381
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002382 // With EABI is it possible to have 16 args on registers.
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002383 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman475871a2008-07-27 21:46:04 +00002384 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002385 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002386
2387 // Walk the register/memloc assignments, inserting copies/loads.
2388 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002389 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002390 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002391 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002392 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2393
2394 // ByVal Arg.
2395 if (Flags.isByVal()) {
2396 assert(Flags.getByValSize() &&
2397 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002398 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002399 assert(!IsTailCall &&
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002400 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002401 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002402 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
2403 ++ByValArg;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002404 continue;
2405 }
Jia Liubb481f82012-02-28 07:46:26 +00002406
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002407 // Promote the value if needed.
2408 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002409 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002410 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002411 if (VA.isRegLoc()) {
2412 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002413 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2414 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002415 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002416 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002417 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002418 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002419 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002420 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002421 if (!Subtarget->isLittle())
2422 std::swap(Lo, Hi);
Jia Liubb481f82012-02-28 07:46:26 +00002423 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002424 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2425 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2426 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002427 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002428 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002429 }
2430 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002431 case CCValAssign::SExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002432 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002433 break;
2434 case CCValAssign::ZExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002435 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002436 break;
2437 case CCValAssign::AExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002438 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002439 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002440 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002441
2442 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002443 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002444 if (VA.isRegLoc()) {
2445 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002446 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002447 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002448
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002449 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002450 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002451
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002452 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002453 // parameter value to a stack Location
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002454 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002455 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002456 }
2457
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002458 // Transform all store nodes into one single node because all store
2459 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002460 if (!MemOpChains.empty())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002461 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002462 &MemOpChains[0], MemOpChains.size());
2463
Bill Wendling056292f2008-09-16 21:48:12 +00002464 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002465 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2466 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002467 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakaed185da2012-12-13 03:17:29 +00002468 bool GlobalOrExternal = false, InternalLinkage = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002469 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002470
2471 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002472 if (IsPICCall) {
Akira Hatanakaed185da2012-12-13 03:17:29 +00002473 InternalLinkage = G->getGlobal()->hasInternalLinkage();
2474
2475 if (InternalLinkage)
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002476 Callee = getAddrLocal(Callee, DAG, HasMips64);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002477 else if (LargeGOT)
2478 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
2479 MipsII::MO_CALL_LO16);
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002480 else
2481 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
2482 } else
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002483 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002484 MipsII::MO_NO_FLAG);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002485 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002486 }
2487 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002488 if (!IsN64 && !IsPIC) // !N64 && static
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002489 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2490 MipsII::MO_NO_FLAG);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002491 else if (LargeGOT)
2492 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
2493 MipsII::MO_CALL_LO16);
Akira Hatanaka60689322013-02-22 21:10:03 +00002494 else // N64 || PIC
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002495 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
2496
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002497 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002498 }
2499
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002500 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002501 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002502
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002503 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2504 CLI, Callee, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002505
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002506 if (IsTailCall)
2507 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, &Ops[0], Ops.size());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002508
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002509 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, &Ops[0], Ops.size());
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002510 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002511
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002512 // Create the CALLSEQ_END node.
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002513 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002514 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002515 InFlag = Chain.getValue(1);
2516
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002517 // Handle result values, copying them out of physregs into vregs that we
2518 // return.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002519 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
2520 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002521}
2522
Dan Gohman98ca4f22009-08-05 01:29:28 +00002523/// LowerCallResult - Lower the result values of a call into the
2524/// appropriate copies out of appropriate physical registers.
2525SDValue
2526MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002527 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002528 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002529 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002530 SmallVectorImpl<SDValue> &InVals,
2531 const SDNode *CallNode,
2532 const Type *RetTy) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002533 // Assign locations to each value returned by this call.
2534 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002535 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka864f6602012-06-14 21:10:56 +00002536 getTargetMachine(), RVLocs, *DAG.getContext());
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002537 MipsCC MipsCCInfo(CallConv, IsO32, CCInfo);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002538
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002539 MipsCCInfo.analyzeCallResult(Ins, getTargetMachine().Options.UseSoftFloat,
2540 CallNode, RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002541
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002542 // Copy all of the result registers out of their specified physreg.
2543 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002544 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002545 RVLocs[i].getLocVT(), InFlag);
2546 Chain = Val.getValue(1);
2547 InFlag = Val.getValue(2);
2548
2549 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002550 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002551
2552 InVals.push_back(Val);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002553 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002554
Dan Gohman98ca4f22009-08-05 01:29:28 +00002555 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002556}
2557
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002558//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002559// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002560//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002561/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002562/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002563SDValue
2564MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002565 CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002566 bool IsVarArg,
Akira Hatanaka82099682011-12-19 19:52:25 +00002567 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002568 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002569 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002570 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002571 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002572 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002573 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002574
Dan Gohman1e93df62010-04-17 14:41:14 +00002575 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002576
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002577 // Used with vargs to acumulate store chains.
2578 std::vector<SDValue> OutChains;
2579
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002580 // Assign locations to all of the incoming arguments.
2581 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002582 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002583 getTargetMachine(), ArgLocs, *DAG.getContext());
Akira Hatanakaffd28a42013-02-15 21:45:11 +00002584 MipsCC MipsCCInfo(CallConv, IsO32, CCInfo);
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002585 Function::const_arg_iterator FuncArg =
2586 DAG.getMachineFunction().getFunction()->arg_begin();
2587 bool UseSoftFloat = getTargetMachine().Options.UseSoftFloat;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002588
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002589 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
Akira Hatanakab33b34a2012-10-30 19:37:25 +00002590 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2591 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002592
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002593 unsigned CurArgIdx = 0;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002594 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002595
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002596 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002597 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002598 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2599 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002600 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002601 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2602 bool IsRegLoc = VA.isRegLoc();
2603
2604 if (Flags.isByVal()) {
2605 assert(Flags.getByValSize() &&
2606 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002607 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002608 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002609 MipsCCInfo, *ByValArg);
2610 ++ByValArg;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002611 continue;
2612 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002613
2614 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002615 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002616 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002617 unsigned ArgReg = VA.getLocReg();
Craig Topper44d23822012-02-22 05:59:10 +00002618 const TargetRegisterClass *RC;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002619
Owen Anderson825b72b2009-08-11 20:47:22 +00002620 if (RegVT == MVT::i32)
Reed Kotlerbacbf1c2012-12-20 06:06:35 +00002621 RC = Subtarget->inMips16Mode()? &Mips::CPU16RegsRegClass :
Akira Hatanaka18587862013-08-06 23:08:38 +00002622 &Mips::GPR32RegClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002623 else if (RegVT == MVT::i64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002624 RC = &Mips::GPR64RegClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002625 else if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002626 RC = &Mips::FGR32RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002627 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002628 RC = HasMips64 ? &Mips::FGR64RegClass : &Mips::AFGR64RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002629 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002630 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002631
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002632 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002633 // physical registers into virtual ones
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002634 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2635 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002636
2637 // If this is an 8 or 16-bit value, it has been passed promoted
2638 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002639 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002640 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002641 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002642 if (VA.getLocInfo() == CCValAssign::SExt)
2643 Opcode = ISD::AssertSext;
2644 else if (VA.getLocInfo() == CCValAssign::ZExt)
2645 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002646 if (Opcode)
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002647 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002648 DAG.getValueType(ValVT));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002649 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002650 }
2651
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002652 // Handle floating point arguments passed in integer registers and
2653 // long double arguments passed in floating point registers.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002654 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002655 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2656 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002657 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002658 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002659 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002660 getNextIntArgReg(ArgReg), RC);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002661 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002662 if (!Subtarget->isLittle())
2663 std::swap(ArgValue, ArgValue2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002664 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002665 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002666 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002667
Dan Gohman98ca4f22009-08-05 01:29:28 +00002668 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002669 } else { // VA.isRegLoc()
2670
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002671 // sanity check
2672 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002673
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002674 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002675 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002676 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002677
2678 // Create load nodes to retrieve arguments from the stack
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002679 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002680 InVals.push_back(DAG.getLoad(ValVT, DL, Chain, FIN,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002681 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002682 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002683 }
2684 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002685
2686 // The mips ABIs for returning structs by value requires that we copy
2687 // the sret argument into $v0 for the return. Save the argument into
2688 // a virtual register so that we can access it from the return points.
2689 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2690 unsigned Reg = MipsFI->getSRetReturnReg();
2691 if (!Reg) {
Akira Hatanaka30580ce2012-10-19 22:11:40 +00002692 Reg = MF.getRegInfo().
2693 createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002694 MipsFI->setSRetReturnReg(Reg);
2695 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002696 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);
2697 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002698 }
2699
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002700 if (IsVarArg)
2701 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002702
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002703 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002704 // the size of Ins and InVals. This only happens when on varg functions
2705 if (!OutChains.empty()) {
2706 OutChains.push_back(Chain);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002707 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002708 &OutChains[0], OutChains.size());
2709 }
2710
Dan Gohman98ca4f22009-08-05 01:29:28 +00002711 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002712}
2713
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002714//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002715// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002716//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002717
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002718bool
2719MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002720 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002721 const SmallVectorImpl<ISD::OutputArg> &Outs,
2722 LLVMContext &Context) const {
2723 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002724 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002725 RVLocs, Context);
2726 return CCInfo.CheckReturn(Outs, RetCC_Mips);
2727}
2728
Dan Gohman98ca4f22009-08-05 01:29:28 +00002729SDValue
2730MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002731 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002732 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002733 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002734 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002735 // CCValAssign - represent the assignment of
2736 // the return value to a location
2737 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002738 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002739
2740 // CCState - Info about the registers and stack slot.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002741 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002742 *DAG.getContext());
2743 MipsCC MipsCCInfo(CallConv, IsO32, CCInfo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002744
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002745 // Analyze return values.
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002746 MipsCCInfo.analyzeReturn(Outs, getTargetMachine().Options.UseSoftFloat,
2747 MF.getFunction()->getReturnType());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002748
Dan Gohman475871a2008-07-27 21:46:04 +00002749 SDValue Flag;
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002750 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002751
2752 // Copy the result values into the output registers.
2753 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002754 SDValue Val = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002755 CCValAssign &VA = RVLocs[i];
2756 assert(VA.isRegLoc() && "Can only return in registers!");
2757
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002758 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002759 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002760
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002761 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002762
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002763 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002764 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002765 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002766 }
2767
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002768 // The mips ABIs for returning structs by value requires that we copy
2769 // the sret argument into $v0 for the return. We saved the argument into
2770 // a virtual register in the entry block, so now we copy the value out
2771 // and into $v0.
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002772 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002773 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2774 unsigned Reg = MipsFI->getSRetReturnReg();
2775
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002776 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002777 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002778 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Akira Hatanaka2ef5bd32012-10-24 02:10:54 +00002779 unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002780
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002781 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002782 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002783 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002784 }
2785
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002786 RetOps[0] = Chain; // Update chain.
Akira Hatanaka182ef6f2012-07-10 00:19:06 +00002787
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002788 // Add the flag if we have it.
2789 if (Flag.getNode())
2790 RetOps.push_back(Flag);
2791
2792 // Return on Mips is always a "jr $ra"
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002793 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, &RetOps[0], RetOps.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002794}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002795
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002796//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002797// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002798//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002799
2800/// getConstraintType - Given a constraint letter, return the type of
2801/// constraint it is for this target.
2802MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002803getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002804{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002805 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002806 // GCC config/mips/constraints.md
2807 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002808 // 'd' : An address register. Equivalent to r
2809 // unless generating MIPS16 code.
2810 // 'y' : Equivalent to r; retained for
2811 // backwards compatibility.
Eric Christopher1d5a3922012-05-07 06:25:10 +00002812 // 'c' : A register suitable for use in an indirect
2813 // jump. This will always be $25 for -mabicalls.
Eric Christopheraf97f732012-05-07 06:25:19 +00002814 // 'l' : The lo register. 1 word storage.
2815 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002816 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002817 switch (Constraint[0]) {
2818 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002819 case 'd':
2820 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002821 case 'f':
Eric Christopher1d5a3922012-05-07 06:25:10 +00002822 case 'c':
Eric Christopher4adbefe2012-05-07 06:25:15 +00002823 case 'l':
Eric Christopheraf97f732012-05-07 06:25:19 +00002824 case 'x':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002825 return C_RegisterClass;
Jack Carter0b9675d2013-03-04 21:33:15 +00002826 case 'R':
2827 return C_Memory;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002828 }
2829 }
2830 return TargetLowering::getConstraintType(Constraint);
2831}
2832
John Thompson44ab89e2010-10-29 17:29:13 +00002833/// Examine constraint type and operand type and determine a weight value.
2834/// This object must already have been set up with the operand type
2835/// and the current alternative constraint selected.
2836TargetLowering::ConstraintWeight
2837MipsTargetLowering::getSingleConstraintMatchWeight(
2838 AsmOperandInfo &info, const char *constraint) const {
2839 ConstraintWeight weight = CW_Invalid;
2840 Value *CallOperandVal = info.CallOperandVal;
2841 // If we don't have a value, we can't do a match,
2842 // but allow it at the lowest weight.
2843 if (CallOperandVal == NULL)
2844 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002845 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002846 // Look at the constraint type.
2847 switch (*constraint) {
2848 default:
2849 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2850 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002851 case 'd':
2852 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002853 if (type->isIntegerTy())
2854 weight = CW_Register;
2855 break;
2856 case 'f':
2857 if (type->isFloatTy())
2858 weight = CW_Register;
2859 break;
Eric Christopher1d5a3922012-05-07 06:25:10 +00002860 case 'c': // $25 for indirect jumps
Eric Christopher4adbefe2012-05-07 06:25:15 +00002861 case 'l': // lo register
Eric Christopheraf97f732012-05-07 06:25:19 +00002862 case 'x': // hilo register pair
Eric Christopher1d5a3922012-05-07 06:25:10 +00002863 if (type->isIntegerTy())
2864 weight = CW_SpecificReg;
2865 break;
Eric Christopher50ab0392012-05-07 03:13:32 +00002866 case 'I': // signed 16 bit immediate
Eric Christophere5076d42012-05-07 03:13:42 +00002867 case 'J': // integer zero
Eric Christopherf49f8462012-05-07 05:46:29 +00002868 case 'K': // unsigned 16 bit immediate
Eric Christopher5ac47bb2012-05-07 05:46:37 +00002869 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christopher60cfc792012-05-07 05:46:43 +00002870 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher1ce20342012-05-07 05:46:48 +00002871 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopher54412a72012-05-07 06:25:02 +00002872 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher50ab0392012-05-07 03:13:32 +00002873 if (isa<ConstantInt>(CallOperandVal))
2874 weight = CW_Constant;
2875 break;
Jack Carter0b9675d2013-03-04 21:33:15 +00002876 case 'R':
2877 weight = CW_Memory;
2878 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002879 }
2880 return weight;
2881}
2882
Eric Christopher38d64262011-06-29 19:33:04 +00002883/// Given a register class constraint, like 'r', if this corresponds directly
2884/// to an LLVM register class, return a register of 0 and the register class
2885/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002886std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier5b3fca52013-06-22 18:37:38 +00002887getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002888{
2889 if (Constraint.size() == 1) {
2890 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002891 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2892 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002893 case 'r':
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002894 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
2895 if (Subtarget->inMips16Mode())
2896 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka18587862013-08-06 23:08:38 +00002897 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002898 }
Jack Carter10de0252012-07-02 23:35:23 +00002899 if (VT == MVT::i64 && !HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002900 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002901 if (VT == MVT::i64 && HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002902 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002903 // This will generate an error message
2904 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002905 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002906 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002907 return std::make_pair(0U, &Mips::FGR32RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002908 if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
2909 if (Subtarget->isFP64bit())
Craig Topper420761a2012-04-20 07:30:17 +00002910 return std::make_pair(0U, &Mips::FGR64RegClass);
2911 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002912 }
Eric Christopher1d5a3922012-05-07 06:25:10 +00002913 break;
2914 case 'c': // register suitable for indirect jump
2915 if (VT == MVT::i32)
Akira Hatanaka18587862013-08-06 23:08:38 +00002916 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christopher1d5a3922012-05-07 06:25:10 +00002917 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka18587862013-08-06 23:08:38 +00002918 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher4adbefe2012-05-07 06:25:15 +00002919 case 'l': // register suitable for indirect jump
2920 if (VT == MVT::i32)
Akira Hatanakac147c1b2013-04-30 23:22:09 +00002921 return std::make_pair((unsigned)Mips::LO, &Mips::LORegsRegClass);
2922 return std::make_pair((unsigned)Mips::LO64, &Mips::LORegs64RegClass);
Eric Christopheraf97f732012-05-07 06:25:19 +00002923 case 'x': // register suitable for indirect jump
2924 // Fixme: Not triggering the use of both hi and low
2925 // This will generate an error message
2926 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002927 }
2928 }
2929 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2930}
2931
Eric Christopher50ab0392012-05-07 03:13:32 +00002932/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
2933/// vector. If it is invalid, don't add anything to Ops.
2934void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
2935 std::string &Constraint,
2936 std::vector<SDValue>&Ops,
2937 SelectionDAG &DAG) const {
2938 SDValue Result(0, 0);
2939
2940 // Only support length 1 constraints for now.
2941 if (Constraint.length() > 1) return;
2942
2943 char ConstraintLetter = Constraint[0];
2944 switch (ConstraintLetter) {
2945 default: break; // This will fall through to the generic implementation
2946 case 'I': // Signed 16 bit constant
2947 // If this fails, the parent routine will give an error
2948 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
2949 EVT Type = Op.getValueType();
2950 int64_t Val = C->getSExtValue();
2951 if (isInt<16>(Val)) {
2952 Result = DAG.getTargetConstant(Val, Type);
2953 break;
2954 }
2955 }
2956 return;
Eric Christophere5076d42012-05-07 03:13:42 +00002957 case 'J': // integer zero
2958 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
2959 EVT Type = Op.getValueType();
2960 int64_t Val = C->getZExtValue();
2961 if (Val == 0) {
2962 Result = DAG.getTargetConstant(0, Type);
2963 break;
2964 }
2965 }
2966 return;
Eric Christopherf49f8462012-05-07 05:46:29 +00002967 case 'K': // unsigned 16 bit immediate
2968 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
2969 EVT Type = Op.getValueType();
2970 uint64_t Val = (uint64_t)C->getZExtValue();
2971 if (isUInt<16>(Val)) {
2972 Result = DAG.getTargetConstant(Val, Type);
2973 break;
2974 }
2975 }
2976 return;
Eric Christopher5ac47bb2012-05-07 05:46:37 +00002977 case 'L': // signed 32 bit immediate where lower 16 bits are 0
2978 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
2979 EVT Type = Op.getValueType();
2980 int64_t Val = C->getSExtValue();
2981 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
2982 Result = DAG.getTargetConstant(Val, Type);
2983 break;
2984 }
2985 }
2986 return;
Eric Christopher60cfc792012-05-07 05:46:43 +00002987 case 'N': // immediate in the range of -65535 to -1 (inclusive)
2988 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
2989 EVT Type = Op.getValueType();
2990 int64_t Val = C->getSExtValue();
2991 if ((Val >= -65535) && (Val <= -1)) {
2992 Result = DAG.getTargetConstant(Val, Type);
2993 break;
2994 }
2995 }
2996 return;
Eric Christopher1ce20342012-05-07 05:46:48 +00002997 case 'O': // signed 15 bit immediate
2998 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
2999 EVT Type = Op.getValueType();
3000 int64_t Val = C->getSExtValue();
3001 if ((isInt<15>(Val))) {
3002 Result = DAG.getTargetConstant(Val, Type);
3003 break;
3004 }
3005 }
3006 return;
Eric Christopher54412a72012-05-07 06:25:02 +00003007 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3008 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3009 EVT Type = Op.getValueType();
3010 int64_t Val = C->getSExtValue();
3011 if ((Val <= 65535) && (Val >= 1)) {
3012 Result = DAG.getTargetConstant(Val, Type);
3013 break;
3014 }
3015 }
3016 return;
Eric Christopher50ab0392012-05-07 03:13:32 +00003017 }
3018
3019 if (Result.getNode()) {
3020 Ops.push_back(Result);
3021 return;
3022 }
3023
3024 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3025}
3026
Dan Gohman6520e202008-10-18 02:06:02 +00003027bool
Akira Hatanaka94e47282012-11-17 00:25:41 +00003028MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM, Type *Ty) const {
3029 // No global is ever allowed as a base.
3030 if (AM.BaseGV)
3031 return false;
3032
3033 switch (AM.Scale) {
3034 case 0: // "r+i" or just "i", depending on HasBaseReg.
3035 break;
3036 case 1:
3037 if (!AM.HasBaseReg) // allow "r+i".
3038 break;
3039 return false; // disallow "r+r" or "r+r+i".
3040 default:
3041 return false;
3042 }
3043
3044 return true;
3045}
3046
3047bool
Dan Gohman6520e202008-10-18 02:06:02 +00003048MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3049 // The Mips target isn't yet aware of offsets.
3050 return false;
3051}
Evan Chengeb2f9692009-10-27 19:56:55 +00003052
Akira Hatanakae193b322012-06-13 19:33:32 +00003053EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003054 unsigned SrcAlign,
3055 bool IsMemset, bool ZeroMemset,
Akira Hatanakae193b322012-06-13 19:33:32 +00003056 bool MemcpyStrSrc,
3057 MachineFunction &MF) const {
3058 if (Subtarget->hasMips64())
3059 return MVT::i64;
3060
3061 return MVT::i32;
3062}
3063
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003064bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3065 if (VT != MVT::f32 && VT != MVT::f64)
3066 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00003067 if (Imm.isNegZero())
3068 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00003069 return Imm.isZero();
3070}
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003071
3072unsigned MipsTargetLowering::getJumpTableEncoding() const {
3073 if (IsN64)
3074 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liubb481f82012-02-28 07:46:26 +00003075
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003076 return TargetLowering::getJumpTableEncoding();
3077}
Akira Hatanaka7887c902012-10-26 23:56:38 +00003078
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003079/// This function returns true if CallSym is a long double emulation routine.
3080static bool isF128SoftLibCall(const char *CallSym) {
3081 const char *const LibCalls[] =
3082 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3083 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3084 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3085 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3086 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3087 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3088 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3089 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3090 "truncl"};
3091
3092 const char * const *End = LibCalls + array_lengthof(LibCalls);
3093
3094 // Check that LibCalls is sorted alphabetically.
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003095 MipsTargetLowering::LTStr Comp;
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003096
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003097#ifndef NDEBUG
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003098 for (const char * const *I = LibCalls; I < End - 1; ++I)
3099 assert(Comp(*I, *(I + 1)));
3100#endif
3101
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003102 return std::binary_search(LibCalls, End, CallSym, Comp);
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003103}
3104
3105/// This function returns true if Ty is fp128 or i128 which was originally a
3106/// fp128.
3107static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3108 if (Ty->isFP128Ty())
3109 return true;
3110
3111 const ExternalSymbolSDNode *ES =
3112 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3113
3114 // If the Ty is i128 and the function being called is a long double emulation
3115 // routine, then the original type is f128.
3116 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3117}
3118
Reed Kotler46090912013-05-10 22:25:39 +00003119MipsTargetLowering::MipsCC::SpecialCallingConvType
3120 MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
3121 MipsCC::SpecialCallingConvType SpecialCallingConv =
3122 MipsCC::NoSpecialCallingConv;;
3123 if (Subtarget->inMips16HardFloat()) {
3124 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3125 llvm::StringRef Sym = G->getGlobal()->getName();
3126 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
3127 if (F->hasFnAttribute("__Mips16RetHelper")) {
3128 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3129 }
3130 }
3131 }
3132 return SpecialCallingConv;
3133}
3134
3135MipsTargetLowering::MipsCC::MipsCC(
3136 CallingConv::ID CC, bool IsO32_, CCState &Info,
3137 MipsCC::SpecialCallingConvType SpecialCallingConv_)
3138 : CCInfo(Info), CallConv(CC), IsO32(IsO32_),
3139 SpecialCallingConv(SpecialCallingConv_){
Akira Hatanaka7887c902012-10-26 23:56:38 +00003140 // Pre-allocate reserved argument area.
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003141 CCInfo.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka7887c902012-10-26 23:56:38 +00003142}
3143
Reed Kotler46090912013-05-10 22:25:39 +00003144
Akira Hatanaka7887c902012-10-26 23:56:38 +00003145void MipsTargetLowering::MipsCC::
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003146analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003147 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
3148 std::vector<ArgListEntry> &FuncArgs) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003149 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3150 "CallingConv::Fast shouldn't be used for vararg functions.");
3151
Akira Hatanaka7887c902012-10-26 23:56:38 +00003152 unsigned NumOpnds = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003153 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003154
3155 for (unsigned I = 0; I != NumOpnds; ++I) {
3156 MVT ArgVT = Args[I].VT;
3157 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3158 bool R;
3159
3160 if (ArgFlags.isByVal()) {
3161 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3162 continue;
3163 }
3164
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003165 if (IsVarArg && !Args[I].IsFixed)
Akira Hatanaka7887c902012-10-26 23:56:38 +00003166 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003167 else {
3168 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3169 IsSoftFloat);
3170 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
3171 }
Akira Hatanaka7887c902012-10-26 23:56:38 +00003172
3173 if (R) {
3174#ifndef NDEBUG
3175 dbgs() << "Call operand #" << I << " has unhandled type "
3176 << EVT(ArgVT).getEVTString();
3177#endif
3178 llvm_unreachable(0);
3179 }
3180 }
3181}
3182
3183void MipsTargetLowering::MipsCC::
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003184analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
3185 bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
Akira Hatanaka7887c902012-10-26 23:56:38 +00003186 unsigned NumArgs = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003187 llvm::CCAssignFn *FixedFn = fixedArgFn();
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003188 unsigned CurArgIdx = 0;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003189
3190 for (unsigned I = 0; I != NumArgs; ++I) {
3191 MVT ArgVT = Args[I].VT;
3192 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003193 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3194 CurArgIdx = Args[I].OrigArgIndex;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003195
3196 if (ArgFlags.isByVal()) {
3197 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3198 continue;
3199 }
3200
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003201 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), 0, IsSoftFloat);
3202
3203 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
Akira Hatanaka7887c902012-10-26 23:56:38 +00003204 continue;
3205
3206#ifndef NDEBUG
3207 dbgs() << "Formal Arg #" << I << " has unhandled type "
3208 << EVT(ArgVT).getEVTString();
3209#endif
3210 llvm_unreachable(0);
3211 }
3212}
3213
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003214template<typename Ty>
3215void MipsTargetLowering::MipsCC::
3216analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
3217 const SDNode *CallNode, const Type *RetTy) const {
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003218 CCAssignFn *Fn;
3219
3220 if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
3221 Fn = RetCC_F128Soft;
3222 else
3223 Fn = RetCC_Mips;
3224
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003225 for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
3226 MVT VT = RetVals[I].VT;
3227 ISD::ArgFlagsTy Flags = RetVals[I].Flags;
3228 MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
3229
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003230 if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003231#ifndef NDEBUG
3232 dbgs() << "Call result #" << I << " has unhandled type "
3233 << EVT(VT).getEVTString() << '\n';
3234#endif
3235 llvm_unreachable(0);
3236 }
3237 }
3238}
3239
3240void MipsTargetLowering::MipsCC::
3241analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
3242 const SDNode *CallNode, const Type *RetTy) const {
3243 analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
3244}
3245
3246void MipsTargetLowering::MipsCC::
3247analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
3248 const Type *RetTy) const {
3249 analyzeReturn(Outs, IsSoftFloat, 0, RetTy);
3250}
3251
Akira Hatanaka7887c902012-10-26 23:56:38 +00003252void
3253MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3254 MVT LocVT,
3255 CCValAssign::LocInfo LocInfo,
3256 ISD::ArgFlagsTy ArgFlags) {
3257 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3258
3259 struct ByValArgInfo ByVal;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003260 unsigned RegSize = regSize();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003261 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3262 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3263 RegSize * 2);
3264
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003265 if (useRegsForByval())
Akira Hatanaka7887c902012-10-26 23:56:38 +00003266 allocateRegs(ByVal, ByValSize, Align);
3267
3268 // Allocate space on caller's stack.
3269 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3270 Align);
3271 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3272 LocInfo));
3273 ByValArgs.push_back(ByVal);
3274}
3275
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003276unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
3277 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
3278}
3279
3280unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
3281 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
3282}
3283
3284const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const {
3285 return IsO32 ? O32IntRegs : Mips64IntRegs;
3286}
3287
3288llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3289 if (CallConv == CallingConv::Fast)
3290 return CC_Mips_FastCC;
3291
Reed Kotler46090912013-05-10 22:25:39 +00003292 if (SpecialCallingConv == Mips16RetHelperConv)
3293 return CC_Mips16RetHelper;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003294 return IsO32 ? CC_MipsO32 : CC_MipsN;
3295}
3296
3297llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
3298 return IsO32 ? CC_MipsO32 : CC_MipsN_VarArg;
3299}
3300
3301const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const {
3302 return IsO32 ? O32IntRegs : Mips64DPRegs;
3303}
3304
Akira Hatanaka7887c902012-10-26 23:56:38 +00003305void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3306 unsigned ByValSize,
3307 unsigned Align) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003308 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
3309 const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003310 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3311 "Byval argument's size and alignment should be a multiple of"
3312 "RegSize.");
3313
3314 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3315
3316 // If Align > RegSize, the first arg register must be even.
3317 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3318 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3319 ++ByVal.FirstIdx;
3320 }
3321
3322 // Mark the registers allocated.
3323 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3324 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3325 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3326}
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003327
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003328MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3329 const SDNode *CallNode,
3330 bool IsSoftFloat) const {
3331 if (IsSoftFloat || IsO32)
3332 return VT;
3333
3334 // Check if the original type was fp128.
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003335 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003336 assert(VT == MVT::i64);
3337 return MVT::f64;
3338 }
3339
3340 return VT;
3341}
3342
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003343void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003344copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003345 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3346 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3347 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3348 MachineFunction &MF = DAG.getMachineFunction();
3349 MachineFrameInfo *MFI = MF.getFrameInfo();
3350 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
3351 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3352 int FrameObjOffset;
3353
3354 if (RegAreaSize)
3355 FrameObjOffset = (int)CC.reservedArgArea() -
3356 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
3357 else
3358 FrameObjOffset = ByVal.Address;
3359
3360 // Create frame object.
3361 EVT PtrTy = getPointerTy();
3362 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3363 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3364 InVals.push_back(FIN);
3365
3366 if (!ByVal.NumRegs)
3367 return;
3368
3369 // Copy arg registers.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003370 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003371 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3372
3373 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3374 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003375 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003376 unsigned Offset = I * CC.regSize();
3377 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3378 DAG.getConstant(Offset, PtrTy));
3379 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3380 StorePtr, MachinePointerInfo(FuncArg, Offset),
3381 false, false, 0);
3382 OutChains.push_back(Store);
3383 }
3384}
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003385
3386// Copy byVal arg to registers and stack.
3387void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003388passByValArg(SDValue Chain, SDLoc DL,
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00003389 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Craig Toppera0ec3f92013-07-14 04:42:23 +00003390 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003391 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3392 const MipsCC &CC, const ByValArgInfo &ByVal,
3393 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
3394 unsigned ByValSize = Flags.getByValSize();
3395 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
3396 unsigned RegSize = CC.regSize();
3397 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
3398 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
3399
3400 if (ByVal.NumRegs) {
3401 const uint16_t *ArgRegs = CC.intArgRegs();
3402 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
3403 unsigned I = 0;
3404
3405 // Copy words to registers.
3406 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
3407 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3408 DAG.getConstant(Offset, PtrTy));
3409 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3410 MachinePointerInfo(), false, false, false,
3411 Alignment);
3412 MemOpChains.push_back(LoadVal.getValue(1));
3413 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3414 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3415 }
3416
3417 // Return if the struct has been fully copied.
3418 if (ByValSize == Offset)
3419 return;
3420
3421 // Copy the remainder of the byval argument with sub-word loads and shifts.
3422 if (LeftoverBytes) {
3423 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
3424 "Size of the remainder should be smaller than RegSize.");
3425 SDValue Val;
3426
3427 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
3428 Offset < ByValSize; LoadSize /= 2) {
3429 unsigned RemSize = ByValSize - Offset;
3430
3431 if (RemSize < LoadSize)
3432 continue;
3433
3434 // Load subword.
3435 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3436 DAG.getConstant(Offset, PtrTy));
3437 SDValue LoadVal =
3438 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
3439 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
3440 false, false, Alignment);
3441 MemOpChains.push_back(LoadVal.getValue(1));
3442
3443 // Shift the loaded value.
3444 unsigned Shamt;
3445
3446 if (isLittle)
3447 Shamt = TotalSizeLoaded;
3448 else
3449 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
3450
3451 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3452 DAG.getConstant(Shamt, MVT::i32));
3453
3454 if (Val.getNode())
3455 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3456 else
3457 Val = Shift;
3458
3459 Offset += LoadSize;
3460 TotalSizeLoaded += LoadSize;
3461 Alignment = std::min(Alignment, LoadSize);
3462 }
3463
3464 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3465 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3466 return;
3467 }
3468 }
3469
3470 // Copy remainder of byval arg to it with memcpy.
3471 unsigned MemCpySize = ByValSize - Offset;
3472 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3473 DAG.getConstant(Offset, PtrTy));
3474 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3475 DAG.getIntPtrConstant(ByVal.Address));
3476 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3477 DAG.getConstant(MemCpySize, PtrTy), Alignment,
3478 /*isVolatile=*/false, /*AlwaysInline=*/false,
3479 MachinePointerInfo(0), MachinePointerInfo(0));
3480 MemOpChains.push_back(Chain);
3481}
Akira Hatanakaf0848472012-10-27 00:21:13 +00003482
3483void
3484MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3485 const MipsCC &CC, SDValue Chain,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003486 SDLoc DL, SelectionDAG &DAG) const {
Akira Hatanakaf0848472012-10-27 00:21:13 +00003487 unsigned NumRegs = CC.numIntArgRegs();
3488 const uint16_t *ArgRegs = CC.intArgRegs();
3489 const CCState &CCInfo = CC.getCCInfo();
3490 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
3491 unsigned RegSize = CC.regSize();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003492 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003493 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3494 MachineFunction &MF = DAG.getMachineFunction();
3495 MachineFrameInfo *MFI = MF.getFrameInfo();
3496 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3497
3498 // Offset of the first variable argument from stack pointer.
3499 int VaArgOffset;
3500
3501 if (NumRegs == Idx)
3502 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
3503 else
3504 VaArgOffset =
3505 (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
3506
3507 // Record the frame index of the first variable argument
3508 // which is a value necessary to VASTART.
3509 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3510 MipsFI->setVarArgsFrameIndex(FI);
3511
3512 // Copy the integer registers that have not been used for argument passing
3513 // to the argument register save area. For O32, the save area is allocated
3514 // in the caller's stack frame, while for N32/64, it is allocated in the
3515 // callee's stack frame.
3516 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003517 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003518 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3519 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3520 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3521 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3522 MachinePointerInfo(), false, false, 0);
3523 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
3524 OutChains.push_back(Store);
3525 }
3526}