blob: 04f90833d82790066443956a3c67c1522e5a5c0c [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";
Daniel Sanders3c380d52013-08-28 12:14:50 +0000211 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
212 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
213 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
214 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersae1fb8f2013-09-24 10:46:19 +0000215 case MipsISD::VCEQ: return "MipsISD::VCEQ";
216 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
217 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
218 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
219 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders89d13c12013-09-24 12:18:31 +0000220 case MipsISD::VSMAX: return "MipsISD::VSMAX";
221 case MipsISD::VSMIN: return "MipsISD::VSMIN";
222 case MipsISD::VUMAX: return "MipsISD::VUMAX";
223 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +0000224 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
225 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sanders915432c2013-09-23 13:22:24 +0000226 case MipsISD::VNOR: return "MipsISD::VNOR";
Daniel Sanders7e0df9a2013-09-24 14:02:15 +0000227 case MipsISD::VSHF: return "MipsISD::VSHF";
Akira Hatanaka0f843822011-06-07 18:58:42 +0000228 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000229 }
230}
231
232MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +0000233MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka8b4198d2011-09-26 21:47:02 +0000234 : TargetLowering(TM, new MipsTargetObjectFile()),
235 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +0000236 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
237 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000238 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000239 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +0000240 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000241 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000242
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000243 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
245 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
246 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000247
Eli Friedman6055a6a2009-07-17 04:07:24 +0000248 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
250 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000251
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000252 // Used by legalize types to correctly generate the setcc result.
253 // Without this, every float setcc comes with a AND/OR with the result,
254 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000255 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000257
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000258 // Mips Custom Operations
Akira Hatanakab7656a92013-03-06 21:32:03 +0000259 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000260 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000261 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
263 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
264 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
265 setOperationAction(ISD::SELECT, MVT::f32, Custom);
266 setOperationAction(ISD::SELECT, MVT::f64, Custom);
267 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +0000268 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
269 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanaka0a40c232012-03-09 23:46:03 +0000270 setOperationAction(ISD::SETCC, MVT::f32, Custom);
271 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000273 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000274 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
275 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000276 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000277
Akira Hatanakac12a6e62012-04-11 22:49:04 +0000278 if (!TM.Options.NoNaNsFPMath) {
279 setOperationAction(ISD::FABS, MVT::f32, Custom);
280 setOperationAction(ISD::FABS, MVT::f64, Custom);
281 }
282
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000283 if (HasMips64) {
284 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
285 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
286 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
287 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
288 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
289 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka7664f052012-06-02 00:04:42 +0000290 setOperationAction(ISD::LOAD, MVT::i64, Custom);
291 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000292 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000293 }
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000294
Akira Hatanakaa284acb2012-05-09 00:55:21 +0000295 if (!HasMips64) {
296 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
297 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
298 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
299 }
300
Akira Hatanakae90a3bc2012-11-07 19:10:58 +0000301 setOperationAction(ISD::ADD, MVT::i32, Custom);
302 if (HasMips64)
303 setOperationAction(ISD::ADD, MVT::i64, Custom);
304
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000305 setOperationAction(ISD::SDIV, MVT::i32, Expand);
306 setOperationAction(ISD::SREM, MVT::i32, Expand);
307 setOperationAction(ISD::UDIV, MVT::i32, Expand);
308 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000309 setOperationAction(ISD::SDIV, MVT::i64, Expand);
310 setOperationAction(ISD::SREM, MVT::i64, Expand);
311 setOperationAction(ISD::UDIV, MVT::i64, Expand);
312 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000313
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000314 // Operations not directly supported by Mips.
Tom Stellard3ef53832013-03-08 15:36:57 +0000315 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
316 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
317 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
318 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
320 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000321 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000323 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
325 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000326 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000328 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000329 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
330 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
331 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
332 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000334 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka1d165f12012-07-31 20:54:48 +0000335 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
336 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000337
Akira Hatanaka56633442011-09-20 23:53:09 +0000338 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000339 setOperationAction(ISD::ROTR, MVT::i32, Expand);
340
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000341 if (!Subtarget->hasMips64r2())
342 setOperationAction(ISD::ROTR, MVT::i64, Expand);
343
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000345 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000346 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000347 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000348 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
349 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000350 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
351 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000352 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000353 setOperationAction(ISD::FLOG, MVT::f32, Expand);
354 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
355 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
356 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000357 setOperationAction(ISD::FMA, MVT::f32, Expand);
358 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka21ecc2f2012-03-29 18:43:11 +0000359 setOperationAction(ISD::FREM, MVT::f32, Expand);
360 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000361
Akira Hatanaka1cc63332012-04-11 22:59:08 +0000362 if (!TM.Options.NoNaNsFPMath) {
363 setOperationAction(ISD::FNEG, MVT::f32, Expand);
364 setOperationAction(ISD::FNEG, MVT::f64, Expand);
365 }
366
Akira Hatanaka544cc212013-01-30 00:26:49 +0000367 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
368
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000369 setOperationAction(ISD::VAARG, MVT::Other, Expand);
370 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
371 setOperationAction(ISD::VAEND, MVT::Other, Expand);
372
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000373 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
375 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000376
Jia Liubb481f82012-02-28 07:46:26 +0000377 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
378 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
379 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
380 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman4db5aca2011-08-29 18:23:02 +0000381
Eli Friedman26689ac2011-08-03 21:06:02 +0000382 setInsertFencesForAtomic(true);
383
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000384 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
386 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000387 }
388
Akira Hatanakac79507a2011-12-21 00:20:27 +0000389 if (!Subtarget->hasBitCount()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000390 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Akira Hatanakac79507a2011-12-21 00:20:27 +0000391 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
392 }
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000393
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000394 if (!Subtarget->hasSwap()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000395 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000396 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
397 }
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000398
Akira Hatanaka7664f052012-06-02 00:04:42 +0000399 if (HasMips64) {
400 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
401 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
402 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
403 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
404 }
405
Akira Hatanaka97585622013-07-26 20:58:55 +0000406 setOperationAction(ISD::TRAP, MVT::Other, Legal);
407
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000408 setTargetDAGCombine(ISD::SDIVREM);
409 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanakaee8c3b02012-03-08 03:26:37 +0000410 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000411 setTargetDAGCombine(ISD::AND);
412 setTargetDAGCombine(ISD::OR);
Akira Hatanaka87827072012-06-13 20:33:18 +0000413 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000414
Akira Hatanaka5fdf5002012-03-08 01:59:33 +0000415 setMinFunctionAlignment(HasMips64 ? 3 : 2);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000416
Akira Hatanaka3f5b1072012-02-02 03:17:04 +0000417 setStackPointerRegisterToSaveRestore(IsN64 ? Mips::SP_64 : Mips::SP);
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000418
Akira Hatanaka590baca2012-02-02 03:13:40 +0000419 setExceptionPointerRegister(IsN64 ? Mips::A0_64 : Mips::A0);
420 setExceptionSelectorRegister(IsN64 ? Mips::A1_64 : Mips::A1);
Akira Hatanakae193b322012-06-13 19:33:32 +0000421
Jim Grosbach3450f802013-02-20 21:13:59 +0000422 MaxStoresPerMemcpy = 16;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000423}
424
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000425const MipsTargetLowering *MipsTargetLowering::create(MipsTargetMachine &TM) {
426 if (TM.getSubtargetImpl()->inMips16Mode())
427 return llvm::createMips16TargetLowering(TM);
Jia Liubb481f82012-02-28 07:46:26 +0000428
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000429 return llvm::createMipsSETargetLowering(TM);
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000430}
431
Matt Arsenault225ed702013-05-18 00:21:46 +0000432EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakae13f4412013-01-04 20:06:01 +0000433 if (!VT.isVector())
434 return MVT::i32;
435 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +0000436}
437
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000438static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000439 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000440 const MipsSubtarget *Subtarget) {
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000441 if (DCI.isBeforeLegalizeOps())
442 return SDValue();
443
Akira Hatanakadda4a072011-10-03 21:06:13 +0000444 EVT Ty = N->getValueType(0);
Akira Hatanakacbaf6d02013-08-14 00:47:08 +0000445 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
446 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000447 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
448 MipsISD::DivRemU16;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000449 SDLoc DL(N);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000450
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000451 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000452 N->getOperand(0), N->getOperand(1));
453 SDValue InChain = DAG.getEntryNode();
454 SDValue InGlue = DivRem;
455
456 // insert MFLO
457 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000458 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000459 InGlue);
460 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
461 InChain = CopyFromLo.getValue(1);
462 InGlue = CopyFromLo.getValue(2);
463 }
464
465 // insert MFHI
466 if (N->hasAnyUseOfValue(1)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000467 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000468 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000469 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
470 }
471
472 return SDValue();
473}
474
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000475static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000476 switch (CC) {
477 default: llvm_unreachable("Unknown fp condition code!");
478 case ISD::SETEQ:
479 case ISD::SETOEQ: return Mips::FCOND_OEQ;
480 case ISD::SETUNE: return Mips::FCOND_UNE;
481 case ISD::SETLT:
482 case ISD::SETOLT: return Mips::FCOND_OLT;
483 case ISD::SETGT:
484 case ISD::SETOGT: return Mips::FCOND_OGT;
485 case ISD::SETLE:
486 case ISD::SETOLE: return Mips::FCOND_OLE;
487 case ISD::SETGE:
488 case ISD::SETOGE: return Mips::FCOND_OGE;
489 case ISD::SETULT: return Mips::FCOND_ULT;
490 case ISD::SETULE: return Mips::FCOND_ULE;
491 case ISD::SETUGT: return Mips::FCOND_UGT;
492 case ISD::SETUGE: return Mips::FCOND_UGE;
493 case ISD::SETUO: return Mips::FCOND_UN;
494 case ISD::SETO: return Mips::FCOND_OR;
495 case ISD::SETNE:
496 case ISD::SETONE: return Mips::FCOND_ONE;
497 case ISD::SETUEQ: return Mips::FCOND_UEQ;
498 }
499}
500
501
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000502/// This function returns true if the floating point conditional branches and
503/// conditional moves which use condition code CC should be inverted.
504static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000505 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
506 return false;
507
Akira Hatanaka82099682011-12-19 19:52:25 +0000508 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
509 "Illegal Condition Code");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000510
Akira Hatanaka82099682011-12-19 19:52:25 +0000511 return true;
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000512}
513
514// Creates and returns an FPCmp node from a setcc node.
515// Returns Op if setcc is not a floating point comparison.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000516static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000517 // must be a SETCC node
518 if (Op.getOpcode() != ISD::SETCC)
519 return Op;
520
521 SDValue LHS = Op.getOperand(0);
522
523 if (!LHS.getValueType().isFloatingPoint())
524 return Op;
525
526 SDValue RHS = Op.getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000527 SDLoc DL(Op);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000528
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000529 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
530 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000531 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
532
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000533 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000534 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000535}
536
537// Creates and returns a CMovFPT/F node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000538static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000539 SDValue False, SDLoc DL) {
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000540 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
541 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka407883b2013-07-26 20:51:20 +0000542 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000543
544 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka407883b2013-07-26 20:51:20 +0000545 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000546}
547
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000548static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000549 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000550 const MipsSubtarget *Subtarget) {
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000551 if (DCI.isBeforeLegalizeOps())
552 return SDValue();
553
554 SDValue SetCC = N->getOperand(0);
555
556 if ((SetCC.getOpcode() != ISD::SETCC) ||
557 !SetCC.getOperand(0).getValueType().isInteger())
558 return SDValue();
559
560 SDValue False = N->getOperand(2);
561 EVT FalseTy = False.getValueType();
562
563 if (!FalseTy.isInteger())
564 return SDValue();
565
566 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(False);
567
568 if (!CN || CN->getZExtValue())
569 return SDValue();
570
Andrew Trickac6d9be2013-05-25 02:42:55 +0000571 const SDLoc DL(N);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000572 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
573 SDValue True = N->getOperand(1);
Akira Hatanaka864f6602012-06-14 21:10:56 +0000574
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000575 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
576 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
Akira Hatanaka864f6602012-06-14 21:10:56 +0000577
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000578 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
579}
580
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000581static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000582 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000583 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000584 // Pattern match EXT.
585 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
586 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000587 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000588 return SDValue();
589
590 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000591 unsigned ShiftRightOpc = ShiftRight.getOpcode();
592
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000593 // Op's first operand must be a shift right.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000594 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000595 return SDValue();
596
597 // The second operand of the shift must be an immediate.
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000598 ConstantSDNode *CN;
599 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
600 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000601
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000602 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000603 uint64_t SMPos, SMSize;
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000604
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000605 // Op's second operand must be a shifted mask.
606 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000607 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000608 return SDValue();
609
610 // Return if the shifted mask does not start at bit 0 or the sum of its size
611 // and Pos exceeds the word's size.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000612 EVT ValTy = N->getValueType(0);
613 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000614 return SDValue();
615
Andrew Trickac6d9be2013-05-25 02:42:55 +0000616 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka82099682011-12-19 19:52:25 +0000617 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000618 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000619}
Jia Liubb481f82012-02-28 07:46:26 +0000620
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000621static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000622 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000623 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000624 // Pattern match INS.
625 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liubb481f82012-02-28 07:46:26 +0000626 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000627 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000628 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000629 return SDValue();
630
631 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
632 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
633 ConstantSDNode *CN;
634
635 // See if Op's first operand matches (and $src1 , mask0).
636 if (And0.getOpcode() != ISD::AND)
637 return SDValue();
638
639 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000640 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000641 return SDValue();
642
643 // See if Op's second operand matches (and (shl $src, pos), mask1).
644 if (And1.getOpcode() != ISD::AND)
645 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000646
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000647 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000648 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000649 return SDValue();
650
651 // The shift masks must have the same position and size.
652 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
653 return SDValue();
654
655 SDValue Shl = And1.getOperand(0);
656 if (Shl.getOpcode() != ISD::SHL)
657 return SDValue();
658
659 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
660 return SDValue();
661
662 unsigned Shamt = CN->getZExtValue();
663
664 // Return if the shift amount and the first bit position of mask are not the
Jia Liubb481f82012-02-28 07:46:26 +0000665 // same.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000666 EVT ValTy = N->getValueType(0);
667 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000668 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000669
Andrew Trickac6d9be2013-05-25 02:42:55 +0000670 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000671 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka82099682011-12-19 19:52:25 +0000672 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000673}
Jia Liubb481f82012-02-28 07:46:26 +0000674
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000675static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka87827072012-06-13 20:33:18 +0000676 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000677 const MipsSubtarget *Subtarget) {
Akira Hatanaka87827072012-06-13 20:33:18 +0000678 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
679
680 if (DCI.isBeforeLegalizeOps())
681 return SDValue();
682
683 SDValue Add = N->getOperand(1);
684
685 if (Add.getOpcode() != ISD::ADD)
686 return SDValue();
687
688 SDValue Lo = Add.getOperand(1);
689
690 if ((Lo.getOpcode() != MipsISD::Lo) ||
691 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
692 return SDValue();
693
694 EVT ValTy = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000695 SDLoc DL(N);
Akira Hatanaka87827072012-06-13 20:33:18 +0000696
697 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
698 Add.getOperand(0));
699 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
700}
701
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000702SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000703 const {
704 SelectionDAG &DAG = DCI.DAG;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000705 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000706
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000707 switch (Opc) {
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000708 default: break;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000709 case ISD::SDIVREM:
710 case ISD::UDIVREM:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000711 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000712 case ISD::SELECT:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000713 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000714 case ISD::AND:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000715 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000716 case ISD::OR:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000717 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka87827072012-06-13 20:33:18 +0000718 case ISD::ADD:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000719 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000720 }
721
722 return SDValue();
723}
724
Akira Hatanakab430cec2012-09-21 23:58:31 +0000725void
726MipsTargetLowering::LowerOperationWrapper(SDNode *N,
727 SmallVectorImpl<SDValue> &Results,
728 SelectionDAG &DAG) const {
729 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
730
731 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
732 Results.push_back(Res.getValue(I));
733}
734
735void
736MipsTargetLowering::ReplaceNodeResults(SDNode *N,
737 SmallVectorImpl<SDValue> &Results,
738 SelectionDAG &DAG) const {
Akira Hatanaka13ec4812013-04-30 21:17:07 +0000739 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakab430cec2012-09-21 23:58:31 +0000740}
741
Dan Gohman475871a2008-07-27 21:46:04 +0000742SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000743LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000744{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000745 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000746 {
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000747 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
748 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
749 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
750 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
751 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
752 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
753 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
754 case ISD::SELECT: return lowerSELECT(Op, DAG);
755 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
756 case ISD::SETCC: return lowerSETCC(Op, DAG);
757 case ISD::VASTART: return lowerVASTART(Op, DAG);
758 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
759 case ISD::FABS: return lowerFABS(Op, DAG);
760 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
761 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
762 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000763 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
764 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
765 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
766 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
767 case ISD::LOAD: return lowerLOAD(Op, DAG);
768 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000769 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000770 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000771 }
Dan Gohman475871a2008-07-27 21:46:04 +0000772 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000773}
774
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000775//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000776// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000777//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000778
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000779// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000780// MachineFunction as a live in value. It also creates a corresponding
781// virtual register for it.
782static unsigned
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000783addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000784{
Chris Lattner84bc5422007-12-31 04:13:23 +0000785 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
786 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000787 return VReg;
788}
789
Akira Hatanakaf8941992013-05-20 18:07:43 +0000790static MachineBasicBlock *expandPseudoDIV(MachineInstr *MI,
791 MachineBasicBlock &MBB,
792 const TargetInstrInfo &TII,
793 bool Is64Bit) {
794 if (NoZeroDivCheck)
795 return &MBB;
796
797 // Insert instruction "teq $divisor_reg, $zero, 7".
798 MachineBasicBlock::iterator I(MI);
799 MachineInstrBuilder MIB;
800 MIB = BuildMI(MBB, llvm::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
801 .addOperand(MI->getOperand(2)).addReg(Mips::ZERO).addImm(7);
802
803 // Use the 32-bit sub-register if this is a 64-bit division.
804 if (Is64Bit)
805 MIB->getOperand(0).setSubReg(Mips::sub_32);
806
807 return &MBB;
808}
809
Akira Hatanaka01f70892012-09-27 02:15:57 +0000810MachineBasicBlock *
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000811MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000812 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000813 switch (MI->getOpcode()) {
Reed Kotlerffbe4322013-02-21 04:22:38 +0000814 default:
815 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000816 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000817 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000818 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000819 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000820 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000821 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000822 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000823 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000824
825 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000826 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000827 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000828 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000829 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000830 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000831 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000832 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000833
834 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000835 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000836 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000837 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000838 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000839 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000840 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000841 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000842
843 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000844 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000845 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000846 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000847 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000848 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000849 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000850 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000851
852 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000853 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000854 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000855 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000856 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000857 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000858 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000859 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000860
861 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000862 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000863 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000864 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000865 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000866 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000867 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000868 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000869
870 case Mips::ATOMIC_SWAP_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000871 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000872 case Mips::ATOMIC_SWAP_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000873 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000874 case Mips::ATOMIC_SWAP_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000875 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000876 case Mips::ATOMIC_SWAP_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000877 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000878
879 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000880 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000881 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000882 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000883 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000884 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000885 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000886 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanakaf8941992013-05-20 18:07:43 +0000887 case Mips::PseudoSDIV:
888 case Mips::PseudoUDIV:
889 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), false);
890 case Mips::PseudoDSDIV:
891 case Mips::PseudoDUDIV:
892 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), true);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000893 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000894}
895
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000896// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
897// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
898MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000899MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000900 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000901 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000902 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000903
904 MachineFunction *MF = BB->getParent();
905 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000906 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000907 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000908 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000909 unsigned LL, SC, AND, NOR, ZERO, BEQ;
910
911 if (Size == 4) {
Akira Hatanakaa98a4862013-08-20 21:08:22 +0000912 LL = Mips::LL;
913 SC = Mips::SC;
Akira Hatanaka59068062011-11-11 04:14:30 +0000914 AND = Mips::AND;
915 NOR = Mips::NOR;
916 ZERO = Mips::ZERO;
917 BEQ = Mips::BEQ;
918 }
919 else {
Akira Hatanakaa98a4862013-08-20 21:08:22 +0000920 LL = Mips::LLD;
921 SC = Mips::SCD;
Akira Hatanaka59068062011-11-11 04:14:30 +0000922 AND = Mips::AND64;
923 NOR = Mips::NOR64;
924 ZERO = Mips::ZERO_64;
925 BEQ = Mips::BEQ64;
926 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000927
Akira Hatanaka4061da12011-07-19 20:11:17 +0000928 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000929 unsigned Ptr = MI->getOperand(1).getReg();
930 unsigned Incr = MI->getOperand(2).getReg();
931
Akira Hatanaka4061da12011-07-19 20:11:17 +0000932 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
933 unsigned AndRes = RegInfo.createVirtualRegister(RC);
934 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000935
936 // insert new blocks after the current block
937 const BasicBlock *LLVM_BB = BB->getBasicBlock();
938 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
939 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
940 MachineFunction::iterator It = BB;
941 ++It;
942 MF->insert(It, loopMBB);
943 MF->insert(It, exitMBB);
944
945 // Transfer the remainder of BB and its successor edges to exitMBB.
946 exitMBB->splice(exitMBB->begin(), BB,
947 llvm::next(MachineBasicBlock::iterator(MI)),
948 BB->end());
949 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
950
951 // thisMBB:
952 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000953 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000954 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000955 loopMBB->addSuccessor(loopMBB);
956 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000957
958 // loopMBB:
959 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000960 // <binop> storeval, oldval, incr
961 // sc success, storeval, 0(ptr)
962 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000963 BB = loopMBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000964 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000965 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000966 // and andres, oldval, incr
967 // nor storeval, $0, andres
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000968 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
969 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000970 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000971 // <binop> storeval, oldval, incr
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000972 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000973 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000974 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000975 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000976 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
977 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000978
979 MI->eraseFromParent(); // The instruction is gone now.
980
Akira Hatanaka939ece12011-07-19 03:42:13 +0000981 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000982}
983
984MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000985MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000986 MachineBasicBlock *BB,
987 unsigned Size, unsigned BinOpcode,
988 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000989 assert((Size == 1 || Size == 2) &&
990 "Unsupported size for EmitAtomicBinaryPartial.");
991
992 MachineFunction *MF = BB->getParent();
993 MachineRegisterInfo &RegInfo = MF->getRegInfo();
994 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
995 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000996 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000997
998 unsigned Dest = MI->getOperand(0).getReg();
999 unsigned Ptr = MI->getOperand(1).getReg();
1000 unsigned Incr = MI->getOperand(2).getReg();
1001
Akira Hatanaka4061da12011-07-19 20:11:17 +00001002 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1003 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001004 unsigned Mask = RegInfo.createVirtualRegister(RC);
1005 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001006 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1007 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001008 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001009 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1010 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1011 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1012 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1013 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001014 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001015 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1016 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1017 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1018 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1019 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001020
1021 // insert new blocks after the current block
1022 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1023 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001024 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001025 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1026 MachineFunction::iterator It = BB;
1027 ++It;
1028 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001029 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001030 MF->insert(It, exitMBB);
1031
1032 // Transfer the remainder of BB and its successor edges to exitMBB.
1033 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001034 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001035 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1036
Akira Hatanaka81b44112011-07-19 17:09:53 +00001037 BB->addSuccessor(loopMBB);
1038 loopMBB->addSuccessor(loopMBB);
1039 loopMBB->addSuccessor(sinkMBB);
1040 sinkMBB->addSuccessor(exitMBB);
1041
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001042 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001043 // addiu masklsb2,$0,-4 # 0xfffffffc
1044 // and alignedaddr,ptr,masklsb2
1045 // andi ptrlsb2,ptr,3
1046 // sll shiftamt,ptrlsb2,3
1047 // ori maskupper,$0,255 # 0xff
1048 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001049 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001050 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001051
1052 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001053 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001054 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001055 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001056 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001057 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001058 if (Subtarget->isLittle()) {
1059 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1060 } else {
1061 unsigned Off = RegInfo.createVirtualRegister(RC);
1062 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1063 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1064 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1065 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001066 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001067 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001068 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001069 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001070 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka51122432013-07-01 20:39:53 +00001071 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001072
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001073 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001074 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001075 // ll oldval,0(alignedaddr)
1076 // binop binopres,oldval,incr2
1077 // and newval,binopres,mask
1078 // and maskedoldval0,oldval,mask2
1079 // or storeval,maskedoldval0,newval
1080 // sc success,storeval,0(alignedaddr)
1081 // beq success,$0,loopMBB
1082
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001083 // atomic.swap
1084 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001085 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001086 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001087 // and maskedoldval0,oldval,mask2
1088 // or storeval,maskedoldval0,newval
1089 // sc success,storeval,0(alignedaddr)
1090 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001091
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001092 BB = loopMBB;
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001093 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001094 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001095 // and andres, oldval, incr2
1096 // nor binopres, $0, andres
1097 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001098 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1099 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001100 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001101 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001102 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001103 // <binop> binopres, oldval, incr2
1104 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001105 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1106 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001107 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001108 // and newval, incr2, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001109 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001110 }
Jia Liubb481f82012-02-28 07:46:26 +00001111
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001112 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001113 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001114 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001115 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001116 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001117 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001118 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001119 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001120
Akira Hatanaka939ece12011-07-19 03:42:13 +00001121 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001122 // and maskedoldval1,oldval,mask
1123 // srl srlres,maskedoldval1,shiftamt
1124 // sll sllres,srlres,24
1125 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001126 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001127 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001128
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001129 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001130 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001131 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001132 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001133 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001134 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001135 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001136 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001137
1138 MI->eraseFromParent(); // The instruction is gone now.
1139
Akira Hatanaka939ece12011-07-19 03:42:13 +00001140 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001141}
1142
1143MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001144MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001145 MachineBasicBlock *BB,
1146 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001147 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001148
1149 MachineFunction *MF = BB->getParent();
1150 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001151 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001152 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001153 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001154 unsigned LL, SC, ZERO, BNE, BEQ;
1155
1156 if (Size == 4) {
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001157 LL = Mips::LL;
1158 SC = Mips::SC;
Akira Hatanaka59068062011-11-11 04:14:30 +00001159 ZERO = Mips::ZERO;
1160 BNE = Mips::BNE;
1161 BEQ = Mips::BEQ;
1162 }
1163 else {
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001164 LL = Mips::LLD;
1165 SC = Mips::SCD;
Akira Hatanaka59068062011-11-11 04:14:30 +00001166 ZERO = Mips::ZERO_64;
1167 BNE = Mips::BNE64;
1168 BEQ = Mips::BEQ64;
1169 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001170
1171 unsigned Dest = MI->getOperand(0).getReg();
1172 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001173 unsigned OldVal = MI->getOperand(2).getReg();
1174 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001175
Akira Hatanaka4061da12011-07-19 20:11:17 +00001176 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001177
1178 // insert new blocks after the current block
1179 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1180 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1181 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1182 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1183 MachineFunction::iterator It = BB;
1184 ++It;
1185 MF->insert(It, loop1MBB);
1186 MF->insert(It, loop2MBB);
1187 MF->insert(It, exitMBB);
1188
1189 // Transfer the remainder of BB and its successor edges to exitMBB.
1190 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001191 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001192 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1193
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001194 // thisMBB:
1195 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001196 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001197 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001198 loop1MBB->addSuccessor(exitMBB);
1199 loop1MBB->addSuccessor(loop2MBB);
1200 loop2MBB->addSuccessor(loop1MBB);
1201 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001202
1203 // loop1MBB:
1204 // ll dest, 0(ptr)
1205 // bne dest, oldval, exitMBB
1206 BB = loop1MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001207 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1208 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001209 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001210
1211 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001212 // sc success, newval, 0(ptr)
1213 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001214 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001215 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001216 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001217 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka59068062011-11-11 04:14:30 +00001218 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001219
1220 MI->eraseFromParent(); // The instruction is gone now.
1221
Akira Hatanaka939ece12011-07-19 03:42:13 +00001222 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001223}
1224
1225MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001226MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001227 MachineBasicBlock *BB,
1228 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001229 assert((Size == 1 || Size == 2) &&
1230 "Unsupported size for EmitAtomicCmpSwapPartial.");
1231
1232 MachineFunction *MF = BB->getParent();
1233 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1234 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1235 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001236 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001237
1238 unsigned Dest = MI->getOperand(0).getReg();
1239 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001240 unsigned CmpVal = MI->getOperand(2).getReg();
1241 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001242
Akira Hatanaka4061da12011-07-19 20:11:17 +00001243 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1244 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001245 unsigned Mask = RegInfo.createVirtualRegister(RC);
1246 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001247 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1248 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1249 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1250 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1251 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1252 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1253 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1254 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1255 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1256 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1257 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1258 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1259 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1260 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001261
1262 // insert new blocks after the current block
1263 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1264 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1265 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001266 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001267 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1268 MachineFunction::iterator It = BB;
1269 ++It;
1270 MF->insert(It, loop1MBB);
1271 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001272 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001273 MF->insert(It, exitMBB);
1274
1275 // Transfer the remainder of BB and its successor edges to exitMBB.
1276 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001277 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001278 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1279
Akira Hatanaka81b44112011-07-19 17:09:53 +00001280 BB->addSuccessor(loop1MBB);
1281 loop1MBB->addSuccessor(sinkMBB);
1282 loop1MBB->addSuccessor(loop2MBB);
1283 loop2MBB->addSuccessor(loop1MBB);
1284 loop2MBB->addSuccessor(sinkMBB);
1285 sinkMBB->addSuccessor(exitMBB);
1286
Akira Hatanaka70564a92011-07-19 18:14:26 +00001287 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001288 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001289 // addiu masklsb2,$0,-4 # 0xfffffffc
1290 // and alignedaddr,ptr,masklsb2
1291 // andi ptrlsb2,ptr,3
1292 // sll shiftamt,ptrlsb2,3
1293 // ori maskupper,$0,255 # 0xff
1294 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001295 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001296 // andi maskedcmpval,cmpval,255
1297 // sll shiftedcmpval,maskedcmpval,shiftamt
1298 // andi maskednewval,newval,255
1299 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001300 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001301 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001302 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001303 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001304 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001305 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001306 if (Subtarget->isLittle()) {
1307 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1308 } else {
1309 unsigned Off = RegInfo.createVirtualRegister(RC);
1310 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1311 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1312 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1313 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001314 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001315 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001316 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001317 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001318 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1319 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001320 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001321 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001322 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001323 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001324 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001325 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001326 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001327
1328 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001329 // ll oldval,0(alginedaddr)
1330 // and maskedoldval0,oldval,mask
1331 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001332 BB = loop1MBB;
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001333 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001334 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001335 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001336 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001337 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001338
1339 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001340 // and maskedoldval1,oldval,mask2
1341 // or storeval,maskedoldval1,shiftednewval
1342 // sc success,storeval,0(alignedaddr)
1343 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001344 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001345 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001346 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001347 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001348 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001349 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001350 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001351 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001352 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001353
Akira Hatanaka939ece12011-07-19 03:42:13 +00001354 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001355 // srl srlres,maskedoldval0,shiftamt
1356 // sll sllres,srlres,24
1357 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001358 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001359 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001360
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001361 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001362 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001363 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001364 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001365 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001366 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001367
1368 MI->eraseFromParent(); // The instruction is gone now.
1369
Akira Hatanaka939ece12011-07-19 03:42:13 +00001370 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001371}
1372
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001373//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001374// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001375//===----------------------------------------------------------------------===//
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001376SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakab7656a92013-03-06 21:32:03 +00001377 SDValue Chain = Op.getOperand(0);
1378 SDValue Table = Op.getOperand(1);
1379 SDValue Index = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001380 SDLoc DL(Op);
Akira Hatanakab7656a92013-03-06 21:32:03 +00001381 EVT PTy = getPointerTy();
1382 unsigned EntrySize =
1383 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1384
1385 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1386 DAG.getConstant(EntrySize, PTy));
1387 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1388
1389 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1390 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1391 MachinePointerInfo::getJumpTable(), MemVT, false, false,
1392 0);
1393 Chain = Addr.getValue(1);
1394
1395 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || IsN64) {
1396 // For PIC, the sequence is:
1397 // BRIND(load(Jumptable + index) + RelocBase)
1398 // RelocBase can be JumpTable, GOT or some sort of global base.
1399 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1400 getPICJumpTableRelocBase(Table, DAG));
1401 }
1402
1403 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1404}
1405
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001406SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001407lowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001408{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001409 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001410 // the block to branch to if the condition is true.
1411 SDValue Chain = Op.getOperand(0);
1412 SDValue Dest = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001413 SDLoc DL(Op);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001414
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001415 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001416
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001417 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001418 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001419 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001420
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001421 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001422 Mips::CondCode CC =
1423 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanaka9cf07242013-03-30 01:16:38 +00001424 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1425 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001426 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001427 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001428 FCC0, Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001429}
1430
1431SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001432lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001433{
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001434 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001435
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001436 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001437 if (Cond.getOpcode() != MipsISD::FPCmp)
1438 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001439
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001440 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001441 SDLoc(Op));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001442}
1443
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001444SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001445lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001446{
Andrew Trickac6d9be2013-05-25 02:42:55 +00001447 SDLoc DL(Op);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001448 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault225ed702013-05-18 00:21:46 +00001449 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1450 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001451 Op.getOperand(0), Op.getOperand(1),
1452 Op.getOperand(4));
1453
1454 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1455 Op.getOperand(3));
1456}
1457
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001458SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1459 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001460
1461 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1462 "Floating point operand expected.");
1463
1464 SDValue True = DAG.getConstant(1, MVT::i32);
1465 SDValue False = DAG.getConstant(0, MVT::i32);
1466
Andrew Trickac6d9be2013-05-25 02:42:55 +00001467 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001468}
1469
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001470SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001471 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001472 // FIXME there isn't actually debug info here
Andrew Trickac6d9be2013-05-25 02:42:55 +00001473 SDLoc DL(Op);
Jia Liubb481f82012-02-28 07:46:26 +00001474 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001475
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001476 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaafc945b2012-09-12 23:27:55 +00001477 const MipsTargetObjectFile &TLOF =
1478 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001479
Chris Lattnere3736f82009-08-13 05:41:27 +00001480 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001481 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001482 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001483 MipsII::MO_GPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001484 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001485 DAG.getVTList(MVT::i32), &GA, 1);
Akira Hatanakae7338cd2012-08-22 03:18:13 +00001486 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001487 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001488 }
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001489
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001490 // %hi/%lo relocation
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001491 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001492 }
1493
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001494 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
1495 return getAddrLocal(Op, DAG, HasMips64);
1496
Akira Hatanakaf09a0372012-11-21 20:40:38 +00001497 if (LargeGOT)
1498 return getAddrGlobalLargeGOT(Op, DAG, MipsII::MO_GOT_HI16,
1499 MipsII::MO_GOT_LO16);
1500
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001501 return getAddrGlobal(Op, DAG,
1502 HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001503}
1504
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001505SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001506 SelectionDAG &DAG) const {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001507 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1508 return getAddrNonPIC(Op, DAG);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001509
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001510 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001511}
1512
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001513SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001514lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001515{
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001516 // If the relocation model is PIC, use the General Dynamic TLS Model or
1517 // Local Dynamic TLS model, otherwise use the Initial Exec or
1518 // Local Exec TLS Model.
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001519
1520 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001521 SDLoc DL(GA);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001522 const GlobalValue *GV = GA->getGlobal();
1523 EVT PtrVT = getPointerTy();
1524
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001525 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1526
1527 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg70a07c72012-06-04 14:02:08 +00001528 // General Dynamic and Local Dynamic TLS Model.
1529 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1530 : MipsII::MO_TLSGD;
1531
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001532 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1533 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1534 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001535 unsigned PtrSize = PtrVT.getSizeInBits();
1536 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1537
Benjamin Kramer5eccf672011-12-11 12:21:34 +00001538 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001539
1540 ArgListTy Args;
1541 ArgListEntry Entry;
1542 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00001543 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001544 Args.push_back(Entry);
Jia Liubb481f82012-02-28 07:46:26 +00001545
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001546 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001547 false, false, false, false, 0, CallingConv::C,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001548 /*IsTailCall=*/false, /*doesNotRet=*/false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001549 /*isReturnValueUsed=*/true,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001550 TlsGetAddr, Args, DAG, DL);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001551 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001552
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001553 SDValue Ret = CallResult.first;
1554
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001555 if (model != TLSModel::LocalDynamic)
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001556 return Ret;
1557
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001558 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001559 MipsII::MO_DTPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001560 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1561 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001562 MipsII::MO_DTPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001563 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1564 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1565 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001566 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001567
1568 SDValue Offset;
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001569 if (model == TLSModel::InitialExec) {
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001570 // Initial Exec TLS Model
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001571 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001572 MipsII::MO_GOTTPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001573 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanaka648f00c2012-02-24 22:34:47 +00001574 TGA);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001575 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001576 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001577 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001578 } else {
1579 // Local Exec TLS Model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001580 assert(model == TLSModel::LocalExec);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001581 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001582 MipsII::MO_TPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001583 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001584 MipsII::MO_TPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001585 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1586 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1587 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001588 }
1589
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001590 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1591 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001592}
1593
1594SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001595lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001596{
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001597 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1598 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001599
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001600 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001601}
1602
Dan Gohman475871a2008-07-27 21:46:04 +00001603SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001604lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001605{
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001606 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001607 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001608 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001609 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001610 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001611 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001612 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1613 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001614 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001615
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001616 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1617 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001618
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001619 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001620}
1621
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001622SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001623 MachineFunction &MF = DAG.getMachineFunction();
1624 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1625
Andrew Trickac6d9be2013-05-25 02:42:55 +00001626 SDLoc DL(Op);
Dan Gohman1e93df62010-04-17 14:41:14 +00001627 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1628 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001629
1630 // vastart just stores the address of the VarArgsFrameIndex slot into the
1631 // memory location argument.
1632 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001633 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka82099682011-12-19 19:52:25 +00001634 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001635}
Jia Liubb481f82012-02-28 07:46:26 +00001636
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001637static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001638 EVT TyX = Op.getOperand(0).getValueType();
1639 EVT TyY = Op.getOperand(1).getValueType();
1640 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1641 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001642 SDLoc DL(Op);
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001643 SDValue Res;
1644
1645 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1646 // to i32.
1647 SDValue X = (TyX == MVT::f32) ?
1648 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1649 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1650 Const1);
1651 SDValue Y = (TyY == MVT::f32) ?
1652 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1653 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1654 Const1);
1655
1656 if (HasR2) {
1657 // ext E, Y, 31, 1 ; extract bit31 of Y
1658 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1659 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1660 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1661 } else {
1662 // sll SllX, X, 1
1663 // srl SrlX, SllX, 1
1664 // srl SrlY, Y, 31
1665 // sll SllY, SrlX, 31
1666 // or Or, SrlX, SllY
1667 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1668 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1669 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1670 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1671 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1672 }
1673
1674 if (TyX == MVT::f32)
1675 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1676
1677 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1678 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1679 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001680}
1681
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001682static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001683 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1684 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1685 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1686 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001687 SDLoc DL(Op);
Eric Christopher471e4222011-06-08 23:55:35 +00001688
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001689 // Bitcast to integer nodes.
1690 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1691 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001692
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001693 if (HasR2) {
1694 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1695 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1696 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1697 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001698
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001699 if (WidthX > WidthY)
1700 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1701 else if (WidthY > WidthX)
1702 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001703
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001704 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1705 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1706 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1707 }
1708
1709 // (d)sll SllX, X, 1
1710 // (d)srl SrlX, SllX, 1
1711 // (d)srl SrlY, Y, width(Y)-1
1712 // (d)sll SllY, SrlX, width(Y)-1
1713 // or Or, SrlX, SllY
1714 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1715 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1716 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1717 DAG.getConstant(WidthY - 1, MVT::i32));
1718
1719 if (WidthX > WidthY)
1720 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1721 else if (WidthY > WidthX)
1722 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1723
1724 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1725 DAG.getConstant(WidthX - 1, MVT::i32));
1726 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1727 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001728}
1729
Akira Hatanaka82099682011-12-19 19:52:25 +00001730SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001731MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001732 if (Subtarget->hasMips64())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001733 return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001734
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001735 return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001736}
1737
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001738static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001739 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001740 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001741
1742 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1743 // to i32.
1744 SDValue X = (Op.getValueType() == MVT::f32) ?
1745 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1746 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1747 Const1);
1748
1749 // Clear MSB.
1750 if (HasR2)
1751 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
1752 DAG.getRegister(Mips::ZERO, MVT::i32),
1753 DAG.getConstant(31, MVT::i32), Const1, X);
1754 else {
1755 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1756 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1757 }
1758
1759 if (Op.getValueType() == MVT::f32)
1760 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
1761
1762 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1763 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1764 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1765}
1766
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001767static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001768 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001769 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001770
1771 // Bitcast to integer node.
1772 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
1773
1774 // Clear MSB.
1775 if (HasR2)
1776 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
1777 DAG.getRegister(Mips::ZERO_64, MVT::i64),
1778 DAG.getConstant(63, MVT::i32), Const1, X);
1779 else {
1780 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
1781 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
1782 }
1783
1784 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
1785}
1786
1787SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001788MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001789 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001790 return lowerFABS64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001791
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001792 return lowerFABS32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001793}
1794
Akira Hatanaka2e591472011-06-02 00:24:44 +00001795SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001796lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001797 // check the depth
1798 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001799 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001800
1801 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1802 MFI->setFrameAddressIsTaken(true);
1803 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001804 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001805 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001806 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001807 return FrameAddr;
1808}
1809
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001810SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001811 SelectionDAG &DAG) const {
1812 // check the depth
1813 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1814 "Return address can be determined only for current frame.");
1815
1816 MachineFunction &MF = DAG.getMachineFunction();
1817 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001818 MVT VT = Op.getSimpleValueType();
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001819 unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
1820 MFI->setReturnAddressIsTaken(true);
1821
1822 // Return RA, which contains the return address. Mark it an implicit live-in.
1823 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001824 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001825}
1826
Akira Hatanaka544cc212013-01-30 00:26:49 +00001827// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1828// generated from __builtin_eh_return (offset, handler)
1829// The effect of this is to adjust the stack pointer by "offset"
1830// and then branch to "handler".
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001831SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanaka544cc212013-01-30 00:26:49 +00001832 const {
1833 MachineFunction &MF = DAG.getMachineFunction();
1834 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1835
1836 MipsFI->setCallsEhReturn();
1837 SDValue Chain = Op.getOperand(0);
1838 SDValue Offset = Op.getOperand(1);
1839 SDValue Handler = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001840 SDLoc DL(Op);
Akira Hatanaka544cc212013-01-30 00:26:49 +00001841 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
1842
1843 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1844 // EH_RETURN nodes, so that instructions are emitted back-to-back.
1845 unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1;
1846 unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0;
1847 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1848 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1849 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1850 DAG.getRegister(OffsetReg, Ty),
1851 DAG.getRegister(AddrReg, getPointerTy()),
1852 Chain.getValue(1));
1853}
1854
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001855SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001856 SelectionDAG &DAG) const {
Eli Friedman14648462011-07-27 22:21:52 +00001857 // FIXME: Need pseudo-fence for 'singlethread' fences
1858 // FIXME: Set SType for weaker fences where supported/appropriate.
1859 unsigned SType = 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001860 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001861 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00001862 DAG.getConstant(SType, MVT::i32));
1863}
1864
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001865SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001866 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001867 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001868 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1869 SDValue Shamt = Op.getOperand(2);
1870
1871 // if shamt < 32:
1872 // lo = (shl lo, shamt)
1873 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
1874 // else:
1875 // lo = 0
1876 // hi = (shl lo, shamt[4:0])
1877 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1878 DAG.getConstant(-1, MVT::i32));
1879 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
1880 DAG.getConstant(1, MVT::i32));
1881 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
1882 Not);
1883 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
1884 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1885 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
1886 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1887 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka864f6602012-06-14 21:10:56 +00001888 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1889 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001890 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
1891
1892 SDValue Ops[2] = {Lo, Hi};
1893 return DAG.getMergeValues(Ops, 2, DL);
1894}
1895
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001896SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001897 bool IsSRA) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001898 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001899 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1900 SDValue Shamt = Op.getOperand(2);
1901
1902 // if shamt < 32:
1903 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
1904 // if isSRA:
1905 // hi = (sra hi, shamt)
1906 // else:
1907 // hi = (srl hi, shamt)
1908 // else:
1909 // if isSRA:
1910 // lo = (sra hi, shamt[4:0])
1911 // hi = (sra hi, 31)
1912 // else:
1913 // lo = (srl hi, shamt[4:0])
1914 // hi = 0
1915 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1916 DAG.getConstant(-1, MVT::i32));
1917 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
1918 DAG.getConstant(1, MVT::i32));
1919 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
1920 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
1921 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1922 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
1923 Hi, Shamt);
1924 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1925 DAG.getConstant(0x20, MVT::i32));
1926 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
1927 DAG.getConstant(31, MVT::i32));
1928 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
1929 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1930 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
1931 ShiftRightHi);
1932
1933 SDValue Ops[2] = {Lo, Hi};
1934 return DAG.getMergeValues(Ops, 2, DL);
1935}
1936
Akira Hatanakafee62c12013-04-11 19:07:14 +00001937static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001938 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001939 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001940 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001941 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001942 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001943 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1944
1945 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001946 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001947 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001948
1949 SDValue Ops[] = { Chain, Ptr, Src };
1950 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
1951 LD->getMemOperand());
1952}
1953
1954// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001955SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001956 LoadSDNode *LD = cast<LoadSDNode>(Op);
1957 EVT MemVT = LD->getMemoryVT();
1958
1959 // Return if load is aligned or if MemVT is neither i32 nor i64.
1960 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
1961 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
1962 return SDValue();
1963
1964 bool IsLittle = Subtarget->isLittle();
1965 EVT VT = Op.getValueType();
1966 ISD::LoadExtType ExtType = LD->getExtensionType();
1967 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
1968
1969 assert((VT == MVT::i32) || (VT == MVT::i64));
1970
1971 // Expand
1972 // (set dst, (i64 (load baseptr)))
1973 // to
1974 // (set tmp, (ldl (add baseptr, 7), undef))
1975 // (set dst, (ldr baseptr, tmp))
1976 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00001977 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001978 IsLittle ? 7 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00001979 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001980 IsLittle ? 0 : 7);
1981 }
1982
Akira Hatanakafee62c12013-04-11 19:07:14 +00001983 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001984 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00001985 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001986 IsLittle ? 0 : 3);
1987
1988 // Expand
1989 // (set dst, (i32 (load baseptr))) or
1990 // (set dst, (i64 (sextload baseptr))) or
1991 // (set dst, (i64 (extload baseptr)))
1992 // to
1993 // (set tmp, (lwl (add baseptr, 3), undef))
1994 // (set dst, (lwr baseptr, tmp))
1995 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
1996 (ExtType == ISD::EXTLOAD))
1997 return LWR;
1998
1999 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2000
2001 // Expand
2002 // (set dst, (i64 (zextload baseptr)))
2003 // to
2004 // (set tmp0, (lwl (add baseptr, 3), undef))
2005 // (set tmp1, (lwr baseptr, tmp0))
2006 // (set tmp2, (shl tmp1, 32))
2007 // (set dst, (srl tmp2, 32))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002008 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002009 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2010 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka94ccee22012-06-04 17:46:29 +00002011 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2012 SDValue Ops[] = { SRL, LWR.getValue(1) };
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002013 return DAG.getMergeValues(Ops, 2, DL);
2014}
2015
Akira Hatanakafee62c12013-04-11 19:07:14 +00002016static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002017 SDValue Chain, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002018 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2019 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002020 SDLoc DL(SD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002021 SDVTList VTList = DAG.getVTList(MVT::Other);
2022
2023 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002024 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002025 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002026
2027 SDValue Ops[] = { Chain, Value, Ptr };
2028 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2029 SD->getMemOperand());
2030}
2031
2032// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanaka63451432013-05-16 20:45:17 +00002033static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2034 bool IsLittle) {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002035 SDValue Value = SD->getValue(), Chain = SD->getChain();
2036 EVT VT = Value.getValueType();
2037
2038 // Expand
2039 // (store val, baseptr) or
2040 // (truncstore val, baseptr)
2041 // to
2042 // (swl val, (add baseptr, 3))
2043 // (swr val, baseptr)
2044 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00002045 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002046 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00002047 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002048 }
2049
2050 assert(VT == MVT::i64);
2051
2052 // Expand
2053 // (store val, baseptr)
2054 // to
2055 // (sdl val, (add baseptr, 7))
2056 // (sdr val, baseptr)
Akira Hatanakafee62c12013-04-11 19:07:14 +00002057 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2058 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002059}
2060
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002061// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2062static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2063 SDValue Val = SD->getValue();
2064
2065 if (Val.getOpcode() != ISD::FP_TO_SINT)
2066 return SDValue();
2067
2068 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002069 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002070 Val.getOperand(0));
2071
Andrew Trickac6d9be2013-05-25 02:42:55 +00002072 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002073 SD->getPointerInfo(), SD->isVolatile(),
2074 SD->isNonTemporal(), SD->getAlignment());
2075}
2076
Akira Hatanaka63451432013-05-16 20:45:17 +00002077SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2078 StoreSDNode *SD = cast<StoreSDNode>(Op);
2079 EVT MemVT = SD->getMemoryVT();
2080
2081 // Lower unaligned integer stores.
2082 if ((SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2083 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2084 return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle());
2085
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002086 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanaka63451432013-05-16 20:45:17 +00002087}
2088
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002089SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002090 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2091 || cast<ConstantSDNode>
2092 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2093 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2094 return SDValue();
2095
2096 // The pattern
2097 // (add (frameaddr 0), (frame_to_args_offset))
2098 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2099 // (add FrameObject, 0)
2100 // where FrameObject is a fixed StackObject with offset 0 which points to
2101 // the old stack pointer.
2102 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2103 EVT ValTy = Op->getValueType(0);
2104 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2105 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002106 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002107 DAG.getConstant(0, ValTy));
2108}
2109
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002110SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2111 SelectionDAG &DAG) const {
2112 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002113 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002114 Op.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002115 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002116}
2117
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002118//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002119// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002120//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002121
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002122//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002123// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002124// Mips O32 ABI rules:
2125// ---
2126// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002127// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002128// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002129// f64 - Only passed in two aliased f32 registers if no int reg has been used
2130// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002131// not used, it must be shadowed. If only A3 is avaiable, shadow it and
2132// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002133//
2134// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002135//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002136
Duncan Sands1e96bab2010-11-04 10:49:57 +00002137static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00002138 MVT LocVT, CCValAssign::LocInfo LocInfo,
Akira Hatanakaad341d42013-08-20 23:38:40 +00002139 ISD::ArgFlagsTy ArgFlags, CCState &State,
2140 const uint16_t *F64Regs) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002141
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002142 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002143
Craig Topperc5eaae42012-03-11 07:57:25 +00002144 static const uint16_t IntRegs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002145 Mips::A0, Mips::A1, Mips::A2, Mips::A3
2146 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002147 static const uint16_t F32Regs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002148 Mips::F12, Mips::F14
2149 };
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002150
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002151 // Do not process byval args here.
2152 if (ArgFlags.isByVal())
2153 return true;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002154
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002155 // Promote i8 and i16
2156 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2157 LocVT = MVT::i32;
2158 if (ArgFlags.isSExt())
2159 LocInfo = CCValAssign::SExt;
2160 else if (ArgFlags.isZExt())
2161 LocInfo = CCValAssign::ZExt;
2162 else
2163 LocInfo = CCValAssign::AExt;
2164 }
2165
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002166 unsigned Reg;
2167
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002168 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2169 // is true: function is vararg, argument is 3rd or higher, there is previous
2170 // argument which is not f32 or f64.
2171 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2172 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002173 unsigned OrigAlign = ArgFlags.getOrigAlign();
2174 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002175
2176 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002177 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002178 // If this is the first part of an i64 arg,
2179 // the allocated register must be either A0 or A2.
2180 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2181 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002182 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002183 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2184 // Allocate int register and shadow next int register. If first
2185 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002186 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2187 if (Reg == Mips::A1 || Reg == Mips::A3)
2188 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2189 State.AllocateReg(IntRegs, IntRegsSize);
2190 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002191 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2192 // we are guaranteed to find an available float register
2193 if (ValVT == MVT::f32) {
2194 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2195 // Shadow int register
2196 State.AllocateReg(IntRegs, IntRegsSize);
2197 } else {
2198 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2199 // Shadow int registers
2200 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2201 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2202 State.AllocateReg(IntRegs, IntRegsSize);
2203 State.AllocateReg(IntRegs, IntRegsSize);
2204 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002205 } else
2206 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002207
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002208 if (!Reg) {
2209 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2210 OrigAlign);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002211 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002212 } else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002213 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002214
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002215 return false;
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002216}
2217
Akira Hatanakaad341d42013-08-20 23:38:40 +00002218static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2219 MVT LocVT, CCValAssign::LocInfo LocInfo,
2220 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2221 static const uint16_t F64Regs[] = { Mips::D6, Mips::D7 };
2222
2223 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2224}
2225
2226static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2227 MVT LocVT, CCValAssign::LocInfo LocInfo,
2228 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2229 static const uint16_t F64Regs[] = { Mips::D12_64, Mips::D12_64 };
2230
2231 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2232}
2233
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002234#include "MipsGenCallingConv.inc"
2235
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002236//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002237// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002238//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002239
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002240// Return next O32 integer argument register.
2241static unsigned getNextIntArgReg(unsigned Reg) {
2242 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2243 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2244}
2245
Akira Hatanaka7d712092012-10-30 19:23:25 +00002246SDValue
2247MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002248 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka7d712092012-10-30 19:23:25 +00002249 bool IsTailCall, SelectionDAG &DAG) const {
2250 if (!IsTailCall) {
2251 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2252 DAG.getIntPtrConstant(Offset));
2253 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2254 false, 0);
2255 }
2256
2257 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2258 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2259 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2260 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2261 /*isVolatile=*/ true, false, 0);
2262}
2263
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002264void MipsTargetLowering::
2265getOpndList(SmallVectorImpl<SDValue> &Ops,
2266 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2267 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2268 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
2269 // Insert node "GP copy globalreg" before call to function.
2270 //
2271 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2272 // in PIC mode) allow symbols to be resolved via lazy binding.
2273 // The lazy binding stub requires GP to point to the GOT.
2274 if (IsPICCall && !InternalLinkage) {
2275 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
2276 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2277 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2278 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002279
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002280 // Build a sequence of copy-to-reg nodes chained together with token
2281 // chain and flag operands which copy the outgoing args into registers.
2282 // The InFlag in necessary since all emitted instructions must be
2283 // stuck together.
2284 SDValue InFlag;
Reed Kotler8453b3f2013-01-24 04:24:02 +00002285
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002286 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2287 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2288 RegsToPass[i].second, InFlag);
2289 InFlag = Chain.getValue(1);
2290 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002291
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002292 // Add argument registers to the end of the list so that they are
2293 // known live into the call.
2294 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2295 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2296 RegsToPass[i].second.getValueType()));
Reed Kotler8453b3f2013-01-24 04:24:02 +00002297
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002298 // Add a register mask operand representing the call-preserved registers.
2299 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2300 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2301 assert(Mask && "Missing call preserved mask for calling convention");
Reed Kotler46090912013-05-10 22:25:39 +00002302 if (Subtarget->inMips16HardFloat()) {
2303 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2304 llvm::StringRef Sym = G->getGlobal()->getName();
2305 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
2306 if (F->hasFnAttribute("__Mips16RetHelper")) {
2307 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2308 }
2309 }
2310 }
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002311 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2312
2313 if (InFlag.getNode())
2314 Ops.push_back(InFlag);
Reed Kotler8453b3f2013-01-24 04:24:02 +00002315}
2316
Dan Gohman98ca4f22009-08-05 01:29:28 +00002317/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002318/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002319SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002320MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002321 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002322 SelectionDAG &DAG = CLI.DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002323 SDLoc DL = CLI.DL;
Craig Toppera0ec3f92013-07-14 04:42:23 +00002324 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2325 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2326 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002327 SDValue Chain = CLI.Chain;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002328 SDValue Callee = CLI.Callee;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002329 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002330 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002331 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002332
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002333 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002334 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002335 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002336 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002337
2338 // Analyze operands of the call, assigning locations to each operand.
2339 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002340 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002341 getTargetMachine(), ArgLocs, *DAG.getContext());
Reed Kotler46090912013-05-10 22:25:39 +00002342 MipsCC::SpecialCallingConvType SpecialCallingConv =
2343 getSpecialCallingConv(Callee);
Akira Hatanakaad341d42013-08-20 23:38:40 +00002344 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo,
2345 SpecialCallingConv);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002346
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002347 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002348 Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002349 Callee.getNode(), CLI.Args);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002350
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002351 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002352 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002353
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002354 // Check if it's really possible to do a tail call.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002355 if (IsTailCall)
2356 IsTailCall =
2357 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002358 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002359
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002360 if (IsTailCall)
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002361 ++NumTailCalls;
2362
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002363 // Chain is the output chain of the last Load/Store or CopyToReg node.
2364 // ByValChain is the output chain of the last Memcpy node created for copying
2365 // byval arguments to the stack.
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002366 unsigned StackAlignment = TFL->getStackAlignment();
2367 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002368 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002369
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002370 if (!IsTailCall)
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002371 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002372
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002373 SDValue StackPtr = DAG.getCopyFromReg(Chain, DL,
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002374 IsN64 ? Mips::SP_64 : Mips::SP,
2375 getPointerTy());
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002376
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002377 // With EABI is it possible to have 16 args on registers.
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002378 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman475871a2008-07-27 21:46:04 +00002379 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002380 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002381
2382 // Walk the register/memloc assignments, inserting copies/loads.
2383 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002384 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002385 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002386 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002387 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2388
2389 // ByVal Arg.
2390 if (Flags.isByVal()) {
2391 assert(Flags.getByValSize() &&
2392 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002393 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002394 assert(!IsTailCall &&
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002395 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002396 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002397 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
2398 ++ByValArg;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002399 continue;
2400 }
Jia Liubb481f82012-02-28 07:46:26 +00002401
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002402 // Promote the value if needed.
2403 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002404 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002405 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002406 if (VA.isRegLoc()) {
2407 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002408 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2409 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002410 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002411 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002412 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002413 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002414 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002415 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002416 if (!Subtarget->isLittle())
2417 std::swap(Lo, Hi);
Jia Liubb481f82012-02-28 07:46:26 +00002418 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002419 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2420 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2421 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002422 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002423 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002424 }
2425 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002426 case CCValAssign::SExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002427 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002428 break;
2429 case CCValAssign::ZExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002430 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002431 break;
2432 case CCValAssign::AExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002433 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002434 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002435 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002436
2437 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002438 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002439 if (VA.isRegLoc()) {
2440 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002441 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002442 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002443
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002444 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002445 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002446
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002447 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002448 // parameter value to a stack Location
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002449 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002450 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002451 }
2452
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002453 // Transform all store nodes into one single node because all store
2454 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002455 if (!MemOpChains.empty())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002456 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002457 &MemOpChains[0], MemOpChains.size());
2458
Bill Wendling056292f2008-09-16 21:48:12 +00002459 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002460 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2461 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002462 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakaed185da2012-12-13 03:17:29 +00002463 bool GlobalOrExternal = false, InternalLinkage = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002464 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002465
2466 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002467 if (IsPICCall) {
Akira Hatanakaed185da2012-12-13 03:17:29 +00002468 InternalLinkage = G->getGlobal()->hasInternalLinkage();
2469
2470 if (InternalLinkage)
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002471 Callee = getAddrLocal(Callee, DAG, HasMips64);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002472 else if (LargeGOT)
2473 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
2474 MipsII::MO_CALL_LO16);
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002475 else
2476 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
2477 } else
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002478 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002479 MipsII::MO_NO_FLAG);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002480 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002481 }
2482 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002483 if (!IsN64 && !IsPIC) // !N64 && static
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002484 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2485 MipsII::MO_NO_FLAG);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002486 else if (LargeGOT)
2487 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
2488 MipsII::MO_CALL_LO16);
Akira Hatanaka60689322013-02-22 21:10:03 +00002489 else // N64 || PIC
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002490 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
2491
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002492 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002493 }
2494
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002495 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002496 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002497
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002498 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2499 CLI, Callee, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002500
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002501 if (IsTailCall)
2502 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, &Ops[0], Ops.size());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002503
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002504 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, &Ops[0], Ops.size());
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002505 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002506
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002507 // Create the CALLSEQ_END node.
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002508 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002509 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002510 InFlag = Chain.getValue(1);
2511
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002512 // Handle result values, copying them out of physregs into vregs that we
2513 // return.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002514 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
2515 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002516}
2517
Dan Gohman98ca4f22009-08-05 01:29:28 +00002518/// LowerCallResult - Lower the result values of a call into the
2519/// appropriate copies out of appropriate physical registers.
2520SDValue
2521MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002522 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002523 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002524 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002525 SmallVectorImpl<SDValue> &InVals,
2526 const SDNode *CallNode,
2527 const Type *RetTy) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002528 // Assign locations to each value returned by this call.
2529 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002530 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka864f6602012-06-14 21:10:56 +00002531 getTargetMachine(), RVLocs, *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002532 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002533
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002534 MipsCCInfo.analyzeCallResult(Ins, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002535 CallNode, RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002536
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002537 // Copy all of the result registers out of their specified physreg.
2538 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002539 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002540 RVLocs[i].getLocVT(), InFlag);
2541 Chain = Val.getValue(1);
2542 InFlag = Val.getValue(2);
2543
2544 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002545 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002546
2547 InVals.push_back(Val);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002548 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002549
Dan Gohman98ca4f22009-08-05 01:29:28 +00002550 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002551}
2552
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002553//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002554// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002555//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002556/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002557/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002558SDValue
2559MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002560 CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002561 bool IsVarArg,
Akira Hatanaka82099682011-12-19 19:52:25 +00002562 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002563 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002564 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002565 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002566 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002567 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002568 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002569
Dan Gohman1e93df62010-04-17 14:41:14 +00002570 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002571
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002572 // Used with vargs to acumulate store chains.
2573 std::vector<SDValue> OutChains;
2574
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002575 // Assign locations to all of the incoming arguments.
2576 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002577 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002578 getTargetMachine(), ArgLocs, *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002579 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002580 Function::const_arg_iterator FuncArg =
2581 DAG.getMachineFunction().getFunction()->arg_begin();
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002582 bool UseSoftFloat = Subtarget->mipsSEUsesSoftFloat();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002583
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002584 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
Akira Hatanakab33b34a2012-10-30 19:37:25 +00002585 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2586 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002587
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002588 unsigned CurArgIdx = 0;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002589 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002590
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002591 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002592 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002593 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2594 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002595 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002596 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2597 bool IsRegLoc = VA.isRegLoc();
2598
2599 if (Flags.isByVal()) {
2600 assert(Flags.getByValSize() &&
2601 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002602 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002603 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002604 MipsCCInfo, *ByValArg);
2605 ++ByValArg;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002606 continue;
2607 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002608
2609 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002610 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002611 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002612 unsigned ArgReg = VA.getLocReg();
Craig Topper44d23822012-02-22 05:59:10 +00002613 const TargetRegisterClass *RC;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002614
Owen Anderson825b72b2009-08-11 20:47:22 +00002615 if (RegVT == MVT::i32)
Reed Kotlerbacbf1c2012-12-20 06:06:35 +00002616 RC = Subtarget->inMips16Mode()? &Mips::CPU16RegsRegClass :
Akira Hatanaka18587862013-08-06 23:08:38 +00002617 &Mips::GPR32RegClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002618 else if (RegVT == MVT::i64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002619 RC = &Mips::GPR64RegClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002620 else if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002621 RC = &Mips::FGR32RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002622 else if (RegVT == MVT::f64)
Akira Hatanakaad341d42013-08-20 23:38:40 +00002623 RC = Subtarget->isFP64bit() ? &Mips::FGR64RegClass :
2624 &Mips::AFGR64RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002625 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002626 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002627
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002628 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002629 // physical registers into virtual ones
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002630 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2631 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002632
2633 // If this is an 8 or 16-bit value, it has been passed promoted
2634 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002635 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002636 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002637 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002638 if (VA.getLocInfo() == CCValAssign::SExt)
2639 Opcode = ISD::AssertSext;
2640 else if (VA.getLocInfo() == CCValAssign::ZExt)
2641 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002642 if (Opcode)
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002643 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002644 DAG.getValueType(ValVT));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002645 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002646 }
2647
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002648 // Handle floating point arguments passed in integer registers and
2649 // long double arguments passed in floating point registers.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002650 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002651 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2652 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002653 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002654 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002655 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002656 getNextIntArgReg(ArgReg), RC);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002657 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002658 if (!Subtarget->isLittle())
2659 std::swap(ArgValue, ArgValue2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002660 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002661 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002662 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002663
Dan Gohman98ca4f22009-08-05 01:29:28 +00002664 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002665 } else { // VA.isRegLoc()
2666
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002667 // sanity check
2668 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002669
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002670 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002671 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002672 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002673
2674 // Create load nodes to retrieve arguments from the stack
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002675 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002676 InVals.push_back(DAG.getLoad(ValVT, DL, Chain, FIN,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002677 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002678 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002679 }
2680 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002681
2682 // The mips ABIs for returning structs by value requires that we copy
2683 // the sret argument into $v0 for the return. Save the argument into
2684 // a virtual register so that we can access it from the return points.
2685 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2686 unsigned Reg = MipsFI->getSRetReturnReg();
2687 if (!Reg) {
Akira Hatanaka30580ce2012-10-19 22:11:40 +00002688 Reg = MF.getRegInfo().
2689 createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002690 MipsFI->setSRetReturnReg(Reg);
2691 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002692 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);
2693 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002694 }
2695
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002696 if (IsVarArg)
2697 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002698
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002699 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002700 // the size of Ins and InVals. This only happens when on varg functions
2701 if (!OutChains.empty()) {
2702 OutChains.push_back(Chain);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002703 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002704 &OutChains[0], OutChains.size());
2705 }
2706
Dan Gohman98ca4f22009-08-05 01:29:28 +00002707 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002708}
2709
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002710//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002711// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002712//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002713
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002714bool
2715MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002716 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002717 const SmallVectorImpl<ISD::OutputArg> &Outs,
2718 LLVMContext &Context) const {
2719 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002720 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002721 RVLocs, Context);
2722 return CCInfo.CheckReturn(Outs, RetCC_Mips);
2723}
2724
Dan Gohman98ca4f22009-08-05 01:29:28 +00002725SDValue
2726MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002727 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002728 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002729 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002730 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002731 // CCValAssign - represent the assignment of
2732 // the return value to a location
2733 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002734 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002735
2736 // CCState - Info about the registers and stack slot.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002737 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002738 *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002739 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002740
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002741 // Analyze return values.
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002742 MipsCCInfo.analyzeReturn(Outs, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002743 MF.getFunction()->getReturnType());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002744
Dan Gohman475871a2008-07-27 21:46:04 +00002745 SDValue Flag;
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002746 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002747
2748 // Copy the result values into the output registers.
2749 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002750 SDValue Val = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002751 CCValAssign &VA = RVLocs[i];
2752 assert(VA.isRegLoc() && "Can only return in registers!");
2753
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002754 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002755 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002756
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002757 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002758
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002759 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002760 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002761 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002762 }
2763
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002764 // The mips ABIs for returning structs by value requires that we copy
2765 // the sret argument into $v0 for the return. We saved the argument into
2766 // a virtual register in the entry block, so now we copy the value out
2767 // and into $v0.
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002768 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002769 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2770 unsigned Reg = MipsFI->getSRetReturnReg();
2771
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002772 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002773 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002774 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Akira Hatanaka2ef5bd32012-10-24 02:10:54 +00002775 unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002776
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002777 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002778 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002779 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002780 }
2781
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002782 RetOps[0] = Chain; // Update chain.
Akira Hatanaka182ef6f2012-07-10 00:19:06 +00002783
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002784 // Add the flag if we have it.
2785 if (Flag.getNode())
2786 RetOps.push_back(Flag);
2787
2788 // Return on Mips is always a "jr $ra"
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002789 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, &RetOps[0], RetOps.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002790}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002791
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002792//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002793// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002794//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002795
2796/// getConstraintType - Given a constraint letter, return the type of
2797/// constraint it is for this target.
2798MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002799getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002800{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002801 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002802 // GCC config/mips/constraints.md
2803 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002804 // 'd' : An address register. Equivalent to r
2805 // unless generating MIPS16 code.
2806 // 'y' : Equivalent to r; retained for
2807 // backwards compatibility.
Eric Christopher1d5a3922012-05-07 06:25:10 +00002808 // 'c' : A register suitable for use in an indirect
2809 // jump. This will always be $25 for -mabicalls.
Eric Christopheraf97f732012-05-07 06:25:19 +00002810 // 'l' : The lo register. 1 word storage.
2811 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002812 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002813 switch (Constraint[0]) {
2814 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002815 case 'd':
2816 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002817 case 'f':
Eric Christopher1d5a3922012-05-07 06:25:10 +00002818 case 'c':
Eric Christopher4adbefe2012-05-07 06:25:15 +00002819 case 'l':
Eric Christopheraf97f732012-05-07 06:25:19 +00002820 case 'x':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002821 return C_RegisterClass;
Jack Carter0b9675d2013-03-04 21:33:15 +00002822 case 'R':
2823 return C_Memory;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002824 }
2825 }
2826 return TargetLowering::getConstraintType(Constraint);
2827}
2828
John Thompson44ab89e2010-10-29 17:29:13 +00002829/// Examine constraint type and operand type and determine a weight value.
2830/// This object must already have been set up with the operand type
2831/// and the current alternative constraint selected.
2832TargetLowering::ConstraintWeight
2833MipsTargetLowering::getSingleConstraintMatchWeight(
2834 AsmOperandInfo &info, const char *constraint) const {
2835 ConstraintWeight weight = CW_Invalid;
2836 Value *CallOperandVal = info.CallOperandVal;
2837 // If we don't have a value, we can't do a match,
2838 // but allow it at the lowest weight.
2839 if (CallOperandVal == NULL)
2840 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002841 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002842 // Look at the constraint type.
2843 switch (*constraint) {
2844 default:
2845 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2846 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002847 case 'd':
2848 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002849 if (type->isIntegerTy())
2850 weight = CW_Register;
2851 break;
2852 case 'f':
2853 if (type->isFloatTy())
2854 weight = CW_Register;
2855 break;
Eric Christopher1d5a3922012-05-07 06:25:10 +00002856 case 'c': // $25 for indirect jumps
Eric Christopher4adbefe2012-05-07 06:25:15 +00002857 case 'l': // lo register
Eric Christopheraf97f732012-05-07 06:25:19 +00002858 case 'x': // hilo register pair
Eric Christopher1d5a3922012-05-07 06:25:10 +00002859 if (type->isIntegerTy())
2860 weight = CW_SpecificReg;
2861 break;
Eric Christopher50ab0392012-05-07 03:13:32 +00002862 case 'I': // signed 16 bit immediate
Eric Christophere5076d42012-05-07 03:13:42 +00002863 case 'J': // integer zero
Eric Christopherf49f8462012-05-07 05:46:29 +00002864 case 'K': // unsigned 16 bit immediate
Eric Christopher5ac47bb2012-05-07 05:46:37 +00002865 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christopher60cfc792012-05-07 05:46:43 +00002866 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher1ce20342012-05-07 05:46:48 +00002867 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopher54412a72012-05-07 06:25:02 +00002868 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher50ab0392012-05-07 03:13:32 +00002869 if (isa<ConstantInt>(CallOperandVal))
2870 weight = CW_Constant;
2871 break;
Jack Carter0b9675d2013-03-04 21:33:15 +00002872 case 'R':
2873 weight = CW_Memory;
2874 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002875 }
2876 return weight;
2877}
2878
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002879/// This is a helper function to parse a physical register string and split it
2880/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
2881/// that is returned indicates whether parsing was successful. The second flag
2882/// is true if the numeric part exists.
2883static std::pair<bool, bool>
2884parsePhysicalReg(const StringRef &C, std::string &Prefix,
2885 unsigned long long &Reg) {
2886 if (C.front() != '{' || C.back() != '}')
2887 return std::make_pair(false, false);
2888
2889 // Search for the first numeric character.
2890 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
2891 I = std::find_if(B, E, std::ptr_fun(isdigit));
2892
2893 Prefix.assign(B, I - B);
2894
2895 // The second flag is set to false if no numeric characters were found.
2896 if (I == E)
2897 return std::make_pair(true, false);
2898
2899 // Parse the numeric characters.
2900 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
2901 true);
2902}
2903
2904std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
2905parseRegForInlineAsmConstraint(const StringRef &C, MVT VT) const {
2906 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2907 const TargetRegisterClass *RC;
2908 std::string Prefix;
2909 unsigned long long Reg;
2910
2911 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
2912
2913 if (!R.first)
2914 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2915
2916 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
2917 // No numeric characters follow "hi" or "lo".
2918 if (R.second)
2919 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2920
2921 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002922 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002923 return std::make_pair(*(RC->begin()), RC);
2924 }
2925
2926 if (!R.second)
2927 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2928
2929 if (Prefix == "$f") { // Parse $f0-$f31.
2930 // If the size of FP registers is 64-bit or Reg is an even number, select
2931 // the 64-bit register class. Otherwise, select the 32-bit register class.
2932 if (VT == MVT::Other)
2933 VT = (Subtarget->isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
2934
2935 RC= getRegClassFor(VT);
2936
2937 if (RC == &Mips::AFGR64RegClass) {
2938 assert(Reg % 2 == 0);
2939 Reg >>= 1;
2940 }
2941 } else if (Prefix == "$fcc") { // Parse $fcc0-$fcc7.
2942 RC = TRI->getRegClass(Mips::FCCRegClassID);
2943 } else { // Parse $0-$31.
2944 assert(Prefix == "$");
2945 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
2946 }
2947
2948 assert(Reg < RC->getNumRegs());
2949 return std::make_pair(*(RC->begin() + Reg), RC);
2950}
2951
Eric Christopher38d64262011-06-29 19:33:04 +00002952/// Given a register class constraint, like 'r', if this corresponds directly
2953/// to an LLVM register class, return a register of 0 and the register class
2954/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002955std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier5b3fca52013-06-22 18:37:38 +00002956getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002957{
2958 if (Constraint.size() == 1) {
2959 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002960 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2961 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002962 case 'r':
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002963 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
2964 if (Subtarget->inMips16Mode())
2965 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka18587862013-08-06 23:08:38 +00002966 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002967 }
Jack Carter10de0252012-07-02 23:35:23 +00002968 if (VT == MVT::i64 && !HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002969 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002970 if (VT == MVT::i64 && HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002971 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002972 // This will generate an error message
2973 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002974 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002975 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002976 return std::make_pair(0U, &Mips::FGR32RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002977 if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
2978 if (Subtarget->isFP64bit())
Craig Topper420761a2012-04-20 07:30:17 +00002979 return std::make_pair(0U, &Mips::FGR64RegClass);
2980 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002981 }
Eric Christopher1d5a3922012-05-07 06:25:10 +00002982 break;
2983 case 'c': // register suitable for indirect jump
2984 if (VT == MVT::i32)
Akira Hatanaka18587862013-08-06 23:08:38 +00002985 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christopher1d5a3922012-05-07 06:25:10 +00002986 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka18587862013-08-06 23:08:38 +00002987 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher4adbefe2012-05-07 06:25:15 +00002988 case 'l': // register suitable for indirect jump
2989 if (VT == MVT::i32)
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002990 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
2991 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopheraf97f732012-05-07 06:25:19 +00002992 case 'x': // register suitable for indirect jump
2993 // Fixme: Not triggering the use of both hi and low
2994 // This will generate an error message
2995 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002996 }
2997 }
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002998
2999 std::pair<unsigned, const TargetRegisterClass *> R;
3000 R = parseRegForInlineAsmConstraint(Constraint, VT);
3001
3002 if (R.second)
3003 return R;
3004
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003005 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3006}
3007
Eric Christopher50ab0392012-05-07 03:13:32 +00003008/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3009/// vector. If it is invalid, don't add anything to Ops.
3010void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3011 std::string &Constraint,
3012 std::vector<SDValue>&Ops,
3013 SelectionDAG &DAG) const {
3014 SDValue Result(0, 0);
3015
3016 // Only support length 1 constraints for now.
3017 if (Constraint.length() > 1) return;
3018
3019 char ConstraintLetter = Constraint[0];
3020 switch (ConstraintLetter) {
3021 default: break; // This will fall through to the generic implementation
3022 case 'I': // Signed 16 bit constant
3023 // If this fails, the parent routine will give an error
3024 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3025 EVT Type = Op.getValueType();
3026 int64_t Val = C->getSExtValue();
3027 if (isInt<16>(Val)) {
3028 Result = DAG.getTargetConstant(Val, Type);
3029 break;
3030 }
3031 }
3032 return;
Eric Christophere5076d42012-05-07 03:13:42 +00003033 case 'J': // integer zero
3034 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3035 EVT Type = Op.getValueType();
3036 int64_t Val = C->getZExtValue();
3037 if (Val == 0) {
3038 Result = DAG.getTargetConstant(0, Type);
3039 break;
3040 }
3041 }
3042 return;
Eric Christopherf49f8462012-05-07 05:46:29 +00003043 case 'K': // unsigned 16 bit immediate
3044 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3045 EVT Type = Op.getValueType();
3046 uint64_t Val = (uint64_t)C->getZExtValue();
3047 if (isUInt<16>(Val)) {
3048 Result = DAG.getTargetConstant(Val, Type);
3049 break;
3050 }
3051 }
3052 return;
Eric Christopher5ac47bb2012-05-07 05:46:37 +00003053 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3054 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3055 EVT Type = Op.getValueType();
3056 int64_t Val = C->getSExtValue();
3057 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3058 Result = DAG.getTargetConstant(Val, Type);
3059 break;
3060 }
3061 }
3062 return;
Eric Christopher60cfc792012-05-07 05:46:43 +00003063 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3064 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3065 EVT Type = Op.getValueType();
3066 int64_t Val = C->getSExtValue();
3067 if ((Val >= -65535) && (Val <= -1)) {
3068 Result = DAG.getTargetConstant(Val, Type);
3069 break;
3070 }
3071 }
3072 return;
Eric Christopher1ce20342012-05-07 05:46:48 +00003073 case 'O': // signed 15 bit immediate
3074 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3075 EVT Type = Op.getValueType();
3076 int64_t Val = C->getSExtValue();
3077 if ((isInt<15>(Val))) {
3078 Result = DAG.getTargetConstant(Val, Type);
3079 break;
3080 }
3081 }
3082 return;
Eric Christopher54412a72012-05-07 06:25:02 +00003083 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3084 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3085 EVT Type = Op.getValueType();
3086 int64_t Val = C->getSExtValue();
3087 if ((Val <= 65535) && (Val >= 1)) {
3088 Result = DAG.getTargetConstant(Val, Type);
3089 break;
3090 }
3091 }
3092 return;
Eric Christopher50ab0392012-05-07 03:13:32 +00003093 }
3094
3095 if (Result.getNode()) {
3096 Ops.push_back(Result);
3097 return;
3098 }
3099
3100 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3101}
3102
Dan Gohman6520e202008-10-18 02:06:02 +00003103bool
Akira Hatanaka94e47282012-11-17 00:25:41 +00003104MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM, Type *Ty) const {
3105 // No global is ever allowed as a base.
3106 if (AM.BaseGV)
3107 return false;
3108
3109 switch (AM.Scale) {
3110 case 0: // "r+i" or just "i", depending on HasBaseReg.
3111 break;
3112 case 1:
3113 if (!AM.HasBaseReg) // allow "r+i".
3114 break;
3115 return false; // disallow "r+r" or "r+r+i".
3116 default:
3117 return false;
3118 }
3119
3120 return true;
3121}
3122
3123bool
Dan Gohman6520e202008-10-18 02:06:02 +00003124MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3125 // The Mips target isn't yet aware of offsets.
3126 return false;
3127}
Evan Chengeb2f9692009-10-27 19:56:55 +00003128
Akira Hatanakae193b322012-06-13 19:33:32 +00003129EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003130 unsigned SrcAlign,
3131 bool IsMemset, bool ZeroMemset,
Akira Hatanakae193b322012-06-13 19:33:32 +00003132 bool MemcpyStrSrc,
3133 MachineFunction &MF) const {
3134 if (Subtarget->hasMips64())
3135 return MVT::i64;
3136
3137 return MVT::i32;
3138}
3139
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003140bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3141 if (VT != MVT::f32 && VT != MVT::f64)
3142 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00003143 if (Imm.isNegZero())
3144 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00003145 return Imm.isZero();
3146}
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003147
3148unsigned MipsTargetLowering::getJumpTableEncoding() const {
3149 if (IsN64)
3150 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liubb481f82012-02-28 07:46:26 +00003151
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003152 return TargetLowering::getJumpTableEncoding();
3153}
Akira Hatanaka7887c902012-10-26 23:56:38 +00003154
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003155/// This function returns true if CallSym is a long double emulation routine.
3156static bool isF128SoftLibCall(const char *CallSym) {
3157 const char *const LibCalls[] =
3158 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3159 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3160 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3161 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3162 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3163 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3164 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3165 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3166 "truncl"};
3167
3168 const char * const *End = LibCalls + array_lengthof(LibCalls);
3169
3170 // Check that LibCalls is sorted alphabetically.
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003171 MipsTargetLowering::LTStr Comp;
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003172
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003173#ifndef NDEBUG
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003174 for (const char * const *I = LibCalls; I < End - 1; ++I)
3175 assert(Comp(*I, *(I + 1)));
3176#endif
3177
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003178 return std::binary_search(LibCalls, End, CallSym, Comp);
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003179}
3180
3181/// This function returns true if Ty is fp128 or i128 which was originally a
3182/// fp128.
3183static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3184 if (Ty->isFP128Ty())
3185 return true;
3186
3187 const ExternalSymbolSDNode *ES =
3188 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3189
3190 // If the Ty is i128 and the function being called is a long double emulation
3191 // routine, then the original type is f128.
3192 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3193}
3194
Reed Kotler46090912013-05-10 22:25:39 +00003195MipsTargetLowering::MipsCC::SpecialCallingConvType
3196 MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
3197 MipsCC::SpecialCallingConvType SpecialCallingConv =
3198 MipsCC::NoSpecialCallingConv;;
3199 if (Subtarget->inMips16HardFloat()) {
3200 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3201 llvm::StringRef Sym = G->getGlobal()->getName();
3202 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
3203 if (F->hasFnAttribute("__Mips16RetHelper")) {
3204 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3205 }
3206 }
3207 }
3208 return SpecialCallingConv;
3209}
3210
3211MipsTargetLowering::MipsCC::MipsCC(
Akira Hatanakaad341d42013-08-20 23:38:40 +00003212 CallingConv::ID CC, bool IsO32_, bool IsFP64_, CCState &Info,
Reed Kotler46090912013-05-10 22:25:39 +00003213 MipsCC::SpecialCallingConvType SpecialCallingConv_)
Akira Hatanakaad341d42013-08-20 23:38:40 +00003214 : CCInfo(Info), CallConv(CC), IsO32(IsO32_), IsFP64(IsFP64_),
Reed Kotler46090912013-05-10 22:25:39 +00003215 SpecialCallingConv(SpecialCallingConv_){
Akira Hatanaka7887c902012-10-26 23:56:38 +00003216 // Pre-allocate reserved argument area.
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003217 CCInfo.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka7887c902012-10-26 23:56:38 +00003218}
3219
Reed Kotler46090912013-05-10 22:25:39 +00003220
Akira Hatanaka7887c902012-10-26 23:56:38 +00003221void MipsTargetLowering::MipsCC::
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003222analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003223 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
3224 std::vector<ArgListEntry> &FuncArgs) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003225 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3226 "CallingConv::Fast shouldn't be used for vararg functions.");
3227
Akira Hatanaka7887c902012-10-26 23:56:38 +00003228 unsigned NumOpnds = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003229 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003230
3231 for (unsigned I = 0; I != NumOpnds; ++I) {
3232 MVT ArgVT = Args[I].VT;
3233 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3234 bool R;
3235
3236 if (ArgFlags.isByVal()) {
3237 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3238 continue;
3239 }
3240
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003241 if (IsVarArg && !Args[I].IsFixed)
Akira Hatanaka7887c902012-10-26 23:56:38 +00003242 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003243 else {
3244 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3245 IsSoftFloat);
3246 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
3247 }
Akira Hatanaka7887c902012-10-26 23:56:38 +00003248
3249 if (R) {
3250#ifndef NDEBUG
3251 dbgs() << "Call operand #" << I << " has unhandled type "
3252 << EVT(ArgVT).getEVTString();
3253#endif
3254 llvm_unreachable(0);
3255 }
3256 }
3257}
3258
3259void MipsTargetLowering::MipsCC::
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003260analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
3261 bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
Akira Hatanaka7887c902012-10-26 23:56:38 +00003262 unsigned NumArgs = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003263 llvm::CCAssignFn *FixedFn = fixedArgFn();
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003264 unsigned CurArgIdx = 0;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003265
3266 for (unsigned I = 0; I != NumArgs; ++I) {
3267 MVT ArgVT = Args[I].VT;
3268 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003269 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3270 CurArgIdx = Args[I].OrigArgIndex;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003271
3272 if (ArgFlags.isByVal()) {
3273 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3274 continue;
3275 }
3276
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003277 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), 0, IsSoftFloat);
3278
3279 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
Akira Hatanaka7887c902012-10-26 23:56:38 +00003280 continue;
3281
3282#ifndef NDEBUG
3283 dbgs() << "Formal Arg #" << I << " has unhandled type "
3284 << EVT(ArgVT).getEVTString();
3285#endif
3286 llvm_unreachable(0);
3287 }
3288}
3289
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003290template<typename Ty>
3291void MipsTargetLowering::MipsCC::
3292analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
3293 const SDNode *CallNode, const Type *RetTy) const {
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003294 CCAssignFn *Fn;
3295
3296 if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
3297 Fn = RetCC_F128Soft;
3298 else
3299 Fn = RetCC_Mips;
3300
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003301 for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
3302 MVT VT = RetVals[I].VT;
3303 ISD::ArgFlagsTy Flags = RetVals[I].Flags;
3304 MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
3305
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003306 if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003307#ifndef NDEBUG
3308 dbgs() << "Call result #" << I << " has unhandled type "
3309 << EVT(VT).getEVTString() << '\n';
3310#endif
3311 llvm_unreachable(0);
3312 }
3313 }
3314}
3315
3316void MipsTargetLowering::MipsCC::
3317analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
3318 const SDNode *CallNode, const Type *RetTy) const {
3319 analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
3320}
3321
3322void MipsTargetLowering::MipsCC::
3323analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
3324 const Type *RetTy) const {
3325 analyzeReturn(Outs, IsSoftFloat, 0, RetTy);
3326}
3327
Akira Hatanaka7887c902012-10-26 23:56:38 +00003328void
3329MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3330 MVT LocVT,
3331 CCValAssign::LocInfo LocInfo,
3332 ISD::ArgFlagsTy ArgFlags) {
3333 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3334
3335 struct ByValArgInfo ByVal;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003336 unsigned RegSize = regSize();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003337 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3338 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3339 RegSize * 2);
3340
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003341 if (useRegsForByval())
Akira Hatanaka7887c902012-10-26 23:56:38 +00003342 allocateRegs(ByVal, ByValSize, Align);
3343
3344 // Allocate space on caller's stack.
3345 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3346 Align);
3347 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3348 LocInfo));
3349 ByValArgs.push_back(ByVal);
3350}
3351
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003352unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
3353 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
3354}
3355
3356unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
3357 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
3358}
3359
3360const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const {
3361 return IsO32 ? O32IntRegs : Mips64IntRegs;
3362}
3363
3364llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3365 if (CallConv == CallingConv::Fast)
3366 return CC_Mips_FastCC;
3367
Reed Kotler46090912013-05-10 22:25:39 +00003368 if (SpecialCallingConv == Mips16RetHelperConv)
3369 return CC_Mips16RetHelper;
Akira Hatanakaad341d42013-08-20 23:38:40 +00003370 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003371}
3372
3373llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
Akira Hatanakaad341d42013-08-20 23:38:40 +00003374 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN_VarArg;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003375}
3376
3377const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const {
3378 return IsO32 ? O32IntRegs : Mips64DPRegs;
3379}
3380
Akira Hatanaka7887c902012-10-26 23:56:38 +00003381void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3382 unsigned ByValSize,
3383 unsigned Align) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003384 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
3385 const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003386 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3387 "Byval argument's size and alignment should be a multiple of"
3388 "RegSize.");
3389
3390 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3391
3392 // If Align > RegSize, the first arg register must be even.
3393 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3394 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3395 ++ByVal.FirstIdx;
3396 }
3397
3398 // Mark the registers allocated.
3399 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3400 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3401 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3402}
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003403
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003404MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3405 const SDNode *CallNode,
3406 bool IsSoftFloat) const {
3407 if (IsSoftFloat || IsO32)
3408 return VT;
3409
3410 // Check if the original type was fp128.
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003411 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003412 assert(VT == MVT::i64);
3413 return MVT::f64;
3414 }
3415
3416 return VT;
3417}
3418
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003419void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003420copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003421 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3422 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3423 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3424 MachineFunction &MF = DAG.getMachineFunction();
3425 MachineFrameInfo *MFI = MF.getFrameInfo();
3426 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
3427 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3428 int FrameObjOffset;
3429
3430 if (RegAreaSize)
3431 FrameObjOffset = (int)CC.reservedArgArea() -
3432 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
3433 else
3434 FrameObjOffset = ByVal.Address;
3435
3436 // Create frame object.
3437 EVT PtrTy = getPointerTy();
3438 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3439 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3440 InVals.push_back(FIN);
3441
3442 if (!ByVal.NumRegs)
3443 return;
3444
3445 // Copy arg registers.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003446 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003447 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3448
3449 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3450 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003451 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003452 unsigned Offset = I * CC.regSize();
3453 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3454 DAG.getConstant(Offset, PtrTy));
3455 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3456 StorePtr, MachinePointerInfo(FuncArg, Offset),
3457 false, false, 0);
3458 OutChains.push_back(Store);
3459 }
3460}
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003461
3462// Copy byVal arg to registers and stack.
3463void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003464passByValArg(SDValue Chain, SDLoc DL,
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00003465 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Craig Toppera0ec3f92013-07-14 04:42:23 +00003466 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003467 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3468 const MipsCC &CC, const ByValArgInfo &ByVal,
3469 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
3470 unsigned ByValSize = Flags.getByValSize();
3471 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
3472 unsigned RegSize = CC.regSize();
3473 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
3474 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
3475
3476 if (ByVal.NumRegs) {
3477 const uint16_t *ArgRegs = CC.intArgRegs();
3478 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
3479 unsigned I = 0;
3480
3481 // Copy words to registers.
3482 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
3483 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3484 DAG.getConstant(Offset, PtrTy));
3485 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3486 MachinePointerInfo(), false, false, false,
3487 Alignment);
3488 MemOpChains.push_back(LoadVal.getValue(1));
3489 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3490 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3491 }
3492
3493 // Return if the struct has been fully copied.
3494 if (ByValSize == Offset)
3495 return;
3496
3497 // Copy the remainder of the byval argument with sub-word loads and shifts.
3498 if (LeftoverBytes) {
3499 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
3500 "Size of the remainder should be smaller than RegSize.");
3501 SDValue Val;
3502
3503 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
3504 Offset < ByValSize; LoadSize /= 2) {
3505 unsigned RemSize = ByValSize - Offset;
3506
3507 if (RemSize < LoadSize)
3508 continue;
3509
3510 // Load subword.
3511 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3512 DAG.getConstant(Offset, PtrTy));
3513 SDValue LoadVal =
3514 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
3515 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
3516 false, false, Alignment);
3517 MemOpChains.push_back(LoadVal.getValue(1));
3518
3519 // Shift the loaded value.
3520 unsigned Shamt;
3521
3522 if (isLittle)
3523 Shamt = TotalSizeLoaded;
3524 else
3525 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
3526
3527 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3528 DAG.getConstant(Shamt, MVT::i32));
3529
3530 if (Val.getNode())
3531 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3532 else
3533 Val = Shift;
3534
3535 Offset += LoadSize;
3536 TotalSizeLoaded += LoadSize;
3537 Alignment = std::min(Alignment, LoadSize);
3538 }
3539
3540 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3541 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3542 return;
3543 }
3544 }
3545
3546 // Copy remainder of byval arg to it with memcpy.
3547 unsigned MemCpySize = ByValSize - Offset;
3548 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3549 DAG.getConstant(Offset, PtrTy));
3550 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3551 DAG.getIntPtrConstant(ByVal.Address));
3552 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3553 DAG.getConstant(MemCpySize, PtrTy), Alignment,
3554 /*isVolatile=*/false, /*AlwaysInline=*/false,
3555 MachinePointerInfo(0), MachinePointerInfo(0));
3556 MemOpChains.push_back(Chain);
3557}
Akira Hatanakaf0848472012-10-27 00:21:13 +00003558
3559void
3560MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3561 const MipsCC &CC, SDValue Chain,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003562 SDLoc DL, SelectionDAG &DAG) const {
Akira Hatanakaf0848472012-10-27 00:21:13 +00003563 unsigned NumRegs = CC.numIntArgRegs();
3564 const uint16_t *ArgRegs = CC.intArgRegs();
3565 const CCState &CCInfo = CC.getCCInfo();
3566 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
3567 unsigned RegSize = CC.regSize();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003568 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003569 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3570 MachineFunction &MF = DAG.getMachineFunction();
3571 MachineFrameInfo *MFI = MF.getFrameInfo();
3572 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3573
3574 // Offset of the first variable argument from stack pointer.
3575 int VaArgOffset;
3576
3577 if (NumRegs == Idx)
3578 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
3579 else
3580 VaArgOffset =
3581 (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
3582
3583 // Record the frame index of the first variable argument
3584 // which is a value necessary to VASTART.
3585 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3586 MipsFI->setVarArgsFrameIndex(FI);
3587
3588 // Copy the integer registers that have not been used for argument passing
3589 // to the argument register save area. For O32, the save area is allocated
3590 // in the caller's stack frame, while for N32/64, it is allocated in the
3591 // callee's stack frame.
3592 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003593 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003594 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3595 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3596 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3597 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3598 MachinePointerInfo(), false, false, 0);
3599 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
3600 OutChains.push_back(Store);
3601 }
3602}