blob: 377236a9252fef13096441586958a0f29ff930e8 [file] [log] [blame]
Jia Liubb481f82012-02-28 07:46:26 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014#define DEBUG_TYPE "mips-lower"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000015#include "MipsISelLowering.h"
Craig Topper79aa3412012-03-17 18:46:09 +000016#include "InstPrinter/MipsInstPrinter.h"
17#include "MCTargetDesc/MipsBaseInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "MipsMachineFunction.h"
19#include "MipsSubtarget.h"
20#include "MipsTargetMachine.h"
21#include "MipsTargetObjectFile.h"
Akira Hatanaka2b861be2012-10-19 21:47:33 +000022#include "llvm/ADT/Statistic.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000023#include "llvm/CodeGen/CallingConvLower.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000028#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000029#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000030#include "llvm/IR/CallingConv.h"
31#include "llvm/IR/DerivedTypes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000032#include "llvm/IR/GlobalVariable.h"
Akira Hatanaka2b861be2012-10-19 21:47:33 +000033#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000034#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumi89593932012-04-21 15:31:45 +000036#include "llvm/Support/raw_ostream.h"
Akira Hatanakabfb07b12013-08-14 00:21:25 +000037#include <cctype>
NAKAMURA Takumi89593932012-04-21 15:31:45 +000038
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000039using namespace llvm;
40
Akira Hatanaka2b861be2012-10-19 21:47:33 +000041STATISTIC(NumTailCalls, "Number of tail calls");
42
43static cl::opt<bool>
Akira Hatanaka81784cb2012-11-21 20:21:11 +000044LargeGOT("mxgot", cl::Hidden,
45 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
46
Akira Hatanakaf8941992013-05-20 18:07:43 +000047static cl::opt<bool>
Akira Hatanaka2591b5c2013-05-21 17:17:59 +000048NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
Akira Hatanakaf8941992013-05-20 18:07:43 +000049 cl::desc("MIPS: Don't trap on integer division by zero."),
50 cl::init(false));
51
Akira Hatanakafe30a9b2012-10-27 00:29:43 +000052static const uint16_t O32IntRegs[4] = {
53 Mips::A0, Mips::A1, Mips::A2, Mips::A3
54};
55
56static const uint16_t Mips64IntRegs[8] = {
57 Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
58 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64
59};
60
61static const uint16_t Mips64DPRegs[8] = {
62 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
63 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
64};
65
Jia Liubb481f82012-02-28 07:46:26 +000066// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanakadbe9a312011-08-18 20:07:42 +000067// mask (Pos), and return true.
Jia Liubb481f82012-02-28 07:46:26 +000068// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanakaf635ef42013-03-12 00:16:36 +000069static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanakad6bc5232011-12-05 21:26:34 +000070 if (!isShiftedMask_64(I))
Akira Hatanaka854a7db2011-08-19 22:59:00 +000071 return false;
Akira Hatanakabb15e112011-08-17 02:05:42 +000072
Akira Hatanakad6bc5232011-12-05 21:26:34 +000073 Size = CountPopulation_64(I);
Michael J. Spencerc6af2432013-05-24 22:23:49 +000074 Pos = countTrailingZeros(I);
Akira Hatanakadbe9a312011-08-18 20:07:42 +000075 return true;
Akira Hatanakabb15e112011-08-17 02:05:42 +000076}
77
Akira Hatanaka5ac065a2013-03-13 00:54:29 +000078SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
Akira Hatanaka648f00c2012-02-24 22:34:47 +000079 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
80 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
81}
82
Akira Hatanaka6b28b802012-11-21 20:26:38 +000083static SDValue getTargetNode(SDValue Op, SelectionDAG &DAG, unsigned Flag) {
84 EVT Ty = Op.getValueType();
85
86 if (GlobalAddressSDNode *N = dyn_cast<GlobalAddressSDNode>(Op))
Andrew Trickac6d9be2013-05-25 02:42:55 +000087 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(Op), Ty, 0,
Akira Hatanaka6b28b802012-11-21 20:26:38 +000088 Flag);
89 if (ExternalSymbolSDNode *N = dyn_cast<ExternalSymbolSDNode>(Op))
90 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
91 if (BlockAddressSDNode *N = dyn_cast<BlockAddressSDNode>(Op))
92 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
93 if (JumpTableSDNode *N = dyn_cast<JumpTableSDNode>(Op))
94 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
95 if (ConstantPoolSDNode *N = dyn_cast<ConstantPoolSDNode>(Op))
96 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
97 N->getOffset(), Flag);
98
99 llvm_unreachable("Unexpected node type.");
100 return SDValue();
101}
102
103static SDValue getAddrNonPIC(SDValue Op, SelectionDAG &DAG) {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000104 SDLoc DL(Op);
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000105 EVT Ty = Op.getValueType();
106 SDValue Hi = getTargetNode(Op, DAG, MipsII::MO_ABS_HI);
107 SDValue Lo = getTargetNode(Op, DAG, MipsII::MO_ABS_LO);
108 return DAG.getNode(ISD::ADD, DL, Ty,
109 DAG.getNode(MipsISD::Hi, DL, Ty, Hi),
110 DAG.getNode(MipsISD::Lo, DL, Ty, Lo));
111}
112
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000113SDValue MipsTargetLowering::getAddrLocal(SDValue Op, SelectionDAG &DAG,
114 bool HasMips64) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000115 SDLoc DL(Op);
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000116 EVT Ty = Op.getValueType();
117 unsigned GOTFlag = HasMips64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000118 SDValue GOT = DAG.getNode(MipsISD::Wrapper, DL, Ty, getGlobalReg(DAG, Ty),
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000119 getTargetNode(Op, DAG, GOTFlag));
120 SDValue Load = DAG.getLoad(Ty, DL, DAG.getEntryNode(), GOT,
121 MachinePointerInfo::getGOT(), false, false, false,
122 0);
123 unsigned LoFlag = HasMips64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
124 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, Ty, getTargetNode(Op, DAG, LoFlag));
125 return DAG.getNode(ISD::ADD, DL, Ty, Load, Lo);
126}
127
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000128SDValue MipsTargetLowering::getAddrGlobal(SDValue Op, SelectionDAG &DAG,
129 unsigned Flag) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000130 SDLoc DL(Op);
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000131 EVT Ty = Op.getValueType();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000132 SDValue Tgt = DAG.getNode(MipsISD::Wrapper, DL, Ty, getGlobalReg(DAG, Ty),
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000133 getTargetNode(Op, DAG, Flag));
134 return DAG.getLoad(Ty, DL, DAG.getEntryNode(), Tgt,
135 MachinePointerInfo::getGOT(), false, false, false, 0);
136}
137
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000138SDValue MipsTargetLowering::getAddrGlobalLargeGOT(SDValue Op, SelectionDAG &DAG,
139 unsigned HiFlag,
140 unsigned LoFlag) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000141 SDLoc DL(Op);
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000142 EVT Ty = Op.getValueType();
143 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, Ty, getTargetNode(Op, DAG, HiFlag));
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000144 Hi = DAG.getNode(ISD::ADD, DL, Ty, Hi, getGlobalReg(DAG, Ty));
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000145 SDValue Wrapper = DAG.getNode(MipsISD::Wrapper, DL, Ty, Hi,
146 getTargetNode(Op, DAG, LoFlag));
147 return DAG.getLoad(Ty, DL, DAG.getEntryNode(), Wrapper,
148 MachinePointerInfo::getGOT(), false, false, false, 0);
149}
150
Chris Lattnerf0144122009-07-28 03:13:23 +0000151const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
152 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000153 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka58d1e3f2012-10-19 20:59:39 +0000154 case MipsISD::TailCall: return "MipsISD::TailCall";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000155 case MipsISD::Hi: return "MipsISD::Hi";
156 case MipsISD::Lo: return "MipsISD::Lo";
157 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000158 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000159 case MipsISD::Ret: return "MipsISD::Ret";
Akira Hatanaka544cc212013-01-30 00:26:49 +0000160 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000161 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
162 case MipsISD::FPCmp: return "MipsISD::FPCmp";
163 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
164 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000165 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakadd958922013-03-30 01:14:04 +0000166 case MipsISD::ExtractLOHI: return "MipsISD::ExtractLOHI";
167 case MipsISD::InsertLOHI: return "MipsISD::InsertLOHI";
168 case MipsISD::Mult: return "MipsISD::Mult";
169 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000170 case MipsISD::MAdd: return "MipsISD::MAdd";
171 case MipsISD::MAddu: return "MipsISD::MAddu";
172 case MipsISD::MSub: return "MipsISD::MSub";
173 case MipsISD::MSubu: return "MipsISD::MSubu";
174 case MipsISD::DivRem: return "MipsISD::DivRem";
175 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanakadd958922013-03-30 01:14:04 +0000176 case MipsISD::DivRem16: return "MipsISD::DivRem16";
177 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000178 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
179 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakabfcb83f2011-12-12 22:38:19 +0000180 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanakadb548262011-07-19 23:30:50 +0000181 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanakabb15e112011-08-17 02:05:42 +0000182 case MipsISD::Ext: return "MipsISD::Ext";
183 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab6f1dc22012-06-02 00:03:12 +0000184 case MipsISD::LWL: return "MipsISD::LWL";
185 case MipsISD::LWR: return "MipsISD::LWR";
186 case MipsISD::SWL: return "MipsISD::SWL";
187 case MipsISD::SWR: return "MipsISD::SWR";
188 case MipsISD::LDL: return "MipsISD::LDL";
189 case MipsISD::LDR: return "MipsISD::LDR";
190 case MipsISD::SDL: return "MipsISD::SDL";
191 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka6fad5e72012-09-21 23:52:47 +0000192 case MipsISD::EXTP: return "MipsISD::EXTP";
193 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
194 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
195 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
196 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
197 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
198 case MipsISD::SHILO: return "MipsISD::SHILO";
199 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
200 case MipsISD::MULT: return "MipsISD::MULT";
201 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liub3ea8802013-03-04 01:06:54 +0000202 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka6fad5e72012-09-21 23:52:47 +0000203 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
204 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
205 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka97a62bf2013-04-19 23:21:32 +0000206 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
207 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
208 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000209 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
210 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Akira Hatanaka0f843822011-06-07 18:58:42 +0000211 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000212 }
213}
214
215MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +0000216MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka8b4198d2011-09-26 21:47:02 +0000217 : TargetLowering(TM, new MipsTargetObjectFile()),
218 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +0000219 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
220 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000221 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000222 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +0000223 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000224 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000225
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000226 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
228 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
229 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000230
Eli Friedman6055a6a2009-07-17 04:07:24 +0000231 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
233 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000234
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000235 // Used by legalize types to correctly generate the setcc result.
236 // Without this, every float setcc comes with a AND/OR with the result,
237 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000238 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000239 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000240
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000241 // Mips Custom Operations
Akira Hatanakab7656a92013-03-06 21:32:03 +0000242 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000244 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
246 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
247 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
248 setOperationAction(ISD::SELECT, MVT::f32, Custom);
249 setOperationAction(ISD::SELECT, MVT::f64, Custom);
250 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +0000251 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
252 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanaka0a40c232012-03-09 23:46:03 +0000253 setOperationAction(ISD::SETCC, MVT::f32, Custom);
254 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000256 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000257 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
258 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000259 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000260
Akira Hatanakac12a6e62012-04-11 22:49:04 +0000261 if (!TM.Options.NoNaNsFPMath) {
262 setOperationAction(ISD::FABS, MVT::f32, Custom);
263 setOperationAction(ISD::FABS, MVT::f64, Custom);
264 }
265
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000266 if (HasMips64) {
267 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
268 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
269 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
270 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
271 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
272 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka7664f052012-06-02 00:04:42 +0000273 setOperationAction(ISD::LOAD, MVT::i64, Custom);
274 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000275 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000276 }
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000277
Akira Hatanakaa284acb2012-05-09 00:55:21 +0000278 if (!HasMips64) {
279 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
280 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
281 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
282 }
283
Akira Hatanakae90a3bc2012-11-07 19:10:58 +0000284 setOperationAction(ISD::ADD, MVT::i32, Custom);
285 if (HasMips64)
286 setOperationAction(ISD::ADD, MVT::i64, Custom);
287
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000288 setOperationAction(ISD::SDIV, MVT::i32, Expand);
289 setOperationAction(ISD::SREM, MVT::i32, Expand);
290 setOperationAction(ISD::UDIV, MVT::i32, Expand);
291 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000292 setOperationAction(ISD::SDIV, MVT::i64, Expand);
293 setOperationAction(ISD::SREM, MVT::i64, Expand);
294 setOperationAction(ISD::UDIV, MVT::i64, Expand);
295 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000296
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000297 // Operations not directly supported by Mips.
Tom Stellard3ef53832013-03-08 15:36:57 +0000298 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
299 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
300 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
301 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
303 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000304 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000305 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000306 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
308 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000309 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000311 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000312 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
313 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
314 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
315 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000316 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000317 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka1d165f12012-07-31 20:54:48 +0000318 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
319 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000320
Akira Hatanaka56633442011-09-20 23:53:09 +0000321 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000322 setOperationAction(ISD::ROTR, MVT::i32, Expand);
323
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000324 if (!Subtarget->hasMips64r2())
325 setOperationAction(ISD::ROTR, MVT::i64, Expand);
326
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000328 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000329 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000330 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000331 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
332 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
334 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000335 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 setOperationAction(ISD::FLOG, MVT::f32, Expand);
337 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
338 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
339 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000340 setOperationAction(ISD::FMA, MVT::f32, Expand);
341 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka21ecc2f2012-03-29 18:43:11 +0000342 setOperationAction(ISD::FREM, MVT::f32, Expand);
343 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000344
Akira Hatanaka1cc63332012-04-11 22:59:08 +0000345 if (!TM.Options.NoNaNsFPMath) {
346 setOperationAction(ISD::FNEG, MVT::f32, Expand);
347 setOperationAction(ISD::FNEG, MVT::f64, Expand);
348 }
349
Akira Hatanaka544cc212013-01-30 00:26:49 +0000350 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
351
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000352 setOperationAction(ISD::VAARG, MVT::Other, Expand);
353 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
354 setOperationAction(ISD::VAEND, MVT::Other, Expand);
355
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000356 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000357 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
358 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000359
Jia Liubb481f82012-02-28 07:46:26 +0000360 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
361 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
362 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
363 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman4db5aca2011-08-29 18:23:02 +0000364
Eli Friedman26689ac2011-08-03 21:06:02 +0000365 setInsertFencesForAtomic(true);
366
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000367 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000368 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
369 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000370 }
371
Akira Hatanakac79507a2011-12-21 00:20:27 +0000372 if (!Subtarget->hasBitCount()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000373 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Akira Hatanakac79507a2011-12-21 00:20:27 +0000374 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
375 }
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000376
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000377 if (!Subtarget->hasSwap()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000379 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
380 }
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000381
Akira Hatanaka7664f052012-06-02 00:04:42 +0000382 if (HasMips64) {
383 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
384 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
385 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
386 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
387 }
388
Akira Hatanaka97585622013-07-26 20:58:55 +0000389 setOperationAction(ISD::TRAP, MVT::Other, Legal);
390
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000391 setTargetDAGCombine(ISD::SDIVREM);
392 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanakaee8c3b02012-03-08 03:26:37 +0000393 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000394 setTargetDAGCombine(ISD::AND);
395 setTargetDAGCombine(ISD::OR);
Akira Hatanaka87827072012-06-13 20:33:18 +0000396 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000397
Akira Hatanaka5fdf5002012-03-08 01:59:33 +0000398 setMinFunctionAlignment(HasMips64 ? 3 : 2);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000399
Akira Hatanaka3f5b1072012-02-02 03:17:04 +0000400 setStackPointerRegisterToSaveRestore(IsN64 ? Mips::SP_64 : Mips::SP);
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000401
Akira Hatanaka590baca2012-02-02 03:13:40 +0000402 setExceptionPointerRegister(IsN64 ? Mips::A0_64 : Mips::A0);
403 setExceptionSelectorRegister(IsN64 ? Mips::A1_64 : Mips::A1);
Akira Hatanakae193b322012-06-13 19:33:32 +0000404
Jim Grosbach3450f802013-02-20 21:13:59 +0000405 MaxStoresPerMemcpy = 16;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000406}
407
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000408const MipsTargetLowering *MipsTargetLowering::create(MipsTargetMachine &TM) {
409 if (TM.getSubtargetImpl()->inMips16Mode())
410 return llvm::createMips16TargetLowering(TM);
Jia Liubb481f82012-02-28 07:46:26 +0000411
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000412 return llvm::createMipsSETargetLowering(TM);
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000413}
414
Matt Arsenault225ed702013-05-18 00:21:46 +0000415EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakae13f4412013-01-04 20:06:01 +0000416 if (!VT.isVector())
417 return MVT::i32;
418 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +0000419}
420
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000421static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000422 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000423 const MipsSubtarget *Subtarget) {
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000424 if (DCI.isBeforeLegalizeOps())
425 return SDValue();
426
Akira Hatanakadda4a072011-10-03 21:06:13 +0000427 EVT Ty = N->getValueType(0);
Akira Hatanakacbaf6d02013-08-14 00:47:08 +0000428 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
429 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000430 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
431 MipsISD::DivRemU16;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000432 SDLoc DL(N);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000433
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000434 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000435 N->getOperand(0), N->getOperand(1));
436 SDValue InChain = DAG.getEntryNode();
437 SDValue InGlue = DivRem;
438
439 // insert MFLO
440 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000441 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000442 InGlue);
443 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
444 InChain = CopyFromLo.getValue(1);
445 InGlue = CopyFromLo.getValue(2);
446 }
447
448 // insert MFHI
449 if (N->hasAnyUseOfValue(1)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000450 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000451 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000452 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
453 }
454
455 return SDValue();
456}
457
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000458static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000459 switch (CC) {
460 default: llvm_unreachable("Unknown fp condition code!");
461 case ISD::SETEQ:
462 case ISD::SETOEQ: return Mips::FCOND_OEQ;
463 case ISD::SETUNE: return Mips::FCOND_UNE;
464 case ISD::SETLT:
465 case ISD::SETOLT: return Mips::FCOND_OLT;
466 case ISD::SETGT:
467 case ISD::SETOGT: return Mips::FCOND_OGT;
468 case ISD::SETLE:
469 case ISD::SETOLE: return Mips::FCOND_OLE;
470 case ISD::SETGE:
471 case ISD::SETOGE: return Mips::FCOND_OGE;
472 case ISD::SETULT: return Mips::FCOND_ULT;
473 case ISD::SETULE: return Mips::FCOND_ULE;
474 case ISD::SETUGT: return Mips::FCOND_UGT;
475 case ISD::SETUGE: return Mips::FCOND_UGE;
476 case ISD::SETUO: return Mips::FCOND_UN;
477 case ISD::SETO: return Mips::FCOND_OR;
478 case ISD::SETNE:
479 case ISD::SETONE: return Mips::FCOND_ONE;
480 case ISD::SETUEQ: return Mips::FCOND_UEQ;
481 }
482}
483
484
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000485/// This function returns true if the floating point conditional branches and
486/// conditional moves which use condition code CC should be inverted.
487static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000488 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
489 return false;
490
Akira Hatanaka82099682011-12-19 19:52:25 +0000491 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
492 "Illegal Condition Code");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000493
Akira Hatanaka82099682011-12-19 19:52:25 +0000494 return true;
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000495}
496
497// Creates and returns an FPCmp node from a setcc node.
498// Returns Op if setcc is not a floating point comparison.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000499static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000500 // must be a SETCC node
501 if (Op.getOpcode() != ISD::SETCC)
502 return Op;
503
504 SDValue LHS = Op.getOperand(0);
505
506 if (!LHS.getValueType().isFloatingPoint())
507 return Op;
508
509 SDValue RHS = Op.getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000510 SDLoc DL(Op);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000511
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000512 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
513 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000514 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
515
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000516 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000517 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000518}
519
520// Creates and returns a CMovFPT/F node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000521static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000522 SDValue False, SDLoc DL) {
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000523 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
524 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka407883b2013-07-26 20:51:20 +0000525 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000526
527 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka407883b2013-07-26 20:51:20 +0000528 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000529}
530
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000531static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000532 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000533 const MipsSubtarget *Subtarget) {
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000534 if (DCI.isBeforeLegalizeOps())
535 return SDValue();
536
537 SDValue SetCC = N->getOperand(0);
538
539 if ((SetCC.getOpcode() != ISD::SETCC) ||
540 !SetCC.getOperand(0).getValueType().isInteger())
541 return SDValue();
542
543 SDValue False = N->getOperand(2);
544 EVT FalseTy = False.getValueType();
545
546 if (!FalseTy.isInteger())
547 return SDValue();
548
549 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(False);
550
551 if (!CN || CN->getZExtValue())
552 return SDValue();
553
Andrew Trickac6d9be2013-05-25 02:42:55 +0000554 const SDLoc DL(N);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000555 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
556 SDValue True = N->getOperand(1);
Akira Hatanaka864f6602012-06-14 21:10:56 +0000557
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000558 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
559 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
Akira Hatanaka864f6602012-06-14 21:10:56 +0000560
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000561 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
562}
563
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000564static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000565 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000566 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000567 // Pattern match EXT.
568 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
569 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000570 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000571 return SDValue();
572
573 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000574 unsigned ShiftRightOpc = ShiftRight.getOpcode();
575
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000576 // Op's first operand must be a shift right.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000577 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000578 return SDValue();
579
580 // The second operand of the shift must be an immediate.
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000581 ConstantSDNode *CN;
582 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
583 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000584
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000585 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000586 uint64_t SMPos, SMSize;
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000587
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000588 // Op's second operand must be a shifted mask.
589 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000590 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000591 return SDValue();
592
593 // Return if the shifted mask does not start at bit 0 or the sum of its size
594 // and Pos exceeds the word's size.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000595 EVT ValTy = N->getValueType(0);
596 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000597 return SDValue();
598
Andrew Trickac6d9be2013-05-25 02:42:55 +0000599 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka82099682011-12-19 19:52:25 +0000600 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000601 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000602}
Jia Liubb481f82012-02-28 07:46:26 +0000603
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000604static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000605 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000606 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000607 // Pattern match INS.
608 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liubb481f82012-02-28 07:46:26 +0000609 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000610 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000611 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000612 return SDValue();
613
614 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
615 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
616 ConstantSDNode *CN;
617
618 // See if Op's first operand matches (and $src1 , mask0).
619 if (And0.getOpcode() != ISD::AND)
620 return SDValue();
621
622 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000623 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000624 return SDValue();
625
626 // See if Op's second operand matches (and (shl $src, pos), mask1).
627 if (And1.getOpcode() != ISD::AND)
628 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000629
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000630 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000631 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000632 return SDValue();
633
634 // The shift masks must have the same position and size.
635 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
636 return SDValue();
637
638 SDValue Shl = And1.getOperand(0);
639 if (Shl.getOpcode() != ISD::SHL)
640 return SDValue();
641
642 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
643 return SDValue();
644
645 unsigned Shamt = CN->getZExtValue();
646
647 // Return if the shift amount and the first bit position of mask are not the
Jia Liubb481f82012-02-28 07:46:26 +0000648 // same.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000649 EVT ValTy = N->getValueType(0);
650 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000651 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000652
Andrew Trickac6d9be2013-05-25 02:42:55 +0000653 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000654 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka82099682011-12-19 19:52:25 +0000655 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000656}
Jia Liubb481f82012-02-28 07:46:26 +0000657
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000658static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka87827072012-06-13 20:33:18 +0000659 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000660 const MipsSubtarget *Subtarget) {
Akira Hatanaka87827072012-06-13 20:33:18 +0000661 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
662
663 if (DCI.isBeforeLegalizeOps())
664 return SDValue();
665
666 SDValue Add = N->getOperand(1);
667
668 if (Add.getOpcode() != ISD::ADD)
669 return SDValue();
670
671 SDValue Lo = Add.getOperand(1);
672
673 if ((Lo.getOpcode() != MipsISD::Lo) ||
674 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
675 return SDValue();
676
677 EVT ValTy = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000678 SDLoc DL(N);
Akira Hatanaka87827072012-06-13 20:33:18 +0000679
680 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
681 Add.getOperand(0));
682 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
683}
684
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000685SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000686 const {
687 SelectionDAG &DAG = DCI.DAG;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000688 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000689
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000690 switch (Opc) {
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000691 default: break;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000692 case ISD::SDIVREM:
693 case ISD::UDIVREM:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000694 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000695 case ISD::SELECT:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000696 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000697 case ISD::AND:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000698 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000699 case ISD::OR:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000700 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka87827072012-06-13 20:33:18 +0000701 case ISD::ADD:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000702 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000703 }
704
705 return SDValue();
706}
707
Akira Hatanakab430cec2012-09-21 23:58:31 +0000708void
709MipsTargetLowering::LowerOperationWrapper(SDNode *N,
710 SmallVectorImpl<SDValue> &Results,
711 SelectionDAG &DAG) const {
712 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
713
714 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
715 Results.push_back(Res.getValue(I));
716}
717
718void
719MipsTargetLowering::ReplaceNodeResults(SDNode *N,
720 SmallVectorImpl<SDValue> &Results,
721 SelectionDAG &DAG) const {
Akira Hatanaka13ec4812013-04-30 21:17:07 +0000722 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakab430cec2012-09-21 23:58:31 +0000723}
724
Dan Gohman475871a2008-07-27 21:46:04 +0000725SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000726LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000727{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000728 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000729 {
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000730 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
731 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
732 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
733 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
734 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
735 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
736 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
737 case ISD::SELECT: return lowerSELECT(Op, DAG);
738 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
739 case ISD::SETCC: return lowerSETCC(Op, DAG);
740 case ISD::VASTART: return lowerVASTART(Op, DAG);
741 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
742 case ISD::FABS: return lowerFABS(Op, DAG);
743 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
744 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
745 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000746 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
747 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
748 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
749 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
750 case ISD::LOAD: return lowerLOAD(Op, DAG);
751 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000752 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000753 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000754 }
Dan Gohman475871a2008-07-27 21:46:04 +0000755 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000756}
757
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000758//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000759// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000760//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000761
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000762// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000763// MachineFunction as a live in value. It also creates a corresponding
764// virtual register for it.
765static unsigned
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000766addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000767{
Chris Lattner84bc5422007-12-31 04:13:23 +0000768 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
769 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000770 return VReg;
771}
772
Akira Hatanakaf8941992013-05-20 18:07:43 +0000773static MachineBasicBlock *expandPseudoDIV(MachineInstr *MI,
774 MachineBasicBlock &MBB,
775 const TargetInstrInfo &TII,
776 bool Is64Bit) {
777 if (NoZeroDivCheck)
778 return &MBB;
779
780 // Insert instruction "teq $divisor_reg, $zero, 7".
781 MachineBasicBlock::iterator I(MI);
782 MachineInstrBuilder MIB;
783 MIB = BuildMI(MBB, llvm::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
784 .addOperand(MI->getOperand(2)).addReg(Mips::ZERO).addImm(7);
785
786 // Use the 32-bit sub-register if this is a 64-bit division.
787 if (Is64Bit)
788 MIB->getOperand(0).setSubReg(Mips::sub_32);
789
790 return &MBB;
791}
792
Akira Hatanaka01f70892012-09-27 02:15:57 +0000793MachineBasicBlock *
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000794MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000795 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000796 switch (MI->getOpcode()) {
Reed Kotlerffbe4322013-02-21 04:22:38 +0000797 default:
798 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000799 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000800 case Mips::ATOMIC_LOAD_ADD_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000801 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000802 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000803 case Mips::ATOMIC_LOAD_ADD_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000804 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000805 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000806 case Mips::ATOMIC_LOAD_ADD_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000807 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000808 case Mips::ATOMIC_LOAD_ADD_I64:
809 case Mips::ATOMIC_LOAD_ADD_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000810 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000811
812 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000813 case Mips::ATOMIC_LOAD_AND_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000814 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000815 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000816 case Mips::ATOMIC_LOAD_AND_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000817 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000818 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000819 case Mips::ATOMIC_LOAD_AND_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000820 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000821 case Mips::ATOMIC_LOAD_AND_I64:
822 case Mips::ATOMIC_LOAD_AND_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000823 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000824
825 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000826 case Mips::ATOMIC_LOAD_OR_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000827 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000828 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000829 case Mips::ATOMIC_LOAD_OR_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000830 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000831 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000832 case Mips::ATOMIC_LOAD_OR_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000833 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000834 case Mips::ATOMIC_LOAD_OR_I64:
835 case Mips::ATOMIC_LOAD_OR_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000836 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000837
838 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000839 case Mips::ATOMIC_LOAD_XOR_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000840 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000841 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000842 case Mips::ATOMIC_LOAD_XOR_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000843 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000844 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000845 case Mips::ATOMIC_LOAD_XOR_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000846 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000847 case Mips::ATOMIC_LOAD_XOR_I64:
848 case Mips::ATOMIC_LOAD_XOR_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000849 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000850
851 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000852 case Mips::ATOMIC_LOAD_NAND_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000853 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000854 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000855 case Mips::ATOMIC_LOAD_NAND_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000856 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000857 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000858 case Mips::ATOMIC_LOAD_NAND_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000859 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000860 case Mips::ATOMIC_LOAD_NAND_I64:
861 case Mips::ATOMIC_LOAD_NAND_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000862 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000863
864 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000865 case Mips::ATOMIC_LOAD_SUB_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000866 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000867 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000868 case Mips::ATOMIC_LOAD_SUB_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000869 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000870 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000871 case Mips::ATOMIC_LOAD_SUB_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000872 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000873 case Mips::ATOMIC_LOAD_SUB_I64:
874 case Mips::ATOMIC_LOAD_SUB_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000875 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000876
877 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000878 case Mips::ATOMIC_SWAP_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000879 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000880 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000881 case Mips::ATOMIC_SWAP_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000882 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000883 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000884 case Mips::ATOMIC_SWAP_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000885 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000886 case Mips::ATOMIC_SWAP_I64:
887 case Mips::ATOMIC_SWAP_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000888 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000889
890 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000891 case Mips::ATOMIC_CMP_SWAP_I8_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000892 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000893 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000894 case Mips::ATOMIC_CMP_SWAP_I16_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000895 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000896 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000897 case Mips::ATOMIC_CMP_SWAP_I32_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000898 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000899 case Mips::ATOMIC_CMP_SWAP_I64:
900 case Mips::ATOMIC_CMP_SWAP_I64_P8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000901 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanakaf8941992013-05-20 18:07:43 +0000902 case Mips::PseudoSDIV:
903 case Mips::PseudoUDIV:
904 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), false);
905 case Mips::PseudoDSDIV:
906 case Mips::PseudoDUDIV:
907 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), true);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000908 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000909}
910
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000911// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
912// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
913MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000914MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000915 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000916 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000917 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000918
919 MachineFunction *MF = BB->getParent();
920 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000921 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000922 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000923 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000924 unsigned LL, SC, AND, NOR, ZERO, BEQ;
925
926 if (Size == 4) {
927 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
928 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
929 AND = Mips::AND;
930 NOR = Mips::NOR;
931 ZERO = Mips::ZERO;
932 BEQ = Mips::BEQ;
933 }
934 else {
935 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
936 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
937 AND = Mips::AND64;
938 NOR = Mips::NOR64;
939 ZERO = Mips::ZERO_64;
940 BEQ = Mips::BEQ64;
941 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000942
Akira Hatanaka4061da12011-07-19 20:11:17 +0000943 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000944 unsigned Ptr = MI->getOperand(1).getReg();
945 unsigned Incr = MI->getOperand(2).getReg();
946
Akira Hatanaka4061da12011-07-19 20:11:17 +0000947 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
948 unsigned AndRes = RegInfo.createVirtualRegister(RC);
949 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000950
951 // insert new blocks after the current block
952 const BasicBlock *LLVM_BB = BB->getBasicBlock();
953 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
954 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
955 MachineFunction::iterator It = BB;
956 ++It;
957 MF->insert(It, loopMBB);
958 MF->insert(It, exitMBB);
959
960 // Transfer the remainder of BB and its successor edges to exitMBB.
961 exitMBB->splice(exitMBB->begin(), BB,
962 llvm::next(MachineBasicBlock::iterator(MI)),
963 BB->end());
964 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
965
966 // thisMBB:
967 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000968 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000969 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000970 loopMBB->addSuccessor(loopMBB);
971 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000972
973 // loopMBB:
974 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000975 // <binop> storeval, oldval, incr
976 // sc success, storeval, 0(ptr)
977 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000978 BB = loopMBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000979 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000980 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000981 // and andres, oldval, incr
982 // nor storeval, $0, andres
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000983 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
984 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000985 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000986 // <binop> storeval, oldval, incr
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000987 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000988 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000989 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000990 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000991 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
992 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000993
994 MI->eraseFromParent(); // The instruction is gone now.
995
Akira Hatanaka939ece12011-07-19 03:42:13 +0000996 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000997}
998
999MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001000MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001001 MachineBasicBlock *BB,
1002 unsigned Size, unsigned BinOpcode,
1003 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001004 assert((Size == 1 || Size == 2) &&
1005 "Unsupported size for EmitAtomicBinaryPartial.");
1006
1007 MachineFunction *MF = BB->getParent();
1008 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1009 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1010 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001011 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001012 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1013 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001014
1015 unsigned Dest = MI->getOperand(0).getReg();
1016 unsigned Ptr = MI->getOperand(1).getReg();
1017 unsigned Incr = MI->getOperand(2).getReg();
1018
Akira Hatanaka4061da12011-07-19 20:11:17 +00001019 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1020 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001021 unsigned Mask = RegInfo.createVirtualRegister(RC);
1022 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001023 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1024 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001025 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001026 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1027 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1028 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1029 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1030 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001031 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001032 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1033 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1034 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1035 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1036 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001037
1038 // insert new blocks after the current block
1039 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1040 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001041 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001042 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1043 MachineFunction::iterator It = BB;
1044 ++It;
1045 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001046 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001047 MF->insert(It, exitMBB);
1048
1049 // Transfer the remainder of BB and its successor edges to exitMBB.
1050 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001051 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001052 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1053
Akira Hatanaka81b44112011-07-19 17:09:53 +00001054 BB->addSuccessor(loopMBB);
1055 loopMBB->addSuccessor(loopMBB);
1056 loopMBB->addSuccessor(sinkMBB);
1057 sinkMBB->addSuccessor(exitMBB);
1058
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001059 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001060 // addiu masklsb2,$0,-4 # 0xfffffffc
1061 // and alignedaddr,ptr,masklsb2
1062 // andi ptrlsb2,ptr,3
1063 // sll shiftamt,ptrlsb2,3
1064 // ori maskupper,$0,255 # 0xff
1065 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001066 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001067 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001068
1069 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001070 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001071 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001072 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001073 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001074 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001075 if (Subtarget->isLittle()) {
1076 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1077 } else {
1078 unsigned Off = RegInfo.createVirtualRegister(RC);
1079 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1080 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1081 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1082 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001083 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001084 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001085 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001086 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001087 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka51122432013-07-01 20:39:53 +00001088 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001089
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001090 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001091 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001092 // ll oldval,0(alignedaddr)
1093 // binop binopres,oldval,incr2
1094 // and newval,binopres,mask
1095 // and maskedoldval0,oldval,mask2
1096 // or storeval,maskedoldval0,newval
1097 // sc success,storeval,0(alignedaddr)
1098 // beq success,$0,loopMBB
1099
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001100 // atomic.swap
1101 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001102 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001103 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001104 // and maskedoldval0,oldval,mask2
1105 // or storeval,maskedoldval0,newval
1106 // sc success,storeval,0(alignedaddr)
1107 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001108
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001109 BB = loopMBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001110 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001111 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001112 // and andres, oldval, incr2
1113 // nor binopres, $0, andres
1114 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001115 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1116 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001117 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001118 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001119 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001120 // <binop> binopres, oldval, incr2
1121 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001122 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1123 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001124 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001125 // and newval, incr2, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001126 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001127 }
Jia Liubb481f82012-02-28 07:46:26 +00001128
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001129 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001130 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001131 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001132 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001133 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001134 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001135 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001136 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001137
Akira Hatanaka939ece12011-07-19 03:42:13 +00001138 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001139 // and maskedoldval1,oldval,mask
1140 // srl srlres,maskedoldval1,shiftamt
1141 // sll sllres,srlres,24
1142 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001143 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001144 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001145
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001146 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001147 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001148 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001149 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001150 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001151 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001152 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001153 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001154
1155 MI->eraseFromParent(); // The instruction is gone now.
1156
Akira Hatanaka939ece12011-07-19 03:42:13 +00001157 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001158}
1159
1160MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001161MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001162 MachineBasicBlock *BB,
1163 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001164 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001165
1166 MachineFunction *MF = BB->getParent();
1167 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001168 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001169 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001170 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001171 unsigned LL, SC, ZERO, BNE, BEQ;
1172
1173 if (Size == 4) {
1174 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1175 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1176 ZERO = Mips::ZERO;
1177 BNE = Mips::BNE;
1178 BEQ = Mips::BEQ;
1179 }
1180 else {
1181 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1182 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1183 ZERO = Mips::ZERO_64;
1184 BNE = Mips::BNE64;
1185 BEQ = Mips::BEQ64;
1186 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001187
1188 unsigned Dest = MI->getOperand(0).getReg();
1189 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001190 unsigned OldVal = MI->getOperand(2).getReg();
1191 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001192
Akira Hatanaka4061da12011-07-19 20:11:17 +00001193 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001194
1195 // insert new blocks after the current block
1196 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1197 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1198 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1199 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1200 MachineFunction::iterator It = BB;
1201 ++It;
1202 MF->insert(It, loop1MBB);
1203 MF->insert(It, loop2MBB);
1204 MF->insert(It, exitMBB);
1205
1206 // Transfer the remainder of BB and its successor edges to exitMBB.
1207 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001208 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001209 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1210
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001211 // thisMBB:
1212 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001213 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001214 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001215 loop1MBB->addSuccessor(exitMBB);
1216 loop1MBB->addSuccessor(loop2MBB);
1217 loop2MBB->addSuccessor(loop1MBB);
1218 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001219
1220 // loop1MBB:
1221 // ll dest, 0(ptr)
1222 // bne dest, oldval, exitMBB
1223 BB = loop1MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001224 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1225 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001226 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001227
1228 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001229 // sc success, newval, 0(ptr)
1230 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001231 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001232 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001233 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001234 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka59068062011-11-11 04:14:30 +00001235 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001236
1237 MI->eraseFromParent(); // The instruction is gone now.
1238
Akira Hatanaka939ece12011-07-19 03:42:13 +00001239 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001240}
1241
1242MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001243MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001244 MachineBasicBlock *BB,
1245 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001246 assert((Size == 1 || Size == 2) &&
1247 "Unsupported size for EmitAtomicCmpSwapPartial.");
1248
1249 MachineFunction *MF = BB->getParent();
1250 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1251 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1252 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001253 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001254 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1255 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001256
1257 unsigned Dest = MI->getOperand(0).getReg();
1258 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001259 unsigned CmpVal = MI->getOperand(2).getReg();
1260 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001261
Akira Hatanaka4061da12011-07-19 20:11:17 +00001262 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1263 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001264 unsigned Mask = RegInfo.createVirtualRegister(RC);
1265 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001266 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1267 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1268 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1269 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1270 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1271 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1272 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1273 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1274 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1275 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1276 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1277 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1278 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1279 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001280
1281 // insert new blocks after the current block
1282 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1283 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1284 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001285 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001286 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1287 MachineFunction::iterator It = BB;
1288 ++It;
1289 MF->insert(It, loop1MBB);
1290 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001291 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001292 MF->insert(It, exitMBB);
1293
1294 // Transfer the remainder of BB and its successor edges to exitMBB.
1295 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001296 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001297 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1298
Akira Hatanaka81b44112011-07-19 17:09:53 +00001299 BB->addSuccessor(loop1MBB);
1300 loop1MBB->addSuccessor(sinkMBB);
1301 loop1MBB->addSuccessor(loop2MBB);
1302 loop2MBB->addSuccessor(loop1MBB);
1303 loop2MBB->addSuccessor(sinkMBB);
1304 sinkMBB->addSuccessor(exitMBB);
1305
Akira Hatanaka70564a92011-07-19 18:14:26 +00001306 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001307 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001308 // addiu masklsb2,$0,-4 # 0xfffffffc
1309 // and alignedaddr,ptr,masklsb2
1310 // andi ptrlsb2,ptr,3
1311 // sll shiftamt,ptrlsb2,3
1312 // ori maskupper,$0,255 # 0xff
1313 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001314 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001315 // andi maskedcmpval,cmpval,255
1316 // sll shiftedcmpval,maskedcmpval,shiftamt
1317 // andi maskednewval,newval,255
1318 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001319 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001320 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001321 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001322 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001323 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001324 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001325 if (Subtarget->isLittle()) {
1326 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1327 } else {
1328 unsigned Off = RegInfo.createVirtualRegister(RC);
1329 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1330 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1331 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1332 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001333 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001334 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001335 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001336 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001337 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1338 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001339 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001340 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001341 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001342 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001343 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001344 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001345 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001346
1347 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001348 // ll oldval,0(alginedaddr)
1349 // and maskedoldval0,oldval,mask
1350 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001351 BB = loop1MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001352 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
1353 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001354 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001355 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001356 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001357
1358 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001359 // and maskedoldval1,oldval,mask2
1360 // or storeval,maskedoldval1,shiftednewval
1361 // sc success,storeval,0(alignedaddr)
1362 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001363 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001364 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001365 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001366 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001367 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001368 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001369 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001370 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001371 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001372
Akira Hatanaka939ece12011-07-19 03:42:13 +00001373 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001374 // srl srlres,maskedoldval0,shiftamt
1375 // sll sllres,srlres,24
1376 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001377 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001378 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001379
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001380 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001381 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001382 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001383 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001384 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001385 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001386
1387 MI->eraseFromParent(); // The instruction is gone now.
1388
Akira Hatanaka939ece12011-07-19 03:42:13 +00001389 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001390}
1391
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001392//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001393// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001394//===----------------------------------------------------------------------===//
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001395SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakab7656a92013-03-06 21:32:03 +00001396 SDValue Chain = Op.getOperand(0);
1397 SDValue Table = Op.getOperand(1);
1398 SDValue Index = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001399 SDLoc DL(Op);
Akira Hatanakab7656a92013-03-06 21:32:03 +00001400 EVT PTy = getPointerTy();
1401 unsigned EntrySize =
1402 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1403
1404 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1405 DAG.getConstant(EntrySize, PTy));
1406 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1407
1408 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1409 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1410 MachinePointerInfo::getJumpTable(), MemVT, false, false,
1411 0);
1412 Chain = Addr.getValue(1);
1413
1414 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || IsN64) {
1415 // For PIC, the sequence is:
1416 // BRIND(load(Jumptable + index) + RelocBase)
1417 // RelocBase can be JumpTable, GOT or some sort of global base.
1418 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1419 getPICJumpTableRelocBase(Table, DAG));
1420 }
1421
1422 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1423}
1424
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001425SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001426lowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001427{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001428 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001429 // the block to branch to if the condition is true.
1430 SDValue Chain = Op.getOperand(0);
1431 SDValue Dest = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001432 SDLoc DL(Op);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001433
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001434 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001435
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001436 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001437 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001438 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001439
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001440 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001441 Mips::CondCode CC =
1442 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanaka9cf07242013-03-30 01:16:38 +00001443 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1444 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001445 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001446 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001447 FCC0, Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001448}
1449
1450SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001451lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001452{
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001453 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001454
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001455 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001456 if (Cond.getOpcode() != MipsISD::FPCmp)
1457 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001458
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001459 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001460 SDLoc(Op));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001461}
1462
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001463SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001464lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001465{
Andrew Trickac6d9be2013-05-25 02:42:55 +00001466 SDLoc DL(Op);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001467 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault225ed702013-05-18 00:21:46 +00001468 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1469 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001470 Op.getOperand(0), Op.getOperand(1),
1471 Op.getOperand(4));
1472
1473 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1474 Op.getOperand(3));
1475}
1476
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001477SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1478 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001479
1480 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1481 "Floating point operand expected.");
1482
1483 SDValue True = DAG.getConstant(1, MVT::i32);
1484 SDValue False = DAG.getConstant(0, MVT::i32);
1485
Andrew Trickac6d9be2013-05-25 02:42:55 +00001486 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001487}
1488
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001489SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001490 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001491 // FIXME there isn't actually debug info here
Andrew Trickac6d9be2013-05-25 02:42:55 +00001492 SDLoc DL(Op);
Jia Liubb481f82012-02-28 07:46:26 +00001493 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001494
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001495 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaafc945b2012-09-12 23:27:55 +00001496 const MipsTargetObjectFile &TLOF =
1497 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001498
Chris Lattnere3736f82009-08-13 05:41:27 +00001499 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001500 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001501 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001502 MipsII::MO_GPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001503 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001504 DAG.getVTList(MVT::i32), &GA, 1);
Akira Hatanakae7338cd2012-08-22 03:18:13 +00001505 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001506 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001507 }
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001508
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001509 // %hi/%lo relocation
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001510 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001511 }
1512
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001513 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
1514 return getAddrLocal(Op, DAG, HasMips64);
1515
Akira Hatanakaf09a0372012-11-21 20:40:38 +00001516 if (LargeGOT)
1517 return getAddrGlobalLargeGOT(Op, DAG, MipsII::MO_GOT_HI16,
1518 MipsII::MO_GOT_LO16);
1519
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001520 return getAddrGlobal(Op, DAG,
1521 HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001522}
1523
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001524SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001525 SelectionDAG &DAG) const {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001526 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1527 return getAddrNonPIC(Op, DAG);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001528
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001529 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001530}
1531
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001532SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001533lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001534{
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001535 // If the relocation model is PIC, use the General Dynamic TLS Model or
1536 // Local Dynamic TLS model, otherwise use the Initial Exec or
1537 // Local Exec TLS Model.
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001538
1539 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001540 SDLoc DL(GA);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001541 const GlobalValue *GV = GA->getGlobal();
1542 EVT PtrVT = getPointerTy();
1543
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001544 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1545
1546 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg70a07c72012-06-04 14:02:08 +00001547 // General Dynamic and Local Dynamic TLS Model.
1548 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1549 : MipsII::MO_TLSGD;
1550
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001551 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1552 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1553 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001554 unsigned PtrSize = PtrVT.getSizeInBits();
1555 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1556
Benjamin Kramer5eccf672011-12-11 12:21:34 +00001557 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001558
1559 ArgListTy Args;
1560 ArgListEntry Entry;
1561 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00001562 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001563 Args.push_back(Entry);
Jia Liubb481f82012-02-28 07:46:26 +00001564
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001565 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001566 false, false, false, false, 0, CallingConv::C,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001567 /*IsTailCall=*/false, /*doesNotRet=*/false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001568 /*isReturnValueUsed=*/true,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001569 TlsGetAddr, Args, DAG, DL);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001570 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001571
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001572 SDValue Ret = CallResult.first;
1573
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001574 if (model != TLSModel::LocalDynamic)
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001575 return Ret;
1576
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001577 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001578 MipsII::MO_DTPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001579 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1580 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001581 MipsII::MO_DTPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001582 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1583 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1584 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001585 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001586
1587 SDValue Offset;
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001588 if (model == TLSModel::InitialExec) {
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001589 // Initial Exec TLS Model
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001590 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001591 MipsII::MO_GOTTPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001592 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanaka648f00c2012-02-24 22:34:47 +00001593 TGA);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001594 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001595 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001596 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001597 } else {
1598 // Local Exec TLS Model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001599 assert(model == TLSModel::LocalExec);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001600 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001601 MipsII::MO_TPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001602 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001603 MipsII::MO_TPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001604 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1605 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1606 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001607 }
1608
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001609 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1610 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001611}
1612
1613SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001614lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001615{
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001616 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1617 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001618
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001619 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001620}
1621
Dan Gohman475871a2008-07-27 21:46:04 +00001622SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001623lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001624{
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001625 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001626 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001627 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001628 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001629 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001630 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001631 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1632 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001633 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001634
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001635 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1636 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001637
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001638 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001639}
1640
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001641SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001642 MachineFunction &MF = DAG.getMachineFunction();
1643 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1644
Andrew Trickac6d9be2013-05-25 02:42:55 +00001645 SDLoc DL(Op);
Dan Gohman1e93df62010-04-17 14:41:14 +00001646 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1647 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001648
1649 // vastart just stores the address of the VarArgsFrameIndex slot into the
1650 // memory location argument.
1651 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001652 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka82099682011-12-19 19:52:25 +00001653 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001654}
Jia Liubb481f82012-02-28 07:46:26 +00001655
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001656static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001657 EVT TyX = Op.getOperand(0).getValueType();
1658 EVT TyY = Op.getOperand(1).getValueType();
1659 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1660 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001661 SDLoc DL(Op);
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001662 SDValue Res;
1663
1664 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1665 // to i32.
1666 SDValue X = (TyX == MVT::f32) ?
1667 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1668 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1669 Const1);
1670 SDValue Y = (TyY == MVT::f32) ?
1671 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1672 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1673 Const1);
1674
1675 if (HasR2) {
1676 // ext E, Y, 31, 1 ; extract bit31 of Y
1677 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1678 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1679 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1680 } else {
1681 // sll SllX, X, 1
1682 // srl SrlX, SllX, 1
1683 // srl SrlY, Y, 31
1684 // sll SllY, SrlX, 31
1685 // or Or, SrlX, SllY
1686 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1687 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1688 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1689 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1690 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1691 }
1692
1693 if (TyX == MVT::f32)
1694 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1695
1696 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1697 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1698 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001699}
1700
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001701static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001702 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1703 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1704 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1705 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001706 SDLoc DL(Op);
Eric Christopher471e4222011-06-08 23:55:35 +00001707
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001708 // Bitcast to integer nodes.
1709 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1710 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001711
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001712 if (HasR2) {
1713 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1714 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1715 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1716 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001717
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001718 if (WidthX > WidthY)
1719 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1720 else if (WidthY > WidthX)
1721 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001722
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001723 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1724 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1725 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1726 }
1727
1728 // (d)sll SllX, X, 1
1729 // (d)srl SrlX, SllX, 1
1730 // (d)srl SrlY, Y, width(Y)-1
1731 // (d)sll SllY, SrlX, width(Y)-1
1732 // or Or, SrlX, SllY
1733 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1734 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1735 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1736 DAG.getConstant(WidthY - 1, MVT::i32));
1737
1738 if (WidthX > WidthY)
1739 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1740 else if (WidthY > WidthX)
1741 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1742
1743 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1744 DAG.getConstant(WidthX - 1, MVT::i32));
1745 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1746 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001747}
1748
Akira Hatanaka82099682011-12-19 19:52:25 +00001749SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001750MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001751 if (Subtarget->hasMips64())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001752 return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001753
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001754 return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001755}
1756
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001757static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001758 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001759 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001760
1761 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1762 // to i32.
1763 SDValue X = (Op.getValueType() == MVT::f32) ?
1764 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1765 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1766 Const1);
1767
1768 // Clear MSB.
1769 if (HasR2)
1770 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
1771 DAG.getRegister(Mips::ZERO, MVT::i32),
1772 DAG.getConstant(31, MVT::i32), Const1, X);
1773 else {
1774 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1775 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1776 }
1777
1778 if (Op.getValueType() == MVT::f32)
1779 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
1780
1781 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1782 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1783 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1784}
1785
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001786static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001787 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001788 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001789
1790 // Bitcast to integer node.
1791 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
1792
1793 // Clear MSB.
1794 if (HasR2)
1795 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
1796 DAG.getRegister(Mips::ZERO_64, MVT::i64),
1797 DAG.getConstant(63, MVT::i32), Const1, X);
1798 else {
1799 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
1800 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
1801 }
1802
1803 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
1804}
1805
1806SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001807MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001808 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001809 return lowerFABS64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001810
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001811 return lowerFABS32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001812}
1813
Akira Hatanaka2e591472011-06-02 00:24:44 +00001814SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001815lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001816 // check the depth
1817 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001818 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001819
1820 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1821 MFI->setFrameAddressIsTaken(true);
1822 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001823 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001824 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001825 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001826 return FrameAddr;
1827}
1828
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001829SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001830 SelectionDAG &DAG) const {
1831 // check the depth
1832 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1833 "Return address can be determined only for current frame.");
1834
1835 MachineFunction &MF = DAG.getMachineFunction();
1836 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001837 MVT VT = Op.getSimpleValueType();
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001838 unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
1839 MFI->setReturnAddressIsTaken(true);
1840
1841 // Return RA, which contains the return address. Mark it an implicit live-in.
1842 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001843 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001844}
1845
Akira Hatanaka544cc212013-01-30 00:26:49 +00001846// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1847// generated from __builtin_eh_return (offset, handler)
1848// The effect of this is to adjust the stack pointer by "offset"
1849// and then branch to "handler".
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001850SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanaka544cc212013-01-30 00:26:49 +00001851 const {
1852 MachineFunction &MF = DAG.getMachineFunction();
1853 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1854
1855 MipsFI->setCallsEhReturn();
1856 SDValue Chain = Op.getOperand(0);
1857 SDValue Offset = Op.getOperand(1);
1858 SDValue Handler = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001859 SDLoc DL(Op);
Akira Hatanaka544cc212013-01-30 00:26:49 +00001860 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
1861
1862 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1863 // EH_RETURN nodes, so that instructions are emitted back-to-back.
1864 unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1;
1865 unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0;
1866 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1867 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1868 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1869 DAG.getRegister(OffsetReg, Ty),
1870 DAG.getRegister(AddrReg, getPointerTy()),
1871 Chain.getValue(1));
1872}
1873
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001874SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001875 SelectionDAG &DAG) const {
Eli Friedman14648462011-07-27 22:21:52 +00001876 // FIXME: Need pseudo-fence for 'singlethread' fences
1877 // FIXME: Set SType for weaker fences where supported/appropriate.
1878 unsigned SType = 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001879 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001880 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00001881 DAG.getConstant(SType, MVT::i32));
1882}
1883
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001884SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001885 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001886 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001887 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1888 SDValue Shamt = Op.getOperand(2);
1889
1890 // if shamt < 32:
1891 // lo = (shl lo, shamt)
1892 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
1893 // else:
1894 // lo = 0
1895 // hi = (shl lo, shamt[4:0])
1896 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1897 DAG.getConstant(-1, MVT::i32));
1898 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
1899 DAG.getConstant(1, MVT::i32));
1900 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
1901 Not);
1902 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
1903 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1904 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
1905 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1906 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka864f6602012-06-14 21:10:56 +00001907 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1908 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001909 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
1910
1911 SDValue Ops[2] = {Lo, Hi};
1912 return DAG.getMergeValues(Ops, 2, DL);
1913}
1914
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001915SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001916 bool IsSRA) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001917 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001918 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1919 SDValue Shamt = Op.getOperand(2);
1920
1921 // if shamt < 32:
1922 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
1923 // if isSRA:
1924 // hi = (sra hi, shamt)
1925 // else:
1926 // hi = (srl hi, shamt)
1927 // else:
1928 // if isSRA:
1929 // lo = (sra hi, shamt[4:0])
1930 // hi = (sra hi, 31)
1931 // else:
1932 // lo = (srl hi, shamt[4:0])
1933 // hi = 0
1934 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1935 DAG.getConstant(-1, MVT::i32));
1936 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
1937 DAG.getConstant(1, MVT::i32));
1938 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
1939 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
1940 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1941 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
1942 Hi, Shamt);
1943 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1944 DAG.getConstant(0x20, MVT::i32));
1945 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
1946 DAG.getConstant(31, MVT::i32));
1947 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
1948 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1949 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
1950 ShiftRightHi);
1951
1952 SDValue Ops[2] = {Lo, Hi};
1953 return DAG.getMergeValues(Ops, 2, DL);
1954}
1955
Akira Hatanakafee62c12013-04-11 19:07:14 +00001956static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001957 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001958 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001959 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001960 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001961 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001962 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1963
1964 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001965 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001966 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001967
1968 SDValue Ops[] = { Chain, Ptr, Src };
1969 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
1970 LD->getMemOperand());
1971}
1972
1973// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001974SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001975 LoadSDNode *LD = cast<LoadSDNode>(Op);
1976 EVT MemVT = LD->getMemoryVT();
1977
1978 // Return if load is aligned or if MemVT is neither i32 nor i64.
1979 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
1980 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
1981 return SDValue();
1982
1983 bool IsLittle = Subtarget->isLittle();
1984 EVT VT = Op.getValueType();
1985 ISD::LoadExtType ExtType = LD->getExtensionType();
1986 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
1987
1988 assert((VT == MVT::i32) || (VT == MVT::i64));
1989
1990 // Expand
1991 // (set dst, (i64 (load baseptr)))
1992 // to
1993 // (set tmp, (ldl (add baseptr, 7), undef))
1994 // (set dst, (ldr baseptr, tmp))
1995 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00001996 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001997 IsLittle ? 7 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00001998 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001999 IsLittle ? 0 : 7);
2000 }
2001
Akira Hatanakafee62c12013-04-11 19:07:14 +00002002 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002003 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00002004 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002005 IsLittle ? 0 : 3);
2006
2007 // Expand
2008 // (set dst, (i32 (load baseptr))) or
2009 // (set dst, (i64 (sextload baseptr))) or
2010 // (set dst, (i64 (extload baseptr)))
2011 // to
2012 // (set tmp, (lwl (add baseptr, 3), undef))
2013 // (set dst, (lwr baseptr, tmp))
2014 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2015 (ExtType == ISD::EXTLOAD))
2016 return LWR;
2017
2018 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2019
2020 // Expand
2021 // (set dst, (i64 (zextload baseptr)))
2022 // to
2023 // (set tmp0, (lwl (add baseptr, 3), undef))
2024 // (set tmp1, (lwr baseptr, tmp0))
2025 // (set tmp2, (shl tmp1, 32))
2026 // (set dst, (srl tmp2, 32))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002027 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002028 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2029 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka94ccee22012-06-04 17:46:29 +00002030 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2031 SDValue Ops[] = { SRL, LWR.getValue(1) };
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002032 return DAG.getMergeValues(Ops, 2, DL);
2033}
2034
Akira Hatanakafee62c12013-04-11 19:07:14 +00002035static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002036 SDValue Chain, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002037 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2038 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002039 SDLoc DL(SD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002040 SDVTList VTList = DAG.getVTList(MVT::Other);
2041
2042 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002043 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002044 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002045
2046 SDValue Ops[] = { Chain, Value, Ptr };
2047 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2048 SD->getMemOperand());
2049}
2050
2051// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanaka63451432013-05-16 20:45:17 +00002052static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2053 bool IsLittle) {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002054 SDValue Value = SD->getValue(), Chain = SD->getChain();
2055 EVT VT = Value.getValueType();
2056
2057 // Expand
2058 // (store val, baseptr) or
2059 // (truncstore val, baseptr)
2060 // to
2061 // (swl val, (add baseptr, 3))
2062 // (swr val, baseptr)
2063 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00002064 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002065 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00002066 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002067 }
2068
2069 assert(VT == MVT::i64);
2070
2071 // Expand
2072 // (store val, baseptr)
2073 // to
2074 // (sdl val, (add baseptr, 7))
2075 // (sdr val, baseptr)
Akira Hatanakafee62c12013-04-11 19:07:14 +00002076 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2077 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002078}
2079
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002080// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2081static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2082 SDValue Val = SD->getValue();
2083
2084 if (Val.getOpcode() != ISD::FP_TO_SINT)
2085 return SDValue();
2086
2087 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002088 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002089 Val.getOperand(0));
2090
Andrew Trickac6d9be2013-05-25 02:42:55 +00002091 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002092 SD->getPointerInfo(), SD->isVolatile(),
2093 SD->isNonTemporal(), SD->getAlignment());
2094}
2095
Akira Hatanaka63451432013-05-16 20:45:17 +00002096SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2097 StoreSDNode *SD = cast<StoreSDNode>(Op);
2098 EVT MemVT = SD->getMemoryVT();
2099
2100 // Lower unaligned integer stores.
2101 if ((SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2102 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2103 return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle());
2104
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002105 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanaka63451432013-05-16 20:45:17 +00002106}
2107
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002108SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002109 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2110 || cast<ConstantSDNode>
2111 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2112 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2113 return SDValue();
2114
2115 // The pattern
2116 // (add (frameaddr 0), (frame_to_args_offset))
2117 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2118 // (add FrameObject, 0)
2119 // where FrameObject is a fixed StackObject with offset 0 which points to
2120 // the old stack pointer.
2121 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2122 EVT ValTy = Op->getValueType(0);
2123 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2124 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002125 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002126 DAG.getConstant(0, ValTy));
2127}
2128
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002129SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2130 SelectionDAG &DAG) const {
2131 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002132 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002133 Op.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002134 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002135}
2136
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002137//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002138// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002139//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002140
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002141//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002142// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002143// Mips O32 ABI rules:
2144// ---
2145// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002146// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002147// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002148// f64 - Only passed in two aliased f32 registers if no int reg has been used
2149// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002150// not used, it must be shadowed. If only A3 is avaiable, shadow it and
2151// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002152//
2153// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002154//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002155
Duncan Sands1e96bab2010-11-04 10:49:57 +00002156static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00002157 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002158 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2159
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002160 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002161
Craig Topperc5eaae42012-03-11 07:57:25 +00002162 static const uint16_t IntRegs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002163 Mips::A0, Mips::A1, Mips::A2, Mips::A3
2164 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002165 static const uint16_t F32Regs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002166 Mips::F12, Mips::F14
2167 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002168 static const uint16_t F64Regs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002169 Mips::D6, Mips::D7
2170 };
2171
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002172 // Do not process byval args here.
2173 if (ArgFlags.isByVal())
2174 return true;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002175
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002176 // Promote i8 and i16
2177 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2178 LocVT = MVT::i32;
2179 if (ArgFlags.isSExt())
2180 LocInfo = CCValAssign::SExt;
2181 else if (ArgFlags.isZExt())
2182 LocInfo = CCValAssign::ZExt;
2183 else
2184 LocInfo = CCValAssign::AExt;
2185 }
2186
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002187 unsigned Reg;
2188
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002189 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2190 // is true: function is vararg, argument is 3rd or higher, there is previous
2191 // argument which is not f32 or f64.
2192 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2193 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002194 unsigned OrigAlign = ArgFlags.getOrigAlign();
2195 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002196
2197 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002198 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002199 // If this is the first part of an i64 arg,
2200 // the allocated register must be either A0 or A2.
2201 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2202 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002203 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002204 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2205 // Allocate int register and shadow next int register. If first
2206 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002207 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2208 if (Reg == Mips::A1 || Reg == Mips::A3)
2209 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2210 State.AllocateReg(IntRegs, IntRegsSize);
2211 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002212 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2213 // we are guaranteed to find an available float register
2214 if (ValVT == MVT::f32) {
2215 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2216 // Shadow int register
2217 State.AllocateReg(IntRegs, IntRegsSize);
2218 } else {
2219 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2220 // Shadow int registers
2221 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2222 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2223 State.AllocateReg(IntRegs, IntRegsSize);
2224 State.AllocateReg(IntRegs, IntRegsSize);
2225 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002226 } else
2227 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002228
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002229 if (!Reg) {
2230 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2231 OrigAlign);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002232 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002233 } else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002234 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002235
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002236 return false;
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002237}
2238
2239#include "MipsGenCallingConv.inc"
2240
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002241//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002242// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002243//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002244
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002245static const unsigned O32IntRegsSize = 4;
2246
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002247// Return next O32 integer argument register.
2248static unsigned getNextIntArgReg(unsigned Reg) {
2249 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2250 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2251}
2252
Akira Hatanaka7d712092012-10-30 19:23:25 +00002253SDValue
2254MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002255 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka7d712092012-10-30 19:23:25 +00002256 bool IsTailCall, SelectionDAG &DAG) const {
2257 if (!IsTailCall) {
2258 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2259 DAG.getIntPtrConstant(Offset));
2260 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2261 false, 0);
2262 }
2263
2264 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2265 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2266 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2267 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2268 /*isVolatile=*/ true, false, 0);
2269}
2270
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002271void MipsTargetLowering::
2272getOpndList(SmallVectorImpl<SDValue> &Ops,
2273 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2274 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2275 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
2276 // Insert node "GP copy globalreg" before call to function.
2277 //
2278 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2279 // in PIC mode) allow symbols to be resolved via lazy binding.
2280 // The lazy binding stub requires GP to point to the GOT.
2281 if (IsPICCall && !InternalLinkage) {
2282 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
2283 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2284 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2285 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002286
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002287 // Build a sequence of copy-to-reg nodes chained together with token
2288 // chain and flag operands which copy the outgoing args into registers.
2289 // The InFlag in necessary since all emitted instructions must be
2290 // stuck together.
2291 SDValue InFlag;
Reed Kotler8453b3f2013-01-24 04:24:02 +00002292
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002293 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2294 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2295 RegsToPass[i].second, InFlag);
2296 InFlag = Chain.getValue(1);
2297 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002298
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002299 // Add argument registers to the end of the list so that they are
2300 // known live into the call.
2301 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2302 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2303 RegsToPass[i].second.getValueType()));
Reed Kotler8453b3f2013-01-24 04:24:02 +00002304
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002305 // Add a register mask operand representing the call-preserved registers.
2306 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2307 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2308 assert(Mask && "Missing call preserved mask for calling convention");
Reed Kotler46090912013-05-10 22:25:39 +00002309 if (Subtarget->inMips16HardFloat()) {
2310 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2311 llvm::StringRef Sym = G->getGlobal()->getName();
2312 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
2313 if (F->hasFnAttribute("__Mips16RetHelper")) {
2314 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2315 }
2316 }
2317 }
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002318 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2319
2320 if (InFlag.getNode())
2321 Ops.push_back(InFlag);
Reed Kotler8453b3f2013-01-24 04:24:02 +00002322}
2323
Dan Gohman98ca4f22009-08-05 01:29:28 +00002324/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002325/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002326SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002327MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002328 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002329 SelectionDAG &DAG = CLI.DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002330 SDLoc DL = CLI.DL;
Craig Toppera0ec3f92013-07-14 04:42:23 +00002331 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2332 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2333 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002334 SDValue Chain = CLI.Chain;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002335 SDValue Callee = CLI.Callee;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002336 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002337 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002338 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002339
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002340 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002341 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002342 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002343 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002344
2345 // Analyze operands of the call, assigning locations to each operand.
2346 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002347 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002348 getTargetMachine(), ArgLocs, *DAG.getContext());
Reed Kotler46090912013-05-10 22:25:39 +00002349 MipsCC::SpecialCallingConvType SpecialCallingConv =
2350 getSpecialCallingConv(Callee);
2351 MipsCC MipsCCInfo(CallConv, IsO32, CCInfo, SpecialCallingConv);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002352
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002353 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002354 getTargetMachine().Options.UseSoftFloat,
2355 Callee.getNode(), CLI.Args);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002356
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002357 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002358 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002359
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002360 // Check if it's really possible to do a tail call.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002361 if (IsTailCall)
2362 IsTailCall =
2363 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002364 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002365
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002366 if (IsTailCall)
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002367 ++NumTailCalls;
2368
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002369 // Chain is the output chain of the last Load/Store or CopyToReg node.
2370 // ByValChain is the output chain of the last Memcpy node created for copying
2371 // byval arguments to the stack.
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002372 unsigned StackAlignment = TFL->getStackAlignment();
2373 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002374 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002375
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002376 if (!IsTailCall)
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002377 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002378
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002379 SDValue StackPtr = DAG.getCopyFromReg(Chain, DL,
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002380 IsN64 ? Mips::SP_64 : Mips::SP,
2381 getPointerTy());
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002382
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002383 // With EABI is it possible to have 16 args on registers.
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002384 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman475871a2008-07-27 21:46:04 +00002385 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002386 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002387
2388 // Walk the register/memloc assignments, inserting copies/loads.
2389 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002390 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002391 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002392 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002393 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2394
2395 // ByVal Arg.
2396 if (Flags.isByVal()) {
2397 assert(Flags.getByValSize() &&
2398 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002399 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002400 assert(!IsTailCall &&
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002401 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002402 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002403 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
2404 ++ByValArg;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002405 continue;
2406 }
Jia Liubb481f82012-02-28 07:46:26 +00002407
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002408 // Promote the value if needed.
2409 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002410 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002411 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002412 if (VA.isRegLoc()) {
2413 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002414 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2415 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002416 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002417 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002418 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002419 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002420 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002421 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002422 if (!Subtarget->isLittle())
2423 std::swap(Lo, Hi);
Jia Liubb481f82012-02-28 07:46:26 +00002424 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002425 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2426 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2427 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002428 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002429 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002430 }
2431 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002432 case CCValAssign::SExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002433 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002434 break;
2435 case CCValAssign::ZExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002436 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002437 break;
2438 case CCValAssign::AExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002439 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002440 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002441 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002442
2443 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002444 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002445 if (VA.isRegLoc()) {
2446 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002447 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002448 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002449
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002450 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002451 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002452
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002453 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002454 // parameter value to a stack Location
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002455 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002456 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002457 }
2458
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002459 // Transform all store nodes into one single node because all store
2460 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002461 if (!MemOpChains.empty())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002462 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002463 &MemOpChains[0], MemOpChains.size());
2464
Bill Wendling056292f2008-09-16 21:48:12 +00002465 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002466 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2467 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002468 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakaed185da2012-12-13 03:17:29 +00002469 bool GlobalOrExternal = false, InternalLinkage = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002470 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002471
2472 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002473 if (IsPICCall) {
Akira Hatanakaed185da2012-12-13 03:17:29 +00002474 InternalLinkage = G->getGlobal()->hasInternalLinkage();
2475
2476 if (InternalLinkage)
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002477 Callee = getAddrLocal(Callee, DAG, HasMips64);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002478 else if (LargeGOT)
2479 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
2480 MipsII::MO_CALL_LO16);
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002481 else
2482 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
2483 } else
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002484 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002485 MipsII::MO_NO_FLAG);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002486 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002487 }
2488 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002489 if (!IsN64 && !IsPIC) // !N64 && static
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002490 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2491 MipsII::MO_NO_FLAG);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002492 else if (LargeGOT)
2493 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
2494 MipsII::MO_CALL_LO16);
Akira Hatanaka60689322013-02-22 21:10:03 +00002495 else // N64 || PIC
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002496 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
2497
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002498 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002499 }
2500
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002501 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002502 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002503
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002504 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2505 CLI, Callee, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002506
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002507 if (IsTailCall)
2508 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, &Ops[0], Ops.size());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002509
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002510 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, &Ops[0], Ops.size());
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002511 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002512
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002513 // Create the CALLSEQ_END node.
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002514 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002515 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002516 InFlag = Chain.getValue(1);
2517
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002518 // Handle result values, copying them out of physregs into vregs that we
2519 // return.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002520 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
2521 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002522}
2523
Dan Gohman98ca4f22009-08-05 01:29:28 +00002524/// LowerCallResult - Lower the result values of a call into the
2525/// appropriate copies out of appropriate physical registers.
2526SDValue
2527MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002528 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002529 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002530 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002531 SmallVectorImpl<SDValue> &InVals,
2532 const SDNode *CallNode,
2533 const Type *RetTy) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002534 // Assign locations to each value returned by this call.
2535 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002536 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka864f6602012-06-14 21:10:56 +00002537 getTargetMachine(), RVLocs, *DAG.getContext());
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002538 MipsCC MipsCCInfo(CallConv, IsO32, CCInfo);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002539
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002540 MipsCCInfo.analyzeCallResult(Ins, getTargetMachine().Options.UseSoftFloat,
2541 CallNode, RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002542
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002543 // Copy all of the result registers out of their specified physreg.
2544 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002545 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002546 RVLocs[i].getLocVT(), InFlag);
2547 Chain = Val.getValue(1);
2548 InFlag = Val.getValue(2);
2549
2550 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002551 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002552
2553 InVals.push_back(Val);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002554 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002555
Dan Gohman98ca4f22009-08-05 01:29:28 +00002556 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002557}
2558
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002559//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002560// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002561//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002562/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002563/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002564SDValue
2565MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002566 CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002567 bool IsVarArg,
Akira Hatanaka82099682011-12-19 19:52:25 +00002568 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002569 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002570 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002571 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002572 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002573 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002574 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002575
Dan Gohman1e93df62010-04-17 14:41:14 +00002576 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002577
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002578 // Used with vargs to acumulate store chains.
2579 std::vector<SDValue> OutChains;
2580
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002581 // Assign locations to all of the incoming arguments.
2582 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002583 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002584 getTargetMachine(), ArgLocs, *DAG.getContext());
Akira Hatanakaffd28a42013-02-15 21:45:11 +00002585 MipsCC MipsCCInfo(CallConv, IsO32, CCInfo);
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002586 Function::const_arg_iterator FuncArg =
2587 DAG.getMachineFunction().getFunction()->arg_begin();
2588 bool UseSoftFloat = getTargetMachine().Options.UseSoftFloat;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002589
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002590 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
Akira Hatanakab33b34a2012-10-30 19:37:25 +00002591 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2592 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002593
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002594 unsigned CurArgIdx = 0;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002595 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002596
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002597 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002598 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002599 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2600 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002601 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002602 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2603 bool IsRegLoc = VA.isRegLoc();
2604
2605 if (Flags.isByVal()) {
2606 assert(Flags.getByValSize() &&
2607 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002608 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002609 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002610 MipsCCInfo, *ByValArg);
2611 ++ByValArg;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002612 continue;
2613 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002614
2615 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002616 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002617 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002618 unsigned ArgReg = VA.getLocReg();
Craig Topper44d23822012-02-22 05:59:10 +00002619 const TargetRegisterClass *RC;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002620
Owen Anderson825b72b2009-08-11 20:47:22 +00002621 if (RegVT == MVT::i32)
Reed Kotlerbacbf1c2012-12-20 06:06:35 +00002622 RC = Subtarget->inMips16Mode()? &Mips::CPU16RegsRegClass :
Akira Hatanaka18587862013-08-06 23:08:38 +00002623 &Mips::GPR32RegClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002624 else if (RegVT == MVT::i64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002625 RC = &Mips::GPR64RegClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002626 else if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002627 RC = &Mips::FGR32RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002628 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002629 RC = HasMips64 ? &Mips::FGR64RegClass : &Mips::AFGR64RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002630 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002631 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002632
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002633 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002634 // physical registers into virtual ones
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002635 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2636 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002637
2638 // If this is an 8 or 16-bit value, it has been passed promoted
2639 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002640 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002641 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002642 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002643 if (VA.getLocInfo() == CCValAssign::SExt)
2644 Opcode = ISD::AssertSext;
2645 else if (VA.getLocInfo() == CCValAssign::ZExt)
2646 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002647 if (Opcode)
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002648 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002649 DAG.getValueType(ValVT));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002650 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002651 }
2652
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002653 // Handle floating point arguments passed in integer registers and
2654 // long double arguments passed in floating point registers.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002655 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002656 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2657 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002658 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002659 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002660 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002661 getNextIntArgReg(ArgReg), RC);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002662 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002663 if (!Subtarget->isLittle())
2664 std::swap(ArgValue, ArgValue2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002665 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002666 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002667 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002668
Dan Gohman98ca4f22009-08-05 01:29:28 +00002669 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002670 } else { // VA.isRegLoc()
2671
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002672 // sanity check
2673 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002674
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002675 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002676 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002677 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002678
2679 // Create load nodes to retrieve arguments from the stack
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002680 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002681 InVals.push_back(DAG.getLoad(ValVT, DL, Chain, FIN,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002682 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002683 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002684 }
2685 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002686
2687 // The mips ABIs for returning structs by value requires that we copy
2688 // the sret argument into $v0 for the return. Save the argument into
2689 // a virtual register so that we can access it from the return points.
2690 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2691 unsigned Reg = MipsFI->getSRetReturnReg();
2692 if (!Reg) {
Akira Hatanaka30580ce2012-10-19 22:11:40 +00002693 Reg = MF.getRegInfo().
2694 createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002695 MipsFI->setSRetReturnReg(Reg);
2696 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002697 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);
2698 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002699 }
2700
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002701 if (IsVarArg)
2702 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002703
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002704 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002705 // the size of Ins and InVals. This only happens when on varg functions
2706 if (!OutChains.empty()) {
2707 OutChains.push_back(Chain);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002708 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002709 &OutChains[0], OutChains.size());
2710 }
2711
Dan Gohman98ca4f22009-08-05 01:29:28 +00002712 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002713}
2714
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002715//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002716// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002717//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002718
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002719bool
2720MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002721 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002722 const SmallVectorImpl<ISD::OutputArg> &Outs,
2723 LLVMContext &Context) const {
2724 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002725 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002726 RVLocs, Context);
2727 return CCInfo.CheckReturn(Outs, RetCC_Mips);
2728}
2729
Dan Gohman98ca4f22009-08-05 01:29:28 +00002730SDValue
2731MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002732 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002733 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002734 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002735 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002736 // CCValAssign - represent the assignment of
2737 // the return value to a location
2738 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002739 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002740
2741 // CCState - Info about the registers and stack slot.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002742 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002743 *DAG.getContext());
2744 MipsCC MipsCCInfo(CallConv, IsO32, CCInfo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002745
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002746 // Analyze return values.
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002747 MipsCCInfo.analyzeReturn(Outs, getTargetMachine().Options.UseSoftFloat,
2748 MF.getFunction()->getReturnType());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002749
Dan Gohman475871a2008-07-27 21:46:04 +00002750 SDValue Flag;
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002751 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002752
2753 // Copy the result values into the output registers.
2754 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002755 SDValue Val = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002756 CCValAssign &VA = RVLocs[i];
2757 assert(VA.isRegLoc() && "Can only return in registers!");
2758
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002759 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002760 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002761
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002762 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002763
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002764 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002765 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002766 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002767 }
2768
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002769 // The mips ABIs for returning structs by value requires that we copy
2770 // the sret argument into $v0 for the return. We saved the argument into
2771 // a virtual register in the entry block, so now we copy the value out
2772 // and into $v0.
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002773 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002774 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2775 unsigned Reg = MipsFI->getSRetReturnReg();
2776
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002777 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002778 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002779 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Akira Hatanaka2ef5bd32012-10-24 02:10:54 +00002780 unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002781
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002782 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002783 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002784 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002785 }
2786
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002787 RetOps[0] = Chain; // Update chain.
Akira Hatanaka182ef6f2012-07-10 00:19:06 +00002788
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002789 // Add the flag if we have it.
2790 if (Flag.getNode())
2791 RetOps.push_back(Flag);
2792
2793 // Return on Mips is always a "jr $ra"
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002794 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, &RetOps[0], RetOps.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002795}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002796
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002797//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002798// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002799//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002800
2801/// getConstraintType - Given a constraint letter, return the type of
2802/// constraint it is for this target.
2803MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002804getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002805{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002806 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002807 // GCC config/mips/constraints.md
2808 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002809 // 'd' : An address register. Equivalent to r
2810 // unless generating MIPS16 code.
2811 // 'y' : Equivalent to r; retained for
2812 // backwards compatibility.
Eric Christopher1d5a3922012-05-07 06:25:10 +00002813 // 'c' : A register suitable for use in an indirect
2814 // jump. This will always be $25 for -mabicalls.
Eric Christopheraf97f732012-05-07 06:25:19 +00002815 // 'l' : The lo register. 1 word storage.
2816 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002817 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002818 switch (Constraint[0]) {
2819 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002820 case 'd':
2821 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002822 case 'f':
Eric Christopher1d5a3922012-05-07 06:25:10 +00002823 case 'c':
Eric Christopher4adbefe2012-05-07 06:25:15 +00002824 case 'l':
Eric Christopheraf97f732012-05-07 06:25:19 +00002825 case 'x':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002826 return C_RegisterClass;
Jack Carter0b9675d2013-03-04 21:33:15 +00002827 case 'R':
2828 return C_Memory;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002829 }
2830 }
2831 return TargetLowering::getConstraintType(Constraint);
2832}
2833
John Thompson44ab89e2010-10-29 17:29:13 +00002834/// Examine constraint type and operand type and determine a weight value.
2835/// This object must already have been set up with the operand type
2836/// and the current alternative constraint selected.
2837TargetLowering::ConstraintWeight
2838MipsTargetLowering::getSingleConstraintMatchWeight(
2839 AsmOperandInfo &info, const char *constraint) const {
2840 ConstraintWeight weight = CW_Invalid;
2841 Value *CallOperandVal = info.CallOperandVal;
2842 // If we don't have a value, we can't do a match,
2843 // but allow it at the lowest weight.
2844 if (CallOperandVal == NULL)
2845 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002846 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002847 // Look at the constraint type.
2848 switch (*constraint) {
2849 default:
2850 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2851 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002852 case 'd':
2853 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002854 if (type->isIntegerTy())
2855 weight = CW_Register;
2856 break;
2857 case 'f':
2858 if (type->isFloatTy())
2859 weight = CW_Register;
2860 break;
Eric Christopher1d5a3922012-05-07 06:25:10 +00002861 case 'c': // $25 for indirect jumps
Eric Christopher4adbefe2012-05-07 06:25:15 +00002862 case 'l': // lo register
Eric Christopheraf97f732012-05-07 06:25:19 +00002863 case 'x': // hilo register pair
Eric Christopher1d5a3922012-05-07 06:25:10 +00002864 if (type->isIntegerTy())
2865 weight = CW_SpecificReg;
2866 break;
Eric Christopher50ab0392012-05-07 03:13:32 +00002867 case 'I': // signed 16 bit immediate
Eric Christophere5076d42012-05-07 03:13:42 +00002868 case 'J': // integer zero
Eric Christopherf49f8462012-05-07 05:46:29 +00002869 case 'K': // unsigned 16 bit immediate
Eric Christopher5ac47bb2012-05-07 05:46:37 +00002870 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christopher60cfc792012-05-07 05:46:43 +00002871 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher1ce20342012-05-07 05:46:48 +00002872 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopher54412a72012-05-07 06:25:02 +00002873 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher50ab0392012-05-07 03:13:32 +00002874 if (isa<ConstantInt>(CallOperandVal))
2875 weight = CW_Constant;
2876 break;
Jack Carter0b9675d2013-03-04 21:33:15 +00002877 case 'R':
2878 weight = CW_Memory;
2879 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002880 }
2881 return weight;
2882}
2883
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002884/// This is a helper function to parse a physical register string and split it
2885/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
2886/// that is returned indicates whether parsing was successful. The second flag
2887/// is true if the numeric part exists.
2888static std::pair<bool, bool>
2889parsePhysicalReg(const StringRef &C, std::string &Prefix,
2890 unsigned long long &Reg) {
2891 if (C.front() != '{' || C.back() != '}')
2892 return std::make_pair(false, false);
2893
2894 // Search for the first numeric character.
2895 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
2896 I = std::find_if(B, E, std::ptr_fun(isdigit));
2897
2898 Prefix.assign(B, I - B);
2899
2900 // The second flag is set to false if no numeric characters were found.
2901 if (I == E)
2902 return std::make_pair(true, false);
2903
2904 // Parse the numeric characters.
2905 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
2906 true);
2907}
2908
2909std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
2910parseRegForInlineAsmConstraint(const StringRef &C, MVT VT) const {
2911 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2912 const TargetRegisterClass *RC;
2913 std::string Prefix;
2914 unsigned long long Reg;
2915
2916 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
2917
2918 if (!R.first)
2919 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2920
2921 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
2922 // No numeric characters follow "hi" or "lo".
2923 if (R.second)
2924 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2925
2926 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002927 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002928 return std::make_pair(*(RC->begin()), RC);
2929 }
2930
2931 if (!R.second)
2932 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2933
2934 if (Prefix == "$f") { // Parse $f0-$f31.
2935 // If the size of FP registers is 64-bit or Reg is an even number, select
2936 // the 64-bit register class. Otherwise, select the 32-bit register class.
2937 if (VT == MVT::Other)
2938 VT = (Subtarget->isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
2939
2940 RC= getRegClassFor(VT);
2941
2942 if (RC == &Mips::AFGR64RegClass) {
2943 assert(Reg % 2 == 0);
2944 Reg >>= 1;
2945 }
2946 } else if (Prefix == "$fcc") { // Parse $fcc0-$fcc7.
2947 RC = TRI->getRegClass(Mips::FCCRegClassID);
2948 } else { // Parse $0-$31.
2949 assert(Prefix == "$");
2950 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
2951 }
2952
2953 assert(Reg < RC->getNumRegs());
2954 return std::make_pair(*(RC->begin() + Reg), RC);
2955}
2956
Eric Christopher38d64262011-06-29 19:33:04 +00002957/// Given a register class constraint, like 'r', if this corresponds directly
2958/// to an LLVM register class, return a register of 0 and the register class
2959/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002960std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier5b3fca52013-06-22 18:37:38 +00002961getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002962{
2963 if (Constraint.size() == 1) {
2964 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002965 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2966 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002967 case 'r':
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002968 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
2969 if (Subtarget->inMips16Mode())
2970 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka18587862013-08-06 23:08:38 +00002971 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002972 }
Jack Carter10de0252012-07-02 23:35:23 +00002973 if (VT == MVT::i64 && !HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002974 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002975 if (VT == MVT::i64 && HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002976 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002977 // This will generate an error message
2978 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002979 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002980 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002981 return std::make_pair(0U, &Mips::FGR32RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002982 if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
2983 if (Subtarget->isFP64bit())
Craig Topper420761a2012-04-20 07:30:17 +00002984 return std::make_pair(0U, &Mips::FGR64RegClass);
2985 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002986 }
Eric Christopher1d5a3922012-05-07 06:25:10 +00002987 break;
2988 case 'c': // register suitable for indirect jump
2989 if (VT == MVT::i32)
Akira Hatanaka18587862013-08-06 23:08:38 +00002990 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christopher1d5a3922012-05-07 06:25:10 +00002991 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka18587862013-08-06 23:08:38 +00002992 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher4adbefe2012-05-07 06:25:15 +00002993 case 'l': // register suitable for indirect jump
2994 if (VT == MVT::i32)
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002995 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
2996 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopheraf97f732012-05-07 06:25:19 +00002997 case 'x': // register suitable for indirect jump
2998 // Fixme: Not triggering the use of both hi and low
2999 // This will generate an error message
3000 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003001 }
3002 }
Akira Hatanakabfb07b12013-08-14 00:21:25 +00003003
3004 std::pair<unsigned, const TargetRegisterClass *> R;
3005 R = parseRegForInlineAsmConstraint(Constraint, VT);
3006
3007 if (R.second)
3008 return R;
3009
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003010 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3011}
3012
Eric Christopher50ab0392012-05-07 03:13:32 +00003013/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3014/// vector. If it is invalid, don't add anything to Ops.
3015void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3016 std::string &Constraint,
3017 std::vector<SDValue>&Ops,
3018 SelectionDAG &DAG) const {
3019 SDValue Result(0, 0);
3020
3021 // Only support length 1 constraints for now.
3022 if (Constraint.length() > 1) return;
3023
3024 char ConstraintLetter = Constraint[0];
3025 switch (ConstraintLetter) {
3026 default: break; // This will fall through to the generic implementation
3027 case 'I': // Signed 16 bit constant
3028 // If this fails, the parent routine will give an error
3029 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3030 EVT Type = Op.getValueType();
3031 int64_t Val = C->getSExtValue();
3032 if (isInt<16>(Val)) {
3033 Result = DAG.getTargetConstant(Val, Type);
3034 break;
3035 }
3036 }
3037 return;
Eric Christophere5076d42012-05-07 03:13:42 +00003038 case 'J': // integer zero
3039 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3040 EVT Type = Op.getValueType();
3041 int64_t Val = C->getZExtValue();
3042 if (Val == 0) {
3043 Result = DAG.getTargetConstant(0, Type);
3044 break;
3045 }
3046 }
3047 return;
Eric Christopherf49f8462012-05-07 05:46:29 +00003048 case 'K': // unsigned 16 bit immediate
3049 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3050 EVT Type = Op.getValueType();
3051 uint64_t Val = (uint64_t)C->getZExtValue();
3052 if (isUInt<16>(Val)) {
3053 Result = DAG.getTargetConstant(Val, Type);
3054 break;
3055 }
3056 }
3057 return;
Eric Christopher5ac47bb2012-05-07 05:46:37 +00003058 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3059 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3060 EVT Type = Op.getValueType();
3061 int64_t Val = C->getSExtValue();
3062 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3063 Result = DAG.getTargetConstant(Val, Type);
3064 break;
3065 }
3066 }
3067 return;
Eric Christopher60cfc792012-05-07 05:46:43 +00003068 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3069 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3070 EVT Type = Op.getValueType();
3071 int64_t Val = C->getSExtValue();
3072 if ((Val >= -65535) && (Val <= -1)) {
3073 Result = DAG.getTargetConstant(Val, Type);
3074 break;
3075 }
3076 }
3077 return;
Eric Christopher1ce20342012-05-07 05:46:48 +00003078 case 'O': // signed 15 bit immediate
3079 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3080 EVT Type = Op.getValueType();
3081 int64_t Val = C->getSExtValue();
3082 if ((isInt<15>(Val))) {
3083 Result = DAG.getTargetConstant(Val, Type);
3084 break;
3085 }
3086 }
3087 return;
Eric Christopher54412a72012-05-07 06:25:02 +00003088 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3089 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3090 EVT Type = Op.getValueType();
3091 int64_t Val = C->getSExtValue();
3092 if ((Val <= 65535) && (Val >= 1)) {
3093 Result = DAG.getTargetConstant(Val, Type);
3094 break;
3095 }
3096 }
3097 return;
Eric Christopher50ab0392012-05-07 03:13:32 +00003098 }
3099
3100 if (Result.getNode()) {
3101 Ops.push_back(Result);
3102 return;
3103 }
3104
3105 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3106}
3107
Dan Gohman6520e202008-10-18 02:06:02 +00003108bool
Akira Hatanaka94e47282012-11-17 00:25:41 +00003109MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM, Type *Ty) const {
3110 // No global is ever allowed as a base.
3111 if (AM.BaseGV)
3112 return false;
3113
3114 switch (AM.Scale) {
3115 case 0: // "r+i" or just "i", depending on HasBaseReg.
3116 break;
3117 case 1:
3118 if (!AM.HasBaseReg) // allow "r+i".
3119 break;
3120 return false; // disallow "r+r" or "r+r+i".
3121 default:
3122 return false;
3123 }
3124
3125 return true;
3126}
3127
3128bool
Dan Gohman6520e202008-10-18 02:06:02 +00003129MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3130 // The Mips target isn't yet aware of offsets.
3131 return false;
3132}
Evan Chengeb2f9692009-10-27 19:56:55 +00003133
Akira Hatanakae193b322012-06-13 19:33:32 +00003134EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003135 unsigned SrcAlign,
3136 bool IsMemset, bool ZeroMemset,
Akira Hatanakae193b322012-06-13 19:33:32 +00003137 bool MemcpyStrSrc,
3138 MachineFunction &MF) const {
3139 if (Subtarget->hasMips64())
3140 return MVT::i64;
3141
3142 return MVT::i32;
3143}
3144
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003145bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3146 if (VT != MVT::f32 && VT != MVT::f64)
3147 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00003148 if (Imm.isNegZero())
3149 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00003150 return Imm.isZero();
3151}
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003152
3153unsigned MipsTargetLowering::getJumpTableEncoding() const {
3154 if (IsN64)
3155 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liubb481f82012-02-28 07:46:26 +00003156
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003157 return TargetLowering::getJumpTableEncoding();
3158}
Akira Hatanaka7887c902012-10-26 23:56:38 +00003159
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003160/// This function returns true if CallSym is a long double emulation routine.
3161static bool isF128SoftLibCall(const char *CallSym) {
3162 const char *const LibCalls[] =
3163 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3164 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3165 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3166 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3167 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3168 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3169 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3170 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3171 "truncl"};
3172
3173 const char * const *End = LibCalls + array_lengthof(LibCalls);
3174
3175 // Check that LibCalls is sorted alphabetically.
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003176 MipsTargetLowering::LTStr Comp;
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003177
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003178#ifndef NDEBUG
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003179 for (const char * const *I = LibCalls; I < End - 1; ++I)
3180 assert(Comp(*I, *(I + 1)));
3181#endif
3182
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003183 return std::binary_search(LibCalls, End, CallSym, Comp);
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003184}
3185
3186/// This function returns true if Ty is fp128 or i128 which was originally a
3187/// fp128.
3188static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3189 if (Ty->isFP128Ty())
3190 return true;
3191
3192 const ExternalSymbolSDNode *ES =
3193 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3194
3195 // If the Ty is i128 and the function being called is a long double emulation
3196 // routine, then the original type is f128.
3197 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3198}
3199
Reed Kotler46090912013-05-10 22:25:39 +00003200MipsTargetLowering::MipsCC::SpecialCallingConvType
3201 MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
3202 MipsCC::SpecialCallingConvType SpecialCallingConv =
3203 MipsCC::NoSpecialCallingConv;;
3204 if (Subtarget->inMips16HardFloat()) {
3205 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3206 llvm::StringRef Sym = G->getGlobal()->getName();
3207 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
3208 if (F->hasFnAttribute("__Mips16RetHelper")) {
3209 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3210 }
3211 }
3212 }
3213 return SpecialCallingConv;
3214}
3215
3216MipsTargetLowering::MipsCC::MipsCC(
3217 CallingConv::ID CC, bool IsO32_, CCState &Info,
3218 MipsCC::SpecialCallingConvType SpecialCallingConv_)
3219 : CCInfo(Info), CallConv(CC), IsO32(IsO32_),
3220 SpecialCallingConv(SpecialCallingConv_){
Akira Hatanaka7887c902012-10-26 23:56:38 +00003221 // Pre-allocate reserved argument area.
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003222 CCInfo.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka7887c902012-10-26 23:56:38 +00003223}
3224
Reed Kotler46090912013-05-10 22:25:39 +00003225
Akira Hatanaka7887c902012-10-26 23:56:38 +00003226void MipsTargetLowering::MipsCC::
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003227analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003228 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
3229 std::vector<ArgListEntry> &FuncArgs) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003230 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3231 "CallingConv::Fast shouldn't be used for vararg functions.");
3232
Akira Hatanaka7887c902012-10-26 23:56:38 +00003233 unsigned NumOpnds = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003234 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003235
3236 for (unsigned I = 0; I != NumOpnds; ++I) {
3237 MVT ArgVT = Args[I].VT;
3238 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3239 bool R;
3240
3241 if (ArgFlags.isByVal()) {
3242 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3243 continue;
3244 }
3245
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003246 if (IsVarArg && !Args[I].IsFixed)
Akira Hatanaka7887c902012-10-26 23:56:38 +00003247 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003248 else {
3249 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3250 IsSoftFloat);
3251 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
3252 }
Akira Hatanaka7887c902012-10-26 23:56:38 +00003253
3254 if (R) {
3255#ifndef NDEBUG
3256 dbgs() << "Call operand #" << I << " has unhandled type "
3257 << EVT(ArgVT).getEVTString();
3258#endif
3259 llvm_unreachable(0);
3260 }
3261 }
3262}
3263
3264void MipsTargetLowering::MipsCC::
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003265analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
3266 bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
Akira Hatanaka7887c902012-10-26 23:56:38 +00003267 unsigned NumArgs = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003268 llvm::CCAssignFn *FixedFn = fixedArgFn();
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003269 unsigned CurArgIdx = 0;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003270
3271 for (unsigned I = 0; I != NumArgs; ++I) {
3272 MVT ArgVT = Args[I].VT;
3273 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003274 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3275 CurArgIdx = Args[I].OrigArgIndex;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003276
3277 if (ArgFlags.isByVal()) {
3278 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3279 continue;
3280 }
3281
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003282 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), 0, IsSoftFloat);
3283
3284 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
Akira Hatanaka7887c902012-10-26 23:56:38 +00003285 continue;
3286
3287#ifndef NDEBUG
3288 dbgs() << "Formal Arg #" << I << " has unhandled type "
3289 << EVT(ArgVT).getEVTString();
3290#endif
3291 llvm_unreachable(0);
3292 }
3293}
3294
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003295template<typename Ty>
3296void MipsTargetLowering::MipsCC::
3297analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
3298 const SDNode *CallNode, const Type *RetTy) const {
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003299 CCAssignFn *Fn;
3300
3301 if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
3302 Fn = RetCC_F128Soft;
3303 else
3304 Fn = RetCC_Mips;
3305
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003306 for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
3307 MVT VT = RetVals[I].VT;
3308 ISD::ArgFlagsTy Flags = RetVals[I].Flags;
3309 MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
3310
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003311 if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003312#ifndef NDEBUG
3313 dbgs() << "Call result #" << I << " has unhandled type "
3314 << EVT(VT).getEVTString() << '\n';
3315#endif
3316 llvm_unreachable(0);
3317 }
3318 }
3319}
3320
3321void MipsTargetLowering::MipsCC::
3322analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
3323 const SDNode *CallNode, const Type *RetTy) const {
3324 analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
3325}
3326
3327void MipsTargetLowering::MipsCC::
3328analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
3329 const Type *RetTy) const {
3330 analyzeReturn(Outs, IsSoftFloat, 0, RetTy);
3331}
3332
Akira Hatanaka7887c902012-10-26 23:56:38 +00003333void
3334MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3335 MVT LocVT,
3336 CCValAssign::LocInfo LocInfo,
3337 ISD::ArgFlagsTy ArgFlags) {
3338 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3339
3340 struct ByValArgInfo ByVal;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003341 unsigned RegSize = regSize();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003342 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3343 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3344 RegSize * 2);
3345
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003346 if (useRegsForByval())
Akira Hatanaka7887c902012-10-26 23:56:38 +00003347 allocateRegs(ByVal, ByValSize, Align);
3348
3349 // Allocate space on caller's stack.
3350 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3351 Align);
3352 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3353 LocInfo));
3354 ByValArgs.push_back(ByVal);
3355}
3356
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003357unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
3358 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
3359}
3360
3361unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
3362 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
3363}
3364
3365const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const {
3366 return IsO32 ? O32IntRegs : Mips64IntRegs;
3367}
3368
3369llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3370 if (CallConv == CallingConv::Fast)
3371 return CC_Mips_FastCC;
3372
Reed Kotler46090912013-05-10 22:25:39 +00003373 if (SpecialCallingConv == Mips16RetHelperConv)
3374 return CC_Mips16RetHelper;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003375 return IsO32 ? CC_MipsO32 : CC_MipsN;
3376}
3377
3378llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
3379 return IsO32 ? CC_MipsO32 : CC_MipsN_VarArg;
3380}
3381
3382const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const {
3383 return IsO32 ? O32IntRegs : Mips64DPRegs;
3384}
3385
Akira Hatanaka7887c902012-10-26 23:56:38 +00003386void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3387 unsigned ByValSize,
3388 unsigned Align) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003389 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
3390 const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003391 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3392 "Byval argument's size and alignment should be a multiple of"
3393 "RegSize.");
3394
3395 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3396
3397 // If Align > RegSize, the first arg register must be even.
3398 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3399 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3400 ++ByVal.FirstIdx;
3401 }
3402
3403 // Mark the registers allocated.
3404 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3405 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3406 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3407}
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003408
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003409MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3410 const SDNode *CallNode,
3411 bool IsSoftFloat) const {
3412 if (IsSoftFloat || IsO32)
3413 return VT;
3414
3415 // Check if the original type was fp128.
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003416 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003417 assert(VT == MVT::i64);
3418 return MVT::f64;
3419 }
3420
3421 return VT;
3422}
3423
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003424void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003425copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003426 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3427 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3428 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3429 MachineFunction &MF = DAG.getMachineFunction();
3430 MachineFrameInfo *MFI = MF.getFrameInfo();
3431 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
3432 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3433 int FrameObjOffset;
3434
3435 if (RegAreaSize)
3436 FrameObjOffset = (int)CC.reservedArgArea() -
3437 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
3438 else
3439 FrameObjOffset = ByVal.Address;
3440
3441 // Create frame object.
3442 EVT PtrTy = getPointerTy();
3443 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3444 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3445 InVals.push_back(FIN);
3446
3447 if (!ByVal.NumRegs)
3448 return;
3449
3450 // Copy arg registers.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003451 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003452 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3453
3454 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3455 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003456 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003457 unsigned Offset = I * CC.regSize();
3458 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3459 DAG.getConstant(Offset, PtrTy));
3460 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3461 StorePtr, MachinePointerInfo(FuncArg, Offset),
3462 false, false, 0);
3463 OutChains.push_back(Store);
3464 }
3465}
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003466
3467// Copy byVal arg to registers and stack.
3468void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003469passByValArg(SDValue Chain, SDLoc DL,
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00003470 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Craig Toppera0ec3f92013-07-14 04:42:23 +00003471 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003472 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3473 const MipsCC &CC, const ByValArgInfo &ByVal,
3474 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
3475 unsigned ByValSize = Flags.getByValSize();
3476 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
3477 unsigned RegSize = CC.regSize();
3478 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
3479 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
3480
3481 if (ByVal.NumRegs) {
3482 const uint16_t *ArgRegs = CC.intArgRegs();
3483 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
3484 unsigned I = 0;
3485
3486 // Copy words to registers.
3487 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
3488 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3489 DAG.getConstant(Offset, PtrTy));
3490 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3491 MachinePointerInfo(), false, false, false,
3492 Alignment);
3493 MemOpChains.push_back(LoadVal.getValue(1));
3494 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3495 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3496 }
3497
3498 // Return if the struct has been fully copied.
3499 if (ByValSize == Offset)
3500 return;
3501
3502 // Copy the remainder of the byval argument with sub-word loads and shifts.
3503 if (LeftoverBytes) {
3504 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
3505 "Size of the remainder should be smaller than RegSize.");
3506 SDValue Val;
3507
3508 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
3509 Offset < ByValSize; LoadSize /= 2) {
3510 unsigned RemSize = ByValSize - Offset;
3511
3512 if (RemSize < LoadSize)
3513 continue;
3514
3515 // Load subword.
3516 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3517 DAG.getConstant(Offset, PtrTy));
3518 SDValue LoadVal =
3519 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
3520 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
3521 false, false, Alignment);
3522 MemOpChains.push_back(LoadVal.getValue(1));
3523
3524 // Shift the loaded value.
3525 unsigned Shamt;
3526
3527 if (isLittle)
3528 Shamt = TotalSizeLoaded;
3529 else
3530 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
3531
3532 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3533 DAG.getConstant(Shamt, MVT::i32));
3534
3535 if (Val.getNode())
3536 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3537 else
3538 Val = Shift;
3539
3540 Offset += LoadSize;
3541 TotalSizeLoaded += LoadSize;
3542 Alignment = std::min(Alignment, LoadSize);
3543 }
3544
3545 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3546 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3547 return;
3548 }
3549 }
3550
3551 // Copy remainder of byval arg to it with memcpy.
3552 unsigned MemCpySize = ByValSize - Offset;
3553 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3554 DAG.getConstant(Offset, PtrTy));
3555 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3556 DAG.getIntPtrConstant(ByVal.Address));
3557 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3558 DAG.getConstant(MemCpySize, PtrTy), Alignment,
3559 /*isVolatile=*/false, /*AlwaysInline=*/false,
3560 MachinePointerInfo(0), MachinePointerInfo(0));
3561 MemOpChains.push_back(Chain);
3562}
Akira Hatanakaf0848472012-10-27 00:21:13 +00003563
3564void
3565MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3566 const MipsCC &CC, SDValue Chain,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003567 SDLoc DL, SelectionDAG &DAG) const {
Akira Hatanakaf0848472012-10-27 00:21:13 +00003568 unsigned NumRegs = CC.numIntArgRegs();
3569 const uint16_t *ArgRegs = CC.intArgRegs();
3570 const CCState &CCInfo = CC.getCCInfo();
3571 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
3572 unsigned RegSize = CC.regSize();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003573 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003574 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3575 MachineFunction &MF = DAG.getMachineFunction();
3576 MachineFrameInfo *MFI = MF.getFrameInfo();
3577 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3578
3579 // Offset of the first variable argument from stack pointer.
3580 int VaArgOffset;
3581
3582 if (NumRegs == Idx)
3583 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
3584 else
3585 VaArgOffset =
3586 (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
3587
3588 // Record the frame index of the first variable argument
3589 // which is a value necessary to VASTART.
3590 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3591 MipsFI->setVarArgsFrameIndex(FI);
3592
3593 // Copy the integer registers that have not been used for argument passing
3594 // to the argument register save area. For O32, the save area is allocated
3595 // in the caller's stack frame, while for N32/64, it is allocated in the
3596 // callee's stack frame.
3597 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003598 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003599 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3600 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3601 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3602 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3603 MachinePointerInfo(), false, false, 0);
3604 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
3605 OutChains.push_back(Store);
3606 }
3607}