blob: c13f53a3ebe05bd7f9d44162497ae9ac21b3d0e7 [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 Hatanakaf635ef42013-03-12 00:16:36 +0000800 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000801 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000802 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000803 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000804 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000805 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000806 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000807
808 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000809 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000810 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000811 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000812 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000813 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000814 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000815 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000816
817 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000818 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000819 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000820 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000821 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000822 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000823 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000824 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000825
826 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000827 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000828 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000829 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000830 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000831 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000832 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000833 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000834
835 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000836 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000837 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000838 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000839 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000840 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000841 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000842 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000843
844 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000845 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000846 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000847 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000848 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000849 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000850 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000851 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000852
853 case Mips::ATOMIC_SWAP_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000854 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000855 case Mips::ATOMIC_SWAP_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000856 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000857 case Mips::ATOMIC_SWAP_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000858 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000859 case Mips::ATOMIC_SWAP_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000860 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000861
862 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000863 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000864 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000865 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000866 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000867 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000868 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000869 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanakaf8941992013-05-20 18:07:43 +0000870 case Mips::PseudoSDIV:
871 case Mips::PseudoUDIV:
872 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), false);
873 case Mips::PseudoDSDIV:
874 case Mips::PseudoDUDIV:
875 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), true);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000876 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000877}
878
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000879// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
880// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
881MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000882MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000883 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000884 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000885 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000886
887 MachineFunction *MF = BB->getParent();
888 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000889 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000890 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000891 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000892 unsigned LL, SC, AND, NOR, ZERO, BEQ;
893
894 if (Size == 4) {
Akira Hatanakaa98a4862013-08-20 21:08:22 +0000895 LL = Mips::LL;
896 SC = Mips::SC;
Akira Hatanaka59068062011-11-11 04:14:30 +0000897 AND = Mips::AND;
898 NOR = Mips::NOR;
899 ZERO = Mips::ZERO;
900 BEQ = Mips::BEQ;
901 }
902 else {
Akira Hatanakaa98a4862013-08-20 21:08:22 +0000903 LL = Mips::LLD;
904 SC = Mips::SCD;
Akira Hatanaka59068062011-11-11 04:14:30 +0000905 AND = Mips::AND64;
906 NOR = Mips::NOR64;
907 ZERO = Mips::ZERO_64;
908 BEQ = Mips::BEQ64;
909 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000910
Akira Hatanaka4061da12011-07-19 20:11:17 +0000911 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000912 unsigned Ptr = MI->getOperand(1).getReg();
913 unsigned Incr = MI->getOperand(2).getReg();
914
Akira Hatanaka4061da12011-07-19 20:11:17 +0000915 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
916 unsigned AndRes = RegInfo.createVirtualRegister(RC);
917 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000918
919 // insert new blocks after the current block
920 const BasicBlock *LLVM_BB = BB->getBasicBlock();
921 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
922 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
923 MachineFunction::iterator It = BB;
924 ++It;
925 MF->insert(It, loopMBB);
926 MF->insert(It, exitMBB);
927
928 // Transfer the remainder of BB and its successor edges to exitMBB.
929 exitMBB->splice(exitMBB->begin(), BB,
930 llvm::next(MachineBasicBlock::iterator(MI)),
931 BB->end());
932 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
933
934 // thisMBB:
935 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000936 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000937 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000938 loopMBB->addSuccessor(loopMBB);
939 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000940
941 // loopMBB:
942 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000943 // <binop> storeval, oldval, incr
944 // sc success, storeval, 0(ptr)
945 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000946 BB = loopMBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000947 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000948 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000949 // and andres, oldval, incr
950 // nor storeval, $0, andres
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000951 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
952 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000953 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000954 // <binop> storeval, oldval, incr
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000955 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000956 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000957 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000958 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000959 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
960 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000961
962 MI->eraseFromParent(); // The instruction is gone now.
963
Akira Hatanaka939ece12011-07-19 03:42:13 +0000964 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000965}
966
967MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000968MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000969 MachineBasicBlock *BB,
970 unsigned Size, unsigned BinOpcode,
971 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000972 assert((Size == 1 || Size == 2) &&
973 "Unsupported size for EmitAtomicBinaryPartial.");
974
975 MachineFunction *MF = BB->getParent();
976 MachineRegisterInfo &RegInfo = MF->getRegInfo();
977 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
978 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000979 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000980
981 unsigned Dest = MI->getOperand(0).getReg();
982 unsigned Ptr = MI->getOperand(1).getReg();
983 unsigned Incr = MI->getOperand(2).getReg();
984
Akira Hatanaka4061da12011-07-19 20:11:17 +0000985 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
986 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000987 unsigned Mask = RegInfo.createVirtualRegister(RC);
988 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000989 unsigned NewVal = RegInfo.createVirtualRegister(RC);
990 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000991 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000992 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
993 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
994 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
995 unsigned AndRes = RegInfo.createVirtualRegister(RC);
996 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000997 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000998 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
999 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1000 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1001 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1002 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001003
1004 // insert new blocks after the current block
1005 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1006 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001007 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001008 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1009 MachineFunction::iterator It = BB;
1010 ++It;
1011 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001012 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001013 MF->insert(It, exitMBB);
1014
1015 // Transfer the remainder of BB and its successor edges to exitMBB.
1016 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001017 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001018 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1019
Akira Hatanaka81b44112011-07-19 17:09:53 +00001020 BB->addSuccessor(loopMBB);
1021 loopMBB->addSuccessor(loopMBB);
1022 loopMBB->addSuccessor(sinkMBB);
1023 sinkMBB->addSuccessor(exitMBB);
1024
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001025 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001026 // addiu masklsb2,$0,-4 # 0xfffffffc
1027 // and alignedaddr,ptr,masklsb2
1028 // andi ptrlsb2,ptr,3
1029 // sll shiftamt,ptrlsb2,3
1030 // ori maskupper,$0,255 # 0xff
1031 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001032 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001033 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001034
1035 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001036 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001037 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001038 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001039 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001040 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001041 if (Subtarget->isLittle()) {
1042 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1043 } else {
1044 unsigned Off = RegInfo.createVirtualRegister(RC);
1045 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1046 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1047 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1048 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001049 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001050 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001051 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001052 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001053 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka51122432013-07-01 20:39:53 +00001054 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001055
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001056 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001057 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001058 // ll oldval,0(alignedaddr)
1059 // binop binopres,oldval,incr2
1060 // and newval,binopres,mask
1061 // and maskedoldval0,oldval,mask2
1062 // or storeval,maskedoldval0,newval
1063 // sc success,storeval,0(alignedaddr)
1064 // beq success,$0,loopMBB
1065
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001066 // atomic.swap
1067 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001068 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001069 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001070 // and maskedoldval0,oldval,mask2
1071 // or storeval,maskedoldval0,newval
1072 // sc success,storeval,0(alignedaddr)
1073 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001074
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001075 BB = loopMBB;
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001076 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001077 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001078 // and andres, oldval, incr2
1079 // nor binopres, $0, andres
1080 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001081 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1082 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001083 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001084 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001085 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001086 // <binop> binopres, oldval, incr2
1087 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001088 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1089 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001090 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001091 // and newval, incr2, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001092 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001093 }
Jia Liubb481f82012-02-28 07:46:26 +00001094
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001095 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001096 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001097 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001098 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001099 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001100 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001101 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001102 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001103
Akira Hatanaka939ece12011-07-19 03:42:13 +00001104 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001105 // and maskedoldval1,oldval,mask
1106 // srl srlres,maskedoldval1,shiftamt
1107 // sll sllres,srlres,24
1108 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001109 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001110 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001111
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001112 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001113 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001114 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001115 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001116 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001117 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001118 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001119 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001120
1121 MI->eraseFromParent(); // The instruction is gone now.
1122
Akira Hatanaka939ece12011-07-19 03:42:13 +00001123 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001124}
1125
1126MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001127MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001128 MachineBasicBlock *BB,
1129 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001130 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001131
1132 MachineFunction *MF = BB->getParent();
1133 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001134 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001135 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001136 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001137 unsigned LL, SC, ZERO, BNE, BEQ;
1138
1139 if (Size == 4) {
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001140 LL = Mips::LL;
1141 SC = Mips::SC;
Akira Hatanaka59068062011-11-11 04:14:30 +00001142 ZERO = Mips::ZERO;
1143 BNE = Mips::BNE;
1144 BEQ = Mips::BEQ;
1145 }
1146 else {
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001147 LL = Mips::LLD;
1148 SC = Mips::SCD;
Akira Hatanaka59068062011-11-11 04:14:30 +00001149 ZERO = Mips::ZERO_64;
1150 BNE = Mips::BNE64;
1151 BEQ = Mips::BEQ64;
1152 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001153
1154 unsigned Dest = MI->getOperand(0).getReg();
1155 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001156 unsigned OldVal = MI->getOperand(2).getReg();
1157 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001158
Akira Hatanaka4061da12011-07-19 20:11:17 +00001159 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001160
1161 // insert new blocks after the current block
1162 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1163 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1164 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1165 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1166 MachineFunction::iterator It = BB;
1167 ++It;
1168 MF->insert(It, loop1MBB);
1169 MF->insert(It, loop2MBB);
1170 MF->insert(It, exitMBB);
1171
1172 // Transfer the remainder of BB and its successor edges to exitMBB.
1173 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001174 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001175 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1176
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001177 // thisMBB:
1178 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001179 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001180 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001181 loop1MBB->addSuccessor(exitMBB);
1182 loop1MBB->addSuccessor(loop2MBB);
1183 loop2MBB->addSuccessor(loop1MBB);
1184 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001185
1186 // loop1MBB:
1187 // ll dest, 0(ptr)
1188 // bne dest, oldval, exitMBB
1189 BB = loop1MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001190 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1191 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001192 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001193
1194 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001195 // sc success, newval, 0(ptr)
1196 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001197 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001198 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001199 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001200 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka59068062011-11-11 04:14:30 +00001201 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001202
1203 MI->eraseFromParent(); // The instruction is gone now.
1204
Akira Hatanaka939ece12011-07-19 03:42:13 +00001205 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001206}
1207
1208MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001209MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001210 MachineBasicBlock *BB,
1211 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001212 assert((Size == 1 || Size == 2) &&
1213 "Unsupported size for EmitAtomicCmpSwapPartial.");
1214
1215 MachineFunction *MF = BB->getParent();
1216 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1217 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1218 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001219 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001220
1221 unsigned Dest = MI->getOperand(0).getReg();
1222 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001223 unsigned CmpVal = MI->getOperand(2).getReg();
1224 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001225
Akira Hatanaka4061da12011-07-19 20:11:17 +00001226 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1227 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001228 unsigned Mask = RegInfo.createVirtualRegister(RC);
1229 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001230 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1231 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1232 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1233 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1234 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1235 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1236 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1237 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1238 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1239 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1240 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1241 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1242 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1243 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001244
1245 // insert new blocks after the current block
1246 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1247 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1248 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001249 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001250 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1251 MachineFunction::iterator It = BB;
1252 ++It;
1253 MF->insert(It, loop1MBB);
1254 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001255 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001256 MF->insert(It, exitMBB);
1257
1258 // Transfer the remainder of BB and its successor edges to exitMBB.
1259 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001260 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001261 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1262
Akira Hatanaka81b44112011-07-19 17:09:53 +00001263 BB->addSuccessor(loop1MBB);
1264 loop1MBB->addSuccessor(sinkMBB);
1265 loop1MBB->addSuccessor(loop2MBB);
1266 loop2MBB->addSuccessor(loop1MBB);
1267 loop2MBB->addSuccessor(sinkMBB);
1268 sinkMBB->addSuccessor(exitMBB);
1269
Akira Hatanaka70564a92011-07-19 18:14:26 +00001270 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001271 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001272 // addiu masklsb2,$0,-4 # 0xfffffffc
1273 // and alignedaddr,ptr,masklsb2
1274 // andi ptrlsb2,ptr,3
1275 // sll shiftamt,ptrlsb2,3
1276 // ori maskupper,$0,255 # 0xff
1277 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001278 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001279 // andi maskedcmpval,cmpval,255
1280 // sll shiftedcmpval,maskedcmpval,shiftamt
1281 // andi maskednewval,newval,255
1282 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001283 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001284 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001285 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001286 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001287 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001288 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001289 if (Subtarget->isLittle()) {
1290 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1291 } else {
1292 unsigned Off = RegInfo.createVirtualRegister(RC);
1293 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1294 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1295 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1296 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001297 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001298 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001299 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001300 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001301 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1302 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001303 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001304 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001305 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001306 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001307 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001308 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001309 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001310
1311 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001312 // ll oldval,0(alginedaddr)
1313 // and maskedoldval0,oldval,mask
1314 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001315 BB = loop1MBB;
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001316 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001317 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001318 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001319 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001320 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001321
1322 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001323 // and maskedoldval1,oldval,mask2
1324 // or storeval,maskedoldval1,shiftednewval
1325 // sc success,storeval,0(alignedaddr)
1326 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001327 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001328 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001329 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001330 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001331 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001332 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001333 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001334 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001335 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001336
Akira Hatanaka939ece12011-07-19 03:42:13 +00001337 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001338 // srl srlres,maskedoldval0,shiftamt
1339 // sll sllres,srlres,24
1340 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001341 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001342 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001343
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001344 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001345 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001346 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001347 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001348 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001349 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001350
1351 MI->eraseFromParent(); // The instruction is gone now.
1352
Akira Hatanaka939ece12011-07-19 03:42:13 +00001353 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001354}
1355
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001356//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001357// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001358//===----------------------------------------------------------------------===//
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001359SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakab7656a92013-03-06 21:32:03 +00001360 SDValue Chain = Op.getOperand(0);
1361 SDValue Table = Op.getOperand(1);
1362 SDValue Index = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001363 SDLoc DL(Op);
Akira Hatanakab7656a92013-03-06 21:32:03 +00001364 EVT PTy = getPointerTy();
1365 unsigned EntrySize =
1366 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1367
1368 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1369 DAG.getConstant(EntrySize, PTy));
1370 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1371
1372 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1373 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1374 MachinePointerInfo::getJumpTable(), MemVT, false, false,
1375 0);
1376 Chain = Addr.getValue(1);
1377
1378 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || IsN64) {
1379 // For PIC, the sequence is:
1380 // BRIND(load(Jumptable + index) + RelocBase)
1381 // RelocBase can be JumpTable, GOT or some sort of global base.
1382 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1383 getPICJumpTableRelocBase(Table, DAG));
1384 }
1385
1386 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1387}
1388
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001389SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001390lowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001391{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001392 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001393 // the block to branch to if the condition is true.
1394 SDValue Chain = Op.getOperand(0);
1395 SDValue Dest = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001396 SDLoc DL(Op);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001397
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001398 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001399
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001400 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001401 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001402 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001403
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001404 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001405 Mips::CondCode CC =
1406 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanaka9cf07242013-03-30 01:16:38 +00001407 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1408 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001409 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001410 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001411 FCC0, Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001412}
1413
1414SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001415lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001416{
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001417 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001418
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001419 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001420 if (Cond.getOpcode() != MipsISD::FPCmp)
1421 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001422
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001423 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001424 SDLoc(Op));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001425}
1426
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001427SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001428lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001429{
Andrew Trickac6d9be2013-05-25 02:42:55 +00001430 SDLoc DL(Op);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001431 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault225ed702013-05-18 00:21:46 +00001432 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1433 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001434 Op.getOperand(0), Op.getOperand(1),
1435 Op.getOperand(4));
1436
1437 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1438 Op.getOperand(3));
1439}
1440
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001441SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1442 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001443
1444 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1445 "Floating point operand expected.");
1446
1447 SDValue True = DAG.getConstant(1, MVT::i32);
1448 SDValue False = DAG.getConstant(0, MVT::i32);
1449
Andrew Trickac6d9be2013-05-25 02:42:55 +00001450 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001451}
1452
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001453SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001454 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001455 // FIXME there isn't actually debug info here
Andrew Trickac6d9be2013-05-25 02:42:55 +00001456 SDLoc DL(Op);
Jia Liubb481f82012-02-28 07:46:26 +00001457 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001458
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001459 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaafc945b2012-09-12 23:27:55 +00001460 const MipsTargetObjectFile &TLOF =
1461 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001462
Chris Lattnere3736f82009-08-13 05:41:27 +00001463 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001464 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001465 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001466 MipsII::MO_GPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001467 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001468 DAG.getVTList(MVT::i32), &GA, 1);
Akira Hatanakae7338cd2012-08-22 03:18:13 +00001469 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001470 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001471 }
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001472
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001473 // %hi/%lo relocation
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001474 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001475 }
1476
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001477 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
1478 return getAddrLocal(Op, DAG, HasMips64);
1479
Akira Hatanakaf09a0372012-11-21 20:40:38 +00001480 if (LargeGOT)
1481 return getAddrGlobalLargeGOT(Op, DAG, MipsII::MO_GOT_HI16,
1482 MipsII::MO_GOT_LO16);
1483
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001484 return getAddrGlobal(Op, DAG,
1485 HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001486}
1487
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001488SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001489 SelectionDAG &DAG) const {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001490 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1491 return getAddrNonPIC(Op, DAG);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001492
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001493 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001494}
1495
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001496SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001497lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001498{
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001499 // If the relocation model is PIC, use the General Dynamic TLS Model or
1500 // Local Dynamic TLS model, otherwise use the Initial Exec or
1501 // Local Exec TLS Model.
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001502
1503 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001504 SDLoc DL(GA);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001505 const GlobalValue *GV = GA->getGlobal();
1506 EVT PtrVT = getPointerTy();
1507
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001508 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1509
1510 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg70a07c72012-06-04 14:02:08 +00001511 // General Dynamic and Local Dynamic TLS Model.
1512 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1513 : MipsII::MO_TLSGD;
1514
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001515 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1516 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1517 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001518 unsigned PtrSize = PtrVT.getSizeInBits();
1519 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1520
Benjamin Kramer5eccf672011-12-11 12:21:34 +00001521 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001522
1523 ArgListTy Args;
1524 ArgListEntry Entry;
1525 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00001526 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001527 Args.push_back(Entry);
Jia Liubb481f82012-02-28 07:46:26 +00001528
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001529 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001530 false, false, false, false, 0, CallingConv::C,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001531 /*IsTailCall=*/false, /*doesNotRet=*/false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001532 /*isReturnValueUsed=*/true,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001533 TlsGetAddr, Args, DAG, DL);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001534 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001535
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001536 SDValue Ret = CallResult.first;
1537
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001538 if (model != TLSModel::LocalDynamic)
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001539 return Ret;
1540
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001541 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001542 MipsII::MO_DTPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001543 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1544 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001545 MipsII::MO_DTPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001546 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1547 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1548 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001549 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001550
1551 SDValue Offset;
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001552 if (model == TLSModel::InitialExec) {
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001553 // Initial Exec TLS Model
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001554 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001555 MipsII::MO_GOTTPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001556 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanaka648f00c2012-02-24 22:34:47 +00001557 TGA);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001558 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001559 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001560 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001561 } else {
1562 // Local Exec TLS Model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001563 assert(model == TLSModel::LocalExec);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001564 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001565 MipsII::MO_TPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001566 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001567 MipsII::MO_TPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001568 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1569 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1570 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001571 }
1572
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001573 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1574 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001575}
1576
1577SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001578lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001579{
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001580 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1581 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001582
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001583 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001584}
1585
Dan Gohman475871a2008-07-27 21:46:04 +00001586SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001587lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001588{
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001589 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001590 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001591 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001592 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001593 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001594 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001595 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1596 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001597 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001598
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001599 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1600 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001601
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001602 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001603}
1604
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001605SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001606 MachineFunction &MF = DAG.getMachineFunction();
1607 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1608
Andrew Trickac6d9be2013-05-25 02:42:55 +00001609 SDLoc DL(Op);
Dan Gohman1e93df62010-04-17 14:41:14 +00001610 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1611 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001612
1613 // vastart just stores the address of the VarArgsFrameIndex slot into the
1614 // memory location argument.
1615 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001616 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka82099682011-12-19 19:52:25 +00001617 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001618}
Jia Liubb481f82012-02-28 07:46:26 +00001619
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001620static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001621 EVT TyX = Op.getOperand(0).getValueType();
1622 EVT TyY = Op.getOperand(1).getValueType();
1623 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1624 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001625 SDLoc DL(Op);
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001626 SDValue Res;
1627
1628 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1629 // to i32.
1630 SDValue X = (TyX == MVT::f32) ?
1631 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1632 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1633 Const1);
1634 SDValue Y = (TyY == MVT::f32) ?
1635 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1636 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1637 Const1);
1638
1639 if (HasR2) {
1640 // ext E, Y, 31, 1 ; extract bit31 of Y
1641 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1642 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1643 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1644 } else {
1645 // sll SllX, X, 1
1646 // srl SrlX, SllX, 1
1647 // srl SrlY, Y, 31
1648 // sll SllY, SrlX, 31
1649 // or Or, SrlX, SllY
1650 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1651 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1652 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1653 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1654 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1655 }
1656
1657 if (TyX == MVT::f32)
1658 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1659
1660 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1661 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1662 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001663}
1664
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001665static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001666 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1667 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1668 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1669 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001670 SDLoc DL(Op);
Eric Christopher471e4222011-06-08 23:55:35 +00001671
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001672 // Bitcast to integer nodes.
1673 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1674 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001675
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001676 if (HasR2) {
1677 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1678 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1679 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1680 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001681
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001682 if (WidthX > WidthY)
1683 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1684 else if (WidthY > WidthX)
1685 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001686
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001687 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1688 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1689 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1690 }
1691
1692 // (d)sll SllX, X, 1
1693 // (d)srl SrlX, SllX, 1
1694 // (d)srl SrlY, Y, width(Y)-1
1695 // (d)sll SllY, SrlX, width(Y)-1
1696 // or Or, SrlX, SllY
1697 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1698 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1699 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1700 DAG.getConstant(WidthY - 1, MVT::i32));
1701
1702 if (WidthX > WidthY)
1703 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1704 else if (WidthY > WidthX)
1705 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1706
1707 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1708 DAG.getConstant(WidthX - 1, MVT::i32));
1709 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1710 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001711}
1712
Akira Hatanaka82099682011-12-19 19:52:25 +00001713SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001714MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001715 if (Subtarget->hasMips64())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001716 return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001717
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001718 return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001719}
1720
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001721static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001722 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001723 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001724
1725 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1726 // to i32.
1727 SDValue X = (Op.getValueType() == MVT::f32) ?
1728 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1729 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1730 Const1);
1731
1732 // Clear MSB.
1733 if (HasR2)
1734 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
1735 DAG.getRegister(Mips::ZERO, MVT::i32),
1736 DAG.getConstant(31, MVT::i32), Const1, X);
1737 else {
1738 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1739 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1740 }
1741
1742 if (Op.getValueType() == MVT::f32)
1743 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
1744
1745 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1746 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1747 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1748}
1749
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001750static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001751 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001752 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001753
1754 // Bitcast to integer node.
1755 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
1756
1757 // Clear MSB.
1758 if (HasR2)
1759 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
1760 DAG.getRegister(Mips::ZERO_64, MVT::i64),
1761 DAG.getConstant(63, MVT::i32), Const1, X);
1762 else {
1763 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
1764 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
1765 }
1766
1767 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
1768}
1769
1770SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001771MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001772 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001773 return lowerFABS64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001774
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001775 return lowerFABS32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001776}
1777
Akira Hatanaka2e591472011-06-02 00:24:44 +00001778SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001779lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001780 // check the depth
1781 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001782 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001783
1784 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1785 MFI->setFrameAddressIsTaken(true);
1786 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001787 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001788 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001789 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001790 return FrameAddr;
1791}
1792
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001793SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001794 SelectionDAG &DAG) const {
1795 // check the depth
1796 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1797 "Return address can be determined only for current frame.");
1798
1799 MachineFunction &MF = DAG.getMachineFunction();
1800 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001801 MVT VT = Op.getSimpleValueType();
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001802 unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
1803 MFI->setReturnAddressIsTaken(true);
1804
1805 // Return RA, which contains the return address. Mark it an implicit live-in.
1806 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001807 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001808}
1809
Akira Hatanaka544cc212013-01-30 00:26:49 +00001810// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1811// generated from __builtin_eh_return (offset, handler)
1812// The effect of this is to adjust the stack pointer by "offset"
1813// and then branch to "handler".
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001814SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanaka544cc212013-01-30 00:26:49 +00001815 const {
1816 MachineFunction &MF = DAG.getMachineFunction();
1817 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1818
1819 MipsFI->setCallsEhReturn();
1820 SDValue Chain = Op.getOperand(0);
1821 SDValue Offset = Op.getOperand(1);
1822 SDValue Handler = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001823 SDLoc DL(Op);
Akira Hatanaka544cc212013-01-30 00:26:49 +00001824 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
1825
1826 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1827 // EH_RETURN nodes, so that instructions are emitted back-to-back.
1828 unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1;
1829 unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0;
1830 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1831 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1832 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1833 DAG.getRegister(OffsetReg, Ty),
1834 DAG.getRegister(AddrReg, getPointerTy()),
1835 Chain.getValue(1));
1836}
1837
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001838SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001839 SelectionDAG &DAG) const {
Eli Friedman14648462011-07-27 22:21:52 +00001840 // FIXME: Need pseudo-fence for 'singlethread' fences
1841 // FIXME: Set SType for weaker fences where supported/appropriate.
1842 unsigned SType = 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001843 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001844 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00001845 DAG.getConstant(SType, MVT::i32));
1846}
1847
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001848SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001849 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001850 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001851 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1852 SDValue Shamt = Op.getOperand(2);
1853
1854 // if shamt < 32:
1855 // lo = (shl lo, shamt)
1856 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
1857 // else:
1858 // lo = 0
1859 // hi = (shl lo, shamt[4:0])
1860 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1861 DAG.getConstant(-1, MVT::i32));
1862 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
1863 DAG.getConstant(1, MVT::i32));
1864 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
1865 Not);
1866 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
1867 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1868 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
1869 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1870 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka864f6602012-06-14 21:10:56 +00001871 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1872 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001873 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
1874
1875 SDValue Ops[2] = {Lo, Hi};
1876 return DAG.getMergeValues(Ops, 2, DL);
1877}
1878
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001879SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001880 bool IsSRA) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001881 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001882 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1883 SDValue Shamt = Op.getOperand(2);
1884
1885 // if shamt < 32:
1886 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
1887 // if isSRA:
1888 // hi = (sra hi, shamt)
1889 // else:
1890 // hi = (srl hi, shamt)
1891 // else:
1892 // if isSRA:
1893 // lo = (sra hi, shamt[4:0])
1894 // hi = (sra hi, 31)
1895 // else:
1896 // lo = (srl hi, shamt[4:0])
1897 // hi = 0
1898 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1899 DAG.getConstant(-1, MVT::i32));
1900 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
1901 DAG.getConstant(1, MVT::i32));
1902 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
1903 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
1904 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1905 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
1906 Hi, Shamt);
1907 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1908 DAG.getConstant(0x20, MVT::i32));
1909 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
1910 DAG.getConstant(31, MVT::i32));
1911 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
1912 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1913 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
1914 ShiftRightHi);
1915
1916 SDValue Ops[2] = {Lo, Hi};
1917 return DAG.getMergeValues(Ops, 2, DL);
1918}
1919
Akira Hatanakafee62c12013-04-11 19:07:14 +00001920static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001921 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001922 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001923 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001924 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001925 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001926 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1927
1928 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001929 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001930 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001931
1932 SDValue Ops[] = { Chain, Ptr, Src };
1933 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
1934 LD->getMemOperand());
1935}
1936
1937// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001938SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001939 LoadSDNode *LD = cast<LoadSDNode>(Op);
1940 EVT MemVT = LD->getMemoryVT();
1941
1942 // Return if load is aligned or if MemVT is neither i32 nor i64.
1943 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
1944 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
1945 return SDValue();
1946
1947 bool IsLittle = Subtarget->isLittle();
1948 EVT VT = Op.getValueType();
1949 ISD::LoadExtType ExtType = LD->getExtensionType();
1950 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
1951
1952 assert((VT == MVT::i32) || (VT == MVT::i64));
1953
1954 // Expand
1955 // (set dst, (i64 (load baseptr)))
1956 // to
1957 // (set tmp, (ldl (add baseptr, 7), undef))
1958 // (set dst, (ldr baseptr, tmp))
1959 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00001960 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001961 IsLittle ? 7 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00001962 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001963 IsLittle ? 0 : 7);
1964 }
1965
Akira Hatanakafee62c12013-04-11 19:07:14 +00001966 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001967 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00001968 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001969 IsLittle ? 0 : 3);
1970
1971 // Expand
1972 // (set dst, (i32 (load baseptr))) or
1973 // (set dst, (i64 (sextload baseptr))) or
1974 // (set dst, (i64 (extload baseptr)))
1975 // to
1976 // (set tmp, (lwl (add baseptr, 3), undef))
1977 // (set dst, (lwr baseptr, tmp))
1978 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
1979 (ExtType == ISD::EXTLOAD))
1980 return LWR;
1981
1982 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
1983
1984 // Expand
1985 // (set dst, (i64 (zextload baseptr)))
1986 // to
1987 // (set tmp0, (lwl (add baseptr, 3), undef))
1988 // (set tmp1, (lwr baseptr, tmp0))
1989 // (set tmp2, (shl tmp1, 32))
1990 // (set dst, (srl tmp2, 32))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001991 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001992 SDValue Const32 = DAG.getConstant(32, MVT::i32);
1993 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka94ccee22012-06-04 17:46:29 +00001994 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
1995 SDValue Ops[] = { SRL, LWR.getValue(1) };
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001996 return DAG.getMergeValues(Ops, 2, DL);
1997}
1998
Akira Hatanakafee62c12013-04-11 19:07:14 +00001999static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002000 SDValue Chain, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002001 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2002 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002003 SDLoc DL(SD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002004 SDVTList VTList = DAG.getVTList(MVT::Other);
2005
2006 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002007 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002008 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002009
2010 SDValue Ops[] = { Chain, Value, Ptr };
2011 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2012 SD->getMemOperand());
2013}
2014
2015// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanaka63451432013-05-16 20:45:17 +00002016static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2017 bool IsLittle) {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002018 SDValue Value = SD->getValue(), Chain = SD->getChain();
2019 EVT VT = Value.getValueType();
2020
2021 // Expand
2022 // (store val, baseptr) or
2023 // (truncstore val, baseptr)
2024 // to
2025 // (swl val, (add baseptr, 3))
2026 // (swr val, baseptr)
2027 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00002028 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002029 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00002030 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002031 }
2032
2033 assert(VT == MVT::i64);
2034
2035 // Expand
2036 // (store val, baseptr)
2037 // to
2038 // (sdl val, (add baseptr, 7))
2039 // (sdr val, baseptr)
Akira Hatanakafee62c12013-04-11 19:07:14 +00002040 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2041 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002042}
2043
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002044// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2045static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2046 SDValue Val = SD->getValue();
2047
2048 if (Val.getOpcode() != ISD::FP_TO_SINT)
2049 return SDValue();
2050
2051 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002052 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002053 Val.getOperand(0));
2054
Andrew Trickac6d9be2013-05-25 02:42:55 +00002055 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002056 SD->getPointerInfo(), SD->isVolatile(),
2057 SD->isNonTemporal(), SD->getAlignment());
2058}
2059
Akira Hatanaka63451432013-05-16 20:45:17 +00002060SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2061 StoreSDNode *SD = cast<StoreSDNode>(Op);
2062 EVT MemVT = SD->getMemoryVT();
2063
2064 // Lower unaligned integer stores.
2065 if ((SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2066 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2067 return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle());
2068
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002069 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanaka63451432013-05-16 20:45:17 +00002070}
2071
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002072SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002073 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2074 || cast<ConstantSDNode>
2075 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2076 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2077 return SDValue();
2078
2079 // The pattern
2080 // (add (frameaddr 0), (frame_to_args_offset))
2081 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2082 // (add FrameObject, 0)
2083 // where FrameObject is a fixed StackObject with offset 0 which points to
2084 // the old stack pointer.
2085 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2086 EVT ValTy = Op->getValueType(0);
2087 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2088 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002089 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002090 DAG.getConstant(0, ValTy));
2091}
2092
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002093SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2094 SelectionDAG &DAG) const {
2095 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002096 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002097 Op.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002098 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002099}
2100
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002101//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002102// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002103//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002104
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002105//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002106// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002107// Mips O32 ABI rules:
2108// ---
2109// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002110// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002111// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002112// f64 - Only passed in two aliased f32 registers if no int reg has been used
2113// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002114// not used, it must be shadowed. If only A3 is avaiable, shadow it and
2115// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002116//
2117// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002118//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002119
Duncan Sands1e96bab2010-11-04 10:49:57 +00002120static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00002121 MVT LocVT, CCValAssign::LocInfo LocInfo,
Akira Hatanakaad341d42013-08-20 23:38:40 +00002122 ISD::ArgFlagsTy ArgFlags, CCState &State,
2123 const uint16_t *F64Regs) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002124
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002125 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002126
Craig Topperc5eaae42012-03-11 07:57:25 +00002127 static const uint16_t IntRegs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002128 Mips::A0, Mips::A1, Mips::A2, Mips::A3
2129 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002130 static const uint16_t F32Regs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002131 Mips::F12, Mips::F14
2132 };
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002133
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002134 // Do not process byval args here.
2135 if (ArgFlags.isByVal())
2136 return true;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002137
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002138 // Promote i8 and i16
2139 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2140 LocVT = MVT::i32;
2141 if (ArgFlags.isSExt())
2142 LocInfo = CCValAssign::SExt;
2143 else if (ArgFlags.isZExt())
2144 LocInfo = CCValAssign::ZExt;
2145 else
2146 LocInfo = CCValAssign::AExt;
2147 }
2148
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002149 unsigned Reg;
2150
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002151 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2152 // is true: function is vararg, argument is 3rd or higher, there is previous
2153 // argument which is not f32 or f64.
2154 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2155 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002156 unsigned OrigAlign = ArgFlags.getOrigAlign();
2157 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002158
2159 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002160 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002161 // If this is the first part of an i64 arg,
2162 // the allocated register must be either A0 or A2.
2163 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2164 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002165 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002166 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2167 // Allocate int register and shadow next int register. If first
2168 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002169 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2170 if (Reg == Mips::A1 || Reg == Mips::A3)
2171 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2172 State.AllocateReg(IntRegs, IntRegsSize);
2173 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002174 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2175 // we are guaranteed to find an available float register
2176 if (ValVT == MVT::f32) {
2177 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2178 // Shadow int register
2179 State.AllocateReg(IntRegs, IntRegsSize);
2180 } else {
2181 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2182 // Shadow int registers
2183 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2184 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2185 State.AllocateReg(IntRegs, IntRegsSize);
2186 State.AllocateReg(IntRegs, IntRegsSize);
2187 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002188 } else
2189 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002190
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002191 if (!Reg) {
2192 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2193 OrigAlign);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002194 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002195 } else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002196 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002197
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002198 return false;
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002199}
2200
Akira Hatanakaad341d42013-08-20 23:38:40 +00002201static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2202 MVT LocVT, CCValAssign::LocInfo LocInfo,
2203 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2204 static const uint16_t F64Regs[] = { Mips::D6, Mips::D7 };
2205
2206 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2207}
2208
2209static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2210 MVT LocVT, CCValAssign::LocInfo LocInfo,
2211 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2212 static const uint16_t F64Regs[] = { Mips::D12_64, Mips::D12_64 };
2213
2214 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2215}
2216
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002217#include "MipsGenCallingConv.inc"
2218
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002219//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002220// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002221//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002222
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002223static const unsigned O32IntRegsSize = 4;
2224
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002225// Return next O32 integer argument register.
2226static unsigned getNextIntArgReg(unsigned Reg) {
2227 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2228 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2229}
2230
Akira Hatanaka7d712092012-10-30 19:23:25 +00002231SDValue
2232MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002233 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka7d712092012-10-30 19:23:25 +00002234 bool IsTailCall, SelectionDAG &DAG) const {
2235 if (!IsTailCall) {
2236 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2237 DAG.getIntPtrConstant(Offset));
2238 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2239 false, 0);
2240 }
2241
2242 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2243 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2244 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2245 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2246 /*isVolatile=*/ true, false, 0);
2247}
2248
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002249void MipsTargetLowering::
2250getOpndList(SmallVectorImpl<SDValue> &Ops,
2251 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2252 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2253 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
2254 // Insert node "GP copy globalreg" before call to function.
2255 //
2256 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2257 // in PIC mode) allow symbols to be resolved via lazy binding.
2258 // The lazy binding stub requires GP to point to the GOT.
2259 if (IsPICCall && !InternalLinkage) {
2260 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
2261 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2262 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2263 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002264
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002265 // Build a sequence of copy-to-reg nodes chained together with token
2266 // chain and flag operands which copy the outgoing args into registers.
2267 // The InFlag in necessary since all emitted instructions must be
2268 // stuck together.
2269 SDValue InFlag;
Reed Kotler8453b3f2013-01-24 04:24:02 +00002270
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002271 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2272 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2273 RegsToPass[i].second, InFlag);
2274 InFlag = Chain.getValue(1);
2275 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002276
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002277 // Add argument registers to the end of the list so that they are
2278 // known live into the call.
2279 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2280 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2281 RegsToPass[i].second.getValueType()));
Reed Kotler8453b3f2013-01-24 04:24:02 +00002282
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002283 // Add a register mask operand representing the call-preserved registers.
2284 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2285 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2286 assert(Mask && "Missing call preserved mask for calling convention");
Reed Kotler46090912013-05-10 22:25:39 +00002287 if (Subtarget->inMips16HardFloat()) {
2288 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2289 llvm::StringRef Sym = G->getGlobal()->getName();
2290 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
2291 if (F->hasFnAttribute("__Mips16RetHelper")) {
2292 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2293 }
2294 }
2295 }
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002296 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2297
2298 if (InFlag.getNode())
2299 Ops.push_back(InFlag);
Reed Kotler8453b3f2013-01-24 04:24:02 +00002300}
2301
Dan Gohman98ca4f22009-08-05 01:29:28 +00002302/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002303/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002304SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002305MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002306 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002307 SelectionDAG &DAG = CLI.DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002308 SDLoc DL = CLI.DL;
Craig Toppera0ec3f92013-07-14 04:42:23 +00002309 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2310 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2311 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002312 SDValue Chain = CLI.Chain;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002313 SDValue Callee = CLI.Callee;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002314 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002315 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002316 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002317
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002318 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002319 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002320 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002321 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002322
2323 // Analyze operands of the call, assigning locations to each operand.
2324 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002325 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002326 getTargetMachine(), ArgLocs, *DAG.getContext());
Reed Kotler46090912013-05-10 22:25:39 +00002327 MipsCC::SpecialCallingConvType SpecialCallingConv =
2328 getSpecialCallingConv(Callee);
Akira Hatanakaad341d42013-08-20 23:38:40 +00002329 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo,
2330 SpecialCallingConv);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002331
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002332 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002333 getTargetMachine().Options.UseSoftFloat,
2334 Callee.getNode(), CLI.Args);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002335
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002336 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002337 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002338
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002339 // Check if it's really possible to do a tail call.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002340 if (IsTailCall)
2341 IsTailCall =
2342 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002343 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002344
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002345 if (IsTailCall)
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002346 ++NumTailCalls;
2347
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002348 // Chain is the output chain of the last Load/Store or CopyToReg node.
2349 // ByValChain is the output chain of the last Memcpy node created for copying
2350 // byval arguments to the stack.
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002351 unsigned StackAlignment = TFL->getStackAlignment();
2352 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002353 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002354
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002355 if (!IsTailCall)
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002356 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002357
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002358 SDValue StackPtr = DAG.getCopyFromReg(Chain, DL,
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002359 IsN64 ? Mips::SP_64 : Mips::SP,
2360 getPointerTy());
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002361
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002362 // With EABI is it possible to have 16 args on registers.
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002363 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman475871a2008-07-27 21:46:04 +00002364 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002365 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002366
2367 // Walk the register/memloc assignments, inserting copies/loads.
2368 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002369 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002370 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002371 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002372 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2373
2374 // ByVal Arg.
2375 if (Flags.isByVal()) {
2376 assert(Flags.getByValSize() &&
2377 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002378 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002379 assert(!IsTailCall &&
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002380 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002381 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002382 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
2383 ++ByValArg;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002384 continue;
2385 }
Jia Liubb481f82012-02-28 07:46:26 +00002386
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002387 // Promote the value if needed.
2388 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002389 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002390 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002391 if (VA.isRegLoc()) {
2392 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002393 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2394 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002395 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002396 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002397 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002398 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002399 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002400 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002401 if (!Subtarget->isLittle())
2402 std::swap(Lo, Hi);
Jia Liubb481f82012-02-28 07:46:26 +00002403 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002404 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2405 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2406 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002407 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002408 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002409 }
2410 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002411 case CCValAssign::SExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002412 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002413 break;
2414 case CCValAssign::ZExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002415 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002416 break;
2417 case CCValAssign::AExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002418 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002419 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002420 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002421
2422 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002423 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002424 if (VA.isRegLoc()) {
2425 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002426 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002427 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002428
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002429 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002430 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002431
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002432 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002433 // parameter value to a stack Location
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002434 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002435 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002436 }
2437
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002438 // Transform all store nodes into one single node because all store
2439 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002440 if (!MemOpChains.empty())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002441 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002442 &MemOpChains[0], MemOpChains.size());
2443
Bill Wendling056292f2008-09-16 21:48:12 +00002444 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002445 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2446 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002447 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakaed185da2012-12-13 03:17:29 +00002448 bool GlobalOrExternal = false, InternalLinkage = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002449 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002450
2451 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002452 if (IsPICCall) {
Akira Hatanakaed185da2012-12-13 03:17:29 +00002453 InternalLinkage = G->getGlobal()->hasInternalLinkage();
2454
2455 if (InternalLinkage)
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002456 Callee = getAddrLocal(Callee, DAG, HasMips64);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002457 else if (LargeGOT)
2458 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
2459 MipsII::MO_CALL_LO16);
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002460 else
2461 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
2462 } else
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002463 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002464 MipsII::MO_NO_FLAG);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002465 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002466 }
2467 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002468 if (!IsN64 && !IsPIC) // !N64 && static
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002469 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2470 MipsII::MO_NO_FLAG);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002471 else if (LargeGOT)
2472 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
2473 MipsII::MO_CALL_LO16);
Akira Hatanaka60689322013-02-22 21:10:03 +00002474 else // N64 || PIC
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002475 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
2476
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002477 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002478 }
2479
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002480 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002481 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002482
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002483 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2484 CLI, Callee, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002485
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002486 if (IsTailCall)
2487 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, &Ops[0], Ops.size());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002488
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002489 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, &Ops[0], Ops.size());
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002490 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002491
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002492 // Create the CALLSEQ_END node.
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002493 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002494 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002495 InFlag = Chain.getValue(1);
2496
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002497 // Handle result values, copying them out of physregs into vregs that we
2498 // return.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002499 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
2500 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002501}
2502
Dan Gohman98ca4f22009-08-05 01:29:28 +00002503/// LowerCallResult - Lower the result values of a call into the
2504/// appropriate copies out of appropriate physical registers.
2505SDValue
2506MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002507 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002508 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002509 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002510 SmallVectorImpl<SDValue> &InVals,
2511 const SDNode *CallNode,
2512 const Type *RetTy) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002513 // Assign locations to each value returned by this call.
2514 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002515 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka864f6602012-06-14 21:10:56 +00002516 getTargetMachine(), RVLocs, *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002517 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002518
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002519 MipsCCInfo.analyzeCallResult(Ins, getTargetMachine().Options.UseSoftFloat,
2520 CallNode, RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002521
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002522 // Copy all of the result registers out of their specified physreg.
2523 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002524 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002525 RVLocs[i].getLocVT(), InFlag);
2526 Chain = Val.getValue(1);
2527 InFlag = Val.getValue(2);
2528
2529 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002530 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002531
2532 InVals.push_back(Val);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002533 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002534
Dan Gohman98ca4f22009-08-05 01:29:28 +00002535 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002536}
2537
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002538//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002539// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002540//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002541/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002542/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002543SDValue
2544MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002545 CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002546 bool IsVarArg,
Akira Hatanaka82099682011-12-19 19:52:25 +00002547 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002548 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002549 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002550 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002551 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002552 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002553 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002554
Dan Gohman1e93df62010-04-17 14:41:14 +00002555 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002556
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002557 // Used with vargs to acumulate store chains.
2558 std::vector<SDValue> OutChains;
2559
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002560 // Assign locations to all of the incoming arguments.
2561 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002562 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002563 getTargetMachine(), ArgLocs, *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002564 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002565 Function::const_arg_iterator FuncArg =
2566 DAG.getMachineFunction().getFunction()->arg_begin();
2567 bool UseSoftFloat = getTargetMachine().Options.UseSoftFloat;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002568
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002569 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
Akira Hatanakab33b34a2012-10-30 19:37:25 +00002570 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2571 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002572
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002573 unsigned CurArgIdx = 0;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002574 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002575
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002576 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002577 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002578 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2579 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002580 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002581 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2582 bool IsRegLoc = VA.isRegLoc();
2583
2584 if (Flags.isByVal()) {
2585 assert(Flags.getByValSize() &&
2586 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002587 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002588 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002589 MipsCCInfo, *ByValArg);
2590 ++ByValArg;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002591 continue;
2592 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002593
2594 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002595 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002596 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002597 unsigned ArgReg = VA.getLocReg();
Craig Topper44d23822012-02-22 05:59:10 +00002598 const TargetRegisterClass *RC;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002599
Owen Anderson825b72b2009-08-11 20:47:22 +00002600 if (RegVT == MVT::i32)
Reed Kotlerbacbf1c2012-12-20 06:06:35 +00002601 RC = Subtarget->inMips16Mode()? &Mips::CPU16RegsRegClass :
Akira Hatanaka18587862013-08-06 23:08:38 +00002602 &Mips::GPR32RegClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002603 else if (RegVT == MVT::i64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002604 RC = &Mips::GPR64RegClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002605 else if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002606 RC = &Mips::FGR32RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002607 else if (RegVT == MVT::f64)
Akira Hatanakaad341d42013-08-20 23:38:40 +00002608 RC = Subtarget->isFP64bit() ? &Mips::FGR64RegClass :
2609 &Mips::AFGR64RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002610 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002611 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002612
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002613 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002614 // physical registers into virtual ones
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002615 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2616 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002617
2618 // If this is an 8 or 16-bit value, it has been passed promoted
2619 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002620 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002621 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002622 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002623 if (VA.getLocInfo() == CCValAssign::SExt)
2624 Opcode = ISD::AssertSext;
2625 else if (VA.getLocInfo() == CCValAssign::ZExt)
2626 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002627 if (Opcode)
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002628 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002629 DAG.getValueType(ValVT));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002630 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002631 }
2632
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002633 // Handle floating point arguments passed in integer registers and
2634 // long double arguments passed in floating point registers.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002635 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002636 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2637 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002638 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002639 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002640 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002641 getNextIntArgReg(ArgReg), RC);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002642 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002643 if (!Subtarget->isLittle())
2644 std::swap(ArgValue, ArgValue2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002645 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002646 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002647 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002648
Dan Gohman98ca4f22009-08-05 01:29:28 +00002649 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002650 } else { // VA.isRegLoc()
2651
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002652 // sanity check
2653 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002654
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002655 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002656 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002657 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002658
2659 // Create load nodes to retrieve arguments from the stack
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002660 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002661 InVals.push_back(DAG.getLoad(ValVT, DL, Chain, FIN,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002662 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002663 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002664 }
2665 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002666
2667 // The mips ABIs for returning structs by value requires that we copy
2668 // the sret argument into $v0 for the return. Save the argument into
2669 // a virtual register so that we can access it from the return points.
2670 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2671 unsigned Reg = MipsFI->getSRetReturnReg();
2672 if (!Reg) {
Akira Hatanaka30580ce2012-10-19 22:11:40 +00002673 Reg = MF.getRegInfo().
2674 createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002675 MipsFI->setSRetReturnReg(Reg);
2676 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002677 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);
2678 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002679 }
2680
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002681 if (IsVarArg)
2682 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002683
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002684 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002685 // the size of Ins and InVals. This only happens when on varg functions
2686 if (!OutChains.empty()) {
2687 OutChains.push_back(Chain);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002688 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002689 &OutChains[0], OutChains.size());
2690 }
2691
Dan Gohman98ca4f22009-08-05 01:29:28 +00002692 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002693}
2694
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002695//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002696// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002697//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002698
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002699bool
2700MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002701 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002702 const SmallVectorImpl<ISD::OutputArg> &Outs,
2703 LLVMContext &Context) const {
2704 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002705 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002706 RVLocs, Context);
2707 return CCInfo.CheckReturn(Outs, RetCC_Mips);
2708}
2709
Dan Gohman98ca4f22009-08-05 01:29:28 +00002710SDValue
2711MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002712 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002713 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002714 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002715 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002716 // CCValAssign - represent the assignment of
2717 // the return value to a location
2718 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002719 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002720
2721 // CCState - Info about the registers and stack slot.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002722 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002723 *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002724 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002725
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002726 // Analyze return values.
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002727 MipsCCInfo.analyzeReturn(Outs, getTargetMachine().Options.UseSoftFloat,
2728 MF.getFunction()->getReturnType());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002729
Dan Gohman475871a2008-07-27 21:46:04 +00002730 SDValue Flag;
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002731 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002732
2733 // Copy the result values into the output registers.
2734 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002735 SDValue Val = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002736 CCValAssign &VA = RVLocs[i];
2737 assert(VA.isRegLoc() && "Can only return in registers!");
2738
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002739 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002740 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002741
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002742 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002743
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002744 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002745 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002746 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002747 }
2748
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002749 // The mips ABIs for returning structs by value requires that we copy
2750 // the sret argument into $v0 for the return. We saved the argument into
2751 // a virtual register in the entry block, so now we copy the value out
2752 // and into $v0.
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002753 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002754 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2755 unsigned Reg = MipsFI->getSRetReturnReg();
2756
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002757 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002758 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002759 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Akira Hatanaka2ef5bd32012-10-24 02:10:54 +00002760 unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002761
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002762 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002763 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002764 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002765 }
2766
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002767 RetOps[0] = Chain; // Update chain.
Akira Hatanaka182ef6f2012-07-10 00:19:06 +00002768
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002769 // Add the flag if we have it.
2770 if (Flag.getNode())
2771 RetOps.push_back(Flag);
2772
2773 // Return on Mips is always a "jr $ra"
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002774 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, &RetOps[0], RetOps.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002775}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002776
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002777//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002778// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002779//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002780
2781/// getConstraintType - Given a constraint letter, return the type of
2782/// constraint it is for this target.
2783MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002784getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002785{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002786 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002787 // GCC config/mips/constraints.md
2788 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002789 // 'd' : An address register. Equivalent to r
2790 // unless generating MIPS16 code.
2791 // 'y' : Equivalent to r; retained for
2792 // backwards compatibility.
Eric Christopher1d5a3922012-05-07 06:25:10 +00002793 // 'c' : A register suitable for use in an indirect
2794 // jump. This will always be $25 for -mabicalls.
Eric Christopheraf97f732012-05-07 06:25:19 +00002795 // 'l' : The lo register. 1 word storage.
2796 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002797 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002798 switch (Constraint[0]) {
2799 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002800 case 'd':
2801 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002802 case 'f':
Eric Christopher1d5a3922012-05-07 06:25:10 +00002803 case 'c':
Eric Christopher4adbefe2012-05-07 06:25:15 +00002804 case 'l':
Eric Christopheraf97f732012-05-07 06:25:19 +00002805 case 'x':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002806 return C_RegisterClass;
Jack Carter0b9675d2013-03-04 21:33:15 +00002807 case 'R':
2808 return C_Memory;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002809 }
2810 }
2811 return TargetLowering::getConstraintType(Constraint);
2812}
2813
John Thompson44ab89e2010-10-29 17:29:13 +00002814/// Examine constraint type and operand type and determine a weight value.
2815/// This object must already have been set up with the operand type
2816/// and the current alternative constraint selected.
2817TargetLowering::ConstraintWeight
2818MipsTargetLowering::getSingleConstraintMatchWeight(
2819 AsmOperandInfo &info, const char *constraint) const {
2820 ConstraintWeight weight = CW_Invalid;
2821 Value *CallOperandVal = info.CallOperandVal;
2822 // If we don't have a value, we can't do a match,
2823 // but allow it at the lowest weight.
2824 if (CallOperandVal == NULL)
2825 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002826 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002827 // Look at the constraint type.
2828 switch (*constraint) {
2829 default:
2830 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2831 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002832 case 'd':
2833 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002834 if (type->isIntegerTy())
2835 weight = CW_Register;
2836 break;
2837 case 'f':
2838 if (type->isFloatTy())
2839 weight = CW_Register;
2840 break;
Eric Christopher1d5a3922012-05-07 06:25:10 +00002841 case 'c': // $25 for indirect jumps
Eric Christopher4adbefe2012-05-07 06:25:15 +00002842 case 'l': // lo register
Eric Christopheraf97f732012-05-07 06:25:19 +00002843 case 'x': // hilo register pair
Eric Christopher1d5a3922012-05-07 06:25:10 +00002844 if (type->isIntegerTy())
2845 weight = CW_SpecificReg;
2846 break;
Eric Christopher50ab0392012-05-07 03:13:32 +00002847 case 'I': // signed 16 bit immediate
Eric Christophere5076d42012-05-07 03:13:42 +00002848 case 'J': // integer zero
Eric Christopherf49f8462012-05-07 05:46:29 +00002849 case 'K': // unsigned 16 bit immediate
Eric Christopher5ac47bb2012-05-07 05:46:37 +00002850 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christopher60cfc792012-05-07 05:46:43 +00002851 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher1ce20342012-05-07 05:46:48 +00002852 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopher54412a72012-05-07 06:25:02 +00002853 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher50ab0392012-05-07 03:13:32 +00002854 if (isa<ConstantInt>(CallOperandVal))
2855 weight = CW_Constant;
2856 break;
Jack Carter0b9675d2013-03-04 21:33:15 +00002857 case 'R':
2858 weight = CW_Memory;
2859 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002860 }
2861 return weight;
2862}
2863
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002864/// This is a helper function to parse a physical register string and split it
2865/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
2866/// that is returned indicates whether parsing was successful. The second flag
2867/// is true if the numeric part exists.
2868static std::pair<bool, bool>
2869parsePhysicalReg(const StringRef &C, std::string &Prefix,
2870 unsigned long long &Reg) {
2871 if (C.front() != '{' || C.back() != '}')
2872 return std::make_pair(false, false);
2873
2874 // Search for the first numeric character.
2875 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
2876 I = std::find_if(B, E, std::ptr_fun(isdigit));
2877
2878 Prefix.assign(B, I - B);
2879
2880 // The second flag is set to false if no numeric characters were found.
2881 if (I == E)
2882 return std::make_pair(true, false);
2883
2884 // Parse the numeric characters.
2885 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
2886 true);
2887}
2888
2889std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
2890parseRegForInlineAsmConstraint(const StringRef &C, MVT VT) const {
2891 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2892 const TargetRegisterClass *RC;
2893 std::string Prefix;
2894 unsigned long long Reg;
2895
2896 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
2897
2898 if (!R.first)
2899 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2900
2901 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
2902 // No numeric characters follow "hi" or "lo".
2903 if (R.second)
2904 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2905
2906 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002907 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002908 return std::make_pair(*(RC->begin()), RC);
2909 }
2910
2911 if (!R.second)
2912 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2913
2914 if (Prefix == "$f") { // Parse $f0-$f31.
2915 // If the size of FP registers is 64-bit or Reg is an even number, select
2916 // the 64-bit register class. Otherwise, select the 32-bit register class.
2917 if (VT == MVT::Other)
2918 VT = (Subtarget->isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
2919
2920 RC= getRegClassFor(VT);
2921
2922 if (RC == &Mips::AFGR64RegClass) {
2923 assert(Reg % 2 == 0);
2924 Reg >>= 1;
2925 }
2926 } else if (Prefix == "$fcc") { // Parse $fcc0-$fcc7.
2927 RC = TRI->getRegClass(Mips::FCCRegClassID);
2928 } else { // Parse $0-$31.
2929 assert(Prefix == "$");
2930 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
2931 }
2932
2933 assert(Reg < RC->getNumRegs());
2934 return std::make_pair(*(RC->begin() + Reg), RC);
2935}
2936
Eric Christopher38d64262011-06-29 19:33:04 +00002937/// Given a register class constraint, like 'r', if this corresponds directly
2938/// to an LLVM register class, return a register of 0 and the register class
2939/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002940std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier5b3fca52013-06-22 18:37:38 +00002941getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002942{
2943 if (Constraint.size() == 1) {
2944 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002945 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2946 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002947 case 'r':
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002948 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
2949 if (Subtarget->inMips16Mode())
2950 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka18587862013-08-06 23:08:38 +00002951 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002952 }
Jack Carter10de0252012-07-02 23:35:23 +00002953 if (VT == MVT::i64 && !HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002954 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002955 if (VT == MVT::i64 && HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002956 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002957 // This will generate an error message
2958 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002959 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002960 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002961 return std::make_pair(0U, &Mips::FGR32RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002962 if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
2963 if (Subtarget->isFP64bit())
Craig Topper420761a2012-04-20 07:30:17 +00002964 return std::make_pair(0U, &Mips::FGR64RegClass);
2965 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002966 }
Eric Christopher1d5a3922012-05-07 06:25:10 +00002967 break;
2968 case 'c': // register suitable for indirect jump
2969 if (VT == MVT::i32)
Akira Hatanaka18587862013-08-06 23:08:38 +00002970 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christopher1d5a3922012-05-07 06:25:10 +00002971 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka18587862013-08-06 23:08:38 +00002972 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher4adbefe2012-05-07 06:25:15 +00002973 case 'l': // register suitable for indirect jump
2974 if (VT == MVT::i32)
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002975 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
2976 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopheraf97f732012-05-07 06:25:19 +00002977 case 'x': // register suitable for indirect jump
2978 // Fixme: Not triggering the use of both hi and low
2979 // This will generate an error message
2980 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002981 }
2982 }
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002983
2984 std::pair<unsigned, const TargetRegisterClass *> R;
2985 R = parseRegForInlineAsmConstraint(Constraint, VT);
2986
2987 if (R.second)
2988 return R;
2989
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002990 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2991}
2992
Eric Christopher50ab0392012-05-07 03:13:32 +00002993/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
2994/// vector. If it is invalid, don't add anything to Ops.
2995void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
2996 std::string &Constraint,
2997 std::vector<SDValue>&Ops,
2998 SelectionDAG &DAG) const {
2999 SDValue Result(0, 0);
3000
3001 // Only support length 1 constraints for now.
3002 if (Constraint.length() > 1) return;
3003
3004 char ConstraintLetter = Constraint[0];
3005 switch (ConstraintLetter) {
3006 default: break; // This will fall through to the generic implementation
3007 case 'I': // Signed 16 bit constant
3008 // If this fails, the parent routine will give an error
3009 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3010 EVT Type = Op.getValueType();
3011 int64_t Val = C->getSExtValue();
3012 if (isInt<16>(Val)) {
3013 Result = DAG.getTargetConstant(Val, Type);
3014 break;
3015 }
3016 }
3017 return;
Eric Christophere5076d42012-05-07 03:13:42 +00003018 case 'J': // integer zero
3019 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3020 EVT Type = Op.getValueType();
3021 int64_t Val = C->getZExtValue();
3022 if (Val == 0) {
3023 Result = DAG.getTargetConstant(0, Type);
3024 break;
3025 }
3026 }
3027 return;
Eric Christopherf49f8462012-05-07 05:46:29 +00003028 case 'K': // unsigned 16 bit immediate
3029 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3030 EVT Type = Op.getValueType();
3031 uint64_t Val = (uint64_t)C->getZExtValue();
3032 if (isUInt<16>(Val)) {
3033 Result = DAG.getTargetConstant(Val, Type);
3034 break;
3035 }
3036 }
3037 return;
Eric Christopher5ac47bb2012-05-07 05:46:37 +00003038 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3039 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3040 EVT Type = Op.getValueType();
3041 int64_t Val = C->getSExtValue();
3042 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3043 Result = DAG.getTargetConstant(Val, Type);
3044 break;
3045 }
3046 }
3047 return;
Eric Christopher60cfc792012-05-07 05:46:43 +00003048 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3049 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3050 EVT Type = Op.getValueType();
3051 int64_t Val = C->getSExtValue();
3052 if ((Val >= -65535) && (Val <= -1)) {
3053 Result = DAG.getTargetConstant(Val, Type);
3054 break;
3055 }
3056 }
3057 return;
Eric Christopher1ce20342012-05-07 05:46:48 +00003058 case 'O': // signed 15 bit immediate
3059 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3060 EVT Type = Op.getValueType();
3061 int64_t Val = C->getSExtValue();
3062 if ((isInt<15>(Val))) {
3063 Result = DAG.getTargetConstant(Val, Type);
3064 break;
3065 }
3066 }
3067 return;
Eric Christopher54412a72012-05-07 06:25:02 +00003068 case 'P': // immediate in the range of 1 to 65535 (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 Christopher50ab0392012-05-07 03:13:32 +00003078 }
3079
3080 if (Result.getNode()) {
3081 Ops.push_back(Result);
3082 return;
3083 }
3084
3085 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3086}
3087
Dan Gohman6520e202008-10-18 02:06:02 +00003088bool
Akira Hatanaka94e47282012-11-17 00:25:41 +00003089MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM, Type *Ty) const {
3090 // No global is ever allowed as a base.
3091 if (AM.BaseGV)
3092 return false;
3093
3094 switch (AM.Scale) {
3095 case 0: // "r+i" or just "i", depending on HasBaseReg.
3096 break;
3097 case 1:
3098 if (!AM.HasBaseReg) // allow "r+i".
3099 break;
3100 return false; // disallow "r+r" or "r+r+i".
3101 default:
3102 return false;
3103 }
3104
3105 return true;
3106}
3107
3108bool
Dan Gohman6520e202008-10-18 02:06:02 +00003109MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3110 // The Mips target isn't yet aware of offsets.
3111 return false;
3112}
Evan Chengeb2f9692009-10-27 19:56:55 +00003113
Akira Hatanakae193b322012-06-13 19:33:32 +00003114EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003115 unsigned SrcAlign,
3116 bool IsMemset, bool ZeroMemset,
Akira Hatanakae193b322012-06-13 19:33:32 +00003117 bool MemcpyStrSrc,
3118 MachineFunction &MF) const {
3119 if (Subtarget->hasMips64())
3120 return MVT::i64;
3121
3122 return MVT::i32;
3123}
3124
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003125bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3126 if (VT != MVT::f32 && VT != MVT::f64)
3127 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00003128 if (Imm.isNegZero())
3129 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00003130 return Imm.isZero();
3131}
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003132
3133unsigned MipsTargetLowering::getJumpTableEncoding() const {
3134 if (IsN64)
3135 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liubb481f82012-02-28 07:46:26 +00003136
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003137 return TargetLowering::getJumpTableEncoding();
3138}
Akira Hatanaka7887c902012-10-26 23:56:38 +00003139
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003140/// This function returns true if CallSym is a long double emulation routine.
3141static bool isF128SoftLibCall(const char *CallSym) {
3142 const char *const LibCalls[] =
3143 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3144 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3145 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3146 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3147 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3148 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3149 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3150 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3151 "truncl"};
3152
3153 const char * const *End = LibCalls + array_lengthof(LibCalls);
3154
3155 // Check that LibCalls is sorted alphabetically.
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003156 MipsTargetLowering::LTStr Comp;
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003157
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003158#ifndef NDEBUG
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003159 for (const char * const *I = LibCalls; I < End - 1; ++I)
3160 assert(Comp(*I, *(I + 1)));
3161#endif
3162
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003163 return std::binary_search(LibCalls, End, CallSym, Comp);
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003164}
3165
3166/// This function returns true if Ty is fp128 or i128 which was originally a
3167/// fp128.
3168static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3169 if (Ty->isFP128Ty())
3170 return true;
3171
3172 const ExternalSymbolSDNode *ES =
3173 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3174
3175 // If the Ty is i128 and the function being called is a long double emulation
3176 // routine, then the original type is f128.
3177 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3178}
3179
Reed Kotler46090912013-05-10 22:25:39 +00003180MipsTargetLowering::MipsCC::SpecialCallingConvType
3181 MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
3182 MipsCC::SpecialCallingConvType SpecialCallingConv =
3183 MipsCC::NoSpecialCallingConv;;
3184 if (Subtarget->inMips16HardFloat()) {
3185 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3186 llvm::StringRef Sym = G->getGlobal()->getName();
3187 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
3188 if (F->hasFnAttribute("__Mips16RetHelper")) {
3189 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3190 }
3191 }
3192 }
3193 return SpecialCallingConv;
3194}
3195
3196MipsTargetLowering::MipsCC::MipsCC(
Akira Hatanakaad341d42013-08-20 23:38:40 +00003197 CallingConv::ID CC, bool IsO32_, bool IsFP64_, CCState &Info,
Reed Kotler46090912013-05-10 22:25:39 +00003198 MipsCC::SpecialCallingConvType SpecialCallingConv_)
Akira Hatanakaad341d42013-08-20 23:38:40 +00003199 : CCInfo(Info), CallConv(CC), IsO32(IsO32_), IsFP64(IsFP64_),
Reed Kotler46090912013-05-10 22:25:39 +00003200 SpecialCallingConv(SpecialCallingConv_){
Akira Hatanaka7887c902012-10-26 23:56:38 +00003201 // Pre-allocate reserved argument area.
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003202 CCInfo.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka7887c902012-10-26 23:56:38 +00003203}
3204
Reed Kotler46090912013-05-10 22:25:39 +00003205
Akira Hatanaka7887c902012-10-26 23:56:38 +00003206void MipsTargetLowering::MipsCC::
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003207analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003208 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
3209 std::vector<ArgListEntry> &FuncArgs) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003210 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3211 "CallingConv::Fast shouldn't be used for vararg functions.");
3212
Akira Hatanaka7887c902012-10-26 23:56:38 +00003213 unsigned NumOpnds = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003214 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003215
3216 for (unsigned I = 0; I != NumOpnds; ++I) {
3217 MVT ArgVT = Args[I].VT;
3218 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3219 bool R;
3220
3221 if (ArgFlags.isByVal()) {
3222 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3223 continue;
3224 }
3225
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003226 if (IsVarArg && !Args[I].IsFixed)
Akira Hatanaka7887c902012-10-26 23:56:38 +00003227 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003228 else {
3229 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3230 IsSoftFloat);
3231 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
3232 }
Akira Hatanaka7887c902012-10-26 23:56:38 +00003233
3234 if (R) {
3235#ifndef NDEBUG
3236 dbgs() << "Call operand #" << I << " has unhandled type "
3237 << EVT(ArgVT).getEVTString();
3238#endif
3239 llvm_unreachable(0);
3240 }
3241 }
3242}
3243
3244void MipsTargetLowering::MipsCC::
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003245analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
3246 bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
Akira Hatanaka7887c902012-10-26 23:56:38 +00003247 unsigned NumArgs = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003248 llvm::CCAssignFn *FixedFn = fixedArgFn();
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003249 unsigned CurArgIdx = 0;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003250
3251 for (unsigned I = 0; I != NumArgs; ++I) {
3252 MVT ArgVT = Args[I].VT;
3253 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003254 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3255 CurArgIdx = Args[I].OrigArgIndex;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003256
3257 if (ArgFlags.isByVal()) {
3258 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3259 continue;
3260 }
3261
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003262 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), 0, IsSoftFloat);
3263
3264 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
Akira Hatanaka7887c902012-10-26 23:56:38 +00003265 continue;
3266
3267#ifndef NDEBUG
3268 dbgs() << "Formal Arg #" << I << " has unhandled type "
3269 << EVT(ArgVT).getEVTString();
3270#endif
3271 llvm_unreachable(0);
3272 }
3273}
3274
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003275template<typename Ty>
3276void MipsTargetLowering::MipsCC::
3277analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
3278 const SDNode *CallNode, const Type *RetTy) const {
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003279 CCAssignFn *Fn;
3280
3281 if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
3282 Fn = RetCC_F128Soft;
3283 else
3284 Fn = RetCC_Mips;
3285
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003286 for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
3287 MVT VT = RetVals[I].VT;
3288 ISD::ArgFlagsTy Flags = RetVals[I].Flags;
3289 MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
3290
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003291 if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003292#ifndef NDEBUG
3293 dbgs() << "Call result #" << I << " has unhandled type "
3294 << EVT(VT).getEVTString() << '\n';
3295#endif
3296 llvm_unreachable(0);
3297 }
3298 }
3299}
3300
3301void MipsTargetLowering::MipsCC::
3302analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
3303 const SDNode *CallNode, const Type *RetTy) const {
3304 analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
3305}
3306
3307void MipsTargetLowering::MipsCC::
3308analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
3309 const Type *RetTy) const {
3310 analyzeReturn(Outs, IsSoftFloat, 0, RetTy);
3311}
3312
Akira Hatanaka7887c902012-10-26 23:56:38 +00003313void
3314MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3315 MVT LocVT,
3316 CCValAssign::LocInfo LocInfo,
3317 ISD::ArgFlagsTy ArgFlags) {
3318 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3319
3320 struct ByValArgInfo ByVal;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003321 unsigned RegSize = regSize();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003322 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3323 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3324 RegSize * 2);
3325
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003326 if (useRegsForByval())
Akira Hatanaka7887c902012-10-26 23:56:38 +00003327 allocateRegs(ByVal, ByValSize, Align);
3328
3329 // Allocate space on caller's stack.
3330 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3331 Align);
3332 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3333 LocInfo));
3334 ByValArgs.push_back(ByVal);
3335}
3336
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003337unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
3338 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
3339}
3340
3341unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
3342 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
3343}
3344
3345const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const {
3346 return IsO32 ? O32IntRegs : Mips64IntRegs;
3347}
3348
3349llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3350 if (CallConv == CallingConv::Fast)
3351 return CC_Mips_FastCC;
3352
Reed Kotler46090912013-05-10 22:25:39 +00003353 if (SpecialCallingConv == Mips16RetHelperConv)
3354 return CC_Mips16RetHelper;
Akira Hatanakaad341d42013-08-20 23:38:40 +00003355 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003356}
3357
3358llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
Akira Hatanakaad341d42013-08-20 23:38:40 +00003359 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN_VarArg;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003360}
3361
3362const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const {
3363 return IsO32 ? O32IntRegs : Mips64DPRegs;
3364}
3365
Akira Hatanaka7887c902012-10-26 23:56:38 +00003366void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3367 unsigned ByValSize,
3368 unsigned Align) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003369 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
3370 const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003371 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3372 "Byval argument's size and alignment should be a multiple of"
3373 "RegSize.");
3374
3375 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3376
3377 // If Align > RegSize, the first arg register must be even.
3378 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3379 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3380 ++ByVal.FirstIdx;
3381 }
3382
3383 // Mark the registers allocated.
3384 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3385 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3386 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3387}
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003388
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003389MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3390 const SDNode *CallNode,
3391 bool IsSoftFloat) const {
3392 if (IsSoftFloat || IsO32)
3393 return VT;
3394
3395 // Check if the original type was fp128.
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003396 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003397 assert(VT == MVT::i64);
3398 return MVT::f64;
3399 }
3400
3401 return VT;
3402}
3403
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003404void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003405copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003406 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3407 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3408 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3409 MachineFunction &MF = DAG.getMachineFunction();
3410 MachineFrameInfo *MFI = MF.getFrameInfo();
3411 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
3412 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3413 int FrameObjOffset;
3414
3415 if (RegAreaSize)
3416 FrameObjOffset = (int)CC.reservedArgArea() -
3417 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
3418 else
3419 FrameObjOffset = ByVal.Address;
3420
3421 // Create frame object.
3422 EVT PtrTy = getPointerTy();
3423 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3424 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3425 InVals.push_back(FIN);
3426
3427 if (!ByVal.NumRegs)
3428 return;
3429
3430 // Copy arg registers.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003431 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003432 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3433
3434 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3435 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003436 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003437 unsigned Offset = I * CC.regSize();
3438 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3439 DAG.getConstant(Offset, PtrTy));
3440 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3441 StorePtr, MachinePointerInfo(FuncArg, Offset),
3442 false, false, 0);
3443 OutChains.push_back(Store);
3444 }
3445}
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003446
3447// Copy byVal arg to registers and stack.
3448void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003449passByValArg(SDValue Chain, SDLoc DL,
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00003450 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Craig Toppera0ec3f92013-07-14 04:42:23 +00003451 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003452 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3453 const MipsCC &CC, const ByValArgInfo &ByVal,
3454 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
3455 unsigned ByValSize = Flags.getByValSize();
3456 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
3457 unsigned RegSize = CC.regSize();
3458 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
3459 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
3460
3461 if (ByVal.NumRegs) {
3462 const uint16_t *ArgRegs = CC.intArgRegs();
3463 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
3464 unsigned I = 0;
3465
3466 // Copy words to registers.
3467 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
3468 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3469 DAG.getConstant(Offset, PtrTy));
3470 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3471 MachinePointerInfo(), false, false, false,
3472 Alignment);
3473 MemOpChains.push_back(LoadVal.getValue(1));
3474 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3475 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3476 }
3477
3478 // Return if the struct has been fully copied.
3479 if (ByValSize == Offset)
3480 return;
3481
3482 // Copy the remainder of the byval argument with sub-word loads and shifts.
3483 if (LeftoverBytes) {
3484 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
3485 "Size of the remainder should be smaller than RegSize.");
3486 SDValue Val;
3487
3488 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
3489 Offset < ByValSize; LoadSize /= 2) {
3490 unsigned RemSize = ByValSize - Offset;
3491
3492 if (RemSize < LoadSize)
3493 continue;
3494
3495 // Load subword.
3496 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3497 DAG.getConstant(Offset, PtrTy));
3498 SDValue LoadVal =
3499 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
3500 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
3501 false, false, Alignment);
3502 MemOpChains.push_back(LoadVal.getValue(1));
3503
3504 // Shift the loaded value.
3505 unsigned Shamt;
3506
3507 if (isLittle)
3508 Shamt = TotalSizeLoaded;
3509 else
3510 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
3511
3512 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3513 DAG.getConstant(Shamt, MVT::i32));
3514
3515 if (Val.getNode())
3516 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3517 else
3518 Val = Shift;
3519
3520 Offset += LoadSize;
3521 TotalSizeLoaded += LoadSize;
3522 Alignment = std::min(Alignment, LoadSize);
3523 }
3524
3525 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3526 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3527 return;
3528 }
3529 }
3530
3531 // Copy remainder of byval arg to it with memcpy.
3532 unsigned MemCpySize = ByValSize - Offset;
3533 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3534 DAG.getConstant(Offset, PtrTy));
3535 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3536 DAG.getIntPtrConstant(ByVal.Address));
3537 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3538 DAG.getConstant(MemCpySize, PtrTy), Alignment,
3539 /*isVolatile=*/false, /*AlwaysInline=*/false,
3540 MachinePointerInfo(0), MachinePointerInfo(0));
3541 MemOpChains.push_back(Chain);
3542}
Akira Hatanakaf0848472012-10-27 00:21:13 +00003543
3544void
3545MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3546 const MipsCC &CC, SDValue Chain,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003547 SDLoc DL, SelectionDAG &DAG) const {
Akira Hatanakaf0848472012-10-27 00:21:13 +00003548 unsigned NumRegs = CC.numIntArgRegs();
3549 const uint16_t *ArgRegs = CC.intArgRegs();
3550 const CCState &CCInfo = CC.getCCInfo();
3551 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
3552 unsigned RegSize = CC.regSize();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003553 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003554 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3555 MachineFunction &MF = DAG.getMachineFunction();
3556 MachineFrameInfo *MFI = MF.getFrameInfo();
3557 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3558
3559 // Offset of the first variable argument from stack pointer.
3560 int VaArgOffset;
3561
3562 if (NumRegs == Idx)
3563 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
3564 else
3565 VaArgOffset =
3566 (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
3567
3568 // Record the frame index of the first variable argument
3569 // which is a value necessary to VASTART.
3570 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3571 MipsFI->setVarArgsFrameIndex(FI);
3572
3573 // Copy the integer registers that have not been used for argument passing
3574 // to the argument register save area. For O32, the save area is allocated
3575 // in the caller's stack frame, while for N32/64, it is allocated in the
3576 // callee's stack frame.
3577 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003578 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003579 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3580 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3581 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3582 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3583 MachinePointerInfo(), false, false, 0);
3584 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
3585 OutChains.push_back(Store);
3586 }
3587}