blob: a1706ab1e47bae1530078115e5f7bddc10c475c2 [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";
Akira Hatanaka0f843822011-06-07 18:58:42 +0000215 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000216 }
217}
218
219MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +0000220MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka8b4198d2011-09-26 21:47:02 +0000221 : TargetLowering(TM, new MipsTargetObjectFile()),
222 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +0000223 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
224 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000225 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000226 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +0000227 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000228 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000229
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000230 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
232 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
233 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000234
Eli Friedman6055a6a2009-07-17 04:07:24 +0000235 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
237 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000238
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000239 // Used by legalize types to correctly generate the setcc result.
240 // Without this, every float setcc comes with a AND/OR with the result,
241 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000242 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000244
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000245 // Mips Custom Operations
Akira Hatanakab7656a92013-03-06 21:32:03 +0000246 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000247 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000248 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
250 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
251 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
252 setOperationAction(ISD::SELECT, MVT::f32, Custom);
253 setOperationAction(ISD::SELECT, MVT::f64, Custom);
254 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +0000255 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
256 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanaka0a40c232012-03-09 23:46:03 +0000257 setOperationAction(ISD::SETCC, MVT::f32, Custom);
258 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000260 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000261 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
262 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000263 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000264
Akira Hatanakac12a6e62012-04-11 22:49:04 +0000265 if (!TM.Options.NoNaNsFPMath) {
266 setOperationAction(ISD::FABS, MVT::f32, Custom);
267 setOperationAction(ISD::FABS, MVT::f64, Custom);
268 }
269
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000270 if (HasMips64) {
271 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
272 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
273 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
274 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
275 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
276 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka7664f052012-06-02 00:04:42 +0000277 setOperationAction(ISD::LOAD, MVT::i64, Custom);
278 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000279 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000280 }
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000281
Akira Hatanakaa284acb2012-05-09 00:55:21 +0000282 if (!HasMips64) {
283 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
284 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
285 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
286 }
287
Akira Hatanakae90a3bc2012-11-07 19:10:58 +0000288 setOperationAction(ISD::ADD, MVT::i32, Custom);
289 if (HasMips64)
290 setOperationAction(ISD::ADD, MVT::i64, Custom);
291
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000292 setOperationAction(ISD::SDIV, MVT::i32, Expand);
293 setOperationAction(ISD::SREM, MVT::i32, Expand);
294 setOperationAction(ISD::UDIV, MVT::i32, Expand);
295 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000296 setOperationAction(ISD::SDIV, MVT::i64, Expand);
297 setOperationAction(ISD::SREM, MVT::i64, Expand);
298 setOperationAction(ISD::UDIV, MVT::i64, Expand);
299 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000300
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000301 // Operations not directly supported by Mips.
Tom Stellard3ef53832013-03-08 15:36:57 +0000302 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
303 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
304 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
305 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
307 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000308 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000310 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
312 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000313 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000315 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000316 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
317 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
318 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
319 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000321 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka1d165f12012-07-31 20:54:48 +0000322 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
323 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000324
Akira Hatanaka56633442011-09-20 23:53:09 +0000325 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000326 setOperationAction(ISD::ROTR, MVT::i32, Expand);
327
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000328 if (!Subtarget->hasMips64r2())
329 setOperationAction(ISD::ROTR, MVT::i64, Expand);
330
Owen Anderson825b72b2009-08-11 20:47:22 +0000331 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000332 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000334 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000335 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
336 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
338 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000339 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000340 setOperationAction(ISD::FLOG, MVT::f32, Expand);
341 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
342 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
343 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000344 setOperationAction(ISD::FMA, MVT::f32, Expand);
345 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka21ecc2f2012-03-29 18:43:11 +0000346 setOperationAction(ISD::FREM, MVT::f32, Expand);
347 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000348
Akira Hatanaka1cc63332012-04-11 22:59:08 +0000349 if (!TM.Options.NoNaNsFPMath) {
350 setOperationAction(ISD::FNEG, MVT::f32, Expand);
351 setOperationAction(ISD::FNEG, MVT::f64, Expand);
352 }
353
Akira Hatanaka544cc212013-01-30 00:26:49 +0000354 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
355
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000356 setOperationAction(ISD::VAARG, MVT::Other, Expand);
357 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
358 setOperationAction(ISD::VAEND, MVT::Other, Expand);
359
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000360 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000361 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
362 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000363
Jia Liubb481f82012-02-28 07:46:26 +0000364 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
365 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
366 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
367 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman4db5aca2011-08-29 18:23:02 +0000368
Eli Friedman26689ac2011-08-03 21:06:02 +0000369 setInsertFencesForAtomic(true);
370
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000371 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000374 }
375
Akira Hatanakac79507a2011-12-21 00:20:27 +0000376 if (!Subtarget->hasBitCount()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000377 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Akira Hatanakac79507a2011-12-21 00:20:27 +0000378 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
379 }
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000380
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000381 if (!Subtarget->hasSwap()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000383 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
384 }
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000385
Akira Hatanaka7664f052012-06-02 00:04:42 +0000386 if (HasMips64) {
387 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
388 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
389 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
390 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
391 }
392
Akira Hatanaka97585622013-07-26 20:58:55 +0000393 setOperationAction(ISD::TRAP, MVT::Other, Legal);
394
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000395 setTargetDAGCombine(ISD::SDIVREM);
396 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanakaee8c3b02012-03-08 03:26:37 +0000397 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000398 setTargetDAGCombine(ISD::AND);
399 setTargetDAGCombine(ISD::OR);
Akira Hatanaka87827072012-06-13 20:33:18 +0000400 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000401
Akira Hatanaka5fdf5002012-03-08 01:59:33 +0000402 setMinFunctionAlignment(HasMips64 ? 3 : 2);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000403
Akira Hatanaka3f5b1072012-02-02 03:17:04 +0000404 setStackPointerRegisterToSaveRestore(IsN64 ? Mips::SP_64 : Mips::SP);
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000405
Akira Hatanaka590baca2012-02-02 03:13:40 +0000406 setExceptionPointerRegister(IsN64 ? Mips::A0_64 : Mips::A0);
407 setExceptionSelectorRegister(IsN64 ? Mips::A1_64 : Mips::A1);
Akira Hatanakae193b322012-06-13 19:33:32 +0000408
Jim Grosbach3450f802013-02-20 21:13:59 +0000409 MaxStoresPerMemcpy = 16;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000410}
411
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000412const MipsTargetLowering *MipsTargetLowering::create(MipsTargetMachine &TM) {
413 if (TM.getSubtargetImpl()->inMips16Mode())
414 return llvm::createMips16TargetLowering(TM);
Jia Liubb481f82012-02-28 07:46:26 +0000415
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000416 return llvm::createMipsSETargetLowering(TM);
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000417}
418
Matt Arsenault225ed702013-05-18 00:21:46 +0000419EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakae13f4412013-01-04 20:06:01 +0000420 if (!VT.isVector())
421 return MVT::i32;
422 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +0000423}
424
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000425static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000426 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000427 const MipsSubtarget *Subtarget) {
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000428 if (DCI.isBeforeLegalizeOps())
429 return SDValue();
430
Akira Hatanakadda4a072011-10-03 21:06:13 +0000431 EVT Ty = N->getValueType(0);
Akira Hatanakacbaf6d02013-08-14 00:47:08 +0000432 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
433 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000434 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
435 MipsISD::DivRemU16;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000436 SDLoc DL(N);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000437
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000438 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000439 N->getOperand(0), N->getOperand(1));
440 SDValue InChain = DAG.getEntryNode();
441 SDValue InGlue = DivRem;
442
443 // insert MFLO
444 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000445 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000446 InGlue);
447 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
448 InChain = CopyFromLo.getValue(1);
449 InGlue = CopyFromLo.getValue(2);
450 }
451
452 // insert MFHI
453 if (N->hasAnyUseOfValue(1)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000454 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000455 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000456 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
457 }
458
459 return SDValue();
460}
461
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000462static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000463 switch (CC) {
464 default: llvm_unreachable("Unknown fp condition code!");
465 case ISD::SETEQ:
466 case ISD::SETOEQ: return Mips::FCOND_OEQ;
467 case ISD::SETUNE: return Mips::FCOND_UNE;
468 case ISD::SETLT:
469 case ISD::SETOLT: return Mips::FCOND_OLT;
470 case ISD::SETGT:
471 case ISD::SETOGT: return Mips::FCOND_OGT;
472 case ISD::SETLE:
473 case ISD::SETOLE: return Mips::FCOND_OLE;
474 case ISD::SETGE:
475 case ISD::SETOGE: return Mips::FCOND_OGE;
476 case ISD::SETULT: return Mips::FCOND_ULT;
477 case ISD::SETULE: return Mips::FCOND_ULE;
478 case ISD::SETUGT: return Mips::FCOND_UGT;
479 case ISD::SETUGE: return Mips::FCOND_UGE;
480 case ISD::SETUO: return Mips::FCOND_UN;
481 case ISD::SETO: return Mips::FCOND_OR;
482 case ISD::SETNE:
483 case ISD::SETONE: return Mips::FCOND_ONE;
484 case ISD::SETUEQ: return Mips::FCOND_UEQ;
485 }
486}
487
488
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000489/// This function returns true if the floating point conditional branches and
490/// conditional moves which use condition code CC should be inverted.
491static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000492 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
493 return false;
494
Akira Hatanaka82099682011-12-19 19:52:25 +0000495 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
496 "Illegal Condition Code");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000497
Akira Hatanaka82099682011-12-19 19:52:25 +0000498 return true;
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000499}
500
501// Creates and returns an FPCmp node from a setcc node.
502// Returns Op if setcc is not a floating point comparison.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000503static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000504 // must be a SETCC node
505 if (Op.getOpcode() != ISD::SETCC)
506 return Op;
507
508 SDValue LHS = Op.getOperand(0);
509
510 if (!LHS.getValueType().isFloatingPoint())
511 return Op;
512
513 SDValue RHS = Op.getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000514 SDLoc DL(Op);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000515
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000516 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
517 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000518 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
519
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000520 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000521 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000522}
523
524// Creates and returns a CMovFPT/F node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000525static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000526 SDValue False, SDLoc DL) {
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000527 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
528 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka407883b2013-07-26 20:51:20 +0000529 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000530
531 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka407883b2013-07-26 20:51:20 +0000532 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000533}
534
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000535static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000536 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000537 const MipsSubtarget *Subtarget) {
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000538 if (DCI.isBeforeLegalizeOps())
539 return SDValue();
540
541 SDValue SetCC = N->getOperand(0);
542
543 if ((SetCC.getOpcode() != ISD::SETCC) ||
544 !SetCC.getOperand(0).getValueType().isInteger())
545 return SDValue();
546
547 SDValue False = N->getOperand(2);
548 EVT FalseTy = False.getValueType();
549
550 if (!FalseTy.isInteger())
551 return SDValue();
552
553 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(False);
554
555 if (!CN || CN->getZExtValue())
556 return SDValue();
557
Andrew Trickac6d9be2013-05-25 02:42:55 +0000558 const SDLoc DL(N);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000559 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
560 SDValue True = N->getOperand(1);
Akira Hatanaka864f6602012-06-14 21:10:56 +0000561
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000562 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
563 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
Akira Hatanaka864f6602012-06-14 21:10:56 +0000564
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000565 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
566}
567
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000568static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000569 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000570 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000571 // Pattern match EXT.
572 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
573 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000574 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000575 return SDValue();
576
577 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000578 unsigned ShiftRightOpc = ShiftRight.getOpcode();
579
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000580 // Op's first operand must be a shift right.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000581 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000582 return SDValue();
583
584 // The second operand of the shift must be an immediate.
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000585 ConstantSDNode *CN;
586 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
587 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000588
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000589 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000590 uint64_t SMPos, SMSize;
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000591
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000592 // Op's second operand must be a shifted mask.
593 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000594 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000595 return SDValue();
596
597 // Return if the shifted mask does not start at bit 0 or the sum of its size
598 // and Pos exceeds the word's size.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000599 EVT ValTy = N->getValueType(0);
600 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000601 return SDValue();
602
Andrew Trickac6d9be2013-05-25 02:42:55 +0000603 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka82099682011-12-19 19:52:25 +0000604 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000605 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000606}
Jia Liubb481f82012-02-28 07:46:26 +0000607
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000608static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000609 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000610 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000611 // Pattern match INS.
612 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liubb481f82012-02-28 07:46:26 +0000613 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000614 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000615 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000616 return SDValue();
617
618 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
619 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
620 ConstantSDNode *CN;
621
622 // See if Op's first operand matches (and $src1 , mask0).
623 if (And0.getOpcode() != ISD::AND)
624 return SDValue();
625
626 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000627 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000628 return SDValue();
629
630 // See if Op's second operand matches (and (shl $src, pos), mask1).
631 if (And1.getOpcode() != ISD::AND)
632 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000633
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000634 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000635 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000636 return SDValue();
637
638 // The shift masks must have the same position and size.
639 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
640 return SDValue();
641
642 SDValue Shl = And1.getOperand(0);
643 if (Shl.getOpcode() != ISD::SHL)
644 return SDValue();
645
646 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
647 return SDValue();
648
649 unsigned Shamt = CN->getZExtValue();
650
651 // Return if the shift amount and the first bit position of mask are not the
Jia Liubb481f82012-02-28 07:46:26 +0000652 // same.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000653 EVT ValTy = N->getValueType(0);
654 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000655 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000656
Andrew Trickac6d9be2013-05-25 02:42:55 +0000657 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000658 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka82099682011-12-19 19:52:25 +0000659 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000660}
Jia Liubb481f82012-02-28 07:46:26 +0000661
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000662static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka87827072012-06-13 20:33:18 +0000663 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000664 const MipsSubtarget *Subtarget) {
Akira Hatanaka87827072012-06-13 20:33:18 +0000665 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
666
667 if (DCI.isBeforeLegalizeOps())
668 return SDValue();
669
670 SDValue Add = N->getOperand(1);
671
672 if (Add.getOpcode() != ISD::ADD)
673 return SDValue();
674
675 SDValue Lo = Add.getOperand(1);
676
677 if ((Lo.getOpcode() != MipsISD::Lo) ||
678 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
679 return SDValue();
680
681 EVT ValTy = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000682 SDLoc DL(N);
Akira Hatanaka87827072012-06-13 20:33:18 +0000683
684 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
685 Add.getOperand(0));
686 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
687}
688
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000689SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000690 const {
691 SelectionDAG &DAG = DCI.DAG;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000692 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000693
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000694 switch (Opc) {
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000695 default: break;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000696 case ISD::SDIVREM:
697 case ISD::UDIVREM:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000698 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000699 case ISD::SELECT:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000700 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000701 case ISD::AND:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000702 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000703 case ISD::OR:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000704 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka87827072012-06-13 20:33:18 +0000705 case ISD::ADD:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000706 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000707 }
708
709 return SDValue();
710}
711
Akira Hatanakab430cec2012-09-21 23:58:31 +0000712void
713MipsTargetLowering::LowerOperationWrapper(SDNode *N,
714 SmallVectorImpl<SDValue> &Results,
715 SelectionDAG &DAG) const {
716 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
717
718 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
719 Results.push_back(Res.getValue(I));
720}
721
722void
723MipsTargetLowering::ReplaceNodeResults(SDNode *N,
724 SmallVectorImpl<SDValue> &Results,
725 SelectionDAG &DAG) const {
Akira Hatanaka13ec4812013-04-30 21:17:07 +0000726 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakab430cec2012-09-21 23:58:31 +0000727}
728
Dan Gohman475871a2008-07-27 21:46:04 +0000729SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000730LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000731{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000732 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000733 {
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000734 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
735 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
736 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
737 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
738 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
739 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
740 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
741 case ISD::SELECT: return lowerSELECT(Op, DAG);
742 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
743 case ISD::SETCC: return lowerSETCC(Op, DAG);
744 case ISD::VASTART: return lowerVASTART(Op, DAG);
745 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
746 case ISD::FABS: return lowerFABS(Op, DAG);
747 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
748 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
749 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000750 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
751 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
752 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
753 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
754 case ISD::LOAD: return lowerLOAD(Op, DAG);
755 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000756 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000757 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000758 }
Dan Gohman475871a2008-07-27 21:46:04 +0000759 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000760}
761
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000762//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000763// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000764//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000765
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000766// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000767// MachineFunction as a live in value. It also creates a corresponding
768// virtual register for it.
769static unsigned
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000770addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000771{
Chris Lattner84bc5422007-12-31 04:13:23 +0000772 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
773 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000774 return VReg;
775}
776
Akira Hatanakaf8941992013-05-20 18:07:43 +0000777static MachineBasicBlock *expandPseudoDIV(MachineInstr *MI,
778 MachineBasicBlock &MBB,
779 const TargetInstrInfo &TII,
780 bool Is64Bit) {
781 if (NoZeroDivCheck)
782 return &MBB;
783
784 // Insert instruction "teq $divisor_reg, $zero, 7".
785 MachineBasicBlock::iterator I(MI);
786 MachineInstrBuilder MIB;
787 MIB = BuildMI(MBB, llvm::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
788 .addOperand(MI->getOperand(2)).addReg(Mips::ZERO).addImm(7);
789
790 // Use the 32-bit sub-register if this is a 64-bit division.
791 if (Is64Bit)
792 MIB->getOperand(0).setSubReg(Mips::sub_32);
793
794 return &MBB;
795}
796
Akira Hatanaka01f70892012-09-27 02:15:57 +0000797MachineBasicBlock *
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000798MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000799 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000800 switch (MI->getOpcode()) {
Reed Kotlerffbe4322013-02-21 04:22:38 +0000801 default:
802 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000803 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000804 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000805 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000806 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000807 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000808 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000809 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000810 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000811
812 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000813 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000814 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000815 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000816 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000817 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000818 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000819 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000820
821 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000822 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000823 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000824 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000825 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000826 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000827 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000828 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000829
830 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000831 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000832 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000833 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000834 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000835 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000836 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000837 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000838
839 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000840 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000841 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000842 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000843 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000844 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000845 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000846 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000847
848 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000849 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000850 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000851 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000852 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000853 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000854 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000855 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000856
857 case Mips::ATOMIC_SWAP_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000858 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000859 case Mips::ATOMIC_SWAP_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000860 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000861 case Mips::ATOMIC_SWAP_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000862 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000863 case Mips::ATOMIC_SWAP_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000864 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000865
866 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000867 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000868 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000869 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000870 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000871 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000872 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000873 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanakaf8941992013-05-20 18:07:43 +0000874 case Mips::PseudoSDIV:
875 case Mips::PseudoUDIV:
876 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), false);
877 case Mips::PseudoDSDIV:
878 case Mips::PseudoDUDIV:
879 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), true);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000880 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000881}
882
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000883// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
884// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
885MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000886MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000887 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000888 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000889 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000890
891 MachineFunction *MF = BB->getParent();
892 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000893 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000894 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000895 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000896 unsigned LL, SC, AND, NOR, ZERO, BEQ;
897
898 if (Size == 4) {
Akira Hatanakaa98a4862013-08-20 21:08:22 +0000899 LL = Mips::LL;
900 SC = Mips::SC;
Akira Hatanaka59068062011-11-11 04:14:30 +0000901 AND = Mips::AND;
902 NOR = Mips::NOR;
903 ZERO = Mips::ZERO;
904 BEQ = Mips::BEQ;
905 }
906 else {
Akira Hatanakaa98a4862013-08-20 21:08:22 +0000907 LL = Mips::LLD;
908 SC = Mips::SCD;
Akira Hatanaka59068062011-11-11 04:14:30 +0000909 AND = Mips::AND64;
910 NOR = Mips::NOR64;
911 ZERO = Mips::ZERO_64;
912 BEQ = Mips::BEQ64;
913 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000914
Akira Hatanaka4061da12011-07-19 20:11:17 +0000915 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000916 unsigned Ptr = MI->getOperand(1).getReg();
917 unsigned Incr = MI->getOperand(2).getReg();
918
Akira Hatanaka4061da12011-07-19 20:11:17 +0000919 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
920 unsigned AndRes = RegInfo.createVirtualRegister(RC);
921 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000922
923 // insert new blocks after the current block
924 const BasicBlock *LLVM_BB = BB->getBasicBlock();
925 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
926 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
927 MachineFunction::iterator It = BB;
928 ++It;
929 MF->insert(It, loopMBB);
930 MF->insert(It, exitMBB);
931
932 // Transfer the remainder of BB and its successor edges to exitMBB.
933 exitMBB->splice(exitMBB->begin(), BB,
934 llvm::next(MachineBasicBlock::iterator(MI)),
935 BB->end());
936 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
937
938 // thisMBB:
939 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000940 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000941 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000942 loopMBB->addSuccessor(loopMBB);
943 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000944
945 // loopMBB:
946 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000947 // <binop> storeval, oldval, incr
948 // sc success, storeval, 0(ptr)
949 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000950 BB = loopMBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000951 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000952 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000953 // and andres, oldval, incr
954 // nor storeval, $0, andres
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000955 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
956 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000957 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000958 // <binop> storeval, oldval, incr
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000959 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000960 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000961 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000962 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000963 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
964 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000965
966 MI->eraseFromParent(); // The instruction is gone now.
967
Akira Hatanaka939ece12011-07-19 03:42:13 +0000968 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000969}
970
971MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000972MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000973 MachineBasicBlock *BB,
974 unsigned Size, unsigned BinOpcode,
975 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000976 assert((Size == 1 || Size == 2) &&
977 "Unsupported size for EmitAtomicBinaryPartial.");
978
979 MachineFunction *MF = BB->getParent();
980 MachineRegisterInfo &RegInfo = MF->getRegInfo();
981 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
982 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000983 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000984
985 unsigned Dest = MI->getOperand(0).getReg();
986 unsigned Ptr = MI->getOperand(1).getReg();
987 unsigned Incr = MI->getOperand(2).getReg();
988
Akira Hatanaka4061da12011-07-19 20:11:17 +0000989 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
990 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000991 unsigned Mask = RegInfo.createVirtualRegister(RC);
992 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000993 unsigned NewVal = RegInfo.createVirtualRegister(RC);
994 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000995 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000996 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
997 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
998 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
999 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1000 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001001 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001002 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1003 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1004 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1005 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1006 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001007
1008 // insert new blocks after the current block
1009 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1010 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001011 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001012 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1013 MachineFunction::iterator It = BB;
1014 ++It;
1015 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001016 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001017 MF->insert(It, exitMBB);
1018
1019 // Transfer the remainder of BB and its successor edges to exitMBB.
1020 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001021 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001022 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1023
Akira Hatanaka81b44112011-07-19 17:09:53 +00001024 BB->addSuccessor(loopMBB);
1025 loopMBB->addSuccessor(loopMBB);
1026 loopMBB->addSuccessor(sinkMBB);
1027 sinkMBB->addSuccessor(exitMBB);
1028
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001029 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001030 // addiu masklsb2,$0,-4 # 0xfffffffc
1031 // and alignedaddr,ptr,masklsb2
1032 // andi ptrlsb2,ptr,3
1033 // sll shiftamt,ptrlsb2,3
1034 // ori maskupper,$0,255 # 0xff
1035 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001036 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001037 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001038
1039 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001040 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001041 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001042 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001043 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001044 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001045 if (Subtarget->isLittle()) {
1046 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1047 } else {
1048 unsigned Off = RegInfo.createVirtualRegister(RC);
1049 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1050 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1051 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1052 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001053 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001054 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001055 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001056 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001057 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka51122432013-07-01 20:39:53 +00001058 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001059
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001060 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001061 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001062 // ll oldval,0(alignedaddr)
1063 // binop binopres,oldval,incr2
1064 // and newval,binopres,mask
1065 // and maskedoldval0,oldval,mask2
1066 // or storeval,maskedoldval0,newval
1067 // sc success,storeval,0(alignedaddr)
1068 // beq success,$0,loopMBB
1069
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001070 // atomic.swap
1071 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001072 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001073 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001074 // and maskedoldval0,oldval,mask2
1075 // or storeval,maskedoldval0,newval
1076 // sc success,storeval,0(alignedaddr)
1077 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001078
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001079 BB = loopMBB;
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001080 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001081 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001082 // and andres, oldval, incr2
1083 // nor binopres, $0, andres
1084 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001085 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1086 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001087 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001088 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001089 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001090 // <binop> binopres, oldval, incr2
1091 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001092 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1093 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001094 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001095 // and newval, incr2, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001096 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001097 }
Jia Liubb481f82012-02-28 07:46:26 +00001098
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001099 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001100 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001101 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001102 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001103 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001104 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001105 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001106 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001107
Akira Hatanaka939ece12011-07-19 03:42:13 +00001108 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001109 // and maskedoldval1,oldval,mask
1110 // srl srlres,maskedoldval1,shiftamt
1111 // sll sllres,srlres,24
1112 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001113 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001114 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001115
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001116 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001117 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001118 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001119 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001120 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001121 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001122 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001123 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001124
1125 MI->eraseFromParent(); // The instruction is gone now.
1126
Akira Hatanaka939ece12011-07-19 03:42:13 +00001127 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001128}
1129
1130MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001131MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001132 MachineBasicBlock *BB,
1133 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001134 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001135
1136 MachineFunction *MF = BB->getParent();
1137 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001138 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001139 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001140 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001141 unsigned LL, SC, ZERO, BNE, BEQ;
1142
1143 if (Size == 4) {
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001144 LL = Mips::LL;
1145 SC = Mips::SC;
Akira Hatanaka59068062011-11-11 04:14:30 +00001146 ZERO = Mips::ZERO;
1147 BNE = Mips::BNE;
1148 BEQ = Mips::BEQ;
1149 }
1150 else {
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001151 LL = Mips::LLD;
1152 SC = Mips::SCD;
Akira Hatanaka59068062011-11-11 04:14:30 +00001153 ZERO = Mips::ZERO_64;
1154 BNE = Mips::BNE64;
1155 BEQ = Mips::BEQ64;
1156 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001157
1158 unsigned Dest = MI->getOperand(0).getReg();
1159 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001160 unsigned OldVal = MI->getOperand(2).getReg();
1161 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001162
Akira Hatanaka4061da12011-07-19 20:11:17 +00001163 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001164
1165 // insert new blocks after the current block
1166 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1167 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1168 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1169 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1170 MachineFunction::iterator It = BB;
1171 ++It;
1172 MF->insert(It, loop1MBB);
1173 MF->insert(It, loop2MBB);
1174 MF->insert(It, exitMBB);
1175
1176 // Transfer the remainder of BB and its successor edges to exitMBB.
1177 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001178 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001179 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1180
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001181 // thisMBB:
1182 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001183 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001184 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001185 loop1MBB->addSuccessor(exitMBB);
1186 loop1MBB->addSuccessor(loop2MBB);
1187 loop2MBB->addSuccessor(loop1MBB);
1188 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001189
1190 // loop1MBB:
1191 // ll dest, 0(ptr)
1192 // bne dest, oldval, exitMBB
1193 BB = loop1MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001194 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1195 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001196 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001197
1198 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001199 // sc success, newval, 0(ptr)
1200 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001201 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001202 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001203 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001204 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka59068062011-11-11 04:14:30 +00001205 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001206
1207 MI->eraseFromParent(); // The instruction is gone now.
1208
Akira Hatanaka939ece12011-07-19 03:42:13 +00001209 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001210}
1211
1212MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001213MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001214 MachineBasicBlock *BB,
1215 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001216 assert((Size == 1 || Size == 2) &&
1217 "Unsupported size for EmitAtomicCmpSwapPartial.");
1218
1219 MachineFunction *MF = BB->getParent();
1220 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1221 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1222 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001223 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001224
1225 unsigned Dest = MI->getOperand(0).getReg();
1226 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001227 unsigned CmpVal = MI->getOperand(2).getReg();
1228 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001229
Akira Hatanaka4061da12011-07-19 20:11:17 +00001230 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1231 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001232 unsigned Mask = RegInfo.createVirtualRegister(RC);
1233 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001234 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1235 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1236 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1237 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1238 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1239 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1240 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1241 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1242 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1243 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1244 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1245 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1246 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1247 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001248
1249 // insert new blocks after the current block
1250 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1251 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1252 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001253 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001254 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1255 MachineFunction::iterator It = BB;
1256 ++It;
1257 MF->insert(It, loop1MBB);
1258 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001259 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001260 MF->insert(It, exitMBB);
1261
1262 // Transfer the remainder of BB and its successor edges to exitMBB.
1263 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001264 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001265 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1266
Akira Hatanaka81b44112011-07-19 17:09:53 +00001267 BB->addSuccessor(loop1MBB);
1268 loop1MBB->addSuccessor(sinkMBB);
1269 loop1MBB->addSuccessor(loop2MBB);
1270 loop2MBB->addSuccessor(loop1MBB);
1271 loop2MBB->addSuccessor(sinkMBB);
1272 sinkMBB->addSuccessor(exitMBB);
1273
Akira Hatanaka70564a92011-07-19 18:14:26 +00001274 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001275 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001276 // addiu masklsb2,$0,-4 # 0xfffffffc
1277 // and alignedaddr,ptr,masklsb2
1278 // andi ptrlsb2,ptr,3
1279 // sll shiftamt,ptrlsb2,3
1280 // ori maskupper,$0,255 # 0xff
1281 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001282 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001283 // andi maskedcmpval,cmpval,255
1284 // sll shiftedcmpval,maskedcmpval,shiftamt
1285 // andi maskednewval,newval,255
1286 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001287 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001288 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001289 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001290 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001291 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001292 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001293 if (Subtarget->isLittle()) {
1294 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1295 } else {
1296 unsigned Off = RegInfo.createVirtualRegister(RC);
1297 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1298 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1299 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1300 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001301 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001302 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001303 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001304 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001305 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1306 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001307 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001308 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001309 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001310 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001311 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001312 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001313 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001314
1315 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001316 // ll oldval,0(alginedaddr)
1317 // and maskedoldval0,oldval,mask
1318 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001319 BB = loop1MBB;
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001320 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001321 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001322 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001323 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001324 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001325
1326 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001327 // and maskedoldval1,oldval,mask2
1328 // or storeval,maskedoldval1,shiftednewval
1329 // sc success,storeval,0(alignedaddr)
1330 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001331 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001332 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001333 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001334 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001335 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001336 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001337 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001338 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001339 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001340
Akira Hatanaka939ece12011-07-19 03:42:13 +00001341 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001342 // srl srlres,maskedoldval0,shiftamt
1343 // sll sllres,srlres,24
1344 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001345 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001346 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001347
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001348 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001349 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001350 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001351 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001352 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001353 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001354
1355 MI->eraseFromParent(); // The instruction is gone now.
1356
Akira Hatanaka939ece12011-07-19 03:42:13 +00001357 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001358}
1359
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001360//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001361// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001362//===----------------------------------------------------------------------===//
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001363SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakab7656a92013-03-06 21:32:03 +00001364 SDValue Chain = Op.getOperand(0);
1365 SDValue Table = Op.getOperand(1);
1366 SDValue Index = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001367 SDLoc DL(Op);
Akira Hatanakab7656a92013-03-06 21:32:03 +00001368 EVT PTy = getPointerTy();
1369 unsigned EntrySize =
1370 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1371
1372 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1373 DAG.getConstant(EntrySize, PTy));
1374 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1375
1376 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1377 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1378 MachinePointerInfo::getJumpTable(), MemVT, false, false,
1379 0);
1380 Chain = Addr.getValue(1);
1381
1382 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || IsN64) {
1383 // For PIC, the sequence is:
1384 // BRIND(load(Jumptable + index) + RelocBase)
1385 // RelocBase can be JumpTable, GOT or some sort of global base.
1386 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1387 getPICJumpTableRelocBase(Table, DAG));
1388 }
1389
1390 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1391}
1392
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001393SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001394lowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001395{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001396 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001397 // the block to branch to if the condition is true.
1398 SDValue Chain = Op.getOperand(0);
1399 SDValue Dest = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001400 SDLoc DL(Op);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001401
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001402 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001403
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001404 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001405 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001406 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001407
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001408 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001409 Mips::CondCode CC =
1410 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanaka9cf07242013-03-30 01:16:38 +00001411 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1412 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001413 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001414 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001415 FCC0, Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001416}
1417
1418SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001419lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001420{
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001421 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001422
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001423 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001424 if (Cond.getOpcode() != MipsISD::FPCmp)
1425 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001426
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001427 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001428 SDLoc(Op));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001429}
1430
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001431SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001432lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001433{
Andrew Trickac6d9be2013-05-25 02:42:55 +00001434 SDLoc DL(Op);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001435 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault225ed702013-05-18 00:21:46 +00001436 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1437 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001438 Op.getOperand(0), Op.getOperand(1),
1439 Op.getOperand(4));
1440
1441 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1442 Op.getOperand(3));
1443}
1444
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001445SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1446 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001447
1448 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1449 "Floating point operand expected.");
1450
1451 SDValue True = DAG.getConstant(1, MVT::i32);
1452 SDValue False = DAG.getConstant(0, MVT::i32);
1453
Andrew Trickac6d9be2013-05-25 02:42:55 +00001454 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001455}
1456
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001457SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001458 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001459 // FIXME there isn't actually debug info here
Andrew Trickac6d9be2013-05-25 02:42:55 +00001460 SDLoc DL(Op);
Jia Liubb481f82012-02-28 07:46:26 +00001461 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001462
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001463 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaafc945b2012-09-12 23:27:55 +00001464 const MipsTargetObjectFile &TLOF =
1465 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001466
Chris Lattnere3736f82009-08-13 05:41:27 +00001467 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001468 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001469 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001470 MipsII::MO_GPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001471 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001472 DAG.getVTList(MVT::i32), &GA, 1);
Akira Hatanakae7338cd2012-08-22 03:18:13 +00001473 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001474 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001475 }
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001476
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001477 // %hi/%lo relocation
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001478 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001479 }
1480
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001481 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
1482 return getAddrLocal(Op, DAG, HasMips64);
1483
Akira Hatanakaf09a0372012-11-21 20:40:38 +00001484 if (LargeGOT)
1485 return getAddrGlobalLargeGOT(Op, DAG, MipsII::MO_GOT_HI16,
1486 MipsII::MO_GOT_LO16);
1487
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001488 return getAddrGlobal(Op, DAG,
1489 HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001490}
1491
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001492SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001493 SelectionDAG &DAG) const {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001494 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1495 return getAddrNonPIC(Op, DAG);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001496
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001497 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001498}
1499
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001500SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001501lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001502{
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001503 // If the relocation model is PIC, use the General Dynamic TLS Model or
1504 // Local Dynamic TLS model, otherwise use the Initial Exec or
1505 // Local Exec TLS Model.
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001506
1507 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001508 SDLoc DL(GA);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001509 const GlobalValue *GV = GA->getGlobal();
1510 EVT PtrVT = getPointerTy();
1511
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001512 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1513
1514 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg70a07c72012-06-04 14:02:08 +00001515 // General Dynamic and Local Dynamic TLS Model.
1516 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1517 : MipsII::MO_TLSGD;
1518
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001519 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1520 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1521 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001522 unsigned PtrSize = PtrVT.getSizeInBits();
1523 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1524
Benjamin Kramer5eccf672011-12-11 12:21:34 +00001525 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001526
1527 ArgListTy Args;
1528 ArgListEntry Entry;
1529 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00001530 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001531 Args.push_back(Entry);
Jia Liubb481f82012-02-28 07:46:26 +00001532
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001533 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001534 false, false, false, false, 0, CallingConv::C,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001535 /*IsTailCall=*/false, /*doesNotRet=*/false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001536 /*isReturnValueUsed=*/true,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001537 TlsGetAddr, Args, DAG, DL);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001538 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001539
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001540 SDValue Ret = CallResult.first;
1541
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001542 if (model != TLSModel::LocalDynamic)
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001543 return Ret;
1544
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001545 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001546 MipsII::MO_DTPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001547 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1548 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001549 MipsII::MO_DTPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001550 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1551 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1552 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001553 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001554
1555 SDValue Offset;
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001556 if (model == TLSModel::InitialExec) {
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001557 // Initial Exec TLS Model
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001558 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001559 MipsII::MO_GOTTPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001560 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanaka648f00c2012-02-24 22:34:47 +00001561 TGA);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001562 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001563 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001564 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001565 } else {
1566 // Local Exec TLS Model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001567 assert(model == TLSModel::LocalExec);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001568 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001569 MipsII::MO_TPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001570 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001571 MipsII::MO_TPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001572 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1573 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1574 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001575 }
1576
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001577 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1578 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001579}
1580
1581SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001582lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001583{
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001584 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1585 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001586
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001587 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001588}
1589
Dan Gohman475871a2008-07-27 21:46:04 +00001590SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001591lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001592{
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001593 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001594 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001595 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001596 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001597 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001598 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001599 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1600 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001601 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001602
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001603 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1604 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001605
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001606 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001607}
1608
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001609SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001610 MachineFunction &MF = DAG.getMachineFunction();
1611 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1612
Andrew Trickac6d9be2013-05-25 02:42:55 +00001613 SDLoc DL(Op);
Dan Gohman1e93df62010-04-17 14:41:14 +00001614 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1615 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001616
1617 // vastart just stores the address of the VarArgsFrameIndex slot into the
1618 // memory location argument.
1619 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001620 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka82099682011-12-19 19:52:25 +00001621 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001622}
Jia Liubb481f82012-02-28 07:46:26 +00001623
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001624static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001625 EVT TyX = Op.getOperand(0).getValueType();
1626 EVT TyY = Op.getOperand(1).getValueType();
1627 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1628 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001629 SDLoc DL(Op);
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001630 SDValue Res;
1631
1632 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1633 // to i32.
1634 SDValue X = (TyX == MVT::f32) ?
1635 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1636 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1637 Const1);
1638 SDValue Y = (TyY == MVT::f32) ?
1639 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1640 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1641 Const1);
1642
1643 if (HasR2) {
1644 // ext E, Y, 31, 1 ; extract bit31 of Y
1645 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1646 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1647 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1648 } else {
1649 // sll SllX, X, 1
1650 // srl SrlX, SllX, 1
1651 // srl SrlY, Y, 31
1652 // sll SllY, SrlX, 31
1653 // or Or, SrlX, SllY
1654 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1655 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1656 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1657 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1658 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1659 }
1660
1661 if (TyX == MVT::f32)
1662 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1663
1664 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1665 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1666 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001667}
1668
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001669static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001670 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1671 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1672 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1673 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001674 SDLoc DL(Op);
Eric Christopher471e4222011-06-08 23:55:35 +00001675
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001676 // Bitcast to integer nodes.
1677 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1678 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001679
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001680 if (HasR2) {
1681 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1682 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1683 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1684 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001685
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001686 if (WidthX > WidthY)
1687 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1688 else if (WidthY > WidthX)
1689 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001690
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001691 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1692 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1693 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1694 }
1695
1696 // (d)sll SllX, X, 1
1697 // (d)srl SrlX, SllX, 1
1698 // (d)srl SrlY, Y, width(Y)-1
1699 // (d)sll SllY, SrlX, width(Y)-1
1700 // or Or, SrlX, SllY
1701 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1702 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1703 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1704 DAG.getConstant(WidthY - 1, MVT::i32));
1705
1706 if (WidthX > WidthY)
1707 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1708 else if (WidthY > WidthX)
1709 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1710
1711 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1712 DAG.getConstant(WidthX - 1, MVT::i32));
1713 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1714 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001715}
1716
Akira Hatanaka82099682011-12-19 19:52:25 +00001717SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001718MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001719 if (Subtarget->hasMips64())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001720 return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001721
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001722 return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001723}
1724
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001725static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001726 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001727 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001728
1729 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1730 // to i32.
1731 SDValue X = (Op.getValueType() == MVT::f32) ?
1732 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1733 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1734 Const1);
1735
1736 // Clear MSB.
1737 if (HasR2)
1738 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
1739 DAG.getRegister(Mips::ZERO, MVT::i32),
1740 DAG.getConstant(31, MVT::i32), Const1, X);
1741 else {
1742 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1743 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1744 }
1745
1746 if (Op.getValueType() == MVT::f32)
1747 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
1748
1749 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1750 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1751 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1752}
1753
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001754static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001755 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001756 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001757
1758 // Bitcast to integer node.
1759 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
1760
1761 // Clear MSB.
1762 if (HasR2)
1763 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
1764 DAG.getRegister(Mips::ZERO_64, MVT::i64),
1765 DAG.getConstant(63, MVT::i32), Const1, X);
1766 else {
1767 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
1768 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
1769 }
1770
1771 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
1772}
1773
1774SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001775MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001776 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001777 return lowerFABS64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001778
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001779 return lowerFABS32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001780}
1781
Akira Hatanaka2e591472011-06-02 00:24:44 +00001782SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001783lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001784 // check the depth
1785 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001786 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001787
1788 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1789 MFI->setFrameAddressIsTaken(true);
1790 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001791 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001792 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001793 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001794 return FrameAddr;
1795}
1796
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001797SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001798 SelectionDAG &DAG) const {
1799 // check the depth
1800 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1801 "Return address can be determined only for current frame.");
1802
1803 MachineFunction &MF = DAG.getMachineFunction();
1804 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001805 MVT VT = Op.getSimpleValueType();
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001806 unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
1807 MFI->setReturnAddressIsTaken(true);
1808
1809 // Return RA, which contains the return address. Mark it an implicit live-in.
1810 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001811 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001812}
1813
Akira Hatanaka544cc212013-01-30 00:26:49 +00001814// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1815// generated from __builtin_eh_return (offset, handler)
1816// The effect of this is to adjust the stack pointer by "offset"
1817// and then branch to "handler".
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001818SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanaka544cc212013-01-30 00:26:49 +00001819 const {
1820 MachineFunction &MF = DAG.getMachineFunction();
1821 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1822
1823 MipsFI->setCallsEhReturn();
1824 SDValue Chain = Op.getOperand(0);
1825 SDValue Offset = Op.getOperand(1);
1826 SDValue Handler = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001827 SDLoc DL(Op);
Akira Hatanaka544cc212013-01-30 00:26:49 +00001828 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
1829
1830 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1831 // EH_RETURN nodes, so that instructions are emitted back-to-back.
1832 unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1;
1833 unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0;
1834 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1835 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1836 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1837 DAG.getRegister(OffsetReg, Ty),
1838 DAG.getRegister(AddrReg, getPointerTy()),
1839 Chain.getValue(1));
1840}
1841
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001842SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001843 SelectionDAG &DAG) const {
Eli Friedman14648462011-07-27 22:21:52 +00001844 // FIXME: Need pseudo-fence for 'singlethread' fences
1845 // FIXME: Set SType for weaker fences where supported/appropriate.
1846 unsigned SType = 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001847 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001848 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00001849 DAG.getConstant(SType, MVT::i32));
1850}
1851
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001852SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001853 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001854 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001855 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1856 SDValue Shamt = Op.getOperand(2);
1857
1858 // if shamt < 32:
1859 // lo = (shl lo, shamt)
1860 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
1861 // else:
1862 // lo = 0
1863 // hi = (shl lo, shamt[4:0])
1864 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1865 DAG.getConstant(-1, MVT::i32));
1866 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
1867 DAG.getConstant(1, MVT::i32));
1868 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
1869 Not);
1870 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
1871 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1872 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
1873 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1874 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka864f6602012-06-14 21:10:56 +00001875 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1876 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001877 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
1878
1879 SDValue Ops[2] = {Lo, Hi};
1880 return DAG.getMergeValues(Ops, 2, DL);
1881}
1882
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001883SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001884 bool IsSRA) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001885 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001886 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1887 SDValue Shamt = Op.getOperand(2);
1888
1889 // if shamt < 32:
1890 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
1891 // if isSRA:
1892 // hi = (sra hi, shamt)
1893 // else:
1894 // hi = (srl hi, shamt)
1895 // else:
1896 // if isSRA:
1897 // lo = (sra hi, shamt[4:0])
1898 // hi = (sra hi, 31)
1899 // else:
1900 // lo = (srl hi, shamt[4:0])
1901 // hi = 0
1902 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1903 DAG.getConstant(-1, MVT::i32));
1904 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
1905 DAG.getConstant(1, MVT::i32));
1906 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
1907 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
1908 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1909 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
1910 Hi, Shamt);
1911 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1912 DAG.getConstant(0x20, MVT::i32));
1913 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
1914 DAG.getConstant(31, MVT::i32));
1915 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
1916 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1917 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
1918 ShiftRightHi);
1919
1920 SDValue Ops[2] = {Lo, Hi};
1921 return DAG.getMergeValues(Ops, 2, DL);
1922}
1923
Akira Hatanakafee62c12013-04-11 19:07:14 +00001924static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001925 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001926 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001927 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001928 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001929 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001930 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1931
1932 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001933 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001934 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001935
1936 SDValue Ops[] = { Chain, Ptr, Src };
1937 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
1938 LD->getMemOperand());
1939}
1940
1941// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001942SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001943 LoadSDNode *LD = cast<LoadSDNode>(Op);
1944 EVT MemVT = LD->getMemoryVT();
1945
1946 // Return if load is aligned or if MemVT is neither i32 nor i64.
1947 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
1948 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
1949 return SDValue();
1950
1951 bool IsLittle = Subtarget->isLittle();
1952 EVT VT = Op.getValueType();
1953 ISD::LoadExtType ExtType = LD->getExtensionType();
1954 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
1955
1956 assert((VT == MVT::i32) || (VT == MVT::i64));
1957
1958 // Expand
1959 // (set dst, (i64 (load baseptr)))
1960 // to
1961 // (set tmp, (ldl (add baseptr, 7), undef))
1962 // (set dst, (ldr baseptr, tmp))
1963 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00001964 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001965 IsLittle ? 7 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00001966 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001967 IsLittle ? 0 : 7);
1968 }
1969
Akira Hatanakafee62c12013-04-11 19:07:14 +00001970 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001971 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00001972 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001973 IsLittle ? 0 : 3);
1974
1975 // Expand
1976 // (set dst, (i32 (load baseptr))) or
1977 // (set dst, (i64 (sextload baseptr))) or
1978 // (set dst, (i64 (extload baseptr)))
1979 // to
1980 // (set tmp, (lwl (add baseptr, 3), undef))
1981 // (set dst, (lwr baseptr, tmp))
1982 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
1983 (ExtType == ISD::EXTLOAD))
1984 return LWR;
1985
1986 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
1987
1988 // Expand
1989 // (set dst, (i64 (zextload baseptr)))
1990 // to
1991 // (set tmp0, (lwl (add baseptr, 3), undef))
1992 // (set tmp1, (lwr baseptr, tmp0))
1993 // (set tmp2, (shl tmp1, 32))
1994 // (set dst, (srl tmp2, 32))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001995 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001996 SDValue Const32 = DAG.getConstant(32, MVT::i32);
1997 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka94ccee22012-06-04 17:46:29 +00001998 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
1999 SDValue Ops[] = { SRL, LWR.getValue(1) };
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002000 return DAG.getMergeValues(Ops, 2, DL);
2001}
2002
Akira Hatanakafee62c12013-04-11 19:07:14 +00002003static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002004 SDValue Chain, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002005 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2006 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002007 SDLoc DL(SD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002008 SDVTList VTList = DAG.getVTList(MVT::Other);
2009
2010 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002011 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002012 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002013
2014 SDValue Ops[] = { Chain, Value, Ptr };
2015 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2016 SD->getMemOperand());
2017}
2018
2019// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanaka63451432013-05-16 20:45:17 +00002020static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2021 bool IsLittle) {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002022 SDValue Value = SD->getValue(), Chain = SD->getChain();
2023 EVT VT = Value.getValueType();
2024
2025 // Expand
2026 // (store val, baseptr) or
2027 // (truncstore val, baseptr)
2028 // to
2029 // (swl val, (add baseptr, 3))
2030 // (swr val, baseptr)
2031 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00002032 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002033 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00002034 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002035 }
2036
2037 assert(VT == MVT::i64);
2038
2039 // Expand
2040 // (store val, baseptr)
2041 // to
2042 // (sdl val, (add baseptr, 7))
2043 // (sdr val, baseptr)
Akira Hatanakafee62c12013-04-11 19:07:14 +00002044 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2045 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002046}
2047
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002048// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2049static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2050 SDValue Val = SD->getValue();
2051
2052 if (Val.getOpcode() != ISD::FP_TO_SINT)
2053 return SDValue();
2054
2055 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002056 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002057 Val.getOperand(0));
2058
Andrew Trickac6d9be2013-05-25 02:42:55 +00002059 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002060 SD->getPointerInfo(), SD->isVolatile(),
2061 SD->isNonTemporal(), SD->getAlignment());
2062}
2063
Akira Hatanaka63451432013-05-16 20:45:17 +00002064SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2065 StoreSDNode *SD = cast<StoreSDNode>(Op);
2066 EVT MemVT = SD->getMemoryVT();
2067
2068 // Lower unaligned integer stores.
2069 if ((SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2070 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2071 return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle());
2072
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002073 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanaka63451432013-05-16 20:45:17 +00002074}
2075
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002076SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002077 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2078 || cast<ConstantSDNode>
2079 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2080 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2081 return SDValue();
2082
2083 // The pattern
2084 // (add (frameaddr 0), (frame_to_args_offset))
2085 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2086 // (add FrameObject, 0)
2087 // where FrameObject is a fixed StackObject with offset 0 which points to
2088 // the old stack pointer.
2089 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2090 EVT ValTy = Op->getValueType(0);
2091 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2092 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002093 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002094 DAG.getConstant(0, ValTy));
2095}
2096
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002097SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2098 SelectionDAG &DAG) const {
2099 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002100 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002101 Op.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002102 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002103}
2104
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002105//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002106// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002107//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002108
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002109//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002110// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002111// Mips O32 ABI rules:
2112// ---
2113// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002114// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002115// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002116// f64 - Only passed in two aliased f32 registers if no int reg has been used
2117// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002118// not used, it must be shadowed. If only A3 is avaiable, shadow it and
2119// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002120//
2121// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002122//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002123
Duncan Sands1e96bab2010-11-04 10:49:57 +00002124static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00002125 MVT LocVT, CCValAssign::LocInfo LocInfo,
Akira Hatanakaad341d42013-08-20 23:38:40 +00002126 ISD::ArgFlagsTy ArgFlags, CCState &State,
2127 const uint16_t *F64Regs) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002128
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002129 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002130
Craig Topperc5eaae42012-03-11 07:57:25 +00002131 static const uint16_t IntRegs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002132 Mips::A0, Mips::A1, Mips::A2, Mips::A3
2133 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002134 static const uint16_t F32Regs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002135 Mips::F12, Mips::F14
2136 };
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002137
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002138 // Do not process byval args here.
2139 if (ArgFlags.isByVal())
2140 return true;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002141
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002142 // Promote i8 and i16
2143 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2144 LocVT = MVT::i32;
2145 if (ArgFlags.isSExt())
2146 LocInfo = CCValAssign::SExt;
2147 else if (ArgFlags.isZExt())
2148 LocInfo = CCValAssign::ZExt;
2149 else
2150 LocInfo = CCValAssign::AExt;
2151 }
2152
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002153 unsigned Reg;
2154
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002155 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2156 // is true: function is vararg, argument is 3rd or higher, there is previous
2157 // argument which is not f32 or f64.
2158 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2159 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002160 unsigned OrigAlign = ArgFlags.getOrigAlign();
2161 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002162
2163 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002164 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002165 // If this is the first part of an i64 arg,
2166 // the allocated register must be either A0 or A2.
2167 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2168 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002169 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002170 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2171 // Allocate int register and shadow next int register. If first
2172 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002173 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2174 if (Reg == Mips::A1 || Reg == Mips::A3)
2175 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2176 State.AllocateReg(IntRegs, IntRegsSize);
2177 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002178 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2179 // we are guaranteed to find an available float register
2180 if (ValVT == MVT::f32) {
2181 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2182 // Shadow int register
2183 State.AllocateReg(IntRegs, IntRegsSize);
2184 } else {
2185 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2186 // Shadow int registers
2187 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2188 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2189 State.AllocateReg(IntRegs, IntRegsSize);
2190 State.AllocateReg(IntRegs, IntRegsSize);
2191 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002192 } else
2193 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002194
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002195 if (!Reg) {
2196 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2197 OrigAlign);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002198 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002199 } else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002200 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002201
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002202 return false;
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002203}
2204
Akira Hatanakaad341d42013-08-20 23:38:40 +00002205static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2206 MVT LocVT, CCValAssign::LocInfo LocInfo,
2207 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2208 static const uint16_t F64Regs[] = { Mips::D6, Mips::D7 };
2209
2210 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2211}
2212
2213static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2214 MVT LocVT, CCValAssign::LocInfo LocInfo,
2215 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2216 static const uint16_t F64Regs[] = { Mips::D12_64, Mips::D12_64 };
2217
2218 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2219}
2220
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002221#include "MipsGenCallingConv.inc"
2222
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002223//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002224// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002225//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002226
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002227static const unsigned O32IntRegsSize = 4;
2228
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002229// Return next O32 integer argument register.
2230static unsigned getNextIntArgReg(unsigned Reg) {
2231 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2232 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2233}
2234
Akira Hatanaka7d712092012-10-30 19:23:25 +00002235SDValue
2236MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002237 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka7d712092012-10-30 19:23:25 +00002238 bool IsTailCall, SelectionDAG &DAG) const {
2239 if (!IsTailCall) {
2240 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2241 DAG.getIntPtrConstant(Offset));
2242 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2243 false, 0);
2244 }
2245
2246 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2247 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2248 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2249 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2250 /*isVolatile=*/ true, false, 0);
2251}
2252
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002253void MipsTargetLowering::
2254getOpndList(SmallVectorImpl<SDValue> &Ops,
2255 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2256 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2257 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
2258 // Insert node "GP copy globalreg" before call to function.
2259 //
2260 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2261 // in PIC mode) allow symbols to be resolved via lazy binding.
2262 // The lazy binding stub requires GP to point to the GOT.
2263 if (IsPICCall && !InternalLinkage) {
2264 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
2265 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2266 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2267 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002268
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002269 // Build a sequence of copy-to-reg nodes chained together with token
2270 // chain and flag operands which copy the outgoing args into registers.
2271 // The InFlag in necessary since all emitted instructions must be
2272 // stuck together.
2273 SDValue InFlag;
Reed Kotler8453b3f2013-01-24 04:24:02 +00002274
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002275 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2276 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2277 RegsToPass[i].second, InFlag);
2278 InFlag = Chain.getValue(1);
2279 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002280
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002281 // Add argument registers to the end of the list so that they are
2282 // known live into the call.
2283 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2284 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2285 RegsToPass[i].second.getValueType()));
Reed Kotler8453b3f2013-01-24 04:24:02 +00002286
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002287 // Add a register mask operand representing the call-preserved registers.
2288 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2289 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2290 assert(Mask && "Missing call preserved mask for calling convention");
Reed Kotler46090912013-05-10 22:25:39 +00002291 if (Subtarget->inMips16HardFloat()) {
2292 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2293 llvm::StringRef Sym = G->getGlobal()->getName();
2294 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
2295 if (F->hasFnAttribute("__Mips16RetHelper")) {
2296 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2297 }
2298 }
2299 }
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002300 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2301
2302 if (InFlag.getNode())
2303 Ops.push_back(InFlag);
Reed Kotler8453b3f2013-01-24 04:24:02 +00002304}
2305
Dan Gohman98ca4f22009-08-05 01:29:28 +00002306/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002307/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002308SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002309MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002310 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002311 SelectionDAG &DAG = CLI.DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002312 SDLoc DL = CLI.DL;
Craig Toppera0ec3f92013-07-14 04:42:23 +00002313 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2314 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2315 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002316 SDValue Chain = CLI.Chain;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002317 SDValue Callee = CLI.Callee;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002318 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002319 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002320 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002321
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002322 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002323 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002324 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002325 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002326
2327 // Analyze operands of the call, assigning locations to each operand.
2328 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002329 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002330 getTargetMachine(), ArgLocs, *DAG.getContext());
Reed Kotler46090912013-05-10 22:25:39 +00002331 MipsCC::SpecialCallingConvType SpecialCallingConv =
2332 getSpecialCallingConv(Callee);
Akira Hatanakaad341d42013-08-20 23:38:40 +00002333 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo,
2334 SpecialCallingConv);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002335
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002336 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002337 Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002338 Callee.getNode(), CLI.Args);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002339
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002340 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002341 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002342
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002343 // Check if it's really possible to do a tail call.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002344 if (IsTailCall)
2345 IsTailCall =
2346 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002347 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002348
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002349 if (IsTailCall)
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002350 ++NumTailCalls;
2351
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002352 // Chain is the output chain of the last Load/Store or CopyToReg node.
2353 // ByValChain is the output chain of the last Memcpy node created for copying
2354 // byval arguments to the stack.
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002355 unsigned StackAlignment = TFL->getStackAlignment();
2356 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002357 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002358
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002359 if (!IsTailCall)
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002360 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002361
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002362 SDValue StackPtr = DAG.getCopyFromReg(Chain, DL,
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002363 IsN64 ? Mips::SP_64 : Mips::SP,
2364 getPointerTy());
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002365
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002366 // With EABI is it possible to have 16 args on registers.
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002367 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman475871a2008-07-27 21:46:04 +00002368 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002369 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002370
2371 // Walk the register/memloc assignments, inserting copies/loads.
2372 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002373 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002374 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002375 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002376 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2377
2378 // ByVal Arg.
2379 if (Flags.isByVal()) {
2380 assert(Flags.getByValSize() &&
2381 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002382 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002383 assert(!IsTailCall &&
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002384 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002385 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002386 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
2387 ++ByValArg;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002388 continue;
2389 }
Jia Liubb481f82012-02-28 07:46:26 +00002390
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002391 // Promote the value if needed.
2392 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002393 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002394 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002395 if (VA.isRegLoc()) {
2396 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002397 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2398 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002399 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002400 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002401 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002402 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002403 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002404 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002405 if (!Subtarget->isLittle())
2406 std::swap(Lo, Hi);
Jia Liubb481f82012-02-28 07:46:26 +00002407 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002408 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2409 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2410 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002411 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002412 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002413 }
2414 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002415 case CCValAssign::SExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002416 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002417 break;
2418 case CCValAssign::ZExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002419 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002420 break;
2421 case CCValAssign::AExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002422 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002423 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002424 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002425
2426 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002427 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002428 if (VA.isRegLoc()) {
2429 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002430 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002431 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002432
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002433 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002434 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002435
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002436 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002437 // parameter value to a stack Location
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002438 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002439 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002440 }
2441
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002442 // Transform all store nodes into one single node because all store
2443 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002444 if (!MemOpChains.empty())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002445 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002446 &MemOpChains[0], MemOpChains.size());
2447
Bill Wendling056292f2008-09-16 21:48:12 +00002448 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002449 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2450 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002451 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakaed185da2012-12-13 03:17:29 +00002452 bool GlobalOrExternal = false, InternalLinkage = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002453 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002454
2455 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002456 if (IsPICCall) {
Akira Hatanakaed185da2012-12-13 03:17:29 +00002457 InternalLinkage = G->getGlobal()->hasInternalLinkage();
2458
2459 if (InternalLinkage)
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002460 Callee = getAddrLocal(Callee, DAG, HasMips64);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002461 else if (LargeGOT)
2462 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
2463 MipsII::MO_CALL_LO16);
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002464 else
2465 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
2466 } else
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002467 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002468 MipsII::MO_NO_FLAG);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002469 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002470 }
2471 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002472 if (!IsN64 && !IsPIC) // !N64 && static
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002473 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2474 MipsII::MO_NO_FLAG);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002475 else if (LargeGOT)
2476 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
2477 MipsII::MO_CALL_LO16);
Akira Hatanaka60689322013-02-22 21:10:03 +00002478 else // N64 || PIC
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002479 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
2480
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002481 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002482 }
2483
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002484 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002485 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002486
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002487 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2488 CLI, Callee, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002489
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002490 if (IsTailCall)
2491 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, &Ops[0], Ops.size());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002492
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002493 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, &Ops[0], Ops.size());
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002494 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002495
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002496 // Create the CALLSEQ_END node.
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002497 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002498 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002499 InFlag = Chain.getValue(1);
2500
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002501 // Handle result values, copying them out of physregs into vregs that we
2502 // return.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002503 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
2504 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002505}
2506
Dan Gohman98ca4f22009-08-05 01:29:28 +00002507/// LowerCallResult - Lower the result values of a call into the
2508/// appropriate copies out of appropriate physical registers.
2509SDValue
2510MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002511 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002512 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002513 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002514 SmallVectorImpl<SDValue> &InVals,
2515 const SDNode *CallNode,
2516 const Type *RetTy) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002517 // Assign locations to each value returned by this call.
2518 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002519 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka864f6602012-06-14 21:10:56 +00002520 getTargetMachine(), RVLocs, *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002521 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002522
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002523 MipsCCInfo.analyzeCallResult(Ins, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002524 CallNode, RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002525
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002526 // Copy all of the result registers out of their specified physreg.
2527 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002528 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002529 RVLocs[i].getLocVT(), InFlag);
2530 Chain = Val.getValue(1);
2531 InFlag = Val.getValue(2);
2532
2533 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002534 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002535
2536 InVals.push_back(Val);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002537 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002538
Dan Gohman98ca4f22009-08-05 01:29:28 +00002539 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002540}
2541
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002542//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002543// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002544//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002545/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002546/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002547SDValue
2548MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002549 CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002550 bool IsVarArg,
Akira Hatanaka82099682011-12-19 19:52:25 +00002551 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002552 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002553 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002554 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002555 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002556 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002557 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002558
Dan Gohman1e93df62010-04-17 14:41:14 +00002559 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002560
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002561 // Used with vargs to acumulate store chains.
2562 std::vector<SDValue> OutChains;
2563
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002564 // Assign locations to all of the incoming arguments.
2565 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002566 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002567 getTargetMachine(), ArgLocs, *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002568 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002569 Function::const_arg_iterator FuncArg =
2570 DAG.getMachineFunction().getFunction()->arg_begin();
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002571 bool UseSoftFloat = Subtarget->mipsSEUsesSoftFloat();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002572
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002573 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
Akira Hatanakab33b34a2012-10-30 19:37:25 +00002574 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2575 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002576
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002577 unsigned CurArgIdx = 0;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002578 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002579
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002580 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002581 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002582 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2583 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002584 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002585 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2586 bool IsRegLoc = VA.isRegLoc();
2587
2588 if (Flags.isByVal()) {
2589 assert(Flags.getByValSize() &&
2590 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002591 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002592 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002593 MipsCCInfo, *ByValArg);
2594 ++ByValArg;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002595 continue;
2596 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002597
2598 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002599 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002600 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002601 unsigned ArgReg = VA.getLocReg();
Craig Topper44d23822012-02-22 05:59:10 +00002602 const TargetRegisterClass *RC;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002603
Owen Anderson825b72b2009-08-11 20:47:22 +00002604 if (RegVT == MVT::i32)
Reed Kotlerbacbf1c2012-12-20 06:06:35 +00002605 RC = Subtarget->inMips16Mode()? &Mips::CPU16RegsRegClass :
Akira Hatanaka18587862013-08-06 23:08:38 +00002606 &Mips::GPR32RegClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002607 else if (RegVT == MVT::i64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002608 RC = &Mips::GPR64RegClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002609 else if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002610 RC = &Mips::FGR32RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002611 else if (RegVT == MVT::f64)
Akira Hatanakaad341d42013-08-20 23:38:40 +00002612 RC = Subtarget->isFP64bit() ? &Mips::FGR64RegClass :
2613 &Mips::AFGR64RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002614 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002615 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002616
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002617 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002618 // physical registers into virtual ones
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002619 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2620 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002621
2622 // If this is an 8 or 16-bit value, it has been passed promoted
2623 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002624 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002625 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002626 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002627 if (VA.getLocInfo() == CCValAssign::SExt)
2628 Opcode = ISD::AssertSext;
2629 else if (VA.getLocInfo() == CCValAssign::ZExt)
2630 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002631 if (Opcode)
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002632 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002633 DAG.getValueType(ValVT));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002634 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002635 }
2636
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002637 // Handle floating point arguments passed in integer registers and
2638 // long double arguments passed in floating point registers.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002639 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002640 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2641 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002642 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002643 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002644 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002645 getNextIntArgReg(ArgReg), RC);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002646 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002647 if (!Subtarget->isLittle())
2648 std::swap(ArgValue, ArgValue2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002649 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002650 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002651 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002652
Dan Gohman98ca4f22009-08-05 01:29:28 +00002653 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002654 } else { // VA.isRegLoc()
2655
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002656 // sanity check
2657 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002658
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002659 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002660 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002661 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002662
2663 // Create load nodes to retrieve arguments from the stack
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002664 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002665 InVals.push_back(DAG.getLoad(ValVT, DL, Chain, FIN,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002666 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002667 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002668 }
2669 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002670
2671 // The mips ABIs for returning structs by value requires that we copy
2672 // the sret argument into $v0 for the return. Save the argument into
2673 // a virtual register so that we can access it from the return points.
2674 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2675 unsigned Reg = MipsFI->getSRetReturnReg();
2676 if (!Reg) {
Akira Hatanaka30580ce2012-10-19 22:11:40 +00002677 Reg = MF.getRegInfo().
2678 createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002679 MipsFI->setSRetReturnReg(Reg);
2680 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002681 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);
2682 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002683 }
2684
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002685 if (IsVarArg)
2686 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002687
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002688 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002689 // the size of Ins and InVals. This only happens when on varg functions
2690 if (!OutChains.empty()) {
2691 OutChains.push_back(Chain);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002692 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002693 &OutChains[0], OutChains.size());
2694 }
2695
Dan Gohman98ca4f22009-08-05 01:29:28 +00002696 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002697}
2698
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002699//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002700// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002701//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002702
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002703bool
2704MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002705 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002706 const SmallVectorImpl<ISD::OutputArg> &Outs,
2707 LLVMContext &Context) const {
2708 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002709 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002710 RVLocs, Context);
2711 return CCInfo.CheckReturn(Outs, RetCC_Mips);
2712}
2713
Dan Gohman98ca4f22009-08-05 01:29:28 +00002714SDValue
2715MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002716 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002717 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002718 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002719 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002720 // CCValAssign - represent the assignment of
2721 // the return value to a location
2722 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002723 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002724
2725 // CCState - Info about the registers and stack slot.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002726 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002727 *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002728 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002729
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002730 // Analyze return values.
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002731 MipsCCInfo.analyzeReturn(Outs, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002732 MF.getFunction()->getReturnType());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002733
Dan Gohman475871a2008-07-27 21:46:04 +00002734 SDValue Flag;
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002735 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002736
2737 // Copy the result values into the output registers.
2738 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002739 SDValue Val = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002740 CCValAssign &VA = RVLocs[i];
2741 assert(VA.isRegLoc() && "Can only return in registers!");
2742
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002743 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002744 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002745
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002746 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002747
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002748 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002749 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002750 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002751 }
2752
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002753 // The mips ABIs for returning structs by value requires that we copy
2754 // the sret argument into $v0 for the return. We saved the argument into
2755 // a virtual register in the entry block, so now we copy the value out
2756 // and into $v0.
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002757 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002758 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2759 unsigned Reg = MipsFI->getSRetReturnReg();
2760
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002761 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002762 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002763 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Akira Hatanaka2ef5bd32012-10-24 02:10:54 +00002764 unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002765
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002766 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002767 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002768 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002769 }
2770
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002771 RetOps[0] = Chain; // Update chain.
Akira Hatanaka182ef6f2012-07-10 00:19:06 +00002772
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002773 // Add the flag if we have it.
2774 if (Flag.getNode())
2775 RetOps.push_back(Flag);
2776
2777 // Return on Mips is always a "jr $ra"
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002778 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, &RetOps[0], RetOps.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002779}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002780
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002781//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002782// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002783//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002784
2785/// getConstraintType - Given a constraint letter, return the type of
2786/// constraint it is for this target.
2787MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002788getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002789{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002790 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002791 // GCC config/mips/constraints.md
2792 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002793 // 'd' : An address register. Equivalent to r
2794 // unless generating MIPS16 code.
2795 // 'y' : Equivalent to r; retained for
2796 // backwards compatibility.
Eric Christopher1d5a3922012-05-07 06:25:10 +00002797 // 'c' : A register suitable for use in an indirect
2798 // jump. This will always be $25 for -mabicalls.
Eric Christopheraf97f732012-05-07 06:25:19 +00002799 // 'l' : The lo register. 1 word storage.
2800 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002801 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002802 switch (Constraint[0]) {
2803 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002804 case 'd':
2805 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002806 case 'f':
Eric Christopher1d5a3922012-05-07 06:25:10 +00002807 case 'c':
Eric Christopher4adbefe2012-05-07 06:25:15 +00002808 case 'l':
Eric Christopheraf97f732012-05-07 06:25:19 +00002809 case 'x':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002810 return C_RegisterClass;
Jack Carter0b9675d2013-03-04 21:33:15 +00002811 case 'R':
2812 return C_Memory;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002813 }
2814 }
2815 return TargetLowering::getConstraintType(Constraint);
2816}
2817
John Thompson44ab89e2010-10-29 17:29:13 +00002818/// Examine constraint type and operand type and determine a weight value.
2819/// This object must already have been set up with the operand type
2820/// and the current alternative constraint selected.
2821TargetLowering::ConstraintWeight
2822MipsTargetLowering::getSingleConstraintMatchWeight(
2823 AsmOperandInfo &info, const char *constraint) const {
2824 ConstraintWeight weight = CW_Invalid;
2825 Value *CallOperandVal = info.CallOperandVal;
2826 // If we don't have a value, we can't do a match,
2827 // but allow it at the lowest weight.
2828 if (CallOperandVal == NULL)
2829 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002830 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002831 // Look at the constraint type.
2832 switch (*constraint) {
2833 default:
2834 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2835 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002836 case 'd':
2837 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002838 if (type->isIntegerTy())
2839 weight = CW_Register;
2840 break;
2841 case 'f':
2842 if (type->isFloatTy())
2843 weight = CW_Register;
2844 break;
Eric Christopher1d5a3922012-05-07 06:25:10 +00002845 case 'c': // $25 for indirect jumps
Eric Christopher4adbefe2012-05-07 06:25:15 +00002846 case 'l': // lo register
Eric Christopheraf97f732012-05-07 06:25:19 +00002847 case 'x': // hilo register pair
Eric Christopher1d5a3922012-05-07 06:25:10 +00002848 if (type->isIntegerTy())
2849 weight = CW_SpecificReg;
2850 break;
Eric Christopher50ab0392012-05-07 03:13:32 +00002851 case 'I': // signed 16 bit immediate
Eric Christophere5076d42012-05-07 03:13:42 +00002852 case 'J': // integer zero
Eric Christopherf49f8462012-05-07 05:46:29 +00002853 case 'K': // unsigned 16 bit immediate
Eric Christopher5ac47bb2012-05-07 05:46:37 +00002854 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christopher60cfc792012-05-07 05:46:43 +00002855 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher1ce20342012-05-07 05:46:48 +00002856 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopher54412a72012-05-07 06:25:02 +00002857 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher50ab0392012-05-07 03:13:32 +00002858 if (isa<ConstantInt>(CallOperandVal))
2859 weight = CW_Constant;
2860 break;
Jack Carter0b9675d2013-03-04 21:33:15 +00002861 case 'R':
2862 weight = CW_Memory;
2863 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002864 }
2865 return weight;
2866}
2867
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002868/// This is a helper function to parse a physical register string and split it
2869/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
2870/// that is returned indicates whether parsing was successful. The second flag
2871/// is true if the numeric part exists.
2872static std::pair<bool, bool>
2873parsePhysicalReg(const StringRef &C, std::string &Prefix,
2874 unsigned long long &Reg) {
2875 if (C.front() != '{' || C.back() != '}')
2876 return std::make_pair(false, false);
2877
2878 // Search for the first numeric character.
2879 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
2880 I = std::find_if(B, E, std::ptr_fun(isdigit));
2881
2882 Prefix.assign(B, I - B);
2883
2884 // The second flag is set to false if no numeric characters were found.
2885 if (I == E)
2886 return std::make_pair(true, false);
2887
2888 // Parse the numeric characters.
2889 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
2890 true);
2891}
2892
2893std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
2894parseRegForInlineAsmConstraint(const StringRef &C, MVT VT) const {
2895 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2896 const TargetRegisterClass *RC;
2897 std::string Prefix;
2898 unsigned long long Reg;
2899
2900 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
2901
2902 if (!R.first)
2903 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2904
2905 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
2906 // No numeric characters follow "hi" or "lo".
2907 if (R.second)
2908 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2909
2910 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002911 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002912 return std::make_pair(*(RC->begin()), RC);
2913 }
2914
2915 if (!R.second)
2916 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2917
2918 if (Prefix == "$f") { // Parse $f0-$f31.
2919 // If the size of FP registers is 64-bit or Reg is an even number, select
2920 // the 64-bit register class. Otherwise, select the 32-bit register class.
2921 if (VT == MVT::Other)
2922 VT = (Subtarget->isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
2923
2924 RC= getRegClassFor(VT);
2925
2926 if (RC == &Mips::AFGR64RegClass) {
2927 assert(Reg % 2 == 0);
2928 Reg >>= 1;
2929 }
2930 } else if (Prefix == "$fcc") { // Parse $fcc0-$fcc7.
2931 RC = TRI->getRegClass(Mips::FCCRegClassID);
2932 } else { // Parse $0-$31.
2933 assert(Prefix == "$");
2934 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
2935 }
2936
2937 assert(Reg < RC->getNumRegs());
2938 return std::make_pair(*(RC->begin() + Reg), RC);
2939}
2940
Eric Christopher38d64262011-06-29 19:33:04 +00002941/// Given a register class constraint, like 'r', if this corresponds directly
2942/// to an LLVM register class, return a register of 0 and the register class
2943/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002944std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier5b3fca52013-06-22 18:37:38 +00002945getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002946{
2947 if (Constraint.size() == 1) {
2948 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002949 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2950 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002951 case 'r':
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002952 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
2953 if (Subtarget->inMips16Mode())
2954 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka18587862013-08-06 23:08:38 +00002955 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002956 }
Jack Carter10de0252012-07-02 23:35:23 +00002957 if (VT == MVT::i64 && !HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002958 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002959 if (VT == MVT::i64 && HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002960 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002961 // This will generate an error message
2962 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002963 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002964 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002965 return std::make_pair(0U, &Mips::FGR32RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002966 if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
2967 if (Subtarget->isFP64bit())
Craig Topper420761a2012-04-20 07:30:17 +00002968 return std::make_pair(0U, &Mips::FGR64RegClass);
2969 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002970 }
Eric Christopher1d5a3922012-05-07 06:25:10 +00002971 break;
2972 case 'c': // register suitable for indirect jump
2973 if (VT == MVT::i32)
Akira Hatanaka18587862013-08-06 23:08:38 +00002974 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christopher1d5a3922012-05-07 06:25:10 +00002975 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka18587862013-08-06 23:08:38 +00002976 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher4adbefe2012-05-07 06:25:15 +00002977 case 'l': // register suitable for indirect jump
2978 if (VT == MVT::i32)
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002979 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
2980 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopheraf97f732012-05-07 06:25:19 +00002981 case 'x': // register suitable for indirect jump
2982 // Fixme: Not triggering the use of both hi and low
2983 // This will generate an error message
2984 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002985 }
2986 }
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002987
2988 std::pair<unsigned, const TargetRegisterClass *> R;
2989 R = parseRegForInlineAsmConstraint(Constraint, VT);
2990
2991 if (R.second)
2992 return R;
2993
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002994 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2995}
2996
Eric Christopher50ab0392012-05-07 03:13:32 +00002997/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
2998/// vector. If it is invalid, don't add anything to Ops.
2999void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3000 std::string &Constraint,
3001 std::vector<SDValue>&Ops,
3002 SelectionDAG &DAG) const {
3003 SDValue Result(0, 0);
3004
3005 // Only support length 1 constraints for now.
3006 if (Constraint.length() > 1) return;
3007
3008 char ConstraintLetter = Constraint[0];
3009 switch (ConstraintLetter) {
3010 default: break; // This will fall through to the generic implementation
3011 case 'I': // Signed 16 bit constant
3012 // If this fails, the parent routine will give an error
3013 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3014 EVT Type = Op.getValueType();
3015 int64_t Val = C->getSExtValue();
3016 if (isInt<16>(Val)) {
3017 Result = DAG.getTargetConstant(Val, Type);
3018 break;
3019 }
3020 }
3021 return;
Eric Christophere5076d42012-05-07 03:13:42 +00003022 case 'J': // integer zero
3023 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3024 EVT Type = Op.getValueType();
3025 int64_t Val = C->getZExtValue();
3026 if (Val == 0) {
3027 Result = DAG.getTargetConstant(0, Type);
3028 break;
3029 }
3030 }
3031 return;
Eric Christopherf49f8462012-05-07 05:46:29 +00003032 case 'K': // unsigned 16 bit immediate
3033 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3034 EVT Type = Op.getValueType();
3035 uint64_t Val = (uint64_t)C->getZExtValue();
3036 if (isUInt<16>(Val)) {
3037 Result = DAG.getTargetConstant(Val, Type);
3038 break;
3039 }
3040 }
3041 return;
Eric Christopher5ac47bb2012-05-07 05:46:37 +00003042 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3043 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3044 EVT Type = Op.getValueType();
3045 int64_t Val = C->getSExtValue();
3046 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3047 Result = DAG.getTargetConstant(Val, Type);
3048 break;
3049 }
3050 }
3051 return;
Eric Christopher60cfc792012-05-07 05:46:43 +00003052 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3053 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3054 EVT Type = Op.getValueType();
3055 int64_t Val = C->getSExtValue();
3056 if ((Val >= -65535) && (Val <= -1)) {
3057 Result = DAG.getTargetConstant(Val, Type);
3058 break;
3059 }
3060 }
3061 return;
Eric Christopher1ce20342012-05-07 05:46:48 +00003062 case 'O': // signed 15 bit immediate
3063 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3064 EVT Type = Op.getValueType();
3065 int64_t Val = C->getSExtValue();
3066 if ((isInt<15>(Val))) {
3067 Result = DAG.getTargetConstant(Val, Type);
3068 break;
3069 }
3070 }
3071 return;
Eric Christopher54412a72012-05-07 06:25:02 +00003072 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3073 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3074 EVT Type = Op.getValueType();
3075 int64_t Val = C->getSExtValue();
3076 if ((Val <= 65535) && (Val >= 1)) {
3077 Result = DAG.getTargetConstant(Val, Type);
3078 break;
3079 }
3080 }
3081 return;
Eric Christopher50ab0392012-05-07 03:13:32 +00003082 }
3083
3084 if (Result.getNode()) {
3085 Ops.push_back(Result);
3086 return;
3087 }
3088
3089 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3090}
3091
Dan Gohman6520e202008-10-18 02:06:02 +00003092bool
Akira Hatanaka94e47282012-11-17 00:25:41 +00003093MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM, Type *Ty) const {
3094 // No global is ever allowed as a base.
3095 if (AM.BaseGV)
3096 return false;
3097
3098 switch (AM.Scale) {
3099 case 0: // "r+i" or just "i", depending on HasBaseReg.
3100 break;
3101 case 1:
3102 if (!AM.HasBaseReg) // allow "r+i".
3103 break;
3104 return false; // disallow "r+r" or "r+r+i".
3105 default:
3106 return false;
3107 }
3108
3109 return true;
3110}
3111
3112bool
Dan Gohman6520e202008-10-18 02:06:02 +00003113MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3114 // The Mips target isn't yet aware of offsets.
3115 return false;
3116}
Evan Chengeb2f9692009-10-27 19:56:55 +00003117
Akira Hatanakae193b322012-06-13 19:33:32 +00003118EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003119 unsigned SrcAlign,
3120 bool IsMemset, bool ZeroMemset,
Akira Hatanakae193b322012-06-13 19:33:32 +00003121 bool MemcpyStrSrc,
3122 MachineFunction &MF) const {
3123 if (Subtarget->hasMips64())
3124 return MVT::i64;
3125
3126 return MVT::i32;
3127}
3128
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003129bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3130 if (VT != MVT::f32 && VT != MVT::f64)
3131 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00003132 if (Imm.isNegZero())
3133 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00003134 return Imm.isZero();
3135}
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003136
3137unsigned MipsTargetLowering::getJumpTableEncoding() const {
3138 if (IsN64)
3139 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liubb481f82012-02-28 07:46:26 +00003140
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003141 return TargetLowering::getJumpTableEncoding();
3142}
Akira Hatanaka7887c902012-10-26 23:56:38 +00003143
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003144/// This function returns true if CallSym is a long double emulation routine.
3145static bool isF128SoftLibCall(const char *CallSym) {
3146 const char *const LibCalls[] =
3147 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3148 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3149 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3150 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3151 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3152 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3153 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3154 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3155 "truncl"};
3156
3157 const char * const *End = LibCalls + array_lengthof(LibCalls);
3158
3159 // Check that LibCalls is sorted alphabetically.
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003160 MipsTargetLowering::LTStr Comp;
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003161
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003162#ifndef NDEBUG
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003163 for (const char * const *I = LibCalls; I < End - 1; ++I)
3164 assert(Comp(*I, *(I + 1)));
3165#endif
3166
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003167 return std::binary_search(LibCalls, End, CallSym, Comp);
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003168}
3169
3170/// This function returns true if Ty is fp128 or i128 which was originally a
3171/// fp128.
3172static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3173 if (Ty->isFP128Ty())
3174 return true;
3175
3176 const ExternalSymbolSDNode *ES =
3177 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3178
3179 // If the Ty is i128 and the function being called is a long double emulation
3180 // routine, then the original type is f128.
3181 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3182}
3183
Reed Kotler46090912013-05-10 22:25:39 +00003184MipsTargetLowering::MipsCC::SpecialCallingConvType
3185 MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
3186 MipsCC::SpecialCallingConvType SpecialCallingConv =
3187 MipsCC::NoSpecialCallingConv;;
3188 if (Subtarget->inMips16HardFloat()) {
3189 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3190 llvm::StringRef Sym = G->getGlobal()->getName();
3191 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
3192 if (F->hasFnAttribute("__Mips16RetHelper")) {
3193 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3194 }
3195 }
3196 }
3197 return SpecialCallingConv;
3198}
3199
3200MipsTargetLowering::MipsCC::MipsCC(
Akira Hatanakaad341d42013-08-20 23:38:40 +00003201 CallingConv::ID CC, bool IsO32_, bool IsFP64_, CCState &Info,
Reed Kotler46090912013-05-10 22:25:39 +00003202 MipsCC::SpecialCallingConvType SpecialCallingConv_)
Akira Hatanakaad341d42013-08-20 23:38:40 +00003203 : CCInfo(Info), CallConv(CC), IsO32(IsO32_), IsFP64(IsFP64_),
Reed Kotler46090912013-05-10 22:25:39 +00003204 SpecialCallingConv(SpecialCallingConv_){
Akira Hatanaka7887c902012-10-26 23:56:38 +00003205 // Pre-allocate reserved argument area.
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003206 CCInfo.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka7887c902012-10-26 23:56:38 +00003207}
3208
Reed Kotler46090912013-05-10 22:25:39 +00003209
Akira Hatanaka7887c902012-10-26 23:56:38 +00003210void MipsTargetLowering::MipsCC::
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003211analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003212 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
3213 std::vector<ArgListEntry> &FuncArgs) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003214 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3215 "CallingConv::Fast shouldn't be used for vararg functions.");
3216
Akira Hatanaka7887c902012-10-26 23:56:38 +00003217 unsigned NumOpnds = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003218 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003219
3220 for (unsigned I = 0; I != NumOpnds; ++I) {
3221 MVT ArgVT = Args[I].VT;
3222 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3223 bool R;
3224
3225 if (ArgFlags.isByVal()) {
3226 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3227 continue;
3228 }
3229
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003230 if (IsVarArg && !Args[I].IsFixed)
Akira Hatanaka7887c902012-10-26 23:56:38 +00003231 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003232 else {
3233 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3234 IsSoftFloat);
3235 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
3236 }
Akira Hatanaka7887c902012-10-26 23:56:38 +00003237
3238 if (R) {
3239#ifndef NDEBUG
3240 dbgs() << "Call operand #" << I << " has unhandled type "
3241 << EVT(ArgVT).getEVTString();
3242#endif
3243 llvm_unreachable(0);
3244 }
3245 }
3246}
3247
3248void MipsTargetLowering::MipsCC::
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003249analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
3250 bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
Akira Hatanaka7887c902012-10-26 23:56:38 +00003251 unsigned NumArgs = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003252 llvm::CCAssignFn *FixedFn = fixedArgFn();
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003253 unsigned CurArgIdx = 0;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003254
3255 for (unsigned I = 0; I != NumArgs; ++I) {
3256 MVT ArgVT = Args[I].VT;
3257 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003258 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3259 CurArgIdx = Args[I].OrigArgIndex;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003260
3261 if (ArgFlags.isByVal()) {
3262 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3263 continue;
3264 }
3265
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003266 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), 0, IsSoftFloat);
3267
3268 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
Akira Hatanaka7887c902012-10-26 23:56:38 +00003269 continue;
3270
3271#ifndef NDEBUG
3272 dbgs() << "Formal Arg #" << I << " has unhandled type "
3273 << EVT(ArgVT).getEVTString();
3274#endif
3275 llvm_unreachable(0);
3276 }
3277}
3278
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003279template<typename Ty>
3280void MipsTargetLowering::MipsCC::
3281analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
3282 const SDNode *CallNode, const Type *RetTy) const {
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003283 CCAssignFn *Fn;
3284
3285 if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
3286 Fn = RetCC_F128Soft;
3287 else
3288 Fn = RetCC_Mips;
3289
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003290 for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
3291 MVT VT = RetVals[I].VT;
3292 ISD::ArgFlagsTy Flags = RetVals[I].Flags;
3293 MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
3294
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003295 if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003296#ifndef NDEBUG
3297 dbgs() << "Call result #" << I << " has unhandled type "
3298 << EVT(VT).getEVTString() << '\n';
3299#endif
3300 llvm_unreachable(0);
3301 }
3302 }
3303}
3304
3305void MipsTargetLowering::MipsCC::
3306analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
3307 const SDNode *CallNode, const Type *RetTy) const {
3308 analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
3309}
3310
3311void MipsTargetLowering::MipsCC::
3312analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
3313 const Type *RetTy) const {
3314 analyzeReturn(Outs, IsSoftFloat, 0, RetTy);
3315}
3316
Akira Hatanaka7887c902012-10-26 23:56:38 +00003317void
3318MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3319 MVT LocVT,
3320 CCValAssign::LocInfo LocInfo,
3321 ISD::ArgFlagsTy ArgFlags) {
3322 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3323
3324 struct ByValArgInfo ByVal;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003325 unsigned RegSize = regSize();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003326 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3327 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3328 RegSize * 2);
3329
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003330 if (useRegsForByval())
Akira Hatanaka7887c902012-10-26 23:56:38 +00003331 allocateRegs(ByVal, ByValSize, Align);
3332
3333 // Allocate space on caller's stack.
3334 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3335 Align);
3336 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3337 LocInfo));
3338 ByValArgs.push_back(ByVal);
3339}
3340
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003341unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
3342 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
3343}
3344
3345unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
3346 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
3347}
3348
3349const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const {
3350 return IsO32 ? O32IntRegs : Mips64IntRegs;
3351}
3352
3353llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3354 if (CallConv == CallingConv::Fast)
3355 return CC_Mips_FastCC;
3356
Reed Kotler46090912013-05-10 22:25:39 +00003357 if (SpecialCallingConv == Mips16RetHelperConv)
3358 return CC_Mips16RetHelper;
Akira Hatanakaad341d42013-08-20 23:38:40 +00003359 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003360}
3361
3362llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
Akira Hatanakaad341d42013-08-20 23:38:40 +00003363 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN_VarArg;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003364}
3365
3366const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const {
3367 return IsO32 ? O32IntRegs : Mips64DPRegs;
3368}
3369
Akira Hatanaka7887c902012-10-26 23:56:38 +00003370void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3371 unsigned ByValSize,
3372 unsigned Align) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003373 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
3374 const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003375 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3376 "Byval argument's size and alignment should be a multiple of"
3377 "RegSize.");
3378
3379 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3380
3381 // If Align > RegSize, the first arg register must be even.
3382 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3383 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3384 ++ByVal.FirstIdx;
3385 }
3386
3387 // Mark the registers allocated.
3388 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3389 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3390 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3391}
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003392
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003393MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3394 const SDNode *CallNode,
3395 bool IsSoftFloat) const {
3396 if (IsSoftFloat || IsO32)
3397 return VT;
3398
3399 // Check if the original type was fp128.
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003400 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003401 assert(VT == MVT::i64);
3402 return MVT::f64;
3403 }
3404
3405 return VT;
3406}
3407
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003408void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003409copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003410 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3411 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3412 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3413 MachineFunction &MF = DAG.getMachineFunction();
3414 MachineFrameInfo *MFI = MF.getFrameInfo();
3415 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
3416 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3417 int FrameObjOffset;
3418
3419 if (RegAreaSize)
3420 FrameObjOffset = (int)CC.reservedArgArea() -
3421 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
3422 else
3423 FrameObjOffset = ByVal.Address;
3424
3425 // Create frame object.
3426 EVT PtrTy = getPointerTy();
3427 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3428 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3429 InVals.push_back(FIN);
3430
3431 if (!ByVal.NumRegs)
3432 return;
3433
3434 // Copy arg registers.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003435 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003436 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3437
3438 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3439 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003440 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003441 unsigned Offset = I * CC.regSize();
3442 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3443 DAG.getConstant(Offset, PtrTy));
3444 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3445 StorePtr, MachinePointerInfo(FuncArg, Offset),
3446 false, false, 0);
3447 OutChains.push_back(Store);
3448 }
3449}
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003450
3451// Copy byVal arg to registers and stack.
3452void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003453passByValArg(SDValue Chain, SDLoc DL,
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00003454 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Craig Toppera0ec3f92013-07-14 04:42:23 +00003455 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003456 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3457 const MipsCC &CC, const ByValArgInfo &ByVal,
3458 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
3459 unsigned ByValSize = Flags.getByValSize();
3460 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
3461 unsigned RegSize = CC.regSize();
3462 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
3463 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
3464
3465 if (ByVal.NumRegs) {
3466 const uint16_t *ArgRegs = CC.intArgRegs();
3467 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
3468 unsigned I = 0;
3469
3470 // Copy words to registers.
3471 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
3472 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3473 DAG.getConstant(Offset, PtrTy));
3474 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3475 MachinePointerInfo(), false, false, false,
3476 Alignment);
3477 MemOpChains.push_back(LoadVal.getValue(1));
3478 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3479 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3480 }
3481
3482 // Return if the struct has been fully copied.
3483 if (ByValSize == Offset)
3484 return;
3485
3486 // Copy the remainder of the byval argument with sub-word loads and shifts.
3487 if (LeftoverBytes) {
3488 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
3489 "Size of the remainder should be smaller than RegSize.");
3490 SDValue Val;
3491
3492 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
3493 Offset < ByValSize; LoadSize /= 2) {
3494 unsigned RemSize = ByValSize - Offset;
3495
3496 if (RemSize < LoadSize)
3497 continue;
3498
3499 // Load subword.
3500 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3501 DAG.getConstant(Offset, PtrTy));
3502 SDValue LoadVal =
3503 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
3504 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
3505 false, false, Alignment);
3506 MemOpChains.push_back(LoadVal.getValue(1));
3507
3508 // Shift the loaded value.
3509 unsigned Shamt;
3510
3511 if (isLittle)
3512 Shamt = TotalSizeLoaded;
3513 else
3514 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
3515
3516 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3517 DAG.getConstant(Shamt, MVT::i32));
3518
3519 if (Val.getNode())
3520 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3521 else
3522 Val = Shift;
3523
3524 Offset += LoadSize;
3525 TotalSizeLoaded += LoadSize;
3526 Alignment = std::min(Alignment, LoadSize);
3527 }
3528
3529 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3530 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3531 return;
3532 }
3533 }
3534
3535 // Copy remainder of byval arg to it with memcpy.
3536 unsigned MemCpySize = ByValSize - Offset;
3537 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3538 DAG.getConstant(Offset, PtrTy));
3539 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3540 DAG.getIntPtrConstant(ByVal.Address));
3541 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3542 DAG.getConstant(MemCpySize, PtrTy), Alignment,
3543 /*isVolatile=*/false, /*AlwaysInline=*/false,
3544 MachinePointerInfo(0), MachinePointerInfo(0));
3545 MemOpChains.push_back(Chain);
3546}
Akira Hatanakaf0848472012-10-27 00:21:13 +00003547
3548void
3549MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3550 const MipsCC &CC, SDValue Chain,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003551 SDLoc DL, SelectionDAG &DAG) const {
Akira Hatanakaf0848472012-10-27 00:21:13 +00003552 unsigned NumRegs = CC.numIntArgRegs();
3553 const uint16_t *ArgRegs = CC.intArgRegs();
3554 const CCState &CCInfo = CC.getCCInfo();
3555 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
3556 unsigned RegSize = CC.regSize();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003557 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003558 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3559 MachineFunction &MF = DAG.getMachineFunction();
3560 MachineFrameInfo *MFI = MF.getFrameInfo();
3561 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3562
3563 // Offset of the first variable argument from stack pointer.
3564 int VaArgOffset;
3565
3566 if (NumRegs == Idx)
3567 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
3568 else
3569 VaArgOffset =
3570 (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
3571
3572 // Record the frame index of the first variable argument
3573 // which is a value necessary to VASTART.
3574 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3575 MipsFI->setVarArgsFrameIndex(FI);
3576
3577 // Copy the integer registers that have not been used for argument passing
3578 // to the argument register save area. For O32, the save area is allocated
3579 // in the caller's stack frame, while for N32/64, it is allocated in the
3580 // callee's stack frame.
3581 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003582 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003583 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3584 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3585 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3586 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3587 MachinePointerInfo(), false, false, 0);
3588 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
3589 OutChains.push_back(Store);
3590 }
3591}