blob: 5f019367b295840b3f60b231f5e1209c302ea761 [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";
Akira Hatanaka0f843822011-06-07 18:58:42 +0000227 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000228 }
229}
230
231MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +0000232MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka8b4198d2011-09-26 21:47:02 +0000233 : TargetLowering(TM, new MipsTargetObjectFile()),
234 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +0000235 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
236 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000237 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000238 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +0000239 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000240 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000241
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000242 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
244 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
245 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000246
Eli Friedman6055a6a2009-07-17 04:07:24 +0000247 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
249 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000250
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000251 // Used by legalize types to correctly generate the setcc result.
252 // Without this, every float setcc comes with a AND/OR with the result,
253 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000254 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000256
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000257 // Mips Custom Operations
Akira Hatanakab7656a92013-03-06 21:32:03 +0000258 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000260 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
262 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
263 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
264 setOperationAction(ISD::SELECT, MVT::f32, Custom);
265 setOperationAction(ISD::SELECT, MVT::f64, Custom);
266 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +0000267 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
268 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanaka0a40c232012-03-09 23:46:03 +0000269 setOperationAction(ISD::SETCC, MVT::f32, Custom);
270 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000272 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000273 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
274 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000275 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000276
Akira Hatanakac12a6e62012-04-11 22:49:04 +0000277 if (!TM.Options.NoNaNsFPMath) {
278 setOperationAction(ISD::FABS, MVT::f32, Custom);
279 setOperationAction(ISD::FABS, MVT::f64, Custom);
280 }
281
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000282 if (HasMips64) {
283 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
284 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
285 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
286 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
287 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
288 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka7664f052012-06-02 00:04:42 +0000289 setOperationAction(ISD::LOAD, MVT::i64, Custom);
290 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000291 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000292 }
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000293
Akira Hatanakaa284acb2012-05-09 00:55:21 +0000294 if (!HasMips64) {
295 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
296 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
297 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
298 }
299
Akira Hatanakae90a3bc2012-11-07 19:10:58 +0000300 setOperationAction(ISD::ADD, MVT::i32, Custom);
301 if (HasMips64)
302 setOperationAction(ISD::ADD, MVT::i64, Custom);
303
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000304 setOperationAction(ISD::SDIV, MVT::i32, Expand);
305 setOperationAction(ISD::SREM, MVT::i32, Expand);
306 setOperationAction(ISD::UDIV, MVT::i32, Expand);
307 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000308 setOperationAction(ISD::SDIV, MVT::i64, Expand);
309 setOperationAction(ISD::SREM, MVT::i64, Expand);
310 setOperationAction(ISD::UDIV, MVT::i64, Expand);
311 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000312
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000313 // Operations not directly supported by Mips.
Tom Stellard3ef53832013-03-08 15:36:57 +0000314 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
315 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
316 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
317 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
319 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000320 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000321 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000322 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
324 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000325 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000327 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000328 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
329 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
330 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
331 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000333 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka1d165f12012-07-31 20:54:48 +0000334 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
335 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000336
Akira Hatanaka56633442011-09-20 23:53:09 +0000337 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000338 setOperationAction(ISD::ROTR, MVT::i32, Expand);
339
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000340 if (!Subtarget->hasMips64r2())
341 setOperationAction(ISD::ROTR, MVT::i64, Expand);
342
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000344 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000345 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000346 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000347 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
348 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
350 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000351 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000352 setOperationAction(ISD::FLOG, MVT::f32, Expand);
353 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
354 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
355 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000356 setOperationAction(ISD::FMA, MVT::f32, Expand);
357 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka21ecc2f2012-03-29 18:43:11 +0000358 setOperationAction(ISD::FREM, MVT::f32, Expand);
359 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000360
Akira Hatanaka1cc63332012-04-11 22:59:08 +0000361 if (!TM.Options.NoNaNsFPMath) {
362 setOperationAction(ISD::FNEG, MVT::f32, Expand);
363 setOperationAction(ISD::FNEG, MVT::f64, Expand);
364 }
365
Akira Hatanaka544cc212013-01-30 00:26:49 +0000366 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
367
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000368 setOperationAction(ISD::VAARG, MVT::Other, Expand);
369 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
370 setOperationAction(ISD::VAEND, MVT::Other, Expand);
371
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000372 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000373 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
374 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000375
Jia Liubb481f82012-02-28 07:46:26 +0000376 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
377 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
378 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
379 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman4db5aca2011-08-29 18:23:02 +0000380
Eli Friedman26689ac2011-08-03 21:06:02 +0000381 setInsertFencesForAtomic(true);
382
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000383 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000384 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
385 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000386 }
387
Akira Hatanakac79507a2011-12-21 00:20:27 +0000388 if (!Subtarget->hasBitCount()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000389 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Akira Hatanakac79507a2011-12-21 00:20:27 +0000390 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
391 }
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000392
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000393 if (!Subtarget->hasSwap()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000395 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
396 }
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000397
Akira Hatanaka7664f052012-06-02 00:04:42 +0000398 if (HasMips64) {
399 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
400 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
401 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
402 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
403 }
404
Akira Hatanaka97585622013-07-26 20:58:55 +0000405 setOperationAction(ISD::TRAP, MVT::Other, Legal);
406
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000407 setTargetDAGCombine(ISD::SDIVREM);
408 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanakaee8c3b02012-03-08 03:26:37 +0000409 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000410 setTargetDAGCombine(ISD::AND);
411 setTargetDAGCombine(ISD::OR);
Akira Hatanaka87827072012-06-13 20:33:18 +0000412 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000413
Akira Hatanaka5fdf5002012-03-08 01:59:33 +0000414 setMinFunctionAlignment(HasMips64 ? 3 : 2);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000415
Akira Hatanaka3f5b1072012-02-02 03:17:04 +0000416 setStackPointerRegisterToSaveRestore(IsN64 ? Mips::SP_64 : Mips::SP);
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000417
Akira Hatanaka590baca2012-02-02 03:13:40 +0000418 setExceptionPointerRegister(IsN64 ? Mips::A0_64 : Mips::A0);
419 setExceptionSelectorRegister(IsN64 ? Mips::A1_64 : Mips::A1);
Akira Hatanakae193b322012-06-13 19:33:32 +0000420
Jim Grosbach3450f802013-02-20 21:13:59 +0000421 MaxStoresPerMemcpy = 16;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000422}
423
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000424const MipsTargetLowering *MipsTargetLowering::create(MipsTargetMachine &TM) {
425 if (TM.getSubtargetImpl()->inMips16Mode())
426 return llvm::createMips16TargetLowering(TM);
Jia Liubb481f82012-02-28 07:46:26 +0000427
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000428 return llvm::createMipsSETargetLowering(TM);
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000429}
430
Matt Arsenault225ed702013-05-18 00:21:46 +0000431EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakae13f4412013-01-04 20:06:01 +0000432 if (!VT.isVector())
433 return MVT::i32;
434 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +0000435}
436
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000437static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000438 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000439 const MipsSubtarget *Subtarget) {
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000440 if (DCI.isBeforeLegalizeOps())
441 return SDValue();
442
Akira Hatanakadda4a072011-10-03 21:06:13 +0000443 EVT Ty = N->getValueType(0);
Akira Hatanakacbaf6d02013-08-14 00:47:08 +0000444 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
445 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000446 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
447 MipsISD::DivRemU16;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000448 SDLoc DL(N);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000449
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000450 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000451 N->getOperand(0), N->getOperand(1));
452 SDValue InChain = DAG.getEntryNode();
453 SDValue InGlue = DivRem;
454
455 // insert MFLO
456 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000457 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000458 InGlue);
459 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
460 InChain = CopyFromLo.getValue(1);
461 InGlue = CopyFromLo.getValue(2);
462 }
463
464 // insert MFHI
465 if (N->hasAnyUseOfValue(1)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000466 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000467 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000468 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
469 }
470
471 return SDValue();
472}
473
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000474static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000475 switch (CC) {
476 default: llvm_unreachable("Unknown fp condition code!");
477 case ISD::SETEQ:
478 case ISD::SETOEQ: return Mips::FCOND_OEQ;
479 case ISD::SETUNE: return Mips::FCOND_UNE;
480 case ISD::SETLT:
481 case ISD::SETOLT: return Mips::FCOND_OLT;
482 case ISD::SETGT:
483 case ISD::SETOGT: return Mips::FCOND_OGT;
484 case ISD::SETLE:
485 case ISD::SETOLE: return Mips::FCOND_OLE;
486 case ISD::SETGE:
487 case ISD::SETOGE: return Mips::FCOND_OGE;
488 case ISD::SETULT: return Mips::FCOND_ULT;
489 case ISD::SETULE: return Mips::FCOND_ULE;
490 case ISD::SETUGT: return Mips::FCOND_UGT;
491 case ISD::SETUGE: return Mips::FCOND_UGE;
492 case ISD::SETUO: return Mips::FCOND_UN;
493 case ISD::SETO: return Mips::FCOND_OR;
494 case ISD::SETNE:
495 case ISD::SETONE: return Mips::FCOND_ONE;
496 case ISD::SETUEQ: return Mips::FCOND_UEQ;
497 }
498}
499
500
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000501/// This function returns true if the floating point conditional branches and
502/// conditional moves which use condition code CC should be inverted.
503static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000504 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
505 return false;
506
Akira Hatanaka82099682011-12-19 19:52:25 +0000507 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
508 "Illegal Condition Code");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000509
Akira Hatanaka82099682011-12-19 19:52:25 +0000510 return true;
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000511}
512
513// Creates and returns an FPCmp node from a setcc node.
514// Returns Op if setcc is not a floating point comparison.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000515static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000516 // must be a SETCC node
517 if (Op.getOpcode() != ISD::SETCC)
518 return Op;
519
520 SDValue LHS = Op.getOperand(0);
521
522 if (!LHS.getValueType().isFloatingPoint())
523 return Op;
524
525 SDValue RHS = Op.getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000526 SDLoc DL(Op);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000527
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000528 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
529 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000530 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
531
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000532 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000533 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000534}
535
536// Creates and returns a CMovFPT/F node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000537static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000538 SDValue False, SDLoc DL) {
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000539 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
540 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka407883b2013-07-26 20:51:20 +0000541 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000542
543 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka407883b2013-07-26 20:51:20 +0000544 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000545}
546
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000547static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000548 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000549 const MipsSubtarget *Subtarget) {
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000550 if (DCI.isBeforeLegalizeOps())
551 return SDValue();
552
553 SDValue SetCC = N->getOperand(0);
554
555 if ((SetCC.getOpcode() != ISD::SETCC) ||
556 !SetCC.getOperand(0).getValueType().isInteger())
557 return SDValue();
558
559 SDValue False = N->getOperand(2);
560 EVT FalseTy = False.getValueType();
561
562 if (!FalseTy.isInteger())
563 return SDValue();
564
565 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(False);
566
567 if (!CN || CN->getZExtValue())
568 return SDValue();
569
Andrew Trickac6d9be2013-05-25 02:42:55 +0000570 const SDLoc DL(N);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000571 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
572 SDValue True = N->getOperand(1);
Akira Hatanaka864f6602012-06-14 21:10:56 +0000573
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000574 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
575 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
Akira Hatanaka864f6602012-06-14 21:10:56 +0000576
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000577 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
578}
579
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000580static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000581 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000582 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000583 // Pattern match EXT.
584 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
585 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000586 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000587 return SDValue();
588
589 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000590 unsigned ShiftRightOpc = ShiftRight.getOpcode();
591
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000592 // Op's first operand must be a shift right.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000593 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000594 return SDValue();
595
596 // The second operand of the shift must be an immediate.
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000597 ConstantSDNode *CN;
598 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
599 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000600
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000601 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000602 uint64_t SMPos, SMSize;
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000603
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000604 // Op's second operand must be a shifted mask.
605 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000606 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000607 return SDValue();
608
609 // Return if the shifted mask does not start at bit 0 or the sum of its size
610 // and Pos exceeds the word's size.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000611 EVT ValTy = N->getValueType(0);
612 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000613 return SDValue();
614
Andrew Trickac6d9be2013-05-25 02:42:55 +0000615 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka82099682011-12-19 19:52:25 +0000616 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000617 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000618}
Jia Liubb481f82012-02-28 07:46:26 +0000619
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000620static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000621 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000622 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000623 // Pattern match INS.
624 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liubb481f82012-02-28 07:46:26 +0000625 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000626 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000627 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000628 return SDValue();
629
630 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
631 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
632 ConstantSDNode *CN;
633
634 // See if Op's first operand matches (and $src1 , mask0).
635 if (And0.getOpcode() != ISD::AND)
636 return SDValue();
637
638 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000639 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000640 return SDValue();
641
642 // See if Op's second operand matches (and (shl $src, pos), mask1).
643 if (And1.getOpcode() != ISD::AND)
644 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000645
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000646 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000647 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000648 return SDValue();
649
650 // The shift masks must have the same position and size.
651 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
652 return SDValue();
653
654 SDValue Shl = And1.getOperand(0);
655 if (Shl.getOpcode() != ISD::SHL)
656 return SDValue();
657
658 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
659 return SDValue();
660
661 unsigned Shamt = CN->getZExtValue();
662
663 // Return if the shift amount and the first bit position of mask are not the
Jia Liubb481f82012-02-28 07:46:26 +0000664 // same.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000665 EVT ValTy = N->getValueType(0);
666 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000667 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000668
Andrew Trickac6d9be2013-05-25 02:42:55 +0000669 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000670 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka82099682011-12-19 19:52:25 +0000671 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000672}
Jia Liubb481f82012-02-28 07:46:26 +0000673
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000674static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka87827072012-06-13 20:33:18 +0000675 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000676 const MipsSubtarget *Subtarget) {
Akira Hatanaka87827072012-06-13 20:33:18 +0000677 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
678
679 if (DCI.isBeforeLegalizeOps())
680 return SDValue();
681
682 SDValue Add = N->getOperand(1);
683
684 if (Add.getOpcode() != ISD::ADD)
685 return SDValue();
686
687 SDValue Lo = Add.getOperand(1);
688
689 if ((Lo.getOpcode() != MipsISD::Lo) ||
690 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
691 return SDValue();
692
693 EVT ValTy = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000694 SDLoc DL(N);
Akira Hatanaka87827072012-06-13 20:33:18 +0000695
696 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
697 Add.getOperand(0));
698 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
699}
700
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000701SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000702 const {
703 SelectionDAG &DAG = DCI.DAG;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000704 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000705
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000706 switch (Opc) {
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000707 default: break;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000708 case ISD::SDIVREM:
709 case ISD::UDIVREM:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000710 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000711 case ISD::SELECT:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000712 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000713 case ISD::AND:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000714 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000715 case ISD::OR:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000716 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka87827072012-06-13 20:33:18 +0000717 case ISD::ADD:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000718 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000719 }
720
721 return SDValue();
722}
723
Akira Hatanakab430cec2012-09-21 23:58:31 +0000724void
725MipsTargetLowering::LowerOperationWrapper(SDNode *N,
726 SmallVectorImpl<SDValue> &Results,
727 SelectionDAG &DAG) const {
728 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
729
730 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
731 Results.push_back(Res.getValue(I));
732}
733
734void
735MipsTargetLowering::ReplaceNodeResults(SDNode *N,
736 SmallVectorImpl<SDValue> &Results,
737 SelectionDAG &DAG) const {
Akira Hatanaka13ec4812013-04-30 21:17:07 +0000738 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakab430cec2012-09-21 23:58:31 +0000739}
740
Dan Gohman475871a2008-07-27 21:46:04 +0000741SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000742LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000743{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000744 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000745 {
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000746 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
747 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
748 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
749 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
750 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
751 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
752 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
753 case ISD::SELECT: return lowerSELECT(Op, DAG);
754 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
755 case ISD::SETCC: return lowerSETCC(Op, DAG);
756 case ISD::VASTART: return lowerVASTART(Op, DAG);
757 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
758 case ISD::FABS: return lowerFABS(Op, DAG);
759 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
760 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
761 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000762 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
763 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
764 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
765 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
766 case ISD::LOAD: return lowerLOAD(Op, DAG);
767 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000768 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000769 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000770 }
Dan Gohman475871a2008-07-27 21:46:04 +0000771 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000772}
773
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000774//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000775// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000776//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000777
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000778// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000779// MachineFunction as a live in value. It also creates a corresponding
780// virtual register for it.
781static unsigned
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000782addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000783{
Chris Lattner84bc5422007-12-31 04:13:23 +0000784 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
785 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000786 return VReg;
787}
788
Akira Hatanakaf8941992013-05-20 18:07:43 +0000789static MachineBasicBlock *expandPseudoDIV(MachineInstr *MI,
790 MachineBasicBlock &MBB,
791 const TargetInstrInfo &TII,
792 bool Is64Bit) {
793 if (NoZeroDivCheck)
794 return &MBB;
795
796 // Insert instruction "teq $divisor_reg, $zero, 7".
797 MachineBasicBlock::iterator I(MI);
798 MachineInstrBuilder MIB;
799 MIB = BuildMI(MBB, llvm::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
800 .addOperand(MI->getOperand(2)).addReg(Mips::ZERO).addImm(7);
801
802 // Use the 32-bit sub-register if this is a 64-bit division.
803 if (Is64Bit)
804 MIB->getOperand(0).setSubReg(Mips::sub_32);
805
806 return &MBB;
807}
808
Akira Hatanaka01f70892012-09-27 02:15:57 +0000809MachineBasicBlock *
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000810MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000811 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000812 switch (MI->getOpcode()) {
Reed Kotlerffbe4322013-02-21 04:22:38 +0000813 default:
814 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000815 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000816 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000817 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000818 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000819 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000820 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000821 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000822 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000823
824 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000825 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000826 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000827 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000828 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000829 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000830 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000831 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000832
833 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000834 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000835 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000836 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000837 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000838 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000839 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000840 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000841
842 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000843 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000844 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000845 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000846 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000847 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000848 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000849 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000850
851 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000852 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000853 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000854 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000855 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000856 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000857 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000858 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000859
860 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000861 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000862 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000863 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000864 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000865 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000866 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000867 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000868
869 case Mips::ATOMIC_SWAP_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000870 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000871 case Mips::ATOMIC_SWAP_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000872 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000873 case Mips::ATOMIC_SWAP_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000874 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000875 case Mips::ATOMIC_SWAP_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000876 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000877
878 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000879 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000880 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000881 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000882 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000883 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000884 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000885 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanakaf8941992013-05-20 18:07:43 +0000886 case Mips::PseudoSDIV:
887 case Mips::PseudoUDIV:
888 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), false);
889 case Mips::PseudoDSDIV:
890 case Mips::PseudoDUDIV:
891 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), true);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000892 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000893}
894
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000895// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
896// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
897MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000898MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000899 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000900 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000901 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000902
903 MachineFunction *MF = BB->getParent();
904 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000905 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000906 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000907 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000908 unsigned LL, SC, AND, NOR, ZERO, BEQ;
909
910 if (Size == 4) {
Akira Hatanakaa98a4862013-08-20 21:08:22 +0000911 LL = Mips::LL;
912 SC = Mips::SC;
Akira Hatanaka59068062011-11-11 04:14:30 +0000913 AND = Mips::AND;
914 NOR = Mips::NOR;
915 ZERO = Mips::ZERO;
916 BEQ = Mips::BEQ;
917 }
918 else {
Akira Hatanakaa98a4862013-08-20 21:08:22 +0000919 LL = Mips::LLD;
920 SC = Mips::SCD;
Akira Hatanaka59068062011-11-11 04:14:30 +0000921 AND = Mips::AND64;
922 NOR = Mips::NOR64;
923 ZERO = Mips::ZERO_64;
924 BEQ = Mips::BEQ64;
925 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000926
Akira Hatanaka4061da12011-07-19 20:11:17 +0000927 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000928 unsigned Ptr = MI->getOperand(1).getReg();
929 unsigned Incr = MI->getOperand(2).getReg();
930
Akira Hatanaka4061da12011-07-19 20:11:17 +0000931 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
932 unsigned AndRes = RegInfo.createVirtualRegister(RC);
933 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000934
935 // insert new blocks after the current block
936 const BasicBlock *LLVM_BB = BB->getBasicBlock();
937 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
938 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
939 MachineFunction::iterator It = BB;
940 ++It;
941 MF->insert(It, loopMBB);
942 MF->insert(It, exitMBB);
943
944 // Transfer the remainder of BB and its successor edges to exitMBB.
945 exitMBB->splice(exitMBB->begin(), BB,
946 llvm::next(MachineBasicBlock::iterator(MI)),
947 BB->end());
948 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
949
950 // thisMBB:
951 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000952 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000953 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000954 loopMBB->addSuccessor(loopMBB);
955 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000956
957 // loopMBB:
958 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000959 // <binop> storeval, oldval, incr
960 // sc success, storeval, 0(ptr)
961 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000962 BB = loopMBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000963 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000964 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000965 // and andres, oldval, incr
966 // nor storeval, $0, andres
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000967 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
968 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000969 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000970 // <binop> storeval, oldval, incr
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000971 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000972 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000973 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000974 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000975 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
976 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000977
978 MI->eraseFromParent(); // The instruction is gone now.
979
Akira Hatanaka939ece12011-07-19 03:42:13 +0000980 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000981}
982
983MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000984MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000985 MachineBasicBlock *BB,
986 unsigned Size, unsigned BinOpcode,
987 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000988 assert((Size == 1 || Size == 2) &&
989 "Unsupported size for EmitAtomicBinaryPartial.");
990
991 MachineFunction *MF = BB->getParent();
992 MachineRegisterInfo &RegInfo = MF->getRegInfo();
993 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
994 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000995 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000996
997 unsigned Dest = MI->getOperand(0).getReg();
998 unsigned Ptr = MI->getOperand(1).getReg();
999 unsigned Incr = MI->getOperand(2).getReg();
1000
Akira Hatanaka4061da12011-07-19 20:11:17 +00001001 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1002 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001003 unsigned Mask = RegInfo.createVirtualRegister(RC);
1004 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001005 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1006 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001007 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001008 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1009 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1010 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1011 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1012 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001013 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001014 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1015 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1016 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1017 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1018 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001019
1020 // insert new blocks after the current block
1021 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1022 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001023 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001024 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1025 MachineFunction::iterator It = BB;
1026 ++It;
1027 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001028 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001029 MF->insert(It, exitMBB);
1030
1031 // Transfer the remainder of BB and its successor edges to exitMBB.
1032 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001033 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001034 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1035
Akira Hatanaka81b44112011-07-19 17:09:53 +00001036 BB->addSuccessor(loopMBB);
1037 loopMBB->addSuccessor(loopMBB);
1038 loopMBB->addSuccessor(sinkMBB);
1039 sinkMBB->addSuccessor(exitMBB);
1040
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001041 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001042 // addiu masklsb2,$0,-4 # 0xfffffffc
1043 // and alignedaddr,ptr,masklsb2
1044 // andi ptrlsb2,ptr,3
1045 // sll shiftamt,ptrlsb2,3
1046 // ori maskupper,$0,255 # 0xff
1047 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001048 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001049 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001050
1051 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001052 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001053 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001054 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001055 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001056 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001057 if (Subtarget->isLittle()) {
1058 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1059 } else {
1060 unsigned Off = RegInfo.createVirtualRegister(RC);
1061 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1062 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1063 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1064 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001065 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001066 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001067 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001068 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001069 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka51122432013-07-01 20:39:53 +00001070 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001071
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001072 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001073 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001074 // ll oldval,0(alignedaddr)
1075 // binop binopres,oldval,incr2
1076 // and newval,binopres,mask
1077 // and maskedoldval0,oldval,mask2
1078 // or storeval,maskedoldval0,newval
1079 // sc success,storeval,0(alignedaddr)
1080 // beq success,$0,loopMBB
1081
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001082 // atomic.swap
1083 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001084 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001085 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001086 // and maskedoldval0,oldval,mask2
1087 // or storeval,maskedoldval0,newval
1088 // sc success,storeval,0(alignedaddr)
1089 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001090
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001091 BB = loopMBB;
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001092 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001093 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001094 // and andres, oldval, incr2
1095 // nor binopres, $0, andres
1096 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001097 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1098 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001099 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001100 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001101 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001102 // <binop> binopres, oldval, incr2
1103 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001104 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1105 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001106 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001107 // and newval, incr2, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001108 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001109 }
Jia Liubb481f82012-02-28 07:46:26 +00001110
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001111 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001112 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001113 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001114 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001115 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001116 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001117 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001118 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001119
Akira Hatanaka939ece12011-07-19 03:42:13 +00001120 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001121 // and maskedoldval1,oldval,mask
1122 // srl srlres,maskedoldval1,shiftamt
1123 // sll sllres,srlres,24
1124 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001125 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001126 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001127
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001128 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001129 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001130 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001131 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001132 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001133 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001134 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001135 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001136
1137 MI->eraseFromParent(); // The instruction is gone now.
1138
Akira Hatanaka939ece12011-07-19 03:42:13 +00001139 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001140}
1141
1142MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001143MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001144 MachineBasicBlock *BB,
1145 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001146 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001147
1148 MachineFunction *MF = BB->getParent();
1149 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001150 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001151 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001152 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001153 unsigned LL, SC, ZERO, BNE, BEQ;
1154
1155 if (Size == 4) {
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001156 LL = Mips::LL;
1157 SC = Mips::SC;
Akira Hatanaka59068062011-11-11 04:14:30 +00001158 ZERO = Mips::ZERO;
1159 BNE = Mips::BNE;
1160 BEQ = Mips::BEQ;
1161 }
1162 else {
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001163 LL = Mips::LLD;
1164 SC = Mips::SCD;
Akira Hatanaka59068062011-11-11 04:14:30 +00001165 ZERO = Mips::ZERO_64;
1166 BNE = Mips::BNE64;
1167 BEQ = Mips::BEQ64;
1168 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001169
1170 unsigned Dest = MI->getOperand(0).getReg();
1171 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001172 unsigned OldVal = MI->getOperand(2).getReg();
1173 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001174
Akira Hatanaka4061da12011-07-19 20:11:17 +00001175 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001176
1177 // insert new blocks after the current block
1178 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1179 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1180 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1181 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1182 MachineFunction::iterator It = BB;
1183 ++It;
1184 MF->insert(It, loop1MBB);
1185 MF->insert(It, loop2MBB);
1186 MF->insert(It, exitMBB);
1187
1188 // Transfer the remainder of BB and its successor edges to exitMBB.
1189 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001190 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001191 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1192
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001193 // thisMBB:
1194 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001195 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001196 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001197 loop1MBB->addSuccessor(exitMBB);
1198 loop1MBB->addSuccessor(loop2MBB);
1199 loop2MBB->addSuccessor(loop1MBB);
1200 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001201
1202 // loop1MBB:
1203 // ll dest, 0(ptr)
1204 // bne dest, oldval, exitMBB
1205 BB = loop1MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001206 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1207 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001208 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001209
1210 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001211 // sc success, newval, 0(ptr)
1212 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001213 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001214 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001215 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001216 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka59068062011-11-11 04:14:30 +00001217 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001218
1219 MI->eraseFromParent(); // The instruction is gone now.
1220
Akira Hatanaka939ece12011-07-19 03:42:13 +00001221 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001222}
1223
1224MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001225MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001226 MachineBasicBlock *BB,
1227 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001228 assert((Size == 1 || Size == 2) &&
1229 "Unsupported size for EmitAtomicCmpSwapPartial.");
1230
1231 MachineFunction *MF = BB->getParent();
1232 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1233 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1234 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001235 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001236
1237 unsigned Dest = MI->getOperand(0).getReg();
1238 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001239 unsigned CmpVal = MI->getOperand(2).getReg();
1240 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001241
Akira Hatanaka4061da12011-07-19 20:11:17 +00001242 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1243 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001244 unsigned Mask = RegInfo.createVirtualRegister(RC);
1245 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001246 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1247 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1248 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1249 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1250 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1251 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1252 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1253 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1254 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1255 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1256 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1257 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1258 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1259 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001260
1261 // insert new blocks after the current block
1262 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1263 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1264 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001265 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001266 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1267 MachineFunction::iterator It = BB;
1268 ++It;
1269 MF->insert(It, loop1MBB);
1270 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001271 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001272 MF->insert(It, exitMBB);
1273
1274 // Transfer the remainder of BB and its successor edges to exitMBB.
1275 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001276 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001277 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1278
Akira Hatanaka81b44112011-07-19 17:09:53 +00001279 BB->addSuccessor(loop1MBB);
1280 loop1MBB->addSuccessor(sinkMBB);
1281 loop1MBB->addSuccessor(loop2MBB);
1282 loop2MBB->addSuccessor(loop1MBB);
1283 loop2MBB->addSuccessor(sinkMBB);
1284 sinkMBB->addSuccessor(exitMBB);
1285
Akira Hatanaka70564a92011-07-19 18:14:26 +00001286 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001287 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001288 // addiu masklsb2,$0,-4 # 0xfffffffc
1289 // and alignedaddr,ptr,masklsb2
1290 // andi ptrlsb2,ptr,3
1291 // sll shiftamt,ptrlsb2,3
1292 // ori maskupper,$0,255 # 0xff
1293 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001294 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001295 // andi maskedcmpval,cmpval,255
1296 // sll shiftedcmpval,maskedcmpval,shiftamt
1297 // andi maskednewval,newval,255
1298 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001299 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001300 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001301 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001302 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001303 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001304 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001305 if (Subtarget->isLittle()) {
1306 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1307 } else {
1308 unsigned Off = RegInfo.createVirtualRegister(RC);
1309 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1310 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1311 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1312 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001313 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001314 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001315 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001316 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001317 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1318 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001319 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001320 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001321 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001322 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001323 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001324 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001325 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001326
1327 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001328 // ll oldval,0(alginedaddr)
1329 // and maskedoldval0,oldval,mask
1330 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001331 BB = loop1MBB;
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001332 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001333 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001334 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001335 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001336 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001337
1338 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001339 // and maskedoldval1,oldval,mask2
1340 // or storeval,maskedoldval1,shiftednewval
1341 // sc success,storeval,0(alignedaddr)
1342 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001343 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001344 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001345 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001346 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001347 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001348 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001349 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001350 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001351 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001352
Akira Hatanaka939ece12011-07-19 03:42:13 +00001353 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001354 // srl srlres,maskedoldval0,shiftamt
1355 // sll sllres,srlres,24
1356 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001357 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001358 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001359
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001360 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001361 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001362 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001363 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001364 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001365 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001366
1367 MI->eraseFromParent(); // The instruction is gone now.
1368
Akira Hatanaka939ece12011-07-19 03:42:13 +00001369 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001370}
1371
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001372//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001373// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001374//===----------------------------------------------------------------------===//
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001375SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakab7656a92013-03-06 21:32:03 +00001376 SDValue Chain = Op.getOperand(0);
1377 SDValue Table = Op.getOperand(1);
1378 SDValue Index = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001379 SDLoc DL(Op);
Akira Hatanakab7656a92013-03-06 21:32:03 +00001380 EVT PTy = getPointerTy();
1381 unsigned EntrySize =
1382 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1383
1384 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1385 DAG.getConstant(EntrySize, PTy));
1386 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1387
1388 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1389 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1390 MachinePointerInfo::getJumpTable(), MemVT, false, false,
1391 0);
1392 Chain = Addr.getValue(1);
1393
1394 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || IsN64) {
1395 // For PIC, the sequence is:
1396 // BRIND(load(Jumptable + index) + RelocBase)
1397 // RelocBase can be JumpTable, GOT or some sort of global base.
1398 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1399 getPICJumpTableRelocBase(Table, DAG));
1400 }
1401
1402 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1403}
1404
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001405SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001406lowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001407{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001408 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001409 // the block to branch to if the condition is true.
1410 SDValue Chain = Op.getOperand(0);
1411 SDValue Dest = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001412 SDLoc DL(Op);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001413
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001414 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001415
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001416 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001417 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001418 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001419
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001420 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001421 Mips::CondCode CC =
1422 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanaka9cf07242013-03-30 01:16:38 +00001423 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1424 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001425 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001426 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001427 FCC0, Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001428}
1429
1430SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001431lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001432{
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001433 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001434
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001435 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001436 if (Cond.getOpcode() != MipsISD::FPCmp)
1437 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001438
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001439 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001440 SDLoc(Op));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001441}
1442
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001443SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001444lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001445{
Andrew Trickac6d9be2013-05-25 02:42:55 +00001446 SDLoc DL(Op);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001447 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault225ed702013-05-18 00:21:46 +00001448 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1449 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001450 Op.getOperand(0), Op.getOperand(1),
1451 Op.getOperand(4));
1452
1453 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1454 Op.getOperand(3));
1455}
1456
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001457SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1458 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001459
1460 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1461 "Floating point operand expected.");
1462
1463 SDValue True = DAG.getConstant(1, MVT::i32);
1464 SDValue False = DAG.getConstant(0, MVT::i32);
1465
Andrew Trickac6d9be2013-05-25 02:42:55 +00001466 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001467}
1468
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001469SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001470 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001471 // FIXME there isn't actually debug info here
Andrew Trickac6d9be2013-05-25 02:42:55 +00001472 SDLoc DL(Op);
Jia Liubb481f82012-02-28 07:46:26 +00001473 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001474
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001475 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaafc945b2012-09-12 23:27:55 +00001476 const MipsTargetObjectFile &TLOF =
1477 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001478
Chris Lattnere3736f82009-08-13 05:41:27 +00001479 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001480 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001481 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001482 MipsII::MO_GPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001483 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001484 DAG.getVTList(MVT::i32), &GA, 1);
Akira Hatanakae7338cd2012-08-22 03:18:13 +00001485 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001486 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001487 }
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001488
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001489 // %hi/%lo relocation
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001490 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001491 }
1492
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001493 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
1494 return getAddrLocal(Op, DAG, HasMips64);
1495
Akira Hatanakaf09a0372012-11-21 20:40:38 +00001496 if (LargeGOT)
1497 return getAddrGlobalLargeGOT(Op, DAG, MipsII::MO_GOT_HI16,
1498 MipsII::MO_GOT_LO16);
1499
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001500 return getAddrGlobal(Op, DAG,
1501 HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001502}
1503
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001504SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001505 SelectionDAG &DAG) const {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001506 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1507 return getAddrNonPIC(Op, DAG);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001508
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001509 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001510}
1511
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001512SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001513lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001514{
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001515 // If the relocation model is PIC, use the General Dynamic TLS Model or
1516 // Local Dynamic TLS model, otherwise use the Initial Exec or
1517 // Local Exec TLS Model.
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001518
1519 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001520 SDLoc DL(GA);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001521 const GlobalValue *GV = GA->getGlobal();
1522 EVT PtrVT = getPointerTy();
1523
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001524 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1525
1526 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg70a07c72012-06-04 14:02:08 +00001527 // General Dynamic and Local Dynamic TLS Model.
1528 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1529 : MipsII::MO_TLSGD;
1530
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001531 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1532 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1533 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001534 unsigned PtrSize = PtrVT.getSizeInBits();
1535 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1536
Benjamin Kramer5eccf672011-12-11 12:21:34 +00001537 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001538
1539 ArgListTy Args;
1540 ArgListEntry Entry;
1541 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00001542 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001543 Args.push_back(Entry);
Jia Liubb481f82012-02-28 07:46:26 +00001544
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001545 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001546 false, false, false, false, 0, CallingConv::C,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001547 /*IsTailCall=*/false, /*doesNotRet=*/false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001548 /*isReturnValueUsed=*/true,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001549 TlsGetAddr, Args, DAG, DL);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001550 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001551
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001552 SDValue Ret = CallResult.first;
1553
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001554 if (model != TLSModel::LocalDynamic)
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001555 return Ret;
1556
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001557 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001558 MipsII::MO_DTPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001559 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1560 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001561 MipsII::MO_DTPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001562 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1563 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1564 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001565 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001566
1567 SDValue Offset;
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001568 if (model == TLSModel::InitialExec) {
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001569 // Initial Exec TLS Model
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001570 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001571 MipsII::MO_GOTTPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001572 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanaka648f00c2012-02-24 22:34:47 +00001573 TGA);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001574 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001575 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001576 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001577 } else {
1578 // Local Exec TLS Model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001579 assert(model == TLSModel::LocalExec);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001580 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001581 MipsII::MO_TPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001582 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001583 MipsII::MO_TPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001584 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1585 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1586 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001587 }
1588
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001589 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1590 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001591}
1592
1593SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001594lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001595{
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001596 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1597 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001598
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001599 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001600}
1601
Dan Gohman475871a2008-07-27 21:46:04 +00001602SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001603lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001604{
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001605 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001606 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001607 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001608 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001609 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001610 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001611 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1612 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001613 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001614
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001615 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1616 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001617
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001618 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001619}
1620
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001621SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001622 MachineFunction &MF = DAG.getMachineFunction();
1623 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1624
Andrew Trickac6d9be2013-05-25 02:42:55 +00001625 SDLoc DL(Op);
Dan Gohman1e93df62010-04-17 14:41:14 +00001626 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1627 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001628
1629 // vastart just stores the address of the VarArgsFrameIndex slot into the
1630 // memory location argument.
1631 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001632 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka82099682011-12-19 19:52:25 +00001633 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001634}
Jia Liubb481f82012-02-28 07:46:26 +00001635
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001636static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001637 EVT TyX = Op.getOperand(0).getValueType();
1638 EVT TyY = Op.getOperand(1).getValueType();
1639 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1640 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001641 SDLoc DL(Op);
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001642 SDValue Res;
1643
1644 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1645 // to i32.
1646 SDValue X = (TyX == MVT::f32) ?
1647 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1648 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1649 Const1);
1650 SDValue Y = (TyY == MVT::f32) ?
1651 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1652 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1653 Const1);
1654
1655 if (HasR2) {
1656 // ext E, Y, 31, 1 ; extract bit31 of Y
1657 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1658 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1659 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1660 } else {
1661 // sll SllX, X, 1
1662 // srl SrlX, SllX, 1
1663 // srl SrlY, Y, 31
1664 // sll SllY, SrlX, 31
1665 // or Or, SrlX, SllY
1666 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1667 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1668 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1669 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1670 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1671 }
1672
1673 if (TyX == MVT::f32)
1674 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1675
1676 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1677 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1678 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001679}
1680
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001681static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001682 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1683 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1684 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1685 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001686 SDLoc DL(Op);
Eric Christopher471e4222011-06-08 23:55:35 +00001687
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001688 // Bitcast to integer nodes.
1689 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1690 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001691
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001692 if (HasR2) {
1693 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1694 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1695 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1696 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001697
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001698 if (WidthX > WidthY)
1699 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1700 else if (WidthY > WidthX)
1701 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001702
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001703 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1704 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1705 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1706 }
1707
1708 // (d)sll SllX, X, 1
1709 // (d)srl SrlX, SllX, 1
1710 // (d)srl SrlY, Y, width(Y)-1
1711 // (d)sll SllY, SrlX, width(Y)-1
1712 // or Or, SrlX, SllY
1713 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1714 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1715 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1716 DAG.getConstant(WidthY - 1, MVT::i32));
1717
1718 if (WidthX > WidthY)
1719 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1720 else if (WidthY > WidthX)
1721 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1722
1723 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1724 DAG.getConstant(WidthX - 1, MVT::i32));
1725 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1726 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001727}
1728
Akira Hatanaka82099682011-12-19 19:52:25 +00001729SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001730MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001731 if (Subtarget->hasMips64())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001732 return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001733
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001734 return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001735}
1736
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001737static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001738 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001739 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001740
1741 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1742 // to i32.
1743 SDValue X = (Op.getValueType() == MVT::f32) ?
1744 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1745 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1746 Const1);
1747
1748 // Clear MSB.
1749 if (HasR2)
1750 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
1751 DAG.getRegister(Mips::ZERO, MVT::i32),
1752 DAG.getConstant(31, MVT::i32), Const1, X);
1753 else {
1754 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1755 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1756 }
1757
1758 if (Op.getValueType() == MVT::f32)
1759 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
1760
1761 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1762 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1763 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1764}
1765
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001766static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001767 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001768 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001769
1770 // Bitcast to integer node.
1771 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
1772
1773 // Clear MSB.
1774 if (HasR2)
1775 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
1776 DAG.getRegister(Mips::ZERO_64, MVT::i64),
1777 DAG.getConstant(63, MVT::i32), Const1, X);
1778 else {
1779 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
1780 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
1781 }
1782
1783 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
1784}
1785
1786SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001787MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001788 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001789 return lowerFABS64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001790
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001791 return lowerFABS32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001792}
1793
Akira Hatanaka2e591472011-06-02 00:24:44 +00001794SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001795lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001796 // check the depth
1797 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001798 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001799
1800 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1801 MFI->setFrameAddressIsTaken(true);
1802 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001803 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001804 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001805 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001806 return FrameAddr;
1807}
1808
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001809SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001810 SelectionDAG &DAG) const {
1811 // check the depth
1812 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1813 "Return address can be determined only for current frame.");
1814
1815 MachineFunction &MF = DAG.getMachineFunction();
1816 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001817 MVT VT = Op.getSimpleValueType();
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001818 unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
1819 MFI->setReturnAddressIsTaken(true);
1820
1821 // Return RA, which contains the return address. Mark it an implicit live-in.
1822 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001823 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001824}
1825
Akira Hatanaka544cc212013-01-30 00:26:49 +00001826// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1827// generated from __builtin_eh_return (offset, handler)
1828// The effect of this is to adjust the stack pointer by "offset"
1829// and then branch to "handler".
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001830SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanaka544cc212013-01-30 00:26:49 +00001831 const {
1832 MachineFunction &MF = DAG.getMachineFunction();
1833 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1834
1835 MipsFI->setCallsEhReturn();
1836 SDValue Chain = Op.getOperand(0);
1837 SDValue Offset = Op.getOperand(1);
1838 SDValue Handler = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001839 SDLoc DL(Op);
Akira Hatanaka544cc212013-01-30 00:26:49 +00001840 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
1841
1842 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1843 // EH_RETURN nodes, so that instructions are emitted back-to-back.
1844 unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1;
1845 unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0;
1846 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1847 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1848 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1849 DAG.getRegister(OffsetReg, Ty),
1850 DAG.getRegister(AddrReg, getPointerTy()),
1851 Chain.getValue(1));
1852}
1853
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001854SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001855 SelectionDAG &DAG) const {
Eli Friedman14648462011-07-27 22:21:52 +00001856 // FIXME: Need pseudo-fence for 'singlethread' fences
1857 // FIXME: Set SType for weaker fences where supported/appropriate.
1858 unsigned SType = 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001859 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001860 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00001861 DAG.getConstant(SType, MVT::i32));
1862}
1863
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001864SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001865 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001866 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001867 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1868 SDValue Shamt = Op.getOperand(2);
1869
1870 // if shamt < 32:
1871 // lo = (shl lo, shamt)
1872 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
1873 // else:
1874 // lo = 0
1875 // hi = (shl lo, shamt[4:0])
1876 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1877 DAG.getConstant(-1, MVT::i32));
1878 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
1879 DAG.getConstant(1, MVT::i32));
1880 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
1881 Not);
1882 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
1883 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1884 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
1885 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1886 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka864f6602012-06-14 21:10:56 +00001887 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1888 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001889 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
1890
1891 SDValue Ops[2] = {Lo, Hi};
1892 return DAG.getMergeValues(Ops, 2, DL);
1893}
1894
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001895SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001896 bool IsSRA) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001897 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001898 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1899 SDValue Shamt = Op.getOperand(2);
1900
1901 // if shamt < 32:
1902 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
1903 // if isSRA:
1904 // hi = (sra hi, shamt)
1905 // else:
1906 // hi = (srl hi, shamt)
1907 // else:
1908 // if isSRA:
1909 // lo = (sra hi, shamt[4:0])
1910 // hi = (sra hi, 31)
1911 // else:
1912 // lo = (srl hi, shamt[4:0])
1913 // hi = 0
1914 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1915 DAG.getConstant(-1, MVT::i32));
1916 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
1917 DAG.getConstant(1, MVT::i32));
1918 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
1919 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
1920 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1921 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
1922 Hi, Shamt);
1923 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1924 DAG.getConstant(0x20, MVT::i32));
1925 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
1926 DAG.getConstant(31, MVT::i32));
1927 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
1928 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1929 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
1930 ShiftRightHi);
1931
1932 SDValue Ops[2] = {Lo, Hi};
1933 return DAG.getMergeValues(Ops, 2, DL);
1934}
1935
Akira Hatanakafee62c12013-04-11 19:07:14 +00001936static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001937 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001938 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001939 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001940 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001941 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001942 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1943
1944 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001945 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001946 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001947
1948 SDValue Ops[] = { Chain, Ptr, Src };
1949 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
1950 LD->getMemOperand());
1951}
1952
1953// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001954SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001955 LoadSDNode *LD = cast<LoadSDNode>(Op);
1956 EVT MemVT = LD->getMemoryVT();
1957
1958 // Return if load is aligned or if MemVT is neither i32 nor i64.
1959 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
1960 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
1961 return SDValue();
1962
1963 bool IsLittle = Subtarget->isLittle();
1964 EVT VT = Op.getValueType();
1965 ISD::LoadExtType ExtType = LD->getExtensionType();
1966 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
1967
1968 assert((VT == MVT::i32) || (VT == MVT::i64));
1969
1970 // Expand
1971 // (set dst, (i64 (load baseptr)))
1972 // to
1973 // (set tmp, (ldl (add baseptr, 7), undef))
1974 // (set dst, (ldr baseptr, tmp))
1975 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00001976 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001977 IsLittle ? 7 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00001978 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001979 IsLittle ? 0 : 7);
1980 }
1981
Akira Hatanakafee62c12013-04-11 19:07:14 +00001982 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001983 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00001984 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001985 IsLittle ? 0 : 3);
1986
1987 // Expand
1988 // (set dst, (i32 (load baseptr))) or
1989 // (set dst, (i64 (sextload baseptr))) or
1990 // (set dst, (i64 (extload baseptr)))
1991 // to
1992 // (set tmp, (lwl (add baseptr, 3), undef))
1993 // (set dst, (lwr baseptr, tmp))
1994 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
1995 (ExtType == ISD::EXTLOAD))
1996 return LWR;
1997
1998 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
1999
2000 // Expand
2001 // (set dst, (i64 (zextload baseptr)))
2002 // to
2003 // (set tmp0, (lwl (add baseptr, 3), undef))
2004 // (set tmp1, (lwr baseptr, tmp0))
2005 // (set tmp2, (shl tmp1, 32))
2006 // (set dst, (srl tmp2, 32))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002007 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002008 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2009 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka94ccee22012-06-04 17:46:29 +00002010 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2011 SDValue Ops[] = { SRL, LWR.getValue(1) };
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002012 return DAG.getMergeValues(Ops, 2, DL);
2013}
2014
Akira Hatanakafee62c12013-04-11 19:07:14 +00002015static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002016 SDValue Chain, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002017 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2018 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002019 SDLoc DL(SD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002020 SDVTList VTList = DAG.getVTList(MVT::Other);
2021
2022 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002023 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002024 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002025
2026 SDValue Ops[] = { Chain, Value, Ptr };
2027 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2028 SD->getMemOperand());
2029}
2030
2031// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanaka63451432013-05-16 20:45:17 +00002032static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2033 bool IsLittle) {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002034 SDValue Value = SD->getValue(), Chain = SD->getChain();
2035 EVT VT = Value.getValueType();
2036
2037 // Expand
2038 // (store val, baseptr) or
2039 // (truncstore val, baseptr)
2040 // to
2041 // (swl val, (add baseptr, 3))
2042 // (swr val, baseptr)
2043 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00002044 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002045 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00002046 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002047 }
2048
2049 assert(VT == MVT::i64);
2050
2051 // Expand
2052 // (store val, baseptr)
2053 // to
2054 // (sdl val, (add baseptr, 7))
2055 // (sdr val, baseptr)
Akira Hatanakafee62c12013-04-11 19:07:14 +00002056 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2057 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002058}
2059
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002060// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2061static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2062 SDValue Val = SD->getValue();
2063
2064 if (Val.getOpcode() != ISD::FP_TO_SINT)
2065 return SDValue();
2066
2067 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002068 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002069 Val.getOperand(0));
2070
Andrew Trickac6d9be2013-05-25 02:42:55 +00002071 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002072 SD->getPointerInfo(), SD->isVolatile(),
2073 SD->isNonTemporal(), SD->getAlignment());
2074}
2075
Akira Hatanaka63451432013-05-16 20:45:17 +00002076SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2077 StoreSDNode *SD = cast<StoreSDNode>(Op);
2078 EVT MemVT = SD->getMemoryVT();
2079
2080 // Lower unaligned integer stores.
2081 if ((SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2082 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2083 return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle());
2084
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002085 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanaka63451432013-05-16 20:45:17 +00002086}
2087
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002088SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002089 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2090 || cast<ConstantSDNode>
2091 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2092 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2093 return SDValue();
2094
2095 // The pattern
2096 // (add (frameaddr 0), (frame_to_args_offset))
2097 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2098 // (add FrameObject, 0)
2099 // where FrameObject is a fixed StackObject with offset 0 which points to
2100 // the old stack pointer.
2101 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2102 EVT ValTy = Op->getValueType(0);
2103 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2104 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002105 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002106 DAG.getConstant(0, ValTy));
2107}
2108
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002109SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2110 SelectionDAG &DAG) const {
2111 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002112 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002113 Op.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002114 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002115}
2116
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002117//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002118// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002119//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002120
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002121//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002122// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002123// Mips O32 ABI rules:
2124// ---
2125// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002126// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002127// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002128// f64 - Only passed in two aliased f32 registers if no int reg has been used
2129// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002130// not used, it must be shadowed. If only A3 is avaiable, shadow it and
2131// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002132//
2133// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002134//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002135
Duncan Sands1e96bab2010-11-04 10:49:57 +00002136static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00002137 MVT LocVT, CCValAssign::LocInfo LocInfo,
Akira Hatanakaad341d42013-08-20 23:38:40 +00002138 ISD::ArgFlagsTy ArgFlags, CCState &State,
2139 const uint16_t *F64Regs) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002140
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002141 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002142
Craig Topperc5eaae42012-03-11 07:57:25 +00002143 static const uint16_t IntRegs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002144 Mips::A0, Mips::A1, Mips::A2, Mips::A3
2145 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002146 static const uint16_t F32Regs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002147 Mips::F12, Mips::F14
2148 };
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002149
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002150 // Do not process byval args here.
2151 if (ArgFlags.isByVal())
2152 return true;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002153
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002154 // Promote i8 and i16
2155 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2156 LocVT = MVT::i32;
2157 if (ArgFlags.isSExt())
2158 LocInfo = CCValAssign::SExt;
2159 else if (ArgFlags.isZExt())
2160 LocInfo = CCValAssign::ZExt;
2161 else
2162 LocInfo = CCValAssign::AExt;
2163 }
2164
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002165 unsigned Reg;
2166
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002167 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2168 // is true: function is vararg, argument is 3rd or higher, there is previous
2169 // argument which is not f32 or f64.
2170 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2171 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002172 unsigned OrigAlign = ArgFlags.getOrigAlign();
2173 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002174
2175 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002176 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002177 // If this is the first part of an i64 arg,
2178 // the allocated register must be either A0 or A2.
2179 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2180 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002181 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002182 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2183 // Allocate int register and shadow next int register. If first
2184 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002185 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2186 if (Reg == Mips::A1 || Reg == Mips::A3)
2187 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2188 State.AllocateReg(IntRegs, IntRegsSize);
2189 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002190 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2191 // we are guaranteed to find an available float register
2192 if (ValVT == MVT::f32) {
2193 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2194 // Shadow int register
2195 State.AllocateReg(IntRegs, IntRegsSize);
2196 } else {
2197 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2198 // Shadow int registers
2199 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2200 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2201 State.AllocateReg(IntRegs, IntRegsSize);
2202 State.AllocateReg(IntRegs, IntRegsSize);
2203 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002204 } else
2205 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002206
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002207 if (!Reg) {
2208 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2209 OrigAlign);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002210 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002211 } else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002212 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002213
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002214 return false;
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002215}
2216
Akira Hatanakaad341d42013-08-20 23:38:40 +00002217static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2218 MVT LocVT, CCValAssign::LocInfo LocInfo,
2219 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2220 static const uint16_t F64Regs[] = { Mips::D6, Mips::D7 };
2221
2222 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2223}
2224
2225static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2226 MVT LocVT, CCValAssign::LocInfo LocInfo,
2227 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2228 static const uint16_t F64Regs[] = { Mips::D12_64, Mips::D12_64 };
2229
2230 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2231}
2232
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002233#include "MipsGenCallingConv.inc"
2234
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002235//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002236// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002237//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002238
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002239// Return next O32 integer argument register.
2240static unsigned getNextIntArgReg(unsigned Reg) {
2241 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2242 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2243}
2244
Akira Hatanaka7d712092012-10-30 19:23:25 +00002245SDValue
2246MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002247 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka7d712092012-10-30 19:23:25 +00002248 bool IsTailCall, SelectionDAG &DAG) const {
2249 if (!IsTailCall) {
2250 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2251 DAG.getIntPtrConstant(Offset));
2252 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2253 false, 0);
2254 }
2255
2256 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2257 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2258 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2259 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2260 /*isVolatile=*/ true, false, 0);
2261}
2262
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002263void MipsTargetLowering::
2264getOpndList(SmallVectorImpl<SDValue> &Ops,
2265 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2266 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2267 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
2268 // Insert node "GP copy globalreg" before call to function.
2269 //
2270 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2271 // in PIC mode) allow symbols to be resolved via lazy binding.
2272 // The lazy binding stub requires GP to point to the GOT.
2273 if (IsPICCall && !InternalLinkage) {
2274 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
2275 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2276 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2277 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002278
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002279 // Build a sequence of copy-to-reg nodes chained together with token
2280 // chain and flag operands which copy the outgoing args into registers.
2281 // The InFlag in necessary since all emitted instructions must be
2282 // stuck together.
2283 SDValue InFlag;
Reed Kotler8453b3f2013-01-24 04:24:02 +00002284
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002285 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2286 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2287 RegsToPass[i].second, InFlag);
2288 InFlag = Chain.getValue(1);
2289 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002290
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002291 // Add argument registers to the end of the list so that they are
2292 // known live into the call.
2293 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2294 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2295 RegsToPass[i].second.getValueType()));
Reed Kotler8453b3f2013-01-24 04:24:02 +00002296
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002297 // Add a register mask operand representing the call-preserved registers.
2298 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2299 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2300 assert(Mask && "Missing call preserved mask for calling convention");
Reed Kotler46090912013-05-10 22:25:39 +00002301 if (Subtarget->inMips16HardFloat()) {
2302 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2303 llvm::StringRef Sym = G->getGlobal()->getName();
2304 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
2305 if (F->hasFnAttribute("__Mips16RetHelper")) {
2306 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2307 }
2308 }
2309 }
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002310 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2311
2312 if (InFlag.getNode())
2313 Ops.push_back(InFlag);
Reed Kotler8453b3f2013-01-24 04:24:02 +00002314}
2315
Dan Gohman98ca4f22009-08-05 01:29:28 +00002316/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002317/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002318SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002319MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002320 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002321 SelectionDAG &DAG = CLI.DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002322 SDLoc DL = CLI.DL;
Craig Toppera0ec3f92013-07-14 04:42:23 +00002323 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2324 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2325 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002326 SDValue Chain = CLI.Chain;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002327 SDValue Callee = CLI.Callee;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002328 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002329 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002330 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002331
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002332 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002333 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002334 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002335 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002336
2337 // Analyze operands of the call, assigning locations to each operand.
2338 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002339 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002340 getTargetMachine(), ArgLocs, *DAG.getContext());
Reed Kotler46090912013-05-10 22:25:39 +00002341 MipsCC::SpecialCallingConvType SpecialCallingConv =
2342 getSpecialCallingConv(Callee);
Akira Hatanakaad341d42013-08-20 23:38:40 +00002343 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo,
2344 SpecialCallingConv);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002345
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002346 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002347 Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002348 Callee.getNode(), CLI.Args);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002349
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002350 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002351 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002352
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002353 // Check if it's really possible to do a tail call.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002354 if (IsTailCall)
2355 IsTailCall =
2356 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002357 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002358
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002359 if (IsTailCall)
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002360 ++NumTailCalls;
2361
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002362 // Chain is the output chain of the last Load/Store or CopyToReg node.
2363 // ByValChain is the output chain of the last Memcpy node created for copying
2364 // byval arguments to the stack.
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002365 unsigned StackAlignment = TFL->getStackAlignment();
2366 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002367 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002368
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002369 if (!IsTailCall)
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002370 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002371
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002372 SDValue StackPtr = DAG.getCopyFromReg(Chain, DL,
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002373 IsN64 ? Mips::SP_64 : Mips::SP,
2374 getPointerTy());
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002375
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002376 // With EABI is it possible to have 16 args on registers.
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002377 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman475871a2008-07-27 21:46:04 +00002378 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002379 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002380
2381 // Walk the register/memloc assignments, inserting copies/loads.
2382 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002383 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002384 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002385 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002386 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2387
2388 // ByVal Arg.
2389 if (Flags.isByVal()) {
2390 assert(Flags.getByValSize() &&
2391 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002392 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002393 assert(!IsTailCall &&
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002394 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002395 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002396 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
2397 ++ByValArg;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002398 continue;
2399 }
Jia Liubb481f82012-02-28 07:46:26 +00002400
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002401 // Promote the value if needed.
2402 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002403 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002404 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002405 if (VA.isRegLoc()) {
2406 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002407 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2408 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002409 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002410 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002411 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002412 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002413 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002414 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002415 if (!Subtarget->isLittle())
2416 std::swap(Lo, Hi);
Jia Liubb481f82012-02-28 07:46:26 +00002417 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002418 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2419 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2420 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002421 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002422 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002423 }
2424 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002425 case CCValAssign::SExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002426 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002427 break;
2428 case CCValAssign::ZExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002429 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002430 break;
2431 case CCValAssign::AExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002432 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002433 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002434 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002435
2436 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002437 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002438 if (VA.isRegLoc()) {
2439 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002440 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002441 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002442
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002443 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002444 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002445
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002446 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002447 // parameter value to a stack Location
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002448 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002449 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002450 }
2451
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002452 // Transform all store nodes into one single node because all store
2453 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002454 if (!MemOpChains.empty())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002455 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002456 &MemOpChains[0], MemOpChains.size());
2457
Bill Wendling056292f2008-09-16 21:48:12 +00002458 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002459 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2460 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002461 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakaed185da2012-12-13 03:17:29 +00002462 bool GlobalOrExternal = false, InternalLinkage = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002463 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002464
2465 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002466 if (IsPICCall) {
Akira Hatanakaed185da2012-12-13 03:17:29 +00002467 InternalLinkage = G->getGlobal()->hasInternalLinkage();
2468
2469 if (InternalLinkage)
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002470 Callee = getAddrLocal(Callee, DAG, HasMips64);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002471 else if (LargeGOT)
2472 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
2473 MipsII::MO_CALL_LO16);
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002474 else
2475 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
2476 } else
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002477 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002478 MipsII::MO_NO_FLAG);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002479 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002480 }
2481 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002482 if (!IsN64 && !IsPIC) // !N64 && static
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002483 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2484 MipsII::MO_NO_FLAG);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002485 else if (LargeGOT)
2486 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
2487 MipsII::MO_CALL_LO16);
Akira Hatanaka60689322013-02-22 21:10:03 +00002488 else // N64 || PIC
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002489 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
2490
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002491 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002492 }
2493
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002494 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002495 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002496
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002497 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2498 CLI, Callee, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002499
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002500 if (IsTailCall)
2501 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, &Ops[0], Ops.size());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002502
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002503 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, &Ops[0], Ops.size());
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002504 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002505
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002506 // Create the CALLSEQ_END node.
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002507 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002508 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002509 InFlag = Chain.getValue(1);
2510
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002511 // Handle result values, copying them out of physregs into vregs that we
2512 // return.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002513 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
2514 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002515}
2516
Dan Gohman98ca4f22009-08-05 01:29:28 +00002517/// LowerCallResult - Lower the result values of a call into the
2518/// appropriate copies out of appropriate physical registers.
2519SDValue
2520MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002521 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002522 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002523 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002524 SmallVectorImpl<SDValue> &InVals,
2525 const SDNode *CallNode,
2526 const Type *RetTy) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002527 // Assign locations to each value returned by this call.
2528 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002529 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka864f6602012-06-14 21:10:56 +00002530 getTargetMachine(), RVLocs, *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002531 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002532
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002533 MipsCCInfo.analyzeCallResult(Ins, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002534 CallNode, RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002535
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002536 // Copy all of the result registers out of their specified physreg.
2537 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002538 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002539 RVLocs[i].getLocVT(), InFlag);
2540 Chain = Val.getValue(1);
2541 InFlag = Val.getValue(2);
2542
2543 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002544 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002545
2546 InVals.push_back(Val);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002547 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002548
Dan Gohman98ca4f22009-08-05 01:29:28 +00002549 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002550}
2551
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002552//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002553// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002554//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002555/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002556/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002557SDValue
2558MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002559 CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002560 bool IsVarArg,
Akira Hatanaka82099682011-12-19 19:52:25 +00002561 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002562 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002563 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002564 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002565 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002566 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002567 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002568
Dan Gohman1e93df62010-04-17 14:41:14 +00002569 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002570
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002571 // Used with vargs to acumulate store chains.
2572 std::vector<SDValue> OutChains;
2573
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002574 // Assign locations to all of the incoming arguments.
2575 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002576 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002577 getTargetMachine(), ArgLocs, *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002578 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002579 Function::const_arg_iterator FuncArg =
2580 DAG.getMachineFunction().getFunction()->arg_begin();
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002581 bool UseSoftFloat = Subtarget->mipsSEUsesSoftFloat();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002582
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002583 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
Akira Hatanakab33b34a2012-10-30 19:37:25 +00002584 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2585 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002586
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002587 unsigned CurArgIdx = 0;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002588 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002589
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002590 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002591 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002592 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2593 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002594 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002595 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2596 bool IsRegLoc = VA.isRegLoc();
2597
2598 if (Flags.isByVal()) {
2599 assert(Flags.getByValSize() &&
2600 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002601 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002602 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002603 MipsCCInfo, *ByValArg);
2604 ++ByValArg;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002605 continue;
2606 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002607
2608 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002609 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002610 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002611 unsigned ArgReg = VA.getLocReg();
Craig Topper44d23822012-02-22 05:59:10 +00002612 const TargetRegisterClass *RC;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002613
Owen Anderson825b72b2009-08-11 20:47:22 +00002614 if (RegVT == MVT::i32)
Reed Kotlerbacbf1c2012-12-20 06:06:35 +00002615 RC = Subtarget->inMips16Mode()? &Mips::CPU16RegsRegClass :
Akira Hatanaka18587862013-08-06 23:08:38 +00002616 &Mips::GPR32RegClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002617 else if (RegVT == MVT::i64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002618 RC = &Mips::GPR64RegClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002619 else if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002620 RC = &Mips::FGR32RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002621 else if (RegVT == MVT::f64)
Akira Hatanakaad341d42013-08-20 23:38:40 +00002622 RC = Subtarget->isFP64bit() ? &Mips::FGR64RegClass :
2623 &Mips::AFGR64RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002624 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002625 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002626
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002627 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002628 // physical registers into virtual ones
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002629 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2630 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002631
2632 // If this is an 8 or 16-bit value, it has been passed promoted
2633 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002634 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002635 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002636 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002637 if (VA.getLocInfo() == CCValAssign::SExt)
2638 Opcode = ISD::AssertSext;
2639 else if (VA.getLocInfo() == CCValAssign::ZExt)
2640 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002641 if (Opcode)
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002642 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002643 DAG.getValueType(ValVT));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002644 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002645 }
2646
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002647 // Handle floating point arguments passed in integer registers and
2648 // long double arguments passed in floating point registers.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002649 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002650 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2651 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002652 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002653 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002654 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002655 getNextIntArgReg(ArgReg), RC);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002656 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002657 if (!Subtarget->isLittle())
2658 std::swap(ArgValue, ArgValue2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002659 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002660 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002661 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002662
Dan Gohman98ca4f22009-08-05 01:29:28 +00002663 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002664 } else { // VA.isRegLoc()
2665
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002666 // sanity check
2667 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002668
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002669 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002670 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002671 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002672
2673 // Create load nodes to retrieve arguments from the stack
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002674 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002675 InVals.push_back(DAG.getLoad(ValVT, DL, Chain, FIN,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002676 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002677 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002678 }
2679 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002680
2681 // The mips ABIs for returning structs by value requires that we copy
2682 // the sret argument into $v0 for the return. Save the argument into
2683 // a virtual register so that we can access it from the return points.
2684 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2685 unsigned Reg = MipsFI->getSRetReturnReg();
2686 if (!Reg) {
Akira Hatanaka30580ce2012-10-19 22:11:40 +00002687 Reg = MF.getRegInfo().
2688 createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002689 MipsFI->setSRetReturnReg(Reg);
2690 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002691 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);
2692 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002693 }
2694
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002695 if (IsVarArg)
2696 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002697
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002698 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002699 // the size of Ins and InVals. This only happens when on varg functions
2700 if (!OutChains.empty()) {
2701 OutChains.push_back(Chain);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002702 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002703 &OutChains[0], OutChains.size());
2704 }
2705
Dan Gohman98ca4f22009-08-05 01:29:28 +00002706 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002707}
2708
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002709//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002710// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002711//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002712
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002713bool
2714MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002715 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002716 const SmallVectorImpl<ISD::OutputArg> &Outs,
2717 LLVMContext &Context) const {
2718 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002719 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002720 RVLocs, Context);
2721 return CCInfo.CheckReturn(Outs, RetCC_Mips);
2722}
2723
Dan Gohman98ca4f22009-08-05 01:29:28 +00002724SDValue
2725MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002726 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002727 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002728 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002729 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002730 // CCValAssign - represent the assignment of
2731 // the return value to a location
2732 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002733 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002734
2735 // CCState - Info about the registers and stack slot.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002736 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002737 *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002738 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002739
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002740 // Analyze return values.
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002741 MipsCCInfo.analyzeReturn(Outs, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002742 MF.getFunction()->getReturnType());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002743
Dan Gohman475871a2008-07-27 21:46:04 +00002744 SDValue Flag;
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002745 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002746
2747 // Copy the result values into the output registers.
2748 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002749 SDValue Val = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002750 CCValAssign &VA = RVLocs[i];
2751 assert(VA.isRegLoc() && "Can only return in registers!");
2752
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002753 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002754 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002755
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002756 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002757
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002758 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002759 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002760 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002761 }
2762
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002763 // The mips ABIs for returning structs by value requires that we copy
2764 // the sret argument into $v0 for the return. We saved the argument into
2765 // a virtual register in the entry block, so now we copy the value out
2766 // and into $v0.
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002767 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002768 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2769 unsigned Reg = MipsFI->getSRetReturnReg();
2770
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002771 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002772 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002773 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Akira Hatanaka2ef5bd32012-10-24 02:10:54 +00002774 unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002775
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002776 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002777 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002778 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002779 }
2780
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002781 RetOps[0] = Chain; // Update chain.
Akira Hatanaka182ef6f2012-07-10 00:19:06 +00002782
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002783 // Add the flag if we have it.
2784 if (Flag.getNode())
2785 RetOps.push_back(Flag);
2786
2787 // Return on Mips is always a "jr $ra"
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002788 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, &RetOps[0], RetOps.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002789}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002790
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002791//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002792// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002793//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002794
2795/// getConstraintType - Given a constraint letter, return the type of
2796/// constraint it is for this target.
2797MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002798getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002799{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002800 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002801 // GCC config/mips/constraints.md
2802 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002803 // 'd' : An address register. Equivalent to r
2804 // unless generating MIPS16 code.
2805 // 'y' : Equivalent to r; retained for
2806 // backwards compatibility.
Eric Christopher1d5a3922012-05-07 06:25:10 +00002807 // 'c' : A register suitable for use in an indirect
2808 // jump. This will always be $25 for -mabicalls.
Eric Christopheraf97f732012-05-07 06:25:19 +00002809 // 'l' : The lo register. 1 word storage.
2810 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002811 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002812 switch (Constraint[0]) {
2813 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002814 case 'd':
2815 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002816 case 'f':
Eric Christopher1d5a3922012-05-07 06:25:10 +00002817 case 'c':
Eric Christopher4adbefe2012-05-07 06:25:15 +00002818 case 'l':
Eric Christopheraf97f732012-05-07 06:25:19 +00002819 case 'x':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002820 return C_RegisterClass;
Jack Carter0b9675d2013-03-04 21:33:15 +00002821 case 'R':
2822 return C_Memory;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002823 }
2824 }
2825 return TargetLowering::getConstraintType(Constraint);
2826}
2827
John Thompson44ab89e2010-10-29 17:29:13 +00002828/// Examine constraint type and operand type and determine a weight value.
2829/// This object must already have been set up with the operand type
2830/// and the current alternative constraint selected.
2831TargetLowering::ConstraintWeight
2832MipsTargetLowering::getSingleConstraintMatchWeight(
2833 AsmOperandInfo &info, const char *constraint) const {
2834 ConstraintWeight weight = CW_Invalid;
2835 Value *CallOperandVal = info.CallOperandVal;
2836 // If we don't have a value, we can't do a match,
2837 // but allow it at the lowest weight.
2838 if (CallOperandVal == NULL)
2839 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002840 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002841 // Look at the constraint type.
2842 switch (*constraint) {
2843 default:
2844 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2845 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002846 case 'd':
2847 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002848 if (type->isIntegerTy())
2849 weight = CW_Register;
2850 break;
2851 case 'f':
2852 if (type->isFloatTy())
2853 weight = CW_Register;
2854 break;
Eric Christopher1d5a3922012-05-07 06:25:10 +00002855 case 'c': // $25 for indirect jumps
Eric Christopher4adbefe2012-05-07 06:25:15 +00002856 case 'l': // lo register
Eric Christopheraf97f732012-05-07 06:25:19 +00002857 case 'x': // hilo register pair
Eric Christopher1d5a3922012-05-07 06:25:10 +00002858 if (type->isIntegerTy())
2859 weight = CW_SpecificReg;
2860 break;
Eric Christopher50ab0392012-05-07 03:13:32 +00002861 case 'I': // signed 16 bit immediate
Eric Christophere5076d42012-05-07 03:13:42 +00002862 case 'J': // integer zero
Eric Christopherf49f8462012-05-07 05:46:29 +00002863 case 'K': // unsigned 16 bit immediate
Eric Christopher5ac47bb2012-05-07 05:46:37 +00002864 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christopher60cfc792012-05-07 05:46:43 +00002865 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher1ce20342012-05-07 05:46:48 +00002866 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopher54412a72012-05-07 06:25:02 +00002867 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher50ab0392012-05-07 03:13:32 +00002868 if (isa<ConstantInt>(CallOperandVal))
2869 weight = CW_Constant;
2870 break;
Jack Carter0b9675d2013-03-04 21:33:15 +00002871 case 'R':
2872 weight = CW_Memory;
2873 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002874 }
2875 return weight;
2876}
2877
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002878/// This is a helper function to parse a physical register string and split it
2879/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
2880/// that is returned indicates whether parsing was successful. The second flag
2881/// is true if the numeric part exists.
2882static std::pair<bool, bool>
2883parsePhysicalReg(const StringRef &C, std::string &Prefix,
2884 unsigned long long &Reg) {
2885 if (C.front() != '{' || C.back() != '}')
2886 return std::make_pair(false, false);
2887
2888 // Search for the first numeric character.
2889 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
2890 I = std::find_if(B, E, std::ptr_fun(isdigit));
2891
2892 Prefix.assign(B, I - B);
2893
2894 // The second flag is set to false if no numeric characters were found.
2895 if (I == E)
2896 return std::make_pair(true, false);
2897
2898 // Parse the numeric characters.
2899 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
2900 true);
2901}
2902
2903std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
2904parseRegForInlineAsmConstraint(const StringRef &C, MVT VT) const {
2905 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2906 const TargetRegisterClass *RC;
2907 std::string Prefix;
2908 unsigned long long Reg;
2909
2910 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
2911
2912 if (!R.first)
2913 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2914
2915 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
2916 // No numeric characters follow "hi" or "lo".
2917 if (R.second)
2918 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2919
2920 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002921 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002922 return std::make_pair(*(RC->begin()), RC);
2923 }
2924
2925 if (!R.second)
2926 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2927
2928 if (Prefix == "$f") { // Parse $f0-$f31.
2929 // If the size of FP registers is 64-bit or Reg is an even number, select
2930 // the 64-bit register class. Otherwise, select the 32-bit register class.
2931 if (VT == MVT::Other)
2932 VT = (Subtarget->isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
2933
2934 RC= getRegClassFor(VT);
2935
2936 if (RC == &Mips::AFGR64RegClass) {
2937 assert(Reg % 2 == 0);
2938 Reg >>= 1;
2939 }
2940 } else if (Prefix == "$fcc") { // Parse $fcc0-$fcc7.
2941 RC = TRI->getRegClass(Mips::FCCRegClassID);
2942 } else { // Parse $0-$31.
2943 assert(Prefix == "$");
2944 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
2945 }
2946
2947 assert(Reg < RC->getNumRegs());
2948 return std::make_pair(*(RC->begin() + Reg), RC);
2949}
2950
Eric Christopher38d64262011-06-29 19:33:04 +00002951/// Given a register class constraint, like 'r', if this corresponds directly
2952/// to an LLVM register class, return a register of 0 and the register class
2953/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002954std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier5b3fca52013-06-22 18:37:38 +00002955getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002956{
2957 if (Constraint.size() == 1) {
2958 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002959 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2960 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002961 case 'r':
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002962 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
2963 if (Subtarget->inMips16Mode())
2964 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka18587862013-08-06 23:08:38 +00002965 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002966 }
Jack Carter10de0252012-07-02 23:35:23 +00002967 if (VT == MVT::i64 && !HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002968 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002969 if (VT == MVT::i64 && HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002970 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002971 // This will generate an error message
2972 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002973 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002974 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002975 return std::make_pair(0U, &Mips::FGR32RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002976 if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
2977 if (Subtarget->isFP64bit())
Craig Topper420761a2012-04-20 07:30:17 +00002978 return std::make_pair(0U, &Mips::FGR64RegClass);
2979 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002980 }
Eric Christopher1d5a3922012-05-07 06:25:10 +00002981 break;
2982 case 'c': // register suitable for indirect jump
2983 if (VT == MVT::i32)
Akira Hatanaka18587862013-08-06 23:08:38 +00002984 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christopher1d5a3922012-05-07 06:25:10 +00002985 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka18587862013-08-06 23:08:38 +00002986 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher4adbefe2012-05-07 06:25:15 +00002987 case 'l': // register suitable for indirect jump
2988 if (VT == MVT::i32)
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002989 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
2990 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopheraf97f732012-05-07 06:25:19 +00002991 case 'x': // register suitable for indirect jump
2992 // Fixme: Not triggering the use of both hi and low
2993 // This will generate an error message
2994 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002995 }
2996 }
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002997
2998 std::pair<unsigned, const TargetRegisterClass *> R;
2999 R = parseRegForInlineAsmConstraint(Constraint, VT);
3000
3001 if (R.second)
3002 return R;
3003
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003004 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3005}
3006
Eric Christopher50ab0392012-05-07 03:13:32 +00003007/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3008/// vector. If it is invalid, don't add anything to Ops.
3009void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3010 std::string &Constraint,
3011 std::vector<SDValue>&Ops,
3012 SelectionDAG &DAG) const {
3013 SDValue Result(0, 0);
3014
3015 // Only support length 1 constraints for now.
3016 if (Constraint.length() > 1) return;
3017
3018 char ConstraintLetter = Constraint[0];
3019 switch (ConstraintLetter) {
3020 default: break; // This will fall through to the generic implementation
3021 case 'I': // Signed 16 bit constant
3022 // If this fails, the parent routine will give an error
3023 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3024 EVT Type = Op.getValueType();
3025 int64_t Val = C->getSExtValue();
3026 if (isInt<16>(Val)) {
3027 Result = DAG.getTargetConstant(Val, Type);
3028 break;
3029 }
3030 }
3031 return;
Eric Christophere5076d42012-05-07 03:13:42 +00003032 case 'J': // integer zero
3033 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3034 EVT Type = Op.getValueType();
3035 int64_t Val = C->getZExtValue();
3036 if (Val == 0) {
3037 Result = DAG.getTargetConstant(0, Type);
3038 break;
3039 }
3040 }
3041 return;
Eric Christopherf49f8462012-05-07 05:46:29 +00003042 case 'K': // unsigned 16 bit immediate
3043 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3044 EVT Type = Op.getValueType();
3045 uint64_t Val = (uint64_t)C->getZExtValue();
3046 if (isUInt<16>(Val)) {
3047 Result = DAG.getTargetConstant(Val, Type);
3048 break;
3049 }
3050 }
3051 return;
Eric Christopher5ac47bb2012-05-07 05:46:37 +00003052 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3053 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3054 EVT Type = Op.getValueType();
3055 int64_t Val = C->getSExtValue();
3056 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3057 Result = DAG.getTargetConstant(Val, Type);
3058 break;
3059 }
3060 }
3061 return;
Eric Christopher60cfc792012-05-07 05:46:43 +00003062 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3063 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3064 EVT Type = Op.getValueType();
3065 int64_t Val = C->getSExtValue();
3066 if ((Val >= -65535) && (Val <= -1)) {
3067 Result = DAG.getTargetConstant(Val, Type);
3068 break;
3069 }
3070 }
3071 return;
Eric Christopher1ce20342012-05-07 05:46:48 +00003072 case 'O': // signed 15 bit immediate
3073 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3074 EVT Type = Op.getValueType();
3075 int64_t Val = C->getSExtValue();
3076 if ((isInt<15>(Val))) {
3077 Result = DAG.getTargetConstant(Val, Type);
3078 break;
3079 }
3080 }
3081 return;
Eric Christopher54412a72012-05-07 06:25:02 +00003082 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3083 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3084 EVT Type = Op.getValueType();
3085 int64_t Val = C->getSExtValue();
3086 if ((Val <= 65535) && (Val >= 1)) {
3087 Result = DAG.getTargetConstant(Val, Type);
3088 break;
3089 }
3090 }
3091 return;
Eric Christopher50ab0392012-05-07 03:13:32 +00003092 }
3093
3094 if (Result.getNode()) {
3095 Ops.push_back(Result);
3096 return;
3097 }
3098
3099 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3100}
3101
Dan Gohman6520e202008-10-18 02:06:02 +00003102bool
Akira Hatanaka94e47282012-11-17 00:25:41 +00003103MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM, Type *Ty) const {
3104 // No global is ever allowed as a base.
3105 if (AM.BaseGV)
3106 return false;
3107
3108 switch (AM.Scale) {
3109 case 0: // "r+i" or just "i", depending on HasBaseReg.
3110 break;
3111 case 1:
3112 if (!AM.HasBaseReg) // allow "r+i".
3113 break;
3114 return false; // disallow "r+r" or "r+r+i".
3115 default:
3116 return false;
3117 }
3118
3119 return true;
3120}
3121
3122bool
Dan Gohman6520e202008-10-18 02:06:02 +00003123MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3124 // The Mips target isn't yet aware of offsets.
3125 return false;
3126}
Evan Chengeb2f9692009-10-27 19:56:55 +00003127
Akira Hatanakae193b322012-06-13 19:33:32 +00003128EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003129 unsigned SrcAlign,
3130 bool IsMemset, bool ZeroMemset,
Akira Hatanakae193b322012-06-13 19:33:32 +00003131 bool MemcpyStrSrc,
3132 MachineFunction &MF) const {
3133 if (Subtarget->hasMips64())
3134 return MVT::i64;
3135
3136 return MVT::i32;
3137}
3138
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003139bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3140 if (VT != MVT::f32 && VT != MVT::f64)
3141 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00003142 if (Imm.isNegZero())
3143 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00003144 return Imm.isZero();
3145}
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003146
3147unsigned MipsTargetLowering::getJumpTableEncoding() const {
3148 if (IsN64)
3149 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liubb481f82012-02-28 07:46:26 +00003150
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003151 return TargetLowering::getJumpTableEncoding();
3152}
Akira Hatanaka7887c902012-10-26 23:56:38 +00003153
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003154/// This function returns true if CallSym is a long double emulation routine.
3155static bool isF128SoftLibCall(const char *CallSym) {
3156 const char *const LibCalls[] =
3157 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3158 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3159 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3160 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3161 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3162 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3163 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3164 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3165 "truncl"};
3166
3167 const char * const *End = LibCalls + array_lengthof(LibCalls);
3168
3169 // Check that LibCalls is sorted alphabetically.
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003170 MipsTargetLowering::LTStr Comp;
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003171
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003172#ifndef NDEBUG
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003173 for (const char * const *I = LibCalls; I < End - 1; ++I)
3174 assert(Comp(*I, *(I + 1)));
3175#endif
3176
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003177 return std::binary_search(LibCalls, End, CallSym, Comp);
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003178}
3179
3180/// This function returns true if Ty is fp128 or i128 which was originally a
3181/// fp128.
3182static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3183 if (Ty->isFP128Ty())
3184 return true;
3185
3186 const ExternalSymbolSDNode *ES =
3187 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3188
3189 // If the Ty is i128 and the function being called is a long double emulation
3190 // routine, then the original type is f128.
3191 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3192}
3193
Reed Kotler46090912013-05-10 22:25:39 +00003194MipsTargetLowering::MipsCC::SpecialCallingConvType
3195 MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
3196 MipsCC::SpecialCallingConvType SpecialCallingConv =
3197 MipsCC::NoSpecialCallingConv;;
3198 if (Subtarget->inMips16HardFloat()) {
3199 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3200 llvm::StringRef Sym = G->getGlobal()->getName();
3201 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
3202 if (F->hasFnAttribute("__Mips16RetHelper")) {
3203 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3204 }
3205 }
3206 }
3207 return SpecialCallingConv;
3208}
3209
3210MipsTargetLowering::MipsCC::MipsCC(
Akira Hatanakaad341d42013-08-20 23:38:40 +00003211 CallingConv::ID CC, bool IsO32_, bool IsFP64_, CCState &Info,
Reed Kotler46090912013-05-10 22:25:39 +00003212 MipsCC::SpecialCallingConvType SpecialCallingConv_)
Akira Hatanakaad341d42013-08-20 23:38:40 +00003213 : CCInfo(Info), CallConv(CC), IsO32(IsO32_), IsFP64(IsFP64_),
Reed Kotler46090912013-05-10 22:25:39 +00003214 SpecialCallingConv(SpecialCallingConv_){
Akira Hatanaka7887c902012-10-26 23:56:38 +00003215 // Pre-allocate reserved argument area.
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003216 CCInfo.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka7887c902012-10-26 23:56:38 +00003217}
3218
Reed Kotler46090912013-05-10 22:25:39 +00003219
Akira Hatanaka7887c902012-10-26 23:56:38 +00003220void MipsTargetLowering::MipsCC::
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003221analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003222 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
3223 std::vector<ArgListEntry> &FuncArgs) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003224 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3225 "CallingConv::Fast shouldn't be used for vararg functions.");
3226
Akira Hatanaka7887c902012-10-26 23:56:38 +00003227 unsigned NumOpnds = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003228 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003229
3230 for (unsigned I = 0; I != NumOpnds; ++I) {
3231 MVT ArgVT = Args[I].VT;
3232 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3233 bool R;
3234
3235 if (ArgFlags.isByVal()) {
3236 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3237 continue;
3238 }
3239
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003240 if (IsVarArg && !Args[I].IsFixed)
Akira Hatanaka7887c902012-10-26 23:56:38 +00003241 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003242 else {
3243 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3244 IsSoftFloat);
3245 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
3246 }
Akira Hatanaka7887c902012-10-26 23:56:38 +00003247
3248 if (R) {
3249#ifndef NDEBUG
3250 dbgs() << "Call operand #" << I << " has unhandled type "
3251 << EVT(ArgVT).getEVTString();
3252#endif
3253 llvm_unreachable(0);
3254 }
3255 }
3256}
3257
3258void MipsTargetLowering::MipsCC::
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003259analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
3260 bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
Akira Hatanaka7887c902012-10-26 23:56:38 +00003261 unsigned NumArgs = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003262 llvm::CCAssignFn *FixedFn = fixedArgFn();
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003263 unsigned CurArgIdx = 0;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003264
3265 for (unsigned I = 0; I != NumArgs; ++I) {
3266 MVT ArgVT = Args[I].VT;
3267 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003268 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3269 CurArgIdx = Args[I].OrigArgIndex;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003270
3271 if (ArgFlags.isByVal()) {
3272 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3273 continue;
3274 }
3275
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003276 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), 0, IsSoftFloat);
3277
3278 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
Akira Hatanaka7887c902012-10-26 23:56:38 +00003279 continue;
3280
3281#ifndef NDEBUG
3282 dbgs() << "Formal Arg #" << I << " has unhandled type "
3283 << EVT(ArgVT).getEVTString();
3284#endif
3285 llvm_unreachable(0);
3286 }
3287}
3288
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003289template<typename Ty>
3290void MipsTargetLowering::MipsCC::
3291analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
3292 const SDNode *CallNode, const Type *RetTy) const {
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003293 CCAssignFn *Fn;
3294
3295 if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
3296 Fn = RetCC_F128Soft;
3297 else
3298 Fn = RetCC_Mips;
3299
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003300 for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
3301 MVT VT = RetVals[I].VT;
3302 ISD::ArgFlagsTy Flags = RetVals[I].Flags;
3303 MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
3304
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003305 if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003306#ifndef NDEBUG
3307 dbgs() << "Call result #" << I << " has unhandled type "
3308 << EVT(VT).getEVTString() << '\n';
3309#endif
3310 llvm_unreachable(0);
3311 }
3312 }
3313}
3314
3315void MipsTargetLowering::MipsCC::
3316analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
3317 const SDNode *CallNode, const Type *RetTy) const {
3318 analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
3319}
3320
3321void MipsTargetLowering::MipsCC::
3322analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
3323 const Type *RetTy) const {
3324 analyzeReturn(Outs, IsSoftFloat, 0, RetTy);
3325}
3326
Akira Hatanaka7887c902012-10-26 23:56:38 +00003327void
3328MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3329 MVT LocVT,
3330 CCValAssign::LocInfo LocInfo,
3331 ISD::ArgFlagsTy ArgFlags) {
3332 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3333
3334 struct ByValArgInfo ByVal;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003335 unsigned RegSize = regSize();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003336 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3337 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3338 RegSize * 2);
3339
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003340 if (useRegsForByval())
Akira Hatanaka7887c902012-10-26 23:56:38 +00003341 allocateRegs(ByVal, ByValSize, Align);
3342
3343 // Allocate space on caller's stack.
3344 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3345 Align);
3346 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3347 LocInfo));
3348 ByValArgs.push_back(ByVal);
3349}
3350
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003351unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
3352 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
3353}
3354
3355unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
3356 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
3357}
3358
3359const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const {
3360 return IsO32 ? O32IntRegs : Mips64IntRegs;
3361}
3362
3363llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3364 if (CallConv == CallingConv::Fast)
3365 return CC_Mips_FastCC;
3366
Reed Kotler46090912013-05-10 22:25:39 +00003367 if (SpecialCallingConv == Mips16RetHelperConv)
3368 return CC_Mips16RetHelper;
Akira Hatanakaad341d42013-08-20 23:38:40 +00003369 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003370}
3371
3372llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
Akira Hatanakaad341d42013-08-20 23:38:40 +00003373 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN_VarArg;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003374}
3375
3376const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const {
3377 return IsO32 ? O32IntRegs : Mips64DPRegs;
3378}
3379
Akira Hatanaka7887c902012-10-26 23:56:38 +00003380void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3381 unsigned ByValSize,
3382 unsigned Align) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003383 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
3384 const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003385 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3386 "Byval argument's size and alignment should be a multiple of"
3387 "RegSize.");
3388
3389 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3390
3391 // If Align > RegSize, the first arg register must be even.
3392 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3393 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3394 ++ByVal.FirstIdx;
3395 }
3396
3397 // Mark the registers allocated.
3398 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3399 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3400 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3401}
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003402
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003403MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3404 const SDNode *CallNode,
3405 bool IsSoftFloat) const {
3406 if (IsSoftFloat || IsO32)
3407 return VT;
3408
3409 // Check if the original type was fp128.
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003410 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003411 assert(VT == MVT::i64);
3412 return MVT::f64;
3413 }
3414
3415 return VT;
3416}
3417
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003418void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003419copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003420 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3421 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3422 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3423 MachineFunction &MF = DAG.getMachineFunction();
3424 MachineFrameInfo *MFI = MF.getFrameInfo();
3425 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
3426 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3427 int FrameObjOffset;
3428
3429 if (RegAreaSize)
3430 FrameObjOffset = (int)CC.reservedArgArea() -
3431 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
3432 else
3433 FrameObjOffset = ByVal.Address;
3434
3435 // Create frame object.
3436 EVT PtrTy = getPointerTy();
3437 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3438 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3439 InVals.push_back(FIN);
3440
3441 if (!ByVal.NumRegs)
3442 return;
3443
3444 // Copy arg registers.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003445 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003446 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3447
3448 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3449 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003450 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003451 unsigned Offset = I * CC.regSize();
3452 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3453 DAG.getConstant(Offset, PtrTy));
3454 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3455 StorePtr, MachinePointerInfo(FuncArg, Offset),
3456 false, false, 0);
3457 OutChains.push_back(Store);
3458 }
3459}
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003460
3461// Copy byVal arg to registers and stack.
3462void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003463passByValArg(SDValue Chain, SDLoc DL,
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00003464 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Craig Toppera0ec3f92013-07-14 04:42:23 +00003465 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003466 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3467 const MipsCC &CC, const ByValArgInfo &ByVal,
3468 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
3469 unsigned ByValSize = Flags.getByValSize();
3470 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
3471 unsigned RegSize = CC.regSize();
3472 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
3473 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
3474
3475 if (ByVal.NumRegs) {
3476 const uint16_t *ArgRegs = CC.intArgRegs();
3477 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
3478 unsigned I = 0;
3479
3480 // Copy words to registers.
3481 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
3482 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3483 DAG.getConstant(Offset, PtrTy));
3484 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3485 MachinePointerInfo(), false, false, false,
3486 Alignment);
3487 MemOpChains.push_back(LoadVal.getValue(1));
3488 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3489 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3490 }
3491
3492 // Return if the struct has been fully copied.
3493 if (ByValSize == Offset)
3494 return;
3495
3496 // Copy the remainder of the byval argument with sub-word loads and shifts.
3497 if (LeftoverBytes) {
3498 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
3499 "Size of the remainder should be smaller than RegSize.");
3500 SDValue Val;
3501
3502 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
3503 Offset < ByValSize; LoadSize /= 2) {
3504 unsigned RemSize = ByValSize - Offset;
3505
3506 if (RemSize < LoadSize)
3507 continue;
3508
3509 // Load subword.
3510 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3511 DAG.getConstant(Offset, PtrTy));
3512 SDValue LoadVal =
3513 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
3514 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
3515 false, false, Alignment);
3516 MemOpChains.push_back(LoadVal.getValue(1));
3517
3518 // Shift the loaded value.
3519 unsigned Shamt;
3520
3521 if (isLittle)
3522 Shamt = TotalSizeLoaded;
3523 else
3524 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
3525
3526 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3527 DAG.getConstant(Shamt, MVT::i32));
3528
3529 if (Val.getNode())
3530 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3531 else
3532 Val = Shift;
3533
3534 Offset += LoadSize;
3535 TotalSizeLoaded += LoadSize;
3536 Alignment = std::min(Alignment, LoadSize);
3537 }
3538
3539 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3540 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3541 return;
3542 }
3543 }
3544
3545 // Copy remainder of byval arg to it with memcpy.
3546 unsigned MemCpySize = ByValSize - Offset;
3547 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3548 DAG.getConstant(Offset, PtrTy));
3549 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3550 DAG.getIntPtrConstant(ByVal.Address));
3551 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3552 DAG.getConstant(MemCpySize, PtrTy), Alignment,
3553 /*isVolatile=*/false, /*AlwaysInline=*/false,
3554 MachinePointerInfo(0), MachinePointerInfo(0));
3555 MemOpChains.push_back(Chain);
3556}
Akira Hatanakaf0848472012-10-27 00:21:13 +00003557
3558void
3559MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3560 const MipsCC &CC, SDValue Chain,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003561 SDLoc DL, SelectionDAG &DAG) const {
Akira Hatanakaf0848472012-10-27 00:21:13 +00003562 unsigned NumRegs = CC.numIntArgRegs();
3563 const uint16_t *ArgRegs = CC.intArgRegs();
3564 const CCState &CCInfo = CC.getCCInfo();
3565 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
3566 unsigned RegSize = CC.regSize();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003567 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003568 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3569 MachineFunction &MF = DAG.getMachineFunction();
3570 MachineFrameInfo *MFI = MF.getFrameInfo();
3571 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3572
3573 // Offset of the first variable argument from stack pointer.
3574 int VaArgOffset;
3575
3576 if (NumRegs == Idx)
3577 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
3578 else
3579 VaArgOffset =
3580 (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
3581
3582 // Record the frame index of the first variable argument
3583 // which is a value necessary to VASTART.
3584 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3585 MipsFI->setVarArgsFrameIndex(FI);
3586
3587 // Copy the integer registers that have not been used for argument passing
3588 // to the argument register save area. For O32, the save area is allocated
3589 // in the caller's stack frame, while for N32/64, it is allocated in the
3590 // callee's stack frame.
3591 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003592 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003593 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3594 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3595 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3596 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3597 MachinePointerInfo(), false, false, 0);
3598 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
3599 OutChains.push_back(Store);
3600 }
3601}